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