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 /* The maximum size of variable values we'll expand in catalog entries. We bind this to PATH_MAX for now, as
49 * we want to be able to show all officially valid paths at least */
50 #define REPLACE_VAR_MAX PATH_MAX
52 #define DEFAULT_DATA_THRESHOLD (64*1024)
54 static void remove_file_real(sd_journal
*j
, JournalFile
*f
);
56 static bool journal_pid_changed(sd_journal
*j
) {
59 /* We don't support people creating a journal object and
60 * keeping it around over a fork(). Let's complain. */
62 return j
->original_pid
!= getpid_cached();
65 static int journal_put_error(sd_journal
*j
, int r
, const char *path
) {
69 /* Memorize an error we encountered, and store which
70 * file/directory it was generated from. Note that we store
71 * only *one* path per error code, as the error code is the
72 * key into the hashmap, and the path is the value. This means
73 * we keep track only of all error kinds, but not of all error
74 * locations. This has the benefit that the hashmap cannot
77 * We return an error here only if we didn't manage to
78 * memorize the real error. */
83 k
= hashmap_ensure_allocated(&j
->errors
, NULL
);
94 k
= hashmap_put(j
->errors
, INT_TO_PTR(r
), copy
);
107 static void detach_location(sd_journal
*j
) {
113 j
->current_file
= NULL
;
114 j
->current_field
= 0;
116 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
)
117 journal_file_reset_location(f
);
120 static void init_location(Location
*l
, LocationType type
, JournalFile
*f
, Object
*o
) {
122 assert(IN_SET(type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
127 .seqnum
= le64toh(o
->entry
.seqnum
),
128 .seqnum_id
= f
->header
->seqnum_id
,
129 .realtime
= le64toh(o
->entry
.realtime
),
130 .monotonic
= le64toh(o
->entry
.monotonic
),
131 .boot_id
= o
->entry
.boot_id
,
132 .xor_hash
= le64toh(o
->entry
.xor_hash
),
134 .realtime_set
= true,
135 .monotonic_set
= true,
136 .xor_hash_set
= true,
140 static void set_location(sd_journal
*j
, JournalFile
*f
, Object
*o
) {
145 init_location(&j
->current_location
, LOCATION_DISCRETE
, f
, o
);
148 j
->current_field
= 0;
150 /* Let f know its candidate entry was picked. */
151 assert(f
->location_type
== LOCATION_SEEK
);
152 f
->location_type
= LOCATION_DISCRETE
;
155 static int match_is_valid(const void *data
, size_t size
) {
163 if (((char*) data
)[0] == '_' && ((char*) data
)[1] == '_')
167 for (p
= b
; p
< b
+ size
; p
++) {
175 if (*p
>= 'A' && *p
<= 'Z')
178 if (*p
>= '0' && *p
<= '9')
187 static bool same_field(const void *_a
, size_t s
, const void *_b
, size_t t
) {
188 const uint8_t *a
= _a
, *b
= _b
;
191 for (j
= 0; j
< s
&& j
< t
; j
++) {
200 assert_not_reached("\"=\" not found");
203 static Match
*match_new(Match
*p
, MatchType t
) {
216 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(const JournalFile
*f
, const Location
*l
, const JournalFile
*current_file
) {
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
&&
454 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
)) {
456 r
= CMP(f
->current_seqnum
, l
->seqnum
);
461 if (l
->monotonic_set
&&
462 sd_id128_equal(f
->current_boot_id
, l
->boot_id
)) {
464 r
= CMP(f
->current_monotonic
, l
->monotonic
);
469 if (l
->realtime_set
) {
471 r
= CMP(f
->current_realtime
, l
->realtime
);
476 if (l
->xor_hash_set
) {
478 r
= CMP(f
->current_xor_hash
, l
->xor_hash
);
486 static int next_for_match(
490 uint64_t after_offset
,
491 direction_t direction
,
503 if (m
->type
== MATCH_DISCRETE
) {
506 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
510 return journal_file_move_to_entry_by_offset_for_data(f
, dp
, after_offset
, direction
, ret
, offset
);
512 } else if (m
->type
== MATCH_OR_TERM
) {
515 /* Find the earliest match beyond after_offset */
517 LIST_FOREACH(matches
, i
, m
->matches
) {
520 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
524 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
532 } else if (m
->type
== MATCH_AND_TERM
) {
533 Match
*i
, *last_moved
;
535 /* Always jump to the next matching entry and repeat
536 * this until we find an offset that matches for all
542 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
546 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
547 last_moved
= m
->matches
;
549 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
552 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
556 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
557 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
566 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
578 static int find_location_for_match(
582 direction_t direction
,
592 if (m
->type
== MATCH_DISCRETE
) {
595 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
599 /* FIXME: missing: find by monotonic */
601 if (j
->current_location
.type
== LOCATION_HEAD
)
602 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_DOWN
, ret
, offset
);
603 if (j
->current_location
.type
== LOCATION_TAIL
)
604 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_UP
, ret
, offset
);
605 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
606 return journal_file_move_to_entry_by_seqnum_for_data(f
, dp
, j
->current_location
.seqnum
, direction
, ret
, offset
);
607 if (j
->current_location
.monotonic_set
) {
608 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, dp
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
612 if (j
->current_location
.realtime_set
)
613 return journal_file_move_to_entry_by_realtime_for_data(f
, dp
, j
->current_location
.realtime
, direction
, ret
, offset
);
615 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, direction
, ret
, offset
);
617 } else if (m
->type
== MATCH_OR_TERM
) {
622 /* Find the earliest match */
624 LIST_FOREACH(matches
, i
, m
->matches
) {
627 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
631 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
639 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
654 assert(m
->type
== MATCH_AND_TERM
);
656 /* First jump to the last match, and then find the
657 * next one where all matches match */
662 LIST_FOREACH(matches
, i
, m
->matches
) {
665 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
669 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
673 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
677 static int find_location_with_matches(
680 direction_t direction
,
692 /* No matches is simple */
694 if (j
->current_location
.type
== LOCATION_HEAD
)
695 return journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
);
696 if (j
->current_location
.type
== LOCATION_TAIL
)
697 return journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
);
698 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
699 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
700 if (j
->current_location
.monotonic_set
) {
701 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
705 if (j
->current_location
.realtime_set
)
706 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
708 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
710 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
713 static int next_with_matches(
716 direction_t direction
,
725 /* No matches is easy. We simple advance the file
728 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
730 /* If we have a match then we look for the next matching entry
731 * with an offset at least one step larger */
732 return next_for_match(j
, j
->level0
, f
,
733 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
734 : f
->current_offset
- 1,
735 direction
, ret
, offset
);
738 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
740 uint64_t cp
, n_entries
;
746 n_entries
= le64toh(f
->header
->n_entries
);
748 /* If we hit EOF before, we don't need to look into this file again
749 * unless direction changed or new entries appeared. */
750 if (f
->last_direction
== direction
&& f
->location_type
== LOCATION_TAIL
&&
751 n_entries
== f
->last_n_entries
)
754 f
->last_n_entries
= n_entries
;
756 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
757 /* LOCATION_SEEK here means we did the work in a previous
758 * iteration and the current location already points to a
759 * candidate entry. */
760 if (f
->location_type
!= LOCATION_SEEK
) {
761 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
765 journal_file_save_location(f
, c
, cp
);
768 f
->last_direction
= direction
;
770 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
774 journal_file_save_location(f
, c
, cp
);
777 /* OK, we found the spot, now let's advance until an entry
778 * that is actually different from what we were previously
779 * looking at. This is necessary to handle entries which exist
780 * in two (or more) journal files, and which shall all be
781 * suppressed but one. */
786 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
789 k
= compare_with_location(f
, &j
->current_location
, j
->current_file
);
791 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
798 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
802 journal_file_save_location(f
, c
, cp
);
806 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
807 JournalFile
*new_file
= NULL
;
813 assert_return(j
, -EINVAL
);
814 assert_return(!journal_pid_changed(j
), -ECHILD
);
816 r
= iterated_cache_get(j
->files_cache
, NULL
, &files
, &n_files
);
820 for (i
= 0; i
< n_files
; i
++) {
821 JournalFile
*f
= (JournalFile
*)files
[i
];
824 r
= next_beyond_location(j
, f
, direction
);
826 log_debug_errno(r
, "Can't iterate through %s, ignoring: %m", f
->path
);
827 remove_file_real(j
, f
);
830 f
->location_type
= LOCATION_TAIL
;
839 k
= journal_file_compare_locations(f
, new_file
);
841 found
= direction
== DIRECTION_DOWN
? k
< 0 : k
> 0;
851 r
= journal_file_move_to_object(new_file
, OBJECT_ENTRY
, new_file
->current_offset
, &o
);
855 set_location(j
, new_file
, o
);
860 _public_
int sd_journal_next(sd_journal
*j
) {
861 return real_journal_next(j
, DIRECTION_DOWN
);
864 _public_
int sd_journal_previous(sd_journal
*j
) {
865 return real_journal_next(j
, DIRECTION_UP
);
868 static int real_journal_next_skip(sd_journal
*j
, direction_t direction
, uint64_t skip
) {
871 assert_return(j
, -EINVAL
);
872 assert_return(!journal_pid_changed(j
), -ECHILD
);
875 /* If this is not a discrete skip, then at least
876 * resolve the current location */
877 if (j
->current_location
.type
!= LOCATION_DISCRETE
) {
878 r
= real_journal_next(j
, direction
);
887 r
= real_journal_next(j
, direction
);
901 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
902 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
905 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
906 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
909 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
912 char bid
[SD_ID128_STRING_MAX
], sid
[SD_ID128_STRING_MAX
];
914 assert_return(j
, -EINVAL
);
915 assert_return(!journal_pid_changed(j
), -ECHILD
);
916 assert_return(cursor
, -EINVAL
);
918 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
919 return -EADDRNOTAVAIL
;
921 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
925 sd_id128_to_string(j
->current_file
->header
->seqnum_id
, sid
);
926 sd_id128_to_string(o
->entry
.boot_id
, bid
);
929 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
930 sid
, le64toh(o
->entry
.seqnum
),
931 bid
, le64toh(o
->entry
.monotonic
),
932 le64toh(o
->entry
.realtime
),
933 le64toh(o
->entry
.xor_hash
)) < 0)
939 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
940 const char *word
, *state
;
942 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
944 seqnum_id_set
= false,
947 monotonic_set
= false,
948 realtime_set
= false,
949 xor_hash_set
= false;
950 sd_id128_t seqnum_id
, boot_id
;
952 assert_return(j
, -EINVAL
);
953 assert_return(!journal_pid_changed(j
), -ECHILD
);
954 assert_return(!isempty(cursor
), -EINVAL
);
956 FOREACH_WORD_SEPARATOR(word
, l
, cursor
, ";", state
) {
960 if (l
< 2 || word
[1] != '=')
963 item
= strndup(word
, l
);
970 seqnum_id_set
= true;
971 k
= sd_id128_from_string(item
+2, &seqnum_id
);
976 if (sscanf(item
+2, "%llx", &seqnum
) != 1)
982 k
= sd_id128_from_string(item
+2, &boot_id
);
986 monotonic_set
= true;
987 if (sscanf(item
+2, "%llx", &monotonic
) != 1)
993 if (sscanf(item
+2, "%llx", &realtime
) != 1)
999 if (sscanf(item
+2, "%llx", &xor_hash
) != 1)
1010 if ((!seqnum_set
|| !seqnum_id_set
) &&
1011 (!monotonic_set
|| !boot_id_set
) &&
1016 j
->current_location
= (Location
) {
1017 .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
);
1133 j
->current_location
= (Location
) {
1134 .type
= LOCATION_SEEK
,
1137 .monotonic_set
= true,
1143 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1144 assert_return(j
, -EINVAL
);
1145 assert_return(!journal_pid_changed(j
), -ECHILD
);
1149 j
->current_location
= (Location
) {
1150 .type
= LOCATION_SEEK
,
1152 .realtime_set
= true,
1158 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1159 assert_return(j
, -EINVAL
);
1160 assert_return(!journal_pid_changed(j
), -ECHILD
);
1164 j
->current_location
= (Location
) {
1165 .type
= LOCATION_HEAD
,
1171 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1172 assert_return(j
, -EINVAL
);
1173 assert_return(!journal_pid_changed(j
), -ECHILD
);
1177 j
->current_location
= (Location
) {
1178 .type
= LOCATION_TAIL
,
1184 static void check_network(sd_journal
*j
, int fd
) {
1190 j
->on_network
= fd_is_network_fs(fd
);
1193 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1194 const char *full
, *tilded
, *atted
;
1196 full
= strjoina(prefix
, ".journal");
1197 tilded
= strjoina(full
, "~");
1198 atted
= strjoina(prefix
, "@");
1200 return STR_IN_SET(filename
, full
, tilded
) ||
1201 startswith(filename
, atted
);
1204 static bool file_type_wanted(int flags
, const char *filename
) {
1207 if (!endswith(filename
, ".journal") && !endswith(filename
, ".journal~"))
1210 /* no flags set → every type is OK */
1211 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1214 if (flags
& SD_JOURNAL_SYSTEM
&& file_has_type_prefix("system", filename
))
1217 if (flags
& SD_JOURNAL_CURRENT_USER
) {
1218 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1220 xsprintf(prefix
, "user-"UID_FMT
, getuid());
1222 if (file_has_type_prefix(prefix
, filename
))
1229 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1234 if (j
->toplevel_fd
>= 0)
1237 return path_startswith(path
, prefix
);
1240 static void track_file_disposition(sd_journal
*j
, JournalFile
*f
) {
1244 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1245 j
->has_runtime_files
= true;
1246 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1247 j
->has_persistent_files
= true;
1250 static const char *skip_slash(const char *p
) {
1261 static int add_any_file(
1266 bool close_fd
= false;
1272 assert(fd
>= 0 || path
);
1275 if (j
->toplevel_fd
>= 0)
1276 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1277 * openat() ignores the first argument. */
1279 fd
= openat(j
->toplevel_fd
, skip_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1281 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1283 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1289 r
= fd_nonblock(fd
, false);
1291 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1296 if (fstat(fd
, &st
) < 0) {
1297 r
= log_debug_errno(errno
, "Failed to fstat file '%s': %m", path
);
1301 r
= stat_verify_regular(&st
);
1303 log_debug_errno(r
, "Refusing to open '%s', as it is not a regular file.", path
);
1307 f
= ordered_hashmap_get(j
->files
, path
);
1309 if (f
->last_stat
.st_dev
== st
.st_dev
&&
1310 f
->last_stat
.st_ino
== st
.st_ino
) {
1312 /* We already track this file, under the same path and with the same device/inode numbers, it's
1313 * hence really the same. Mark this file as seen in this generation. This is used to GC old
1314 * files in process_q_overflow() to detect journal files that are still there and discern them
1315 * from those which are gone. */
1317 f
->last_seen_generation
= j
->generation
;
1322 /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
1323 * replaced (probably due to rotation?), let's drop it hence from our list. */
1324 remove_file_real(j
, f
);
1328 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1329 log_debug("Too many open journal files, not adding %s.", path
);
1334 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, false, 0, false, NULL
, j
->mmap
, NULL
, NULL
, &f
);
1336 log_debug_errno(r
, "Failed to open journal file %s: %m", path
);
1340 /* journal_file_dump(f); */
1342 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1344 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 */
1345 (void) journal_file_close(f
);
1349 close_fd
= false; /* the fd is now owned by the JournalFile object */
1351 f
->last_seen_generation
= j
->generation
;
1353 track_file_disposition(j
, f
);
1354 check_network(j
, f
->fd
);
1356 j
->current_invalidate_counter
++;
1358 log_debug("File %s added.", f
->path
);
1367 k
= journal_put_error(j
, r
, path
);
1375 static int add_file_by_name(
1378 const char *filename
) {
1386 if (j
->no_new_files
)
1389 if (!file_type_wanted(j
->flags
, filename
))
1392 path
= prefix_roota(prefix
, filename
);
1393 return add_any_file(j
, -1, path
);
1396 static void remove_file_by_name(
1399 const char *filename
) {
1408 path
= prefix_roota(prefix
, filename
);
1409 f
= ordered_hashmap_get(j
->files
, path
);
1413 remove_file_real(j
, f
);
1416 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1420 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1422 log_debug("File %s removed.", f
->path
);
1424 if (j
->current_file
== f
) {
1425 j
->current_file
= NULL
;
1426 j
->current_field
= 0;
1429 if (j
->unique_file
== f
) {
1430 /* Jump to the next unique_file or NULL if that one was last */
1431 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1432 j
->unique_offset
= 0;
1433 if (!j
->unique_file
)
1434 j
->unique_file_lost
= true;
1437 if (j
->fields_file
== f
) {
1438 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1439 j
->fields_offset
= 0;
1440 if (!j
->fields_file
)
1441 j
->fields_file_lost
= true;
1444 (void) journal_file_close(f
);
1446 j
->current_invalidate_counter
++;
1449 static int dirname_is_machine_id(const char *fn
) {
1450 sd_id128_t id
, machine
;
1454 /* Returns true if the specified directory name matches the local machine ID */
1456 r
= sd_id128_get_machine(&machine
);
1460 e
= strchr(fn
, '.');
1464 /* Looks like it has a namespace suffix. Verify that. */
1465 if (!log_namespace_name_valid(e
+ 1))
1468 k
= strndupa(fn
, e
- fn
);
1469 r
= sd_id128_from_string(k
, &id
);
1471 r
= sd_id128_from_string(fn
, &id
);
1475 return sd_id128_equal(id
, machine
);
1478 static int dirname_has_namespace(const char *fn
, const char *namespace) {
1481 /* Returns true if the specified directory name matches the specified namespace */
1483 e
= strchr(fn
, '.');
1490 if (!streq(e
+ 1, namespace))
1493 k
= strndupa(fn
, e
- fn
);
1494 return id128_is_valid(k
);
1500 return id128_is_valid(fn
);
1503 static bool dirent_is_journal_file(const struct dirent
*de
) {
1506 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1508 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1511 return endswith(de
->d_name
, ".journal") ||
1512 endswith(de
->d_name
, ".journal~");
1515 static bool dirent_is_journal_subdir(const struct dirent
*de
) {
1519 /* returns true if the specified directory entry looks like a directory that might contain journal
1520 * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a
1523 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1526 e
= strchr(de
->d_name
, '.');
1528 return id128_is_valid(de
->d_name
); /* No namespace */
1530 n
= strndupa(de
->d_name
, e
- de
->d_name
);
1531 if (!id128_is_valid(n
))
1534 return log_namespace_name_valid(e
+ 1);
1537 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1544 if (j
->toplevel_fd
< 0)
1547 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1548 * relative, by dropping the initial slash */
1549 d
= xopendirat(j
->toplevel_fd
, skip_slash(path
), 0);
1557 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1559 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1566 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1568 if (dirent_is_journal_file(de
))
1569 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1571 if (m
->is_root
&& dirent_is_journal_subdir(de
))
1572 (void) add_directory(j
, m
->path
, de
->d_name
);
1578 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1581 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1588 /* Watch this directory if that's enabled and if it not being watched yet. */
1590 if (m
->wd
> 0) /* Already have a watch? */
1592 if (j
->inotify_fd
< 0) /* Not watching at all? */
1595 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1597 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1601 r
= hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
);
1603 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1605 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1606 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1611 static int add_directory(
1614 const char *dirname
) {
1616 _cleanup_free_
char *path
= NULL
;
1617 _cleanup_closedir_
DIR *d
= NULL
;
1624 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1625 * and reenumerates directory contents */
1627 path
= path_join(prefix
, dirname
);
1633 log_debug("Considering directory '%s'.", path
);
1635 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1636 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1637 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1641 (!(FLAGS_SET(j
->flags
, SD_JOURNAL_ALL_NAMESPACES
) ||
1642 dirname_has_namespace(dirname
, j
->namespace) > 0 ||
1643 (FLAGS_SET(j
->flags
, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE
) && dirname_has_namespace(dirname
, NULL
) > 0))))
1646 r
= directory_open(j
, path
, &d
);
1648 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
1652 m
= hashmap_get(j
->directories_by_path
, path
);
1654 m
= new(Directory
, 1);
1665 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1671 path
= NULL
; /* avoid freeing in cleanup */
1672 j
->current_invalidate_counter
++;
1674 log_debug("Directory %s added.", m
->path
);
1676 } else if (m
->is_root
)
1677 return 0; /* Don't 'downgrade' from root directory */
1679 m
->last_seen_generation
= j
->generation
;
1681 directory_watch(j
, m
, dirfd(d
),
1682 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1683 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
1686 if (!j
->no_new_files
)
1687 directory_enumerate(j
, m
, d
);
1689 check_network(j
, dirfd(d
));
1694 k
= journal_put_error(j
, r
, path
?: prefix
);
1701 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
1703 _cleanup_closedir_
DIR *d
= NULL
;
1709 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1710 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1711 * populate the set, as well as to update it later. */
1714 /* If there's a path specified, use it. */
1716 log_debug("Considering root directory '%s'.", p
);
1718 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
1719 !path_has_prefix(j
, p
, "/run"))
1723 p
= strjoina(j
->prefix
, p
);
1725 r
= directory_open(j
, p
, &d
);
1726 if (r
== -ENOENT
&& missing_ok
)
1729 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
1733 _cleanup_close_
int dfd
= -1;
1735 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1736 * opendir() will take possession of the fd, and close it, which we don't want. */
1738 p
= "."; /* store this as "." in the directories hashmap */
1740 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
1746 d
= take_fdopendir(&dfd
);
1755 m
= hashmap_get(j
->directories_by_path
, p
);
1757 m
= new0(Directory
, 1);
1765 m
->path
= strdup(p
);
1772 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1779 j
->current_invalidate_counter
++;
1781 log_debug("Root directory %s added.", m
->path
);
1783 } else if (!m
->is_root
)
1786 directory_watch(j
, m
, dirfd(d
),
1787 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1790 if (!j
->no_new_files
)
1791 directory_enumerate(j
, m
, d
);
1793 check_network(j
, dirfd(d
));
1798 k
= journal_put_error(j
, r
, p
);
1805 static void remove_directory(sd_journal
*j
, Directory
*d
) {
1809 hashmap_remove(j
->directories_by_wd
, INT_TO_PTR(d
->wd
));
1811 if (j
->inotify_fd
>= 0)
1812 (void) inotify_rm_watch(j
->inotify_fd
, d
->wd
);
1815 hashmap_remove(j
->directories_by_path
, d
->path
);
1818 log_debug("Root directory %s removed.", d
->path
);
1820 log_debug("Directory %s removed.", d
->path
);
1826 static int add_search_paths(sd_journal
*j
) {
1828 static const char search_paths
[] =
1829 "/run/log/journal\0"
1830 "/var/log/journal\0";
1835 /* We ignore most errors here, since the idea is to only open
1836 * what's actually accessible, and ignore the rest. */
1838 NULSTR_FOREACH(p
, search_paths
)
1839 (void) add_root_directory(j
, p
, true);
1841 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
1842 (void) add_root_directory(j
, "/var/log/journal/remote", true);
1847 static int add_current_paths(sd_journal
*j
) {
1852 assert(j
->no_new_files
);
1854 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1855 * treat them as fatal. */
1857 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
1858 _cleanup_free_
char *dir
;
1861 dir
= dirname_malloc(f
->path
);
1865 r
= add_directory(j
, dir
, NULL
);
1873 static int allocate_inotify(sd_journal
*j
) {
1876 if (j
->inotify_fd
< 0) {
1877 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1878 if (j
->inotify_fd
< 0)
1882 return hashmap_ensure_allocated(&j
->directories_by_wd
, NULL
);
1885 static sd_journal
*journal_new(int flags
, const char *path
, const char *namespace) {
1886 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1888 j
= new0(sd_journal
, 1);
1892 j
->original_pid
= getpid_cached();
1893 j
->toplevel_fd
= -1;
1896 j
->data_threshold
= DEFAULT_DATA_THRESHOLD
;
1905 if (flags
& SD_JOURNAL_OS_ROOT
)
1912 j
->namespace = strdup(namespace);
1917 j
->files
= ordered_hashmap_new(&path_hash_ops
);
1921 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
1922 j
->directories_by_path
= hashmap_new(&path_hash_ops
);
1923 j
->mmap
= mmap_cache_new();
1924 if (!j
->files_cache
|| !j
->directories_by_path
|| !j
->mmap
)
1930 #define OPEN_ALLOWED_FLAGS \
1931 (SD_JOURNAL_LOCAL_ONLY | \
1932 SD_JOURNAL_RUNTIME_ONLY | \
1933 SD_JOURNAL_SYSTEM | \
1934 SD_JOURNAL_CURRENT_USER | \
1935 SD_JOURNAL_ALL_NAMESPACES | \
1936 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
1938 _public_
int sd_journal_open_namespace(sd_journal
**ret
, const char *namespace, int flags
) {
1939 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1942 assert_return(ret
, -EINVAL
);
1943 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
1945 j
= journal_new(flags
, NULL
, namespace);
1949 r
= add_search_paths(j
);
1957 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1958 return sd_journal_open_namespace(ret
, NULL
, flags
);
1961 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1962 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1964 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1965 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1966 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1970 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1971 * combination with sd_journal_open_directory_fd(). */
1973 assert_return(machine
, -EINVAL
);
1974 assert_return(ret
, -EINVAL
);
1975 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
1976 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1978 p
= strjoina("/run/systemd/machines/", machine
);
1979 r
= parse_env_file(NULL
, p
,
1989 if (!streq_ptr(class, "container"))
1992 j
= journal_new(flags
, root
, NULL
);
1996 r
= add_search_paths(j
);
2004 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
2005 (SD_JOURNAL_OS_ROOT | \
2006 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2008 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
2009 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2012 assert_return(ret
, -EINVAL
);
2013 assert_return(path
, -EINVAL
);
2014 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
2016 j
= journal_new(flags
, path
, NULL
);
2020 if (flags
& SD_JOURNAL_OS_ROOT
)
2021 r
= add_search_paths(j
);
2023 r
= add_root_directory(j
, path
, false);
2031 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
2032 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2036 assert_return(ret
, -EINVAL
);
2037 assert_return(flags
== 0, -EINVAL
);
2039 j
= journal_new(flags
, NULL
, NULL
);
2043 STRV_FOREACH(path
, paths
) {
2044 r
= add_any_file(j
, -1, *path
);
2049 j
->no_new_files
= true;
2055 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
2056 (SD_JOURNAL_OS_ROOT | \
2057 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2059 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
2060 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2064 assert_return(ret
, -EINVAL
);
2065 assert_return(fd
>= 0, -EBADF
);
2066 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2068 if (fstat(fd
, &st
) < 0)
2071 if (!S_ISDIR(st
.st_mode
))
2074 j
= journal_new(flags
, NULL
, NULL
);
2078 j
->toplevel_fd
= fd
;
2080 if (flags
& SD_JOURNAL_OS_ROOT
)
2081 r
= add_search_paths(j
);
2083 r
= add_root_directory(j
, NULL
, false);
2091 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2094 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2098 assert_return(ret
, -EINVAL
);
2099 assert_return(n_fds
> 0, -EBADF
);
2100 assert_return(flags
== 0, -EINVAL
);
2102 j
= journal_new(flags
, NULL
, NULL
);
2106 for (i
= 0; i
< n_fds
; i
++) {
2114 if (fstat(fds
[i
], &st
) < 0) {
2119 r
= stat_verify_regular(&st
);
2123 r
= add_any_file(j
, fds
[i
], NULL
);
2128 j
->no_new_files
= true;
2129 j
->no_inotify
= true;
2135 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2137 ORDERED_HASHMAP_FOREACH(f
, j
->files
, iterator
)
2138 f
->close_fd
= false;
2143 _public_
void sd_journal_close(sd_journal
*j
) {
2149 sd_journal_flush_matches(j
);
2151 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2152 iterated_cache_free(j
->files_cache
);
2154 while ((d
= hashmap_first(j
->directories_by_path
)))
2155 remove_directory(j
, d
);
2157 while ((d
= hashmap_first(j
->directories_by_wd
)))
2158 remove_directory(j
, d
);
2160 hashmap_free(j
->directories_by_path
);
2161 hashmap_free(j
->directories_by_wd
);
2163 safe_close(j
->inotify_fd
);
2166 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j
->mmap
), mmap_cache_get_missed(j
->mmap
));
2167 mmap_cache_unref(j
->mmap
);
2170 hashmap_free_free(j
->errors
);
2175 free(j
->unique_field
);
2176 free(j
->fields_buffer
);
2180 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2185 assert_return(j
, -EINVAL
);
2186 assert_return(!journal_pid_changed(j
), -ECHILD
);
2187 assert_return(ret
, -EINVAL
);
2189 f
= j
->current_file
;
2191 return -EADDRNOTAVAIL
;
2193 if (f
->current_offset
<= 0)
2194 return -EADDRNOTAVAIL
;
2196 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2200 *ret
= le64toh(o
->entry
.realtime
);
2204 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2210 assert_return(j
, -EINVAL
);
2211 assert_return(!journal_pid_changed(j
), -ECHILD
);
2213 f
= j
->current_file
;
2215 return -EADDRNOTAVAIL
;
2217 if (f
->current_offset
<= 0)
2218 return -EADDRNOTAVAIL
;
2220 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2225 *ret_boot_id
= o
->entry
.boot_id
;
2227 r
= sd_id128_get_boot(&id
);
2231 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2236 *ret
= le64toh(o
->entry
.monotonic
);
2241 static bool field_is_valid(const char *field
) {
2249 if (startswith(field
, "__"))
2252 for (p
= field
; *p
; p
++) {
2257 if (*p
>= 'A' && *p
<= 'Z')
2260 if (*p
>= '0' && *p
<= '9')
2269 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2272 size_t field_length
;
2276 assert_return(j
, -EINVAL
);
2277 assert_return(!journal_pid_changed(j
), -ECHILD
);
2278 assert_return(field
, -EINVAL
);
2279 assert_return(data
, -EINVAL
);
2280 assert_return(size
, -EINVAL
);
2281 assert_return(field_is_valid(field
), -EINVAL
);
2283 f
= j
->current_file
;
2285 return -EADDRNOTAVAIL
;
2287 if (f
->current_offset
<= 0)
2288 return -EADDRNOTAVAIL
;
2290 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2294 field_length
= strlen(field
);
2296 n
= journal_file_entry_n_items(o
);
2297 for (i
= 0; i
< n
; i
++) {
2303 p
= le64toh(o
->entry
.items
[i
].object_offset
);
2304 le_hash
= o
->entry
.items
[i
].hash
;
2305 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2309 if (le_hash
!= o
->data
.hash
)
2312 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2314 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2316 #if HAVE_XZ || HAVE_LZ4
2317 r
= decompress_startswith(compression
,
2319 &f
->compress_buffer
, &f
->compress_buffer_size
,
2320 field
, field_length
, '=');
2322 log_debug_errno(r
, "Cannot decompress %s object of length %"PRIu64
" at offset "OFSfmt
": %m",
2323 object_compressed_to_string(compression
), l
, p
);
2328 r
= decompress_blob(compression
,
2330 &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
,
2335 *data
= f
->compress_buffer
;
2336 *size
= (size_t) rsize
;
2341 return -EPROTONOSUPPORT
;
2343 } else if (l
>= field_length
+1 &&
2344 memcmp(o
->data
.payload
, field
, field_length
) == 0 &&
2345 o
->data
.payload
[field_length
] == '=') {
2349 if ((uint64_t) t
!= l
)
2352 *data
= o
->data
.payload
;
2358 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2366 static int return_data(sd_journal
*j
, JournalFile
*f
, Object
*o
, const void **data
, size_t *size
) {
2371 l
= le64toh(READ_NOW(o
->object
.size
));
2372 if (l
< offsetof(Object
, data
.payload
))
2374 l
-= offsetof(Object
, data
.payload
);
2377 /* We can't read objects larger than 4G on a 32bit machine */
2378 if ((uint64_t) t
!= l
)
2381 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2383 #if HAVE_XZ || HAVE_LZ4
2387 r
= decompress_blob(compression
,
2388 o
->data
.payload
, l
, &f
->compress_buffer
,
2389 &f
->compress_buffer_size
, &rsize
, j
->data_threshold
);
2393 *data
= f
->compress_buffer
;
2394 *size
= (size_t) rsize
;
2396 return -EPROTONOSUPPORT
;
2399 *data
= o
->data
.payload
;
2406 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2413 assert_return(j
, -EINVAL
);
2414 assert_return(!journal_pid_changed(j
), -ECHILD
);
2415 assert_return(data
, -EINVAL
);
2416 assert_return(size
, -EINVAL
);
2418 f
= j
->current_file
;
2420 return -EADDRNOTAVAIL
;
2422 if (f
->current_offset
<= 0)
2423 return -EADDRNOTAVAIL
;
2425 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2429 n
= journal_file_entry_n_items(o
);
2430 if (j
->current_field
>= n
)
2433 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2434 le_hash
= o
->entry
.items
[j
->current_field
].hash
;
2435 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2439 if (le_hash
!= o
->data
.hash
)
2442 r
= return_data(j
, f
, o
, data
, size
);
2451 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2455 j
->current_field
= 0;
2458 static int reiterate_all_paths(sd_journal
*j
) {
2461 if (j
->no_new_files
)
2462 return add_current_paths(j
);
2464 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2465 return add_search_paths(j
);
2467 if (j
->toplevel_fd
>= 0)
2468 return add_root_directory(j
, NULL
, false);
2471 return add_root_directory(j
, j
->path
, true);
2473 return add_search_paths(j
);
2476 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2479 assert_return(j
, -EINVAL
);
2480 assert_return(!journal_pid_changed(j
), -ECHILD
);
2483 return -EMEDIUMTYPE
;
2485 if (j
->inotify_fd
>= 0)
2486 return j
->inotify_fd
;
2488 r
= allocate_inotify(j
);
2492 log_debug("Reiterating files to get inotify watches established.");
2494 /* Iterate through all dirs again, to add them to the inotify */
2495 r
= reiterate_all_paths(j
);
2499 return j
->inotify_fd
;
2502 _public_
int sd_journal_get_events(sd_journal
*j
) {
2505 assert_return(j
, -EINVAL
);
2506 assert_return(!journal_pid_changed(j
), -ECHILD
);
2508 fd
= sd_journal_get_fd(j
);
2515 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2518 assert_return(j
, -EINVAL
);
2519 assert_return(!journal_pid_changed(j
), -ECHILD
);
2520 assert_return(timeout_usec
, -EINVAL
);
2522 fd
= sd_journal_get_fd(j
);
2526 if (!j
->on_network
) {
2527 *timeout_usec
= (uint64_t) -1;
2531 /* If we are on the network we need to regularly check for
2532 * changes manually */
2534 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2538 static void process_q_overflow(sd_journal
*j
) {
2545 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2546 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2547 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2548 * are subject for unloading. */
2550 log_debug("Inotify queue overrun, reiterating everything.");
2553 (void) reiterate_all_paths(j
);
2555 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2557 if (f
->last_seen_generation
== j
->generation
)
2560 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2561 remove_file_real(j
, f
);
2564 HASHMAP_FOREACH(m
, j
->directories_by_path
, i
) {
2566 if (m
->last_seen_generation
== j
->generation
)
2569 if (m
->is_root
) /* Never GC root directories */
2572 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2573 remove_directory(j
, m
);
2576 log_debug("Reiteration complete.");
2579 static void process_inotify_event(sd_journal
*j
, const struct inotify_event
*e
) {
2585 if (e
->mask
& IN_Q_OVERFLOW
) {
2586 process_q_overflow(j
);
2590 /* Is this a subdirectory we watch? */
2591 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2593 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2594 (endswith(e
->name
, ".journal") ||
2595 endswith(e
->name
, ".journal~"))) {
2597 /* Event for a journal file */
2599 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2600 (void) add_file_by_name(j
, d
->path
, e
->name
);
2601 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2602 remove_file_by_name(j
, d
->path
, e
->name
);
2604 } else if (!d
->is_root
&& e
->len
== 0) {
2606 /* Event for a subdirectory */
2608 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2609 remove_directory(j
, d
);
2611 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2613 /* Event for root directory */
2615 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2616 (void) add_directory(j
, d
->path
, e
->name
);
2622 if (e
->mask
& IN_IGNORED
)
2625 log_debug("Unexpected inotify event.");
2628 static int determine_change(sd_journal
*j
) {
2633 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2634 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2636 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2639 _public_
int sd_journal_process(sd_journal
*j
) {
2640 bool got_something
= false;
2642 assert_return(j
, -EINVAL
);
2643 assert_return(!journal_pid_changed(j
), -ECHILD
);
2645 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
2648 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2649 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2652 union inotify_event_buffer buffer
;
2653 struct inotify_event
*e
;
2656 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2658 if (IN_SET(errno
, EAGAIN
, EINTR
))
2659 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2664 got_something
= true;
2666 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2667 process_inotify_event(j
, e
);
2671 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2675 assert_return(j
, -EINVAL
);
2676 assert_return(!journal_pid_changed(j
), -ECHILD
);
2678 if (j
->inotify_fd
< 0) {
2682 /* This is the first invocation, hence create the
2684 r
= sd_journal_get_fd(j
);
2688 /* Server might have done some vacuuming while we weren't watching.
2689 Get rid of the deleted files now so they don't stay around indefinitely. */
2690 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2691 r
= journal_file_fstat(f
);
2693 remove_file_real(j
, f
);
2695 log_debug_errno(r
,"Failed to fstat() journal file '%s' : %m", f
->path
);
2700 /* The journal might have changed since the context
2701 * object was created and we weren't watching before,
2702 * hence don't wait for anything, and return
2704 return determine_change(j
);
2707 r
= sd_journal_get_timeout(j
, &t
);
2711 if (t
!= (uint64_t) -1) {
2714 n
= now(CLOCK_MONOTONIC
);
2715 t
= t
> n
? t
- n
: 0;
2717 if (timeout_usec
== (uint64_t) -1 || timeout_usec
> t
)
2722 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2723 } while (r
== -EINTR
);
2728 return sd_journal_process(j
);
2731 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2735 uint64_t fmin
= 0, tmax
= 0;
2738 assert_return(j
, -EINVAL
);
2739 assert_return(!journal_pid_changed(j
), -ECHILD
);
2740 assert_return(from
|| to
, -EINVAL
);
2741 assert_return(from
!= to
, -EINVAL
);
2743 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2746 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2759 fmin
= MIN(fr
, fmin
);
2760 tmax
= MAX(t
, tmax
);
2769 return first
? 0 : 1;
2772 _public_
int sd_journal_get_cutoff_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t *from
, uint64_t *to
) {
2778 assert_return(j
, -EINVAL
);
2779 assert_return(!journal_pid_changed(j
), -ECHILD
);
2780 assert_return(from
|| to
, -EINVAL
);
2781 assert_return(from
!= to
, -EINVAL
);
2783 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2786 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &fr
, &t
);
2796 *from
= MIN(fr
, *from
);
2811 void journal_print_header(sd_journal
*j
) {
2814 bool newline
= false;
2818 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2824 journal_file_print_header(f
);
2828 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *bytes
) {
2833 assert_return(j
, -EINVAL
);
2834 assert_return(!journal_pid_changed(j
), -ECHILD
);
2835 assert_return(bytes
, -EINVAL
);
2837 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2840 if (fstat(f
->fd
, &st
) < 0)
2843 sum
+= (uint64_t) st
.st_blocks
* 512ULL;
2850 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2853 assert_return(j
, -EINVAL
);
2854 assert_return(!journal_pid_changed(j
), -ECHILD
);
2855 assert_return(!isempty(field
), -EINVAL
);
2856 assert_return(field_is_valid(field
), -EINVAL
);
2862 free(j
->unique_field
);
2863 j
->unique_field
= f
;
2864 j
->unique_file
= NULL
;
2865 j
->unique_offset
= 0;
2866 j
->unique_file_lost
= false;
2871 _public_
int sd_journal_enumerate_unique(sd_journal
*j
, const void **data
, size_t *l
) {
2874 assert_return(j
, -EINVAL
);
2875 assert_return(!journal_pid_changed(j
), -ECHILD
);
2876 assert_return(data
, -EINVAL
);
2877 assert_return(l
, -EINVAL
);
2878 assert_return(j
->unique_field
, -EINVAL
);
2880 k
= strlen(j
->unique_field
);
2882 if (!j
->unique_file
) {
2883 if (j
->unique_file_lost
)
2886 j
->unique_file
= ordered_hashmap_first(j
->files
);
2887 if (!j
->unique_file
)
2890 j
->unique_offset
= 0;
2902 /* Proceed to next data object in the field's linked list */
2903 if (j
->unique_offset
== 0) {
2904 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2908 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2910 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2914 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2917 /* We reached the end of the list? Then start again, with the next file */
2918 if (j
->unique_offset
== 0) {
2919 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2920 if (!j
->unique_file
)
2926 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2927 * instead, so that we can look at this data object at the same
2928 * time as one on another file */
2929 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2933 /* Let's do the type check by hand, since we used 0 context above. */
2934 if (o
->object
.type
!= OBJECT_DATA
)
2935 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2936 "%s:offset " OFSfmt
": object has type %d, expected %d",
2937 j
->unique_file
->path
,
2939 o
->object
.type
, OBJECT_DATA
);
2941 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2945 /* Check if we have at least the field name and "=". */
2947 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2948 "%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2949 j
->unique_file
->path
,
2950 j
->unique_offset
, ol
, k
+ 1);
2952 if (memcmp(odata
, j
->unique_field
, k
) || ((const char*) odata
)[k
] != '=')
2953 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2954 "%s:offset " OFSfmt
": object does not start with \"%s=\"",
2955 j
->unique_file
->path
,
2959 /* OK, now let's see if we already returned this data
2960 * object by checking if it exists in the earlier
2961 * traversed files. */
2963 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2964 if (of
== j
->unique_file
)
2967 /* Skip this file it didn't have any fields indexed */
2968 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2971 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
2983 r
= return_data(j
, j
->unique_file
, o
, data
, l
);
2991 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
2995 j
->unique_file
= NULL
;
2996 j
->unique_offset
= 0;
2997 j
->unique_file_lost
= false;
3000 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
3003 assert_return(j
, -EINVAL
);
3004 assert_return(!journal_pid_changed(j
), -ECHILD
);
3005 assert_return(field
, -EINVAL
);
3007 if (!j
->fields_file
) {
3008 if (j
->fields_file_lost
)
3011 j
->fields_file
= ordered_hashmap_first(j
->files
);
3012 if (!j
->fields_file
)
3015 j
->fields_hash_table_index
= 0;
3016 j
->fields_offset
= 0;
3020 JournalFile
*f
, *of
;
3029 if (j
->fields_offset
== 0) {
3032 /* We are not yet positioned at any field. Let's pick the first one */
3033 r
= journal_file_map_field_hash_table(f
);
3037 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
3039 if (j
->fields_hash_table_index
>= m
) {
3040 /* Reached the end of the hash table, go to the next file. */
3045 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
3047 if (j
->fields_offset
!= 0)
3050 /* Empty hash table bucket, go to next one */
3051 j
->fields_hash_table_index
++;
3055 /* Proceed with next file */
3056 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
3057 if (!j
->fields_file
) {
3062 j
->fields_offset
= 0;
3063 j
->fields_hash_table_index
= 0;
3068 /* We are already positioned at a field. If so, let's figure out the next field from it */
3070 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
3074 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
3075 if (j
->fields_offset
== 0) {
3076 /* Reached the end of the hash table chain */
3077 j
->fields_hash_table_index
++;
3082 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
3083 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
3087 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
3088 if (o
->object
.type
!= OBJECT_FIELD
)
3089 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3090 "%s:offset " OFSfmt
": object has type %i, expected %i",
3091 f
->path
, j
->fields_offset
,
3092 o
->object
.type
, OBJECT_FIELD
);
3094 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
3096 /* Let's see if we already returned this field name before. */
3098 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
3102 /* Skip this file it didn't have any fields indexed */
3103 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3106 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
, le64toh(o
->field
.hash
), NULL
, NULL
);
3118 /* Check if this is really a valid string containing no NUL byte */
3119 if (memchr(o
->field
.payload
, 0, sz
))
3122 if (sz
> j
->data_threshold
)
3123 sz
= j
->data_threshold
;
3125 if (!GREEDY_REALLOC(j
->fields_buffer
, j
->fields_buffer_allocated
, sz
+ 1))
3128 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3129 j
->fields_buffer
[sz
] = 0;
3131 if (!field_is_valid(j
->fields_buffer
))
3134 *field
= j
->fields_buffer
;
3139 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3143 j
->fields_file
= NULL
;
3144 j
->fields_hash_table_index
= 0;
3145 j
->fields_offset
= 0;
3146 j
->fields_file_lost
= false;
3149 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3150 assert_return(j
, -EINVAL
);
3151 assert_return(!journal_pid_changed(j
), -ECHILD
);
3153 return !j
->on_network
;
3156 static char *lookup_field(const char *field
, void *userdata
) {
3157 sd_journal
*j
= userdata
;
3165 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3167 size
> REPLACE_VAR_MAX
)
3168 return strdup(field
);
3170 d
= strlen(field
) + 1;
3172 return strndup((const char*) data
+ d
, size
- d
);
3175 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3179 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3183 assert_return(j
, -EINVAL
);
3184 assert_return(!journal_pid_changed(j
), -ECHILD
);
3185 assert_return(ret
, -EINVAL
);
3187 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3191 cid
= strndup((const char*) data
+ 11, size
- 11);
3195 r
= sd_id128_from_string(cid
, &id
);
3199 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
3203 t
= replace_var(text
, lookup_field
, j
);
3211 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3212 assert_return(ret
, -EINVAL
);
3214 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3217 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3218 assert_return(j
, -EINVAL
);
3219 assert_return(!journal_pid_changed(j
), -ECHILD
);
3221 j
->data_threshold
= sz
;
3225 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3226 assert_return(j
, -EINVAL
);
3227 assert_return(!journal_pid_changed(j
), -ECHILD
);
3228 assert_return(sz
, -EINVAL
);
3230 *sz
= j
->data_threshold
;
3234 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3235 assert_return(j
, -EINVAL
);
3237 return j
->has_runtime_files
;
3240 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3241 assert_return(j
, -EINVAL
);
3243 return j
->has_persistent_files
;