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