1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "sd-journal.h"
27 #include "journal-def.h"
28 #include "journal-file.h"
33 typedef struct Match Match;
40 LIST_FIELDS(Match, matches);
43 typedef enum location_type {
49 typedef struct Location {
70 Location current_location;
71 JournalFile *current_file;
72 uint64_t current_field;
74 LIST_HEAD(Match, matches);
78 static void detach_location(sd_journal *j) {
84 j->current_file = NULL;
87 HASHMAP_FOREACH(f, j->files, i)
88 f->current_offset = 0;
91 static void reset_location(sd_journal *j) {
95 zero(j->current_location);
98 static void init_location(Location *l, JournalFile *f, Object *o) {
101 assert(o->object.type == OBJECT_ENTRY);
103 l->type = LOCATION_DISCRETE;
104 l->seqnum = le64toh(o->entry.seqnum);
105 l->seqnum_id = f->header->seqnum_id;
106 l->realtime = le64toh(o->entry.realtime);
107 l->monotonic = le64toh(o->entry.monotonic);
108 l->boot_id = le64toh(o->entry.boot_id);
109 l->xor_hash = le64toh(o->entry.xor_hash);
111 l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
114 static void set_location(sd_journal *j, JournalFile *f, Object *o, uint64_t offset) {
119 init_location(&j->current_location, f, o);
122 j->current_field = 0;
124 f->current_offset = offset;
127 static int same_field(const void *_a, size_t s, const void *_b, size_t t) {
128 const uint8_t *a = _a, *b = _b;
130 bool a_good = false, b_good = false, different = false;
132 for (j = 0; j < s && j < t; j++) {
141 if (a_good && b_good)
142 return different ? 0 : 1;
148 int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
149 Match *m, *after = NULL;
159 le_hash = htole64(hash64(data, size));
161 LIST_FOREACH(matches, m, j->matches) {
164 if (m->le_hash == le_hash &&
166 memcmp(m->data, data, size) == 0)
169 r = same_field(data, size, m->data, m->size);
182 m->data = malloc(m->size);
188 memcpy(m->data, data, size);
189 m->le_hash = le_hash;
191 /* Matches for the same fields we order adjacent to each
193 LIST_INSERT_AFTER(Match, matches, j->matches, after, m);
201 void sd_journal_flush_matches(sd_journal *j) {
205 Match *m = j->matches;
207 LIST_REMOVE(Match, matches, j->matches, m);
217 static int compare_order(JournalFile *af, Object *ao,
218 JournalFile *bf, Object *bo) {
227 /* We operate on two different files here, hence we can access
228 * two objects at the same time, which we normally can't.
230 * If contents and timestamps match, these entries are
231 * identical, even if the seqnum does not match */
233 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) &&
234 ao->entry.monotonic == bo->entry.monotonic &&
235 ao->entry.realtime == bo->entry.realtime &&
236 ao->entry.xor_hash == bo->entry.xor_hash)
239 if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
241 /* If this is from the same seqnum source, compare
243 a = le64toh(ao->entry.seqnum);
244 b = le64toh(bo->entry.seqnum);
251 /* Wow! This is weird, different data but the same
252 * seqnums? Something is borked, but let's make the
253 * best of it and compare by time. */
256 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
258 /* If the boot id matches compare monotonic time */
259 a = le64toh(ao->entry.monotonic);
260 b = le64toh(bo->entry.monotonic);
268 /* Otherwise compare UTC time */
269 a = le64toh(ao->entry.realtime);
270 b = le64toh(ao->entry.realtime);
277 /* Finally, compare by contents */
278 a = le64toh(ao->entry.xor_hash);
279 b = le64toh(ao->entry.xor_hash);
289 static int compare_with_location(JournalFile *af, Object *ao, Location *l) {
295 assert(l->type == LOCATION_DISCRETE);
297 if (l->monotonic_set &&
298 sd_id128_equal(ao->entry.boot_id, l->boot_id) &&
300 le64toh(ao->entry.realtime) == l->realtime &&
302 le64toh(ao->entry.xor_hash) == l->xor_hash)
306 sd_id128_equal(af->header->seqnum_id, l->seqnum_id)) {
308 a = le64toh(ao->entry.seqnum);
316 if (l->monotonic_set &&
317 sd_id128_equal(ao->entry.boot_id, l->boot_id)) {
319 a = le64toh(ao->entry.monotonic);
321 if (a < l->monotonic)
323 if (a > l->monotonic)
327 if (l->realtime_set) {
329 a = le64toh(ao->entry.realtime);
337 if (l->xor_hash_set) {
338 a = le64toh(ao->entry.xor_hash);
349 static int find_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
357 /* No matches is simple */
359 if (j->current_location.type == LOCATION_HEAD)
360 r = journal_file_next_entry(f, NULL, 0, DIRECTION_DOWN, &o, &p);
361 else if (j->current_location.type == LOCATION_TAIL)
362 r = journal_file_next_entry(f, NULL, 0, DIRECTION_UP, &o, &p);
363 else if (j->current_location.seqnum_set &&
364 sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
365 r = journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, &o, &p);
366 else if (j->current_location.monotonic_set)
367 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
368 else if (j->current_location.realtime_set)
369 r = journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, &o, &p);
371 r = journal_file_next_entry(f, NULL, 0, direction, &o, &p);
377 Match *m, *term_match = NULL;
381 /* We have matches, first, let's jump to the monotonic
382 * position if we have any, since it implies a
385 if (j->current_location.type == LOCATION_DISCRETE &&
386 j->current_location.monotonic_set) {
388 r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, &o, &p);
393 LIST_FOREACH(matches, m, j->matches) {
397 r = journal_file_find_data_object_with_hash(f, m->data, m->size, m->le_hash, &d, &dp);
401 if (j->current_location.type == LOCATION_HEAD)
402 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, &c, &cp);
403 else if (j->current_location.type == LOCATION_TAIL)
404 r = journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, &c, &cp);
405 else if (j->current_location.seqnum_set &&
406 sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
407 r = journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, &c, &cp);
408 else if (j->current_location.realtime_set)
409 r = journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, &c, &cp);
411 r = journal_file_next_entry_for_data(f, NULL, 0, dp, direction, &c, &cp);
420 } else if (same_field(term_match->data, term_match->size, m->data, m->size)) {
422 /* Same field as previous match... */
425 /* Find the earliest of the OR matches */
428 (direction == DIRECTION_DOWN && cp < tp) ||
429 (direction == DIRECTION_UP && cp > tp)) {
438 /* Previous term is finished, did anything match? */
442 /* Find the last of the AND matches */
444 (direction == DIRECTION_DOWN && tp > p) ||
445 (direction == DIRECTION_UP && tp < p)) {
462 /* Last term is finished, did anything match? */
467 (direction == DIRECTION_DOWN && tp > p) ||
468 (direction == DIRECTION_UP && tp < p)) {
486 static int next_with_matches(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
500 /* No matches is easy */
502 r = journal_file_next_entry(f, c, cp, direction, &c, &cp);
513 /* So there are matches we have to adhere to, let's find the
514 * first entry that matches all of them */
518 bool found, term_result = false;
519 Match *m, *term_match = NULL;
521 n = journal_file_entry_n_items(c);
523 /* Make sure we don't match the entry we are starting
525 found = cp > *offset;
528 LIST_FOREACH(matches, m, j->matches) {
531 /* Let's check if this is the beginning of a
532 * new term, i.e. has a different field prefix
533 * as the preceeding match. */
537 } else if (!same_field(term_match->data, term_match->size, m->data, m->size)) {
545 for (k = 0; k < n; k++)
546 if (c->entry.items[k].hash == m->le_hash)
550 /* Hmm, didn't find any field that
551 * matched this rule, so ignore this
552 * match. Go on with next match */
558 /* Hmm, so, this field matched, let's remember
559 * where we'd have to try next, in case the other
560 * matches are not OK */
562 r = journal_file_next_entry_for_data(f, c, cp, le64toh(c->entry.items[k].object_offset), direction, NULL, &q);
565 if (direction == DIRECTION_DOWN) {
569 if (np == 0 || q < np)
575 /* Check the last term */
576 if (term_match && term_result)
579 /* Did this entry match against all matches? */
588 /* Did we find a subsequent entry? */
592 /* Hmm, ok, this entry only matched partially, so
593 * let's try another one */
598 static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
601 int compare_value, r;
606 if (f->current_offset > 0) {
607 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &c);
611 cp = f->current_offset;
613 r = next_with_matches(j, f, direction, &c, &cp);
619 r = find_location(j, f, direction, &c, &cp);
629 if (j->current_location.type == LOCATION_DISCRETE) {
632 k = compare_with_location(f, c, &j->current_location);
633 if (direction == DIRECTION_DOWN)
634 found = k >= compare_value;
636 found = k <= -compare_value;
648 r = next_with_matches(j, f, direction, &c, &cp);
654 static int real_journal_next(sd_journal *j, direction_t direction) {
655 JournalFile *f, *new_current = NULL;
658 uint64_t new_offset = 0;
659 Object *new_entry = NULL;
663 HASHMAP_FOREACH(f, j->files, i) {
668 r = next_beyond_location(j, f, direction, &o, &p);
679 k = compare_order(f, o, new_current, new_entry);
681 if (direction == DIRECTION_DOWN)
697 set_location(j, new_current, new_entry, new_offset);
702 int sd_journal_next(sd_journal *j) {
703 return real_journal_next(j, DIRECTION_DOWN);
706 int sd_journal_previous(sd_journal *j) {
707 return real_journal_next(j, DIRECTION_UP);
710 int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
716 r = sd_journal_next(j);
730 int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
736 r = sd_journal_previous(j);
750 int sd_journal_get_cursor(sd_journal *j, char **cursor) {
753 char bid[33], sid[33];
758 if (!j->current_file || j->current_file->current_offset <= 0)
759 return -EADDRNOTAVAIL;
761 r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
765 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
766 sd_id128_to_string(o->entry.boot_id, bid);
769 "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
770 sid, (unsigned long long) le64toh(o->entry.seqnum),
771 bid, (unsigned long long) le64toh(o->entry.monotonic),
772 (unsigned long long) le64toh(o->entry.realtime),
773 (unsigned long long) le64toh(o->entry.xor_hash),
774 file_name_from_path(j->current_file->path)) < 0)
780 int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
784 unsigned long long seqnum, monotonic, realtime, xor_hash;
786 seqnum_id_set = false,
789 monotonic_set = false,
790 realtime_set = false,
791 xor_hash_set = false;
792 sd_id128_t seqnum_id, boot_id;
797 FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) {
801 if (l < 2 || w[1] != '=')
804 item = strndup(w, l);
811 seqnum_id_set = true;
812 k = sd_id128_from_string(w+2, &seqnum_id);
817 if (sscanf(w+2, "%llx", &seqnum) != 1)
823 k = sd_id128_from_string(w+2, &boot_id);
827 monotonic_set = true;
828 if (sscanf(w+2, "%llx", &monotonic) != 1)
834 if (sscanf(w+2, "%llx", &realtime) != 1)
840 if (sscanf(w+2, "%llx", &xor_hash) != 1)
851 if ((!seqnum_set || !seqnum_id_set) &&
852 (!monotonic_set || !boot_id_set) &&
858 j->current_location.type = LOCATION_DISCRETE;
861 j->current_location.realtime = (uint64_t) realtime;
862 j->current_location.realtime_set = true;
865 if (seqnum_set && seqnum_id_set) {
866 j->current_location.seqnum = (uint64_t) seqnum;
867 j->current_location.seqnum_id = seqnum_id;
868 j->current_location.seqnum_set = true;
871 if (monotonic_set && boot_id_set) {
872 j->current_location.monotonic = (uint64_t) monotonic;
873 j->current_location.boot_id = boot_id;
874 j->current_location.monotonic_set = true;
878 j->current_location.xor_hash = (uint64_t) xor_hash;
879 j->current_location.xor_hash_set = true;
885 int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
889 j->current_location.type = LOCATION_DISCRETE;
890 j->current_location.boot_id = boot_id;
891 j->current_location.monotonic = usec;
892 j->current_location.monotonic_set = true;
897 int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
901 j->current_location.type = LOCATION_DISCRETE;
902 j->current_location.realtime = usec;
903 j->current_location.realtime_set = true;
908 int sd_journal_seek_head(sd_journal *j) {
912 j->current_location.type = LOCATION_HEAD;
917 int sd_journal_seek_tail(sd_journal *j) {
921 j->current_location.type = LOCATION_TAIL;
926 static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
936 fn = join(prefix, "/", dir, "/", filename, NULL);
938 fn = join(prefix, "/", filename, NULL);
943 r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
953 journal_file_dump(f);
956 r = hashmap_put(j->files, f->path, f);
958 journal_file_close(f);
965 static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
974 fn = join(prefix, "/", dir, NULL);
989 struct dirent buf, *de;
991 r = readdir_r(d, &buf, &de);
995 if (!dirent_is_file_with_suffix(de, ".journal"))
998 r = add_file(j, prefix, dir, de->d_name);
1000 log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
1008 int sd_journal_open(sd_journal **ret) {
1011 const char search_paths[] =
1012 "/run/log/journal\0"
1013 "/var/log/journal\0";
1018 j = new0(sd_journal, 1);
1022 j->files = hashmap_new(string_hash_func, string_compare_func);
1028 /* We ignore most errors here, since the idea is to only open
1029 * what's actually accessible, and ignore the rest. */
1031 NULSTR_FOREACH(p, search_paths) {
1036 if (errno != ENOENT)
1037 log_debug("Failed to open %s: %m", p);
1042 struct dirent buf, *de;
1045 r = readdir_r(d, &buf, &de);
1049 if (dirent_is_file_with_suffix(de, ".journal")) {
1050 r = add_file(j, p, NULL, de->d_name);
1052 log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r));
1054 } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
1055 sd_id128_from_string(de->d_name, &id) >= 0) {
1057 r = add_directory(j, p, de->d_name);
1059 log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r));
1070 sd_journal_close(j);
1075 void sd_journal_close(sd_journal *j) {
1081 while ((f = hashmap_steal_first(j->files)))
1082 journal_file_close(f);
1084 hashmap_free(j->files);
1087 sd_journal_flush_matches(j);
1092 int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
1100 f = j->current_file;
1102 return -EADDRNOTAVAIL;
1104 if (f->current_offset <= 0)
1105 return -EADDRNOTAVAIL;
1107 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1111 *ret = le64toh(o->entry.realtime);
1115 int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
1124 f = j->current_file;
1126 return -EADDRNOTAVAIL;
1128 if (f->current_offset <= 0)
1129 return -EADDRNOTAVAIL;
1131 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1136 *ret_boot_id = o->entry.boot_id;
1138 r = sd_id128_get_boot(&id);
1142 if (!sd_id128_equal(id, o->entry.boot_id))
1146 *ret = le64toh(o->entry.monotonic);
1150 int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
1153 size_t field_length;
1162 if (isempty(field) || strchr(field, '='))
1165 f = j->current_file;
1167 return -EADDRNOTAVAIL;
1169 if (f->current_offset <= 0)
1170 return -EADDRNOTAVAIL;
1172 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1176 field_length = strlen(field);
1178 n = journal_file_entry_n_items(o);
1179 for (i = 0; i < n; i++) {
1180 uint64_t p, l, le_hash;
1183 p = le64toh(o->entry.items[i].object_offset);
1184 le_hash = o->entry.items[j->current_field].hash;
1185 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1189 if (le_hash != o->data.hash)
1192 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1194 if (l >= field_length+1 &&
1195 memcmp(o->data.payload, field, field_length) == 0 &&
1196 o->data.payload[field_length] == '=') {
1200 if ((uint64_t) t != l)
1203 *data = o->data.payload;
1209 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1217 int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
1219 uint64_t p, l, n, le_hash;
1228 f = j->current_file;
1230 return -EADDRNOTAVAIL;
1232 if (f->current_offset <= 0)
1233 return -EADDRNOTAVAIL;
1235 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1239 n = journal_file_entry_n_items(o);
1240 if (j->current_field >= n)
1243 p = le64toh(o->entry.items[j->current_field].object_offset);
1244 le_hash = o->entry.items[j->current_field].hash;
1245 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
1249 if (le_hash != o->data.hash)
1252 l = le64toh(o->object.size) - offsetof(Object, data.payload);
1255 /* We can't read objects larger than 4G on a 32bit machine */
1256 if ((uint64_t) t != l)
1259 *data = o->data.payload;
1262 j->current_field ++;
1267 void sd_journal_restart_data(sd_journal *j) {
1270 j->current_field = 0;