1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <linux/magic.h>
9 #include <sys/inotify.h>
13 #include "sd-journal.h"
15 #include "alloc-util.h"
18 #include "dirent-util.h"
23 #include "format-util.h"
26 #include "hostname-util.h"
27 #include "id128-util.h"
29 #include "journal-def.h"
30 #include "journal-file.h"
31 #include "journal-internal.h"
34 #include "nulstr-util.h"
35 #include "path-util.h"
36 #include "process-util.h"
37 #include "replace-var.h"
38 #include "stat-util.h"
39 #include "stdio-util.h"
40 #include "string-util.h"
42 #include "syslog-util.h"
44 #define JOURNAL_FILES_MAX 7168
46 #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
48 /* The maximum size of variable values we'll expand in catalog entries. We bind this to PATH_MAX for now, as
49 * we want to be able to show all officially valid paths at least */
50 #define REPLACE_VAR_MAX PATH_MAX
52 #define DEFAULT_DATA_THRESHOLD (64*1024)
54 static void remove_file_real(sd_journal
*j
, JournalFile
*f
);
56 static bool journal_pid_changed(sd_journal
*j
) {
59 /* We don't support people creating a journal object and
60 * keeping it around over a fork(). Let's complain. */
62 return j
->original_pid
!= getpid_cached();
65 static int journal_put_error(sd_journal
*j
, int r
, const char *path
) {
69 /* Memorize an error we encountered, and store which
70 * file/directory it was generated from. Note that we store
71 * only *one* path per error code, as the error code is the
72 * key into the hashmap, and the path is the value. This means
73 * we keep track only of all error kinds, but not of all error
74 * locations. This has the benefit that the hashmap cannot
77 * We return an error here only if we didn't manage to
78 * memorize the real error. */
83 k
= hashmap_ensure_allocated(&j
->errors
, NULL
);
94 k
= hashmap_put(j
->errors
, INT_TO_PTR(r
), copy
);
107 static void detach_location(sd_journal
*j
) {
113 j
->current_file
= NULL
;
114 j
->current_field
= 0;
116 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
)
117 journal_file_reset_location(f
);
120 static void init_location(Location
*l
, LocationType type
, JournalFile
*f
, Object
*o
) {
122 assert(IN_SET(type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
127 .seqnum
= le64toh(o
->entry
.seqnum
),
128 .seqnum_id
= f
->header
->seqnum_id
,
129 .realtime
= le64toh(o
->entry
.realtime
),
130 .monotonic
= le64toh(o
->entry
.monotonic
),
131 .boot_id
= o
->entry
.boot_id
,
132 .xor_hash
= le64toh(o
->entry
.xor_hash
),
134 .realtime_set
= true,
135 .monotonic_set
= true,
136 .xor_hash_set
= true,
140 static void set_location(sd_journal
*j
, JournalFile
*f
, Object
*o
) {
145 init_location(&j
->current_location
, LOCATION_DISCRETE
, f
, o
);
148 j
->current_field
= 0;
150 /* Let f know its candidate entry was picked. */
151 assert(f
->location_type
== LOCATION_SEEK
);
152 f
->location_type
= LOCATION_DISCRETE
;
155 static int match_is_valid(const void *data
, size_t size
) {
163 if (((char*) data
)[0] == '_' && ((char*) data
)[1] == '_')
167 for (p
= b
; p
< b
+ size
; p
++) {
175 if (*p
>= 'A' && *p
<= 'Z')
178 if (*p
>= '0' && *p
<= '9')
187 static bool same_field(const void *_a
, size_t s
, const void *_b
, size_t t
) {
188 const uint8_t *a
= _a
, *b
= _b
;
191 for (j
= 0; j
< s
&& j
< t
; j
++) {
200 assert_not_reached("\"=\" not found");
203 static Match
*match_new(Match
*p
, MatchType t
) {
216 LIST_PREPEND(matches
, p
->matches
, m
);
221 static void match_free(Match
*m
) {
225 match_free(m
->matches
);
228 LIST_REMOVE(matches
, m
->parent
->matches
, m
);
234 static void match_free_if_empty(Match
*m
) {
235 if (!m
|| m
->matches
)
241 _public_
int sd_journal_add_match(sd_journal
*j
, const void *data
, size_t size
) {
242 Match
*l3
, *l4
, *add_here
= NULL
, *m
;
245 assert_return(j
, -EINVAL
);
246 assert_return(!journal_pid_changed(j
), -ECHILD
);
247 assert_return(data
, -EINVAL
);
252 assert_return(match_is_valid(data
, size
), -EINVAL
);
258 * level 4: concrete matches */
261 j
->level0
= match_new(NULL
, MATCH_AND_TERM
);
267 j
->level1
= match_new(j
->level0
, MATCH_OR_TERM
);
273 j
->level2
= match_new(j
->level1
, MATCH_AND_TERM
);
278 assert(j
->level0
->type
== MATCH_AND_TERM
);
279 assert(j
->level1
->type
== MATCH_OR_TERM
);
280 assert(j
->level2
->type
== MATCH_AND_TERM
);
282 /* Old-style Jenkins (unkeyed) hashing only here. We do not cover new-style siphash (keyed) hashing
283 * here, since it's different for each file, and thus can't be pre-calculated in the Match object. */
284 hash
= jenkins_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
->hash
== 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
);
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(f
->location_type
== LOCATION_SEEK
);
444 assert(IN_SET(l
->type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
446 if (l
->monotonic_set
&&
447 sd_id128_equal(f
->current_boot_id
, l
->boot_id
) &&
449 f
->current_realtime
== l
->realtime
&&
451 f
->current_xor_hash
== l
->xor_hash
&&
456 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
)) {
458 r
= CMP(f
->current_seqnum
, l
->seqnum
);
463 if (l
->monotonic_set
&&
464 sd_id128_equal(f
->current_boot_id
, l
->boot_id
)) {
466 r
= CMP(f
->current_monotonic
, l
->monotonic
);
471 if (l
->realtime_set
) {
473 r
= CMP(f
->current_realtime
, l
->realtime
);
478 if (l
->xor_hash_set
) {
480 r
= CMP(f
->current_xor_hash
, l
->xor_hash
);
488 static int next_for_match(
492 uint64_t after_offset
,
493 direction_t direction
,
505 if (m
->type
== MATCH_DISCRETE
) {
508 /* If the keyed hash logic is used, we need to calculate the hash fresh per file. Otherwise
509 * we can use what we pre-calculated. */
510 if (JOURNAL_HEADER_KEYED_HASH(f
->header
))
511 hash
= journal_file_hash_data(f
, m
->data
, m
->size
);
515 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, hash
, NULL
, &dp
);
519 return journal_file_move_to_entry_by_offset_for_data(f
, dp
, after_offset
, direction
, ret
, offset
);
521 } else if (m
->type
== MATCH_OR_TERM
) {
524 /* Find the earliest match beyond after_offset */
526 LIST_FOREACH(matches
, i
, m
->matches
) {
529 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
533 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
541 } else if (m
->type
== MATCH_AND_TERM
) {
542 Match
*i
, *last_moved
;
544 /* Always jump to the next matching entry and repeat
545 * this until we find an offset that matches for all
551 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
555 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
556 last_moved
= m
->matches
;
558 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
561 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
565 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
566 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
575 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
587 static int find_location_for_match(
591 direction_t direction
,
601 if (m
->type
== MATCH_DISCRETE
) {
604 if (JOURNAL_HEADER_KEYED_HASH(f
->header
))
605 hash
= journal_file_hash_data(f
, m
->data
, m
->size
);
609 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, hash
, NULL
, &dp
);
613 /* FIXME: missing: find by monotonic */
615 if (j
->current_location
.type
== LOCATION_HEAD
)
616 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_DOWN
, ret
, offset
);
617 if (j
->current_location
.type
== LOCATION_TAIL
)
618 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_UP
, ret
, offset
);
619 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
620 return journal_file_move_to_entry_by_seqnum_for_data(f
, dp
, j
->current_location
.seqnum
, direction
, ret
, offset
);
621 if (j
->current_location
.monotonic_set
) {
622 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, dp
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
626 if (j
->current_location
.realtime_set
)
627 return journal_file_move_to_entry_by_realtime_for_data(f
, dp
, j
->current_location
.realtime
, direction
, ret
, offset
);
629 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, direction
, ret
, offset
);
631 } else if (m
->type
== MATCH_OR_TERM
) {
636 /* Find the earliest match */
638 LIST_FOREACH(matches
, i
, m
->matches
) {
641 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
645 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
653 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
668 assert(m
->type
== MATCH_AND_TERM
);
670 /* First jump to the last match, and then find the
671 * next one where all matches match */
676 LIST_FOREACH(matches
, i
, m
->matches
) {
679 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
683 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
687 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
691 static int find_location_with_matches(
694 direction_t direction
,
706 /* No matches is simple */
708 if (j
->current_location
.type
== LOCATION_HEAD
)
709 return journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
);
710 if (j
->current_location
.type
== LOCATION_TAIL
)
711 return journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
);
712 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
713 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
714 if (j
->current_location
.monotonic_set
) {
715 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
719 if (j
->current_location
.realtime_set
)
720 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
722 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
724 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
727 static int next_with_matches(
730 direction_t direction
,
739 /* No matches is easy. We simple advance the file
742 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
744 /* If we have a match then we look for the next matching entry
745 * with an offset at least one step larger */
746 return next_for_match(j
, j
->level0
, f
,
747 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
748 : f
->current_offset
- 1,
749 direction
, ret
, offset
);
752 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
754 uint64_t cp
, n_entries
;
760 n_entries
= le64toh(f
->header
->n_entries
);
762 /* If we hit EOF before, we don't need to look into this file again
763 * unless direction changed or new entries appeared. */
764 if (f
->last_direction
== direction
&& f
->location_type
== LOCATION_TAIL
&&
765 n_entries
== f
->last_n_entries
)
768 f
->last_n_entries
= n_entries
;
770 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
771 /* LOCATION_SEEK here means we did the work in a previous
772 * iteration and the current location already points to a
773 * candidate entry. */
774 if (f
->location_type
!= LOCATION_SEEK
) {
775 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
779 journal_file_save_location(f
, c
, cp
);
782 f
->last_direction
= direction
;
784 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
788 journal_file_save_location(f
, c
, cp
);
791 /* OK, we found the spot, now let's advance until an entry
792 * that is actually different from what we were previously
793 * looking at. This is necessary to handle entries which exist
794 * in two (or more) journal files, and which shall all be
795 * suppressed but one. */
800 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
803 k
= compare_with_location(f
, &j
->current_location
, j
->current_file
);
805 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
812 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
816 journal_file_save_location(f
, c
, cp
);
820 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
821 JournalFile
*new_file
= NULL
;
827 assert_return(j
, -EINVAL
);
828 assert_return(!journal_pid_changed(j
), -ECHILD
);
830 r
= iterated_cache_get(j
->files_cache
, NULL
, &files
, &n_files
);
834 for (i
= 0; i
< n_files
; i
++) {
835 JournalFile
*f
= (JournalFile
*)files
[i
];
838 r
= next_beyond_location(j
, f
, direction
);
840 log_debug_errno(r
, "Can't iterate through %s, ignoring: %m", f
->path
);
841 remove_file_real(j
, f
);
844 f
->location_type
= LOCATION_TAIL
;
853 k
= journal_file_compare_locations(f
, new_file
);
855 found
= direction
== DIRECTION_DOWN
? k
< 0 : k
> 0;
865 r
= journal_file_move_to_object(new_file
, OBJECT_ENTRY
, new_file
->current_offset
, &o
);
869 set_location(j
, new_file
, o
);
874 _public_
int sd_journal_next(sd_journal
*j
) {
875 return real_journal_next(j
, DIRECTION_DOWN
);
878 _public_
int sd_journal_previous(sd_journal
*j
) {
879 return real_journal_next(j
, DIRECTION_UP
);
882 static int real_journal_next_skip(sd_journal
*j
, direction_t direction
, uint64_t skip
) {
885 assert_return(j
, -EINVAL
);
886 assert_return(!journal_pid_changed(j
), -ECHILD
);
889 /* If this is not a discrete skip, then at least
890 * resolve the current location */
891 if (j
->current_location
.type
!= LOCATION_DISCRETE
) {
892 r
= real_journal_next(j
, direction
);
901 r
= real_journal_next(j
, direction
);
915 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
916 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
919 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
920 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
923 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
926 char bid
[SD_ID128_STRING_MAX
], sid
[SD_ID128_STRING_MAX
];
928 assert_return(j
, -EINVAL
);
929 assert_return(!journal_pid_changed(j
), -ECHILD
);
930 assert_return(cursor
, -EINVAL
);
932 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
933 return -EADDRNOTAVAIL
;
935 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
939 sd_id128_to_string(j
->current_file
->header
->seqnum_id
, sid
);
940 sd_id128_to_string(o
->entry
.boot_id
, bid
);
943 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
944 sid
, le64toh(o
->entry
.seqnum
),
945 bid
, le64toh(o
->entry
.monotonic
),
946 le64toh(o
->entry
.realtime
),
947 le64toh(o
->entry
.xor_hash
)) < 0)
953 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
954 const char *word
, *state
;
956 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
958 seqnum_id_set
= false,
961 monotonic_set
= false,
962 realtime_set
= false,
963 xor_hash_set
= false;
964 sd_id128_t seqnum_id
, boot_id
;
966 assert_return(j
, -EINVAL
);
967 assert_return(!journal_pid_changed(j
), -ECHILD
);
968 assert_return(!isempty(cursor
), -EINVAL
);
970 FOREACH_WORD_SEPARATOR(word
, l
, cursor
, ";", state
) {
974 if (l
< 2 || word
[1] != '=')
977 item
= strndup(word
, l
);
984 seqnum_id_set
= true;
985 k
= sd_id128_from_string(item
+2, &seqnum_id
);
990 if (sscanf(item
+2, "%llx", &seqnum
) != 1)
996 k
= sd_id128_from_string(item
+2, &boot_id
);
1000 monotonic_set
= true;
1001 if (sscanf(item
+2, "%llx", &monotonic
) != 1)
1006 realtime_set
= true;
1007 if (sscanf(item
+2, "%llx", &realtime
) != 1)
1012 xor_hash_set
= true;
1013 if (sscanf(item
+2, "%llx", &xor_hash
) != 1)
1024 if ((!seqnum_set
|| !seqnum_id_set
) &&
1025 (!monotonic_set
|| !boot_id_set
) &&
1030 j
->current_location
= (Location
) {
1031 .type
= LOCATION_SEEK
,
1035 j
->current_location
.realtime
= (uint64_t) realtime
;
1036 j
->current_location
.realtime_set
= true;
1039 if (seqnum_set
&& seqnum_id_set
) {
1040 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1041 j
->current_location
.seqnum_id
= seqnum_id
;
1042 j
->current_location
.seqnum_set
= true;
1045 if (monotonic_set
&& boot_id_set
) {
1046 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1047 j
->current_location
.boot_id
= boot_id
;
1048 j
->current_location
.monotonic_set
= true;
1052 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1053 j
->current_location
.xor_hash_set
= true;
1059 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1063 assert_return(j
, -EINVAL
);
1064 assert_return(!journal_pid_changed(j
), -ECHILD
);
1065 assert_return(!isempty(cursor
), -EINVAL
);
1067 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1068 return -EADDRNOTAVAIL
;
1070 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1075 _cleanup_free_
char *item
= NULL
;
1076 unsigned long long ll
;
1080 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1087 if (strlen(item
) < 2 || item
[1] != '=')
1093 k
= sd_id128_from_string(item
+2, &id
);
1096 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1101 if (sscanf(item
+2, "%llx", &ll
) != 1)
1103 if (ll
!= le64toh(o
->entry
.seqnum
))
1108 k
= sd_id128_from_string(item
+2, &id
);
1111 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1116 if (sscanf(item
+2, "%llx", &ll
) != 1)
1118 if (ll
!= le64toh(o
->entry
.monotonic
))
1123 if (sscanf(item
+2, "%llx", &ll
) != 1)
1125 if (ll
!= le64toh(o
->entry
.realtime
))
1130 if (sscanf(item
+2, "%llx", &ll
) != 1)
1132 if (ll
!= le64toh(o
->entry
.xor_hash
))
1141 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, 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
,
1151 .monotonic_set
= true,
1157 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1158 assert_return(j
, -EINVAL
);
1159 assert_return(!journal_pid_changed(j
), -ECHILD
);
1163 j
->current_location
= (Location
) {
1164 .type
= LOCATION_SEEK
,
1166 .realtime_set
= true,
1172 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1173 assert_return(j
, -EINVAL
);
1174 assert_return(!journal_pid_changed(j
), -ECHILD
);
1178 j
->current_location
= (Location
) {
1179 .type
= LOCATION_HEAD
,
1185 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1186 assert_return(j
, -EINVAL
);
1187 assert_return(!journal_pid_changed(j
), -ECHILD
);
1191 j
->current_location
= (Location
) {
1192 .type
= LOCATION_TAIL
,
1198 static void check_network(sd_journal
*j
, int fd
) {
1204 j
->on_network
= fd_is_network_fs(fd
);
1207 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1208 const char *full
, *tilded
, *atted
;
1210 full
= strjoina(prefix
, ".journal");
1211 tilded
= strjoina(full
, "~");
1212 atted
= strjoina(prefix
, "@");
1214 return STR_IN_SET(filename
, full
, tilded
) ||
1215 startswith(filename
, atted
);
1218 static bool file_type_wanted(int flags
, const char *filename
) {
1221 if (!endswith(filename
, ".journal") && !endswith(filename
, ".journal~"))
1224 /* no flags set → every type is OK */
1225 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1228 if (flags
& SD_JOURNAL_SYSTEM
&& file_has_type_prefix("system", filename
))
1231 if (flags
& SD_JOURNAL_CURRENT_USER
) {
1232 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1234 xsprintf(prefix
, "user-"UID_FMT
, getuid());
1236 if (file_has_type_prefix(prefix
, filename
))
1243 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1248 if (j
->toplevel_fd
>= 0)
1251 return path_startswith(path
, prefix
);
1254 static void track_file_disposition(sd_journal
*j
, JournalFile
*f
) {
1258 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1259 j
->has_runtime_files
= true;
1260 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1261 j
->has_persistent_files
= true;
1264 static const char *skip_slash(const char *p
) {
1275 static int add_any_file(
1280 bool close_fd
= false;
1286 assert(fd
>= 0 || path
);
1289 if (j
->toplevel_fd
>= 0)
1290 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1291 * openat() ignores the first argument. */
1293 fd
= openat(j
->toplevel_fd
, skip_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1295 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1297 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1303 r
= fd_nonblock(fd
, false);
1305 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1310 if (fstat(fd
, &st
) < 0) {
1311 r
= log_debug_errno(errno
, "Failed to fstat file '%s': %m", path
);
1315 r
= stat_verify_regular(&st
);
1317 log_debug_errno(r
, "Refusing to open '%s', as it is not a regular file.", path
);
1321 f
= ordered_hashmap_get(j
->files
, path
);
1323 if (f
->last_stat
.st_dev
== st
.st_dev
&&
1324 f
->last_stat
.st_ino
== st
.st_ino
) {
1326 /* We already track this file, under the same path and with the same device/inode numbers, it's
1327 * hence really the same. Mark this file as seen in this generation. This is used to GC old
1328 * files in process_q_overflow() to detect journal files that are still there and discern them
1329 * from those which are gone. */
1331 f
->last_seen_generation
= j
->generation
;
1336 /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
1337 * replaced (probably due to rotation?), let's drop it hence from our list. */
1338 remove_file_real(j
, f
);
1342 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1343 log_debug("Too many open journal files, not adding %s.", path
);
1348 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, false, 0, false, NULL
, j
->mmap
, NULL
, NULL
, &f
);
1350 log_debug_errno(r
, "Failed to open journal file %s: %m", path
);
1354 /* journal_file_dump(f); */
1356 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1358 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 */
1359 (void) journal_file_close(f
);
1363 close_fd
= false; /* the fd is now owned by the JournalFile object */
1365 f
->last_seen_generation
= j
->generation
;
1367 track_file_disposition(j
, f
);
1368 check_network(j
, f
->fd
);
1370 j
->current_invalidate_counter
++;
1372 log_debug("File %s added.", f
->path
);
1381 k
= journal_put_error(j
, r
, path
);
1389 static int add_file_by_name(
1392 const char *filename
) {
1400 if (j
->no_new_files
)
1403 if (!file_type_wanted(j
->flags
, filename
))
1406 path
= prefix_roota(prefix
, filename
);
1407 return add_any_file(j
, -1, path
);
1410 static void remove_file_by_name(
1413 const char *filename
) {
1422 path
= prefix_roota(prefix
, filename
);
1423 f
= ordered_hashmap_get(j
->files
, path
);
1427 remove_file_real(j
, f
);
1430 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1434 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1436 log_debug("File %s removed.", f
->path
);
1438 if (j
->current_file
== f
) {
1439 j
->current_file
= NULL
;
1440 j
->current_field
= 0;
1443 if (j
->unique_file
== f
) {
1444 /* Jump to the next unique_file or NULL if that one was last */
1445 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1446 j
->unique_offset
= 0;
1447 if (!j
->unique_file
)
1448 j
->unique_file_lost
= true;
1451 if (j
->fields_file
== f
) {
1452 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1453 j
->fields_offset
= 0;
1454 if (!j
->fields_file
)
1455 j
->fields_file_lost
= true;
1458 (void) journal_file_close(f
);
1460 j
->current_invalidate_counter
++;
1463 static int dirname_is_machine_id(const char *fn
) {
1464 sd_id128_t id
, machine
;
1468 /* Returns true if the specified directory name matches the local machine ID */
1470 r
= sd_id128_get_machine(&machine
);
1474 e
= strchr(fn
, '.');
1478 /* Looks like it has a namespace suffix. Verify that. */
1479 if (!log_namespace_name_valid(e
+ 1))
1482 k
= strndupa(fn
, e
- fn
);
1483 r
= sd_id128_from_string(k
, &id
);
1485 r
= sd_id128_from_string(fn
, &id
);
1489 return sd_id128_equal(id
, machine
);
1492 static int dirname_has_namespace(const char *fn
, const char *namespace) {
1495 /* Returns true if the specified directory name matches the specified namespace */
1497 e
= strchr(fn
, '.');
1504 if (!streq(e
+ 1, namespace))
1507 k
= strndupa(fn
, e
- fn
);
1508 return id128_is_valid(k
);
1514 return id128_is_valid(fn
);
1517 static bool dirent_is_journal_file(const struct dirent
*de
) {
1520 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1522 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1525 return endswith(de
->d_name
, ".journal") ||
1526 endswith(de
->d_name
, ".journal~");
1529 static bool dirent_is_journal_subdir(const struct dirent
*de
) {
1533 /* returns true if the specified directory entry looks like a directory that might contain journal
1534 * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a
1537 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1540 e
= strchr(de
->d_name
, '.');
1542 return id128_is_valid(de
->d_name
); /* No namespace */
1544 n
= strndupa(de
->d_name
, e
- de
->d_name
);
1545 if (!id128_is_valid(n
))
1548 return log_namespace_name_valid(e
+ 1);
1551 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1558 if (j
->toplevel_fd
< 0)
1561 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1562 * relative, by dropping the initial slash */
1563 d
= xopendirat(j
->toplevel_fd
, skip_slash(path
), 0);
1571 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1573 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1580 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1582 if (dirent_is_journal_file(de
))
1583 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1585 if (m
->is_root
&& dirent_is_journal_subdir(de
))
1586 (void) add_directory(j
, m
->path
, de
->d_name
);
1592 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1595 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1602 /* Watch this directory if that's enabled and if it not being watched yet. */
1604 if (m
->wd
> 0) /* Already have a watch? */
1606 if (j
->inotify_fd
< 0) /* Not watching at all? */
1609 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1611 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1615 r
= hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
);
1617 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1619 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1620 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1625 static int add_directory(
1628 const char *dirname
) {
1630 _cleanup_free_
char *path
= NULL
;
1631 _cleanup_closedir_
DIR *d
= NULL
;
1638 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1639 * and reenumerates directory contents */
1641 path
= path_join(prefix
, dirname
);
1647 log_debug("Considering directory '%s'.", path
);
1649 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1650 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1651 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1655 (!(FLAGS_SET(j
->flags
, SD_JOURNAL_ALL_NAMESPACES
) ||
1656 dirname_has_namespace(dirname
, j
->namespace) > 0 ||
1657 (FLAGS_SET(j
->flags
, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE
) && dirname_has_namespace(dirname
, NULL
) > 0))))
1660 r
= directory_open(j
, path
, &d
);
1662 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
1666 m
= hashmap_get(j
->directories_by_path
, path
);
1668 m
= new(Directory
, 1);
1679 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1685 path
= NULL
; /* avoid freeing in cleanup */
1686 j
->current_invalidate_counter
++;
1688 log_debug("Directory %s added.", m
->path
);
1690 } else if (m
->is_root
)
1691 return 0; /* Don't 'downgrade' from root directory */
1693 m
->last_seen_generation
= j
->generation
;
1695 directory_watch(j
, m
, dirfd(d
),
1696 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1697 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
1700 if (!j
->no_new_files
)
1701 directory_enumerate(j
, m
, d
);
1703 check_network(j
, dirfd(d
));
1708 k
= journal_put_error(j
, r
, path
?: prefix
);
1715 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
1717 _cleanup_closedir_
DIR *d
= NULL
;
1723 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1724 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1725 * populate the set, as well as to update it later. */
1728 /* If there's a path specified, use it. */
1730 log_debug("Considering root directory '%s'.", p
);
1732 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
1733 !path_has_prefix(j
, p
, "/run"))
1737 p
= strjoina(j
->prefix
, p
);
1739 r
= directory_open(j
, p
, &d
);
1740 if (r
== -ENOENT
&& missing_ok
)
1743 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
1747 _cleanup_close_
int dfd
= -1;
1749 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1750 * opendir() will take possession of the fd, and close it, which we don't want. */
1752 p
= "."; /* store this as "." in the directories hashmap */
1754 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
1760 d
= take_fdopendir(&dfd
);
1769 m
= hashmap_get(j
->directories_by_path
, p
);
1771 m
= new0(Directory
, 1);
1779 m
->path
= strdup(p
);
1786 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1793 j
->current_invalidate_counter
++;
1795 log_debug("Root directory %s added.", m
->path
);
1797 } else if (!m
->is_root
)
1800 directory_watch(j
, m
, dirfd(d
),
1801 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1804 if (!j
->no_new_files
)
1805 directory_enumerate(j
, m
, d
);
1807 check_network(j
, dirfd(d
));
1812 k
= journal_put_error(j
, r
, p
);
1819 static void remove_directory(sd_journal
*j
, Directory
*d
) {
1823 hashmap_remove(j
->directories_by_wd
, INT_TO_PTR(d
->wd
));
1825 if (j
->inotify_fd
>= 0)
1826 (void) inotify_rm_watch(j
->inotify_fd
, d
->wd
);
1829 hashmap_remove(j
->directories_by_path
, d
->path
);
1832 log_debug("Root directory %s removed.", d
->path
);
1834 log_debug("Directory %s removed.", d
->path
);
1840 static int add_search_paths(sd_journal
*j
) {
1842 static const char search_paths
[] =
1843 "/run/log/journal\0"
1844 "/var/log/journal\0";
1849 /* We ignore most errors here, since the idea is to only open
1850 * what's actually accessible, and ignore the rest. */
1852 NULSTR_FOREACH(p
, search_paths
)
1853 (void) add_root_directory(j
, p
, true);
1855 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
1856 (void) add_root_directory(j
, "/var/log/journal/remote", true);
1861 static int add_current_paths(sd_journal
*j
) {
1866 assert(j
->no_new_files
);
1868 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1869 * treat them as fatal. */
1871 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
1872 _cleanup_free_
char *dir
;
1875 dir
= dirname_malloc(f
->path
);
1879 r
= add_directory(j
, dir
, NULL
);
1887 static int allocate_inotify(sd_journal
*j
) {
1890 if (j
->inotify_fd
< 0) {
1891 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1892 if (j
->inotify_fd
< 0)
1896 return hashmap_ensure_allocated(&j
->directories_by_wd
, NULL
);
1899 static sd_journal
*journal_new(int flags
, const char *path
, const char *namespace) {
1900 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1902 j
= new0(sd_journal
, 1);
1906 j
->original_pid
= getpid_cached();
1907 j
->toplevel_fd
= -1;
1910 j
->data_threshold
= DEFAULT_DATA_THRESHOLD
;
1919 if (flags
& SD_JOURNAL_OS_ROOT
)
1926 j
->namespace = strdup(namespace);
1931 j
->files
= ordered_hashmap_new(&path_hash_ops
);
1935 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
1936 j
->directories_by_path
= hashmap_new(&path_hash_ops
);
1937 j
->mmap
= mmap_cache_new();
1938 if (!j
->files_cache
|| !j
->directories_by_path
|| !j
->mmap
)
1944 #define OPEN_ALLOWED_FLAGS \
1945 (SD_JOURNAL_LOCAL_ONLY | \
1946 SD_JOURNAL_RUNTIME_ONLY | \
1947 SD_JOURNAL_SYSTEM | \
1948 SD_JOURNAL_CURRENT_USER | \
1949 SD_JOURNAL_ALL_NAMESPACES | \
1950 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
1952 _public_
int sd_journal_open_namespace(sd_journal
**ret
, const char *namespace, int flags
) {
1953 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1956 assert_return(ret
, -EINVAL
);
1957 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
1959 j
= journal_new(flags
, NULL
, namespace);
1963 r
= add_search_paths(j
);
1971 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1972 return sd_journal_open_namespace(ret
, NULL
, flags
);
1975 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1976 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1978 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1979 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1980 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1984 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1985 * combination with sd_journal_open_directory_fd(). */
1987 assert_return(machine
, -EINVAL
);
1988 assert_return(ret
, -EINVAL
);
1989 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
1990 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1992 p
= strjoina("/run/systemd/machines/", machine
);
1993 r
= parse_env_file(NULL
, p
,
2003 if (!streq_ptr(class, "container"))
2006 j
= journal_new(flags
, root
, NULL
);
2010 r
= add_search_paths(j
);
2018 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
2019 (SD_JOURNAL_OS_ROOT | \
2020 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2022 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
2023 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2026 assert_return(ret
, -EINVAL
);
2027 assert_return(path
, -EINVAL
);
2028 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
2030 j
= journal_new(flags
, path
, NULL
);
2034 if (flags
& SD_JOURNAL_OS_ROOT
)
2035 r
= add_search_paths(j
);
2037 r
= add_root_directory(j
, path
, false);
2045 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
2046 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2050 assert_return(ret
, -EINVAL
);
2051 assert_return(flags
== 0, -EINVAL
);
2053 j
= journal_new(flags
, NULL
, NULL
);
2057 STRV_FOREACH(path
, paths
) {
2058 r
= add_any_file(j
, -1, *path
);
2063 j
->no_new_files
= true;
2069 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
2070 (SD_JOURNAL_OS_ROOT | \
2071 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2073 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
2074 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2078 assert_return(ret
, -EINVAL
);
2079 assert_return(fd
>= 0, -EBADF
);
2080 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2082 if (fstat(fd
, &st
) < 0)
2085 if (!S_ISDIR(st
.st_mode
))
2088 j
= journal_new(flags
, NULL
, NULL
);
2092 j
->toplevel_fd
= fd
;
2094 if (flags
& SD_JOURNAL_OS_ROOT
)
2095 r
= add_search_paths(j
);
2097 r
= add_root_directory(j
, NULL
, false);
2105 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2108 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2112 assert_return(ret
, -EINVAL
);
2113 assert_return(n_fds
> 0, -EBADF
);
2114 assert_return(flags
== 0, -EINVAL
);
2116 j
= journal_new(flags
, NULL
, NULL
);
2120 for (i
= 0; i
< n_fds
; i
++) {
2128 if (fstat(fds
[i
], &st
) < 0) {
2133 r
= stat_verify_regular(&st
);
2137 r
= add_any_file(j
, fds
[i
], NULL
);
2142 j
->no_new_files
= true;
2143 j
->no_inotify
= true;
2149 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2151 ORDERED_HASHMAP_FOREACH(f
, j
->files
, iterator
)
2152 f
->close_fd
= false;
2157 _public_
void sd_journal_close(sd_journal
*j
) {
2163 sd_journal_flush_matches(j
);
2165 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2166 iterated_cache_free(j
->files_cache
);
2168 while ((d
= hashmap_first(j
->directories_by_path
)))
2169 remove_directory(j
, d
);
2171 while ((d
= hashmap_first(j
->directories_by_wd
)))
2172 remove_directory(j
, d
);
2174 hashmap_free(j
->directories_by_path
);
2175 hashmap_free(j
->directories_by_wd
);
2177 safe_close(j
->inotify_fd
);
2180 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j
->mmap
), mmap_cache_get_missed(j
->mmap
));
2181 mmap_cache_unref(j
->mmap
);
2184 hashmap_free_free(j
->errors
);
2189 free(j
->unique_field
);
2190 free(j
->fields_buffer
);
2194 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2199 assert_return(j
, -EINVAL
);
2200 assert_return(!journal_pid_changed(j
), -ECHILD
);
2201 assert_return(ret
, -EINVAL
);
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
);
2214 *ret
= le64toh(o
->entry
.realtime
);
2218 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2224 assert_return(j
, -EINVAL
);
2225 assert_return(!journal_pid_changed(j
), -ECHILD
);
2227 f
= j
->current_file
;
2229 return -EADDRNOTAVAIL
;
2231 if (f
->current_offset
<= 0)
2232 return -EADDRNOTAVAIL
;
2234 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2239 *ret_boot_id
= o
->entry
.boot_id
;
2241 r
= sd_id128_get_boot(&id
);
2245 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2250 *ret
= le64toh(o
->entry
.monotonic
);
2255 static bool field_is_valid(const char *field
) {
2263 if (startswith(field
, "__"))
2266 for (p
= field
; *p
; p
++) {
2271 if (*p
>= 'A' && *p
<= 'Z')
2274 if (*p
>= '0' && *p
<= '9')
2283 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2286 size_t field_length
;
2290 assert_return(j
, -EINVAL
);
2291 assert_return(!journal_pid_changed(j
), -ECHILD
);
2292 assert_return(field
, -EINVAL
);
2293 assert_return(data
, -EINVAL
);
2294 assert_return(size
, -EINVAL
);
2295 assert_return(field_is_valid(field
), -EINVAL
);
2297 f
= j
->current_file
;
2299 return -EADDRNOTAVAIL
;
2301 if (f
->current_offset
<= 0)
2302 return -EADDRNOTAVAIL
;
2304 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2308 field_length
= strlen(field
);
2310 n
= journal_file_entry_n_items(o
);
2311 for (i
= 0; i
< n
; i
++) {
2317 p
= le64toh(o
->entry
.items
[i
].object_offset
);
2318 le_hash
= o
->entry
.items
[i
].hash
;
2319 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2323 if (le_hash
!= o
->data
.hash
)
2326 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2328 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2330 #if HAVE_COMPRESSION
2331 r
= decompress_startswith(compression
,
2333 &f
->compress_buffer
, &f
->compress_buffer_size
,
2334 field
, field_length
, '=');
2336 log_debug_errno(r
, "Cannot decompress %s object of length %"PRIu64
" at offset "OFSfmt
": %m",
2337 object_compressed_to_string(compression
), l
, p
);
2342 r
= decompress_blob(compression
,
2344 &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
,
2349 *data
= f
->compress_buffer
;
2350 *size
= (size_t) rsize
;
2355 return -EPROTONOSUPPORT
;
2357 } else if (l
>= field_length
+1 &&
2358 memcmp(o
->data
.payload
, field
, field_length
) == 0 &&
2359 o
->data
.payload
[field_length
] == '=') {
2363 if ((uint64_t) t
!= l
)
2366 *data
= o
->data
.payload
;
2372 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2380 static int return_data(sd_journal
*j
, JournalFile
*f
, Object
*o
, const void **data
, size_t *size
) {
2385 l
= le64toh(READ_NOW(o
->object
.size
));
2386 if (l
< offsetof(Object
, data
.payload
))
2388 l
-= offsetof(Object
, data
.payload
);
2391 /* We can't read objects larger than 4G on a 32bit machine */
2392 if ((uint64_t) t
!= l
)
2395 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2397 #if HAVE_COMPRESSION
2401 r
= decompress_blob(compression
,
2402 o
->data
.payload
, l
, &f
->compress_buffer
,
2403 &f
->compress_buffer_size
, &rsize
, j
->data_threshold
);
2407 *data
= f
->compress_buffer
;
2408 *size
= (size_t) rsize
;
2410 return -EPROTONOSUPPORT
;
2413 *data
= o
->data
.payload
;
2420 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2427 assert_return(j
, -EINVAL
);
2428 assert_return(!journal_pid_changed(j
), -ECHILD
);
2429 assert_return(data
, -EINVAL
);
2430 assert_return(size
, -EINVAL
);
2432 f
= j
->current_file
;
2434 return -EADDRNOTAVAIL
;
2436 if (f
->current_offset
<= 0)
2437 return -EADDRNOTAVAIL
;
2439 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2443 n
= journal_file_entry_n_items(o
);
2444 if (j
->current_field
>= n
)
2447 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2448 le_hash
= o
->entry
.items
[j
->current_field
].hash
;
2449 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2453 if (le_hash
!= o
->data
.hash
)
2456 r
= return_data(j
, f
, o
, data
, size
);
2465 _public_
int sd_journal_enumerate_available_data(sd_journal
*j
, const void **data
, size_t *size
) {
2469 r
= sd_journal_enumerate_data(j
, data
, size
);
2472 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r
))
2474 j
->current_field
++; /* Try with the next field */
2478 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2482 j
->current_field
= 0;
2485 static int reiterate_all_paths(sd_journal
*j
) {
2488 if (j
->no_new_files
)
2489 return add_current_paths(j
);
2491 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2492 return add_search_paths(j
);
2494 if (j
->toplevel_fd
>= 0)
2495 return add_root_directory(j
, NULL
, false);
2498 return add_root_directory(j
, j
->path
, true);
2500 return add_search_paths(j
);
2503 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2506 assert_return(j
, -EINVAL
);
2507 assert_return(!journal_pid_changed(j
), -ECHILD
);
2510 return -EMEDIUMTYPE
;
2512 if (j
->inotify_fd
>= 0)
2513 return j
->inotify_fd
;
2515 r
= allocate_inotify(j
);
2519 log_debug("Reiterating files to get inotify watches established.");
2521 /* Iterate through all dirs again, to add them to the inotify */
2522 r
= reiterate_all_paths(j
);
2526 return j
->inotify_fd
;
2529 _public_
int sd_journal_get_events(sd_journal
*j
) {
2532 assert_return(j
, -EINVAL
);
2533 assert_return(!journal_pid_changed(j
), -ECHILD
);
2535 fd
= sd_journal_get_fd(j
);
2542 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2545 assert_return(j
, -EINVAL
);
2546 assert_return(!journal_pid_changed(j
), -ECHILD
);
2547 assert_return(timeout_usec
, -EINVAL
);
2549 fd
= sd_journal_get_fd(j
);
2553 if (!j
->on_network
) {
2554 *timeout_usec
= (uint64_t) -1;
2558 /* If we are on the network we need to regularly check for
2559 * changes manually */
2561 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2565 static void process_q_overflow(sd_journal
*j
) {
2572 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2573 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2574 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2575 * are subject for unloading. */
2577 log_debug("Inotify queue overrun, reiterating everything.");
2580 (void) reiterate_all_paths(j
);
2582 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2584 if (f
->last_seen_generation
== j
->generation
)
2587 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2588 remove_file_real(j
, f
);
2591 HASHMAP_FOREACH(m
, j
->directories_by_path
, i
) {
2593 if (m
->last_seen_generation
== j
->generation
)
2596 if (m
->is_root
) /* Never GC root directories */
2599 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2600 remove_directory(j
, m
);
2603 log_debug("Reiteration complete.");
2606 static void process_inotify_event(sd_journal
*j
, const struct inotify_event
*e
) {
2612 if (e
->mask
& IN_Q_OVERFLOW
) {
2613 process_q_overflow(j
);
2617 /* Is this a subdirectory we watch? */
2618 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2620 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2621 (endswith(e
->name
, ".journal") ||
2622 endswith(e
->name
, ".journal~"))) {
2624 /* Event for a journal file */
2626 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2627 (void) add_file_by_name(j
, d
->path
, e
->name
);
2628 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2629 remove_file_by_name(j
, d
->path
, e
->name
);
2631 } else if (!d
->is_root
&& e
->len
== 0) {
2633 /* Event for a subdirectory */
2635 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2636 remove_directory(j
, d
);
2638 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2640 /* Event for root directory */
2642 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2643 (void) add_directory(j
, d
->path
, e
->name
);
2649 if (e
->mask
& IN_IGNORED
)
2652 log_debug("Unexpected inotify event.");
2655 static int determine_change(sd_journal
*j
) {
2660 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2661 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2663 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2666 _public_
int sd_journal_process(sd_journal
*j
) {
2667 bool got_something
= false;
2669 assert_return(j
, -EINVAL
);
2670 assert_return(!journal_pid_changed(j
), -ECHILD
);
2672 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
2675 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2676 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2679 union inotify_event_buffer buffer
;
2680 struct inotify_event
*e
;
2683 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2685 if (IN_SET(errno
, EAGAIN
, EINTR
))
2686 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2691 got_something
= true;
2693 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2694 process_inotify_event(j
, e
);
2698 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2702 assert_return(j
, -EINVAL
);
2703 assert_return(!journal_pid_changed(j
), -ECHILD
);
2705 if (j
->inotify_fd
< 0) {
2709 /* This is the first invocation, hence create the
2711 r
= sd_journal_get_fd(j
);
2715 /* Server might have done some vacuuming while we weren't watching.
2716 Get rid of the deleted files now so they don't stay around indefinitely. */
2717 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2718 r
= journal_file_fstat(f
);
2720 remove_file_real(j
, f
);
2722 log_debug_errno(r
,"Failed to fstat() journal file '%s' : %m", f
->path
);
2727 /* The journal might have changed since the context
2728 * object was created and we weren't watching before,
2729 * hence don't wait for anything, and return
2731 return determine_change(j
);
2734 r
= sd_journal_get_timeout(j
, &t
);
2738 if (t
!= (uint64_t) -1) {
2741 n
= now(CLOCK_MONOTONIC
);
2742 t
= t
> n
? t
- n
: 0;
2744 if (timeout_usec
== (uint64_t) -1 || timeout_usec
> t
)
2749 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2750 } while (r
== -EINTR
);
2755 return sd_journal_process(j
);
2758 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2762 uint64_t fmin
= 0, tmax
= 0;
2765 assert_return(j
, -EINVAL
);
2766 assert_return(!journal_pid_changed(j
), -ECHILD
);
2767 assert_return(from
|| to
, -EINVAL
);
2768 assert_return(from
!= to
, -EINVAL
);
2770 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2773 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2786 fmin
= MIN(fr
, fmin
);
2787 tmax
= MAX(t
, tmax
);
2796 return first
? 0 : 1;
2799 _public_
int sd_journal_get_cutoff_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t *from
, uint64_t *to
) {
2805 assert_return(j
, -EINVAL
);
2806 assert_return(!journal_pid_changed(j
), -ECHILD
);
2807 assert_return(from
|| to
, -EINVAL
);
2808 assert_return(from
!= to
, -EINVAL
);
2810 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2813 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &fr
, &t
);
2823 *from
= MIN(fr
, *from
);
2838 void journal_print_header(sd_journal
*j
) {
2841 bool newline
= false;
2845 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2851 journal_file_print_header(f
);
2855 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *bytes
) {
2860 assert_return(j
, -EINVAL
);
2861 assert_return(!journal_pid_changed(j
), -ECHILD
);
2862 assert_return(bytes
, -EINVAL
);
2864 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2867 if (fstat(f
->fd
, &st
) < 0)
2870 sum
+= (uint64_t) st
.st_blocks
* 512ULL;
2877 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2880 assert_return(j
, -EINVAL
);
2881 assert_return(!journal_pid_changed(j
), -ECHILD
);
2882 assert_return(!isempty(field
), -EINVAL
);
2883 assert_return(field_is_valid(field
), -EINVAL
);
2889 free(j
->unique_field
);
2890 j
->unique_field
= f
;
2891 j
->unique_file
= NULL
;
2892 j
->unique_offset
= 0;
2893 j
->unique_file_lost
= false;
2898 _public_
int sd_journal_enumerate_unique(sd_journal
*j
, const void **data
, size_t *l
) {
2901 assert_return(j
, -EINVAL
);
2902 assert_return(!journal_pid_changed(j
), -ECHILD
);
2903 assert_return(data
, -EINVAL
);
2904 assert_return(l
, -EINVAL
);
2905 assert_return(j
->unique_field
, -EINVAL
);
2907 k
= strlen(j
->unique_field
);
2909 if (!j
->unique_file
) {
2910 if (j
->unique_file_lost
)
2913 j
->unique_file
= ordered_hashmap_first(j
->files
);
2914 if (!j
->unique_file
)
2917 j
->unique_offset
= 0;
2929 /* Proceed to next data object in the field's linked list */
2930 if (j
->unique_offset
== 0) {
2931 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2935 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2937 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2941 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2944 /* We reached the end of the list? Then start again, with the next file */
2945 if (j
->unique_offset
== 0) {
2946 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2947 if (!j
->unique_file
)
2953 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2954 * instead, so that we can look at this data object at the same
2955 * time as one on another file */
2956 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2960 /* Let's do the type check by hand, since we used 0 context above. */
2961 if (o
->object
.type
!= OBJECT_DATA
)
2962 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2963 "%s:offset " OFSfmt
": object has type %d, expected %d",
2964 j
->unique_file
->path
,
2966 o
->object
.type
, OBJECT_DATA
);
2968 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2972 /* Check if we have at least the field name and "=". */
2974 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2975 "%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2976 j
->unique_file
->path
,
2977 j
->unique_offset
, ol
, k
+ 1);
2979 if (memcmp(odata
, j
->unique_field
, k
) || ((const char*) odata
)[k
] != '=')
2980 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2981 "%s:offset " OFSfmt
": object does not start with \"%s=\"",
2982 j
->unique_file
->path
,
2986 /* OK, now let's see if we already returned this data
2987 * object by checking if it exists in the earlier
2988 * traversed files. */
2990 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2991 if (of
== j
->unique_file
)
2994 /* Skip this file it didn't have any fields indexed */
2995 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2998 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
3010 r
= return_data(j
, j
->unique_file
, o
, data
, l
);
3018 _public_
int sd_journal_enumerate_available_unique(sd_journal
*j
, const void **data
, size_t *size
) {
3022 r
= sd_journal_enumerate_unique(j
, data
, size
);
3025 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r
))
3027 /* Try with the next field. sd_journal_enumerate_unique() modifies state, so on the next try
3028 * we will access the next field. */
3032 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
3036 j
->unique_file
= NULL
;
3037 j
->unique_offset
= 0;
3038 j
->unique_file_lost
= false;
3041 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
3044 assert_return(j
, -EINVAL
);
3045 assert_return(!journal_pid_changed(j
), -ECHILD
);
3046 assert_return(field
, -EINVAL
);
3048 if (!j
->fields_file
) {
3049 if (j
->fields_file_lost
)
3052 j
->fields_file
= ordered_hashmap_first(j
->files
);
3053 if (!j
->fields_file
)
3056 j
->fields_hash_table_index
= 0;
3057 j
->fields_offset
= 0;
3061 JournalFile
*f
, *of
;
3070 if (j
->fields_offset
== 0) {
3073 /* We are not yet positioned at any field. Let's pick the first one */
3074 r
= journal_file_map_field_hash_table(f
);
3078 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
3080 if (j
->fields_hash_table_index
>= m
) {
3081 /* Reached the end of the hash table, go to the next file. */
3086 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
3088 if (j
->fields_offset
!= 0)
3091 /* Empty hash table bucket, go to next one */
3092 j
->fields_hash_table_index
++;
3096 /* Proceed with next file */
3097 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
3098 if (!j
->fields_file
) {
3103 j
->fields_offset
= 0;
3104 j
->fields_hash_table_index
= 0;
3109 /* We are already positioned at a field. If so, let's figure out the next field from it */
3111 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
3115 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
3116 if (j
->fields_offset
== 0) {
3117 /* Reached the end of the hash table chain */
3118 j
->fields_hash_table_index
++;
3123 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
3124 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
3128 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
3129 if (o
->object
.type
!= OBJECT_FIELD
)
3130 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3131 "%s:offset " OFSfmt
": object has type %i, expected %i",
3132 f
->path
, j
->fields_offset
,
3133 o
->object
.type
, OBJECT_FIELD
);
3135 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
3137 /* Let's see if we already returned this field name before. */
3139 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
3143 /* Skip this file it didn't have any fields indexed */
3144 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3147 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
, le64toh(o
->field
.hash
), NULL
, NULL
);
3159 /* Check if this is really a valid string containing no NUL byte */
3160 if (memchr(o
->field
.payload
, 0, sz
))
3163 if (sz
> j
->data_threshold
)
3164 sz
= j
->data_threshold
;
3166 if (!GREEDY_REALLOC(j
->fields_buffer
, j
->fields_buffer_allocated
, sz
+ 1))
3169 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3170 j
->fields_buffer
[sz
] = 0;
3172 if (!field_is_valid(j
->fields_buffer
))
3175 *field
= j
->fields_buffer
;
3180 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3184 j
->fields_file
= NULL
;
3185 j
->fields_hash_table_index
= 0;
3186 j
->fields_offset
= 0;
3187 j
->fields_file_lost
= false;
3190 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3191 assert_return(j
, -EINVAL
);
3192 assert_return(!journal_pid_changed(j
), -ECHILD
);
3194 return !j
->on_network
;
3197 static char *lookup_field(const char *field
, void *userdata
) {
3198 sd_journal
*j
= userdata
;
3206 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3208 size
> REPLACE_VAR_MAX
)
3209 return strdup(field
);
3211 d
= strlen(field
) + 1;
3213 return strndup((const char*) data
+ d
, size
- d
);
3216 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3220 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3224 assert_return(j
, -EINVAL
);
3225 assert_return(!journal_pid_changed(j
), -ECHILD
);
3226 assert_return(ret
, -EINVAL
);
3228 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3232 cid
= strndup((const char*) data
+ 11, size
- 11);
3236 r
= sd_id128_from_string(cid
, &id
);
3240 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
3244 t
= replace_var(text
, lookup_field
, j
);
3252 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3253 assert_return(ret
, -EINVAL
);
3255 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3258 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3259 assert_return(j
, -EINVAL
);
3260 assert_return(!journal_pid_changed(j
), -ECHILD
);
3262 j
->data_threshold
= sz
;
3266 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3267 assert_return(j
, -EINVAL
);
3268 assert_return(!journal_pid_changed(j
), -ECHILD
);
3269 assert_return(sz
, -EINVAL
);
3271 *sz
= j
->data_threshold
;
3275 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3276 assert_return(j
, -EINVAL
);
3278 return j
->has_runtime_files
;
3281 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3282 assert_return(j
, -EINVAL
);
3284 return j
->has_persistent_files
;