1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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
) {
66 _cleanup_free_
char *copy
= NULL
;
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. */
89 k
= hashmap_ensure_put(&j
->errors
, NULL
, INT_TO_PTR(r
), copy
);
101 static void detach_location(sd_journal
*j
) {
106 j
->current_file
= NULL
;
107 j
->current_field
= 0;
109 ORDERED_HASHMAP_FOREACH(f
, j
->files
)
110 journal_file_reset_location(f
);
113 static void init_location(Location
*l
, LocationType type
, JournalFile
*f
, Object
*o
) {
115 assert(IN_SET(type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
120 .seqnum
= le64toh(o
->entry
.seqnum
),
121 .seqnum_id
= f
->header
->seqnum_id
,
122 .realtime
= le64toh(o
->entry
.realtime
),
123 .monotonic
= le64toh(o
->entry
.monotonic
),
124 .boot_id
= o
->entry
.boot_id
,
125 .xor_hash
= le64toh(o
->entry
.xor_hash
),
127 .realtime_set
= true,
128 .monotonic_set
= true,
129 .xor_hash_set
= true,
133 static void set_location(sd_journal
*j
, JournalFile
*f
, Object
*o
) {
138 init_location(&j
->current_location
, LOCATION_DISCRETE
, f
, o
);
141 j
->current_field
= 0;
143 /* Let f know its candidate entry was picked. */
144 assert(f
->location_type
== LOCATION_SEEK
);
145 f
->location_type
= LOCATION_DISCRETE
;
148 static int match_is_valid(const void *data
, size_t size
) {
156 if (((char*) data
)[0] == '_' && ((char*) data
)[1] == '_')
160 for (p
= b
; p
< b
+ size
; p
++) {
168 if (*p
>= 'A' && *p
<= 'Z')
171 if (*p
>= '0' && *p
<= '9')
180 static bool same_field(const void *_a
, size_t s
, const void *_b
, size_t t
) {
181 const uint8_t *a
= _a
, *b
= _b
;
184 for (j
= 0; j
< s
&& j
< t
; j
++) {
193 assert_not_reached("\"=\" not found");
196 static Match
*match_new(Match
*p
, MatchType t
) {
209 LIST_PREPEND(matches
, p
->matches
, m
);
214 static void match_free(Match
*m
) {
218 match_free(m
->matches
);
221 LIST_REMOVE(matches
, m
->parent
->matches
, m
);
227 static void match_free_if_empty(Match
*m
) {
228 if (!m
|| m
->matches
)
234 _public_
int sd_journal_add_match(sd_journal
*j
, const void *data
, size_t size
) {
235 Match
*l3
, *l4
, *add_here
= NULL
, *m
;
238 assert_return(j
, -EINVAL
);
239 assert_return(!journal_pid_changed(j
), -ECHILD
);
240 assert_return(data
, -EINVAL
);
245 assert_return(match_is_valid(data
, size
), -EINVAL
);
251 * level 4: concrete matches */
254 j
->level0
= match_new(NULL
, MATCH_AND_TERM
);
260 j
->level1
= match_new(j
->level0
, MATCH_OR_TERM
);
266 j
->level2
= match_new(j
->level1
, MATCH_AND_TERM
);
271 assert(j
->level0
->type
== MATCH_AND_TERM
);
272 assert(j
->level1
->type
== MATCH_OR_TERM
);
273 assert(j
->level2
->type
== MATCH_AND_TERM
);
275 /* Old-style Jenkins (unkeyed) hashing only here. We do not cover new-style siphash (keyed) hashing
276 * here, since it's different for each file, and thus can't be pre-calculated in the Match object. */
277 hash
= jenkins_hash64(data
, size
);
279 LIST_FOREACH(matches
, l3
, j
->level2
->matches
) {
280 assert(l3
->type
== MATCH_OR_TERM
);
282 LIST_FOREACH(matches
, l4
, l3
->matches
) {
283 assert(l4
->type
== MATCH_DISCRETE
);
285 /* Exactly the same match already? Then ignore
287 if (l4
->hash
== hash
&&
289 memcmp(l4
->data
, data
, size
) == 0)
292 /* Same field? Then let's add this to this OR term */
293 if (same_field(data
, size
, l4
->data
, l4
->size
)) {
304 add_here
= match_new(j
->level2
, MATCH_OR_TERM
);
309 m
= match_new(add_here
, MATCH_DISCRETE
);
315 m
->data
= memdup(data
, size
);
324 match_free_if_empty(add_here
);
325 match_free_if_empty(j
->level2
);
326 match_free_if_empty(j
->level1
);
327 match_free_if_empty(j
->level0
);
332 _public_
int sd_journal_add_conjunction(sd_journal
*j
) {
333 assert_return(j
, -EINVAL
);
334 assert_return(!journal_pid_changed(j
), -ECHILD
);
342 if (!j
->level1
->matches
)
351 _public_
int sd_journal_add_disjunction(sd_journal
*j
) {
352 assert_return(j
, -EINVAL
);
353 assert_return(!journal_pid_changed(j
), -ECHILD
);
364 if (!j
->level2
->matches
)
371 static char *match_make_string(Match
*m
) {
374 bool enclose
= false;
377 return strdup("none");
379 if (m
->type
== MATCH_DISCRETE
)
380 return cescape_length(m
->data
, m
->size
);
382 LIST_FOREACH(matches
, i
, m
->matches
) {
385 t
= match_make_string(i
);
390 k
= strjoin(p
, m
->type
== MATCH_OR_TERM
? " OR " : " AND ", t
);
405 r
= strjoin("(", p
, ")");
413 char *journal_make_match_string(sd_journal
*j
) {
416 return match_make_string(j
->level0
);
419 _public_
void sd_journal_flush_matches(sd_journal
*j
) {
424 match_free(j
->level0
);
426 j
->level0
= j
->level1
= j
->level2
= NULL
;
431 _pure_
static int compare_with_location(const JournalFile
*f
, const Location
*l
, const JournalFile
*current_file
) {
436 assert(f
->location_type
== LOCATION_SEEK
);
437 assert(IN_SET(l
->type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
439 if (l
->monotonic_set
&&
440 sd_id128_equal(f
->current_boot_id
, l
->boot_id
) &&
442 f
->current_realtime
== l
->realtime
&&
444 f
->current_xor_hash
== l
->xor_hash
&&
446 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
) &&
447 f
->current_seqnum
== l
->seqnum
&&
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 /* If the keyed hash logic is used, we need to calculate the hash fresh per file. Otherwise
505 * we can use what we pre-calculated. */
506 if (JOURNAL_HEADER_KEYED_HASH(f
->header
))
507 hash
= journal_file_hash_data(f
, m
->data
, m
->size
);
511 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, hash
, NULL
, &dp
);
515 return journal_file_move_to_entry_by_offset_for_data(f
, dp
, after_offset
, direction
, ret
, offset
);
517 } else if (m
->type
== MATCH_OR_TERM
) {
520 /* Find the earliest match beyond after_offset */
522 LIST_FOREACH(matches
, i
, m
->matches
) {
525 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
529 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
537 } else if (m
->type
== MATCH_AND_TERM
) {
538 Match
*i
, *last_moved
;
540 /* Always jump to the next matching entry and repeat
541 * this until we find an offset that matches for all
547 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
551 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
552 last_moved
= m
->matches
;
554 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
557 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
561 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
562 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
571 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
583 static int find_location_for_match(
587 direction_t direction
,
597 if (m
->type
== MATCH_DISCRETE
) {
600 if (JOURNAL_HEADER_KEYED_HASH(f
->header
))
601 hash
= journal_file_hash_data(f
, m
->data
, m
->size
);
605 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, hash
, NULL
, &dp
);
609 /* FIXME: missing: find by monotonic */
611 if (j
->current_location
.type
== LOCATION_HEAD
)
612 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_DOWN
, ret
, offset
);
613 if (j
->current_location
.type
== LOCATION_TAIL
)
614 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_UP
, ret
, offset
);
615 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
616 return journal_file_move_to_entry_by_seqnum_for_data(f
, dp
, j
->current_location
.seqnum
, direction
, ret
, offset
);
617 if (j
->current_location
.monotonic_set
) {
618 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, dp
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
622 if (j
->current_location
.realtime_set
)
623 return journal_file_move_to_entry_by_realtime_for_data(f
, dp
, j
->current_location
.realtime
, direction
, ret
, offset
);
625 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, direction
, ret
, offset
);
627 } else if (m
->type
== MATCH_OR_TERM
) {
632 /* Find the earliest match */
634 LIST_FOREACH(matches
, i
, m
->matches
) {
637 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
641 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
649 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
664 assert(m
->type
== MATCH_AND_TERM
);
666 /* First jump to the last match, and then find the
667 * next one where all matches match */
672 LIST_FOREACH(matches
, i
, m
->matches
) {
675 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
679 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
683 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
687 static int find_location_with_matches(
690 direction_t direction
,
702 /* No matches is simple */
704 if (j
->current_location
.type
== LOCATION_HEAD
)
705 return journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
);
706 if (j
->current_location
.type
== LOCATION_TAIL
)
707 return journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
);
708 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
709 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
710 if (j
->current_location
.monotonic_set
) {
711 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
715 if (j
->current_location
.realtime_set
)
716 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
718 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
720 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
723 static int next_with_matches(
726 direction_t direction
,
735 /* No matches is easy. We simple advance the file
738 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
740 /* If we have a match then we look for the next matching entry
741 * with an offset at least one step larger */
742 return next_for_match(j
, j
->level0
, f
,
743 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
744 : f
->current_offset
- 1,
745 direction
, ret
, offset
);
748 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
750 uint64_t cp
, n_entries
;
756 n_entries
= le64toh(f
->header
->n_entries
);
758 /* If we hit EOF before, we don't need to look into this file again
759 * unless direction changed or new entries appeared. */
760 if (f
->last_direction
== direction
&& f
->location_type
== LOCATION_TAIL
&&
761 n_entries
== f
->last_n_entries
)
764 f
->last_n_entries
= n_entries
;
766 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
767 /* LOCATION_SEEK here means we did the work in a previous
768 * iteration and the current location already points to a
769 * candidate entry. */
770 if (f
->location_type
!= LOCATION_SEEK
) {
771 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
775 journal_file_save_location(f
, c
, cp
);
778 f
->last_direction
= direction
;
780 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
784 journal_file_save_location(f
, c
, cp
);
787 /* OK, we found the spot, now let's advance until an entry
788 * that is actually different from what we were previously
789 * looking at. This is necessary to handle entries which exist
790 * in two (or more) journal files, and which shall all be
791 * suppressed but one. */
796 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
799 k
= compare_with_location(f
, &j
->current_location
, j
->current_file
);
801 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
808 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
812 journal_file_save_location(f
, c
, cp
);
816 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
817 JournalFile
*new_file
= NULL
;
823 assert_return(j
, -EINVAL
);
824 assert_return(!journal_pid_changed(j
), -ECHILD
);
826 r
= iterated_cache_get(j
->files_cache
, NULL
, &files
, &n_files
);
830 for (i
= 0; i
< n_files
; i
++) {
831 JournalFile
*f
= (JournalFile
*)files
[i
];
834 r
= next_beyond_location(j
, f
, direction
);
836 log_debug_errno(r
, "Can't iterate through %s, ignoring: %m", f
->path
);
837 remove_file_real(j
, f
);
840 f
->location_type
= LOCATION_TAIL
;
849 k
= journal_file_compare_locations(f
, new_file
);
851 found
= direction
== DIRECTION_DOWN
? k
< 0 : k
> 0;
861 r
= journal_file_move_to_object(new_file
, OBJECT_ENTRY
, new_file
->current_offset
, &o
);
865 set_location(j
, new_file
, o
);
870 _public_
int sd_journal_next(sd_journal
*j
) {
871 return real_journal_next(j
, DIRECTION_DOWN
);
874 _public_
int sd_journal_previous(sd_journal
*j
) {
875 return real_journal_next(j
, DIRECTION_UP
);
878 static int real_journal_next_skip(sd_journal
*j
, direction_t direction
, uint64_t skip
) {
881 assert_return(j
, -EINVAL
);
882 assert_return(!journal_pid_changed(j
), -ECHILD
);
883 assert_return(skip
<= INT_MAX
, -ERANGE
);
886 /* If this is not a discrete skip, then at least
887 * resolve the current location */
888 if (j
->current_location
.type
!= LOCATION_DISCRETE
) {
889 r
= real_journal_next(j
, direction
);
898 r
= real_journal_next(j
, direction
);
912 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
913 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
916 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
917 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
920 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
923 char bid
[SD_ID128_STRING_MAX
], sid
[SD_ID128_STRING_MAX
];
925 assert_return(j
, -EINVAL
);
926 assert_return(!journal_pid_changed(j
), -ECHILD
);
927 assert_return(cursor
, -EINVAL
);
929 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
930 return -EADDRNOTAVAIL
;
932 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
936 sd_id128_to_string(j
->current_file
->header
->seqnum_id
, sid
);
937 sd_id128_to_string(o
->entry
.boot_id
, bid
);
940 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
941 sid
, le64toh(o
->entry
.seqnum
),
942 bid
, le64toh(o
->entry
.monotonic
),
943 le64toh(o
->entry
.realtime
),
944 le64toh(o
->entry
.xor_hash
)) < 0)
950 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
951 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
952 bool seqnum_id_set
= false,
955 monotonic_set
= false,
956 realtime_set
= false,
957 xor_hash_set
= false;
958 sd_id128_t seqnum_id
, boot_id
;
961 assert_return(j
, -EINVAL
);
962 assert_return(!journal_pid_changed(j
), -ECHILD
);
963 assert_return(!isempty(cursor
), -EINVAL
);
965 for (const char *p
= cursor
;;) {
966 _cleanup_free_
char *word
= NULL
;
968 r
= extract_first_word(&p
, &word
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
974 if (word
[0] == '\0' || word
[1] != '=')
979 seqnum_id_set
= true;
980 r
= sd_id128_from_string(word
+ 2, &seqnum_id
);
987 if (sscanf(word
+ 2, "%llx", &seqnum
) != 1)
993 r
= sd_id128_from_string(word
+ 2, &boot_id
);
997 monotonic_set
= true;
998 if (sscanf(word
+ 2, "%llx", &monotonic
) != 1)
1003 realtime_set
= true;
1004 if (sscanf(word
+ 2, "%llx", &realtime
) != 1)
1009 xor_hash_set
= true;
1010 if (sscanf(word
+ 2, "%llx", &xor_hash
) != 1)
1016 if ((!seqnum_set
|| !seqnum_id_set
) &&
1017 (!monotonic_set
|| !boot_id_set
) &&
1022 j
->current_location
= (Location
) {
1023 .type
= LOCATION_SEEK
,
1027 j
->current_location
.realtime
= (uint64_t) realtime
;
1028 j
->current_location
.realtime_set
= true;
1031 if (seqnum_set
&& seqnum_id_set
) {
1032 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1033 j
->current_location
.seqnum_id
= seqnum_id
;
1034 j
->current_location
.seqnum_set
= true;
1037 if (monotonic_set
&& boot_id_set
) {
1038 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1039 j
->current_location
.boot_id
= boot_id
;
1040 j
->current_location
.monotonic_set
= true;
1044 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1045 j
->current_location
.xor_hash_set
= true;
1051 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1055 assert_return(j
, -EINVAL
);
1056 assert_return(!journal_pid_changed(j
), -ECHILD
);
1057 assert_return(!isempty(cursor
), -EINVAL
);
1059 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1060 return -EADDRNOTAVAIL
;
1062 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1067 _cleanup_free_
char *item
= NULL
;
1068 unsigned long long ll
;
1072 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1079 if (strlen(item
) < 2 || item
[1] != '=')
1085 k
= sd_id128_from_string(item
+2, &id
);
1088 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1093 if (sscanf(item
+2, "%llx", &ll
) != 1)
1095 if (ll
!= le64toh(o
->entry
.seqnum
))
1100 k
= sd_id128_from_string(item
+2, &id
);
1103 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1108 if (sscanf(item
+2, "%llx", &ll
) != 1)
1110 if (ll
!= le64toh(o
->entry
.monotonic
))
1115 if (sscanf(item
+2, "%llx", &ll
) != 1)
1117 if (ll
!= le64toh(o
->entry
.realtime
))
1122 if (sscanf(item
+2, "%llx", &ll
) != 1)
1124 if (ll
!= le64toh(o
->entry
.xor_hash
))
1133 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t usec
) {
1134 assert_return(j
, -EINVAL
);
1135 assert_return(!journal_pid_changed(j
), -ECHILD
);
1139 j
->current_location
= (Location
) {
1140 .type
= LOCATION_SEEK
,
1143 .monotonic_set
= true,
1149 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1150 assert_return(j
, -EINVAL
);
1151 assert_return(!journal_pid_changed(j
), -ECHILD
);
1155 j
->current_location
= (Location
) {
1156 .type
= LOCATION_SEEK
,
1158 .realtime_set
= true,
1164 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1165 assert_return(j
, -EINVAL
);
1166 assert_return(!journal_pid_changed(j
), -ECHILD
);
1170 j
->current_location
= (Location
) {
1171 .type
= LOCATION_HEAD
,
1177 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1178 assert_return(j
, -EINVAL
);
1179 assert_return(!journal_pid_changed(j
), -ECHILD
);
1183 j
->current_location
= (Location
) {
1184 .type
= LOCATION_TAIL
,
1190 static void check_network(sd_journal
*j
, int fd
) {
1196 j
->on_network
= fd_is_network_fs(fd
);
1199 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1200 const char *full
, *tilded
, *atted
;
1202 full
= strjoina(prefix
, ".journal");
1203 tilded
= strjoina(full
, "~");
1204 atted
= strjoina(prefix
, "@");
1206 return STR_IN_SET(filename
, full
, tilded
) ||
1207 startswith(filename
, atted
);
1210 static bool file_type_wanted(int flags
, const char *filename
) {
1213 if (!endswith(filename
, ".journal") && !endswith(filename
, ".journal~"))
1216 /* no flags set → every type is OK */
1217 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1220 if (flags
& SD_JOURNAL_SYSTEM
&& file_has_type_prefix("system", filename
))
1223 if (flags
& SD_JOURNAL_CURRENT_USER
) {
1224 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1226 xsprintf(prefix
, "user-"UID_FMT
, getuid());
1228 if (file_has_type_prefix(prefix
, filename
))
1235 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1240 if (j
->toplevel_fd
>= 0)
1243 return path_startswith(path
, prefix
);
1246 static void track_file_disposition(sd_journal
*j
, JournalFile
*f
) {
1250 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1251 j
->has_runtime_files
= true;
1252 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1253 j
->has_persistent_files
= true;
1256 static const char *skip_slash(const char *p
) {
1267 static int add_any_file(
1272 bool close_fd
= false;
1278 assert(fd
>= 0 || path
);
1281 if (j
->toplevel_fd
>= 0)
1282 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1283 * openat() ignores the first argument. */
1285 fd
= openat(j
->toplevel_fd
, skip_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1287 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1289 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1295 r
= fd_nonblock(fd
, false);
1297 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1302 if (fstat(fd
, &st
) < 0) {
1303 r
= log_debug_errno(errno
, "Failed to fstat file '%s': %m", path
);
1307 r
= stat_verify_regular(&st
);
1309 log_debug_errno(r
, "Refusing to open '%s', as it is not a regular file.", path
);
1313 f
= ordered_hashmap_get(j
->files
, path
);
1315 if (f
->last_stat
.st_dev
== st
.st_dev
&&
1316 f
->last_stat
.st_ino
== st
.st_ino
) {
1318 /* We already track this file, under the same path and with the same device/inode numbers, it's
1319 * hence really the same. Mark this file as seen in this generation. This is used to GC old
1320 * files in process_q_overflow() to detect journal files that are still there and discern them
1321 * from those which are gone. */
1323 f
->last_seen_generation
= j
->generation
;
1328 /* So we tracked a file under this name, but it has a different inode/device. In that case, it got
1329 * replaced (probably due to rotation?), let's drop it hence from our list. */
1330 remove_file_real(j
, f
);
1334 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1335 log_debug("Too many open journal files, not adding %s.", path
);
1340 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, false, 0, false, NULL
, j
->mmap
, NULL
, NULL
, &f
);
1342 log_debug_errno(r
, "Failed to open journal file %s: %m", path
);
1346 /* journal_file_dump(f); */
1348 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1350 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 */
1351 (void) journal_file_close(f
);
1355 close_fd
= false; /* the fd is now owned by the JournalFile object */
1357 f
->last_seen_generation
= j
->generation
;
1359 track_file_disposition(j
, f
);
1360 check_network(j
, f
->fd
);
1362 j
->current_invalidate_counter
++;
1364 log_debug("File %s added.", f
->path
);
1373 k
= journal_put_error(j
, r
, path
);
1381 static int add_file_by_name(
1384 const char *filename
) {
1392 if (j
->no_new_files
)
1395 if (!file_type_wanted(j
->flags
, filename
))
1398 path
= prefix_roota(prefix
, filename
);
1399 return add_any_file(j
, -1, path
);
1402 static void remove_file_by_name(
1405 const char *filename
) {
1414 path
= prefix_roota(prefix
, filename
);
1415 f
= ordered_hashmap_get(j
->files
, path
);
1419 remove_file_real(j
, f
);
1422 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1426 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1428 log_debug("File %s removed.", f
->path
);
1430 if (j
->current_file
== f
) {
1431 j
->current_file
= NULL
;
1432 j
->current_field
= 0;
1435 if (j
->unique_file
== f
) {
1436 /* Jump to the next unique_file or NULL if that one was last */
1437 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1438 j
->unique_offset
= 0;
1439 if (!j
->unique_file
)
1440 j
->unique_file_lost
= true;
1443 if (j
->fields_file
== f
) {
1444 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1445 j
->fields_offset
= 0;
1446 if (!j
->fields_file
)
1447 j
->fields_file_lost
= true;
1450 (void) journal_file_close(f
);
1452 j
->current_invalidate_counter
++;
1455 static int dirname_is_machine_id(const char *fn
) {
1456 sd_id128_t id
, machine
;
1460 /* Returns true if the specified directory name matches the local machine ID */
1462 r
= sd_id128_get_machine(&machine
);
1466 e
= strchr(fn
, '.');
1470 /* Looks like it has a namespace suffix. Verify that. */
1471 if (!log_namespace_name_valid(e
+ 1))
1474 k
= strndupa(fn
, e
- fn
);
1475 r
= sd_id128_from_string(k
, &id
);
1477 r
= sd_id128_from_string(fn
, &id
);
1481 return sd_id128_equal(id
, machine
);
1484 static int dirname_has_namespace(const char *fn
, const char *namespace) {
1487 /* Returns true if the specified directory name matches the specified namespace */
1489 e
= strchr(fn
, '.');
1496 if (!streq(e
+ 1, namespace))
1499 k
= strndupa(fn
, e
- fn
);
1500 return id128_is_valid(k
);
1506 return id128_is_valid(fn
);
1509 static bool dirent_is_journal_file(const struct dirent
*de
) {
1512 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1514 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1517 return endswith(de
->d_name
, ".journal") ||
1518 endswith(de
->d_name
, ".journal~");
1521 static bool dirent_is_journal_subdir(const struct dirent
*de
) {
1525 /* returns true if the specified directory entry looks like a directory that might contain journal
1526 * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a
1529 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1532 e
= strchr(de
->d_name
, '.');
1534 return id128_is_valid(de
->d_name
); /* No namespace */
1536 n
= strndupa(de
->d_name
, e
- de
->d_name
);
1537 if (!id128_is_valid(n
))
1540 return log_namespace_name_valid(e
+ 1);
1543 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1550 if (j
->toplevel_fd
< 0)
1553 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1554 * relative, by dropping the initial slash */
1555 d
= xopendirat(j
->toplevel_fd
, skip_slash(path
), 0);
1563 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1565 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1572 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1574 if (dirent_is_journal_file(de
))
1575 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1577 if (m
->is_root
&& dirent_is_journal_subdir(de
))
1578 (void) add_directory(j
, m
->path
, de
->d_name
);
1584 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1587 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1594 /* Watch this directory if that's enabled and if it not being watched yet. */
1596 if (m
->wd
> 0) /* Already have a watch? */
1598 if (j
->inotify_fd
< 0) /* Not watching at all? */
1601 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1603 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1607 r
= hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
);
1609 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1611 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1612 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1617 static int add_directory(
1620 const char *dirname
) {
1622 _cleanup_free_
char *path
= NULL
;
1623 _cleanup_closedir_
DIR *d
= NULL
;
1630 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1631 * and reenumerates directory contents */
1633 path
= path_join(prefix
, dirname
);
1639 log_debug("Considering directory '%s'.", path
);
1641 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1642 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1643 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1647 (!(FLAGS_SET(j
->flags
, SD_JOURNAL_ALL_NAMESPACES
) ||
1648 dirname_has_namespace(dirname
, j
->namespace) > 0 ||
1649 (FLAGS_SET(j
->flags
, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE
) && dirname_has_namespace(dirname
, NULL
) > 0))))
1652 r
= directory_open(j
, path
, &d
);
1654 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
1658 m
= hashmap_get(j
->directories_by_path
, path
);
1660 m
= new(Directory
, 1);
1671 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1677 path
= NULL
; /* avoid freeing in cleanup */
1678 j
->current_invalidate_counter
++;
1680 log_debug("Directory %s added.", m
->path
);
1682 } else if (m
->is_root
)
1683 return 0; /* Don't 'downgrade' from root directory */
1685 m
->last_seen_generation
= j
->generation
;
1687 directory_watch(j
, m
, dirfd(d
),
1688 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1689 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
1692 if (!j
->no_new_files
)
1693 directory_enumerate(j
, m
, d
);
1695 check_network(j
, dirfd(d
));
1700 k
= journal_put_error(j
, r
, path
?: prefix
);
1707 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
1709 _cleanup_closedir_
DIR *d
= NULL
;
1715 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1716 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1717 * populate the set, as well as to update it later. */
1720 /* If there's a path specified, use it. */
1722 log_debug("Considering root directory '%s'.", p
);
1724 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
1725 !path_has_prefix(j
, p
, "/run"))
1729 p
= strjoina(j
->prefix
, p
);
1731 r
= directory_open(j
, p
, &d
);
1732 if (r
== -ENOENT
&& missing_ok
)
1735 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
1739 _cleanup_close_
int dfd
= -1;
1741 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1742 * opendir() will take possession of the fd, and close it, which we don't want. */
1744 p
= "."; /* store this as "." in the directories hashmap */
1746 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
1752 d
= take_fdopendir(&dfd
);
1761 m
= hashmap_get(j
->directories_by_path
, p
);
1763 m
= new0(Directory
, 1);
1771 m
->path
= strdup(p
);
1778 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1785 j
->current_invalidate_counter
++;
1787 log_debug("Root directory %s added.", m
->path
);
1789 } else if (!m
->is_root
)
1792 directory_watch(j
, m
, dirfd(d
),
1793 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1796 if (!j
->no_new_files
)
1797 directory_enumerate(j
, m
, d
);
1799 check_network(j
, dirfd(d
));
1804 k
= journal_put_error(j
, r
, p
);
1811 static void remove_directory(sd_journal
*j
, Directory
*d
) {
1815 hashmap_remove(j
->directories_by_wd
, INT_TO_PTR(d
->wd
));
1817 if (j
->inotify_fd
>= 0)
1818 (void) inotify_rm_watch(j
->inotify_fd
, d
->wd
);
1821 hashmap_remove(j
->directories_by_path
, d
->path
);
1824 log_debug("Root directory %s removed.", d
->path
);
1826 log_debug("Directory %s removed.", d
->path
);
1832 static int add_search_paths(sd_journal
*j
) {
1834 static const char search_paths
[] =
1835 "/run/log/journal\0"
1836 "/var/log/journal\0";
1841 /* We ignore most errors here, since the idea is to only open
1842 * what's actually accessible, and ignore the rest. */
1844 NULSTR_FOREACH(p
, search_paths
)
1845 (void) add_root_directory(j
, p
, true);
1847 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
1848 (void) add_root_directory(j
, "/var/log/journal/remote", true);
1853 static int add_current_paths(sd_journal
*j
) {
1857 assert(j
->no_new_files
);
1859 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1860 * treat them as fatal. */
1862 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
1863 _cleanup_free_
char *dir
= NULL
;
1866 dir
= dirname_malloc(f
->path
);
1870 r
= add_directory(j
, dir
, NULL
);
1878 static int allocate_inotify(sd_journal
*j
) {
1881 if (j
->inotify_fd
< 0) {
1882 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1883 if (j
->inotify_fd
< 0)
1887 return hashmap_ensure_allocated(&j
->directories_by_wd
, NULL
);
1890 static sd_journal
*journal_new(int flags
, const char *path
, const char *namespace) {
1891 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1893 j
= new0(sd_journal
, 1);
1897 j
->original_pid
= getpid_cached();
1898 j
->toplevel_fd
= -1;
1901 j
->data_threshold
= DEFAULT_DATA_THRESHOLD
;
1910 if (flags
& SD_JOURNAL_OS_ROOT
)
1917 j
->namespace = strdup(namespace);
1922 j
->files
= ordered_hashmap_new(&path_hash_ops
);
1926 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
1927 j
->directories_by_path
= hashmap_new(&path_hash_ops
);
1928 j
->mmap
= mmap_cache_new();
1929 if (!j
->files_cache
|| !j
->directories_by_path
|| !j
->mmap
)
1935 #define OPEN_ALLOWED_FLAGS \
1936 (SD_JOURNAL_LOCAL_ONLY | \
1937 SD_JOURNAL_RUNTIME_ONLY | \
1938 SD_JOURNAL_SYSTEM | \
1939 SD_JOURNAL_CURRENT_USER | \
1940 SD_JOURNAL_ALL_NAMESPACES | \
1941 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
1943 _public_
int sd_journal_open_namespace(sd_journal
**ret
, const char *namespace, int flags
) {
1944 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1947 assert_return(ret
, -EINVAL
);
1948 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
1950 j
= journal_new(flags
, NULL
, namespace);
1954 r
= add_search_paths(j
);
1962 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1963 return sd_journal_open_namespace(ret
, NULL
, flags
);
1966 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1967 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1969 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1970 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1971 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1975 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1976 * combination with sd_journal_open_directory_fd(). */
1978 assert_return(machine
, -EINVAL
);
1979 assert_return(ret
, -EINVAL
);
1980 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
1981 assert_return(hostname_is_valid(machine
, 0), -EINVAL
);
1983 p
= strjoina("/run/systemd/machines/", machine
);
1984 r
= parse_env_file(NULL
, p
,
1994 if (!streq_ptr(class, "container"))
1997 j
= journal_new(flags
, root
, NULL
);
2001 r
= add_search_paths(j
);
2009 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
2010 (SD_JOURNAL_OS_ROOT | \
2011 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2013 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
2014 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2017 assert_return(ret
, -EINVAL
);
2018 assert_return(path
, -EINVAL
);
2019 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
2021 j
= journal_new(flags
, path
, NULL
);
2025 if (flags
& SD_JOURNAL_OS_ROOT
)
2026 r
= add_search_paths(j
);
2028 r
= add_root_directory(j
, path
, false);
2036 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
2037 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2041 assert_return(ret
, -EINVAL
);
2042 assert_return(flags
== 0, -EINVAL
);
2044 j
= journal_new(flags
, NULL
, NULL
);
2048 STRV_FOREACH(path
, paths
) {
2049 r
= add_any_file(j
, -1, *path
);
2054 j
->no_new_files
= true;
2060 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
2061 (SD_JOURNAL_OS_ROOT | \
2062 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2064 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
2065 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2069 assert_return(ret
, -EINVAL
);
2070 assert_return(fd
>= 0, -EBADF
);
2071 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2073 if (fstat(fd
, &st
) < 0)
2076 if (!S_ISDIR(st
.st_mode
))
2079 j
= journal_new(flags
, NULL
, NULL
);
2083 j
->toplevel_fd
= fd
;
2085 if (flags
& SD_JOURNAL_OS_ROOT
)
2086 r
= add_search_paths(j
);
2088 r
= add_root_directory(j
, NULL
, false);
2096 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2098 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2102 assert_return(ret
, -EINVAL
);
2103 assert_return(n_fds
> 0, -EBADF
);
2104 assert_return(flags
== 0, -EINVAL
);
2106 j
= journal_new(flags
, NULL
, NULL
);
2110 for (i
= 0; i
< n_fds
; i
++) {
2118 if (fstat(fds
[i
], &st
) < 0) {
2123 r
= stat_verify_regular(&st
);
2127 r
= add_any_file(j
, fds
[i
], NULL
);
2132 j
->no_new_files
= true;
2133 j
->no_inotify
= true;
2139 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2141 ORDERED_HASHMAP_FOREACH(f
, j
->files
)
2142 f
->close_fd
= false;
2147 _public_
void sd_journal_close(sd_journal
*j
) {
2153 sd_journal_flush_matches(j
);
2155 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2156 iterated_cache_free(j
->files_cache
);
2158 while ((d
= hashmap_first(j
->directories_by_path
)))
2159 remove_directory(j
, d
);
2161 while ((d
= hashmap_first(j
->directories_by_wd
)))
2162 remove_directory(j
, d
);
2164 hashmap_free(j
->directories_by_path
);
2165 hashmap_free(j
->directories_by_wd
);
2167 safe_close(j
->inotify_fd
);
2170 mmap_cache_stats_log_debug(j
->mmap
);
2171 mmap_cache_unref(j
->mmap
);
2174 hashmap_free_free(j
->errors
);
2179 free(j
->unique_field
);
2180 free(j
->fields_buffer
);
2184 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2189 assert_return(j
, -EINVAL
);
2190 assert_return(!journal_pid_changed(j
), -ECHILD
);
2191 assert_return(ret
, -EINVAL
);
2193 f
= j
->current_file
;
2195 return -EADDRNOTAVAIL
;
2197 if (f
->current_offset
<= 0)
2198 return -EADDRNOTAVAIL
;
2200 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2204 *ret
= le64toh(o
->entry
.realtime
);
2208 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2213 assert_return(j
, -EINVAL
);
2214 assert_return(!journal_pid_changed(j
), -ECHILD
);
2216 f
= j
->current_file
;
2218 return -EADDRNOTAVAIL
;
2220 if (f
->current_offset
<= 0)
2221 return -EADDRNOTAVAIL
;
2223 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2228 *ret_boot_id
= o
->entry
.boot_id
;
2232 r
= sd_id128_get_boot(&id
);
2236 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2241 *ret
= le64toh(o
->entry
.monotonic
);
2246 static bool field_is_valid(const char *field
) {
2254 if (startswith(field
, "__"))
2257 for (p
= field
; *p
; p
++) {
2262 if (*p
>= 'A' && *p
<= 'Z')
2265 if (*p
>= '0' && *p
<= '9')
2274 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2277 size_t field_length
;
2281 assert_return(j
, -EINVAL
);
2282 assert_return(!journal_pid_changed(j
), -ECHILD
);
2283 assert_return(field
, -EINVAL
);
2284 assert_return(data
, -EINVAL
);
2285 assert_return(size
, -EINVAL
);
2286 assert_return(field_is_valid(field
), -EINVAL
);
2288 f
= j
->current_file
;
2290 return -EADDRNOTAVAIL
;
2292 if (f
->current_offset
<= 0)
2293 return -EADDRNOTAVAIL
;
2295 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2299 field_length
= strlen(field
);
2301 n
= journal_file_entry_n_items(o
);
2302 for (i
= 0; i
< n
; i
++) {
2308 p
= le64toh(o
->entry
.items
[i
].object_offset
);
2309 le_hash
= o
->entry
.items
[i
].hash
;
2310 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2314 if (le_hash
!= o
->data
.hash
)
2317 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2319 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2321 #if HAVE_COMPRESSION
2322 r
= decompress_startswith(compression
,
2324 &f
->compress_buffer
,
2325 field
, field_length
, '=');
2327 log_debug_errno(r
, "Cannot decompress %s object of length %"PRIu64
" at offset "OFSfmt
": %m",
2328 object_compressed_to_string(compression
), l
, p
);
2333 r
= decompress_blob(compression
,
2335 &f
->compress_buffer
, &rsize
,
2340 *data
= f
->compress_buffer
;
2341 *size
= (size_t) rsize
;
2346 return -EPROTONOSUPPORT
;
2348 } else if (l
>= field_length
+1 &&
2349 memcmp(o
->data
.payload
, field
, field_length
) == 0 &&
2350 o
->data
.payload
[field_length
] == '=') {
2354 if ((uint64_t) t
!= l
)
2357 *data
= o
->data
.payload
;
2363 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2371 static int return_data(sd_journal
*j
, JournalFile
*f
, Object
*o
, const void **data
, size_t *size
) {
2376 l
= le64toh(READ_NOW(o
->object
.size
));
2377 if (l
< offsetof(Object
, data
.payload
))
2379 l
-= offsetof(Object
, data
.payload
);
2382 /* We can't read objects larger than 4G on a 32bit machine */
2383 if ((uint64_t) t
!= l
)
2386 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2388 #if HAVE_COMPRESSION
2392 r
= decompress_blob(
2395 &f
->compress_buffer
, &rsize
,
2400 *data
= f
->compress_buffer
;
2401 *size
= (size_t) rsize
;
2403 return -EPROTONOSUPPORT
;
2406 *data
= o
->data
.payload
;
2413 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2420 assert_return(j
, -EINVAL
);
2421 assert_return(!journal_pid_changed(j
), -ECHILD
);
2422 assert_return(data
, -EINVAL
);
2423 assert_return(size
, -EINVAL
);
2425 f
= j
->current_file
;
2427 return -EADDRNOTAVAIL
;
2429 if (f
->current_offset
<= 0)
2430 return -EADDRNOTAVAIL
;
2432 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2436 n
= journal_file_entry_n_items(o
);
2437 if (j
->current_field
>= n
)
2440 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2441 le_hash
= o
->entry
.items
[j
->current_field
].hash
;
2442 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2446 if (le_hash
!= o
->data
.hash
)
2449 r
= return_data(j
, f
, o
, data
, size
);
2458 _public_
int sd_journal_enumerate_available_data(sd_journal
*j
, const void **data
, size_t *size
) {
2462 r
= sd_journal_enumerate_data(j
, data
, size
);
2465 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r
))
2467 j
->current_field
++; /* Try with the next field */
2471 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2475 j
->current_field
= 0;
2478 static int reiterate_all_paths(sd_journal
*j
) {
2481 if (j
->no_new_files
)
2482 return add_current_paths(j
);
2484 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2485 return add_search_paths(j
);
2487 if (j
->toplevel_fd
>= 0)
2488 return add_root_directory(j
, NULL
, false);
2491 return add_root_directory(j
, j
->path
, true);
2493 return add_search_paths(j
);
2496 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2499 assert_return(j
, -EINVAL
);
2500 assert_return(!journal_pid_changed(j
), -ECHILD
);
2503 return -EMEDIUMTYPE
;
2505 if (j
->inotify_fd
>= 0)
2506 return j
->inotify_fd
;
2508 r
= allocate_inotify(j
);
2512 log_debug("Reiterating files to get inotify watches established.");
2514 /* Iterate through all dirs again, to add them to the inotify */
2515 r
= reiterate_all_paths(j
);
2519 return j
->inotify_fd
;
2522 _public_
int sd_journal_get_events(sd_journal
*j
) {
2525 assert_return(j
, -EINVAL
);
2526 assert_return(!journal_pid_changed(j
), -ECHILD
);
2528 fd
= sd_journal_get_fd(j
);
2535 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2538 assert_return(j
, -EINVAL
);
2539 assert_return(!journal_pid_changed(j
), -ECHILD
);
2540 assert_return(timeout_usec
, -EINVAL
);
2542 fd
= sd_journal_get_fd(j
);
2546 if (!j
->on_network
) {
2547 *timeout_usec
= UINT64_MAX
;
2551 /* If we are on the network we need to regularly check for
2552 * changes manually */
2554 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2558 static void process_q_overflow(sd_journal
*j
) {
2564 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2565 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2566 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2567 * are subject for unloading. */
2569 log_debug("Inotify queue overrun, reiterating everything.");
2572 (void) reiterate_all_paths(j
);
2574 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2576 if (f
->last_seen_generation
== j
->generation
)
2579 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2580 remove_file_real(j
, f
);
2583 HASHMAP_FOREACH(m
, j
->directories_by_path
) {
2585 if (m
->last_seen_generation
== j
->generation
)
2588 if (m
->is_root
) /* Never GC root directories */
2591 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2592 remove_directory(j
, m
);
2595 log_debug("Reiteration complete.");
2598 static void process_inotify_event(sd_journal
*j
, const struct inotify_event
*e
) {
2604 if (e
->mask
& IN_Q_OVERFLOW
) {
2605 process_q_overflow(j
);
2609 /* Is this a subdirectory we watch? */
2610 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2612 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2613 (endswith(e
->name
, ".journal") ||
2614 endswith(e
->name
, ".journal~"))) {
2616 /* Event for a journal file */
2618 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2619 (void) add_file_by_name(j
, d
->path
, e
->name
);
2620 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2621 remove_file_by_name(j
, d
->path
, e
->name
);
2623 } else if (!d
->is_root
&& e
->len
== 0) {
2625 /* Event for a subdirectory */
2627 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2628 remove_directory(j
, d
);
2630 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2632 /* Event for root directory */
2634 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2635 (void) add_directory(j
, d
->path
, e
->name
);
2641 if (e
->mask
& IN_IGNORED
)
2644 log_debug("Unexpected inotify event.");
2647 static int determine_change(sd_journal
*j
) {
2652 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2653 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2655 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2658 _public_
int sd_journal_process(sd_journal
*j
) {
2659 bool got_something
= false;
2661 assert_return(j
, -EINVAL
);
2662 assert_return(!journal_pid_changed(j
), -ECHILD
);
2664 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
2667 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2668 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2671 union inotify_event_buffer buffer
;
2672 struct inotify_event
*e
;
2675 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2677 if (IN_SET(errno
, EAGAIN
, EINTR
))
2678 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2683 got_something
= true;
2685 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2686 process_inotify_event(j
, e
);
2690 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2694 assert_return(j
, -EINVAL
);
2695 assert_return(!journal_pid_changed(j
), -ECHILD
);
2697 if (j
->inotify_fd
< 0) {
2700 /* This is the first invocation, hence create the
2702 r
= sd_journal_get_fd(j
);
2706 /* Server might have done some vacuuming while we weren't watching.
2707 Get rid of the deleted files now so they don't stay around indefinitely. */
2708 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2709 r
= journal_file_fstat(f
);
2711 remove_file_real(j
, f
);
2713 log_debug_errno(r
,"Failed to fstat() journal file '%s' : %m", f
->path
);
2718 /* The journal might have changed since the context
2719 * object was created and we weren't watching before,
2720 * hence don't wait for anything, and return
2722 return determine_change(j
);
2725 r
= sd_journal_get_timeout(j
, &t
);
2729 if (t
!= UINT64_MAX
) {
2730 t
= usec_sub_unsigned(t
, now(CLOCK_MONOTONIC
));
2732 if (timeout_usec
== UINT64_MAX
|| timeout_usec
> t
)
2737 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2738 } while (r
== -EINTR
);
2743 return sd_journal_process(j
);
2746 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2749 uint64_t fmin
= 0, tmax
= 0;
2752 assert_return(j
, -EINVAL
);
2753 assert_return(!journal_pid_changed(j
), -ECHILD
);
2754 assert_return(from
|| to
, -EINVAL
);
2755 assert_return(from
!= to
, -EINVAL
);
2757 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2760 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2773 fmin
= MIN(fr
, fmin
);
2774 tmax
= MAX(t
, tmax
);
2783 return first
? 0 : 1;
2786 _public_
int sd_journal_get_cutoff_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t *from
, uint64_t *to
) {
2791 assert_return(j
, -EINVAL
);
2792 assert_return(!journal_pid_changed(j
), -ECHILD
);
2793 assert_return(from
|| to
, -EINVAL
);
2794 assert_return(from
!= to
, -EINVAL
);
2796 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2799 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &fr
, &t
);
2809 *from
= MIN(fr
, *from
);
2824 void journal_print_header(sd_journal
*j
) {
2826 bool newline
= false;
2830 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2836 journal_file_print_header(f
);
2840 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *bytes
) {
2844 assert_return(j
, -EINVAL
);
2845 assert_return(!journal_pid_changed(j
), -ECHILD
);
2846 assert_return(bytes
, -EINVAL
);
2848 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2851 if (fstat(f
->fd
, &st
) < 0)
2854 sum
+= (uint64_t) st
.st_blocks
* 512ULL;
2861 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2864 assert_return(j
, -EINVAL
);
2865 assert_return(!journal_pid_changed(j
), -ECHILD
);
2866 assert_return(!isempty(field
), -EINVAL
);
2867 assert_return(field_is_valid(field
), -EINVAL
);
2873 free(j
->unique_field
);
2874 j
->unique_field
= f
;
2875 j
->unique_file
= NULL
;
2876 j
->unique_offset
= 0;
2877 j
->unique_file_lost
= false;
2882 _public_
int sd_journal_enumerate_unique(sd_journal
*j
, const void **data
, size_t *l
) {
2885 assert_return(j
, -EINVAL
);
2886 assert_return(!journal_pid_changed(j
), -ECHILD
);
2887 assert_return(data
, -EINVAL
);
2888 assert_return(l
, -EINVAL
);
2889 assert_return(j
->unique_field
, -EINVAL
);
2891 k
= strlen(j
->unique_field
);
2893 if (!j
->unique_file
) {
2894 if (j
->unique_file_lost
)
2897 j
->unique_file
= ordered_hashmap_first(j
->files
);
2898 if (!j
->unique_file
)
2901 j
->unique_offset
= 0;
2912 /* Proceed to next data object in the field's linked list */
2913 if (j
->unique_offset
== 0) {
2914 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2918 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2920 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2924 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2927 /* We reached the end of the list? Then start again, with the next file */
2928 if (j
->unique_offset
== 0) {
2929 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2930 if (!j
->unique_file
)
2936 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2937 * instead, so that we can look at this data object at the same
2938 * time as one on another file */
2939 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2943 /* Let's do the type check by hand, since we used 0 context above. */
2944 if (o
->object
.type
!= OBJECT_DATA
)
2945 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2946 "%s:offset " OFSfmt
": object has type %d, expected %d",
2947 j
->unique_file
->path
,
2949 o
->object
.type
, OBJECT_DATA
);
2951 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2955 /* Check if we have at least the field name and "=". */
2957 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2958 "%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2959 j
->unique_file
->path
,
2960 j
->unique_offset
, ol
, k
+ 1);
2962 if (memcmp(odata
, j
->unique_field
, k
) || ((const char*) odata
)[k
] != '=')
2963 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2964 "%s:offset " OFSfmt
": object does not start with \"%s=\"",
2965 j
->unique_file
->path
,
2969 /* OK, now let's see if we already returned this data
2970 * object by checking if it exists in the earlier
2971 * traversed files. */
2973 ORDERED_HASHMAP_FOREACH(of
, j
->files
) {
2974 if (of
== j
->unique_file
)
2977 /* Skip this file it didn't have any fields indexed */
2978 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2981 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
2993 r
= return_data(j
, j
->unique_file
, o
, data
, l
);
3001 _public_
int sd_journal_enumerate_available_unique(sd_journal
*j
, const void **data
, size_t *size
) {
3005 r
= sd_journal_enumerate_unique(j
, data
, size
);
3008 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r
))
3010 /* Try with the next field. sd_journal_enumerate_unique() modifies state, so on the next try
3011 * we will access the next field. */
3015 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
3019 j
->unique_file
= NULL
;
3020 j
->unique_offset
= 0;
3021 j
->unique_file_lost
= false;
3024 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
3027 assert_return(j
, -EINVAL
);
3028 assert_return(!journal_pid_changed(j
), -ECHILD
);
3029 assert_return(field
, -EINVAL
);
3031 if (!j
->fields_file
) {
3032 if (j
->fields_file_lost
)
3035 j
->fields_file
= ordered_hashmap_first(j
->files
);
3036 if (!j
->fields_file
)
3039 j
->fields_hash_table_index
= 0;
3040 j
->fields_offset
= 0;
3044 JournalFile
*f
, *of
;
3052 if (j
->fields_offset
== 0) {
3055 /* We are not yet positioned at any field. Let's pick the first one */
3056 r
= journal_file_map_field_hash_table(f
);
3060 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
3062 if (j
->fields_hash_table_index
>= m
) {
3063 /* Reached the end of the hash table, go to the next file. */
3068 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
3070 if (j
->fields_offset
!= 0)
3073 /* Empty hash table bucket, go to next one */
3074 j
->fields_hash_table_index
++;
3078 /* Proceed with next file */
3079 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
3080 if (!j
->fields_file
) {
3085 j
->fields_offset
= 0;
3086 j
->fields_hash_table_index
= 0;
3091 /* We are already positioned at a field. If so, let's figure out the next field from it */
3093 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
3097 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
3098 if (j
->fields_offset
== 0) {
3099 /* Reached the end of the hash table chain */
3100 j
->fields_hash_table_index
++;
3105 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
3106 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
3110 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
3111 if (o
->object
.type
!= OBJECT_FIELD
)
3112 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3113 "%s:offset " OFSfmt
": object has type %i, expected %i",
3114 f
->path
, j
->fields_offset
,
3115 o
->object
.type
, OBJECT_FIELD
);
3117 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
3119 /* Let's see if we already returned this field name before. */
3121 ORDERED_HASHMAP_FOREACH(of
, j
->files
) {
3125 /* Skip this file it didn't have any fields indexed */
3126 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3129 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
, le64toh(o
->field
.hash
), NULL
, NULL
);
3141 /* Check if this is really a valid string containing no NUL byte */
3142 if (memchr(o
->field
.payload
, 0, sz
))
3145 if (sz
> j
->data_threshold
)
3146 sz
= j
->data_threshold
;
3148 if (!GREEDY_REALLOC(j
->fields_buffer
, sz
+ 1))
3151 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3152 j
->fields_buffer
[sz
] = 0;
3154 if (!field_is_valid(j
->fields_buffer
))
3157 *field
= j
->fields_buffer
;
3162 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3166 j
->fields_file
= NULL
;
3167 j
->fields_hash_table_index
= 0;
3168 j
->fields_offset
= 0;
3169 j
->fields_file_lost
= false;
3172 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3173 assert_return(j
, -EINVAL
);
3174 assert_return(!journal_pid_changed(j
), -ECHILD
);
3176 return !j
->on_network
;
3179 static char *lookup_field(const char *field
, void *userdata
) {
3180 sd_journal
*j
= userdata
;
3188 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3190 size
> REPLACE_VAR_MAX
)
3191 return strdup(field
);
3193 d
= strlen(field
) + 1;
3195 return strndup((const char*) data
+ d
, size
- d
);
3198 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3202 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3206 assert_return(j
, -EINVAL
);
3207 assert_return(!journal_pid_changed(j
), -ECHILD
);
3208 assert_return(ret
, -EINVAL
);
3210 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3214 cid
= strndup((const char*) data
+ 11, size
- 11);
3218 r
= sd_id128_from_string(cid
, &id
);
3222 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
3226 t
= replace_var(text
, lookup_field
, j
);
3234 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3235 assert_return(ret
, -EINVAL
);
3237 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3240 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3241 assert_return(j
, -EINVAL
);
3242 assert_return(!journal_pid_changed(j
), -ECHILD
);
3244 j
->data_threshold
= sz
;
3248 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3249 assert_return(j
, -EINVAL
);
3250 assert_return(!journal_pid_changed(j
), -ECHILD
);
3251 assert_return(sz
, -EINVAL
);
3253 *sz
= j
->data_threshold
;
3257 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3258 assert_return(j
, -EINVAL
);
3260 return j
->has_runtime_files
;
3263 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3264 assert_return(j
, -EINVAL
);
3266 return j
->has_persistent_files
;