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