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_get_events</refname>
48 <refname>sd_journal_get_timeout</refname>
49 <refname>sd_journal_process</refname>
50 <refname>sd_journal_wait</refname>
51 <refname>sd_journal_reliable_fd</refname>
52 <refname>SD_JOURNAL_NOP</refname>
53 <refname>SD_JOURNAL_APPEND</refname>
54 <refname>SD_JOURNAL_INVALIDATE</refname>
55 <refpurpose>Journal change notification
56 interface</refpurpose>
61 <funcsynopsisinfo>#include <systemd/sd-journal.h></funcsynopsisinfo>
64 <funcdef>int <function>sd_journal_get_fd</function></funcdef>
65 <paramdef>sd_journal* <parameter>j</parameter></paramdef>
69 <funcdef>int <function>sd_journal_get_events</function></funcdef>
70 <paramdef>sd_journal* <parameter>j</parameter></paramdef>
74 <funcdef>int <function>sd_journal_get_timeout</function></funcdef>
75 <paramdef>sd_journal* <parameter>j</parameter></paramdef>
76 <paramdef>uint64_t* <parameter>timeout_usec</parameter></paramdef>
80 <funcdef>int <function>sd_journal_process</function></funcdef>
81 <paramdef>sd_journal* <parameter>j</parameter></paramdef>
85 <funcdef>int <function>sd_journal_wait</function></funcdef>
86 <paramdef>sd_journal* <parameter>j</parameter></paramdef>
87 <paramdef>uint64_t <parameter>timeout_usec</parameter></paramdef>
91 <funcdef>int <function>sd_journal_reliable_fd</function></funcdef>
92 <paramdef>sd_journal* <parameter>j</parameter></paramdef>
99 <title>Description</title>
101 <para><function>sd_journal_get_fd()</function> returns
102 a file descriptor that may be asynchronously polled in
103 an external event loop and is signaled as soon as the
104 journal changes, because new entries or files were
105 added, rotation took place, or files have been
106 deleted, and similar. The file descriptor is suitable
108 <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>. Use
109 <function>sd_journal_get_events()</function> for an
110 events mask to watch for. The call takes one argument:
111 the journal context object. Note that not all file
112 systems are capable of generating the necessary events
113 for wakeups from this file descriptor for changes to
114 be noticed immediately. In particular network files
115 systems do not generate suitable file change events in
116 all cases. Cases like this can be detected with
117 <function>sd_journal_reliable_fd()</function>,
118 below. <function>sd_journal_get_timeout()</function>
119 will ensure in these cases that wake-ups happen
120 frequently enough for changes to be noticed, although
121 with a certain latency.</para>
123 <para><function>sd_journal_get_events()</function>
124 will return the <function>poll()</function> mask to
125 wait for. This function will return a combination of
126 <literal>POLLIN</literal> and
127 <literal>POLLOUT</literal> and similar to fill into
128 the <literal>.events</literal> field of
129 <literal>struct pollfd</literal>.</para>
131 <para><function>sd_journal_get_timeout()</function>
132 will return a timeout value for usage in <function>poll()</function>. This returns a value in microseconds since the epoch of CLOCK_MONOTONIC for timing out <function>poll()</function> in <literal>timeout_usec</literal>. See
133 <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry>
135 <literal>CLOCK_MONOTONIC</literal>. If there's no
136 timeout to wait for this will fill in
137 <literal>(uint64_t) -1</literal> instead. Note that
138 <function>poll()</function> takes a relative timeout
139 in milliseconds rather than an absolute timeout in
140 microseconds. To convert the absolute 'us' timeout into
141 relative 'ms', use code like the following:</para>
143 <programlisting>uint64_t t;
145 sd_journal_get_timeout(m, &t);
146 if (t == (uint64_t) -1)
151 clock_getttime(CLOCK_MONOTONIC, &ts);
152 n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
153 msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
156 <para>The code above does not do any error checking
157 for brevity's sake. The calculated <literal>msec</literal>
158 integer can be passed directly as
159 <function>poll()</function>'s timeout
162 <para>After each <function>poll()</function> wake-up
163 <function>sd_journal_process()</function> needs to be
164 called to process events. This call will also indicate
165 what kind of change has been detected (see below; note
166 that spurious wake-ups are possible).</para>
168 <para>A synchronous alternative for using
169 <function>sd_journal_get_fd()</function>,
170 <function>sd_journal_get_events()</function>,
171 <function>sd_journal_get_timeout()</function> and
172 <function>sd_journal_process()</function> is
173 <function>sd_journal_wait()</function>. It will
174 synchronously wait until the journal gets changed. The
175 maximum time this call sleeps may be controlled with
176 the <parameter>timeout_usec</parameter>
177 parameter. Pass <literal>(uint64_t) -1</literal> to
178 wait indefinitely. Internally this call simply
179 combines <function>sd_journal_get_fd()</function>,
180 <function>sd_journal_get_events()</function>,
181 <function>sd_journal_get_timeout()</function>,
182 <function>poll()</function> and
183 <function>sd_journal_process()</function> into
186 <para><function>sd_journal_reliable_fd()</function>
187 may be used to check whether the wakeup events from
188 the file descriptor returned by
189 <function>sd_journal_get_fd()</function> are known to
190 be immediately triggered. On certain file systems
191 where file change events from the OS are not available
192 (such as NFS) changes need to be polled for
193 repeatedly, and hence are detected only with a certain
194 latency. This call will return a positive value if the
195 journal changes are detected immediately and zero when
196 they need to be polled for and hence might be noticed
197 only with a certain latency. Note that there's usually
198 no need to invoke this function directly as
199 <function>sd_journal_get_timeout()</function> on these
200 file systems will ask for timeouts explicitly
205 <title>Return Value</title>
207 <para><function>sd_journal_get_fd()</function> returns
208 a valid file descriptor on success or a negative
209 errno-style error code.</para>
211 <para><function>sd_journal_get_events()</function>
212 returns a combination of <literal>POLLIN</literal>,
213 <literal>POLLOUT</literal> and suchlike on success or
214 a negative errno-style error code.</para>
216 <para><function>sd_journal_reliable_fd()</function>
217 returns a positive integer if the file descriptor
218 returned by <function>sd_journal_get_fd()</function>
219 will generate wake-ups immediately for all journal
220 changes. Returns 0 if there might be a latency
223 <para><function>sd_journal_process()</function> and
224 <function>sd_journal_wait()</function> return one of
225 <literal>SD_JOURNAL_NOP</literal>,
226 <literal>SD_JOURNAL_APPEND</literal> or
227 <literal>SD_JOURNAL_INVALIDATE</literal> on success or
228 a negative errno-style error code. If
229 <literal>SD_JOURNAL_NOP</literal> is returned the
230 journal didn't change since the last invocation. If
231 <literal>SD_JOURNAL_APPEND</literal> is returned new
232 entries have been appended to the end of the
233 journal. If <literal>SD_JOURNAL_INVALIDATE</literal>
234 journal files were added or removed (possibly due to
235 rotation). In the latter event live-view UIs should
236 probably refresh their entire display while in the
237 case of <literal>SD_JOURNAL_APPEND</literal> it is
238 sufficient to simply continue reading at the previous
239 end of the journal.</para>
245 <para>The <function>sd_journal_get_fd()</function>,
246 <function>sd_journal_get_events()</function>,
247 <function>sd_journal_reliable_fd()</function>,
248 <function>sd_journal_process()</function> and
249 <function>sd_journal_wait()</function> interfaces are
250 available as shared library, which can be compiled and
252 <literal>libsystemd-journal</literal>
253 <citerefentry><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
258 <title>Examples</title>
260 <para>Iterating through the journal, in a live view tracking all changes:</para>
262 <programlisting>#include <stdio.h>
263 #include <string.h>
264 #include <systemd/sd-journal.h>
266 int main(int argc, char *argv[]) {
269 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
271 fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
277 r = sd_journal_next(j);
279 fprintf(stderr, "Failed to iterate to next entry: %s\n", strerror(-r));
283 /* Reached the end, let's wait for changes, and try again */
284 r = sd_journal_wait(j, (uint64_t) -1);
286 fprintf(stderr, "Failed to wait for changes: %s\n", strerror(-r));
291 r = sd_journal_get_data(j, "MESSAGE", &d, &l);
293 fprintf(stderr, "Failed to read message field: %s\n", strerror(-r));
296 printf("%.*s\n", (int) l, (const char*) d);
302 <para>Waiting with <function>poll()</function> (this
303 example lacks all error checking for the sake of
306 <programlisting>#include <sys/poll.h>
307 #include <systemd/sd-journal.h>
309 int wait_for_changes(sd_journal *j) {
310 struct pollfd pollfd;
313 sd_journal_get_timeout(m, &t);
314 if (t == (uint64_t) -1)
319 clock_getttime(CLOCK_MONOTONIC, &ts);
320 n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
321 msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
324 pollfd.fd = sd_journal_get_fd(j);
325 pollfd.events = sd_journal_get_events(j);
326 poll(&pollfd, 1, msec);
327 return sd_journal_process(j);
333 <title>See Also</title>
336 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
337 <citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
338 <citerefentry><refentrytitle>sd_journal_open</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
339 <citerefentry><refentrytitle>sd_journal_next</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
340 <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
341 <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry>