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