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"
28 #include "inotify-util.h"
30 #include "journal-def.h"
31 #include "journal-file.h"
32 #include "journal-internal.h"
35 #include "nulstr-util.h"
36 #include "path-util.h"
37 #include "process-util.h"
38 #include "replace-var.h"
39 #include "stat-util.h"
40 #include "stdio-util.h"
41 #include "string-util.h"
43 #include "syslog-util.h"
45 #define JOURNAL_FILES_MAX 7168
47 #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
49 /* The maximum size of variable values we'll expand in catalog entries. We bind this to PATH_MAX for now, as
50 * we want to be able to show all officially valid paths at least */
51 #define REPLACE_VAR_MAX PATH_MAX
53 #define DEFAULT_DATA_THRESHOLD (64*1024)
55 static void remove_file_real(sd_journal
*j
, JournalFile
*f
);
57 static bool journal_pid_changed(sd_journal
*j
) {
60 /* We don't support people creating a journal object and
61 * keeping it around over a fork(). Let's complain. */
63 return j
->original_pid
!= getpid_cached();
66 static int journal_put_error(sd_journal
*j
, int r
, const char *path
) {
67 _cleanup_free_
char *copy
= NULL
;
70 /* Memorize an error we encountered, and store which
71 * file/directory it was generated from. Note that we store
72 * only *one* path per error code, as the error code is the
73 * key into the hashmap, and the path is the value. This means
74 * we keep track only of all error kinds, but not of all error
75 * locations. This has the benefit that the hashmap cannot
78 * We return an error here only if we didn't manage to
79 * memorize the real error. */
90 k
= hashmap_ensure_put(&j
->errors
, NULL
, INT_TO_PTR(r
), copy
);
102 static void detach_location(sd_journal
*j
) {
107 j
->current_file
= NULL
;
108 j
->current_field
= 0;
110 ORDERED_HASHMAP_FOREACH(f
, j
->files
)
111 journal_file_reset_location(f
);
114 static void init_location(Location
*l
, LocationType type
, JournalFile
*f
, Object
*o
) {
116 assert(IN_SET(type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
121 .seqnum
= le64toh(o
->entry
.seqnum
),
122 .seqnum_id
= f
->header
->seqnum_id
,
123 .realtime
= le64toh(o
->entry
.realtime
),
124 .monotonic
= le64toh(o
->entry
.monotonic
),
125 .boot_id
= o
->entry
.boot_id
,
126 .xor_hash
= le64toh(o
->entry
.xor_hash
),
128 .realtime_set
= true,
129 .monotonic_set
= true,
130 .xor_hash_set
= true,
134 static void set_location(sd_journal
*j
, JournalFile
*f
, Object
*o
) {
139 init_location(&j
->current_location
, LOCATION_DISCRETE
, f
, o
);
142 j
->current_field
= 0;
144 /* Let f know its candidate entry was picked. */
145 assert(f
->location_type
== LOCATION_SEEK
);
146 f
->location_type
= LOCATION_DISCRETE
;
149 static int match_is_valid(const void *data
, size_t size
) {
155 if (((char*) data
)[0] == '_' && ((char*) data
)[1] == '_')
158 const char *b
= data
;
159 for (const char *p
= b
; p
< b
+ size
; p
++) {
167 if (*p
>= 'A' && *p
<= 'Z')
170 if (ascii_isdigit(*p
))
179 static bool same_field(const void *_a
, size_t s
, const void *_b
, size_t t
) {
180 const uint8_t *a
= _a
, *b
= _b
;
182 for (size_t j
= 0; j
< s
&& j
< t
; j
++) {
191 assert_not_reached();
194 static Match
*match_new(Match
*p
, MatchType t
) {
207 LIST_PREPEND(matches
, p
->matches
, m
);
212 static Match
*match_free(Match
*m
) {
216 match_free(m
->matches
);
219 LIST_REMOVE(matches
, m
->parent
->matches
, m
);
225 static Match
*match_free_if_empty(Match
*m
) {
226 if (!m
|| m
->matches
)
229 return match_free(m
);
232 _public_
int sd_journal_add_match(sd_journal
*j
, const void *data
, size_t size
) {
233 Match
*add_here
= NULL
, *m
= NULL
;
236 assert_return(j
, -EINVAL
);
237 assert_return(!journal_pid_changed(j
), -ECHILD
);
238 assert_return(data
, -EINVAL
);
243 assert_return(match_is_valid(data
, size
), -EINVAL
);
249 * level 4: concrete matches */
252 j
->level0
= match_new(NULL
, MATCH_AND_TERM
);
258 j
->level1
= match_new(j
->level0
, MATCH_OR_TERM
);
264 j
->level2
= match_new(j
->level1
, MATCH_AND_TERM
);
269 assert(j
->level0
->type
== MATCH_AND_TERM
);
270 assert(j
->level1
->type
== MATCH_OR_TERM
);
271 assert(j
->level2
->type
== MATCH_AND_TERM
);
273 /* Old-style Jenkins (unkeyed) hashing only here. We do not cover new-style siphash (keyed) hashing
274 * here, since it's different for each file, and thus can't be pre-calculated in the Match object. */
275 hash
= jenkins_hash64(data
, size
);
277 LIST_FOREACH(matches
, l3
, j
->level2
->matches
) {
278 assert(l3
->type
== MATCH_OR_TERM
);
280 LIST_FOREACH(matches
, l4
, l3
->matches
) {
281 assert(l4
->type
== MATCH_DISCRETE
);
283 /* Exactly the same match already? Then ignore
285 if (l4
->hash
== hash
&&
287 memcmp(l4
->data
, data
, size
) == 0)
290 /* Same field? Then let's add this to this OR term */
291 if (same_field(data
, size
, l4
->data
, l4
->size
)) {
302 add_here
= match_new(j
->level2
, MATCH_OR_TERM
);
307 m
= match_new(add_here
, MATCH_DISCRETE
);
313 m
->data
= memdup(data
, size
);
323 match_free_if_empty(add_here
);
324 j
->level2
= match_free_if_empty(j
->level2
);
325 j
->level1
= match_free_if_empty(j
->level1
);
326 j
->level0
= match_free_if_empty(j
->level0
);
331 _public_
int sd_journal_add_conjunction(sd_journal
*j
) {
332 assert_return(j
, -EINVAL
);
333 assert_return(!journal_pid_changed(j
), -ECHILD
);
341 if (!j
->level1
->matches
)
350 _public_
int sd_journal_add_disjunction(sd_journal
*j
) {
351 assert_return(j
, -EINVAL
);
352 assert_return(!journal_pid_changed(j
), -ECHILD
);
363 if (!j
->level2
->matches
)
370 static char *match_make_string(Match
*m
) {
372 bool enclose
= false;
375 return strdup("none");
377 if (m
->type
== MATCH_DISCRETE
)
378 return cescape_length(m
->data
, m
->size
);
380 LIST_FOREACH(matches
, i
, m
->matches
) {
383 t
= match_make_string(i
);
388 k
= strjoin(p
, m
->type
== MATCH_OR_TERM
? " OR " : " AND ", t
);
403 r
= strjoin("(", p
, ")");
411 char *journal_make_match_string(sd_journal
*j
) {
414 return match_make_string(j
->level0
);
417 _public_
void sd_journal_flush_matches(sd_journal
*j
) {
422 match_free(j
->level0
);
424 j
->level0
= j
->level1
= j
->level2
= NULL
;
429 _pure_
static int compare_with_location(const JournalFile
*f
, const Location
*l
, const JournalFile
*current_file
) {
434 assert(f
->location_type
== LOCATION_SEEK
);
435 assert(IN_SET(l
->type
, LOCATION_DISCRETE
, LOCATION_SEEK
));
437 if (l
->monotonic_set
&&
438 sd_id128_equal(f
->current_boot_id
, l
->boot_id
) &&
440 f
->current_realtime
== l
->realtime
&&
442 f
->current_xor_hash
== l
->xor_hash
&&
444 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
) &&
445 f
->current_seqnum
== l
->seqnum
&&
450 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
)) {
452 r
= CMP(f
->current_seqnum
, l
->seqnum
);
457 if (l
->monotonic_set
&&
458 sd_id128_equal(f
->current_boot_id
, l
->boot_id
)) {
460 r
= CMP(f
->current_monotonic
, l
->monotonic
);
465 if (l
->realtime_set
) {
467 r
= CMP(f
->current_realtime
, l
->realtime
);
472 if (l
->xor_hash_set
) {
474 r
= CMP(f
->current_xor_hash
, l
->xor_hash
);
482 static int next_for_match(
486 uint64_t after_offset
,
487 direction_t direction
,
498 if (m
->type
== MATCH_DISCRETE
) {
502 /* If the keyed hash logic is used, we need to calculate the hash fresh per file. Otherwise
503 * we can use what we pre-calculated. */
504 if (JOURNAL_HEADER_KEYED_HASH(f
->header
))
505 hash
= journal_file_hash_data(f
, m
->data
, m
->size
);
509 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, hash
, &d
, NULL
);
513 return journal_file_move_to_entry_by_offset_for_data(f
, d
, after_offset
, direction
, ret
, offset
);
515 } else if (m
->type
== MATCH_OR_TERM
) {
517 /* Find the earliest match beyond after_offset */
519 LIST_FOREACH(matches
, i
, m
->matches
) {
522 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
526 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
534 } else if (m
->type
== MATCH_AND_TERM
) {
537 /* Always jump to the next matching entry and repeat
538 * this until we find an offset that matches for all
544 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
548 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
549 last_moved
= m
->matches
;
551 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
554 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
558 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
559 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
569 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, ret
);
580 static int find_location_for_match(
584 direction_t direction
,
594 if (m
->type
== MATCH_DISCRETE
) {
598 if (JOURNAL_HEADER_KEYED_HASH(f
->header
))
599 hash
= journal_file_hash_data(f
, m
->data
, m
->size
);
603 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, hash
, &d
, &dp
);
607 /* FIXME: missing: find by monotonic */
609 if (j
->current_location
.type
== LOCATION_HEAD
)
610 return direction
== DIRECTION_DOWN
? journal_file_next_entry_for_data(f
, d
, DIRECTION_DOWN
, ret
, offset
) : 0;
611 if (j
->current_location
.type
== LOCATION_TAIL
)
612 return direction
== DIRECTION_UP
? journal_file_next_entry_for_data(f
, d
, DIRECTION_UP
, ret
, offset
) : 0;
613 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
614 return journal_file_move_to_entry_by_seqnum_for_data(f
, d
, j
->current_location
.seqnum
, direction
, ret
, offset
);
615 if (j
->current_location
.monotonic_set
) {
616 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, d
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
620 /* The data object might have been invalidated. */
621 r
= journal_file_move_to_object(f
, OBJECT_DATA
, dp
, &d
);
625 if (j
->current_location
.realtime_set
)
626 return journal_file_move_to_entry_by_realtime_for_data(f
, d
, j
->current_location
.realtime
, direction
, ret
, offset
);
628 return journal_file_next_entry_for_data(f
, d
, direction
, ret
, offset
);
630 } else if (m
->type
== MATCH_OR_TERM
) {
633 /* Find the earliest match */
635 LIST_FOREACH(matches
, i
, m
->matches
) {
638 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
642 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
651 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, ret
);
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 direction
== DIRECTION_DOWN
? journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
) : 0;
706 if (j
->current_location
.type
== LOCATION_TAIL
)
707 return direction
== DIRECTION_UP
? journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
) : 0;
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 (unsigned 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
) {
924 assert_return(j
, -EINVAL
);
925 assert_return(!journal_pid_changed(j
), -ECHILD
);
926 assert_return(cursor
, -EINVAL
);
928 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
929 return -EADDRNOTAVAIL
;
931 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
936 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
937 SD_ID128_TO_STRING(j
->current_file
->header
->seqnum_id
), le64toh(o
->entry
.seqnum
),
938 SD_ID128_TO_STRING(o
->entry
.boot_id
), le64toh(o
->entry
.monotonic
),
939 le64toh(o
->entry
.realtime
),
940 le64toh(o
->entry
.xor_hash
)) < 0)
946 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
947 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
948 bool seqnum_id_set
= false,
951 monotonic_set
= false,
952 realtime_set
= false,
953 xor_hash_set
= false;
954 sd_id128_t seqnum_id
, boot_id
;
957 assert_return(j
, -EINVAL
);
958 assert_return(!journal_pid_changed(j
), -ECHILD
);
959 assert_return(!isempty(cursor
), -EINVAL
);
961 for (const char *p
= cursor
;;) {
962 _cleanup_free_
char *word
= NULL
;
964 r
= extract_first_word(&p
, &word
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
970 if (word
[0] == '\0' || word
[1] != '=')
975 seqnum_id_set
= true;
976 r
= sd_id128_from_string(word
+ 2, &seqnum_id
);
983 if (sscanf(word
+ 2, "%llx", &seqnum
) != 1)
989 r
= sd_id128_from_string(word
+ 2, &boot_id
);
995 monotonic_set
= true;
996 if (sscanf(word
+ 2, "%llx", &monotonic
) != 1)
1001 realtime_set
= true;
1002 if (sscanf(word
+ 2, "%llx", &realtime
) != 1)
1007 xor_hash_set
= true;
1008 if (sscanf(word
+ 2, "%llx", &xor_hash
) != 1)
1014 if ((!seqnum_set
|| !seqnum_id_set
) &&
1015 (!monotonic_set
|| !boot_id_set
) &&
1020 j
->current_location
= (Location
) {
1021 .type
= LOCATION_SEEK
,
1025 j
->current_location
.realtime
= (uint64_t) realtime
;
1026 j
->current_location
.realtime_set
= true;
1029 if (seqnum_set
&& seqnum_id_set
) {
1030 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1031 j
->current_location
.seqnum_id
= seqnum_id
;
1032 j
->current_location
.seqnum_set
= true;
1035 if (monotonic_set
&& boot_id_set
) {
1036 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1037 j
->current_location
.boot_id
= boot_id
;
1038 j
->current_location
.monotonic_set
= true;
1042 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1043 j
->current_location
.xor_hash_set
= true;
1049 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1053 assert_return(j
, -EINVAL
);
1054 assert_return(!journal_pid_changed(j
), -ECHILD
);
1055 assert_return(!isempty(cursor
), -EINVAL
);
1057 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1058 return -EADDRNOTAVAIL
;
1060 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1065 _cleanup_free_
char *item
= NULL
;
1066 unsigned long long ll
;
1070 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1077 if (strlen(item
) < 2 || item
[1] != '=')
1083 k
= sd_id128_from_string(item
+2, &id
);
1086 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1091 if (sscanf(item
+2, "%llx", &ll
) != 1)
1093 if (ll
!= le64toh(o
->entry
.seqnum
))
1098 k
= sd_id128_from_string(item
+2, &id
);
1101 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1106 if (sscanf(item
+2, "%llx", &ll
) != 1)
1108 if (ll
!= le64toh(o
->entry
.monotonic
))
1113 if (sscanf(item
+2, "%llx", &ll
) != 1)
1115 if (ll
!= le64toh(o
->entry
.realtime
))
1120 if (sscanf(item
+2, "%llx", &ll
) != 1)
1122 if (ll
!= le64toh(o
->entry
.xor_hash
))
1131 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t usec
) {
1132 assert_return(j
, -EINVAL
);
1133 assert_return(!journal_pid_changed(j
), -ECHILD
);
1137 j
->current_location
= (Location
) {
1138 .type
= LOCATION_SEEK
,
1141 .monotonic_set
= true,
1147 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1148 assert_return(j
, -EINVAL
);
1149 assert_return(!journal_pid_changed(j
), -ECHILD
);
1153 j
->current_location
= (Location
) {
1154 .type
= LOCATION_SEEK
,
1156 .realtime_set
= true,
1162 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1163 assert_return(j
, -EINVAL
);
1164 assert_return(!journal_pid_changed(j
), -ECHILD
);
1168 j
->current_location
= (Location
) {
1169 .type
= LOCATION_HEAD
,
1175 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1176 assert_return(j
, -EINVAL
);
1177 assert_return(!journal_pid_changed(j
), -ECHILD
);
1181 j
->current_location
= (Location
) {
1182 .type
= LOCATION_TAIL
,
1188 static void check_network(sd_journal
*j
, int fd
) {
1194 j
->on_network
= fd_is_network_fs(fd
);
1197 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1198 const char *full
, *tilded
, *atted
;
1200 full
= strjoina(prefix
, ".journal");
1201 tilded
= strjoina(full
, "~");
1202 atted
= strjoina(prefix
, "@");
1204 return STR_IN_SET(filename
, full
, tilded
) ||
1205 startswith(filename
, atted
);
1208 static bool file_type_wanted(int flags
, const char *filename
) {
1211 if (!endswith(filename
, ".journal") && !endswith(filename
, ".journal~"))
1214 /* no flags set → every type is OK */
1215 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1218 if (flags
& SD_JOURNAL_SYSTEM
&& file_has_type_prefix("system", filename
))
1221 if (flags
& SD_JOURNAL_CURRENT_USER
) {
1222 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1224 xsprintf(prefix
, "user-"UID_FMT
, getuid());
1226 if (file_has_type_prefix(prefix
, filename
))
1233 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1238 if (j
->toplevel_fd
>= 0)
1241 return path_startswith(path
, prefix
);
1244 static void track_file_disposition(sd_journal
*j
, JournalFile
*f
) {
1248 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1249 j
->has_runtime_files
= true;
1250 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1251 j
->has_persistent_files
= true;
1254 static const char *skip_slash(const char *p
) {
1265 static int add_any_file(
1270 _cleanup_close_
int our_fd
= -1;
1276 assert(fd
>= 0 || path
);
1279 assert(path
); /* For gcc. */
1280 if (j
->toplevel_fd
>= 0)
1281 /* If there's a top-level fd defined make the path relative, explicitly, since otherwise
1282 * openat() ignores the first argument. */
1284 fd
= our_fd
= openat(j
->toplevel_fd
, skip_slash(path
), O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1286 fd
= our_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
1288 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1292 r
= fd_nonblock(fd
, false);
1294 r
= log_debug_errno(errno
, "Failed to turn off O_NONBLOCK for %s: %m", path
);
1299 if (fstat(fd
, &st
) < 0) {
1300 r
= log_debug_errno(errno
, "Failed to fstat %s: %m", path
?: "fd");
1304 r
= stat_verify_regular(&st
);
1306 log_debug_errno(r
, "Refusing to open %s: %m", path
?: "fd");
1311 f
= ordered_hashmap_get(j
->files
, path
);
1313 if (stat_inode_same(&f
->last_stat
, &st
)) {
1314 /* We already track this file, under the same path and with the same
1315 * device/inode numbers, it's hence really the same. Mark this file as seen
1316 * in this generation. This is used to GC old files in process_q_overflow()
1317 * to detect journal files that are still there and discern them from those
1318 * which are gone. */
1320 f
->last_seen_generation
= j
->generation
;
1324 /* So we tracked a file under this name, but it has a different inode/device. In that
1325 * case, it got replaced (probably due to rotation?), let's drop it hence from our
1327 remove_file_real(j
, f
);
1332 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1333 r
= log_debug_errno(SYNTHETIC_ERRNO(ETOOMANYREFS
),
1334 "Too many open journal files, not adding %s.", path
?: "fd");
1338 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, 0, 0, NULL
, j
->mmap
, NULL
, &f
);
1340 log_debug_errno(r
, "Failed to open journal file %s: %m", path
?: "from fd");
1344 /* journal_file_dump(f); */
1346 /* journal_file_open() generates an replacement fname if necessary, so we can use f->path. */
1347 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1349 f
->close_fd
= false; /* Make sure journal_file_close() doesn't close the caller's fd
1350 * (or our own). The caller or we will do that ourselves. */
1351 (void) journal_file_close(f
);
1355 TAKE_FD(our_fd
); /* 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
);
1369 (void) journal_put_error(j
, r
, path
); /* path==NULL is OK. */
1373 static int add_file_by_name(
1376 const char *filename
) {
1384 if (j
->no_new_files
)
1387 if (!file_type_wanted(j
->flags
, filename
))
1390 path
= prefix_roota(prefix
, filename
);
1391 return add_any_file(j
, -1, path
);
1394 static void remove_file_by_name(
1397 const char *filename
) {
1406 path
= prefix_roota(prefix
, filename
);
1407 f
= ordered_hashmap_get(j
->files
, path
);
1411 remove_file_real(j
, f
);
1414 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1418 (void) ordered_hashmap_remove(j
->files
, f
->path
);
1420 log_debug("File %s removed.", f
->path
);
1422 if (j
->current_file
== f
) {
1423 j
->current_file
= NULL
;
1424 j
->current_field
= 0;
1427 if (j
->unique_file
== f
) {
1428 /* Jump to the next unique_file or NULL if that one was last */
1429 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1430 j
->unique_offset
= 0;
1431 if (!j
->unique_file
)
1432 j
->unique_file_lost
= true;
1435 if (j
->fields_file
== f
) {
1436 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1437 j
->fields_offset
= 0;
1438 if (!j
->fields_file
)
1439 j
->fields_file_lost
= true;
1442 (void) journal_file_close(f
);
1444 j
->current_invalidate_counter
++;
1447 static int dirname_is_machine_id(const char *fn
) {
1448 sd_id128_t id
, machine
;
1452 /* Returns true if the specified directory name matches the local machine ID */
1454 r
= sd_id128_get_machine(&machine
);
1458 e
= strchr(fn
, '.');
1462 /* Looks like it has a namespace suffix. Verify that. */
1463 if (!log_namespace_name_valid(e
+ 1))
1466 k
= strndupa_safe(fn
, e
- fn
);
1467 r
= sd_id128_from_string(k
, &id
);
1469 r
= sd_id128_from_string(fn
, &id
);
1473 return sd_id128_equal(id
, machine
);
1476 static int dirname_has_namespace(const char *fn
, const char *namespace) {
1479 /* Returns true if the specified directory name matches the specified namespace */
1481 e
= strchr(fn
, '.');
1488 if (!streq(e
+ 1, namespace))
1491 k
= strndupa_safe(fn
, e
- fn
);
1492 return id128_is_valid(k
);
1498 return id128_is_valid(fn
);
1501 static bool dirent_is_journal_file(const struct dirent
*de
) {
1504 /* Returns true if the specified directory entry looks like a journal file we might be interested in */
1506 if (!IN_SET(de
->d_type
, DT_REG
, DT_LNK
, DT_UNKNOWN
))
1509 return endswith(de
->d_name
, ".journal") ||
1510 endswith(de
->d_name
, ".journal~");
1513 static bool dirent_is_journal_subdir(const struct dirent
*de
) {
1517 /* returns true if the specified directory entry looks like a directory that might contain journal
1518 * files we might be interested in, i.e. is either a 128bit ID or a 128bit ID suffixed by a
1521 if (!IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
))
1524 e
= strchr(de
->d_name
, '.');
1526 return id128_is_valid(de
->d_name
); /* No namespace */
1528 n
= strndupa_safe(de
->d_name
, e
- de
->d_name
);
1529 if (!id128_is_valid(n
))
1532 return log_namespace_name_valid(e
+ 1);
1535 static int directory_open(sd_journal
*j
, const char *path
, DIR **ret
) {
1542 if (j
->toplevel_fd
< 0)
1545 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1546 * relative, by dropping the initial slash */
1547 d
= xopendirat(j
->toplevel_fd
, skip_slash(path
), 0);
1555 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
);
1557 static void directory_enumerate(sd_journal
*j
, Directory
*m
, DIR *d
) {
1562 FOREACH_DIRENT_ALL(de
, d
, goto fail
) {
1563 if (dirent_is_journal_file(de
))
1564 (void) add_file_by_name(j
, m
->path
, de
->d_name
);
1566 if (m
->is_root
&& dirent_is_journal_subdir(de
))
1567 (void) add_directory(j
, m
->path
, de
->d_name
);
1572 log_debug_errno(errno
, "Failed to enumerate directory %s, ignoring: %m", m
->path
);
1575 static void directory_watch(sd_journal
*j
, Directory
*m
, int fd
, uint32_t mask
) {
1582 /* Watch this directory if that's enabled and if it not being watched yet. */
1584 if (m
->wd
> 0) /* Already have a watch? */
1586 if (j
->inotify_fd
< 0) /* Not watching at all? */
1589 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, fd
, mask
);
1591 log_debug_errno(errno
, "Failed to watch journal directory '%s', ignoring: %m", m
->path
);
1595 r
= hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
);
1597 log_debug_errno(r
, "Directory '%s' already being watched under a different path, ignoring: %m", m
->path
);
1599 log_debug_errno(r
, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m
->path
);
1600 (void) inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1605 static int add_directory(
1608 const char *dirname
) {
1610 _cleanup_free_
char *path
= NULL
;
1611 _cleanup_closedir_
DIR *d
= NULL
;
1618 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1619 * and reenumerates directory contents */
1621 path
= path_join(prefix
, dirname
);
1627 log_debug("Considering directory '%s'.", path
);
1629 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1630 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1631 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1635 (!(FLAGS_SET(j
->flags
, SD_JOURNAL_ALL_NAMESPACES
) ||
1636 dirname_has_namespace(dirname
, j
->namespace) > 0 ||
1637 (FLAGS_SET(j
->flags
, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE
) && dirname_has_namespace(dirname
, NULL
) > 0))))
1640 r
= directory_open(j
, path
, &d
);
1642 log_debug_errno(r
, "Failed to open directory '%s': %m", path
);
1646 m
= hashmap_get(j
->directories_by_path
, path
);
1648 m
= new(Directory
, 1);
1659 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1665 path
= NULL
; /* avoid freeing in cleanup */
1666 j
->current_invalidate_counter
++;
1668 log_debug("Directory %s added.", m
->path
);
1670 } else if (m
->is_root
)
1671 return 0; /* Don't 'downgrade' from root directory */
1673 m
->last_seen_generation
= j
->generation
;
1675 directory_watch(j
, m
, dirfd(d
),
1676 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1677 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
1680 if (!j
->no_new_files
)
1681 directory_enumerate(j
, m
, d
);
1683 check_network(j
, dirfd(d
));
1688 k
= journal_put_error(j
, r
, path
?: prefix
);
1695 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
1697 _cleanup_closedir_
DIR *d
= NULL
;
1703 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1704 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1705 * populate the set, as well as to update it later. */
1708 /* If there's a path specified, use it. */
1710 log_debug("Considering root directory '%s'.", p
);
1712 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
1713 !path_has_prefix(j
, p
, "/run"))
1717 p
= strjoina(j
->prefix
, p
);
1719 r
= directory_open(j
, p
, &d
);
1720 if (r
== -ENOENT
&& missing_ok
)
1723 log_debug_errno(r
, "Failed to open root directory %s: %m", p
);
1727 _cleanup_close_
int dfd
= -1;
1729 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1730 * opendir() will take possession of the fd, and close it, which we don't want. */
1732 p
= "."; /* store this as "." in the directories hashmap */
1734 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
1740 d
= take_fdopendir(&dfd
);
1749 m
= hashmap_get(j
->directories_by_path
, p
);
1751 m
= new0(Directory
, 1);
1759 m
->path
= strdup(p
);
1766 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1773 j
->current_invalidate_counter
++;
1775 log_debug("Root directory %s added.", m
->path
);
1777 } else if (!m
->is_root
)
1780 directory_watch(j
, m
, dirfd(d
),
1781 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1784 if (!j
->no_new_files
)
1785 directory_enumerate(j
, m
, d
);
1787 check_network(j
, dirfd(d
));
1792 k
= journal_put_error(j
, r
, p
);
1799 static void remove_directory(sd_journal
*j
, Directory
*d
) {
1803 hashmap_remove(j
->directories_by_wd
, INT_TO_PTR(d
->wd
));
1805 if (j
->inotify_fd
>= 0)
1806 (void) inotify_rm_watch(j
->inotify_fd
, d
->wd
);
1809 hashmap_remove(j
->directories_by_path
, d
->path
);
1812 log_debug("Root directory %s removed.", d
->path
);
1814 log_debug("Directory %s removed.", d
->path
);
1820 static int add_search_paths(sd_journal
*j
) {
1822 static const char search_paths
[] =
1823 "/run/log/journal\0"
1824 "/var/log/journal\0";
1829 /* We ignore most errors here, since the idea is to only open
1830 * what's actually accessible, and ignore the rest. */
1832 NULSTR_FOREACH(p
, search_paths
)
1833 (void) add_root_directory(j
, p
, true);
1835 if (!(j
->flags
& SD_JOURNAL_LOCAL_ONLY
))
1836 (void) add_root_directory(j
, "/var/log/journal/remote", true);
1841 static int add_current_paths(sd_journal
*j
) {
1845 assert(j
->no_new_files
);
1847 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1848 * treat them as fatal. */
1850 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
1851 _cleanup_free_
char *dir
= NULL
;
1854 dir
= dirname_malloc(f
->path
);
1858 r
= add_directory(j
, dir
, NULL
);
1866 static int allocate_inotify(sd_journal
*j
) {
1869 if (j
->inotify_fd
< 0) {
1870 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1871 if (j
->inotify_fd
< 0)
1875 return hashmap_ensure_allocated(&j
->directories_by_wd
, NULL
);
1878 static sd_journal
*journal_new(int flags
, const char *path
, const char *namespace) {
1879 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1881 j
= new0(sd_journal
, 1);
1885 j
->original_pid
= getpid_cached();
1886 j
->toplevel_fd
= -1;
1889 j
->data_threshold
= DEFAULT_DATA_THRESHOLD
;
1898 if (flags
& SD_JOURNAL_OS_ROOT
)
1905 j
->namespace = strdup(namespace);
1910 j
->files
= ordered_hashmap_new(&path_hash_ops
);
1914 j
->files_cache
= ordered_hashmap_iterated_cache_new(j
->files
);
1915 j
->directories_by_path
= hashmap_new(&path_hash_ops
);
1916 j
->mmap
= mmap_cache_new();
1917 if (!j
->files_cache
|| !j
->directories_by_path
|| !j
->mmap
)
1923 #define OPEN_ALLOWED_FLAGS \
1924 (SD_JOURNAL_LOCAL_ONLY | \
1925 SD_JOURNAL_RUNTIME_ONLY | \
1926 SD_JOURNAL_SYSTEM | \
1927 SD_JOURNAL_CURRENT_USER | \
1928 SD_JOURNAL_ALL_NAMESPACES | \
1929 SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE)
1931 _public_
int sd_journal_open_namespace(sd_journal
**ret
, const char *namespace, int flags
) {
1932 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1935 assert_return(ret
, -EINVAL
);
1936 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
1938 j
= journal_new(flags
, NULL
, namespace);
1942 r
= add_search_paths(j
);
1950 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1951 return sd_journal_open_namespace(ret
, NULL
, flags
);
1954 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1955 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1957 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1958 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1959 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1963 /* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1964 * combination with sd_journal_open_directory_fd(). */
1966 assert_return(machine
, -EINVAL
);
1967 assert_return(ret
, -EINVAL
);
1968 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
1969 assert_return(hostname_is_valid(machine
, 0), -EINVAL
);
1971 p
= strjoina("/run/systemd/machines/", machine
);
1972 r
= parse_env_file(NULL
, p
,
1982 if (!streq_ptr(class, "container"))
1985 j
= journal_new(flags
, root
, NULL
);
1989 r
= add_search_paths(j
);
1997 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
1998 (SD_JOURNAL_OS_ROOT | \
1999 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2001 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
2002 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2005 assert_return(ret
, -EINVAL
);
2006 assert_return(path
, -EINVAL
);
2007 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
2009 j
= journal_new(flags
, path
, NULL
);
2013 if (flags
& SD_JOURNAL_OS_ROOT
)
2014 r
= add_search_paths(j
);
2016 r
= add_root_directory(j
, path
, false);
2024 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
2025 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2028 assert_return(ret
, -EINVAL
);
2029 assert_return(flags
== 0, -EINVAL
);
2031 j
= journal_new(flags
, NULL
, NULL
);
2035 STRV_FOREACH(path
, paths
) {
2036 r
= add_any_file(j
, -1, *path
);
2041 j
->no_new_files
= true;
2047 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
2048 (SD_JOURNAL_OS_ROOT | \
2049 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
2051 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
2052 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2056 assert_return(ret
, -EINVAL
);
2057 assert_return(fd
>= 0, -EBADF
);
2058 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
2060 if (fstat(fd
, &st
) < 0)
2063 if (!S_ISDIR(st
.st_mode
))
2066 j
= journal_new(flags
, NULL
, NULL
);
2070 j
->toplevel_fd
= fd
;
2072 if (flags
& SD_JOURNAL_OS_ROOT
)
2073 r
= add_search_paths(j
);
2075 r
= add_root_directory(j
, NULL
, false);
2083 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
2085 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
2088 assert_return(ret
, -EINVAL
);
2089 assert_return(n_fds
> 0, -EBADF
);
2090 assert_return(flags
== 0, -EINVAL
);
2092 j
= journal_new(flags
, NULL
, NULL
);
2096 for (unsigned i
= 0; i
< n_fds
; i
++) {
2104 if (fstat(fds
[i
], &st
) < 0) {
2109 r
= stat_verify_regular(&st
);
2113 r
= add_any_file(j
, fds
[i
], NULL
);
2118 j
->no_new_files
= true;
2119 j
->no_inotify
= true;
2125 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
2127 ORDERED_HASHMAP_FOREACH(f
, j
->files
)
2128 f
->close_fd
= false;
2133 _public_
void sd_journal_close(sd_journal
*j
) {
2139 sd_journal_flush_matches(j
);
2141 ordered_hashmap_free_with_destructor(j
->files
, journal_file_close
);
2142 iterated_cache_free(j
->files_cache
);
2144 while ((d
= hashmap_first(j
->directories_by_path
)))
2145 remove_directory(j
, d
);
2147 while ((d
= hashmap_first(j
->directories_by_wd
)))
2148 remove_directory(j
, d
);
2150 hashmap_free(j
->directories_by_path
);
2151 hashmap_free(j
->directories_by_wd
);
2153 safe_close(j
->inotify_fd
);
2156 mmap_cache_stats_log_debug(j
->mmap
);
2157 mmap_cache_unref(j
->mmap
);
2160 hashmap_free_free(j
->errors
);
2165 free(j
->unique_field
);
2166 free(j
->fields_buffer
);
2170 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2175 assert_return(j
, -EINVAL
);
2176 assert_return(!journal_pid_changed(j
), -ECHILD
);
2177 assert_return(ret
, -EINVAL
);
2179 f
= j
->current_file
;
2181 return -EADDRNOTAVAIL
;
2183 if (f
->current_offset
<= 0)
2184 return -EADDRNOTAVAIL
;
2186 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2190 *ret
= le64toh(o
->entry
.realtime
);
2194 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2199 assert_return(j
, -EINVAL
);
2200 assert_return(!journal_pid_changed(j
), -ECHILD
);
2202 f
= j
->current_file
;
2204 return -EADDRNOTAVAIL
;
2206 if (f
->current_offset
<= 0)
2207 return -EADDRNOTAVAIL
;
2209 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2214 *ret_boot_id
= o
->entry
.boot_id
;
2218 r
= sd_id128_get_boot(&id
);
2222 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2227 *ret
= le64toh(o
->entry
.monotonic
);
2232 static bool field_is_valid(const char *field
) {
2238 if (startswith(field
, "__"))
2241 for (const char *p
= field
; *p
; p
++) {
2246 if (*p
>= 'A' && *p
<= 'Z')
2249 if (ascii_isdigit(*p
))
2258 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2260 size_t field_length
;
2264 assert_return(j
, -EINVAL
);
2265 assert_return(!journal_pid_changed(j
), -ECHILD
);
2266 assert_return(field
, -EINVAL
);
2267 assert_return(data
, -EINVAL
);
2268 assert_return(size
, -EINVAL
);
2269 assert_return(field_is_valid(field
), -EINVAL
);
2271 f
= j
->current_file
;
2273 return -EADDRNOTAVAIL
;
2275 if (f
->current_offset
<= 0)
2276 return -EADDRNOTAVAIL
;
2278 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2282 field_length
= strlen(field
);
2284 uint64_t n
= journal_file_entry_n_items(o
);
2285 for (uint64_t i
= 0; i
< n
; i
++) {
2291 p
= le64toh(o
->entry
.items
[i
].object_offset
);
2292 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &d
);
2293 if (IN_SET(r
, -EADDRNOTAVAIL
, -EBADMSG
)) {
2294 log_debug_errno(r
, "Entry item %"PRIu64
" data object is bad, skipping over it: %m", i
);
2300 l
= le64toh(d
->object
.size
) - offsetof(Object
, data
.payload
);
2302 c
= COMPRESSION_FROM_OBJECT(d
);
2304 return -EPROTONOSUPPORT
;
2305 if (c
!= COMPRESSION_NONE
) {
2306 #if HAVE_COMPRESSION
2307 r
= decompress_startswith(
2310 &f
->compress_buffer
,
2311 field
, field_length
, '=');
2313 log_debug_errno(r
, "Cannot decompress %s object of length %"PRIu64
" at offset "OFSfmt
": %m",
2314 compression_to_string(c
), l
, p
);
2319 r
= decompress_blob(
2322 &f
->compress_buffer
, &rsize
,
2327 *data
= f
->compress_buffer
;
2328 *size
= (size_t) rsize
;
2333 return -EPROTONOSUPPORT
;
2335 } else if (l
>= field_length
+1 &&
2336 memcmp(d
->data
.payload
, field
, field_length
) == 0 &&
2337 d
->data
.payload
[field_length
] == '=') {
2341 if ((uint64_t) t
!= l
)
2344 *data
= d
->data
.payload
;
2354 static int return_data(
2358 const void **ret_data
,
2368 l
= le64toh(READ_NOW(o
->object
.size
));
2369 if (l
< offsetof(Object
, data
.payload
))
2371 l
-= offsetof(Object
, data
.payload
);
2373 /* We can't read objects larger than 4G on a 32bit machine */
2375 if ((uint64_t) t
!= l
)
2378 c
= COMPRESSION_FROM_OBJECT(o
);
2380 return -EPROTONOSUPPORT
;
2381 if (c
!= COMPRESSION_NONE
) {
2382 #if HAVE_COMPRESSION
2386 r
= decompress_blob(
2389 &f
->compress_buffer
, &rsize
,
2395 *ret_data
= f
->compress_buffer
;
2397 *ret_size
= (size_t) rsize
;
2399 return -EPROTONOSUPPORT
;
2403 *ret_data
= o
->data
.payload
;
2411 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2416 assert_return(j
, -EINVAL
);
2417 assert_return(!journal_pid_changed(j
), -ECHILD
);
2418 assert_return(data
, -EINVAL
);
2419 assert_return(size
, -EINVAL
);
2421 f
= j
->current_file
;
2423 return -EADDRNOTAVAIL
;
2425 if (f
->current_offset
<= 0)
2426 return -EADDRNOTAVAIL
;
2428 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2432 for (uint64_t n
= journal_file_entry_n_items(o
); j
->current_field
< n
; j
->current_field
++) {
2435 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2436 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2437 if (IN_SET(r
, -EADDRNOTAVAIL
, -EBADMSG
)) {
2438 log_debug_errno(r
, "Entry item %"PRIu64
" data object is bad, skipping over it: %m", j
->current_field
);
2444 r
= return_data(j
, f
, o
, data
, size
);
2445 if (r
== -EBADMSG
) {
2446 log_debug("Entry item %"PRIu64
" data payload is bad, skipping over it.", j
->current_field
);
2460 _public_
int sd_journal_enumerate_available_data(sd_journal
*j
, const void **data
, size_t *size
) {
2464 r
= sd_journal_enumerate_data(j
, data
, size
);
2467 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r
))
2469 j
->current_field
++; /* Try with the next field */
2473 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2477 j
->current_field
= 0;
2480 static int reiterate_all_paths(sd_journal
*j
) {
2483 if (j
->no_new_files
)
2484 return add_current_paths(j
);
2486 if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2487 return add_search_paths(j
);
2489 if (j
->toplevel_fd
>= 0)
2490 return add_root_directory(j
, NULL
, false);
2493 return add_root_directory(j
, j
->path
, true);
2495 return add_search_paths(j
);
2498 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2501 assert_return(j
, -EINVAL
);
2502 assert_return(!journal_pid_changed(j
), -ECHILD
);
2505 return -EMEDIUMTYPE
;
2507 if (j
->inotify_fd
>= 0)
2508 return j
->inotify_fd
;
2510 r
= allocate_inotify(j
);
2514 log_debug("Reiterating files to get inotify watches established.");
2516 /* Iterate through all dirs again, to add them to the inotify */
2517 r
= reiterate_all_paths(j
);
2521 return j
->inotify_fd
;
2524 _public_
int sd_journal_get_events(sd_journal
*j
) {
2527 assert_return(j
, -EINVAL
);
2528 assert_return(!journal_pid_changed(j
), -ECHILD
);
2530 fd
= sd_journal_get_fd(j
);
2537 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2540 assert_return(j
, -EINVAL
);
2541 assert_return(!journal_pid_changed(j
), -ECHILD
);
2542 assert_return(timeout_usec
, -EINVAL
);
2544 fd
= sd_journal_get_fd(j
);
2548 if (!j
->on_network
) {
2549 *timeout_usec
= UINT64_MAX
;
2553 /* If we are on the network we need to regularly check for
2554 * changes manually */
2556 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2560 static void process_q_overflow(sd_journal
*j
) {
2566 /* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
2567 * back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
2568 * journal files we encounter. All journal files and all directories that don't carry it after reenumeration
2569 * are subject for unloading. */
2571 log_debug("Inotify queue overrun, reiterating everything.");
2574 (void) reiterate_all_paths(j
);
2576 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2578 if (f
->last_seen_generation
== j
->generation
)
2581 log_debug("File '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2582 remove_file_real(j
, f
);
2585 HASHMAP_FOREACH(m
, j
->directories_by_path
) {
2587 if (m
->last_seen_generation
== j
->generation
)
2590 if (m
->is_root
) /* Never GC root directories */
2593 log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", f
->path
);
2594 remove_directory(j
, m
);
2597 log_debug("Reiteration complete.");
2600 static void process_inotify_event(sd_journal
*j
, const struct inotify_event
*e
) {
2606 if (e
->mask
& IN_Q_OVERFLOW
) {
2607 process_q_overflow(j
);
2611 /* Is this a subdirectory we watch? */
2612 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2614 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2615 (endswith(e
->name
, ".journal") ||
2616 endswith(e
->name
, ".journal~"))) {
2618 /* Event for a journal file */
2620 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2621 (void) add_file_by_name(j
, d
->path
, e
->name
);
2622 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2623 remove_file_by_name(j
, d
->path
, e
->name
);
2625 } else if (!d
->is_root
&& e
->len
== 0) {
2627 /* Event for a subdirectory */
2629 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2630 remove_directory(j
, d
);
2632 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && id128_is_valid(e
->name
)) {
2634 /* Event for root directory */
2636 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2637 (void) add_directory(j
, d
->path
, e
->name
);
2643 if (e
->mask
& IN_IGNORED
)
2646 log_debug("Unexpected inotify event.");
2649 static int determine_change(sd_journal
*j
) {
2654 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2655 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2657 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2660 _public_
int sd_journal_process(sd_journal
*j
) {
2661 bool got_something
= false;
2663 assert_return(j
, -EINVAL
);
2664 assert_return(!journal_pid_changed(j
), -ECHILD
);
2666 if (j
->inotify_fd
< 0) /* We have no inotify fd yet? Then there's noting to process. */
2669 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2670 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2673 union inotify_event_buffer buffer
;
2676 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2678 if (ERRNO_IS_TRANSIENT(errno
))
2679 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2684 got_something
= true;
2686 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2687 process_inotify_event(j
, e
);
2691 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2695 assert_return(j
, -EINVAL
);
2696 assert_return(!journal_pid_changed(j
), -ECHILD
);
2698 if (j
->inotify_fd
< 0) {
2701 /* This is the first invocation, hence create the
2703 r
= sd_journal_get_fd(j
);
2707 /* Server might have done some vacuuming while we weren't watching.
2708 Get rid of the deleted files now so they don't stay around indefinitely. */
2709 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2710 r
= journal_file_fstat(f
);
2712 remove_file_real(j
, f
);
2714 log_debug_errno(r
,"Failed to fstat() journal file '%s' : %m", f
->path
);
2719 /* The journal might have changed since the context
2720 * object was created and we weren't watching before,
2721 * hence don't wait for anything, and return
2723 return determine_change(j
);
2726 r
= sd_journal_get_timeout(j
, &t
);
2730 if (t
!= UINT64_MAX
) {
2731 t
= usec_sub_unsigned(t
, now(CLOCK_MONOTONIC
));
2733 if (timeout_usec
== UINT64_MAX
|| timeout_usec
> t
)
2738 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2739 } while (r
== -EINTR
);
2744 return sd_journal_process(j
);
2747 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2750 uint64_t fmin
= 0, tmax
= 0;
2753 assert_return(j
, -EINVAL
);
2754 assert_return(!journal_pid_changed(j
), -ECHILD
);
2755 assert_return(from
|| to
, -EINVAL
);
2756 assert_return(from
!= to
, -EINVAL
);
2758 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2761 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2774 fmin
= MIN(fr
, fmin
);
2775 tmax
= MAX(t
, tmax
);
2784 return first
? 0 : 1;
2787 _public_
int sd_journal_get_cutoff_monotonic_usec(
2793 uint64_t from
= UINT64_MAX
, to
= UINT64_MAX
;
2798 assert_return(j
, -EINVAL
);
2799 assert_return(!journal_pid_changed(j
), -ECHILD
);
2800 assert_return(ret_from
!= ret_to
, -EINVAL
);
2802 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2805 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &ff
, &tt
);
2814 from
= MIN(ff
, from
);
2831 void journal_print_header(sd_journal
*j
) {
2833 bool newline
= false;
2837 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2843 journal_file_print_header(f
);
2847 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *ret
) {
2851 assert_return(j
, -EINVAL
);
2852 assert_return(!journal_pid_changed(j
), -ECHILD
);
2853 assert_return(ret
, -EINVAL
);
2855 ORDERED_HASHMAP_FOREACH(f
, j
->files
) {
2859 if (fstat(f
->fd
, &st
) < 0)
2862 b
= (uint64_t) st
.st_blocks
;
2863 if (b
> UINT64_MAX
/ 512)
2867 if (sum
> UINT64_MAX
- b
)
2876 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2879 assert_return(j
, -EINVAL
);
2880 assert_return(!journal_pid_changed(j
), -ECHILD
);
2881 assert_return(!isempty(field
), -EINVAL
);
2882 assert_return(field_is_valid(field
), -EINVAL
);
2884 r
= free_and_strdup(&j
->unique_field
, field
);
2888 j
->unique_file
= NULL
;
2889 j
->unique_offset
= 0;
2890 j
->unique_file_lost
= false;
2895 _public_
int sd_journal_enumerate_unique(
2897 const void **ret_data
,
2902 assert_return(j
, -EINVAL
);
2903 assert_return(!journal_pid_changed(j
), -ECHILD
);
2904 assert_return(j
->unique_field
, -EINVAL
);
2906 k
= strlen(j
->unique_field
);
2908 if (!j
->unique_file
) {
2909 if (j
->unique_file_lost
)
2912 j
->unique_file
= ordered_hashmap_first(j
->files
);
2913 if (!j
->unique_file
)
2916 j
->unique_offset
= 0;
2927 /* Proceed to next data object in the field's linked list */
2928 if (j
->unique_offset
== 0) {
2929 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2933 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2935 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2939 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2942 /* We reached the end of the list? Then start again, with the next file */
2943 if (j
->unique_offset
== 0) {
2944 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2945 if (!j
->unique_file
)
2951 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2952 * instead, so that we can look at this data object at the same
2953 * time as one on another file */
2954 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2958 /* Let's do the type check by hand, since we used 0 context above. */
2959 if (o
->object
.type
!= OBJECT_DATA
)
2960 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2961 "%s:offset " OFSfmt
": object has type %d, expected %d",
2962 j
->unique_file
->path
,
2964 o
->object
.type
, OBJECT_DATA
);
2966 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2970 /* Check if we have at least the field name and "=". */
2972 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2973 "%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2974 j
->unique_file
->path
,
2975 j
->unique_offset
, ol
, k
+ 1);
2977 if (memcmp(odata
, j
->unique_field
, k
) != 0 || ((const char*) odata
)[k
] != '=')
2978 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2979 "%s:offset " OFSfmt
": object does not start with \"%s=\"",
2980 j
->unique_file
->path
,
2984 /* OK, now let's see if we already returned this data object by checking if it exists in the
2985 * earlier traversed files. */
2987 ORDERED_HASHMAP_FOREACH(of
, j
->files
) {
2988 if (of
== j
->unique_file
)
2991 /* Skip this file it didn't have any fields indexed */
2992 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2995 /* We can reuse the hash from our current file only on old-style journal files
2996 * without keyed hashes. On new-style files we have to calculate the hash anew, to
2997 * take the per-file hash seed into consideration. */
2998 if (!JOURNAL_HEADER_KEYED_HASH(j
->unique_file
->header
) && !JOURNAL_HEADER_KEYED_HASH(of
->header
))
2999 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
3001 r
= journal_file_find_data_object(of
, odata
, ol
, NULL
, NULL
);
3013 r
= return_data(j
, j
->unique_file
, o
, ret_data
, ret_size
);
3021 _public_
int sd_journal_enumerate_available_unique(sd_journal
*j
, const void **data
, size_t *size
) {
3025 r
= sd_journal_enumerate_unique(j
, data
, size
);
3028 if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r
))
3030 /* Try with the next field. sd_journal_enumerate_unique() modifies state, so on the next try
3031 * we will access the next field. */
3035 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
3039 j
->unique_file
= NULL
;
3040 j
->unique_offset
= 0;
3041 j
->unique_file_lost
= false;
3044 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
3047 assert_return(j
, -EINVAL
);
3048 assert_return(!journal_pid_changed(j
), -ECHILD
);
3049 assert_return(field
, -EINVAL
);
3051 if (!j
->fields_file
) {
3052 if (j
->fields_file_lost
)
3055 j
->fields_file
= ordered_hashmap_first(j
->files
);
3056 if (!j
->fields_file
)
3059 j
->fields_hash_table_index
= 0;
3060 j
->fields_offset
= 0;
3064 JournalFile
*f
, *of
;
3072 if (j
->fields_offset
== 0) {
3075 /* We are not yet positioned at any field. Let's pick the first one */
3076 r
= journal_file_map_field_hash_table(f
);
3080 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
3082 if (j
->fields_hash_table_index
>= m
) {
3083 /* Reached the end of the hash table, go to the next file. */
3088 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
3090 if (j
->fields_offset
!= 0)
3093 /* Empty hash table bucket, go to next one */
3094 j
->fields_hash_table_index
++;
3098 /* Proceed with next file */
3099 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
3100 if (!j
->fields_file
) {
3105 j
->fields_offset
= 0;
3106 j
->fields_hash_table_index
= 0;
3111 /* We are already positioned at a field. If so, let's figure out the next field from it */
3113 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
3117 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
3118 if (j
->fields_offset
== 0) {
3119 /* Reached the end of the hash table chain */
3120 j
->fields_hash_table_index
++;
3125 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
3126 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
3130 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
3131 if (o
->object
.type
!= OBJECT_FIELD
)
3132 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3133 "%s:offset " OFSfmt
": object has type %i, expected %i",
3134 f
->path
, j
->fields_offset
,
3135 o
->object
.type
, OBJECT_FIELD
);
3137 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
3139 /* Let's see if we already returned this field name before. */
3141 ORDERED_HASHMAP_FOREACH(of
, j
->files
) {
3145 /* Skip this file it didn't have any fields indexed */
3146 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
3149 if (!JOURNAL_HEADER_KEYED_HASH(f
->header
) && !JOURNAL_HEADER_KEYED_HASH(of
->header
))
3150 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
,
3151 le64toh(o
->field
.hash
), NULL
, NULL
);
3153 r
= journal_file_find_field_object(of
, o
->field
.payload
, sz
, NULL
, NULL
);
3165 /* Check if this is really a valid string containing no NUL byte */
3166 if (memchr(o
->field
.payload
, 0, sz
))
3169 if (j
->data_threshold
> 0 && sz
> j
->data_threshold
)
3170 sz
= j
->data_threshold
;
3172 if (!GREEDY_REALLOC(j
->fields_buffer
, sz
+ 1))
3175 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
3176 j
->fields_buffer
[sz
] = 0;
3178 if (!field_is_valid(j
->fields_buffer
))
3181 *field
= j
->fields_buffer
;
3186 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
3190 j
->fields_file
= NULL
;
3191 j
->fields_hash_table_index
= 0;
3192 j
->fields_offset
= 0;
3193 j
->fields_file_lost
= false;
3196 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
3197 assert_return(j
, -EINVAL
);
3198 assert_return(!journal_pid_changed(j
), -ECHILD
);
3200 return !j
->on_network
;
3203 static char *lookup_field(const char *field
, void *userdata
) {
3204 sd_journal
*j
= userdata
;
3212 r
= sd_journal_get_data(j
, field
, &data
, &size
);
3214 size
> REPLACE_VAR_MAX
)
3215 return strdup(field
);
3217 d
= strlen(field
) + 1;
3219 return strndup((const char*) data
+ d
, size
- d
);
3222 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
3226 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
3230 assert_return(j
, -EINVAL
);
3231 assert_return(!journal_pid_changed(j
), -ECHILD
);
3232 assert_return(ret
, -EINVAL
);
3234 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
3238 cid
= strndup((const char*) data
+ 11, size
- 11);
3242 r
= sd_id128_from_string(cid
, &id
);
3246 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
3250 t
= replace_var(text
, lookup_field
, j
);
3258 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
3259 assert_return(ret
, -EINVAL
);
3261 return catalog_get(CATALOG_DATABASE
, id
, ret
);
3264 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
3265 assert_return(j
, -EINVAL
);
3266 assert_return(!journal_pid_changed(j
), -ECHILD
);
3268 j
->data_threshold
= sz
;
3272 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
3273 assert_return(j
, -EINVAL
);
3274 assert_return(!journal_pid_changed(j
), -ECHILD
);
3275 assert_return(sz
, -EINVAL
);
3277 *sz
= j
->data_threshold
;
3281 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
3282 assert_return(j
, -EINVAL
);
3284 return j
->has_runtime_files
;
3287 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3288 assert_return(j
, -EINVAL
);
3290 return j
->has_persistent_files
;