1 /* SPDX-License-Identifier: LGPL-2.1+ */
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"
29 #include "journal-def.h"
30 #include "journal-file.h"
31 #include "journal-internal.h"
35 #include "nulstr-util.h"
36 #include "path-util.h"
37 #include "process-util.h"
38 #include "replace-var.h"
39 #include "stat-util.h"
40 #include "stdio-util.h"
41 #include "string-util.h"
44 #define JOURNAL_FILES_MAX 7168
46 #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
48 #define REPLACE_VAR_MAX 256
50 #define DEFAULT_DATA_THRESHOLD (64*1024)
52 static void remove_file_real(sd_journal
*j
, JournalFile
*f
);
54 static bool journal_pid_changed(sd_journal
*j
) {
57 /* We don't support people creating a journal object and
58 * keeping it around over a fork(). Let's complain. */
60 return j
->original_pid
!= getpid_cached();
63 static int journal_put_error(sd_journal
*j
, int r
, const char *path
) {
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. */
81 k
= hashmap_ensure_allocated(&j
->errors
, NULL
);
92 k
= hashmap_put(j
->errors
, INT_TO_PTR(r
), copy
);
105 static void detach_location(sd_journal
*j
) {
111 j
->current_file
= NULL
;
112 j
->current_field
= 0;
114 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
)
115 journal_file_reset_location(f
);
118 static void reset_location(sd_journal
*j
) {
122 zero(j
->current_location
);
125 static void init_location(Location
*l
, LocationType type
, JournalFile
*f
, Object
*o
) {
127 assert(IN_SET(type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
129 assert(o
->object
.type
== OBJECT_ENTRY
);
132 l
->seqnum
= le64toh(o
->entry
.seqnum
);
133 l
->seqnum_id
= f
->header
->seqnum_id
;
134 l
->realtime
= le64toh(o
->entry
.realtime
);
135 l
->monotonic
= le64toh(o
->entry
.monotonic
);
136 l
->boot_id
= o
->entry
.boot_id
;
137 l
->xor_hash
= le64toh(o
->entry
.xor_hash
);
139 l
->seqnum_set
= l
->realtime_set
= l
->monotonic_set
= l
->xor_hash_set
= true;
142 static void set_location(sd_journal
*j
, JournalFile
*f
, Object
*o
) {
147 init_location(&j
->current_location
, LOCATION_DISCRETE
, f
, o
);
150 j
->current_field
= 0;
152 /* Let f know its candidate entry was picked. */
153 assert(f
->location_type
== LOCATION_SEEK
);
154 f
->location_type
= LOCATION_DISCRETE
;
157 static int match_is_valid(const void *data
, size_t size
) {
165 if (startswith(data
, "__"))
169 for (p
= b
; p
< b
+ size
; p
++) {
177 if (*p
>= 'A' && *p
<= 'Z')
180 if (*p
>= '0' && *p
<= '9')
189 static bool same_field(const void *_a
, size_t s
, const void *_b
, size_t t
) {
190 const uint8_t *a
= _a
, *b
= _b
;
193 for (j
= 0; j
< s
&& j
< t
; j
++) {
202 assert_not_reached("\"=\" not found");
205 static Match
*match_new(Match
*p
, MatchType t
) {
216 LIST_PREPEND(matches
, p
->matches
, m
);
222 static void match_free(Match
*m
) {
226 match_free(m
->matches
);
229 LIST_REMOVE(matches
, m
->parent
->matches
, m
);
235 static void match_free_if_empty(Match
*m
) {
236 if (!m
|| m
->matches
)
242 _public_
int sd_journal_add_match(sd_journal
*j
, const void *data
, size_t size
) {
243 Match
*l3
, *l4
, *add_here
= NULL
, *m
;
246 assert_return(j
, -EINVAL
);
247 assert_return(!journal_pid_changed(j
), -ECHILD
);
248 assert_return(data
, -EINVAL
);
253 assert_return(match_is_valid(data
, size
), -EINVAL
);
259 * level 4: concrete matches */
262 j
->level0
= match_new(NULL
, MATCH_AND_TERM
);
268 j
->level1
= match_new(j
->level0
, MATCH_OR_TERM
);
274 j
->level2
= match_new(j
->level1
, MATCH_AND_TERM
);
279 assert(j
->level0
->type
== MATCH_AND_TERM
);
280 assert(j
->level1
->type
== MATCH_OR_TERM
);
281 assert(j
->level2
->type
== MATCH_AND_TERM
);
283 le_hash
= htole64(hash64(data
, size
));
285 LIST_FOREACH(matches
, l3
, j
->level2
->matches
) {
286 assert(l3
->type
== MATCH_OR_TERM
);
288 LIST_FOREACH(matches
, l4
, l3
->matches
) {
289 assert(l4
->type
== MATCH_DISCRETE
);
291 /* Exactly the same match already? Then ignore
293 if (l4
->le_hash
== le_hash
&&
295 memcmp(l4
->data
, data
, size
) == 0)
298 /* Same field? Then let's add this to this OR term */
299 if (same_field(data
, size
, l4
->data
, l4
->size
)) {
310 add_here
= match_new(j
->level2
, MATCH_OR_TERM
);
315 m
= match_new(add_here
, MATCH_DISCRETE
);
319 m
->le_hash
= le_hash
;
321 m
->data
= memdup(data
, size
);
330 match_free_if_empty(add_here
);
331 match_free_if_empty(j
->level2
);
332 match_free_if_empty(j
->level1
);
333 match_free_if_empty(j
->level0
);
338 _public_
int sd_journal_add_conjunction(sd_journal
*j
) {
339 assert_return(j
, -EINVAL
);
340 assert_return(!journal_pid_changed(j
), -ECHILD
);
348 if (!j
->level1
->matches
)
357 _public_
int sd_journal_add_disjunction(sd_journal
*j
) {
358 assert_return(j
, -EINVAL
);
359 assert_return(!journal_pid_changed(j
), -ECHILD
);
370 if (!j
->level2
->matches
)
377 static char *match_make_string(Match
*m
) {
380 bool enclose
= false;
383 return strdup("none");
385 if (m
->type
== MATCH_DISCRETE
)
386 return cescape_length(m
->data
, m
->size
);
388 LIST_FOREACH(matches
, i
, m
->matches
) {
391 t
= match_make_string(i
);
396 k
= strjoin(p
, m
->type
== MATCH_OR_TERM
? " OR " : " AND ", t
);
411 r
= strjoin("(", p
, ")");
419 char *journal_make_match_string(sd_journal
*j
) {
422 return match_make_string(j
->level0
);
425 _public_
void sd_journal_flush_matches(sd_journal
*j
) {
430 match_free(j
->level0
);
432 j
->level0
= j
->level1
= j
->level2
= NULL
;
437 _pure_
static int compare_with_location(JournalFile
*f
, Location
*l
) {
442 assert(f
->location_type
== LOCATION_SEEK
);
443 assert(IN_SET(l
->type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
445 if (l
->monotonic_set
&&
446 sd_id128_equal(f
->current_boot_id
, l
->boot_id
) &&
448 f
->current_realtime
== l
->realtime
&&
450 f
->current_xor_hash
== l
->xor_hash
)
454 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
)) {
456 r
= CMP(f
->current_seqnum
, l
->seqnum
);
461 if (l
->monotonic_set
&&
462 sd_id128_equal(f
->current_boot_id
, l
->boot_id
)) {
464 r
= CMP(f
->current_monotonic
, l
->monotonic
);
469 if (l
->realtime_set
) {
471 r
= CMP(f
->current_realtime
, l
->realtime
);
476 if (l
->xor_hash_set
) {
478 r
= CMP(f
->current_xor_hash
, l
->xor_hash
);
486 static int next_for_match(
490 uint64_t after_offset
,
491 direction_t direction
,
503 if (m
->type
== MATCH_DISCRETE
) {
506 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
510 return journal_file_move_to_entry_by_offset_for_data(f
, dp
, after_offset
, direction
, ret
, offset
);
512 } else if (m
->type
== MATCH_OR_TERM
) {
515 /* Find the earliest match beyond after_offset */
517 LIST_FOREACH(matches
, i
, m
->matches
) {
520 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
524 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
532 } else if (m
->type
== MATCH_AND_TERM
) {
533 Match
*i
, *last_moved
;
535 /* Always jump to the next matching entry and repeat
536 * this until we find an offset that matches for all
542 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
546 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
547 last_moved
= m
->matches
;
549 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
552 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
556 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
557 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
566 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
578 static int find_location_for_match(
582 direction_t direction
,
592 if (m
->type
== MATCH_DISCRETE
) {
595 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
599 /* FIXME: missing: find by monotonic */
601 if (j
->current_location
.type
== LOCATION_HEAD
)
602 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_DOWN
, ret
, offset
);
603 if (j
->current_location
.type
== LOCATION_TAIL
)
604 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_UP
, ret
, offset
);
605 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
606 return journal_file_move_to_entry_by_seqnum_for_data(f
, dp
, j
->current_location
.seqnum
, direction
, ret
, offset
);
607 if (j
->current_location
.monotonic_set
) {
608 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, dp
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
612 if (j
->current_location
.realtime_set
)
613 return journal_file_move_to_entry_by_realtime_for_data(f
, dp
, j
->current_location
.realtime
, direction
, ret
, offset
);
615 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, direction
, ret
, offset
);
617 } else if (m
->type
== MATCH_OR_TERM
) {
622 /* Find the earliest match */
624 LIST_FOREACH(matches
, i
, m
->matches
) {
627 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
631 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
639 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
654 assert(m
->type
== MATCH_AND_TERM
);
656 /* First jump to the last match, and then find the
657 * next one where all matches match */
662 LIST_FOREACH(matches
, i
, m
->matches
) {
665 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
669 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
673 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
677 static int find_location_with_matches(
680 direction_t direction
,
692 /* No matches is simple */
694 if (j
->current_location
.type
== LOCATION_HEAD
)
695 return journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
);
696 if (j
->current_location
.type
== LOCATION_TAIL
)
697 return journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
);
698 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
699 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
700 if (j
->current_location
.monotonic_set
) {
701 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
705 if (j
->current_location
.realtime_set
)
706 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
708 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
710 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
713 static int next_with_matches(
716 direction_t direction
,
725 /* No matches is easy. We simple advance the file
728 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
730 /* If we have a match then we look for the next matching entry
731 * with an offset at least one step larger */
732 return next_for_match(j
, j
->level0
, f
,
733 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
734 : f
->current_offset
- 1,
735 direction
, ret
, offset
);
738 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
740 uint64_t cp
, n_entries
;
746 n_entries
= le64toh(f
->header
->n_entries
);
748 /* If we hit EOF before, we don't need to look into this file again
749 * unless direction changed or new entries appeared. */
750 if (f
->last_direction
== direction
&& f
->location_type
== LOCATION_TAIL
&&
751 n_entries
== f
->last_n_entries
)
754 f
->last_n_entries
= n_entries
;
756 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
757 /* LOCATION_SEEK here means we did the work in a previous
758 * iteration and the current location already points to a
759 * candidate entry. */
760 if (f
->location_type
!= LOCATION_SEEK
) {
761 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
765 journal_file_save_location(f
, c
, cp
);
768 f
->last_direction
= direction
;
770 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
774 journal_file_save_location(f
, c
, cp
);
777 /* OK, we found the spot, now let's advance until an entry
778 * that is actually different from what we were previously
779 * looking at. This is necessary to handle entries which exist
780 * in two (or more) journal files, and which shall all be
781 * suppressed but one. */
786 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
789 k
= compare_with_location(f
, &j
->current_location
);
791 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
798 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
802 journal_file_save_location(f
, c
, cp
);
806 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
807 JournalFile
*new_file
= NULL
;
813 assert_return(j
, -EINVAL
);
814 assert_return(!journal_pid_changed(j
), -ECHILD
);
816 r
= iterated_cache_get(j
->files_cache
, NULL
, &files
, &n_files
);
820 for (i
= 0; i
< n_files
; i
++) {
821 JournalFile
*f
= (JournalFile
*)files
[i
];
824 r
= next_beyond_location(j
, f
, direction
);
826 log_debug_errno(r
, "Can't iterate through %s, ignoring: %m", f
->path
);
827 remove_file_real(j
, f
);
830 f
->location_type
= LOCATION_TAIL
;
839 k
= journal_file_compare_locations(f
, new_file
);
841 found
= direction
== DIRECTION_DOWN
? k
< 0 : k
> 0;
851 r
= journal_file_move_to_object(new_file
, OBJECT_ENTRY
, new_file
->current_offset
, &o
);
855 set_location(j
, new_file
, o
);
860 _public_
int sd_journal_next(sd_journal
*j
) {
861 return real_journal_next(j
, DIRECTION_DOWN
);
864 _public_
int sd_journal_previous(sd_journal
*j
) {
865 return real_journal_next(j
, DIRECTION_UP
);
868 static int real_journal_next_skip(sd_journal
*j
, direction_t direction
, uint64_t skip
) {
871 assert_return(j
, -EINVAL
);
872 assert_return(!journal_pid_changed(j
), -ECHILD
);
875 /* If this is not a discrete skip, then at least
876 * resolve the current location */
877 if (j
->current_location
.type
!= LOCATION_DISCRETE
) {
878 r
= real_journal_next(j
, direction
);
887 r
= real_journal_next(j
, direction
);
901 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
902 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
905 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
906 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
909 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
912 char bid
[33], sid
[33];
914 assert_return(j
, -EINVAL
);
915 assert_return(!journal_pid_changed(j
), -ECHILD
);
916 assert_return(cursor
, -EINVAL
);
918 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
919 return -EADDRNOTAVAIL
;
921 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
925 sd_id128_to_string(j
->current_file
->header
->seqnum_id
, sid
);
926 sd_id128_to_string(o
->entry
.boot_id
, bid
);
929 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
930 sid
, le64toh(o
->entry
.seqnum
),
931 bid
, le64toh(o
->entry
.monotonic
),
932 le64toh(o
->entry
.realtime
),
933 le64toh(o
->entry
.xor_hash
)) < 0)
939 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
940 const char *word
, *state
;
942 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
944 seqnum_id_set
= false,
947 monotonic_set
= false,
948 realtime_set
= false,
949 xor_hash_set
= false;
950 sd_id128_t seqnum_id
, boot_id
;
952 assert_return(j
, -EINVAL
);
953 assert_return(!journal_pid_changed(j
), -ECHILD
);
954 assert_return(!isempty(cursor
), -EINVAL
);
956 FOREACH_WORD_SEPARATOR(word
, l
, cursor
, ";", state
) {
960 if (l
< 2 || word
[1] != '=')
963 item
= strndup(word
, l
);
970 seqnum_id_set
= true;
971 k
= sd_id128_from_string(item
+2, &seqnum_id
);
976 if (sscanf(item
+2, "%llx", &seqnum
) != 1)
982 k
= sd_id128_from_string(item
+2, &boot_id
);
986 monotonic_set
= true;
987 if (sscanf(item
+2, "%llx", &monotonic
) != 1)
993 if (sscanf(item
+2, "%llx", &realtime
) != 1)
999 if (sscanf(item
+2, "%llx", &xor_hash
) != 1)
1010 if ((!seqnum_set
|| !seqnum_id_set
) &&
1011 (!monotonic_set
|| !boot_id_set
) &&
1017 j
->current_location
.type
= LOCATION_SEEK
;
1020 j
->current_location
.realtime
= (uint64_t) realtime
;
1021 j
->current_location
.realtime_set
= true;
1024 if (seqnum_set
&& seqnum_id_set
) {
1025 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1026 j
->current_location
.seqnum_id
= seqnum_id
;
1027 j
->current_location
.seqnum_set
= true;
1030 if (monotonic_set
&& boot_id_set
) {
1031 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1032 j
->current_location
.boot_id
= boot_id
;
1033 j
->current_location
.monotonic_set
= true;
1037 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1038 j
->current_location
.xor_hash_set
= true;
1044 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1048 assert_return(j
, -EINVAL
);
1049 assert_return(!journal_pid_changed(j
), -ECHILD
);
1050 assert_return(!isempty(cursor
), -EINVAL
);
1052 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1053 return -EADDRNOTAVAIL
;
1055 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1060 _cleanup_free_
char *item
= NULL
;
1061 unsigned long long ll
;
1065 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1072 if (strlen(item
) < 2 || item
[1] != '=')
1078 k
= sd_id128_from_string(item
+2, &id
);
1081 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1086 if (sscanf(item
+2, "%llx", &ll
) != 1)
1088 if (ll
!= le64toh(o
->entry
.seqnum
))
1093 k
= sd_id128_from_string(item
+2, &id
);
1096 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1101 if (sscanf(item
+2, "%llx", &ll
) != 1)
1103 if (ll
!= le64toh(o
->entry
.monotonic
))
1108 if (sscanf(item
+2, "%llx", &ll
) != 1)
1110 if (ll
!= le64toh(o
->entry
.realtime
))
1115 if (sscanf(item
+2, "%llx", &ll
) != 1)
1117 if (ll
!= le64toh(o
->entry
.xor_hash
))
1126 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t usec
) {
1127 assert_return(j
, -EINVAL
);
1128 assert_return(!journal_pid_changed(j
), -ECHILD
);
1131 j
->current_location
.type
= LOCATION_SEEK
;
1132 j
->current_location
.boot_id
= boot_id
;
1133 j
->current_location
.monotonic
= usec
;
1134 j
->current_location
.monotonic_set
= true;
1139 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1140 assert_return(j
, -EINVAL
);
1141 assert_return(!journal_pid_changed(j
), -ECHILD
);
1144 j
->current_location
.type
= LOCATION_SEEK
;
1145 j
->current_location
.realtime
= usec
;
1146 j
->current_location
.realtime_set
= true;
1151 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1152 assert_return(j
, -EINVAL
);
1153 assert_return(!journal_pid_changed(j
), -ECHILD
);
1156 j
->current_location
.type
= LOCATION_HEAD
;
1161 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1162 assert_return(j
, -EINVAL
);
1163 assert_return(!journal_pid_changed(j
), -ECHILD
);
1166 j
->current_location
.type
= LOCATION_TAIL
;
1171 static void check_network(sd_journal
*j
, int fd
) {
1177 j
->on_network
= fd_is_network_fs(fd
);
1180 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1181 const char *full
, *tilded
, *atted
;
1183 full
= strjoina(prefix
, ".journal");
1184 tilded
= strjoina(full
, "~");
1185 atted
= strjoina(prefix
, "@");
1187 return STR_IN_SET(filename
, full
, tilded
) ||
1188 startswith(filename
, atted
);
1191 static bool file_type_wanted(int flags
, const char *filename
) {
1194 if (!endswith(filename
, ".journal") && !endswith(filename
, ".journal~"))
1197 /* no flags set → every type is OK */
1198 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1201 if (flags
& SD_JOURNAL_SYSTEM
&& file_has_type_prefix("system", filename
))
1204 if (flags
& SD_JOURNAL_CURRENT_USER
) {
1205 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1207 xsprintf(prefix
, "user-"UID_FMT
, getuid());
1209 if (file_has_type_prefix(prefix
, filename
))
1216 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1221 if (j
->toplevel_fd
>= 0)
1224 return path_startswith(path
, prefix
);
1227 static void track_file_disposition(sd_journal
*j
, JournalFile
*f
) {
1231 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1232 j
->has_runtime_files
= true;
1233 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1234 j
->has_persistent_files
= true;
1237 static const char *skip_slash(const char *p
) {
1248 static int add_any_file(
1253 bool close_fd
= false;
1259 assert(fd
>= 0 || path
);
1262 if (j
->toplevel_fd
>= 0)
1263 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1264 * openat() ignores the first argument. */
1266 fd
= openat(j
->toplevel_fd
, skip_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1268 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1270 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1276 r
= fd_nonblock(fd
, false);
1278 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1283 if (fstat(fd
, &st
) < 0) {
1284 r
= log_debug_errno(errno
, "Failed to fstat file '%s': %m", path
);
1288 r
= stat_verify_regular(&st
);
1290 log_debug_errno(r
, "Refusing to open '%s', as it is not a regular file.", path
);
1294 f
= ordered_hashmap_get(j
->files
, path
);
1296 if (f
->last_stat
.st_dev
== st
.st_dev
&&
1297 f
->last_stat
.st_ino
== st
.st_ino
) {
1299 /* We already track this file, under the same path and with the same device/inode numbers, it's
1300 * hence really the same. Mark this file as seen in this generation. This is used to GC old
1301 * files in process_q_overflow() to detect journal files that are still there and discern them
1302 * from those which are gone. */
1304 f
->last_seen_generation
= j
->generation
;
1309 /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
1310 * replaced (probably due to rotation?), let's drop it hence from our list. */
1311 remove_file_real(j
, f
);
1315 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1316 log_debug("Too many open journal files, not adding %s.", path
);
1321 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, false, 0, false, NULL
, j
->mmap
, NULL
, NULL
, &f
);
1323 log_debug_errno(r
, "Failed to open journal file %s: %m", path
);
1327 /* journal_file_dump(f); */
1329 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1331 f
->close_fd
= false; /* make sure journal_file_close() doesn't close the caller's fd (or our own). We'll let the caller do that, or ourselves */
1332 (void) journal_file_close(f
);
1336 close_fd
= false; /* the fd is now owned by the JournalFile object */
1338 f
->last_seen_generation
= j
->generation
;
1340 track_file_disposition(j
, f
);
1341 check_network(j
, f
->fd
);
1343 j
->current_invalidate_counter
++;
1345 log_debug("File %s added.", f
->path
);
1354 k
= journal_put_error(j
, r
, path
);
1362 static int add_file_by_name(
1365 const char *filename
) {
1373 if (j
->no_new_files
)
1376 if (!file_type_wanted(j
->flags
, filename
))
1379 path
= strjoina(prefix
, "/", filename
);
1380 return add_any_file(j
, -1, path
);
1383 static void remove_file_by_name(
1386 const char *filename
) {
1395 path
= strjoina(prefix
, "/", filename
);
1396 f
= ordered_hashmap_get(j
->files
, path
);
1400 remove_file_real(j
, f
);
1403 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1407 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1409 log_debug("File %s removed.", f
->path
);
1411 if (j
->current_file
== f
) {
1412 j
->current_file
= NULL
;
1413 j
->current_field
= 0;
1416 if (j
->unique_file
== f
) {
1417 /* Jump to the next unique_file or NULL if that one was last */
1418 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1419 j
->unique_offset
= 0;
1420 if (!j
->unique_file
)
1421 j
->unique_file_lost
= true;
1424 if (j
->fields_file
== f
) {
1425 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1426 j
->fields_offset
= 0;
1427 if (!j
->fields_file
)
1428 j
->fields_file_lost
= true;
1431 (void) journal_file_close(f
);
1433 j
->current_invalidate_counter
++;
1436 static int dirname_is_machine_id(const char *fn
) {
1437 sd_id128_t id
, machine
;
1440 r
= sd_id128_get_machine(&machine
);
1444 r
= sd_id128_from_string(fn
, &id
);
1448 return sd_id128_equal(id
, machine
);
1451 static bool dirent_is_journal_file(const struct dirent
*de
) {
1454 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1457 return endswith(de
->d_name
, ".journal") ||
1458 endswith(de
->d_name
, ".journal~");
1461 static bool dirent_is_id128_subdir(const struct dirent
*de
) {
1464 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1467 return id128_is_valid(de
->d_name
);
1470 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1477 if (j
->toplevel_fd
< 0)
1480 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1481 * relative, by dropping the initial slash */
1482 d
= xopendirat(j
->toplevel_fd
, skip_slash(path
), 0);
1490 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1492 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1499 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1501 if (dirent_is_journal_file(de
))
1502 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1504 if (m
->is_root
&& dirent_is_id128_subdir(de
))
1505 (void) add_directory(j
, m
->path
, de
->d_name
);
1511 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1514 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1521 /* Watch this directory if that's enabled and if it not being watched yet. */
1523 if (m
->wd
> 0) /* Already have a watch? */
1525 if (j
->inotify_fd
< 0) /* Not watching at all? */
1528 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1530 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1534 r
= hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
);
1536 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1538 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1539 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1544 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
) {
1545 _cleanup_free_
char *path
= NULL
;
1546 _cleanup_closedir_
DIR *d
= NULL
;
1553 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1554 * and reenumerates directory contents */
1556 path
= path_join(prefix
, dirname
);
1562 log_debug("Considering directory '%s'.", path
);
1564 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1565 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1566 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1569 r
= directory_open(j
, path
, &d
);
1571 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
1575 m
= hashmap_get(j
->directories_by_path
, path
);
1577 m
= new0(Directory
, 1);
1586 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1592 path
= NULL
; /* avoid freeing in cleanup */
1593 j
->current_invalidate_counter
++;
1595 log_debug("Directory %s added.", m
->path
);
1597 } else if (m
->is_root
)
1598 return 0; /* Don't 'downgrade' from root directory */
1600 m
->last_seen_generation
= j
->generation
;
1602 directory_watch(j
, m
, dirfd(d
),
1603 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1604 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
1607 if (!j
->no_new_files
)
1608 directory_enumerate(j
, m
, d
);
1610 check_network(j
, dirfd(d
));
1615 k
= journal_put_error(j
, r
, path
?: prefix
);
1622 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
1624 _cleanup_closedir_
DIR *d
= NULL
;
1630 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1631 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1632 * populate the set, as well as to update it later. */
1635 /* If there's a path specified, use it. */
1637 log_debug("Considering root directory '%s'.", p
);
1639 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
1640 !path_has_prefix(j
, p
, "/run"))
1644 p
= strjoina(j
->prefix
, p
);
1646 r
= directory_open(j
, p
, &d
);
1647 if (r
== -ENOENT
&& missing_ok
)
1650 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
1656 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1657 * opendir() will take possession of the fd, and close it, which we don't want. */
1659 p
= "."; /* store this as "." in the directories hashmap */
1661 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
1677 m
= hashmap_get(j
->directories_by_path
, p
);
1679 m
= new0(Directory
, 1);
1687 m
->path
= strdup(p
);
1694 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1701 j
->current_invalidate_counter
++;
1703 log_debug("Root directory %s added.", m
->path
);
1705 } else if (!m
->is_root
)
1708 directory_watch(j
, m
, dirfd(d
),
1709 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1712 if (!j
->no_new_files
)
1713 directory_enumerate(j
, m
, d
);
1715 check_network(j
, dirfd(d
));
1720 k
= journal_put_error(j
, r
, p
);
1727 static void remove_directory(sd_journal
*j
, Directory
*d
) {
1731 hashmap_remove(j
->directories_by_wd
, INT_TO_PTR(d
->wd
));
1733 if (j
->inotify_fd
>= 0)
1734 (void) inotify_rm_watch(j
->inotify_fd
, d
->wd
);
1737 hashmap_remove(j
->directories_by_path
, d
->path
);
1740 log_debug("Root directory %s removed.", d
->path
);
1742 log_debug("Directory %s removed.", d
->path
);
1748 static int add_search_paths(sd_journal
*j
) {
1750 static const char search_paths
[] =
1751 "/run/log/journal\0"
1752 "/var/log/journal\0";
1757 /* We ignore most errors here, since the idea is to only open
1758 * what's actually accessible, and ignore the rest. */
1760 NULSTR_FOREACH(p
, search_paths
)
1761 (void) add_root_directory(j
, p
, true);
1763 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
1764 (void) add_root_directory(j
, "/var/log/journal/remote", true);
1769 static int add_current_paths(sd_journal
*j
) {
1774 assert(j
->no_new_files
);
1776 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1777 * treat them as fatal. */
1779 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
1780 _cleanup_free_
char *dir
;
1783 dir
= dirname_malloc(f
->path
);
1787 r
= add_directory(j
, dir
, NULL
);
1795 static int allocate_inotify(sd_journal
*j
) {
1798 if (j
->inotify_fd
< 0) {
1799 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1800 if (j
->inotify_fd
< 0)
1804 return hashmap_ensure_allocated(&j
->directories_by_wd
, NULL
);
1807 static sd_journal
*journal_new(int flags
, const char *path
) {
1808 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1810 j
= new0(sd_journal
, 1);
1814 j
->original_pid
= getpid_cached();
1815 j
->toplevel_fd
= -1;
1818 j
->data_threshold
= DEFAULT_DATA_THRESHOLD
;
1827 if (flags
& SD_JOURNAL_OS_ROOT
)
1833 j
->files
= ordered_hashmap_new(&path_hash_ops
);
1837 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
1838 j
->directories_by_path
= hashmap_new(&path_hash_ops
);
1839 j
->mmap
= mmap_cache_new();
1840 if (!j
->files_cache
|| !j
->directories_by_path
|| !j
->mmap
)
1846 #define OPEN_ALLOWED_FLAGS \
1847 (SD_JOURNAL_LOCAL_ONLY | \
1848 SD_JOURNAL_RUNTIME_ONLY | \
1849 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)
1851 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1852 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1855 assert_return(ret
, -EINVAL
);
1856 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
1858 j
= journal_new(flags
, NULL
);
1862 r
= add_search_paths(j
);
1870 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1871 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1873 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1874 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1875 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1879 /* This is pretty much deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1880 * combination with sd_journal_open_directory_fd(). */
1882 assert_return(machine
, -EINVAL
);
1883 assert_return(ret
, -EINVAL
);
1884 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
1885 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1887 p
= strjoina("/run/systemd/machines/", machine
);
1888 r
= parse_env_file(NULL
, p
,
1898 if (!streq_ptr(class, "container"))
1901 j
= journal_new(flags
, root
);
1905 r
= add_search_paths(j
);
1913 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
1914 (SD_JOURNAL_OS_ROOT | \
1915 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1917 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
1918 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1921 assert_return(ret
, -EINVAL
);
1922 assert_return(path
, -EINVAL
);
1923 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
1925 j
= journal_new(flags
, path
);
1929 if (flags
& SD_JOURNAL_OS_ROOT
)
1930 r
= add_search_paths(j
);
1932 r
= add_root_directory(j
, path
, false);
1940 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
1941 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1945 assert_return(ret
, -EINVAL
);
1946 assert_return(flags
== 0, -EINVAL
);
1948 j
= journal_new(flags
, NULL
);
1952 STRV_FOREACH(path
, paths
) {
1953 r
= add_any_file(j
, -1, *path
);
1958 j
->no_new_files
= true;
1964 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
1965 (SD_JOURNAL_OS_ROOT | \
1966 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1968 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
1969 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1973 assert_return(ret
, -EINVAL
);
1974 assert_return(fd
>= 0, -EBADF
);
1975 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
1977 if (fstat(fd
, &st
) < 0)
1980 if (!S_ISDIR(st
.st_mode
))
1983 j
= journal_new(flags
, NULL
);
1987 j
->toplevel_fd
= fd
;
1989 if (flags
& SD_JOURNAL_OS_ROOT
)
1990 r
= add_search_paths(j
);
1992 r
= add_root_directory(j
, NULL
, false);
2000 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2003 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2007 assert_return(ret
, -EINVAL
);
2008 assert_return(n_fds
> 0, -EBADF
);
2009 assert_return(flags
== 0, -EINVAL
);
2011 j
= journal_new(flags
, NULL
);
2015 for (i
= 0; i
< n_fds
; i
++) {
2023 if (fstat(fds
[i
], &st
) < 0) {
2028 r
= stat_verify_regular(&st
);
2032 r
= add_any_file(j
, fds
[i
], NULL
);
2037 j
->no_new_files
= true;
2038 j
->no_inotify
= true;
2044 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2046 ORDERED_HASHMAP_FOREACH(f
, j
->files
, iterator
)
2047 f
->close_fd
= false;
2052 _public_
void sd_journal_close(sd_journal
*j
) {
2058 sd_journal_flush_matches(j
);
2060 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2061 iterated_cache_free(j
->files_cache
);
2063 while ((d
= hashmap_first(j
->directories_by_path
)))
2064 remove_directory(j
, d
);
2066 while ((d
= hashmap_first(j
->directories_by_wd
)))
2067 remove_directory(j
, d
);
2069 hashmap_free(j
->directories_by_path
);
2070 hashmap_free(j
->directories_by_wd
);
2072 safe_close(j
->inotify_fd
);
2075 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j
->mmap
), mmap_cache_get_missed(j
->mmap
));
2076 mmap_cache_unref(j
->mmap
);
2079 hashmap_free_free(j
->errors
);
2083 free(j
->unique_field
);
2084 free(j
->fields_buffer
);
2088 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2093 assert_return(j
, -EINVAL
);
2094 assert_return(!journal_pid_changed(j
), -ECHILD
);
2095 assert_return(ret
, -EINVAL
);
2097 f
= j
->current_file
;
2099 return -EADDRNOTAVAIL
;
2101 if (f
->current_offset
<= 0)
2102 return -EADDRNOTAVAIL
;
2104 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2108 *ret
= le64toh(o
->entry
.realtime
);
2112 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2118 assert_return(j
, -EINVAL
);
2119 assert_return(!journal_pid_changed(j
), -ECHILD
);
2121 f
= j
->current_file
;
2123 return -EADDRNOTAVAIL
;
2125 if (f
->current_offset
<= 0)
2126 return -EADDRNOTAVAIL
;
2128 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2133 *ret_boot_id
= o
->entry
.boot_id
;
2135 r
= sd_id128_get_boot(&id
);
2139 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2144 *ret
= le64toh(o
->entry
.monotonic
);
2149 static bool field_is_valid(const char *field
) {
2157 if (startswith(field
, "__"))
2160 for (p
= field
; *p
; p
++) {
2165 if (*p
>= 'A' && *p
<= 'Z')
2168 if (*p
>= '0' && *p
<= '9')
2177 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2180 size_t field_length
;
2184 assert_return(j
, -EINVAL
);
2185 assert_return(!journal_pid_changed(j
), -ECHILD
);
2186 assert_return(field
, -EINVAL
);
2187 assert_return(data
, -EINVAL
);
2188 assert_return(size
, -EINVAL
);
2189 assert_return(field_is_valid(field
), -EINVAL
);
2191 f
= j
->current_file
;
2193 return -EADDRNOTAVAIL
;
2195 if (f
->current_offset
<= 0)
2196 return -EADDRNOTAVAIL
;
2198 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2202 field_length
= strlen(field
);
2204 n
= journal_file_entry_n_items(o
);
2205 for (i
= 0; i
< n
; i
++) {
2211 p
= le64toh(o
->entry
.items
[i
].object_offset
);
2212 le_hash
= o
->entry
.items
[i
].hash
;
2213 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2217 if (le_hash
!= o
->data
.hash
)
2220 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2222 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2224 #if HAVE_XZ || HAVE_LZ4
2225 r
= decompress_startswith(compression
,
2227 &f
->compress_buffer
, &f
->compress_buffer_size
,
2228 field
, field_length
, '=');
2230 log_debug_errno(r
, "Cannot decompress %s object of length %"PRIu64
" at offset "OFSfmt
": %m",
2231 object_compressed_to_string(compression
), l
, p
);
2236 r
= decompress_blob(compression
,
2238 &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
,
2243 *data
= f
->compress_buffer
;
2244 *size
= (size_t) rsize
;
2249 return -EPROTONOSUPPORT
;
2251 } else if (l
>= field_length
+1 &&
2252 memcmp(o
->data
.payload
, field
, field_length
) == 0 &&
2253 o
->data
.payload
[field_length
] == '=') {
2257 if ((uint64_t) t
!= l
)
2260 *data
= o
->data
.payload
;
2266 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2274 static int return_data(sd_journal
*j
, JournalFile
*f
, Object
*o
, const void **data
, size_t *size
) {
2279 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2282 /* We can't read objects larger than 4G on a 32bit machine */
2283 if ((uint64_t) t
!= l
)
2286 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2288 #if HAVE_XZ || HAVE_LZ4
2292 r
= decompress_blob(compression
,
2293 o
->data
.payload
, l
, &f
->compress_buffer
,
2294 &f
->compress_buffer_size
, &rsize
, j
->data_threshold
);
2298 *data
= f
->compress_buffer
;
2299 *size
= (size_t) rsize
;
2301 return -EPROTONOSUPPORT
;
2304 *data
= o
->data
.payload
;
2311 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2318 assert_return(j
, -EINVAL
);
2319 assert_return(!journal_pid_changed(j
), -ECHILD
);
2320 assert_return(data
, -EINVAL
);
2321 assert_return(size
, -EINVAL
);
2323 f
= j
->current_file
;
2325 return -EADDRNOTAVAIL
;
2327 if (f
->current_offset
<= 0)
2328 return -EADDRNOTAVAIL
;
2330 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2334 n
= journal_file_entry_n_items(o
);
2335 if (j
->current_field
>= n
)
2338 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2339 le_hash
= o
->entry
.items
[j
->current_field
].hash
;
2340 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2344 if (le_hash
!= o
->data
.hash
)
2347 r
= return_data(j
, f
, o
, data
, size
);
2356 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2360 j
->current_field
= 0;
2363 static int reiterate_all_paths(sd_journal
*j
) {
2366 if (j
->no_new_files
)
2367 return add_current_paths(j
);
2369 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2370 return add_search_paths(j
);
2372 if (j
->toplevel_fd
>= 0)
2373 return add_root_directory(j
, NULL
, false);
2376 return add_root_directory(j
, j
->path
, true);
2378 return add_search_paths(j
);
2381 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2384 assert_return(j
, -EINVAL
);
2385 assert_return(!journal_pid_changed(j
), -ECHILD
);
2388 return -EMEDIUMTYPE
;
2390 if (j
->inotify_fd
>= 0)
2391 return j
->inotify_fd
;
2393 r
= allocate_inotify(j
);
2397 log_debug("Reiterating files to get inotify watches established.");
2399 /* Iterate through all dirs again, to add them to the inotify */
2400 r
= reiterate_all_paths(j
);
2404 return j
->inotify_fd
;
2407 _public_
int sd_journal_get_events(sd_journal
*j
) {
2410 assert_return(j
, -EINVAL
);
2411 assert_return(!journal_pid_changed(j
), -ECHILD
);
2413 fd
= sd_journal_get_fd(j
);
2420 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2423 assert_return(j
, -EINVAL
);
2424 assert_return(!journal_pid_changed(j
), -ECHILD
);
2425 assert_return(timeout_usec
, -EINVAL
);
2427 fd
= sd_journal_get_fd(j
);
2431 if (!j
->on_network
) {
2432 *timeout_usec
= (uint64_t) -1;
2436 /* If we are on the network we need to regularly check for
2437 * changes manually */
2439 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2443 static void process_q_overflow(sd_journal
*j
) {
2450 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2451 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2452 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2453 * are subject for unloading. */
2455 log_debug("Inotify queue overrun, reiterating everything.");
2458 (void) reiterate_all_paths(j
);
2460 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2462 if (f
->last_seen_generation
== j
->generation
)
2465 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2466 remove_file_real(j
, f
);
2469 HASHMAP_FOREACH(m
, j
->directories_by_path
, i
) {
2471 if (m
->last_seen_generation
== j
->generation
)
2474 if (m
->is_root
) /* Never GC root directories */
2477 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2478 remove_directory(j
, m
);
2481 log_debug("Reiteration complete.");
2484 static void process_inotify_event(sd_journal
*j
, struct inotify_event
*e
) {
2490 if (e
->mask
& IN_Q_OVERFLOW
) {
2491 process_q_overflow(j
);
2495 /* Is this a subdirectory we watch? */
2496 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2498 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2499 (endswith(e
->name
, ".journal") ||
2500 endswith(e
->name
, ".journal~"))) {
2502 /* Event for a journal file */
2504 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2505 (void) add_file_by_name(j
, d
->path
, e
->name
);
2506 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2507 remove_file_by_name(j
, d
->path
, e
->name
);
2509 } else if (!d
->is_root
&& e
->len
== 0) {
2511 /* Event for a subdirectory */
2513 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2514 remove_directory(j
, d
);
2516 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2518 /* Event for root directory */
2520 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2521 (void) add_directory(j
, d
->path
, e
->name
);
2527 if (e
->mask
& IN_IGNORED
)
2530 log_debug("Unexpected inotify event.");
2533 static int determine_change(sd_journal
*j
) {
2538 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2539 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2541 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2544 _public_
int sd_journal_process(sd_journal
*j
) {
2545 bool got_something
= false;
2547 assert_return(j
, -EINVAL
);
2548 assert_return(!journal_pid_changed(j
), -ECHILD
);
2550 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
2553 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2554 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2557 union inotify_event_buffer buffer
;
2558 struct inotify_event
*e
;
2561 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2563 if (IN_SET(errno
, EAGAIN
, EINTR
))
2564 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2569 got_something
= true;
2571 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2572 process_inotify_event(j
, e
);
2576 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2580 assert_return(j
, -EINVAL
);
2581 assert_return(!journal_pid_changed(j
), -ECHILD
);
2583 if (j
->inotify_fd
< 0) {
2585 /* This is the first invocation, hence create the
2587 r
= sd_journal_get_fd(j
);
2591 /* The journal might have changed since the context
2592 * object was created and we weren't watching before,
2593 * hence don't wait for anything, and return
2595 return determine_change(j
);
2598 r
= sd_journal_get_timeout(j
, &t
);
2602 if (t
!= (uint64_t) -1) {
2605 n
= now(CLOCK_MONOTONIC
);
2606 t
= t
> n
? t
- n
: 0;
2608 if (timeout_usec
== (uint64_t) -1 || timeout_usec
> t
)
2613 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2614 } while (r
== -EINTR
);
2619 return sd_journal_process(j
);
2622 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2626 uint64_t fmin
= 0, tmax
= 0;
2629 assert_return(j
, -EINVAL
);
2630 assert_return(!journal_pid_changed(j
), -ECHILD
);
2631 assert_return(from
|| to
, -EINVAL
);
2632 assert_return(from
!= to
, -EINVAL
);
2634 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2637 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2650 fmin
= MIN(fr
, fmin
);
2651 tmax
= MAX(t
, tmax
);
2660 return first
? 0 : 1;
2663 _public_
int sd_journal_get_cutoff_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t *from
, uint64_t *to
) {
2669 assert_return(j
, -EINVAL
);
2670 assert_return(!journal_pid_changed(j
), -ECHILD
);
2671 assert_return(from
|| to
, -EINVAL
);
2672 assert_return(from
!= to
, -EINVAL
);
2674 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2677 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &fr
, &t
);
2687 *from
= MIN(fr
, *from
);
2702 void journal_print_header(sd_journal
*j
) {
2705 bool newline
= false;
2709 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2715 journal_file_print_header(f
);
2719 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *bytes
) {
2724 assert_return(j
, -EINVAL
);
2725 assert_return(!journal_pid_changed(j
), -ECHILD
);
2726 assert_return(bytes
, -EINVAL
);
2728 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2731 if (fstat(f
->fd
, &st
) < 0)
2734 sum
+= (uint64_t) st
.st_blocks
* 512ULL;
2741 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2744 assert_return(j
, -EINVAL
);
2745 assert_return(!journal_pid_changed(j
), -ECHILD
);
2746 assert_return(!isempty(field
), -EINVAL
);
2747 assert_return(field_is_valid(field
), -EINVAL
);
2753 free(j
->unique_field
);
2754 j
->unique_field
= f
;
2755 j
->unique_file
= NULL
;
2756 j
->unique_offset
= 0;
2757 j
->unique_file_lost
= false;
2762 _public_
int sd_journal_enumerate_unique(sd_journal
*j
, const void **data
, size_t *l
) {
2765 assert_return(j
, -EINVAL
);
2766 assert_return(!journal_pid_changed(j
), -ECHILD
);
2767 assert_return(data
, -EINVAL
);
2768 assert_return(l
, -EINVAL
);
2769 assert_return(j
->unique_field
, -EINVAL
);
2771 k
= strlen(j
->unique_field
);
2773 if (!j
->unique_file
) {
2774 if (j
->unique_file_lost
)
2777 j
->unique_file
= ordered_hashmap_first(j
->files
);
2778 if (!j
->unique_file
)
2781 j
->unique_offset
= 0;
2793 /* Proceed to next data object in the field's linked list */
2794 if (j
->unique_offset
== 0) {
2795 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2799 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2801 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2805 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2808 /* We reached the end of the list? Then start again, with the next file */
2809 if (j
->unique_offset
== 0) {
2810 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2811 if (!j
->unique_file
)
2817 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2818 * instead, so that we can look at this data object at the same
2819 * time as one on another file */
2820 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2824 /* Let's do the type check by hand, since we used 0 context above. */
2825 if (o
->object
.type
!= OBJECT_DATA
)
2826 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2827 "%s:offset " OFSfmt
": object has type %d, expected %d",
2828 j
->unique_file
->path
,
2830 o
->object
.type
, OBJECT_DATA
);
2832 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2836 /* Check if we have at least the field name and "=". */
2838 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2839 "%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2840 j
->unique_file
->path
,
2841 j
->unique_offset
, ol
, k
+ 1);
2843 if (memcmp(odata
, j
->unique_field
, k
) || ((const char*) odata
)[k
] != '=')
2844 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2845 "%s:offset " OFSfmt
": object does not start with \"%s=\"",
2846 j
->unique_file
->path
,
2850 /* OK, now let's see if we already returned this data
2851 * object by checking if it exists in the earlier
2852 * traversed files. */
2854 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2855 if (of
== j
->unique_file
)
2858 /* Skip this file it didn't have any fields indexed */
2859 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2862 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
2874 r
= return_data(j
, j
->unique_file
, o
, data
, l
);
2882 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
2886 j
->unique_file
= NULL
;
2887 j
->unique_offset
= 0;
2888 j
->unique_file_lost
= false;
2891 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
2894 assert_return(j
, -EINVAL
);
2895 assert_return(!journal_pid_changed(j
), -ECHILD
);
2896 assert_return(field
, -EINVAL
);
2898 if (!j
->fields_file
) {
2899 if (j
->fields_file_lost
)
2902 j
->fields_file
= ordered_hashmap_first(j
->files
);
2903 if (!j
->fields_file
)
2906 j
->fields_hash_table_index
= 0;
2907 j
->fields_offset
= 0;
2911 JournalFile
*f
, *of
;
2920 if (j
->fields_offset
== 0) {
2923 /* We are not yet positioned at any field. Let's pick the first one */
2924 r
= journal_file_map_field_hash_table(f
);
2928 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
2930 if (j
->fields_hash_table_index
>= m
) {
2931 /* Reached the end of the hash table, go to the next file. */
2936 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
2938 if (j
->fields_offset
!= 0)
2941 /* Empty hash table bucket, go to next one */
2942 j
->fields_hash_table_index
++;
2946 /* Proceed with next file */
2947 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
2948 if (!j
->fields_file
) {
2953 j
->fields_offset
= 0;
2954 j
->fields_hash_table_index
= 0;
2959 /* We are already positioned at a field. If so, let's figure out the next field from it */
2961 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
2965 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
2966 if (j
->fields_offset
== 0) {
2967 /* Reached the end of the hash table chain */
2968 j
->fields_hash_table_index
++;
2973 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
2974 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
2978 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
2979 if (o
->object
.type
!= OBJECT_FIELD
)
2980 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2981 "%s:offset " OFSfmt
": object has type %i, expected %i",
2982 f
->path
, j
->fields_offset
,
2983 o
->object
.type
, OBJECT_FIELD
);
2985 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
2987 /* Let's see if we already returned this field name before. */
2989 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2993 /* Skip this file it didn't have any fields indexed */
2994 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2997 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
, le64toh(o
->field
.hash
), NULL
, NULL
);
3009 /* Check if this is really a valid string containing no NUL byte */
3010 if (memchr(o
->field
.payload
, 0, sz
))
3013 if (sz
> j
->data_threshold
)
3014 sz
= j
->data_threshold
;
3016 if (!GREEDY_REALLOC(j
->fields_buffer
, j
->fields_buffer_allocated
, sz
+ 1))
3019 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3020 j
->fields_buffer
[sz
] = 0;
3022 if (!field_is_valid(j
->fields_buffer
))
3025 *field
= j
->fields_buffer
;
3030 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3034 j
->fields_file
= NULL
;
3035 j
->fields_hash_table_index
= 0;
3036 j
->fields_offset
= 0;
3037 j
->fields_file_lost
= false;
3040 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3041 assert_return(j
, -EINVAL
);
3042 assert_return(!journal_pid_changed(j
), -ECHILD
);
3044 return !j
->on_network
;
3047 static char *lookup_field(const char *field
, void *userdata
) {
3048 sd_journal
*j
= userdata
;
3056 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3058 size
> REPLACE_VAR_MAX
)
3059 return strdup(field
);
3061 d
= strlen(field
) + 1;
3063 return strndup((const char*) data
+ d
, size
- d
);
3066 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3070 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3074 assert_return(j
, -EINVAL
);
3075 assert_return(!journal_pid_changed(j
), -ECHILD
);
3076 assert_return(ret
, -EINVAL
);
3078 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3082 cid
= strndup((const char*) data
+ 11, size
- 11);
3086 r
= sd_id128_from_string(cid
, &id
);
3090 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
3094 t
= replace_var(text
, lookup_field
, j
);
3102 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3103 assert_return(ret
, -EINVAL
);
3105 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3108 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3109 assert_return(j
, -EINVAL
);
3110 assert_return(!journal_pid_changed(j
), -ECHILD
);
3112 j
->data_threshold
= sz
;
3116 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3117 assert_return(j
, -EINVAL
);
3118 assert_return(!journal_pid_changed(j
), -ECHILD
);
3119 assert_return(sz
, -EINVAL
);
3121 *sz
= j
->data_threshold
;
3125 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3126 assert_return(j
, -EINVAL
);
3128 return j
->has_runtime_files
;
3131 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3132 assert_return(j
, -EINVAL
);
3134 return j
->has_persistent_files
;