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