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