]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/sd_journal_get_fd.xml
man: generate configured paths in manpages
[thirdparty/systemd.git] / man / sd_journal_get_fd.xml
CommitLineData
91a03172
LP
1<?xml version='1.0'?> <!--*-nxml-*-->
2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
681eb9cf
FB
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
4<!ENTITY % entities SYSTEM "custom-entities.ent" >
5%entities;
6]>
91a03172
LP
7
8<!--
9 This file is part of systemd.
10
11 Copyright 2012 Lennart Poettering
12
13 systemd is free software; you can redistribute it and/or modify it
14 under the terms of the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 2.1 of the License, or
16 (at your option) any later version.
17
18 systemd is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public License
24 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25-->
26
27<refentry id="sd_journal_get_fd">
28
798d3a52
ZJS
29 <refentryinfo>
30 <title>sd_journal_get_fd</title>
31 <productname>systemd</productname>
32
33 <authorgroup>
34 <author>
35 <contrib>Developer</contrib>
36 <firstname>Lennart</firstname>
37 <surname>Poettering</surname>
38 <email>lennart@poettering.net</email>
39 </author>
40 </authorgroup>
41 </refentryinfo>
42
43 <refmeta>
44 <refentrytitle>sd_journal_get_fd</refentrytitle>
45 <manvolnum>3</manvolnum>
46 </refmeta>
47
48 <refnamediv>
49 <refname>sd_journal_get_fd</refname>
50 <refname>sd_journal_get_events</refname>
51 <refname>sd_journal_get_timeout</refname>
52 <refname>sd_journal_process</refname>
53 <refname>sd_journal_wait</refname>
54 <refname>sd_journal_reliable_fd</refname>
55 <refname>SD_JOURNAL_NOP</refname>
56 <refname>SD_JOURNAL_APPEND</refname>
57 <refname>SD_JOURNAL_INVALIDATE</refname>
58 <refpurpose>Journal change notification
59 interface</refpurpose>
60 </refnamediv>
61
62 <refsynopsisdiv>
63 <funcsynopsis>
64 <funcsynopsisinfo>#include &lt;systemd/sd-journal.h&gt;</funcsynopsisinfo>
65
66 <funcprototype>
67 <funcdef>int <function>sd_journal_get_fd</function></funcdef>
68 <paramdef>sd_journal *<parameter>j</parameter></paramdef>
69 </funcprototype>
70
71 <funcprototype>
72 <funcdef>int <function>sd_journal_get_events</function></funcdef>
73 <paramdef>sd_journal *<parameter>j</parameter></paramdef>
74 </funcprototype>
75
76 <funcprototype>
77 <funcdef>int <function>sd_journal_get_timeout</function></funcdef>
78 <paramdef>sd_journal *<parameter>j</parameter></paramdef>
79 <paramdef>uint64_t *<parameter>timeout_usec</parameter></paramdef>
80 </funcprototype>
81
82 <funcprototype>
83 <funcdef>int <function>sd_journal_process</function></funcdef>
84 <paramdef>sd_journal *<parameter>j</parameter></paramdef>
85 </funcprototype>
86
87 <funcprototype>
88 <funcdef>int <function>sd_journal_wait</function></funcdef>
89 <paramdef>sd_journal *<parameter>j</parameter></paramdef>
90 <paramdef>uint64_t <parameter>timeout_usec</parameter></paramdef>
91 </funcprototype>
92
93 <funcprototype>
94 <funcdef>int <function>sd_journal_reliable_fd</function></funcdef>
95 <paramdef>sd_journal *<parameter>j</parameter></paramdef>
96 </funcprototype>
97
98 </funcsynopsis>
99 </refsynopsisdiv>
100
101 <refsect1>
102 <title>Description</title>
103
104 <para><function>sd_journal_get_fd()</function> returns a file
105 descriptor that may be asynchronously polled in an external event
106 loop and is signaled as soon as the journal changes, because new
107 entries or files were added, rotation took place, or files have
108 been deleted, and similar. The file descriptor is suitable for
109 usage in
110 <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>.
111 Use <function>sd_journal_get_events()</function> for an events
112 mask to watch for. The call takes one argument: the journal
113 context object. Note that not all file systems are capable of
114 generating the necessary events for wakeups from this file
115 descriptor for changes to be noticed immediately. In particular
116 network files systems do not generate suitable file change events
117 in all cases. Cases like this can be detected with
118 <function>sd_journal_reliable_fd()</function>, below.
119 <function>sd_journal_get_timeout()</function> will ensure in these
120 cases that wake-ups happen frequently enough for changes to be
121 noticed, although with a certain latency.</para>
122
123 <para><function>sd_journal_get_events()</function> will return the
124 <function>poll()</function> mask to wait for. This function will
125 return a combination of <constant>POLLIN</constant> and
126 <constant>POLLOUT</constant> and similar to fill into the
127 <literal>.events</literal> field of <varname>struct
128 pollfd</varname>.</para>
129
130 <para><function>sd_journal_get_timeout()</function> will return a
131 timeout value for usage in <function>poll()</function>. This
132 returns a value in microseconds since the epoch of
133 <constant>CLOCK_MONOTONIC</constant> for timing out
134 <function>poll()</function> in <varname>timeout_usec</varname>.
135 See
136 <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry>
137 for details about <constant>CLOCK_MONOTONIC</constant>. If there
138 is no timeout to wait for, this will fill in <constant>(uint64_t)
139 -1</constant> instead. Note that <function>poll()</function> takes
140 a relative timeout in milliseconds rather than an absolute timeout
141 in microseconds. To convert the absolute 'us' timeout into
142 relative 'ms', use code like the following:</para>
143
144 <programlisting>uint64_t t;
39c155ea
LP
145int msec;
146sd_journal_get_timeout(m, &amp;t);
147if (t == (uint64_t) -1)
798d3a52 148 msec = -1;
39c155ea 149else {
798d3a52
ZJS
150 struct timespec ts;
151 uint64_t n;
152 clock_getttime(CLOCK_MONOTONIC, &amp;ts);
153 n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
154 msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
39c155ea
LP
155}</programlisting>
156
798d3a52
ZJS
157 <para>The code above does not do any error checking for brevity's
158 sake. The calculated <varname>msec</varname> integer can be passed
159 directly as <function>poll()</function>'s timeout
160 parameter.</para>
161
162 <para>After each <function>poll()</function> wake-up
163 <function>sd_journal_process()</function> needs to be called to
164 process events. This call will also indicate what kind of change
165 has been detected (see below; note that spurious wake-ups are
166 possible).</para>
167
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 synchronously wait
174 until the journal gets changed. The maximum time this call sleeps
175 may be controlled with the <parameter>timeout_usec</parameter>
176 parameter. Pass <constant>(uint64_t) -1</constant> to wait
177 indefinitely. Internally this call simply combines
178 <function>sd_journal_get_fd()</function>,
179 <function>sd_journal_get_events()</function>,
180 <function>sd_journal_get_timeout()</function>,
181 <function>poll()</function> and
182 <function>sd_journal_process()</function> into one.</para>
183
184 <para><function>sd_journal_reliable_fd()</function> may be used to
185 check whether the wakeup events from the file descriptor returned
186 by <function>sd_journal_get_fd()</function> are known to be
187 immediately triggered. On certain file systems where file change
188 events from the OS are not available (such as NFS) changes need to
189 be polled for repeatedly, and hence are detected only with a
190 certain latency. This call will return a positive value if the
191 journal changes are detected immediately and zero when they need
192 to be polled for and hence might be noticed only with a certain
193 latency. Note that there's usually no need to invoke this function
194 directly as <function>sd_journal_get_timeout()</function> on these
195 file systems will ask for timeouts explicitly anyway.</para>
196 </refsect1>
197
198 <refsect1>
199 <title>Return Value</title>
200
201 <para><function>sd_journal_get_fd()</function> returns a valid
202 file descriptor on success or a negative errno-style error
203 code.</para>
204
205 <para><function>sd_journal_get_events()</function> returns a
206 combination of <constant>POLLIN</constant>,
207 <constant>POLLOUT</constant> and suchlike on success or a negative
208 errno-style error code.</para>
209
210 <para><function>sd_journal_reliable_fd()</function> returns a
211 positive integer if the file descriptor returned by
212 <function>sd_journal_get_fd()</function> will generate wake-ups
213 immediately for all journal changes. Returns 0 if there might be a
214 latency involved.</para>
215
216 <para><function>sd_journal_process()</function> and
217 <function>sd_journal_wait()</function> return one of
218 <constant>SD_JOURNAL_NOP</constant>,
219 <constant>SD_JOURNAL_APPEND</constant> or
220 <constant>SD_JOURNAL_INVALIDATE</constant> on success or a
221 negative errno-style error code. If
222 <constant>SD_JOURNAL_NOP</constant> is returned, the journal did
223 not change since the last invocation. If
224 <constant>SD_JOURNAL_APPEND</constant> is returned, new entries
225 have been appended to the end of the journal. If
226 <constant>SD_JOURNAL_INVALIDATE</constant>, journal files were
227 added or removed (possibly due to rotation). In the latter event,
228 live-view UIs should probably refresh their entire display, while
229 in the case of <constant>SD_JOURNAL_APPEND</constant>, it is
230 sufficient to simply continue reading at the previous end of the
231 journal.</para>
232 </refsect1>
233
234 <refsect1>
235 <title>Notes</title>
236
237 <para>The <function>sd_journal_get_fd()</function>,
238 <function>sd_journal_get_events()</function>,
239 <function>sd_journal_reliable_fd()</function>,
240 <function>sd_journal_process()</function> and
241 <function>sd_journal_wait()</function> interfaces are available as
242 a shared library, which can be compiled and linked to with the
243 <constant>libsystemd</constant> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
244 file.</para>
245 </refsect1>
246
247 <refsect1>
248 <title>Examples</title>
249
250 <para>Iterating through the journal, in a live view tracking all
251 changes:</para>
252
253 <programlisting>#include &lt;stdio.h&gt;
85210bff
LP
254#include &lt;string.h&gt;
255#include &lt;systemd/sd-journal.h&gt;
256
257int main(int argc, char *argv[]) {
798d3a52
ZJS
258 int r;
259 sd_journal *j;
260 r = sd_journal_open(&amp;j, SD_JOURNAL_LOCAL_ONLY);
261 if (r &lt; 0) {
262 fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
263 return 1;
264 }
265 for (;;) {
266 const void *d;
267 size_t l;
268 r = sd_journal_next(j);
269 if (r &lt; 0) {
270 fprintf(stderr, "Failed to iterate to next entry: %s\n", strerror(-r));
271 break;
272 }
273 if (r == 0) {
274 /* Reached the end, let's wait for changes, and try again */
275 r = sd_journal_wait(j, (uint64_t) -1);
276 if (r &lt; 0) {
277 fprintf(stderr, "Failed to wait for changes: %s\n", strerror(-r));
278 break;
279 }
280 continue;
281 }
282 r = sd_journal_get_data(j, "MESSAGE", &amp;d, &amp;l);
283 if (r &lt; 0) {
284 fprintf(stderr, "Failed to read message field: %s\n", strerror(-r));
285 continue;
286 }
287 printf("%.*s\n", (int) l, (const char*) d);
288 }
289 sd_journal_close(j);
290 return 0;
85210bff
LP
291}</programlisting>
292
798d3a52
ZJS
293 <para>Waiting with <function>poll()</function> (this
294 example lacks all error checking for the sake of
295 simplicity):</para>
85210bff 296
0a6f50c0 297 <programlisting>#include &lt;poll.h&gt;
85210bff
LP
298#include &lt;systemd/sd-journal.h&gt;
299
300int wait_for_changes(sd_journal *j) {
798d3a52
ZJS
301 struct pollfd pollfd;
302 int msec;
303
304 sd_journal_get_timeout(m, &amp;t);
305 if (t == (uint64_t) -1)
306 msec = -1;
307 else {
308 struct timespec ts;
309 uint64_t n;
310 clock_getttime(CLOCK_MONOTONIC, &amp;ts);
311 n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
312 msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
313 }
314
315 pollfd.fd = sd_journal_get_fd(j);
316 pollfd.events = sd_journal_get_events(j);
317 poll(&amp;pollfd, 1, msec);
318 return sd_journal_process(j);
9fccdb0f 319}</programlisting>
798d3a52
ZJS
320 </refsect1>
321
322 <refsect1>
323 <title>See Also</title>
324
325 <para>
326 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
327 <citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
328 <citerefentry><refentrytitle>sd_journal_open</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
329 <citerefentry><refentrytitle>sd_journal_next</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
330 <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
331 <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry>
332 </para>
333 </refsect1>
91a03172
LP
334
335</refentry>