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"
42 #include "syslog-util.h"
44 #define JOURNAL_FILES_MAX 7168
46 #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
48 #define REPLACE_VAR_MAX 256
50 #define DEFAULT_DATA_THRESHOLD (64*1024)
52 static void remove_file_real(sd_journal
*j
, JournalFile
*f
);
54 static bool journal_pid_changed(sd_journal
*j
) {
57 /* We don't support people creating a journal object and
58 * keeping it around over a fork(). Let's complain. */
60 return j
->original_pid
!= getpid_cached();
63 static int journal_put_error(sd_journal
*j
, int r
, const char *path
) {
67 /* Memorize an error we encountered, and store which
68 * file/directory it was generated from. Note that we store
69 * only *one* path per error code, as the error code is the
70 * key into the hashmap, and the path is the value. This means
71 * we keep track only of all error kinds, but not of all error
72 * locations. This has the benefit that the hashmap cannot
75 * We return an error here only if we didn't manage to
76 * memorize the real error. */
81 k
= hashmap_ensure_allocated(&j
->errors
, NULL
);
92 k
= hashmap_put(j
->errors
, INT_TO_PTR(r
), copy
);
105 static void detach_location(sd_journal
*j
) {
111 j
->current_file
= NULL
;
112 j
->current_field
= 0;
114 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
)
115 journal_file_reset_location(f
);
118 static void reset_location(sd_journal
*j
) {
122 zero(j
->current_location
);
125 static void init_location(Location
*l
, LocationType type
, JournalFile
*f
, Object
*o
) {
127 assert(IN_SET(type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
129 assert(o
->object
.type
== OBJECT_ENTRY
);
132 l
->seqnum
= le64toh(o
->entry
.seqnum
);
133 l
->seqnum_id
= f
->header
->seqnum_id
;
134 l
->realtime
= le64toh(o
->entry
.realtime
);
135 l
->monotonic
= le64toh(o
->entry
.monotonic
);
136 l
->boot_id
= o
->entry
.boot_id
;
137 l
->xor_hash
= le64toh(o
->entry
.xor_hash
);
139 l
->seqnum_set
= l
->realtime_set
= l
->monotonic_set
= l
->xor_hash_set
= true;
142 static void set_location(sd_journal
*j
, JournalFile
*f
, Object
*o
) {
147 init_location(&j
->current_location
, LOCATION_DISCRETE
, f
, o
);
150 j
->current_field
= 0;
152 /* Let f know its candidate entry was picked. */
153 assert(f
->location_type
== LOCATION_SEEK
);
154 f
->location_type
= LOCATION_DISCRETE
;
157 static int match_is_valid(const void *data
, size_t size
) {
165 if (((char*) data
)[0] == '_' && ((char*) data
)[1] == '_')
169 for (p
= b
; p
< b
+ size
; p
++) {
177 if (*p
>= 'A' && *p
<= 'Z')
180 if (*p
>= '0' && *p
<= '9')
189 static bool same_field(const void *_a
, size_t s
, const void *_b
, size_t t
) {
190 const uint8_t *a
= _a
, *b
= _b
;
193 for (j
= 0; j
< s
&& j
< t
; j
++) {
202 assert_not_reached("\"=\" not found");
205 static Match
*match_new(Match
*p
, MatchType t
) {
218 LIST_PREPEND(matches
, p
->matches
, m
);
223 static void match_free(Match
*m
) {
227 match_free(m
->matches
);
230 LIST_REMOVE(matches
, m
->parent
->matches
, m
);
236 static void match_free_if_empty(Match
*m
) {
237 if (!m
|| m
->matches
)
243 _public_
int sd_journal_add_match(sd_journal
*j
, const void *data
, size_t size
) {
244 Match
*l3
, *l4
, *add_here
= NULL
, *m
;
247 assert_return(j
, -EINVAL
);
248 assert_return(!journal_pid_changed(j
), -ECHILD
);
249 assert_return(data
, -EINVAL
);
254 assert_return(match_is_valid(data
, size
), -EINVAL
);
260 * level 4: concrete matches */
263 j
->level0
= match_new(NULL
, MATCH_AND_TERM
);
269 j
->level1
= match_new(j
->level0
, MATCH_OR_TERM
);
275 j
->level2
= match_new(j
->level1
, MATCH_AND_TERM
);
280 assert(j
->level0
->type
== MATCH_AND_TERM
);
281 assert(j
->level1
->type
== MATCH_OR_TERM
);
282 assert(j
->level2
->type
== MATCH_AND_TERM
);
284 le_hash
= htole64(hash64(data
, size
));
286 LIST_FOREACH(matches
, l3
, j
->level2
->matches
) {
287 assert(l3
->type
== MATCH_OR_TERM
);
289 LIST_FOREACH(matches
, l4
, l3
->matches
) {
290 assert(l4
->type
== MATCH_DISCRETE
);
292 /* Exactly the same match already? Then ignore
294 if (l4
->le_hash
== le_hash
&&
296 memcmp(l4
->data
, data
, size
) == 0)
299 /* Same field? Then let's add this to this OR term */
300 if (same_field(data
, size
, l4
->data
, l4
->size
)) {
311 add_here
= match_new(j
->level2
, MATCH_OR_TERM
);
316 m
= match_new(add_here
, MATCH_DISCRETE
);
320 m
->le_hash
= le_hash
;
322 m
->data
= memdup(data
, size
);
331 match_free_if_empty(add_here
);
332 match_free_if_empty(j
->level2
);
333 match_free_if_empty(j
->level1
);
334 match_free_if_empty(j
->level0
);
339 _public_
int sd_journal_add_conjunction(sd_journal
*j
) {
340 assert_return(j
, -EINVAL
);
341 assert_return(!journal_pid_changed(j
), -ECHILD
);
349 if (!j
->level1
->matches
)
358 _public_
int sd_journal_add_disjunction(sd_journal
*j
) {
359 assert_return(j
, -EINVAL
);
360 assert_return(!journal_pid_changed(j
), -ECHILD
);
371 if (!j
->level2
->matches
)
378 static char *match_make_string(Match
*m
) {
381 bool enclose
= false;
384 return strdup("none");
386 if (m
->type
== MATCH_DISCRETE
)
387 return cescape_length(m
->data
, m
->size
);
389 LIST_FOREACH(matches
, i
, m
->matches
) {
392 t
= match_make_string(i
);
397 k
= strjoin(p
, m
->type
== MATCH_OR_TERM
? " OR " : " AND ", t
);
412 r
= strjoin("(", p
, ")");
420 char *journal_make_match_string(sd_journal
*j
) {
423 return match_make_string(j
->level0
);
426 _public_
void sd_journal_flush_matches(sd_journal
*j
) {
431 match_free(j
->level0
);
433 j
->level0
= j
->level1
= j
->level2
= NULL
;
438 _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 r
= CMP(f
->current_seqnum
, l
->seqnum
);
462 if (l
->monotonic_set
&&
463 sd_id128_equal(f
->current_boot_id
, l
->boot_id
)) {
465 r
= CMP(f
->current_monotonic
, l
->monotonic
);
470 if (l
->realtime_set
) {
472 r
= CMP(f
->current_realtime
, l
->realtime
);
477 if (l
->xor_hash_set
) {
479 r
= CMP(f
->current_xor_hash
, l
->xor_hash
);
487 static int next_for_match(
491 uint64_t after_offset
,
492 direction_t direction
,
504 if (m
->type
== MATCH_DISCRETE
) {
507 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
511 return journal_file_move_to_entry_by_offset_for_data(f
, dp
, after_offset
, direction
, ret
, offset
);
513 } else if (m
->type
== MATCH_OR_TERM
) {
516 /* Find the earliest match beyond after_offset */
518 LIST_FOREACH(matches
, i
, m
->matches
) {
521 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
525 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
533 } else if (m
->type
== MATCH_AND_TERM
) {
534 Match
*i
, *last_moved
;
536 /* Always jump to the next matching entry and repeat
537 * this until we find an offset that matches for all
543 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
547 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
548 last_moved
= m
->matches
;
550 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
553 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
557 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
558 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
567 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
579 static int find_location_for_match(
583 direction_t direction
,
593 if (m
->type
== MATCH_DISCRETE
) {
596 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
600 /* FIXME: missing: find by monotonic */
602 if (j
->current_location
.type
== LOCATION_HEAD
)
603 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_DOWN
, ret
, offset
);
604 if (j
->current_location
.type
== LOCATION_TAIL
)
605 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_UP
, ret
, offset
);
606 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
607 return journal_file_move_to_entry_by_seqnum_for_data(f
, dp
, j
->current_location
.seqnum
, direction
, ret
, offset
);
608 if (j
->current_location
.monotonic_set
) {
609 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, dp
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
613 if (j
->current_location
.realtime_set
)
614 return journal_file_move_to_entry_by_realtime_for_data(f
, dp
, j
->current_location
.realtime
, direction
, ret
, offset
);
616 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, direction
, ret
, offset
);
618 } else if (m
->type
== MATCH_OR_TERM
) {
623 /* Find the earliest match */
625 LIST_FOREACH(matches
, i
, m
->matches
) {
628 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
632 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
640 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
655 assert(m
->type
== MATCH_AND_TERM
);
657 /* First jump to the last match, and then find the
658 * next one where all matches match */
663 LIST_FOREACH(matches
, i
, m
->matches
) {
666 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
670 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
674 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
678 static int find_location_with_matches(
681 direction_t direction
,
693 /* No matches is simple */
695 if (j
->current_location
.type
== LOCATION_HEAD
)
696 return journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
);
697 if (j
->current_location
.type
== LOCATION_TAIL
)
698 return journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
);
699 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
700 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
701 if (j
->current_location
.monotonic_set
) {
702 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
706 if (j
->current_location
.realtime_set
)
707 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
709 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
711 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
714 static int next_with_matches(
717 direction_t direction
,
726 /* No matches is easy. We simple advance the file
729 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
731 /* If we have a match then we look for the next matching entry
732 * with an offset at least one step larger */
733 return next_for_match(j
, j
->level0
, f
,
734 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
735 : f
->current_offset
- 1,
736 direction
, ret
, offset
);
739 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
741 uint64_t cp
, n_entries
;
747 n_entries
= le64toh(f
->header
->n_entries
);
749 /* If we hit EOF before, we don't need to look into this file again
750 * unless direction changed or new entries appeared. */
751 if (f
->last_direction
== direction
&& f
->location_type
== LOCATION_TAIL
&&
752 n_entries
== f
->last_n_entries
)
755 f
->last_n_entries
= n_entries
;
757 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
758 /* LOCATION_SEEK here means we did the work in a previous
759 * iteration and the current location already points to a
760 * candidate entry. */
761 if (f
->location_type
!= LOCATION_SEEK
) {
762 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
766 journal_file_save_location(f
, c
, cp
);
769 f
->last_direction
= direction
;
771 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
775 journal_file_save_location(f
, c
, cp
);
778 /* OK, we found the spot, now let's advance until an entry
779 * that is actually different from what we were previously
780 * looking at. This is necessary to handle entries which exist
781 * in two (or more) journal files, and which shall all be
782 * suppressed but one. */
787 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
790 k
= compare_with_location(f
, &j
->current_location
);
792 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
799 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
803 journal_file_save_location(f
, c
, cp
);
807 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
808 JournalFile
*new_file
= NULL
;
814 assert_return(j
, -EINVAL
);
815 assert_return(!journal_pid_changed(j
), -ECHILD
);
817 r
= iterated_cache_get(j
->files_cache
, NULL
, &files
, &n_files
);
821 for (i
= 0; i
< n_files
; i
++) {
822 JournalFile
*f
= (JournalFile
*)files
[i
];
825 r
= next_beyond_location(j
, f
, direction
);
827 log_debug_errno(r
, "Can't iterate through %s, ignoring: %m", f
->path
);
828 remove_file_real(j
, f
);
831 f
->location_type
= LOCATION_TAIL
;
840 k
= journal_file_compare_locations(f
, new_file
);
842 found
= direction
== DIRECTION_DOWN
? k
< 0 : k
> 0;
852 r
= journal_file_move_to_object(new_file
, OBJECT_ENTRY
, new_file
->current_offset
, &o
);
856 set_location(j
, new_file
, o
);
861 _public_
int sd_journal_next(sd_journal
*j
) {
862 return real_journal_next(j
, DIRECTION_DOWN
);
865 _public_
int sd_journal_previous(sd_journal
*j
) {
866 return real_journal_next(j
, DIRECTION_UP
);
869 static int real_journal_next_skip(sd_journal
*j
, direction_t direction
, uint64_t skip
) {
872 assert_return(j
, -EINVAL
);
873 assert_return(!journal_pid_changed(j
), -ECHILD
);
876 /* If this is not a discrete skip, then at least
877 * resolve the current location */
878 if (j
->current_location
.type
!= LOCATION_DISCRETE
) {
879 r
= real_journal_next(j
, direction
);
888 r
= real_journal_next(j
, direction
);
902 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
903 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
906 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
907 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
910 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
913 char bid
[SD_ID128_STRING_MAX
], sid
[SD_ID128_STRING_MAX
];
915 assert_return(j
, -EINVAL
);
916 assert_return(!journal_pid_changed(j
), -ECHILD
);
917 assert_return(cursor
, -EINVAL
);
919 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
920 return -EADDRNOTAVAIL
;
922 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
926 sd_id128_to_string(j
->current_file
->header
->seqnum_id
, sid
);
927 sd_id128_to_string(o
->entry
.boot_id
, bid
);
930 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
931 sid
, le64toh(o
->entry
.seqnum
),
932 bid
, le64toh(o
->entry
.monotonic
),
933 le64toh(o
->entry
.realtime
),
934 le64toh(o
->entry
.xor_hash
)) < 0)
940 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
941 const char *word
, *state
;
943 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
945 seqnum_id_set
= false,
948 monotonic_set
= false,
949 realtime_set
= false,
950 xor_hash_set
= false;
951 sd_id128_t seqnum_id
, boot_id
;
953 assert_return(j
, -EINVAL
);
954 assert_return(!journal_pid_changed(j
), -ECHILD
);
955 assert_return(!isempty(cursor
), -EINVAL
);
957 FOREACH_WORD_SEPARATOR(word
, l
, cursor
, ";", state
) {
961 if (l
< 2 || word
[1] != '=')
964 item
= strndup(word
, l
);
971 seqnum_id_set
= true;
972 k
= sd_id128_from_string(item
+2, &seqnum_id
);
977 if (sscanf(item
+2, "%llx", &seqnum
) != 1)
983 k
= sd_id128_from_string(item
+2, &boot_id
);
987 monotonic_set
= true;
988 if (sscanf(item
+2, "%llx", &monotonic
) != 1)
994 if (sscanf(item
+2, "%llx", &realtime
) != 1)
1000 if (sscanf(item
+2, "%llx", &xor_hash
) != 1)
1011 if ((!seqnum_set
|| !seqnum_id_set
) &&
1012 (!monotonic_set
|| !boot_id_set
) &&
1018 j
->current_location
.type
= LOCATION_SEEK
;
1021 j
->current_location
.realtime
= (uint64_t) realtime
;
1022 j
->current_location
.realtime_set
= true;
1025 if (seqnum_set
&& seqnum_id_set
) {
1026 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1027 j
->current_location
.seqnum_id
= seqnum_id
;
1028 j
->current_location
.seqnum_set
= true;
1031 if (monotonic_set
&& boot_id_set
) {
1032 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1033 j
->current_location
.boot_id
= boot_id
;
1034 j
->current_location
.monotonic_set
= true;
1038 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1039 j
->current_location
.xor_hash_set
= true;
1045 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1049 assert_return(j
, -EINVAL
);
1050 assert_return(!journal_pid_changed(j
), -ECHILD
);
1051 assert_return(!isempty(cursor
), -EINVAL
);
1053 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1054 return -EADDRNOTAVAIL
;
1056 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1061 _cleanup_free_
char *item
= NULL
;
1062 unsigned long long ll
;
1066 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1073 if (strlen(item
) < 2 || item
[1] != '=')
1079 k
= sd_id128_from_string(item
+2, &id
);
1082 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1087 if (sscanf(item
+2, "%llx", &ll
) != 1)
1089 if (ll
!= le64toh(o
->entry
.seqnum
))
1094 k
= sd_id128_from_string(item
+2, &id
);
1097 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1102 if (sscanf(item
+2, "%llx", &ll
) != 1)
1104 if (ll
!= le64toh(o
->entry
.monotonic
))
1109 if (sscanf(item
+2, "%llx", &ll
) != 1)
1111 if (ll
!= le64toh(o
->entry
.realtime
))
1116 if (sscanf(item
+2, "%llx", &ll
) != 1)
1118 if (ll
!= le64toh(o
->entry
.xor_hash
))
1127 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t usec
) {
1128 assert_return(j
, -EINVAL
);
1129 assert_return(!journal_pid_changed(j
), -ECHILD
);
1132 j
->current_location
.type
= LOCATION_SEEK
;
1133 j
->current_location
.boot_id
= boot_id
;
1134 j
->current_location
.monotonic
= usec
;
1135 j
->current_location
.monotonic_set
= true;
1140 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1141 assert_return(j
, -EINVAL
);
1142 assert_return(!journal_pid_changed(j
), -ECHILD
);
1145 j
->current_location
.type
= LOCATION_SEEK
;
1146 j
->current_location
.realtime
= usec
;
1147 j
->current_location
.realtime_set
= true;
1152 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1153 assert_return(j
, -EINVAL
);
1154 assert_return(!journal_pid_changed(j
), -ECHILD
);
1157 j
->current_location
.type
= LOCATION_HEAD
;
1162 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1163 assert_return(j
, -EINVAL
);
1164 assert_return(!journal_pid_changed(j
), -ECHILD
);
1167 j
->current_location
.type
= LOCATION_TAIL
;
1172 static void check_network(sd_journal
*j
, int fd
) {
1178 j
->on_network
= fd_is_network_fs(fd
);
1181 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1182 const char *full
, *tilded
, *atted
;
1184 full
= strjoina(prefix
, ".journal");
1185 tilded
= strjoina(full
, "~");
1186 atted
= strjoina(prefix
, "@");
1188 return STR_IN_SET(filename
, full
, tilded
) ||
1189 startswith(filename
, atted
);
1192 static bool file_type_wanted(int flags
, const char *filename
) {
1195 if (!endswith(filename
, ".journal") && !endswith(filename
, ".journal~"))
1198 /* no flags set → every type is OK */
1199 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1202 if (flags
& SD_JOURNAL_SYSTEM
&& file_has_type_prefix("system", filename
))
1205 if (flags
& SD_JOURNAL_CURRENT_USER
) {
1206 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1208 xsprintf(prefix
, "user-"UID_FMT
, getuid());
1210 if (file_has_type_prefix(prefix
, filename
))
1217 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1222 if (j
->toplevel_fd
>= 0)
1225 return path_startswith(path
, prefix
);
1228 static void track_file_disposition(sd_journal
*j
, JournalFile
*f
) {
1232 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1233 j
->has_runtime_files
= true;
1234 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1235 j
->has_persistent_files
= true;
1238 static const char *skip_slash(const char *p
) {
1249 static int add_any_file(
1254 bool close_fd
= false;
1260 assert(fd
>= 0 || path
);
1263 if (j
->toplevel_fd
>= 0)
1264 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1265 * openat() ignores the first argument. */
1267 fd
= openat(j
->toplevel_fd
, skip_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1269 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1271 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1277 r
= fd_nonblock(fd
, false);
1279 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1284 if (fstat(fd
, &st
) < 0) {
1285 r
= log_debug_errno(errno
, "Failed to fstat file '%s': %m", path
);
1289 r
= stat_verify_regular(&st
);
1291 log_debug_errno(r
, "Refusing to open '%s', as it is not a regular file.", path
);
1295 f
= ordered_hashmap_get(j
->files
, path
);
1297 if (f
->last_stat
.st_dev
== st
.st_dev
&&
1298 f
->last_stat
.st_ino
== st
.st_ino
) {
1300 /* We already track this file, under the same path and with the same device/inode numbers, it's
1301 * hence really the same. Mark this file as seen in this generation. This is used to GC old
1302 * files in process_q_overflow() to detect journal files that are still there and discern them
1303 * from those which are gone. */
1305 f
->last_seen_generation
= j
->generation
;
1310 /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
1311 * replaced (probably due to rotation?), let's drop it hence from our list. */
1312 remove_file_real(j
, f
);
1316 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1317 log_debug("Too many open journal files, not adding %s.", path
);
1322 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, false, 0, false, NULL
, j
->mmap
, NULL
, NULL
, &f
);
1324 log_debug_errno(r
, "Failed to open journal file %s: %m", path
);
1328 /* journal_file_dump(f); */
1330 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1332 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 */
1333 (void) journal_file_close(f
);
1337 close_fd
= false; /* the fd is now owned by the JournalFile object */
1339 f
->last_seen_generation
= j
->generation
;
1341 track_file_disposition(j
, f
);
1342 check_network(j
, f
->fd
);
1344 j
->current_invalidate_counter
++;
1346 log_debug("File %s added.", f
->path
);
1355 k
= journal_put_error(j
, r
, path
);
1363 static int add_file_by_name(
1366 const char *filename
) {
1374 if (j
->no_new_files
)
1377 if (!file_type_wanted(j
->flags
, filename
))
1380 path
= prefix_roota(prefix
, filename
);
1381 return add_any_file(j
, -1, path
);
1384 static void remove_file_by_name(
1387 const char *filename
) {
1396 path
= prefix_roota(prefix
, filename
);
1397 f
= ordered_hashmap_get(j
->files
, path
);
1401 remove_file_real(j
, f
);
1404 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1408 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1410 log_debug("File %s removed.", f
->path
);
1412 if (j
->current_file
== f
) {
1413 j
->current_file
= NULL
;
1414 j
->current_field
= 0;
1417 if (j
->unique_file
== f
) {
1418 /* Jump to the next unique_file or NULL if that one was last */
1419 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1420 j
->unique_offset
= 0;
1421 if (!j
->unique_file
)
1422 j
->unique_file_lost
= true;
1425 if (j
->fields_file
== f
) {
1426 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1427 j
->fields_offset
= 0;
1428 if (!j
->fields_file
)
1429 j
->fields_file_lost
= true;
1432 (void) journal_file_close(f
);
1434 j
->current_invalidate_counter
++;
1437 static int dirname_is_machine_id(const char *fn
) {
1438 sd_id128_t id
, machine
;
1442 /* Returns true if the specified directory name matches the local machine ID */
1444 r
= sd_id128_get_machine(&machine
);
1448 e
= strchr(fn
, '.');
1452 /* Looks like it has a namespace suffix. Verify that. */
1453 if (!log_namespace_name_valid(e
+ 1))
1456 k
= strndupa(fn
, e
- fn
);
1457 r
= sd_id128_from_string(k
, &id
);
1459 r
= sd_id128_from_string(fn
, &id
);
1463 return sd_id128_equal(id
, machine
);
1466 static int dirname_has_namespace(const char *fn
, const char *namespace) {
1469 /* Returns true if the specified directory name matches the specified namespace */
1471 e
= strchr(fn
, '.');
1478 if (!streq(e
+ 1, namespace))
1481 k
= strndupa(fn
, e
- fn
);
1482 return id128_is_valid(k
);
1488 return id128_is_valid(fn
);
1491 static bool dirent_is_journal_file(const struct dirent
*de
) {
1494 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1496 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1499 return endswith(de
->d_name
, ".journal") ||
1500 endswith(de
->d_name
, ".journal~");
1503 static bool dirent_is_journal_subdir(const struct dirent
*de
) {
1507 /* returns true if the specified directory entry looks like a directory that might contain journal
1508 * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a
1511 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1514 e
= strchr(de
->d_name
, '.');
1516 return id128_is_valid(de
->d_name
); /* No namespace */
1518 n
= strndupa(de
->d_name
, e
- de
->d_name
);
1519 if (!id128_is_valid(n
))
1522 return log_namespace_name_valid(e
+ 1);
1525 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1532 if (j
->toplevel_fd
< 0)
1535 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1536 * relative, by dropping the initial slash */
1537 d
= xopendirat(j
->toplevel_fd
, skip_slash(path
), 0);
1545 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1547 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1554 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1556 if (dirent_is_journal_file(de
))
1557 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1559 if (m
->is_root
&& dirent_is_journal_subdir(de
))
1560 (void) add_directory(j
, m
->path
, de
->d_name
);
1566 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1569 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1576 /* Watch this directory if that's enabled and if it not being watched yet. */
1578 if (m
->wd
> 0) /* Already have a watch? */
1580 if (j
->inotify_fd
< 0) /* Not watching at all? */
1583 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1585 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1589 r
= hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
);
1591 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1593 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1594 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1599 static int add_directory(
1602 const char *dirname
) {
1604 _cleanup_free_
char *path
= NULL
;
1605 _cleanup_closedir_
DIR *d
= NULL
;
1612 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1613 * and reenumerates directory contents */
1615 path
= path_join(prefix
, dirname
);
1621 log_debug("Considering directory '%s'.", path
);
1623 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1624 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1625 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1628 if (!(FLAGS_SET(j
->flags
, SD_JOURNAL_ALL_NAMESPACES
) ||
1629 dirname_has_namespace(dirname
, j
->namespace) > 0 ||
1630 (FLAGS_SET(j
->flags
, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE
) && dirname_has_namespace(dirname
, NULL
) > 0)))
1633 r
= directory_open(j
, path
, &d
);
1635 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
1639 m
= hashmap_get(j
->directories_by_path
, path
);
1641 m
= new(Directory
, 1);
1652 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1658 path
= NULL
; /* avoid freeing in cleanup */
1659 j
->current_invalidate_counter
++;
1661 log_debug("Directory %s added.", m
->path
);
1663 } else if (m
->is_root
)
1664 return 0; /* Don't 'downgrade' from root directory */
1666 m
->last_seen_generation
= j
->generation
;
1668 directory_watch(j
, m
, dirfd(d
),
1669 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1670 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
1673 if (!j
->no_new_files
)
1674 directory_enumerate(j
, m
, d
);
1676 check_network(j
, dirfd(d
));
1681 k
= journal_put_error(j
, r
, path
?: prefix
);
1688 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
1690 _cleanup_closedir_
DIR *d
= NULL
;
1696 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1697 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1698 * populate the set, as well as to update it later. */
1701 /* If there's a path specified, use it. */
1703 log_debug("Considering root directory '%s'.", p
);
1705 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
1706 !path_has_prefix(j
, p
, "/run"))
1710 p
= strjoina(j
->prefix
, p
);
1712 r
= directory_open(j
, p
, &d
);
1713 if (r
== -ENOENT
&& missing_ok
)
1716 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
1722 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1723 * opendir() will take possession of the fd, and close it, which we don't want. */
1725 p
= "."; /* store this as "." in the directories hashmap */
1727 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
1743 m
= hashmap_get(j
->directories_by_path
, p
);
1745 m
= new0(Directory
, 1);
1753 m
->path
= strdup(p
);
1760 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1767 j
->current_invalidate_counter
++;
1769 log_debug("Root directory %s added.", m
->path
);
1771 } else if (!m
->is_root
)
1774 directory_watch(j
, m
, dirfd(d
),
1775 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1778 if (!j
->no_new_files
)
1779 directory_enumerate(j
, m
, d
);
1781 check_network(j
, dirfd(d
));
1786 k
= journal_put_error(j
, r
, p
);
1793 static void remove_directory(sd_journal
*j
, Directory
*d
) {
1797 hashmap_remove(j
->directories_by_wd
, INT_TO_PTR(d
->wd
));
1799 if (j
->inotify_fd
>= 0)
1800 (void) inotify_rm_watch(j
->inotify_fd
, d
->wd
);
1803 hashmap_remove(j
->directories_by_path
, d
->path
);
1806 log_debug("Root directory %s removed.", d
->path
);
1808 log_debug("Directory %s removed.", d
->path
);
1814 static int add_search_paths(sd_journal
*j
) {
1816 static const char search_paths
[] =
1817 "/run/log/journal\0"
1818 "/var/log/journal\0";
1823 /* We ignore most errors here, since the idea is to only open
1824 * what's actually accessible, and ignore the rest. */
1826 NULSTR_FOREACH(p
, search_paths
)
1827 (void) add_root_directory(j
, p
, true);
1829 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
1830 (void) add_root_directory(j
, "/var/log/journal/remote", true);
1835 static int add_current_paths(sd_journal
*j
) {
1840 assert(j
->no_new_files
);
1842 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1843 * treat them as fatal. */
1845 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
1846 _cleanup_free_
char *dir
;
1849 dir
= dirname_malloc(f
->path
);
1853 r
= add_directory(j
, dir
, NULL
);
1861 static int allocate_inotify(sd_journal
*j
) {
1864 if (j
->inotify_fd
< 0) {
1865 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1866 if (j
->inotify_fd
< 0)
1870 return hashmap_ensure_allocated(&j
->directories_by_wd
, NULL
);
1873 static sd_journal
*journal_new(int flags
, const char *path
, const char *namespace) {
1874 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1876 j
= new0(sd_journal
, 1);
1880 j
->original_pid
= getpid_cached();
1881 j
->toplevel_fd
= -1;
1884 j
->data_threshold
= DEFAULT_DATA_THRESHOLD
;
1893 if (flags
& SD_JOURNAL_OS_ROOT
)
1900 j
->namespace = strdup(namespace);
1905 j
->files
= ordered_hashmap_new(&path_hash_ops
);
1909 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
1910 j
->directories_by_path
= hashmap_new(&path_hash_ops
);
1911 j
->mmap
= mmap_cache_new();
1912 if (!j
->files_cache
|| !j
->directories_by_path
|| !j
->mmap
)
1918 #define OPEN_ALLOWED_FLAGS \
1919 (SD_JOURNAL_LOCAL_ONLY | \
1920 SD_JOURNAL_RUNTIME_ONLY | \
1921 SD_JOURNAL_SYSTEM | \
1922 SD_JOURNAL_CURRENT_USER | \
1923 SD_JOURNAL_ALL_NAMESPACES | \
1924 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
1926 _public_
int sd_journal_open_namespace(sd_journal
**ret
, const char *namespace, int flags
) {
1927 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1930 assert_return(ret
, -EINVAL
);
1931 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
1933 j
= journal_new(flags
, NULL
, namespace);
1937 r
= add_search_paths(j
);
1945 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1946 return sd_journal_open_namespace(ret
, NULL
, flags
);
1949 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1950 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1952 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1953 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1954 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1958 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1959 * combination with sd_journal_open_directory_fd(). */
1961 assert_return(machine
, -EINVAL
);
1962 assert_return(ret
, -EINVAL
);
1963 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
1964 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1966 p
= strjoina("/run/systemd/machines/", machine
);
1967 r
= parse_env_file(NULL
, p
,
1977 if (!streq_ptr(class, "container"))
1980 j
= journal_new(flags
, root
, NULL
);
1984 r
= add_search_paths(j
);
1992 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
1993 (SD_JOURNAL_OS_ROOT | \
1994 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1996 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
1997 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2000 assert_return(ret
, -EINVAL
);
2001 assert_return(path
, -EINVAL
);
2002 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
2004 j
= journal_new(flags
, path
, NULL
);
2008 if (flags
& SD_JOURNAL_OS_ROOT
)
2009 r
= add_search_paths(j
);
2011 r
= add_root_directory(j
, path
, false);
2019 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
2020 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2024 assert_return(ret
, -EINVAL
);
2025 assert_return(flags
== 0, -EINVAL
);
2027 j
= journal_new(flags
, NULL
, NULL
);
2031 STRV_FOREACH(path
, paths
) {
2032 r
= add_any_file(j
, -1, *path
);
2037 j
->no_new_files
= true;
2043 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
2044 (SD_JOURNAL_OS_ROOT | \
2045 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2047 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
2048 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2052 assert_return(ret
, -EINVAL
);
2053 assert_return(fd
>= 0, -EBADF
);
2054 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2056 if (fstat(fd
, &st
) < 0)
2059 if (!S_ISDIR(st
.st_mode
))
2062 j
= journal_new(flags
, NULL
, NULL
);
2066 j
->toplevel_fd
= fd
;
2068 if (flags
& SD_JOURNAL_OS_ROOT
)
2069 r
= add_search_paths(j
);
2071 r
= add_root_directory(j
, NULL
, false);
2079 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2082 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2086 assert_return(ret
, -EINVAL
);
2087 assert_return(n_fds
> 0, -EBADF
);
2088 assert_return(flags
== 0, -EINVAL
);
2090 j
= journal_new(flags
, NULL
, NULL
);
2094 for (i
= 0; i
< n_fds
; i
++) {
2102 if (fstat(fds
[i
], &st
) < 0) {
2107 r
= stat_verify_regular(&st
);
2111 r
= add_any_file(j
, fds
[i
], NULL
);
2116 j
->no_new_files
= true;
2117 j
->no_inotify
= true;
2123 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2125 ORDERED_HASHMAP_FOREACH(f
, j
->files
, iterator
)
2126 f
->close_fd
= false;
2131 _public_
void sd_journal_close(sd_journal
*j
) {
2137 sd_journal_flush_matches(j
);
2139 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2140 iterated_cache_free(j
->files_cache
);
2142 while ((d
= hashmap_first(j
->directories_by_path
)))
2143 remove_directory(j
, d
);
2145 while ((d
= hashmap_first(j
->directories_by_wd
)))
2146 remove_directory(j
, d
);
2148 hashmap_free(j
->directories_by_path
);
2149 hashmap_free(j
->directories_by_wd
);
2151 safe_close(j
->inotify_fd
);
2154 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j
->mmap
), mmap_cache_get_missed(j
->mmap
));
2155 mmap_cache_unref(j
->mmap
);
2158 hashmap_free_free(j
->errors
);
2163 free(j
->unique_field
);
2164 free(j
->fields_buffer
);
2168 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2173 assert_return(j
, -EINVAL
);
2174 assert_return(!journal_pid_changed(j
), -ECHILD
);
2175 assert_return(ret
, -EINVAL
);
2177 f
= j
->current_file
;
2179 return -EADDRNOTAVAIL
;
2181 if (f
->current_offset
<= 0)
2182 return -EADDRNOTAVAIL
;
2184 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2188 *ret
= le64toh(o
->entry
.realtime
);
2192 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2198 assert_return(j
, -EINVAL
);
2199 assert_return(!journal_pid_changed(j
), -ECHILD
);
2201 f
= j
->current_file
;
2203 return -EADDRNOTAVAIL
;
2205 if (f
->current_offset
<= 0)
2206 return -EADDRNOTAVAIL
;
2208 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2213 *ret_boot_id
= o
->entry
.boot_id
;
2215 r
= sd_id128_get_boot(&id
);
2219 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2224 *ret
= le64toh(o
->entry
.monotonic
);
2229 static bool field_is_valid(const char *field
) {
2237 if (startswith(field
, "__"))
2240 for (p
= field
; *p
; p
++) {
2245 if (*p
>= 'A' && *p
<= 'Z')
2248 if (*p
>= '0' && *p
<= '9')
2257 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2260 size_t field_length
;
2264 assert_return(j
, -EINVAL
);
2265 assert_return(!journal_pid_changed(j
), -ECHILD
);
2266 assert_return(field
, -EINVAL
);
2267 assert_return(data
, -EINVAL
);
2268 assert_return(size
, -EINVAL
);
2269 assert_return(field_is_valid(field
), -EINVAL
);
2271 f
= j
->current_file
;
2273 return -EADDRNOTAVAIL
;
2275 if (f
->current_offset
<= 0)
2276 return -EADDRNOTAVAIL
;
2278 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2282 field_length
= strlen(field
);
2284 n
= journal_file_entry_n_items(o
);
2285 for (i
= 0; i
< n
; i
++) {
2291 p
= le64toh(o
->entry
.items
[i
].object_offset
);
2292 le_hash
= o
->entry
.items
[i
].hash
;
2293 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2297 if (le_hash
!= o
->data
.hash
)
2300 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2302 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2304 #if HAVE_XZ || HAVE_LZ4
2305 r
= decompress_startswith(compression
,
2307 &f
->compress_buffer
, &f
->compress_buffer_size
,
2308 field
, field_length
, '=');
2310 log_debug_errno(r
, "Cannot decompress %s object of length %"PRIu64
" at offset "OFSfmt
": %m",
2311 object_compressed_to_string(compression
), l
, p
);
2316 r
= decompress_blob(compression
,
2318 &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
,
2323 *data
= f
->compress_buffer
;
2324 *size
= (size_t) rsize
;
2329 return -EPROTONOSUPPORT
;
2331 } else if (l
>= field_length
+1 &&
2332 memcmp(o
->data
.payload
, field
, field_length
) == 0 &&
2333 o
->data
.payload
[field_length
] == '=') {
2337 if ((uint64_t) t
!= l
)
2340 *data
= o
->data
.payload
;
2346 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2354 static int return_data(sd_journal
*j
, JournalFile
*f
, Object
*o
, const void **data
, size_t *size
) {
2359 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2362 /* We can't read objects larger than 4G on a 32bit machine */
2363 if ((uint64_t) t
!= l
)
2366 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2368 #if HAVE_XZ || HAVE_LZ4
2372 r
= decompress_blob(compression
,
2373 o
->data
.payload
, l
, &f
->compress_buffer
,
2374 &f
->compress_buffer_size
, &rsize
, j
->data_threshold
);
2378 *data
= f
->compress_buffer
;
2379 *size
= (size_t) rsize
;
2381 return -EPROTONOSUPPORT
;
2384 *data
= o
->data
.payload
;
2391 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2398 assert_return(j
, -EINVAL
);
2399 assert_return(!journal_pid_changed(j
), -ECHILD
);
2400 assert_return(data
, -EINVAL
);
2401 assert_return(size
, -EINVAL
);
2403 f
= j
->current_file
;
2405 return -EADDRNOTAVAIL
;
2407 if (f
->current_offset
<= 0)
2408 return -EADDRNOTAVAIL
;
2410 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2414 n
= journal_file_entry_n_items(o
);
2415 if (j
->current_field
>= n
)
2418 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2419 le_hash
= o
->entry
.items
[j
->current_field
].hash
;
2420 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2424 if (le_hash
!= o
->data
.hash
)
2427 r
= return_data(j
, f
, o
, data
, size
);
2436 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2440 j
->current_field
= 0;
2443 static int reiterate_all_paths(sd_journal
*j
) {
2446 if (j
->no_new_files
)
2447 return add_current_paths(j
);
2449 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2450 return add_search_paths(j
);
2452 if (j
->toplevel_fd
>= 0)
2453 return add_root_directory(j
, NULL
, false);
2456 return add_root_directory(j
, j
->path
, true);
2458 return add_search_paths(j
);
2461 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2464 assert_return(j
, -EINVAL
);
2465 assert_return(!journal_pid_changed(j
), -ECHILD
);
2468 return -EMEDIUMTYPE
;
2470 if (j
->inotify_fd
>= 0)
2471 return j
->inotify_fd
;
2473 r
= allocate_inotify(j
);
2477 log_debug("Reiterating files to get inotify watches established.");
2479 /* Iterate through all dirs again, to add them to the inotify */
2480 r
= reiterate_all_paths(j
);
2484 return j
->inotify_fd
;
2487 _public_
int sd_journal_get_events(sd_journal
*j
) {
2490 assert_return(j
, -EINVAL
);
2491 assert_return(!journal_pid_changed(j
), -ECHILD
);
2493 fd
= sd_journal_get_fd(j
);
2500 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2503 assert_return(j
, -EINVAL
);
2504 assert_return(!journal_pid_changed(j
), -ECHILD
);
2505 assert_return(timeout_usec
, -EINVAL
);
2507 fd
= sd_journal_get_fd(j
);
2511 if (!j
->on_network
) {
2512 *timeout_usec
= (uint64_t) -1;
2516 /* If we are on the network we need to regularly check for
2517 * changes manually */
2519 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2523 static void process_q_overflow(sd_journal
*j
) {
2530 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2531 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2532 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2533 * are subject for unloading. */
2535 log_debug("Inotify queue overrun, reiterating everything.");
2538 (void) reiterate_all_paths(j
);
2540 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2542 if (f
->last_seen_generation
== j
->generation
)
2545 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2546 remove_file_real(j
, f
);
2549 HASHMAP_FOREACH(m
, j
->directories_by_path
, i
) {
2551 if (m
->last_seen_generation
== j
->generation
)
2554 if (m
->is_root
) /* Never GC root directories */
2557 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2558 remove_directory(j
, m
);
2561 log_debug("Reiteration complete.");
2564 static void process_inotify_event(sd_journal
*j
, const struct inotify_event
*e
) {
2570 if (e
->mask
& IN_Q_OVERFLOW
) {
2571 process_q_overflow(j
);
2575 /* Is this a subdirectory we watch? */
2576 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2578 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2579 (endswith(e
->name
, ".journal") ||
2580 endswith(e
->name
, ".journal~"))) {
2582 /* Event for a journal file */
2584 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2585 (void) add_file_by_name(j
, d
->path
, e
->name
);
2586 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2587 remove_file_by_name(j
, d
->path
, e
->name
);
2589 } else if (!d
->is_root
&& e
->len
== 0) {
2591 /* Event for a subdirectory */
2593 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2594 remove_directory(j
, d
);
2596 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2598 /* Event for root directory */
2600 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2601 (void) add_directory(j
, d
->path
, e
->name
);
2607 if (e
->mask
& IN_IGNORED
)
2610 log_debug("Unexpected inotify event.");
2613 static int determine_change(sd_journal
*j
) {
2618 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2619 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2621 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2624 _public_
int sd_journal_process(sd_journal
*j
) {
2625 bool got_something
= false;
2627 assert_return(j
, -EINVAL
);
2628 assert_return(!journal_pid_changed(j
), -ECHILD
);
2630 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
2633 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2634 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2637 union inotify_event_buffer buffer
;
2638 struct inotify_event
*e
;
2641 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2643 if (IN_SET(errno
, EAGAIN
, EINTR
))
2644 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2649 got_something
= true;
2651 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2652 process_inotify_event(j
, e
);
2656 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2660 assert_return(j
, -EINVAL
);
2661 assert_return(!journal_pid_changed(j
), -ECHILD
);
2663 if (j
->inotify_fd
< 0) {
2665 /* This is the first invocation, hence create the
2667 r
= sd_journal_get_fd(j
);
2671 /* The journal might have changed since the context
2672 * object was created and we weren't watching before,
2673 * hence don't wait for anything, and return
2675 return determine_change(j
);
2678 r
= sd_journal_get_timeout(j
, &t
);
2682 if (t
!= (uint64_t) -1) {
2685 n
= now(CLOCK_MONOTONIC
);
2686 t
= t
> n
? t
- n
: 0;
2688 if (timeout_usec
== (uint64_t) -1 || timeout_usec
> t
)
2693 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2694 } while (r
== -EINTR
);
2699 return sd_journal_process(j
);
2702 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2706 uint64_t fmin
= 0, tmax
= 0;
2709 assert_return(j
, -EINVAL
);
2710 assert_return(!journal_pid_changed(j
), -ECHILD
);
2711 assert_return(from
|| to
, -EINVAL
);
2712 assert_return(from
!= to
, -EINVAL
);
2714 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2717 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2730 fmin
= MIN(fr
, fmin
);
2731 tmax
= MAX(t
, tmax
);
2740 return first
? 0 : 1;
2743 _public_
int sd_journal_get_cutoff_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t *from
, uint64_t *to
) {
2749 assert_return(j
, -EINVAL
);
2750 assert_return(!journal_pid_changed(j
), -ECHILD
);
2751 assert_return(from
|| to
, -EINVAL
);
2752 assert_return(from
!= to
, -EINVAL
);
2754 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2757 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &fr
, &t
);
2767 *from
= MIN(fr
, *from
);
2782 void journal_print_header(sd_journal
*j
) {
2785 bool newline
= false;
2789 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2795 journal_file_print_header(f
);
2799 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *bytes
) {
2804 assert_return(j
, -EINVAL
);
2805 assert_return(!journal_pid_changed(j
), -ECHILD
);
2806 assert_return(bytes
, -EINVAL
);
2808 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2811 if (fstat(f
->fd
, &st
) < 0)
2814 sum
+= (uint64_t) st
.st_blocks
* 512ULL;
2821 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2824 assert_return(j
, -EINVAL
);
2825 assert_return(!journal_pid_changed(j
), -ECHILD
);
2826 assert_return(!isempty(field
), -EINVAL
);
2827 assert_return(field_is_valid(field
), -EINVAL
);
2833 free(j
->unique_field
);
2834 j
->unique_field
= f
;
2835 j
->unique_file
= NULL
;
2836 j
->unique_offset
= 0;
2837 j
->unique_file_lost
= false;
2842 _public_
int sd_journal_enumerate_unique(sd_journal
*j
, const void **data
, size_t *l
) {
2845 assert_return(j
, -EINVAL
);
2846 assert_return(!journal_pid_changed(j
), -ECHILD
);
2847 assert_return(data
, -EINVAL
);
2848 assert_return(l
, -EINVAL
);
2849 assert_return(j
->unique_field
, -EINVAL
);
2851 k
= strlen(j
->unique_field
);
2853 if (!j
->unique_file
) {
2854 if (j
->unique_file_lost
)
2857 j
->unique_file
= ordered_hashmap_first(j
->files
);
2858 if (!j
->unique_file
)
2861 j
->unique_offset
= 0;
2873 /* Proceed to next data object in the field's linked list */
2874 if (j
->unique_offset
== 0) {
2875 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2879 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2881 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2885 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2888 /* We reached the end of the list? Then start again, with the next file */
2889 if (j
->unique_offset
== 0) {
2890 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2891 if (!j
->unique_file
)
2897 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2898 * instead, so that we can look at this data object at the same
2899 * time as one on another file */
2900 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2904 /* Let's do the type check by hand, since we used 0 context above. */
2905 if (o
->object
.type
!= OBJECT_DATA
)
2906 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2907 "%s:offset " OFSfmt
": object has type %d, expected %d",
2908 j
->unique_file
->path
,
2910 o
->object
.type
, OBJECT_DATA
);
2912 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2916 /* Check if we have at least the field name and "=". */
2918 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2919 "%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2920 j
->unique_file
->path
,
2921 j
->unique_offset
, ol
, k
+ 1);
2923 if (memcmp(odata
, j
->unique_field
, k
) || ((const char*) odata
)[k
] != '=')
2924 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2925 "%s:offset " OFSfmt
": object does not start with \"%s=\"",
2926 j
->unique_file
->path
,
2930 /* OK, now let's see if we already returned this data
2931 * object by checking if it exists in the earlier
2932 * traversed files. */
2934 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2935 if (of
== j
->unique_file
)
2938 /* Skip this file it didn't have any fields indexed */
2939 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2942 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
2954 r
= return_data(j
, j
->unique_file
, o
, data
, l
);
2962 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
2966 j
->unique_file
= NULL
;
2967 j
->unique_offset
= 0;
2968 j
->unique_file_lost
= false;
2971 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
2974 assert_return(j
, -EINVAL
);
2975 assert_return(!journal_pid_changed(j
), -ECHILD
);
2976 assert_return(field
, -EINVAL
);
2978 if (!j
->fields_file
) {
2979 if (j
->fields_file_lost
)
2982 j
->fields_file
= ordered_hashmap_first(j
->files
);
2983 if (!j
->fields_file
)
2986 j
->fields_hash_table_index
= 0;
2987 j
->fields_offset
= 0;
2991 JournalFile
*f
, *of
;
3000 if (j
->fields_offset
== 0) {
3003 /* We are not yet positioned at any field. Let's pick the first one */
3004 r
= journal_file_map_field_hash_table(f
);
3008 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
3010 if (j
->fields_hash_table_index
>= m
) {
3011 /* Reached the end of the hash table, go to the next file. */
3016 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
3018 if (j
->fields_offset
!= 0)
3021 /* Empty hash table bucket, go to next one */
3022 j
->fields_hash_table_index
++;
3026 /* Proceed with next file */
3027 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
3028 if (!j
->fields_file
) {
3033 j
->fields_offset
= 0;
3034 j
->fields_hash_table_index
= 0;
3039 /* We are already positioned at a field. If so, let's figure out the next field from it */
3041 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
3045 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
3046 if (j
->fields_offset
== 0) {
3047 /* Reached the end of the hash table chain */
3048 j
->fields_hash_table_index
++;
3053 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
3054 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
3058 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
3059 if (o
->object
.type
!= OBJECT_FIELD
)
3060 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3061 "%s:offset " OFSfmt
": object has type %i, expected %i",
3062 f
->path
, j
->fields_offset
,
3063 o
->object
.type
, OBJECT_FIELD
);
3065 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
3067 /* Let's see if we already returned this field name before. */
3069 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
3073 /* Skip this file it didn't have any fields indexed */
3074 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3077 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
, le64toh(o
->field
.hash
), NULL
, NULL
);
3089 /* Check if this is really a valid string containing no NUL byte */
3090 if (memchr(o
->field
.payload
, 0, sz
))
3093 if (sz
> j
->data_threshold
)
3094 sz
= j
->data_threshold
;
3096 if (!GREEDY_REALLOC(j
->fields_buffer
, j
->fields_buffer_allocated
, sz
+ 1))
3099 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3100 j
->fields_buffer
[sz
] = 0;
3102 if (!field_is_valid(j
->fields_buffer
))
3105 *field
= j
->fields_buffer
;
3110 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3114 j
->fields_file
= NULL
;
3115 j
->fields_hash_table_index
= 0;
3116 j
->fields_offset
= 0;
3117 j
->fields_file_lost
= false;
3120 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3121 assert_return(j
, -EINVAL
);
3122 assert_return(!journal_pid_changed(j
), -ECHILD
);
3124 return !j
->on_network
;
3127 static char *lookup_field(const char *field
, void *userdata
) {
3128 sd_journal
*j
= userdata
;
3136 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3138 size
> REPLACE_VAR_MAX
)
3139 return strdup(field
);
3141 d
= strlen(field
) + 1;
3143 return strndup((const char*) data
+ d
, size
- d
);
3146 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3150 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3154 assert_return(j
, -EINVAL
);
3155 assert_return(!journal_pid_changed(j
), -ECHILD
);
3156 assert_return(ret
, -EINVAL
);
3158 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3162 cid
= strndup((const char*) data
+ 11, size
- 11);
3166 r
= sd_id128_from_string(cid
, &id
);
3170 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
3174 t
= replace_var(text
, lookup_field
, j
);
3182 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3183 assert_return(ret
, -EINVAL
);
3185 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3188 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3189 assert_return(j
, -EINVAL
);
3190 assert_return(!journal_pid_changed(j
), -ECHILD
);
3192 j
->data_threshold
= sz
;
3196 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3197 assert_return(j
, -EINVAL
);
3198 assert_return(!journal_pid_changed(j
), -ECHILD
);
3199 assert_return(sz
, -EINVAL
);
3201 *sz
= j
->data_threshold
;
3205 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3206 assert_return(j
, -EINVAL
);
3208 return j
->has_runtime_files
;
3211 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3212 assert_return(j
, -EINVAL
);
3214 return j
->has_persistent_files
;