]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/sd-journal.c
journalctl: show duplicate entries if they are from the same file (#14898)
[thirdparty/systemd.git] / src / journal / sd-journal.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <inttypes.h>
6 #include <linux/magic.h>
7 #include <poll.h>
8 #include <stddef.h>
9 #include <sys/inotify.h>
10 #include <sys/vfs.h>
11 #include <unistd.h>
12
13 #include "sd-journal.h"
14
15 #include "alloc-util.h"
16 #include "catalog.h"
17 #include "compress.h"
18 #include "dirent-util.h"
19 #include "env-file.h"
20 #include "escape.h"
21 #include "fd-util.h"
22 #include "fileio.h"
23 #include "format-util.h"
24 #include "fs-util.h"
25 #include "hashmap.h"
26 #include "hostname-util.h"
27 #include "id128-util.h"
28 #include "io-util.h"
29 #include "journal-def.h"
30 #include "journal-file.h"
31 #include "journal-internal.h"
32 #include "list.h"
33 #include "lookup3.h"
34 #include "nulstr-util.h"
35 #include "path-util.h"
36 #include "process-util.h"
37 #include "replace-var.h"
38 #include "stat-util.h"
39 #include "stdio-util.h"
40 #include "string-util.h"
41 #include "strv.h"
42 #include "syslog-util.h"
43
44 #define JOURNAL_FILES_MAX 7168
45
46 #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
47
48 #define REPLACE_VAR_MAX 256
49
50 #define DEFAULT_DATA_THRESHOLD (64*1024)
51
52 static void remove_file_real(sd_journal *j, JournalFile *f);
53
54 static bool journal_pid_changed(sd_journal *j) {
55 assert(j);
56
57 /* We don't support people creating a journal object and
58 * keeping it around over a fork(). Let's complain. */
59
60 return j->original_pid != getpid_cached();
61 }
62
63 static int journal_put_error(sd_journal *j, int r, const char *path) {
64 char *copy;
65 int k;
66
67 /* Memorize an error we encountered, and store which
68 * file/directory it was generated from. Note that we store
69 * only *one* path per error code, as the error code is the
70 * key into the hashmap, and the path is the value. This means
71 * we keep track only of all error kinds, but not of all error
72 * locations. This has the benefit that the hashmap cannot
73 * grow beyond bounds.
74 *
75 * We return an error here only if we didn't manage to
76 * memorize the real error. */
77
78 if (r >= 0)
79 return r;
80
81 k = hashmap_ensure_allocated(&j->errors, NULL);
82 if (k < 0)
83 return k;
84
85 if (path) {
86 copy = strdup(path);
87 if (!copy)
88 return -ENOMEM;
89 } else
90 copy = NULL;
91
92 k = hashmap_put(j->errors, INT_TO_PTR(r), copy);
93 if (k < 0) {
94 free(copy);
95
96 if (k == -EEXIST)
97 return 0;
98
99 return k;
100 }
101
102 return 0;
103 }
104
105 static void detach_location(sd_journal *j) {
106 Iterator i;
107 JournalFile *f;
108
109 assert(j);
110
111 j->current_file = NULL;
112 j->current_field = 0;
113
114 ORDERED_HASHMAP_FOREACH(f, j->files, i)
115 journal_file_reset_location(f);
116 }
117
118 static void reset_location(sd_journal *j) {
119 assert(j);
120
121 detach_location(j);
122 zero(j->current_location);
123 }
124
125 static void init_location(Location *l, LocationType type, JournalFile *f, Object *o) {
126 assert(l);
127 assert(IN_SET(type, LOCATION_DISCRETE, LOCATION_SEEK));
128 assert(f);
129 assert(o->object.type == OBJECT_ENTRY);
130
131 l->type = type;
132 l->seqnum = le64toh(o->entry.seqnum);
133 l->seqnum_id = f->header->seqnum_id;
134 l->realtime = le64toh(o->entry.realtime);
135 l->monotonic = le64toh(o->entry.monotonic);
136 l->boot_id = o->entry.boot_id;
137 l->xor_hash = le64toh(o->entry.xor_hash);
138
139 l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
140 }
141
142 static void set_location(sd_journal *j, JournalFile *f, Object *o) {
143 assert(j);
144 assert(f);
145 assert(o);
146
147 init_location(&j->current_location, LOCATION_DISCRETE, f, o);
148
149 j->current_file = f;
150 j->current_field = 0;
151
152 /* Let f know its candidate entry was picked. */
153 assert(f->location_type == LOCATION_SEEK);
154 f->location_type = LOCATION_DISCRETE;
155 }
156
157 static int match_is_valid(const void *data, size_t size) {
158 const char *b, *p;
159
160 assert(data);
161
162 if (size < 2)
163 return false;
164
165 if (((char*) data)[0] == '_' && ((char*) data)[1] == '_')
166 return false;
167
168 b = data;
169 for (p = b; p < b + size; p++) {
170
171 if (*p == '=')
172 return p > b;
173
174 if (*p == '_')
175 continue;
176
177 if (*p >= 'A' && *p <= 'Z')
178 continue;
179
180 if (*p >= '0' && *p <= '9')
181 continue;
182
183 return false;
184 }
185
186 return false;
187 }
188
189 static bool same_field(const void *_a, size_t s, const void *_b, size_t t) {
190 const uint8_t *a = _a, *b = _b;
191 size_t j;
192
193 for (j = 0; j < s && j < t; j++) {
194
195 if (a[j] != b[j])
196 return false;
197
198 if (a[j] == '=')
199 return true;
200 }
201
202 assert_not_reached("\"=\" not found");
203 }
204
205 static Match *match_new(Match *p, MatchType t) {
206 Match *m;
207
208 m = new(Match, 1);
209 if (!m)
210 return NULL;
211
212 *m = (Match) {
213 .type = t,
214 .parent = p,
215 };
216
217 if (p)
218 LIST_PREPEND(matches, p->matches, m);
219
220 return m;
221 }
222
223 static void match_free(Match *m) {
224 assert(m);
225
226 while (m->matches)
227 match_free(m->matches);
228
229 if (m->parent)
230 LIST_REMOVE(matches, m->parent->matches, m);
231
232 free(m->data);
233 free(m);
234 }
235
236 static void match_free_if_empty(Match *m) {
237 if (!m || m->matches)
238 return;
239
240 match_free(m);
241 }
242
243 _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
244 Match *l3, *l4, *add_here = NULL, *m;
245 le64_t le_hash;
246
247 assert_return(j, -EINVAL);
248 assert_return(!journal_pid_changed(j), -ECHILD);
249 assert_return(data, -EINVAL);
250
251 if (size == 0)
252 size = strlen(data);
253
254 assert_return(match_is_valid(data, size), -EINVAL);
255
256 /* level 0: AND term
257 * level 1: OR terms
258 * level 2: AND terms
259 * level 3: OR terms
260 * level 4: concrete matches */
261
262 if (!j->level0) {
263 j->level0 = match_new(NULL, MATCH_AND_TERM);
264 if (!j->level0)
265 return -ENOMEM;
266 }
267
268 if (!j->level1) {
269 j->level1 = match_new(j->level0, MATCH_OR_TERM);
270 if (!j->level1)
271 return -ENOMEM;
272 }
273
274 if (!j->level2) {
275 j->level2 = match_new(j->level1, MATCH_AND_TERM);
276 if (!j->level2)
277 return -ENOMEM;
278 }
279
280 assert(j->level0->type == MATCH_AND_TERM);
281 assert(j->level1->type == MATCH_OR_TERM);
282 assert(j->level2->type == MATCH_AND_TERM);
283
284 le_hash = htole64(hash64(data, size));
285
286 LIST_FOREACH(matches, l3, j->level2->matches) {
287 assert(l3->type == MATCH_OR_TERM);
288
289 LIST_FOREACH(matches, l4, l3->matches) {
290 assert(l4->type == MATCH_DISCRETE);
291
292 /* Exactly the same match already? Then ignore
293 * this addition */
294 if (l4->le_hash == le_hash &&
295 l4->size == size &&
296 memcmp(l4->data, data, size) == 0)
297 return 0;
298
299 /* Same field? Then let's add this to this OR term */
300 if (same_field(data, size, l4->data, l4->size)) {
301 add_here = l3;
302 break;
303 }
304 }
305
306 if (add_here)
307 break;
308 }
309
310 if (!add_here) {
311 add_here = match_new(j->level2, MATCH_OR_TERM);
312 if (!add_here)
313 goto fail;
314 }
315
316 m = match_new(add_here, MATCH_DISCRETE);
317 if (!m)
318 goto fail;
319
320 m->le_hash = le_hash;
321 m->size = size;
322 m->data = memdup(data, size);
323 if (!m->data)
324 goto fail;
325
326 detach_location(j);
327
328 return 0;
329
330 fail:
331 match_free_if_empty(add_here);
332 match_free_if_empty(j->level2);
333 match_free_if_empty(j->level1);
334 match_free_if_empty(j->level0);
335
336 return -ENOMEM;
337 }
338
339 _public_ int sd_journal_add_conjunction(sd_journal *j) {
340 assert_return(j, -EINVAL);
341 assert_return(!journal_pid_changed(j), -ECHILD);
342
343 if (!j->level0)
344 return 0;
345
346 if (!j->level1)
347 return 0;
348
349 if (!j->level1->matches)
350 return 0;
351
352 j->level1 = NULL;
353 j->level2 = NULL;
354
355 return 0;
356 }
357
358 _public_ int sd_journal_add_disjunction(sd_journal *j) {
359 assert_return(j, -EINVAL);
360 assert_return(!journal_pid_changed(j), -ECHILD);
361
362 if (!j->level0)
363 return 0;
364
365 if (!j->level1)
366 return 0;
367
368 if (!j->level2)
369 return 0;
370
371 if (!j->level2->matches)
372 return 0;
373
374 j->level2 = NULL;
375 return 0;
376 }
377
378 static char *match_make_string(Match *m) {
379 char *p = NULL, *r;
380 Match *i;
381 bool enclose = false;
382
383 if (!m)
384 return strdup("none");
385
386 if (m->type == MATCH_DISCRETE)
387 return cescape_length(m->data, m->size);
388
389 LIST_FOREACH(matches, i, m->matches) {
390 char *t, *k;
391
392 t = match_make_string(i);
393 if (!t)
394 return mfree(p);
395
396 if (p) {
397 k = strjoin(p, m->type == MATCH_OR_TERM ? " OR " : " AND ", t);
398 free(p);
399 free(t);
400
401 if (!k)
402 return NULL;
403
404 p = k;
405
406 enclose = true;
407 } else
408 p = t;
409 }
410
411 if (enclose) {
412 r = strjoin("(", p, ")");
413 free(p);
414 return r;
415 }
416
417 return p;
418 }
419
420 char *journal_make_match_string(sd_journal *j) {
421 assert(j);
422
423 return match_make_string(j->level0);
424 }
425
426 _public_ void sd_journal_flush_matches(sd_journal *j) {
427 if (!j)
428 return;
429
430 if (j->level0)
431 match_free(j->level0);
432
433 j->level0 = j->level1 = j->level2 = NULL;
434
435 detach_location(j);
436 }
437
438 _pure_ static int compare_with_location(const JournalFile *f, const Location *l, const JournalFile *current_file) {
439 int r;
440
441 assert(f);
442 assert(l);
443 assert(current_file);
444 assert(f->location_type == LOCATION_SEEK);
445 assert(IN_SET(l->type, LOCATION_DISCRETE, LOCATION_SEEK));
446
447 if (l->monotonic_set &&
448 sd_id128_equal(f->current_boot_id, l->boot_id) &&
449 l->realtime_set &&
450 f->current_realtime == l->realtime &&
451 l->xor_hash_set &&
452 f->current_xor_hash == l->xor_hash &&
453 f != current_file)
454 return 0;
455
456 if (l->seqnum_set &&
457 sd_id128_equal(f->header->seqnum_id, l->seqnum_id)) {
458
459 r = CMP(f->current_seqnum, l->seqnum);
460 if (r != 0)
461 return r;
462 }
463
464 if (l->monotonic_set &&
465 sd_id128_equal(f->current_boot_id, l->boot_id)) {
466
467 r = CMP(f->current_monotonic, l->monotonic);
468 if (r != 0)
469 return r;
470 }
471
472 if (l->realtime_set) {
473
474 r = CMP(f->current_realtime, l->realtime);
475 if (r != 0)
476 return r;
477 }
478
479 if (l->xor_hash_set) {
480
481 r = CMP(f->current_xor_hash, l->xor_hash);
482 if (r != 0)
483 return r;
484 }
485
486 return 0;
487 }
488
489 static int next_for_match(
490 sd_journal *j,
491 Match *m,
492 JournalFile *f,
493 uint64_t after_offset,
494 direction_t direction,
495 Object **ret,
496 uint64_t *offset) {
497
498 int r;
499 uint64_t np = 0;
500 Object *n;
501
502 assert(j);
503 assert(m);
504 assert(f);
505
506 if (m->type == MATCH_DISCRETE) {
507 uint64_t dp;
508
509 r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
510 if (r <= 0)
511 return r;
512
513 return journal_file_move_to_entry_by_offset_for_data(f, dp, after_offset, direction, ret, offset);
514
515 } else if (m->type == MATCH_OR_TERM) {
516 Match *i;
517
518 /* Find the earliest match beyond after_offset */
519
520 LIST_FOREACH(matches, i, m->matches) {
521 uint64_t cp;
522
523 r = next_for_match(j, i, f, after_offset, direction, NULL, &cp);
524 if (r < 0)
525 return r;
526 else if (r > 0) {
527 if (np == 0 || (direction == DIRECTION_DOWN ? cp < np : cp > np))
528 np = cp;
529 }
530 }
531
532 if (np == 0)
533 return 0;
534
535 } else if (m->type == MATCH_AND_TERM) {
536 Match *i, *last_moved;
537
538 /* Always jump to the next matching entry and repeat
539 * this until we find an offset that matches for all
540 * matches. */
541
542 if (!m->matches)
543 return 0;
544
545 r = next_for_match(j, m->matches, f, after_offset, direction, NULL, &np);
546 if (r <= 0)
547 return r;
548
549 assert(direction == DIRECTION_DOWN ? np >= after_offset : np <= after_offset);
550 last_moved = m->matches;
551
552 LIST_LOOP_BUT_ONE(matches, i, m->matches, last_moved) {
553 uint64_t cp;
554
555 r = next_for_match(j, i, f, np, direction, NULL, &cp);
556 if (r <= 0)
557 return r;
558
559 assert(direction == DIRECTION_DOWN ? cp >= np : cp <= np);
560 if (direction == DIRECTION_DOWN ? cp > np : cp < np) {
561 np = cp;
562 last_moved = i;
563 }
564 }
565 }
566
567 assert(np > 0);
568
569 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
570 if (r < 0)
571 return r;
572
573 if (ret)
574 *ret = n;
575 if (offset)
576 *offset = np;
577
578 return 1;
579 }
580
581 static int find_location_for_match(
582 sd_journal *j,
583 Match *m,
584 JournalFile *f,
585 direction_t direction,
586 Object **ret,
587 uint64_t *offset) {
588
589 int r;
590
591 assert(j);
592 assert(m);
593 assert(f);
594
595 if (m->type == MATCH_DISCRETE) {
596 uint64_t dp;
597
598 r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
599 if (r <= 0)
600 return r;
601
602 /* FIXME: missing: find by monotonic */
603
604 if (j->current_location.type == LOCATION_HEAD)
605 return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, ret, offset);
606 if (j->current_location.type == LOCATION_TAIL)
607 return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, ret, offset);
608 if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
609 return journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, ret, offset);
610 if (j->current_location.monotonic_set) {
611 r = journal_file_move_to_entry_by_monotonic_for_data(f, dp, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
612 if (r != -ENOENT)
613 return r;
614 }
615 if (j->current_location.realtime_set)
616 return journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, ret, offset);
617
618 return journal_file_next_entry_for_data(f, NULL, 0, dp, direction, ret, offset);
619
620 } else if (m->type == MATCH_OR_TERM) {
621 uint64_t np = 0;
622 Object *n;
623 Match *i;
624
625 /* Find the earliest match */
626
627 LIST_FOREACH(matches, i, m->matches) {
628 uint64_t cp;
629
630 r = find_location_for_match(j, i, f, direction, NULL, &cp);
631 if (r < 0)
632 return r;
633 else if (r > 0) {
634 if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp))
635 np = cp;
636 }
637 }
638
639 if (np == 0)
640 return 0;
641
642 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
643 if (r < 0)
644 return r;
645
646 if (ret)
647 *ret = n;
648 if (offset)
649 *offset = np;
650
651 return 1;
652
653 } else {
654 Match *i;
655 uint64_t np = 0;
656
657 assert(m->type == MATCH_AND_TERM);
658
659 /* First jump to the last match, and then find the
660 * next one where all matches match */
661
662 if (!m->matches)
663 return 0;
664
665 LIST_FOREACH(matches, i, m->matches) {
666 uint64_t cp;
667
668 r = find_location_for_match(j, i, f, direction, NULL, &cp);
669 if (r <= 0)
670 return r;
671
672 if (np == 0 || (direction == DIRECTION_DOWN ? cp > np : cp < np))
673 np = cp;
674 }
675
676 return next_for_match(j, m, f, np, direction, ret, offset);
677 }
678 }
679
680 static int find_location_with_matches(
681 sd_journal *j,
682 JournalFile *f,
683 direction_t direction,
684 Object **ret,
685 uint64_t *offset) {
686
687 int r;
688
689 assert(j);
690 assert(f);
691 assert(ret);
692 assert(offset);
693
694 if (!j->level0) {
695 /* No matches is simple */
696
697 if (j->current_location.type == LOCATION_HEAD)
698 return journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, offset);
699 if (j->current_location.type == LOCATION_TAIL)
700 return journal_file_next_entry(f, 0, DIRECTION_UP, ret, offset);
701 if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
702 return journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, ret, offset);
703 if (j->current_location.monotonic_set) {
704 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
705 if (r != -ENOENT)
706 return r;
707 }
708 if (j->current_location.realtime_set)
709 return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, offset);
710
711 return journal_file_next_entry(f, 0, direction, ret, offset);
712 } else
713 return find_location_for_match(j, j->level0, f, direction, ret, offset);
714 }
715
716 static int next_with_matches(
717 sd_journal *j,
718 JournalFile *f,
719 direction_t direction,
720 Object **ret,
721 uint64_t *offset) {
722
723 assert(j);
724 assert(f);
725 assert(ret);
726 assert(offset);
727
728 /* No matches is easy. We simple advance the file
729 * pointer by one. */
730 if (!j->level0)
731 return journal_file_next_entry(f, f->current_offset, direction, ret, offset);
732
733 /* If we have a match then we look for the next matching entry
734 * with an offset at least one step larger */
735 return next_for_match(j, j->level0, f,
736 direction == DIRECTION_DOWN ? f->current_offset + 1
737 : f->current_offset - 1,
738 direction, ret, offset);
739 }
740
741 static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction) {
742 Object *c;
743 uint64_t cp, n_entries;
744 int r;
745
746 assert(j);
747 assert(f);
748
749 n_entries = le64toh(f->header->n_entries);
750
751 /* If we hit EOF before, we don't need to look into this file again
752 * unless direction changed or new entries appeared. */
753 if (f->last_direction == direction && f->location_type == LOCATION_TAIL &&
754 n_entries == f->last_n_entries)
755 return 0;
756
757 f->last_n_entries = n_entries;
758
759 if (f->last_direction == direction && f->current_offset > 0) {
760 /* LOCATION_SEEK here means we did the work in a previous
761 * iteration and the current location already points to a
762 * candidate entry. */
763 if (f->location_type != LOCATION_SEEK) {
764 r = next_with_matches(j, f, direction, &c, &cp);
765 if (r <= 0)
766 return r;
767
768 journal_file_save_location(f, c, cp);
769 }
770 } else {
771 f->last_direction = direction;
772
773 r = find_location_with_matches(j, f, direction, &c, &cp);
774 if (r <= 0)
775 return r;
776
777 journal_file_save_location(f, c, cp);
778 }
779
780 /* OK, we found the spot, now let's advance until an entry
781 * that is actually different from what we were previously
782 * looking at. This is necessary to handle entries which exist
783 * in two (or more) journal files, and which shall all be
784 * suppressed but one. */
785
786 for (;;) {
787 bool found;
788
789 if (j->current_location.type == LOCATION_DISCRETE) {
790 int k;
791
792 k = compare_with_location(f, &j->current_location, j->current_file);
793
794 found = direction == DIRECTION_DOWN ? k > 0 : k < 0;
795 } else
796 found = true;
797
798 if (found)
799 return 1;
800
801 r = next_with_matches(j, f, direction, &c, &cp);
802 if (r <= 0)
803 return r;
804
805 journal_file_save_location(f, c, cp);
806 }
807 }
808
809 static int real_journal_next(sd_journal *j, direction_t direction) {
810 JournalFile *new_file = NULL;
811 unsigned i, n_files;
812 const void **files;
813 Object *o;
814 int r;
815
816 assert_return(j, -EINVAL);
817 assert_return(!journal_pid_changed(j), -ECHILD);
818
819 r = iterated_cache_get(j->files_cache, NULL, &files, &n_files);
820 if (r < 0)
821 return r;
822
823 for (i = 0; i < n_files; i++) {
824 JournalFile *f = (JournalFile *)files[i];
825 bool found;
826
827 r = next_beyond_location(j, f, direction);
828 if (r < 0) {
829 log_debug_errno(r, "Can't iterate through %s, ignoring: %m", f->path);
830 remove_file_real(j, f);
831 continue;
832 } else if (r == 0) {
833 f->location_type = LOCATION_TAIL;
834 continue;
835 }
836
837 if (!new_file)
838 found = true;
839 else {
840 int k;
841
842 k = journal_file_compare_locations(f, new_file);
843
844 found = direction == DIRECTION_DOWN ? k < 0 : k > 0;
845 }
846
847 if (found)
848 new_file = f;
849 }
850
851 if (!new_file)
852 return 0;
853
854 r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_file->current_offset, &o);
855 if (r < 0)
856 return r;
857
858 set_location(j, new_file, o);
859
860 return 1;
861 }
862
863 _public_ int sd_journal_next(sd_journal *j) {
864 return real_journal_next(j, DIRECTION_DOWN);
865 }
866
867 _public_ int sd_journal_previous(sd_journal *j) {
868 return real_journal_next(j, DIRECTION_UP);
869 }
870
871 static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
872 int c = 0, r;
873
874 assert_return(j, -EINVAL);
875 assert_return(!journal_pid_changed(j), -ECHILD);
876
877 if (skip == 0) {
878 /* If this is not a discrete skip, then at least
879 * resolve the current location */
880 if (j->current_location.type != LOCATION_DISCRETE) {
881 r = real_journal_next(j, direction);
882 if (r < 0)
883 return r;
884 }
885
886 return 0;
887 }
888
889 do {
890 r = real_journal_next(j, direction);
891 if (r < 0)
892 return r;
893
894 if (r == 0)
895 return c;
896
897 skip--;
898 c++;
899 } while (skip > 0);
900
901 return c;
902 }
903
904 _public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
905 return real_journal_next_skip(j, DIRECTION_DOWN, skip);
906 }
907
908 _public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
909 return real_journal_next_skip(j, DIRECTION_UP, skip);
910 }
911
912 _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
913 Object *o;
914 int r;
915 char bid[SD_ID128_STRING_MAX], sid[SD_ID128_STRING_MAX];
916
917 assert_return(j, -EINVAL);
918 assert_return(!journal_pid_changed(j), -ECHILD);
919 assert_return(cursor, -EINVAL);
920
921 if (!j->current_file || j->current_file->current_offset <= 0)
922 return -EADDRNOTAVAIL;
923
924 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
925 if (r < 0)
926 return r;
927
928 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
929 sd_id128_to_string(o->entry.boot_id, bid);
930
931 if (asprintf(cursor,
932 "s=%s;i=%"PRIx64";b=%s;m=%"PRIx64";t=%"PRIx64";x=%"PRIx64,
933 sid, le64toh(o->entry.seqnum),
934 bid, le64toh(o->entry.monotonic),
935 le64toh(o->entry.realtime),
936 le64toh(o->entry.xor_hash)) < 0)
937 return -ENOMEM;
938
939 return 0;
940 }
941
942 _public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
943 const char *word, *state;
944 size_t l;
945 unsigned long long seqnum, monotonic, realtime, xor_hash;
946 bool
947 seqnum_id_set = false,
948 seqnum_set = false,
949 boot_id_set = false,
950 monotonic_set = false,
951 realtime_set = false,
952 xor_hash_set = false;
953 sd_id128_t seqnum_id, boot_id;
954
955 assert_return(j, -EINVAL);
956 assert_return(!journal_pid_changed(j), -ECHILD);
957 assert_return(!isempty(cursor), -EINVAL);
958
959 FOREACH_WORD_SEPARATOR(word, l, cursor, ";", state) {
960 char *item;
961 int k = 0;
962
963 if (l < 2 || word[1] != '=')
964 return -EINVAL;
965
966 item = strndup(word, l);
967 if (!item)
968 return -ENOMEM;
969
970 switch (word[0]) {
971
972 case 's':
973 seqnum_id_set = true;
974 k = sd_id128_from_string(item+2, &seqnum_id);
975 break;
976
977 case 'i':
978 seqnum_set = true;
979 if (sscanf(item+2, "%llx", &seqnum) != 1)
980 k = -EINVAL;
981 break;
982
983 case 'b':
984 boot_id_set = true;
985 k = sd_id128_from_string(item+2, &boot_id);
986 break;
987
988 case 'm':
989 monotonic_set = true;
990 if (sscanf(item+2, "%llx", &monotonic) != 1)
991 k = -EINVAL;
992 break;
993
994 case 't':
995 realtime_set = true;
996 if (sscanf(item+2, "%llx", &realtime) != 1)
997 k = -EINVAL;
998 break;
999
1000 case 'x':
1001 xor_hash_set = true;
1002 if (sscanf(item+2, "%llx", &xor_hash) != 1)
1003 k = -EINVAL;
1004 break;
1005 }
1006
1007 free(item);
1008
1009 if (k < 0)
1010 return k;
1011 }
1012
1013 if ((!seqnum_set || !seqnum_id_set) &&
1014 (!monotonic_set || !boot_id_set) &&
1015 !realtime_set)
1016 return -EINVAL;
1017
1018 reset_location(j);
1019
1020 j->current_location.type = LOCATION_SEEK;
1021
1022 if (realtime_set) {
1023 j->current_location.realtime = (uint64_t) realtime;
1024 j->current_location.realtime_set = true;
1025 }
1026
1027 if (seqnum_set && seqnum_id_set) {
1028 j->current_location.seqnum = (uint64_t) seqnum;
1029 j->current_location.seqnum_id = seqnum_id;
1030 j->current_location.seqnum_set = true;
1031 }
1032
1033 if (monotonic_set && boot_id_set) {
1034 j->current_location.monotonic = (uint64_t) monotonic;
1035 j->current_location.boot_id = boot_id;
1036 j->current_location.monotonic_set = true;
1037 }
1038
1039 if (xor_hash_set) {
1040 j->current_location.xor_hash = (uint64_t) xor_hash;
1041 j->current_location.xor_hash_set = true;
1042 }
1043
1044 return 0;
1045 }
1046
1047 _public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
1048 int r;
1049 Object *o;
1050
1051 assert_return(j, -EINVAL);
1052 assert_return(!journal_pid_changed(j), -ECHILD);
1053 assert_return(!isempty(cursor), -EINVAL);
1054
1055 if (!j->current_file || j->current_file->current_offset <= 0)
1056 return -EADDRNOTAVAIL;
1057
1058 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
1059 if (r < 0)
1060 return r;
1061
1062 for (;;) {
1063 _cleanup_free_ char *item = NULL;
1064 unsigned long long ll;
1065 sd_id128_t id;
1066 int k = 0;
1067
1068 r = extract_first_word(&cursor, &item, ";", EXTRACT_DONT_COALESCE_SEPARATORS);
1069 if (r < 0)
1070 return r;
1071
1072 if (r == 0)
1073 break;
1074
1075 if (strlen(item) < 2 || item[1] != '=')
1076 return -EINVAL;
1077
1078 switch (item[0]) {
1079
1080 case 's':
1081 k = sd_id128_from_string(item+2, &id);
1082 if (k < 0)
1083 return k;
1084 if (!sd_id128_equal(id, j->current_file->header->seqnum_id))
1085 return 0;
1086 break;
1087
1088 case 'i':
1089 if (sscanf(item+2, "%llx", &ll) != 1)
1090 return -EINVAL;
1091 if (ll != le64toh(o->entry.seqnum))
1092 return 0;
1093 break;
1094
1095 case 'b':
1096 k = sd_id128_from_string(item+2, &id);
1097 if (k < 0)
1098 return k;
1099 if (!sd_id128_equal(id, o->entry.boot_id))
1100 return 0;
1101 break;
1102
1103 case 'm':
1104 if (sscanf(item+2, "%llx", &ll) != 1)
1105 return -EINVAL;
1106 if (ll != le64toh(o->entry.monotonic))
1107 return 0;
1108 break;
1109
1110 case 't':
1111 if (sscanf(item+2, "%llx", &ll) != 1)
1112 return -EINVAL;
1113 if (ll != le64toh(o->entry.realtime))
1114 return 0;
1115 break;
1116
1117 case 'x':
1118 if (sscanf(item+2, "%llx", &ll) != 1)
1119 return -EINVAL;
1120 if (ll != le64toh(o->entry.xor_hash))
1121 return 0;
1122 break;
1123 }
1124 }
1125
1126 return 1;
1127 }
1128
1129 _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
1130 assert_return(j, -EINVAL);
1131 assert_return(!journal_pid_changed(j), -ECHILD);
1132
1133 reset_location(j);
1134 j->current_location.type = LOCATION_SEEK;
1135 j->current_location.boot_id = boot_id;
1136 j->current_location.monotonic = usec;
1137 j->current_location.monotonic_set = true;
1138
1139 return 0;
1140 }
1141
1142 _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
1143 assert_return(j, -EINVAL);
1144 assert_return(!journal_pid_changed(j), -ECHILD);
1145
1146 reset_location(j);
1147 j->current_location.type = LOCATION_SEEK;
1148 j->current_location.realtime = usec;
1149 j->current_location.realtime_set = true;
1150
1151 return 0;
1152 }
1153
1154 _public_ int sd_journal_seek_head(sd_journal *j) {
1155 assert_return(j, -EINVAL);
1156 assert_return(!journal_pid_changed(j), -ECHILD);
1157
1158 reset_location(j);
1159 j->current_location.type = LOCATION_HEAD;
1160
1161 return 0;
1162 }
1163
1164 _public_ int sd_journal_seek_tail(sd_journal *j) {
1165 assert_return(j, -EINVAL);
1166 assert_return(!journal_pid_changed(j), -ECHILD);
1167
1168 reset_location(j);
1169 j->current_location.type = LOCATION_TAIL;
1170
1171 return 0;
1172 }
1173
1174 static void check_network(sd_journal *j, int fd) {
1175 assert(j);
1176
1177 if (j->on_network)
1178 return;
1179
1180 j->on_network = fd_is_network_fs(fd);
1181 }
1182
1183 static bool file_has_type_prefix(const char *prefix, const char *filename) {
1184 const char *full, *tilded, *atted;
1185
1186 full = strjoina(prefix, ".journal");
1187 tilded = strjoina(full, "~");
1188 atted = strjoina(prefix, "@");
1189
1190 return STR_IN_SET(filename, full, tilded) ||
1191 startswith(filename, atted);
1192 }
1193
1194 static bool file_type_wanted(int flags, const char *filename) {
1195 assert(filename);
1196
1197 if (!endswith(filename, ".journal") && !endswith(filename, ".journal~"))
1198 return false;
1199
1200 /* no flags set → every type is OK */
1201 if (!(flags & (SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)))
1202 return true;
1203
1204 if (flags & SD_JOURNAL_SYSTEM && file_has_type_prefix("system", filename))
1205 return true;
1206
1207 if (flags & SD_JOURNAL_CURRENT_USER) {
1208 char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1];
1209
1210 xsprintf(prefix, "user-"UID_FMT, getuid());
1211
1212 if (file_has_type_prefix(prefix, filename))
1213 return true;
1214 }
1215
1216 return false;
1217 }
1218
1219 static bool path_has_prefix(sd_journal *j, const char *path, const char *prefix) {
1220 assert(j);
1221 assert(path);
1222 assert(prefix);
1223
1224 if (j->toplevel_fd >= 0)
1225 return false;
1226
1227 return path_startswith(path, prefix);
1228 }
1229
1230 static void track_file_disposition(sd_journal *j, JournalFile *f) {
1231 assert(j);
1232 assert(f);
1233
1234 if (!j->has_runtime_files && path_has_prefix(j, f->path, "/run"))
1235 j->has_runtime_files = true;
1236 else if (!j->has_persistent_files && path_has_prefix(j, f->path, "/var"))
1237 j->has_persistent_files = true;
1238 }
1239
1240 static const char *skip_slash(const char *p) {
1241
1242 if (!p)
1243 return NULL;
1244
1245 while (*p == '/')
1246 p++;
1247
1248 return p;
1249 }
1250
1251 static int add_any_file(
1252 sd_journal *j,
1253 int fd,
1254 const char *path) {
1255
1256 bool close_fd = false;
1257 JournalFile *f;
1258 struct stat st;
1259 int r, k;
1260
1261 assert(j);
1262 assert(fd >= 0 || path);
1263
1264 if (fd < 0) {
1265 if (j->toplevel_fd >= 0)
1266 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1267 * openat() ignores the first argument. */
1268
1269 fd = openat(j->toplevel_fd, skip_slash(path), O_RDONLY|O_CLOEXEC|O_NONBLOCK);
1270 else
1271 fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
1272 if (fd < 0) {
1273 r = log_debug_errno(errno, "Failed to open journal file %s: %m", path);
1274 goto finish;
1275 }
1276
1277 close_fd = true;
1278
1279 r = fd_nonblock(fd, false);
1280 if (r < 0) {
1281 r = log_debug_errno(errno, "Failed to turn off O_NONBLOCK for %s: %m", path);
1282 goto finish;
1283 }
1284 }
1285
1286 if (fstat(fd, &st) < 0) {
1287 r = log_debug_errno(errno, "Failed to fstat file '%s': %m", path);
1288 goto finish;
1289 }
1290
1291 r = stat_verify_regular(&st);
1292 if (r < 0) {
1293 log_debug_errno(r, "Refusing to open '%s', as it is not a regular file.", path);
1294 goto finish;
1295 }
1296
1297 f = ordered_hashmap_get(j->files, path);
1298 if (f) {
1299 if (f->last_stat.st_dev == st.st_dev &&
1300 f->last_stat.st_ino == st.st_ino) {
1301
1302 /* We already track this file, under the same path and with the same device/inode numbers, it's
1303 * hence really the same. Mark this file as seen in this generation. This is used to GC old
1304 * files in process_q_overflow() to detect journal files that are still there and discern them
1305 * from those which are gone. */
1306
1307 f->last_seen_generation = j->generation;
1308 r = 0;
1309 goto finish;
1310 }
1311
1312 /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
1313 * replaced (probably due to rotation?), let's drop it hence from our list. */
1314 remove_file_real(j, f);
1315 f = NULL;
1316 }
1317
1318 if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
1319 log_debug("Too many open journal files, not adding %s.", path);
1320 r = -ETOOMANYREFS;
1321 goto finish;
1322 }
1323
1324 r = journal_file_open(fd, path, O_RDONLY, 0, false, 0, false, NULL, j->mmap, NULL, NULL, &f);
1325 if (r < 0) {
1326 log_debug_errno(r, "Failed to open journal file %s: %m", path);
1327 goto finish;
1328 }
1329
1330 /* journal_file_dump(f); */
1331
1332 r = ordered_hashmap_put(j->files, f->path, f);
1333 if (r < 0) {
1334 f->close_fd = false; /* make sure journal_file_close() doesn't close the caller's fd (or our own). We'll let the caller do that, or ourselves */
1335 (void) journal_file_close(f);
1336 goto finish;
1337 }
1338
1339 close_fd = false; /* the fd is now owned by the JournalFile object */
1340
1341 f->last_seen_generation = j->generation;
1342
1343 track_file_disposition(j, f);
1344 check_network(j, f->fd);
1345
1346 j->current_invalidate_counter++;
1347
1348 log_debug("File %s added.", f->path);
1349
1350 r = 0;
1351
1352 finish:
1353 if (close_fd)
1354 safe_close(fd);
1355
1356 if (r < 0) {
1357 k = journal_put_error(j, r, path);
1358 if (k < 0)
1359 return k;
1360 }
1361
1362 return r;
1363 }
1364
1365 static int add_file_by_name(
1366 sd_journal *j,
1367 const char *prefix,
1368 const char *filename) {
1369
1370 const char *path;
1371
1372 assert(j);
1373 assert(prefix);
1374 assert(filename);
1375
1376 if (j->no_new_files)
1377 return 0;
1378
1379 if (!file_type_wanted(j->flags, filename))
1380 return 0;
1381
1382 path = prefix_roota(prefix, filename);
1383 return add_any_file(j, -1, path);
1384 }
1385
1386 static void remove_file_by_name(
1387 sd_journal *j,
1388 const char *prefix,
1389 const char *filename) {
1390
1391 const char *path;
1392 JournalFile *f;
1393
1394 assert(j);
1395 assert(prefix);
1396 assert(filename);
1397
1398 path = prefix_roota(prefix, filename);
1399 f = ordered_hashmap_get(j->files, path);
1400 if (!f)
1401 return;
1402
1403 remove_file_real(j, f);
1404 }
1405
1406 static void remove_file_real(sd_journal *j, JournalFile *f) {
1407 assert(j);
1408 assert(f);
1409
1410 (void) ordered_hashmap_remove(j->files, f->path);
1411
1412 log_debug("File %s removed.", f->path);
1413
1414 if (j->current_file == f) {
1415 j->current_file = NULL;
1416 j->current_field = 0;
1417 }
1418
1419 if (j->unique_file == f) {
1420 /* Jump to the next unique_file or NULL if that one was last */
1421 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
1422 j->unique_offset = 0;
1423 if (!j->unique_file)
1424 j->unique_file_lost = true;
1425 }
1426
1427 if (j->fields_file == f) {
1428 j->fields_file = ordered_hashmap_next(j->files, j->fields_file->path);
1429 j->fields_offset = 0;
1430 if (!j->fields_file)
1431 j->fields_file_lost = true;
1432 }
1433
1434 (void) journal_file_close(f);
1435
1436 j->current_invalidate_counter++;
1437 }
1438
1439 static int dirname_is_machine_id(const char *fn) {
1440 sd_id128_t id, machine;
1441 const char *e;
1442 int r;
1443
1444 /* Returns true if the specified directory name matches the local machine ID */
1445
1446 r = sd_id128_get_machine(&machine);
1447 if (r < 0)
1448 return r;
1449
1450 e = strchr(fn, '.');
1451 if (e) {
1452 const char *k;
1453
1454 /* Looks like it has a namespace suffix. Verify that. */
1455 if (!log_namespace_name_valid(e + 1))
1456 return false;
1457
1458 k = strndupa(fn, e - fn);
1459 r = sd_id128_from_string(k, &id);
1460 } else
1461 r = sd_id128_from_string(fn, &id);
1462 if (r < 0)
1463 return r;
1464
1465 return sd_id128_equal(id, machine);
1466 }
1467
1468 static int dirname_has_namespace(const char *fn, const char *namespace) {
1469 const char *e;
1470
1471 /* Returns true if the specified directory name matches the specified namespace */
1472
1473 e = strchr(fn, '.');
1474 if (e) {
1475 const char *k;
1476
1477 if (!namespace)
1478 return false;
1479
1480 if (!streq(e + 1, namespace))
1481 return false;
1482
1483 k = strndupa(fn, e - fn);
1484 return id128_is_valid(k);
1485 }
1486
1487 if (namespace)
1488 return false;
1489
1490 return id128_is_valid(fn);
1491 }
1492
1493 static bool dirent_is_journal_file(const struct dirent *de) {
1494 assert(de);
1495
1496 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1497
1498 if (!IN_SET(de->d_type, DT_REG, DT_LNK, DT_UNKNOWN))
1499 return false;
1500
1501 return endswith(de->d_name, ".journal") ||
1502 endswith(de->d_name, ".journal~");
1503 }
1504
1505 static bool dirent_is_journal_subdir(const struct dirent *de) {
1506 const char *e, *n;
1507 assert(de);
1508
1509 /* returns true if the specified directory entry looks like a directory that might contain journal
1510 * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a
1511 * namespace. */
1512
1513 if (!IN_SET(de->d_type, DT_DIR, DT_LNK, DT_UNKNOWN))
1514 return false;
1515
1516 e = strchr(de->d_name, '.');
1517 if (!e)
1518 return id128_is_valid(de->d_name); /* No namespace */
1519
1520 n = strndupa(de->d_name, e - de->d_name);
1521 if (!id128_is_valid(n))
1522 return false;
1523
1524 return log_namespace_name_valid(e + 1);
1525 }
1526
1527 static int directory_open(sd_journal *j, const char *path, DIR **ret) {
1528 DIR *d;
1529
1530 assert(j);
1531 assert(path);
1532 assert(ret);
1533
1534 if (j->toplevel_fd < 0)
1535 d = opendir(path);
1536 else
1537 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1538 * relative, by dropping the initial slash */
1539 d = xopendirat(j->toplevel_fd, skip_slash(path), 0);
1540 if (!d)
1541 return -errno;
1542
1543 *ret = d;
1544 return 0;
1545 }
1546
1547 static int add_directory(sd_journal *j, const char *prefix, const char *dirname);
1548
1549 static void directory_enumerate(sd_journal *j, Directory *m, DIR *d) {
1550 struct dirent *de;
1551
1552 assert(j);
1553 assert(m);
1554 assert(d);
1555
1556 FOREACH_DIRENT_ALL(de, d, goto fail) {
1557
1558 if (dirent_is_journal_file(de))
1559 (void) add_file_by_name(j, m->path, de->d_name);
1560
1561 if (m->is_root && dirent_is_journal_subdir(de))
1562 (void) add_directory(j, m->path, de->d_name);
1563 }
1564
1565 return;
1566
1567 fail:
1568 log_debug_errno(errno, "Failed to enumerate directory %s, ignoring: %m", m->path);
1569 }
1570
1571 static void directory_watch(sd_journal *j, Directory *m, int fd, uint32_t mask) {
1572 int r;
1573
1574 assert(j);
1575 assert(m);
1576 assert(fd >= 0);
1577
1578 /* Watch this directory if that's enabled and if it not being watched yet. */
1579
1580 if (m->wd > 0) /* Already have a watch? */
1581 return;
1582 if (j->inotify_fd < 0) /* Not watching at all? */
1583 return;
1584
1585 m->wd = inotify_add_watch_fd(j->inotify_fd, fd, mask);
1586 if (m->wd < 0) {
1587 log_debug_errno(errno, "Failed to watch journal directory '%s', ignoring: %m", m->path);
1588 return;
1589 }
1590
1591 r = hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m);
1592 if (r == -EEXIST)
1593 log_debug_errno(r, "Directory '%s' already being watched under a different path, ignoring: %m", m->path);
1594 if (r < 0) {
1595 log_debug_errno(r, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m->path);
1596 (void) inotify_rm_watch(j->inotify_fd, m->wd);
1597 m->wd = -1;
1598 }
1599 }
1600
1601 static int add_directory(
1602 sd_journal *j,
1603 const char *prefix,
1604 const char *dirname) {
1605
1606 _cleanup_free_ char *path = NULL;
1607 _cleanup_closedir_ DIR *d = NULL;
1608 Directory *m;
1609 int r, k;
1610
1611 assert(j);
1612 assert(prefix);
1613
1614 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1615 * and reenumerates directory contents */
1616
1617 path = path_join(prefix, dirname);
1618 if (!path) {
1619 r = -ENOMEM;
1620 goto fail;
1621 }
1622
1623 log_debug("Considering directory '%s'.", path);
1624
1625 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1626 if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
1627 !((dirname && dirname_is_machine_id(dirname) > 0) || path_has_prefix(j, path, "/run")))
1628 return 0;
1629
1630 if (!(FLAGS_SET(j->flags, SD_JOURNAL_ALL_NAMESPACES) ||
1631 dirname_has_namespace(dirname, j->namespace) > 0 ||
1632 (FLAGS_SET(j->flags, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE) && dirname_has_namespace(dirname, NULL) > 0)))
1633 return 0;
1634
1635 r = directory_open(j, path, &d);
1636 if (r < 0) {
1637 log_debug_errno(r, "Failed to open directory '%s': %m", path);
1638 goto fail;
1639 }
1640
1641 m = hashmap_get(j->directories_by_path, path);
1642 if (!m) {
1643 m = new(Directory, 1);
1644 if (!m) {
1645 r = -ENOMEM;
1646 goto fail;
1647 }
1648
1649 *m = (Directory) {
1650 .is_root = false,
1651 .path = path,
1652 };
1653
1654 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
1655 free(m);
1656 r = -ENOMEM;
1657 goto fail;
1658 }
1659
1660 path = NULL; /* avoid freeing in cleanup */
1661 j->current_invalidate_counter++;
1662
1663 log_debug("Directory %s added.", m->path);
1664
1665 } else if (m->is_root)
1666 return 0; /* Don't 'downgrade' from root directory */
1667
1668 m->last_seen_generation = j->generation;
1669
1670 directory_watch(j, m, dirfd(d),
1671 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1672 IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_MOVED_FROM|
1673 IN_ONLYDIR);
1674
1675 if (!j->no_new_files)
1676 directory_enumerate(j, m, d);
1677
1678 check_network(j, dirfd(d));
1679
1680 return 0;
1681
1682 fail:
1683 k = journal_put_error(j, r, path ?: prefix);
1684 if (k < 0)
1685 return k;
1686
1687 return r;
1688 }
1689
1690 static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) {
1691
1692 _cleanup_closedir_ DIR *d = NULL;
1693 Directory *m;
1694 int r, k;
1695
1696 assert(j);
1697
1698 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1699 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1700 * populate the set, as well as to update it later. */
1701
1702 if (p) {
1703 /* If there's a path specified, use it. */
1704
1705 log_debug("Considering root directory '%s'.", p);
1706
1707 if ((j->flags & SD_JOURNAL_RUNTIME_ONLY) &&
1708 !path_has_prefix(j, p, "/run"))
1709 return -EINVAL;
1710
1711 if (j->prefix)
1712 p = strjoina(j->prefix, p);
1713
1714 r = directory_open(j, p, &d);
1715 if (r == -ENOENT && missing_ok)
1716 return 0;
1717 if (r < 0) {
1718 log_debug_errno(r, "Failed to open root directory %s: %m", p);
1719 goto fail;
1720 }
1721 } else {
1722 int dfd;
1723
1724 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1725 * opendir() will take possession of the fd, and close it, which we don't want. */
1726
1727 p = "."; /* store this as "." in the directories hashmap */
1728
1729 dfd = fcntl(j->toplevel_fd, F_DUPFD_CLOEXEC, 3);
1730 if (dfd < 0) {
1731 r = -errno;
1732 goto fail;
1733 }
1734
1735 d = fdopendir(dfd);
1736 if (!d) {
1737 r = -errno;
1738 safe_close(dfd);
1739 goto fail;
1740 }
1741
1742 rewinddir(d);
1743 }
1744
1745 m = hashmap_get(j->directories_by_path, p);
1746 if (!m) {
1747 m = new0(Directory, 1);
1748 if (!m) {
1749 r = -ENOMEM;
1750 goto fail;
1751 }
1752
1753 m->is_root = true;
1754
1755 m->path = strdup(p);
1756 if (!m->path) {
1757 free(m);
1758 r = -ENOMEM;
1759 goto fail;
1760 }
1761
1762 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
1763 free(m->path);
1764 free(m);
1765 r = -ENOMEM;
1766 goto fail;
1767 }
1768
1769 j->current_invalidate_counter++;
1770
1771 log_debug("Root directory %s added.", m->path);
1772
1773 } else if (!m->is_root)
1774 return 0;
1775
1776 directory_watch(j, m, dirfd(d),
1777 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1778 IN_ONLYDIR);
1779
1780 if (!j->no_new_files)
1781 directory_enumerate(j, m, d);
1782
1783 check_network(j, dirfd(d));
1784
1785 return 0;
1786
1787 fail:
1788 k = journal_put_error(j, r, p);
1789 if (k < 0)
1790 return k;
1791
1792 return r;
1793 }
1794
1795 static void remove_directory(sd_journal *j, Directory *d) {
1796 assert(j);
1797
1798 if (d->wd > 0) {
1799 hashmap_remove(j->directories_by_wd, INT_TO_PTR(d->wd));
1800
1801 if (j->inotify_fd >= 0)
1802 (void) inotify_rm_watch(j->inotify_fd, d->wd);
1803 }
1804
1805 hashmap_remove(j->directories_by_path, d->path);
1806
1807 if (d->is_root)
1808 log_debug("Root directory %s removed.", d->path);
1809 else
1810 log_debug("Directory %s removed.", d->path);
1811
1812 free(d->path);
1813 free(d);
1814 }
1815
1816 static int add_search_paths(sd_journal *j) {
1817
1818 static const char search_paths[] =
1819 "/run/log/journal\0"
1820 "/var/log/journal\0";
1821 const char *p;
1822
1823 assert(j);
1824
1825 /* We ignore most errors here, since the idea is to only open
1826 * what's actually accessible, and ignore the rest. */
1827
1828 NULSTR_FOREACH(p, search_paths)
1829 (void) add_root_directory(j, p, true);
1830
1831 if (!(j->flags & SD_JOURNAL_LOCAL_ONLY))
1832 (void) add_root_directory(j, "/var/log/journal/remote", true);
1833
1834 return 0;
1835 }
1836
1837 static int add_current_paths(sd_journal *j) {
1838 Iterator i;
1839 JournalFile *f;
1840
1841 assert(j);
1842 assert(j->no_new_files);
1843
1844 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1845 * treat them as fatal. */
1846
1847 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
1848 _cleanup_free_ char *dir;
1849 int r;
1850
1851 dir = dirname_malloc(f->path);
1852 if (!dir)
1853 return -ENOMEM;
1854
1855 r = add_directory(j, dir, NULL);
1856 if (r < 0)
1857 return r;
1858 }
1859
1860 return 0;
1861 }
1862
1863 static int allocate_inotify(sd_journal *j) {
1864 assert(j);
1865
1866 if (j->inotify_fd < 0) {
1867 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1868 if (j->inotify_fd < 0)
1869 return -errno;
1870 }
1871
1872 return hashmap_ensure_allocated(&j->directories_by_wd, NULL);
1873 }
1874
1875 static sd_journal *journal_new(int flags, const char *path, const char *namespace) {
1876 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1877
1878 j = new0(sd_journal, 1);
1879 if (!j)
1880 return NULL;
1881
1882 j->original_pid = getpid_cached();
1883 j->toplevel_fd = -1;
1884 j->inotify_fd = -1;
1885 j->flags = flags;
1886 j->data_threshold = DEFAULT_DATA_THRESHOLD;
1887
1888 if (path) {
1889 char *t;
1890
1891 t = strdup(path);
1892 if (!t)
1893 return NULL;
1894
1895 if (flags & SD_JOURNAL_OS_ROOT)
1896 j->prefix = t;
1897 else
1898 j->path = t;
1899 }
1900
1901 if (namespace) {
1902 j->namespace = strdup(namespace);
1903 if (!j->namespace)
1904 return NULL;
1905 }
1906
1907 j->files = ordered_hashmap_new(&path_hash_ops);
1908 if (!j->files)
1909 return NULL;
1910
1911 j->files_cache = ordered_hashmap_iterated_cache_new(j->files);
1912 j->directories_by_path = hashmap_new(&path_hash_ops);
1913 j->mmap = mmap_cache_new();
1914 if (!j->files_cache || !j->directories_by_path || !j->mmap)
1915 return NULL;
1916
1917 return TAKE_PTR(j);
1918 }
1919
1920 #define OPEN_ALLOWED_FLAGS \
1921 (SD_JOURNAL_LOCAL_ONLY | \
1922 SD_JOURNAL_RUNTIME_ONLY | \
1923 SD_JOURNAL_SYSTEM | \
1924 SD_JOURNAL_CURRENT_USER | \
1925 SD_JOURNAL_ALL_NAMESPACES | \
1926 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
1927
1928 _public_ int sd_journal_open_namespace(sd_journal **ret, const char *namespace, int flags) {
1929 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1930 int r;
1931
1932 assert_return(ret, -EINVAL);
1933 assert_return((flags & ~OPEN_ALLOWED_FLAGS) == 0, -EINVAL);
1934
1935 j = journal_new(flags, NULL, namespace);
1936 if (!j)
1937 return -ENOMEM;
1938
1939 r = add_search_paths(j);
1940 if (r < 0)
1941 return r;
1942
1943 *ret = TAKE_PTR(j);
1944 return 0;
1945 }
1946
1947 _public_ int sd_journal_open(sd_journal **ret, int flags) {
1948 return sd_journal_open_namespace(ret, NULL, flags);
1949 }
1950
1951 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1952 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1953
1954 _public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) {
1955 _cleanup_free_ char *root = NULL, *class = NULL;
1956 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1957 char *p;
1958 int r;
1959
1960 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1961 * combination with sd_journal_open_directory_fd(). */
1962
1963 assert_return(machine, -EINVAL);
1964 assert_return(ret, -EINVAL);
1965 assert_return((flags & ~OPEN_CONTAINER_ALLOWED_FLAGS) == 0, -EINVAL);
1966 assert_return(machine_name_is_valid(machine), -EINVAL);
1967
1968 p = strjoina("/run/systemd/machines/", machine);
1969 r = parse_env_file(NULL, p,
1970 "ROOT", &root,
1971 "CLASS", &class);
1972 if (r == -ENOENT)
1973 return -EHOSTDOWN;
1974 if (r < 0)
1975 return r;
1976 if (!root)
1977 return -ENODATA;
1978
1979 if (!streq_ptr(class, "container"))
1980 return -EIO;
1981
1982 j = journal_new(flags, root, NULL);
1983 if (!j)
1984 return -ENOMEM;
1985
1986 r = add_search_paths(j);
1987 if (r < 0)
1988 return r;
1989
1990 *ret = TAKE_PTR(j);
1991 return 0;
1992 }
1993
1994 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
1995 (SD_JOURNAL_OS_ROOT | \
1996 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1997
1998 _public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
1999 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
2000 int r;
2001
2002 assert_return(ret, -EINVAL);
2003 assert_return(path, -EINVAL);
2004 assert_return((flags & ~OPEN_DIRECTORY_ALLOWED_FLAGS) == 0, -EINVAL);
2005
2006 j = journal_new(flags, path, NULL);
2007 if (!j)
2008 return -ENOMEM;
2009
2010 if (flags & SD_JOURNAL_OS_ROOT)
2011 r = add_search_paths(j);
2012 else
2013 r = add_root_directory(j, path, false);
2014 if (r < 0)
2015 return r;
2016
2017 *ret = TAKE_PTR(j);
2018 return 0;
2019 }
2020
2021 _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) {
2022 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
2023 const char **path;
2024 int r;
2025
2026 assert_return(ret, -EINVAL);
2027 assert_return(flags == 0, -EINVAL);
2028
2029 j = journal_new(flags, NULL, NULL);
2030 if (!j)
2031 return -ENOMEM;
2032
2033 STRV_FOREACH(path, paths) {
2034 r = add_any_file(j, -1, *path);
2035 if (r < 0)
2036 return r;
2037 }
2038
2039 j->no_new_files = true;
2040
2041 *ret = TAKE_PTR(j);
2042 return 0;
2043 }
2044
2045 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
2046 (SD_JOURNAL_OS_ROOT | \
2047 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2048
2049 _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
2050 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
2051 struct stat st;
2052 int r;
2053
2054 assert_return(ret, -EINVAL);
2055 assert_return(fd >= 0, -EBADF);
2056 assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL);
2057
2058 if (fstat(fd, &st) < 0)
2059 return -errno;
2060
2061 if (!S_ISDIR(st.st_mode))
2062 return -EBADFD;
2063
2064 j = journal_new(flags, NULL, NULL);
2065 if (!j)
2066 return -ENOMEM;
2067
2068 j->toplevel_fd = fd;
2069
2070 if (flags & SD_JOURNAL_OS_ROOT)
2071 r = add_search_paths(j);
2072 else
2073 r = add_root_directory(j, NULL, false);
2074 if (r < 0)
2075 return r;
2076
2077 *ret = TAKE_PTR(j);
2078 return 0;
2079 }
2080
2081 _public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) {
2082 Iterator iterator;
2083 JournalFile *f;
2084 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
2085 unsigned i;
2086 int r;
2087
2088 assert_return(ret, -EINVAL);
2089 assert_return(n_fds > 0, -EBADF);
2090 assert_return(flags == 0, -EINVAL);
2091
2092 j = journal_new(flags, NULL, NULL);
2093 if (!j)
2094 return -ENOMEM;
2095
2096 for (i = 0; i < n_fds; i++) {
2097 struct stat st;
2098
2099 if (fds[i] < 0) {
2100 r = -EBADF;
2101 goto fail;
2102 }
2103
2104 if (fstat(fds[i], &st) < 0) {
2105 r = -errno;
2106 goto fail;
2107 }
2108
2109 r = stat_verify_regular(&st);
2110 if (r < 0)
2111 goto fail;
2112
2113 r = add_any_file(j, fds[i], NULL);
2114 if (r < 0)
2115 goto fail;
2116 }
2117
2118 j->no_new_files = true;
2119 j->no_inotify = true;
2120
2121 *ret = TAKE_PTR(j);
2122 return 0;
2123
2124 fail:
2125 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2126 * remain open */
2127 ORDERED_HASHMAP_FOREACH(f, j->files, iterator)
2128 f->close_fd = false;
2129
2130 return r;
2131 }
2132
2133 _public_ void sd_journal_close(sd_journal *j) {
2134 Directory *d;
2135
2136 if (!j)
2137 return;
2138
2139 sd_journal_flush_matches(j);
2140
2141 ordered_hashmap_free_with_destructor(j->files, journal_file_close);
2142 iterated_cache_free(j->files_cache);
2143
2144 while ((d = hashmap_first(j->directories_by_path)))
2145 remove_directory(j, d);
2146
2147 while ((d = hashmap_first(j->directories_by_wd)))
2148 remove_directory(j, d);
2149
2150 hashmap_free(j->directories_by_path);
2151 hashmap_free(j->directories_by_wd);
2152
2153 safe_close(j->inotify_fd);
2154
2155 if (j->mmap) {
2156 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap));
2157 mmap_cache_unref(j->mmap);
2158 }
2159
2160 hashmap_free_free(j->errors);
2161
2162 free(j->path);
2163 free(j->prefix);
2164 free(j->namespace);
2165 free(j->unique_field);
2166 free(j->fields_buffer);
2167 free(j);
2168 }
2169
2170 _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
2171 Object *o;
2172 JournalFile *f;
2173 int r;
2174
2175 assert_return(j, -EINVAL);
2176 assert_return(!journal_pid_changed(j), -ECHILD);
2177 assert_return(ret, -EINVAL);
2178
2179 f = j->current_file;
2180 if (!f)
2181 return -EADDRNOTAVAIL;
2182
2183 if (f->current_offset <= 0)
2184 return -EADDRNOTAVAIL;
2185
2186 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2187 if (r < 0)
2188 return r;
2189
2190 *ret = le64toh(o->entry.realtime);
2191 return 0;
2192 }
2193
2194 _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
2195 Object *o;
2196 JournalFile *f;
2197 int r;
2198 sd_id128_t id;
2199
2200 assert_return(j, -EINVAL);
2201 assert_return(!journal_pid_changed(j), -ECHILD);
2202
2203 f = j->current_file;
2204 if (!f)
2205 return -EADDRNOTAVAIL;
2206
2207 if (f->current_offset <= 0)
2208 return -EADDRNOTAVAIL;
2209
2210 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2211 if (r < 0)
2212 return r;
2213
2214 if (ret_boot_id)
2215 *ret_boot_id = o->entry.boot_id;
2216 else {
2217 r = sd_id128_get_boot(&id);
2218 if (r < 0)
2219 return r;
2220
2221 if (!sd_id128_equal(id, o->entry.boot_id))
2222 return -ESTALE;
2223 }
2224
2225 if (ret)
2226 *ret = le64toh(o->entry.monotonic);
2227
2228 return 0;
2229 }
2230
2231 static bool field_is_valid(const char *field) {
2232 const char *p;
2233
2234 assert(field);
2235
2236 if (isempty(field))
2237 return false;
2238
2239 if (startswith(field, "__"))
2240 return false;
2241
2242 for (p = field; *p; p++) {
2243
2244 if (*p == '_')
2245 continue;
2246
2247 if (*p >= 'A' && *p <= 'Z')
2248 continue;
2249
2250 if (*p >= '0' && *p <= '9')
2251 continue;
2252
2253 return false;
2254 }
2255
2256 return true;
2257 }
2258
2259 _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
2260 JournalFile *f;
2261 uint64_t i, n;
2262 size_t field_length;
2263 int r;
2264 Object *o;
2265
2266 assert_return(j, -EINVAL);
2267 assert_return(!journal_pid_changed(j), -ECHILD);
2268 assert_return(field, -EINVAL);
2269 assert_return(data, -EINVAL);
2270 assert_return(size, -EINVAL);
2271 assert_return(field_is_valid(field), -EINVAL);
2272
2273 f = j->current_file;
2274 if (!f)
2275 return -EADDRNOTAVAIL;
2276
2277 if (f->current_offset <= 0)
2278 return -EADDRNOTAVAIL;
2279
2280 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2281 if (r < 0)
2282 return r;
2283
2284 field_length = strlen(field);
2285
2286 n = journal_file_entry_n_items(o);
2287 for (i = 0; i < n; i++) {
2288 uint64_t p, l;
2289 le64_t le_hash;
2290 size_t t;
2291 int compression;
2292
2293 p = le64toh(o->entry.items[i].object_offset);
2294 le_hash = o->entry.items[i].hash;
2295 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
2296 if (r < 0)
2297 return r;
2298
2299 if (le_hash != o->data.hash)
2300 return -EBADMSG;
2301
2302 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2303
2304 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
2305 if (compression) {
2306 #if HAVE_XZ || HAVE_LZ4
2307 r = decompress_startswith(compression,
2308 o->data.payload, l,
2309 &f->compress_buffer, &f->compress_buffer_size,
2310 field, field_length, '=');
2311 if (r < 0)
2312 log_debug_errno(r, "Cannot decompress %s object of length %"PRIu64" at offset "OFSfmt": %m",
2313 object_compressed_to_string(compression), l, p);
2314 else if (r > 0) {
2315
2316 size_t rsize;
2317
2318 r = decompress_blob(compression,
2319 o->data.payload, l,
2320 &f->compress_buffer, &f->compress_buffer_size, &rsize,
2321 j->data_threshold);
2322 if (r < 0)
2323 return r;
2324
2325 *data = f->compress_buffer;
2326 *size = (size_t) rsize;
2327
2328 return 0;
2329 }
2330 #else
2331 return -EPROTONOSUPPORT;
2332 #endif
2333 } else if (l >= field_length+1 &&
2334 memcmp(o->data.payload, field, field_length) == 0 &&
2335 o->data.payload[field_length] == '=') {
2336
2337 t = (size_t) l;
2338
2339 if ((uint64_t) t != l)
2340 return -E2BIG;
2341
2342 *data = o->data.payload;
2343 *size = t;
2344
2345 return 0;
2346 }
2347
2348 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2349 if (r < 0)
2350 return r;
2351 }
2352
2353 return -ENOENT;
2354 }
2355
2356 static int return_data(sd_journal *j, JournalFile *f, Object *o, const void **data, size_t *size) {
2357 size_t t;
2358 uint64_t l;
2359 int compression;
2360
2361 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2362 t = (size_t) l;
2363
2364 /* We can't read objects larger than 4G on a 32bit machine */
2365 if ((uint64_t) t != l)
2366 return -E2BIG;
2367
2368 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
2369 if (compression) {
2370 #if HAVE_XZ || HAVE_LZ4
2371 size_t rsize;
2372 int r;
2373
2374 r = decompress_blob(compression,
2375 o->data.payload, l, &f->compress_buffer,
2376 &f->compress_buffer_size, &rsize, j->data_threshold);
2377 if (r < 0)
2378 return r;
2379
2380 *data = f->compress_buffer;
2381 *size = (size_t) rsize;
2382 #else
2383 return -EPROTONOSUPPORT;
2384 #endif
2385 } else {
2386 *data = o->data.payload;
2387 *size = t;
2388 }
2389
2390 return 0;
2391 }
2392
2393 _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
2394 JournalFile *f;
2395 uint64_t p, n;
2396 le64_t le_hash;
2397 int r;
2398 Object *o;
2399
2400 assert_return(j, -EINVAL);
2401 assert_return(!journal_pid_changed(j), -ECHILD);
2402 assert_return(data, -EINVAL);
2403 assert_return(size, -EINVAL);
2404
2405 f = j->current_file;
2406 if (!f)
2407 return -EADDRNOTAVAIL;
2408
2409 if (f->current_offset <= 0)
2410 return -EADDRNOTAVAIL;
2411
2412 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2413 if (r < 0)
2414 return r;
2415
2416 n = journal_file_entry_n_items(o);
2417 if (j->current_field >= n)
2418 return 0;
2419
2420 p = le64toh(o->entry.items[j->current_field].object_offset);
2421 le_hash = o->entry.items[j->current_field].hash;
2422 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
2423 if (r < 0)
2424 return r;
2425
2426 if (le_hash != o->data.hash)
2427 return -EBADMSG;
2428
2429 r = return_data(j, f, o, data, size);
2430 if (r < 0)
2431 return r;
2432
2433 j->current_field++;
2434
2435 return 1;
2436 }
2437
2438 _public_ void sd_journal_restart_data(sd_journal *j) {
2439 if (!j)
2440 return;
2441
2442 j->current_field = 0;
2443 }
2444
2445 static int reiterate_all_paths(sd_journal *j) {
2446 assert(j);
2447
2448 if (j->no_new_files)
2449 return add_current_paths(j);
2450
2451 if (j->flags & SD_JOURNAL_OS_ROOT)
2452 return add_search_paths(j);
2453
2454 if (j->toplevel_fd >= 0)
2455 return add_root_directory(j, NULL, false);
2456
2457 if (j->path)
2458 return add_root_directory(j, j->path, true);
2459
2460 return add_search_paths(j);
2461 }
2462
2463 _public_ int sd_journal_get_fd(sd_journal *j) {
2464 int r;
2465
2466 assert_return(j, -EINVAL);
2467 assert_return(!journal_pid_changed(j), -ECHILD);
2468
2469 if (j->no_inotify)
2470 return -EMEDIUMTYPE;
2471
2472 if (j->inotify_fd >= 0)
2473 return j->inotify_fd;
2474
2475 r = allocate_inotify(j);
2476 if (r < 0)
2477 return r;
2478
2479 log_debug("Reiterating files to get inotify watches established.");
2480
2481 /* Iterate through all dirs again, to add them to the inotify */
2482 r = reiterate_all_paths(j);
2483 if (r < 0)
2484 return r;
2485
2486 return j->inotify_fd;
2487 }
2488
2489 _public_ int sd_journal_get_events(sd_journal *j) {
2490 int fd;
2491
2492 assert_return(j, -EINVAL);
2493 assert_return(!journal_pid_changed(j), -ECHILD);
2494
2495 fd = sd_journal_get_fd(j);
2496 if (fd < 0)
2497 return fd;
2498
2499 return POLLIN;
2500 }
2501
2502 _public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
2503 int fd;
2504
2505 assert_return(j, -EINVAL);
2506 assert_return(!journal_pid_changed(j), -ECHILD);
2507 assert_return(timeout_usec, -EINVAL);
2508
2509 fd = sd_journal_get_fd(j);
2510 if (fd < 0)
2511 return fd;
2512
2513 if (!j->on_network) {
2514 *timeout_usec = (uint64_t) -1;
2515 return 0;
2516 }
2517
2518 /* If we are on the network we need to regularly check for
2519 * changes manually */
2520
2521 *timeout_usec = j->last_process_usec + JOURNAL_FILES_RECHECK_USEC;
2522 return 1;
2523 }
2524
2525 static void process_q_overflow(sd_journal *j) {
2526 JournalFile *f;
2527 Directory *m;
2528 Iterator i;
2529
2530 assert(j);
2531
2532 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2533 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2534 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2535 * are subject for unloading. */
2536
2537 log_debug("Inotify queue overrun, reiterating everything.");
2538
2539 j->generation++;
2540 (void) reiterate_all_paths(j);
2541
2542 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2543
2544 if (f->last_seen_generation == j->generation)
2545 continue;
2546
2547 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f->path);
2548 remove_file_real(j, f);
2549 }
2550
2551 HASHMAP_FOREACH(m, j->directories_by_path, i) {
2552
2553 if (m->last_seen_generation == j->generation)
2554 continue;
2555
2556 if (m->is_root) /* Never GC root directories */
2557 continue;
2558
2559 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f->path);
2560 remove_directory(j, m);
2561 }
2562
2563 log_debug("Reiteration complete.");
2564 }
2565
2566 static void process_inotify_event(sd_journal *j, const struct inotify_event *e) {
2567 Directory *d;
2568
2569 assert(j);
2570 assert(e);
2571
2572 if (e->mask & IN_Q_OVERFLOW) {
2573 process_q_overflow(j);
2574 return;
2575 }
2576
2577 /* Is this a subdirectory we watch? */
2578 d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd));
2579 if (d) {
2580 if (!(e->mask & IN_ISDIR) && e->len > 0 &&
2581 (endswith(e->name, ".journal") ||
2582 endswith(e->name, ".journal~"))) {
2583
2584 /* Event for a journal file */
2585
2586 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2587 (void) add_file_by_name(j, d->path, e->name);
2588 else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT))
2589 remove_file_by_name(j, d->path, e->name);
2590
2591 } else if (!d->is_root && e->len == 0) {
2592
2593 /* Event for a subdirectory */
2594
2595 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
2596 remove_directory(j, d);
2597
2598 } else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && id128_is_valid(e->name)) {
2599
2600 /* Event for root directory */
2601
2602 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2603 (void) add_directory(j, d->path, e->name);
2604 }
2605
2606 return;
2607 }
2608
2609 if (e->mask & IN_IGNORED)
2610 return;
2611
2612 log_debug("Unexpected inotify event.");
2613 }
2614
2615 static int determine_change(sd_journal *j) {
2616 bool b;
2617
2618 assert(j);
2619
2620 b = j->current_invalidate_counter != j->last_invalidate_counter;
2621 j->last_invalidate_counter = j->current_invalidate_counter;
2622
2623 return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND;
2624 }
2625
2626 _public_ int sd_journal_process(sd_journal *j) {
2627 bool got_something = false;
2628
2629 assert_return(j, -EINVAL);
2630 assert_return(!journal_pid_changed(j), -ECHILD);
2631
2632 if (j->inotify_fd < 0) /* We have no inotify fd yet? Then there's noting to process. */
2633 return 0;
2634
2635 j->last_process_usec = now(CLOCK_MONOTONIC);
2636 j->last_invalidate_counter = j->current_invalidate_counter;
2637
2638 for (;;) {
2639 union inotify_event_buffer buffer;
2640 struct inotify_event *e;
2641 ssize_t l;
2642
2643 l = read(j->inotify_fd, &buffer, sizeof(buffer));
2644 if (l < 0) {
2645 if (IN_SET(errno, EAGAIN, EINTR))
2646 return got_something ? determine_change(j) : SD_JOURNAL_NOP;
2647
2648 return -errno;
2649 }
2650
2651 got_something = true;
2652
2653 FOREACH_INOTIFY_EVENT(e, buffer, l)
2654 process_inotify_event(j, e);
2655 }
2656 }
2657
2658 _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
2659 int r;
2660 uint64_t t;
2661
2662 assert_return(j, -EINVAL);
2663 assert_return(!journal_pid_changed(j), -ECHILD);
2664
2665 if (j->inotify_fd < 0) {
2666 Iterator i;
2667 JournalFile *f;
2668
2669 /* This is the first invocation, hence create the
2670 * inotify watch */
2671 r = sd_journal_get_fd(j);
2672 if (r < 0)
2673 return r;
2674
2675 /* Server might have done some vacuuming while we weren't watching.
2676 Get rid of the deleted files now so they don't stay around indefinitely. */
2677 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2678 r = journal_file_fstat(f);
2679 if (r < 0) {
2680 log_debug_errno(r,"Failed to fstat() journal file '%s' : %m", f->path);
2681 continue;
2682 }
2683
2684 if (f->last_stat.st_nlink <= 0)
2685 remove_file_real(j, f);
2686 }
2687
2688 /* The journal might have changed since the context
2689 * object was created and we weren't watching before,
2690 * hence don't wait for anything, and return
2691 * immediately. */
2692 return determine_change(j);
2693 }
2694
2695 r = sd_journal_get_timeout(j, &t);
2696 if (r < 0)
2697 return r;
2698
2699 if (t != (uint64_t) -1) {
2700 usec_t n;
2701
2702 n = now(CLOCK_MONOTONIC);
2703 t = t > n ? t - n : 0;
2704
2705 if (timeout_usec == (uint64_t) -1 || timeout_usec > t)
2706 timeout_usec = t;
2707 }
2708
2709 do {
2710 r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
2711 } while (r == -EINTR);
2712
2713 if (r < 0)
2714 return r;
2715
2716 return sd_journal_process(j);
2717 }
2718
2719 _public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
2720 Iterator i;
2721 JournalFile *f;
2722 bool first = true;
2723 uint64_t fmin = 0, tmax = 0;
2724 int r;
2725
2726 assert_return(j, -EINVAL);
2727 assert_return(!journal_pid_changed(j), -ECHILD);
2728 assert_return(from || to, -EINVAL);
2729 assert_return(from != to, -EINVAL);
2730
2731 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2732 usec_t fr, t;
2733
2734 r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
2735 if (r == -ENOENT)
2736 continue;
2737 if (r < 0)
2738 return r;
2739 if (r == 0)
2740 continue;
2741
2742 if (first) {
2743 fmin = fr;
2744 tmax = t;
2745 first = false;
2746 } else {
2747 fmin = MIN(fr, fmin);
2748 tmax = MAX(t, tmax);
2749 }
2750 }
2751
2752 if (from)
2753 *from = fmin;
2754 if (to)
2755 *to = tmax;
2756
2757 return first ? 0 : 1;
2758 }
2759
2760 _public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t *from, uint64_t *to) {
2761 Iterator i;
2762 JournalFile *f;
2763 bool found = false;
2764 int r;
2765
2766 assert_return(j, -EINVAL);
2767 assert_return(!journal_pid_changed(j), -ECHILD);
2768 assert_return(from || to, -EINVAL);
2769 assert_return(from != to, -EINVAL);
2770
2771 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2772 usec_t fr, t;
2773
2774 r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
2775 if (r == -ENOENT)
2776 continue;
2777 if (r < 0)
2778 return r;
2779 if (r == 0)
2780 continue;
2781
2782 if (found) {
2783 if (from)
2784 *from = MIN(fr, *from);
2785 if (to)
2786 *to = MAX(t, *to);
2787 } else {
2788 if (from)
2789 *from = fr;
2790 if (to)
2791 *to = t;
2792 found = true;
2793 }
2794 }
2795
2796 return found;
2797 }
2798
2799 void journal_print_header(sd_journal *j) {
2800 Iterator i;
2801 JournalFile *f;
2802 bool newline = false;
2803
2804 assert(j);
2805
2806 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2807 if (newline)
2808 putchar('\n');
2809 else
2810 newline = true;
2811
2812 journal_file_print_header(f);
2813 }
2814 }
2815
2816 _public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
2817 Iterator i;
2818 JournalFile *f;
2819 uint64_t sum = 0;
2820
2821 assert_return(j, -EINVAL);
2822 assert_return(!journal_pid_changed(j), -ECHILD);
2823 assert_return(bytes, -EINVAL);
2824
2825 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2826 struct stat st;
2827
2828 if (fstat(f->fd, &st) < 0)
2829 return -errno;
2830
2831 sum += (uint64_t) st.st_blocks * 512ULL;
2832 }
2833
2834 *bytes = sum;
2835 return 0;
2836 }
2837
2838 _public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
2839 char *f;
2840
2841 assert_return(j, -EINVAL);
2842 assert_return(!journal_pid_changed(j), -ECHILD);
2843 assert_return(!isempty(field), -EINVAL);
2844 assert_return(field_is_valid(field), -EINVAL);
2845
2846 f = strdup(field);
2847 if (!f)
2848 return -ENOMEM;
2849
2850 free(j->unique_field);
2851 j->unique_field = f;
2852 j->unique_file = NULL;
2853 j->unique_offset = 0;
2854 j->unique_file_lost = false;
2855
2856 return 0;
2857 }
2858
2859 _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
2860 size_t k;
2861
2862 assert_return(j, -EINVAL);
2863 assert_return(!journal_pid_changed(j), -ECHILD);
2864 assert_return(data, -EINVAL);
2865 assert_return(l, -EINVAL);
2866 assert_return(j->unique_field, -EINVAL);
2867
2868 k = strlen(j->unique_field);
2869
2870 if (!j->unique_file) {
2871 if (j->unique_file_lost)
2872 return 0;
2873
2874 j->unique_file = ordered_hashmap_first(j->files);
2875 if (!j->unique_file)
2876 return 0;
2877
2878 j->unique_offset = 0;
2879 }
2880
2881 for (;;) {
2882 JournalFile *of;
2883 Iterator i;
2884 Object *o;
2885 const void *odata;
2886 size_t ol;
2887 bool found;
2888 int r;
2889
2890 /* Proceed to next data object in the field's linked list */
2891 if (j->unique_offset == 0) {
2892 r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL);
2893 if (r < 0)
2894 return r;
2895
2896 j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0;
2897 } else {
2898 r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o);
2899 if (r < 0)
2900 return r;
2901
2902 j->unique_offset = le64toh(o->data.next_field_offset);
2903 }
2904
2905 /* We reached the end of the list? Then start again, with the next file */
2906 if (j->unique_offset == 0) {
2907 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
2908 if (!j->unique_file)
2909 return 0;
2910
2911 continue;
2912 }
2913
2914 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2915 * instead, so that we can look at this data object at the same
2916 * time as one on another file */
2917 r = journal_file_move_to_object(j->unique_file, OBJECT_UNUSED, j->unique_offset, &o);
2918 if (r < 0)
2919 return r;
2920
2921 /* Let's do the type check by hand, since we used 0 context above. */
2922 if (o->object.type != OBJECT_DATA)
2923 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
2924 "%s:offset " OFSfmt ": object has type %d, expected %d",
2925 j->unique_file->path,
2926 j->unique_offset,
2927 o->object.type, OBJECT_DATA);
2928
2929 r = return_data(j, j->unique_file, o, &odata, &ol);
2930 if (r < 0)
2931 return r;
2932
2933 /* Check if we have at least the field name and "=". */
2934 if (ol <= k)
2935 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
2936 "%s:offset " OFSfmt ": object has size %zu, expected at least %zu",
2937 j->unique_file->path,
2938 j->unique_offset, ol, k + 1);
2939
2940 if (memcmp(odata, j->unique_field, k) || ((const char*) odata)[k] != '=')
2941 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
2942 "%s:offset " OFSfmt ": object does not start with \"%s=\"",
2943 j->unique_file->path,
2944 j->unique_offset,
2945 j->unique_field);
2946
2947 /* OK, now let's see if we already returned this data
2948 * object by checking if it exists in the earlier
2949 * traversed files. */
2950 found = false;
2951 ORDERED_HASHMAP_FOREACH(of, j->files, i) {
2952 if (of == j->unique_file)
2953 break;
2954
2955 /* Skip this file it didn't have any fields indexed */
2956 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
2957 continue;
2958
2959 r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), NULL, NULL);
2960 if (r < 0)
2961 return r;
2962 if (r > 0) {
2963 found = true;
2964 break;
2965 }
2966 }
2967
2968 if (found)
2969 continue;
2970
2971 r = return_data(j, j->unique_file, o, data, l);
2972 if (r < 0)
2973 return r;
2974
2975 return 1;
2976 }
2977 }
2978
2979 _public_ void sd_journal_restart_unique(sd_journal *j) {
2980 if (!j)
2981 return;
2982
2983 j->unique_file = NULL;
2984 j->unique_offset = 0;
2985 j->unique_file_lost = false;
2986 }
2987
2988 _public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) {
2989 int r;
2990
2991 assert_return(j, -EINVAL);
2992 assert_return(!journal_pid_changed(j), -ECHILD);
2993 assert_return(field, -EINVAL);
2994
2995 if (!j->fields_file) {
2996 if (j->fields_file_lost)
2997 return 0;
2998
2999 j->fields_file = ordered_hashmap_first(j->files);
3000 if (!j->fields_file)
3001 return 0;
3002
3003 j->fields_hash_table_index = 0;
3004 j->fields_offset = 0;
3005 }
3006
3007 for (;;) {
3008 JournalFile *f, *of;
3009 Iterator i;
3010 uint64_t m;
3011 Object *o;
3012 size_t sz;
3013 bool found;
3014
3015 f = j->fields_file;
3016
3017 if (j->fields_offset == 0) {
3018 bool eof = false;
3019
3020 /* We are not yet positioned at any field. Let's pick the first one */
3021 r = journal_file_map_field_hash_table(f);
3022 if (r < 0)
3023 return r;
3024
3025 m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
3026 for (;;) {
3027 if (j->fields_hash_table_index >= m) {
3028 /* Reached the end of the hash table, go to the next file. */
3029 eof = true;
3030 break;
3031 }
3032
3033 j->fields_offset = le64toh(f->field_hash_table[j->fields_hash_table_index].head_hash_offset);
3034
3035 if (j->fields_offset != 0)
3036 break;
3037
3038 /* Empty hash table bucket, go to next one */
3039 j->fields_hash_table_index++;
3040 }
3041
3042 if (eof) {
3043 /* Proceed with next file */
3044 j->fields_file = ordered_hashmap_next(j->files, f->path);
3045 if (!j->fields_file) {
3046 *field = NULL;
3047 return 0;
3048 }
3049
3050 j->fields_offset = 0;
3051 j->fields_hash_table_index = 0;
3052 continue;
3053 }
3054
3055 } else {
3056 /* We are already positioned at a field. If so, let's figure out the next field from it */
3057
3058 r = journal_file_move_to_object(f, OBJECT_FIELD, j->fields_offset, &o);
3059 if (r < 0)
3060 return r;
3061
3062 j->fields_offset = le64toh(o->field.next_hash_offset);
3063 if (j->fields_offset == 0) {
3064 /* Reached the end of the hash table chain */
3065 j->fields_hash_table_index++;
3066 continue;
3067 }
3068 }
3069
3070 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
3071 r = journal_file_move_to_object(f, OBJECT_UNUSED, j->fields_offset, &o);
3072 if (r < 0)
3073 return r;
3074
3075 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
3076 if (o->object.type != OBJECT_FIELD)
3077 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
3078 "%s:offset " OFSfmt ": object has type %i, expected %i",
3079 f->path, j->fields_offset,
3080 o->object.type, OBJECT_FIELD);
3081
3082 sz = le64toh(o->object.size) - offsetof(Object, field.payload);
3083
3084 /* Let's see if we already returned this field name before. */
3085 found = false;
3086 ORDERED_HASHMAP_FOREACH(of, j->files, i) {
3087 if (of == f)
3088 break;
3089
3090 /* Skip this file it didn't have any fields indexed */
3091 if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
3092 continue;
3093
3094 r = journal_file_find_field_object_with_hash(of, o->field.payload, sz, le64toh(o->field.hash), NULL, NULL);
3095 if (r < 0)
3096 return r;
3097 if (r > 0) {
3098 found = true;
3099 break;
3100 }
3101 }
3102
3103 if (found)
3104 continue;
3105
3106 /* Check if this is really a valid string containing no NUL byte */
3107 if (memchr(o->field.payload, 0, sz))
3108 return -EBADMSG;
3109
3110 if (sz > j->data_threshold)
3111 sz = j->data_threshold;
3112
3113 if (!GREEDY_REALLOC(j->fields_buffer, j->fields_buffer_allocated, sz + 1))
3114 return -ENOMEM;
3115
3116 memcpy(j->fields_buffer, o->field.payload, sz);
3117 j->fields_buffer[sz] = 0;
3118
3119 if (!field_is_valid(j->fields_buffer))
3120 return -EBADMSG;
3121
3122 *field = j->fields_buffer;
3123 return 1;
3124 }
3125 }
3126
3127 _public_ void sd_journal_restart_fields(sd_journal *j) {
3128 if (!j)
3129 return;
3130
3131 j->fields_file = NULL;
3132 j->fields_hash_table_index = 0;
3133 j->fields_offset = 0;
3134 j->fields_file_lost = false;
3135 }
3136
3137 _public_ int sd_journal_reliable_fd(sd_journal *j) {
3138 assert_return(j, -EINVAL);
3139 assert_return(!journal_pid_changed(j), -ECHILD);
3140
3141 return !j->on_network;
3142 }
3143
3144 static char *lookup_field(const char *field, void *userdata) {
3145 sd_journal *j = userdata;
3146 const void *data;
3147 size_t size, d;
3148 int r;
3149
3150 assert(field);
3151 assert(j);
3152
3153 r = sd_journal_get_data(j, field, &data, &size);
3154 if (r < 0 ||
3155 size > REPLACE_VAR_MAX)
3156 return strdup(field);
3157
3158 d = strlen(field) + 1;
3159
3160 return strndup((const char*) data + d, size - d);
3161 }
3162
3163 _public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
3164 const void *data;
3165 size_t size;
3166 sd_id128_t id;
3167 _cleanup_free_ char *text = NULL, *cid = NULL;
3168 char *t;
3169 int r;
3170
3171 assert_return(j, -EINVAL);
3172 assert_return(!journal_pid_changed(j), -ECHILD);
3173 assert_return(ret, -EINVAL);
3174
3175 r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size);
3176 if (r < 0)
3177 return r;
3178
3179 cid = strndup((const char*) data + 11, size - 11);
3180 if (!cid)
3181 return -ENOMEM;
3182
3183 r = sd_id128_from_string(cid, &id);
3184 if (r < 0)
3185 return r;
3186
3187 r = catalog_get(CATALOG_DATABASE, id, &text);
3188 if (r < 0)
3189 return r;
3190
3191 t = replace_var(text, lookup_field, j);
3192 if (!t)
3193 return -ENOMEM;
3194
3195 *ret = t;
3196 return 0;
3197 }
3198
3199 _public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) {
3200 assert_return(ret, -EINVAL);
3201
3202 return catalog_get(CATALOG_DATABASE, id, ret);
3203 }
3204
3205 _public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {
3206 assert_return(j, -EINVAL);
3207 assert_return(!journal_pid_changed(j), -ECHILD);
3208
3209 j->data_threshold = sz;
3210 return 0;
3211 }
3212
3213 _public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) {
3214 assert_return(j, -EINVAL);
3215 assert_return(!journal_pid_changed(j), -ECHILD);
3216 assert_return(sz, -EINVAL);
3217
3218 *sz = j->data_threshold;
3219 return 0;
3220 }
3221
3222 _public_ int sd_journal_has_runtime_files(sd_journal *j) {
3223 assert_return(j, -EINVAL);
3224
3225 return j->has_runtime_files;
3226 }
3227
3228 _public_ int sd_journal_has_persistent_files(sd_journal *j) {
3229 assert_return(j, -EINVAL);
3230
3231 return j->has_persistent_files;
3232 }