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
) {
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 sd_journal_close(j
);
1859 #define OPEN_ALLOWED_FLAGS \
1860 (SD_JOURNAL_LOCAL_ONLY | \
1861 SD_JOURNAL_RUNTIME_ONLY | \
1862 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)
1864 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1868 assert_return(ret
, -EINVAL
);
1869 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
1871 j
= journal_new(flags
, NULL
);
1875 r
= add_search_paths(j
);
1883 sd_journal_close(j
);
1888 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1889 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1891 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1892 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1897 /* This is pretty much deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1898 * combination with sd_journal_open_directory_fd(). */
1900 assert_return(machine
, -EINVAL
);
1901 assert_return(ret
, -EINVAL
);
1902 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
1903 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1905 p
= strjoina("/run/systemd/machines/", machine
);
1906 r
= parse_env_file(p
, NEWLINE
, "ROOT", &root
, "CLASS", &class, NULL
);
1914 if (!streq_ptr(class, "container"))
1917 j
= journal_new(flags
, root
);
1921 r
= add_search_paths(j
);
1929 sd_journal_close(j
);
1933 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
1934 (SD_JOURNAL_OS_ROOT | \
1935 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1937 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
1941 assert_return(ret
, -EINVAL
);
1942 assert_return(path
, -EINVAL
);
1943 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
1945 j
= journal_new(flags
, path
);
1949 if (flags
& SD_JOURNAL_OS_ROOT
)
1950 r
= add_search_paths(j
);
1952 r
= add_root_directory(j
, path
, false);
1960 sd_journal_close(j
);
1964 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
1969 assert_return(ret
, -EINVAL
);
1970 assert_return(flags
== 0, -EINVAL
);
1972 j
= journal_new(flags
, NULL
);
1976 STRV_FOREACH(path
, paths
) {
1977 r
= add_any_file(j
, -1, *path
);
1982 j
->no_new_files
= true;
1988 sd_journal_close(j
);
1992 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
1993 (SD_JOURNAL_OS_ROOT | \
1994 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1996 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
2001 assert_return(ret
, -EINVAL
);
2002 assert_return(fd
>= 0, -EBADF
);
2003 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2005 if (fstat(fd
, &st
) < 0)
2008 if (!S_ISDIR(st
.st_mode
))
2011 j
= journal_new(flags
, NULL
);
2015 j
->toplevel_fd
= fd
;
2017 if (flags
& SD_JOURNAL_OS_ROOT
)
2018 r
= add_search_paths(j
);
2020 r
= add_root_directory(j
, NULL
, false);
2028 sd_journal_close(j
);
2032 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2039 assert_return(ret
, -EINVAL
);
2040 assert_return(n_fds
> 0, -EBADF
);
2041 assert_return(flags
== 0, -EINVAL
);
2043 j
= journal_new(flags
, NULL
);
2047 for (i
= 0; i
< n_fds
; i
++) {
2055 if (fstat(fds
[i
], &st
) < 0) {
2060 r
= stat_verify_regular(&st
);
2064 r
= add_any_file(j
, fds
[i
], NULL
);
2069 j
->no_new_files
= true;
2070 j
->no_inotify
= true;
2076 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2078 ORDERED_HASHMAP_FOREACH(f
, j
->files
, iterator
)
2079 f
->close_fd
= false;
2081 sd_journal_close(j
);
2085 _public_
void sd_journal_close(sd_journal
*j
) {
2091 sd_journal_flush_matches(j
);
2093 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2094 iterated_cache_free(j
->files_cache
);
2096 while ((d
= hashmap_first(j
->directories_by_path
)))
2097 remove_directory(j
, d
);
2099 while ((d
= hashmap_first(j
->directories_by_wd
)))
2100 remove_directory(j
, d
);
2102 hashmap_free(j
->directories_by_path
);
2103 hashmap_free(j
->directories_by_wd
);
2105 safe_close(j
->inotify_fd
);
2108 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j
->mmap
), mmap_cache_get_missed(j
->mmap
));
2109 mmap_cache_unref(j
->mmap
);
2112 hashmap_free_free(j
->errors
);
2116 free(j
->unique_field
);
2117 free(j
->fields_buffer
);
2121 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2126 assert_return(j
, -EINVAL
);
2127 assert_return(!journal_pid_changed(j
), -ECHILD
);
2128 assert_return(ret
, -EINVAL
);
2130 f
= j
->current_file
;
2132 return -EADDRNOTAVAIL
;
2134 if (f
->current_offset
<= 0)
2135 return -EADDRNOTAVAIL
;
2137 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2141 *ret
= le64toh(o
->entry
.realtime
);
2145 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2151 assert_return(j
, -EINVAL
);
2152 assert_return(!journal_pid_changed(j
), -ECHILD
);
2154 f
= j
->current_file
;
2156 return -EADDRNOTAVAIL
;
2158 if (f
->current_offset
<= 0)
2159 return -EADDRNOTAVAIL
;
2161 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2166 *ret_boot_id
= o
->entry
.boot_id
;
2168 r
= sd_id128_get_boot(&id
);
2172 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2177 *ret
= le64toh(o
->entry
.monotonic
);
2182 static bool field_is_valid(const char *field
) {
2190 if (startswith(field
, "__"))
2193 for (p
= field
; *p
; p
++) {
2198 if (*p
>= 'A' && *p
<= 'Z')
2201 if (*p
>= '0' && *p
<= '9')
2210 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2213 size_t field_length
;
2217 assert_return(j
, -EINVAL
);
2218 assert_return(!journal_pid_changed(j
), -ECHILD
);
2219 assert_return(field
, -EINVAL
);
2220 assert_return(data
, -EINVAL
);
2221 assert_return(size
, -EINVAL
);
2222 assert_return(field_is_valid(field
), -EINVAL
);
2224 f
= j
->current_file
;
2226 return -EADDRNOTAVAIL
;
2228 if (f
->current_offset
<= 0)
2229 return -EADDRNOTAVAIL
;
2231 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2235 field_length
= strlen(field
);
2237 n
= journal_file_entry_n_items(o
);
2238 for (i
= 0; i
< n
; i
++) {
2244 p
= le64toh(o
->entry
.items
[i
].object_offset
);
2245 le_hash
= o
->entry
.items
[i
].hash
;
2246 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2250 if (le_hash
!= o
->data
.hash
)
2253 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2255 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2257 #if HAVE_XZ || HAVE_LZ4
2258 r
= decompress_startswith(compression
,
2260 &f
->compress_buffer
, &f
->compress_buffer_size
,
2261 field
, field_length
, '=');
2263 log_debug_errno(r
, "Cannot decompress %s object of length %"PRIu64
" at offset "OFSfmt
": %m",
2264 object_compressed_to_string(compression
), l
, p
);
2269 r
= decompress_blob(compression
,
2271 &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
,
2276 *data
= f
->compress_buffer
;
2277 *size
= (size_t) rsize
;
2282 return -EPROTONOSUPPORT
;
2284 } else if (l
>= field_length
+1 &&
2285 memcmp(o
->data
.payload
, field
, field_length
) == 0 &&
2286 o
->data
.payload
[field_length
] == '=') {
2290 if ((uint64_t) t
!= l
)
2293 *data
= o
->data
.payload
;
2299 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2307 static int return_data(sd_journal
*j
, JournalFile
*f
, Object
*o
, const void **data
, size_t *size
) {
2312 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2315 /* We can't read objects larger than 4G on a 32bit machine */
2316 if ((uint64_t) t
!= l
)
2319 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2321 #if HAVE_XZ || HAVE_LZ4
2325 r
= decompress_blob(compression
,
2326 o
->data
.payload
, l
, &f
->compress_buffer
,
2327 &f
->compress_buffer_size
, &rsize
, j
->data_threshold
);
2331 *data
= f
->compress_buffer
;
2332 *size
= (size_t) rsize
;
2334 return -EPROTONOSUPPORT
;
2337 *data
= o
->data
.payload
;
2344 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2351 assert_return(j
, -EINVAL
);
2352 assert_return(!journal_pid_changed(j
), -ECHILD
);
2353 assert_return(data
, -EINVAL
);
2354 assert_return(size
, -EINVAL
);
2356 f
= j
->current_file
;
2358 return -EADDRNOTAVAIL
;
2360 if (f
->current_offset
<= 0)
2361 return -EADDRNOTAVAIL
;
2363 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2367 n
= journal_file_entry_n_items(o
);
2368 if (j
->current_field
>= n
)
2371 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2372 le_hash
= o
->entry
.items
[j
->current_field
].hash
;
2373 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2377 if (le_hash
!= o
->data
.hash
)
2380 r
= return_data(j
, f
, o
, data
, size
);
2389 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2393 j
->current_field
= 0;
2396 static int reiterate_all_paths(sd_journal
*j
) {
2399 if (j
->no_new_files
)
2400 return add_current_paths(j
);
2402 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2403 return add_search_paths(j
);
2405 if (j
->toplevel_fd
>= 0)
2406 return add_root_directory(j
, NULL
, false);
2409 return add_root_directory(j
, j
->path
, true);
2411 return add_search_paths(j
);
2414 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2417 assert_return(j
, -EINVAL
);
2418 assert_return(!journal_pid_changed(j
), -ECHILD
);
2421 return -EMEDIUMTYPE
;
2423 if (j
->inotify_fd
>= 0)
2424 return j
->inotify_fd
;
2426 r
= allocate_inotify(j
);
2430 log_debug("Reiterating files to get inotify watches established.");
2432 /* Iterate through all dirs again, to add them to the inotify */
2433 r
= reiterate_all_paths(j
);
2437 return j
->inotify_fd
;
2440 _public_
int sd_journal_get_events(sd_journal
*j
) {
2443 assert_return(j
, -EINVAL
);
2444 assert_return(!journal_pid_changed(j
), -ECHILD
);
2446 fd
= sd_journal_get_fd(j
);
2453 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2456 assert_return(j
, -EINVAL
);
2457 assert_return(!journal_pid_changed(j
), -ECHILD
);
2458 assert_return(timeout_usec
, -EINVAL
);
2460 fd
= sd_journal_get_fd(j
);
2464 if (!j
->on_network
) {
2465 *timeout_usec
= (uint64_t) -1;
2469 /* If we are on the network we need to regularly check for
2470 * changes manually */
2472 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2476 static void process_q_overflow(sd_journal
*j
) {
2483 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2484 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2485 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2486 * are subject for unloading. */
2488 log_debug("Inotify queue overrun, reiterating everything.");
2491 (void) reiterate_all_paths(j
);
2493 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2495 if (f
->last_seen_generation
== j
->generation
)
2498 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2499 remove_file_real(j
, f
);
2502 HASHMAP_FOREACH(m
, j
->directories_by_path
, i
) {
2504 if (m
->last_seen_generation
== j
->generation
)
2507 if (m
->is_root
) /* Never GC root directories */
2510 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2511 remove_directory(j
, m
);
2514 log_debug("Reiteration complete.");
2517 static void process_inotify_event(sd_journal
*j
, struct inotify_event
*e
) {
2523 if (e
->mask
& IN_Q_OVERFLOW
) {
2524 process_q_overflow(j
);
2528 /* Is this a subdirectory we watch? */
2529 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2531 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2532 (endswith(e
->name
, ".journal") ||
2533 endswith(e
->name
, ".journal~"))) {
2535 /* Event for a journal file */
2537 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2538 (void) add_file_by_name(j
, d
->path
, e
->name
);
2539 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2540 remove_file_by_name(j
, d
->path
, e
->name
);
2542 } else if (!d
->is_root
&& e
->len
== 0) {
2544 /* Event for a subdirectory */
2546 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2547 remove_directory(j
, d
);
2549 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2551 /* Event for root directory */
2553 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2554 (void) add_directory(j
, d
->path
, e
->name
);
2560 if (e
->mask
& IN_IGNORED
)
2563 log_debug("Unexpected inotify event.");
2566 static int determine_change(sd_journal
*j
) {
2571 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2572 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2574 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2577 _public_
int sd_journal_process(sd_journal
*j
) {
2578 bool got_something
= false;
2580 assert_return(j
, -EINVAL
);
2581 assert_return(!journal_pid_changed(j
), -ECHILD
);
2583 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
2586 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2587 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2590 union inotify_event_buffer buffer
;
2591 struct inotify_event
*e
;
2594 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2596 if (IN_SET(errno
, EAGAIN
, EINTR
))
2597 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2602 got_something
= true;
2604 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2605 process_inotify_event(j
, e
);
2609 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2613 assert_return(j
, -EINVAL
);
2614 assert_return(!journal_pid_changed(j
), -ECHILD
);
2616 if (j
->inotify_fd
< 0) {
2618 /* This is the first invocation, hence create the
2620 r
= sd_journal_get_fd(j
);
2624 /* The journal might have changed since the context
2625 * object was created and we weren't watching before,
2626 * hence don't wait for anything, and return
2628 return determine_change(j
);
2631 r
= sd_journal_get_timeout(j
, &t
);
2635 if (t
!= (uint64_t) -1) {
2638 n
= now(CLOCK_MONOTONIC
);
2639 t
= t
> n
? t
- n
: 0;
2641 if (timeout_usec
== (uint64_t) -1 || timeout_usec
> t
)
2646 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2647 } while (r
== -EINTR
);
2652 return sd_journal_process(j
);
2655 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2659 uint64_t fmin
= 0, tmax
= 0;
2662 assert_return(j
, -EINVAL
);
2663 assert_return(!journal_pid_changed(j
), -ECHILD
);
2664 assert_return(from
|| to
, -EINVAL
);
2665 assert_return(from
!= to
, -EINVAL
);
2667 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2670 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2683 fmin
= MIN(fr
, fmin
);
2684 tmax
= MAX(t
, tmax
);
2693 return first
? 0 : 1;
2696 _public_
int sd_journal_get_cutoff_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t *from
, uint64_t *to
) {
2702 assert_return(j
, -EINVAL
);
2703 assert_return(!journal_pid_changed(j
), -ECHILD
);
2704 assert_return(from
|| to
, -EINVAL
);
2705 assert_return(from
!= to
, -EINVAL
);
2707 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2710 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &fr
, &t
);
2720 *from
= MIN(fr
, *from
);
2735 void journal_print_header(sd_journal
*j
) {
2738 bool newline
= false;
2742 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2748 journal_file_print_header(f
);
2752 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *bytes
) {
2757 assert_return(j
, -EINVAL
);
2758 assert_return(!journal_pid_changed(j
), -ECHILD
);
2759 assert_return(bytes
, -EINVAL
);
2761 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2764 if (fstat(f
->fd
, &st
) < 0)
2767 sum
+= (uint64_t) st
.st_blocks
* 512ULL;
2774 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2777 assert_return(j
, -EINVAL
);
2778 assert_return(!journal_pid_changed(j
), -ECHILD
);
2779 assert_return(!isempty(field
), -EINVAL
);
2780 assert_return(field_is_valid(field
), -EINVAL
);
2786 free(j
->unique_field
);
2787 j
->unique_field
= f
;
2788 j
->unique_file
= NULL
;
2789 j
->unique_offset
= 0;
2790 j
->unique_file_lost
= false;
2795 _public_
int sd_journal_enumerate_unique(sd_journal
*j
, const void **data
, size_t *l
) {
2798 assert_return(j
, -EINVAL
);
2799 assert_return(!journal_pid_changed(j
), -ECHILD
);
2800 assert_return(data
, -EINVAL
);
2801 assert_return(l
, -EINVAL
);
2802 assert_return(j
->unique_field
, -EINVAL
);
2804 k
= strlen(j
->unique_field
);
2806 if (!j
->unique_file
) {
2807 if (j
->unique_file_lost
)
2810 j
->unique_file
= ordered_hashmap_first(j
->files
);
2811 if (!j
->unique_file
)
2814 j
->unique_offset
= 0;
2826 /* Proceed to next data object in the field's linked list */
2827 if (j
->unique_offset
== 0) {
2828 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2832 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2834 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2838 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2841 /* We reached the end of the list? Then start again, with the next file */
2842 if (j
->unique_offset
== 0) {
2843 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2844 if (!j
->unique_file
)
2850 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2851 * instead, so that we can look at this data object at the same
2852 * time as one on another file */
2853 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2857 /* Let's do the type check by hand, since we used 0 context above. */
2858 if (o
->object
.type
!= OBJECT_DATA
) {
2859 log_debug("%s:offset " OFSfmt
": object has type %d, expected %d",
2860 j
->unique_file
->path
, j
->unique_offset
,
2861 o
->object
.type
, OBJECT_DATA
);
2865 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2869 /* Check if we have at least the field name and "=". */
2871 log_debug("%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2872 j
->unique_file
->path
, j
->unique_offset
,
2877 if (memcmp(odata
, j
->unique_field
, k
) || ((const char*) odata
)[k
] != '=') {
2878 log_debug("%s:offset " OFSfmt
": object does not start with \"%s=\"",
2879 j
->unique_file
->path
, j
->unique_offset
,
2884 /* OK, now let's see if we already returned this data
2885 * object by checking if it exists in the earlier
2886 * traversed files. */
2888 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2889 if (of
== j
->unique_file
)
2892 /* Skip this file it didn't have any fields indexed */
2893 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2896 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
2908 r
= return_data(j
, j
->unique_file
, o
, data
, l
);
2916 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
2920 j
->unique_file
= NULL
;
2921 j
->unique_offset
= 0;
2922 j
->unique_file_lost
= false;
2925 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
2928 assert_return(j
, -EINVAL
);
2929 assert_return(!journal_pid_changed(j
), -ECHILD
);
2930 assert_return(field
, -EINVAL
);
2932 if (!j
->fields_file
) {
2933 if (j
->fields_file_lost
)
2936 j
->fields_file
= ordered_hashmap_first(j
->files
);
2937 if (!j
->fields_file
)
2940 j
->fields_hash_table_index
= 0;
2941 j
->fields_offset
= 0;
2945 JournalFile
*f
, *of
;
2954 if (j
->fields_offset
== 0) {
2957 /* We are not yet positioned at any field. Let's pick the first one */
2958 r
= journal_file_map_field_hash_table(f
);
2962 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
2964 if (j
->fields_hash_table_index
>= m
) {
2965 /* Reached the end of the hash table, go to the next file. */
2970 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
2972 if (j
->fields_offset
!= 0)
2975 /* Empty hash table bucket, go to next one */
2976 j
->fields_hash_table_index
++;
2980 /* Proceed with next file */
2981 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
2982 if (!j
->fields_file
) {
2987 j
->fields_offset
= 0;
2988 j
->fields_hash_table_index
= 0;
2993 /* We are already positioned at a field. If so, let's figure out the next field from it */
2995 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
2999 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
3000 if (j
->fields_offset
== 0) {
3001 /* Reached the end of the hash table chain */
3002 j
->fields_hash_table_index
++;
3007 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
3008 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
3012 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
3013 if (o
->object
.type
!= OBJECT_FIELD
) {
3014 log_debug("%s:offset " OFSfmt
": object has type %i, expected %i", f
->path
, j
->fields_offset
, o
->object
.type
, OBJECT_FIELD
);
3018 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
3020 /* Let's see if we already returned this field name before. */
3022 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
3026 /* Skip this file it didn't have any fields indexed */
3027 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3030 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
, le64toh(o
->field
.hash
), NULL
, NULL
);
3042 /* Check if this is really a valid string containing no NUL byte */
3043 if (memchr(o
->field
.payload
, 0, sz
))
3046 if (sz
> j
->data_threshold
)
3047 sz
= j
->data_threshold
;
3049 if (!GREEDY_REALLOC(j
->fields_buffer
, j
->fields_buffer_allocated
, sz
+ 1))
3052 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3053 j
->fields_buffer
[sz
] = 0;
3055 if (!field_is_valid(j
->fields_buffer
))
3058 *field
= j
->fields_buffer
;
3063 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3067 j
->fields_file
= NULL
;
3068 j
->fields_hash_table_index
= 0;
3069 j
->fields_offset
= 0;
3070 j
->fields_file_lost
= false;
3073 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3074 assert_return(j
, -EINVAL
);
3075 assert_return(!journal_pid_changed(j
), -ECHILD
);
3077 return !j
->on_network
;
3080 static char *lookup_field(const char *field
, void *userdata
) {
3081 sd_journal
*j
= userdata
;
3089 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3091 size
> REPLACE_VAR_MAX
)
3092 return strdup(field
);
3094 d
= strlen(field
) + 1;
3096 return strndup((const char*) data
+ d
, size
- d
);
3099 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3103 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3107 assert_return(j
, -EINVAL
);
3108 assert_return(!journal_pid_changed(j
), -ECHILD
);
3109 assert_return(ret
, -EINVAL
);
3111 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3115 cid
= strndup((const char*) data
+ 11, size
- 11);
3119 r
= sd_id128_from_string(cid
, &id
);
3123 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
3127 t
= replace_var(text
, lookup_field
, j
);
3135 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3136 assert_return(ret
, -EINVAL
);
3138 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3141 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3142 assert_return(j
, -EINVAL
);
3143 assert_return(!journal_pid_changed(j
), -ECHILD
);
3145 j
->data_threshold
= sz
;
3149 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3150 assert_return(j
, -EINVAL
);
3151 assert_return(!journal_pid_changed(j
), -ECHILD
);
3152 assert_return(sz
, -EINVAL
);
3154 *sz
= j
->data_threshold
;
3158 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3159 assert_return(j
, -EINVAL
);
3161 return j
->has_runtime_files
;
3164 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3165 assert_return(j
, -EINVAL
);
3167 return j
->has_persistent_files
;