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 _public_
int sd_journal_add_conjunction(sd_journal
*j
) {
329 assert_return(j
, -EINVAL
);
330 assert_return(!journal_origin_changed(j
), -ECHILD
);
338 if (!j
->level1
->matches
)
347 _public_
int sd_journal_add_disjunction(sd_journal
*j
) {
348 assert_return(j
, -EINVAL
);
349 assert_return(!journal_origin_changed(j
), -ECHILD
);
360 if (!j
->level2
->matches
)
367 static char *match_make_string(Match
*m
) {
368 _cleanup_free_
char *p
= NULL
;
369 bool enclose
= false;
372 return strdup("none");
374 if (m
->type
== MATCH_DISCRETE
)
375 return cescape_length(m
->data
, m
->size
);
377 LIST_FOREACH(matches
, i
, m
->matches
) {
378 _cleanup_free_
char *t
= NULL
;
380 t
= match_make_string(i
);
385 if (!strextend(&p
, m
->type
== MATCH_OR_TERM
? " OR " : " AND ", t
))
394 return strjoin("(", p
, ")");
399 char *journal_make_match_string(sd_journal
*j
) {
402 return match_make_string(j
->level0
);
405 _public_
void sd_journal_flush_matches(sd_journal
*j
) {
406 if (!j
|| journal_origin_changed(j
))
410 match_free(j
->level0
);
412 j
->level0
= j
->level1
= j
->level2
= NULL
;
417 static int newest_by_boot_id_compare(const NewestByBootId
*a
, const NewestByBootId
*b
) {
418 return id128_compare_func(&a
->boot_id
, &b
->boot_id
);
421 static void journal_file_unlink_newest_by_boot_id(sd_journal
*j
, JournalFile
*f
) {
422 NewestByBootId
*found
;
427 if (f
->newest_boot_id_prioq_idx
== PRIOQ_IDX_NULL
) /* not linked currently, hence this is a NOP */
430 found
= typesafe_bsearch(&(NewestByBootId
) { .boot_id
= f
->newest_boot_id
},
431 j
->newest_by_boot_id
, j
->n_newest_by_boot_id
, newest_by_boot_id_compare
);
434 assert_se(prioq_remove(found
->prioq
, f
, &f
->newest_boot_id_prioq_idx
) > 0);
435 f
->newest_boot_id_prioq_idx
= PRIOQ_IDX_NULL
;
437 /* The prioq may be empty, but that should not cause any issue. Let's keep it. */
440 static void journal_clear_newest_by_boot_id(sd_journal
*j
) {
441 FOREACH_ARRAY(i
, j
->newest_by_boot_id
, j
->n_newest_by_boot_id
) {
444 while ((f
= prioq_peek(i
->prioq
)))
445 journal_file_unlink_newest_by_boot_id(j
, f
);
447 prioq_free(i
->prioq
);
450 j
->newest_by_boot_id
= mfree(j
->newest_by_boot_id
);
451 j
->n_newest_by_boot_id
= 0;
454 static int journal_file_newest_monotonic_compare(const void *a
, const void *b
) {
455 const JournalFile
*x
= a
, *y
= b
;
457 return -CMP(x
->newest_monotonic_usec
, y
->newest_monotonic_usec
); /* Invert order, we want newest first! */
460 static int journal_file_reshuffle_newest_by_boot_id(sd_journal
*j
, JournalFile
*f
) {
461 NewestByBootId
*found
;
467 found
= typesafe_bsearch(&(NewestByBootId
) { .boot_id
= f
->newest_boot_id
},
468 j
->newest_by_boot_id
, j
->n_newest_by_boot_id
, newest_by_boot_id_compare
);
470 /* There's already a priority queue for this boot ID */
472 if (f
->newest_boot_id_prioq_idx
== PRIOQ_IDX_NULL
) {
473 r
= prioq_put(found
->prioq
, f
, &f
->newest_boot_id_prioq_idx
); /* Insert if we aren't in there yet */
477 prioq_reshuffle(found
->prioq
, f
, &f
->newest_boot_id_prioq_idx
); /* Reshuffle otherwise */
480 _cleanup_(prioq_freep
) Prioq
*q
= NULL
;
482 /* No priority queue yet, then allocate one */
484 assert(f
->newest_boot_id_prioq_idx
== PRIOQ_IDX_NULL
); /* we can't be a member either */
486 q
= prioq_new(journal_file_newest_monotonic_compare
);
490 r
= prioq_put(q
, f
, &f
->newest_boot_id_prioq_idx
);
494 if (!GREEDY_REALLOC(j
->newest_by_boot_id
, j
->n_newest_by_boot_id
+ 1)) {
495 f
->newest_boot_id_prioq_idx
= PRIOQ_IDX_NULL
;
499 j
->newest_by_boot_id
[j
->n_newest_by_boot_id
++] = (NewestByBootId
) {
500 .boot_id
= f
->newest_boot_id
,
501 .prioq
= TAKE_PTR(q
),
504 typesafe_qsort(j
->newest_by_boot_id
, j
->n_newest_by_boot_id
, newest_by_boot_id_compare
);
510 static int journal_file_find_newest_for_boot_id(
515 JournalFile
*prev
= NULL
;
521 /* Before we use it, let's refresh the timestamp from the header, and reshuffle our prioq
522 * accordingly. We do this only a bunch of times, to not be caught in some update loop. */
523 for (unsigned n_tries
= 0;; n_tries
++) {
524 NewestByBootId
*found
;
527 found
= typesafe_bsearch(&(NewestByBootId
) { .boot_id
= id
},
528 j
->newest_by_boot_id
, j
->n_newest_by_boot_id
, newest_by_boot_id_compare
);
530 f
= found
? prioq_peek(found
->prioq
) : NULL
;
532 return log_debug_errno(SYNTHETIC_ERRNO(ENODATA
),
533 "Requested delta for boot ID %s, but we have no information about that boot ID.", SD_ID128_TO_STRING(id
));
535 if (f
== prev
|| n_tries
>= 5) {
536 /* This was already the best answer in the previous run, or we tried too often, use it */
543 /* Let's read the journal file's current timestamp once, before we return it, maybe it has changed. */
544 r
= journal_file_read_tail_timestamp(j
, f
);
546 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
));
548 /* No new entry found. */
553 /* Refreshing the timestamp we read might have reshuffled the prioq, hence let's check the
554 * prioq again and only use the information once we reached an equilibrium or hit a limit */
558 static int compare_boot_ids(sd_journal
*j
, sd_id128_t a
, sd_id128_t b
) {
563 /* Try to find the newest open journal file for the two boot ids */
564 if (journal_file_find_newest_for_boot_id(j
, a
, &x
) < 0 ||
565 journal_file_find_newest_for_boot_id(j
, b
, &y
) < 0)
568 /* Only compare the boot id timestamps if they originate from the same machine. If they are from
569 * different machines, then we timestamps of the boot ids might be as off as the timestamps on the
570 * entries and hence not useful for comparing. */
571 if (!sd_id128_equal(x
->newest_machine_id
, y
->newest_machine_id
))
574 return CMP(x
->newest_realtime_usec
, y
->newest_realtime_usec
);
577 static int compare_with_location(
579 const JournalFile
*f
,
581 const JournalFile
*current_file
) {
587 assert(f
->location_type
== LOCATION_SEEK
);
588 assert(IN_SET(l
->type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
590 if (l
->monotonic_set
&&
591 sd_id128_equal(f
->current_boot_id
, l
->boot_id
) &&
593 f
->current_realtime
== l
->realtime
&&
595 f
->current_xor_hash
== l
->xor_hash
&&
597 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
) &&
598 f
->current_seqnum
== l
->seqnum
&&
603 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
)) {
604 r
= CMP(f
->current_seqnum
, l
->seqnum
);
609 if (l
->monotonic_set
) {
610 /* If both arguments have the same boot ID, then we can compare the monotonic timestamps. If
611 * they are distinct, then we might able to lookup the timestamps of those boot IDs (if they
612 * are from the same machine) and order by that. */
613 if (sd_id128_equal(f
->current_boot_id
, l
->boot_id
))
614 r
= CMP(f
->current_monotonic
, l
->monotonic
);
616 r
= compare_boot_ids(j
, f
->current_boot_id
, l
->boot_id
);
621 if (l
->realtime_set
) {
622 r
= CMP(f
->current_realtime
, l
->realtime
);
627 if (l
->xor_hash_set
) {
628 r
= CMP(f
->current_xor_hash
, l
->xor_hash
);
636 static int next_for_match(
640 uint64_t after_offset
,
641 direction_t direction
,
652 if (m
->type
== MATCH_DISCRETE
) {
656 /* If the keyed hash logic is used, we need to calculate the hash fresh per file. Otherwise
657 * we can use what we pre-calculated. */
658 if (JOURNAL_HEADER_KEYED_HASH(f
->header
))
659 hash
= journal_file_hash_data(f
, m
->data
, m
->size
);
663 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, hash
, &d
, NULL
);
667 return journal_file_move_to_entry_by_offset_for_data(f
, d
, after_offset
, direction
, ret
, offset
);
669 } else if (m
->type
== MATCH_OR_TERM
) {
671 /* Find the earliest match beyond after_offset */
673 LIST_FOREACH(matches
, i
, m
->matches
) {
676 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
680 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
688 } else if (m
->type
== MATCH_AND_TERM
) {
691 /* Always jump to the next matching entry and repeat
692 * this until we find an offset that matches for all
698 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
702 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
703 last_moved
= m
->matches
;
705 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
708 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
712 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
713 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
723 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, ret
);
734 static int find_location_for_match(
738 direction_t direction
,
748 if (m
->type
== MATCH_DISCRETE
) {
752 if (JOURNAL_HEADER_KEYED_HASH(f
->header
))
753 hash
= journal_file_hash_data(f
, m
->data
, m
->size
);
757 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, hash
, &d
, &dp
);
761 /* FIXME: missing: find by monotonic */
763 if (j
->current_location
.type
== LOCATION_HEAD
)
764 return direction
== DIRECTION_DOWN
? journal_file_move_to_entry_for_data(f
, d
, DIRECTION_DOWN
, ret
, offset
) : 0;
765 if (j
->current_location
.type
== LOCATION_TAIL
)
766 return direction
== DIRECTION_UP
? journal_file_move_to_entry_for_data(f
, d
, DIRECTION_UP
, ret
, offset
) : 0;
767 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
768 return journal_file_move_to_entry_by_seqnum_for_data(f
, d
, j
->current_location
.seqnum
, direction
, ret
, offset
);
769 if (j
->current_location
.monotonic_set
) {
770 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, d
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
774 /* The data object might have been invalidated. */
775 r
= journal_file_move_to_object(f
, OBJECT_DATA
, dp
, &d
);
779 if (j
->current_location
.realtime_set
)
780 return journal_file_move_to_entry_by_realtime_for_data(f
, d
, j
->current_location
.realtime
, direction
, ret
, offset
);
782 return journal_file_move_to_entry_for_data(f
, d
, direction
, ret
, offset
);
784 } else if (m
->type
== MATCH_OR_TERM
) {
787 /* Find the earliest match */
789 LIST_FOREACH(matches
, i
, m
->matches
) {
792 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
796 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
805 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, ret
);
818 assert(m
->type
== MATCH_AND_TERM
);
820 /* First jump to the last match, and then find the
821 * next one where all matches match */
826 LIST_FOREACH(matches
, i
, m
->matches
) {
829 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
833 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
837 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
841 static int find_location_with_matches(
844 direction_t direction
,
856 /* No matches is simple */
858 if (j
->current_location
.type
== LOCATION_HEAD
)
859 return direction
== DIRECTION_DOWN
? journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
) : 0;
860 if (j
->current_location
.type
== LOCATION_TAIL
)
861 return direction
== DIRECTION_UP
? journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
) : 0;
862 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
863 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
864 if (j
->current_location
.monotonic_set
) {
865 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
869 if (j
->current_location
.realtime_set
)
870 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
872 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
874 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
877 static int next_with_matches(
880 direction_t direction
,
889 /* No matches is easy. We simple advance the file
892 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
894 /* If we have a match then we look for the next matching entry
895 * with an offset at least one step larger */
896 return next_for_match(j
, j
->level0
, f
,
897 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
898 : f
->current_offset
- 1,
899 direction
, ret
, offset
);
902 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
904 uint64_t cp
, n_entries
;
910 (void) journal_file_read_tail_timestamp(j
, f
);
912 n_entries
= le64toh(f
->header
->n_entries
);
914 /* If we hit EOF before, we don't need to look into this file again
915 * unless direction changed or new entries appeared. */
916 if (f
->last_direction
== direction
&&
917 f
->location_type
== (direction
== DIRECTION_DOWN
? LOCATION_TAIL
: LOCATION_HEAD
) &&
918 n_entries
== f
->last_n_entries
)
921 f
->last_n_entries
= n_entries
;
923 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
924 /* LOCATION_SEEK here means we did the work in a previous
925 * iteration and the current location already points to a
926 * candidate entry. */
927 if (f
->location_type
!= LOCATION_SEEK
) {
928 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
932 journal_file_save_location(f
, c
, cp
);
935 f
->last_direction
= direction
;
937 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
941 journal_file_save_location(f
, c
, cp
);
944 /* OK, we found the spot, now let's advance until an entry
945 * that is actually different from what we were previously
946 * looking at. This is necessary to handle entries which exist
947 * in two (or more) journal files, and which shall all be
948 * suppressed but one. */
953 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
956 k
= compare_with_location(j
, f
, &j
->current_location
, j
->current_file
);
958 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
965 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
969 journal_file_save_location(f
, c
, cp
);
973 static int compare_locations(sd_journal
*j
, JournalFile
*af
, JournalFile
*bf
) {
981 assert(af
->location_type
== LOCATION_SEEK
);
982 assert(bf
->location_type
== LOCATION_SEEK
);
984 /* If contents, timestamps and seqnum match, these entries are identical. */
985 if (sd_id128_equal(af
->current_boot_id
, bf
->current_boot_id
) &&
986 af
->current_monotonic
== bf
->current_monotonic
&&
987 af
->current_realtime
== bf
->current_realtime
&&
988 af
->current_xor_hash
== bf
->current_xor_hash
&&
989 sd_id128_equal(af
->header
->seqnum_id
, bf
->header
->seqnum_id
) &&
990 af
->current_seqnum
== bf
->current_seqnum
)
993 if (sd_id128_equal(af
->header
->seqnum_id
, bf
->header
->seqnum_id
)) {
994 /* If this is from the same seqnum source, compare seqnums */
995 r
= CMP(af
->current_seqnum
, bf
->current_seqnum
);
999 /* Wow! This is weird, different data but the same seqnums? Something is borked, but let's
1000 * make the best of it and compare by time. */
1003 if (sd_id128_equal(af
->current_boot_id
, bf
->current_boot_id
))
1004 /* If the boot id matches, compare monotonic time */
1005 r
= CMP(af
->current_monotonic
, bf
->current_monotonic
);
1007 /* If they don't match try to compare boot IDs */
1008 r
= compare_boot_ids(j
, af
->current_boot_id
, bf
->current_boot_id
);
1012 /* Otherwise, compare UTC time */
1013 r
= CMP(af
->current_realtime
, bf
->current_realtime
);
1017 /* Finally, compare by contents */
1018 return CMP(af
->current_xor_hash
, bf
->current_xor_hash
);
1021 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
1022 JournalFile
*new_file
= NULL
;
1028 assert_return(j
, -EINVAL
);
1029 assert_return(!journal_origin_changed(j
), -ECHILD
);
1031 r
= iterated_cache_get(j
->files_cache
, NULL
, &files
, &n_files
);
1035 FOREACH_ARRAY(_f
, files
, n_files
) {
1036 JournalFile
*f
= (JournalFile
*) *_f
;
1039 r
= next_beyond_location(j
, f
, direction
);
1041 log_debug_errno(r
, "Can't iterate through %s, ignoring: %m", f
->path
);
1042 remove_file_real(j
, f
);
1044 } else if (r
== 0) {
1045 f
->location_type
= direction
== DIRECTION_DOWN
? LOCATION_TAIL
: LOCATION_HEAD
;
1054 k
= compare_locations(j
, f
, new_file
);
1056 found
= direction
== DIRECTION_DOWN
? k
< 0 : k
> 0;
1066 r
= journal_file_move_to_object(new_file
, OBJECT_ENTRY
, new_file
->current_offset
, &o
);
1070 set_location(j
, new_file
, o
);
1075 _public_
int sd_journal_next(sd_journal
*j
) {
1076 return real_journal_next(j
, DIRECTION_DOWN
);
1079 _public_
int sd_journal_previous(sd_journal
*j
) {
1080 return real_journal_next(j
, DIRECTION_UP
);
1083 _public_
int sd_journal_step_one(sd_journal
*j
, int advanced
) {
1084 assert_return(j
, -EINVAL
);
1086 if (j
->current_location
.type
== LOCATION_HEAD
)
1087 return sd_journal_next(j
);
1088 if (j
->current_location
.type
== LOCATION_TAIL
)
1089 return sd_journal_previous(j
);
1090 return real_journal_next(j
, advanced
? DIRECTION_DOWN
: DIRECTION_UP
);
1093 static int real_journal_next_skip(sd_journal
*j
, direction_t direction
, uint64_t skip
) {
1096 assert_return(j
, -EINVAL
);
1097 assert_return(!journal_origin_changed(j
), -ECHILD
);
1098 assert_return(skip
<= INT_MAX
, -ERANGE
);
1101 /* If this is not a discrete skip, then at least
1102 * resolve the current location */
1103 if (j
->current_location
.type
!= LOCATION_DISCRETE
) {
1104 r
= real_journal_next(j
, direction
);
1113 r
= real_journal_next(j
, direction
);
1127 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
1128 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
1131 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
1132 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
1135 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
1139 assert_return(j
, -EINVAL
);
1140 assert_return(!journal_origin_changed(j
), -ECHILD
);
1141 assert_return(cursor
, -EINVAL
);
1143 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1144 return -EADDRNOTAVAIL
;
1146 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1150 if (asprintf(cursor
,
1151 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
1152 SD_ID128_TO_STRING(j
->current_file
->header
->seqnum_id
), le64toh(o
->entry
.seqnum
),
1153 SD_ID128_TO_STRING(o
->entry
.boot_id
), le64toh(o
->entry
.monotonic
),
1154 le64toh(o
->entry
.realtime
),
1155 le64toh(o
->entry
.xor_hash
)) < 0)
1161 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
1162 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
1163 bool seqnum_id_set
= false,
1165 boot_id_set
= false,
1166 monotonic_set
= false,
1167 realtime_set
= false,
1168 xor_hash_set
= false;
1169 sd_id128_t seqnum_id
, boot_id
;
1172 assert_return(j
, -EINVAL
);
1173 assert_return(!journal_origin_changed(j
), -ECHILD
);
1174 assert_return(!isempty(cursor
), -EINVAL
);
1176 for (const char *p
= cursor
;;) {
1177 _cleanup_free_
char *word
= NULL
;
1179 r
= extract_first_word(&p
, &word
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1185 if (word
[0] == '\0' || word
[1] != '=')
1190 seqnum_id_set
= true;
1191 r
= sd_id128_from_string(word
+ 2, &seqnum_id
);
1198 if (sscanf(word
+ 2, "%llx", &seqnum
) != 1)
1204 r
= sd_id128_from_string(word
+ 2, &boot_id
);
1210 monotonic_set
= true;
1211 if (sscanf(word
+ 2, "%llx", &monotonic
) != 1)
1216 realtime_set
= true;
1217 if (sscanf(word
+ 2, "%llx", &realtime
) != 1)
1222 xor_hash_set
= true;
1223 if (sscanf(word
+ 2, "%llx", &xor_hash
) != 1)
1229 if ((!seqnum_set
|| !seqnum_id_set
) &&
1230 (!monotonic_set
|| !boot_id_set
) &&
1235 j
->current_location
= (Location
) {
1236 .type
= LOCATION_SEEK
,
1240 j
->current_location
.realtime
= (uint64_t) realtime
;
1241 j
->current_location
.realtime_set
= true;
1244 if (seqnum_set
&& seqnum_id_set
) {
1245 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1246 j
->current_location
.seqnum_id
= seqnum_id
;
1247 j
->current_location
.seqnum_set
= true;
1250 if (monotonic_set
&& boot_id_set
) {
1251 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1252 j
->current_location
.boot_id
= boot_id
;
1253 j
->current_location
.monotonic_set
= true;
1257 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1258 j
->current_location
.xor_hash_set
= true;
1264 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1268 assert_return(j
, -EINVAL
);
1269 assert_return(!journal_origin_changed(j
), -ECHILD
);
1270 assert_return(!isempty(cursor
), -EINVAL
);
1272 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1273 return -EADDRNOTAVAIL
;
1275 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1280 _cleanup_free_
char *item
= NULL
;
1281 unsigned long long ll
;
1285 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1292 if (strlen(item
) < 2 || item
[1] != '=')
1298 k
= sd_id128_from_string(item
+2, &id
);
1301 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1306 if (sscanf(item
+2, "%llx", &ll
) != 1)
1308 if (ll
!= le64toh(o
->entry
.seqnum
))
1313 k
= sd_id128_from_string(item
+2, &id
);
1316 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1321 if (sscanf(item
+2, "%llx", &ll
) != 1)
1323 if (ll
!= le64toh(o
->entry
.monotonic
))
1328 if (sscanf(item
+2, "%llx", &ll
) != 1)
1330 if (ll
!= le64toh(o
->entry
.realtime
))
1335 if (sscanf(item
+2, "%llx", &ll
) != 1)
1337 if (ll
!= le64toh(o
->entry
.xor_hash
))
1346 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t usec
) {
1347 assert_return(j
, -EINVAL
);
1348 assert_return(!journal_origin_changed(j
), -ECHILD
);
1352 j
->current_location
= (Location
) {
1353 .type
= LOCATION_SEEK
,
1356 .monotonic_set
= true,
1362 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1363 assert_return(j
, -EINVAL
);
1364 assert_return(!journal_origin_changed(j
), -ECHILD
);
1368 j
->current_location
= (Location
) {
1369 .type
= LOCATION_SEEK
,
1371 .realtime_set
= true,
1377 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1378 assert_return(j
, -EINVAL
);
1379 assert_return(!journal_origin_changed(j
), -ECHILD
);
1383 j
->current_location
= (Location
) {
1384 .type
= LOCATION_HEAD
,
1390 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1391 assert_return(j
, -EINVAL
);
1392 assert_return(!journal_origin_changed(j
), -ECHILD
);
1396 j
->current_location
= (Location
) {
1397 .type
= LOCATION_TAIL
,
1403 static void check_network(sd_journal
*j
, int fd
) {
1409 j
->on_network
= fd_is_network_fs(fd
);
1412 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1413 const char *full
, *tilded
, *atted
;
1415 full
= strjoina(prefix
, ".journal");
1416 tilded
= strjoina(full
, "~");
1417 atted
= strjoina(prefix
, "@");
1419 return STR_IN_SET(filename
, full
, tilded
) ||
1420 startswith(filename
, atted
);
1423 static bool file_type_wanted(int flags
, const char *filename
) {
1426 if (!ENDSWITH_SET(filename
, ".journal", ".journal~"))
1429 /* no flags set → every type is OK */
1430 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1433 if (FLAGS_SET(flags
, SD_JOURNAL_CURRENT_USER
)) {
1434 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1436 xsprintf(prefix
, "user-" UID_FMT
, getuid());
1438 if (file_has_type_prefix(prefix
, filename
))
1441 /* If SD_JOURNAL_CURRENT_USER is specified and we are invoked under a system UID, then
1442 * automatically enable SD_JOURNAL_SYSTEM too, because journald will actually put system user
1443 * data into the system journal. */
1445 if (uid_for_system_journal(getuid()))
1446 flags
|= SD_JOURNAL_SYSTEM
;
1449 if (FLAGS_SET(flags
, SD_JOURNAL_SYSTEM
) && file_has_type_prefix("system", filename
))
1455 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1460 if (j
->toplevel_fd
>= 0)
1463 return path_startswith(path
, prefix
);
1466 static void track_file_disposition(sd_journal
*j
, JournalFile
*f
) {
1470 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1471 j
->has_runtime_files
= true;
1472 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1473 j
->has_persistent_files
= true;
1476 static int add_any_file(
1481 _cleanup_close_
int our_fd
= -EBADF
;
1487 assert(fd
>= 0 || path
);
1490 assert(path
); /* For gcc. */
1491 if (j
->toplevel_fd
>= 0)
1492 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1493 * openat() ignores the first argument. */
1495 fd
= our_fd
= openat(j
->toplevel_fd
, skip_leading_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1497 fd
= our_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1499 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1503 r
= fd_nonblock(fd
, false);
1505 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1510 if (fstat(fd
, &st
) < 0) {
1511 r
= log_debug_errno(errno
, "Failed to fstat %s: %m", path
?: "fd");
1515 r
= stat_verify_regular(&st
);
1517 log_debug_errno(r
, "Refusing to open %s: %m", path
?: "fd");
1522 f
= ordered_hashmap_get(j
->files
, path
);
1524 if (stat_inode_same(&f
->last_stat
, &st
)) {
1525 /* We already track this file, under the same path and with the same
1526 * device/inode numbers, it's hence really the same. Mark this file as seen
1527 * in this generation. This is used to GC old files in process_q_overflow()
1528 * to detect journal files that are still there and discern them from those
1529 * which are gone. */
1531 f
->last_seen_generation
= j
->generation
;
1532 (void) journal_file_read_tail_timestamp(j
, f
);
1536 /* So we tracked a file under this name, but it has a different inode/device. In that
1537 * case, it got replaced (probably due to rotation?), let's drop it hence from our
1539 remove_file_real(j
, f
);
1544 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1545 r
= log_debug_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
1546 "Too many open journal files, not adding %s.", path
?: "fd");
1550 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, 0, 0, NULL
, j
->mmap
, NULL
, &f
);
1552 log_debug_errno(r
, "Failed to open journal file %s: %m", path
?: "from fd");
1556 /* journal_file_dump(f); */
1558 /* journal_file_open() generates an replacement fname if necessary, so we can use f->path. */
1559 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1561 f
->close_fd
= false; /* Make sure journal_file_close() doesn't close the caller's fd
1562 * (or our own). The caller or we will do that ourselves. */
1563 (void) journal_file_close(f
);
1567 TAKE_FD(our_fd
); /* the fd is now owned by the JournalFile object */
1569 f
->last_seen_generation
= j
->generation
;
1571 track_file_disposition(j
, f
);
1572 check_network(j
, f
->fd
);
1573 (void) journal_file_read_tail_timestamp(j
, f
);
1575 j
->current_invalidate_counter
++;
1577 log_debug("File %s added.", f
->path
);
1582 (void) journal_put_error(j
, r
, path
); /* path==NULL is OK. */
1586 int journal_get_directories(sd_journal
*j
, char ***ret
) {
1587 _cleanup_strv_free_
char **paths
= NULL
;
1590 size_t n
= SIZE_MAX
;
1596 /* This returns parent directories of opened journal files. */
1598 ORDERED_HASHMAP_FOREACH_KEY(f
, p
, j
->files
) {
1599 _cleanup_free_
char *d
= NULL
;
1601 /* Ignore paths generated from fd. */
1602 if (path_startswith(p
, "/proc/"))
1605 r
= path_extract_directory(p
, &d
);
1609 if (path_strv_contains(paths
, d
))
1612 r
= strv_extend_with_size(&paths
, &n
, d
);
1617 *ret
= TAKE_PTR(paths
);
1621 static int add_file_by_name(
1624 const char *filename
) {
1626 _cleanup_free_
char *path
= NULL
;
1632 if (j
->no_new_files
)
1635 if (!file_type_wanted(j
->flags
, filename
))
1638 path
= path_join(prefix
, filename
);
1642 return add_any_file(j
, -1, path
);
1645 static int remove_file_by_name(
1648 const char *filename
) {
1650 _cleanup_free_
char *path
= NULL
;
1657 path
= path_join(prefix
, filename
);
1661 f
= ordered_hashmap_get(j
->files
, path
);
1665 remove_file_real(j
, f
);
1669 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1673 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1675 log_debug("File %s removed.", f
->path
);
1677 if (j
->current_file
== f
) {
1678 j
->current_file
= NULL
;
1679 j
->current_field
= 0;
1682 if (j
->unique_file
== f
) {
1683 /* Jump to the next unique_file or NULL if that one was last */
1684 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1685 j
->unique_offset
= 0;
1686 if (!j
->unique_file
)
1687 j
->unique_file_lost
= true;
1690 if (j
->fields_file
== f
) {
1691 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1692 j
->fields_offset
= 0;
1693 if (!j
->fields_file
)
1694 j
->fields_file_lost
= true;
1697 journal_file_unlink_newest_by_boot_id(j
, f
);
1698 (void) journal_file_close(f
);
1700 j
->current_invalidate_counter
++;
1703 static int dirname_is_machine_id(const char *fn
) {
1704 sd_id128_t id
, machine
;
1708 /* Returns true if the specified directory name matches the local machine ID */
1710 r
= sd_id128_get_machine(&machine
);
1714 e
= strchr(fn
, '.');
1718 /* Looks like it has a namespace suffix. Verify that. */
1719 if (!log_namespace_name_valid(e
+ 1))
1722 k
= strndupa_safe(fn
, e
- fn
);
1723 r
= sd_id128_from_string(k
, &id
);
1725 r
= sd_id128_from_string(fn
, &id
);
1729 return sd_id128_equal(id
, machine
);
1732 static int dirname_has_namespace(const char *fn
, const char *namespace) {
1735 /* Returns true if the specified directory name matches the specified namespace */
1737 e
= strchr(fn
, '.');
1744 if (!streq(e
+ 1, namespace))
1747 k
= strndupa_safe(fn
, e
- fn
);
1748 return id128_is_valid(k
);
1754 return id128_is_valid(fn
);
1757 static bool dirent_is_journal_file(const struct dirent
*de
) {
1760 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1762 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1765 return endswith(de
->d_name
, ".journal") ||
1766 endswith(de
->d_name
, ".journal~");
1769 static bool dirent_is_journal_subdir(const struct dirent
*de
) {
1773 /* returns true if the specified directory entry looks like a directory that might contain journal
1774 * files we might be interested in, i.e. is either a 128-bit ID or a 128-bit ID suffixed by a
1777 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1780 e
= strchr(de
->d_name
, '.');
1782 return id128_is_valid(de
->d_name
); /* No namespace */
1784 n
= strndupa_safe(de
->d_name
, e
- de
->d_name
);
1785 if (!id128_is_valid(n
))
1788 return log_namespace_name_valid(e
+ 1);
1791 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1798 if (j
->toplevel_fd
< 0)
1801 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1802 * relative, by dropping the initial slash */
1803 d
= xopendirat(j
->toplevel_fd
, skip_leading_slash(path
), 0);
1811 static Directory
* directory_free(Directory
*d
) {
1817 hashmap_remove_value(d
->journal
->directories_by_wd
, INT_TO_PTR(d
->wd
), d
) &&
1818 d
->journal
->inotify_fd
>= 0)
1819 (void) inotify_rm_watch(d
->journal
->inotify_fd
, d
->wd
);
1822 hashmap_remove_value(d
->journal
->directories_by_path
, d
->path
, d
);
1827 log_debug("Root directory %s removed.", d
->path
);
1829 log_debug("Directory %s removed.", d
->path
);
1837 DEFINE_TRIVIAL_CLEANUP_FUNC(Directory
*, directory_free
);
1839 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
1840 directories_by_path_hash_ops
,
1847 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
1848 directories_by_wd_hash_ops
,
1851 trivial_compare_func
,
1855 static int add_directory_impl(sd_journal
*j
, const char *path
, bool is_root
, Directory
**ret
) {
1856 _cleanup_(directory_freep
) Directory
*m
= NULL
;
1857 Directory
*existing
;
1864 existing
= hashmap_get(j
->directories_by_path
, path
);
1866 if (existing
->is_root
!= is_root
) {
1867 /* Don't 'downgrade' from root directory */
1876 m
= new(Directory
, 1);
1883 .path
= strdup(path
),
1890 r
= hashmap_ensure_put(&j
->directories_by_path
, &directories_by_path_hash_ops
, m
->path
, m
);
1894 j
->current_invalidate_counter
++;
1897 log_debug("Root directory %s added.", m
->path
);
1899 log_debug("Directory %s added.", m
->path
);
1905 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1907 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1912 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1913 if (dirent_is_journal_file(de
))
1914 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1916 if (m
->is_root
&& dirent_is_journal_subdir(de
))
1917 (void) add_directory(j
, m
->path
, de
->d_name
);
1922 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1925 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1932 /* Watch this directory if that's enabled and if it not being watched yet. */
1934 if (m
->wd
> 0) /* Already have a watch? */
1936 if (j
->inotify_fd
< 0) /* Not watching at all? */
1939 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1941 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1945 r
= hashmap_ensure_put(&j
->directories_by_wd
, &directories_by_wd_hash_ops
, INT_TO_PTR(m
->wd
), m
);
1948 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1950 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1951 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1957 static int add_directory(
1960 const char *dirname
) {
1962 _cleanup_free_
char *path
= NULL
;
1963 _cleanup_closedir_
DIR *d
= NULL
;
1970 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1971 * and reenumerates directory contents */
1973 path
= path_join(prefix
, dirname
);
1979 log_debug("Considering directory '%s'.", path
);
1981 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1982 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1983 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1987 (!(FLAGS_SET(j
->flags
, SD_JOURNAL_ALL_NAMESPACES
) ||
1988 dirname_has_namespace(dirname
, j
->namespace) > 0 ||
1989 (FLAGS_SET(j
->flags
, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE
) && dirname_has_namespace(dirname
, NULL
) > 0))))
1992 r
= directory_open(j
, path
, &d
);
1994 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
1998 r
= add_directory_impl(j
, path
, /* is_root = */ false, &m
);
2004 m
->last_seen_generation
= j
->generation
;
2006 directory_watch(j
, m
, dirfd(d
),
2007 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
2008 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
2011 if (!j
->no_new_files
)
2012 directory_enumerate(j
, m
, d
);
2014 check_network(j
, dirfd(d
));
2019 k
= journal_put_error(j
, r
, path
?: prefix
);
2026 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
2028 _cleanup_closedir_
DIR *d
= NULL
;
2034 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
2035 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
2036 * populate the set, as well as to update it later. */
2039 /* If there's a path specified, use it. */
2041 log_debug("Considering root directory '%s'.", p
);
2043 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
2044 !path_has_prefix(j
, p
, "/run"))
2048 p
= strjoina(j
->prefix
, p
);
2050 r
= directory_open(j
, p
, &d
);
2051 if (r
== -ENOENT
&& missing_ok
)
2054 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
2058 _cleanup_close_
int dfd
= -EBADF
;
2060 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
2061 * opendir() will take possession of the fd, and close it, which we don't want. */
2063 p
= "."; /* store this as "." in the directories hashmap */
2065 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
2071 d
= take_fdopendir(&dfd
);
2080 r
= add_directory_impl(j
, p
, /* is_root = */ true, &m
);
2086 directory_watch(j
, m
, dirfd(d
),
2087 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
2090 if (!j
->no_new_files
)
2091 directory_enumerate(j
, m
, d
);
2093 check_network(j
, dirfd(d
));
2098 k
= journal_put_error(j
, r
, p
);
2105 static int add_search_paths(sd_journal
*j
) {
2107 static const char search_paths
[] =
2108 "/run/log/journal\0"
2109 "/var/log/journal\0";
2113 /* We ignore most errors here, since the idea is to only open
2114 * what's actually accessible, and ignore the rest. */
2116 NULSTR_FOREACH(p
, search_paths
)
2117 (void) add_root_directory(j
, p
, true);
2119 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
2120 (void) add_root_directory(j
, "/var/log/journal/remote", true);
2125 static int add_current_paths(sd_journal
*j
) {
2129 assert(j
->no_new_files
);
2131 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
2132 * treat them as fatal. */
2134 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2135 _cleanup_free_
char *dir
= NULL
;
2138 r
= path_extract_directory(f
->path
, &dir
);
2142 r
= add_directory(j
, dir
, NULL
);
2150 static int allocate_inotify(sd_journal
*j
) {
2153 if (j
->inotify_fd
< 0) {
2154 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
2155 if (j
->inotify_fd
< 0)
2162 static sd_journal
*journal_new(int flags
, const char *path
, const char *namespace) {
2163 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2165 j
= new(sd_journal
, 1);
2170 .origin_id
= origin_id_query(),
2171 .toplevel_fd
= -EBADF
,
2172 .inotify_fd
= -EBADF
,
2174 .data_threshold
= DEFAULT_DATA_THRESHOLD
,
2184 if (flags
& SD_JOURNAL_OS_ROOT
)
2191 j
->namespace = strdup(namespace);
2196 j
->files
= ordered_hashmap_new(&path_hash_ops
);
2200 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
2201 j
->mmap
= mmap_cache_new();
2202 if (!j
->files_cache
|| !j
->mmap
)
2208 #define OPEN_ALLOWED_FLAGS \
2209 (SD_JOURNAL_LOCAL_ONLY | \
2210 SD_JOURNAL_RUNTIME_ONLY | \
2211 SD_JOURNAL_SYSTEM | \
2212 SD_JOURNAL_CURRENT_USER | \
2213 SD_JOURNAL_ALL_NAMESPACES | \
2214 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE | \
2215 SD_JOURNAL_ASSUME_IMMUTABLE)
2217 _public_
int sd_journal_open_namespace(sd_journal
**ret
, const char *namespace, int flags
) {
2218 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2221 assert_return(ret
, -EINVAL
);
2222 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
2224 j
= journal_new(flags
, NULL
, namespace);
2228 r
= add_search_paths(j
);
2236 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
2237 return sd_journal_open_namespace(ret
, NULL
, flags
);
2240 #define OPEN_CONTAINER_ALLOWED_FLAGS \
2241 (SD_JOURNAL_LOCAL_ONLY | \
2242 SD_JOURNAL_SYSTEM | \
2243 SD_JOURNAL_ASSUME_IMMUTABLE)
2245 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
2246 _cleanup_free_
char *root
= NULL
, *class = NULL
;
2247 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2251 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
2252 * combination with sd_journal_open_directory_fd(). */
2254 assert_return(machine
, -EINVAL
);
2255 assert_return(ret
, -EINVAL
);
2256 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
2257 assert_return(hostname_is_valid(machine
, 0), -EINVAL
);
2259 p
= strjoina("/run/systemd/machines/", machine
);
2260 r
= parse_env_file(NULL
, p
,
2270 if (!streq_ptr(class, "container"))
2273 j
= journal_new(flags
, root
, NULL
);
2277 r
= add_search_paths(j
);
2285 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
2286 (SD_JOURNAL_OS_ROOT | \
2287 SD_JOURNAL_SYSTEM | \
2288 SD_JOURNAL_CURRENT_USER | \
2289 SD_JOURNAL_ASSUME_IMMUTABLE)
2291 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
2292 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2295 assert_return(ret
, -EINVAL
);
2296 assert_return(path
, -EINVAL
);
2297 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
2299 j
= journal_new(flags
, path
, NULL
);
2303 if (flags
& SD_JOURNAL_OS_ROOT
)
2304 r
= add_search_paths(j
);
2306 r
= add_root_directory(j
, path
, false);
2314 #define OPEN_FILES_ALLOWED_FLAGS \
2315 (SD_JOURNAL_ASSUME_IMMUTABLE)
2317 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
2318 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2321 assert_return(ret
, -EINVAL
);
2322 assert_return((flags
& ~OPEN_FILES_ALLOWED_FLAGS
) == 0, -EINVAL
);
2324 j
= journal_new(flags
, NULL
, NULL
);
2328 STRV_FOREACH(path
, paths
) {
2329 r
= add_any_file(j
, -1, *path
);
2334 j
->no_new_files
= true;
2340 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
2341 (SD_JOURNAL_OS_ROOT | \
2342 SD_JOURNAL_SYSTEM | \
2343 SD_JOURNAL_CURRENT_USER | \
2344 SD_JOURNAL_TAKE_DIRECTORY_FD | \
2345 SD_JOURNAL_ASSUME_IMMUTABLE)
2347 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
2348 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2353 assert_return(ret
, -EINVAL
);
2354 assert_return(fd
>= 0, -EBADF
);
2355 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2357 if (fstat(fd
, &st
) < 0)
2360 if (!S_ISDIR(st
.st_mode
))
2363 take_fd
= FLAGS_SET(flags
, SD_JOURNAL_TAKE_DIRECTORY_FD
);
2364 j
= journal_new(flags
& ~SD_JOURNAL_TAKE_DIRECTORY_FD
, NULL
, NULL
);
2368 j
->toplevel_fd
= fd
;
2370 if (flags
& SD_JOURNAL_OS_ROOT
)
2371 r
= add_search_paths(j
);
2373 r
= add_root_directory(j
, NULL
, false);
2377 SET_FLAG(j
->flags
, SD_JOURNAL_TAKE_DIRECTORY_FD
, take_fd
);
2383 #define OPEN_FILES_FD_ALLOWED_FLAGS \
2384 (SD_JOURNAL_ASSUME_IMMUTABLE)
2386 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2388 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2391 assert_return(ret
, -EINVAL
);
2392 assert_return(n_fds
> 0, -EBADF
);
2393 assert_return((flags
& ~OPEN_FILES_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2395 j
= journal_new(flags
, NULL
, NULL
);
2399 for (unsigned i
= 0; i
< n_fds
; i
++) {
2407 if (fstat(fds
[i
], &st
) < 0) {
2412 r
= stat_verify_regular(&st
);
2416 r
= add_any_file(j
, fds
[i
], NULL
);
2421 j
->no_new_files
= true;
2422 j
->no_inotify
= true;
2428 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2430 ORDERED_HASHMAP_FOREACH(f
, j
->files
)
2431 f
->close_fd
= false;
2436 _public_
void sd_journal_close(sd_journal
*j
) {
2437 if (!j
|| journal_origin_changed(j
))
2440 journal_clear_newest_by_boot_id(j
);
2442 sd_journal_flush_matches(j
);
2444 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2445 iterated_cache_free(j
->files_cache
);
2447 hashmap_free(j
->directories_by_path
);
2448 hashmap_free(j
->directories_by_wd
);
2450 if (FLAGS_SET(j
->flags
, SD_JOURNAL_TAKE_DIRECTORY_FD
))
2451 safe_close(j
->toplevel_fd
);
2453 safe_close(j
->inotify_fd
);
2456 mmap_cache_stats_log_debug(j
->mmap
);
2457 mmap_cache_unref(j
->mmap
);
2460 hashmap_free_free(j
->errors
);
2462 set_free(j
->exclude_syslog_identifiers
);
2467 free(j
->unique_field
);
2468 free(j
->fields_buffer
);
2472 static int journal_file_read_tail_timestamp(sd_journal
*j
, JournalFile
*f
) {
2473 uint64_t offset
, mo
, rt
;
2483 /* Tries to read the timestamp of the most recently written entry. */
2485 if (FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
) && f
->newest_entry_offset
!= 0)
2486 return 0; /* We have already read the file, and we assume that the file is immutable. */
2488 if (f
->header
->state
== f
->newest_state
&&
2489 f
->header
->state
== STATE_ARCHIVED
&&
2490 f
->newest_entry_offset
!= 0)
2491 return 0; /* We have already read archived file. */
2493 if (JOURNAL_HEADER_CONTAINS(f
->header
, tail_entry_offset
)) {
2494 offset
= le64toh(READ_NOW(f
->header
->tail_entry_offset
));
2495 type
= OBJECT_ENTRY
;
2497 offset
= le64toh(READ_NOW(f
->header
->tail_object_offset
));
2498 type
= OBJECT_UNUSED
;
2501 return -ENODATA
; /* not a single object/entry, hence no tail timestamp */
2502 if (offset
== f
->newest_entry_offset
)
2503 return 0; /* No new entry is added after we read last time. */
2505 /* Move to the last object in the journal file, in the hope it is an entry (which it usually will
2506 * be). If we lack the "tail_entry_offset" field in the header, we specify the type as OBJECT_UNUSED
2507 * here, since we cannot be sure what the last object will be, and want no noisy logging if it isn't
2508 * an entry. We instead check after figuring out the pointer. */
2509 r
= journal_file_move_to_object(f
, type
, offset
, &o
);
2511 log_debug_errno(r
, "Failed to move to last object in journal file, ignoring: %m");
2515 if (o
&& o
->object
.type
== OBJECT_ENTRY
) {
2516 /* Yay, last object is an entry, let's use the data. */
2517 id
= o
->entry
.boot_id
;
2518 mo
= le64toh(o
->entry
.monotonic
);
2519 rt
= le64toh(o
->entry
.realtime
);
2521 /* So the object is not an entry or we couldn't access it? In that case, let's read the most
2522 * recent entry timestamps from the header. It's equally good. Unfortunately though, in old
2523 * versions of the journal the boot ID in the header doesn't have to match the monotonic
2524 * timestamp of the header. Let's check the header flag that indicates whether this strictly
2525 * matches first hence, before using the data. */
2527 if (JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f
->header
) && f
->header
->state
== STATE_ARCHIVED
) {
2528 mo
= le64toh(f
->header
->tail_entry_monotonic
);
2529 rt
= le64toh(f
->header
->tail_entry_realtime
);
2530 id
= f
->header
->tail_entry_boot_id
;
2531 offset
= UINT64_MAX
;
2533 /* Otherwise let's find the last entry manually (this possibly means traversing the
2534 * chain of entry arrays, till the end */
2535 r
= journal_file_next_entry(f
, 0, DIRECTION_UP
, &o
, offset
== 0 ? &offset
: NULL
);
2541 id
= o
->entry
.boot_id
;
2542 mo
= le64toh(o
->entry
.monotonic
);
2543 rt
= le64toh(o
->entry
.realtime
);
2547 if (mo
> rt
) /* monotonic clock is further ahead than realtime? that's weird, refuse to use the data */
2550 if (offset
== f
->newest_entry_offset
) {
2551 /* Cached data and the current one should be equivalent. */
2552 if (!sd_id128_equal(f
->newest_machine_id
, f
->header
->machine_id
) ||
2553 !sd_id128_equal(f
->newest_boot_id
, id
) ||
2554 f
->newest_monotonic_usec
!= mo
||
2555 f
->newest_realtime_usec
!= rt
)
2558 return 0; /* No new entry is added after we read last time. */
2561 if (!sd_id128_equal(f
->newest_boot_id
, id
))
2562 journal_file_unlink_newest_by_boot_id(j
, f
);
2564 f
->newest_boot_id
= id
;
2565 f
->newest_monotonic_usec
= mo
;
2566 f
->newest_realtime_usec
= rt
;
2567 f
->newest_machine_id
= f
->header
->machine_id
;
2568 f
->newest_entry_offset
= offset
;
2569 f
->newest_state
= f
->header
->state
;
2571 r
= journal_file_reshuffle_newest_by_boot_id(j
, f
);
2575 return 1; /* Updated. */
2578 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2583 assert_return(j
, -EINVAL
);
2584 assert_return(!journal_origin_changed(j
), -ECHILD
);
2586 f
= j
->current_file
;
2588 return -EADDRNOTAVAIL
;
2589 if (f
->current_offset
<= 0)
2590 return -EADDRNOTAVAIL
;
2592 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2596 uint64_t t
= le64toh(o
->entry
.realtime
);
2597 if (!VALID_REALTIME(t
))
2606 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2611 assert_return(j
, -EINVAL
);
2612 assert_return(!journal_origin_changed(j
), -ECHILD
);
2614 f
= j
->current_file
;
2616 return -EADDRNOTAVAIL
;
2617 if (f
->current_offset
<= 0)
2618 return -EADDRNOTAVAIL
;
2620 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2625 *ret_boot_id
= o
->entry
.boot_id
;
2629 r
= sd_id128_get_boot(&id
);
2633 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2637 uint64_t t
= le64toh(o
->entry
.monotonic
);
2638 if (!VALID_MONOTONIC(t
))
2647 _public_
int sd_journal_get_seqnum(
2649 uint64_t *ret_seqnum
,
2650 sd_id128_t
*ret_seqnum_id
) {
2656 assert_return(j
, -EINVAL
);
2657 assert_return(!journal_origin_changed(j
), -ECHILD
);
2659 f
= j
->current_file
;
2661 return -EADDRNOTAVAIL
;
2663 if (f
->current_offset
<= 0)
2664 return -EADDRNOTAVAIL
;
2666 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2671 *ret_seqnum_id
= f
->header
->seqnum_id
;
2673 *ret_seqnum
= le64toh(o
->entry
.seqnum
);
2678 static bool field_is_valid(const char *field
) {
2684 if (startswith(field
, "__"))
2687 for (const char *p
= field
; *p
; p
++) {
2692 if (*p
>= 'A' && *p
<= 'Z')
2695 if (ascii_isdigit(*p
))
2704 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2706 size_t field_length
;
2710 assert_return(j
, -EINVAL
);
2711 assert_return(!journal_origin_changed(j
), -ECHILD
);
2712 assert_return(field
, -EINVAL
);
2713 assert_return(data
, -EINVAL
);
2714 assert_return(size
, -EINVAL
);
2715 assert_return(field_is_valid(field
), -EINVAL
);
2717 f
= j
->current_file
;
2719 return -EADDRNOTAVAIL
;
2721 if (f
->current_offset
<= 0)
2722 return -EADDRNOTAVAIL
;
2724 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2728 field_length
= strlen(field
);
2730 uint64_t n
= journal_file_entry_n_items(f
, o
);
2731 for (uint64_t i
= 0; i
< n
; i
++) {
2736 p
= journal_file_entry_item_object_offset(f
, o
, i
);
2737 r
= journal_file_data_payload(f
, NULL
, p
, field
, field_length
, j
->data_threshold
, &d
, &l
);
2740 if (IN_SET(r
, -EADDRNOTAVAIL
, -EBADMSG
)) {
2741 log_debug_errno(r
, "Entry item %"PRIu64
" data object is bad, skipping over it: %m", i
);
2756 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2761 assert_return(j
, -EINVAL
);
2762 assert_return(!journal_origin_changed(j
), -ECHILD
);
2763 assert_return(data
, -EINVAL
);
2764 assert_return(size
, -EINVAL
);
2766 f
= j
->current_file
;
2768 return -EADDRNOTAVAIL
;
2770 if (f
->current_offset
<= 0)
2771 return -EADDRNOTAVAIL
;
2773 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2777 for (uint64_t n
= journal_file_entry_n_items(f
, o
); j
->current_field
< n
; j
->current_field
++) {
2782 p
= journal_file_entry_item_object_offset(f
, o
, j
->current_field
);
2783 r
= journal_file_data_payload(f
, NULL
, p
, NULL
, 0, j
->data_threshold
, &d
, &l
);
2784 if (IN_SET(r
, -EADDRNOTAVAIL
, -EBADMSG
)) {
2785 log_debug_errno(r
, "Entry item %"PRIu64
" data object is bad, skipping over it: %m", j
->current_field
);
2803 _public_
int sd_journal_enumerate_available_data(sd_journal
*j
, const void **data
, size_t *size
) {
2807 r
= sd_journal_enumerate_data(j
, data
, size
);
2810 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r
))
2812 j
->current_field
++; /* Try with the next field */
2816 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2817 if (!j
|| journal_origin_changed(j
))
2820 j
->current_field
= 0;
2823 static int reiterate_all_paths(sd_journal
*j
) {
2826 if (j
->no_new_files
)
2827 return add_current_paths(j
);
2829 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2830 return add_search_paths(j
);
2832 if (j
->toplevel_fd
>= 0)
2833 return add_root_directory(j
, NULL
, false);
2836 return add_root_directory(j
, j
->path
, true);
2838 return add_search_paths(j
);
2841 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2844 assert_return(j
, -EINVAL
);
2845 assert_return(!journal_origin_changed(j
), -ECHILD
);
2846 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
2849 return -EMEDIUMTYPE
;
2851 if (j
->inotify_fd
>= 0)
2852 return j
->inotify_fd
;
2854 r
= allocate_inotify(j
);
2858 log_debug("Reiterating files to get inotify watches established.");
2860 /* Iterate through all dirs again, to add them to the inotify */
2861 r
= reiterate_all_paths(j
);
2865 return j
->inotify_fd
;
2868 _public_
int sd_journal_get_events(sd_journal
*j
) {
2871 assert_return(j
, -EINVAL
);
2872 assert_return(!journal_origin_changed(j
), -ECHILD
);
2873 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
2875 fd
= sd_journal_get_fd(j
);
2882 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2885 assert_return(j
, -EINVAL
);
2886 assert_return(!journal_origin_changed(j
), -ECHILD
);
2887 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
2888 assert_return(timeout_usec
, -EINVAL
);
2890 fd
= sd_journal_get_fd(j
);
2894 if (!j
->on_network
) {
2895 *timeout_usec
= UINT64_MAX
;
2899 /* If we are on the network we need to regularly check for
2900 * changes manually */
2902 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2906 static void process_q_overflow(sd_journal
*j
) {
2912 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2913 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2914 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2915 * are subject for unloading. */
2917 log_debug("Inotify queue overrun, reiterating everything.");
2920 (void) reiterate_all_paths(j
);
2922 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2924 if (f
->last_seen_generation
== j
->generation
)
2927 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2928 remove_file_real(j
, f
);
2931 HASHMAP_FOREACH(m
, j
->directories_by_path
) {
2933 if (m
->last_seen_generation
== j
->generation
)
2936 if (m
->is_root
) /* Never GC root directories */
2939 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2943 log_debug("Reiteration complete.");
2946 static void process_inotify_event(sd_journal
*j
, const struct inotify_event
*e
) {
2952 if (e
->mask
& IN_Q_OVERFLOW
) {
2953 process_q_overflow(j
);
2957 /* Is this a subdirectory we watch? */
2958 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2960 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2961 (endswith(e
->name
, ".journal") ||
2962 endswith(e
->name
, ".journal~"))) {
2964 /* Event for a journal file */
2966 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2967 (void) add_file_by_name(j
, d
->path
, e
->name
);
2968 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2969 (void) remove_file_by_name(j
, d
->path
, e
->name
);
2971 } else if (!d
->is_root
&& e
->len
== 0) {
2973 /* Event for a subdirectory */
2975 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2978 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2980 /* Event for root directory */
2982 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2983 (void) add_directory(j
, d
->path
, e
->name
);
2989 if (e
->mask
& IN_IGNORED
)
2992 log_debug("Unexpected inotify event.");
2995 static int determine_change(sd_journal
*j
) {
3000 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
3001 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
3003 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
3006 _public_
int sd_journal_process(sd_journal
*j
) {
3007 bool got_something
= false;
3009 assert_return(j
, -EINVAL
);
3010 assert_return(!journal_origin_changed(j
), -ECHILD
);
3012 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
3015 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
3017 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
3018 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
3021 union inotify_event_buffer buffer
;
3024 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
3026 if (ERRNO_IS_TRANSIENT(errno
))
3027 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
3032 got_something
= true;
3034 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
3035 process_inotify_event(j
, e
);
3039 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
3043 assert_return(j
, -EINVAL
);
3044 assert_return(!journal_origin_changed(j
), -ECHILD
);
3045 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
3047 if (j
->inotify_fd
< 0) {
3050 /* This is the first invocation, hence create the inotify watch */
3051 r
= sd_journal_get_fd(j
);
3055 /* Server might have done some vacuuming while we weren't watching. Get rid of the deleted
3056 * files now so they don't stay around indefinitely. */
3057 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3058 r
= journal_file_fstat(f
);
3060 remove_file_real(j
, f
);
3062 log_debug_errno(r
, "Failed to fstat() journal file '%s', ignoring: %m", f
->path
);
3065 /* The journal might have changed since the context object was created and we weren't
3066 * watching before, hence don't wait for anything, and return immediately. */
3067 return determine_change(j
);
3070 r
= sd_journal_get_timeout(j
, &t
);
3074 if (t
!= UINT64_MAX
) {
3075 t
= usec_sub_unsigned(t
, now(CLOCK_MONOTONIC
));
3077 if (timeout_usec
== UINT64_MAX
|| timeout_usec
> t
)
3082 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
3083 } while (r
== -EINTR
);
3088 return sd_journal_process(j
);
3091 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
3094 uint64_t fmin
= 0, tmax
= 0;
3097 assert_return(j
, -EINVAL
);
3098 assert_return(!journal_origin_changed(j
), -ECHILD
);
3099 assert_return(from
|| to
, -EINVAL
);
3100 assert_return(from
!= to
, -EINVAL
);
3102 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3105 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
3118 fmin
= MIN(fr
, fmin
);
3119 tmax
= MAX(t
, tmax
);
3128 return first
? 0 : 1;
3131 _public_
int sd_journal_get_cutoff_monotonic_usec(
3137 uint64_t from
= UINT64_MAX
, to
= UINT64_MAX
;
3142 assert_return(j
, -EINVAL
);
3143 assert_return(!journal_origin_changed(j
), -ECHILD
);
3144 assert_return(ret_from
!= ret_to
, -EINVAL
);
3146 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3149 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &ff
, &tt
);
3158 from
= MIN(ff
, from
);
3175 void journal_print_header(sd_journal
*j
) {
3177 bool newline
= false;
3181 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3187 journal_file_print_header(f
);
3191 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *ret
) {
3195 assert_return(j
, -EINVAL
);
3196 assert_return(!journal_origin_changed(j
), -ECHILD
);
3197 assert_return(ret
, -EINVAL
);
3199 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3203 if (fstat(f
->fd
, &st
) < 0)
3206 b
= (uint64_t) st
.st_blocks
;
3207 if (b
> UINT64_MAX
/ 512)
3211 if (sum
> UINT64_MAX
- b
)
3220 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
3223 assert_return(j
, -EINVAL
);
3224 assert_return(!journal_origin_changed(j
), -ECHILD
);
3226 if (!field_is_valid(field
))
3229 r
= free_and_strdup(&j
->unique_field
, field
);
3233 j
->unique_file
= NULL
;
3234 j
->unique_offset
= 0;
3235 j
->unique_file_lost
= false;
3240 _public_
int sd_journal_enumerate_unique(
3242 const void **ret_data
,
3247 assert_return(j
, -EINVAL
);
3248 assert_return(!journal_origin_changed(j
), -ECHILD
);
3249 assert_return(j
->unique_field
, -EINVAL
);
3251 k
= strlen(j
->unique_field
);
3253 if (!j
->unique_file
) {
3254 if (j
->unique_file_lost
)
3257 j
->unique_file
= ordered_hashmap_first(j
->files
);
3258 if (!j
->unique_file
)
3261 j
->unique_offset
= 0;
3272 /* Proceed to next data object in the field's linked list */
3273 if (j
->unique_offset
== 0) {
3274 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
3278 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
3280 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
3284 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
3287 /* We reached the end of the list? Then start again, with the next file */
3288 if (j
->unique_offset
== 0) {
3289 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
3290 if (!j
->unique_file
)
3296 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
3300 /* Let's pin the data object, so we can look at it at the same time as one on another file. */
3301 r
= journal_file_pin_object(j
->unique_file
, o
);
3305 r
= journal_file_data_payload(j
->unique_file
, o
, j
->unique_offset
, NULL
, 0,
3306 j
->data_threshold
, &odata
, &ol
);
3310 /* Check if we have at least the field name and "=". */
3312 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3313 "%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
3314 j
->unique_file
->path
,
3315 j
->unique_offset
, ol
, k
+ 1);
3317 if (memcmp(odata
, j
->unique_field
, k
) != 0 || ((const char*) odata
)[k
] != '=')
3318 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3319 "%s:offset " OFSfmt
": object does not start with \"%s=\"",
3320 j
->unique_file
->path
,
3324 /* OK, now let's see if we already returned this data object by checking if it exists in the
3325 * earlier traversed files. */
3327 ORDERED_HASHMAP_FOREACH(of
, j
->files
) {
3328 if (of
== j
->unique_file
)
3331 /* Skip this file it didn't have any fields indexed */
3332 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3335 /* We can reuse the hash from our current file only on old-style journal files
3336 * without keyed hashes. On new-style files we have to calculate the hash anew, to
3337 * take the per-file hash seed into consideration. */
3338 if (!JOURNAL_HEADER_KEYED_HASH(j
->unique_file
->header
) && !JOURNAL_HEADER_KEYED_HASH(of
->header
))
3339 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
3341 r
= journal_file_find_data_object(of
, odata
, ol
, NULL
, NULL
);
3360 _public_
int sd_journal_enumerate_available_unique(sd_journal
*j
, const void **data
, size_t *size
) {
3364 r
= sd_journal_enumerate_unique(j
, data
, size
);
3367 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r
))
3369 /* Try with the next field. sd_journal_enumerate_unique() modifies state, so on the next try
3370 * we will access the next field. */
3374 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
3375 if (!j
|| journal_origin_changed(j
))
3378 j
->unique_file
= NULL
;
3379 j
->unique_offset
= 0;
3380 j
->unique_file_lost
= false;
3383 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
3386 assert_return(j
, -EINVAL
);
3387 assert_return(!journal_origin_changed(j
), -ECHILD
);
3388 assert_return(field
, -EINVAL
);
3390 if (!j
->fields_file
) {
3391 if (j
->fields_file_lost
)
3394 j
->fields_file
= ordered_hashmap_first(j
->files
);
3395 if (!j
->fields_file
)
3398 j
->fields_hash_table_index
= 0;
3399 j
->fields_offset
= 0;
3403 JournalFile
*f
, *of
;
3411 if (j
->fields_offset
== 0) {
3414 /* We are not yet positioned at any field. Let's pick the first one */
3415 r
= journal_file_map_field_hash_table(f
);
3419 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
3421 if (j
->fields_hash_table_index
>= m
) {
3422 /* Reached the end of the hash table, go to the next file. */
3427 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
3429 if (j
->fields_offset
!= 0)
3432 /* Empty hash table bucket, go to next one */
3433 j
->fields_hash_table_index
++;
3437 /* Proceed with next file */
3438 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
3439 if (!j
->fields_file
) {
3444 j
->fields_offset
= 0;
3445 j
->fields_hash_table_index
= 0;
3450 /* We are already positioned at a field. If so, let's figure out the next field from it */
3452 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
3456 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
3457 if (j
->fields_offset
== 0) {
3458 /* Reached the end of the hash table chain */
3459 j
->fields_hash_table_index
++;
3464 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
3465 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
3469 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
3470 if (o
->object
.type
!= OBJECT_FIELD
)
3471 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3472 "%s:offset " OFSfmt
": object has type %i, expected %i",
3473 f
->path
, j
->fields_offset
,
3474 o
->object
.type
, OBJECT_FIELD
);
3476 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
3478 /* Let's see if we already returned this field name before. */
3480 ORDERED_HASHMAP_FOREACH(of
, j
->files
) {
3484 /* Skip this file it didn't have any fields indexed */
3485 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3488 if (!JOURNAL_HEADER_KEYED_HASH(f
->header
) && !JOURNAL_HEADER_KEYED_HASH(of
->header
))
3489 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
,
3490 le64toh(o
->field
.hash
), NULL
, NULL
);
3492 r
= journal_file_find_field_object(of
, o
->field
.payload
, sz
, NULL
, NULL
);
3504 /* Check if this is really a valid string containing no NUL byte */
3505 if (memchr(o
->field
.payload
, 0, sz
))
3508 if (j
->data_threshold
> 0 && sz
> j
->data_threshold
)
3509 sz
= j
->data_threshold
;
3511 if (!GREEDY_REALLOC(j
->fields_buffer
, sz
+ 1))
3514 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3515 j
->fields_buffer
[sz
] = 0;
3517 if (!field_is_valid(j
->fields_buffer
))
3520 *field
= j
->fields_buffer
;
3525 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3526 if (!j
|| journal_origin_changed(j
))
3529 j
->fields_file
= NULL
;
3530 j
->fields_hash_table_index
= 0;
3531 j
->fields_offset
= 0;
3532 j
->fields_file_lost
= false;
3535 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3536 assert_return(j
, -EINVAL
);
3537 assert_return(!journal_origin_changed(j
), -ECHILD
);
3539 return !j
->on_network
;
3542 static char *lookup_field(const char *field
, void *userdata
) {
3543 sd_journal
*j
= ASSERT_PTR(userdata
);
3550 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3552 size
> REPLACE_VAR_MAX
)
3553 return strdup(field
);
3555 d
= strlen(field
) + 1;
3557 return strndup((const char*) data
+ d
, size
- d
);
3560 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3564 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3568 assert_return(j
, -EINVAL
);
3569 assert_return(!journal_origin_changed(j
), -ECHILD
);
3570 assert_return(ret
, -EINVAL
);
3572 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3576 cid
= strndup((const char*) data
+ 11, size
- 11);
3580 r
= sd_id128_from_string(cid
, &id
);
3584 r
= catalog_get(secure_getenv("SYSTEMD_CATALOG") ?: CATALOG_DATABASE
, id
, &text
);
3588 t
= replace_var(text
, lookup_field
, j
);
3596 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3597 assert_return(ret
, -EINVAL
);
3599 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3602 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3603 assert_return(j
, -EINVAL
);
3604 assert_return(!journal_origin_changed(j
), -ECHILD
);
3606 j
->data_threshold
= sz
;
3610 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3611 assert_return(j
, -EINVAL
);
3612 assert_return(!journal_origin_changed(j
), -ECHILD
);
3613 assert_return(sz
, -EINVAL
);
3615 *sz
= j
->data_threshold
;
3619 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3620 assert_return(j
, -EINVAL
);
3622 return j
->has_runtime_files
;
3625 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3626 assert_return(j
, -EINVAL
);
3628 return j
->has_persistent_files
;