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/>.
25 #include "sd-journal.h"
26 #include "journal-def.h"
27 #include "journal-file.h"
31 typedef struct Match Match;
38 LIST_FIELDS(Match, matches);
44 JournalFile *current_file;
46 LIST_HEAD(Match, matches);
49 int sd_journal_add_match(sd_journal *j, const char *field, const void *data, size_t size) {
55 assert(data || size == 0);
61 m->size = strlen(field) + 1 + size;
62 m->data = malloc(m->size);
68 e = stpcpy(m->data, field);
70 memcpy(e, data, size);
72 LIST_PREPEND(Match, matches, j->matches, m);
76 void sd_journal_flush_matches(sd_journal *j) {
80 Match *m = j->matches;
82 LIST_REMOVE(Match, matches, j->matches, m);
88 static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
89 JournalFile *bf, Object *bo, uint64_t bp) {
93 if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
95 /* If this is from the same seqnum source, compare
97 a = le64toh(ao->entry.seqnum);
98 b = le64toh(bo->entry.seqnum);
101 } else if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
103 /* If the boot id matches compare monotonic time */
104 a = le64toh(ao->entry.monotonic);
105 b = le64toh(bo->entry.monotonic);
109 /* Otherwise compare UTC time */
110 a = le64toh(ao->entry.realtime);
111 b = le64toh(ao->entry.realtime);
119 int sd_journal_next(sd_journal *j) {
120 JournalFile *f, *new_current = NULL;
123 uint64_t new_offset = 0;
124 Object *new_entry = NULL;
128 HASHMAP_FOREACH(f, j->files, i) {
132 if (f->current_offset > 0) {
133 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
139 r = journal_file_next_entry(f, o, &o, &p);
145 if (!new_current || compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
153 j->current_file = new_current;
154 f->current_offset = new_offset;
161 int sd_journal_previous(sd_journal *j) {
162 JournalFile *f, *new_current = NULL;
165 uint64_t new_offset = 0;
166 Object *new_entry = NULL;
170 HASHMAP_FOREACH(f, j->files, i) {
174 if (f->current_offset > 0) {
175 r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
181 r = journal_file_prev_entry(f, o, &o, &p);
187 if (!new_current || compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
195 j->current_file = new_current;
196 f->current_offset = new_offset;
203 int sd_journal_get_cursor(sd_journal *j, void **cursor, size_t *size) {
212 if (!j->current_file || !j->current_file->current_offset <= 0)
215 r = journal_file_move_to_object(j->current_file, j->current_file->current_offset, OBJECT_ENTRY, &o);
219 c = new0(JournalCursor, 1);
224 c->seqnum = o->entry.seqnum;
225 c->seqnum_id = j->current_file->header->seqnum_id;
226 c->boot_id = o->entry.boot_id;
227 c->monotonic = o->entry.monotonic;
228 c->realtime = o->entry.realtime;
229 c->xor_hash = o->entry.xor_hash;
232 *size = sizeof(JournalCursor);
237 int sd_journal_set_cursor(sd_journal *j, const void *cursor, size_t size) {
241 int sd_journal_open(sd_journal **ret) {
246 const char search_paths[] =
248 "/var/log/journal\0";
252 j = new0(sd_journal, 1);
256 j->files = hashmap_new(string_hash_func, string_compare_func);
260 NULSTR_FOREACH(p, search_paths) {
265 if (errno != ENOENT && r == 0)
272 struct dirent buf, *de;
276 k = readdir_r(d, &buf, &de);
287 if (!dirent_is_file_with_suffix(de, ".journal"))
290 fn = join(p, "/", de->d_name, NULL);
297 k = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
305 k = hashmap_put(j->files, f->path, f);
307 journal_file_close(f);
326 void sd_journal_close(sd_journal *j) {
332 while ((f = hashmap_steal_first(j->files)))
333 journal_file_close(f);
335 hashmap_free(j->files);