]>
Commit | Line | Data |
---|---|---|
91a03172 LP |
1 | <?xml version='1.0'?> <!--*-nxml-*--> |
2 | <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" | |
12b42c76 | 3 | "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> |
91a03172 LP |
4 | |
5 | <!-- | |
572eb058 | 6 | SPDX-License-Identifier: LGPL-2.1+ |
91a03172 LP |
7 | --> |
8 | ||
929f5263 | 9 | <refentry id="sd_journal_get_fd" xmlns:xi="http://www.w3.org/2001/XInclude"> |
91a03172 | 10 | |
798d3a52 ZJS |
11 | <refentryinfo> |
12 | <title>sd_journal_get_fd</title> | |
13 | <productname>systemd</productname> | |
14 | ||
15 | <authorgroup> | |
16 | <author> | |
17 | <contrib>Developer</contrib> | |
18 | <firstname>Lennart</firstname> | |
19 | <surname>Poettering</surname> | |
20 | <email>lennart@poettering.net</email> | |
21 | </author> | |
22 | </authorgroup> | |
23 | </refentryinfo> | |
24 | ||
25 | <refmeta> | |
26 | <refentrytitle>sd_journal_get_fd</refentrytitle> | |
27 | <manvolnum>3</manvolnum> | |
28 | </refmeta> | |
29 | ||
30 | <refnamediv> | |
31 | <refname>sd_journal_get_fd</refname> | |
32 | <refname>sd_journal_get_events</refname> | |
33 | <refname>sd_journal_get_timeout</refname> | |
34 | <refname>sd_journal_process</refname> | |
35 | <refname>sd_journal_wait</refname> | |
36 | <refname>sd_journal_reliable_fd</refname> | |
37 | <refname>SD_JOURNAL_NOP</refname> | |
38 | <refname>SD_JOURNAL_APPEND</refname> | |
39 | <refname>SD_JOURNAL_INVALIDATE</refname> | |
40 | <refpurpose>Journal change notification | |
41 | interface</refpurpose> | |
42 | </refnamediv> | |
43 | ||
44 | <refsynopsisdiv> | |
45 | <funcsynopsis> | |
46 | <funcsynopsisinfo>#include <systemd/sd-journal.h></funcsynopsisinfo> | |
47 | ||
48 | <funcprototype> | |
49 | <funcdef>int <function>sd_journal_get_fd</function></funcdef> | |
50 | <paramdef>sd_journal *<parameter>j</parameter></paramdef> | |
51 | </funcprototype> | |
52 | ||
53 | <funcprototype> | |
54 | <funcdef>int <function>sd_journal_get_events</function></funcdef> | |
55 | <paramdef>sd_journal *<parameter>j</parameter></paramdef> | |
56 | </funcprototype> | |
57 | ||
58 | <funcprototype> | |
59 | <funcdef>int <function>sd_journal_get_timeout</function></funcdef> | |
60 | <paramdef>sd_journal *<parameter>j</parameter></paramdef> | |
61 | <paramdef>uint64_t *<parameter>timeout_usec</parameter></paramdef> | |
62 | </funcprototype> | |
63 | ||
64 | <funcprototype> | |
65 | <funcdef>int <function>sd_journal_process</function></funcdef> | |
66 | <paramdef>sd_journal *<parameter>j</parameter></paramdef> | |
67 | </funcprototype> | |
68 | ||
69 | <funcprototype> | |
70 | <funcdef>int <function>sd_journal_wait</function></funcdef> | |
71 | <paramdef>sd_journal *<parameter>j</parameter></paramdef> | |
72 | <paramdef>uint64_t <parameter>timeout_usec</parameter></paramdef> | |
73 | </funcprototype> | |
74 | ||
75 | <funcprototype> | |
76 | <funcdef>int <function>sd_journal_reliable_fd</function></funcdef> | |
77 | <paramdef>sd_journal *<parameter>j</parameter></paramdef> | |
78 | </funcprototype> | |
79 | ||
80 | </funcsynopsis> | |
81 | </refsynopsisdiv> | |
82 | ||
83 | <refsect1> | |
84 | <title>Description</title> | |
85 | ||
86 | <para><function>sd_journal_get_fd()</function> returns a file | |
87 | descriptor that may be asynchronously polled in an external event | |
88 | loop and is signaled as soon as the journal changes, because new | |
89 | entries or files were added, rotation took place, or files have | |
90 | been deleted, and similar. The file descriptor is suitable for | |
91 | usage in | |
92 | <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>. | |
93 | Use <function>sd_journal_get_events()</function> for an events | |
94 | mask to watch for. The call takes one argument: the journal | |
95 | context object. Note that not all file systems are capable of | |
96 | generating the necessary events for wakeups from this file | |
97 | descriptor for changes to be noticed immediately. In particular | |
98 | network files systems do not generate suitable file change events | |
99 | in all cases. Cases like this can be detected with | |
100 | <function>sd_journal_reliable_fd()</function>, below. | |
101 | <function>sd_journal_get_timeout()</function> will ensure in these | |
102 | cases that wake-ups happen frequently enough for changes to be | |
103 | noticed, although with a certain latency.</para> | |
104 | ||
105 | <para><function>sd_journal_get_events()</function> will return the | |
106 | <function>poll()</function> mask to wait for. This function will | |
107 | return a combination of <constant>POLLIN</constant> and | |
108 | <constant>POLLOUT</constant> and similar to fill into the | |
109 | <literal>.events</literal> field of <varname>struct | |
110 | pollfd</varname>.</para> | |
111 | ||
112 | <para><function>sd_journal_get_timeout()</function> will return a | |
113 | timeout value for usage in <function>poll()</function>. This | |
114 | returns a value in microseconds since the epoch of | |
115 | <constant>CLOCK_MONOTONIC</constant> for timing out | |
116 | <function>poll()</function> in <varname>timeout_usec</varname>. | |
117 | See | |
118 | <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry> | |
119 | for details about <constant>CLOCK_MONOTONIC</constant>. If there | |
120 | is no timeout to wait for, this will fill in <constant>(uint64_t) | |
121 | -1</constant> instead. Note that <function>poll()</function> takes | |
122 | a relative timeout in milliseconds rather than an absolute timeout | |
123 | in microseconds. To convert the absolute 'us' timeout into | |
124 | relative 'ms', use code like the following:</para> | |
125 | ||
126 | <programlisting>uint64_t t; | |
39c155ea LP |
127 | int msec; |
128 | sd_journal_get_timeout(m, &t); | |
129 | if (t == (uint64_t) -1) | |
798d3a52 | 130 | msec = -1; |
39c155ea | 131 | else { |
798d3a52 ZJS |
132 | struct timespec ts; |
133 | uint64_t n; | |
8feabc46 | 134 | clock_gettime(CLOCK_MONOTONIC, &ts); |
798d3a52 ZJS |
135 | n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000; |
136 | msec = t > n ? (int) ((t - n + 999) / 1000) : 0; | |
39c155ea LP |
137 | }</programlisting> |
138 | ||
798d3a52 ZJS |
139 | <para>The code above does not do any error checking for brevity's |
140 | sake. The calculated <varname>msec</varname> integer can be passed | |
141 | directly as <function>poll()</function>'s timeout | |
142 | parameter.</para> | |
143 | ||
144 | <para>After each <function>poll()</function> wake-up | |
145 | <function>sd_journal_process()</function> needs to be called to | |
146 | process events. This call will also indicate what kind of change | |
147 | has been detected (see below; note that spurious wake-ups are | |
148 | possible).</para> | |
149 | ||
150 | <para>A synchronous alternative for using | |
151 | <function>sd_journal_get_fd()</function>, | |
152 | <function>sd_journal_get_events()</function>, | |
153 | <function>sd_journal_get_timeout()</function> and | |
154 | <function>sd_journal_process()</function> is | |
155 | <function>sd_journal_wait()</function>. It will synchronously wait | |
156 | until the journal gets changed. The maximum time this call sleeps | |
157 | may be controlled with the <parameter>timeout_usec</parameter> | |
158 | parameter. Pass <constant>(uint64_t) -1</constant> to wait | |
159 | indefinitely. Internally this call simply combines | |
160 | <function>sd_journal_get_fd()</function>, | |
161 | <function>sd_journal_get_events()</function>, | |
162 | <function>sd_journal_get_timeout()</function>, | |
163 | <function>poll()</function> and | |
164 | <function>sd_journal_process()</function> into one.</para> | |
165 | ||
166 | <para><function>sd_journal_reliable_fd()</function> may be used to | |
167 | check whether the wakeup events from the file descriptor returned | |
168 | by <function>sd_journal_get_fd()</function> are known to be | |
169 | immediately triggered. On certain file systems where file change | |
170 | events from the OS are not available (such as NFS) changes need to | |
171 | be polled for repeatedly, and hence are detected only with a | |
172 | certain latency. This call will return a positive value if the | |
173 | journal changes are detected immediately and zero when they need | |
174 | to be polled for and hence might be noticed only with a certain | |
7ca41557 | 175 | latency. Note that there is usually no need to invoke this function |
798d3a52 ZJS |
176 | directly as <function>sd_journal_get_timeout()</function> on these |
177 | file systems will ask for timeouts explicitly anyway.</para> | |
178 | </refsect1> | |
179 | ||
180 | <refsect1> | |
181 | <title>Return Value</title> | |
182 | ||
183 | <para><function>sd_journal_get_fd()</function> returns a valid | |
184 | file descriptor on success or a negative errno-style error | |
185 | code.</para> | |
186 | ||
187 | <para><function>sd_journal_get_events()</function> returns a | |
188 | combination of <constant>POLLIN</constant>, | |
189 | <constant>POLLOUT</constant> and suchlike on success or a negative | |
190 | errno-style error code.</para> | |
191 | ||
192 | <para><function>sd_journal_reliable_fd()</function> returns a | |
193 | positive integer if the file descriptor returned by | |
194 | <function>sd_journal_get_fd()</function> will generate wake-ups | |
195 | immediately for all journal changes. Returns 0 if there might be a | |
196 | latency involved.</para> | |
197 | ||
aafcd252 LP |
198 | <para><function>sd_journal_process()</function> and <function>sd_journal_wait()</function> return a negative |
199 | errno-style error code, or one of <constant>SD_JOURNAL_NOP</constant>, <constant>SD_JOURNAL_APPEND</constant> or | |
200 | <constant>SD_JOURNAL_INVALIDATE</constant> on success:</para> | |
201 | ||
202 | <itemizedlist> | |
203 | <listitem><para>If <constant>SD_JOURNAL_NOP</constant> is returned, the journal did not change since the last | |
204 | invocation.</para></listitem> | |
205 | ||
206 | <listitem><para>If <constant>SD_JOURNAL_APPEND</constant> is returned, new entries have been appended to the end | |
207 | of the journal. In this case it is sufficient to simply continue reading at the previous end location of the | |
208 | journal, to read the newly added entries.</para></listitem> | |
209 | ||
210 | <listitem><para>If <constant>SD_JOURNAL_INVALIDATE</constant>, journal files were added to or removed from the | |
1b2ad5d9 | 211 | set of journal files watched (e.g. due to rotation or vacuuming), and thus entries might have appeared or |
aafcd252 LP |
212 | disappeared at arbitrary places in the log stream, possibly before or after the previous end of the log |
213 | stream. If <constant>SD_JOURNAL_INVALIDATE</constant> is returned, live-view UIs that want to reflect on screen | |
214 | the precise state of the log data on disk should probably refresh their entire display (relative to the cursor of | |
1b2ad5d9 | 215 | the log entry on the top of the screen). Programs only interested in a strictly sequential stream of log data may |
aafcd252 LP |
216 | treat <constant>SD_JOURNAL_INVALIDATE</constant> the same way as <constant>SD_JOURNAL_APPEND</constant>, thus |
217 | ignoring any changes to the log view earlier than the old end of the log stream.</para></listitem> | |
218 | </itemizedlist> | |
798d3a52 ZJS |
219 | </refsect1> |
220 | ||
89f552c0 ZJS |
221 | <refsect1> |
222 | <title>Signal safety</title> | |
223 | ||
2695b872 LP |
224 | <para>In general, <function>sd_journal_get_fd()</function>, <function>sd_journal_get_events()</function>, and |
225 | <function>sd_journal_get_timeout()</function> are <emphasis>not</emphasis> "async signal safe" in the meaning of | |
226 | <citerefentry | |
227 | project='man-pages'><refentrytitle>signal-safety</refentrytitle><manvolnum>7</manvolnum></citerefentry>. | |
228 | Nevertheless, only the first call to any of those three functions performs unsafe operations, so subsequent calls | |
229 | <emphasis>are</emphasis> safe.</para> | |
89f552c0 ZJS |
230 | |
231 | <para><function>sd_journal_process()</function> and <function>sd_journal_wait()</function> are not | |
232 | safe. <function>sd_journal_reliable_fd()</function> is safe.</para> | |
233 | </refsect1> | |
234 | ||
798d3a52 ZJS |
235 | <refsect1> |
236 | <title>Notes</title> | |
237 | ||
238 | <para>The <function>sd_journal_get_fd()</function>, | |
239 | <function>sd_journal_get_events()</function>, | |
240 | <function>sd_journal_reliable_fd()</function>, | |
241 | <function>sd_journal_process()</function> and | |
242 | <function>sd_journal_wait()</function> interfaces are available as | |
243 | a shared library, which can be compiled and linked to with the | |
244 | <constant>libsystemd</constant> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry> | |
245 | file.</para> | |
246 | </refsect1> | |
247 | ||
248 | <refsect1> | |
249 | <title>Examples</title> | |
250 | ||
251 | <para>Iterating through the journal, in a live view tracking all | |
252 | changes:</para> | |
253 | ||
929f5263 | 254 | <programlisting><xi:include href="journal-iterate-wait.c" parse="text" /></programlisting> |
85210bff | 255 | |
798d3a52 ZJS |
256 | <para>Waiting with <function>poll()</function> (this |
257 | example lacks all error checking for the sake of | |
258 | simplicity):</para> | |
85210bff | 259 | |
929f5263 | 260 | <programlisting><xi:include href="journal-iterate-poll.c" parse="text" /></programlisting> |
798d3a52 ZJS |
261 | </refsect1> |
262 | ||
263 | <refsect1> | |
264 | <title>See Also</title> | |
265 | ||
266 | <para> | |
267 | <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>, | |
268 | <citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
269 | <citerefentry><refentrytitle>sd_journal_open</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
270 | <citerefentry><refentrytitle>sd_journal_next</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
271 | <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>, | |
272 | <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry> | |
273 | </para> | |
274 | </refsect1> | |
91a03172 LP |
275 | |
276 | </refentry> |