]> git.proxmox.com Git - systemd.git/blame - man/sd_journal_get_fd.3
Imported Upstream version 217
[systemd.git] / man / sd_journal_get_fd.3
CommitLineData
14228c0d 1'\" t
5eef597e 2.TH "SD_JOURNAL_GET_FD" "3" "" "systemd 217" "sd_journal_get_fd"
14228c0d
MB
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"
23sd_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
60f067b4 32.BI "int sd_journal_get_fd(sd_journal\ *" "j" ");"
14228c0d 33.HP \w'int\ sd_journal_get_events('u
60f067b4 34.BI "int sd_journal_get_events(sd_journal\ *" "j" ");"
14228c0d 35.HP \w'int\ sd_journal_get_timeout('u
60f067b4 36.BI "int sd_journal_get_timeout(sd_journal\ *" "j" ", uint64_t\ *" "timeout_usec" ");"
14228c0d 37.HP \w'int\ sd_journal_process('u
60f067b4 38.BI "int sd_journal_process(sd_journal\ *" "j" ");"
14228c0d 39.HP \w'int\ sd_journal_wait('u
60f067b4 40.BI "int sd_journal_wait(sd_journal\ *" "j" ", uint64_t\ " "timeout_usec" ");"
14228c0d 41.HP \w'int\ sd_journal_reliable_fd('u
60f067b4 42.BI "int sd_journal_reliable_fd(sd_journal\ *" "j" ");"
14228c0d
MB
43.SH "DESCRIPTION"
44.PP
45\fBsd_journal_get_fd()\fR
46returns 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
49for 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
52will 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
55will return the
56\fBpoll()\fR
57mask to wait for\&. This function will return a combination of
58\fBPOLLIN\fR
59and
60\fBPOLLOUT\fR
61and similar to fill into the
62"\&.events"
63field of
64\fIstruct pollfd\fR\&.
65.PP
66\fBsd_journal_get_timeout()\fR
67will return a timeout value for usage in
68\fBpoll()\fR\&. This returns a value in microseconds since the epoch of
69\fBCLOCK_MONOTONIC\fR
70for timing out
71\fBpoll()\fR
72in
73\fItimeout_usec\fR\&. See
74\fBclock_gettime\fR(2)
75for details about
76\fBCLOCK_MONOTONIC\fR\&. If there is no timeout to wait for, this will fill in
77\fB(uint64_t) \-1\fR
78instead\&. Note that
79\fBpoll()\fR
80takes 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
86uint64_t t;
87int msec;
88sd_journal_get_timeout(m, &t);
89if (t == (uint64_t) \-1)
90 msec = \-1;
91else {
92 struct timespec ts;
93 uint64_t n;
94 clock_getttime(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
103The code above does not do any error checking for brevity\*(Aqs sake\&. The calculated
104\fImsec\fR
105integer can be passed directly as
106\fBpoll()\fR\*(Aqs timeout parameter\&.
107.PP
108After each
109\fBpoll()\fR
110wake\-up
111\fBsd_journal_process()\fR
112needs 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
114A synchronous alternative for using
115\fBsd_journal_get_fd()\fR,
116\fBsd_journal_get_events()\fR,
117\fBsd_journal_get_timeout()\fR
118and
119\fBsd_journal_process()\fR
120is
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
123parameter\&. Pass
124\fB(uint64_t) \-1\fR
125to 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
130and
131\fBsd_journal_process()\fR
132into one\&.
133.PP
134\fBsd_journal_reliable_fd()\fR
135may be used to check whether the wakeup events from the file descriptor returned by
136\fBsd_journal_get_fd()\fR
137are known to be immediately 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 certain latency\&. This call will return a positive value if the journal changes are detected immediately and zero when they need to be polled for and hence might be noticed only with a certain latency\&. Note that there\*(Aqs usually no need to invoke this function directly as
138\fBsd_journal_get_timeout()\fR
139on these file systems will ask for timeouts explicitly anyway\&.
140.SH "RETURN VALUE"
141.PP
142\fBsd_journal_get_fd()\fR
143returns a valid file descriptor on success or a negative errno\-style error code\&.
144.PP
145\fBsd_journal_get_events()\fR
146returns a combination of
147\fBPOLLIN\fR,
148\fBPOLLOUT\fR
149and suchlike on success or a negative errno\-style error code\&.
150.PP
151\fBsd_journal_reliable_fd()\fR
152returns a positive integer if the file descriptor returned by
153\fBsd_journal_get_fd()\fR
154will generate wake\-ups immediately for all journal changes\&. Returns 0 if there might be a latency involved\&.
155.PP
156\fBsd_journal_process()\fR
157and
158\fBsd_journal_wait()\fR
159return one of
160\fBSD_JOURNAL_NOP\fR,
161\fBSD_JOURNAL_APPEND\fR
162or
163\fBSD_JOURNAL_INVALIDATE\fR
164on success or a negative errno\-style error code\&. If
165\fBSD_JOURNAL_NOP\fR
166is returned, the journal did not change since the last invocation\&. If
167\fBSD_JOURNAL_APPEND\fR
168is returned, new entries have been appended to the end of the journal\&. If
169\fBSD_JOURNAL_INVALIDATE\fR, journal files were added or removed (possibly due to rotation)\&. In the latter event, live\-view UIs should probably refresh their entire display, while in the case of
170\fBSD_JOURNAL_APPEND\fR, it is sufficient to simply continue reading at the previous end of the journal\&.
171.SH "NOTES"
172.PP
173The
174\fBsd_journal_get_fd()\fR,
175\fBsd_journal_get_events()\fR,
176\fBsd_journal_reliable_fd()\fR,
177\fBsd_journal_process()\fR
178and
179\fBsd_journal_wait()\fR
60f067b4
JS
180interfaces are available as a shared library, which can be compiled and linked to with the
181\fBlibsystemd\fR\ \&\fBpkg-config\fR(1)
14228c0d
MB
182file\&.
183.SH "EXAMPLES"
184.PP
185Iterating through the journal, in a live view tracking all changes:
186.sp
187.if n \{\
188.RS 4
189.\}
190.nf
191#include <stdio\&.h>
192#include <string\&.h>
193#include <systemd/sd\-journal\&.h>
194
195int main(int argc, char *argv[]) {
196 int r;
197 sd_journal *j;
198 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
199 if (r < 0) {
200 fprintf(stderr, "Failed to open journal: %s\en", strerror(\-r));
201 return 1;
202 }
203 for (;;) {
204 const void *d;
205 size_t l;
206 r = sd_journal_next(j);
207 if (r < 0) {
208 fprintf(stderr, "Failed to iterate to next entry: %s\en", strerror(\-r));
209 break;
210 }
211 if (r == 0) {
212 /* Reached the end, let\*(Aqs wait for changes, and try again */
213 r = sd_journal_wait(j, (uint64_t) \-1);
214 if (r < 0) {
215 fprintf(stderr, "Failed to wait for changes: %s\en", strerror(\-r));
216 break;
217 }
218 continue;
219 }
220 r = sd_journal_get_data(j, "MESSAGE", &d, &l);
221 if (r < 0) {
222 fprintf(stderr, "Failed to read message field: %s\en", strerror(\-r));
223 continue;
224 }
225 printf("%\&.*s\en", (int) l, (const char*) d);
226 }
227 sd_journal_close(j);
228 return 0;
229}
230.fi
231.if n \{\
232.RE
233.\}
234.PP
235Waiting with
236\fBpoll()\fR
237(this example lacks all error checking for the sake of simplicity):
238.sp
239.if n \{\
240.RS 4
241.\}
242.nf
243#include <sys/poll\&.h>
244#include <systemd/sd\-journal\&.h>
245
246int wait_for_changes(sd_journal *j) {
247 struct pollfd pollfd;
248 int msec;
249
250 sd_journal_get_timeout(m, &t);
251 if (t == (uint64_t) \-1)
252 msec = \-1;
253 else {
254 struct timespec ts;
255 uint64_t n;
256 clock_getttime(CLOCK_MONOTONIC, &ts);
257 n = (uint64_t) ts\&.tv_sec * 1000000 + ts\&.tv_nsec / 1000;
258 msec = t > n ? (int) ((t \- n + 999) / 1000) : 0;
259 }
260
261 pollfd\&.fd = sd_journal_get_fd(j);
262 pollfd\&.events = sd_journal_get_events(j);
263 poll(&pollfd, 1, msec);
264 return sd_journal_process(j);
265}
14228c0d
MB
266.fi
267.if n \{\
268.RE
269.\}
270.SH "SEE ALSO"
271.PP
272\fBsystemd\fR(1),
273\fBsd-journal\fR(3),
274\fBsd_journal_open\fR(3),
275\fBsd_journal_next\fR(3),
276\fBpoll\fR(2),
277\fBclock_gettime\fR(2)