1 <?xml version='
1.0'
?> <!--*-nxml-*-->
2 <!DOCTYPE refentry PUBLIC
"-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
6 This file is part of systemd.
8 Copyright 2012 Lennart Poettering
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 <refentry id=
"sd_journal_get_fd">
27 <title>sd_journal_get_fd
</title>
28 <productname>systemd
</productname>
32 <contrib>Developer
</contrib>
33 <firstname>Lennart
</firstname>
34 <surname>Poettering
</surname>
35 <email>lennart@poettering.net
</email>
41 <refentrytitle>sd_journal_get_fd
</refentrytitle>
42 <manvolnum>3</manvolnum>
46 <refname>sd_journal_get_fd
</refname>
47 <refname>sd_journal_reliable_fd
</refname>
48 <refname>sd_journal_process
</refname>
49 <refname>sd_journal_wait
</refname>
50 <refname>SD_JOURNAL_NOP
</refname>
51 <refname>SD_JOURNAL_APPEND
</refname>
52 <refname>SD_JOURNAL_INVALIDATE
</refname>
53 <refpurpose>Journal change notification
54 interface
</refpurpose>
59 <funcsynopsisinfo>#include
<systemd/sd-journal.h
></funcsynopsisinfo>
62 <funcdef>int
<function>sd_journal_get_fd
</function></funcdef>
63 <paramdef>sd_journal*
<parameter>j
</parameter></paramdef>
67 <funcdef>int
<function>sd_journal_reliable_fd
</function></funcdef>
68 <paramdef>sd_journal*
<parameter>j
</parameter></paramdef>
72 <funcdef>int
<function>sd_journal_process
</function></funcdef>
73 <paramdef>sd_journal*
<parameter>j
</parameter></paramdef>
77 <funcdef>int
<function>sd_journal_wait
</function></funcdef>
78 <paramdef>sd_journal*
<parameter>j
</parameter></paramdef>
79 <paramdef>uint64_t
<parameter>timeout_usec
</parameter></paramdef>
86 <title>Description
</title>
88 <para><function>sd_journal_get_fd()
</function> returns
89 a file descriptor that may be asynchronously polled in
90 an external event loop and is signaled readable as
91 soon as the journal changes, because new entries or
92 files were added, rotation took place, or files have
93 been deleted, and similar. The file descriptor is
95 <citerefentry><refentrytitle>poll
</refentrytitle><manvolnum>2</manvolnum></citerefentry>
96 where it will yield POLLIN on changes. The call takes
97 one argument: the journal context object. Note that
98 not all file systems are capable of generating the
99 necessary events for wakeups from this file descriptor
100 to be enirely reliable. In particular network files
101 systems do not generate suitable file change events in
102 all cases. In such a case an application should not
103 rely alone on wake-ups from this file descriptor but
104 wake up and recheck the journal in regular time
105 intervals, for example every
2s. To detect
106 cases where this is necessary, use
107 <function>sd_journal_reliable_fd()
</function>,
110 <para><function>sd_journal_reliable_fd()
</function>
111 may be used to check whether the wakeup events from
112 the file descriptor returned by
113 <function>sd_journal_get_fd
</function> are sufficient
114 to track changes to the journal. If this call returns
115 0, it is necessary to regularly recheck for journal
116 changes (suggestion: every
2s). If this call returns a
117 positive integer this is not necessary, and wakeups
118 from the file descriptor returned by
119 <function>sd_journal_get_fd()
</function> are
120 sufficient as only source for wake-ups.
</para>
122 <para>After each POLLIN wake-up
123 <function>sd_journal_process()
</function> needs to be
124 called to process events and reset the readable state
125 of the file descriptor. This call will also indicate
126 what kind of change has been detected (see below; note
127 that spurious wake-ups are possible).
</para>
129 <para>A synchronous alternative for using
130 <function>sd_journal_get_fd()
</function>,
131 <function>sd_journal_reliable_fd()
</function> and
132 <function>sd_journal_process()
</function> is
133 <function>sd_journal_wait()
</function>. It will
134 synchronously wait until the journal gets changed,
135 possibly using a
2s time-out if this is necessary (see
136 above). In either way the maximum time this call
137 sleeps may be controlled with the
138 <parameter>timeout_usec
</parameter> parameter. Pass
139 <literal>(uint64_t) -
1</literal> to wait
140 indefinitely. Internally this call simply combines
141 <function>sd_journal_get_fd()
</function>,
142 <function>sd_journal_reliable_fd()
</function>,
143 <function>poll()
</function> and
144 <function>sd_journal_process()
</function> into
150 <title>Return Value
</title>
152 <para><function>sd_journal_get_fd()
</function> returns a valid file descriptor on success or a negative errno-style error
155 <para><function>sd_journal_reliable_fd()
</function>
156 returns a positive integer if the file descriptor
157 returned by
<function>sd_journal_get_fd()
</function>
158 is sufficient as sole wake-up source for journal
159 change events. Returns
0 if it is not sufficient and
160 the journal needs to be checked manually in regular
161 time intervals for changes. Returns a negative
162 errno-style error code on failure.
</para>
164 <para><function>sd_journal_process()
</function> and
165 <function>sd_journal_wait()
</function> return one of
166 <literal>SD_JOURNAL_NOP
</literal>,
167 <literal>SD_JOURNAL_APPEND
</literal> or
168 <literal>SD_JOURNAL_INVALIDATE
</literal> on success or
169 a negative errno-style error code. If
170 <literal>SD_JOURNAL_NOP
</literal> is returned the
171 journal didn't change since the last invocation. If
172 <literal>SD_JOURNAL_APPEND
</literal> is returned new
173 entries have been appended to the end of the
174 journal. If
<literal>SD_JOURNAL_INVALIDATE
</literal>
175 journal files were added or removed (possibly due to
176 rotation). In the latter event live-view UIs should
177 probably refresh their entire display while in the
178 case of
<literal>SD_JOURNAL_APPEND
</literal> it is
179 sufficient to simply continue reading at the previous
180 end of the journal.
</para>
186 <para>The
<function>sd_journal_get_fd()
</function>,
187 <function>sd_journal_reliable_fd()
</function>,
188 <function>sd_journal_process()
</function> and
189 <function>sd_journal_wait()
</function> interfaces are
190 available as shared library, which can be compiled and
192 <literal>libsystemd-journal
</literal>
193 <citerefentry><refentrytitle>pkg-config
</refentrytitle><manvolnum>1</manvolnum></citerefentry>
198 <title>Examples
</title>
200 <para>Iterating through the journal, in a live view tracking all changes:
</para>
202 <programlisting>#include
<stdio.h
>
203 #include
<string.h
>
204 #include
<systemd/sd-journal.h
>
206 int main(int argc, char *argv[]) {
209 r = sd_journal_open(
&j, SD_JOURNAL_LOCAL_ONLY);
211 fprintf(stderr,
"Failed to open journal: %s\n", strerror(-r));
217 r = sd_journal_next(j);
219 fprintf(stderr,
"Failed to iterate to next entry: %s\n", strerror(-r));
223 /* Reached the end, let's wait for changes, and try again */
224 r = sd_journal_wait(j, (uint64_t) -
1);
226 fprintf(stderr,
"Failed to wait for changes: %s\n", strerror(-r));
231 r = sd_journal_get_data(j,
"MESSAGE",
&d,
&l);
233 fprintf(stderr,
"Failed to read message field: %s\n", strerror(-r));
236 printf(
"%.*s\n", (int) l, (const char*) d);
242 <para>Waiting with
<function>poll()
</function> (this
243 example lacks all error checking for the sake of
246 <programlisting>#include
<sys/poll.h
>
247 #include
<systemd/sd-journal.h
>
249 int wait_for_changes(sd_journal *j) {
250 struct pollfd pollfd;
251 pollfd.fd = sd_journal_get_fd(j);
252 pollfd.events = POLLIN;
253 poll(
&pollfd,
1, sd_journal_reliable_fd(j)
> 0 ? -
1 :
2000);
254 return sd_journal_process(j);
261 <title>See Also
</title>
264 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
265 <citerefentry><refentrytitle>sd-journal
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
266 <citerefentry><refentrytitle>sd_journal_open
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
267 <citerefentry><refentrytitle>sd_journal_next
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
268 <citerefentry><refentrytitle>poll
</refentrytitle><manvolnum>2</manvolnum></citerefentry>