]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/journal-iterate-wait.c
docs/RANDOM_SEEDS: update NetBSD link
[thirdparty/systemd.git] / man / journal-iterate-wait.c
1 /* SPDX-License-Identifier: MIT-0 */
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <systemd/sd-journal.h>
6
7 int main(int argc, char *argv[]) {
8 int r;
9 sd_journal *j;
10 r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
11 if (r < 0) {
12 errno = -r;
13 fprintf(stderr, "Failed to open journal: %m\n");
14 return 1;
15 }
16 for (;;) {
17 const void *d;
18 size_t l;
19 r = sd_journal_next(j);
20 if (r < 0) {
21 errno = -r;
22 fprintf(stderr, "Failed to iterate to next entry: %m\n");
23 break;
24 }
25 if (r == 0) {
26 /* Reached the end, let's wait for changes, and try again */
27 r = sd_journal_wait(j, (uint64_t) -1);
28 if (r < 0) {
29 errno = -r;
30 fprintf(stderr, "Failed to wait for changes: %m\n");
31 break;
32 }
33 continue;
34 }
35 r = sd_journal_get_data(j, "MESSAGE", &d, &l);
36 if (r < 0) {
37 errno = -r;
38 fprintf(stderr, "Failed to read message field: %m\n");
39 continue;
40 }
41 printf("%.*s\n", (int) l, (const char*) d);
42 }
43 sd_journal_close(j);
44 return 0;
45 }