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"
32 typedef struct Match Match;
39 LIST_FIELDS(Match, matches);
45 JournalFile *current_file;
46 uint64_t current_field;
48 LIST_HEAD(Match, matches);
51 int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
67 m->data = malloc(m->size);
73 memcpy(m->data, data, size);
75 LIST_PREPEND(Match, matches, j->matches, m);
79 void sd_journal_flush_matches(sd_journal *j) {
83 Match *m = j->matches;
85 LIST_REMOVE(Match, matches, j->matches, m);
91 static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
92 JournalFile *bf, Object *bo, uint64_t bp) {
96 /* We operate on two different files here, hence we can access
97 * two objects at the same time, which we normally can't.
99 * If contents and timestamps match, these entries are
100 * identical, even if the seqnum does not match */
102 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) &&
103 ao->entry.monotonic == bo->entry.monotonic &&
104 ao->entry.realtime == bo->entry.realtime &&
105 ao->entry.xor_hash == bo->entry.xor_hash)
108 if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
110 /* If this is from the same seqnum source, compare
112 a = le64toh(ao->entry.seqnum);
113 b = le64toh(bo->entry.seqnum);
120 /* Wow! This is weird, different data but the same
121 * seqnums? Something is borked, but let's make the
122 * best of it and compare by time. */
125 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
127 /* If the boot id matches compare monotonic time */
128 a = le64toh(ao->entry.monotonic);
129 b = le64toh(bo->entry.monotonic);
137 /* Otherwise compare UTC time */
138 a = le64toh(ao->entry.realtime);
139 b = le64toh(ao->entry.realtime);
146 /* Finally, compare by contents */
147 a = le64toh(ao->entry.xor_hash);
148 b = le64toh(ao->entry.xor_hash);
158 int sd_journal_next(sd_journal *j) {
159 JournalFile *f, *new_current = NULL;
162 uint64_t new_offset = 0;
163 Object *new_entry = NULL;
167 HASHMAP_FOREACH(f, j->files, i) {
171 if (f->current_offset > 0) {
172 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
178 r = journal_file_next_entry(f, o, &o, &p);
185 compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
193 j->current_file = new_current;
194 j->current_file->current_offset = new_offset;
195 j->current_field = 0;
197 /* Skip over any identical entries in the other files too */
199 HASHMAP_FOREACH(f, j->files, i) {
203 if (j->current_file == f)
206 if (f->current_offset > 0) {
207 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
213 r = journal_file_next_entry(f, o, &o, &p);
219 if (compare_order(new_current, new_entry, new_offset, f, o, p) == 0)
220 f->current_offset = p;
229 int sd_journal_previous(sd_journal *j) {
230 JournalFile *f, *new_current = NULL;
233 uint64_t new_offset = 0;
234 Object *new_entry = NULL;
238 HASHMAP_FOREACH(f, j->files, i) {
242 if (f->current_offset > 0) {
243 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
249 r = journal_file_prev_entry(f, o, &o, &p);
255 if (!new_current || compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
263 j->current_file = new_current;
264 j->current_file->current_offset = new_offset;
265 j->current_field = 0;
267 /* Skip over any identical entries in the other files too */
269 HASHMAP_FOREACH(f, j->files, i) {
273 if (j->current_file == f)
276 if (f->current_offset > 0) {
277 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
283 r = journal_file_prev_entry(f, o, &o, &p);
289 if (compare_order(new_current, new_entry, new_offset, f, o, p) == 0)
290 f->current_offset = p;
299 int sd_journal_get_cursor(sd_journal *j, char **cursor) {
302 char bid[33], sid[33];
307 if (!j->current_file || j->current_file->current_offset <= 0)
308 return -EADDRNOTAVAIL;
310 r = journal_file_move_to_object(j->current_file, j->current_file->current_offset, OBJECT_ENTRY, &o);
314 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
315 sd_id128_to_string(o->entry.boot_id, bid);
318 "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
319 sid, (unsigned long long) le64toh(o->entry.seqnum),
320 bid, (unsigned long long) le64toh(o->entry.monotonic),
321 (unsigned long long) le64toh(o->entry.realtime),
322 (unsigned long long) le64toh(o->entry.xor_hash),
323 file_name_from_path(j->current_file->path)) < 0)
329 int sd_journal_set_cursor(sd_journal *j, const char *cursor) {
333 static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
343 fn = join(prefix, "/", dir, "/", filename, NULL);
345 fn = join(prefix, "/", filename, NULL);
350 r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
360 r = hashmap_put(j->files, f->path, f);
362 journal_file_close(f);
369 static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
378 fn = join(prefix, "/", dir, NULL);
393 struct dirent buf, *de;
395 r = readdir_r(d, &buf, &de);
399 if (!dirent_is_file_with_suffix(de, ".journal"))
402 r = add_file(j, prefix, dir, de->d_name);
404 log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
412 int sd_journal_open(sd_journal **ret) {
415 const char search_paths[] =
417 "/var/log/journal\0";
422 j = new0(sd_journal, 1);
426 j->files = hashmap_new(string_hash_func, string_compare_func);
432 /* We ignore most errors here, since the idea is to only open
433 * what's actually accessible, and ignore the rest. */
435 NULSTR_FOREACH(p, search_paths) {
441 log_debug("Failed to open %s: %m", p);
446 struct dirent buf, *de;
449 r = readdir_r(d, &buf, &de);
453 if (dirent_is_file_with_suffix(de, ".journal")) {
454 r = add_file(j, p, NULL, de->d_name);
456 log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r));
458 } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
459 sd_id128_from_string(de->d_name, &id) >= 0) {
461 r = add_directory(j, p, de->d_name);
463 log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r));
479 void sd_journal_close(sd_journal *j) {
485 while ((f = hashmap_steal_first(j->files)))
486 journal_file_close(f);
488 hashmap_free(j->files);
491 sd_journal_flush_matches(j);
496 int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
508 if (f->current_offset <= 0)
511 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
515 *ret = le64toh(o->entry.realtime);
519 int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret) {
532 if (f->current_offset <= 0)
535 r = sd_id128_get_machine(&id);
539 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
543 if (!sd_id128_equal(id, o->entry.boot_id))
546 *ret = le64toh(o->entry.monotonic);
551 int sd_journal_get_field(sd_journal *j, const char *field, const void **data, size_t *size) {
563 if (isempty(field) || strchr(field, '='))
570 if (f->current_offset <= 0)
573 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
577 field_length = strlen(field);
579 n = journal_file_entry_n_items(o);
580 for (i = 0; i < n; i++) {
584 p = le64toh(o->entry.items[i].object_offset);
585 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
589 l = le64toh(o->object.size) - offsetof(Object, data.payload);
591 if (l >= field_length+1 &&
592 memcmp(o->data.payload, field, field_length) == 0 &&
593 o->data.payload[field_length] == '=') {
597 if ((uint64_t) t != l)
600 *data = o->data.payload;
606 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
614 int sd_journal_iterate_fields(sd_journal *j, const void **data, size_t *size) {
629 if (f->current_offset <= 0)
632 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
636 n = journal_file_entry_n_items(o);
637 if (j->current_field >= n)
640 p = le64toh(o->entry.items[j->current_field].object_offset);
641 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
645 l = le64toh(o->object.size) - offsetof(Object, data.payload);
648 /* We can't read objects larger than 4G on a 32bit machine */
649 if ((uint64_t) t != l)
652 *data = o->data.payload;
660 int sd_journal_seek_head(sd_journal *j) {
665 int sd_journal_seek_tail(sd_journal *j) {