2011-10-07 21:06:39 +02:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2011 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "sd-journal.h"
|
|
|
|
#include "journal-def.h"
|
2011-10-08 02:20:44 +02:00
|
|
|
#include "journal-file.h"
|
2011-10-07 22:00:05 +02:00
|
|
|
#include "hashmap.h"
|
2011-10-08 02:20:44 +02:00
|
|
|
#include "list.h"
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
typedef struct Match Match;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
struct Match {
|
|
|
|
char *data;
|
|
|
|
size_t size;
|
|
|
|
uint64_t hash;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
LIST_FIELDS(Match, matches);
|
|
|
|
};
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
struct sd_journal {
|
2011-10-07 22:00:05 +02:00
|
|
|
Hashmap *files;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
JournalFile *current_file;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
LIST_HEAD(Match, matches);
|
|
|
|
};
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
int sd_journal_add_match(sd_journal *j, const char *field, const void *data, size_t size) {
|
|
|
|
Match *m;
|
|
|
|
char *e;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
assert(j);
|
|
|
|
assert(field);
|
|
|
|
assert(data || size == 0);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
m = new0(Match, 1);
|
|
|
|
if (!m)
|
|
|
|
return -ENOMEM;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
m->size = strlen(field) + 1 + size;
|
|
|
|
m->data = malloc(m->size);
|
|
|
|
if (!m->data) {
|
|
|
|
free(m);
|
|
|
|
return -ENOMEM;
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
e = stpcpy(m->data, field);
|
|
|
|
*(e++) = '=';
|
|
|
|
memcpy(e, data, size);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
LIST_PREPEND(Match, matches, j->matches, m);
|
2011-10-07 21:06:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
void sd_journal_flush_matches(sd_journal *j) {
|
|
|
|
assert(j);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
while (j->matches) {
|
|
|
|
Match *m = j->matches;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
LIST_REMOVE(Match, matches, j->matches, m);
|
|
|
|
free(m->data);
|
|
|
|
free(m);
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
|
|
|
|
JournalFile *bf, Object *bo, uint64_t bp) {
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
uint64_t a, b;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
/* If this is from the same seqnum source, compare
|
|
|
|
* seqnums */
|
|
|
|
a = le64toh(ao->entry.seqnum);
|
|
|
|
b = le64toh(bo->entry.seqnum);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
} else if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
/* If the boot id matches compare monotonic time */
|
|
|
|
a = le64toh(ao->entry.monotonic);
|
|
|
|
b = le64toh(bo->entry.monotonic);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
/* Otherwise compare UTC time */
|
|
|
|
a = le64toh(ao->entry.realtime);
|
|
|
|
b = le64toh(ao->entry.realtime);
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
return
|
|
|
|
a < b ? -1 :
|
|
|
|
a > b ? +1 : 0;
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
int sd_journal_next(sd_journal *j) {
|
|
|
|
JournalFile *f, *new_current = NULL;
|
|
|
|
Iterator i;
|
2011-10-07 21:06:39 +02:00
|
|
|
int r;
|
2011-10-08 02:20:44 +02:00
|
|
|
uint64_t new_offset = 0;
|
|
|
|
Object *new_entry = NULL;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
assert(j);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
HASHMAP_FOREACH(f, j->files, i) {
|
|
|
|
Object *o;
|
|
|
|
uint64_t p;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
if (f->current_offset > 0) {
|
|
|
|
r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
} else
|
|
|
|
o = NULL;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
r = journal_file_next_entry(f, o, &o, &p);
|
2011-10-07 21:06:39 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-10-08 02:20:44 +02:00
|
|
|
else if (r == 0)
|
|
|
|
continue;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
if (!new_current || compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
|
|
|
|
new_current = f;
|
|
|
|
new_entry = o;
|
|
|
|
new_offset = p;
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
if (new_current) {
|
|
|
|
j->current_file = new_current;
|
|
|
|
f->current_offset = new_offset;
|
|
|
|
return 1;
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
int sd_journal_previous(sd_journal *j) {
|
|
|
|
JournalFile *f, *new_current = NULL;
|
|
|
|
Iterator i;
|
2011-10-07 21:06:39 +02:00
|
|
|
int r;
|
2011-10-08 02:20:44 +02:00
|
|
|
uint64_t new_offset = 0;
|
|
|
|
Object *new_entry = NULL;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
assert(j);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
HASHMAP_FOREACH(f, j->files, i) {
|
2011-10-07 21:56:11 +02:00
|
|
|
Object *o;
|
2011-10-08 02:20:44 +02:00
|
|
|
uint64_t p;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
if (f->current_offset > 0) {
|
|
|
|
r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
} else
|
|
|
|
o = NULL;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
r = journal_file_prev_entry(f, o, &o, &p);
|
2011-10-07 21:06:39 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-10-08 02:20:44 +02:00
|
|
|
else if (r == 0)
|
|
|
|
continue;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
if (!new_current || compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
|
|
|
|
new_current = f;
|
|
|
|
new_entry = o;
|
|
|
|
new_offset = p;
|
|
|
|
}
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
if (new_current) {
|
|
|
|
j->current_file = new_current;
|
|
|
|
f->current_offset = new_offset;
|
|
|
|
return 1;
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
int sd_journal_get_cursor(sd_journal *j, void **cursor, size_t *size) {
|
|
|
|
JournalCursor *c;
|
|
|
|
Object *o;
|
2011-10-07 21:06:39 +02:00
|
|
|
int r;
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
assert(j);
|
|
|
|
assert(cursor);
|
|
|
|
assert(size);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
if (!j->current_file || !j->current_file->current_offset <= 0)
|
2011-10-07 21:06:39 +02:00
|
|
|
return 0;
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
r = journal_file_move_to_object(j->current_file, j->current_file->current_offset, OBJECT_ENTRY, &o);
|
2011-10-07 21:06:39 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
c = new0(JournalCursor, 1);
|
|
|
|
if (!c)
|
|
|
|
return -ENOMEM;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
c->version = 1;
|
|
|
|
c->seqnum = o->entry.seqnum;
|
|
|
|
c->seqnum_id = j->current_file->header->seqnum_id;
|
|
|
|
c->boot_id = o->entry.boot_id;
|
|
|
|
c->monotonic = o->entry.monotonic;
|
|
|
|
c->realtime = o->entry.realtime;
|
|
|
|
c->xor_hash = o->entry.xor_hash;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
*cursor = c;
|
|
|
|
*size = sizeof(JournalCursor);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-10-08 02:20:44 +02:00
|
|
|
int sd_journal_set_cursor(sd_journal *j, const void *cursor, size_t size) {
|
|
|
|
return -EINVAL;
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int sd_journal_open(sd_journal **ret) {
|
|
|
|
sd_journal *j;
|
|
|
|
char *fn;
|
|
|
|
const char *p;
|
|
|
|
int r = 0;
|
|
|
|
const char search_paths[] =
|
|
|
|
"/run/log/journal\0"
|
|
|
|
"/var/log/journal\0";
|
|
|
|
|
|
|
|
assert(ret);
|
|
|
|
|
|
|
|
j = new0(sd_journal, 1);
|
|
|
|
if (!j)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-10-07 22:00:05 +02:00
|
|
|
j->files = hashmap_new(string_hash_func, string_compare_func);
|
|
|
|
if (!j->files)
|
|
|
|
goto fail;
|
|
|
|
|
2011-10-07 21:06:39 +02:00
|
|
|
NULSTR_FOREACH(p, search_paths) {
|
|
|
|
DIR *d;
|
|
|
|
|
|
|
|
d = opendir(p);
|
|
|
|
if (!d) {
|
|
|
|
if (errno != ENOENT && r == 0)
|
|
|
|
r = -errno;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
struct dirent buf, *de;
|
|
|
|
int k;
|
2011-10-07 22:00:05 +02:00
|
|
|
JournalFile *f;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
k = readdir_r(d, &buf, &de);
|
|
|
|
if (k != 0) {
|
|
|
|
if (r == 0)
|
|
|
|
r = -k;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!de)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!dirent_is_file_with_suffix(de, ".journal"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fn = join(p, "/", de->d_name, NULL);
|
|
|
|
if (!fn) {
|
|
|
|
r = -ENOMEM;
|
|
|
|
closedir(d);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-10-13 05:19:35 +02:00
|
|
|
k = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
|
2011-10-07 21:06:39 +02:00
|
|
|
free(fn);
|
|
|
|
|
2011-10-07 22:00:05 +02:00
|
|
|
if (k < 0) {
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-07 22:00:05 +02:00
|
|
|
if (r == 0)
|
|
|
|
r = -k;
|
|
|
|
} else {
|
|
|
|
k = hashmap_put(j->files, f->path, f);
|
|
|
|
if (k < 0) {
|
|
|
|
journal_file_close(f);
|
|
|
|
closedir(d);
|
|
|
|
|
|
|
|
r = k;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*ret = j;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
sd_journal_close(j);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
|
|
|
void sd_journal_close(sd_journal *j) {
|
|
|
|
assert(j);
|
|
|
|
|
2011-10-07 22:00:05 +02:00
|
|
|
if (j->files) {
|
|
|
|
JournalFile *f;
|
|
|
|
|
|
|
|
while ((f = hashmap_steal_first(j->files)))
|
|
|
|
journal_file_close(f);
|
|
|
|
|
|
|
|
hashmap_free(j->files);
|
|
|
|
}
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
free(j);
|
|
|
|
}
|