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 init_location(Location
*l
, LocationType type
, JournalFile
*f
, Object
*o
) {
120 assert(IN_SET(type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
125 .seqnum
= le64toh(o
->entry
.seqnum
),
126 .seqnum_id
= f
->header
->seqnum_id
,
127 .realtime
= le64toh(o
->entry
.realtime
),
128 .monotonic
= le64toh(o
->entry
.monotonic
),
129 .boot_id
= o
->entry
.boot_id
,
130 .xor_hash
= le64toh(o
->entry
.xor_hash
),
132 .realtime_set
= true,
133 .monotonic_set
= true,
134 .xor_hash_set
= true,
138 static void set_location(sd_journal
*j
, JournalFile
*f
, Object
*o
) {
143 init_location(&j
->current_location
, LOCATION_DISCRETE
, f
, o
);
146 j
->current_field
= 0;
148 /* Let f know its candidate entry was picked. */
149 assert(f
->location_type
== LOCATION_SEEK
);
150 f
->location_type
= LOCATION_DISCRETE
;
153 static int match_is_valid(const void *data
, size_t size
) {
161 if (((char*) data
)[0] == '_' && ((char*) data
)[1] == '_')
165 for (p
= b
; p
< b
+ size
; p
++) {
173 if (*p
>= 'A' && *p
<= 'Z')
176 if (*p
>= '0' && *p
<= '9')
185 static bool same_field(const void *_a
, size_t s
, const void *_b
, size_t t
) {
186 const uint8_t *a
= _a
, *b
= _b
;
189 for (j
= 0; j
< s
&& j
< t
; j
++) {
198 assert_not_reached("\"=\" not found");
201 static Match
*match_new(Match
*p
, MatchType t
) {
214 LIST_PREPEND(matches
, p
->matches
, m
);
219 static void match_free(Match
*m
) {
223 match_free(m
->matches
);
226 LIST_REMOVE(matches
, m
->parent
->matches
, m
);
232 static void match_free_if_empty(Match
*m
) {
233 if (!m
|| m
->matches
)
239 _public_
int sd_journal_add_match(sd_journal
*j
, const void *data
, size_t size
) {
240 Match
*l3
, *l4
, *add_here
= NULL
, *m
;
243 assert_return(j
, -EINVAL
);
244 assert_return(!journal_pid_changed(j
), -ECHILD
);
245 assert_return(data
, -EINVAL
);
250 assert_return(match_is_valid(data
, size
), -EINVAL
);
256 * level 4: concrete matches */
259 j
->level0
= match_new(NULL
, MATCH_AND_TERM
);
265 j
->level1
= match_new(j
->level0
, MATCH_OR_TERM
);
271 j
->level2
= match_new(j
->level1
, MATCH_AND_TERM
);
276 assert(j
->level0
->type
== MATCH_AND_TERM
);
277 assert(j
->level1
->type
== MATCH_OR_TERM
);
278 assert(j
->level2
->type
== MATCH_AND_TERM
);
280 le_hash
= htole64(hash64(data
, size
));
282 LIST_FOREACH(matches
, l3
, j
->level2
->matches
) {
283 assert(l3
->type
== MATCH_OR_TERM
);
285 LIST_FOREACH(matches
, l4
, l3
->matches
) {
286 assert(l4
->type
== MATCH_DISCRETE
);
288 /* Exactly the same match already? Then ignore
290 if (l4
->le_hash
== le_hash
&&
292 memcmp(l4
->data
, data
, size
) == 0)
295 /* Same field? Then let's add this to this OR term */
296 if (same_field(data
, size
, l4
->data
, l4
->size
)) {
307 add_here
= match_new(j
->level2
, MATCH_OR_TERM
);
312 m
= match_new(add_here
, MATCH_DISCRETE
);
316 m
->le_hash
= le_hash
;
318 m
->data
= memdup(data
, size
);
327 match_free_if_empty(add_here
);
328 match_free_if_empty(j
->level2
);
329 match_free_if_empty(j
->level1
);
330 match_free_if_empty(j
->level0
);
335 _public_
int sd_journal_add_conjunction(sd_journal
*j
) {
336 assert_return(j
, -EINVAL
);
337 assert_return(!journal_pid_changed(j
), -ECHILD
);
345 if (!j
->level1
->matches
)
354 _public_
int sd_journal_add_disjunction(sd_journal
*j
) {
355 assert_return(j
, -EINVAL
);
356 assert_return(!journal_pid_changed(j
), -ECHILD
);
367 if (!j
->level2
->matches
)
374 static char *match_make_string(Match
*m
) {
377 bool enclose
= false;
380 return strdup("none");
382 if (m
->type
== MATCH_DISCRETE
)
383 return cescape_length(m
->data
, m
->size
);
385 LIST_FOREACH(matches
, i
, m
->matches
) {
388 t
= match_make_string(i
);
393 k
= strjoin(p
, m
->type
== MATCH_OR_TERM
? " OR " : " AND ", t
);
408 r
= strjoin("(", p
, ")");
416 char *journal_make_match_string(sd_journal
*j
) {
419 return match_make_string(j
->level0
);
422 _public_
void sd_journal_flush_matches(sd_journal
*j
) {
427 match_free(j
->level0
);
429 j
->level0
= j
->level1
= j
->level2
= NULL
;
434 _pure_
static int compare_with_location(const JournalFile
*f
, const Location
*l
, const JournalFile
*current_file
) {
439 assert(f
->location_type
== LOCATION_SEEK
);
440 assert(IN_SET(l
->type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
442 if (l
->monotonic_set
&&
443 sd_id128_equal(f
->current_boot_id
, l
->boot_id
) &&
445 f
->current_realtime
== l
->realtime
&&
447 f
->current_xor_hash
== l
->xor_hash
&&
452 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
)) {
454 r
= CMP(f
->current_seqnum
, l
->seqnum
);
459 if (l
->monotonic_set
&&
460 sd_id128_equal(f
->current_boot_id
, l
->boot_id
)) {
462 r
= CMP(f
->current_monotonic
, l
->monotonic
);
467 if (l
->realtime_set
) {
469 r
= CMP(f
->current_realtime
, l
->realtime
);
474 if (l
->xor_hash_set
) {
476 r
= CMP(f
->current_xor_hash
, l
->xor_hash
);
484 static int next_for_match(
488 uint64_t after_offset
,
489 direction_t direction
,
501 if (m
->type
== MATCH_DISCRETE
) {
504 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
508 return journal_file_move_to_entry_by_offset_for_data(f
, dp
, after_offset
, direction
, ret
, offset
);
510 } else if (m
->type
== MATCH_OR_TERM
) {
513 /* Find the earliest match beyond after_offset */
515 LIST_FOREACH(matches
, i
, m
->matches
) {
518 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
522 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
530 } else if (m
->type
== MATCH_AND_TERM
) {
531 Match
*i
, *last_moved
;
533 /* Always jump to the next matching entry and repeat
534 * this until we find an offset that matches for all
540 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
544 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
545 last_moved
= m
->matches
;
547 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
550 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
554 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
555 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
564 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
576 static int find_location_for_match(
580 direction_t direction
,
590 if (m
->type
== MATCH_DISCRETE
) {
593 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
597 /* FIXME: missing: find by monotonic */
599 if (j
->current_location
.type
== LOCATION_HEAD
)
600 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_DOWN
, ret
, offset
);
601 if (j
->current_location
.type
== LOCATION_TAIL
)
602 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_UP
, ret
, offset
);
603 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
604 return journal_file_move_to_entry_by_seqnum_for_data(f
, dp
, j
->current_location
.seqnum
, direction
, ret
, offset
);
605 if (j
->current_location
.monotonic_set
) {
606 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, dp
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
610 if (j
->current_location
.realtime_set
)
611 return journal_file_move_to_entry_by_realtime_for_data(f
, dp
, j
->current_location
.realtime
, direction
, ret
, offset
);
613 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, direction
, ret
, offset
);
615 } else if (m
->type
== MATCH_OR_TERM
) {
620 /* Find the earliest match */
622 LIST_FOREACH(matches
, i
, m
->matches
) {
625 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
629 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
637 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
652 assert(m
->type
== MATCH_AND_TERM
);
654 /* First jump to the last match, and then find the
655 * next one where all matches match */
660 LIST_FOREACH(matches
, i
, m
->matches
) {
663 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
667 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
671 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
675 static int find_location_with_matches(
678 direction_t direction
,
690 /* No matches is simple */
692 if (j
->current_location
.type
== LOCATION_HEAD
)
693 return journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
);
694 if (j
->current_location
.type
== LOCATION_TAIL
)
695 return journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
);
696 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
697 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
698 if (j
->current_location
.monotonic_set
) {
699 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
703 if (j
->current_location
.realtime_set
)
704 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
706 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
708 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
711 static int next_with_matches(
714 direction_t direction
,
723 /* No matches is easy. We simple advance the file
726 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
728 /* If we have a match then we look for the next matching entry
729 * with an offset at least one step larger */
730 return next_for_match(j
, j
->level0
, f
,
731 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
732 : f
->current_offset
- 1,
733 direction
, ret
, offset
);
736 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
738 uint64_t cp
, n_entries
;
744 n_entries
= le64toh(f
->header
->n_entries
);
746 /* If we hit EOF before, we don't need to look into this file again
747 * unless direction changed or new entries appeared. */
748 if (f
->last_direction
== direction
&& f
->location_type
== LOCATION_TAIL
&&
749 n_entries
== f
->last_n_entries
)
752 f
->last_n_entries
= n_entries
;
754 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
755 /* LOCATION_SEEK here means we did the work in a previous
756 * iteration and the current location already points to a
757 * candidate entry. */
758 if (f
->location_type
!= LOCATION_SEEK
) {
759 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
763 journal_file_save_location(f
, c
, cp
);
766 f
->last_direction
= direction
;
768 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
772 journal_file_save_location(f
, c
, cp
);
775 /* OK, we found the spot, now let's advance until an entry
776 * that is actually different from what we were previously
777 * looking at. This is necessary to handle entries which exist
778 * in two (or more) journal files, and which shall all be
779 * suppressed but one. */
784 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
787 k
= compare_with_location(f
, &j
->current_location
, j
->current_file
);
789 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
796 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
800 journal_file_save_location(f
, c
, cp
);
804 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
805 JournalFile
*new_file
= NULL
;
811 assert_return(j
, -EINVAL
);
812 assert_return(!journal_pid_changed(j
), -ECHILD
);
814 r
= iterated_cache_get(j
->files_cache
, NULL
, &files
, &n_files
);
818 for (i
= 0; i
< n_files
; i
++) {
819 JournalFile
*f
= (JournalFile
*)files
[i
];
822 r
= next_beyond_location(j
, f
, direction
);
824 log_debug_errno(r
, "Can't iterate through %s, ignoring: %m", f
->path
);
825 remove_file_real(j
, f
);
828 f
->location_type
= LOCATION_TAIL
;
837 k
= journal_file_compare_locations(f
, new_file
);
839 found
= direction
== DIRECTION_DOWN
? k
< 0 : k
> 0;
849 r
= journal_file_move_to_object(new_file
, OBJECT_ENTRY
, new_file
->current_offset
, &o
);
853 set_location(j
, new_file
, o
);
858 _public_
int sd_journal_next(sd_journal
*j
) {
859 return real_journal_next(j
, DIRECTION_DOWN
);
862 _public_
int sd_journal_previous(sd_journal
*j
) {
863 return real_journal_next(j
, DIRECTION_UP
);
866 static int real_journal_next_skip(sd_journal
*j
, direction_t direction
, uint64_t skip
) {
869 assert_return(j
, -EINVAL
);
870 assert_return(!journal_pid_changed(j
), -ECHILD
);
873 /* If this is not a discrete skip, then at least
874 * resolve the current location */
875 if (j
->current_location
.type
!= LOCATION_DISCRETE
) {
876 r
= real_journal_next(j
, direction
);
885 r
= real_journal_next(j
, direction
);
899 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
900 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
903 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
904 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
907 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
910 char bid
[SD_ID128_STRING_MAX
], sid
[SD_ID128_STRING_MAX
];
912 assert_return(j
, -EINVAL
);
913 assert_return(!journal_pid_changed(j
), -ECHILD
);
914 assert_return(cursor
, -EINVAL
);
916 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
917 return -EADDRNOTAVAIL
;
919 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
923 sd_id128_to_string(j
->current_file
->header
->seqnum_id
, sid
);
924 sd_id128_to_string(o
->entry
.boot_id
, bid
);
927 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
928 sid
, le64toh(o
->entry
.seqnum
),
929 bid
, le64toh(o
->entry
.monotonic
),
930 le64toh(o
->entry
.realtime
),
931 le64toh(o
->entry
.xor_hash
)) < 0)
937 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
938 const char *word
, *state
;
940 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
942 seqnum_id_set
= false,
945 monotonic_set
= false,
946 realtime_set
= false,
947 xor_hash_set
= false;
948 sd_id128_t seqnum_id
, boot_id
;
950 assert_return(j
, -EINVAL
);
951 assert_return(!journal_pid_changed(j
), -ECHILD
);
952 assert_return(!isempty(cursor
), -EINVAL
);
954 FOREACH_WORD_SEPARATOR(word
, l
, cursor
, ";", state
) {
958 if (l
< 2 || word
[1] != '=')
961 item
= strndup(word
, l
);
968 seqnum_id_set
= true;
969 k
= sd_id128_from_string(item
+2, &seqnum_id
);
974 if (sscanf(item
+2, "%llx", &seqnum
) != 1)
980 k
= sd_id128_from_string(item
+2, &boot_id
);
984 monotonic_set
= true;
985 if (sscanf(item
+2, "%llx", &monotonic
) != 1)
991 if (sscanf(item
+2, "%llx", &realtime
) != 1)
997 if (sscanf(item
+2, "%llx", &xor_hash
) != 1)
1008 if ((!seqnum_set
|| !seqnum_id_set
) &&
1009 (!monotonic_set
|| !boot_id_set
) &&
1014 j
->current_location
= (Location
) {
1015 .type
= LOCATION_SEEK
,
1019 j
->current_location
.realtime
= (uint64_t) realtime
;
1020 j
->current_location
.realtime_set
= true;
1023 if (seqnum_set
&& seqnum_id_set
) {
1024 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1025 j
->current_location
.seqnum_id
= seqnum_id
;
1026 j
->current_location
.seqnum_set
= true;
1029 if (monotonic_set
&& boot_id_set
) {
1030 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1031 j
->current_location
.boot_id
= boot_id
;
1032 j
->current_location
.monotonic_set
= true;
1036 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1037 j
->current_location
.xor_hash_set
= true;
1043 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1047 assert_return(j
, -EINVAL
);
1048 assert_return(!journal_pid_changed(j
), -ECHILD
);
1049 assert_return(!isempty(cursor
), -EINVAL
);
1051 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1052 return -EADDRNOTAVAIL
;
1054 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1059 _cleanup_free_
char *item
= NULL
;
1060 unsigned long long ll
;
1064 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1071 if (strlen(item
) < 2 || item
[1] != '=')
1077 k
= sd_id128_from_string(item
+2, &id
);
1080 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1085 if (sscanf(item
+2, "%llx", &ll
) != 1)
1087 if (ll
!= le64toh(o
->entry
.seqnum
))
1092 k
= sd_id128_from_string(item
+2, &id
);
1095 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1100 if (sscanf(item
+2, "%llx", &ll
) != 1)
1102 if (ll
!= le64toh(o
->entry
.monotonic
))
1107 if (sscanf(item
+2, "%llx", &ll
) != 1)
1109 if (ll
!= le64toh(o
->entry
.realtime
))
1114 if (sscanf(item
+2, "%llx", &ll
) != 1)
1116 if (ll
!= le64toh(o
->entry
.xor_hash
))
1125 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t usec
) {
1126 assert_return(j
, -EINVAL
);
1127 assert_return(!journal_pid_changed(j
), -ECHILD
);
1131 j
->current_location
= (Location
) {
1132 .type
= LOCATION_SEEK
,
1135 .monotonic_set
= true,
1141 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1142 assert_return(j
, -EINVAL
);
1143 assert_return(!journal_pid_changed(j
), -ECHILD
);
1147 j
->current_location
= (Location
) {
1148 .type
= LOCATION_SEEK
,
1150 .realtime_set
= true,
1156 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1157 assert_return(j
, -EINVAL
);
1158 assert_return(!journal_pid_changed(j
), -ECHILD
);
1162 j
->current_location
= (Location
) {
1163 .type
= LOCATION_HEAD
,
1169 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1170 assert_return(j
, -EINVAL
);
1171 assert_return(!journal_pid_changed(j
), -ECHILD
);
1175 j
->current_location
= (Location
) {
1176 .type
= LOCATION_TAIL
,
1182 static void check_network(sd_journal
*j
, int fd
) {
1188 j
->on_network
= fd_is_network_fs(fd
);
1191 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1192 const char *full
, *tilded
, *atted
;
1194 full
= strjoina(prefix
, ".journal");
1195 tilded
= strjoina(full
, "~");
1196 atted
= strjoina(prefix
, "@");
1198 return STR_IN_SET(filename
, full
, tilded
) ||
1199 startswith(filename
, atted
);
1202 static bool file_type_wanted(int flags
, const char *filename
) {
1205 if (!endswith(filename
, ".journal") && !endswith(filename
, ".journal~"))
1208 /* no flags set → every type is OK */
1209 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1212 if (flags
& SD_JOURNAL_SYSTEM
&& file_has_type_prefix("system", filename
))
1215 if (flags
& SD_JOURNAL_CURRENT_USER
) {
1216 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1218 xsprintf(prefix
, "user-"UID_FMT
, getuid());
1220 if (file_has_type_prefix(prefix
, filename
))
1227 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1232 if (j
->toplevel_fd
>= 0)
1235 return path_startswith(path
, prefix
);
1238 static void track_file_disposition(sd_journal
*j
, JournalFile
*f
) {
1242 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1243 j
->has_runtime_files
= true;
1244 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1245 j
->has_persistent_files
= true;
1248 static const char *skip_slash(const char *p
) {
1259 static int add_any_file(
1264 bool close_fd
= false;
1270 assert(fd
>= 0 || path
);
1273 if (j
->toplevel_fd
>= 0)
1274 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1275 * openat() ignores the first argument. */
1277 fd
= openat(j
->toplevel_fd
, skip_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1279 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1281 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1287 r
= fd_nonblock(fd
, false);
1289 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1294 if (fstat(fd
, &st
) < 0) {
1295 r
= log_debug_errno(errno
, "Failed to fstat file '%s': %m", path
);
1299 r
= stat_verify_regular(&st
);
1301 log_debug_errno(r
, "Refusing to open '%s', as it is not a regular file.", path
);
1305 f
= ordered_hashmap_get(j
->files
, path
);
1307 if (f
->last_stat
.st_dev
== st
.st_dev
&&
1308 f
->last_stat
.st_ino
== st
.st_ino
) {
1310 /* We already track this file, under the same path and with the same device/inode numbers, it's
1311 * hence really the same. Mark this file as seen in this generation. This is used to GC old
1312 * files in process_q_overflow() to detect journal files that are still there and discern them
1313 * from those which are gone. */
1315 f
->last_seen_generation
= j
->generation
;
1320 /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
1321 * replaced (probably due to rotation?), let's drop it hence from our list. */
1322 remove_file_real(j
, f
);
1326 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1327 log_debug("Too many open journal files, not adding %s.", path
);
1332 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, false, 0, false, NULL
, j
->mmap
, NULL
, NULL
, &f
);
1334 log_debug_errno(r
, "Failed to open journal file %s: %m", path
);
1338 /* journal_file_dump(f); */
1340 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1342 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 */
1343 (void) journal_file_close(f
);
1347 close_fd
= false; /* the fd is now owned by the JournalFile object */
1349 f
->last_seen_generation
= j
->generation
;
1351 track_file_disposition(j
, f
);
1352 check_network(j
, f
->fd
);
1354 j
->current_invalidate_counter
++;
1356 log_debug("File %s added.", f
->path
);
1365 k
= journal_put_error(j
, r
, path
);
1373 static int add_file_by_name(
1376 const char *filename
) {
1384 if (j
->no_new_files
)
1387 if (!file_type_wanted(j
->flags
, filename
))
1390 path
= prefix_roota(prefix
, filename
);
1391 return add_any_file(j
, -1, path
);
1394 static void remove_file_by_name(
1397 const char *filename
) {
1406 path
= prefix_roota(prefix
, filename
);
1407 f
= ordered_hashmap_get(j
->files
, path
);
1411 remove_file_real(j
, f
);
1414 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1418 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1420 log_debug("File %s removed.", f
->path
);
1422 if (j
->current_file
== f
) {
1423 j
->current_file
= NULL
;
1424 j
->current_field
= 0;
1427 if (j
->unique_file
== f
) {
1428 /* Jump to the next unique_file or NULL if that one was last */
1429 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1430 j
->unique_offset
= 0;
1431 if (!j
->unique_file
)
1432 j
->unique_file_lost
= true;
1435 if (j
->fields_file
== f
) {
1436 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1437 j
->fields_offset
= 0;
1438 if (!j
->fields_file
)
1439 j
->fields_file_lost
= true;
1442 (void) journal_file_close(f
);
1444 j
->current_invalidate_counter
++;
1447 static int dirname_is_machine_id(const char *fn
) {
1448 sd_id128_t id
, machine
;
1452 /* Returns true if the specified directory name matches the local machine ID */
1454 r
= sd_id128_get_machine(&machine
);
1458 e
= strchr(fn
, '.');
1462 /* Looks like it has a namespace suffix. Verify that. */
1463 if (!log_namespace_name_valid(e
+ 1))
1466 k
= strndupa(fn
, e
- fn
);
1467 r
= sd_id128_from_string(k
, &id
);
1469 r
= sd_id128_from_string(fn
, &id
);
1473 return sd_id128_equal(id
, machine
);
1476 static int dirname_has_namespace(const char *fn
, const char *namespace) {
1479 /* Returns true if the specified directory name matches the specified namespace */
1481 e
= strchr(fn
, '.');
1488 if (!streq(e
+ 1, namespace))
1491 k
= strndupa(fn
, e
- fn
);
1492 return id128_is_valid(k
);
1498 return id128_is_valid(fn
);
1501 static bool dirent_is_journal_file(const struct dirent
*de
) {
1504 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1506 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1509 return endswith(de
->d_name
, ".journal") ||
1510 endswith(de
->d_name
, ".journal~");
1513 static bool dirent_is_journal_subdir(const struct dirent
*de
) {
1517 /* returns true if the specified directory entry looks like a directory that might contain journal
1518 * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a
1521 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1524 e
= strchr(de
->d_name
, '.');
1526 return id128_is_valid(de
->d_name
); /* No namespace */
1528 n
= strndupa(de
->d_name
, e
- de
->d_name
);
1529 if (!id128_is_valid(n
))
1532 return log_namespace_name_valid(e
+ 1);
1535 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1542 if (j
->toplevel_fd
< 0)
1545 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1546 * relative, by dropping the initial slash */
1547 d
= xopendirat(j
->toplevel_fd
, skip_slash(path
), 0);
1555 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1557 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1564 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1566 if (dirent_is_journal_file(de
))
1567 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1569 if (m
->is_root
&& dirent_is_journal_subdir(de
))
1570 (void) add_directory(j
, m
->path
, de
->d_name
);
1576 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1579 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1586 /* Watch this directory if that's enabled and if it not being watched yet. */
1588 if (m
->wd
> 0) /* Already have a watch? */
1590 if (j
->inotify_fd
< 0) /* Not watching at all? */
1593 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1595 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1599 r
= hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
);
1601 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1603 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1604 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1609 static int add_directory(
1612 const char *dirname
) {
1614 _cleanup_free_
char *path
= NULL
;
1615 _cleanup_closedir_
DIR *d
= NULL
;
1622 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1623 * and reenumerates directory contents */
1625 path
= path_join(prefix
, dirname
);
1631 log_debug("Considering directory '%s'.", path
);
1633 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1634 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1635 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1638 if (!(FLAGS_SET(j
->flags
, SD_JOURNAL_ALL_NAMESPACES
) ||
1639 dirname_has_namespace(dirname
, j
->namespace) > 0 ||
1640 (FLAGS_SET(j
->flags
, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE
) && dirname_has_namespace(dirname
, NULL
) > 0)))
1643 r
= directory_open(j
, path
, &d
);
1645 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
1649 m
= hashmap_get(j
->directories_by_path
, path
);
1651 m
= new(Directory
, 1);
1662 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1668 path
= NULL
; /* avoid freeing in cleanup */
1669 j
->current_invalidate_counter
++;
1671 log_debug("Directory %s added.", m
->path
);
1673 } else if (m
->is_root
)
1674 return 0; /* Don't 'downgrade' from root directory */
1676 m
->last_seen_generation
= j
->generation
;
1678 directory_watch(j
, m
, dirfd(d
),
1679 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1680 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
1683 if (!j
->no_new_files
)
1684 directory_enumerate(j
, m
, d
);
1686 check_network(j
, dirfd(d
));
1691 k
= journal_put_error(j
, r
, path
?: prefix
);
1698 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
1700 _cleanup_closedir_
DIR *d
= NULL
;
1706 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1707 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1708 * populate the set, as well as to update it later. */
1711 /* If there's a path specified, use it. */
1713 log_debug("Considering root directory '%s'.", p
);
1715 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
1716 !path_has_prefix(j
, p
, "/run"))
1720 p
= strjoina(j
->prefix
, p
);
1722 r
= directory_open(j
, p
, &d
);
1723 if (r
== -ENOENT
&& missing_ok
)
1726 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
1730 _cleanup_close_
int dfd
= -1;
1732 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1733 * opendir() will take possession of the fd, and close it, which we don't want. */
1735 p
= "."; /* store this as "." in the directories hashmap */
1737 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
1743 d
= take_fdopendir(&dfd
);
1752 m
= hashmap_get(j
->directories_by_path
, p
);
1754 m
= new0(Directory
, 1);
1762 m
->path
= strdup(p
);
1769 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1776 j
->current_invalidate_counter
++;
1778 log_debug("Root directory %s added.", m
->path
);
1780 } else if (!m
->is_root
)
1783 directory_watch(j
, m
, dirfd(d
),
1784 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1787 if (!j
->no_new_files
)
1788 directory_enumerate(j
, m
, d
);
1790 check_network(j
, dirfd(d
));
1795 k
= journal_put_error(j
, r
, p
);
1802 static void remove_directory(sd_journal
*j
, Directory
*d
) {
1806 hashmap_remove(j
->directories_by_wd
, INT_TO_PTR(d
->wd
));
1808 if (j
->inotify_fd
>= 0)
1809 (void) inotify_rm_watch(j
->inotify_fd
, d
->wd
);
1812 hashmap_remove(j
->directories_by_path
, d
->path
);
1815 log_debug("Root directory %s removed.", d
->path
);
1817 log_debug("Directory %s removed.", d
->path
);
1823 static int add_search_paths(sd_journal
*j
) {
1825 static const char search_paths
[] =
1826 "/run/log/journal\0"
1827 "/var/log/journal\0";
1832 /* We ignore most errors here, since the idea is to only open
1833 * what's actually accessible, and ignore the rest. */
1835 NULSTR_FOREACH(p
, search_paths
)
1836 (void) add_root_directory(j
, p
, true);
1838 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
1839 (void) add_root_directory(j
, "/var/log/journal/remote", true);
1844 static int add_current_paths(sd_journal
*j
) {
1849 assert(j
->no_new_files
);
1851 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1852 * treat them as fatal. */
1854 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
1855 _cleanup_free_
char *dir
;
1858 dir
= dirname_malloc(f
->path
);
1862 r
= add_directory(j
, dir
, NULL
);
1870 static int allocate_inotify(sd_journal
*j
) {
1873 if (j
->inotify_fd
< 0) {
1874 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1875 if (j
->inotify_fd
< 0)
1879 return hashmap_ensure_allocated(&j
->directories_by_wd
, NULL
);
1882 static sd_journal
*journal_new(int flags
, const char *path
, const char *namespace) {
1883 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1885 j
= new0(sd_journal
, 1);
1889 j
->original_pid
= getpid_cached();
1890 j
->toplevel_fd
= -1;
1893 j
->data_threshold
= DEFAULT_DATA_THRESHOLD
;
1902 if (flags
& SD_JOURNAL_OS_ROOT
)
1909 j
->namespace = strdup(namespace);
1914 j
->files
= ordered_hashmap_new(&path_hash_ops
);
1918 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
1919 j
->directories_by_path
= hashmap_new(&path_hash_ops
);
1920 j
->mmap
= mmap_cache_new();
1921 if (!j
->files_cache
|| !j
->directories_by_path
|| !j
->mmap
)
1927 #define OPEN_ALLOWED_FLAGS \
1928 (SD_JOURNAL_LOCAL_ONLY | \
1929 SD_JOURNAL_RUNTIME_ONLY | \
1930 SD_JOURNAL_SYSTEM | \
1931 SD_JOURNAL_CURRENT_USER | \
1932 SD_JOURNAL_ALL_NAMESPACES | \
1933 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
1935 _public_
int sd_journal_open_namespace(sd_journal
**ret
, const char *namespace, int flags
) {
1936 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1939 assert_return(ret
, -EINVAL
);
1940 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
1942 j
= journal_new(flags
, NULL
, namespace);
1946 r
= add_search_paths(j
);
1954 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1955 return sd_journal_open_namespace(ret
, NULL
, flags
);
1958 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1959 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1961 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1962 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1963 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1967 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1968 * combination with sd_journal_open_directory_fd(). */
1970 assert_return(machine
, -EINVAL
);
1971 assert_return(ret
, -EINVAL
);
1972 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
1973 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1975 p
= strjoina("/run/systemd/machines/", machine
);
1976 r
= parse_env_file(NULL
, p
,
1986 if (!streq_ptr(class, "container"))
1989 j
= journal_new(flags
, root
, NULL
);
1993 r
= add_search_paths(j
);
2001 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
2002 (SD_JOURNAL_OS_ROOT | \
2003 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2005 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
2006 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2009 assert_return(ret
, -EINVAL
);
2010 assert_return(path
, -EINVAL
);
2011 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
2013 j
= journal_new(flags
, path
, NULL
);
2017 if (flags
& SD_JOURNAL_OS_ROOT
)
2018 r
= add_search_paths(j
);
2020 r
= add_root_directory(j
, path
, false);
2028 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
2029 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2033 assert_return(ret
, -EINVAL
);
2034 assert_return(flags
== 0, -EINVAL
);
2036 j
= journal_new(flags
, NULL
, NULL
);
2040 STRV_FOREACH(path
, paths
) {
2041 r
= add_any_file(j
, -1, *path
);
2046 j
->no_new_files
= true;
2052 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
2053 (SD_JOURNAL_OS_ROOT | \
2054 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2056 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
2057 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2061 assert_return(ret
, -EINVAL
);
2062 assert_return(fd
>= 0, -EBADF
);
2063 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2065 if (fstat(fd
, &st
) < 0)
2068 if (!S_ISDIR(st
.st_mode
))
2071 j
= journal_new(flags
, NULL
, NULL
);
2075 j
->toplevel_fd
= fd
;
2077 if (flags
& SD_JOURNAL_OS_ROOT
)
2078 r
= add_search_paths(j
);
2080 r
= add_root_directory(j
, NULL
, false);
2088 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2091 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2095 assert_return(ret
, -EINVAL
);
2096 assert_return(n_fds
> 0, -EBADF
);
2097 assert_return(flags
== 0, -EINVAL
);
2099 j
= journal_new(flags
, NULL
, NULL
);
2103 for (i
= 0; i
< n_fds
; i
++) {
2111 if (fstat(fds
[i
], &st
) < 0) {
2116 r
= stat_verify_regular(&st
);
2120 r
= add_any_file(j
, fds
[i
], NULL
);
2125 j
->no_new_files
= true;
2126 j
->no_inotify
= true;
2132 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2134 ORDERED_HASHMAP_FOREACH(f
, j
->files
, iterator
)
2135 f
->close_fd
= false;
2140 _public_
void sd_journal_close(sd_journal
*j
) {
2146 sd_journal_flush_matches(j
);
2148 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2149 iterated_cache_free(j
->files_cache
);
2151 while ((d
= hashmap_first(j
->directories_by_path
)))
2152 remove_directory(j
, d
);
2154 while ((d
= hashmap_first(j
->directories_by_wd
)))
2155 remove_directory(j
, d
);
2157 hashmap_free(j
->directories_by_path
);
2158 hashmap_free(j
->directories_by_wd
);
2160 safe_close(j
->inotify_fd
);
2163 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j
->mmap
), mmap_cache_get_missed(j
->mmap
));
2164 mmap_cache_unref(j
->mmap
);
2167 hashmap_free_free(j
->errors
);
2172 free(j
->unique_field
);
2173 free(j
->fields_buffer
);
2177 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2182 assert_return(j
, -EINVAL
);
2183 assert_return(!journal_pid_changed(j
), -ECHILD
);
2184 assert_return(ret
, -EINVAL
);
2186 f
= j
->current_file
;
2188 return -EADDRNOTAVAIL
;
2190 if (f
->current_offset
<= 0)
2191 return -EADDRNOTAVAIL
;
2193 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2197 *ret
= le64toh(o
->entry
.realtime
);
2201 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2207 assert_return(j
, -EINVAL
);
2208 assert_return(!journal_pid_changed(j
), -ECHILD
);
2210 f
= j
->current_file
;
2212 return -EADDRNOTAVAIL
;
2214 if (f
->current_offset
<= 0)
2215 return -EADDRNOTAVAIL
;
2217 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2222 *ret_boot_id
= o
->entry
.boot_id
;
2224 r
= sd_id128_get_boot(&id
);
2228 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2233 *ret
= le64toh(o
->entry
.monotonic
);
2238 static bool field_is_valid(const char *field
) {
2246 if (startswith(field
, "__"))
2249 for (p
= field
; *p
; p
++) {
2254 if (*p
>= 'A' && *p
<= 'Z')
2257 if (*p
>= '0' && *p
<= '9')
2266 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2269 size_t field_length
;
2273 assert_return(j
, -EINVAL
);
2274 assert_return(!journal_pid_changed(j
), -ECHILD
);
2275 assert_return(field
, -EINVAL
);
2276 assert_return(data
, -EINVAL
);
2277 assert_return(size
, -EINVAL
);
2278 assert_return(field_is_valid(field
), -EINVAL
);
2280 f
= j
->current_file
;
2282 return -EADDRNOTAVAIL
;
2284 if (f
->current_offset
<= 0)
2285 return -EADDRNOTAVAIL
;
2287 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2291 field_length
= strlen(field
);
2293 n
= journal_file_entry_n_items(o
);
2294 for (i
= 0; i
< n
; i
++) {
2300 p
= le64toh(o
->entry
.items
[i
].object_offset
);
2301 le_hash
= o
->entry
.items
[i
].hash
;
2302 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2306 if (le_hash
!= o
->data
.hash
)
2309 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2311 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2313 #if HAVE_XZ || HAVE_LZ4
2314 r
= decompress_startswith(compression
,
2316 &f
->compress_buffer
, &f
->compress_buffer_size
,
2317 field
, field_length
, '=');
2319 log_debug_errno(r
, "Cannot decompress %s object of length %"PRIu64
" at offset "OFSfmt
": %m",
2320 object_compressed_to_string(compression
), l
, p
);
2325 r
= decompress_blob(compression
,
2327 &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
,
2332 *data
= f
->compress_buffer
;
2333 *size
= (size_t) rsize
;
2338 return -EPROTONOSUPPORT
;
2340 } else if (l
>= field_length
+1 &&
2341 memcmp(o
->data
.payload
, field
, field_length
) == 0 &&
2342 o
->data
.payload
[field_length
] == '=') {
2346 if ((uint64_t) t
!= l
)
2349 *data
= o
->data
.payload
;
2355 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2363 static int return_data(sd_journal
*j
, JournalFile
*f
, Object
*o
, const void **data
, size_t *size
) {
2368 l
= le64toh(READ_NOW(o
->object
.size
));
2369 if (l
< offsetof(Object
, data
.payload
))
2371 l
-= offsetof(Object
, data
.payload
);
2374 /* We can't read objects larger than 4G on a 32bit machine */
2375 if ((uint64_t) t
!= l
)
2378 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2380 #if HAVE_XZ || HAVE_LZ4
2384 r
= decompress_blob(compression
,
2385 o
->data
.payload
, l
, &f
->compress_buffer
,
2386 &f
->compress_buffer_size
, &rsize
, j
->data_threshold
);
2390 *data
= f
->compress_buffer
;
2391 *size
= (size_t) rsize
;
2393 return -EPROTONOSUPPORT
;
2396 *data
= o
->data
.payload
;
2403 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2410 assert_return(j
, -EINVAL
);
2411 assert_return(!journal_pid_changed(j
), -ECHILD
);
2412 assert_return(data
, -EINVAL
);
2413 assert_return(size
, -EINVAL
);
2415 f
= j
->current_file
;
2417 return -EADDRNOTAVAIL
;
2419 if (f
->current_offset
<= 0)
2420 return -EADDRNOTAVAIL
;
2422 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2426 n
= journal_file_entry_n_items(o
);
2427 if (j
->current_field
>= n
)
2430 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2431 le_hash
= o
->entry
.items
[j
->current_field
].hash
;
2432 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2436 if (le_hash
!= o
->data
.hash
)
2439 r
= return_data(j
, f
, o
, data
, size
);
2448 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2452 j
->current_field
= 0;
2455 static int reiterate_all_paths(sd_journal
*j
) {
2458 if (j
->no_new_files
)
2459 return add_current_paths(j
);
2461 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2462 return add_search_paths(j
);
2464 if (j
->toplevel_fd
>= 0)
2465 return add_root_directory(j
, NULL
, false);
2468 return add_root_directory(j
, j
->path
, true);
2470 return add_search_paths(j
);
2473 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2476 assert_return(j
, -EINVAL
);
2477 assert_return(!journal_pid_changed(j
), -ECHILD
);
2480 return -EMEDIUMTYPE
;
2482 if (j
->inotify_fd
>= 0)
2483 return j
->inotify_fd
;
2485 r
= allocate_inotify(j
);
2489 log_debug("Reiterating files to get inotify watches established.");
2491 /* Iterate through all dirs again, to add them to the inotify */
2492 r
= reiterate_all_paths(j
);
2496 return j
->inotify_fd
;
2499 _public_
int sd_journal_get_events(sd_journal
*j
) {
2502 assert_return(j
, -EINVAL
);
2503 assert_return(!journal_pid_changed(j
), -ECHILD
);
2505 fd
= sd_journal_get_fd(j
);
2512 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2515 assert_return(j
, -EINVAL
);
2516 assert_return(!journal_pid_changed(j
), -ECHILD
);
2517 assert_return(timeout_usec
, -EINVAL
);
2519 fd
= sd_journal_get_fd(j
);
2523 if (!j
->on_network
) {
2524 *timeout_usec
= (uint64_t) -1;
2528 /* If we are on the network we need to regularly check for
2529 * changes manually */
2531 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2535 static void process_q_overflow(sd_journal
*j
) {
2542 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2543 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2544 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2545 * are subject for unloading. */
2547 log_debug("Inotify queue overrun, reiterating everything.");
2550 (void) reiterate_all_paths(j
);
2552 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2554 if (f
->last_seen_generation
== j
->generation
)
2557 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2558 remove_file_real(j
, f
);
2561 HASHMAP_FOREACH(m
, j
->directories_by_path
, i
) {
2563 if (m
->last_seen_generation
== j
->generation
)
2566 if (m
->is_root
) /* Never GC root directories */
2569 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2570 remove_directory(j
, m
);
2573 log_debug("Reiteration complete.");
2576 static void process_inotify_event(sd_journal
*j
, const struct inotify_event
*e
) {
2582 if (e
->mask
& IN_Q_OVERFLOW
) {
2583 process_q_overflow(j
);
2587 /* Is this a subdirectory we watch? */
2588 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2590 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2591 (endswith(e
->name
, ".journal") ||
2592 endswith(e
->name
, ".journal~"))) {
2594 /* Event for a journal file */
2596 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2597 (void) add_file_by_name(j
, d
->path
, e
->name
);
2598 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2599 remove_file_by_name(j
, d
->path
, e
->name
);
2601 } else if (!d
->is_root
&& e
->len
== 0) {
2603 /* Event for a subdirectory */
2605 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2606 remove_directory(j
, d
);
2608 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2610 /* Event for root directory */
2612 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2613 (void) add_directory(j
, d
->path
, e
->name
);
2619 if (e
->mask
& IN_IGNORED
)
2622 log_debug("Unexpected inotify event.");
2625 static int determine_change(sd_journal
*j
) {
2630 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2631 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2633 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2636 _public_
int sd_journal_process(sd_journal
*j
) {
2637 bool got_something
= false;
2639 assert_return(j
, -EINVAL
);
2640 assert_return(!journal_pid_changed(j
), -ECHILD
);
2642 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
2645 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2646 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2649 union inotify_event_buffer buffer
;
2650 struct inotify_event
*e
;
2653 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2655 if (IN_SET(errno
, EAGAIN
, EINTR
))
2656 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2661 got_something
= true;
2663 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2664 process_inotify_event(j
, e
);
2668 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2672 assert_return(j
, -EINVAL
);
2673 assert_return(!journal_pid_changed(j
), -ECHILD
);
2675 if (j
->inotify_fd
< 0) {
2679 /* This is the first invocation, hence create the
2681 r
= sd_journal_get_fd(j
);
2685 /* Server might have done some vacuuming while we weren't watching.
2686 Get rid of the deleted files now so they don't stay around indefinitely. */
2687 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2688 r
= journal_file_fstat(f
);
2690 remove_file_real(j
, f
);
2692 log_debug_errno(r
,"Failed to fstat() journal file '%s' : %m", f
->path
);
2697 /* The journal might have changed since the context
2698 * object was created and we weren't watching before,
2699 * hence don't wait for anything, and return
2701 return determine_change(j
);
2704 r
= sd_journal_get_timeout(j
, &t
);
2708 if (t
!= (uint64_t) -1) {
2711 n
= now(CLOCK_MONOTONIC
);
2712 t
= t
> n
? t
- n
: 0;
2714 if (timeout_usec
== (uint64_t) -1 || timeout_usec
> t
)
2719 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2720 } while (r
== -EINTR
);
2725 return sd_journal_process(j
);
2728 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2732 uint64_t fmin
= 0, tmax
= 0;
2735 assert_return(j
, -EINVAL
);
2736 assert_return(!journal_pid_changed(j
), -ECHILD
);
2737 assert_return(from
|| to
, -EINVAL
);
2738 assert_return(from
!= to
, -EINVAL
);
2740 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2743 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2756 fmin
= MIN(fr
, fmin
);
2757 tmax
= MAX(t
, tmax
);
2766 return first
? 0 : 1;
2769 _public_
int sd_journal_get_cutoff_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t *from
, uint64_t *to
) {
2775 assert_return(j
, -EINVAL
);
2776 assert_return(!journal_pid_changed(j
), -ECHILD
);
2777 assert_return(from
|| to
, -EINVAL
);
2778 assert_return(from
!= to
, -EINVAL
);
2780 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2783 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &fr
, &t
);
2793 *from
= MIN(fr
, *from
);
2808 void journal_print_header(sd_journal
*j
) {
2811 bool newline
= false;
2815 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2821 journal_file_print_header(f
);
2825 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *bytes
) {
2830 assert_return(j
, -EINVAL
);
2831 assert_return(!journal_pid_changed(j
), -ECHILD
);
2832 assert_return(bytes
, -EINVAL
);
2834 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2837 if (fstat(f
->fd
, &st
) < 0)
2840 sum
+= (uint64_t) st
.st_blocks
* 512ULL;
2847 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2850 assert_return(j
, -EINVAL
);
2851 assert_return(!journal_pid_changed(j
), -ECHILD
);
2852 assert_return(!isempty(field
), -EINVAL
);
2853 assert_return(field_is_valid(field
), -EINVAL
);
2859 free(j
->unique_field
);
2860 j
->unique_field
= f
;
2861 j
->unique_file
= NULL
;
2862 j
->unique_offset
= 0;
2863 j
->unique_file_lost
= false;
2868 _public_
int sd_journal_enumerate_unique(sd_journal
*j
, const void **data
, size_t *l
) {
2871 assert_return(j
, -EINVAL
);
2872 assert_return(!journal_pid_changed(j
), -ECHILD
);
2873 assert_return(data
, -EINVAL
);
2874 assert_return(l
, -EINVAL
);
2875 assert_return(j
->unique_field
, -EINVAL
);
2877 k
= strlen(j
->unique_field
);
2879 if (!j
->unique_file
) {
2880 if (j
->unique_file_lost
)
2883 j
->unique_file
= ordered_hashmap_first(j
->files
);
2884 if (!j
->unique_file
)
2887 j
->unique_offset
= 0;
2899 /* Proceed to next data object in the field's linked list */
2900 if (j
->unique_offset
== 0) {
2901 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2905 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2907 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2911 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2914 /* We reached the end of the list? Then start again, with the next file */
2915 if (j
->unique_offset
== 0) {
2916 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2917 if (!j
->unique_file
)
2923 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2924 * instead, so that we can look at this data object at the same
2925 * time as one on another file */
2926 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2930 /* Let's do the type check by hand, since we used 0 context above. */
2931 if (o
->object
.type
!= OBJECT_DATA
)
2932 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2933 "%s:offset " OFSfmt
": object has type %d, expected %d",
2934 j
->unique_file
->path
,
2936 o
->object
.type
, OBJECT_DATA
);
2938 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2942 /* Check if we have at least the field name and "=". */
2944 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2945 "%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2946 j
->unique_file
->path
,
2947 j
->unique_offset
, ol
, k
+ 1);
2949 if (memcmp(odata
, j
->unique_field
, k
) || ((const char*) odata
)[k
] != '=')
2950 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2951 "%s:offset " OFSfmt
": object does not start with \"%s=\"",
2952 j
->unique_file
->path
,
2956 /* OK, now let's see if we already returned this data
2957 * object by checking if it exists in the earlier
2958 * traversed files. */
2960 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2961 if (of
== j
->unique_file
)
2964 /* Skip this file it didn't have any fields indexed */
2965 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2968 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
2980 r
= return_data(j
, j
->unique_file
, o
, data
, l
);
2988 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
2992 j
->unique_file
= NULL
;
2993 j
->unique_offset
= 0;
2994 j
->unique_file_lost
= false;
2997 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
3000 assert_return(j
, -EINVAL
);
3001 assert_return(!journal_pid_changed(j
), -ECHILD
);
3002 assert_return(field
, -EINVAL
);
3004 if (!j
->fields_file
) {
3005 if (j
->fields_file_lost
)
3008 j
->fields_file
= ordered_hashmap_first(j
->files
);
3009 if (!j
->fields_file
)
3012 j
->fields_hash_table_index
= 0;
3013 j
->fields_offset
= 0;
3017 JournalFile
*f
, *of
;
3026 if (j
->fields_offset
== 0) {
3029 /* We are not yet positioned at any field. Let's pick the first one */
3030 r
= journal_file_map_field_hash_table(f
);
3034 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
3036 if (j
->fields_hash_table_index
>= m
) {
3037 /* Reached the end of the hash table, go to the next file. */
3042 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
3044 if (j
->fields_offset
!= 0)
3047 /* Empty hash table bucket, go to next one */
3048 j
->fields_hash_table_index
++;
3052 /* Proceed with next file */
3053 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
3054 if (!j
->fields_file
) {
3059 j
->fields_offset
= 0;
3060 j
->fields_hash_table_index
= 0;
3065 /* We are already positioned at a field. If so, let's figure out the next field from it */
3067 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
3071 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
3072 if (j
->fields_offset
== 0) {
3073 /* Reached the end of the hash table chain */
3074 j
->fields_hash_table_index
++;
3079 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
3080 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
3084 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
3085 if (o
->object
.type
!= OBJECT_FIELD
)
3086 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3087 "%s:offset " OFSfmt
": object has type %i, expected %i",
3088 f
->path
, j
->fields_offset
,
3089 o
->object
.type
, OBJECT_FIELD
);
3091 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
3093 /* Let's see if we already returned this field name before. */
3095 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
3099 /* Skip this file it didn't have any fields indexed */
3100 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3103 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
, le64toh(o
->field
.hash
), NULL
, NULL
);
3115 /* Check if this is really a valid string containing no NUL byte */
3116 if (memchr(o
->field
.payload
, 0, sz
))
3119 if (sz
> j
->data_threshold
)
3120 sz
= j
->data_threshold
;
3122 if (!GREEDY_REALLOC(j
->fields_buffer
, j
->fields_buffer_allocated
, sz
+ 1))
3125 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3126 j
->fields_buffer
[sz
] = 0;
3128 if (!field_is_valid(j
->fields_buffer
))
3131 *field
= j
->fields_buffer
;
3136 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3140 j
->fields_file
= NULL
;
3141 j
->fields_hash_table_index
= 0;
3142 j
->fields_offset
= 0;
3143 j
->fields_file_lost
= false;
3146 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3147 assert_return(j
, -EINVAL
);
3148 assert_return(!journal_pid_changed(j
), -ECHILD
);
3150 return !j
->on_network
;
3153 static char *lookup_field(const char *field
, void *userdata
) {
3154 sd_journal
*j
= userdata
;
3162 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3164 size
> REPLACE_VAR_MAX
)
3165 return strdup(field
);
3167 d
= strlen(field
) + 1;
3169 return strndup((const char*) data
+ d
, size
- d
);
3172 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3176 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3180 assert_return(j
, -EINVAL
);
3181 assert_return(!journal_pid_changed(j
), -ECHILD
);
3182 assert_return(ret
, -EINVAL
);
3184 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3188 cid
= strndup((const char*) data
+ 11, size
- 11);
3192 r
= sd_id128_from_string(cid
, &id
);
3196 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
3200 t
= replace_var(text
, lookup_field
, j
);
3208 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3209 assert_return(ret
, -EINVAL
);
3211 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3214 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3215 assert_return(j
, -EINVAL
);
3216 assert_return(!journal_pid_changed(j
), -ECHILD
);
3218 j
->data_threshold
= sz
;
3222 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3223 assert_return(j
, -EINVAL
);
3224 assert_return(!journal_pid_changed(j
), -ECHILD
);
3225 assert_return(sz
, -EINVAL
);
3227 *sz
= j
->data_threshold
;
3231 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3232 assert_return(j
, -EINVAL
);
3234 return j
->has_runtime_files
;
3237 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3238 assert_return(j
, -EINVAL
);
3240 return j
->has_persistent_files
;