1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <linux/magic.h>
9 #include <sys/inotify.h>
13 #include "sd-journal.h"
15 #include "alloc-util.h"
18 #include "dirent-util.h"
23 #include "format-util.h"
26 #include "hostname-util.h"
27 #include "id128-util.h"
28 #include "inotify-util.h"
30 #include "journal-def.h"
31 #include "journal-file.h"
32 #include "journal-internal.h"
35 #include "nulstr-util.h"
36 #include "origin-id.h"
37 #include "path-util.h"
39 #include "process-util.h"
40 #include "replace-var.h"
41 #include "sort-util.h"
42 #include "stat-util.h"
43 #include "stdio-util.h"
44 #include "string-util.h"
46 #include "syslog-util.h"
47 #include "uid-classification.h"
49 #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
51 /* The maximum size of variable values we'll expand in catalog entries. We bind this to PATH_MAX for now, as
52 * we want to be able to show all officially valid paths at least */
53 #define REPLACE_VAR_MAX PATH_MAX
55 #define DEFAULT_DATA_THRESHOLD (64*1024)
57 DEFINE_PRIVATE_ORIGIN_ID_HELPERS(sd_journal
, journal
);
59 static void remove_file_real(sd_journal
*j
, JournalFile
*f
);
60 static int journal_file_read_tail_timestamp(sd_journal
*j
, JournalFile
*f
);
61 static void journal_file_unlink_newest_by_boot_id(sd_journal
*j
, JournalFile
*f
);
63 static int journal_put_error(sd_journal
*j
, int r
, const char *path
) {
64 _cleanup_free_
char *copy
= NULL
;
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
75 * We return an error here only if we didn't manage to
76 * memorize the real error. */
87 k
= hashmap_ensure_put(&j
->errors
, NULL
, INT_TO_PTR(r
), copy
);
99 static void detach_location(sd_journal
*j
) {
104 j
->current_file
= NULL
;
105 j
->current_field
= 0;
107 ORDERED_HASHMAP_FOREACH(f
, j
->files
)
108 journal_file_reset_location(f
);
111 static void init_location(Location
*l
, LocationType type
, JournalFile
*f
, Object
*o
) {
113 assert(IN_SET(type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
118 .seqnum
= le64toh(o
->entry
.seqnum
),
119 .seqnum_id
= f
->header
->seqnum_id
,
120 .realtime
= le64toh(o
->entry
.realtime
),
121 .monotonic
= le64toh(o
->entry
.monotonic
),
122 .boot_id
= o
->entry
.boot_id
,
123 .xor_hash
= le64toh(o
->entry
.xor_hash
),
125 .realtime_set
= true,
126 .monotonic_set
= true,
127 .xor_hash_set
= true,
131 static void set_location(sd_journal
*j
, JournalFile
*f
, Object
*o
) {
136 init_location(&j
->current_location
, LOCATION_DISCRETE
, f
, o
);
139 j
->current_field
= 0;
141 /* Let f know its candidate entry was picked. */
142 assert(f
->location_type
== LOCATION_SEEK
);
143 f
->location_type
= LOCATION_DISCRETE
;
146 static int match_is_valid(const void *data
, size_t size
) {
147 const char *b
= ASSERT_PTR(data
);
152 if (((char*) data
)[0] == '_' && ((char*) data
)[1] == '_')
155 for (const char *p
= b
; p
< b
+ size
; p
++) {
163 if (*p
>= 'A' && *p
<= 'Z')
166 if (ascii_isdigit(*p
))
175 static bool same_field(const void *_a
, size_t s
, const void *_b
, size_t t
) {
176 const uint8_t *a
= _a
, *b
= _b
;
178 for (size_t j
= 0; j
< s
&& j
< t
; j
++) {
187 assert_not_reached();
190 static Match
*match_new(Match
*p
, MatchType t
) {
203 LIST_PREPEND(matches
, p
->matches
, m
);
208 static Match
*match_free(Match
*m
) {
212 match_free(m
->matches
);
215 LIST_REMOVE(matches
, m
->parent
->matches
, m
);
221 static Match
*match_free_if_empty(Match
*m
) {
222 if (!m
|| m
->matches
)
225 return match_free(m
);
228 _public_
int sd_journal_add_match(sd_journal
*j
, const void *data
, size_t size
) {
229 Match
*add_here
= NULL
, *m
= NULL
;
232 assert_return(j
, -EINVAL
);
233 assert_return(!journal_origin_changed(j
), -ECHILD
);
234 assert_return(data
, -EINVAL
);
239 if (!match_is_valid(data
, size
))
246 * level 4: concrete matches */
249 j
->level0
= match_new(NULL
, MATCH_AND_TERM
);
255 j
->level1
= match_new(j
->level0
, MATCH_OR_TERM
);
261 j
->level2
= match_new(j
->level1
, MATCH_AND_TERM
);
266 assert(j
->level0
->type
== MATCH_AND_TERM
);
267 assert(j
->level1
->type
== MATCH_OR_TERM
);
268 assert(j
->level2
->type
== MATCH_AND_TERM
);
270 /* Old-style Jenkins (unkeyed) hashing only here. We do not cover new-style siphash (keyed) hashing
271 * here, since it's different for each file, and thus can't be pre-calculated in the Match object. */
272 hash
= jenkins_hash64(data
, size
);
274 LIST_FOREACH(matches
, l3
, j
->level2
->matches
) {
275 assert(l3
->type
== MATCH_OR_TERM
);
277 LIST_FOREACH(matches
, l4
, l3
->matches
) {
278 assert(l4
->type
== MATCH_DISCRETE
);
280 /* Exactly the same match already? Then ignore
282 if (l4
->hash
== hash
&&
284 memcmp(l4
->data
, data
, size
) == 0)
287 /* Same field? Then let's add this to this OR term */
288 if (same_field(data
, size
, l4
->data
, l4
->size
)) {
299 add_here
= match_new(j
->level2
, MATCH_OR_TERM
);
304 m
= match_new(add_here
, MATCH_DISCRETE
);
310 m
->data
= memdup(data
, size
);
320 match_free_if_empty(add_here
);
321 j
->level2
= match_free_if_empty(j
->level2
);
322 j
->level1
= match_free_if_empty(j
->level1
);
323 j
->level0
= match_free_if_empty(j
->level0
);
328 int journal_add_match_pair(sd_journal
*j
, const char *field
, const char *value
) {
329 _cleanup_free_
char *s
= NULL
;
335 s
= strjoin(field
, "=", value
);
339 return sd_journal_add_match(j
, s
, 0);
342 int journal_add_matchf(sd_journal
*j
, const char *format
, ...) {
343 _cleanup_free_
char *s
= NULL
;
350 va_start(ap
, format
);
351 r
= vasprintf(&s
, format
, ap
);
356 return sd_journal_add_match(j
, s
, 0);
359 _public_
int sd_journal_add_conjunction(sd_journal
*j
) {
360 assert_return(j
, -EINVAL
);
361 assert_return(!journal_origin_changed(j
), -ECHILD
);
369 if (!j
->level1
->matches
)
378 _public_
int sd_journal_add_disjunction(sd_journal
*j
) {
379 assert_return(j
, -EINVAL
);
380 assert_return(!journal_origin_changed(j
), -ECHILD
);
391 if (!j
->level2
->matches
)
398 static char *match_make_string(Match
*m
) {
399 _cleanup_free_
char *p
= NULL
;
400 bool enclose
= false;
403 return strdup("none");
405 if (m
->type
== MATCH_DISCRETE
)
406 return cescape_length(m
->data
, m
->size
);
408 LIST_FOREACH(matches
, i
, m
->matches
) {
409 _cleanup_free_
char *t
= NULL
;
411 t
= match_make_string(i
);
416 if (!strextend(&p
, m
->type
== MATCH_OR_TERM
? " OR " : " AND ", t
))
425 return strjoin("(", p
, ")");
430 char *journal_make_match_string(sd_journal
*j
) {
433 return match_make_string(j
->level0
);
436 _public_
void sd_journal_flush_matches(sd_journal
*j
) {
437 if (!j
|| journal_origin_changed(j
))
441 match_free(j
->level0
);
443 j
->level0
= j
->level1
= j
->level2
= NULL
;
448 static int newest_by_boot_id_compare(const NewestByBootId
*a
, const NewestByBootId
*b
) {
449 return id128_compare_func(&a
->boot_id
, &b
->boot_id
);
452 static void journal_file_unlink_newest_by_boot_id(sd_journal
*j
, JournalFile
*f
) {
453 NewestByBootId
*found
;
458 if (f
->newest_boot_id_prioq_idx
== PRIOQ_IDX_NULL
) /* not linked currently, hence this is a NOP */
461 found
= typesafe_bsearch(&(NewestByBootId
) { .boot_id
= f
->newest_boot_id
},
462 j
->newest_by_boot_id
, j
->n_newest_by_boot_id
, newest_by_boot_id_compare
);
465 assert_se(prioq_remove(found
->prioq
, f
, &f
->newest_boot_id_prioq_idx
) > 0);
466 f
->newest_boot_id_prioq_idx
= PRIOQ_IDX_NULL
;
468 /* The prioq may be empty, but that should not cause any issue. Let's keep it. */
471 static void journal_clear_newest_by_boot_id(sd_journal
*j
) {
472 FOREACH_ARRAY(i
, j
->newest_by_boot_id
, j
->n_newest_by_boot_id
) {
475 while ((f
= prioq_peek(i
->prioq
)))
476 journal_file_unlink_newest_by_boot_id(j
, f
);
478 prioq_free(i
->prioq
);
481 j
->newest_by_boot_id
= mfree(j
->newest_by_boot_id
);
482 j
->n_newest_by_boot_id
= 0;
485 static int journal_file_newest_monotonic_compare(const void *a
, const void *b
) {
486 const JournalFile
*x
= a
, *y
= b
;
488 return -CMP(x
->newest_monotonic_usec
, y
->newest_monotonic_usec
); /* Invert order, we want newest first! */
491 static int journal_file_reshuffle_newest_by_boot_id(sd_journal
*j
, JournalFile
*f
) {
492 NewestByBootId
*found
;
498 found
= typesafe_bsearch(&(NewestByBootId
) { .boot_id
= f
->newest_boot_id
},
499 j
->newest_by_boot_id
, j
->n_newest_by_boot_id
, newest_by_boot_id_compare
);
501 /* There's already a priority queue for this boot ID */
503 if (f
->newest_boot_id_prioq_idx
== PRIOQ_IDX_NULL
) {
504 r
= prioq_put(found
->prioq
, f
, &f
->newest_boot_id_prioq_idx
); /* Insert if we aren't in there yet */
508 prioq_reshuffle(found
->prioq
, f
, &f
->newest_boot_id_prioq_idx
); /* Reshuffle otherwise */
511 _cleanup_(prioq_freep
) Prioq
*q
= NULL
;
513 /* No priority queue yet, then allocate one */
515 assert(f
->newest_boot_id_prioq_idx
== PRIOQ_IDX_NULL
); /* we can't be a member either */
517 q
= prioq_new(journal_file_newest_monotonic_compare
);
521 r
= prioq_put(q
, f
, &f
->newest_boot_id_prioq_idx
);
525 if (!GREEDY_REALLOC(j
->newest_by_boot_id
, j
->n_newest_by_boot_id
+ 1)) {
526 f
->newest_boot_id_prioq_idx
= PRIOQ_IDX_NULL
;
530 j
->newest_by_boot_id
[j
->n_newest_by_boot_id
++] = (NewestByBootId
) {
531 .boot_id
= f
->newest_boot_id
,
532 .prioq
= TAKE_PTR(q
),
535 typesafe_qsort(j
->newest_by_boot_id
, j
->n_newest_by_boot_id
, newest_by_boot_id_compare
);
541 static int journal_file_find_newest_for_boot_id(
546 JournalFile
*prev
= NULL
;
552 /* Before we use it, let's refresh the timestamp from the header, and reshuffle our prioq
553 * accordingly. We do this only a bunch of times, to not be caught in some update loop. */
554 for (unsigned n_tries
= 0;; n_tries
++) {
555 NewestByBootId
*found
;
558 found
= typesafe_bsearch(&(NewestByBootId
) { .boot_id
= id
},
559 j
->newest_by_boot_id
, j
->n_newest_by_boot_id
, newest_by_boot_id_compare
);
561 f
= found
? prioq_peek(found
->prioq
) : NULL
;
563 return log_debug_errno(SYNTHETIC_ERRNO(ENODATA
),
564 "Requested delta for boot ID %s, but we have no information about that boot ID.", SD_ID128_TO_STRING(id
));
566 if (f
== prev
|| n_tries
>= 5) {
567 /* This was already the best answer in the previous run, or we tried too often, use it */
574 /* Let's read the journal file's current timestamp once, before we return it, maybe it has changed. */
575 r
= journal_file_read_tail_timestamp(j
, f
);
577 return log_debug_errno(r
, "Failed to read tail timestamp while trying to find newest journal file for boot ID %s.", SD_ID128_TO_STRING(id
));
579 /* No new entry found. */
584 /* Refreshing the timestamp we read might have reshuffled the prioq, hence let's check the
585 * prioq again and only use the information once we reached an equilibrium or hit a limit */
589 static int compare_boot_ids(sd_journal
*j
, sd_id128_t a
, sd_id128_t b
) {
594 /* Try to find the newest open journal file for the two boot ids */
595 if (journal_file_find_newest_for_boot_id(j
, a
, &x
) < 0 ||
596 journal_file_find_newest_for_boot_id(j
, b
, &y
) < 0)
599 /* Only compare the boot id timestamps if they originate from the same machine. If they are from
600 * different machines, then we timestamps of the boot ids might be as off as the timestamps on the
601 * entries and hence not useful for comparing. */
602 if (!sd_id128_equal(x
->newest_machine_id
, y
->newest_machine_id
))
605 return CMP(x
->newest_realtime_usec
, y
->newest_realtime_usec
);
608 static int compare_with_location(
610 const JournalFile
*f
,
612 const JournalFile
*current_file
) {
618 assert(f
->location_type
== LOCATION_SEEK
);
619 assert(IN_SET(l
->type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
621 if (l
->monotonic_set
&&
622 sd_id128_equal(f
->current_boot_id
, l
->boot_id
) &&
624 f
->current_realtime
== l
->realtime
&&
626 f
->current_xor_hash
== l
->xor_hash
&&
628 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
) &&
629 f
->current_seqnum
== l
->seqnum
&&
634 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
)) {
635 r
= CMP(f
->current_seqnum
, l
->seqnum
);
640 if (l
->monotonic_set
) {
641 /* If both arguments have the same boot ID, then we can compare the monotonic timestamps. If
642 * they are distinct, then we might able to lookup the timestamps of those boot IDs (if they
643 * are from the same machine) and order by that. */
644 if (sd_id128_equal(f
->current_boot_id
, l
->boot_id
))
645 r
= CMP(f
->current_monotonic
, l
->monotonic
);
647 r
= compare_boot_ids(j
, f
->current_boot_id
, l
->boot_id
);
652 if (l
->realtime_set
) {
653 r
= CMP(f
->current_realtime
, l
->realtime
);
658 if (l
->xor_hash_set
) {
659 r
= CMP(f
->current_xor_hash
, l
->xor_hash
);
667 static int next_for_match(
671 uint64_t after_offset
,
672 direction_t direction
,
683 if (m
->type
== MATCH_DISCRETE
) {
687 /* If the keyed hash logic is used, we need to calculate the hash fresh per file. Otherwise
688 * we can use what we pre-calculated. */
689 if (JOURNAL_HEADER_KEYED_HASH(f
->header
))
690 hash
= journal_file_hash_data(f
, m
->data
, m
->size
);
694 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, hash
, &d
, NULL
);
698 return journal_file_move_to_entry_by_offset_for_data(f
, d
, after_offset
, direction
, ret
, offset
);
700 } else if (m
->type
== MATCH_OR_TERM
) {
702 /* Find the earliest match beyond after_offset */
704 LIST_FOREACH(matches
, i
, m
->matches
) {
707 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
711 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
719 } else if (m
->type
== MATCH_AND_TERM
) {
722 /* Always jump to the next matching entry and repeat
723 * this until we find an offset that matches for all
729 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
733 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
734 last_moved
= m
->matches
;
736 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
739 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
743 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
744 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
754 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, ret
);
765 static int find_location_for_match(
769 direction_t direction
,
779 if (m
->type
== MATCH_DISCRETE
) {
783 if (JOURNAL_HEADER_KEYED_HASH(f
->header
))
784 hash
= journal_file_hash_data(f
, m
->data
, m
->size
);
788 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, hash
, &d
, &dp
);
792 /* FIXME: missing: find by monotonic */
794 if (j
->current_location
.type
== LOCATION_HEAD
)
795 return direction
== DIRECTION_DOWN
? journal_file_move_to_entry_for_data(f
, d
, DIRECTION_DOWN
, ret
, offset
) : 0;
796 if (j
->current_location
.type
== LOCATION_TAIL
)
797 return direction
== DIRECTION_UP
? journal_file_move_to_entry_for_data(f
, d
, DIRECTION_UP
, ret
, offset
) : 0;
798 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
799 return journal_file_move_to_entry_by_seqnum_for_data(f
, d
, j
->current_location
.seqnum
, direction
, ret
, offset
);
800 if (j
->current_location
.monotonic_set
) {
801 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, d
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
805 /* The data object might have been invalidated. */
806 r
= journal_file_move_to_object(f
, OBJECT_DATA
, dp
, &d
);
810 if (j
->current_location
.realtime_set
)
811 return journal_file_move_to_entry_by_realtime_for_data(f
, d
, j
->current_location
.realtime
, direction
, ret
, offset
);
813 return journal_file_move_to_entry_for_data(f
, d
, direction
, ret
, offset
);
815 } else if (m
->type
== MATCH_OR_TERM
) {
818 /* Find the earliest match */
820 LIST_FOREACH(matches
, i
, m
->matches
) {
823 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
827 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
836 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, ret
);
849 assert(m
->type
== MATCH_AND_TERM
);
851 /* First jump to the last match, and then find the
852 * next one where all matches match */
857 LIST_FOREACH(matches
, i
, m
->matches
) {
860 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
864 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
868 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
872 static int find_location_with_matches(
875 direction_t direction
,
887 /* No matches is simple */
889 if (j
->current_location
.type
== LOCATION_HEAD
)
890 return direction
== DIRECTION_DOWN
? journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
) : 0;
891 if (j
->current_location
.type
== LOCATION_TAIL
)
892 return direction
== DIRECTION_UP
? journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
) : 0;
893 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
894 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
895 if (j
->current_location
.monotonic_set
) {
896 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
900 if (j
->current_location
.realtime_set
)
901 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
903 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
905 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
908 static int next_with_matches(
911 direction_t direction
,
920 /* No matches is easy. We simple advance the file
923 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
925 /* If we have a match then we look for the next matching entry
926 * with an offset at least one step larger */
927 return next_for_match(j
, j
->level0
, f
,
928 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
929 : f
->current_offset
- 1,
930 direction
, ret
, offset
);
933 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
935 uint64_t cp
, n_entries
;
941 (void) journal_file_read_tail_timestamp(j
, f
);
943 n_entries
= le64toh(f
->header
->n_entries
);
945 /* If we hit EOF before, we don't need to look into this file again
946 * unless direction changed or new entries appeared. */
947 if (f
->last_direction
== direction
&&
948 f
->location_type
== (direction
== DIRECTION_DOWN
? LOCATION_TAIL
: LOCATION_HEAD
) &&
949 n_entries
== f
->last_n_entries
)
952 f
->last_n_entries
= n_entries
;
954 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
955 /* LOCATION_SEEK here means we did the work in a previous
956 * iteration and the current location already points to a
957 * candidate entry. */
958 if (f
->location_type
!= LOCATION_SEEK
) {
959 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
963 journal_file_save_location(f
, c
, cp
);
966 f
->last_direction
= direction
;
968 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
972 journal_file_save_location(f
, c
, cp
);
975 /* OK, we found the spot, now let's advance until an entry
976 * that is actually different from what we were previously
977 * looking at. This is necessary to handle entries which exist
978 * in two (or more) journal files, and which shall all be
979 * suppressed but one. */
984 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
987 k
= compare_with_location(j
, f
, &j
->current_location
, j
->current_file
);
989 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
996 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
1000 journal_file_save_location(f
, c
, cp
);
1004 static int compare_locations(sd_journal
*j
, JournalFile
*af
, JournalFile
*bf
) {
1012 assert(af
->location_type
== LOCATION_SEEK
);
1013 assert(bf
->location_type
== LOCATION_SEEK
);
1015 /* If contents, timestamps and seqnum match, these entries are identical. */
1016 if (sd_id128_equal(af
->current_boot_id
, bf
->current_boot_id
) &&
1017 af
->current_monotonic
== bf
->current_monotonic
&&
1018 af
->current_realtime
== bf
->current_realtime
&&
1019 af
->current_xor_hash
== bf
->current_xor_hash
&&
1020 sd_id128_equal(af
->header
->seqnum_id
, bf
->header
->seqnum_id
) &&
1021 af
->current_seqnum
== bf
->current_seqnum
)
1024 if (sd_id128_equal(af
->header
->seqnum_id
, bf
->header
->seqnum_id
)) {
1025 /* If this is from the same seqnum source, compare seqnums */
1026 r
= CMP(af
->current_seqnum
, bf
->current_seqnum
);
1030 /* Wow! This is weird, different data but the same seqnums? Something is borked, but let's
1031 * make the best of it and compare by time. */
1034 if (sd_id128_equal(af
->current_boot_id
, bf
->current_boot_id
))
1035 /* If the boot id matches, compare monotonic time */
1036 r
= CMP(af
->current_monotonic
, bf
->current_monotonic
);
1038 /* If they don't match try to compare boot IDs */
1039 r
= compare_boot_ids(j
, af
->current_boot_id
, bf
->current_boot_id
);
1043 /* Otherwise, compare UTC time */
1044 r
= CMP(af
->current_realtime
, bf
->current_realtime
);
1048 /* Finally, compare by contents */
1049 return CMP(af
->current_xor_hash
, bf
->current_xor_hash
);
1052 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
1053 JournalFile
*new_file
= NULL
;
1059 assert_return(j
, -EINVAL
);
1060 assert_return(!journal_origin_changed(j
), -ECHILD
);
1062 r
= iterated_cache_get(j
->files_cache
, NULL
, &files
, &n_files
);
1066 FOREACH_ARRAY(_f
, files
, n_files
) {
1067 JournalFile
*f
= (JournalFile
*) *_f
;
1070 r
= next_beyond_location(j
, f
, direction
);
1072 log_debug_errno(r
, "Can't iterate through %s, ignoring: %m", f
->path
);
1073 remove_file_real(j
, f
);
1075 } else if (r
== 0) {
1076 f
->location_type
= direction
== DIRECTION_DOWN
? LOCATION_TAIL
: LOCATION_HEAD
;
1085 k
= compare_locations(j
, f
, new_file
);
1087 found
= direction
== DIRECTION_DOWN
? k
< 0 : k
> 0;
1097 r
= journal_file_move_to_object(new_file
, OBJECT_ENTRY
, new_file
->current_offset
, &o
);
1101 set_location(j
, new_file
, o
);
1106 _public_
int sd_journal_next(sd_journal
*j
) {
1107 return real_journal_next(j
, DIRECTION_DOWN
);
1110 _public_
int sd_journal_previous(sd_journal
*j
) {
1111 return real_journal_next(j
, DIRECTION_UP
);
1114 _public_
int sd_journal_step_one(sd_journal
*j
, int advanced
) {
1115 assert_return(j
, -EINVAL
);
1117 if (j
->current_location
.type
== LOCATION_HEAD
)
1118 return sd_journal_next(j
);
1119 if (j
->current_location
.type
== LOCATION_TAIL
)
1120 return sd_journal_previous(j
);
1121 return real_journal_next(j
, advanced
? DIRECTION_DOWN
: DIRECTION_UP
);
1124 static int real_journal_next_skip(sd_journal
*j
, direction_t direction
, uint64_t skip
) {
1127 assert_return(j
, -EINVAL
);
1128 assert_return(!journal_origin_changed(j
), -ECHILD
);
1129 assert_return(skip
<= INT_MAX
, -ERANGE
);
1132 /* If this is not a discrete skip, then at least
1133 * resolve the current location */
1134 if (j
->current_location
.type
!= LOCATION_DISCRETE
) {
1135 r
= real_journal_next(j
, direction
);
1144 r
= real_journal_next(j
, direction
);
1158 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
1159 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
1162 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
1163 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
1166 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
1170 assert_return(j
, -EINVAL
);
1171 assert_return(!journal_origin_changed(j
), -ECHILD
);
1172 assert_return(cursor
, -EINVAL
);
1174 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1175 return -EADDRNOTAVAIL
;
1177 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1181 if (asprintf(cursor
,
1182 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
1183 SD_ID128_TO_STRING(j
->current_file
->header
->seqnum_id
), le64toh(o
->entry
.seqnum
),
1184 SD_ID128_TO_STRING(o
->entry
.boot_id
), le64toh(o
->entry
.monotonic
),
1185 le64toh(o
->entry
.realtime
),
1186 le64toh(o
->entry
.xor_hash
)) < 0)
1192 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
1193 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
1194 bool seqnum_id_set
= false,
1196 boot_id_set
= false,
1197 monotonic_set
= false,
1198 realtime_set
= false,
1199 xor_hash_set
= false;
1200 sd_id128_t seqnum_id
, boot_id
;
1203 assert_return(j
, -EINVAL
);
1204 assert_return(!journal_origin_changed(j
), -ECHILD
);
1205 assert_return(!isempty(cursor
), -EINVAL
);
1207 for (const char *p
= cursor
;;) {
1208 _cleanup_free_
char *word
= NULL
;
1210 r
= extract_first_word(&p
, &word
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1216 if (word
[0] == '\0' || word
[1] != '=')
1221 seqnum_id_set
= true;
1222 r
= sd_id128_from_string(word
+ 2, &seqnum_id
);
1229 if (sscanf(word
+ 2, "%llx", &seqnum
) != 1)
1235 r
= sd_id128_from_string(word
+ 2, &boot_id
);
1241 monotonic_set
= true;
1242 if (sscanf(word
+ 2, "%llx", &monotonic
) != 1)
1247 realtime_set
= true;
1248 if (sscanf(word
+ 2, "%llx", &realtime
) != 1)
1253 xor_hash_set
= true;
1254 if (sscanf(word
+ 2, "%llx", &xor_hash
) != 1)
1260 if ((!seqnum_set
|| !seqnum_id_set
) &&
1261 (!monotonic_set
|| !boot_id_set
) &&
1266 j
->current_location
= (Location
) {
1267 .type
= LOCATION_SEEK
,
1271 j
->current_location
.realtime
= (uint64_t) realtime
;
1272 j
->current_location
.realtime_set
= true;
1275 if (seqnum_set
&& seqnum_id_set
) {
1276 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1277 j
->current_location
.seqnum_id
= seqnum_id
;
1278 j
->current_location
.seqnum_set
= true;
1281 if (monotonic_set
&& boot_id_set
) {
1282 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1283 j
->current_location
.boot_id
= boot_id
;
1284 j
->current_location
.monotonic_set
= true;
1288 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1289 j
->current_location
.xor_hash_set
= true;
1295 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1299 assert_return(j
, -EINVAL
);
1300 assert_return(!journal_origin_changed(j
), -ECHILD
);
1301 assert_return(!isempty(cursor
), -EINVAL
);
1303 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1304 return -EADDRNOTAVAIL
;
1306 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1311 _cleanup_free_
char *item
= NULL
;
1312 unsigned long long ll
;
1316 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1323 if (strlen(item
) < 2 || item
[1] != '=')
1329 k
= sd_id128_from_string(item
+2, &id
);
1332 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1337 if (sscanf(item
+2, "%llx", &ll
) != 1)
1339 if (ll
!= le64toh(o
->entry
.seqnum
))
1344 k
= sd_id128_from_string(item
+2, &id
);
1347 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1352 if (sscanf(item
+2, "%llx", &ll
) != 1)
1354 if (ll
!= le64toh(o
->entry
.monotonic
))
1359 if (sscanf(item
+2, "%llx", &ll
) != 1)
1361 if (ll
!= le64toh(o
->entry
.realtime
))
1366 if (sscanf(item
+2, "%llx", &ll
) != 1)
1368 if (ll
!= le64toh(o
->entry
.xor_hash
))
1377 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t usec
) {
1378 assert_return(j
, -EINVAL
);
1379 assert_return(!journal_origin_changed(j
), -ECHILD
);
1383 j
->current_location
= (Location
) {
1384 .type
= LOCATION_SEEK
,
1387 .monotonic_set
= true,
1393 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1394 assert_return(j
, -EINVAL
);
1395 assert_return(!journal_origin_changed(j
), -ECHILD
);
1399 j
->current_location
= (Location
) {
1400 .type
= LOCATION_SEEK
,
1402 .realtime_set
= true,
1408 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1409 assert_return(j
, -EINVAL
);
1410 assert_return(!journal_origin_changed(j
), -ECHILD
);
1414 j
->current_location
= (Location
) {
1415 .type
= LOCATION_HEAD
,
1421 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1422 assert_return(j
, -EINVAL
);
1423 assert_return(!journal_origin_changed(j
), -ECHILD
);
1427 j
->current_location
= (Location
) {
1428 .type
= LOCATION_TAIL
,
1434 static void check_network(sd_journal
*j
, int fd
) {
1440 j
->on_network
= fd_is_network_fs(fd
);
1443 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1444 const char *full
, *tilded
, *atted
;
1446 full
= strjoina(prefix
, ".journal");
1447 tilded
= strjoina(full
, "~");
1448 atted
= strjoina(prefix
, "@");
1450 return STR_IN_SET(filename
, full
, tilded
) ||
1451 startswith(filename
, atted
);
1454 static bool file_type_wanted(int flags
, const char *filename
) {
1457 if (!ENDSWITH_SET(filename
, ".journal", ".journal~"))
1460 /* no flags set → every type is OK */
1461 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1464 if (FLAGS_SET(flags
, SD_JOURNAL_CURRENT_USER
)) {
1465 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1467 xsprintf(prefix
, "user-" UID_FMT
, getuid());
1469 if (file_has_type_prefix(prefix
, filename
))
1472 /* If SD_JOURNAL_CURRENT_USER is specified and we are invoked under a system UID, then
1473 * automatically enable SD_JOURNAL_SYSTEM too, because journald will actually put system user
1474 * data into the system journal. */
1476 if (uid_for_system_journal(getuid()))
1477 flags
|= SD_JOURNAL_SYSTEM
;
1480 if (FLAGS_SET(flags
, SD_JOURNAL_SYSTEM
) && file_has_type_prefix("system", filename
))
1486 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1491 if (j
->toplevel_fd
>= 0)
1494 return path_startswith(path
, prefix
);
1497 static void track_file_disposition(sd_journal
*j
, JournalFile
*f
) {
1501 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1502 j
->has_runtime_files
= true;
1503 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1504 j
->has_persistent_files
= true;
1507 static int add_any_file(
1512 _cleanup_close_
int our_fd
= -EBADF
;
1518 assert(fd
>= 0 || path
);
1521 assert(path
); /* For gcc. */
1522 if (j
->toplevel_fd
>= 0)
1523 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1524 * openat() ignores the first argument. */
1526 fd
= our_fd
= openat(j
->toplevel_fd
, skip_leading_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1528 fd
= our_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1530 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1534 r
= fd_nonblock(fd
, false);
1536 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1541 if (fstat(fd
, &st
) < 0) {
1542 r
= log_debug_errno(errno
, "Failed to fstat %s: %m", path
?: "fd");
1546 r
= stat_verify_regular(&st
);
1548 log_debug_errno(r
, "Refusing to open %s: %m", path
?: "fd");
1553 f
= ordered_hashmap_get(j
->files
, path
);
1555 if (stat_inode_same(&f
->last_stat
, &st
)) {
1556 /* We already track this file, under the same path and with the same
1557 * device/inode numbers, it's hence really the same. Mark this file as seen
1558 * in this generation. This is used to GC old files in process_q_overflow()
1559 * to detect journal files that are still there and discern them from those
1560 * which are gone. */
1562 f
->last_seen_generation
= j
->generation
;
1563 (void) journal_file_read_tail_timestamp(j
, f
);
1567 /* So we tracked a file under this name, but it has a different inode/device. In that
1568 * case, it got replaced (probably due to rotation?), let's drop it hence from our
1570 remove_file_real(j
, f
);
1575 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1576 r
= log_debug_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
1577 "Too many open journal files, not adding %s.", path
?: "fd");
1581 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, 0, 0, NULL
, j
->mmap
, NULL
, &f
);
1583 log_debug_errno(r
, "Failed to open journal file %s: %m", path
?: "from fd");
1587 /* journal_file_dump(f); */
1589 /* journal_file_open() generates an replacement fname if necessary, so we can use f->path. */
1590 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1592 f
->close_fd
= false; /* Make sure journal_file_close() doesn't close the caller's fd
1593 * (or our own). The caller or we will do that ourselves. */
1594 (void) journal_file_close(f
);
1598 TAKE_FD(our_fd
); /* the fd is now owned by the JournalFile object */
1600 f
->last_seen_generation
= j
->generation
;
1602 track_file_disposition(j
, f
);
1603 check_network(j
, f
->fd
);
1604 (void) journal_file_read_tail_timestamp(j
, f
);
1606 j
->current_invalidate_counter
++;
1608 log_debug("File %s added.", f
->path
);
1613 (void) journal_put_error(j
, r
, path
); /* path==NULL is OK. */
1617 int journal_get_directories(sd_journal
*j
, char ***ret
) {
1618 _cleanup_strv_free_
char **paths
= NULL
;
1621 size_t n
= SIZE_MAX
;
1627 /* This returns parent directories of opened journal files. */
1629 ORDERED_HASHMAP_FOREACH_KEY(f
, p
, j
->files
) {
1630 _cleanup_free_
char *d
= NULL
;
1632 /* Ignore paths generated from fd. */
1633 if (path_startswith(p
, "/proc/"))
1636 r
= path_extract_directory(p
, &d
);
1640 if (path_strv_contains(paths
, d
))
1643 r
= strv_extend_with_size(&paths
, &n
, d
);
1648 *ret
= TAKE_PTR(paths
);
1652 static int add_file_by_name(
1655 const char *filename
) {
1657 _cleanup_free_
char *path
= NULL
;
1663 if (j
->no_new_files
)
1666 if (!file_type_wanted(j
->flags
, filename
))
1669 path
= path_join(prefix
, filename
);
1673 return add_any_file(j
, -1, path
);
1676 static int remove_file_by_name(
1679 const char *filename
) {
1681 _cleanup_free_
char *path
= NULL
;
1688 path
= path_join(prefix
, filename
);
1692 f
= ordered_hashmap_get(j
->files
, path
);
1696 remove_file_real(j
, f
);
1700 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1704 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1706 log_debug("File %s removed.", f
->path
);
1708 if (j
->current_file
== f
) {
1709 j
->current_file
= NULL
;
1710 j
->current_field
= 0;
1713 if (j
->unique_file
== f
) {
1714 /* Jump to the next unique_file or NULL if that one was last */
1715 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1716 j
->unique_offset
= 0;
1717 if (!j
->unique_file
)
1718 j
->unique_file_lost
= true;
1721 if (j
->fields_file
== f
) {
1722 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1723 j
->fields_offset
= 0;
1724 if (!j
->fields_file
)
1725 j
->fields_file_lost
= true;
1728 journal_file_unlink_newest_by_boot_id(j
, f
);
1729 (void) journal_file_close(f
);
1731 j
->current_invalidate_counter
++;
1734 static int dirname_is_machine_id(const char *fn
) {
1735 sd_id128_t id
, machine
;
1739 /* Returns true if the specified directory name matches the local machine ID */
1741 r
= sd_id128_get_machine(&machine
);
1745 e
= strchr(fn
, '.');
1749 /* Looks like it has a namespace suffix. Verify that. */
1750 if (!log_namespace_name_valid(e
+ 1))
1753 k
= strndupa_safe(fn
, e
- fn
);
1754 r
= sd_id128_from_string(k
, &id
);
1756 r
= sd_id128_from_string(fn
, &id
);
1760 return sd_id128_equal(id
, machine
);
1763 static int dirname_has_namespace(const char *fn
, const char *namespace) {
1766 /* Returns true if the specified directory name matches the specified namespace */
1768 e
= strchr(fn
, '.');
1775 if (!streq(e
+ 1, namespace))
1778 k
= strndupa_safe(fn
, e
- fn
);
1779 return id128_is_valid(k
);
1785 return id128_is_valid(fn
);
1788 static bool dirent_is_journal_file(const struct dirent
*de
) {
1791 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1793 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1796 return endswith(de
->d_name
, ".journal") ||
1797 endswith(de
->d_name
, ".journal~");
1800 static bool dirent_is_journal_subdir(const struct dirent
*de
) {
1804 /* returns true if the specified directory entry looks like a directory that might contain journal
1805 * files we might be interested in, i.e. is either a 128-bit ID or a 128-bit ID suffixed by a
1808 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1811 e
= strchr(de
->d_name
, '.');
1813 return id128_is_valid(de
->d_name
); /* No namespace */
1815 n
= strndupa_safe(de
->d_name
, e
- de
->d_name
);
1816 if (!id128_is_valid(n
))
1819 return log_namespace_name_valid(e
+ 1);
1822 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1829 if (j
->toplevel_fd
< 0)
1832 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1833 * relative, by dropping the initial slash */
1834 d
= xopendirat(j
->toplevel_fd
, skip_leading_slash(path
), 0);
1842 static Directory
* directory_free(Directory
*d
) {
1848 hashmap_remove_value(d
->journal
->directories_by_wd
, INT_TO_PTR(d
->wd
), d
) &&
1849 d
->journal
->inotify_fd
>= 0)
1850 (void) inotify_rm_watch(d
->journal
->inotify_fd
, d
->wd
);
1853 hashmap_remove_value(d
->journal
->directories_by_path
, d
->path
, d
);
1858 log_debug("Root directory %s removed.", d
->path
);
1860 log_debug("Directory %s removed.", d
->path
);
1868 DEFINE_TRIVIAL_CLEANUP_FUNC(Directory
*, directory_free
);
1870 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
1871 directories_by_path_hash_ops
,
1878 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
1879 directories_by_wd_hash_ops
,
1882 trivial_compare_func
,
1886 static int add_directory_impl(sd_journal
*j
, const char *path
, bool is_root
, Directory
**ret
) {
1887 _cleanup_(directory_freep
) Directory
*m
= NULL
;
1888 Directory
*existing
;
1895 existing
= hashmap_get(j
->directories_by_path
, path
);
1897 if (existing
->is_root
!= is_root
) {
1898 /* Don't 'downgrade' from root directory */
1907 m
= new(Directory
, 1);
1914 .path
= strdup(path
),
1921 r
= hashmap_ensure_put(&j
->directories_by_path
, &directories_by_path_hash_ops
, m
->path
, m
);
1925 j
->current_invalidate_counter
++;
1928 log_debug("Root directory %s added.", m
->path
);
1930 log_debug("Directory %s added.", m
->path
);
1936 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1938 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1943 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1944 if (dirent_is_journal_file(de
))
1945 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1947 if (m
->is_root
&& dirent_is_journal_subdir(de
))
1948 (void) add_directory(j
, m
->path
, de
->d_name
);
1953 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1956 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1963 /* Watch this directory if that's enabled and if it not being watched yet. */
1965 if (m
->wd
> 0) /* Already have a watch? */
1967 if (j
->inotify_fd
< 0) /* Not watching at all? */
1970 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1972 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1976 r
= hashmap_ensure_put(&j
->directories_by_wd
, &directories_by_wd_hash_ops
, INT_TO_PTR(m
->wd
), m
);
1979 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1981 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1982 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1988 static int add_directory(
1991 const char *dirname
) {
1993 _cleanup_free_
char *path
= NULL
;
1994 _cleanup_closedir_
DIR *d
= NULL
;
2001 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
2002 * and reenumerates directory contents */
2004 path
= path_join(prefix
, dirname
);
2010 log_debug("Considering directory '%s'.", path
);
2012 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
2013 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
2014 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
2018 (!(FLAGS_SET(j
->flags
, SD_JOURNAL_ALL_NAMESPACES
) ||
2019 dirname_has_namespace(dirname
, j
->namespace) > 0 ||
2020 (FLAGS_SET(j
->flags
, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE
) && dirname_has_namespace(dirname
, NULL
) > 0))))
2023 r
= directory_open(j
, path
, &d
);
2025 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
2029 r
= add_directory_impl(j
, path
, /* is_root = */ false, &m
);
2035 m
->last_seen_generation
= j
->generation
;
2037 directory_watch(j
, m
, dirfd(d
),
2038 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
2039 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
2042 if (!j
->no_new_files
)
2043 directory_enumerate(j
, m
, d
);
2045 check_network(j
, dirfd(d
));
2050 k
= journal_put_error(j
, r
, path
?: prefix
);
2057 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
2059 _cleanup_closedir_
DIR *d
= NULL
;
2065 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
2066 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
2067 * populate the set, as well as to update it later. */
2070 /* If there's a path specified, use it. */
2072 log_debug("Considering root directory '%s'.", p
);
2074 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
2075 !path_has_prefix(j
, p
, "/run"))
2079 p
= strjoina(j
->prefix
, p
);
2081 r
= directory_open(j
, p
, &d
);
2082 if (r
== -ENOENT
&& missing_ok
)
2085 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
2089 _cleanup_close_
int dfd
= -EBADF
;
2091 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
2092 * opendir() will take possession of the fd, and close it, which we don't want. */
2094 p
= "."; /* store this as "." in the directories hashmap */
2096 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
2102 d
= take_fdopendir(&dfd
);
2111 r
= add_directory_impl(j
, p
, /* is_root = */ true, &m
);
2117 directory_watch(j
, m
, dirfd(d
),
2118 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
2121 if (!j
->no_new_files
)
2122 directory_enumerate(j
, m
, d
);
2124 check_network(j
, dirfd(d
));
2129 k
= journal_put_error(j
, r
, p
);
2136 static int add_search_paths(sd_journal
*j
) {
2138 static const char search_paths
[] =
2139 "/run/log/journal\0"
2140 "/var/log/journal\0";
2144 /* We ignore most errors here, since the idea is to only open
2145 * what's actually accessible, and ignore the rest. */
2147 NULSTR_FOREACH(p
, search_paths
)
2148 (void) add_root_directory(j
, p
, true);
2150 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
2151 (void) add_root_directory(j
, "/var/log/journal/remote", true);
2156 static int add_current_paths(sd_journal
*j
) {
2160 assert(j
->no_new_files
);
2162 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
2163 * treat them as fatal. */
2165 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2166 _cleanup_free_
char *dir
= NULL
;
2169 r
= path_extract_directory(f
->path
, &dir
);
2173 r
= add_directory(j
, dir
, NULL
);
2181 static int allocate_inotify(sd_journal
*j
) {
2184 if (j
->inotify_fd
< 0) {
2185 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
2186 if (j
->inotify_fd
< 0)
2193 static sd_journal
*journal_new(int flags
, const char *path
, const char *namespace) {
2194 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2196 j
= new(sd_journal
, 1);
2201 .origin_id
= origin_id_query(),
2202 .toplevel_fd
= -EBADF
,
2203 .inotify_fd
= -EBADF
,
2205 .data_threshold
= DEFAULT_DATA_THRESHOLD
,
2215 if (flags
& SD_JOURNAL_OS_ROOT
)
2222 j
->namespace = strdup(namespace);
2227 j
->files
= ordered_hashmap_new(&path_hash_ops
);
2231 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
2232 j
->mmap
= mmap_cache_new();
2233 if (!j
->files_cache
|| !j
->mmap
)
2239 #define OPEN_ALLOWED_FLAGS \
2240 (SD_JOURNAL_LOCAL_ONLY | \
2241 SD_JOURNAL_RUNTIME_ONLY | \
2242 SD_JOURNAL_SYSTEM | \
2243 SD_JOURNAL_CURRENT_USER | \
2244 SD_JOURNAL_ALL_NAMESPACES | \
2245 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE | \
2246 SD_JOURNAL_ASSUME_IMMUTABLE)
2248 _public_
int sd_journal_open_namespace(sd_journal
**ret
, const char *namespace, int flags
) {
2249 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2252 assert_return(ret
, -EINVAL
);
2253 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
2255 j
= journal_new(flags
, NULL
, namespace);
2259 r
= add_search_paths(j
);
2267 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
2268 return sd_journal_open_namespace(ret
, NULL
, flags
);
2271 #define OPEN_CONTAINER_ALLOWED_FLAGS \
2272 (SD_JOURNAL_LOCAL_ONLY | \
2273 SD_JOURNAL_SYSTEM | \
2274 SD_JOURNAL_ASSUME_IMMUTABLE)
2276 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
2277 _cleanup_free_
char *root
= NULL
, *class = NULL
;
2278 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2282 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
2283 * combination with sd_journal_open_directory_fd(). */
2285 assert_return(machine
, -EINVAL
);
2286 assert_return(ret
, -EINVAL
);
2287 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
2288 assert_return(hostname_is_valid(machine
, 0), -EINVAL
);
2290 p
= strjoina("/run/systemd/machines/", machine
);
2291 r
= parse_env_file(NULL
, p
,
2301 if (!streq_ptr(class, "container"))
2304 j
= journal_new(flags
, root
, NULL
);
2308 r
= add_search_paths(j
);
2316 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
2317 (SD_JOURNAL_OS_ROOT | \
2318 SD_JOURNAL_SYSTEM | \
2319 SD_JOURNAL_CURRENT_USER | \
2320 SD_JOURNAL_ASSUME_IMMUTABLE)
2322 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
2323 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2326 assert_return(ret
, -EINVAL
);
2327 assert_return(path
, -EINVAL
);
2328 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
2330 j
= journal_new(flags
, path
, NULL
);
2334 if (flags
& SD_JOURNAL_OS_ROOT
)
2335 r
= add_search_paths(j
);
2337 r
= add_root_directory(j
, path
, false);
2345 #define OPEN_FILES_ALLOWED_FLAGS \
2346 (SD_JOURNAL_ASSUME_IMMUTABLE)
2348 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
2349 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2352 assert_return(ret
, -EINVAL
);
2353 assert_return((flags
& ~OPEN_FILES_ALLOWED_FLAGS
) == 0, -EINVAL
);
2355 j
= journal_new(flags
, NULL
, NULL
);
2359 STRV_FOREACH(path
, paths
) {
2360 r
= add_any_file(j
, -1, *path
);
2365 j
->no_new_files
= true;
2371 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
2372 (SD_JOURNAL_OS_ROOT | \
2373 SD_JOURNAL_SYSTEM | \
2374 SD_JOURNAL_CURRENT_USER | \
2375 SD_JOURNAL_TAKE_DIRECTORY_FD | \
2376 SD_JOURNAL_ASSUME_IMMUTABLE)
2378 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
2379 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2384 assert_return(ret
, -EINVAL
);
2385 assert_return(fd
>= 0, -EBADF
);
2386 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2388 if (fstat(fd
, &st
) < 0)
2391 if (!S_ISDIR(st
.st_mode
))
2394 take_fd
= FLAGS_SET(flags
, SD_JOURNAL_TAKE_DIRECTORY_FD
);
2395 j
= journal_new(flags
& ~SD_JOURNAL_TAKE_DIRECTORY_FD
, NULL
, NULL
);
2399 j
->toplevel_fd
= fd
;
2401 if (flags
& SD_JOURNAL_OS_ROOT
)
2402 r
= add_search_paths(j
);
2404 r
= add_root_directory(j
, NULL
, false);
2408 SET_FLAG(j
->flags
, SD_JOURNAL_TAKE_DIRECTORY_FD
, take_fd
);
2414 #define OPEN_FILES_FD_ALLOWED_FLAGS \
2415 (SD_JOURNAL_ASSUME_IMMUTABLE)
2417 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2419 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2422 assert_return(ret
, -EINVAL
);
2423 assert_return(n_fds
> 0, -EBADF
);
2424 assert_return((flags
& ~OPEN_FILES_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2426 j
= journal_new(flags
, NULL
, NULL
);
2430 for (unsigned i
= 0; i
< n_fds
; i
++) {
2438 if (fstat(fds
[i
], &st
) < 0) {
2443 r
= stat_verify_regular(&st
);
2447 r
= add_any_file(j
, fds
[i
], NULL
);
2452 j
->no_new_files
= true;
2453 j
->no_inotify
= true;
2459 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2461 ORDERED_HASHMAP_FOREACH(f
, j
->files
)
2462 f
->close_fd
= false;
2467 _public_
void sd_journal_close(sd_journal
*j
) {
2468 if (!j
|| journal_origin_changed(j
))
2471 journal_clear_newest_by_boot_id(j
);
2473 sd_journal_flush_matches(j
);
2475 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2476 iterated_cache_free(j
->files_cache
);
2478 hashmap_free(j
->directories_by_path
);
2479 hashmap_free(j
->directories_by_wd
);
2481 if (FLAGS_SET(j
->flags
, SD_JOURNAL_TAKE_DIRECTORY_FD
))
2482 safe_close(j
->toplevel_fd
);
2484 safe_close(j
->inotify_fd
);
2487 mmap_cache_stats_log_debug(j
->mmap
);
2488 mmap_cache_unref(j
->mmap
);
2491 hashmap_free_free(j
->errors
);
2493 set_free(j
->exclude_syslog_identifiers
);
2498 free(j
->unique_field
);
2499 free(j
->fields_buffer
);
2503 static int journal_file_read_tail_timestamp(sd_journal
*j
, JournalFile
*f
) {
2504 uint64_t offset
, mo
, rt
;
2514 /* Tries to read the timestamp of the most recently written entry. */
2516 if (FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
) && f
->newest_entry_offset
!= 0)
2517 return 0; /* We have already read the file, and we assume that the file is immutable. */
2519 if (f
->header
->state
== f
->newest_state
&&
2520 f
->header
->state
== STATE_ARCHIVED
&&
2521 f
->newest_entry_offset
!= 0)
2522 return 0; /* We have already read archived file. */
2524 if (JOURNAL_HEADER_CONTAINS(f
->header
, tail_entry_offset
)) {
2525 offset
= le64toh(READ_NOW(f
->header
->tail_entry_offset
));
2526 type
= OBJECT_ENTRY
;
2528 offset
= le64toh(READ_NOW(f
->header
->tail_object_offset
));
2529 type
= OBJECT_UNUSED
;
2532 return -ENODATA
; /* not a single object/entry, hence no tail timestamp */
2533 if (offset
== f
->newest_entry_offset
)
2534 return 0; /* No new entry is added after we read last time. */
2536 /* Move to the last object in the journal file, in the hope it is an entry (which it usually will
2537 * be). If we lack the "tail_entry_offset" field in the header, we specify the type as OBJECT_UNUSED
2538 * here, since we cannot be sure what the last object will be, and want no noisy logging if it isn't
2539 * an entry. We instead check after figuring out the pointer. */
2540 r
= journal_file_move_to_object(f
, type
, offset
, &o
);
2542 log_debug_errno(r
, "Failed to move to last object in journal file, ignoring: %m");
2546 if (o
&& o
->object
.type
== OBJECT_ENTRY
) {
2547 /* Yay, last object is an entry, let's use the data. */
2548 id
= o
->entry
.boot_id
;
2549 mo
= le64toh(o
->entry
.monotonic
);
2550 rt
= le64toh(o
->entry
.realtime
);
2552 /* So the object is not an entry or we couldn't access it? In that case, let's read the most
2553 * recent entry timestamps from the header. It's equally good. Unfortunately though, in old
2554 * versions of the journal the boot ID in the header doesn't have to match the monotonic
2555 * timestamp of the header. Let's check the header flag that indicates whether this strictly
2556 * matches first hence, before using the data. */
2558 if (JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f
->header
) && f
->header
->state
== STATE_ARCHIVED
) {
2559 mo
= le64toh(f
->header
->tail_entry_monotonic
);
2560 rt
= le64toh(f
->header
->tail_entry_realtime
);
2561 id
= f
->header
->tail_entry_boot_id
;
2562 offset
= UINT64_MAX
;
2564 /* Otherwise let's find the last entry manually (this possibly means traversing the
2565 * chain of entry arrays, till the end */
2566 r
= journal_file_next_entry(f
, 0, DIRECTION_UP
, &o
, offset
== 0 ? &offset
: NULL
);
2572 id
= o
->entry
.boot_id
;
2573 mo
= le64toh(o
->entry
.monotonic
);
2574 rt
= le64toh(o
->entry
.realtime
);
2578 if (mo
> rt
) /* monotonic clock is further ahead than realtime? that's weird, refuse to use the data */
2581 if (offset
== f
->newest_entry_offset
) {
2582 /* Cached data and the current one should be equivalent. */
2583 if (!sd_id128_equal(f
->newest_machine_id
, f
->header
->machine_id
) ||
2584 !sd_id128_equal(f
->newest_boot_id
, id
) ||
2585 f
->newest_monotonic_usec
!= mo
||
2586 f
->newest_realtime_usec
!= rt
)
2589 return 0; /* No new entry is added after we read last time. */
2592 if (!sd_id128_equal(f
->newest_boot_id
, id
))
2593 journal_file_unlink_newest_by_boot_id(j
, f
);
2595 f
->newest_boot_id
= id
;
2596 f
->newest_monotonic_usec
= mo
;
2597 f
->newest_realtime_usec
= rt
;
2598 f
->newest_machine_id
= f
->header
->machine_id
;
2599 f
->newest_entry_offset
= offset
;
2600 f
->newest_state
= f
->header
->state
;
2602 r
= journal_file_reshuffle_newest_by_boot_id(j
, f
);
2606 return 1; /* Updated. */
2609 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2614 assert_return(j
, -EINVAL
);
2615 assert_return(!journal_origin_changed(j
), -ECHILD
);
2617 f
= j
->current_file
;
2619 return -EADDRNOTAVAIL
;
2620 if (f
->current_offset
<= 0)
2621 return -EADDRNOTAVAIL
;
2623 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2627 uint64_t t
= le64toh(o
->entry
.realtime
);
2628 if (!VALID_REALTIME(t
))
2637 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2642 assert_return(j
, -EINVAL
);
2643 assert_return(!journal_origin_changed(j
), -ECHILD
);
2645 f
= j
->current_file
;
2647 return -EADDRNOTAVAIL
;
2648 if (f
->current_offset
<= 0)
2649 return -EADDRNOTAVAIL
;
2651 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2656 *ret_boot_id
= o
->entry
.boot_id
;
2660 r
= sd_id128_get_boot(&id
);
2664 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2668 uint64_t t
= le64toh(o
->entry
.monotonic
);
2669 if (!VALID_MONOTONIC(t
))
2678 _public_
int sd_journal_get_seqnum(
2680 uint64_t *ret_seqnum
,
2681 sd_id128_t
*ret_seqnum_id
) {
2687 assert_return(j
, -EINVAL
);
2688 assert_return(!journal_origin_changed(j
), -ECHILD
);
2690 f
= j
->current_file
;
2692 return -EADDRNOTAVAIL
;
2694 if (f
->current_offset
<= 0)
2695 return -EADDRNOTAVAIL
;
2697 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2702 *ret_seqnum_id
= f
->header
->seqnum_id
;
2704 *ret_seqnum
= le64toh(o
->entry
.seqnum
);
2709 static bool field_is_valid(const char *field
) {
2715 if (startswith(field
, "__"))
2718 for (const char *p
= field
; *p
; p
++) {
2723 if (*p
>= 'A' && *p
<= 'Z')
2726 if (ascii_isdigit(*p
))
2735 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2737 size_t field_length
;
2741 assert_return(j
, -EINVAL
);
2742 assert_return(!journal_origin_changed(j
), -ECHILD
);
2743 assert_return(field
, -EINVAL
);
2744 assert_return(data
, -EINVAL
);
2745 assert_return(size
, -EINVAL
);
2746 assert_return(field_is_valid(field
), -EINVAL
);
2748 f
= j
->current_file
;
2750 return -EADDRNOTAVAIL
;
2752 if (f
->current_offset
<= 0)
2753 return -EADDRNOTAVAIL
;
2755 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2759 field_length
= strlen(field
);
2761 uint64_t n
= journal_file_entry_n_items(f
, o
);
2762 for (uint64_t i
= 0; i
< n
; i
++) {
2767 p
= journal_file_entry_item_object_offset(f
, o
, i
);
2768 r
= journal_file_data_payload(f
, NULL
, p
, field
, field_length
, j
->data_threshold
, &d
, &l
);
2771 if (IN_SET(r
, -EADDRNOTAVAIL
, -EBADMSG
)) {
2772 log_debug_errno(r
, "Entry item %"PRIu64
" data object is bad, skipping over it: %m", i
);
2787 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2792 assert_return(j
, -EINVAL
);
2793 assert_return(!journal_origin_changed(j
), -ECHILD
);
2794 assert_return(data
, -EINVAL
);
2795 assert_return(size
, -EINVAL
);
2797 f
= j
->current_file
;
2799 return -EADDRNOTAVAIL
;
2801 if (f
->current_offset
<= 0)
2802 return -EADDRNOTAVAIL
;
2804 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2808 for (uint64_t n
= journal_file_entry_n_items(f
, o
); j
->current_field
< n
; j
->current_field
++) {
2813 p
= journal_file_entry_item_object_offset(f
, o
, j
->current_field
);
2814 r
= journal_file_data_payload(f
, NULL
, p
, NULL
, 0, j
->data_threshold
, &d
, &l
);
2815 if (IN_SET(r
, -EADDRNOTAVAIL
, -EBADMSG
)) {
2816 log_debug_errno(r
, "Entry item %"PRIu64
" data object is bad, skipping over it: %m", j
->current_field
);
2834 _public_
int sd_journal_enumerate_available_data(sd_journal
*j
, const void **data
, size_t *size
) {
2838 r
= sd_journal_enumerate_data(j
, data
, size
);
2841 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r
))
2843 j
->current_field
++; /* Try with the next field */
2847 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2848 if (!j
|| journal_origin_changed(j
))
2851 j
->current_field
= 0;
2854 static int reiterate_all_paths(sd_journal
*j
) {
2857 if (j
->no_new_files
)
2858 return add_current_paths(j
);
2860 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2861 return add_search_paths(j
);
2863 if (j
->toplevel_fd
>= 0)
2864 return add_root_directory(j
, NULL
, false);
2867 return add_root_directory(j
, j
->path
, true);
2869 return add_search_paths(j
);
2872 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2875 assert_return(j
, -EINVAL
);
2876 assert_return(!journal_origin_changed(j
), -ECHILD
);
2877 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
2880 return -EMEDIUMTYPE
;
2882 if (j
->inotify_fd
>= 0)
2883 return j
->inotify_fd
;
2885 r
= allocate_inotify(j
);
2889 log_debug("Reiterating files to get inotify watches established.");
2891 /* Iterate through all dirs again, to add them to the inotify */
2892 r
= reiterate_all_paths(j
);
2896 return j
->inotify_fd
;
2899 _public_
int sd_journal_get_events(sd_journal
*j
) {
2902 assert_return(j
, -EINVAL
);
2903 assert_return(!journal_origin_changed(j
), -ECHILD
);
2904 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
2906 fd
= sd_journal_get_fd(j
);
2913 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2916 assert_return(j
, -EINVAL
);
2917 assert_return(!journal_origin_changed(j
), -ECHILD
);
2918 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
2919 assert_return(timeout_usec
, -EINVAL
);
2921 fd
= sd_journal_get_fd(j
);
2925 if (!j
->on_network
) {
2926 *timeout_usec
= UINT64_MAX
;
2930 /* If we are on the network we need to regularly check for
2931 * changes manually */
2933 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2937 static void process_q_overflow(sd_journal
*j
) {
2943 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2944 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2945 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2946 * are subject for unloading. */
2948 log_debug("Inotify queue overrun, reiterating everything.");
2951 (void) reiterate_all_paths(j
);
2953 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2955 if (f
->last_seen_generation
== j
->generation
)
2958 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2959 remove_file_real(j
, f
);
2962 HASHMAP_FOREACH(m
, j
->directories_by_path
) {
2964 if (m
->last_seen_generation
== j
->generation
)
2967 if (m
->is_root
) /* Never GC root directories */
2970 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2974 log_debug("Reiteration complete.");
2977 static void process_inotify_event(sd_journal
*j
, const struct inotify_event
*e
) {
2983 if (e
->mask
& IN_Q_OVERFLOW
) {
2984 process_q_overflow(j
);
2988 /* Is this a subdirectory we watch? */
2989 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2991 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2992 (endswith(e
->name
, ".journal") ||
2993 endswith(e
->name
, ".journal~"))) {
2995 /* Event for a journal file */
2997 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2998 (void) add_file_by_name(j
, d
->path
, e
->name
);
2999 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
3000 (void) remove_file_by_name(j
, d
->path
, e
->name
);
3002 } else if (!d
->is_root
&& e
->len
== 0) {
3004 /* Event for a subdirectory */
3006 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
3009 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
3011 /* Event for root directory */
3013 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
3014 (void) add_directory(j
, d
->path
, e
->name
);
3020 if (e
->mask
& IN_IGNORED
)
3023 log_debug("Unexpected inotify event.");
3026 static int determine_change(sd_journal
*j
) {
3031 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
3032 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
3034 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
3037 _public_
int sd_journal_process(sd_journal
*j
) {
3038 bool got_something
= false;
3040 assert_return(j
, -EINVAL
);
3041 assert_return(!journal_origin_changed(j
), -ECHILD
);
3043 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
3046 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
3048 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
3049 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
3052 union inotify_event_buffer buffer
;
3055 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
3057 if (ERRNO_IS_TRANSIENT(errno
))
3058 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
3063 got_something
= true;
3065 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
3066 process_inotify_event(j
, e
);
3070 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
3074 assert_return(j
, -EINVAL
);
3075 assert_return(!journal_origin_changed(j
), -ECHILD
);
3076 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
3078 if (j
->inotify_fd
< 0) {
3081 /* This is the first invocation, hence create the inotify watch */
3082 r
= sd_journal_get_fd(j
);
3086 /* Server might have done some vacuuming while we weren't watching. Get rid of the deleted
3087 * files now so they don't stay around indefinitely. */
3088 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3089 r
= journal_file_fstat(f
);
3091 remove_file_real(j
, f
);
3093 log_debug_errno(r
, "Failed to fstat() journal file '%s', ignoring: %m", f
->path
);
3096 /* The journal might have changed since the context object was created and we weren't
3097 * watching before, hence don't wait for anything, and return immediately. */
3098 return determine_change(j
);
3101 r
= sd_journal_get_timeout(j
, &t
);
3105 if (t
!= UINT64_MAX
) {
3106 t
= usec_sub_unsigned(t
, now(CLOCK_MONOTONIC
));
3108 if (timeout_usec
== UINT64_MAX
|| timeout_usec
> t
)
3113 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
3114 } while (r
== -EINTR
);
3119 return sd_journal_process(j
);
3122 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
3125 uint64_t fmin
= 0, tmax
= 0;
3128 assert_return(j
, -EINVAL
);
3129 assert_return(!journal_origin_changed(j
), -ECHILD
);
3130 assert_return(from
|| to
, -EINVAL
);
3131 assert_return(from
!= to
, -EINVAL
);
3133 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3136 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
3149 fmin
= MIN(fr
, fmin
);
3150 tmax
= MAX(t
, tmax
);
3159 return first
? 0 : 1;
3162 _public_
int sd_journal_get_cutoff_monotonic_usec(
3168 uint64_t from
= UINT64_MAX
, to
= UINT64_MAX
;
3173 assert_return(j
, -EINVAL
);
3174 assert_return(!journal_origin_changed(j
), -ECHILD
);
3175 assert_return(ret_from
!= ret_to
, -EINVAL
);
3177 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3180 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &ff
, &tt
);
3189 from
= MIN(ff
, from
);
3206 void journal_print_header(sd_journal
*j
) {
3208 bool newline
= false;
3212 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3218 journal_file_print_header(f
);
3222 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *ret
) {
3226 assert_return(j
, -EINVAL
);
3227 assert_return(!journal_origin_changed(j
), -ECHILD
);
3228 assert_return(ret
, -EINVAL
);
3230 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3234 if (fstat(f
->fd
, &st
) < 0)
3237 b
= (uint64_t) st
.st_blocks
;
3238 if (b
> UINT64_MAX
/ 512)
3242 if (sum
> UINT64_MAX
- b
)
3251 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
3254 assert_return(j
, -EINVAL
);
3255 assert_return(!journal_origin_changed(j
), -ECHILD
);
3257 if (!field_is_valid(field
))
3260 r
= free_and_strdup(&j
->unique_field
, field
);
3264 j
->unique_file
= NULL
;
3265 j
->unique_offset
= 0;
3266 j
->unique_file_lost
= false;
3271 _public_
int sd_journal_enumerate_unique(
3273 const void **ret_data
,
3278 assert_return(j
, -EINVAL
);
3279 assert_return(!journal_origin_changed(j
), -ECHILD
);
3280 assert_return(j
->unique_field
, -EINVAL
);
3282 k
= strlen(j
->unique_field
);
3284 if (!j
->unique_file
) {
3285 if (j
->unique_file_lost
)
3288 j
->unique_file
= ordered_hashmap_first(j
->files
);
3289 if (!j
->unique_file
)
3292 j
->unique_offset
= 0;
3303 /* Proceed to next data object in the field's linked list */
3304 if (j
->unique_offset
== 0) {
3305 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
3309 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
3311 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
3315 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
3318 /* We reached the end of the list? Then start again, with the next file */
3319 if (j
->unique_offset
== 0) {
3320 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
3321 if (!j
->unique_file
)
3327 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
3331 /* Let's pin the data object, so we can look at it at the same time as one on another file. */
3332 r
= journal_file_pin_object(j
->unique_file
, o
);
3336 r
= journal_file_data_payload(j
->unique_file
, o
, j
->unique_offset
, NULL
, 0,
3337 j
->data_threshold
, &odata
, &ol
);
3341 /* Check if we have at least the field name and "=". */
3343 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3344 "%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
3345 j
->unique_file
->path
,
3346 j
->unique_offset
, ol
, k
+ 1);
3348 if (memcmp(odata
, j
->unique_field
, k
) != 0 || ((const char*) odata
)[k
] != '=')
3349 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3350 "%s:offset " OFSfmt
": object does not start with \"%s=\"",
3351 j
->unique_file
->path
,
3355 /* OK, now let's see if we already returned this data object by checking if it exists in the
3356 * earlier traversed files. */
3358 ORDERED_HASHMAP_FOREACH(of
, j
->files
) {
3359 if (of
== j
->unique_file
)
3362 /* Skip this file it didn't have any fields indexed */
3363 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3366 /* We can reuse the hash from our current file only on old-style journal files
3367 * without keyed hashes. On new-style files we have to calculate the hash anew, to
3368 * take the per-file hash seed into consideration. */
3369 if (!JOURNAL_HEADER_KEYED_HASH(j
->unique_file
->header
) && !JOURNAL_HEADER_KEYED_HASH(of
->header
))
3370 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
3372 r
= journal_file_find_data_object(of
, odata
, ol
, NULL
, NULL
);
3391 _public_
int sd_journal_enumerate_available_unique(sd_journal
*j
, const void **data
, size_t *size
) {
3395 r
= sd_journal_enumerate_unique(j
, data
, size
);
3398 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r
))
3400 /* Try with the next field. sd_journal_enumerate_unique() modifies state, so on the next try
3401 * we will access the next field. */
3405 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
3406 if (!j
|| journal_origin_changed(j
))
3409 j
->unique_file
= NULL
;
3410 j
->unique_offset
= 0;
3411 j
->unique_file_lost
= false;
3414 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
3417 assert_return(j
, -EINVAL
);
3418 assert_return(!journal_origin_changed(j
), -ECHILD
);
3419 assert_return(field
, -EINVAL
);
3421 if (!j
->fields_file
) {
3422 if (j
->fields_file_lost
)
3425 j
->fields_file
= ordered_hashmap_first(j
->files
);
3426 if (!j
->fields_file
)
3429 j
->fields_hash_table_index
= 0;
3430 j
->fields_offset
= 0;
3434 JournalFile
*f
, *of
;
3442 if (j
->fields_offset
== 0) {
3445 /* We are not yet positioned at any field. Let's pick the first one */
3446 r
= journal_file_map_field_hash_table(f
);
3450 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
3452 if (j
->fields_hash_table_index
>= m
) {
3453 /* Reached the end of the hash table, go to the next file. */
3458 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
3460 if (j
->fields_offset
!= 0)
3463 /* Empty hash table bucket, go to next one */
3464 j
->fields_hash_table_index
++;
3468 /* Proceed with next file */
3469 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
3470 if (!j
->fields_file
) {
3475 j
->fields_offset
= 0;
3476 j
->fields_hash_table_index
= 0;
3481 /* We are already positioned at a field. If so, let's figure out the next field from it */
3483 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
3487 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
3488 if (j
->fields_offset
== 0) {
3489 /* Reached the end of the hash table chain */
3490 j
->fields_hash_table_index
++;
3495 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
3496 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
3500 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
3501 if (o
->object
.type
!= OBJECT_FIELD
)
3502 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3503 "%s:offset " OFSfmt
": object has type %i, expected %i",
3504 f
->path
, j
->fields_offset
,
3505 o
->object
.type
, OBJECT_FIELD
);
3507 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
3509 /* Let's see if we already returned this field name before. */
3511 ORDERED_HASHMAP_FOREACH(of
, j
->files
) {
3515 /* Skip this file it didn't have any fields indexed */
3516 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3519 if (!JOURNAL_HEADER_KEYED_HASH(f
->header
) && !JOURNAL_HEADER_KEYED_HASH(of
->header
))
3520 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
,
3521 le64toh(o
->field
.hash
), NULL
, NULL
);
3523 r
= journal_file_find_field_object(of
, o
->field
.payload
, sz
, NULL
, NULL
);
3535 /* Check if this is really a valid string containing no NUL byte */
3536 if (memchr(o
->field
.payload
, 0, sz
))
3539 if (j
->data_threshold
> 0 && sz
> j
->data_threshold
)
3540 sz
= j
->data_threshold
;
3542 if (!GREEDY_REALLOC(j
->fields_buffer
, sz
+ 1))
3545 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3546 j
->fields_buffer
[sz
] = 0;
3548 if (!field_is_valid(j
->fields_buffer
))
3551 *field
= j
->fields_buffer
;
3556 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3557 if (!j
|| journal_origin_changed(j
))
3560 j
->fields_file
= NULL
;
3561 j
->fields_hash_table_index
= 0;
3562 j
->fields_offset
= 0;
3563 j
->fields_file_lost
= false;
3566 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3567 assert_return(j
, -EINVAL
);
3568 assert_return(!journal_origin_changed(j
), -ECHILD
);
3570 return !j
->on_network
;
3573 static char *lookup_field(const char *field
, void *userdata
) {
3574 sd_journal
*j
= ASSERT_PTR(userdata
);
3581 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3583 size
> REPLACE_VAR_MAX
)
3584 return strdup(field
);
3586 d
= strlen(field
) + 1;
3588 return strndup((const char*) data
+ d
, size
- d
);
3591 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3595 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3599 assert_return(j
, -EINVAL
);
3600 assert_return(!journal_origin_changed(j
), -ECHILD
);
3601 assert_return(ret
, -EINVAL
);
3603 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3607 cid
= strndup((const char*) data
+ 11, size
- 11);
3611 r
= sd_id128_from_string(cid
, &id
);
3615 r
= catalog_get(secure_getenv("SYSTEMD_CATALOG") ?: CATALOG_DATABASE
, id
, &text
);
3619 t
= replace_var(text
, lookup_field
, j
);
3627 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3628 assert_return(ret
, -EINVAL
);
3630 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3633 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3634 assert_return(j
, -EINVAL
);
3635 assert_return(!journal_origin_changed(j
), -ECHILD
);
3637 j
->data_threshold
= sz
;
3641 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3642 assert_return(j
, -EINVAL
);
3643 assert_return(!journal_origin_changed(j
), -ECHILD
);
3644 assert_return(sz
, -EINVAL
);
3646 *sz
= j
->data_threshold
;
3650 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3651 assert_return(j
, -EINVAL
);
3653 return j
->has_runtime_files
;
3656 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3657 assert_return(j
, -EINVAL
);
3659 return j
->has_persistent_files
;