2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <linux/magic.h>
26 #include <sys/inotify.h>
30 #include "sd-journal.h"
32 #include "alloc-util.h"
35 #include "dirent-util.h"
38 #include "formats-util.h"
41 #include "hostname-util.h"
43 #include "journal-def.h"
44 #include "journal-file.h"
45 #include "journal-internal.h"
49 #include "path-util.h"
50 #include "replace-var.h"
51 #include "stat-util.h"
52 #include "stdio-util.h"
53 #include "string-util.h"
56 #define JOURNAL_FILES_MAX 7168
58 #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
60 #define REPLACE_VAR_MAX 256
62 #define DEFAULT_DATA_THRESHOLD (64*1024)
64 static void remove_file_real(sd_journal
*j
, JournalFile
*f
);
66 static bool journal_pid_changed(sd_journal
*j
) {
69 /* We don't support people creating a journal object and
70 * keeping it around over a fork(). Let's complain. */
72 return j
->original_pid
!= getpid();
75 static int journal_put_error(sd_journal
*j
, int r
, const char *path
) {
79 /* Memorize an error we encountered, and store which
80 * file/directory it was generated from. Note that we store
81 * only *one* path per error code, as the error code is the
82 * key into the hashmap, and the path is the value. This means
83 * we keep track only of all error kinds, but not of all error
84 * locations. This has the benefit that the hashmap cannot
87 * We return an error here only if we didn't manage to
88 * memorize the real error. */
93 k
= hashmap_ensure_allocated(&j
->errors
, NULL
);
104 k
= hashmap_put(j
->errors
, INT_TO_PTR(r
), copy
);
117 static void detach_location(sd_journal
*j
) {
123 j
->current_file
= NULL
;
124 j
->current_field
= 0;
126 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
)
127 journal_file_reset_location(f
);
130 static void reset_location(sd_journal
*j
) {
134 zero(j
->current_location
);
137 static void init_location(Location
*l
, LocationType type
, JournalFile
*f
, Object
*o
) {
139 assert(type
== LOCATION_DISCRETE
|| type
== LOCATION_SEEK
);
141 assert(o
->object
.type
== OBJECT_ENTRY
);
144 l
->seqnum
= le64toh(o
->entry
.seqnum
);
145 l
->seqnum_id
= f
->header
->seqnum_id
;
146 l
->realtime
= le64toh(o
->entry
.realtime
);
147 l
->monotonic
= le64toh(o
->entry
.monotonic
);
148 l
->boot_id
= o
->entry
.boot_id
;
149 l
->xor_hash
= le64toh(o
->entry
.xor_hash
);
151 l
->seqnum_set
= l
->realtime_set
= l
->monotonic_set
= l
->xor_hash_set
= true;
154 static void set_location(sd_journal
*j
, JournalFile
*f
, Object
*o
) {
159 init_location(&j
->current_location
, LOCATION_DISCRETE
, f
, o
);
162 j
->current_field
= 0;
164 /* Let f know its candidate entry was picked. */
165 assert(f
->location_type
== LOCATION_SEEK
);
166 f
->location_type
= LOCATION_DISCRETE
;
169 static int match_is_valid(const void *data
, size_t size
) {
177 if (startswith(data
, "__"))
181 for (p
= b
; p
< b
+ size
; p
++) {
189 if (*p
>= 'A' && *p
<= 'Z')
192 if (*p
>= '0' && *p
<= '9')
201 static bool same_field(const void *_a
, size_t s
, const void *_b
, size_t t
) {
202 const uint8_t *a
= _a
, *b
= _b
;
205 for (j
= 0; j
< s
&& j
< t
; j
++) {
214 assert_not_reached("\"=\" not found");
217 static Match
*match_new(Match
*p
, MatchType t
) {
228 LIST_PREPEND(matches
, p
->matches
, m
);
234 static void match_free(Match
*m
) {
238 match_free(m
->matches
);
241 LIST_REMOVE(matches
, m
->parent
->matches
, m
);
247 static void match_free_if_empty(Match
*m
) {
248 if (!m
|| m
->matches
)
254 _public_
int sd_journal_add_match(sd_journal
*j
, const void *data
, size_t size
) {
255 Match
*l3
, *l4
, *add_here
= NULL
, *m
;
258 assert_return(j
, -EINVAL
);
259 assert_return(!journal_pid_changed(j
), -ECHILD
);
260 assert_return(data
, -EINVAL
);
265 assert_return(match_is_valid(data
, size
), -EINVAL
);
271 * level 4: concrete matches */
274 j
->level0
= match_new(NULL
, MATCH_AND_TERM
);
280 j
->level1
= match_new(j
->level0
, MATCH_OR_TERM
);
286 j
->level2
= match_new(j
->level1
, MATCH_AND_TERM
);
291 assert(j
->level0
->type
== MATCH_AND_TERM
);
292 assert(j
->level1
->type
== MATCH_OR_TERM
);
293 assert(j
->level2
->type
== MATCH_AND_TERM
);
295 le_hash
= htole64(hash64(data
, size
));
297 LIST_FOREACH(matches
, l3
, j
->level2
->matches
) {
298 assert(l3
->type
== MATCH_OR_TERM
);
300 LIST_FOREACH(matches
, l4
, l3
->matches
) {
301 assert(l4
->type
== MATCH_DISCRETE
);
303 /* Exactly the same match already? Then ignore
305 if (l4
->le_hash
== le_hash
&&
307 memcmp(l4
->data
, data
, size
) == 0)
310 /* Same field? Then let's add this to this OR term */
311 if (same_field(data
, size
, l4
->data
, l4
->size
)) {
322 add_here
= match_new(j
->level2
, MATCH_OR_TERM
);
327 m
= match_new(add_here
, MATCH_DISCRETE
);
331 m
->le_hash
= le_hash
;
333 m
->data
= memdup(data
, size
);
342 match_free_if_empty(add_here
);
343 match_free_if_empty(j
->level2
);
344 match_free_if_empty(j
->level1
);
345 match_free_if_empty(j
->level0
);
350 _public_
int sd_journal_add_conjunction(sd_journal
*j
) {
351 assert_return(j
, -EINVAL
);
352 assert_return(!journal_pid_changed(j
), -ECHILD
);
360 if (!j
->level1
->matches
)
369 _public_
int sd_journal_add_disjunction(sd_journal
*j
) {
370 assert_return(j
, -EINVAL
);
371 assert_return(!journal_pid_changed(j
), -ECHILD
);
382 if (!j
->level2
->matches
)
389 static char *match_make_string(Match
*m
) {
392 bool enclose
= false;
395 return strdup("none");
397 if (m
->type
== MATCH_DISCRETE
)
398 return strndup(m
->data
, m
->size
);
401 LIST_FOREACH(matches
, i
, m
->matches
) {
404 t
= match_make_string(i
);
411 k
= strjoin(p
, m
->type
== MATCH_OR_TERM
? " OR " : " AND ", t
, NULL
);
426 r
= strjoin("(", p
, ")", NULL
);
434 char *journal_make_match_string(sd_journal
*j
) {
437 return match_make_string(j
->level0
);
440 _public_
void sd_journal_flush_matches(sd_journal
*j
) {
445 match_free(j
->level0
);
447 j
->level0
= j
->level1
= j
->level2
= NULL
;
452 _pure_
static int compare_with_location(JournalFile
*f
, Location
*l
) {
455 assert(f
->location_type
== LOCATION_SEEK
);
456 assert(l
->type
== LOCATION_DISCRETE
|| l
->type
== LOCATION_SEEK
);
458 if (l
->monotonic_set
&&
459 sd_id128_equal(f
->current_boot_id
, l
->boot_id
) &&
461 f
->current_realtime
== l
->realtime
&&
463 f
->current_xor_hash
== l
->xor_hash
)
467 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
)) {
469 if (f
->current_seqnum
< l
->seqnum
)
471 if (f
->current_seqnum
> l
->seqnum
)
475 if (l
->monotonic_set
&&
476 sd_id128_equal(f
->current_boot_id
, l
->boot_id
)) {
478 if (f
->current_monotonic
< l
->monotonic
)
480 if (f
->current_monotonic
> l
->monotonic
)
484 if (l
->realtime_set
) {
486 if (f
->current_realtime
< l
->realtime
)
488 if (f
->current_realtime
> l
->realtime
)
492 if (l
->xor_hash_set
) {
494 if (f
->current_xor_hash
< l
->xor_hash
)
496 if (f
->current_xor_hash
> l
->xor_hash
)
503 static int next_for_match(
507 uint64_t after_offset
,
508 direction_t direction
,
520 if (m
->type
== MATCH_DISCRETE
) {
523 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
527 return journal_file_move_to_entry_by_offset_for_data(f
, dp
, after_offset
, direction
, ret
, offset
);
529 } else if (m
->type
== MATCH_OR_TERM
) {
532 /* Find the earliest match beyond after_offset */
534 LIST_FOREACH(matches
, i
, m
->matches
) {
537 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
541 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
549 } else if (m
->type
== MATCH_AND_TERM
) {
550 Match
*i
, *last_moved
;
552 /* Always jump to the next matching entry and repeat
553 * this until we find an offset that matches for all
559 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
563 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
564 last_moved
= m
->matches
;
566 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
569 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
573 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
574 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
583 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
595 static int find_location_for_match(
599 direction_t direction
,
609 if (m
->type
== MATCH_DISCRETE
) {
612 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
616 /* FIXME: missing: find by monotonic */
618 if (j
->current_location
.type
== LOCATION_HEAD
)
619 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_DOWN
, ret
, offset
);
620 if (j
->current_location
.type
== LOCATION_TAIL
)
621 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_UP
, ret
, offset
);
622 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
623 return journal_file_move_to_entry_by_seqnum_for_data(f
, dp
, j
->current_location
.seqnum
, direction
, ret
, offset
);
624 if (j
->current_location
.monotonic_set
) {
625 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, dp
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
629 if (j
->current_location
.realtime_set
)
630 return journal_file_move_to_entry_by_realtime_for_data(f
, dp
, j
->current_location
.realtime
, direction
, ret
, offset
);
632 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, direction
, ret
, offset
);
634 } else if (m
->type
== MATCH_OR_TERM
) {
639 /* Find the earliest match */
641 LIST_FOREACH(matches
, i
, m
->matches
) {
644 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
648 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
656 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
671 assert(m
->type
== MATCH_AND_TERM
);
673 /* First jump to the last match, and then find the
674 * next one where all matches match */
679 LIST_FOREACH(matches
, i
, m
->matches
) {
682 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
686 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
690 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
694 static int find_location_with_matches(
697 direction_t direction
,
709 /* No matches is simple */
711 if (j
->current_location
.type
== LOCATION_HEAD
)
712 return journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
);
713 if (j
->current_location
.type
== LOCATION_TAIL
)
714 return journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
);
715 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
716 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
717 if (j
->current_location
.monotonic_set
) {
718 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
722 if (j
->current_location
.realtime_set
)
723 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
725 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
727 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
730 static int next_with_matches(
733 direction_t direction
,
742 /* No matches is easy. We simple advance the file
745 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
747 /* If we have a match then we look for the next matching entry
748 * with an offset at least one step larger */
749 return next_for_match(j
, j
->level0
, f
,
750 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
751 : f
->current_offset
- 1,
752 direction
, ret
, offset
);
755 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
757 uint64_t cp
, n_entries
;
763 n_entries
= le64toh(f
->header
->n_entries
);
765 /* If we hit EOF before, we don't need to look into this file again
766 * unless direction changed or new entries appeared. */
767 if (f
->last_direction
== direction
&& f
->location_type
== LOCATION_TAIL
&&
768 n_entries
== f
->last_n_entries
)
771 f
->last_n_entries
= n_entries
;
773 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
774 /* LOCATION_SEEK here means we did the work in a previous
775 * iteration and the current location already points to a
776 * candidate entry. */
777 if (f
->location_type
!= LOCATION_SEEK
) {
778 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
782 journal_file_save_location(f
, c
, cp
);
785 f
->last_direction
= direction
;
787 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
791 journal_file_save_location(f
, c
, cp
);
794 /* OK, we found the spot, now let's advance until an entry
795 * that is actually different from what we were previously
796 * looking at. This is necessary to handle entries which exist
797 * in two (or more) journal files, and which shall all be
798 * suppressed but one. */
803 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
806 k
= compare_with_location(f
, &j
->current_location
);
808 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
815 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
819 journal_file_save_location(f
, c
, cp
);
823 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
824 JournalFile
*f
, *new_file
= NULL
;
829 assert_return(j
, -EINVAL
);
830 assert_return(!journal_pid_changed(j
), -ECHILD
);
832 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
835 r
= next_beyond_location(j
, f
, direction
);
837 log_debug_errno(r
, "Can't iterate through %s, ignoring: %m", f
->path
);
838 remove_file_real(j
, f
);
841 f
->location_type
= LOCATION_TAIL
;
850 k
= journal_file_compare_locations(f
, new_file
);
852 found
= direction
== DIRECTION_DOWN
? k
< 0 : k
> 0;
862 r
= journal_file_move_to_object(new_file
, OBJECT_ENTRY
, new_file
->current_offset
, &o
);
866 set_location(j
, new_file
, o
);
871 _public_
int sd_journal_next(sd_journal
*j
) {
872 return real_journal_next(j
, DIRECTION_DOWN
);
875 _public_
int sd_journal_previous(sd_journal
*j
) {
876 return real_journal_next(j
, DIRECTION_UP
);
879 static int real_journal_next_skip(sd_journal
*j
, direction_t direction
, uint64_t skip
) {
882 assert_return(j
, -EINVAL
);
883 assert_return(!journal_pid_changed(j
), -ECHILD
);
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 return real_journal_next(j
, direction
);
895 r
= real_journal_next(j
, direction
);
909 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
910 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
913 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
914 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
917 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
920 char bid
[33], sid
[33];
922 assert_return(j
, -EINVAL
);
923 assert_return(!journal_pid_changed(j
), -ECHILD
);
924 assert_return(cursor
, -EINVAL
);
926 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
927 return -EADDRNOTAVAIL
;
929 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
933 sd_id128_to_string(j
->current_file
->header
->seqnum_id
, sid
);
934 sd_id128_to_string(o
->entry
.boot_id
, bid
);
937 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
938 sid
, le64toh(o
->entry
.seqnum
),
939 bid
, le64toh(o
->entry
.monotonic
),
940 le64toh(o
->entry
.realtime
),
941 le64toh(o
->entry
.xor_hash
)) < 0)
947 _public_
int sd_journal_seek_cursor(sd_journal
*j
, const char *cursor
) {
948 const char *word
, *state
;
950 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
952 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
;
960 assert_return(j
, -EINVAL
);
961 assert_return(!journal_pid_changed(j
), -ECHILD
);
962 assert_return(!isempty(cursor
), -EINVAL
);
964 FOREACH_WORD_SEPARATOR(word
, l
, cursor
, ";", state
) {
968 if (l
< 2 || word
[1] != '=')
971 item
= strndup(word
, l
);
978 seqnum_id_set
= true;
979 k
= sd_id128_from_string(item
+2, &seqnum_id
);
984 if (sscanf(item
+2, "%llx", &seqnum
) != 1)
990 k
= sd_id128_from_string(item
+2, &boot_id
);
994 monotonic_set
= true;
995 if (sscanf(item
+2, "%llx", &monotonic
) != 1)
1000 realtime_set
= true;
1001 if (sscanf(item
+2, "%llx", &realtime
) != 1)
1006 xor_hash_set
= true;
1007 if (sscanf(item
+2, "%llx", &xor_hash
) != 1)
1018 if ((!seqnum_set
|| !seqnum_id_set
) &&
1019 (!monotonic_set
|| !boot_id_set
) &&
1025 j
->current_location
.type
= LOCATION_SEEK
;
1028 j
->current_location
.realtime
= (uint64_t) realtime
;
1029 j
->current_location
.realtime_set
= true;
1032 if (seqnum_set
&& seqnum_id_set
) {
1033 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1034 j
->current_location
.seqnum_id
= seqnum_id
;
1035 j
->current_location
.seqnum_set
= true;
1038 if (monotonic_set
&& boot_id_set
) {
1039 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1040 j
->current_location
.boot_id
= boot_id
;
1041 j
->current_location
.monotonic_set
= true;
1045 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1046 j
->current_location
.xor_hash_set
= true;
1052 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1056 assert_return(j
, -EINVAL
);
1057 assert_return(!journal_pid_changed(j
), -ECHILD
);
1058 assert_return(!isempty(cursor
), -EINVAL
);
1060 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1061 return -EADDRNOTAVAIL
;
1063 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1068 _cleanup_free_
char *item
= NULL
;
1069 unsigned long long ll
;
1073 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1080 if (strlen(item
) < 2 || item
[1] != '=')
1086 k
= sd_id128_from_string(item
+2, &id
);
1089 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1094 if (sscanf(item
+2, "%llx", &ll
) != 1)
1096 if (ll
!= le64toh(o
->entry
.seqnum
))
1101 k
= sd_id128_from_string(item
+2, &id
);
1104 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1109 if (sscanf(item
+2, "%llx", &ll
) != 1)
1111 if (ll
!= le64toh(o
->entry
.monotonic
))
1116 if (sscanf(item
+2, "%llx", &ll
) != 1)
1118 if (ll
!= le64toh(o
->entry
.realtime
))
1123 if (sscanf(item
+2, "%llx", &ll
) != 1)
1125 if (ll
!= le64toh(o
->entry
.xor_hash
))
1135 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t usec
) {
1136 assert_return(j
, -EINVAL
);
1137 assert_return(!journal_pid_changed(j
), -ECHILD
);
1140 j
->current_location
.type
= LOCATION_SEEK
;
1141 j
->current_location
.boot_id
= boot_id
;
1142 j
->current_location
.monotonic
= usec
;
1143 j
->current_location
.monotonic_set
= true;
1148 _public_
int sd_journal_seek_realtime_usec(sd_journal
*j
, uint64_t usec
) {
1149 assert_return(j
, -EINVAL
);
1150 assert_return(!journal_pid_changed(j
), -ECHILD
);
1153 j
->current_location
.type
= LOCATION_SEEK
;
1154 j
->current_location
.realtime
= usec
;
1155 j
->current_location
.realtime_set
= true;
1160 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1161 assert_return(j
, -EINVAL
);
1162 assert_return(!journal_pid_changed(j
), -ECHILD
);
1165 j
->current_location
.type
= LOCATION_HEAD
;
1170 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1171 assert_return(j
, -EINVAL
);
1172 assert_return(!journal_pid_changed(j
), -ECHILD
);
1175 j
->current_location
.type
= LOCATION_TAIL
;
1180 static void check_network(sd_journal
*j
, int fd
) {
1188 if (fstatfs(fd
, &sfs
) < 0)
1192 F_TYPE_EQUAL(sfs
.f_type
, CIFS_MAGIC_NUMBER
) ||
1193 F_TYPE_EQUAL(sfs
.f_type
, CODA_SUPER_MAGIC
) ||
1194 F_TYPE_EQUAL(sfs
.f_type
, NCP_SUPER_MAGIC
) ||
1195 F_TYPE_EQUAL(sfs
.f_type
, NFS_SUPER_MAGIC
) ||
1196 F_TYPE_EQUAL(sfs
.f_type
, SMB_SUPER_MAGIC
);
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 streq(filename
, full
) ||
1207 streq(filename
, tilded
) ||
1208 startswith(filename
, atted
);
1211 static bool file_type_wanted(int flags
, const char *filename
) {
1214 if (!endswith(filename
, ".journal") && !endswith(filename
, ".journal~"))
1217 /* no flags set → every type is OK */
1218 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1221 if (flags
& SD_JOURNAL_SYSTEM
&& file_has_type_prefix("system", filename
))
1224 if (flags
& SD_JOURNAL_CURRENT_USER
) {
1225 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1227 xsprintf(prefix
, "user-"UID_FMT
, getuid());
1229 if (file_has_type_prefix(prefix
, filename
))
1236 static bool path_has_prefix(sd_journal
*j
, const char *path
, const char *prefix
) {
1241 if (j
->toplevel_fd
>= 0)
1244 return path_startswith(path
, prefix
);
1247 static const char *skip_slash(const char *p
) {
1258 static int add_any_file(sd_journal
*j
, int fd
, const char *path
) {
1259 JournalFile
*f
= NULL
;
1260 bool close_fd
= false;
1264 assert(fd
>= 0 || path
);
1266 if (path
&& ordered_hashmap_get(j
->files
, path
))
1269 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1270 log_debug("Too many open journal files, not adding %s.", path
);
1275 if (fd
< 0 && j
->toplevel_fd
>= 0) {
1277 /* If there's a top-level fd defined, open the file relative to this now. (Make the path relative,
1278 * explicitly, since otherwise openat() ignores the first argument.) */
1280 fd
= openat(j
->toplevel_fd
, skip_slash(path
), O_RDONLY
|O_CLOEXEC
);
1282 r
= log_debug_errno(errno
, "Failed to open journal file %s: %m", path
);
1289 r
= journal_file_open(fd
, path
, O_RDONLY
, 0, false, false, NULL
, j
->mmap
, NULL
, NULL
, &f
);
1293 log_debug_errno(r
, "Failed to open journal file %s: %m", path
);
1297 /* journal_file_dump(f); */
1299 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1301 f
->close_fd
= close_fd
;
1302 (void) journal_file_close(f
);
1306 if (!j
->has_runtime_files
&& path_has_prefix(j
, f
->path
, "/run"))
1307 j
->has_runtime_files
= true;
1308 else if (!j
->has_persistent_files
&& path_has_prefix(j
, f
->path
, "/var"))
1309 j
->has_persistent_files
= true;
1311 log_debug("File %s added.", f
->path
);
1313 check_network(j
, f
->fd
);
1315 j
->current_invalidate_counter
++;
1320 k
= journal_put_error(j
, r
, path
);
1327 static int add_file(sd_journal
*j
, const char *prefix
, const char *filename
) {
1334 if (j
->no_new_files
)
1337 if (!file_type_wanted(j
->flags
, filename
))
1340 path
= strjoina(prefix
, "/", filename
);
1341 return add_any_file(j
, -1, path
);
1344 static void remove_file(sd_journal
*j
, const char *prefix
, const char *filename
) {
1352 path
= strjoina(prefix
, "/", filename
);
1353 f
= ordered_hashmap_get(j
->files
, path
);
1357 remove_file_real(j
, f
);
1360 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1364 ordered_hashmap_remove(j
->files
, f
->path
);
1366 log_debug("File %s removed.", f
->path
);
1368 if (j
->current_file
== f
) {
1369 j
->current_file
= NULL
;
1370 j
->current_field
= 0;
1373 if (j
->unique_file
== f
) {
1374 /* Jump to the next unique_file or NULL if that one was last */
1375 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1376 j
->unique_offset
= 0;
1377 if (!j
->unique_file
)
1378 j
->unique_file_lost
= true;
1381 if (j
->fields_file
== f
) {
1382 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1383 j
->fields_offset
= 0;
1384 if (!j
->fields_file
)
1385 j
->fields_file_lost
= true;
1388 (void) journal_file_close(f
);
1390 j
->current_invalidate_counter
++;
1393 static int dirname_is_machine_id(const char *fn
) {
1394 sd_id128_t id
, machine
;
1397 r
= sd_id128_get_machine(&machine
);
1401 r
= sd_id128_from_string(fn
, &id
);
1405 return sd_id128_equal(id
, machine
);
1408 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
) {
1409 _cleanup_free_
char *path
= NULL
;
1410 _cleanup_closedir_
DIR *d
= NULL
;
1411 struct dirent
*de
= NULL
;
1418 /* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
1419 * and reenumerates directory contents */
1422 path
= strjoin(prefix
, "/", dirname
, NULL
);
1424 path
= strdup(prefix
);
1430 log_debug("Considering directory %s.", path
);
1432 /* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
1433 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1434 !((dirname
&& dirname_is_machine_id(dirname
) > 0) || path_has_prefix(j
, path
, "/run")))
1438 if (j
->toplevel_fd
< 0)
1441 /* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
1442 * relative, by dropping the initial slash */
1443 d
= xopendirat(j
->toplevel_fd
, skip_slash(path
), 0);
1445 r
= log_debug_errno(errno
, "Failed to open directory %s: %m", path
);
1449 m
= hashmap_get(j
->directories_by_path
, path
);
1451 m
= new0(Directory
, 1);
1460 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1466 path
= NULL
; /* avoid freeing in cleanup */
1467 j
->current_invalidate_counter
++;
1469 log_debug("Directory %s added.", m
->path
);
1471 } else if (m
->is_root
)
1474 if (m
->wd
<= 0 && j
->inotify_fd
>= 0) {
1475 /* Watch this directory, if it not being watched yet. */
1477 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, dirfd(d
),
1478 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1479 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
1482 if (m
->wd
> 0 && hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
) < 0)
1483 inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1486 FOREACH_DIRENT_ALL(de
, d
, r
= log_debug_errno(errno
, "Failed to read directory %s: %m", m
->path
); goto fail
) {
1488 if (dirent_is_file_with_suffix(de
, ".journal") ||
1489 dirent_is_file_with_suffix(de
, ".journal~"))
1490 (void) add_file(j
, m
->path
, de
->d_name
);
1493 check_network(j
, dirfd(d
));
1498 k
= journal_put_error(j
, r
, path
?: prefix
);
1505 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
1507 _cleanup_closedir_
DIR *d
= NULL
;
1514 /* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
1515 * update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
1516 * populate the set, as well as to update it later. */
1519 /* If there's a path specified, use it. */
1521 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
1522 !path_has_prefix(j
, p
, "/run"))
1526 p
= strjoina(j
->prefix
, p
);
1528 if (j
->toplevel_fd
< 0)
1531 d
= xopendirat(j
->toplevel_fd
, skip_slash(p
), 0);
1534 if (errno
== ENOENT
&& missing_ok
)
1537 r
= log_debug_errno(errno
, "Failed to open root directory %s: %m", p
);
1543 /* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
1544 * opendir() will take possession of the fd, and close it, which we don't want. */
1546 p
= "."; /* store this as "." in the directories hashmap */
1548 dfd
= fcntl(j
->toplevel_fd
, F_DUPFD_CLOEXEC
, 3);
1564 m
= hashmap_get(j
->directories_by_path
, p
);
1566 m
= new0(Directory
, 1);
1574 m
->path
= strdup(p
);
1581 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1588 j
->current_invalidate_counter
++;
1590 log_debug("Root directory %s added.", m
->path
);
1592 } else if (!m
->is_root
)
1595 if (m
->wd
<= 0 && j
->inotify_fd
>= 0) {
1597 m
->wd
= inotify_add_watch_fd(j
->inotify_fd
, dirfd(d
),
1598 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1601 if (m
->wd
> 0 && hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
) < 0)
1602 inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1605 if (j
->no_new_files
)
1608 FOREACH_DIRENT_ALL(de
, d
, r
= log_debug_errno(errno
, "Failed to read directory %s: %m", m
->path
); goto fail
) {
1611 if (dirent_is_file_with_suffix(de
, ".journal") ||
1612 dirent_is_file_with_suffix(de
, ".journal~"))
1613 (void) add_file(j
, m
->path
, de
->d_name
);
1614 else if (IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
) &&
1615 sd_id128_from_string(de
->d_name
, &id
) >= 0)
1616 (void) add_directory(j
, m
->path
, de
->d_name
);
1619 check_network(j
, dirfd(d
));
1624 k
= journal_put_error(j
, r
, p
);
1631 static void remove_directory(sd_journal
*j
, Directory
*d
) {
1635 hashmap_remove(j
->directories_by_wd
, INT_TO_PTR(d
->wd
));
1637 if (j
->inotify_fd
>= 0)
1638 inotify_rm_watch(j
->inotify_fd
, d
->wd
);
1641 hashmap_remove(j
->directories_by_path
, d
->path
);
1644 log_debug("Root directory %s removed.", d
->path
);
1646 log_debug("Directory %s removed.", d
->path
);
1652 static int add_search_paths(sd_journal
*j
) {
1654 static const char search_paths
[] =
1655 "/run/log/journal\0"
1656 "/var/log/journal\0";
1661 /* We ignore most errors here, since the idea is to only open
1662 * what's actually accessible, and ignore the rest. */
1664 NULSTR_FOREACH(p
, search_paths
)
1665 (void) add_root_directory(j
, p
, true);
1670 static int add_current_paths(sd_journal
*j
) {
1675 assert(j
->no_new_files
);
1677 /* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
1678 * treat them as fatal. */
1680 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
1681 _cleanup_free_
char *dir
;
1684 dir
= dirname_malloc(f
->path
);
1688 r
= add_directory(j
, dir
, NULL
);
1696 static int allocate_inotify(sd_journal
*j
) {
1699 if (j
->inotify_fd
< 0) {
1700 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1701 if (j
->inotify_fd
< 0)
1705 return hashmap_ensure_allocated(&j
->directories_by_wd
, NULL
);
1708 static sd_journal
*journal_new(int flags
, const char *path
) {
1711 j
= new0(sd_journal
, 1);
1715 j
->original_pid
= getpid();
1716 j
->toplevel_fd
= -1;
1719 j
->data_threshold
= DEFAULT_DATA_THRESHOLD
;
1728 if (flags
& SD_JOURNAL_OS_ROOT
)
1734 j
->files
= ordered_hashmap_new(&string_hash_ops
);
1735 j
->directories_by_path
= hashmap_new(&string_hash_ops
);
1736 j
->mmap
= mmap_cache_new();
1737 if (!j
->files
|| !j
->directories_by_path
|| !j
->mmap
)
1743 sd_journal_close(j
);
1747 #define OPEN_ALLOWED_FLAGS \
1748 (SD_JOURNAL_LOCAL_ONLY | \
1749 SD_JOURNAL_RUNTIME_ONLY | \
1750 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)
1752 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1756 assert_return(ret
, -EINVAL
);
1757 assert_return((flags
& ~OPEN_ALLOWED_FLAGS
) == 0, -EINVAL
);
1759 j
= journal_new(flags
, NULL
);
1763 r
= add_search_paths(j
);
1771 sd_journal_close(j
);
1776 #define OPEN_CONTAINER_ALLOWED_FLAGS \
1777 (SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_SYSTEM)
1779 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1780 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1785 /* This is pretty much deprecated, people should use machined's OpenMachineRootDirectory() call instead in
1786 * combination with sd_journal_open_directory_fd(). */
1788 assert_return(machine
, -EINVAL
);
1789 assert_return(ret
, -EINVAL
);
1790 assert_return((flags
& ~OPEN_CONTAINER_ALLOWED_FLAGS
) == 0, -EINVAL
);
1791 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1793 p
= strjoina("/run/systemd/machines/", machine
);
1794 r
= parse_env_file(p
, NEWLINE
, "ROOT", &root
, "CLASS", &class, NULL
);
1802 if (!streq_ptr(class, "container"))
1805 j
= journal_new(flags
, root
);
1809 r
= add_search_paths(j
);
1817 sd_journal_close(j
);
1821 #define OPEN_DIRECTORY_ALLOWED_FLAGS \
1822 (SD_JOURNAL_OS_ROOT | \
1823 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1825 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
1829 assert_return(ret
, -EINVAL
);
1830 assert_return(path
, -EINVAL
);
1831 assert_return((flags
& ~OPEN_DIRECTORY_ALLOWED_FLAGS
) == 0, -EINVAL
);
1833 j
= journal_new(flags
, path
);
1837 if (flags
& SD_JOURNAL_OS_ROOT
)
1838 r
= add_search_paths(j
);
1840 r
= add_root_directory(j
, path
, false);
1848 sd_journal_close(j
);
1852 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
1857 assert_return(ret
, -EINVAL
);
1858 assert_return(flags
== 0, -EINVAL
);
1860 j
= journal_new(flags
, NULL
);
1864 STRV_FOREACH(path
, paths
) {
1865 r
= add_any_file(j
, -1, *path
);
1870 j
->no_new_files
= true;
1876 sd_journal_close(j
);
1880 #define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
1881 (SD_JOURNAL_OS_ROOT | \
1882 SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
1884 _public_
int sd_journal_open_directory_fd(sd_journal
**ret
, int fd
, int flags
) {
1889 assert_return(ret
, -EINVAL
);
1890 assert_return(fd
>= 0, -EBADF
);
1891 assert_return((flags
& ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS
) == 0, -EINVAL
);
1893 if (fstat(fd
, &st
) < 0)
1896 if (!S_ISDIR(st
.st_mode
))
1899 j
= journal_new(flags
, NULL
);
1903 j
->toplevel_fd
= fd
;
1905 if (flags
& SD_JOURNAL_OS_ROOT
)
1906 r
= add_search_paths(j
);
1908 r
= add_root_directory(j
, NULL
, false);
1916 sd_journal_close(j
);
1920 _public_
int sd_journal_open_files_fd(sd_journal
**ret
, int fds
[], unsigned n_fds
, int flags
) {
1927 assert_return(ret
, -EINVAL
);
1928 assert_return(n_fds
> 0, -EBADF
);
1929 assert_return(flags
== 0, -EINVAL
);
1931 j
= journal_new(flags
, NULL
);
1935 for (i
= 0; i
< n_fds
; i
++) {
1943 if (fstat(fds
[i
], &st
) < 0) {
1948 if (!S_ISREG(st
.st_mode
)) {
1953 r
= add_any_file(j
, fds
[i
], NULL
);
1958 j
->no_new_files
= true;
1959 j
->no_inotify
= true;
1965 /* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
1967 ORDERED_HASHMAP_FOREACH(f
, j
->files
, iterator
)
1968 f
->close_fd
= false;
1970 sd_journal_close(j
);
1974 _public_
void sd_journal_close(sd_journal
*j
) {
1982 sd_journal_flush_matches(j
);
1984 while ((f
= ordered_hashmap_steal_first(j
->files
)))
1985 (void) journal_file_close(f
);
1987 ordered_hashmap_free(j
->files
);
1989 while ((d
= hashmap_first(j
->directories_by_path
)))
1990 remove_directory(j
, d
);
1992 while ((d
= hashmap_first(j
->directories_by_wd
)))
1993 remove_directory(j
, d
);
1995 hashmap_free(j
->directories_by_path
);
1996 hashmap_free(j
->directories_by_wd
);
1998 safe_close(j
->inotify_fd
);
2001 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j
->mmap
), mmap_cache_get_missed(j
->mmap
));
2002 mmap_cache_unref(j
->mmap
);
2005 while ((p
= hashmap_steal_first(j
->errors
)))
2007 hashmap_free(j
->errors
);
2011 free(j
->unique_field
);
2012 free(j
->fields_buffer
);
2016 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
2021 assert_return(j
, -EINVAL
);
2022 assert_return(!journal_pid_changed(j
), -ECHILD
);
2023 assert_return(ret
, -EINVAL
);
2025 f
= j
->current_file
;
2027 return -EADDRNOTAVAIL
;
2029 if (f
->current_offset
<= 0)
2030 return -EADDRNOTAVAIL
;
2032 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2036 *ret
= le64toh(o
->entry
.realtime
);
2040 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
2046 assert_return(j
, -EINVAL
);
2047 assert_return(!journal_pid_changed(j
), -ECHILD
);
2049 f
= j
->current_file
;
2051 return -EADDRNOTAVAIL
;
2053 if (f
->current_offset
<= 0)
2054 return -EADDRNOTAVAIL
;
2056 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2061 *ret_boot_id
= o
->entry
.boot_id
;
2063 r
= sd_id128_get_boot(&id
);
2067 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
2072 *ret
= le64toh(o
->entry
.monotonic
);
2077 static bool field_is_valid(const char *field
) {
2085 if (startswith(field
, "__"))
2088 for (p
= field
; *p
; p
++) {
2093 if (*p
>= 'A' && *p
<= 'Z')
2096 if (*p
>= '0' && *p
<= '9')
2105 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
2108 size_t field_length
;
2112 assert_return(j
, -EINVAL
);
2113 assert_return(!journal_pid_changed(j
), -ECHILD
);
2114 assert_return(field
, -EINVAL
);
2115 assert_return(data
, -EINVAL
);
2116 assert_return(size
, -EINVAL
);
2117 assert_return(field_is_valid(field
), -EINVAL
);
2119 f
= j
->current_file
;
2121 return -EADDRNOTAVAIL
;
2123 if (f
->current_offset
<= 0)
2124 return -EADDRNOTAVAIL
;
2126 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2130 field_length
= strlen(field
);
2132 n
= journal_file_entry_n_items(o
);
2133 for (i
= 0; i
< n
; i
++) {
2139 p
= le64toh(o
->entry
.items
[i
].object_offset
);
2140 le_hash
= o
->entry
.items
[i
].hash
;
2141 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2145 if (le_hash
!= o
->data
.hash
)
2148 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2150 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2152 #if defined(HAVE_XZ) || defined(HAVE_LZ4)
2153 r
= decompress_startswith(compression
,
2155 &f
->compress_buffer
, &f
->compress_buffer_size
,
2156 field
, field_length
, '=');
2158 log_debug_errno(r
, "Cannot decompress %s object of length %"PRIu64
" at offset "OFSfmt
": %m",
2159 object_compressed_to_string(compression
), l
, p
);
2164 r
= decompress_blob(compression
,
2166 &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
,
2171 *data
= f
->compress_buffer
;
2172 *size
= (size_t) rsize
;
2177 return -EPROTONOSUPPORT
;
2179 } else if (l
>= field_length
+1 &&
2180 memcmp(o
->data
.payload
, field
, field_length
) == 0 &&
2181 o
->data
.payload
[field_length
] == '=') {
2185 if ((uint64_t) t
!= l
)
2188 *data
= o
->data
.payload
;
2194 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2202 static int return_data(sd_journal
*j
, JournalFile
*f
, Object
*o
, const void **data
, size_t *size
) {
2207 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2210 /* We can't read objects larger than 4G on a 32bit machine */
2211 if ((uint64_t) t
!= l
)
2214 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2216 #if defined(HAVE_XZ) || defined(HAVE_LZ4)
2220 r
= decompress_blob(compression
,
2221 o
->data
.payload
, l
, &f
->compress_buffer
,
2222 &f
->compress_buffer_size
, &rsize
, j
->data_threshold
);
2226 *data
= f
->compress_buffer
;
2227 *size
= (size_t) rsize
;
2229 return -EPROTONOSUPPORT
;
2232 *data
= o
->data
.payload
;
2239 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2246 assert_return(j
, -EINVAL
);
2247 assert_return(!journal_pid_changed(j
), -ECHILD
);
2248 assert_return(data
, -EINVAL
);
2249 assert_return(size
, -EINVAL
);
2251 f
= j
->current_file
;
2253 return -EADDRNOTAVAIL
;
2255 if (f
->current_offset
<= 0)
2256 return -EADDRNOTAVAIL
;
2258 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2262 n
= journal_file_entry_n_items(o
);
2263 if (j
->current_field
>= n
)
2266 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2267 le_hash
= o
->entry
.items
[j
->current_field
].hash
;
2268 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2272 if (le_hash
!= o
->data
.hash
)
2275 r
= return_data(j
, f
, o
, data
, size
);
2284 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2288 j
->current_field
= 0;
2291 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2294 assert_return(j
, -EINVAL
);
2295 assert_return(!journal_pid_changed(j
), -ECHILD
);
2298 return -EMEDIUMTYPE
;
2300 if (j
->inotify_fd
>= 0)
2301 return j
->inotify_fd
;
2303 r
= allocate_inotify(j
);
2307 log_debug("Reiterating files to get inotify watches established");
2309 /* Iterate through all dirs again, to add them to the
2311 if (j
->no_new_files
)
2312 r
= add_current_paths(j
);
2313 else if (j
->flags
& SD_JOURNAL_OS_ROOT
)
2314 r
= add_search_paths(j
);
2315 else if (j
->toplevel_fd
>= 0)
2316 r
= add_root_directory(j
, NULL
, false);
2318 r
= add_root_directory(j
, j
->path
, true);
2320 r
= add_search_paths(j
);
2324 return j
->inotify_fd
;
2327 _public_
int sd_journal_get_events(sd_journal
*j
) {
2330 assert_return(j
, -EINVAL
);
2331 assert_return(!journal_pid_changed(j
), -ECHILD
);
2333 fd
= sd_journal_get_fd(j
);
2340 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2343 assert_return(j
, -EINVAL
);
2344 assert_return(!journal_pid_changed(j
), -ECHILD
);
2345 assert_return(timeout_usec
, -EINVAL
);
2347 fd
= sd_journal_get_fd(j
);
2351 if (!j
->on_network
) {
2352 *timeout_usec
= (uint64_t) -1;
2356 /* If we are on the network we need to regularly check for
2357 * changes manually */
2359 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2363 static void process_inotify_event(sd_journal
*j
, struct inotify_event
*e
) {
2369 /* Is this a subdirectory we watch? */
2370 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2374 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2375 (endswith(e
->name
, ".journal") ||
2376 endswith(e
->name
, ".journal~"))) {
2378 /* Event for a journal file */
2380 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2381 (void) add_file(j
, d
->path
, e
->name
);
2382 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2383 remove_file(j
, d
->path
, e
->name
);
2385 } else if (!d
->is_root
&& e
->len
== 0) {
2387 /* Event for a subdirectory */
2389 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2390 remove_directory(j
, d
);
2392 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && sd_id128_from_string(e
->name
, &id
) >= 0) {
2394 /* Event for root directory */
2396 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2397 (void) add_directory(j
, d
->path
, e
->name
);
2403 if (e
->mask
& IN_IGNORED
)
2406 log_debug("Unknown inotify event.");
2409 static int determine_change(sd_journal
*j
) {
2414 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2415 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2417 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2420 _public_
int sd_journal_process(sd_journal
*j
) {
2421 bool got_something
= false;
2423 assert_return(j
, -EINVAL
);
2424 assert_return(!journal_pid_changed(j
), -ECHILD
);
2426 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2429 union inotify_event_buffer buffer
;
2430 struct inotify_event
*e
;
2433 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2435 if (errno
== EAGAIN
|| errno
== EINTR
)
2436 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2441 got_something
= true;
2443 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2444 process_inotify_event(j
, e
);
2448 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2452 assert_return(j
, -EINVAL
);
2453 assert_return(!journal_pid_changed(j
), -ECHILD
);
2455 if (j
->inotify_fd
< 0) {
2457 /* This is the first invocation, hence create the
2459 r
= sd_journal_get_fd(j
);
2463 /* The journal might have changed since the context
2464 * object was created and we weren't watching before,
2465 * hence don't wait for anything, and return
2467 return determine_change(j
);
2470 r
= sd_journal_get_timeout(j
, &t
);
2474 if (t
!= (uint64_t) -1) {
2477 n
= now(CLOCK_MONOTONIC
);
2478 t
= t
> n
? t
- n
: 0;
2480 if (timeout_usec
== (uint64_t) -1 || timeout_usec
> t
)
2485 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2486 } while (r
== -EINTR
);
2491 return sd_journal_process(j
);
2494 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2498 uint64_t fmin
= 0, tmax
= 0;
2501 assert_return(j
, -EINVAL
);
2502 assert_return(!journal_pid_changed(j
), -ECHILD
);
2503 assert_return(from
|| to
, -EINVAL
);
2504 assert_return(from
!= to
, -EINVAL
);
2506 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2509 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2522 fmin
= MIN(fr
, fmin
);
2523 tmax
= MAX(t
, tmax
);
2532 return first
? 0 : 1;
2535 _public_
int sd_journal_get_cutoff_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t *from
, uint64_t *to
) {
2541 assert_return(j
, -EINVAL
);
2542 assert_return(!journal_pid_changed(j
), -ECHILD
);
2543 assert_return(from
|| to
, -EINVAL
);
2544 assert_return(from
!= to
, -EINVAL
);
2546 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2549 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &fr
, &t
);
2559 *from
= MIN(fr
, *from
);
2574 void journal_print_header(sd_journal
*j
) {
2577 bool newline
= false;
2581 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2587 journal_file_print_header(f
);
2591 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *bytes
) {
2596 assert_return(j
, -EINVAL
);
2597 assert_return(!journal_pid_changed(j
), -ECHILD
);
2598 assert_return(bytes
, -EINVAL
);
2600 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2603 if (fstat(f
->fd
, &st
) < 0)
2606 sum
+= (uint64_t) st
.st_blocks
* 512ULL;
2613 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2616 assert_return(j
, -EINVAL
);
2617 assert_return(!journal_pid_changed(j
), -ECHILD
);
2618 assert_return(!isempty(field
), -EINVAL
);
2619 assert_return(field_is_valid(field
), -EINVAL
);
2625 free(j
->unique_field
);
2626 j
->unique_field
= f
;
2627 j
->unique_file
= NULL
;
2628 j
->unique_offset
= 0;
2629 j
->unique_file_lost
= false;
2634 _public_
int sd_journal_enumerate_unique(sd_journal
*j
, const void **data
, size_t *l
) {
2637 assert_return(j
, -EINVAL
);
2638 assert_return(!journal_pid_changed(j
), -ECHILD
);
2639 assert_return(data
, -EINVAL
);
2640 assert_return(l
, -EINVAL
);
2641 assert_return(j
->unique_field
, -EINVAL
);
2643 k
= strlen(j
->unique_field
);
2645 if (!j
->unique_file
) {
2646 if (j
->unique_file_lost
)
2649 j
->unique_file
= ordered_hashmap_first(j
->files
);
2650 if (!j
->unique_file
)
2653 j
->unique_offset
= 0;
2665 /* Proceed to next data object in the field's linked list */
2666 if (j
->unique_offset
== 0) {
2667 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2671 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2673 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2677 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2680 /* We reached the end of the list? Then start again, with the next file */
2681 if (j
->unique_offset
== 0) {
2682 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2683 if (!j
->unique_file
)
2689 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2690 * instead, so that we can look at this data object at the same
2691 * time as one on another file */
2692 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2696 /* Let's do the type check by hand, since we used 0 context above. */
2697 if (o
->object
.type
!= OBJECT_DATA
) {
2698 log_debug("%s:offset " OFSfmt
": object has type %d, expected %d",
2699 j
->unique_file
->path
, j
->unique_offset
,
2700 o
->object
.type
, OBJECT_DATA
);
2704 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2708 /* Check if we have at least the field name and "=". */
2710 log_debug("%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2711 j
->unique_file
->path
, j
->unique_offset
,
2716 if (memcmp(odata
, j
->unique_field
, k
) || ((const char*) odata
)[k
] != '=') {
2717 log_debug("%s:offset " OFSfmt
": object does not start with \"%s=\"",
2718 j
->unique_file
->path
, j
->unique_offset
,
2723 /* OK, now let's see if we already returned this data
2724 * object by checking if it exists in the earlier
2725 * traversed files. */
2727 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2728 if (of
== j
->unique_file
)
2731 /* Skip this file it didn't have any fields indexed */
2732 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2735 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
2747 r
= return_data(j
, j
->unique_file
, o
, data
, l
);
2755 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
2759 j
->unique_file
= NULL
;
2760 j
->unique_offset
= 0;
2761 j
->unique_file_lost
= false;
2764 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
2767 assert_return(j
, -EINVAL
);
2768 assert_return(!journal_pid_changed(j
), -ECHILD
);
2769 assert_return(field
, -EINVAL
);
2771 if (!j
->fields_file
) {
2772 if (j
->fields_file_lost
)
2775 j
->fields_file
= ordered_hashmap_first(j
->files
);
2776 if (!j
->fields_file
)
2779 j
->fields_hash_table_index
= 0;
2780 j
->fields_offset
= 0;
2784 JournalFile
*f
, *of
;
2793 if (j
->fields_offset
== 0) {
2796 /* We are not yet positioned at any field. Let's pick the first one */
2797 r
= journal_file_map_field_hash_table(f
);
2801 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
2803 if (j
->fields_hash_table_index
>= m
) {
2804 /* Reached the end of the hash table, go to the next file. */
2809 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
2811 if (j
->fields_offset
!= 0)
2814 /* Empty hash table bucket, go to next one */
2815 j
->fields_hash_table_index
++;
2819 /* Proceed with next file */
2820 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
2821 if (!j
->fields_file
) {
2826 j
->fields_offset
= 0;
2827 j
->fields_hash_table_index
= 0;
2832 /* We are already positioned at a field. If so, let's figure out the next field from it */
2834 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
2838 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
2839 if (j
->fields_offset
== 0) {
2840 /* Reached the end of the hash table chain */
2841 j
->fields_hash_table_index
++;
2846 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
2847 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
2851 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
2852 if (o
->object
.type
!= OBJECT_FIELD
) {
2853 log_debug("%s:offset " OFSfmt
": object has type %i, expected %i", f
->path
, j
->fields_offset
, o
->object
.type
, OBJECT_FIELD
);
2857 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
2859 /* Let's see if we already returned this field name before. */
2861 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2865 /* Skip this file it didn't have any fields indexed */
2866 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2869 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
, le64toh(o
->field
.hash
), NULL
, NULL
);
2881 /* Check if this is really a valid string containing no NUL byte */
2882 if (memchr(o
->field
.payload
, 0, sz
))
2885 if (sz
> j
->data_threshold
)
2886 sz
= j
->data_threshold
;
2888 if (!GREEDY_REALLOC(j
->fields_buffer
, j
->fields_buffer_allocated
, sz
+ 1))
2891 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
2892 j
->fields_buffer
[sz
] = 0;
2894 if (!field_is_valid(j
->fields_buffer
))
2897 *field
= j
->fields_buffer
;
2902 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
2906 j
->fields_file
= NULL
;
2907 j
->fields_hash_table_index
= 0;
2908 j
->fields_offset
= 0;
2909 j
->fields_file_lost
= false;
2912 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
2913 assert_return(j
, -EINVAL
);
2914 assert_return(!journal_pid_changed(j
), -ECHILD
);
2916 return !j
->on_network
;
2919 static char *lookup_field(const char *field
, void *userdata
) {
2920 sd_journal
*j
= userdata
;
2928 r
= sd_journal_get_data(j
, field
, &data
, &size
);
2930 size
> REPLACE_VAR_MAX
)
2931 return strdup(field
);
2933 d
= strlen(field
) + 1;
2935 return strndup((const char*) data
+ d
, size
- d
);
2938 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
2942 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
2946 assert_return(j
, -EINVAL
);
2947 assert_return(!journal_pid_changed(j
), -ECHILD
);
2948 assert_return(ret
, -EINVAL
);
2950 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
2954 cid
= strndup((const char*) data
+ 11, size
- 11);
2958 r
= sd_id128_from_string(cid
, &id
);
2962 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
2966 t
= replace_var(text
, lookup_field
, j
);
2974 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
2975 assert_return(ret
, -EINVAL
);
2977 return catalog_get(CATALOG_DATABASE
, id
, ret
);
2980 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
2981 assert_return(j
, -EINVAL
);
2982 assert_return(!journal_pid_changed(j
), -ECHILD
);
2984 j
->data_threshold
= sz
;
2988 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
2989 assert_return(j
, -EINVAL
);
2990 assert_return(!journal_pid_changed(j
), -ECHILD
);
2991 assert_return(sz
, -EINVAL
);
2993 *sz
= j
->data_threshold
;
2997 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
2998 assert_return(j
, -EINVAL
);
3000 return j
->has_runtime_files
;
3003 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
3004 assert_return(j
, -EINVAL
);
3006 return j
->has_persistent_files
;