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(const JournalFile
*f
, const Location
*l
, const JournalFile
*current_file
) {
443 assert(current_file
);
444 assert(f
->location_type
== LOCATION_SEEK
);
445 assert(IN_SET(l
->type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
447 if (l
->monotonic_set
&&
448 sd_id128_equal(f
->current_boot_id
, l
->boot_id
) &&
450 f
->current_realtime
== l
->realtime
&&
452 f
->current_xor_hash
== l
->xor_hash
&&
457 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
)) {
459 r
= CMP(f
->current_seqnum
, l
->seqnum
);
464 if (l
->monotonic_set
&&
465 sd_id128_equal(f
->current_boot_id
, l
->boot_id
)) {
467 r
= CMP(f
->current_monotonic
, l
->monotonic
);
472 if (l
->realtime_set
) {
474 r
= CMP(f
->current_realtime
, l
->realtime
);
479 if (l
->xor_hash_set
) {
481 r
= CMP(f
->current_xor_hash
, l
->xor_hash
);
489 static int next_for_match(
493 uint64_t after_offset
,
494 direction_t direction
,
506 if (m
->type
== MATCH_DISCRETE
) {
509 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
513 return journal_file_move_to_entry_by_offset_for_data(f
, dp
, after_offset
, direction
, ret
, offset
);
515 } else if (m
->type
== MATCH_OR_TERM
) {
518 /* Find the earliest match beyond after_offset */
520 LIST_FOREACH(matches
, i
, m
->matches
) {
523 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
527 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
535 } else if (m
->type
== MATCH_AND_TERM
) {
536 Match
*i
, *last_moved
;
538 /* Always jump to the next matching entry and repeat
539 * this until we find an offset that matches for all
545 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
549 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
550 last_moved
= m
->matches
;
552 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
555 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
559 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
560 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
569 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
581 static int find_location_for_match(
585 direction_t direction
,
595 if (m
->type
== MATCH_DISCRETE
) {
598 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
602 /* FIXME: missing: find by monotonic */
604 if (j
->current_location
.type
== LOCATION_HEAD
)
605 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_DOWN
, ret
, offset
);
606 if (j
->current_location
.type
== LOCATION_TAIL
)
607 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_UP
, ret
, offset
);
608 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
609 return journal_file_move_to_entry_by_seqnum_for_data(f
, dp
, j
->current_location
.seqnum
, direction
, ret
, offset
);
610 if (j
->current_location
.monotonic_set
) {
611 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, dp
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
615 if (j
->current_location
.realtime_set
)
616 return journal_file_move_to_entry_by_realtime_for_data(f
, dp
, j
->current_location
.realtime
, direction
, ret
, offset
);
618 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, direction
, ret
, offset
);
620 } else if (m
->type
== MATCH_OR_TERM
) {
625 /* Find the earliest match */
627 LIST_FOREACH(matches
, i
, m
->matches
) {
630 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
634 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
642 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
657 assert(m
->type
== MATCH_AND_TERM
);
659 /* First jump to the last match, and then find the
660 * next one where all matches match */
665 LIST_FOREACH(matches
, i
, m
->matches
) {
668 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
672 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
676 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
680 static int find_location_with_matches(
683 direction_t direction
,
695 /* No matches is simple */
697 if (j
->current_location
.type
== LOCATION_HEAD
)
698 return journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
);
699 if (j
->current_location
.type
== LOCATION_TAIL
)
700 return journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
);
701 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
702 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
703 if (j
->current_location
.monotonic_set
) {
704 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
708 if (j
->current_location
.realtime_set
)
709 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
711 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
713 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
716 static int next_with_matches(
719 direction_t direction
,
728 /* No matches is easy. We simple advance the file
731 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
733 /* If we have a match then we look for the next matching entry
734 * with an offset at least one step larger */
735 return next_for_match(j
, j
->level0
, f
,
736 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
737 : f
->current_offset
- 1,
738 direction
, ret
, offset
);
741 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
743 uint64_t cp
, n_entries
;
749 n_entries
= le64toh(f
->header
->n_entries
);
751 /* If we hit EOF before, we don't need to look into this file again
752 * unless direction changed or new entries appeared. */
753 if (f
->last_direction
== direction
&& f
->location_type
== LOCATION_TAIL
&&
754 n_entries
== f
->last_n_entries
)
757 f
->last_n_entries
= n_entries
;
759 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
760 /* LOCATION_SEEK here means we did the work in a previous
761 * iteration and the current location already points to a
762 * candidate entry. */
763 if (f
->location_type
!= LOCATION_SEEK
) {
764 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
768 journal_file_save_location(f
, c
, cp
);
771 f
->last_direction
= direction
;
773 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
777 journal_file_save_location(f
, c
, cp
);
780 /* OK, we found the spot, now let's advance until an entry
781 * that is actually different from what we were previously
782 * looking at. This is necessary to handle entries which exist
783 * in two (or more) journal files, and which shall all be
784 * suppressed but one. */
789 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
792 k
= compare_with_location(f
, &j
->current_location
, j
->current_file
);
794 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
801 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
805 journal_file_save_location(f
, c
, cp
);
809 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
810 JournalFile
*new_file
= NULL
;
816 assert_return(j
, -EINVAL
);
817 assert_return(!journal_pid_changed(j
), -ECHILD
);
819 r
= iterated_cache_get(j
->files_cache
, NULL
, &files
, &n_files
);
823 for (i
= 0; i
< n_files
; i
++) {
824 JournalFile
*f
= (JournalFile
*)files
[i
];
827 r
= next_beyond_location(j
, f
, direction
);
829 log_debug_errno(r
, "Can't iterate through %s, ignoring: %m", f
->path
);
830 remove_file_real(j
, f
);
833 f
->location_type
= LOCATION_TAIL
;
842 k
= journal_file_compare_locations(f
, new_file
);
844 found
= direction
== DIRECTION_DOWN
? k
< 0 : k
> 0;
854 r
= journal_file_move_to_object(new_file
, OBJECT_ENTRY
, new_file
->current_offset
, &o
);
858 set_location(j
, new_file
, o
);
863 _public_
int sd_journal_next(sd_journal
*j
) {
864 return real_journal_next(j
, DIRECTION_DOWN
);
867 _public_
int sd_journal_previous(sd_journal
*j
) {
868 return real_journal_next(j
, DIRECTION_UP
);
871 static int real_journal_next_skip(sd_journal
*j
, direction_t direction
, uint64_t skip
) {
874 assert_return(j
, -EINVAL
);
875 assert_return(!journal_pid_changed(j
), -ECHILD
);
878 /* If this is not a discrete skip, then at least
879 * resolve the current location */
880 if (j
->current_location
.type
!= LOCATION_DISCRETE
) {
881 r
= real_journal_next(j
, direction
);
890 r
= real_journal_next(j
, direction
);
904 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
905 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
908 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
909 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
912 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
915 char bid
[SD_ID128_STRING_MAX
], sid
[SD_ID128_STRING_MAX
];
917 assert_return(j
, -EINVAL
);
918 assert_return(!journal_pid_changed(j
), -ECHILD
);
919 assert_return(cursor
, -EINVAL
);
921 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
922 return -EADDRNOTAVAIL
;
924 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
928 sd_id128_to_string(j
->current_file
->header
->seqnum_id
, sid
);
929 sd_id128_to_string(o
->entry
.boot_id
, bid
);
932 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
933 sid
, le64toh(o
->entry
.seqnum
),
934 bid
, le64toh(o
->entry
.monotonic
),
935 le64toh(o
->entry
.realtime
),
936 le64toh(o
->entry
.xor_hash
)) < 0)
942 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
943 const char *word
, *state
;
945 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
947 seqnum_id_set
= false,
950 monotonic_set
= false,
951 realtime_set
= false,
952 xor_hash_set
= false;
953 sd_id128_t seqnum_id
, boot_id
;
955 assert_return(j
, -EINVAL
);
956 assert_return(!journal_pid_changed(j
), -ECHILD
);
957 assert_return(!isempty(cursor
), -EINVAL
);
959 FOREACH_WORD_SEPARATOR(word
, l
, cursor
, ";", state
) {
963 if (l
< 2 || word
[1] != '=')
966 item
= strndup(word
, l
);
973 seqnum_id_set
= true;
974 k
= sd_id128_from_string(item
+2, &seqnum_id
);
979 if (sscanf(item
+2, "%llx", &seqnum
) != 1)
985 k
= sd_id128_from_string(item
+2, &boot_id
);
989 monotonic_set
= true;
990 if (sscanf(item
+2, "%llx", &monotonic
) != 1)
996 if (sscanf(item
+2, "%llx", &realtime
) != 1)
1001 xor_hash_set
= true;
1002 if (sscanf(item
+2, "%llx", &xor_hash
) != 1)
1013 if ((!seqnum_set
|| !seqnum_id_set
) &&
1014 (!monotonic_set
|| !boot_id_set
) &&
1020 j
->current_location
.type
= LOCATION_SEEK
;
1023 j
->current_location
.realtime
= (uint64_t) realtime
;
1024 j
->current_location
.realtime_set
= true;
1027 if (seqnum_set
&& seqnum_id_set
) {
1028 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1029 j
->current_location
.seqnum_id
= seqnum_id
;
1030 j
->current_location
.seqnum_set
= true;
1033 if (monotonic_set
&& boot_id_set
) {
1034 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1035 j
->current_location
.boot_id
= boot_id
;
1036 j
->current_location
.monotonic_set
= true;
1040 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1041 j
->current_location
.xor_hash_set
= true;
1047 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1051 assert_return(j
, -EINVAL
);
1052 assert_return(!journal_pid_changed(j
), -ECHILD
);
1053 assert_return(!isempty(cursor
), -EINVAL
);
1055 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1056 return -EADDRNOTAVAIL
;
1058 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1063 _cleanup_free_
char *item
= NULL
;
1064 unsigned long long ll
;
1068 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1075 if (strlen(item
) < 2 || item
[1] != '=')
1081 k
= sd_id128_from_string(item
+2, &id
);
1084 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1089 if (sscanf(item
+2, "%llx", &ll
) != 1)
1091 if (ll
!= le64toh(o
->entry
.seqnum
))
1096 k
= sd_id128_from_string(item
+2, &id
);
1099 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1104 if (sscanf(item
+2, "%llx", &ll
) != 1)
1106 if (ll
!= le64toh(o
->entry
.monotonic
))
1111 if (sscanf(item
+2, "%llx", &ll
) != 1)
1113 if (ll
!= le64toh(o
->entry
.realtime
))
1118 if (sscanf(item
+2, "%llx", &ll
) != 1)
1120 if (ll
!= le64toh(o
->entry
.xor_hash
))
1129 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t usec
) {
1130 assert_return(j
, -EINVAL
);
1131 assert_return(!journal_pid_changed(j
), -ECHILD
);
1134 j
->current_location
.type
= LOCATION_SEEK
;
1135 j
->current_location
.boot_id
= boot_id
;
1136 j
->current_location
.monotonic
= usec
;
1137 j
->current_location
.monotonic_set
= true;
1142 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1143 assert_return(j
, -EINVAL
);
1144 assert_return(!journal_pid_changed(j
), -ECHILD
);
1147 j
->current_location
.type
= LOCATION_SEEK
;
1148 j
->current_location
.realtime
= usec
;
1149 j
->current_location
.realtime_set
= true;
1154 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1155 assert_return(j
, -EINVAL
);
1156 assert_return(!journal_pid_changed(j
), -ECHILD
);
1159 j
->current_location
.type
= LOCATION_HEAD
;
1164 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1165 assert_return(j
, -EINVAL
);
1166 assert_return(!journal_pid_changed(j
), -ECHILD
);
1169 j
->current_location
.type
= LOCATION_TAIL
;
1174 static void check_network(sd_journal
*j
, int fd
) {
1180 j
->on_network
= fd_is_network_fs(fd
);
1183 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1184 const char *full
, *tilded
, *atted
;
1186 full
= strjoina(prefix
, ".journal");
1187 tilded
= strjoina(full
, "~");
1188 atted
= strjoina(prefix
, "@");
1190 return STR_IN_SET(filename
, full
, tilded
) ||
1191 startswith(filename
, atted
);
1194 static bool file_type_wanted(int flags
, const char *filename
) {
1197 if (!endswith(filename
, ".journal") && !endswith(filename
, ".journal~"))
1200 /* no flags set → every type is OK */
1201 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1204 if (flags
& SD_JOURNAL_SYSTEM
&& file_has_type_prefix("system", filename
))
1207 if (flags
& SD_JOURNAL_CURRENT_USER
) {
1208 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1210 xsprintf(prefix
, "user-"UID_FMT
, getuid());
1212 if (file_has_type_prefix(prefix
, filename
))
1219 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1224 if (j
->toplevel_fd
>= 0)
1227 return path_startswith(path
, prefix
);
1230 static void track_file_disposition(sd_journal
*j
, JournalFile
*f
) {
1234 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1235 j
->has_runtime_files
= true;
1236 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1237 j
->has_persistent_files
= true;
1240 static const char *skip_slash(const char *p
) {
1251 static int add_any_file(
1256 bool close_fd
= false;
1262 assert(fd
>= 0 || path
);
1265 if (j
->toplevel_fd
>= 0)
1266 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1267 * openat() ignores the first argument. */
1269 fd
= openat(j
->toplevel_fd
, skip_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1271 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1273 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1279 r
= fd_nonblock(fd
, false);
1281 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1286 if (fstat(fd
, &st
) < 0) {
1287 r
= log_debug_errno(errno
, "Failed to fstat file '%s': %m", path
);
1291 r
= stat_verify_regular(&st
);
1293 log_debug_errno(r
, "Refusing to open '%s', as it is not a regular file.", path
);
1297 f
= ordered_hashmap_get(j
->files
, path
);
1299 if (f
->last_stat
.st_dev
== st
.st_dev
&&
1300 f
->last_stat
.st_ino
== st
.st_ino
) {
1302 /* We already track this file, under the same path and with the same device/inode numbers, it's
1303 * hence really the same. Mark this file as seen in this generation. This is used to GC old
1304 * files in process_q_overflow() to detect journal files that are still there and discern them
1305 * from those which are gone. */
1307 f
->last_seen_generation
= j
->generation
;
1312 /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
1313 * replaced (probably due to rotation?), let's drop it hence from our list. */
1314 remove_file_real(j
, f
);
1318 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1319 log_debug("Too many open journal files, not adding %s.", path
);
1324 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, false, 0, false, NULL
, j
->mmap
, NULL
, NULL
, &f
);
1326 log_debug_errno(r
, "Failed to open journal file %s: %m", path
);
1330 /* journal_file_dump(f); */
1332 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1334 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 */
1335 (void) journal_file_close(f
);
1339 close_fd
= false; /* the fd is now owned by the JournalFile object */
1341 f
->last_seen_generation
= j
->generation
;
1343 track_file_disposition(j
, f
);
1344 check_network(j
, f
->fd
);
1346 j
->current_invalidate_counter
++;
1348 log_debug("File %s added.", f
->path
);
1357 k
= journal_put_error(j
, r
, path
);
1365 static int add_file_by_name(
1368 const char *filename
) {
1376 if (j
->no_new_files
)
1379 if (!file_type_wanted(j
->flags
, filename
))
1382 path
= prefix_roota(prefix
, filename
);
1383 return add_any_file(j
, -1, path
);
1386 static void remove_file_by_name(
1389 const char *filename
) {
1398 path
= prefix_roota(prefix
, filename
);
1399 f
= ordered_hashmap_get(j
->files
, path
);
1403 remove_file_real(j
, f
);
1406 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1410 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1412 log_debug("File %s removed.", f
->path
);
1414 if (j
->current_file
== f
) {
1415 j
->current_file
= NULL
;
1416 j
->current_field
= 0;
1419 if (j
->unique_file
== f
) {
1420 /* Jump to the next unique_file or NULL if that one was last */
1421 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1422 j
->unique_offset
= 0;
1423 if (!j
->unique_file
)
1424 j
->unique_file_lost
= true;
1427 if (j
->fields_file
== f
) {
1428 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1429 j
->fields_offset
= 0;
1430 if (!j
->fields_file
)
1431 j
->fields_file_lost
= true;
1434 (void) journal_file_close(f
);
1436 j
->current_invalidate_counter
++;
1439 static int dirname_is_machine_id(const char *fn
) {
1440 sd_id128_t id
, machine
;
1444 /* Returns true if the specified directory name matches the local machine ID */
1446 r
= sd_id128_get_machine(&machine
);
1450 e
= strchr(fn
, '.');
1454 /* Looks like it has a namespace suffix. Verify that. */
1455 if (!log_namespace_name_valid(e
+ 1))
1458 k
= strndupa(fn
, e
- fn
);
1459 r
= sd_id128_from_string(k
, &id
);
1461 r
= sd_id128_from_string(fn
, &id
);
1465 return sd_id128_equal(id
, machine
);
1468 static int dirname_has_namespace(const char *fn
, const char *namespace) {
1471 /* Returns true if the specified directory name matches the specified namespace */
1473 e
= strchr(fn
, '.');
1480 if (!streq(e
+ 1, namespace))
1483 k
= strndupa(fn
, e
- fn
);
1484 return id128_is_valid(k
);
1490 return id128_is_valid(fn
);
1493 static bool dirent_is_journal_file(const struct dirent
*de
) {
1496 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1498 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1501 return endswith(de
->d_name
, ".journal") ||
1502 endswith(de
->d_name
, ".journal~");
1505 static bool dirent_is_journal_subdir(const struct dirent
*de
) {
1509 /* returns true if the specified directory entry looks like a directory that might contain journal
1510 * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a
1513 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1516 e
= strchr(de
->d_name
, '.');
1518 return id128_is_valid(de
->d_name
); /* No namespace */
1520 n
= strndupa(de
->d_name
, e
- de
->d_name
);
1521 if (!id128_is_valid(n
))
1524 return log_namespace_name_valid(e
+ 1);
1527 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1534 if (j
->toplevel_fd
< 0)
1537 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1538 * relative, by dropping the initial slash */
1539 d
= xopendirat(j
->toplevel_fd
, skip_slash(path
), 0);
1547 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1549 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1556 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1558 if (dirent_is_journal_file(de
))
1559 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1561 if (m
->is_root
&& dirent_is_journal_subdir(de
))
1562 (void) add_directory(j
, m
->path
, de
->d_name
);
1568 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1571 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1578 /* Watch this directory if that's enabled and if it not being watched yet. */
1580 if (m
->wd
> 0) /* Already have a watch? */
1582 if (j
->inotify_fd
< 0) /* Not watching at all? */
1585 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1587 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1591 r
= hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
);
1593 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1595 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1596 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1601 static int add_directory(
1604 const char *dirname
) {
1606 _cleanup_free_
char *path
= NULL
;
1607 _cleanup_closedir_
DIR *d
= NULL
;
1614 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1615 * and reenumerates directory contents */
1617 path
= path_join(prefix
, dirname
);
1623 log_debug("Considering directory '%s'.", path
);
1625 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1626 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1627 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1630 if (!(FLAGS_SET(j
->flags
, SD_JOURNAL_ALL_NAMESPACES
) ||
1631 dirname_has_namespace(dirname
, j
->namespace) > 0 ||
1632 (FLAGS_SET(j
->flags
, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE
) && dirname_has_namespace(dirname
, NULL
) > 0)))
1635 r
= directory_open(j
, path
, &d
);
1637 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
1641 m
= hashmap_get(j
->directories_by_path
, path
);
1643 m
= new(Directory
, 1);
1654 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1660 path
= NULL
; /* avoid freeing in cleanup */
1661 j
->current_invalidate_counter
++;
1663 log_debug("Directory %s added.", m
->path
);
1665 } else if (m
->is_root
)
1666 return 0; /* Don't 'downgrade' from root directory */
1668 m
->last_seen_generation
= j
->generation
;
1670 directory_watch(j
, m
, dirfd(d
),
1671 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1672 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
1675 if (!j
->no_new_files
)
1676 directory_enumerate(j
, m
, d
);
1678 check_network(j
, dirfd(d
));
1683 k
= journal_put_error(j
, r
, path
?: prefix
);
1690 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
1692 _cleanup_closedir_
DIR *d
= NULL
;
1698 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1699 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1700 * populate the set, as well as to update it later. */
1703 /* If there's a path specified, use it. */
1705 log_debug("Considering root directory '%s'.", p
);
1707 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
1708 !path_has_prefix(j
, p
, "/run"))
1712 p
= strjoina(j
->prefix
, p
);
1714 r
= directory_open(j
, p
, &d
);
1715 if (r
== -ENOENT
&& missing_ok
)
1718 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
1724 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1725 * opendir() will take possession of the fd, and close it, which we don't want. */
1727 p
= "."; /* store this as "." in the directories hashmap */
1729 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
1745 m
= hashmap_get(j
->directories_by_path
, p
);
1747 m
= new0(Directory
, 1);
1755 m
->path
= strdup(p
);
1762 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1769 j
->current_invalidate_counter
++;
1771 log_debug("Root directory %s added.", m
->path
);
1773 } else if (!m
->is_root
)
1776 directory_watch(j
, m
, dirfd(d
),
1777 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1780 if (!j
->no_new_files
)
1781 directory_enumerate(j
, m
, d
);
1783 check_network(j
, dirfd(d
));
1788 k
= journal_put_error(j
, r
, p
);
1795 static void remove_directory(sd_journal
*j
, Directory
*d
) {
1799 hashmap_remove(j
->directories_by_wd
, INT_TO_PTR(d
->wd
));
1801 if (j
->inotify_fd
>= 0)
1802 (void) inotify_rm_watch(j
->inotify_fd
, d
->wd
);
1805 hashmap_remove(j
->directories_by_path
, d
->path
);
1808 log_debug("Root directory %s removed.", d
->path
);
1810 log_debug("Directory %s removed.", d
->path
);
1816 static int add_search_paths(sd_journal
*j
) {
1818 static const char search_paths
[] =
1819 "/run/log/journal\0"
1820 "/var/log/journal\0";
1825 /* We ignore most errors here, since the idea is to only open
1826 * what's actually accessible, and ignore the rest. */
1828 NULSTR_FOREACH(p
, search_paths
)
1829 (void) add_root_directory(j
, p
, true);
1831 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
1832 (void) add_root_directory(j
, "/var/log/journal/remote", true);
1837 static int add_current_paths(sd_journal
*j
) {
1842 assert(j
->no_new_files
);
1844 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1845 * treat them as fatal. */
1847 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
1848 _cleanup_free_
char *dir
;
1851 dir
= dirname_malloc(f
->path
);
1855 r
= add_directory(j
, dir
, NULL
);
1863 static int allocate_inotify(sd_journal
*j
) {
1866 if (j
->inotify_fd
< 0) {
1867 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1868 if (j
->inotify_fd
< 0)
1872 return hashmap_ensure_allocated(&j
->directories_by_wd
, NULL
);
1875 static sd_journal
*journal_new(int flags
, const char *path
, const char *namespace) {
1876 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1878 j
= new0(sd_journal
, 1);
1882 j
->original_pid
= getpid_cached();
1883 j
->toplevel_fd
= -1;
1886 j
->data_threshold
= DEFAULT_DATA_THRESHOLD
;
1895 if (flags
& SD_JOURNAL_OS_ROOT
)
1902 j
->namespace = strdup(namespace);
1907 j
->files
= ordered_hashmap_new(&path_hash_ops
);
1911 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
1912 j
->directories_by_path
= hashmap_new(&path_hash_ops
);
1913 j
->mmap
= mmap_cache_new();
1914 if (!j
->files_cache
|| !j
->directories_by_path
|| !j
->mmap
)
1920 #define OPEN_ALLOWED_FLAGS \
1921 (SD_JOURNAL_LOCAL_ONLY | \
1922 SD_JOURNAL_RUNTIME_ONLY | \
1923 SD_JOURNAL_SYSTEM | \
1924 SD_JOURNAL_CURRENT_USER | \
1925 SD_JOURNAL_ALL_NAMESPACES | \
1926 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
1928 _public_
int sd_journal_open_namespace(sd_journal
**ret
, const char *namespace, int flags
) {
1929 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1932 assert_return(ret
, -EINVAL
);
1933 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
1935 j
= journal_new(flags
, NULL
, namespace);
1939 r
= add_search_paths(j
);
1947 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1948 return sd_journal_open_namespace(ret
, NULL
, flags
);
1951 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1952 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1954 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1955 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1956 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1960 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1961 * combination with sd_journal_open_directory_fd(). */
1963 assert_return(machine
, -EINVAL
);
1964 assert_return(ret
, -EINVAL
);
1965 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
1966 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1968 p
= strjoina("/run/systemd/machines/", machine
);
1969 r
= parse_env_file(NULL
, p
,
1979 if (!streq_ptr(class, "container"))
1982 j
= journal_new(flags
, root
, NULL
);
1986 r
= add_search_paths(j
);
1994 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
1995 (SD_JOURNAL_OS_ROOT | \
1996 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1998 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
1999 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2002 assert_return(ret
, -EINVAL
);
2003 assert_return(path
, -EINVAL
);
2004 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
2006 j
= journal_new(flags
, path
, NULL
);
2010 if (flags
& SD_JOURNAL_OS_ROOT
)
2011 r
= add_search_paths(j
);
2013 r
= add_root_directory(j
, path
, false);
2021 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
2022 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2026 assert_return(ret
, -EINVAL
);
2027 assert_return(flags
== 0, -EINVAL
);
2029 j
= journal_new(flags
, NULL
, NULL
);
2033 STRV_FOREACH(path
, paths
) {
2034 r
= add_any_file(j
, -1, *path
);
2039 j
->no_new_files
= true;
2045 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
2046 (SD_JOURNAL_OS_ROOT | \
2047 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2049 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
2050 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2054 assert_return(ret
, -EINVAL
);
2055 assert_return(fd
>= 0, -EBADF
);
2056 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2058 if (fstat(fd
, &st
) < 0)
2061 if (!S_ISDIR(st
.st_mode
))
2064 j
= journal_new(flags
, NULL
, NULL
);
2068 j
->toplevel_fd
= fd
;
2070 if (flags
& SD_JOURNAL_OS_ROOT
)
2071 r
= add_search_paths(j
);
2073 r
= add_root_directory(j
, NULL
, false);
2081 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2084 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2088 assert_return(ret
, -EINVAL
);
2089 assert_return(n_fds
> 0, -EBADF
);
2090 assert_return(flags
== 0, -EINVAL
);
2092 j
= journal_new(flags
, NULL
, NULL
);
2096 for (i
= 0; i
< n_fds
; i
++) {
2104 if (fstat(fds
[i
], &st
) < 0) {
2109 r
= stat_verify_regular(&st
);
2113 r
= add_any_file(j
, fds
[i
], NULL
);
2118 j
->no_new_files
= true;
2119 j
->no_inotify
= true;
2125 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2127 ORDERED_HASHMAP_FOREACH(f
, j
->files
, iterator
)
2128 f
->close_fd
= false;
2133 _public_
void sd_journal_close(sd_journal
*j
) {
2139 sd_journal_flush_matches(j
);
2141 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2142 iterated_cache_free(j
->files_cache
);
2144 while ((d
= hashmap_first(j
->directories_by_path
)))
2145 remove_directory(j
, d
);
2147 while ((d
= hashmap_first(j
->directories_by_wd
)))
2148 remove_directory(j
, d
);
2150 hashmap_free(j
->directories_by_path
);
2151 hashmap_free(j
->directories_by_wd
);
2153 safe_close(j
->inotify_fd
);
2156 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j
->mmap
), mmap_cache_get_missed(j
->mmap
));
2157 mmap_cache_unref(j
->mmap
);
2160 hashmap_free_free(j
->errors
);
2165 free(j
->unique_field
);
2166 free(j
->fields_buffer
);
2170 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2175 assert_return(j
, -EINVAL
);
2176 assert_return(!journal_pid_changed(j
), -ECHILD
);
2177 assert_return(ret
, -EINVAL
);
2179 f
= j
->current_file
;
2181 return -EADDRNOTAVAIL
;
2183 if (f
->current_offset
<= 0)
2184 return -EADDRNOTAVAIL
;
2186 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2190 *ret
= le64toh(o
->entry
.realtime
);
2194 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2200 assert_return(j
, -EINVAL
);
2201 assert_return(!journal_pid_changed(j
), -ECHILD
);
2203 f
= j
->current_file
;
2205 return -EADDRNOTAVAIL
;
2207 if (f
->current_offset
<= 0)
2208 return -EADDRNOTAVAIL
;
2210 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2215 *ret_boot_id
= o
->entry
.boot_id
;
2217 r
= sd_id128_get_boot(&id
);
2221 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2226 *ret
= le64toh(o
->entry
.monotonic
);
2231 static bool field_is_valid(const char *field
) {
2239 if (startswith(field
, "__"))
2242 for (p
= field
; *p
; p
++) {
2247 if (*p
>= 'A' && *p
<= 'Z')
2250 if (*p
>= '0' && *p
<= '9')
2259 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2262 size_t field_length
;
2266 assert_return(j
, -EINVAL
);
2267 assert_return(!journal_pid_changed(j
), -ECHILD
);
2268 assert_return(field
, -EINVAL
);
2269 assert_return(data
, -EINVAL
);
2270 assert_return(size
, -EINVAL
);
2271 assert_return(field_is_valid(field
), -EINVAL
);
2273 f
= j
->current_file
;
2275 return -EADDRNOTAVAIL
;
2277 if (f
->current_offset
<= 0)
2278 return -EADDRNOTAVAIL
;
2280 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2284 field_length
= strlen(field
);
2286 n
= journal_file_entry_n_items(o
);
2287 for (i
= 0; i
< n
; i
++) {
2293 p
= le64toh(o
->entry
.items
[i
].object_offset
);
2294 le_hash
= o
->entry
.items
[i
].hash
;
2295 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2299 if (le_hash
!= o
->data
.hash
)
2302 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2304 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2306 #if HAVE_XZ || HAVE_LZ4
2307 r
= decompress_startswith(compression
,
2309 &f
->compress_buffer
, &f
->compress_buffer_size
,
2310 field
, field_length
, '=');
2312 log_debug_errno(r
, "Cannot decompress %s object of length %"PRIu64
" at offset "OFSfmt
": %m",
2313 object_compressed_to_string(compression
), l
, p
);
2318 r
= decompress_blob(compression
,
2320 &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
,
2325 *data
= f
->compress_buffer
;
2326 *size
= (size_t) rsize
;
2331 return -EPROTONOSUPPORT
;
2333 } else if (l
>= field_length
+1 &&
2334 memcmp(o
->data
.payload
, field
, field_length
) == 0 &&
2335 o
->data
.payload
[field_length
] == '=') {
2339 if ((uint64_t) t
!= l
)
2342 *data
= o
->data
.payload
;
2348 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2356 static int return_data(sd_journal
*j
, JournalFile
*f
, Object
*o
, const void **data
, size_t *size
) {
2361 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2364 /* We can't read objects larger than 4G on a 32bit machine */
2365 if ((uint64_t) t
!= l
)
2368 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2370 #if HAVE_XZ || HAVE_LZ4
2374 r
= decompress_blob(compression
,
2375 o
->data
.payload
, l
, &f
->compress_buffer
,
2376 &f
->compress_buffer_size
, &rsize
, j
->data_threshold
);
2380 *data
= f
->compress_buffer
;
2381 *size
= (size_t) rsize
;
2383 return -EPROTONOSUPPORT
;
2386 *data
= o
->data
.payload
;
2393 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2400 assert_return(j
, -EINVAL
);
2401 assert_return(!journal_pid_changed(j
), -ECHILD
);
2402 assert_return(data
, -EINVAL
);
2403 assert_return(size
, -EINVAL
);
2405 f
= j
->current_file
;
2407 return -EADDRNOTAVAIL
;
2409 if (f
->current_offset
<= 0)
2410 return -EADDRNOTAVAIL
;
2412 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2416 n
= journal_file_entry_n_items(o
);
2417 if (j
->current_field
>= n
)
2420 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2421 le_hash
= o
->entry
.items
[j
->current_field
].hash
;
2422 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2426 if (le_hash
!= o
->data
.hash
)
2429 r
= return_data(j
, f
, o
, data
, size
);
2438 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2442 j
->current_field
= 0;
2445 static int reiterate_all_paths(sd_journal
*j
) {
2448 if (j
->no_new_files
)
2449 return add_current_paths(j
);
2451 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2452 return add_search_paths(j
);
2454 if (j
->toplevel_fd
>= 0)
2455 return add_root_directory(j
, NULL
, false);
2458 return add_root_directory(j
, j
->path
, true);
2460 return add_search_paths(j
);
2463 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2466 assert_return(j
, -EINVAL
);
2467 assert_return(!journal_pid_changed(j
), -ECHILD
);
2470 return -EMEDIUMTYPE
;
2472 if (j
->inotify_fd
>= 0)
2473 return j
->inotify_fd
;
2475 r
= allocate_inotify(j
);
2479 log_debug("Reiterating files to get inotify watches established.");
2481 /* Iterate through all dirs again, to add them to the inotify */
2482 r
= reiterate_all_paths(j
);
2486 return j
->inotify_fd
;
2489 _public_
int sd_journal_get_events(sd_journal
*j
) {
2492 assert_return(j
, -EINVAL
);
2493 assert_return(!journal_pid_changed(j
), -ECHILD
);
2495 fd
= sd_journal_get_fd(j
);
2502 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2505 assert_return(j
, -EINVAL
);
2506 assert_return(!journal_pid_changed(j
), -ECHILD
);
2507 assert_return(timeout_usec
, -EINVAL
);
2509 fd
= sd_journal_get_fd(j
);
2513 if (!j
->on_network
) {
2514 *timeout_usec
= (uint64_t) -1;
2518 /* If we are on the network we need to regularly check for
2519 * changes manually */
2521 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2525 static void process_q_overflow(sd_journal
*j
) {
2532 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2533 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2534 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2535 * are subject for unloading. */
2537 log_debug("Inotify queue overrun, reiterating everything.");
2540 (void) reiterate_all_paths(j
);
2542 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2544 if (f
->last_seen_generation
== j
->generation
)
2547 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2548 remove_file_real(j
, f
);
2551 HASHMAP_FOREACH(m
, j
->directories_by_path
, i
) {
2553 if (m
->last_seen_generation
== j
->generation
)
2556 if (m
->is_root
) /* Never GC root directories */
2559 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2560 remove_directory(j
, m
);
2563 log_debug("Reiteration complete.");
2566 static void process_inotify_event(sd_journal
*j
, const struct inotify_event
*e
) {
2572 if (e
->mask
& IN_Q_OVERFLOW
) {
2573 process_q_overflow(j
);
2577 /* Is this a subdirectory we watch? */
2578 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2580 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2581 (endswith(e
->name
, ".journal") ||
2582 endswith(e
->name
, ".journal~"))) {
2584 /* Event for a journal file */
2586 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2587 (void) add_file_by_name(j
, d
->path
, e
->name
);
2588 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2589 remove_file_by_name(j
, d
->path
, e
->name
);
2591 } else if (!d
->is_root
&& e
->len
== 0) {
2593 /* Event for a subdirectory */
2595 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2596 remove_directory(j
, d
);
2598 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2600 /* Event for root directory */
2602 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2603 (void) add_directory(j
, d
->path
, e
->name
);
2609 if (e
->mask
& IN_IGNORED
)
2612 log_debug("Unexpected inotify event.");
2615 static int determine_change(sd_journal
*j
) {
2620 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2621 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2623 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2626 _public_
int sd_journal_process(sd_journal
*j
) {
2627 bool got_something
= false;
2629 assert_return(j
, -EINVAL
);
2630 assert_return(!journal_pid_changed(j
), -ECHILD
);
2632 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
2635 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2636 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2639 union inotify_event_buffer buffer
;
2640 struct inotify_event
*e
;
2643 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2645 if (IN_SET(errno
, EAGAIN
, EINTR
))
2646 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2651 got_something
= true;
2653 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2654 process_inotify_event(j
, e
);
2658 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2662 assert_return(j
, -EINVAL
);
2663 assert_return(!journal_pid_changed(j
), -ECHILD
);
2665 if (j
->inotify_fd
< 0) {
2669 /* This is the first invocation, hence create the
2671 r
= sd_journal_get_fd(j
);
2675 /* Server might have done some vacuuming while we weren't watching.
2676 Get rid of the deleted files now so they don't stay around indefinitely. */
2677 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2678 r
= journal_file_fstat(f
);
2680 log_debug_errno(r
,"Failed to fstat() journal file '%s' : %m", f
->path
);
2684 if (f
->last_stat
.st_nlink
<= 0)
2685 remove_file_real(j
, f
);
2688 /* The journal might have changed since the context
2689 * object was created and we weren't watching before,
2690 * hence don't wait for anything, and return
2692 return determine_change(j
);
2695 r
= sd_journal_get_timeout(j
, &t
);
2699 if (t
!= (uint64_t) -1) {
2702 n
= now(CLOCK_MONOTONIC
);
2703 t
= t
> n
? t
- n
: 0;
2705 if (timeout_usec
== (uint64_t) -1 || timeout_usec
> t
)
2710 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2711 } while (r
== -EINTR
);
2716 return sd_journal_process(j
);
2719 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2723 uint64_t fmin
= 0, tmax
= 0;
2726 assert_return(j
, -EINVAL
);
2727 assert_return(!journal_pid_changed(j
), -ECHILD
);
2728 assert_return(from
|| to
, -EINVAL
);
2729 assert_return(from
!= to
, -EINVAL
);
2731 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2734 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2747 fmin
= MIN(fr
, fmin
);
2748 tmax
= MAX(t
, tmax
);
2757 return first
? 0 : 1;
2760 _public_
int sd_journal_get_cutoff_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t *from
, uint64_t *to
) {
2766 assert_return(j
, -EINVAL
);
2767 assert_return(!journal_pid_changed(j
), -ECHILD
);
2768 assert_return(from
|| to
, -EINVAL
);
2769 assert_return(from
!= to
, -EINVAL
);
2771 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2774 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &fr
, &t
);
2784 *from
= MIN(fr
, *from
);
2799 void journal_print_header(sd_journal
*j
) {
2802 bool newline
= false;
2806 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2812 journal_file_print_header(f
);
2816 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *bytes
) {
2821 assert_return(j
, -EINVAL
);
2822 assert_return(!journal_pid_changed(j
), -ECHILD
);
2823 assert_return(bytes
, -EINVAL
);
2825 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2828 if (fstat(f
->fd
, &st
) < 0)
2831 sum
+= (uint64_t) st
.st_blocks
* 512ULL;
2838 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2841 assert_return(j
, -EINVAL
);
2842 assert_return(!journal_pid_changed(j
), -ECHILD
);
2843 assert_return(!isempty(field
), -EINVAL
);
2844 assert_return(field_is_valid(field
), -EINVAL
);
2850 free(j
->unique_field
);
2851 j
->unique_field
= f
;
2852 j
->unique_file
= NULL
;
2853 j
->unique_offset
= 0;
2854 j
->unique_file_lost
= false;
2859 _public_
int sd_journal_enumerate_unique(sd_journal
*j
, const void **data
, size_t *l
) {
2862 assert_return(j
, -EINVAL
);
2863 assert_return(!journal_pid_changed(j
), -ECHILD
);
2864 assert_return(data
, -EINVAL
);
2865 assert_return(l
, -EINVAL
);
2866 assert_return(j
->unique_field
, -EINVAL
);
2868 k
= strlen(j
->unique_field
);
2870 if (!j
->unique_file
) {
2871 if (j
->unique_file_lost
)
2874 j
->unique_file
= ordered_hashmap_first(j
->files
);
2875 if (!j
->unique_file
)
2878 j
->unique_offset
= 0;
2890 /* Proceed to next data object in the field's linked list */
2891 if (j
->unique_offset
== 0) {
2892 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2896 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2898 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2902 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2905 /* We reached the end of the list? Then start again, with the next file */
2906 if (j
->unique_offset
== 0) {
2907 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2908 if (!j
->unique_file
)
2914 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2915 * instead, so that we can look at this data object at the same
2916 * time as one on another file */
2917 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2921 /* Let's do the type check by hand, since we used 0 context above. */
2922 if (o
->object
.type
!= OBJECT_DATA
)
2923 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2924 "%s:offset " OFSfmt
": object has type %d, expected %d",
2925 j
->unique_file
->path
,
2927 o
->object
.type
, OBJECT_DATA
);
2929 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2933 /* Check if we have at least the field name and "=". */
2935 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2936 "%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2937 j
->unique_file
->path
,
2938 j
->unique_offset
, ol
, k
+ 1);
2940 if (memcmp(odata
, j
->unique_field
, k
) || ((const char*) odata
)[k
] != '=')
2941 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2942 "%s:offset " OFSfmt
": object does not start with \"%s=\"",
2943 j
->unique_file
->path
,
2947 /* OK, now let's see if we already returned this data
2948 * object by checking if it exists in the earlier
2949 * traversed files. */
2951 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2952 if (of
== j
->unique_file
)
2955 /* Skip this file it didn't have any fields indexed */
2956 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2959 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
2971 r
= return_data(j
, j
->unique_file
, o
, data
, l
);
2979 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
2983 j
->unique_file
= NULL
;
2984 j
->unique_offset
= 0;
2985 j
->unique_file_lost
= false;
2988 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
2991 assert_return(j
, -EINVAL
);
2992 assert_return(!journal_pid_changed(j
), -ECHILD
);
2993 assert_return(field
, -EINVAL
);
2995 if (!j
->fields_file
) {
2996 if (j
->fields_file_lost
)
2999 j
->fields_file
= ordered_hashmap_first(j
->files
);
3000 if (!j
->fields_file
)
3003 j
->fields_hash_table_index
= 0;
3004 j
->fields_offset
= 0;
3008 JournalFile
*f
, *of
;
3017 if (j
->fields_offset
== 0) {
3020 /* We are not yet positioned at any field. Let's pick the first one */
3021 r
= journal_file_map_field_hash_table(f
);
3025 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
3027 if (j
->fields_hash_table_index
>= m
) {
3028 /* Reached the end of the hash table, go to the next file. */
3033 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
3035 if (j
->fields_offset
!= 0)
3038 /* Empty hash table bucket, go to next one */
3039 j
->fields_hash_table_index
++;
3043 /* Proceed with next file */
3044 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
3045 if (!j
->fields_file
) {
3050 j
->fields_offset
= 0;
3051 j
->fields_hash_table_index
= 0;
3056 /* We are already positioned at a field. If so, let's figure out the next field from it */
3058 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
3062 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
3063 if (j
->fields_offset
== 0) {
3064 /* Reached the end of the hash table chain */
3065 j
->fields_hash_table_index
++;
3070 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
3071 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
3075 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
3076 if (o
->object
.type
!= OBJECT_FIELD
)
3077 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3078 "%s:offset " OFSfmt
": object has type %i, expected %i",
3079 f
->path
, j
->fields_offset
,
3080 o
->object
.type
, OBJECT_FIELD
);
3082 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
3084 /* Let's see if we already returned this field name before. */
3086 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
3090 /* Skip this file it didn't have any fields indexed */
3091 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3094 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
, le64toh(o
->field
.hash
), NULL
, NULL
);
3106 /* Check if this is really a valid string containing no NUL byte */
3107 if (memchr(o
->field
.payload
, 0, sz
))
3110 if (sz
> j
->data_threshold
)
3111 sz
= j
->data_threshold
;
3113 if (!GREEDY_REALLOC(j
->fields_buffer
, j
->fields_buffer_allocated
, sz
+ 1))
3116 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3117 j
->fields_buffer
[sz
] = 0;
3119 if (!field_is_valid(j
->fields_buffer
))
3122 *field
= j
->fields_buffer
;
3127 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3131 j
->fields_file
= NULL
;
3132 j
->fields_hash_table_index
= 0;
3133 j
->fields_offset
= 0;
3134 j
->fields_file_lost
= false;
3137 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3138 assert_return(j
, -EINVAL
);
3139 assert_return(!journal_pid_changed(j
), -ECHILD
);
3141 return !j
->on_network
;
3144 static char *lookup_field(const char *field
, void *userdata
) {
3145 sd_journal
*j
= userdata
;
3153 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3155 size
> REPLACE_VAR_MAX
)
3156 return strdup(field
);
3158 d
= strlen(field
) + 1;
3160 return strndup((const char*) data
+ d
, size
- d
);
3163 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3167 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3171 assert_return(j
, -EINVAL
);
3172 assert_return(!journal_pid_changed(j
), -ECHILD
);
3173 assert_return(ret
, -EINVAL
);
3175 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3179 cid
= strndup((const char*) data
+ 11, size
- 11);
3183 r
= sd_id128_from_string(cid
, &id
);
3187 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
3191 t
= replace_var(text
, lookup_field
, j
);
3199 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3200 assert_return(ret
, -EINVAL
);
3202 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3205 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3206 assert_return(j
, -EINVAL
);
3207 assert_return(!journal_pid_changed(j
), -ECHILD
);
3209 j
->data_threshold
= sz
;
3213 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3214 assert_return(j
, -EINVAL
);
3215 assert_return(!journal_pid_changed(j
), -ECHILD
);
3216 assert_return(sz
, -EINVAL
);
3218 *sz
= j
->data_threshold
;
3222 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3223 assert_return(j
, -EINVAL
);
3225 return j
->has_runtime_files
;
3228 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3229 assert_return(j
, -EINVAL
);
3231 return j
->has_persistent_files
;