1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
11 #include <linux/magic.h>
14 #include <sys/inotify.h>
18 #include "sd-journal.h"
20 #include "alloc-util.h"
23 #include "dirent-util.h"
26 #include "format-util.h"
29 #include "hostname-util.h"
30 #include "id128-util.h"
32 #include "journal-def.h"
33 #include "journal-file.h"
34 #include "journal-internal.h"
38 #include "path-util.h"
39 #include "process-util.h"
40 #include "replace-var.h"
41 #include "stat-util.h"
42 #include "stat-util.h"
43 #include "stdio-util.h"
44 #include "string-util.h"
47 #define JOURNAL_FILES_MAX 7168
49 #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
51 #define REPLACE_VAR_MAX 256
53 #define DEFAULT_DATA_THRESHOLD (64*1024)
55 static void remove_file_real(sd_journal
*j
, JournalFile
*f
);
57 static bool journal_pid_changed(sd_journal
*j
) {
60 /* We don't support people creating a journal object and
61 * keeping it around over a fork(). Let's complain. */
63 return j
->original_pid
!= getpid_cached();
66 static int journal_put_error(sd_journal
*j
, int r
, const char *path
) {
70 /* Memorize an error we encountered, and store which
71 * file/directory it was generated from. Note that we store
72 * only *one* path per error code, as the error code is the
73 * key into the hashmap, and the path is the value. This means
74 * we keep track only of all error kinds, but not of all error
75 * locations. This has the benefit that the hashmap cannot
78 * We return an error here only if we didn't manage to
79 * memorize the real error. */
84 k
= hashmap_ensure_allocated(&j
->errors
, NULL
);
95 k
= hashmap_put(j
->errors
, INT_TO_PTR(r
), copy
);
108 static void detach_location(sd_journal
*j
) {
114 j
->current_file
= NULL
;
115 j
->current_field
= 0;
117 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
)
118 journal_file_reset_location(f
);
121 static void reset_location(sd_journal
*j
) {
125 zero(j
->current_location
);
128 static void init_location(Location
*l
, LocationType type
, JournalFile
*f
, Object
*o
) {
130 assert(IN_SET(type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
132 assert(o
->object
.type
== OBJECT_ENTRY
);
135 l
->seqnum
= le64toh(o
->entry
.seqnum
);
136 l
->seqnum_id
= f
->header
->seqnum_id
;
137 l
->realtime
= le64toh(o
->entry
.realtime
);
138 l
->monotonic
= le64toh(o
->entry
.monotonic
);
139 l
->boot_id
= o
->entry
.boot_id
;
140 l
->xor_hash
= le64toh(o
->entry
.xor_hash
);
142 l
->seqnum_set
= l
->realtime_set
= l
->monotonic_set
= l
->xor_hash_set
= true;
145 static void set_location(sd_journal
*j
, JournalFile
*f
, Object
*o
) {
150 init_location(&j
->current_location
, LOCATION_DISCRETE
, f
, o
);
153 j
->current_field
= 0;
155 /* Let f know its candidate entry was picked. */
156 assert(f
->location_type
== LOCATION_SEEK
);
157 f
->location_type
= LOCATION_DISCRETE
;
160 static int match_is_valid(const void *data
, size_t size
) {
168 if (startswith(data
, "__"))
172 for (p
= b
; p
< b
+ size
; p
++) {
180 if (*p
>= 'A' && *p
<= 'Z')
183 if (*p
>= '0' && *p
<= '9')
192 static bool same_field(const void *_a
, size_t s
, const void *_b
, size_t t
) {
193 const uint8_t *a
= _a
, *b
= _b
;
196 for (j
= 0; j
< s
&& j
< t
; j
++) {
205 assert_not_reached("\"=\" not found");
208 static Match
*match_new(Match
*p
, MatchType t
) {
219 LIST_PREPEND(matches
, p
->matches
, m
);
225 static void match_free(Match
*m
) {
229 match_free(m
->matches
);
232 LIST_REMOVE(matches
, m
->parent
->matches
, m
);
238 static void match_free_if_empty(Match
*m
) {
239 if (!m
|| m
->matches
)
245 _public_
int sd_journal_add_match(sd_journal
*j
, const void *data
, size_t size
) {
246 Match
*l3
, *l4
, *add_here
= NULL
, *m
;
249 assert_return(j
, -EINVAL
);
250 assert_return(!journal_pid_changed(j
), -ECHILD
);
251 assert_return(data
, -EINVAL
);
256 assert_return(match_is_valid(data
, size
), -EINVAL
);
262 * level 4: concrete matches */
265 j
->level0
= match_new(NULL
, MATCH_AND_TERM
);
271 j
->level1
= match_new(j
->level0
, MATCH_OR_TERM
);
277 j
->level2
= match_new(j
->level1
, MATCH_AND_TERM
);
282 assert(j
->level0
->type
== MATCH_AND_TERM
);
283 assert(j
->level1
->type
== MATCH_OR_TERM
);
284 assert(j
->level2
->type
== MATCH_AND_TERM
);
286 le_hash
= htole64(hash64(data
, size
));
288 LIST_FOREACH(matches
, l3
, j
->level2
->matches
) {
289 assert(l3
->type
== MATCH_OR_TERM
);
291 LIST_FOREACH(matches
, l4
, l3
->matches
) {
292 assert(l4
->type
== MATCH_DISCRETE
);
294 /* Exactly the same match already? Then ignore
296 if (l4
->le_hash
== le_hash
&&
298 memcmp(l4
->data
, data
, size
) == 0)
301 /* Same field? Then let's add this to this OR term */
302 if (same_field(data
, size
, l4
->data
, l4
->size
)) {
313 add_here
= match_new(j
->level2
, MATCH_OR_TERM
);
318 m
= match_new(add_here
, MATCH_DISCRETE
);
322 m
->le_hash
= le_hash
;
324 m
->data
= memdup(data
, size
);
333 match_free_if_empty(add_here
);
334 match_free_if_empty(j
->level2
);
335 match_free_if_empty(j
->level1
);
336 match_free_if_empty(j
->level0
);
341 _public_
int sd_journal_add_conjunction(sd_journal
*j
) {
342 assert_return(j
, -EINVAL
);
343 assert_return(!journal_pid_changed(j
), -ECHILD
);
351 if (!j
->level1
->matches
)
360 _public_
int sd_journal_add_disjunction(sd_journal
*j
) {
361 assert_return(j
, -EINVAL
);
362 assert_return(!journal_pid_changed(j
), -ECHILD
);
373 if (!j
->level2
->matches
)
380 static char *match_make_string(Match
*m
) {
383 bool enclose
= false;
386 return strdup("none");
388 if (m
->type
== MATCH_DISCRETE
)
389 return strndup(m
->data
, m
->size
);
391 LIST_FOREACH(matches
, i
, m
->matches
) {
394 t
= match_make_string(i
);
399 k
= strjoin(p
, m
->type
== MATCH_OR_TERM
? " OR " : " AND ", t
);
414 r
= strjoin("(", p
, ")");
422 char *journal_make_match_string(sd_journal
*j
) {
425 return match_make_string(j
->level0
);
428 _public_
void sd_journal_flush_matches(sd_journal
*j
) {
433 match_free(j
->level0
);
435 j
->level0
= j
->level1
= j
->level2
= NULL
;
440 _pure_
static int compare_with_location(JournalFile
*f
, Location
*l
) {
443 assert(f
->location_type
== LOCATION_SEEK
);
444 assert(IN_SET(l
->type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
446 if (l
->monotonic_set
&&
447 sd_id128_equal(f
->current_boot_id
, l
->boot_id
) &&
449 f
->current_realtime
== l
->realtime
&&
451 f
->current_xor_hash
== l
->xor_hash
)
455 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
)) {
457 if (f
->current_seqnum
< l
->seqnum
)
459 if (f
->current_seqnum
> l
->seqnum
)
463 if (l
->monotonic_set
&&
464 sd_id128_equal(f
->current_boot_id
, l
->boot_id
)) {
466 if (f
->current_monotonic
< l
->monotonic
)
468 if (f
->current_monotonic
> l
->monotonic
)
472 if (l
->realtime_set
) {
474 if (f
->current_realtime
< l
->realtime
)
476 if (f
->current_realtime
> l
->realtime
)
480 if (l
->xor_hash_set
) {
482 if (f
->current_xor_hash
< l
->xor_hash
)
484 if (f
->current_xor_hash
> l
->xor_hash
)
491 static int next_for_match(
495 uint64_t after_offset
,
496 direction_t direction
,
508 if (m
->type
== MATCH_DISCRETE
) {
511 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
515 return journal_file_move_to_entry_by_offset_for_data(f
, dp
, after_offset
, direction
, ret
, offset
);
517 } else if (m
->type
== MATCH_OR_TERM
) {
520 /* Find the earliest match beyond after_offset */
522 LIST_FOREACH(matches
, i
, m
->matches
) {
525 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
529 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
537 } else if (m
->type
== MATCH_AND_TERM
) {
538 Match
*i
, *last_moved
;
540 /* Always jump to the next matching entry and repeat
541 * this until we find an offset that matches for all
547 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
551 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
552 last_moved
= m
->matches
;
554 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
557 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
561 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
562 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
571 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
583 static int find_location_for_match(
587 direction_t direction
,
597 if (m
->type
== MATCH_DISCRETE
) {
600 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
604 /* FIXME: missing: find by monotonic */
606 if (j
->current_location
.type
== LOCATION_HEAD
)
607 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_DOWN
, ret
, offset
);
608 if (j
->current_location
.type
== LOCATION_TAIL
)
609 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_UP
, ret
, offset
);
610 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
611 return journal_file_move_to_entry_by_seqnum_for_data(f
, dp
, j
->current_location
.seqnum
, direction
, ret
, offset
);
612 if (j
->current_location
.monotonic_set
) {
613 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, dp
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
617 if (j
->current_location
.realtime_set
)
618 return journal_file_move_to_entry_by_realtime_for_data(f
, dp
, j
->current_location
.realtime
, direction
, ret
, offset
);
620 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, direction
, ret
, offset
);
622 } else if (m
->type
== MATCH_OR_TERM
) {
627 /* Find the earliest match */
629 LIST_FOREACH(matches
, i
, m
->matches
) {
632 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
636 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
644 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
659 assert(m
->type
== MATCH_AND_TERM
);
661 /* First jump to the last match, and then find the
662 * next one where all matches match */
667 LIST_FOREACH(matches
, i
, m
->matches
) {
670 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
674 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
678 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
682 static int find_location_with_matches(
685 direction_t direction
,
697 /* No matches is simple */
699 if (j
->current_location
.type
== LOCATION_HEAD
)
700 return journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
);
701 if (j
->current_location
.type
== LOCATION_TAIL
)
702 return journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
);
703 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
704 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
705 if (j
->current_location
.monotonic_set
) {
706 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
710 if (j
->current_location
.realtime_set
)
711 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
713 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
715 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
718 static int next_with_matches(
721 direction_t direction
,
730 /* No matches is easy. We simple advance the file
733 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
735 /* If we have a match then we look for the next matching entry
736 * with an offset at least one step larger */
737 return next_for_match(j
, j
->level0
, f
,
738 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
739 : f
->current_offset
- 1,
740 direction
, ret
, offset
);
743 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
745 uint64_t cp
, n_entries
;
751 n_entries
= le64toh(f
->header
->n_entries
);
753 /* If we hit EOF before, we don't need to look into this file again
754 * unless direction changed or new entries appeared. */
755 if (f
->last_direction
== direction
&& f
->location_type
== LOCATION_TAIL
&&
756 n_entries
== f
->last_n_entries
)
759 f
->last_n_entries
= n_entries
;
761 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
762 /* LOCATION_SEEK here means we did the work in a previous
763 * iteration and the current location already points to a
764 * candidate entry. */
765 if (f
->location_type
!= LOCATION_SEEK
) {
766 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
770 journal_file_save_location(f
, c
, cp
);
773 f
->last_direction
= direction
;
775 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
779 journal_file_save_location(f
, c
, cp
);
782 /* OK, we found the spot, now let's advance until an entry
783 * that is actually different from what we were previously
784 * looking at. This is necessary to handle entries which exist
785 * in two (or more) journal files, and which shall all be
786 * suppressed but one. */
791 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
794 k
= compare_with_location(f
, &j
->current_location
);
796 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
803 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
807 journal_file_save_location(f
, c
, cp
);
811 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
812 JournalFile
*new_file
= NULL
;
818 assert_return(j
, -EINVAL
);
819 assert_return(!journal_pid_changed(j
), -ECHILD
);
821 r
= iterated_cache_get(j
->files_cache
, NULL
, &files
, &n_files
);
825 for (i
= 0; i
< n_files
; i
++) {
826 JournalFile
*f
= (JournalFile
*)files
[i
];
829 r
= next_beyond_location(j
, f
, direction
);
831 log_debug_errno(r
, "Can't iterate through %s, ignoring: %m", f
->path
);
832 remove_file_real(j
, f
);
835 f
->location_type
= LOCATION_TAIL
;
844 k
= journal_file_compare_locations(f
, new_file
);
846 found
= direction
== DIRECTION_DOWN
? k
< 0 : k
> 0;
856 r
= journal_file_move_to_object(new_file
, OBJECT_ENTRY
, new_file
->current_offset
, &o
);
860 set_location(j
, new_file
, o
);
865 _public_
int sd_journal_next(sd_journal
*j
) {
866 return real_journal_next(j
, DIRECTION_DOWN
);
869 _public_
int sd_journal_previous(sd_journal
*j
) {
870 return real_journal_next(j
, DIRECTION_UP
);
873 static int real_journal_next_skip(sd_journal
*j
, direction_t direction
, uint64_t skip
) {
876 assert_return(j
, -EINVAL
);
877 assert_return(!journal_pid_changed(j
), -ECHILD
);
880 /* If this is not a discrete skip, then at least
881 * resolve the current location */
882 if (j
->current_location
.type
!= LOCATION_DISCRETE
) {
883 r
= real_journal_next(j
, direction
);
892 r
= real_journal_next(j
, direction
);
906 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
907 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
910 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
911 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
914 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
917 char bid
[33], sid
[33];
919 assert_return(j
, -EINVAL
);
920 assert_return(!journal_pid_changed(j
), -ECHILD
);
921 assert_return(cursor
, -EINVAL
);
923 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
924 return -EADDRNOTAVAIL
;
926 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
930 sd_id128_to_string(j
->current_file
->header
->seqnum_id
, sid
);
931 sd_id128_to_string(o
->entry
.boot_id
, bid
);
934 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
935 sid
, le64toh(o
->entry
.seqnum
),
936 bid
, le64toh(o
->entry
.monotonic
),
937 le64toh(o
->entry
.realtime
),
938 le64toh(o
->entry
.xor_hash
)) < 0)
944 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
945 const char *word
, *state
;
947 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
949 seqnum_id_set
= false,
952 monotonic_set
= false,
953 realtime_set
= false,
954 xor_hash_set
= false;
955 sd_id128_t seqnum_id
, boot_id
;
957 assert_return(j
, -EINVAL
);
958 assert_return(!journal_pid_changed(j
), -ECHILD
);
959 assert_return(!isempty(cursor
), -EINVAL
);
961 FOREACH_WORD_SEPARATOR(word
, l
, cursor
, ";", state
) {
965 if (l
< 2 || word
[1] != '=')
968 item
= strndup(word
, l
);
975 seqnum_id_set
= true;
976 k
= sd_id128_from_string(item
+2, &seqnum_id
);
981 if (sscanf(item
+2, "%llx", &seqnum
) != 1)
987 k
= sd_id128_from_string(item
+2, &boot_id
);
991 monotonic_set
= true;
992 if (sscanf(item
+2, "%llx", &monotonic
) != 1)
998 if (sscanf(item
+2, "%llx", &realtime
) != 1)
1003 xor_hash_set
= true;
1004 if (sscanf(item
+2, "%llx", &xor_hash
) != 1)
1015 if ((!seqnum_set
|| !seqnum_id_set
) &&
1016 (!monotonic_set
|| !boot_id_set
) &&
1022 j
->current_location
.type
= LOCATION_SEEK
;
1025 j
->current_location
.realtime
= (uint64_t) realtime
;
1026 j
->current_location
.realtime_set
= true;
1029 if (seqnum_set
&& seqnum_id_set
) {
1030 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1031 j
->current_location
.seqnum_id
= seqnum_id
;
1032 j
->current_location
.seqnum_set
= true;
1035 if (monotonic_set
&& boot_id_set
) {
1036 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1037 j
->current_location
.boot_id
= boot_id
;
1038 j
->current_location
.monotonic_set
= true;
1042 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1043 j
->current_location
.xor_hash_set
= true;
1049 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1053 assert_return(j
, -EINVAL
);
1054 assert_return(!journal_pid_changed(j
), -ECHILD
);
1055 assert_return(!isempty(cursor
), -EINVAL
);
1057 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1058 return -EADDRNOTAVAIL
;
1060 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1065 _cleanup_free_
char *item
= NULL
;
1066 unsigned long long ll
;
1070 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1077 if (strlen(item
) < 2 || item
[1] != '=')
1083 k
= sd_id128_from_string(item
+2, &id
);
1086 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1091 if (sscanf(item
+2, "%llx", &ll
) != 1)
1093 if (ll
!= le64toh(o
->entry
.seqnum
))
1098 k
= sd_id128_from_string(item
+2, &id
);
1101 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1106 if (sscanf(item
+2, "%llx", &ll
) != 1)
1108 if (ll
!= le64toh(o
->entry
.monotonic
))
1113 if (sscanf(item
+2, "%llx", &ll
) != 1)
1115 if (ll
!= le64toh(o
->entry
.realtime
))
1120 if (sscanf(item
+2, "%llx", &ll
) != 1)
1122 if (ll
!= le64toh(o
->entry
.xor_hash
))
1131 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t usec
) {
1132 assert_return(j
, -EINVAL
);
1133 assert_return(!journal_pid_changed(j
), -ECHILD
);
1136 j
->current_location
.type
= LOCATION_SEEK
;
1137 j
->current_location
.boot_id
= boot_id
;
1138 j
->current_location
.monotonic
= usec
;
1139 j
->current_location
.monotonic_set
= true;
1144 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1145 assert_return(j
, -EINVAL
);
1146 assert_return(!journal_pid_changed(j
), -ECHILD
);
1149 j
->current_location
.type
= LOCATION_SEEK
;
1150 j
->current_location
.realtime
= usec
;
1151 j
->current_location
.realtime_set
= true;
1156 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1157 assert_return(j
, -EINVAL
);
1158 assert_return(!journal_pid_changed(j
), -ECHILD
);
1161 j
->current_location
.type
= LOCATION_HEAD
;
1166 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1167 assert_return(j
, -EINVAL
);
1168 assert_return(!journal_pid_changed(j
), -ECHILD
);
1171 j
->current_location
.type
= LOCATION_TAIL
;
1176 static void check_network(sd_journal
*j
, int fd
) {
1182 j
->on_network
= fd_is_network_fs(fd
);
1185 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1186 const char *full
, *tilded
, *atted
;
1188 full
= strjoina(prefix
, ".journal");
1189 tilded
= strjoina(full
, "~");
1190 atted
= strjoina(prefix
, "@");
1192 return streq(filename
, full
) ||
1193 streq(filename
, tilded
) ||
1194 startswith(filename
, atted
);
1197 static bool file_type_wanted(int flags
, const char *filename
) {
1200 if (!endswith(filename
, ".journal") && !endswith(filename
, ".journal~"))
1203 /* no flags set → every type is OK */
1204 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1207 if (flags
& SD_JOURNAL_SYSTEM
&& file_has_type_prefix("system", filename
))
1210 if (flags
& SD_JOURNAL_CURRENT_USER
) {
1211 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1213 xsprintf(prefix
, "user-"UID_FMT
, getuid());
1215 if (file_has_type_prefix(prefix
, filename
))
1222 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1227 if (j
->toplevel_fd
>= 0)
1230 return path_startswith(path
, prefix
);
1233 static void track_file_disposition(sd_journal
*j
, JournalFile
*f
) {
1237 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1238 j
->has_runtime_files
= true;
1239 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1240 j
->has_persistent_files
= true;
1243 static const char *skip_slash(const char *p
) {
1254 static int add_any_file(
1259 bool close_fd
= false;
1265 assert(fd
>= 0 || path
);
1268 if (j
->toplevel_fd
>= 0)
1269 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1270 * openat() ignores the first argument. */
1272 fd
= openat(j
->toplevel_fd
, skip_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1274 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1276 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1282 r
= fd_nonblock(fd
, false);
1284 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1289 if (fstat(fd
, &st
) < 0) {
1290 r
= log_debug_errno(errno
, "Failed to fstat file '%s': %m", path
);
1294 r
= stat_verify_regular(&st
);
1296 log_debug_errno(r
, "Refusing to open '%s', as it is not a regular file.", path
);
1300 f
= ordered_hashmap_get(j
->files
, path
);
1302 if (f
->last_stat
.st_dev
== st
.st_dev
&&
1303 f
->last_stat
.st_ino
== st
.st_ino
) {
1305 /* We already track this file, under the same path and with the same device/inode numbers, it's
1306 * hence really the same. Mark this file as seen in this generation. This is used to GC old
1307 * files in process_q_overflow() to detect journal files that are still there and discern them
1308 * from those which are gone. */
1310 f
->last_seen_generation
= j
->generation
;
1315 /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
1316 * replaced (probably due to rotation?), let's drop it hence from our list. */
1317 remove_file_real(j
, f
);
1321 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1322 log_debug("Too many open journal files, not adding %s.", path
);
1327 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, false, 0, false, NULL
, j
->mmap
, NULL
, NULL
, &f
);
1329 log_debug_errno(r
, "Failed to open journal file %s: %m", path
);
1333 /* journal_file_dump(f); */
1335 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1337 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 */
1338 (void) journal_file_close(f
);
1342 close_fd
= false; /* the fd is now owned by the JournalFile object */
1344 f
->last_seen_generation
= j
->generation
;
1346 track_file_disposition(j
, f
);
1347 check_network(j
, f
->fd
);
1349 j
->current_invalidate_counter
++;
1351 log_debug("File %s added.", f
->path
);
1360 k
= journal_put_error(j
, r
, path
);
1368 static int add_file_by_name(
1371 const char *filename
) {
1379 if (j
->no_new_files
)
1382 if (!file_type_wanted(j
->flags
, filename
))
1385 path
= strjoina(prefix
, "/", filename
);
1386 return add_any_file(j
, -1, path
);
1389 static void remove_file_by_name(
1392 const char *filename
) {
1401 path
= strjoina(prefix
, "/", filename
);
1402 f
= ordered_hashmap_get(j
->files
, path
);
1406 remove_file_real(j
, f
);
1409 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1413 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1415 log_debug("File %s removed.", f
->path
);
1417 if (j
->current_file
== f
) {
1418 j
->current_file
= NULL
;
1419 j
->current_field
= 0;
1422 if (j
->unique_file
== f
) {
1423 /* Jump to the next unique_file or NULL if that one was last */
1424 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1425 j
->unique_offset
= 0;
1426 if (!j
->unique_file
)
1427 j
->unique_file_lost
= true;
1430 if (j
->fields_file
== f
) {
1431 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1432 j
->fields_offset
= 0;
1433 if (!j
->fields_file
)
1434 j
->fields_file_lost
= true;
1437 (void) journal_file_close(f
);
1439 j
->current_invalidate_counter
++;
1442 static int dirname_is_machine_id(const char *fn
) {
1443 sd_id128_t id
, machine
;
1446 r
= sd_id128_get_machine(&machine
);
1450 r
= sd_id128_from_string(fn
, &id
);
1454 return sd_id128_equal(id
, machine
);
1457 static bool dirent_is_journal_file(const struct dirent
*de
) {
1460 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1463 return endswith(de
->d_name
, ".journal") ||
1464 endswith(de
->d_name
, ".journal~");
1467 static bool dirent_is_id128_subdir(const struct dirent
*de
) {
1470 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1473 return id128_is_valid(de
->d_name
);
1476 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1483 if (j
->toplevel_fd
< 0)
1486 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1487 * relative, by dropping the initial slash */
1488 d
= xopendirat(j
->toplevel_fd
, skip_slash(path
), 0);
1496 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1498 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1505 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1507 if (dirent_is_journal_file(de
))
1508 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1510 if (m
->is_root
&& dirent_is_id128_subdir(de
))
1511 (void) add_directory(j
, m
->path
, de
->d_name
);
1517 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1520 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1527 /* Watch this directory if that's enabled and if it not being watched yet. */
1529 if (m
->wd
> 0) /* Already have a watch? */
1531 if (j
->inotify_fd
< 0) /* Not watching at all? */
1534 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1536 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1540 r
= hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
);
1542 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1544 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1545 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1550 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
) {
1551 _cleanup_free_
char *path
= NULL
;
1552 _cleanup_closedir_
DIR *d
= NULL
;
1559 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1560 * and reenumerates directory contents */
1563 path
= strjoin(prefix
, "/", dirname
);
1565 path
= strdup(prefix
);
1571 log_debug("Considering directory '%s'.", path
);
1573 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1574 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1575 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1578 r
= directory_open(j
, path
, &d
);
1580 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
1584 m
= hashmap_get(j
->directories_by_path
, path
);
1586 m
= new0(Directory
, 1);
1595 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1601 path
= NULL
; /* avoid freeing in cleanup */
1602 j
->current_invalidate_counter
++;
1604 log_debug("Directory %s added.", m
->path
);
1606 } else if (m
->is_root
)
1607 return 0; /* Don't 'downgrade' from root directory */
1609 m
->last_seen_generation
= j
->generation
;
1611 directory_watch(j
, m
, dirfd(d
),
1612 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1613 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
1616 if (!j
->no_new_files
)
1617 directory_enumerate(j
, m
, d
);
1619 check_network(j
, dirfd(d
));
1624 k
= journal_put_error(j
, r
, path
?: prefix
);
1631 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
1633 _cleanup_closedir_
DIR *d
= NULL
;
1639 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1640 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1641 * populate the set, as well as to update it later. */
1644 /* If there's a path specified, use it. */
1646 log_debug("Considering root directory '%s'.", p
);
1648 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
1649 !path_has_prefix(j
, p
, "/run"))
1653 p
= strjoina(j
->prefix
, p
);
1655 r
= directory_open(j
, p
, &d
);
1656 if (r
== -ENOENT
&& missing_ok
)
1659 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
1665 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1666 * opendir() will take possession of the fd, and close it, which we don't want. */
1668 p
= "."; /* store this as "." in the directories hashmap */
1670 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
1686 m
= hashmap_get(j
->directories_by_path
, p
);
1688 m
= new0(Directory
, 1);
1696 m
->path
= strdup(p
);
1703 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1710 j
->current_invalidate_counter
++;
1712 log_debug("Root directory %s added.", m
->path
);
1714 } else if (!m
->is_root
)
1717 directory_watch(j
, m
, dirfd(d
),
1718 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1721 if (!j
->no_new_files
)
1722 directory_enumerate(j
, m
, d
);
1724 check_network(j
, dirfd(d
));
1729 k
= journal_put_error(j
, r
, p
);
1736 static void remove_directory(sd_journal
*j
, Directory
*d
) {
1740 hashmap_remove(j
->directories_by_wd
, INT_TO_PTR(d
->wd
));
1742 if (j
->inotify_fd
>= 0)
1743 inotify_rm_watch(j
->inotify_fd
, d
->wd
);
1746 hashmap_remove(j
->directories_by_path
, d
->path
);
1749 log_debug("Root directory %s removed.", d
->path
);
1751 log_debug("Directory %s removed.", d
->path
);
1757 static int add_search_paths(sd_journal
*j
) {
1759 static const char search_paths
[] =
1760 "/run/log/journal\0"
1761 "/var/log/journal\0";
1766 /* We ignore most errors here, since the idea is to only open
1767 * what's actually accessible, and ignore the rest. */
1769 NULSTR_FOREACH(p
, search_paths
)
1770 (void) add_root_directory(j
, p
, true);
1772 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
1773 (void) add_root_directory(j
, "/var/log/journal/remote", true);
1778 static int add_current_paths(sd_journal
*j
) {
1783 assert(j
->no_new_files
);
1785 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1786 * treat them as fatal. */
1788 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
1789 _cleanup_free_
char *dir
;
1792 dir
= dirname_malloc(f
->path
);
1796 r
= add_directory(j
, dir
, NULL
);
1804 static int allocate_inotify(sd_journal
*j
) {
1807 if (j
->inotify_fd
< 0) {
1808 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1809 if (j
->inotify_fd
< 0)
1813 return hashmap_ensure_allocated(&j
->directories_by_wd
, NULL
);
1816 static sd_journal
*journal_new(int flags
, const char *path
) {
1817 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1819 j
= new0(sd_journal
, 1);
1823 j
->original_pid
= getpid_cached();
1824 j
->toplevel_fd
= -1;
1827 j
->data_threshold
= DEFAULT_DATA_THRESHOLD
;
1836 if (flags
& SD_JOURNAL_OS_ROOT
)
1842 j
->files
= ordered_hashmap_new(&path_hash_ops
);
1846 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
1847 j
->directories_by_path
= hashmap_new(&path_hash_ops
);
1848 j
->mmap
= mmap_cache_new();
1849 if (!j
->files_cache
|| !j
->directories_by_path
|| !j
->mmap
)
1855 #define OPEN_ALLOWED_FLAGS \
1856 (SD_JOURNAL_LOCAL_ONLY | \
1857 SD_JOURNAL_RUNTIME_ONLY | \
1858 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)
1860 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1861 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1864 assert_return(ret
, -EINVAL
);
1865 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
1867 j
= journal_new(flags
, NULL
);
1871 r
= add_search_paths(j
);
1879 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1880 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1882 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1883 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1884 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1888 /* This is pretty much deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1889 * combination with sd_journal_open_directory_fd(). */
1891 assert_return(machine
, -EINVAL
);
1892 assert_return(ret
, -EINVAL
);
1893 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
1894 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1896 p
= strjoina("/run/systemd/machines/", machine
);
1897 r
= parse_env_file(p
, NEWLINE
, "ROOT", &root
, "CLASS", &class, NULL
);
1905 if (!streq_ptr(class, "container"))
1908 j
= journal_new(flags
, root
);
1912 r
= add_search_paths(j
);
1920 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
1921 (SD_JOURNAL_OS_ROOT | \
1922 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1924 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
1925 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1928 assert_return(ret
, -EINVAL
);
1929 assert_return(path
, -EINVAL
);
1930 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
1932 j
= journal_new(flags
, path
);
1936 if (flags
& SD_JOURNAL_OS_ROOT
)
1937 r
= add_search_paths(j
);
1939 r
= add_root_directory(j
, path
, false);
1947 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
1948 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1952 assert_return(ret
, -EINVAL
);
1953 assert_return(flags
== 0, -EINVAL
);
1955 j
= journal_new(flags
, NULL
);
1959 STRV_FOREACH(path
, paths
) {
1960 r
= add_any_file(j
, -1, *path
);
1965 j
->no_new_files
= true;
1971 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
1972 (SD_JOURNAL_OS_ROOT | \
1973 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1975 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
1976 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1980 assert_return(ret
, -EINVAL
);
1981 assert_return(fd
>= 0, -EBADF
);
1982 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
1984 if (fstat(fd
, &st
) < 0)
1987 if (!S_ISDIR(st
.st_mode
))
1990 j
= journal_new(flags
, NULL
);
1994 j
->toplevel_fd
= fd
;
1996 if (flags
& SD_JOURNAL_OS_ROOT
)
1997 r
= add_search_paths(j
);
1999 r
= add_root_directory(j
, NULL
, false);
2007 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2010 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2014 assert_return(ret
, -EINVAL
);
2015 assert_return(n_fds
> 0, -EBADF
);
2016 assert_return(flags
== 0, -EINVAL
);
2018 j
= journal_new(flags
, NULL
);
2022 for (i
= 0; i
< n_fds
; i
++) {
2030 if (fstat(fds
[i
], &st
) < 0) {
2035 r
= stat_verify_regular(&st
);
2039 r
= add_any_file(j
, fds
[i
], NULL
);
2044 j
->no_new_files
= true;
2045 j
->no_inotify
= true;
2051 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2053 ORDERED_HASHMAP_FOREACH(f
, j
->files
, iterator
)
2054 f
->close_fd
= false;
2059 _public_
void sd_journal_close(sd_journal
*j
) {
2065 sd_journal_flush_matches(j
);
2067 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2068 iterated_cache_free(j
->files_cache
);
2070 while ((d
= hashmap_first(j
->directories_by_path
)))
2071 remove_directory(j
, d
);
2073 while ((d
= hashmap_first(j
->directories_by_wd
)))
2074 remove_directory(j
, d
);
2076 hashmap_free(j
->directories_by_path
);
2077 hashmap_free(j
->directories_by_wd
);
2079 safe_close(j
->inotify_fd
);
2082 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j
->mmap
), mmap_cache_get_missed(j
->mmap
));
2083 mmap_cache_unref(j
->mmap
);
2086 hashmap_free_free(j
->errors
);
2090 free(j
->unique_field
);
2091 free(j
->fields_buffer
);
2095 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2100 assert_return(j
, -EINVAL
);
2101 assert_return(!journal_pid_changed(j
), -ECHILD
);
2102 assert_return(ret
, -EINVAL
);
2104 f
= j
->current_file
;
2106 return -EADDRNOTAVAIL
;
2108 if (f
->current_offset
<= 0)
2109 return -EADDRNOTAVAIL
;
2111 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2115 *ret
= le64toh(o
->entry
.realtime
);
2119 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2125 assert_return(j
, -EINVAL
);
2126 assert_return(!journal_pid_changed(j
), -ECHILD
);
2128 f
= j
->current_file
;
2130 return -EADDRNOTAVAIL
;
2132 if (f
->current_offset
<= 0)
2133 return -EADDRNOTAVAIL
;
2135 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2140 *ret_boot_id
= o
->entry
.boot_id
;
2142 r
= sd_id128_get_boot(&id
);
2146 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2151 *ret
= le64toh(o
->entry
.monotonic
);
2156 static bool field_is_valid(const char *field
) {
2164 if (startswith(field
, "__"))
2167 for (p
= field
; *p
; p
++) {
2172 if (*p
>= 'A' && *p
<= 'Z')
2175 if (*p
>= '0' && *p
<= '9')
2184 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2187 size_t field_length
;
2191 assert_return(j
, -EINVAL
);
2192 assert_return(!journal_pid_changed(j
), -ECHILD
);
2193 assert_return(field
, -EINVAL
);
2194 assert_return(data
, -EINVAL
);
2195 assert_return(size
, -EINVAL
);
2196 assert_return(field_is_valid(field
), -EINVAL
);
2198 f
= j
->current_file
;
2200 return -EADDRNOTAVAIL
;
2202 if (f
->current_offset
<= 0)
2203 return -EADDRNOTAVAIL
;
2205 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2209 field_length
= strlen(field
);
2211 n
= journal_file_entry_n_items(o
);
2212 for (i
= 0; i
< n
; i
++) {
2218 p
= le64toh(o
->entry
.items
[i
].object_offset
);
2219 le_hash
= o
->entry
.items
[i
].hash
;
2220 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2224 if (le_hash
!= o
->data
.hash
)
2227 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2229 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2231 #if HAVE_XZ || HAVE_LZ4
2232 r
= decompress_startswith(compression
,
2234 &f
->compress_buffer
, &f
->compress_buffer_size
,
2235 field
, field_length
, '=');
2237 log_debug_errno(r
, "Cannot decompress %s object of length %"PRIu64
" at offset "OFSfmt
": %m",
2238 object_compressed_to_string(compression
), l
, p
);
2243 r
= decompress_blob(compression
,
2245 &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
,
2250 *data
= f
->compress_buffer
;
2251 *size
= (size_t) rsize
;
2256 return -EPROTONOSUPPORT
;
2258 } else if (l
>= field_length
+1 &&
2259 memcmp(o
->data
.payload
, field
, field_length
) == 0 &&
2260 o
->data
.payload
[field_length
] == '=') {
2264 if ((uint64_t) t
!= l
)
2267 *data
= o
->data
.payload
;
2273 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2281 static int return_data(sd_journal
*j
, JournalFile
*f
, Object
*o
, const void **data
, size_t *size
) {
2286 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2289 /* We can't read objects larger than 4G on a 32bit machine */
2290 if ((uint64_t) t
!= l
)
2293 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2295 #if HAVE_XZ || HAVE_LZ4
2299 r
= decompress_blob(compression
,
2300 o
->data
.payload
, l
, &f
->compress_buffer
,
2301 &f
->compress_buffer_size
, &rsize
, j
->data_threshold
);
2305 *data
= f
->compress_buffer
;
2306 *size
= (size_t) rsize
;
2308 return -EPROTONOSUPPORT
;
2311 *data
= o
->data
.payload
;
2318 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2325 assert_return(j
, -EINVAL
);
2326 assert_return(!journal_pid_changed(j
), -ECHILD
);
2327 assert_return(data
, -EINVAL
);
2328 assert_return(size
, -EINVAL
);
2330 f
= j
->current_file
;
2332 return -EADDRNOTAVAIL
;
2334 if (f
->current_offset
<= 0)
2335 return -EADDRNOTAVAIL
;
2337 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2341 n
= journal_file_entry_n_items(o
);
2342 if (j
->current_field
>= n
)
2345 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2346 le_hash
= o
->entry
.items
[j
->current_field
].hash
;
2347 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2351 if (le_hash
!= o
->data
.hash
)
2354 r
= return_data(j
, f
, o
, data
, size
);
2363 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2367 j
->current_field
= 0;
2370 static int reiterate_all_paths(sd_journal
*j
) {
2373 if (j
->no_new_files
)
2374 return add_current_paths(j
);
2376 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2377 return add_search_paths(j
);
2379 if (j
->toplevel_fd
>= 0)
2380 return add_root_directory(j
, NULL
, false);
2383 return add_root_directory(j
, j
->path
, true);
2385 return add_search_paths(j
);
2388 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2391 assert_return(j
, -EINVAL
);
2392 assert_return(!journal_pid_changed(j
), -ECHILD
);
2395 return -EMEDIUMTYPE
;
2397 if (j
->inotify_fd
>= 0)
2398 return j
->inotify_fd
;
2400 r
= allocate_inotify(j
);
2404 log_debug("Reiterating files to get inotify watches established.");
2406 /* Iterate through all dirs again, to add them to the inotify */
2407 r
= reiterate_all_paths(j
);
2411 return j
->inotify_fd
;
2414 _public_
int sd_journal_get_events(sd_journal
*j
) {
2417 assert_return(j
, -EINVAL
);
2418 assert_return(!journal_pid_changed(j
), -ECHILD
);
2420 fd
= sd_journal_get_fd(j
);
2427 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2430 assert_return(j
, -EINVAL
);
2431 assert_return(!journal_pid_changed(j
), -ECHILD
);
2432 assert_return(timeout_usec
, -EINVAL
);
2434 fd
= sd_journal_get_fd(j
);
2438 if (!j
->on_network
) {
2439 *timeout_usec
= (uint64_t) -1;
2443 /* If we are on the network we need to regularly check for
2444 * changes manually */
2446 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2450 static void process_q_overflow(sd_journal
*j
) {
2457 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2458 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2459 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2460 * are subject for unloading. */
2462 log_debug("Inotify queue overrun, reiterating everything.");
2465 (void) reiterate_all_paths(j
);
2467 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2469 if (f
->last_seen_generation
== j
->generation
)
2472 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2473 remove_file_real(j
, f
);
2476 HASHMAP_FOREACH(m
, j
->directories_by_path
, i
) {
2478 if (m
->last_seen_generation
== j
->generation
)
2481 if (m
->is_root
) /* Never GC root directories */
2484 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2485 remove_directory(j
, m
);
2488 log_debug("Reiteration complete.");
2491 static void process_inotify_event(sd_journal
*j
, struct inotify_event
*e
) {
2497 if (e
->mask
& IN_Q_OVERFLOW
) {
2498 process_q_overflow(j
);
2502 /* Is this a subdirectory we watch? */
2503 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2505 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2506 (endswith(e
->name
, ".journal") ||
2507 endswith(e
->name
, ".journal~"))) {
2509 /* Event for a journal file */
2511 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2512 (void) add_file_by_name(j
, d
->path
, e
->name
);
2513 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2514 remove_file_by_name(j
, d
->path
, e
->name
);
2516 } else if (!d
->is_root
&& e
->len
== 0) {
2518 /* Event for a subdirectory */
2520 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2521 remove_directory(j
, d
);
2523 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2525 /* Event for root directory */
2527 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2528 (void) add_directory(j
, d
->path
, e
->name
);
2534 if (e
->mask
& IN_IGNORED
)
2537 log_debug("Unexpected inotify event.");
2540 static int determine_change(sd_journal
*j
) {
2545 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2546 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2548 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2551 _public_
int sd_journal_process(sd_journal
*j
) {
2552 bool got_something
= false;
2554 assert_return(j
, -EINVAL
);
2555 assert_return(!journal_pid_changed(j
), -ECHILD
);
2557 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
2560 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2561 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2564 union inotify_event_buffer buffer
;
2565 struct inotify_event
*e
;
2568 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2570 if (IN_SET(errno
, EAGAIN
, EINTR
))
2571 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2576 got_something
= true;
2578 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2579 process_inotify_event(j
, e
);
2583 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2587 assert_return(j
, -EINVAL
);
2588 assert_return(!journal_pid_changed(j
), -ECHILD
);
2590 if (j
->inotify_fd
< 0) {
2592 /* This is the first invocation, hence create the
2594 r
= sd_journal_get_fd(j
);
2598 /* The journal might have changed since the context
2599 * object was created and we weren't watching before,
2600 * hence don't wait for anything, and return
2602 return determine_change(j
);
2605 r
= sd_journal_get_timeout(j
, &t
);
2609 if (t
!= (uint64_t) -1) {
2612 n
= now(CLOCK_MONOTONIC
);
2613 t
= t
> n
? t
- n
: 0;
2615 if (timeout_usec
== (uint64_t) -1 || timeout_usec
> t
)
2620 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2621 } while (r
== -EINTR
);
2626 return sd_journal_process(j
);
2629 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2633 uint64_t fmin
= 0, tmax
= 0;
2636 assert_return(j
, -EINVAL
);
2637 assert_return(!journal_pid_changed(j
), -ECHILD
);
2638 assert_return(from
|| to
, -EINVAL
);
2639 assert_return(from
!= to
, -EINVAL
);
2641 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2644 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2657 fmin
= MIN(fr
, fmin
);
2658 tmax
= MAX(t
, tmax
);
2667 return first
? 0 : 1;
2670 _public_
int sd_journal_get_cutoff_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t *from
, uint64_t *to
) {
2676 assert_return(j
, -EINVAL
);
2677 assert_return(!journal_pid_changed(j
), -ECHILD
);
2678 assert_return(from
|| to
, -EINVAL
);
2679 assert_return(from
!= to
, -EINVAL
);
2681 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2684 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &fr
, &t
);
2694 *from
= MIN(fr
, *from
);
2709 void journal_print_header(sd_journal
*j
) {
2712 bool newline
= false;
2716 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2722 journal_file_print_header(f
);
2726 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *bytes
) {
2731 assert_return(j
, -EINVAL
);
2732 assert_return(!journal_pid_changed(j
), -ECHILD
);
2733 assert_return(bytes
, -EINVAL
);
2735 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2738 if (fstat(f
->fd
, &st
) < 0)
2741 sum
+= (uint64_t) st
.st_blocks
* 512ULL;
2748 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2751 assert_return(j
, -EINVAL
);
2752 assert_return(!journal_pid_changed(j
), -ECHILD
);
2753 assert_return(!isempty(field
), -EINVAL
);
2754 assert_return(field_is_valid(field
), -EINVAL
);
2760 free(j
->unique_field
);
2761 j
->unique_field
= f
;
2762 j
->unique_file
= NULL
;
2763 j
->unique_offset
= 0;
2764 j
->unique_file_lost
= false;
2769 _public_
int sd_journal_enumerate_unique(sd_journal
*j
, const void **data
, size_t *l
) {
2772 assert_return(j
, -EINVAL
);
2773 assert_return(!journal_pid_changed(j
), -ECHILD
);
2774 assert_return(data
, -EINVAL
);
2775 assert_return(l
, -EINVAL
);
2776 assert_return(j
->unique_field
, -EINVAL
);
2778 k
= strlen(j
->unique_field
);
2780 if (!j
->unique_file
) {
2781 if (j
->unique_file_lost
)
2784 j
->unique_file
= ordered_hashmap_first(j
->files
);
2785 if (!j
->unique_file
)
2788 j
->unique_offset
= 0;
2800 /* Proceed to next data object in the field's linked list */
2801 if (j
->unique_offset
== 0) {
2802 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2806 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2808 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2812 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2815 /* We reached the end of the list? Then start again, with the next file */
2816 if (j
->unique_offset
== 0) {
2817 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2818 if (!j
->unique_file
)
2824 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2825 * instead, so that we can look at this data object at the same
2826 * time as one on another file */
2827 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2831 /* Let's do the type check by hand, since we used 0 context above. */
2832 if (o
->object
.type
!= OBJECT_DATA
) {
2833 log_debug("%s:offset " OFSfmt
": object has type %d, expected %d",
2834 j
->unique_file
->path
, j
->unique_offset
,
2835 o
->object
.type
, OBJECT_DATA
);
2839 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2843 /* Check if we have at least the field name and "=". */
2845 log_debug("%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2846 j
->unique_file
->path
, j
->unique_offset
,
2851 if (memcmp(odata
, j
->unique_field
, k
) || ((const char*) odata
)[k
] != '=') {
2852 log_debug("%s:offset " OFSfmt
": object does not start with \"%s=\"",
2853 j
->unique_file
->path
, j
->unique_offset
,
2858 /* OK, now let's see if we already returned this data
2859 * object by checking if it exists in the earlier
2860 * traversed files. */
2862 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2863 if (of
== j
->unique_file
)
2866 /* Skip this file it didn't have any fields indexed */
2867 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2870 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
2882 r
= return_data(j
, j
->unique_file
, o
, data
, l
);
2890 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
2894 j
->unique_file
= NULL
;
2895 j
->unique_offset
= 0;
2896 j
->unique_file_lost
= false;
2899 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
2902 assert_return(j
, -EINVAL
);
2903 assert_return(!journal_pid_changed(j
), -ECHILD
);
2904 assert_return(field
, -EINVAL
);
2906 if (!j
->fields_file
) {
2907 if (j
->fields_file_lost
)
2910 j
->fields_file
= ordered_hashmap_first(j
->files
);
2911 if (!j
->fields_file
)
2914 j
->fields_hash_table_index
= 0;
2915 j
->fields_offset
= 0;
2919 JournalFile
*f
, *of
;
2928 if (j
->fields_offset
== 0) {
2931 /* We are not yet positioned at any field. Let's pick the first one */
2932 r
= journal_file_map_field_hash_table(f
);
2936 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
2938 if (j
->fields_hash_table_index
>= m
) {
2939 /* Reached the end of the hash table, go to the next file. */
2944 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
2946 if (j
->fields_offset
!= 0)
2949 /* Empty hash table bucket, go to next one */
2950 j
->fields_hash_table_index
++;
2954 /* Proceed with next file */
2955 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
2956 if (!j
->fields_file
) {
2961 j
->fields_offset
= 0;
2962 j
->fields_hash_table_index
= 0;
2967 /* We are already positioned at a field. If so, let's figure out the next field from it */
2969 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
2973 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
2974 if (j
->fields_offset
== 0) {
2975 /* Reached the end of the hash table chain */
2976 j
->fields_hash_table_index
++;
2981 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
2982 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
2986 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
2987 if (o
->object
.type
!= OBJECT_FIELD
) {
2988 log_debug("%s:offset " OFSfmt
": object has type %i, expected %i", f
->path
, j
->fields_offset
, o
->object
.type
, OBJECT_FIELD
);
2992 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
2994 /* Let's see if we already returned this field name before. */
2996 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
3000 /* Skip this file it didn't have any fields indexed */
3001 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3004 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
, le64toh(o
->field
.hash
), NULL
, NULL
);
3016 /* Check if this is really a valid string containing no NUL byte */
3017 if (memchr(o
->field
.payload
, 0, sz
))
3020 if (sz
> j
->data_threshold
)
3021 sz
= j
->data_threshold
;
3023 if (!GREEDY_REALLOC(j
->fields_buffer
, j
->fields_buffer_allocated
, sz
+ 1))
3026 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3027 j
->fields_buffer
[sz
] = 0;
3029 if (!field_is_valid(j
->fields_buffer
))
3032 *field
= j
->fields_buffer
;
3037 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3041 j
->fields_file
= NULL
;
3042 j
->fields_hash_table_index
= 0;
3043 j
->fields_offset
= 0;
3044 j
->fields_file_lost
= false;
3047 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3048 assert_return(j
, -EINVAL
);
3049 assert_return(!journal_pid_changed(j
), -ECHILD
);
3051 return !j
->on_network
;
3054 static char *lookup_field(const char *field
, void *userdata
) {
3055 sd_journal
*j
= userdata
;
3063 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3065 size
> REPLACE_VAR_MAX
)
3066 return strdup(field
);
3068 d
= strlen(field
) + 1;
3070 return strndup((const char*) data
+ d
, size
- d
);
3073 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3077 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3081 assert_return(j
, -EINVAL
);
3082 assert_return(!journal_pid_changed(j
), -ECHILD
);
3083 assert_return(ret
, -EINVAL
);
3085 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3089 cid
= strndup((const char*) data
+ 11, size
- 11);
3093 r
= sd_id128_from_string(cid
, &id
);
3097 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
3101 t
= replace_var(text
, lookup_field
, j
);
3109 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3110 assert_return(ret
, -EINVAL
);
3112 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3115 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3116 assert_return(j
, -EINVAL
);
3117 assert_return(!journal_pid_changed(j
), -ECHILD
);
3119 j
->data_threshold
= sz
;
3123 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3124 assert_return(j
, -EINVAL
);
3125 assert_return(!journal_pid_changed(j
), -ECHILD
);
3126 assert_return(sz
, -EINVAL
);
3128 *sz
= j
->data_threshold
;
3132 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3133 assert_return(j
, -EINVAL
);
3135 return j
->has_runtime_files
;
3138 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3139 assert_return(j
, -EINVAL
);
3141 return j
->has_persistent_files
;