Debian Codegraph @

upstream/debian-unstable/man3/sd_journal_get_fd.3

raw
1 '\" t
2 .TH "SD_JOURNAL_GET_FD" "3" "" "systemd 261~rc3" "sd_journal_get_fd"
3 .\" -----------------------------------------------------------------
4 .\" * Define some portability stuff
5 .\" -----------------------------------------------------------------
6 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 .\" http://bugs.debian.org/507673
8 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
9 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 .ie \n(.g .ds Aq \(aq
11 .el .ds Aq '
12 .\" -----------------------------------------------------------------
13 .\" * set default formatting
14 .\" -----------------------------------------------------------------
15 .\" disable hyphenation
16 .nh
17 .\" disable justification (adjust text to left margin only)
18 .ad l
19 .\" -----------------------------------------------------------------
20 .\" * MAIN CONTENT STARTS HERE *
21 .\" -----------------------------------------------------------------
22 .SH "NAME"
23 sd_journal_get_fd, sd_journal_get_events, sd_journal_get_timeout, sd_journal_process, sd_journal_wait, sd_journal_reliable_fd, SD_JOURNAL_NOP, SD_JOURNAL_APPEND, SD_JOURNAL_INVALIDATE \- Journal change notification interface
24 .SH "SYNOPSIS"
25 .sp
26 .ft B
27 .nf
28 #include <systemd/sd\-journal\&.h>
29 .fi
30 .ft
31 .HP \w'int\ sd_journal_get_fd('u
32 .BI "int sd_journal_get_fd(sd_journal\ *" "j" ");"
33 .HP \w'int\ sd_journal_get_events('u
34 .BI "int sd_journal_get_events(sd_journal\ *" "j" ");"
35 .HP \w'int\ sd_journal_get_timeout('u
36 .BI "int sd_journal_get_timeout(sd_journal\ *" "j" ", uint64_t\ *" "timeout_usec" ");"
37 .HP \w'int\ sd_journal_process('u
38 .BI "int sd_journal_process(sd_journal\ *" "j" ");"
39 .HP \w'int\ sd_journal_wait('u
40 .BI "int sd_journal_wait(sd_journal\ *" "j" ", uint64_t\ " "timeout_usec" ");"
41 .HP \w'int\ sd_journal_reliable_fd('u
42 .BI "int sd_journal_reliable_fd(sd_journal\ *" "j" ");"
43 .SH "DESCRIPTION"
44 .PP
45 \fBsd_journal_get_fd()\fR
46 returns a file descriptor that may be asynchronously polled in an external event loop and is signaled as soon as the journal changes, because new entries or files were added, rotation took place, or files have been deleted, and similar\&. The file descriptor is suitable for usage in
47 \fBpoll\fR(2)\&. Use
48 \fBsd_journal_get_events()\fR
49 for an events mask to watch for\&. The call takes one argument: the journal context object\&. Note that not all file systems are capable of generating the necessary events for wakeups from this file descriptor for changes to be noticed immediately\&. In particular network files systems do not generate suitable file change events in all cases\&. Cases like this can be detected with
50 \fBsd_journal_reliable_fd()\fR, below\&.
51 \fBsd_journal_get_timeout()\fR
52 will ensure in these cases that wake\-ups happen frequently enough for changes to be noticed, although with a certain latency\&.
53 .PP
54 \fBsd_journal_get_events()\fR
55 will return the
56 \fBpoll()\fR
57 mask to wait for\&. This function will return a combination of
58 \fBPOLLIN\fR
59 and
60 \fBPOLLOUT\fR
61 and similar to fill into the
62 "\&.events"
63 field of
64 \fIstruct pollfd\fR\&.
65 .PP
66 \fBsd_journal_get_timeout()\fR
67 will return a timeout value for usage in
68 \fBpoll()\fR\&. This returns a value in microseconds since the epoch of
69 \fBCLOCK_MONOTONIC\fR
70 for timing out
71 \fBpoll()\fR
72 in
73 \fItimeout_usec\fR\&. See
74 \fBclock_gettime\fR(2)
75 for details about
76 \fBCLOCK_MONOTONIC\fR\&. If there is no timeout to wait for, this will fill in
77 \fB(uint64_t) \-1\fR
78 instead\&. Note that
79 \fBpoll()\fR
80 takes a relative timeout in milliseconds rather than an absolute timeout in microseconds\&. To convert the absolute \*(Aqus\*(Aq timeout into relative \*(Aqms\*(Aq, use code like the following:
81 .sp
82 .if n \{\
83 .RS 4
84 .\}
85 .nf
86 uint64_t t;
87 int msec;
88 sd_journal_get_timeout(m, &t);
89 if (t == (uint64_t) \-1)
90 msec = \-1;
91 else {
92 struct timespec ts;
93 uint64_t n;
94 clock_gettime(CLOCK_MONOTONIC, &ts);
95 n = (uint64_t) ts\&.tv_sec * 1000000 + ts\&.tv_nsec / 1000;
96 msec = t > n ? (int) ((t \- n + 999) / 1000) : 0;
97 }
98 .fi
99 .if n \{\
100 .RE
101 .\}
102 .PP
103 The code above does not do any error checking for brevity\*(Aqs sake\&. The calculated
104 \fImsec\fR
105 integer can be passed directly as
106 \fBpoll()\fR\*(Aqs timeout parameter\&.
107 .PP
108 After each
109 \fBpoll()\fR
110 wake\-up
111 \fBsd_journal_process()\fR
112 needs to be called to process events\&. This call will also indicate what kind of change has been detected (see below; note that spurious wake\-ups are possible)\&.
113 .PP
114 A synchronous alternative for using
115 \fBsd_journal_get_fd()\fR,
116 \fBsd_journal_get_events()\fR,
117 \fBsd_journal_get_timeout()\fR
118 and
119 \fBsd_journal_process()\fR
120 is
121 \fBsd_journal_wait()\fR\&. It will synchronously wait until the journal gets changed\&. The maximum time this call sleeps may be controlled with the
122 \fItimeout_usec\fR
123 parameter\&. Pass
124 \fB(uint64_t) \-1\fR
125 to wait indefinitely\&. Internally this call simply combines
126 \fBsd_journal_get_fd()\fR,
127 \fBsd_journal_get_events()\fR,
128 \fBsd_journal_get_timeout()\fR,
129 \fBpoll()\fR
130 and
131 \fBsd_journal_process()\fR
132 into one\&.
133 .PP
134 \fBsd_journal_reliable_fd()\fR
135 may be used to check whether the wake\-up events from the file descriptor returned by
136 \fBsd_journal_get_fd()\fR
137 are known to be quickly triggered\&. On certain file systems where file change events from the OS are not available (such as NFS) changes need to be polled for repeatedly, and hence are detected only with a considerable latency\&. This call will return a positive value if the journal changes are detected quickly and zero when they need to be polled for\&. Note that there is usually no need to invoke this function directly as
138 \fBsd_journal_get_timeout()\fR
139 will request appropriate timeouts anyway\&.
140 .PP
141 Note that all of the above change notification interfaces do not report changes instantly\&. Latencies are introduced for multiple reasons: as mentioned certain storage backends require time\-based polling, in other cases wake\-ups are optimized by coalescing events, and the OS introduces additional IO/CPU scheduling latencies\&.
142 .SH "RETURN VALUE"
143 .PP
144 \fBsd_journal_get_fd()\fR
145 returns a valid file descriptor on success or a negative errno\-style error code\&.
146 .PP
147 \fBsd_journal_get_events()\fR
148 returns a combination of
149 \fBPOLLIN\fR,
150 \fBPOLLOUT\fR
151 and suchlike on success or a negative errno\-style error code\&.
152 .PP
153 \fBsd_journal_reliable_fd()\fR
154 returns a positive integer if the file descriptor returned by
155 \fBsd_journal_get_fd()\fR
156 will generate wake\-ups immediately for all journal changes\&. Returns 0 if there might be a latency involved\&.
157 .PP
158 \fBsd_journal_process()\fR
159 and
160 \fBsd_journal_wait()\fR
161 return a negative errno\-style error code, or one of
162 \fBSD_JOURNAL_NOP\fR,
163 \fBSD_JOURNAL_APPEND\fR
164 or
165 \fBSD_JOURNAL_INVALIDATE\fR
166 on success:
167 .sp
168 .RS 4
169 .ie n \{\
170 \h'-04'\(bu\h'+03'\c
171 .\}
172 .el \{\
173 .sp -1
174 .IP \(bu 2.3
175 .\}
176 If
177 \fBSD_JOURNAL_NOP\fR
178 is returned, the journal did not change since the last invocation\&.
179 .RE
180 .sp
181 .RS 4
182 .ie n \{\
183 \h'-04'\(bu\h'+03'\c
184 .\}
185 .el \{\
186 .sp -1
187 .IP \(bu 2.3
188 .\}
189 If
190 \fBSD_JOURNAL_APPEND\fR
191 is returned, new entries have been appended to the end of the journal\&. In this case, it is sufficient to simply continue reading at the previous end location of the journal, to read the newly added entries\&.
192 .RE
193 .sp
194 .RS 4
195 .ie n \{\
196 \h'-04'\(bu\h'+03'\c
197 .\}
198 .el \{\
199 .sp -1
200 .IP \(bu 2.3
201 .\}
202 If
203 \fBSD_JOURNAL_INVALIDATE\fR, journal files were added to or removed from the set of journal files watched (e\&.g\&. due to rotation or vacuuming), and thus entries might have appeared or disappeared at arbitrary places in the log stream, possibly before or after the previous end of the log stream\&. If
204 \fBSD_JOURNAL_INVALIDATE\fR
205 is returned, live\-view UIs that want to reflect on screen the precise state of the log data on disk should probably refresh their entire display (relative to the cursor of the log entry on the top of the screen)\&. Programs only interested in a strictly sequential stream of log data may treat
206 \fBSD_JOURNAL_INVALIDATE\fR
207 the same way as
208 \fBSD_JOURNAL_APPEND\fR, thus ignoring any changes to the log view earlier than the old end of the log stream\&.
209 .RE
210 .SH "SIGNAL SAFETY"
211 .PP
212 In general,
213 \fBsd_journal_get_fd()\fR,
214 \fBsd_journal_get_events()\fR, and
215 \fBsd_journal_get_timeout()\fR
216 are
217 \fInot\fR
218 "async signal safe" in the meaning of
219 \fBsignal-safety\fR(7)\&. Nevertheless, only the first call to any of those three functions performs unsafe operations, so subsequent calls
220 \fIare\fR
221 safe\&.
222 .PP
223 \fBsd_journal_process()\fR
224 and
225 \fBsd_journal_wait()\fR
226 are not safe\&.
227 \fBsd_journal_reliable_fd()\fR
228 is safe\&.
229 .SH "NOTES"
230 .PP
231 All functions listed here are thread\-agnostic and only a single thread may operate on a given object at any given time\&. Different threads may access the same object at different times\&. Multiple independent objects may be used from different threads in parallel\&.
232 .PP
233 Functions described here are available as a shared library, which can be compiled against and linked to with the
234 \fBlibsystemd\fR\ \&\fBpkg-config\fR(1)
235 file\&.
236 .SH "EXAMPLES"
237 .PP
238 Iterating through the journal, in a live view tracking all changes:
239 .sp
240 .if n \{\
241 .RS 4
242 .\}
243 .nf
244 /* SPDX\-License\-Identifier: MIT\-0 */
245
246 #include <errno\&.h>
247 #include <stdio\&.h>
248 #include <systemd/sd\-journal\&.h>
249
250 int main(int argc, char *argv[]) {
251 int r;
252 sd_journal *j;
253
254 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
255 if (r < 0) {
256 fprintf(stderr, "Failed to open journal: %s\en", strerror(\-r));
257 return 1;
258 }
259
260 for (;;) {
261 const void *d;
262 size_t l;
263 r = sd_journal_next(j);
264 if (r < 0) {
265 fprintf(stderr, "Failed to iterate to next entry: %s\en", strerror(\-r));
266 break;
267 }
268 if (r == 0) {
269 /* Reached the end, let\*(Aqs wait for changes, and try again */
270 r = sd_journal_wait(j, (uint64_t) \-1);
271 if (r < 0) {
272 fprintf(stderr, "Failed to wait for changes: %s\en", strerror(\-r));
273 break;
274 }
275 continue;
276 }
277 r = sd_journal_get_data(j, "MESSAGE", &d, &l);
278 if (r < 0) {
279 fprintf(stderr, "Failed to read message field: %s\en", strerror(\-r));
280 continue;
281 }
282 printf("%\&.*s\en", (int) l, (const char*) d);
283 }
284
285 sd_journal_close(j);
286 return 0;
287 }
288 .fi
289 .if n \{\
290 .RE
291 .\}
292 .PP
293 Waiting with
294 \fBpoll()\fR
295 (this example lacks all error checking for the sake of simplicity):
296 .sp
297 .if n \{\
298 .RS 4
299 .\}
300 .nf
301 /* SPDX\-License\-Identifier: MIT\-0 */
302
303 #define _GNU_SOURCE 1
304 #include <poll\&.h>
305 #include <time\&.h>
306 #include <systemd/sd\-journal\&.h>
307
308 int wait_for_changes(sd_journal *j) {
309 uint64_t t;
310 int msec;
311 struct pollfd pollfd;
312
313 sd_journal_get_timeout(j, &t);
314 if (t == (uint64_t) \-1)
315 msec = \-1;
316 else {
317 struct timespec ts;
318 uint64_t n;
319 clock_gettime(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;
322 }
323
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);
328 }
329 .fi
330 .if n \{\
331 .RE
332 .\}
333 .SH "HISTORY"
334 .PP
335 \fBsd_journal_get_fd()\fR,
336 \fBsd_journal_process()\fR, and
337 \fBsd_journal_wait()\fR
338 were added in version 187\&.
339 .PP
340 \fBsd_journal_reliable_fd()\fR
341 was added in version 196\&.
342 .PP
343 \fBsd_journal_get_events()\fR
344 and
345 \fBsd_journal_get_timeout()\fR
346 were added in version 201\&.
347 .SH "SEE ALSO"
348 .PP
349 \fBsystemd\fR(1), \fBsd-journal\fR(3), \fBsd_journal_open\fR(3), \fBsd_journal_next\fR(3), \fBpoll\fR(2), \fBclock_gettime\fR(2)