1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <linux/magic.h>
9 #include <sys/inotify.h>
13 #include "sd-journal.h"
15 #include "alloc-util.h"
18 #include "dirent-util.h"
23 #include "format-util.h"
26 #include "hostname-util.h"
27 #include "id128-util.h"
29 #include "journal-def.h"
30 #include "journal-file.h"
31 #include "journal-internal.h"
34 #include "nulstr-util.h"
35 #include "path-util.h"
36 #include "process-util.h"
37 #include "replace-var.h"
38 #include "stat-util.h"
39 #include "stdio-util.h"
40 #include "string-util.h"
43 #define JOURNAL_FILES_MAX 7168
45 #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
47 #define REPLACE_VAR_MAX 256
49 #define DEFAULT_DATA_THRESHOLD (64*1024)
51 static void remove_file_real(sd_journal
*j
, JournalFile
*f
);
53 static bool journal_pid_changed(sd_journal
*j
) {
56 /* We don't support people creating a journal object and
57 * keeping it around over a fork(). Let's complain. */
59 return j
->original_pid
!= getpid_cached();
62 static int journal_put_error(sd_journal
*j
, int r
, const char *path
) {
66 /* Memorize an error we encountered, and store which
67 * file/directory it was generated from. Note that we store
68 * only *one* path per error code, as the error code is the
69 * key into the hashmap, and the path is the value. This means
70 * we keep track only of all error kinds, but not of all error
71 * locations. This has the benefit that the hashmap cannot
74 * We return an error here only if we didn't manage to
75 * memorize the real error. */
80 k
= hashmap_ensure_allocated(&j
->errors
, NULL
);
91 k
= hashmap_put(j
->errors
, INT_TO_PTR(r
), copy
);
104 static void detach_location(sd_journal
*j
) {
110 j
->current_file
= NULL
;
111 j
->current_field
= 0;
113 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
)
114 journal_file_reset_location(f
);
117 static void reset_location(sd_journal
*j
) {
121 zero(j
->current_location
);
124 static void init_location(Location
*l
, LocationType type
, JournalFile
*f
, Object
*o
) {
126 assert(IN_SET(type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
128 assert(o
->object
.type
== OBJECT_ENTRY
);
131 l
->seqnum
= le64toh(o
->entry
.seqnum
);
132 l
->seqnum_id
= f
->header
->seqnum_id
;
133 l
->realtime
= le64toh(o
->entry
.realtime
);
134 l
->monotonic
= le64toh(o
->entry
.monotonic
);
135 l
->boot_id
= o
->entry
.boot_id
;
136 l
->xor_hash
= le64toh(o
->entry
.xor_hash
);
138 l
->seqnum_set
= l
->realtime_set
= l
->monotonic_set
= l
->xor_hash_set
= true;
141 static void set_location(sd_journal
*j
, JournalFile
*f
, Object
*o
) {
146 init_location(&j
->current_location
, LOCATION_DISCRETE
, f
, o
);
149 j
->current_field
= 0;
151 /* Let f know its candidate entry was picked. */
152 assert(f
->location_type
== LOCATION_SEEK
);
153 f
->location_type
= LOCATION_DISCRETE
;
156 static int match_is_valid(const void *data
, size_t size
) {
164 if (startswith(data
, "__"))
168 for (p
= b
; p
< b
+ size
; p
++) {
176 if (*p
>= 'A' && *p
<= 'Z')
179 if (*p
>= '0' && *p
<= '9')
188 static bool same_field(const void *_a
, size_t s
, const void *_b
, size_t t
) {
189 const uint8_t *a
= _a
, *b
= _b
;
192 for (j
= 0; j
< s
&& j
< t
; j
++) {
201 assert_not_reached("\"=\" not found");
204 static Match
*match_new(Match
*p
, MatchType t
) {
215 LIST_PREPEND(matches
, p
->matches
, m
);
221 static void match_free(Match
*m
) {
225 match_free(m
->matches
);
228 LIST_REMOVE(matches
, m
->parent
->matches
, m
);
234 static void match_free_if_empty(Match
*m
) {
235 if (!m
|| m
->matches
)
241 _public_
int sd_journal_add_match(sd_journal
*j
, const void *data
, size_t size
) {
242 Match
*l3
, *l4
, *add_here
= NULL
, *m
;
245 assert_return(j
, -EINVAL
);
246 assert_return(!journal_pid_changed(j
), -ECHILD
);
247 assert_return(data
, -EINVAL
);
252 assert_return(match_is_valid(data
, size
), -EINVAL
);
258 * level 4: concrete matches */
261 j
->level0
= match_new(NULL
, MATCH_AND_TERM
);
267 j
->level1
= match_new(j
->level0
, MATCH_OR_TERM
);
273 j
->level2
= match_new(j
->level1
, MATCH_AND_TERM
);
278 assert(j
->level0
->type
== MATCH_AND_TERM
);
279 assert(j
->level1
->type
== MATCH_OR_TERM
);
280 assert(j
->level2
->type
== MATCH_AND_TERM
);
282 le_hash
= htole64(hash64(data
, size
));
284 LIST_FOREACH(matches
, l3
, j
->level2
->matches
) {
285 assert(l3
->type
== MATCH_OR_TERM
);
287 LIST_FOREACH(matches
, l4
, l3
->matches
) {
288 assert(l4
->type
== MATCH_DISCRETE
);
290 /* Exactly the same match already? Then ignore
292 if (l4
->le_hash
== le_hash
&&
294 memcmp(l4
->data
, data
, size
) == 0)
297 /* Same field? Then let's add this to this OR term */
298 if (same_field(data
, size
, l4
->data
, l4
->size
)) {
309 add_here
= match_new(j
->level2
, MATCH_OR_TERM
);
314 m
= match_new(add_here
, MATCH_DISCRETE
);
318 m
->le_hash
= le_hash
;
320 m
->data
= memdup(data
, size
);
329 match_free_if_empty(add_here
);
330 match_free_if_empty(j
->level2
);
331 match_free_if_empty(j
->level1
);
332 match_free_if_empty(j
->level0
);
337 _public_
int sd_journal_add_conjunction(sd_journal
*j
) {
338 assert_return(j
, -EINVAL
);
339 assert_return(!journal_pid_changed(j
), -ECHILD
);
347 if (!j
->level1
->matches
)
356 _public_
int sd_journal_add_disjunction(sd_journal
*j
) {
357 assert_return(j
, -EINVAL
);
358 assert_return(!journal_pid_changed(j
), -ECHILD
);
369 if (!j
->level2
->matches
)
376 static char *match_make_string(Match
*m
) {
379 bool enclose
= false;
382 return strdup("none");
384 if (m
->type
== MATCH_DISCRETE
)
385 return cescape_length(m
->data
, m
->size
);
387 LIST_FOREACH(matches
, i
, m
->matches
) {
390 t
= match_make_string(i
);
395 k
= strjoin(p
, m
->type
== MATCH_OR_TERM
? " OR " : " AND ", t
);
410 r
= strjoin("(", p
, ")");
418 char *journal_make_match_string(sd_journal
*j
) {
421 return match_make_string(j
->level0
);
424 _public_
void sd_journal_flush_matches(sd_journal
*j
) {
429 match_free(j
->level0
);
431 j
->level0
= j
->level1
= j
->level2
= NULL
;
436 _pure_
static int compare_with_location(JournalFile
*f
, Location
*l
) {
441 assert(f
->location_type
== LOCATION_SEEK
);
442 assert(IN_SET(l
->type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
444 if (l
->monotonic_set
&&
445 sd_id128_equal(f
->current_boot_id
, l
->boot_id
) &&
447 f
->current_realtime
== l
->realtime
&&
449 f
->current_xor_hash
== l
->xor_hash
)
453 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
)) {
455 r
= CMP(f
->current_seqnum
, l
->seqnum
);
460 if (l
->monotonic_set
&&
461 sd_id128_equal(f
->current_boot_id
, l
->boot_id
)) {
463 r
= CMP(f
->current_monotonic
, l
->monotonic
);
468 if (l
->realtime_set
) {
470 r
= CMP(f
->current_realtime
, l
->realtime
);
475 if (l
->xor_hash_set
) {
477 r
= CMP(f
->current_xor_hash
, l
->xor_hash
);
485 static int next_for_match(
489 uint64_t after_offset
,
490 direction_t direction
,
502 if (m
->type
== MATCH_DISCRETE
) {
505 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
509 return journal_file_move_to_entry_by_offset_for_data(f
, dp
, after_offset
, direction
, ret
, offset
);
511 } else if (m
->type
== MATCH_OR_TERM
) {
514 /* Find the earliest match beyond after_offset */
516 LIST_FOREACH(matches
, i
, m
->matches
) {
519 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
523 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
531 } else if (m
->type
== MATCH_AND_TERM
) {
532 Match
*i
, *last_moved
;
534 /* Always jump to the next matching entry and repeat
535 * this until we find an offset that matches for all
541 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
545 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
546 last_moved
= m
->matches
;
548 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
551 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
555 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
556 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
565 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
577 static int find_location_for_match(
581 direction_t direction
,
591 if (m
->type
== MATCH_DISCRETE
) {
594 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
598 /* FIXME: missing: find by monotonic */
600 if (j
->current_location
.type
== LOCATION_HEAD
)
601 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_DOWN
, ret
, offset
);
602 if (j
->current_location
.type
== LOCATION_TAIL
)
603 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_UP
, ret
, offset
);
604 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
605 return journal_file_move_to_entry_by_seqnum_for_data(f
, dp
, j
->current_location
.seqnum
, direction
, ret
, offset
);
606 if (j
->current_location
.monotonic_set
) {
607 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, dp
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
611 if (j
->current_location
.realtime_set
)
612 return journal_file_move_to_entry_by_realtime_for_data(f
, dp
, j
->current_location
.realtime
, direction
, ret
, offset
);
614 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, direction
, ret
, offset
);
616 } else if (m
->type
== MATCH_OR_TERM
) {
621 /* Find the earliest match */
623 LIST_FOREACH(matches
, i
, m
->matches
) {
626 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
630 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
638 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
653 assert(m
->type
== MATCH_AND_TERM
);
655 /* First jump to the last match, and then find the
656 * next one where all matches match */
661 LIST_FOREACH(matches
, i
, m
->matches
) {
664 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
668 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
672 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
676 static int find_location_with_matches(
679 direction_t direction
,
691 /* No matches is simple */
693 if (j
->current_location
.type
== LOCATION_HEAD
)
694 return journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
);
695 if (j
->current_location
.type
== LOCATION_TAIL
)
696 return journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
);
697 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
698 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
699 if (j
->current_location
.monotonic_set
) {
700 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
704 if (j
->current_location
.realtime_set
)
705 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
707 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
709 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
712 static int next_with_matches(
715 direction_t direction
,
724 /* No matches is easy. We simple advance the file
727 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
729 /* If we have a match then we look for the next matching entry
730 * with an offset at least one step larger */
731 return next_for_match(j
, j
->level0
, f
,
732 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
733 : f
->current_offset
- 1,
734 direction
, ret
, offset
);
737 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
739 uint64_t cp
, n_entries
;
745 n_entries
= le64toh(f
->header
->n_entries
);
747 /* If we hit EOF before, we don't need to look into this file again
748 * unless direction changed or new entries appeared. */
749 if (f
->last_direction
== direction
&& f
->location_type
== LOCATION_TAIL
&&
750 n_entries
== f
->last_n_entries
)
753 f
->last_n_entries
= n_entries
;
755 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
756 /* LOCATION_SEEK here means we did the work in a previous
757 * iteration and the current location already points to a
758 * candidate entry. */
759 if (f
->location_type
!= LOCATION_SEEK
) {
760 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
764 journal_file_save_location(f
, c
, cp
);
767 f
->last_direction
= direction
;
769 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
773 journal_file_save_location(f
, c
, cp
);
776 /* OK, we found the spot, now let's advance until an entry
777 * that is actually different from what we were previously
778 * looking at. This is necessary to handle entries which exist
779 * in two (or more) journal files, and which shall all be
780 * suppressed but one. */
785 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
788 k
= compare_with_location(f
, &j
->current_location
);
790 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
797 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
801 journal_file_save_location(f
, c
, cp
);
805 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
806 JournalFile
*new_file
= NULL
;
812 assert_return(j
, -EINVAL
);
813 assert_return(!journal_pid_changed(j
), -ECHILD
);
815 r
= iterated_cache_get(j
->files_cache
, NULL
, &files
, &n_files
);
819 for (i
= 0; i
< n_files
; i
++) {
820 JournalFile
*f
= (JournalFile
*)files
[i
];
823 r
= next_beyond_location(j
, f
, direction
);
825 log_debug_errno(r
, "Can't iterate through %s, ignoring: %m", f
->path
);
826 remove_file_real(j
, f
);
829 f
->location_type
= LOCATION_TAIL
;
838 k
= journal_file_compare_locations(f
, new_file
);
840 found
= direction
== DIRECTION_DOWN
? k
< 0 : k
> 0;
850 r
= journal_file_move_to_object(new_file
, OBJECT_ENTRY
, new_file
->current_offset
, &o
);
854 set_location(j
, new_file
, o
);
859 _public_
int sd_journal_next(sd_journal
*j
) {
860 return real_journal_next(j
, DIRECTION_DOWN
);
863 _public_
int sd_journal_previous(sd_journal
*j
) {
864 return real_journal_next(j
, DIRECTION_UP
);
867 static int real_journal_next_skip(sd_journal
*j
, direction_t direction
, uint64_t skip
) {
870 assert_return(j
, -EINVAL
);
871 assert_return(!journal_pid_changed(j
), -ECHILD
);
874 /* If this is not a discrete skip, then at least
875 * resolve the current location */
876 if (j
->current_location
.type
!= LOCATION_DISCRETE
) {
877 r
= real_journal_next(j
, direction
);
886 r
= real_journal_next(j
, direction
);
900 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
901 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
904 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
905 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
908 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
911 char bid
[33], sid
[33];
913 assert_return(j
, -EINVAL
);
914 assert_return(!journal_pid_changed(j
), -ECHILD
);
915 assert_return(cursor
, -EINVAL
);
917 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
918 return -EADDRNOTAVAIL
;
920 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
924 sd_id128_to_string(j
->current_file
->header
->seqnum_id
, sid
);
925 sd_id128_to_string(o
->entry
.boot_id
, bid
);
928 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
929 sid
, le64toh(o
->entry
.seqnum
),
930 bid
, le64toh(o
->entry
.monotonic
),
931 le64toh(o
->entry
.realtime
),
932 le64toh(o
->entry
.xor_hash
)) < 0)
938 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
939 const char *word
, *state
;
941 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
943 seqnum_id_set
= false,
946 monotonic_set
= false,
947 realtime_set
= false,
948 xor_hash_set
= false;
949 sd_id128_t seqnum_id
, boot_id
;
951 assert_return(j
, -EINVAL
);
952 assert_return(!journal_pid_changed(j
), -ECHILD
);
953 assert_return(!isempty(cursor
), -EINVAL
);
955 FOREACH_WORD_SEPARATOR(word
, l
, cursor
, ";", state
) {
959 if (l
< 2 || word
[1] != '=')
962 item
= strndup(word
, l
);
969 seqnum_id_set
= true;
970 k
= sd_id128_from_string(item
+2, &seqnum_id
);
975 if (sscanf(item
+2, "%llx", &seqnum
) != 1)
981 k
= sd_id128_from_string(item
+2, &boot_id
);
985 monotonic_set
= true;
986 if (sscanf(item
+2, "%llx", &monotonic
) != 1)
992 if (sscanf(item
+2, "%llx", &realtime
) != 1)
998 if (sscanf(item
+2, "%llx", &xor_hash
) != 1)
1009 if ((!seqnum_set
|| !seqnum_id_set
) &&
1010 (!monotonic_set
|| !boot_id_set
) &&
1016 j
->current_location
.type
= LOCATION_SEEK
;
1019 j
->current_location
.realtime
= (uint64_t) realtime
;
1020 j
->current_location
.realtime_set
= true;
1023 if (seqnum_set
&& seqnum_id_set
) {
1024 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1025 j
->current_location
.seqnum_id
= seqnum_id
;
1026 j
->current_location
.seqnum_set
= true;
1029 if (monotonic_set
&& boot_id_set
) {
1030 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1031 j
->current_location
.boot_id
= boot_id
;
1032 j
->current_location
.monotonic_set
= true;
1036 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1037 j
->current_location
.xor_hash_set
= true;
1043 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1047 assert_return(j
, -EINVAL
);
1048 assert_return(!journal_pid_changed(j
), -ECHILD
);
1049 assert_return(!isempty(cursor
), -EINVAL
);
1051 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1052 return -EADDRNOTAVAIL
;
1054 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1059 _cleanup_free_
char *item
= NULL
;
1060 unsigned long long ll
;
1064 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1071 if (strlen(item
) < 2 || item
[1] != '=')
1077 k
= sd_id128_from_string(item
+2, &id
);
1080 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1085 if (sscanf(item
+2, "%llx", &ll
) != 1)
1087 if (ll
!= le64toh(o
->entry
.seqnum
))
1092 k
= sd_id128_from_string(item
+2, &id
);
1095 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1100 if (sscanf(item
+2, "%llx", &ll
) != 1)
1102 if (ll
!= le64toh(o
->entry
.monotonic
))
1107 if (sscanf(item
+2, "%llx", &ll
) != 1)
1109 if (ll
!= le64toh(o
->entry
.realtime
))
1114 if (sscanf(item
+2, "%llx", &ll
) != 1)
1116 if (ll
!= le64toh(o
->entry
.xor_hash
))
1125 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t usec
) {
1126 assert_return(j
, -EINVAL
);
1127 assert_return(!journal_pid_changed(j
), -ECHILD
);
1130 j
->current_location
.type
= LOCATION_SEEK
;
1131 j
->current_location
.boot_id
= boot_id
;
1132 j
->current_location
.monotonic
= usec
;
1133 j
->current_location
.monotonic_set
= true;
1138 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1139 assert_return(j
, -EINVAL
);
1140 assert_return(!journal_pid_changed(j
), -ECHILD
);
1143 j
->current_location
.type
= LOCATION_SEEK
;
1144 j
->current_location
.realtime
= usec
;
1145 j
->current_location
.realtime_set
= true;
1150 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1151 assert_return(j
, -EINVAL
);
1152 assert_return(!journal_pid_changed(j
), -ECHILD
);
1155 j
->current_location
.type
= LOCATION_HEAD
;
1160 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1161 assert_return(j
, -EINVAL
);
1162 assert_return(!journal_pid_changed(j
), -ECHILD
);
1165 j
->current_location
.type
= LOCATION_TAIL
;
1170 static void check_network(sd_journal
*j
, int fd
) {
1176 j
->on_network
= fd_is_network_fs(fd
);
1179 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1180 const char *full
, *tilded
, *atted
;
1182 full
= strjoina(prefix
, ".journal");
1183 tilded
= strjoina(full
, "~");
1184 atted
= strjoina(prefix
, "@");
1186 return STR_IN_SET(filename
, full
, tilded
) ||
1187 startswith(filename
, atted
);
1190 static bool file_type_wanted(int flags
, const char *filename
) {
1193 if (!endswith(filename
, ".journal") && !endswith(filename
, ".journal~"))
1196 /* no flags set → every type is OK */
1197 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1200 if (flags
& SD_JOURNAL_SYSTEM
&& file_has_type_prefix("system", filename
))
1203 if (flags
& SD_JOURNAL_CURRENT_USER
) {
1204 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1206 xsprintf(prefix
, "user-"UID_FMT
, getuid());
1208 if (file_has_type_prefix(prefix
, filename
))
1215 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1220 if (j
->toplevel_fd
>= 0)
1223 return path_startswith(path
, prefix
);
1226 static void track_file_disposition(sd_journal
*j
, JournalFile
*f
) {
1230 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1231 j
->has_runtime_files
= true;
1232 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1233 j
->has_persistent_files
= true;
1236 static const char *skip_slash(const char *p
) {
1247 static int add_any_file(
1252 bool close_fd
= false;
1258 assert(fd
>= 0 || path
);
1261 if (j
->toplevel_fd
>= 0)
1262 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1263 * openat() ignores the first argument. */
1265 fd
= openat(j
->toplevel_fd
, skip_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1267 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1269 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1275 r
= fd_nonblock(fd
, false);
1277 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1282 if (fstat(fd
, &st
) < 0) {
1283 r
= log_debug_errno(errno
, "Failed to fstat file '%s': %m", path
);
1287 r
= stat_verify_regular(&st
);
1289 log_debug_errno(r
, "Refusing to open '%s', as it is not a regular file.", path
);
1293 f
= ordered_hashmap_get(j
->files
, path
);
1295 if (f
->last_stat
.st_dev
== st
.st_dev
&&
1296 f
->last_stat
.st_ino
== st
.st_ino
) {
1298 /* We already track this file, under the same path and with the same device/inode numbers, it's
1299 * hence really the same. Mark this file as seen in this generation. This is used to GC old
1300 * files in process_q_overflow() to detect journal files that are still there and discern them
1301 * from those which are gone. */
1303 f
->last_seen_generation
= j
->generation
;
1308 /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
1309 * replaced (probably due to rotation?), let's drop it hence from our list. */
1310 remove_file_real(j
, f
);
1314 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1315 log_debug("Too many open journal files, not adding %s.", path
);
1320 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, false, 0, false, NULL
, j
->mmap
, NULL
, NULL
, &f
);
1322 log_debug_errno(r
, "Failed to open journal file %s: %m", path
);
1326 /* journal_file_dump(f); */
1328 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1330 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 */
1331 (void) journal_file_close(f
);
1335 close_fd
= false; /* the fd is now owned by the JournalFile object */
1337 f
->last_seen_generation
= j
->generation
;
1339 track_file_disposition(j
, f
);
1340 check_network(j
, f
->fd
);
1342 j
->current_invalidate_counter
++;
1344 log_debug("File %s added.", f
->path
);
1353 k
= journal_put_error(j
, r
, path
);
1361 static int add_file_by_name(
1364 const char *filename
) {
1372 if (j
->no_new_files
)
1375 if (!file_type_wanted(j
->flags
, filename
))
1378 path
= prefix_roota(prefix
, filename
);
1379 return add_any_file(j
, -1, path
);
1382 static void remove_file_by_name(
1385 const char *filename
) {
1394 path
= prefix_roota(prefix
, filename
);
1395 f
= ordered_hashmap_get(j
->files
, path
);
1399 remove_file_real(j
, f
);
1402 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1406 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1408 log_debug("File %s removed.", f
->path
);
1410 if (j
->current_file
== f
) {
1411 j
->current_file
= NULL
;
1412 j
->current_field
= 0;
1415 if (j
->unique_file
== f
) {
1416 /* Jump to the next unique_file or NULL if that one was last */
1417 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1418 j
->unique_offset
= 0;
1419 if (!j
->unique_file
)
1420 j
->unique_file_lost
= true;
1423 if (j
->fields_file
== f
) {
1424 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1425 j
->fields_offset
= 0;
1426 if (!j
->fields_file
)
1427 j
->fields_file_lost
= true;
1430 (void) journal_file_close(f
);
1432 j
->current_invalidate_counter
++;
1435 static int dirname_is_machine_id(const char *fn
) {
1436 sd_id128_t id
, machine
;
1439 r
= sd_id128_get_machine(&machine
);
1443 r
= sd_id128_from_string(fn
, &id
);
1447 return sd_id128_equal(id
, machine
);
1450 static bool dirent_is_journal_file(const struct dirent
*de
) {
1453 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1456 return endswith(de
->d_name
, ".journal") ||
1457 endswith(de
->d_name
, ".journal~");
1460 static bool dirent_is_id128_subdir(const struct dirent
*de
) {
1463 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1466 return id128_is_valid(de
->d_name
);
1469 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1476 if (j
->toplevel_fd
< 0)
1479 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1480 * relative, by dropping the initial slash */
1481 d
= xopendirat(j
->toplevel_fd
, skip_slash(path
), 0);
1489 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1491 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1498 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1500 if (dirent_is_journal_file(de
))
1501 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1503 if (m
->is_root
&& dirent_is_id128_subdir(de
))
1504 (void) add_directory(j
, m
->path
, de
->d_name
);
1510 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1513 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1520 /* Watch this directory if that's enabled and if it not being watched yet. */
1522 if (m
->wd
> 0) /* Already have a watch? */
1524 if (j
->inotify_fd
< 0) /* Not watching at all? */
1527 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1529 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1533 r
= hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
);
1535 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1537 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1538 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1543 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
) {
1544 _cleanup_free_
char *path
= NULL
;
1545 _cleanup_closedir_
DIR *d
= NULL
;
1552 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1553 * and reenumerates directory contents */
1555 path
= path_join(prefix
, dirname
);
1561 log_debug("Considering directory '%s'.", path
);
1563 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1564 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1565 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1568 r
= directory_open(j
, path
, &d
);
1570 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
1574 m
= hashmap_get(j
->directories_by_path
, path
);
1576 m
= new0(Directory
, 1);
1585 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1591 path
= NULL
; /* avoid freeing in cleanup */
1592 j
->current_invalidate_counter
++;
1594 log_debug("Directory %s added.", m
->path
);
1596 } else if (m
->is_root
)
1597 return 0; /* Don't 'downgrade' from root directory */
1599 m
->last_seen_generation
= j
->generation
;
1601 directory_watch(j
, m
, dirfd(d
),
1602 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1603 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
1606 if (!j
->no_new_files
)
1607 directory_enumerate(j
, m
, d
);
1609 check_network(j
, dirfd(d
));
1614 k
= journal_put_error(j
, r
, path
?: prefix
);
1621 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
1623 _cleanup_closedir_
DIR *d
= NULL
;
1629 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1630 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1631 * populate the set, as well as to update it later. */
1634 /* If there's a path specified, use it. */
1636 log_debug("Considering root directory '%s'.", p
);
1638 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
1639 !path_has_prefix(j
, p
, "/run"))
1643 p
= strjoina(j
->prefix
, p
);
1645 r
= directory_open(j
, p
, &d
);
1646 if (r
== -ENOENT
&& missing_ok
)
1649 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
1655 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1656 * opendir() will take possession of the fd, and close it, which we don't want. */
1658 p
= "."; /* store this as "." in the directories hashmap */
1660 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
1676 m
= hashmap_get(j
->directories_by_path
, p
);
1678 m
= new0(Directory
, 1);
1686 m
->path
= strdup(p
);
1693 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1700 j
->current_invalidate_counter
++;
1702 log_debug("Root directory %s added.", m
->path
);
1704 } else if (!m
->is_root
)
1707 directory_watch(j
, m
, dirfd(d
),
1708 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1711 if (!j
->no_new_files
)
1712 directory_enumerate(j
, m
, d
);
1714 check_network(j
, dirfd(d
));
1719 k
= journal_put_error(j
, r
, p
);
1726 static void remove_directory(sd_journal
*j
, Directory
*d
) {
1730 hashmap_remove(j
->directories_by_wd
, INT_TO_PTR(d
->wd
));
1732 if (j
->inotify_fd
>= 0)
1733 (void) inotify_rm_watch(j
->inotify_fd
, d
->wd
);
1736 hashmap_remove(j
->directories_by_path
, d
->path
);
1739 log_debug("Root directory %s removed.", d
->path
);
1741 log_debug("Directory %s removed.", d
->path
);
1747 static int add_search_paths(sd_journal
*j
) {
1749 static const char search_paths
[] =
1750 "/run/log/journal\0"
1751 "/var/log/journal\0";
1756 /* We ignore most errors here, since the idea is to only open
1757 * what's actually accessible, and ignore the rest. */
1759 NULSTR_FOREACH(p
, search_paths
)
1760 (void) add_root_directory(j
, p
, true);
1762 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
1763 (void) add_root_directory(j
, "/var/log/journal/remote", true);
1768 static int add_current_paths(sd_journal
*j
) {
1773 assert(j
->no_new_files
);
1775 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1776 * treat them as fatal. */
1778 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
1779 _cleanup_free_
char *dir
;
1782 dir
= dirname_malloc(f
->path
);
1786 r
= add_directory(j
, dir
, NULL
);
1794 static int allocate_inotify(sd_journal
*j
) {
1797 if (j
->inotify_fd
< 0) {
1798 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1799 if (j
->inotify_fd
< 0)
1803 return hashmap_ensure_allocated(&j
->directories_by_wd
, NULL
);
1806 static sd_journal
*journal_new(int flags
, const char *path
) {
1807 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1809 j
= new0(sd_journal
, 1);
1813 j
->original_pid
= getpid_cached();
1814 j
->toplevel_fd
= -1;
1817 j
->data_threshold
= DEFAULT_DATA_THRESHOLD
;
1826 if (flags
& SD_JOURNAL_OS_ROOT
)
1832 j
->files
= ordered_hashmap_new(&path_hash_ops
);
1836 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
1837 j
->directories_by_path
= hashmap_new(&path_hash_ops
);
1838 j
->mmap
= mmap_cache_new();
1839 if (!j
->files_cache
|| !j
->directories_by_path
|| !j
->mmap
)
1845 #define OPEN_ALLOWED_FLAGS \
1846 (SD_JOURNAL_LOCAL_ONLY | \
1847 SD_JOURNAL_RUNTIME_ONLY | \
1848 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)
1850 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1851 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1854 assert_return(ret
, -EINVAL
);
1855 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
1857 j
= journal_new(flags
, NULL
);
1861 r
= add_search_paths(j
);
1869 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1870 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1872 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1873 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1874 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1878 /* This is pretty much deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1879 * combination with sd_journal_open_directory_fd(). */
1881 assert_return(machine
, -EINVAL
);
1882 assert_return(ret
, -EINVAL
);
1883 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
1884 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1886 p
= strjoina("/run/systemd/machines/", machine
);
1887 r
= parse_env_file(NULL
, p
,
1897 if (!streq_ptr(class, "container"))
1900 j
= journal_new(flags
, root
);
1904 r
= add_search_paths(j
);
1912 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
1913 (SD_JOURNAL_OS_ROOT | \
1914 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1916 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
1917 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1920 assert_return(ret
, -EINVAL
);
1921 assert_return(path
, -EINVAL
);
1922 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
1924 j
= journal_new(flags
, path
);
1928 if (flags
& SD_JOURNAL_OS_ROOT
)
1929 r
= add_search_paths(j
);
1931 r
= add_root_directory(j
, path
, false);
1939 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
1940 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1944 assert_return(ret
, -EINVAL
);
1945 assert_return(flags
== 0, -EINVAL
);
1947 j
= journal_new(flags
, NULL
);
1951 STRV_FOREACH(path
, paths
) {
1952 r
= add_any_file(j
, -1, *path
);
1957 j
->no_new_files
= true;
1963 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
1964 (SD_JOURNAL_OS_ROOT | \
1965 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1967 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
1968 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1972 assert_return(ret
, -EINVAL
);
1973 assert_return(fd
>= 0, -EBADF
);
1974 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
1976 if (fstat(fd
, &st
) < 0)
1979 if (!S_ISDIR(st
.st_mode
))
1982 j
= journal_new(flags
, NULL
);
1986 j
->toplevel_fd
= fd
;
1988 if (flags
& SD_JOURNAL_OS_ROOT
)
1989 r
= add_search_paths(j
);
1991 r
= add_root_directory(j
, NULL
, false);
1999 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2002 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2006 assert_return(ret
, -EINVAL
);
2007 assert_return(n_fds
> 0, -EBADF
);
2008 assert_return(flags
== 0, -EINVAL
);
2010 j
= journal_new(flags
, NULL
);
2014 for (i
= 0; i
< n_fds
; i
++) {
2022 if (fstat(fds
[i
], &st
) < 0) {
2027 r
= stat_verify_regular(&st
);
2031 r
= add_any_file(j
, fds
[i
], NULL
);
2036 j
->no_new_files
= true;
2037 j
->no_inotify
= true;
2043 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2045 ORDERED_HASHMAP_FOREACH(f
, j
->files
, iterator
)
2046 f
->close_fd
= false;
2051 _public_
void sd_journal_close(sd_journal
*j
) {
2057 sd_journal_flush_matches(j
);
2059 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2060 iterated_cache_free(j
->files_cache
);
2062 while ((d
= hashmap_first(j
->directories_by_path
)))
2063 remove_directory(j
, d
);
2065 while ((d
= hashmap_first(j
->directories_by_wd
)))
2066 remove_directory(j
, d
);
2068 hashmap_free(j
->directories_by_path
);
2069 hashmap_free(j
->directories_by_wd
);
2071 safe_close(j
->inotify_fd
);
2074 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j
->mmap
), mmap_cache_get_missed(j
->mmap
));
2075 mmap_cache_unref(j
->mmap
);
2078 hashmap_free_free(j
->errors
);
2082 free(j
->unique_field
);
2083 free(j
->fields_buffer
);
2087 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2092 assert_return(j
, -EINVAL
);
2093 assert_return(!journal_pid_changed(j
), -ECHILD
);
2094 assert_return(ret
, -EINVAL
);
2096 f
= j
->current_file
;
2098 return -EADDRNOTAVAIL
;
2100 if (f
->current_offset
<= 0)
2101 return -EADDRNOTAVAIL
;
2103 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2107 *ret
= le64toh(o
->entry
.realtime
);
2111 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2117 assert_return(j
, -EINVAL
);
2118 assert_return(!journal_pid_changed(j
), -ECHILD
);
2120 f
= j
->current_file
;
2122 return -EADDRNOTAVAIL
;
2124 if (f
->current_offset
<= 0)
2125 return -EADDRNOTAVAIL
;
2127 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2132 *ret_boot_id
= o
->entry
.boot_id
;
2134 r
= sd_id128_get_boot(&id
);
2138 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2143 *ret
= le64toh(o
->entry
.monotonic
);
2148 static bool field_is_valid(const char *field
) {
2156 if (startswith(field
, "__"))
2159 for (p
= field
; *p
; p
++) {
2164 if (*p
>= 'A' && *p
<= 'Z')
2167 if (*p
>= '0' && *p
<= '9')
2176 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2179 size_t field_length
;
2183 assert_return(j
, -EINVAL
);
2184 assert_return(!journal_pid_changed(j
), -ECHILD
);
2185 assert_return(field
, -EINVAL
);
2186 assert_return(data
, -EINVAL
);
2187 assert_return(size
, -EINVAL
);
2188 assert_return(field_is_valid(field
), -EINVAL
);
2190 f
= j
->current_file
;
2192 return -EADDRNOTAVAIL
;
2194 if (f
->current_offset
<= 0)
2195 return -EADDRNOTAVAIL
;
2197 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2201 field_length
= strlen(field
);
2203 n
= journal_file_entry_n_items(o
);
2204 for (i
= 0; i
< n
; i
++) {
2210 p
= le64toh(o
->entry
.items
[i
].object_offset
);
2211 le_hash
= o
->entry
.items
[i
].hash
;
2212 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2216 if (le_hash
!= o
->data
.hash
)
2219 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2221 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2223 #if HAVE_XZ || HAVE_LZ4
2224 r
= decompress_startswith(compression
,
2226 &f
->compress_buffer
, &f
->compress_buffer_size
,
2227 field
, field_length
, '=');
2229 log_debug_errno(r
, "Cannot decompress %s object of length %"PRIu64
" at offset "OFSfmt
": %m",
2230 object_compressed_to_string(compression
), l
, p
);
2235 r
= decompress_blob(compression
,
2237 &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
,
2242 *data
= f
->compress_buffer
;
2243 *size
= (size_t) rsize
;
2248 return -EPROTONOSUPPORT
;
2250 } else if (l
>= field_length
+1 &&
2251 memcmp(o
->data
.payload
, field
, field_length
) == 0 &&
2252 o
->data
.payload
[field_length
] == '=') {
2256 if ((uint64_t) t
!= l
)
2259 *data
= o
->data
.payload
;
2265 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2273 static int return_data(sd_journal
*j
, JournalFile
*f
, Object
*o
, const void **data
, size_t *size
) {
2278 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2281 /* We can't read objects larger than 4G on a 32bit machine */
2282 if ((uint64_t) t
!= l
)
2285 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2287 #if HAVE_XZ || HAVE_LZ4
2291 r
= decompress_blob(compression
,
2292 o
->data
.payload
, l
, &f
->compress_buffer
,
2293 &f
->compress_buffer_size
, &rsize
, j
->data_threshold
);
2297 *data
= f
->compress_buffer
;
2298 *size
= (size_t) rsize
;
2300 return -EPROTONOSUPPORT
;
2303 *data
= o
->data
.payload
;
2310 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2317 assert_return(j
, -EINVAL
);
2318 assert_return(!journal_pid_changed(j
), -ECHILD
);
2319 assert_return(data
, -EINVAL
);
2320 assert_return(size
, -EINVAL
);
2322 f
= j
->current_file
;
2324 return -EADDRNOTAVAIL
;
2326 if (f
->current_offset
<= 0)
2327 return -EADDRNOTAVAIL
;
2329 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2333 n
= journal_file_entry_n_items(o
);
2334 if (j
->current_field
>= n
)
2337 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2338 le_hash
= o
->entry
.items
[j
->current_field
].hash
;
2339 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2343 if (le_hash
!= o
->data
.hash
)
2346 r
= return_data(j
, f
, o
, data
, size
);
2355 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2359 j
->current_field
= 0;
2362 static int reiterate_all_paths(sd_journal
*j
) {
2365 if (j
->no_new_files
)
2366 return add_current_paths(j
);
2368 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2369 return add_search_paths(j
);
2371 if (j
->toplevel_fd
>= 0)
2372 return add_root_directory(j
, NULL
, false);
2375 return add_root_directory(j
, j
->path
, true);
2377 return add_search_paths(j
);
2380 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2383 assert_return(j
, -EINVAL
);
2384 assert_return(!journal_pid_changed(j
), -ECHILD
);
2387 return -EMEDIUMTYPE
;
2389 if (j
->inotify_fd
>= 0)
2390 return j
->inotify_fd
;
2392 r
= allocate_inotify(j
);
2396 log_debug("Reiterating files to get inotify watches established.");
2398 /* Iterate through all dirs again, to add them to the inotify */
2399 r
= reiterate_all_paths(j
);
2403 return j
->inotify_fd
;
2406 _public_
int sd_journal_get_events(sd_journal
*j
) {
2409 assert_return(j
, -EINVAL
);
2410 assert_return(!journal_pid_changed(j
), -ECHILD
);
2412 fd
= sd_journal_get_fd(j
);
2419 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2422 assert_return(j
, -EINVAL
);
2423 assert_return(!journal_pid_changed(j
), -ECHILD
);
2424 assert_return(timeout_usec
, -EINVAL
);
2426 fd
= sd_journal_get_fd(j
);
2430 if (!j
->on_network
) {
2431 *timeout_usec
= (uint64_t) -1;
2435 /* If we are on the network we need to regularly check for
2436 * changes manually */
2438 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2442 static void process_q_overflow(sd_journal
*j
) {
2449 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2450 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2451 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2452 * are subject for unloading. */
2454 log_debug("Inotify queue overrun, reiterating everything.");
2457 (void) reiterate_all_paths(j
);
2459 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2461 if (f
->last_seen_generation
== j
->generation
)
2464 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2465 remove_file_real(j
, f
);
2468 HASHMAP_FOREACH(m
, j
->directories_by_path
, i
) {
2470 if (m
->last_seen_generation
== j
->generation
)
2473 if (m
->is_root
) /* Never GC root directories */
2476 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2477 remove_directory(j
, m
);
2480 log_debug("Reiteration complete.");
2483 static void process_inotify_event(sd_journal
*j
, struct inotify_event
*e
) {
2489 if (e
->mask
& IN_Q_OVERFLOW
) {
2490 process_q_overflow(j
);
2494 /* Is this a subdirectory we watch? */
2495 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2497 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2498 (endswith(e
->name
, ".journal") ||
2499 endswith(e
->name
, ".journal~"))) {
2501 /* Event for a journal file */
2503 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2504 (void) add_file_by_name(j
, d
->path
, e
->name
);
2505 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2506 remove_file_by_name(j
, d
->path
, e
->name
);
2508 } else if (!d
->is_root
&& e
->len
== 0) {
2510 /* Event for a subdirectory */
2512 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2513 remove_directory(j
, d
);
2515 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2517 /* Event for root directory */
2519 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2520 (void) add_directory(j
, d
->path
, e
->name
);
2526 if (e
->mask
& IN_IGNORED
)
2529 log_debug("Unexpected inotify event.");
2532 static int determine_change(sd_journal
*j
) {
2537 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2538 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2540 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2543 _public_
int sd_journal_process(sd_journal
*j
) {
2544 bool got_something
= false;
2546 assert_return(j
, -EINVAL
);
2547 assert_return(!journal_pid_changed(j
), -ECHILD
);
2549 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
2552 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2553 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2556 union inotify_event_buffer buffer
;
2557 struct inotify_event
*e
;
2560 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2562 if (IN_SET(errno
, EAGAIN
, EINTR
))
2563 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2568 got_something
= true;
2570 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2571 process_inotify_event(j
, e
);
2575 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2579 assert_return(j
, -EINVAL
);
2580 assert_return(!journal_pid_changed(j
), -ECHILD
);
2582 if (j
->inotify_fd
< 0) {
2584 /* This is the first invocation, hence create the
2586 r
= sd_journal_get_fd(j
);
2590 /* The journal might have changed since the context
2591 * object was created and we weren't watching before,
2592 * hence don't wait for anything, and return
2594 return determine_change(j
);
2597 r
= sd_journal_get_timeout(j
, &t
);
2601 if (t
!= (uint64_t) -1) {
2604 n
= now(CLOCK_MONOTONIC
);
2605 t
= t
> n
? t
- n
: 0;
2607 if (timeout_usec
== (uint64_t) -1 || timeout_usec
> t
)
2612 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2613 } while (r
== -EINTR
);
2618 return sd_journal_process(j
);
2621 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2625 uint64_t fmin
= 0, tmax
= 0;
2628 assert_return(j
, -EINVAL
);
2629 assert_return(!journal_pid_changed(j
), -ECHILD
);
2630 assert_return(from
|| to
, -EINVAL
);
2631 assert_return(from
!= to
, -EINVAL
);
2633 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2636 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2649 fmin
= MIN(fr
, fmin
);
2650 tmax
= MAX(t
, tmax
);
2659 return first
? 0 : 1;
2662 _public_
int sd_journal_get_cutoff_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t *from
, uint64_t *to
) {
2668 assert_return(j
, -EINVAL
);
2669 assert_return(!journal_pid_changed(j
), -ECHILD
);
2670 assert_return(from
|| to
, -EINVAL
);
2671 assert_return(from
!= to
, -EINVAL
);
2673 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2676 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &fr
, &t
);
2686 *from
= MIN(fr
, *from
);
2701 void journal_print_header(sd_journal
*j
) {
2704 bool newline
= false;
2708 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2714 journal_file_print_header(f
);
2718 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *bytes
) {
2723 assert_return(j
, -EINVAL
);
2724 assert_return(!journal_pid_changed(j
), -ECHILD
);
2725 assert_return(bytes
, -EINVAL
);
2727 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2730 if (fstat(f
->fd
, &st
) < 0)
2733 sum
+= (uint64_t) st
.st_blocks
* 512ULL;
2740 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2743 assert_return(j
, -EINVAL
);
2744 assert_return(!journal_pid_changed(j
), -ECHILD
);
2745 assert_return(!isempty(field
), -EINVAL
);
2746 assert_return(field_is_valid(field
), -EINVAL
);
2752 free(j
->unique_field
);
2753 j
->unique_field
= f
;
2754 j
->unique_file
= NULL
;
2755 j
->unique_offset
= 0;
2756 j
->unique_file_lost
= false;
2761 _public_
int sd_journal_enumerate_unique(sd_journal
*j
, const void **data
, size_t *l
) {
2764 assert_return(j
, -EINVAL
);
2765 assert_return(!journal_pid_changed(j
), -ECHILD
);
2766 assert_return(data
, -EINVAL
);
2767 assert_return(l
, -EINVAL
);
2768 assert_return(j
->unique_field
, -EINVAL
);
2770 k
= strlen(j
->unique_field
);
2772 if (!j
->unique_file
) {
2773 if (j
->unique_file_lost
)
2776 j
->unique_file
= ordered_hashmap_first(j
->files
);
2777 if (!j
->unique_file
)
2780 j
->unique_offset
= 0;
2792 /* Proceed to next data object in the field's linked list */
2793 if (j
->unique_offset
== 0) {
2794 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2798 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2800 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2804 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2807 /* We reached the end of the list? Then start again, with the next file */
2808 if (j
->unique_offset
== 0) {
2809 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2810 if (!j
->unique_file
)
2816 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2817 * instead, so that we can look at this data object at the same
2818 * time as one on another file */
2819 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2823 /* Let's do the type check by hand, since we used 0 context above. */
2824 if (o
->object
.type
!= OBJECT_DATA
)
2825 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2826 "%s:offset " OFSfmt
": object has type %d, expected %d",
2827 j
->unique_file
->path
,
2829 o
->object
.type
, OBJECT_DATA
);
2831 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2835 /* Check if we have at least the field name and "=". */
2837 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2838 "%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2839 j
->unique_file
->path
,
2840 j
->unique_offset
, ol
, k
+ 1);
2842 if (memcmp(odata
, j
->unique_field
, k
) || ((const char*) odata
)[k
] != '=')
2843 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2844 "%s:offset " OFSfmt
": object does not start with \"%s=\"",
2845 j
->unique_file
->path
,
2849 /* OK, now let's see if we already returned this data
2850 * object by checking if it exists in the earlier
2851 * traversed files. */
2853 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2854 if (of
== j
->unique_file
)
2857 /* Skip this file it didn't have any fields indexed */
2858 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2861 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
2873 r
= return_data(j
, j
->unique_file
, o
, data
, l
);
2881 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
2885 j
->unique_file
= NULL
;
2886 j
->unique_offset
= 0;
2887 j
->unique_file_lost
= false;
2890 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
2893 assert_return(j
, -EINVAL
);
2894 assert_return(!journal_pid_changed(j
), -ECHILD
);
2895 assert_return(field
, -EINVAL
);
2897 if (!j
->fields_file
) {
2898 if (j
->fields_file_lost
)
2901 j
->fields_file
= ordered_hashmap_first(j
->files
);
2902 if (!j
->fields_file
)
2905 j
->fields_hash_table_index
= 0;
2906 j
->fields_offset
= 0;
2910 JournalFile
*f
, *of
;
2919 if (j
->fields_offset
== 0) {
2922 /* We are not yet positioned at any field. Let's pick the first one */
2923 r
= journal_file_map_field_hash_table(f
);
2927 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
2929 if (j
->fields_hash_table_index
>= m
) {
2930 /* Reached the end of the hash table, go to the next file. */
2935 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
2937 if (j
->fields_offset
!= 0)
2940 /* Empty hash table bucket, go to next one */
2941 j
->fields_hash_table_index
++;
2945 /* Proceed with next file */
2946 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
2947 if (!j
->fields_file
) {
2952 j
->fields_offset
= 0;
2953 j
->fields_hash_table_index
= 0;
2958 /* We are already positioned at a field. If so, let's figure out the next field from it */
2960 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
2964 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
2965 if (j
->fields_offset
== 0) {
2966 /* Reached the end of the hash table chain */
2967 j
->fields_hash_table_index
++;
2972 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
2973 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
2977 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
2978 if (o
->object
.type
!= OBJECT_FIELD
)
2979 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2980 "%s:offset " OFSfmt
": object has type %i, expected %i",
2981 f
->path
, j
->fields_offset
,
2982 o
->object
.type
, OBJECT_FIELD
);
2984 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
2986 /* Let's see if we already returned this field name before. */
2988 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2992 /* Skip this file it didn't have any fields indexed */
2993 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2996 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
, le64toh(o
->field
.hash
), NULL
, NULL
);
3008 /* Check if this is really a valid string containing no NUL byte */
3009 if (memchr(o
->field
.payload
, 0, sz
))
3012 if (sz
> j
->data_threshold
)
3013 sz
= j
->data_threshold
;
3015 if (!GREEDY_REALLOC(j
->fields_buffer
, j
->fields_buffer_allocated
, sz
+ 1))
3018 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3019 j
->fields_buffer
[sz
] = 0;
3021 if (!field_is_valid(j
->fields_buffer
))
3024 *field
= j
->fields_buffer
;
3029 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3033 j
->fields_file
= NULL
;
3034 j
->fields_hash_table_index
= 0;
3035 j
->fields_offset
= 0;
3036 j
->fields_file_lost
= false;
3039 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3040 assert_return(j
, -EINVAL
);
3041 assert_return(!journal_pid_changed(j
), -ECHILD
);
3043 return !j
->on_network
;
3046 static char *lookup_field(const char *field
, void *userdata
) {
3047 sd_journal
*j
= userdata
;
3055 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3057 size
> REPLACE_VAR_MAX
)
3058 return strdup(field
);
3060 d
= strlen(field
) + 1;
3062 return strndup((const char*) data
+ d
, size
- d
);
3065 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3069 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3073 assert_return(j
, -EINVAL
);
3074 assert_return(!journal_pid_changed(j
), -ECHILD
);
3075 assert_return(ret
, -EINVAL
);
3077 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3081 cid
= strndup((const char*) data
+ 11, size
- 11);
3085 r
= sd_id128_from_string(cid
, &id
);
3089 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
3093 t
= replace_var(text
, lookup_field
, j
);
3101 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3102 assert_return(ret
, -EINVAL
);
3104 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3107 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3108 assert_return(j
, -EINVAL
);
3109 assert_return(!journal_pid_changed(j
), -ECHILD
);
3111 j
->data_threshold
= sz
;
3115 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3116 assert_return(j
, -EINVAL
);
3117 assert_return(!journal_pid_changed(j
), -ECHILD
);
3118 assert_return(sz
, -EINVAL
);
3120 *sz
= j
->data_threshold
;
3124 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3125 assert_return(j
, -EINVAL
);
3127 return j
->has_runtime_files
;
3130 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3131 assert_return(j
, -EINVAL
);
3133 return j
->has_persistent_files
;