]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/sd-journal.c
Merge pull request #13084 from ddstreet/log_time
[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(const JournalFile *f, const Location *l, const JournalFile *current_file) {
439 int r;
440
441 assert(f);
442 assert(l);
443 assert(f->location_type == LOCATION_SEEK);
444 assert(IN_SET(l->type, LOCATION_DISCRETE, LOCATION_SEEK));
445
446 if (l->monotonic_set &&
447 sd_id128_equal(f->current_boot_id, l->boot_id) &&
448 l->realtime_set &&
449 f->current_realtime == l->realtime &&
450 l->xor_hash_set &&
451 f->current_xor_hash == l->xor_hash &&
452 f != current_file)
453 return 0;
454
455 if (l->seqnum_set &&
456 sd_id128_equal(f->header->seqnum_id, l->seqnum_id)) {
457
458 r = CMP(f->current_seqnum, l->seqnum);
459 if (r != 0)
460 return r;
461 }
462
463 if (l->monotonic_set &&
464 sd_id128_equal(f->current_boot_id, l->boot_id)) {
465
466 r = CMP(f->current_monotonic, l->monotonic);
467 if (r != 0)
468 return r;
469 }
470
471 if (l->realtime_set) {
472
473 r = CMP(f->current_realtime, l->realtime);
474 if (r != 0)
475 return r;
476 }
477
478 if (l->xor_hash_set) {
479
480 r = CMP(f->current_xor_hash, l->xor_hash);
481 if (r != 0)
482 return r;
483 }
484
485 return 0;
486 }
487
488 static int next_for_match(
489 sd_journal *j,
490 Match *m,
491 JournalFile *f,
492 uint64_t after_offset,
493 direction_t direction,
494 Object **ret,
495 uint64_t *offset) {
496
497 int r;
498 uint64_t np = 0;
499 Object *n;
500
501 assert(j);
502 assert(m);
503 assert(f);
504
505 if (m->type == MATCH_DISCRETE) {
506 uint64_t dp;
507
508 r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
509 if (r <= 0)
510 return r;
511
512 return journal_file_move_to_entry_by_offset_for_data(f, dp, after_offset, direction, ret, offset);
513
514 } else if (m->type == MATCH_OR_TERM) {
515 Match *i;
516
517 /* Find the earliest match beyond after_offset */
518
519 LIST_FOREACH(matches, i, m->matches) {
520 uint64_t cp;
521
522 r = next_for_match(j, i, f, after_offset, direction, NULL, &cp);
523 if (r < 0)
524 return r;
525 else if (r > 0) {
526 if (np == 0 || (direction == DIRECTION_DOWN ? cp < np : cp > np))
527 np = cp;
528 }
529 }
530
531 if (np == 0)
532 return 0;
533
534 } else if (m->type == MATCH_AND_TERM) {
535 Match *i, *last_moved;
536
537 /* Always jump to the next matching entry and repeat
538 * this until we find an offset that matches for all
539 * matches. */
540
541 if (!m->matches)
542 return 0;
543
544 r = next_for_match(j, m->matches, f, after_offset, direction, NULL, &np);
545 if (r <= 0)
546 return r;
547
548 assert(direction == DIRECTION_DOWN ? np >= after_offset : np <= after_offset);
549 last_moved = m->matches;
550
551 LIST_LOOP_BUT_ONE(matches, i, m->matches, last_moved) {
552 uint64_t cp;
553
554 r = next_for_match(j, i, f, np, direction, NULL, &cp);
555 if (r <= 0)
556 return r;
557
558 assert(direction == DIRECTION_DOWN ? cp >= np : cp <= np);
559 if (direction == DIRECTION_DOWN ? cp > np : cp < np) {
560 np = cp;
561 last_moved = i;
562 }
563 }
564 }
565
566 assert(np > 0);
567
568 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
569 if (r < 0)
570 return r;
571
572 if (ret)
573 *ret = n;
574 if (offset)
575 *offset = np;
576
577 return 1;
578 }
579
580 static int find_location_for_match(
581 sd_journal *j,
582 Match *m,
583 JournalFile *f,
584 direction_t direction,
585 Object **ret,
586 uint64_t *offset) {
587
588 int r;
589
590 assert(j);
591 assert(m);
592 assert(f);
593
594 if (m->type == MATCH_DISCRETE) {
595 uint64_t dp;
596
597 r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
598 if (r <= 0)
599 return r;
600
601 /* FIXME: missing: find by monotonic */
602
603 if (j->current_location.type == LOCATION_HEAD)
604 return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, ret, offset);
605 if (j->current_location.type == LOCATION_TAIL)
606 return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, ret, offset);
607 if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
608 return journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, ret, offset);
609 if (j->current_location.monotonic_set) {
610 r = journal_file_move_to_entry_by_monotonic_for_data(f, dp, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
611 if (r != -ENOENT)
612 return r;
613 }
614 if (j->current_location.realtime_set)
615 return journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, ret, offset);
616
617 return journal_file_next_entry_for_data(f, NULL, 0, dp, direction, ret, offset);
618
619 } else if (m->type == MATCH_OR_TERM) {
620 uint64_t np = 0;
621 Object *n;
622 Match *i;
623
624 /* Find the earliest match */
625
626 LIST_FOREACH(matches, i, m->matches) {
627 uint64_t cp;
628
629 r = find_location_for_match(j, i, f, direction, NULL, &cp);
630 if (r < 0)
631 return r;
632 else if (r > 0) {
633 if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp))
634 np = cp;
635 }
636 }
637
638 if (np == 0)
639 return 0;
640
641 r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
642 if (r < 0)
643 return r;
644
645 if (ret)
646 *ret = n;
647 if (offset)
648 *offset = np;
649
650 return 1;
651
652 } else {
653 Match *i;
654 uint64_t np = 0;
655
656 assert(m->type == MATCH_AND_TERM);
657
658 /* First jump to the last match, and then find the
659 * next one where all matches match */
660
661 if (!m->matches)
662 return 0;
663
664 LIST_FOREACH(matches, i, m->matches) {
665 uint64_t cp;
666
667 r = find_location_for_match(j, i, f, direction, NULL, &cp);
668 if (r <= 0)
669 return r;
670
671 if (np == 0 || (direction == DIRECTION_DOWN ? cp > np : cp < np))
672 np = cp;
673 }
674
675 return next_for_match(j, m, f, np, direction, ret, offset);
676 }
677 }
678
679 static int find_location_with_matches(
680 sd_journal *j,
681 JournalFile *f,
682 direction_t direction,
683 Object **ret,
684 uint64_t *offset) {
685
686 int r;
687
688 assert(j);
689 assert(f);
690 assert(ret);
691 assert(offset);
692
693 if (!j->level0) {
694 /* No matches is simple */
695
696 if (j->current_location.type == LOCATION_HEAD)
697 return journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, offset);
698 if (j->current_location.type == LOCATION_TAIL)
699 return journal_file_next_entry(f, 0, DIRECTION_UP, ret, offset);
700 if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
701 return journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, ret, offset);
702 if (j->current_location.monotonic_set) {
703 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
704 if (r != -ENOENT)
705 return r;
706 }
707 if (j->current_location.realtime_set)
708 return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, offset);
709
710 return journal_file_next_entry(f, 0, direction, ret, offset);
711 } else
712 return find_location_for_match(j, j->level0, f, direction, ret, offset);
713 }
714
715 static int next_with_matches(
716 sd_journal *j,
717 JournalFile *f,
718 direction_t direction,
719 Object **ret,
720 uint64_t *offset) {
721
722 assert(j);
723 assert(f);
724 assert(ret);
725 assert(offset);
726
727 /* No matches is easy. We simple advance the file
728 * pointer by one. */
729 if (!j->level0)
730 return journal_file_next_entry(f, f->current_offset, direction, ret, offset);
731
732 /* If we have a match then we look for the next matching entry
733 * with an offset at least one step larger */
734 return next_for_match(j, j->level0, f,
735 direction == DIRECTION_DOWN ? f->current_offset + 1
736 : f->current_offset - 1,
737 direction, ret, offset);
738 }
739
740 static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction) {
741 Object *c;
742 uint64_t cp, n_entries;
743 int r;
744
745 assert(j);
746 assert(f);
747
748 n_entries = le64toh(f->header->n_entries);
749
750 /* If we hit EOF before, we don't need to look into this file again
751 * unless direction changed or new entries appeared. */
752 if (f->last_direction == direction && f->location_type == LOCATION_TAIL &&
753 n_entries == f->last_n_entries)
754 return 0;
755
756 f->last_n_entries = n_entries;
757
758 if (f->last_direction == direction && f->current_offset > 0) {
759 /* LOCATION_SEEK here means we did the work in a previous
760 * iteration and the current location already points to a
761 * candidate entry. */
762 if (f->location_type != LOCATION_SEEK) {
763 r = next_with_matches(j, f, direction, &c, &cp);
764 if (r <= 0)
765 return r;
766
767 journal_file_save_location(f, c, cp);
768 }
769 } else {
770 f->last_direction = direction;
771
772 r = find_location_with_matches(j, f, direction, &c, &cp);
773 if (r <= 0)
774 return r;
775
776 journal_file_save_location(f, c, cp);
777 }
778
779 /* OK, we found the spot, now let's advance until an entry
780 * that is actually different from what we were previously
781 * looking at. This is necessary to handle entries which exist
782 * in two (or more) journal files, and which shall all be
783 * suppressed but one. */
784
785 for (;;) {
786 bool found;
787
788 if (j->current_location.type == LOCATION_DISCRETE) {
789 int k;
790
791 k = compare_with_location(f, &j->current_location, j->current_file);
792
793 found = direction == DIRECTION_DOWN ? k > 0 : k < 0;
794 } else
795 found = true;
796
797 if (found)
798 return 1;
799
800 r = next_with_matches(j, f, direction, &c, &cp);
801 if (r <= 0)
802 return r;
803
804 journal_file_save_location(f, c, cp);
805 }
806 }
807
808 static int real_journal_next(sd_journal *j, direction_t direction) {
809 JournalFile *new_file = NULL;
810 unsigned i, n_files;
811 const void **files;
812 Object *o;
813 int r;
814
815 assert_return(j, -EINVAL);
816 assert_return(!journal_pid_changed(j), -ECHILD);
817
818 r = iterated_cache_get(j->files_cache, NULL, &files, &n_files);
819 if (r < 0)
820 return r;
821
822 for (i = 0; i < n_files; i++) {
823 JournalFile *f = (JournalFile *)files[i];
824 bool found;
825
826 r = next_beyond_location(j, f, direction);
827 if (r < 0) {
828 log_debug_errno(r, "Can't iterate through %s, ignoring: %m", f->path);
829 remove_file_real(j, f);
830 continue;
831 } else if (r == 0) {
832 f->location_type = LOCATION_TAIL;
833 continue;
834 }
835
836 if (!new_file)
837 found = true;
838 else {
839 int k;
840
841 k = journal_file_compare_locations(f, new_file);
842
843 found = direction == DIRECTION_DOWN ? k < 0 : k > 0;
844 }
845
846 if (found)
847 new_file = f;
848 }
849
850 if (!new_file)
851 return 0;
852
853 r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_file->current_offset, &o);
854 if (r < 0)
855 return r;
856
857 set_location(j, new_file, o);
858
859 return 1;
860 }
861
862 _public_ int sd_journal_next(sd_journal *j) {
863 return real_journal_next(j, DIRECTION_DOWN);
864 }
865
866 _public_ int sd_journal_previous(sd_journal *j) {
867 return real_journal_next(j, DIRECTION_UP);
868 }
869
870 static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
871 int c = 0, r;
872
873 assert_return(j, -EINVAL);
874 assert_return(!journal_pid_changed(j), -ECHILD);
875
876 if (skip == 0) {
877 /* If this is not a discrete skip, then at least
878 * resolve the current location */
879 if (j->current_location.type != LOCATION_DISCRETE) {
880 r = real_journal_next(j, direction);
881 if (r < 0)
882 return r;
883 }
884
885 return 0;
886 }
887
888 do {
889 r = real_journal_next(j, direction);
890 if (r < 0)
891 return r;
892
893 if (r == 0)
894 return c;
895
896 skip--;
897 c++;
898 } while (skip > 0);
899
900 return c;
901 }
902
903 _public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
904 return real_journal_next_skip(j, DIRECTION_DOWN, skip);
905 }
906
907 _public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
908 return real_journal_next_skip(j, DIRECTION_UP, skip);
909 }
910
911 _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
912 Object *o;
913 int r;
914 char bid[SD_ID128_STRING_MAX], sid[SD_ID128_STRING_MAX];
915
916 assert_return(j, -EINVAL);
917 assert_return(!journal_pid_changed(j), -ECHILD);
918 assert_return(cursor, -EINVAL);
919
920 if (!j->current_file || j->current_file->current_offset <= 0)
921 return -EADDRNOTAVAIL;
922
923 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
924 if (r < 0)
925 return r;
926
927 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
928 sd_id128_to_string(o->entry.boot_id, bid);
929
930 if (asprintf(cursor,
931 "s=%s;i=%"PRIx64";b=%s;m=%"PRIx64";t=%"PRIx64";x=%"PRIx64,
932 sid, le64toh(o->entry.seqnum),
933 bid, le64toh(o->entry.monotonic),
934 le64toh(o->entry.realtime),
935 le64toh(o->entry.xor_hash)) < 0)
936 return -ENOMEM;
937
938 return 0;
939 }
940
941 _public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
942 const char *word, *state;
943 size_t l;
944 unsigned long long seqnum, monotonic, realtime, xor_hash;
945 bool
946 seqnum_id_set = false,
947 seqnum_set = false,
948 boot_id_set = false,
949 monotonic_set = false,
950 realtime_set = false,
951 xor_hash_set = false;
952 sd_id128_t seqnum_id, boot_id;
953
954 assert_return(j, -EINVAL);
955 assert_return(!journal_pid_changed(j), -ECHILD);
956 assert_return(!isempty(cursor), -EINVAL);
957
958 FOREACH_WORD_SEPARATOR(word, l, cursor, ";", state) {
959 char *item;
960 int k = 0;
961
962 if (l < 2 || word[1] != '=')
963 return -EINVAL;
964
965 item = strndup(word, l);
966 if (!item)
967 return -ENOMEM;
968
969 switch (word[0]) {
970
971 case 's':
972 seqnum_id_set = true;
973 k = sd_id128_from_string(item+2, &seqnum_id);
974 break;
975
976 case 'i':
977 seqnum_set = true;
978 if (sscanf(item+2, "%llx", &seqnum) != 1)
979 k = -EINVAL;
980 break;
981
982 case 'b':
983 boot_id_set = true;
984 k = sd_id128_from_string(item+2, &boot_id);
985 break;
986
987 case 'm':
988 monotonic_set = true;
989 if (sscanf(item+2, "%llx", &monotonic) != 1)
990 k = -EINVAL;
991 break;
992
993 case 't':
994 realtime_set = true;
995 if (sscanf(item+2, "%llx", &realtime) != 1)
996 k = -EINVAL;
997 break;
998
999 case 'x':
1000 xor_hash_set = true;
1001 if (sscanf(item+2, "%llx", &xor_hash) != 1)
1002 k = -EINVAL;
1003 break;
1004 }
1005
1006 free(item);
1007
1008 if (k < 0)
1009 return k;
1010 }
1011
1012 if ((!seqnum_set || !seqnum_id_set) &&
1013 (!monotonic_set || !boot_id_set) &&
1014 !realtime_set)
1015 return -EINVAL;
1016
1017 reset_location(j);
1018
1019 j->current_location.type = LOCATION_SEEK;
1020
1021 if (realtime_set) {
1022 j->current_location.realtime = (uint64_t) realtime;
1023 j->current_location.realtime_set = true;
1024 }
1025
1026 if (seqnum_set && seqnum_id_set) {
1027 j->current_location.seqnum = (uint64_t) seqnum;
1028 j->current_location.seqnum_id = seqnum_id;
1029 j->current_location.seqnum_set = true;
1030 }
1031
1032 if (monotonic_set && boot_id_set) {
1033 j->current_location.monotonic = (uint64_t) monotonic;
1034 j->current_location.boot_id = boot_id;
1035 j->current_location.monotonic_set = true;
1036 }
1037
1038 if (xor_hash_set) {
1039 j->current_location.xor_hash = (uint64_t) xor_hash;
1040 j->current_location.xor_hash_set = true;
1041 }
1042
1043 return 0;
1044 }
1045
1046 _public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
1047 int r;
1048 Object *o;
1049
1050 assert_return(j, -EINVAL);
1051 assert_return(!journal_pid_changed(j), -ECHILD);
1052 assert_return(!isempty(cursor), -EINVAL);
1053
1054 if (!j->current_file || j->current_file->current_offset <= 0)
1055 return -EADDRNOTAVAIL;
1056
1057 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
1058 if (r < 0)
1059 return r;
1060
1061 for (;;) {
1062 _cleanup_free_ char *item = NULL;
1063 unsigned long long ll;
1064 sd_id128_t id;
1065 int k = 0;
1066
1067 r = extract_first_word(&cursor, &item, ";", EXTRACT_DONT_COALESCE_SEPARATORS);
1068 if (r < 0)
1069 return r;
1070
1071 if (r == 0)
1072 break;
1073
1074 if (strlen(item) < 2 || item[1] != '=')
1075 return -EINVAL;
1076
1077 switch (item[0]) {
1078
1079 case 's':
1080 k = sd_id128_from_string(item+2, &id);
1081 if (k < 0)
1082 return k;
1083 if (!sd_id128_equal(id, j->current_file->header->seqnum_id))
1084 return 0;
1085 break;
1086
1087 case 'i':
1088 if (sscanf(item+2, "%llx", &ll) != 1)
1089 return -EINVAL;
1090 if (ll != le64toh(o->entry.seqnum))
1091 return 0;
1092 break;
1093
1094 case 'b':
1095 k = sd_id128_from_string(item+2, &id);
1096 if (k < 0)
1097 return k;
1098 if (!sd_id128_equal(id, o->entry.boot_id))
1099 return 0;
1100 break;
1101
1102 case 'm':
1103 if (sscanf(item+2, "%llx", &ll) != 1)
1104 return -EINVAL;
1105 if (ll != le64toh(o->entry.monotonic))
1106 return 0;
1107 break;
1108
1109 case 't':
1110 if (sscanf(item+2, "%llx", &ll) != 1)
1111 return -EINVAL;
1112 if (ll != le64toh(o->entry.realtime))
1113 return 0;
1114 break;
1115
1116 case 'x':
1117 if (sscanf(item+2, "%llx", &ll) != 1)
1118 return -EINVAL;
1119 if (ll != le64toh(o->entry.xor_hash))
1120 return 0;
1121 break;
1122 }
1123 }
1124
1125 return 1;
1126 }
1127
1128 _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
1129 assert_return(j, -EINVAL);
1130 assert_return(!journal_pid_changed(j), -ECHILD);
1131
1132 reset_location(j);
1133 j->current_location.type = LOCATION_SEEK;
1134 j->current_location.boot_id = boot_id;
1135 j->current_location.monotonic = usec;
1136 j->current_location.monotonic_set = true;
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 reset_location(j);
1146 j->current_location.type = LOCATION_SEEK;
1147 j->current_location.realtime = usec;
1148 j->current_location.realtime_set = true;
1149
1150 return 0;
1151 }
1152
1153 _public_ int sd_journal_seek_head(sd_journal *j) {
1154 assert_return(j, -EINVAL);
1155 assert_return(!journal_pid_changed(j), -ECHILD);
1156
1157 reset_location(j);
1158 j->current_location.type = LOCATION_HEAD;
1159
1160 return 0;
1161 }
1162
1163 _public_ int sd_journal_seek_tail(sd_journal *j) {
1164 assert_return(j, -EINVAL);
1165 assert_return(!journal_pid_changed(j), -ECHILD);
1166
1167 reset_location(j);
1168 j->current_location.type = LOCATION_TAIL;
1169
1170 return 0;
1171 }
1172
1173 static void check_network(sd_journal *j, int fd) {
1174 assert(j);
1175
1176 if (j->on_network)
1177 return;
1178
1179 j->on_network = fd_is_network_fs(fd);
1180 }
1181
1182 static bool file_has_type_prefix(const char *prefix, const char *filename) {
1183 const char *full, *tilded, *atted;
1184
1185 full = strjoina(prefix, ".journal");
1186 tilded = strjoina(full, "~");
1187 atted = strjoina(prefix, "@");
1188
1189 return STR_IN_SET(filename, full, tilded) ||
1190 startswith(filename, atted);
1191 }
1192
1193 static bool file_type_wanted(int flags, const char *filename) {
1194 assert(filename);
1195
1196 if (!endswith(filename, ".journal") && !endswith(filename, ".journal~"))
1197 return false;
1198
1199 /* no flags set → every type is OK */
1200 if (!(flags & (SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)))
1201 return true;
1202
1203 if (flags & SD_JOURNAL_SYSTEM && file_has_type_prefix("system", filename))
1204 return true;
1205
1206 if (flags & SD_JOURNAL_CURRENT_USER) {
1207 char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1];
1208
1209 xsprintf(prefix, "user-"UID_FMT, getuid());
1210
1211 if (file_has_type_prefix(prefix, filename))
1212 return true;
1213 }
1214
1215 return false;
1216 }
1217
1218 static bool path_has_prefix(sd_journal *j, const char *path, const char *prefix) {
1219 assert(j);
1220 assert(path);
1221 assert(prefix);
1222
1223 if (j->toplevel_fd >= 0)
1224 return false;
1225
1226 return path_startswith(path, prefix);
1227 }
1228
1229 static void track_file_disposition(sd_journal *j, JournalFile *f) {
1230 assert(j);
1231 assert(f);
1232
1233 if (!j->has_runtime_files && path_has_prefix(j, f->path, "/run"))
1234 j->has_runtime_files = true;
1235 else if (!j->has_persistent_files && path_has_prefix(j, f->path, "/var"))
1236 j->has_persistent_files = true;
1237 }
1238
1239 static const char *skip_slash(const char *p) {
1240
1241 if (!p)
1242 return NULL;
1243
1244 while (*p == '/')
1245 p++;
1246
1247 return p;
1248 }
1249
1250 static int add_any_file(
1251 sd_journal *j,
1252 int fd,
1253 const char *path) {
1254
1255 bool close_fd = false;
1256 JournalFile *f;
1257 struct stat st;
1258 int r, k;
1259
1260 assert(j);
1261 assert(fd >= 0 || path);
1262
1263 if (fd < 0) {
1264 if (j->toplevel_fd >= 0)
1265 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1266 * openat() ignores the first argument. */
1267
1268 fd = openat(j->toplevel_fd, skip_slash(path), O_RDONLY|O_CLOEXEC|O_NONBLOCK);
1269 else
1270 fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
1271 if (fd < 0) {
1272 r = log_debug_errno(errno, "Failed to open journal file %s: %m", path);
1273 goto finish;
1274 }
1275
1276 close_fd = true;
1277
1278 r = fd_nonblock(fd, false);
1279 if (r < 0) {
1280 r = log_debug_errno(errno, "Failed to turn off O_NONBLOCK for %s: %m", path);
1281 goto finish;
1282 }
1283 }
1284
1285 if (fstat(fd, &st) < 0) {
1286 r = log_debug_errno(errno, "Failed to fstat file '%s': %m", path);
1287 goto finish;
1288 }
1289
1290 r = stat_verify_regular(&st);
1291 if (r < 0) {
1292 log_debug_errno(r, "Refusing to open '%s', as it is not a regular file.", path);
1293 goto finish;
1294 }
1295
1296 f = ordered_hashmap_get(j->files, path);
1297 if (f) {
1298 if (f->last_stat.st_dev == st.st_dev &&
1299 f->last_stat.st_ino == st.st_ino) {
1300
1301 /* We already track this file, under the same path and with the same device/inode numbers, it's
1302 * hence really the same. Mark this file as seen in this generation. This is used to GC old
1303 * files in process_q_overflow() to detect journal files that are still there and discern them
1304 * from those which are gone. */
1305
1306 f->last_seen_generation = j->generation;
1307 r = 0;
1308 goto finish;
1309 }
1310
1311 /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
1312 * replaced (probably due to rotation?), let's drop it hence from our list. */
1313 remove_file_real(j, f);
1314 f = NULL;
1315 }
1316
1317 if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
1318 log_debug("Too many open journal files, not adding %s.", path);
1319 r = -ETOOMANYREFS;
1320 goto finish;
1321 }
1322
1323 r = journal_file_open(fd, path, O_RDONLY, 0, false, 0, false, NULL, j->mmap, NULL, NULL, &f);
1324 if (r < 0) {
1325 log_debug_errno(r, "Failed to open journal file %s: %m", path);
1326 goto finish;
1327 }
1328
1329 /* journal_file_dump(f); */
1330
1331 r = ordered_hashmap_put(j->files, f->path, f);
1332 if (r < 0) {
1333 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 */
1334 (void) journal_file_close(f);
1335 goto finish;
1336 }
1337
1338 close_fd = false; /* the fd is now owned by the JournalFile object */
1339
1340 f->last_seen_generation = j->generation;
1341
1342 track_file_disposition(j, f);
1343 check_network(j, f->fd);
1344
1345 j->current_invalidate_counter++;
1346
1347 log_debug("File %s added.", f->path);
1348
1349 r = 0;
1350
1351 finish:
1352 if (close_fd)
1353 safe_close(fd);
1354
1355 if (r < 0) {
1356 k = journal_put_error(j, r, path);
1357 if (k < 0)
1358 return k;
1359 }
1360
1361 return r;
1362 }
1363
1364 static int add_file_by_name(
1365 sd_journal *j,
1366 const char *prefix,
1367 const char *filename) {
1368
1369 const char *path;
1370
1371 assert(j);
1372 assert(prefix);
1373 assert(filename);
1374
1375 if (j->no_new_files)
1376 return 0;
1377
1378 if (!file_type_wanted(j->flags, filename))
1379 return 0;
1380
1381 path = prefix_roota(prefix, filename);
1382 return add_any_file(j, -1, path);
1383 }
1384
1385 static void remove_file_by_name(
1386 sd_journal *j,
1387 const char *prefix,
1388 const char *filename) {
1389
1390 const char *path;
1391 JournalFile *f;
1392
1393 assert(j);
1394 assert(prefix);
1395 assert(filename);
1396
1397 path = prefix_roota(prefix, filename);
1398 f = ordered_hashmap_get(j->files, path);
1399 if (!f)
1400 return;
1401
1402 remove_file_real(j, f);
1403 }
1404
1405 static void remove_file_real(sd_journal *j, JournalFile *f) {
1406 assert(j);
1407 assert(f);
1408
1409 (void) ordered_hashmap_remove(j->files, f->path);
1410
1411 log_debug("File %s removed.", f->path);
1412
1413 if (j->current_file == f) {
1414 j->current_file = NULL;
1415 j->current_field = 0;
1416 }
1417
1418 if (j->unique_file == f) {
1419 /* Jump to the next unique_file or NULL if that one was last */
1420 j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
1421 j->unique_offset = 0;
1422 if (!j->unique_file)
1423 j->unique_file_lost = true;
1424 }
1425
1426 if (j->fields_file == f) {
1427 j->fields_file = ordered_hashmap_next(j->files, j->fields_file->path);
1428 j->fields_offset = 0;
1429 if (!j->fields_file)
1430 j->fields_file_lost = true;
1431 }
1432
1433 (void) journal_file_close(f);
1434
1435 j->current_invalidate_counter++;
1436 }
1437
1438 static int dirname_is_machine_id(const char *fn) {
1439 sd_id128_t id, machine;
1440 const char *e;
1441 int r;
1442
1443 /* Returns true if the specified directory name matches the local machine ID */
1444
1445 r = sd_id128_get_machine(&machine);
1446 if (r < 0)
1447 return r;
1448
1449 e = strchr(fn, '.');
1450 if (e) {
1451 const char *k;
1452
1453 /* Looks like it has a namespace suffix. Verify that. */
1454 if (!log_namespace_name_valid(e + 1))
1455 return false;
1456
1457 k = strndupa(fn, e - fn);
1458 r = sd_id128_from_string(k, &id);
1459 } else
1460 r = sd_id128_from_string(fn, &id);
1461 if (r < 0)
1462 return r;
1463
1464 return sd_id128_equal(id, machine);
1465 }
1466
1467 static int dirname_has_namespace(const char *fn, const char *namespace) {
1468 const char *e;
1469
1470 /* Returns true if the specified directory name matches the specified namespace */
1471
1472 e = strchr(fn, '.');
1473 if (e) {
1474 const char *k;
1475
1476 if (!namespace)
1477 return false;
1478
1479 if (!streq(e + 1, namespace))
1480 return false;
1481
1482 k = strndupa(fn, e - fn);
1483 return id128_is_valid(k);
1484 }
1485
1486 if (namespace)
1487 return false;
1488
1489 return id128_is_valid(fn);
1490 }
1491
1492 static bool dirent_is_journal_file(const struct dirent *de) {
1493 assert(de);
1494
1495 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1496
1497 if (!IN_SET(de->d_type, DT_REG, DT_LNK, DT_UNKNOWN))
1498 return false;
1499
1500 return endswith(de->d_name, ".journal") ||
1501 endswith(de->d_name, ".journal~");
1502 }
1503
1504 static bool dirent_is_journal_subdir(const struct dirent *de) {
1505 const char *e, *n;
1506 assert(de);
1507
1508 /* returns true if the specified directory entry looks like a directory that might contain journal
1509 * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a
1510 * namespace. */
1511
1512 if (!IN_SET(de->d_type, DT_DIR, DT_LNK, DT_UNKNOWN))
1513 return false;
1514
1515 e = strchr(de->d_name, '.');
1516 if (!e)
1517 return id128_is_valid(de->d_name); /* No namespace */
1518
1519 n = strndupa(de->d_name, e - de->d_name);
1520 if (!id128_is_valid(n))
1521 return false;
1522
1523 return log_namespace_name_valid(e + 1);
1524 }
1525
1526 static int directory_open(sd_journal *j, const char *path, DIR **ret) {
1527 DIR *d;
1528
1529 assert(j);
1530 assert(path);
1531 assert(ret);
1532
1533 if (j->toplevel_fd < 0)
1534 d = opendir(path);
1535 else
1536 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1537 * relative, by dropping the initial slash */
1538 d = xopendirat(j->toplevel_fd, skip_slash(path), 0);
1539 if (!d)
1540 return -errno;
1541
1542 *ret = d;
1543 return 0;
1544 }
1545
1546 static int add_directory(sd_journal *j, const char *prefix, const char *dirname);
1547
1548 static void directory_enumerate(sd_journal *j, Directory *m, DIR *d) {
1549 struct dirent *de;
1550
1551 assert(j);
1552 assert(m);
1553 assert(d);
1554
1555 FOREACH_DIRENT_ALL(de, d, goto fail) {
1556
1557 if (dirent_is_journal_file(de))
1558 (void) add_file_by_name(j, m->path, de->d_name);
1559
1560 if (m->is_root && dirent_is_journal_subdir(de))
1561 (void) add_directory(j, m->path, de->d_name);
1562 }
1563
1564 return;
1565
1566 fail:
1567 log_debug_errno(errno, "Failed to enumerate directory %s, ignoring: %m", m->path);
1568 }
1569
1570 static void directory_watch(sd_journal *j, Directory *m, int fd, uint32_t mask) {
1571 int r;
1572
1573 assert(j);
1574 assert(m);
1575 assert(fd >= 0);
1576
1577 /* Watch this directory if that's enabled and if it not being watched yet. */
1578
1579 if (m->wd > 0) /* Already have a watch? */
1580 return;
1581 if (j->inotify_fd < 0) /* Not watching at all? */
1582 return;
1583
1584 m->wd = inotify_add_watch_fd(j->inotify_fd, fd, mask);
1585 if (m->wd < 0) {
1586 log_debug_errno(errno, "Failed to watch journal directory '%s', ignoring: %m", m->path);
1587 return;
1588 }
1589
1590 r = hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m);
1591 if (r == -EEXIST)
1592 log_debug_errno(r, "Directory '%s' already being watched under a different path, ignoring: %m", m->path);
1593 if (r < 0) {
1594 log_debug_errno(r, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m->path);
1595 (void) inotify_rm_watch(j->inotify_fd, m->wd);
1596 m->wd = -1;
1597 }
1598 }
1599
1600 static int add_directory(
1601 sd_journal *j,
1602 const char *prefix,
1603 const char *dirname) {
1604
1605 _cleanup_free_ char *path = NULL;
1606 _cleanup_closedir_ DIR *d = NULL;
1607 Directory *m;
1608 int r, k;
1609
1610 assert(j);
1611 assert(prefix);
1612
1613 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1614 * and reenumerates directory contents */
1615
1616 path = path_join(prefix, dirname);
1617 if (!path) {
1618 r = -ENOMEM;
1619 goto fail;
1620 }
1621
1622 log_debug("Considering directory '%s'.", path);
1623
1624 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1625 if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
1626 !((dirname && dirname_is_machine_id(dirname) > 0) || path_has_prefix(j, path, "/run")))
1627 return 0;
1628
1629 if (!(FLAGS_SET(j->flags, SD_JOURNAL_ALL_NAMESPACES) ||
1630 dirname_has_namespace(dirname, j->namespace) > 0 ||
1631 (FLAGS_SET(j->flags, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE) && dirname_has_namespace(dirname, NULL) > 0)))
1632 return 0;
1633
1634 r = directory_open(j, path, &d);
1635 if (r < 0) {
1636 log_debug_errno(r, "Failed to open directory '%s': %m", path);
1637 goto fail;
1638 }
1639
1640 m = hashmap_get(j->directories_by_path, path);
1641 if (!m) {
1642 m = new(Directory, 1);
1643 if (!m) {
1644 r = -ENOMEM;
1645 goto fail;
1646 }
1647
1648 *m = (Directory) {
1649 .is_root = false,
1650 .path = path,
1651 };
1652
1653 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
1654 free(m);
1655 r = -ENOMEM;
1656 goto fail;
1657 }
1658
1659 path = NULL; /* avoid freeing in cleanup */
1660 j->current_invalidate_counter++;
1661
1662 log_debug("Directory %s added.", m->path);
1663
1664 } else if (m->is_root)
1665 return 0; /* Don't 'downgrade' from root directory */
1666
1667 m->last_seen_generation = j->generation;
1668
1669 directory_watch(j, m, dirfd(d),
1670 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1671 IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_MOVED_FROM|
1672 IN_ONLYDIR);
1673
1674 if (!j->no_new_files)
1675 directory_enumerate(j, m, d);
1676
1677 check_network(j, dirfd(d));
1678
1679 return 0;
1680
1681 fail:
1682 k = journal_put_error(j, r, path ?: prefix);
1683 if (k < 0)
1684 return k;
1685
1686 return r;
1687 }
1688
1689 static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) {
1690
1691 _cleanup_closedir_ DIR *d = NULL;
1692 Directory *m;
1693 int r, k;
1694
1695 assert(j);
1696
1697 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1698 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1699 * populate the set, as well as to update it later. */
1700
1701 if (p) {
1702 /* If there's a path specified, use it. */
1703
1704 log_debug("Considering root directory '%s'.", p);
1705
1706 if ((j->flags & SD_JOURNAL_RUNTIME_ONLY) &&
1707 !path_has_prefix(j, p, "/run"))
1708 return -EINVAL;
1709
1710 if (j->prefix)
1711 p = strjoina(j->prefix, p);
1712
1713 r = directory_open(j, p, &d);
1714 if (r == -ENOENT && missing_ok)
1715 return 0;
1716 if (r < 0) {
1717 log_debug_errno(r, "Failed to open root directory %s: %m", p);
1718 goto fail;
1719 }
1720 } else {
1721 int dfd;
1722
1723 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1724 * opendir() will take possession of the fd, and close it, which we don't want. */
1725
1726 p = "."; /* store this as "." in the directories hashmap */
1727
1728 dfd = fcntl(j->toplevel_fd, F_DUPFD_CLOEXEC, 3);
1729 if (dfd < 0) {
1730 r = -errno;
1731 goto fail;
1732 }
1733
1734 d = fdopendir(dfd);
1735 if (!d) {
1736 r = -errno;
1737 safe_close(dfd);
1738 goto fail;
1739 }
1740
1741 rewinddir(d);
1742 }
1743
1744 m = hashmap_get(j->directories_by_path, p);
1745 if (!m) {
1746 m = new0(Directory, 1);
1747 if (!m) {
1748 r = -ENOMEM;
1749 goto fail;
1750 }
1751
1752 m->is_root = true;
1753
1754 m->path = strdup(p);
1755 if (!m->path) {
1756 free(m);
1757 r = -ENOMEM;
1758 goto fail;
1759 }
1760
1761 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
1762 free(m->path);
1763 free(m);
1764 r = -ENOMEM;
1765 goto fail;
1766 }
1767
1768 j->current_invalidate_counter++;
1769
1770 log_debug("Root directory %s added.", m->path);
1771
1772 } else if (!m->is_root)
1773 return 0;
1774
1775 directory_watch(j, m, dirfd(d),
1776 IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
1777 IN_ONLYDIR);
1778
1779 if (!j->no_new_files)
1780 directory_enumerate(j, m, d);
1781
1782 check_network(j, dirfd(d));
1783
1784 return 0;
1785
1786 fail:
1787 k = journal_put_error(j, r, p);
1788 if (k < 0)
1789 return k;
1790
1791 return r;
1792 }
1793
1794 static void remove_directory(sd_journal *j, Directory *d) {
1795 assert(j);
1796
1797 if (d->wd > 0) {
1798 hashmap_remove(j->directories_by_wd, INT_TO_PTR(d->wd));
1799
1800 if (j->inotify_fd >= 0)
1801 (void) inotify_rm_watch(j->inotify_fd, d->wd);
1802 }
1803
1804 hashmap_remove(j->directories_by_path, d->path);
1805
1806 if (d->is_root)
1807 log_debug("Root directory %s removed.", d->path);
1808 else
1809 log_debug("Directory %s removed.", d->path);
1810
1811 free(d->path);
1812 free(d);
1813 }
1814
1815 static int add_search_paths(sd_journal *j) {
1816
1817 static const char search_paths[] =
1818 "/run/log/journal\0"
1819 "/var/log/journal\0";
1820 const char *p;
1821
1822 assert(j);
1823
1824 /* We ignore most errors here, since the idea is to only open
1825 * what's actually accessible, and ignore the rest. */
1826
1827 NULSTR_FOREACH(p, search_paths)
1828 (void) add_root_directory(j, p, true);
1829
1830 if (!(j->flags & SD_JOURNAL_LOCAL_ONLY))
1831 (void) add_root_directory(j, "/var/log/journal/remote", true);
1832
1833 return 0;
1834 }
1835
1836 static int add_current_paths(sd_journal *j) {
1837 Iterator i;
1838 JournalFile *f;
1839
1840 assert(j);
1841 assert(j->no_new_files);
1842
1843 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1844 * treat them as fatal. */
1845
1846 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
1847 _cleanup_free_ char *dir;
1848 int r;
1849
1850 dir = dirname_malloc(f->path);
1851 if (!dir)
1852 return -ENOMEM;
1853
1854 r = add_directory(j, dir, NULL);
1855 if (r < 0)
1856 return r;
1857 }
1858
1859 return 0;
1860 }
1861
1862 static int allocate_inotify(sd_journal *j) {
1863 assert(j);
1864
1865 if (j->inotify_fd < 0) {
1866 j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
1867 if (j->inotify_fd < 0)
1868 return -errno;
1869 }
1870
1871 return hashmap_ensure_allocated(&j->directories_by_wd, NULL);
1872 }
1873
1874 static sd_journal *journal_new(int flags, const char *path, const char *namespace) {
1875 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1876
1877 j = new0(sd_journal, 1);
1878 if (!j)
1879 return NULL;
1880
1881 j->original_pid = getpid_cached();
1882 j->toplevel_fd = -1;
1883 j->inotify_fd = -1;
1884 j->flags = flags;
1885 j->data_threshold = DEFAULT_DATA_THRESHOLD;
1886
1887 if (path) {
1888 char *t;
1889
1890 t = strdup(path);
1891 if (!t)
1892 return NULL;
1893
1894 if (flags & SD_JOURNAL_OS_ROOT)
1895 j->prefix = t;
1896 else
1897 j->path = t;
1898 }
1899
1900 if (namespace) {
1901 j->namespace = strdup(namespace);
1902 if (!j->namespace)
1903 return NULL;
1904 }
1905
1906 j->files = ordered_hashmap_new(&path_hash_ops);
1907 if (!j->files)
1908 return NULL;
1909
1910 j->files_cache = ordered_hashmap_iterated_cache_new(j->files);
1911 j->directories_by_path = hashmap_new(&path_hash_ops);
1912 j->mmap = mmap_cache_new();
1913 if (!j->files_cache || !j->directories_by_path || !j->mmap)
1914 return NULL;
1915
1916 return TAKE_PTR(j);
1917 }
1918
1919 #define OPEN_ALLOWED_FLAGS \
1920 (SD_JOURNAL_LOCAL_ONLY | \
1921 SD_JOURNAL_RUNTIME_ONLY | \
1922 SD_JOURNAL_SYSTEM | \
1923 SD_JOURNAL_CURRENT_USER | \
1924 SD_JOURNAL_ALL_NAMESPACES | \
1925 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
1926
1927 _public_ int sd_journal_open_namespace(sd_journal **ret, const char *namespace, int flags) {
1928 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1929 int r;
1930
1931 assert_return(ret, -EINVAL);
1932 assert_return((flags & ~OPEN_ALLOWED_FLAGS) == 0, -EINVAL);
1933
1934 j = journal_new(flags, NULL, namespace);
1935 if (!j)
1936 return -ENOMEM;
1937
1938 r = add_search_paths(j);
1939 if (r < 0)
1940 return r;
1941
1942 *ret = TAKE_PTR(j);
1943 return 0;
1944 }
1945
1946 _public_ int sd_journal_open(sd_journal **ret, int flags) {
1947 return sd_journal_open_namespace(ret, NULL, flags);
1948 }
1949
1950 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1951 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1952
1953 _public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) {
1954 _cleanup_free_ char *root = NULL, *class = NULL;
1955 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1956 char *p;
1957 int r;
1958
1959 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1960 * combination with sd_journal_open_directory_fd(). */
1961
1962 assert_return(machine, -EINVAL);
1963 assert_return(ret, -EINVAL);
1964 assert_return((flags & ~OPEN_CONTAINER_ALLOWED_FLAGS) == 0, -EINVAL);
1965 assert_return(machine_name_is_valid(machine), -EINVAL);
1966
1967 p = strjoina("/run/systemd/machines/", machine);
1968 r = parse_env_file(NULL, p,
1969 "ROOT", &root,
1970 "CLASS", &class);
1971 if (r == -ENOENT)
1972 return -EHOSTDOWN;
1973 if (r < 0)
1974 return r;
1975 if (!root)
1976 return -ENODATA;
1977
1978 if (!streq_ptr(class, "container"))
1979 return -EIO;
1980
1981 j = journal_new(flags, root, NULL);
1982 if (!j)
1983 return -ENOMEM;
1984
1985 r = add_search_paths(j);
1986 if (r < 0)
1987 return r;
1988
1989 *ret = TAKE_PTR(j);
1990 return 0;
1991 }
1992
1993 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
1994 (SD_JOURNAL_OS_ROOT | \
1995 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1996
1997 _public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
1998 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1999 int r;
2000
2001 assert_return(ret, -EINVAL);
2002 assert_return(path, -EINVAL);
2003 assert_return((flags & ~OPEN_DIRECTORY_ALLOWED_FLAGS) == 0, -EINVAL);
2004
2005 j = journal_new(flags, path, NULL);
2006 if (!j)
2007 return -ENOMEM;
2008
2009 if (flags & SD_JOURNAL_OS_ROOT)
2010 r = add_search_paths(j);
2011 else
2012 r = add_root_directory(j, path, false);
2013 if (r < 0)
2014 return r;
2015
2016 *ret = TAKE_PTR(j);
2017 return 0;
2018 }
2019
2020 _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) {
2021 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
2022 const char **path;
2023 int r;
2024
2025 assert_return(ret, -EINVAL);
2026 assert_return(flags == 0, -EINVAL);
2027
2028 j = journal_new(flags, NULL, NULL);
2029 if (!j)
2030 return -ENOMEM;
2031
2032 STRV_FOREACH(path, paths) {
2033 r = add_any_file(j, -1, *path);
2034 if (r < 0)
2035 return r;
2036 }
2037
2038 j->no_new_files = true;
2039
2040 *ret = TAKE_PTR(j);
2041 return 0;
2042 }
2043
2044 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
2045 (SD_JOURNAL_OS_ROOT | \
2046 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2047
2048 _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
2049 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
2050 struct stat st;
2051 int r;
2052
2053 assert_return(ret, -EINVAL);
2054 assert_return(fd >= 0, -EBADF);
2055 assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL);
2056
2057 if (fstat(fd, &st) < 0)
2058 return -errno;
2059
2060 if (!S_ISDIR(st.st_mode))
2061 return -EBADFD;
2062
2063 j = journal_new(flags, NULL, NULL);
2064 if (!j)
2065 return -ENOMEM;
2066
2067 j->toplevel_fd = fd;
2068
2069 if (flags & SD_JOURNAL_OS_ROOT)
2070 r = add_search_paths(j);
2071 else
2072 r = add_root_directory(j, NULL, false);
2073 if (r < 0)
2074 return r;
2075
2076 *ret = TAKE_PTR(j);
2077 return 0;
2078 }
2079
2080 _public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) {
2081 Iterator iterator;
2082 JournalFile *f;
2083 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
2084 unsigned i;
2085 int r;
2086
2087 assert_return(ret, -EINVAL);
2088 assert_return(n_fds > 0, -EBADF);
2089 assert_return(flags == 0, -EINVAL);
2090
2091 j = journal_new(flags, NULL, NULL);
2092 if (!j)
2093 return -ENOMEM;
2094
2095 for (i = 0; i < n_fds; i++) {
2096 struct stat st;
2097
2098 if (fds[i] < 0) {
2099 r = -EBADF;
2100 goto fail;
2101 }
2102
2103 if (fstat(fds[i], &st) < 0) {
2104 r = -errno;
2105 goto fail;
2106 }
2107
2108 r = stat_verify_regular(&st);
2109 if (r < 0)
2110 goto fail;
2111
2112 r = add_any_file(j, fds[i], NULL);
2113 if (r < 0)
2114 goto fail;
2115 }
2116
2117 j->no_new_files = true;
2118 j->no_inotify = true;
2119
2120 *ret = TAKE_PTR(j);
2121 return 0;
2122
2123 fail:
2124 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2125 * remain open */
2126 ORDERED_HASHMAP_FOREACH(f, j->files, iterator)
2127 f->close_fd = false;
2128
2129 return r;
2130 }
2131
2132 _public_ void sd_journal_close(sd_journal *j) {
2133 Directory *d;
2134
2135 if (!j)
2136 return;
2137
2138 sd_journal_flush_matches(j);
2139
2140 ordered_hashmap_free_with_destructor(j->files, journal_file_close);
2141 iterated_cache_free(j->files_cache);
2142
2143 while ((d = hashmap_first(j->directories_by_path)))
2144 remove_directory(j, d);
2145
2146 while ((d = hashmap_first(j->directories_by_wd)))
2147 remove_directory(j, d);
2148
2149 hashmap_free(j->directories_by_path);
2150 hashmap_free(j->directories_by_wd);
2151
2152 safe_close(j->inotify_fd);
2153
2154 if (j->mmap) {
2155 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap));
2156 mmap_cache_unref(j->mmap);
2157 }
2158
2159 hashmap_free_free(j->errors);
2160
2161 free(j->path);
2162 free(j->prefix);
2163 free(j->namespace);
2164 free(j->unique_field);
2165 free(j->fields_buffer);
2166 free(j);
2167 }
2168
2169 _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
2170 Object *o;
2171 JournalFile *f;
2172 int r;
2173
2174 assert_return(j, -EINVAL);
2175 assert_return(!journal_pid_changed(j), -ECHILD);
2176 assert_return(ret, -EINVAL);
2177
2178 f = j->current_file;
2179 if (!f)
2180 return -EADDRNOTAVAIL;
2181
2182 if (f->current_offset <= 0)
2183 return -EADDRNOTAVAIL;
2184
2185 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2186 if (r < 0)
2187 return r;
2188
2189 *ret = le64toh(o->entry.realtime);
2190 return 0;
2191 }
2192
2193 _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
2194 Object *o;
2195 JournalFile *f;
2196 int r;
2197 sd_id128_t id;
2198
2199 assert_return(j, -EINVAL);
2200 assert_return(!journal_pid_changed(j), -ECHILD);
2201
2202 f = j->current_file;
2203 if (!f)
2204 return -EADDRNOTAVAIL;
2205
2206 if (f->current_offset <= 0)
2207 return -EADDRNOTAVAIL;
2208
2209 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2210 if (r < 0)
2211 return r;
2212
2213 if (ret_boot_id)
2214 *ret_boot_id = o->entry.boot_id;
2215 else {
2216 r = sd_id128_get_boot(&id);
2217 if (r < 0)
2218 return r;
2219
2220 if (!sd_id128_equal(id, o->entry.boot_id))
2221 return -ESTALE;
2222 }
2223
2224 if (ret)
2225 *ret = le64toh(o->entry.monotonic);
2226
2227 return 0;
2228 }
2229
2230 static bool field_is_valid(const char *field) {
2231 const char *p;
2232
2233 assert(field);
2234
2235 if (isempty(field))
2236 return false;
2237
2238 if (startswith(field, "__"))
2239 return false;
2240
2241 for (p = field; *p; p++) {
2242
2243 if (*p == '_')
2244 continue;
2245
2246 if (*p >= 'A' && *p <= 'Z')
2247 continue;
2248
2249 if (*p >= '0' && *p <= '9')
2250 continue;
2251
2252 return false;
2253 }
2254
2255 return true;
2256 }
2257
2258 _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
2259 JournalFile *f;
2260 uint64_t i, n;
2261 size_t field_length;
2262 int r;
2263 Object *o;
2264
2265 assert_return(j, -EINVAL);
2266 assert_return(!journal_pid_changed(j), -ECHILD);
2267 assert_return(field, -EINVAL);
2268 assert_return(data, -EINVAL);
2269 assert_return(size, -EINVAL);
2270 assert_return(field_is_valid(field), -EINVAL);
2271
2272 f = j->current_file;
2273 if (!f)
2274 return -EADDRNOTAVAIL;
2275
2276 if (f->current_offset <= 0)
2277 return -EADDRNOTAVAIL;
2278
2279 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2280 if (r < 0)
2281 return r;
2282
2283 field_length = strlen(field);
2284
2285 n = journal_file_entry_n_items(o);
2286 for (i = 0; i < n; i++) {
2287 uint64_t p, l;
2288 le64_t le_hash;
2289 size_t t;
2290 int compression;
2291
2292 p = le64toh(o->entry.items[i].object_offset);
2293 le_hash = o->entry.items[i].hash;
2294 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
2295 if (r < 0)
2296 return r;
2297
2298 if (le_hash != o->data.hash)
2299 return -EBADMSG;
2300
2301 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2302
2303 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
2304 if (compression) {
2305 #if HAVE_XZ || HAVE_LZ4
2306 r = decompress_startswith(compression,
2307 o->data.payload, l,
2308 &f->compress_buffer, &f->compress_buffer_size,
2309 field, field_length, '=');
2310 if (r < 0)
2311 log_debug_errno(r, "Cannot decompress %s object of length %"PRIu64" at offset "OFSfmt": %m",
2312 object_compressed_to_string(compression), l, p);
2313 else if (r > 0) {
2314
2315 size_t rsize;
2316
2317 r = decompress_blob(compression,
2318 o->data.payload, l,
2319 &f->compress_buffer, &f->compress_buffer_size, &rsize,
2320 j->data_threshold);
2321 if (r < 0)
2322 return r;
2323
2324 *data = f->compress_buffer;
2325 *size = (size_t) rsize;
2326
2327 return 0;
2328 }
2329 #else
2330 return -EPROTONOSUPPORT;
2331 #endif
2332 } else if (l >= field_length+1 &&
2333 memcmp(o->data.payload, field, field_length) == 0 &&
2334 o->data.payload[field_length] == '=') {
2335
2336 t = (size_t) l;
2337
2338 if ((uint64_t) t != l)
2339 return -E2BIG;
2340
2341 *data = o->data.payload;
2342 *size = t;
2343
2344 return 0;
2345 }
2346
2347 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2348 if (r < 0)
2349 return r;
2350 }
2351
2352 return -ENOENT;
2353 }
2354
2355 static int return_data(sd_journal *j, JournalFile *f, Object *o, const void **data, size_t *size) {
2356 size_t t;
2357 uint64_t l;
2358 int compression;
2359
2360 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2361 t = (size_t) l;
2362
2363 /* We can't read objects larger than 4G on a 32bit machine */
2364 if ((uint64_t) t != l)
2365 return -E2BIG;
2366
2367 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
2368 if (compression) {
2369 #if HAVE_XZ || HAVE_LZ4
2370 size_t rsize;
2371 int r;
2372
2373 r = decompress_blob(compression,
2374 o->data.payload, l, &f->compress_buffer,
2375 &f->compress_buffer_size, &rsize, j->data_threshold);
2376 if (r < 0)
2377 return r;
2378
2379 *data = f->compress_buffer;
2380 *size = (size_t) rsize;
2381 #else
2382 return -EPROTONOSUPPORT;
2383 #endif
2384 } else {
2385 *data = o->data.payload;
2386 *size = t;
2387 }
2388
2389 return 0;
2390 }
2391
2392 _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
2393 JournalFile *f;
2394 uint64_t p, n;
2395 le64_t le_hash;
2396 int r;
2397 Object *o;
2398
2399 assert_return(j, -EINVAL);
2400 assert_return(!journal_pid_changed(j), -ECHILD);
2401 assert_return(data, -EINVAL);
2402 assert_return(size, -EINVAL);
2403
2404 f = j->current_file;
2405 if (!f)
2406 return -EADDRNOTAVAIL;
2407
2408 if (f->current_offset <= 0)
2409 return -EADDRNOTAVAIL;
2410
2411 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2412 if (r < 0)
2413 return r;
2414
2415 n = journal_file_entry_n_items(o);
2416 if (j->current_field >= n)
2417 return 0;
2418
2419 p = le64toh(o->entry.items[j->current_field].object_offset);
2420 le_hash = o->entry.items[j->current_field].hash;
2421 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
2422 if (r < 0)
2423 return r;
2424
2425 if (le_hash != o->data.hash)
2426 return -EBADMSG;
2427
2428 r = return_data(j, f, o, data, size);
2429 if (r < 0)
2430 return r;
2431
2432 j->current_field++;
2433
2434 return 1;
2435 }
2436
2437 _public_ void sd_journal_restart_data(sd_journal *j) {
2438 if (!j)
2439 return;
2440
2441 j->current_field = 0;
2442 }
2443
2444 static int reiterate_all_paths(sd_journal *j) {
2445 assert(j);
2446
2447 if (j->no_new_files)
2448 return add_current_paths(j);
2449
2450 if (j->flags & SD_JOURNAL_OS_ROOT)
2451 return add_search_paths(j);
2452
2453 if (j->toplevel_fd >= 0)
2454 return add_root_directory(j, NULL, false);
2455
2456 if (j->path)
2457 return add_root_directory(j, j->path, true);
2458
2459 return add_search_paths(j);
2460 }
2461
2462 _public_ int sd_journal_get_fd(sd_journal *j) {
2463 int r;
2464
2465 assert_return(j, -EINVAL);
2466 assert_return(!journal_pid_changed(j), -ECHILD);
2467
2468 if (j->no_inotify)
2469 return -EMEDIUMTYPE;
2470
2471 if (j->inotify_fd >= 0)
2472 return j->inotify_fd;
2473
2474 r = allocate_inotify(j);
2475 if (r < 0)
2476 return r;
2477
2478 log_debug("Reiterating files to get inotify watches established.");
2479
2480 /* Iterate through all dirs again, to add them to the inotify */
2481 r = reiterate_all_paths(j);
2482 if (r < 0)
2483 return r;
2484
2485 return j->inotify_fd;
2486 }
2487
2488 _public_ int sd_journal_get_events(sd_journal *j) {
2489 int fd;
2490
2491 assert_return(j, -EINVAL);
2492 assert_return(!journal_pid_changed(j), -ECHILD);
2493
2494 fd = sd_journal_get_fd(j);
2495 if (fd < 0)
2496 return fd;
2497
2498 return POLLIN;
2499 }
2500
2501 _public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
2502 int fd;
2503
2504 assert_return(j, -EINVAL);
2505 assert_return(!journal_pid_changed(j), -ECHILD);
2506 assert_return(timeout_usec, -EINVAL);
2507
2508 fd = sd_journal_get_fd(j);
2509 if (fd < 0)
2510 return fd;
2511
2512 if (!j->on_network) {
2513 *timeout_usec = (uint64_t) -1;
2514 return 0;
2515 }
2516
2517 /* If we are on the network we need to regularly check for
2518 * changes manually */
2519
2520 *timeout_usec = j->last_process_usec + JOURNAL_FILES_RECHECK_USEC;
2521 return 1;
2522 }
2523
2524 static void process_q_overflow(sd_journal *j) {
2525 JournalFile *f;
2526 Directory *m;
2527 Iterator i;
2528
2529 assert(j);
2530
2531 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2532 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2533 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2534 * are subject for unloading. */
2535
2536 log_debug("Inotify queue overrun, reiterating everything.");
2537
2538 j->generation++;
2539 (void) reiterate_all_paths(j);
2540
2541 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2542
2543 if (f->last_seen_generation == j->generation)
2544 continue;
2545
2546 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f->path);
2547 remove_file_real(j, f);
2548 }
2549
2550 HASHMAP_FOREACH(m, j->directories_by_path, i) {
2551
2552 if (m->last_seen_generation == j->generation)
2553 continue;
2554
2555 if (m->is_root) /* Never GC root directories */
2556 continue;
2557
2558 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f->path);
2559 remove_directory(j, m);
2560 }
2561
2562 log_debug("Reiteration complete.");
2563 }
2564
2565 static void process_inotify_event(sd_journal *j, const struct inotify_event *e) {
2566 Directory *d;
2567
2568 assert(j);
2569 assert(e);
2570
2571 if (e->mask & IN_Q_OVERFLOW) {
2572 process_q_overflow(j);
2573 return;
2574 }
2575
2576 /* Is this a subdirectory we watch? */
2577 d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd));
2578 if (d) {
2579 if (!(e->mask & IN_ISDIR) && e->len > 0 &&
2580 (endswith(e->name, ".journal") ||
2581 endswith(e->name, ".journal~"))) {
2582
2583 /* Event for a journal file */
2584
2585 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2586 (void) add_file_by_name(j, d->path, e->name);
2587 else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT))
2588 remove_file_by_name(j, d->path, e->name);
2589
2590 } else if (!d->is_root && e->len == 0) {
2591
2592 /* Event for a subdirectory */
2593
2594 if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
2595 remove_directory(j, d);
2596
2597 } else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && id128_is_valid(e->name)) {
2598
2599 /* Event for root directory */
2600
2601 if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
2602 (void) add_directory(j, d->path, e->name);
2603 }
2604
2605 return;
2606 }
2607
2608 if (e->mask & IN_IGNORED)
2609 return;
2610
2611 log_debug("Unexpected inotify event.");
2612 }
2613
2614 static int determine_change(sd_journal *j) {
2615 bool b;
2616
2617 assert(j);
2618
2619 b = j->current_invalidate_counter != j->last_invalidate_counter;
2620 j->last_invalidate_counter = j->current_invalidate_counter;
2621
2622 return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND;
2623 }
2624
2625 _public_ int sd_journal_process(sd_journal *j) {
2626 bool got_something = false;
2627
2628 assert_return(j, -EINVAL);
2629 assert_return(!journal_pid_changed(j), -ECHILD);
2630
2631 if (j->inotify_fd < 0) /* We have no inotify fd yet? Then there's noting to process. */
2632 return 0;
2633
2634 j->last_process_usec = now(CLOCK_MONOTONIC);
2635 j->last_invalidate_counter = j->current_invalidate_counter;
2636
2637 for (;;) {
2638 union inotify_event_buffer buffer;
2639 struct inotify_event *e;
2640 ssize_t l;
2641
2642 l = read(j->inotify_fd, &buffer, sizeof(buffer));
2643 if (l < 0) {
2644 if (IN_SET(errno, EAGAIN, EINTR))
2645 return got_something ? determine_change(j) : SD_JOURNAL_NOP;
2646
2647 return -errno;
2648 }
2649
2650 got_something = true;
2651
2652 FOREACH_INOTIFY_EVENT(e, buffer, l)
2653 process_inotify_event(j, e);
2654 }
2655 }
2656
2657 _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
2658 int r;
2659 uint64_t t;
2660
2661 assert_return(j, -EINVAL);
2662 assert_return(!journal_pid_changed(j), -ECHILD);
2663
2664 if (j->inotify_fd < 0) {
2665 Iterator i;
2666 JournalFile *f;
2667
2668 /* This is the first invocation, hence create the
2669 * inotify watch */
2670 r = sd_journal_get_fd(j);
2671 if (r < 0)
2672 return r;
2673
2674 /* Server might have done some vacuuming while we weren't watching.
2675 Get rid of the deleted files now so they don't stay around indefinitely. */
2676 ORDERED_HASHMAP_FOREACH(f, j->files, i) {
2677 r = journal_file_fstat(f);
2678 if (r == -EIDRM)
2679 remove_file_real(j, f);
2680 else if (r < 0) {
2681 log_debug_errno(r,"Failed to fstat() journal file '%s' : %m", f->path);
2682 continue;
2683 }
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 }