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 const char *skip_slash(const char *p
) {
1487 static int add_any_file(
1492 _cleanup_close_
int our_fd
= -EBADF
;
1498 assert(fd
>= 0 || path
);
1501 assert(path
); /* For gcc. */
1502 if (j
->toplevel_fd
>= 0)
1503 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1504 * openat() ignores the first argument. */
1506 fd
= our_fd
= openat(j
->toplevel_fd
, skip_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1508 fd
= our_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1510 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1514 r
= fd_nonblock(fd
, false);
1516 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1521 if (fstat(fd
, &st
) < 0) {
1522 r
= log_debug_errno(errno
, "Failed to fstat %s: %m", path
?: "fd");
1526 r
= stat_verify_regular(&st
);
1528 log_debug_errno(r
, "Refusing to open %s: %m", path
?: "fd");
1533 f
= ordered_hashmap_get(j
->files
, path
);
1535 if (stat_inode_same(&f
->last_stat
, &st
)) {
1536 /* We already track this file, under the same path and with the same
1537 * device/inode numbers, it's hence really the same. Mark this file as seen
1538 * in this generation. This is used to GC old files in process_q_overflow()
1539 * to detect journal files that are still there and discern them from those
1540 * which are gone. */
1542 f
->last_seen_generation
= j
->generation
;
1543 (void) journal_file_read_tail_timestamp(j
, f
);
1547 /* So we tracked a file under this name, but it has a different inode/device. In that
1548 * case, it got replaced (probably due to rotation?), let's drop it hence from our
1550 remove_file_real(j
, f
);
1555 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1556 r
= log_debug_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
1557 "Too many open journal files, not adding %s.", path
?: "fd");
1561 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, 0, 0, NULL
, j
->mmap
, NULL
, &f
);
1563 log_debug_errno(r
, "Failed to open journal file %s: %m", path
?: "from fd");
1567 /* journal_file_dump(f); */
1569 /* journal_file_open() generates an replacement fname if necessary, so we can use f->path. */
1570 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1572 f
->close_fd
= false; /* Make sure journal_file_close() doesn't close the caller's fd
1573 * (or our own). The caller or we will do that ourselves. */
1574 (void) journal_file_close(f
);
1578 TAKE_FD(our_fd
); /* the fd is now owned by the JournalFile object */
1580 f
->last_seen_generation
= j
->generation
;
1582 track_file_disposition(j
, f
);
1583 check_network(j
, f
->fd
);
1584 (void) journal_file_read_tail_timestamp(j
, f
);
1586 j
->current_invalidate_counter
++;
1588 log_debug("File %s added.", f
->path
);
1593 (void) journal_put_error(j
, r
, path
); /* path==NULL is OK. */
1597 int journal_get_directories(sd_journal
*j
, char ***ret
) {
1598 _cleanup_strv_free_
char **paths
= NULL
;
1601 size_t n
= SIZE_MAX
;
1607 /* This returns parent directories of opened journal files. */
1609 ORDERED_HASHMAP_FOREACH_KEY(f
, p
, j
->files
) {
1610 _cleanup_free_
char *d
= NULL
;
1612 /* Ignore paths generated from fd. */
1613 if (path_startswith(p
, "/proc/"))
1616 r
= path_extract_directory(p
, &d
);
1620 if (path_strv_contains(paths
, d
))
1623 r
= strv_extend_with_size(&paths
, &n
, d
);
1628 *ret
= TAKE_PTR(paths
);
1632 static int add_file_by_name(
1635 const char *filename
) {
1637 _cleanup_free_
char *path
= NULL
;
1643 if (j
->no_new_files
)
1646 if (!file_type_wanted(j
->flags
, filename
))
1649 path
= path_join(prefix
, filename
);
1653 return add_any_file(j
, -1, path
);
1656 static int remove_file_by_name(
1659 const char *filename
) {
1661 _cleanup_free_
char *path
= NULL
;
1668 path
= path_join(prefix
, filename
);
1672 f
= ordered_hashmap_get(j
->files
, path
);
1676 remove_file_real(j
, f
);
1680 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1684 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1686 log_debug("File %s removed.", f
->path
);
1688 if (j
->current_file
== f
) {
1689 j
->current_file
= NULL
;
1690 j
->current_field
= 0;
1693 if (j
->unique_file
== f
) {
1694 /* Jump to the next unique_file or NULL if that one was last */
1695 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1696 j
->unique_offset
= 0;
1697 if (!j
->unique_file
)
1698 j
->unique_file_lost
= true;
1701 if (j
->fields_file
== f
) {
1702 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1703 j
->fields_offset
= 0;
1704 if (!j
->fields_file
)
1705 j
->fields_file_lost
= true;
1708 journal_file_unlink_newest_by_boot_id(j
, f
);
1709 (void) journal_file_close(f
);
1711 j
->current_invalidate_counter
++;
1714 static int dirname_is_machine_id(const char *fn
) {
1715 sd_id128_t id
, machine
;
1719 /* Returns true if the specified directory name matches the local machine ID */
1721 r
= sd_id128_get_machine(&machine
);
1725 e
= strchr(fn
, '.');
1729 /* Looks like it has a namespace suffix. Verify that. */
1730 if (!log_namespace_name_valid(e
+ 1))
1733 k
= strndupa_safe(fn
, e
- fn
);
1734 r
= sd_id128_from_string(k
, &id
);
1736 r
= sd_id128_from_string(fn
, &id
);
1740 return sd_id128_equal(id
, machine
);
1743 static int dirname_has_namespace(const char *fn
, const char *namespace) {
1746 /* Returns true if the specified directory name matches the specified namespace */
1748 e
= strchr(fn
, '.');
1755 if (!streq(e
+ 1, namespace))
1758 k
= strndupa_safe(fn
, e
- fn
);
1759 return id128_is_valid(k
);
1765 return id128_is_valid(fn
);
1768 static bool dirent_is_journal_file(const struct dirent
*de
) {
1771 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1773 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1776 return endswith(de
->d_name
, ".journal") ||
1777 endswith(de
->d_name
, ".journal~");
1780 static bool dirent_is_journal_subdir(const struct dirent
*de
) {
1784 /* returns true if the specified directory entry looks like a directory that might contain journal
1785 * files we might be interested in, i.e. is either a 128-bit ID or a 128-bit ID suffixed by a
1788 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1791 e
= strchr(de
->d_name
, '.');
1793 return id128_is_valid(de
->d_name
); /* No namespace */
1795 n
= strndupa_safe(de
->d_name
, e
- de
->d_name
);
1796 if (!id128_is_valid(n
))
1799 return log_namespace_name_valid(e
+ 1);
1802 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1809 if (j
->toplevel_fd
< 0)
1812 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1813 * relative, by dropping the initial slash */
1814 d
= xopendirat(j
->toplevel_fd
, skip_slash(path
), 0);
1822 static Directory
* directory_free(Directory
*d
) {
1828 hashmap_remove_value(d
->journal
->directories_by_wd
, INT_TO_PTR(d
->wd
), d
) &&
1829 d
->journal
->inotify_fd
>= 0)
1830 (void) inotify_rm_watch(d
->journal
->inotify_fd
, d
->wd
);
1833 hashmap_remove_value(d
->journal
->directories_by_path
, d
->path
, d
);
1838 log_debug("Root directory %s removed.", d
->path
);
1840 log_debug("Directory %s removed.", d
->path
);
1848 DEFINE_TRIVIAL_CLEANUP_FUNC(Directory
*, directory_free
);
1850 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
1851 directories_by_path_hash_ops
,
1858 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
1859 directories_by_wd_hash_ops
,
1862 trivial_compare_func
,
1866 static int add_directory_impl(sd_journal
*j
, const char *path
, bool is_root
, Directory
**ret
) {
1867 _cleanup_(directory_freep
) Directory
*m
= NULL
;
1868 Directory
*existing
;
1875 existing
= hashmap_get(j
->directories_by_path
, path
);
1877 if (existing
->is_root
!= is_root
) {
1878 /* Don't 'downgrade' from root directory */
1887 m
= new(Directory
, 1);
1894 .path
= strdup(path
),
1901 r
= hashmap_ensure_put(&j
->directories_by_path
, &directories_by_path_hash_ops
, m
->path
, m
);
1905 j
->current_invalidate_counter
++;
1908 log_debug("Root directory %s added.", m
->path
);
1910 log_debug("Directory %s added.", m
->path
);
1916 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1918 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1923 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1924 if (dirent_is_journal_file(de
))
1925 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1927 if (m
->is_root
&& dirent_is_journal_subdir(de
))
1928 (void) add_directory(j
, m
->path
, de
->d_name
);
1933 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1936 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1943 /* Watch this directory if that's enabled and if it not being watched yet. */
1945 if (m
->wd
> 0) /* Already have a watch? */
1947 if (j
->inotify_fd
< 0) /* Not watching at all? */
1950 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1952 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1956 r
= hashmap_ensure_put(&j
->directories_by_wd
, &directories_by_wd_hash_ops
, INT_TO_PTR(m
->wd
), m
);
1959 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1961 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1962 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1968 static int add_directory(
1971 const char *dirname
) {
1973 _cleanup_free_
char *path
= NULL
;
1974 _cleanup_closedir_
DIR *d
= NULL
;
1981 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1982 * and reenumerates directory contents */
1984 path
= path_join(prefix
, dirname
);
1990 log_debug("Considering directory '%s'.", path
);
1992 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1993 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1994 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1998 (!(FLAGS_SET(j
->flags
, SD_JOURNAL_ALL_NAMESPACES
) ||
1999 dirname_has_namespace(dirname
, j
->namespace) > 0 ||
2000 (FLAGS_SET(j
->flags
, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE
) && dirname_has_namespace(dirname
, NULL
) > 0))))
2003 r
= directory_open(j
, path
, &d
);
2005 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
2009 r
= add_directory_impl(j
, path
, /* is_root = */ false, &m
);
2015 m
->last_seen_generation
= j
->generation
;
2017 directory_watch(j
, m
, dirfd(d
),
2018 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
2019 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
2022 if (!j
->no_new_files
)
2023 directory_enumerate(j
, m
, d
);
2025 check_network(j
, dirfd(d
));
2030 k
= journal_put_error(j
, r
, path
?: prefix
);
2037 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
2039 _cleanup_closedir_
DIR *d
= NULL
;
2045 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
2046 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
2047 * populate the set, as well as to update it later. */
2050 /* If there's a path specified, use it. */
2052 log_debug("Considering root directory '%s'.", p
);
2054 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
2055 !path_has_prefix(j
, p
, "/run"))
2059 p
= strjoina(j
->prefix
, p
);
2061 r
= directory_open(j
, p
, &d
);
2062 if (r
== -ENOENT
&& missing_ok
)
2065 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
2069 _cleanup_close_
int dfd
= -EBADF
;
2071 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
2072 * opendir() will take possession of the fd, and close it, which we don't want. */
2074 p
= "."; /* store this as "." in the directories hashmap */
2076 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
2082 d
= take_fdopendir(&dfd
);
2091 r
= add_directory_impl(j
, p
, /* is_root = */ true, &m
);
2097 directory_watch(j
, m
, dirfd(d
),
2098 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
2101 if (!j
->no_new_files
)
2102 directory_enumerate(j
, m
, d
);
2104 check_network(j
, dirfd(d
));
2109 k
= journal_put_error(j
, r
, p
);
2116 static int add_search_paths(sd_journal
*j
) {
2118 static const char search_paths
[] =
2119 "/run/log/journal\0"
2120 "/var/log/journal\0";
2124 /* We ignore most errors here, since the idea is to only open
2125 * what's actually accessible, and ignore the rest. */
2127 NULSTR_FOREACH(p
, search_paths
)
2128 (void) add_root_directory(j
, p
, true);
2130 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
2131 (void) add_root_directory(j
, "/var/log/journal/remote", true);
2136 static int add_current_paths(sd_journal
*j
) {
2140 assert(j
->no_new_files
);
2142 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
2143 * treat them as fatal. */
2145 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2146 _cleanup_free_
char *dir
= NULL
;
2149 r
= path_extract_directory(f
->path
, &dir
);
2153 r
= add_directory(j
, dir
, NULL
);
2161 static int allocate_inotify(sd_journal
*j
) {
2164 if (j
->inotify_fd
< 0) {
2165 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
2166 if (j
->inotify_fd
< 0)
2173 static sd_journal
*journal_new(int flags
, const char *path
, const char *namespace) {
2174 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2176 j
= new(sd_journal
, 1);
2181 .origin_id
= origin_id_query(),
2182 .toplevel_fd
= -EBADF
,
2183 .inotify_fd
= -EBADF
,
2185 .data_threshold
= DEFAULT_DATA_THRESHOLD
,
2195 if (flags
& SD_JOURNAL_OS_ROOT
)
2202 j
->namespace = strdup(namespace);
2207 j
->files
= ordered_hashmap_new(&path_hash_ops
);
2211 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
2212 j
->mmap
= mmap_cache_new();
2213 if (!j
->files_cache
|| !j
->mmap
)
2219 #define OPEN_ALLOWED_FLAGS \
2220 (SD_JOURNAL_LOCAL_ONLY | \
2221 SD_JOURNAL_RUNTIME_ONLY | \
2222 SD_JOURNAL_SYSTEM | \
2223 SD_JOURNAL_CURRENT_USER | \
2224 SD_JOURNAL_ALL_NAMESPACES | \
2225 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE | \
2226 SD_JOURNAL_ASSUME_IMMUTABLE)
2228 _public_
int sd_journal_open_namespace(sd_journal
**ret
, const char *namespace, int flags
) {
2229 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2232 assert_return(ret
, -EINVAL
);
2233 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
2235 j
= journal_new(flags
, NULL
, namespace);
2239 r
= add_search_paths(j
);
2247 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
2248 return sd_journal_open_namespace(ret
, NULL
, flags
);
2251 #define OPEN_CONTAINER_ALLOWED_FLAGS \
2252 (SD_JOURNAL_LOCAL_ONLY | \
2253 SD_JOURNAL_SYSTEM | \
2254 SD_JOURNAL_ASSUME_IMMUTABLE)
2256 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
2257 _cleanup_free_
char *root
= NULL
, *class = NULL
;
2258 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2262 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
2263 * combination with sd_journal_open_directory_fd(). */
2265 assert_return(machine
, -EINVAL
);
2266 assert_return(ret
, -EINVAL
);
2267 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
2268 assert_return(hostname_is_valid(machine
, 0), -EINVAL
);
2270 p
= strjoina("/run/systemd/machines/", machine
);
2271 r
= parse_env_file(NULL
, p
,
2281 if (!streq_ptr(class, "container"))
2284 j
= journal_new(flags
, root
, NULL
);
2288 r
= add_search_paths(j
);
2296 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
2297 (SD_JOURNAL_OS_ROOT | \
2298 SD_JOURNAL_SYSTEM | \
2299 SD_JOURNAL_CURRENT_USER | \
2300 SD_JOURNAL_ASSUME_IMMUTABLE)
2302 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
2303 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2306 assert_return(ret
, -EINVAL
);
2307 assert_return(path
, -EINVAL
);
2308 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
2310 j
= journal_new(flags
, path
, NULL
);
2314 if (flags
& SD_JOURNAL_OS_ROOT
)
2315 r
= add_search_paths(j
);
2317 r
= add_root_directory(j
, path
, false);
2325 #define OPEN_FILES_ALLOWED_FLAGS \
2326 (SD_JOURNAL_ASSUME_IMMUTABLE)
2328 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
2329 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2332 assert_return(ret
, -EINVAL
);
2333 assert_return((flags
& ~OPEN_FILES_ALLOWED_FLAGS
) == 0, -EINVAL
);
2335 j
= journal_new(flags
, NULL
, NULL
);
2339 STRV_FOREACH(path
, paths
) {
2340 r
= add_any_file(j
, -1, *path
);
2345 j
->no_new_files
= true;
2351 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
2352 (SD_JOURNAL_OS_ROOT | \
2353 SD_JOURNAL_SYSTEM | \
2354 SD_JOURNAL_CURRENT_USER | \
2355 SD_JOURNAL_TAKE_DIRECTORY_FD | \
2356 SD_JOURNAL_ASSUME_IMMUTABLE)
2358 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
2359 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2364 assert_return(ret
, -EINVAL
);
2365 assert_return(fd
>= 0, -EBADF
);
2366 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2368 if (fstat(fd
, &st
) < 0)
2371 if (!S_ISDIR(st
.st_mode
))
2374 take_fd
= FLAGS_SET(flags
, SD_JOURNAL_TAKE_DIRECTORY_FD
);
2375 j
= journal_new(flags
& ~SD_JOURNAL_TAKE_DIRECTORY_FD
, NULL
, NULL
);
2379 j
->toplevel_fd
= fd
;
2381 if (flags
& SD_JOURNAL_OS_ROOT
)
2382 r
= add_search_paths(j
);
2384 r
= add_root_directory(j
, NULL
, false);
2388 SET_FLAG(j
->flags
, SD_JOURNAL_TAKE_DIRECTORY_FD
, take_fd
);
2394 #define OPEN_FILES_FD_ALLOWED_FLAGS \
2395 (SD_JOURNAL_ASSUME_IMMUTABLE)
2397 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2399 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2402 assert_return(ret
, -EINVAL
);
2403 assert_return(n_fds
> 0, -EBADF
);
2404 assert_return((flags
& ~OPEN_FILES_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2406 j
= journal_new(flags
, NULL
, NULL
);
2410 for (unsigned i
= 0; i
< n_fds
; i
++) {
2418 if (fstat(fds
[i
], &st
) < 0) {
2423 r
= stat_verify_regular(&st
);
2427 r
= add_any_file(j
, fds
[i
], NULL
);
2432 j
->no_new_files
= true;
2433 j
->no_inotify
= true;
2439 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2441 ORDERED_HASHMAP_FOREACH(f
, j
->files
)
2442 f
->close_fd
= false;
2447 _public_
void sd_journal_close(sd_journal
*j
) {
2448 if (!j
|| journal_origin_changed(j
))
2451 journal_clear_newest_by_boot_id(j
);
2453 sd_journal_flush_matches(j
);
2455 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2456 iterated_cache_free(j
->files_cache
);
2458 hashmap_free(j
->directories_by_path
);
2459 hashmap_free(j
->directories_by_wd
);
2461 if (FLAGS_SET(j
->flags
, SD_JOURNAL_TAKE_DIRECTORY_FD
))
2462 safe_close(j
->toplevel_fd
);
2464 safe_close(j
->inotify_fd
);
2467 mmap_cache_stats_log_debug(j
->mmap
);
2468 mmap_cache_unref(j
->mmap
);
2471 hashmap_free_free(j
->errors
);
2473 set_free(j
->exclude_syslog_identifiers
);
2478 free(j
->unique_field
);
2479 free(j
->fields_buffer
);
2483 static int journal_file_read_tail_timestamp(sd_journal
*j
, JournalFile
*f
) {
2484 uint64_t offset
, mo
, rt
;
2494 /* Tries to read the timestamp of the most recently written entry. */
2496 if (FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
) && f
->newest_entry_offset
!= 0)
2497 return 0; /* We have already read the file, and we assume that the file is immutable. */
2499 if (f
->header
->state
== f
->newest_state
&&
2500 f
->header
->state
== STATE_ARCHIVED
&&
2501 f
->newest_entry_offset
!= 0)
2502 return 0; /* We have already read archived file. */
2504 if (JOURNAL_HEADER_CONTAINS(f
->header
, tail_entry_offset
)) {
2505 offset
= le64toh(READ_NOW(f
->header
->tail_entry_offset
));
2506 type
= OBJECT_ENTRY
;
2508 offset
= le64toh(READ_NOW(f
->header
->tail_object_offset
));
2509 type
= OBJECT_UNUSED
;
2512 return -ENODATA
; /* not a single object/entry, hence no tail timestamp */
2513 if (offset
== f
->newest_entry_offset
)
2514 return 0; /* No new entry is added after we read last time. */
2516 /* Move to the last object in the journal file, in the hope it is an entry (which it usually will
2517 * be). If we lack the "tail_entry_offset" field in the header, we specify the type as OBJECT_UNUSED
2518 * here, since we cannot be sure what the last object will be, and want no noisy logging if it isn't
2519 * an entry. We instead check after figuring out the pointer. */
2520 r
= journal_file_move_to_object(f
, type
, offset
, &o
);
2522 log_debug_errno(r
, "Failed to move to last object in journal file, ignoring: %m");
2526 if (o
&& o
->object
.type
== OBJECT_ENTRY
) {
2527 /* Yay, last object is an entry, let's use the data. */
2528 id
= o
->entry
.boot_id
;
2529 mo
= le64toh(o
->entry
.monotonic
);
2530 rt
= le64toh(o
->entry
.realtime
);
2532 /* So the object is not an entry or we couldn't access it? In that case, let's read the most
2533 * recent entry timestamps from the header. It's equally good. Unfortunately though, in old
2534 * versions of the journal the boot ID in the header doesn't have to match the monotonic
2535 * timestamp of the header. Let's check the header flag that indicates whether this strictly
2536 * matches first hence, before using the data. */
2538 if (JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f
->header
) && f
->header
->state
== STATE_ARCHIVED
) {
2539 mo
= le64toh(f
->header
->tail_entry_monotonic
);
2540 rt
= le64toh(f
->header
->tail_entry_realtime
);
2541 id
= f
->header
->tail_entry_boot_id
;
2542 offset
= UINT64_MAX
;
2544 /* Otherwise let's find the last entry manually (this possibly means traversing the
2545 * chain of entry arrays, till the end */
2546 r
= journal_file_next_entry(f
, 0, DIRECTION_UP
, &o
, offset
== 0 ? &offset
: NULL
);
2552 id
= o
->entry
.boot_id
;
2553 mo
= le64toh(o
->entry
.monotonic
);
2554 rt
= le64toh(o
->entry
.realtime
);
2558 if (mo
> rt
) /* monotonic clock is further ahead than realtime? that's weird, refuse to use the data */
2561 if (offset
== f
->newest_entry_offset
) {
2562 /* Cached data and the current one should be equivalent. */
2563 if (!sd_id128_equal(f
->newest_machine_id
, f
->header
->machine_id
) ||
2564 !sd_id128_equal(f
->newest_boot_id
, id
) ||
2565 f
->newest_monotonic_usec
!= mo
||
2566 f
->newest_realtime_usec
!= rt
)
2569 return 0; /* No new entry is added after we read last time. */
2572 if (!sd_id128_equal(f
->newest_boot_id
, id
))
2573 journal_file_unlink_newest_by_boot_id(j
, f
);
2575 f
->newest_boot_id
= id
;
2576 f
->newest_monotonic_usec
= mo
;
2577 f
->newest_realtime_usec
= rt
;
2578 f
->newest_machine_id
= f
->header
->machine_id
;
2579 f
->newest_entry_offset
= offset
;
2580 f
->newest_state
= f
->header
->state
;
2582 r
= journal_file_reshuffle_newest_by_boot_id(j
, f
);
2586 return 1; /* Updated. */
2589 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2594 assert_return(j
, -EINVAL
);
2595 assert_return(!journal_origin_changed(j
), -ECHILD
);
2597 f
= j
->current_file
;
2599 return -EADDRNOTAVAIL
;
2600 if (f
->current_offset
<= 0)
2601 return -EADDRNOTAVAIL
;
2603 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2607 uint64_t t
= le64toh(o
->entry
.realtime
);
2608 if (!VALID_REALTIME(t
))
2617 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2622 assert_return(j
, -EINVAL
);
2623 assert_return(!journal_origin_changed(j
), -ECHILD
);
2625 f
= j
->current_file
;
2627 return -EADDRNOTAVAIL
;
2628 if (f
->current_offset
<= 0)
2629 return -EADDRNOTAVAIL
;
2631 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2636 *ret_boot_id
= o
->entry
.boot_id
;
2640 r
= sd_id128_get_boot(&id
);
2644 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2648 uint64_t t
= le64toh(o
->entry
.monotonic
);
2649 if (!VALID_MONOTONIC(t
))
2658 _public_
int sd_journal_get_seqnum(
2660 uint64_t *ret_seqnum
,
2661 sd_id128_t
*ret_seqnum_id
) {
2667 assert_return(j
, -EINVAL
);
2668 assert_return(!journal_origin_changed(j
), -ECHILD
);
2670 f
= j
->current_file
;
2672 return -EADDRNOTAVAIL
;
2674 if (f
->current_offset
<= 0)
2675 return -EADDRNOTAVAIL
;
2677 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2682 *ret_seqnum_id
= f
->header
->seqnum_id
;
2684 *ret_seqnum
= le64toh(o
->entry
.seqnum
);
2689 static bool field_is_valid(const char *field
) {
2695 if (startswith(field
, "__"))
2698 for (const char *p
= field
; *p
; p
++) {
2703 if (*p
>= 'A' && *p
<= 'Z')
2706 if (ascii_isdigit(*p
))
2715 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2717 size_t field_length
;
2721 assert_return(j
, -EINVAL
);
2722 assert_return(!journal_origin_changed(j
), -ECHILD
);
2723 assert_return(field
, -EINVAL
);
2724 assert_return(data
, -EINVAL
);
2725 assert_return(size
, -EINVAL
);
2726 assert_return(field_is_valid(field
), -EINVAL
);
2728 f
= j
->current_file
;
2730 return -EADDRNOTAVAIL
;
2732 if (f
->current_offset
<= 0)
2733 return -EADDRNOTAVAIL
;
2735 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2739 field_length
= strlen(field
);
2741 uint64_t n
= journal_file_entry_n_items(f
, o
);
2742 for (uint64_t i
= 0; i
< n
; i
++) {
2747 p
= journal_file_entry_item_object_offset(f
, o
, i
);
2748 r
= journal_file_data_payload(f
, NULL
, p
, field
, field_length
, j
->data_threshold
, &d
, &l
);
2751 if (IN_SET(r
, -EADDRNOTAVAIL
, -EBADMSG
)) {
2752 log_debug_errno(r
, "Entry item %"PRIu64
" data object is bad, skipping over it: %m", i
);
2767 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2772 assert_return(j
, -EINVAL
);
2773 assert_return(!journal_origin_changed(j
), -ECHILD
);
2774 assert_return(data
, -EINVAL
);
2775 assert_return(size
, -EINVAL
);
2777 f
= j
->current_file
;
2779 return -EADDRNOTAVAIL
;
2781 if (f
->current_offset
<= 0)
2782 return -EADDRNOTAVAIL
;
2784 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2788 for (uint64_t n
= journal_file_entry_n_items(f
, o
); j
->current_field
< n
; j
->current_field
++) {
2793 p
= journal_file_entry_item_object_offset(f
, o
, j
->current_field
);
2794 r
= journal_file_data_payload(f
, NULL
, p
, NULL
, 0, j
->data_threshold
, &d
, &l
);
2795 if (IN_SET(r
, -EADDRNOTAVAIL
, -EBADMSG
)) {
2796 log_debug_errno(r
, "Entry item %"PRIu64
" data object is bad, skipping over it: %m", j
->current_field
);
2814 _public_
int sd_journal_enumerate_available_data(sd_journal
*j
, const void **data
, size_t *size
) {
2818 r
= sd_journal_enumerate_data(j
, data
, size
);
2821 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r
))
2823 j
->current_field
++; /* Try with the next field */
2827 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2828 if (!j
|| journal_origin_changed(j
))
2831 j
->current_field
= 0;
2834 static int reiterate_all_paths(sd_journal
*j
) {
2837 if (j
->no_new_files
)
2838 return add_current_paths(j
);
2840 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2841 return add_search_paths(j
);
2843 if (j
->toplevel_fd
>= 0)
2844 return add_root_directory(j
, NULL
, false);
2847 return add_root_directory(j
, j
->path
, true);
2849 return add_search_paths(j
);
2852 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2855 assert_return(j
, -EINVAL
);
2856 assert_return(!journal_origin_changed(j
), -ECHILD
);
2857 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
2860 return -EMEDIUMTYPE
;
2862 if (j
->inotify_fd
>= 0)
2863 return j
->inotify_fd
;
2865 r
= allocate_inotify(j
);
2869 log_debug("Reiterating files to get inotify watches established.");
2871 /* Iterate through all dirs again, to add them to the inotify */
2872 r
= reiterate_all_paths(j
);
2876 return j
->inotify_fd
;
2879 _public_
int sd_journal_get_events(sd_journal
*j
) {
2882 assert_return(j
, -EINVAL
);
2883 assert_return(!journal_origin_changed(j
), -ECHILD
);
2884 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
2886 fd
= sd_journal_get_fd(j
);
2893 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2896 assert_return(j
, -EINVAL
);
2897 assert_return(!journal_origin_changed(j
), -ECHILD
);
2898 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
2899 assert_return(timeout_usec
, -EINVAL
);
2901 fd
= sd_journal_get_fd(j
);
2905 if (!j
->on_network
) {
2906 *timeout_usec
= UINT64_MAX
;
2910 /* If we are on the network we need to regularly check for
2911 * changes manually */
2913 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2917 static void process_q_overflow(sd_journal
*j
) {
2923 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2924 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2925 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2926 * are subject for unloading. */
2928 log_debug("Inotify queue overrun, reiterating everything.");
2931 (void) reiterate_all_paths(j
);
2933 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2935 if (f
->last_seen_generation
== j
->generation
)
2938 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2939 remove_file_real(j
, f
);
2942 HASHMAP_FOREACH(m
, j
->directories_by_path
) {
2944 if (m
->last_seen_generation
== j
->generation
)
2947 if (m
->is_root
) /* Never GC root directories */
2950 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2954 log_debug("Reiteration complete.");
2957 static void process_inotify_event(sd_journal
*j
, const struct inotify_event
*e
) {
2963 if (e
->mask
& IN_Q_OVERFLOW
) {
2964 process_q_overflow(j
);
2968 /* Is this a subdirectory we watch? */
2969 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2971 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2972 (endswith(e
->name
, ".journal") ||
2973 endswith(e
->name
, ".journal~"))) {
2975 /* Event for a journal file */
2977 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2978 (void) add_file_by_name(j
, d
->path
, e
->name
);
2979 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2980 (void) remove_file_by_name(j
, d
->path
, e
->name
);
2982 } else if (!d
->is_root
&& e
->len
== 0) {
2984 /* Event for a subdirectory */
2986 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2989 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2991 /* Event for root directory */
2993 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2994 (void) add_directory(j
, d
->path
, e
->name
);
3000 if (e
->mask
& IN_IGNORED
)
3003 log_debug("Unexpected inotify event.");
3006 static int determine_change(sd_journal
*j
) {
3011 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
3012 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
3014 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
3017 _public_
int sd_journal_process(sd_journal
*j
) {
3018 bool got_something
= false;
3020 assert_return(j
, -EINVAL
);
3021 assert_return(!journal_origin_changed(j
), -ECHILD
);
3023 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
3026 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
3028 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
3029 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
3032 union inotify_event_buffer buffer
;
3035 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
3037 if (ERRNO_IS_TRANSIENT(errno
))
3038 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
3043 got_something
= true;
3045 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
3046 process_inotify_event(j
, e
);
3050 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
3054 assert_return(j
, -EINVAL
);
3055 assert_return(!journal_origin_changed(j
), -ECHILD
);
3056 assert_return(!FLAGS_SET(j
->flags
, SD_JOURNAL_ASSUME_IMMUTABLE
), -EUNATCH
);
3058 if (j
->inotify_fd
< 0) {
3061 /* This is the first invocation, hence create the inotify watch */
3062 r
= sd_journal_get_fd(j
);
3066 /* Server might have done some vacuuming while we weren't watching. Get rid of the deleted
3067 * files now so they don't stay around indefinitely. */
3068 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3069 r
= journal_file_fstat(f
);
3071 remove_file_real(j
, f
);
3073 log_debug_errno(r
, "Failed to fstat() journal file '%s', ignoring: %m", f
->path
);
3076 /* The journal might have changed since the context object was created and we weren't
3077 * watching before, hence don't wait for anything, and return immediately. */
3078 return determine_change(j
);
3081 r
= sd_journal_get_timeout(j
, &t
);
3085 if (t
!= UINT64_MAX
) {
3086 t
= usec_sub_unsigned(t
, now(CLOCK_MONOTONIC
));
3088 if (timeout_usec
== UINT64_MAX
|| timeout_usec
> t
)
3093 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
3094 } while (r
== -EINTR
);
3099 return sd_journal_process(j
);
3102 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
3105 uint64_t fmin
= 0, tmax
= 0;
3108 assert_return(j
, -EINVAL
);
3109 assert_return(!journal_origin_changed(j
), -ECHILD
);
3110 assert_return(from
|| to
, -EINVAL
);
3111 assert_return(from
!= to
, -EINVAL
);
3113 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3116 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
3129 fmin
= MIN(fr
, fmin
);
3130 tmax
= MAX(t
, tmax
);
3139 return first
? 0 : 1;
3142 _public_
int sd_journal_get_cutoff_monotonic_usec(
3148 uint64_t from
= UINT64_MAX
, to
= UINT64_MAX
;
3153 assert_return(j
, -EINVAL
);
3154 assert_return(!journal_origin_changed(j
), -ECHILD
);
3155 assert_return(ret_from
!= ret_to
, -EINVAL
);
3157 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3160 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &ff
, &tt
);
3169 from
= MIN(ff
, from
);
3186 void journal_print_header(sd_journal
*j
) {
3188 bool newline
= false;
3192 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3198 journal_file_print_header(f
);
3202 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *ret
) {
3206 assert_return(j
, -EINVAL
);
3207 assert_return(!journal_origin_changed(j
), -ECHILD
);
3208 assert_return(ret
, -EINVAL
);
3210 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
3214 if (fstat(f
->fd
, &st
) < 0)
3217 b
= (uint64_t) st
.st_blocks
;
3218 if (b
> UINT64_MAX
/ 512)
3222 if (sum
> UINT64_MAX
- b
)
3231 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
3234 assert_return(j
, -EINVAL
);
3235 assert_return(!journal_origin_changed(j
), -ECHILD
);
3237 if (!field_is_valid(field
))
3240 r
= free_and_strdup(&j
->unique_field
, field
);
3244 j
->unique_file
= NULL
;
3245 j
->unique_offset
= 0;
3246 j
->unique_file_lost
= false;
3251 _public_
int sd_journal_enumerate_unique(
3253 const void **ret_data
,
3258 assert_return(j
, -EINVAL
);
3259 assert_return(!journal_origin_changed(j
), -ECHILD
);
3260 assert_return(j
->unique_field
, -EINVAL
);
3262 k
= strlen(j
->unique_field
);
3264 if (!j
->unique_file
) {
3265 if (j
->unique_file_lost
)
3268 j
->unique_file
= ordered_hashmap_first(j
->files
);
3269 if (!j
->unique_file
)
3272 j
->unique_offset
= 0;
3283 /* Proceed to next data object in the field's linked list */
3284 if (j
->unique_offset
== 0) {
3285 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
3289 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
3291 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
3295 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
3298 /* We reached the end of the list? Then start again, with the next file */
3299 if (j
->unique_offset
== 0) {
3300 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
3301 if (!j
->unique_file
)
3307 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
3311 /* Let's pin the data object, so we can look at it at the same time as one on another file. */
3312 r
= journal_file_pin_object(j
->unique_file
, o
);
3316 r
= journal_file_data_payload(j
->unique_file
, o
, j
->unique_offset
, NULL
, 0,
3317 j
->data_threshold
, &odata
, &ol
);
3321 /* Check if we have at least the field name and "=". */
3323 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3324 "%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
3325 j
->unique_file
->path
,
3326 j
->unique_offset
, ol
, k
+ 1);
3328 if (memcmp(odata
, j
->unique_field
, k
) != 0 || ((const char*) odata
)[k
] != '=')
3329 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3330 "%s:offset " OFSfmt
": object does not start with \"%s=\"",
3331 j
->unique_file
->path
,
3335 /* OK, now let's see if we already returned this data object by checking if it exists in the
3336 * earlier traversed files. */
3338 ORDERED_HASHMAP_FOREACH(of
, j
->files
) {
3339 if (of
== j
->unique_file
)
3342 /* Skip this file it didn't have any fields indexed */
3343 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3346 /* We can reuse the hash from our current file only on old-style journal files
3347 * without keyed hashes. On new-style files we have to calculate the hash anew, to
3348 * take the per-file hash seed into consideration. */
3349 if (!JOURNAL_HEADER_KEYED_HASH(j
->unique_file
->header
) && !JOURNAL_HEADER_KEYED_HASH(of
->header
))
3350 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
3352 r
= journal_file_find_data_object(of
, odata
, ol
, NULL
, NULL
);
3371 _public_
int sd_journal_enumerate_available_unique(sd_journal
*j
, const void **data
, size_t *size
) {
3375 r
= sd_journal_enumerate_unique(j
, data
, size
);
3378 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r
))
3380 /* Try with the next field. sd_journal_enumerate_unique() modifies state, so on the next try
3381 * we will access the next field. */
3385 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
3386 if (!j
|| journal_origin_changed(j
))
3389 j
->unique_file
= NULL
;
3390 j
->unique_offset
= 0;
3391 j
->unique_file_lost
= false;
3394 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
3397 assert_return(j
, -EINVAL
);
3398 assert_return(!journal_origin_changed(j
), -ECHILD
);
3399 assert_return(field
, -EINVAL
);
3401 if (!j
->fields_file
) {
3402 if (j
->fields_file_lost
)
3405 j
->fields_file
= ordered_hashmap_first(j
->files
);
3406 if (!j
->fields_file
)
3409 j
->fields_hash_table_index
= 0;
3410 j
->fields_offset
= 0;
3414 JournalFile
*f
, *of
;
3422 if (j
->fields_offset
== 0) {
3425 /* We are not yet positioned at any field. Let's pick the first one */
3426 r
= journal_file_map_field_hash_table(f
);
3430 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
3432 if (j
->fields_hash_table_index
>= m
) {
3433 /* Reached the end of the hash table, go to the next file. */
3438 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
3440 if (j
->fields_offset
!= 0)
3443 /* Empty hash table bucket, go to next one */
3444 j
->fields_hash_table_index
++;
3448 /* Proceed with next file */
3449 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
3450 if (!j
->fields_file
) {
3455 j
->fields_offset
= 0;
3456 j
->fields_hash_table_index
= 0;
3461 /* We are already positioned at a field. If so, let's figure out the next field from it */
3463 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
3467 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
3468 if (j
->fields_offset
== 0) {
3469 /* Reached the end of the hash table chain */
3470 j
->fields_hash_table_index
++;
3475 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
3476 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
3480 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
3481 if (o
->object
.type
!= OBJECT_FIELD
)
3482 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3483 "%s:offset " OFSfmt
": object has type %i, expected %i",
3484 f
->path
, j
->fields_offset
,
3485 o
->object
.type
, OBJECT_FIELD
);
3487 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
3489 /* Let's see if we already returned this field name before. */
3491 ORDERED_HASHMAP_FOREACH(of
, j
->files
) {
3495 /* Skip this file it didn't have any fields indexed */
3496 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3499 if (!JOURNAL_HEADER_KEYED_HASH(f
->header
) && !JOURNAL_HEADER_KEYED_HASH(of
->header
))
3500 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
,
3501 le64toh(o
->field
.hash
), NULL
, NULL
);
3503 r
= journal_file_find_field_object(of
, o
->field
.payload
, sz
, NULL
, NULL
);
3515 /* Check if this is really a valid string containing no NUL byte */
3516 if (memchr(o
->field
.payload
, 0, sz
))
3519 if (j
->data_threshold
> 0 && sz
> j
->data_threshold
)
3520 sz
= j
->data_threshold
;
3522 if (!GREEDY_REALLOC(j
->fields_buffer
, sz
+ 1))
3525 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3526 j
->fields_buffer
[sz
] = 0;
3528 if (!field_is_valid(j
->fields_buffer
))
3531 *field
= j
->fields_buffer
;
3536 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3537 if (!j
|| journal_origin_changed(j
))
3540 j
->fields_file
= NULL
;
3541 j
->fields_hash_table_index
= 0;
3542 j
->fields_offset
= 0;
3543 j
->fields_file_lost
= false;
3546 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3547 assert_return(j
, -EINVAL
);
3548 assert_return(!journal_origin_changed(j
), -ECHILD
);
3550 return !j
->on_network
;
3553 static char *lookup_field(const char *field
, void *userdata
) {
3554 sd_journal
*j
= ASSERT_PTR(userdata
);
3561 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3563 size
> REPLACE_VAR_MAX
)
3564 return strdup(field
);
3566 d
= strlen(field
) + 1;
3568 return strndup((const char*) data
+ d
, size
- d
);
3571 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3575 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3579 assert_return(j
, -EINVAL
);
3580 assert_return(!journal_origin_changed(j
), -ECHILD
);
3581 assert_return(ret
, -EINVAL
);
3583 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3587 cid
= strndup((const char*) data
+ 11, size
- 11);
3591 r
= sd_id128_from_string(cid
, &id
);
3595 r
= catalog_get(secure_getenv("SYSTEMD_CATALOG") ?: CATALOG_DATABASE
, id
, &text
);
3599 t
= replace_var(text
, lookup_field
, j
);
3607 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3608 assert_return(ret
, -EINVAL
);
3610 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3613 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3614 assert_return(j
, -EINVAL
);
3615 assert_return(!journal_origin_changed(j
), -ECHILD
);
3617 j
->data_threshold
= sz
;
3621 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3622 assert_return(j
, -EINVAL
);
3623 assert_return(!journal_origin_changed(j
), -ECHILD
);
3624 assert_return(sz
, -EINVAL
);
3626 *sz
= j
->data_threshold
;
3630 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3631 assert_return(j
, -EINVAL
);
3633 return j
->has_runtime_files
;
3636 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3637 assert_return(j
, -EINVAL
);
3639 return j
->has_persistent_files
;