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;
47 LIST_HEAD(Match, matches);
50 int sd_journal_add_match(sd_journal *j, const char *field, const void *data, size_t size) {
56 assert(data || size == 0);
62 m->size = strlen(field) + 1 + size;
63 m->data = malloc(m->size);
69 e = stpcpy(m->data, field);
71 memcpy(e, data, size);
73 LIST_PREPEND(Match, matches, j->matches, m);
77 void sd_journal_flush_matches(sd_journal *j) {
81 Match *m = j->matches;
83 LIST_REMOVE(Match, matches, j->matches, m);
89 static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
90 JournalFile *bf, Object *bo, uint64_t bp) {
94 /* We operate on two different files here, hence we can access
95 * two objects at the same time, which we normally can't */
97 if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
99 /* If this is from the same seqnum source, compare
101 a = le64toh(ao->entry.seqnum);
102 b = le64toh(bo->entry.seqnum);
110 if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
112 /* If the boot id matches compare monotonic time */
113 a = le64toh(ao->entry.monotonic);
114 b = le64toh(bo->entry.monotonic);
122 /* Otherwise compare UTC time */
123 a = le64toh(ao->entry.realtime);
124 b = le64toh(ao->entry.realtime);
131 /* Finally, compare by contents */
132 a = le64toh(ao->entry.xor_hash);
133 b = le64toh(ao->entry.xor_hash);
143 int sd_journal_next(sd_journal *j) {
144 JournalFile *f, *new_current = NULL;
147 uint64_t new_offset = 0;
148 Object *new_entry = NULL;
152 HASHMAP_FOREACH(f, j->files, i) {
156 if (f->current_offset > 0) {
157 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
163 r = journal_file_next_entry(f, o, &o, &p);
170 compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
178 j->current_file = new_current;
179 j->current_file->current_offset = new_offset;
180 j->current_file->current_field = 0;
182 /* Skip over any identical entries in the other files too */
184 HASHMAP_FOREACH(f, j->files, i) {
188 if (j->current_file == f)
191 if (f->current_offset > 0) {
192 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
198 r = journal_file_next_entry(f, o, &o, &p);
204 if (compare_order(new_current, new_entry, new_offset, f, o, p) == 0) {
205 f->current_offset = p;
206 f->current_field = 0;
216 int sd_journal_previous(sd_journal *j) {
217 JournalFile *f, *new_current = NULL;
220 uint64_t new_offset = 0;
221 Object *new_entry = NULL;
225 HASHMAP_FOREACH(f, j->files, i) {
229 if (f->current_offset > 0) {
230 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
236 r = journal_file_prev_entry(f, o, &o, &p);
242 if (!new_current || compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
250 j->current_file = new_current;
251 j->current_file->current_offset = new_offset;
252 j->current_file->current_field = 0;
259 int sd_journal_get_cursor(sd_journal *j, char **cursor) {
262 char bid[33], sid[33];
267 if (!j->current_file || j->current_file->current_offset <= 0)
268 return -EADDRNOTAVAIL;
270 r = journal_file_move_to_object(j->current_file, j->current_file->current_offset, OBJECT_ENTRY, &o);
274 sd_id128_to_string(j->current_file->header->seqnum_id, sid);
275 sd_id128_to_string(o->entry.boot_id, bid);
278 "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
279 sid, (unsigned long long) le64toh(o->entry.seqnum),
280 bid, (unsigned long long) le64toh(o->entry.monotonic),
281 (unsigned long long) le64toh(o->entry.realtime),
282 (unsigned long long) le64toh(o->entry.xor_hash),
283 file_name_from_path(j->current_file->path)) < 0)
289 int sd_journal_set_cursor(sd_journal *j, const char *cursor) {
293 static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
303 fn = join(prefix, "/", dir, "/", filename, NULL);
305 fn = join(prefix, "/", filename, NULL);
310 r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
320 r = hashmap_put(j->files, f->path, f);
322 journal_file_close(f);
329 static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
338 fn = join(prefix, "/", dir, NULL);
353 struct dirent buf, *de;
355 r = readdir_r(d, &buf, &de);
359 if (!dirent_is_file_with_suffix(de, ".journal"))
362 r = add_file(j, prefix, dir, de->d_name);
364 log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
372 int sd_journal_open(sd_journal **ret) {
375 const char search_paths[] =
377 "/var/log/journal\0";
382 j = new0(sd_journal, 1);
386 j->files = hashmap_new(string_hash_func, string_compare_func);
392 /* We ignore most errors here, since the idea is to only open
393 * what's actually accessible, and ignore the rest. */
395 NULSTR_FOREACH(p, search_paths) {
401 log_debug("Failed to open %s: %m", p);
406 struct dirent buf, *de;
409 r = readdir_r(d, &buf, &de);
413 if (dirent_is_file_with_suffix(de, ".journal")) {
414 r = add_file(j, p, NULL, de->d_name);
416 log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r));
418 } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
419 sd_id128_from_string(de->d_name, &id) >= 0) {
421 r = add_directory(j, p, de->d_name);
423 log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r));
439 void sd_journal_close(sd_journal *j) {
445 while ((f = hashmap_steal_first(j->files)))
446 journal_file_close(f);
448 hashmap_free(j->files);
454 int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
466 if (f->current_offset <= 0)
469 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
473 *ret = le64toh(o->entry.realtime);
477 int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret) {
490 if (f->current_offset <= 0)
493 r = sd_id128_get_machine(&id);
497 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
501 if (!sd_id128_equal(id, o->entry.boot_id))
504 *ret = le64toh(o->entry.monotonic);
509 int sd_journal_get_field(sd_journal *j, const char *field, const void **data, size_t *size) {
521 if (isempty(field) || strchr(field, '='))
528 if (f->current_offset <= 0)
531 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
535 field_length = strlen(field);
537 n = journal_file_entry_n_items(o);
538 for (i = 0; i < n; i++) {
542 p = le64toh(o->entry.items[i].object_offset);
543 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
547 l = le64toh(o->object.size) - offsetof(Object, data.payload);
549 if (l >= field_length+1 &&
550 memcmp(o->data.payload, field, field_length) == 0 &&
551 o->data.payload[field_length] == '=') {
555 if ((uint64_t) t != l)
558 *data = o->data.payload;
564 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
572 int sd_journal_iterate_fields(sd_journal *j, const void **data, size_t *size) {
587 if (f->current_offset <= 0)
590 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
594 n = journal_file_entry_n_items(o);
595 if (f->current_field >= n)
598 p = le64toh(o->entry.items[f->current_field].object_offset);
599 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
603 l = le64toh(o->object.size) - offsetof(Object, data.payload);
606 /* We can't read objects larger than 4G on a 32bit machine */
607 if ((uint64_t) t != l)
610 *data = o->data.payload;
618 int sd_journal_seek_head(sd_journal *j) {
623 int sd_journal_seek_tail(sd_journal *j) {