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