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