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);
46 JournalFile *current_file;
47 uint64_t current_field;
49 LIST_HEAD(Match, matches);
53 static void reset_location(sd_journal *j) {
59 j->current_file = NULL;
62 HASHMAP_FOREACH(f, j->files, i)
63 f->current_offset = 0;
66 int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
82 m->data = malloc(m->size);
88 memcpy(m->data, data, size);
89 m->le_hash = hash64(m->data, size);
91 LIST_PREPEND(Match, matches, j->matches, m);
99 void sd_journal_flush_matches(sd_journal *j) {
103 Match *m = j->matches;
105 LIST_REMOVE(Match, matches, j->matches, m);
115 static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
116 JournalFile *bf, Object *bo, uint64_t bp) {
120 /* We operate on two different files here, hence we can access
121 * two objects at the same time, which we normally can't.
123 * If contents and timestamps match, these entries are
124 * identical, even if the seqnum does not match */
126 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) &&
127 ao->entry.monotonic == bo->entry.monotonic &&
128 ao->entry.realtime == bo->entry.realtime &&
129 ao->entry.xor_hash == bo->entry.xor_hash)
132 if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
134 /* If this is from the same seqnum source, compare
136 a = le64toh(ao->entry.seqnum);
137 b = le64toh(bo->entry.seqnum);
144 /* Wow! This is weird, different data but the same
145 * seqnums? Something is borked, but let's make the
146 * best of it and compare by time. */
149 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
151 /* If the boot id matches compare monotonic time */
152 a = le64toh(ao->entry.monotonic);
153 b = le64toh(bo->entry.monotonic);
161 /* Otherwise compare UTC time */
162 a = le64toh(ao->entry.realtime);
163 b = le64toh(ao->entry.realtime);
170 /* Finally, compare by contents */
171 a = le64toh(ao->entry.xor_hash);
172 b = le64toh(ao->entry.xor_hash);
182 static int move_to_next_with_matches(sd_journal *j, JournalFile *f, direction_t direction, Object **o, uint64_t *p) {
193 /* No matches is easy, just go on to the next entry */
195 if (f->current_offset > 0) {
196 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &c);
202 return journal_file_next_entry(f, c, direction, o, p);
205 /* So there are matches we have to adhere to, let's find the
206 * first entry that matches all of them */
208 if (f->current_offset > 0)
209 cp = f->current_offset;
211 r = journal_file_find_first_entry(f, j->matches->data, j->matches->size, direction, &c, &cp);
215 /* We can shortcut this if there's only one match */
216 if (j->n_matches == 1) {
228 r = journal_file_move_to_object(f, cp, OBJECT_ENTRY, &c);
232 n = journal_file_entry_n_items(c);
234 /* Make sure we don't match the entry we are starting
236 found = f->current_offset != cp;
239 LIST_FOREACH(matches, m, j->matches) {
242 for (k = 0; k < n; k++)
243 if (c->entry.items[k].hash == m->le_hash)
247 /* Hmm, didn't find any field that matched, so ignore
248 * this match. Go on with next match */
254 /* Hmm, so, this field matched, let's remember
255 * where we'd have to try next, in case the other
256 * matches are not OK */
258 if (direction == DIRECTION_DOWN) {
259 q = le64toh(c->entry.items[k].next_entry_offset);
264 q = le64toh(c->entry.items[k].prev_entry_offset);
266 if (q != 0 && (np == 0 || q < np))
271 /* Did this entry match against all matches? */
278 /* Did we find a subsequent entry? */
282 /* Hmm, ok, this entry only matched partially, so
283 * let's try another one */
288 static int real_journal_next(sd_journal *j, direction_t direction) {
289 JournalFile *f, *new_current = NULL;
292 uint64_t new_offset = 0;
293 Object *new_entry = NULL;
297 HASHMAP_FOREACH(f, j->files, i) {
301 r = move_to_next_with_matches(j, f, direction, &o, &p);
308 compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
316 j->current_file = new_current;
317 j->current_file->current_offset = new_offset;
318 j->current_field = 0;
320 /* Skip over any identical entries in the other files too */
322 HASHMAP_FOREACH(f, j->files, i) {
326 if (j->current_file == f)
329 r = move_to_next_with_matches(j, f, direction, &o, &p);
335 if (compare_order(new_current, new_entry, new_offset, f, o, p) == 0)
336 f->current_offset = p;
345 int sd_journal_next(sd_journal *j) {
346 return real_journal_next(j, DIRECTION_DOWN);
349 int sd_journal_previous(sd_journal *j) {
350 return real_journal_next(j, DIRECTION_UP);
353 int sd_journal_get_cursor(sd_journal *j, char **cursor) {
356 char bid[33], sid[33];
361 if (!j->current_file || j->current_file->current_offset <= 0)
362 return -EADDRNOTAVAIL;
364 r = journal_file_move_to_object(j->current_file, j->current_file->current_offset, OBJECT_ENTRY, &o);
368 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
369 sd_id128_to_string(o->entry.boot_id, bid);
372 "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
373 sid, (unsigned long long) le64toh(o->entry.seqnum),
374 bid, (unsigned long long) le64toh(o->entry.monotonic),
375 (unsigned long long) le64toh(o->entry.realtime),
376 (unsigned long long) le64toh(o->entry.xor_hash),
377 file_name_from_path(j->current_file->path)) < 0)
383 int sd_journal_set_cursor(sd_journal *j, const char *cursor) {
387 static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
397 fn = join(prefix, "/", dir, "/", filename, NULL);
399 fn = join(prefix, "/", filename, NULL);
404 r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
414 r = hashmap_put(j->files, f->path, f);
416 journal_file_close(f);
423 static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
432 fn = join(prefix, "/", dir, NULL);
447 struct dirent buf, *de;
449 r = readdir_r(d, &buf, &de);
453 if (!dirent_is_file_with_suffix(de, ".journal"))
456 r = add_file(j, prefix, dir, de->d_name);
458 log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
466 int sd_journal_open(sd_journal **ret) {
469 const char search_paths[] =
471 "/var/log/journal\0";
476 j = new0(sd_journal, 1);
480 j->files = hashmap_new(string_hash_func, string_compare_func);
486 /* We ignore most errors here, since the idea is to only open
487 * what's actually accessible, and ignore the rest. */
489 NULSTR_FOREACH(p, search_paths) {
495 log_debug("Failed to open %s: %m", p);
500 struct dirent buf, *de;
503 r = readdir_r(d, &buf, &de);
507 if (dirent_is_file_with_suffix(de, ".journal")) {
508 r = add_file(j, p, NULL, de->d_name);
510 log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r));
512 } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
513 sd_id128_from_string(de->d_name, &id) >= 0) {
515 r = add_directory(j, p, de->d_name);
517 log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r));
533 void sd_journal_close(sd_journal *j) {
539 while ((f = hashmap_steal_first(j->files)))
540 journal_file_close(f);
542 hashmap_free(j->files);
545 sd_journal_flush_matches(j);
550 int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
562 if (f->current_offset <= 0)
565 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
569 *ret = le64toh(o->entry.realtime);
573 int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret) {
586 if (f->current_offset <= 0)
589 r = sd_id128_get_boot(&id);
593 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
597 if (!sd_id128_equal(id, o->entry.boot_id))
600 *ret = le64toh(o->entry.monotonic);
605 int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
617 if (isempty(field) || strchr(field, '='))
624 if (f->current_offset <= 0)
627 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
631 field_length = strlen(field);
633 n = journal_file_entry_n_items(o);
634 for (i = 0; i < n; i++) {
638 p = le64toh(o->entry.items[i].object_offset);
639 h = o->entry.items[j->current_field].hash;
640 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
644 if (h != o->data.hash)
647 l = le64toh(o->object.size) - offsetof(Object, data.payload);
649 if (l >= field_length+1 &&
650 memcmp(o->data.payload, field, field_length) == 0 &&
651 o->data.payload[field_length] == '=') {
655 if ((uint64_t) t != l)
658 *data = o->data.payload;
664 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
672 int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
687 if (f->current_offset <= 0)
690 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
694 n = journal_file_entry_n_items(o);
695 if (j->current_field >= n)
698 p = le64toh(o->entry.items[j->current_field].object_offset);
699 h = o->entry.items[j->current_field].hash;
700 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
704 if (h != o->data.hash)
707 l = le64toh(o->object.size) - offsetof(Object, data.payload);
710 /* We can't read objects larger than 4G on a 32bit machine */
711 if ((uint64_t) t != l)
714 *data = o->data.payload;
722 void sd_journal_start_data(sd_journal *j) {
725 j->current_field = 0;
728 int sd_journal_seek_head(sd_journal *j) {
733 return real_journal_next(j, DIRECTION_DOWN);
736 int sd_journal_seek_tail(sd_journal *j) {
741 return real_journal_next(j, DIRECTION_UP);