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/>.
22 #include <linux/magic.h>
25 #include <sys/inotify.h>
29 #include "sd-journal.h"
31 #include "alloc-util.h"
34 #include "dirent-util.h"
37 #include "formats-util.h"
40 #include "hostname-util.h"
42 #include "journal-def.h"
43 #include "journal-file.h"
44 #include "journal-internal.h"
48 #include "path-util.h"
49 #include "replace-var.h"
50 #include "stat-util.h"
51 #include "stdio-util.h"
52 #include "string-util.h"
55 #define JOURNAL_FILES_MAX 7168
57 #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
59 #define REPLACE_VAR_MAX 256
61 #define DEFAULT_DATA_THRESHOLD (64*1024)
63 static void remove_file_real(sd_journal
*j
, JournalFile
*f
);
65 static bool journal_pid_changed(sd_journal
*j
) {
68 /* We don't support people creating a journal object and
69 * keeping it around over a fork(). Let's complain. */
71 return j
->original_pid
!= getpid();
74 static int journal_put_error(sd_journal
*j
, int r
, const char *path
) {
78 /* Memorize an error we encountered, and store which
79 * file/directory it was generated from. Note that we store
80 * only *one* path per error code, as the error code is the
81 * key into the hashmap, and the path is the value. This means
82 * we keep track only of all error kinds, but not of all error
83 * locations. This has the benefit that the hashmap cannot
86 * We return an error here only if we didn't manage to
87 * memorize the real error. */
92 k
= hashmap_ensure_allocated(&j
->errors
, NULL
);
103 k
= hashmap_put(j
->errors
, INT_TO_PTR(r
), copy
);
116 static void detach_location(sd_journal
*j
) {
122 j
->current_file
= NULL
;
123 j
->current_field
= 0;
125 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
)
126 journal_file_reset_location(f
);
129 static void reset_location(sd_journal
*j
) {
133 zero(j
->current_location
);
136 static void init_location(Location
*l
, LocationType type
, JournalFile
*f
, Object
*o
) {
138 assert(type
== LOCATION_DISCRETE
|| type
== LOCATION_SEEK
);
140 assert(o
->object
.type
== OBJECT_ENTRY
);
143 l
->seqnum
= le64toh(o
->entry
.seqnum
);
144 l
->seqnum_id
= f
->header
->seqnum_id
;
145 l
->realtime
= le64toh(o
->entry
.realtime
);
146 l
->monotonic
= le64toh(o
->entry
.monotonic
);
147 l
->boot_id
= o
->entry
.boot_id
;
148 l
->xor_hash
= le64toh(o
->entry
.xor_hash
);
150 l
->seqnum_set
= l
->realtime_set
= l
->monotonic_set
= l
->xor_hash_set
= true;
153 static void set_location(sd_journal
*j
, JournalFile
*f
, Object
*o
) {
158 init_location(&j
->current_location
, LOCATION_DISCRETE
, f
, o
);
161 j
->current_field
= 0;
163 /* Let f know its candidate entry was picked. */
164 assert(f
->location_type
== LOCATION_SEEK
);
165 f
->location_type
= LOCATION_DISCRETE
;
168 static int match_is_valid(const void *data
, size_t size
) {
176 if (startswith(data
, "__"))
180 for (p
= b
; p
< b
+ size
; p
++) {
188 if (*p
>= 'A' && *p
<= 'Z')
191 if (*p
>= '0' && *p
<= '9')
200 static bool same_field(const void *_a
, size_t s
, const void *_b
, size_t t
) {
201 const uint8_t *a
= _a
, *b
= _b
;
204 for (j
= 0; j
< s
&& j
< t
; j
++) {
213 assert_not_reached("\"=\" not found");
216 static Match
*match_new(Match
*p
, MatchType t
) {
227 LIST_PREPEND(matches
, p
->matches
, m
);
233 static void match_free(Match
*m
) {
237 match_free(m
->matches
);
240 LIST_REMOVE(matches
, m
->parent
->matches
, m
);
246 static void match_free_if_empty(Match
*m
) {
247 if (!m
|| m
->matches
)
253 _public_
int sd_journal_add_match(sd_journal
*j
, const void *data
, size_t size
) {
254 Match
*l3
, *l4
, *add_here
= NULL
, *m
;
257 assert_return(j
, -EINVAL
);
258 assert_return(!journal_pid_changed(j
), -ECHILD
);
259 assert_return(data
, -EINVAL
);
264 assert_return(match_is_valid(data
, size
), -EINVAL
);
270 * level 4: concrete matches */
273 j
->level0
= match_new(NULL
, MATCH_AND_TERM
);
279 j
->level1
= match_new(j
->level0
, MATCH_OR_TERM
);
285 j
->level2
= match_new(j
->level1
, MATCH_AND_TERM
);
290 assert(j
->level0
->type
== MATCH_AND_TERM
);
291 assert(j
->level1
->type
== MATCH_OR_TERM
);
292 assert(j
->level2
->type
== MATCH_AND_TERM
);
294 le_hash
= htole64(hash64(data
, size
));
296 LIST_FOREACH(matches
, l3
, j
->level2
->matches
) {
297 assert(l3
->type
== MATCH_OR_TERM
);
299 LIST_FOREACH(matches
, l4
, l3
->matches
) {
300 assert(l4
->type
== MATCH_DISCRETE
);
302 /* Exactly the same match already? Then ignore
304 if (l4
->le_hash
== le_hash
&&
306 memcmp(l4
->data
, data
, size
) == 0)
309 /* Same field? Then let's add this to this OR term */
310 if (same_field(data
, size
, l4
->data
, l4
->size
)) {
321 add_here
= match_new(j
->level2
, MATCH_OR_TERM
);
326 m
= match_new(add_here
, MATCH_DISCRETE
);
330 m
->le_hash
= le_hash
;
332 m
->data
= memdup(data
, size
);
341 match_free_if_empty(add_here
);
342 match_free_if_empty(j
->level2
);
343 match_free_if_empty(j
->level1
);
344 match_free_if_empty(j
->level0
);
349 _public_
int sd_journal_add_conjunction(sd_journal
*j
) {
350 assert_return(j
, -EINVAL
);
351 assert_return(!journal_pid_changed(j
), -ECHILD
);
359 if (!j
->level1
->matches
)
368 _public_
int sd_journal_add_disjunction(sd_journal
*j
) {
369 assert_return(j
, -EINVAL
);
370 assert_return(!journal_pid_changed(j
), -ECHILD
);
381 if (!j
->level2
->matches
)
388 static char *match_make_string(Match
*m
) {
391 bool enclose
= false;
394 return strdup("none");
396 if (m
->type
== MATCH_DISCRETE
)
397 return strndup(m
->data
, m
->size
);
400 LIST_FOREACH(matches
, i
, m
->matches
) {
403 t
= match_make_string(i
);
410 k
= strjoin(p
, m
->type
== MATCH_OR_TERM
? " OR " : " AND ", t
, NULL
);
425 r
= strjoin("(", p
, ")", NULL
);
433 char *journal_make_match_string(sd_journal
*j
) {
436 return match_make_string(j
->level0
);
439 _public_
void sd_journal_flush_matches(sd_journal
*j
) {
444 match_free(j
->level0
);
446 j
->level0
= j
->level1
= j
->level2
= NULL
;
451 _pure_
static int compare_with_location(JournalFile
*f
, Location
*l
) {
454 assert(f
->location_type
== LOCATION_SEEK
);
455 assert(l
->type
== LOCATION_DISCRETE
|| l
->type
== LOCATION_SEEK
);
457 if (l
->monotonic_set
&&
458 sd_id128_equal(f
->current_boot_id
, l
->boot_id
) &&
460 f
->current_realtime
== l
->realtime
&&
462 f
->current_xor_hash
== l
->xor_hash
)
466 sd_id128_equal(f
->header
->seqnum_id
, l
->seqnum_id
)) {
468 if (f
->current_seqnum
< l
->seqnum
)
470 if (f
->current_seqnum
> l
->seqnum
)
474 if (l
->monotonic_set
&&
475 sd_id128_equal(f
->current_boot_id
, l
->boot_id
)) {
477 if (f
->current_monotonic
< l
->monotonic
)
479 if (f
->current_monotonic
> l
->monotonic
)
483 if (l
->realtime_set
) {
485 if (f
->current_realtime
< l
->realtime
)
487 if (f
->current_realtime
> l
->realtime
)
491 if (l
->xor_hash_set
) {
493 if (f
->current_xor_hash
< l
->xor_hash
)
495 if (f
->current_xor_hash
> l
->xor_hash
)
502 static int next_for_match(
506 uint64_t after_offset
,
507 direction_t direction
,
519 if (m
->type
== MATCH_DISCRETE
) {
522 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
526 return journal_file_move_to_entry_by_offset_for_data(f
, dp
, after_offset
, direction
, ret
, offset
);
528 } else if (m
->type
== MATCH_OR_TERM
) {
531 /* Find the earliest match beyond after_offset */
533 LIST_FOREACH(matches
, i
, m
->matches
) {
536 r
= next_for_match(j
, i
, f
, after_offset
, direction
, NULL
, &cp
);
540 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
< np
: cp
> np
))
548 } else if (m
->type
== MATCH_AND_TERM
) {
549 Match
*i
, *last_moved
;
551 /* Always jump to the next matching entry and repeat
552 * this until we find an offset that matches for all
558 r
= next_for_match(j
, m
->matches
, f
, after_offset
, direction
, NULL
, &np
);
562 assert(direction
== DIRECTION_DOWN
? np
>= after_offset
: np
<= after_offset
);
563 last_moved
= m
->matches
;
565 LIST_LOOP_BUT_ONE(matches
, i
, m
->matches
, last_moved
) {
568 r
= next_for_match(j
, i
, f
, np
, direction
, NULL
, &cp
);
572 assert(direction
== DIRECTION_DOWN
? cp
>= np
: cp
<= np
);
573 if (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
) {
582 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
594 static int find_location_for_match(
598 direction_t direction
,
608 if (m
->type
== MATCH_DISCRETE
) {
611 r
= journal_file_find_data_object_with_hash(f
, m
->data
, m
->size
, le64toh(m
->le_hash
), NULL
, &dp
);
615 /* FIXME: missing: find by monotonic */
617 if (j
->current_location
.type
== LOCATION_HEAD
)
618 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_DOWN
, ret
, offset
);
619 if (j
->current_location
.type
== LOCATION_TAIL
)
620 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, DIRECTION_UP
, ret
, offset
);
621 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
622 return journal_file_move_to_entry_by_seqnum_for_data(f
, dp
, j
->current_location
.seqnum
, direction
, ret
, offset
);
623 if (j
->current_location
.monotonic_set
) {
624 r
= journal_file_move_to_entry_by_monotonic_for_data(f
, dp
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
628 if (j
->current_location
.realtime_set
)
629 return journal_file_move_to_entry_by_realtime_for_data(f
, dp
, j
->current_location
.realtime
, direction
, ret
, offset
);
631 return journal_file_next_entry_for_data(f
, NULL
, 0, dp
, direction
, ret
, offset
);
633 } else if (m
->type
== MATCH_OR_TERM
) {
638 /* Find the earliest match */
640 LIST_FOREACH(matches
, i
, m
->matches
) {
643 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
647 if (np
== 0 || (direction
== DIRECTION_DOWN
? np
> cp
: np
< cp
))
655 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, np
, &n
);
670 assert(m
->type
== MATCH_AND_TERM
);
672 /* First jump to the last match, and then find the
673 * next one where all matches match */
678 LIST_FOREACH(matches
, i
, m
->matches
) {
681 r
= find_location_for_match(j
, i
, f
, direction
, NULL
, &cp
);
685 if (np
== 0 || (direction
== DIRECTION_DOWN
? cp
> np
: cp
< np
))
689 return next_for_match(j
, m
, f
, np
, direction
, ret
, offset
);
693 static int find_location_with_matches(
696 direction_t direction
,
708 /* No matches is simple */
710 if (j
->current_location
.type
== LOCATION_HEAD
)
711 return journal_file_next_entry(f
, 0, DIRECTION_DOWN
, ret
, offset
);
712 if (j
->current_location
.type
== LOCATION_TAIL
)
713 return journal_file_next_entry(f
, 0, DIRECTION_UP
, ret
, offset
);
714 if (j
->current_location
.seqnum_set
&& sd_id128_equal(j
->current_location
.seqnum_id
, f
->header
->seqnum_id
))
715 return journal_file_move_to_entry_by_seqnum(f
, j
->current_location
.seqnum
, direction
, ret
, offset
);
716 if (j
->current_location
.monotonic_set
) {
717 r
= journal_file_move_to_entry_by_monotonic(f
, j
->current_location
.boot_id
, j
->current_location
.monotonic
, direction
, ret
, offset
);
721 if (j
->current_location
.realtime_set
)
722 return journal_file_move_to_entry_by_realtime(f
, j
->current_location
.realtime
, direction
, ret
, offset
);
724 return journal_file_next_entry(f
, 0, direction
, ret
, offset
);
726 return find_location_for_match(j
, j
->level0
, f
, direction
, ret
, offset
);
729 static int next_with_matches(
732 direction_t direction
,
741 /* No matches is easy. We simple advance the file
744 return journal_file_next_entry(f
, f
->current_offset
, direction
, ret
, offset
);
746 /* If we have a match then we look for the next matching entry
747 * with an offset at least one step larger */
748 return next_for_match(j
, j
->level0
, f
,
749 direction
== DIRECTION_DOWN
? f
->current_offset
+ 1
750 : f
->current_offset
- 1,
751 direction
, ret
, offset
);
754 static int next_beyond_location(sd_journal
*j
, JournalFile
*f
, direction_t direction
) {
756 uint64_t cp
, n_entries
;
762 n_entries
= le64toh(f
->header
->n_entries
);
764 /* If we hit EOF before, we don't need to look into this file again
765 * unless direction changed or new entries appeared. */
766 if (f
->last_direction
== direction
&& f
->location_type
== LOCATION_TAIL
&&
767 n_entries
== f
->last_n_entries
)
770 f
->last_n_entries
= n_entries
;
772 if (f
->last_direction
== direction
&& f
->current_offset
> 0) {
773 /* LOCATION_SEEK here means we did the work in a previous
774 * iteration and the current location already points to a
775 * candidate entry. */
776 if (f
->location_type
!= LOCATION_SEEK
) {
777 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
781 journal_file_save_location(f
, c
, cp
);
784 f
->last_direction
= direction
;
786 r
= find_location_with_matches(j
, f
, direction
, &c
, &cp
);
790 journal_file_save_location(f
, c
, cp
);
793 /* OK, we found the spot, now let's advance until an entry
794 * that is actually different from what we were previously
795 * looking at. This is necessary to handle entries which exist
796 * in two (or more) journal files, and which shall all be
797 * suppressed but one. */
802 if (j
->current_location
.type
== LOCATION_DISCRETE
) {
805 k
= compare_with_location(f
, &j
->current_location
);
807 found
= direction
== DIRECTION_DOWN
? k
> 0 : k
< 0;
814 r
= next_with_matches(j
, f
, direction
, &c
, &cp
);
818 journal_file_save_location(f
, c
, cp
);
822 static int real_journal_next(sd_journal
*j
, direction_t direction
) {
823 JournalFile
*f
, *new_file
= NULL
;
828 assert_return(j
, -EINVAL
);
829 assert_return(!journal_pid_changed(j
), -ECHILD
);
831 ORDERED_HASHMAP_FOREACH(f
, j
->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
);
885 /* If this is not a discrete skip, then at least
886 * resolve the current location */
887 if (j
->current_location
.type
!= LOCATION_DISCRETE
)
888 return real_journal_next(j
, direction
);
894 r
= real_journal_next(j
, direction
);
908 _public_
int sd_journal_next_skip(sd_journal
*j
, uint64_t skip
) {
909 return real_journal_next_skip(j
, DIRECTION_DOWN
, skip
);
912 _public_
int sd_journal_previous_skip(sd_journal
*j
, uint64_t skip
) {
913 return real_journal_next_skip(j
, DIRECTION_UP
, skip
);
916 _public_
int sd_journal_get_cursor(sd_journal
*j
, char **cursor
) {
919 char bid
[33], sid
[33];
921 assert_return(j
, -EINVAL
);
922 assert_return(!journal_pid_changed(j
), -ECHILD
);
923 assert_return(cursor
, -EINVAL
);
925 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
926 return -EADDRNOTAVAIL
;
928 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
932 sd_id128_to_string(j
->current_file
->header
->seqnum_id
, sid
);
933 sd_id128_to_string(o
->entry
.boot_id
, bid
);
936 "s=%s;i=%"PRIx64
";b=%s;m=%"PRIx64
";t=%"PRIx64
";x=%"PRIx64
,
937 sid
, le64toh(o
->entry
.seqnum
),
938 bid
, 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 const char *word
, *state
;
949 unsigned long long seqnum
, monotonic
, realtime
, xor_hash
;
951 seqnum_id_set
= false,
954 monotonic_set
= false,
955 realtime_set
= false,
956 xor_hash_set
= false;
957 sd_id128_t seqnum_id
, boot_id
;
959 assert_return(j
, -EINVAL
);
960 assert_return(!journal_pid_changed(j
), -ECHILD
);
961 assert_return(!isempty(cursor
), -EINVAL
);
963 FOREACH_WORD_SEPARATOR(word
, l
, cursor
, ";", state
) {
967 if (l
< 2 || word
[1] != '=')
970 item
= strndup(word
, l
);
977 seqnum_id_set
= true;
978 k
= sd_id128_from_string(item
+2, &seqnum_id
);
983 if (sscanf(item
+2, "%llx", &seqnum
) != 1)
989 k
= sd_id128_from_string(item
+2, &boot_id
);
993 monotonic_set
= true;
994 if (sscanf(item
+2, "%llx", &monotonic
) != 1)
1000 if (sscanf(item
+2, "%llx", &realtime
) != 1)
1005 xor_hash_set
= true;
1006 if (sscanf(item
+2, "%llx", &xor_hash
) != 1)
1017 if ((!seqnum_set
|| !seqnum_id_set
) &&
1018 (!monotonic_set
|| !boot_id_set
) &&
1024 j
->current_location
.type
= LOCATION_SEEK
;
1027 j
->current_location
.realtime
= (uint64_t) realtime
;
1028 j
->current_location
.realtime_set
= true;
1031 if (seqnum_set
&& seqnum_id_set
) {
1032 j
->current_location
.seqnum
= (uint64_t) seqnum
;
1033 j
->current_location
.seqnum_id
= seqnum_id
;
1034 j
->current_location
.seqnum_set
= true;
1037 if (monotonic_set
&& boot_id_set
) {
1038 j
->current_location
.monotonic
= (uint64_t) monotonic
;
1039 j
->current_location
.boot_id
= boot_id
;
1040 j
->current_location
.monotonic_set
= true;
1044 j
->current_location
.xor_hash
= (uint64_t) xor_hash
;
1045 j
->current_location
.xor_hash_set
= true;
1051 _public_
int sd_journal_test_cursor(sd_journal
*j
, const char *cursor
) {
1055 assert_return(j
, -EINVAL
);
1056 assert_return(!journal_pid_changed(j
), -ECHILD
);
1057 assert_return(!isempty(cursor
), -EINVAL
);
1059 if (!j
->current_file
|| j
->current_file
->current_offset
<= 0)
1060 return -EADDRNOTAVAIL
;
1062 r
= journal_file_move_to_object(j
->current_file
, OBJECT_ENTRY
, j
->current_file
->current_offset
, &o
);
1067 _cleanup_free_
char *item
= NULL
;
1068 unsigned long long ll
;
1072 r
= extract_first_word(&cursor
, &item
, ";", EXTRACT_DONT_COALESCE_SEPARATORS
);
1079 if (strlen(item
) < 2 || item
[1] != '=')
1085 k
= sd_id128_from_string(item
+2, &id
);
1088 if (!sd_id128_equal(id
, j
->current_file
->header
->seqnum_id
))
1093 if (sscanf(item
+2, "%llx", &ll
) != 1)
1095 if (ll
!= le64toh(o
->entry
.seqnum
))
1100 k
= sd_id128_from_string(item
+2, &id
);
1103 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1108 if (sscanf(item
+2, "%llx", &ll
) != 1)
1110 if (ll
!= le64toh(o
->entry
.monotonic
))
1115 if (sscanf(item
+2, "%llx", &ll
) != 1)
1117 if (ll
!= le64toh(o
->entry
.realtime
))
1122 if (sscanf(item
+2, "%llx", &ll
) != 1)
1124 if (ll
!= le64toh(o
->entry
.xor_hash
))
1134 _public_
int sd_journal_seek_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t usec
) {
1135 assert_return(j
, -EINVAL
);
1136 assert_return(!journal_pid_changed(j
), -ECHILD
);
1139 j
->current_location
.type
= LOCATION_SEEK
;
1140 j
->current_location
.boot_id
= boot_id
;
1141 j
->current_location
.monotonic
= usec
;
1142 j
->current_location
.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
);
1152 j
->current_location
.type
= LOCATION_SEEK
;
1153 j
->current_location
.realtime
= usec
;
1154 j
->current_location
.realtime_set
= true;
1159 _public_
int sd_journal_seek_head(sd_journal
*j
) {
1160 assert_return(j
, -EINVAL
);
1161 assert_return(!journal_pid_changed(j
), -ECHILD
);
1164 j
->current_location
.type
= LOCATION_HEAD
;
1169 _public_
int sd_journal_seek_tail(sd_journal
*j
) {
1170 assert_return(j
, -EINVAL
);
1171 assert_return(!journal_pid_changed(j
), -ECHILD
);
1174 j
->current_location
.type
= LOCATION_TAIL
;
1179 static void check_network(sd_journal
*j
, int fd
) {
1187 if (fstatfs(fd
, &sfs
) < 0)
1191 F_TYPE_EQUAL(sfs
.f_type
, CIFS_MAGIC_NUMBER
) ||
1192 F_TYPE_EQUAL(sfs
.f_type
, CODA_SUPER_MAGIC
) ||
1193 F_TYPE_EQUAL(sfs
.f_type
, NCP_SUPER_MAGIC
) ||
1194 F_TYPE_EQUAL(sfs
.f_type
, NFS_SUPER_MAGIC
) ||
1195 F_TYPE_EQUAL(sfs
.f_type
, SMB_SUPER_MAGIC
);
1198 static bool file_has_type_prefix(const char *prefix
, const char *filename
) {
1199 const char *full
, *tilded
, *atted
;
1201 full
= strjoina(prefix
, ".journal");
1202 tilded
= strjoina(full
, "~");
1203 atted
= strjoina(prefix
, "@");
1205 return streq(filename
, full
) ||
1206 streq(filename
, tilded
) ||
1207 startswith(filename
, atted
);
1210 static bool file_type_wanted(int flags
, const char *filename
) {
1213 if (!endswith(filename
, ".journal") && !endswith(filename
, ".journal~"))
1216 /* no flags set → every type is OK */
1217 if (!(flags
& (SD_JOURNAL_SYSTEM
| SD_JOURNAL_CURRENT_USER
)))
1220 if (flags
& SD_JOURNAL_SYSTEM
&& file_has_type_prefix("system", filename
))
1223 if (flags
& SD_JOURNAL_CURRENT_USER
) {
1224 char prefix
[5 + DECIMAL_STR_MAX(uid_t
) + 1];
1226 xsprintf(prefix
, "user-"UID_FMT
, getuid());
1228 if (file_has_type_prefix(prefix
, filename
))
1235 static int add_any_file(sd_journal
*j
, const char *path
) {
1236 JournalFile
*f
= NULL
;
1242 if (ordered_hashmap_get(j
->files
, path
))
1245 if (ordered_hashmap_size(j
->files
) >= JOURNAL_FILES_MAX
) {
1246 log_debug("Too many open journal files, not adding %s.", path
);
1251 r
= journal_file_open(path
, O_RDONLY
, 0, false, false, NULL
, j
->mmap
, NULL
, NULL
, &f
);
1253 log_debug_errno(r
, "Failed to open journal file %s: %m", path
);
1257 /* journal_file_dump(f); */
1259 r
= ordered_hashmap_put(j
->files
, f
->path
, f
);
1261 (void) journal_file_close(f
);
1265 log_debug("File %s added.", f
->path
);
1267 check_network(j
, f
->fd
);
1269 j
->current_invalidate_counter
++;
1274 k
= journal_put_error(j
, r
, path
);
1281 static int add_file(sd_journal
*j
, const char *prefix
, const char *filename
) {
1288 if (j
->no_new_files
||
1289 !file_type_wanted(j
->flags
, filename
))
1292 path
= strjoina(prefix
, "/", filename
);
1294 if (!j
->has_runtime_files
&& path_startswith(path
, "/run/log/journal"))
1295 j
->has_runtime_files
= true;
1296 else if (!j
->has_persistent_files
&& path_startswith(path
, "/var/log/journal"))
1297 j
->has_persistent_files
= true;
1299 return add_any_file(j
, path
);
1302 static void remove_file(sd_journal
*j
, const char *prefix
, const char *filename
) {
1310 path
= strjoina(prefix
, "/", filename
);
1311 f
= ordered_hashmap_get(j
->files
, path
);
1315 remove_file_real(j
, f
);
1318 static void remove_file_real(sd_journal
*j
, JournalFile
*f
) {
1322 ordered_hashmap_remove(j
->files
, f
->path
);
1324 log_debug("File %s removed.", f
->path
);
1326 if (j
->current_file
== f
) {
1327 j
->current_file
= NULL
;
1328 j
->current_field
= 0;
1331 if (j
->unique_file
== f
) {
1332 /* Jump to the next unique_file or NULL if that one was last */
1333 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
1334 j
->unique_offset
= 0;
1335 if (!j
->unique_file
)
1336 j
->unique_file_lost
= true;
1339 if (j
->fields_file
== f
) {
1340 j
->fields_file
= ordered_hashmap_next(j
->files
, j
->fields_file
->path
);
1341 j
->fields_offset
= 0;
1342 if (!j
->fields_file
)
1343 j
->fields_file_lost
= true;
1346 (void) journal_file_close(f
);
1348 j
->current_invalidate_counter
++;
1351 static int dirname_is_machine_id(const char *fn
) {
1352 sd_id128_t id
, machine
;
1355 r
= sd_id128_get_machine(&machine
);
1359 r
= sd_id128_from_string(fn
, &id
);
1363 return sd_id128_equal(id
, machine
);
1366 static int add_directory(sd_journal
*j
, const char *prefix
, const char *dirname
) {
1367 _cleanup_free_
char *path
= NULL
;
1368 _cleanup_closedir_
DIR *d
= NULL
;
1369 struct dirent
*de
= NULL
;
1377 log_debug("Considering %s/%s.", prefix
, dirname
);
1379 if ((j
->flags
& SD_JOURNAL_LOCAL_ONLY
) &&
1380 !(dirname_is_machine_id(dirname
) > 0 || path_startswith(prefix
, "/run")))
1383 path
= strjoin(prefix
, "/", dirname
, NULL
);
1391 r
= log_debug_errno(errno
, "Failed to open directory %s: %m", path
);
1395 m
= hashmap_get(j
->directories_by_path
, path
);
1397 m
= new0(Directory
, 1);
1406 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1412 path
= NULL
; /* avoid freeing in cleanup */
1413 j
->current_invalidate_counter
++;
1415 log_debug("Directory %s added.", m
->path
);
1417 } else if (m
->is_root
)
1420 if (m
->wd
<= 0 && j
->inotify_fd
>= 0) {
1422 m
->wd
= inotify_add_watch(j
->inotify_fd
, m
->path
,
1423 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1424 IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
|IN_MOVED_FROM
|
1427 if (m
->wd
> 0 && hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
) < 0)
1428 inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1431 FOREACH_DIRENT_ALL(de
, d
, return log_debug_errno(errno
, "Failed to read directory %s: %m", m
->path
)) {
1433 if (dirent_is_file_with_suffix(de
, ".journal") ||
1434 dirent_is_file_with_suffix(de
, ".journal~"))
1435 (void) add_file(j
, m
->path
, de
->d_name
);
1438 check_network(j
, dirfd(d
));
1443 k
= journal_put_error(j
, r
, path
?: dirname
);
1450 static int add_root_directory(sd_journal
*j
, const char *p
, bool missing_ok
) {
1451 _cleanup_closedir_
DIR *d
= NULL
;
1459 if ((j
->flags
& SD_JOURNAL_RUNTIME_ONLY
) &&
1460 !path_startswith(p
, "/run"))
1464 p
= strjoina(j
->prefix
, p
);
1468 if (errno
== ENOENT
&& missing_ok
)
1471 r
= log_debug_errno(errno
, "Failed to open root directory %s: %m", p
);
1475 m
= hashmap_get(j
->directories_by_path
, p
);
1477 m
= new0(Directory
, 1);
1484 m
->path
= strdup(p
);
1491 if (hashmap_put(j
->directories_by_path
, m
->path
, m
) < 0) {
1498 j
->current_invalidate_counter
++;
1500 log_debug("Root directory %s added.", m
->path
);
1502 } else if (!m
->is_root
)
1505 if (m
->wd
<= 0 && j
->inotify_fd
>= 0) {
1507 m
->wd
= inotify_add_watch(j
->inotify_fd
, m
->path
,
1508 IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
|IN_DELETE
|
1511 if (m
->wd
> 0 && hashmap_put(j
->directories_by_wd
, INT_TO_PTR(m
->wd
), m
) < 0)
1512 inotify_rm_watch(j
->inotify_fd
, m
->wd
);
1515 if (j
->no_new_files
)
1518 FOREACH_DIRENT_ALL(de
, d
, return log_debug_errno(errno
, "Failed to read directory %s: %m", m
->path
)) {
1521 if (dirent_is_file_with_suffix(de
, ".journal") ||
1522 dirent_is_file_with_suffix(de
, ".journal~"))
1523 (void) add_file(j
, m
->path
, de
->d_name
);
1524 else if (IN_SET(de
->d_type
, DT_DIR
, DT_LNK
, DT_UNKNOWN
) &&
1525 sd_id128_from_string(de
->d_name
, &id
) >= 0)
1526 (void) add_directory(j
, m
->path
, de
->d_name
);
1529 check_network(j
, dirfd(d
));
1534 k
= journal_put_error(j
, r
, p
);
1541 static void remove_directory(sd_journal
*j
, Directory
*d
) {
1545 hashmap_remove(j
->directories_by_wd
, INT_TO_PTR(d
->wd
));
1547 if (j
->inotify_fd
>= 0)
1548 inotify_rm_watch(j
->inotify_fd
, d
->wd
);
1551 hashmap_remove(j
->directories_by_path
, d
->path
);
1554 log_debug("Root directory %s removed.", d
->path
);
1556 log_debug("Directory %s removed.", d
->path
);
1562 static int add_search_paths(sd_journal
*j
) {
1564 static const char search_paths
[] =
1565 "/run/log/journal\0"
1566 "/var/log/journal\0";
1571 /* We ignore most errors here, since the idea is to only open
1572 * what's actually accessible, and ignore the rest. */
1574 NULSTR_FOREACH(p
, search_paths
)
1575 (void) add_root_directory(j
, p
, true);
1580 static int add_current_paths(sd_journal
*j
) {
1585 assert(j
->no_new_files
);
1587 /* Simply adds all directories for files we have open as
1588 * "root" directories. We don't expect errors here, so we
1589 * treat them as fatal. */
1591 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
1592 _cleanup_free_
char *dir
;
1595 dir
= dirname_malloc(f
->path
);
1599 r
= add_root_directory(j
, dir
, true);
1607 static int allocate_inotify(sd_journal
*j
) {
1610 if (j
->inotify_fd
< 0) {
1611 j
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1612 if (j
->inotify_fd
< 0)
1616 if (!j
->directories_by_wd
) {
1617 j
->directories_by_wd
= hashmap_new(NULL
);
1618 if (!j
->directories_by_wd
)
1625 static sd_journal
*journal_new(int flags
, const char *path
) {
1628 j
= new0(sd_journal
, 1);
1632 j
->original_pid
= getpid();
1635 j
->data_threshold
= DEFAULT_DATA_THRESHOLD
;
1638 j
->path
= strdup(path
);
1643 j
->files
= ordered_hashmap_new(&string_hash_ops
);
1644 j
->directories_by_path
= hashmap_new(&string_hash_ops
);
1645 j
->mmap
= mmap_cache_new();
1646 if (!j
->files
|| !j
->directories_by_path
|| !j
->mmap
)
1652 sd_journal_close(j
);
1656 _public_
int sd_journal_open(sd_journal
**ret
, int flags
) {
1660 assert_return(ret
, -EINVAL
);
1661 assert_return((flags
& ~(SD_JOURNAL_LOCAL_ONLY
|SD_JOURNAL_RUNTIME_ONLY
|SD_JOURNAL_SYSTEM
|SD_JOURNAL_CURRENT_USER
)) == 0, -EINVAL
);
1663 j
= journal_new(flags
, NULL
);
1667 r
= add_search_paths(j
);
1675 sd_journal_close(j
);
1680 _public_
int sd_journal_open_container(sd_journal
**ret
, const char *machine
, int flags
) {
1681 _cleanup_free_
char *root
= NULL
, *class = NULL
;
1686 assert_return(machine
, -EINVAL
);
1687 assert_return(ret
, -EINVAL
);
1688 assert_return((flags
& ~(SD_JOURNAL_LOCAL_ONLY
|SD_JOURNAL_SYSTEM
)) == 0, -EINVAL
);
1689 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1691 p
= strjoina("/run/systemd/machines/", machine
);
1692 r
= parse_env_file(p
, NEWLINE
, "ROOT", &root
, "CLASS", &class, NULL
);
1700 if (!streq_ptr(class, "container"))
1703 j
= journal_new(flags
, NULL
);
1710 r
= add_search_paths(j
);
1718 sd_journal_close(j
);
1722 _public_
int sd_journal_open_directory(sd_journal
**ret
, const char *path
, int flags
) {
1726 assert_return(ret
, -EINVAL
);
1727 assert_return(path
, -EINVAL
);
1728 assert_return(flags
== 0, -EINVAL
);
1730 j
= journal_new(flags
, path
);
1734 r
= add_root_directory(j
, path
, false);
1742 sd_journal_close(j
);
1747 _public_
int sd_journal_open_files(sd_journal
**ret
, const char **paths
, int flags
) {
1752 assert_return(ret
, -EINVAL
);
1753 assert_return(flags
== 0, -EINVAL
);
1755 j
= journal_new(flags
, NULL
);
1759 STRV_FOREACH(path
, paths
) {
1760 r
= add_any_file(j
, *path
);
1765 j
->no_new_files
= true;
1771 sd_journal_close(j
);
1776 _public_
void sd_journal_close(sd_journal
*j
) {
1784 sd_journal_flush_matches(j
);
1786 while ((f
= ordered_hashmap_steal_first(j
->files
)))
1787 (void) journal_file_close(f
);
1789 ordered_hashmap_free(j
->files
);
1791 while ((d
= hashmap_first(j
->directories_by_path
)))
1792 remove_directory(j
, d
);
1794 while ((d
= hashmap_first(j
->directories_by_wd
)))
1795 remove_directory(j
, d
);
1797 hashmap_free(j
->directories_by_path
);
1798 hashmap_free(j
->directories_by_wd
);
1800 safe_close(j
->inotify_fd
);
1803 log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j
->mmap
), mmap_cache_get_missed(j
->mmap
));
1804 mmap_cache_unref(j
->mmap
);
1807 while ((p
= hashmap_steal_first(j
->errors
)))
1809 hashmap_free(j
->errors
);
1813 free(j
->unique_field
);
1814 free(j
->fields_buffer
);
1818 _public_
int sd_journal_get_realtime_usec(sd_journal
*j
, uint64_t *ret
) {
1823 assert_return(j
, -EINVAL
);
1824 assert_return(!journal_pid_changed(j
), -ECHILD
);
1825 assert_return(ret
, -EINVAL
);
1827 f
= j
->current_file
;
1829 return -EADDRNOTAVAIL
;
1831 if (f
->current_offset
<= 0)
1832 return -EADDRNOTAVAIL
;
1834 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
1838 *ret
= le64toh(o
->entry
.realtime
);
1842 _public_
int sd_journal_get_monotonic_usec(sd_journal
*j
, uint64_t *ret
, sd_id128_t
*ret_boot_id
) {
1848 assert_return(j
, -EINVAL
);
1849 assert_return(!journal_pid_changed(j
), -ECHILD
);
1851 f
= j
->current_file
;
1853 return -EADDRNOTAVAIL
;
1855 if (f
->current_offset
<= 0)
1856 return -EADDRNOTAVAIL
;
1858 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
1863 *ret_boot_id
= o
->entry
.boot_id
;
1865 r
= sd_id128_get_boot(&id
);
1869 if (!sd_id128_equal(id
, o
->entry
.boot_id
))
1874 *ret
= le64toh(o
->entry
.monotonic
);
1879 static bool field_is_valid(const char *field
) {
1887 if (startswith(field
, "__"))
1890 for (p
= field
; *p
; p
++) {
1895 if (*p
>= 'A' && *p
<= 'Z')
1898 if (*p
>= '0' && *p
<= '9')
1907 _public_
int sd_journal_get_data(sd_journal
*j
, const char *field
, const void **data
, size_t *size
) {
1910 size_t field_length
;
1914 assert_return(j
, -EINVAL
);
1915 assert_return(!journal_pid_changed(j
), -ECHILD
);
1916 assert_return(field
, -EINVAL
);
1917 assert_return(data
, -EINVAL
);
1918 assert_return(size
, -EINVAL
);
1919 assert_return(field_is_valid(field
), -EINVAL
);
1921 f
= j
->current_file
;
1923 return -EADDRNOTAVAIL
;
1925 if (f
->current_offset
<= 0)
1926 return -EADDRNOTAVAIL
;
1928 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
1932 field_length
= strlen(field
);
1934 n
= journal_file_entry_n_items(o
);
1935 for (i
= 0; i
< n
; i
++) {
1941 p
= le64toh(o
->entry
.items
[i
].object_offset
);
1942 le_hash
= o
->entry
.items
[i
].hash
;
1943 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
1947 if (le_hash
!= o
->data
.hash
)
1950 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
1952 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
1954 #if defined(HAVE_XZ) || defined(HAVE_LZ4)
1955 r
= decompress_startswith(compression
,
1957 &f
->compress_buffer
, &f
->compress_buffer_size
,
1958 field
, field_length
, '=');
1960 log_debug_errno(r
, "Cannot decompress %s object of length %zu at offset "OFSfmt
": %m",
1961 object_compressed_to_string(compression
), l
, p
);
1966 r
= decompress_blob(compression
,
1968 &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
,
1973 *data
= f
->compress_buffer
;
1974 *size
= (size_t) rsize
;
1979 return -EPROTONOSUPPORT
;
1981 } else if (l
>= field_length
+1 &&
1982 memcmp(o
->data
.payload
, field
, field_length
) == 0 &&
1983 o
->data
.payload
[field_length
] == '=') {
1987 if ((uint64_t) t
!= l
)
1990 *data
= o
->data
.payload
;
1996 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2004 static int return_data(sd_journal
*j
, JournalFile
*f
, Object
*o
, const void **data
, size_t *size
) {
2009 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2012 /* We can't read objects larger than 4G on a 32bit machine */
2013 if ((uint64_t) t
!= l
)
2016 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
2018 #if defined(HAVE_XZ) || defined(HAVE_LZ4)
2022 r
= decompress_blob(compression
,
2023 o
->data
.payload
, l
, &f
->compress_buffer
,
2024 &f
->compress_buffer_size
, &rsize
, j
->data_threshold
);
2028 *data
= f
->compress_buffer
;
2029 *size
= (size_t) rsize
;
2031 return -EPROTONOSUPPORT
;
2034 *data
= o
->data
.payload
;
2041 _public_
int sd_journal_enumerate_data(sd_journal
*j
, const void **data
, size_t *size
) {
2048 assert_return(j
, -EINVAL
);
2049 assert_return(!journal_pid_changed(j
), -ECHILD
);
2050 assert_return(data
, -EINVAL
);
2051 assert_return(size
, -EINVAL
);
2053 f
= j
->current_file
;
2055 return -EADDRNOTAVAIL
;
2057 if (f
->current_offset
<= 0)
2058 return -EADDRNOTAVAIL
;
2060 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2064 n
= journal_file_entry_n_items(o
);
2065 if (j
->current_field
>= n
)
2068 p
= le64toh(o
->entry
.items
[j
->current_field
].object_offset
);
2069 le_hash
= o
->entry
.items
[j
->current_field
].hash
;
2070 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2074 if (le_hash
!= o
->data
.hash
)
2077 r
= return_data(j
, f
, o
, data
, size
);
2086 _public_
void sd_journal_restart_data(sd_journal
*j
) {
2090 j
->current_field
= 0;
2093 _public_
int sd_journal_get_fd(sd_journal
*j
) {
2096 assert_return(j
, -EINVAL
);
2097 assert_return(!journal_pid_changed(j
), -ECHILD
);
2099 if (j
->inotify_fd
>= 0)
2100 return j
->inotify_fd
;
2102 r
= allocate_inotify(j
);
2106 /* Iterate through all dirs again, to add them to the
2108 if (j
->no_new_files
)
2109 r
= add_current_paths(j
);
2111 r
= add_root_directory(j
, j
->path
, true);
2113 r
= add_search_paths(j
);
2117 return j
->inotify_fd
;
2120 _public_
int sd_journal_get_events(sd_journal
*j
) {
2123 assert_return(j
, -EINVAL
);
2124 assert_return(!journal_pid_changed(j
), -ECHILD
);
2126 fd
= sd_journal_get_fd(j
);
2133 _public_
int sd_journal_get_timeout(sd_journal
*j
, uint64_t *timeout_usec
) {
2136 assert_return(j
, -EINVAL
);
2137 assert_return(!journal_pid_changed(j
), -ECHILD
);
2138 assert_return(timeout_usec
, -EINVAL
);
2140 fd
= sd_journal_get_fd(j
);
2144 if (!j
->on_network
) {
2145 *timeout_usec
= (uint64_t) -1;
2149 /* If we are on the network we need to regularly check for
2150 * changes manually */
2152 *timeout_usec
= j
->last_process_usec
+ JOURNAL_FILES_RECHECK_USEC
;
2156 static void process_inotify_event(sd_journal
*j
, struct inotify_event
*e
) {
2162 /* Is this a subdirectory we watch? */
2163 d
= hashmap_get(j
->directories_by_wd
, INT_TO_PTR(e
->wd
));
2167 if (!(e
->mask
& IN_ISDIR
) && e
->len
> 0 &&
2168 (endswith(e
->name
, ".journal") ||
2169 endswith(e
->name
, ".journal~"))) {
2171 /* Event for a journal file */
2173 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2174 (void) add_file(j
, d
->path
, e
->name
);
2175 else if (e
->mask
& (IN_DELETE
|IN_MOVED_FROM
|IN_UNMOUNT
))
2176 remove_file(j
, d
->path
, e
->name
);
2178 } else if (!d
->is_root
&& e
->len
== 0) {
2180 /* Event for a subdirectory */
2182 if (e
->mask
& (IN_DELETE_SELF
|IN_MOVE_SELF
|IN_UNMOUNT
))
2183 remove_directory(j
, d
);
2185 } else if (d
->is_root
&& (e
->mask
& IN_ISDIR
) && e
->len
> 0 && sd_id128_from_string(e
->name
, &id
) >= 0) {
2187 /* Event for root directory */
2189 if (e
->mask
& (IN_CREATE
|IN_MOVED_TO
|IN_MODIFY
|IN_ATTRIB
))
2190 (void) add_directory(j
, d
->path
, e
->name
);
2196 if (e
->mask
& IN_IGNORED
)
2199 log_debug("Unknown inotify event.");
2202 static int determine_change(sd_journal
*j
) {
2207 b
= j
->current_invalidate_counter
!= j
->last_invalidate_counter
;
2208 j
->last_invalidate_counter
= j
->current_invalidate_counter
;
2210 return b
? SD_JOURNAL_INVALIDATE
: SD_JOURNAL_APPEND
;
2213 _public_
int sd_journal_process(sd_journal
*j
) {
2214 bool got_something
= false;
2216 assert_return(j
, -EINVAL
);
2217 assert_return(!journal_pid_changed(j
), -ECHILD
);
2219 j
->last_process_usec
= now(CLOCK_MONOTONIC
);
2222 union inotify_event_buffer buffer
;
2223 struct inotify_event
*e
;
2226 l
= read(j
->inotify_fd
, &buffer
, sizeof(buffer
));
2228 if (errno
== EAGAIN
|| errno
== EINTR
)
2229 return got_something
? determine_change(j
) : SD_JOURNAL_NOP
;
2234 got_something
= true;
2236 FOREACH_INOTIFY_EVENT(e
, buffer
, l
)
2237 process_inotify_event(j
, e
);
2241 _public_
int sd_journal_wait(sd_journal
*j
, uint64_t timeout_usec
) {
2245 assert_return(j
, -EINVAL
);
2246 assert_return(!journal_pid_changed(j
), -ECHILD
);
2248 if (j
->inotify_fd
< 0) {
2250 /* This is the first invocation, hence create the
2252 r
= sd_journal_get_fd(j
);
2256 /* The journal might have changed since the context
2257 * object was created and we weren't watching before,
2258 * hence don't wait for anything, and return
2260 return determine_change(j
);
2263 r
= sd_journal_get_timeout(j
, &t
);
2267 if (t
!= (uint64_t) -1) {
2270 n
= now(CLOCK_MONOTONIC
);
2271 t
= t
> n
? t
- n
: 0;
2273 if (timeout_usec
== (uint64_t) -1 || timeout_usec
> t
)
2278 r
= fd_wait_for_event(j
->inotify_fd
, POLLIN
, timeout_usec
);
2279 } while (r
== -EINTR
);
2284 return sd_journal_process(j
);
2287 _public_
int sd_journal_get_cutoff_realtime_usec(sd_journal
*j
, uint64_t *from
, uint64_t *to
) {
2291 uint64_t fmin
= 0, tmax
= 0;
2294 assert_return(j
, -EINVAL
);
2295 assert_return(!journal_pid_changed(j
), -ECHILD
);
2296 assert_return(from
|| to
, -EINVAL
);
2297 assert_return(from
!= to
, -EINVAL
);
2299 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2302 r
= journal_file_get_cutoff_realtime_usec(f
, &fr
, &t
);
2315 fmin
= MIN(fr
, fmin
);
2316 tmax
= MAX(t
, tmax
);
2325 return first
? 0 : 1;
2328 _public_
int sd_journal_get_cutoff_monotonic_usec(sd_journal
*j
, sd_id128_t boot_id
, uint64_t *from
, uint64_t *to
) {
2334 assert_return(j
, -EINVAL
);
2335 assert_return(!journal_pid_changed(j
), -ECHILD
);
2336 assert_return(from
|| to
, -EINVAL
);
2337 assert_return(from
!= to
, -EINVAL
);
2339 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2342 r
= journal_file_get_cutoff_monotonic_usec(f
, boot_id
, &fr
, &t
);
2352 *from
= MIN(fr
, *from
);
2367 void journal_print_header(sd_journal
*j
) {
2370 bool newline
= false;
2374 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2380 journal_file_print_header(f
);
2384 _public_
int sd_journal_get_usage(sd_journal
*j
, uint64_t *bytes
) {
2389 assert_return(j
, -EINVAL
);
2390 assert_return(!journal_pid_changed(j
), -ECHILD
);
2391 assert_return(bytes
, -EINVAL
);
2393 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
2396 if (fstat(f
->fd
, &st
) < 0)
2399 sum
+= (uint64_t) st
.st_blocks
* 512ULL;
2406 _public_
int sd_journal_query_unique(sd_journal
*j
, const char *field
) {
2409 assert_return(j
, -EINVAL
);
2410 assert_return(!journal_pid_changed(j
), -ECHILD
);
2411 assert_return(!isempty(field
), -EINVAL
);
2412 assert_return(field_is_valid(field
), -EINVAL
);
2418 free(j
->unique_field
);
2419 j
->unique_field
= f
;
2420 j
->unique_file
= NULL
;
2421 j
->unique_offset
= 0;
2422 j
->unique_file_lost
= false;
2427 _public_
int sd_journal_enumerate_unique(sd_journal
*j
, const void **data
, size_t *l
) {
2430 assert_return(j
, -EINVAL
);
2431 assert_return(!journal_pid_changed(j
), -ECHILD
);
2432 assert_return(data
, -EINVAL
);
2433 assert_return(l
, -EINVAL
);
2434 assert_return(j
->unique_field
, -EINVAL
);
2436 k
= strlen(j
->unique_field
);
2438 if (!j
->unique_file
) {
2439 if (j
->unique_file_lost
)
2442 j
->unique_file
= ordered_hashmap_first(j
->files
);
2443 if (!j
->unique_file
)
2446 j
->unique_offset
= 0;
2458 /* Proceed to next data object in the field's linked list */
2459 if (j
->unique_offset
== 0) {
2460 r
= journal_file_find_field_object(j
->unique_file
, j
->unique_field
, k
, &o
, NULL
);
2464 j
->unique_offset
= r
> 0 ? le64toh(o
->field
.head_data_offset
) : 0;
2466 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_DATA
, j
->unique_offset
, &o
);
2470 j
->unique_offset
= le64toh(o
->data
.next_field_offset
);
2473 /* We reached the end of the list? Then start again, with the next file */
2474 if (j
->unique_offset
== 0) {
2475 j
->unique_file
= ordered_hashmap_next(j
->files
, j
->unique_file
->path
);
2476 if (!j
->unique_file
)
2482 /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
2483 * instead, so that we can look at this data object at the same
2484 * time as one on another file */
2485 r
= journal_file_move_to_object(j
->unique_file
, OBJECT_UNUSED
, j
->unique_offset
, &o
);
2489 /* Let's do the type check by hand, since we used 0 context above. */
2490 if (o
->object
.type
!= OBJECT_DATA
) {
2491 log_debug("%s:offset " OFSfmt
": object has type %d, expected %d",
2492 j
->unique_file
->path
, j
->unique_offset
,
2493 o
->object
.type
, OBJECT_DATA
);
2497 r
= return_data(j
, j
->unique_file
, o
, &odata
, &ol
);
2501 /* Check if we have at least the field name and "=". */
2503 log_debug("%s:offset " OFSfmt
": object has size %zu, expected at least %zu",
2504 j
->unique_file
->path
, j
->unique_offset
,
2509 if (memcmp(odata
, j
->unique_field
, k
) || ((const char*) odata
)[k
] != '=') {
2510 log_debug("%s:offset " OFSfmt
": object does not start with \"%s=\"",
2511 j
->unique_file
->path
, j
->unique_offset
,
2516 /* OK, now let's see if we already returned this data
2517 * object by checking if it exists in the earlier
2518 * traversed files. */
2520 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2521 if (of
== j
->unique_file
)
2524 /* Skip this file it didn't have any fields indexed */
2525 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2528 r
= journal_file_find_data_object_with_hash(of
, odata
, ol
, le64toh(o
->data
.hash
), NULL
, NULL
);
2540 r
= return_data(j
, j
->unique_file
, o
, data
, l
);
2548 _public_
void sd_journal_restart_unique(sd_journal
*j
) {
2552 j
->unique_file
= NULL
;
2553 j
->unique_offset
= 0;
2554 j
->unique_file_lost
= false;
2557 _public_
int sd_journal_enumerate_fields(sd_journal
*j
, const char **field
) {
2560 assert_return(j
, -EINVAL
);
2561 assert_return(!journal_pid_changed(j
), -ECHILD
);
2562 assert_return(field
, -EINVAL
);
2564 if (!j
->fields_file
) {
2565 if (j
->fields_file_lost
)
2568 j
->fields_file
= ordered_hashmap_first(j
->files
);
2569 if (!j
->fields_file
)
2572 j
->fields_hash_table_index
= 0;
2573 j
->fields_offset
= 0;
2577 JournalFile
*f
, *of
;
2586 if (j
->fields_offset
== 0) {
2589 /* We are not yet positioned at any field. Let's pick the first one */
2590 r
= journal_file_map_field_hash_table(f
);
2594 m
= le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
);
2596 if (j
->fields_hash_table_index
>= m
) {
2597 /* Reached the end of the hash table, go to the next file. */
2602 j
->fields_offset
= le64toh(f
->field_hash_table
[j
->fields_hash_table_index
].head_hash_offset
);
2604 if (j
->fields_offset
!= 0)
2607 /* Empty hash table bucket, go to next one */
2608 j
->fields_hash_table_index
++;
2612 /* Proceed with next file */
2613 j
->fields_file
= ordered_hashmap_next(j
->files
, f
->path
);
2614 if (!j
->fields_file
) {
2619 j
->fields_offset
= 0;
2620 j
->fields_hash_table_index
= 0;
2625 /* We are already positioned at a field. If so, let's figure out the next field from it */
2627 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, j
->fields_offset
, &o
);
2631 j
->fields_offset
= le64toh(o
->field
.next_hash_offset
);
2632 if (j
->fields_offset
== 0) {
2633 /* Reached the end of the hash table chain */
2634 j
->fields_hash_table_index
++;
2639 /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
2640 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, j
->fields_offset
, &o
);
2644 /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
2645 if (o
->object
.type
!= OBJECT_FIELD
) {
2646 log_debug("%s:offset " OFSfmt
": object has type %i, expected %i", f
->path
, j
->fields_offset
, o
->object
.type
, OBJECT_FIELD
);
2650 sz
= le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
);
2652 /* Let's see if we already returned this field name before. */
2654 ORDERED_HASHMAP_FOREACH(of
, j
->files
, i
) {
2658 /* Skip this file it didn't have any fields indexed */
2659 if (JOURNAL_HEADER_CONTAINS(of
->header
, n_fields
) && le64toh(of
->header
->n_fields
) <= 0)
2662 r
= journal_file_find_field_object_with_hash(of
, o
->field
.payload
, sz
, le64toh(o
->field
.hash
), NULL
, NULL
);
2674 /* Check if this is really a valid string containing no NUL byte */
2675 if (memchr(o
->field
.payload
, 0, sz
))
2678 if (sz
> j
->data_threshold
)
2679 sz
= j
->data_threshold
;
2681 if (!GREEDY_REALLOC(j
->fields_buffer
, j
->fields_buffer_allocated
, sz
+ 1))
2684 memcpy(j
->fields_buffer
, o
->field
.payload
, sz
);
2685 j
->fields_buffer
[sz
] = 0;
2687 if (!field_is_valid(j
->fields_buffer
))
2690 *field
= j
->fields_buffer
;
2695 _public_
void sd_journal_restart_fields(sd_journal
*j
) {
2699 j
->fields_file
= NULL
;
2700 j
->fields_hash_table_index
= 0;
2701 j
->fields_offset
= 0;
2702 j
->fields_file_lost
= false;
2705 _public_
int sd_journal_reliable_fd(sd_journal
*j
) {
2706 assert_return(j
, -EINVAL
);
2707 assert_return(!journal_pid_changed(j
), -ECHILD
);
2709 return !j
->on_network
;
2712 static char *lookup_field(const char *field
, void *userdata
) {
2713 sd_journal
*j
= userdata
;
2721 r
= sd_journal_get_data(j
, field
, &data
, &size
);
2723 size
> REPLACE_VAR_MAX
)
2724 return strdup(field
);
2726 d
= strlen(field
) + 1;
2728 return strndup((const char*) data
+ d
, size
- d
);
2731 _public_
int sd_journal_get_catalog(sd_journal
*j
, char **ret
) {
2735 _cleanup_free_
char *text
= NULL
, *cid
= NULL
;
2739 assert_return(j
, -EINVAL
);
2740 assert_return(!journal_pid_changed(j
), -ECHILD
);
2741 assert_return(ret
, -EINVAL
);
2743 r
= sd_journal_get_data(j
, "MESSAGE_ID", &data
, &size
);
2747 cid
= strndup((const char*) data
+ 11, size
- 11);
2751 r
= sd_id128_from_string(cid
, &id
);
2755 r
= catalog_get(CATALOG_DATABASE
, id
, &text
);
2759 t
= replace_var(text
, lookup_field
, j
);
2767 _public_
int sd_journal_get_catalog_for_message_id(sd_id128_t id
, char **ret
) {
2768 assert_return(ret
, -EINVAL
);
2770 return catalog_get(CATALOG_DATABASE
, id
, ret
);
2773 _public_
int sd_journal_set_data_threshold(sd_journal
*j
, size_t sz
) {
2774 assert_return(j
, -EINVAL
);
2775 assert_return(!journal_pid_changed(j
), -ECHILD
);
2777 j
->data_threshold
= sz
;
2781 _public_
int sd_journal_get_data_threshold(sd_journal
*j
, size_t *sz
) {
2782 assert_return(j
, -EINVAL
);
2783 assert_return(!journal_pid_changed(j
), -ECHILD
);
2784 assert_return(sz
, -EINVAL
);
2786 *sz
= j
->data_threshold
;
2790 _public_
int sd_journal_has_runtime_files(sd_journal
*j
) {
2791 assert_return(j
, -EINVAL
);
2793 return j
->has_runtime_files
;
2796 _public_
int sd_journal_has_persistent_files(sd_journal
*j
) {
2797 assert_return(j
, -EINVAL
);
2799 return j
->has_persistent_files
;