along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <sys/mman.h>
#include <errno.h>
-#include <sys/uio.h>
-#include <unistd.h>
-#include <sys/statvfs.h>
#include <fcntl.h>
#include <stddef.h>
#include "sd-journal.h"
#include "journal-def.h"
-#include "journal-private.h"
-#include "lookup3.h"
+#include "journal-file.h"
+#include "hashmap.h"
#include "list.h"
+#include "lookup3.h"
-#define DEFAULT_ARENA_MAX_SIZE (16ULL*1024ULL*1024ULL*1024ULL)
-#define DEFAULT_ARENA_MIN_SIZE (256ULL*1024ULL)
-#define DEFAULT_ARENA_KEEP_FREE (1ULL*1024ULL*1024ULL)
+typedef struct Match Match;
-#define DEFAULT_HASH_TABLE_SIZE (2047ULL*16ULL)
-#define DEFAULT_BISECT_TABLE_SIZE ((DEFAULT_ARENA_MAX_SIZE/(64ULL*1024ULL))*8ULL)
+struct Match {
+ char *data;
+ size_t size;
+ uint64_t le_hash;
-#define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
+ LIST_FIELDS(Match, matches);
+};
-struct JournalFile {
- sd_journal *journal;
+struct sd_journal {
+ Hashmap *files;
- int fd;
- char *path;
- struct stat last_stat;
- int prot;
- bool writable;
+ JournalFile *current_file;
+ uint64_t current_field;
- Header *header;
+ LIST_HEAD(Match, matches);
+ unsigned n_matches;
+};
- HashItem *hash_table;
- void *hash_table_window;
- uint64_t hash_table_window_size;
+int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
+ Match *m;
- uint64_t *bisect_table;
- void *bisect_table_window;
- uint64_t bisect_table_window_size;
+ assert(j);
- void *window;
- uint64_t window_offset;
- uint64_t window_size;
+ if (size <= 0)
+ return -EINVAL;
- Object *current;
- uint64_t current_offset;
+ assert(data);
- LIST_FIELDS(JournalFile, files);
-};
+ m = new0(Match, 1);
+ if (!m)
+ return -ENOMEM;
-struct sd_journal {
- LIST_HEAD(JournalFile, files);
-};
+ m->size = size;
-static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
+ m->data = malloc(m->size);
+ if (!m->data) {
+ free(m);
+ return -ENOMEM;
+ }
-#define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
+ memcpy(m->data, data, size);
+ m->le_hash = hash64(m->data, size);
-void journal_file_close(JournalFile *f) {
- assert(f);
+ LIST_PREPEND(Match, matches, j->matches, m);
+ j->n_matches ++;
- if (f->journal)
- LIST_REMOVE(JournalFile, files, f->journal->files, f);
+ return 0;
+}
- if (f->fd >= 0)
- close_nointr_nofail(f->fd);
+void sd_journal_flush_matches(sd_journal *j) {
+ assert(j);
- if (f->header)
- munmap(f->header, PAGE_ALIGN(sizeof(Header)));
+ while (j->matches) {
+ Match *m = j->matches;
- if (f->hash_table_window)
- munmap(f->hash_table_window, f->hash_table_window_size);
+ LIST_REMOVE(Match, matches, j->matches, m);
+ free(m->data);
+ free(m);
+ }
- if (f->bisect_table_window)
- munmap(f->bisect_table_window, f->bisect_table_window_size);
+ j->n_matches = 0;
+}
- if (f->window)
- munmap(f->window, f->window_size);
+static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
+ JournalFile *bf, Object *bo, uint64_t bp) {
- free(f->path);
- free(f);
-}
+ uint64_t a, b;
-static int journal_file_init_header(JournalFile *f) {
- Header h;
- ssize_t k;
- int r;
+ /* We operate on two different files here, hence we can access
+ * two objects at the same time, which we normally can't.
+ *
+ * If contents and timestamps match, these entries are
+ * identical, even if the seqnum does not match */
- assert(f);
+ if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) &&
+ ao->entry.monotonic == bo->entry.monotonic &&
+ ao->entry.realtime == bo->entry.realtime &&
+ ao->entry.xor_hash == bo->entry.xor_hash)
+ return 0;
- zero(h);
- memcpy(h.signature, signature, 8);
- h.arena_offset = htole64(ALIGN64(sizeof(h)));
- h.arena_max_size = htole64(DEFAULT_ARENA_MAX_SIZE);
- h.arena_min_size = htole64(DEFAULT_ARENA_MIN_SIZE);
- h.arena_keep_free = htole64(DEFAULT_ARENA_KEEP_FREE);
+ if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
- r = sd_id128_randomize(&h.file_id);
- if (r < 0)
- return r;
+ /* If this is from the same seqnum source, compare
+ * seqnums */
+ a = le64toh(ao->entry.seqnum);
+ b = le64toh(bo->entry.seqnum);
- k = pwrite(f->fd, &h, sizeof(h), 0);
- if (k < 0)
- return -errno;
+ if (a < b)
+ return -1;
+ if (a > b)
+ return 1;
- if (k != sizeof(h))
- return -EIO;
+ /* Wow! This is weird, different data but the same
+ * seqnums? Something is borked, but let's make the
+ * best of it and compare by time. */
+ }
- return 0;
-}
+ if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
-static int journal_file_refresh_header(JournalFile *f) {
- int r;
+ /* If the boot id matches compare monotonic time */
+ a = le64toh(ao->entry.monotonic);
+ b = le64toh(bo->entry.monotonic);
- assert(f);
+ if (a < b)
+ return -1;
+ if (a > b)
+ return 1;
+ }
- r = sd_id128_get_machine(&f->header->machine_id);
- if (r < 0)
- return r;
+ /* Otherwise compare UTC time */
+ a = le64toh(ao->entry.realtime);
+ b = le64toh(ao->entry.realtime);
- r = sd_id128_get_boot(&f->header->boot_id);
- if (r < 0)
- return r;
+ if (a < b)
+ return -1;
+ if (a > b)
+ return 1;
+
+ /* Finally, compare by contents */
+ a = le64toh(ao->entry.xor_hash);
+ b = le64toh(ao->entry.xor_hash);
+
+ if (a < b)
+ return -1;
+ if (a > b)
+ return 1;
- f->header->state = htole32(STATE_ONLINE);
return 0;
}
-static int journal_file_verify_header(JournalFile *f) {
+static int move_to_next_with_matches(sd_journal *j, JournalFile *f, direction_t direction, Object **o, uint64_t *p) {
+ int r;
+ uint64_t cp;
+ Object *c;
+
+ assert(j);
assert(f);
+ assert(o);
+ assert(p);
- if (memcmp(f->header, signature, 8))
- return -EBADMSG;
+ if (!j->matches) {
+ /* No matches is easy, just go on to the next entry */
- if (f->header->incompatible_flags != 0)
- return -EPROTONOSUPPORT;
+ if (f->current_offset > 0) {
+ r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &c);
+ if (r < 0)
+ return r;
+ } else
+ c = NULL;
- if ((uint64_t) f->last_stat.st_size < (le64toh(f->header->arena_offset) + le64toh(f->header->arena_size)))
- return -ENODATA;
+ return journal_file_next_entry(f, c, direction, o, p);
+ }
- if (f->writable) {
- uint32_t state;
- sd_id128_t machine_id;
- int r;
+ /* So there are matches we have to adhere to, let's find the
+ * first entry that matches all of them */
- r = sd_id128_get_machine(&machine_id);
- if (r < 0)
+ if (f->current_offset > 0)
+ cp = f->current_offset;
+ else {
+ r = journal_file_find_first_entry(f, j->matches->data, j->matches->size, direction, &c, &cp);
+ if (r <= 0)
return r;
- if (!sd_id128_equal(machine_id, f->header->machine_id))
- return -EHOSTDOWN;
-
- state = le32toh(f->header->state);
-
- if (state == STATE_ONLINE)
- log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f->path);
- else if (state == STATE_ARCHIVED)
- return -ESHUTDOWN;
- else if (state != STATE_OFFLINE)
- log_debug("Journal file %s has unknown state %u. Ignoring.", f->path, state);
+ /* We can shortcut this if there's only one match */
+ if (j->n_matches == 1) {
+ *o = c;
+ *p = cp;
+ return r;
+ }
}
- return 0;
-}
-
-static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
- uint64_t asize;
- uint64_t old_size, new_size;
+ for (;;) {
+ uint64_t np, n;
+ bool found;
+ Match *m;
- assert(f);
+ r = journal_file_move_to_object(f, cp, OBJECT_ENTRY, &c);
+ if (r < 0)
+ return r;
- if (offset < le64toh(f->header->arena_offset))
- return -EINVAL;
+ n = journal_file_entry_n_items(c);
- new_size = PAGE_ALIGN(offset + size);
+ /* Make sure we don't match the entry we are starting
+ * from. */
+ found = f->current_offset != cp;
- /* We assume that this file is not sparse, and we know that
- * for sure, since we alway call posix_fallocate()
- * ourselves */
+ np = 0;
+ LIST_FOREACH(matches, m, j->matches) {
+ uint64_t q, k;
- old_size =
- le64toh(f->header->arena_offset) +
- le64toh(f->header->arena_size);
+ for (k = 0; k < n; k++)
+ if (c->entry.items[k].hash == m->le_hash)
+ break;
- if (old_size >= new_size)
- return 0;
+ if (k >= n) {
+ /* Hmm, didn't find any field that matched, so ignore
+ * this match. Go on with next match */
- asize = new_size - le64toh(f->header->arena_offset);
+ found = false;
+ continue;
+ }
- if (asize > le64toh(f->header->arena_min_size)) {
- struct statvfs svfs;
+ /* Hmm, so, this field matched, let's remember
+ * where we'd have to try next, in case the other
+ * matches are not OK */
- if (fstatvfs(f->fd, &svfs) >= 0) {
- uint64_t available;
+ if (direction == DIRECTION_DOWN) {
+ q = le64toh(c->entry.items[k].next_entry_offset);
- available = svfs.f_bfree * svfs.f_bsize;
+ if (q > np)
+ np = q;
+ } else {
+ q = le64toh(c->entry.items[k].prev_entry_offset);
- if (available >= f->header->arena_keep_free)
- available -= f->header->arena_keep_free;
- else
- available = 0;
+ if (q != 0 && (np == 0 || q < np))
+ np = q;
+ }
+ }
- if (new_size - old_size > available)
- return -E2BIG;
+ /* Did this entry match against all matches? */
+ if (found) {
+ *o = c;
+ *p = cp;
+ return 1;
}
- }
- if (asize > le64toh(f->header->arena_max_size))
- return -E2BIG;
+ /* Did we find a subsequent entry? */
+ if (np == 0)
+ return 0;
- if (posix_fallocate(f->fd, 0, new_size) < 0)
- return -errno;
+ /* Hmm, ok, this entry only matched partially, so
+ * let's try another one */
+ cp = np;
+ }
+}
- if (fstat(f->fd, &f->last_stat) < 0)
- return -errno;
+static int real_journal_next(sd_journal *j, direction_t direction) {
+ JournalFile *f, *new_current = NULL;
+ Iterator i;
+ int r;
+ uint64_t new_offset = 0;
+ Object *new_entry = NULL;
- f->header->arena_size = htole64(asize);
+ assert(j);
- return 0;
-}
+ HASHMAP_FOREACH(f, j->files, i) {
+ Object *o;
+ uint64_t p;
-static int journal_file_map(
- JournalFile *f,
- uint64_t offset,
- uint64_t size,
- void **_window,
- uint64_t *_woffset,
- uint64_t *_wsize,
- void **ret) {
+ r = move_to_next_with_matches(j, f, direction, &o, &p);
+ if (r < 0)
+ return r;
+ else if (r == 0)
+ continue;
- uint64_t woffset, wsize;
- void *window;
+ if (!new_current ||
+ compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
+ new_current = f;
+ new_entry = o;
+ new_offset = p;
+ }
+ }
- assert(f);
- assert(size > 0);
- assert(ret);
+ if (new_current) {
+ j->current_file = new_current;
+ j->current_file->current_offset = new_offset;
+ j->current_field = 0;
- woffset = offset & ~((uint64_t) page_size() - 1ULL);
- wsize = size + (offset - woffset);
- wsize = PAGE_ALIGN(wsize);
+ /* Skip over any identical entries in the other files too */
- window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
- if (window == MAP_FAILED)
- return -errno;
+ HASHMAP_FOREACH(f, j->files, i) {
+ Object *o;
+ uint64_t p;
- if (_window)
- *_window = window;
+ if (j->current_file == f)
+ continue;
- if (_woffset)
- *_woffset = woffset;
+ r = move_to_next_with_matches(j, f, direction, &o, &p);
+ if (r < 0)
+ return r;
+ else if (r == 0)
+ continue;
- if (_wsize)
- *_wsize = wsize;
+ if (compare_order(new_current, new_entry, new_offset, f, o, p) == 0)
+ f->current_offset = p;
+ }
- *ret = (uint8_t*) window + (offset - woffset);
+ return 1;
+ }
return 0;
}
-static int journal_file_move_to(JournalFile *f, uint64_t offset, uint64_t size, void **ret) {
- void *p;
- uint64_t delta;
- int r;
-
- assert(f);
- assert(ret);
+int sd_journal_next(sd_journal *j) {
+ return real_journal_next(j, DIRECTION_DOWN);
+}
- if (_likely_(f->window &&
- f->window_offset <= offset &&
- f->window_offset+f->window_size >= offset + size)) {
+int sd_journal_previous(sd_journal *j) {
+ return real_journal_next(j, DIRECTION_UP);
+}
- *ret = (uint8_t*) f->window + (offset - f->window_offset);
- return 0;
- }
+int sd_journal_get_cursor(sd_journal *j, char **cursor) {
+ Object *o;
+ int r;
+ char bid[33], sid[33];
- if (f->window) {
- if (munmap(f->window, f->window_size) < 0)
- return -errno;
+ assert(j);
+ assert(cursor);
- f->window = NULL;
- f->window_size = f->window_offset = 0;
- }
+ if (!j->current_file || j->current_file->current_offset <= 0)
+ return -EADDRNOTAVAIL;
- if (size < DEFAULT_WINDOW_SIZE) {
- /* If the default window size is larger then what was
- * asked for extend the mapping a bit in the hope to
- * minimize needed remappings later on. We add half
- * the window space before and half behind the
- * requested mapping */
+ r = journal_file_move_to_object(j->current_file, j->current_file->current_offset, OBJECT_ENTRY, &o);
+ if (r < 0)
+ return r;
- delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
+ sd_id128_to_string(j->current_file->header->seqnum_id, sid);
+ sd_id128_to_string(o->entry.boot_id, bid);
- if (offset < delta)
- delta = offset;
+ if (asprintf(cursor,
+ "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
+ sid, (unsigned long long) le64toh(o->entry.seqnum),
+ bid, (unsigned long long) le64toh(o->entry.monotonic),
+ (unsigned long long) le64toh(o->entry.realtime),
+ (unsigned long long) le64toh(o->entry.xor_hash),
+ file_name_from_path(j->current_file->path)) < 0)
+ return -ENOMEM;
- offset -= delta;
- size += (DEFAULT_WINDOW_SIZE - delta);
- } else
- delta = 0;
+ return 1;
+}
- r = journal_file_map(f,
- offset, size,
- &f->window, &f->window_offset, &f->window_size,
- & p);
+int sd_journal_set_cursor(sd_journal *j, const char *cursor) {
+ return -EINVAL;
+}
- if (r < 0)
- return r;
+static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) {
+ char *fn;
+ int r;
+ JournalFile *f;
- *ret = (uint8_t*) p + delta;
- return 0;
-}
+ assert(j);
+ assert(prefix);
+ assert(filename);
-static bool verify_hash(Object *o) {
- uint64_t t;
+ if (dir)
+ fn = join(prefix, "/", dir, "/", filename, NULL);
+ else
+ fn = join(prefix, "/", filename, NULL);
- assert(o);
+ if (!fn)
+ return -ENOMEM;
- t = le64toh(o->object.type);
- if (t == OBJECT_DATA) {
- uint64_t s, h1, h2;
+ r = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
+ free(fn);
- s = le64toh(o->object.size);
+ if (r < 0) {
+ if (errno == ENOENT)
+ return 0;
- h1 = le64toh(o->data.hash);
- h2 = hash64(o->data.payload, s - offsetof(Object, data.payload));
+ return r;
+ }
- return h1 == h2;
+ r = hashmap_put(j->files, f->path, f);
+ if (r < 0) {
+ journal_file_close(f);
+ return r;
}
- return true;
+ return 0;
}
-int journal_file_move_to_object(JournalFile *f, uint64_t offset, Object **ret) {
+static int add_directory(sd_journal *j, const char *prefix, const char *dir) {
+ char *fn;
int r;
- void *t;
- Object *o;
- uint64_t s;
-
- assert(f);
- assert(ret);
+ DIR *d;
- r = journal_file_move_to(f, offset, sizeof(ObjectHeader), &t);
- if (r < 0)
- return r;
+ assert(j);
+ assert(prefix);
+ assert(dir);
- o = (Object*) t;
- s = le64toh(o->object.size);
+ fn = join(prefix, "/", dir, NULL);
+ if (!fn)
+ return -ENOMEM;
- if (s < sizeof(ObjectHeader))
- return -EBADMSG;
+ d = opendir(fn);
+ free(fn);
- if (s > sizeof(ObjectHeader)) {
- r = journal_file_move_to(f, offset, s, &t);
- if (r < 0)
- return r;
+ if (!d) {
+ if (errno == ENOENT)
+ return 0;
- o = (Object*) t;
+ return -errno;
}
- if (!verify_hash(o))
- return -EBADMSG;
+ for (;;) {
+ struct dirent buf, *de;
- *ret = o;
- return 0;
-}
+ r = readdir_r(d, &buf, &de);
+ if (r != 0 || !de)
+ break;
-static uint64_t journal_file_seqnum(JournalFile *f) {
- uint64_t r;
+ if (!dirent_is_file_with_suffix(de, ".journal"))
+ continue;
- assert(f);
+ r = add_file(j, prefix, dir, de->d_name);
+ if (r < 0)
+ log_debug("Failed to add file %s/%s/%s: %s", prefix, dir, de->d_name, strerror(-r));
+ }
- r = le64toh(f->header->seqnum) + 1;
- f->header->seqnum = htole64(r);
+ closedir(d);
- return r;
+ return 0;
}
-static int journal_file_append_object(JournalFile *f, uint64_t size, Object **ret, uint64_t *offset) {
+int sd_journal_open(sd_journal **ret) {
+ sd_journal *j;
+ const char *p;
+ const char search_paths[] =
+ "/run/log/journal\0"
+ "/var/log/journal\0";
int r;
- uint64_t p;
- Object *tail, *o;
- void *t;
- assert(f);
- assert(size >= sizeof(ObjectHeader));
- assert(offset);
assert(ret);
- p = le64toh(f->header->tail_object_offset);
-
- if (p == 0)
- p = le64toh(f->header->arena_offset);
- else {
- r = journal_file_move_to_object(f, p, &tail);
- if (r < 0)
- return r;
+ j = new0(sd_journal, 1);
+ if (!j)
+ return -ENOMEM;
- p += ALIGN64(le64toh(tail->object.size));
+ j->files = hashmap_new(string_hash_func, string_compare_func);
+ if (!j->files) {
+ r = -ENOMEM;
+ goto fail;
}
- r = journal_file_allocate(f, p, size);
- if (r < 0)
- return r;
-
- r = journal_file_move_to(f, p, size, &t);
- if (r < 0)
- return r;
-
- o = (Object*) t;
+ /* We ignore most errors here, since the idea is to only open
+ * what's actually accessible, and ignore the rest. */
- zero(o->object);
- o->object.type = htole64(OBJECT_UNUSED);
- zero(o->object.reserved);
- o->object.size = htole64(size);
+ NULSTR_FOREACH(p, search_paths) {
+ DIR *d;
- f->header->tail_object_offset = htole64(p);
- if (f->header->head_object_offset == 0)
- f->header->head_object_offset = htole64(p);
+ d = opendir(p);
+ if (!d) {
+ if (errno != ENOENT)
+ log_debug("Failed to open %s: %m", p);
+ continue;
+ }
- f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
+ for (;;) {
+ struct dirent buf, *de;
+ sd_id128_t id;
- *ret = o;
- *offset = p;
+ r = readdir_r(d, &buf, &de);
+ if (r != 0 || !de)
+ break;
- return 0;
-}
+ if (dirent_is_file_with_suffix(de, ".journal")) {
+ r = add_file(j, p, NULL, de->d_name);
+ if (r < 0)
+ log_debug("Failed to add file %s/%s: %s", p, de->d_name, strerror(-r));
-static int journal_file_setup_hash_table(JournalFile *f) {
- uint64_t s, p;
- Object *o;
- int r;
+ } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
+ sd_id128_from_string(de->d_name, &id) >= 0) {
- assert(f);
+ r = add_directory(j, p, de->d_name);
+ if (r < 0)
+ log_debug("Failed to add directory %s/%s: %s", p, de->d_name, strerror(-r));
+ }
+ }
- s = DEFAULT_HASH_TABLE_SIZE;
- r = journal_file_append_object(f, offsetof(Object, hash_table.table) + s, &o, &p);
- if (r < 0)
- return r;
+ closedir(d);
+ }
- o->object.type = htole64(OBJECT_HASH_TABLE);
- memset(o->hash_table.table, 0, s);
+ *ret = j;
+ return 0;
- f->header->hash_table_offset = htole64(p + offsetof(Object, hash_table.table));
- f->header->hash_table_size = htole64(s);
+fail:
+ sd_journal_close(j);
- return 0;
-}
+ return r;
+};
-static int journal_file_setup_bisect_table(JournalFile *f) {
- uint64_t s, p;
- Object *o;
- int r;
+void sd_journal_close(sd_journal *j) {
+ assert(j);
- assert(f);
+ if (j->files) {
+ JournalFile *f;
- s = DEFAULT_BISECT_TABLE_SIZE;
- r = journal_file_append_object(f, offsetof(Object, bisect_table.table) + s, &o, &p);
- if (r < 0)
- return r;
+ while ((f = hashmap_steal_first(j->files)))
+ journal_file_close(f);
- o->object.type = htole64(OBJECT_BISECT_TABLE);
- memset(o->bisect_table.table, 0, s);
+ hashmap_free(j->files);
+ }
- f->header->bisect_table_offset = htole64(p + offsetof(Object, bisect_table.table));
- f->header->bisect_table_size = htole64(s);
+ sd_journal_flush_matches(j);
- return 0;
+ free(j);
}
-static int journal_file_map_hash_table(JournalFile *f) {
- uint64_t s, p;
- void *t;
+int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
+ Object *o;
+ JournalFile *f;
int r;
- assert(f);
+ assert(j);
+ assert(ret);
+
+ f = j->current_file;
+ if (!f)
+ return 0;
- p = le64toh(f->header->hash_table_offset);
- s = le64toh(f->header->hash_table_size);
+ if (f->current_offset <= 0)
+ return 0;
- r = journal_file_map(f,
- p, s,
- &f->hash_table_window, NULL, &f->hash_table_window_size,
- &t);
+ r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
if (r < 0)
return r;
- f->hash_table = t;
- return 0;
+ *ret = le64toh(o->entry.realtime);
+ return 1;
}
-static int journal_file_map_bisect_table(JournalFile *f) {
- uint64_t s, p;
- void *t;
+int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret) {
+ Object *o;
+ JournalFile *f;
int r;
+ sd_id128_t id;
- assert(f);
+ assert(j);
+ assert(ret);
+
+ f = j->current_file;
+ if (!f)
+ return 0;
- p = le64toh(f->header->bisect_table_offset);
- s = le64toh(f->header->bisect_table_size);
+ if (f->current_offset <= 0)
+ return 0;
- r = journal_file_map(f,
- p, s,
- &f->bisect_table_window, NULL, &f->bisect_table_window_size,
- &t);
+ r = sd_id128_get_boot(&id);
+ if (r < 0)
+ return r;
+ r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
if (r < 0)
return r;
- f->bisect_table = t;
- return 0;
+ if (!sd_id128_equal(id, o->entry.boot_id))
+ return 0;
+
+ *ret = le64toh(o->entry.monotonic);
+ return 1;
+
}
-static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash_index) {
- uint64_t p;
+int sd_journal_get_field(sd_journal *j, const char *field, const void **data, size_t *size) {
+ JournalFile *f;
+ uint64_t i, n;
+ size_t field_length;
int r;
+ Object *o;
- assert(f);
- assert(o);
- assert(offset > 0);
- assert(o->object.type == htole64(OBJECT_DATA));
+ assert(j);
+ assert(field);
+ assert(data);
+ assert(size);
- o->data.head_entry_offset = o->data.tail_entry_offset = 0;
- o->data.next_hash_offset = 0;
+ if (isempty(field) || strchr(field, '='))
+ return -EINVAL;
- p = le64toh(f->hash_table[hash_index].tail_hash_offset);
- if (p == 0) {
- /* Only entry in the hash table is easy */
+ f = j->current_file;
+ if (!f)
+ return 0;
- o->data.prev_hash_offset = 0;
- f->hash_table[hash_index].head_hash_offset = htole64(offset);
- } else {
- o->data.prev_hash_offset = htole64(p);
+ if (f->current_offset <= 0)
+ return 0;
- /* Temporarily move back to the previous data object,
- * to patch in pointer */
+ r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
+ if (r < 0)
+ return r;
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
+ field_length = strlen(field);
- o->data.next_hash_offset = offset;
+ n = journal_file_entry_n_items(o);
+ for (i = 0; i < n; i++) {
+ uint64_t p, l, h;
+ size_t t;
- r = journal_file_move_to_object(f, offset, &o);
+ p = le64toh(o->entry.items[i].object_offset);
+ h = o->entry.items[j->current_field].hash;
+ r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
if (r < 0)
return r;
- }
-
- f->hash_table[hash_index].tail_hash_offset = htole64(offset);
-
- return 0;
-}
-
-static int journal_file_append_data(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
- uint64_t hash, h, p, np;
- uint64_t osize;
- Object *o;
- int r;
-
- assert(f);
- assert(data || size == 0);
-
- osize = offsetof(Object, data.payload) + size;
-
- hash = hash64(data, size);
- h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
- p = le64toh(f->hash_table[h].head_hash_offset);
-
- while (p != 0) {
- /* Look for this data object in the hash table */
-
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
-
- if (le64toh(o->object.type) != OBJECT_DATA)
- return -EBADMSG;
-
- if (le64toh(o->object.size) == osize &&
- memcmp(o->data.payload, data, size) == 0) {
-
- if (le64toh(o->data.hash) != hash)
- return -EBADMSG;
-
- if (ret)
- *ret = o;
-
- if (offset)
- *offset = p;
-
- return 0;
- }
-
- p = le64toh(o->data.next_hash_offset);
- }
-
- r = journal_file_append_object(f, osize, &o, &np);
- if (r < 0)
- return r;
-
- o->object.type = htole64(OBJECT_DATA);
- o->data.hash = htole64(hash);
- memcpy(o->data.payload, data, size);
-
- r = journal_file_link_data(f, o, np, h);
- if (r < 0)
- return r;
-
- if (ret)
- *ret = o;
-
- if (offset)
- *offset = np;
-
- return 0;
-}
-
-uint64_t journal_file_entry_n_items(Object *o) {
- assert(o);
- assert(o->object.type == htole64(OBJECT_ENTRY));
-
- return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
-}
-
-static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
- uint64_t p, q;
- int r;
- assert(f);
- assert(o);
- assert(offset > 0);
-
- p = le64toh(o->entry.items[i].object_offset);
- if (p == 0)
- return -EINVAL;
-
- o->entry.items[i].next_entry_offset = 0;
- /* Move to the data object */
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
-
- if (o->object.type != htole64(OBJECT_DATA))
- return -EBADMSG;
-
- q = le64toh(o->data.tail_entry_offset);
- o->data.tail_entry_offset = htole64(offset);
-
- if (q == 0)
- o->data.head_entry_offset = htole64(offset);
- else {
- uint64_t n, j;
-
- /* Move to previous entry */
- r = journal_file_move_to_object(f, q, &o);
- if (r < 0)
- return r;
-
- if (o->object.type != htole64(OBJECT_ENTRY))
+ if (h != o->data.hash)
return -EBADMSG;
- n = journal_file_entry_n_items(o);
- for (j = 0; j < n; j++)
- if (le64toh(o->entry.items[j].object_offset) == p)
- break;
-
- if (j >= n)
- return -EBADMSG;
-
- o->entry.items[j].next_entry_offset = offset;
- }
-
- /* Move back to original entry */
- r = journal_file_move_to_object(f, offset, &o);
- if (r < 0)
- return r;
-
- o->entry.items[i].prev_entry_offset = q;
- return 0;
-}
-
-static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
- uint64_t p, i, n, k, a, b;
- int r;
-
- assert(f);
- assert(o);
- assert(offset > 0);
- assert(o->object.type == htole64(OBJECT_ENTRY));
+ l = le64toh(o->object.size) - offsetof(Object, data.payload);
- /* Link up the entry itself */
- p = le64toh(f->header->tail_entry_offset);
+ if (l >= field_length+1 &&
+ memcmp(o->data.payload, field, field_length) == 0 &&
+ o->data.payload[field_length] == '=') {
- o->entry.prev_entry_offset = f->header->tail_entry_offset;
- o->entry.next_entry_offset = 0;
+ t = (size_t) l;
- if (p == 0)
- f->header->head_entry_offset = htole64(offset);
- else {
- /* Temporarily move back to the previous entry, to
- * patch in pointer */
+ if ((uint64_t) t != l)
+ return -E2BIG;
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
+ *data = o->data.payload;
+ *size = t;
- o->entry.next_entry_offset = htole64(offset);
+ return 1;
+ }
- r = journal_file_move_to_object(f, offset, &o);
+ r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
if (r < 0)
return r;
}
- f->header->tail_entry_offset = htole64(offset);
-
- /* Link up the items */
- n = journal_file_entry_n_items(o);
- for (i = 0; i < n; i++) {
- r = journal_file_link_entry_item(f, o, offset, i);
- if (r < 0)
- return r;
- }
-
- /* Link up the entry in the bisect table */
- n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
- k = le64toh(f->header->arena_max_size) / n;
-
- a = (le64toh(f->header->last_bisect_offset) + k - 1) / k;
- b = offset / k;
-
- for (; a <= b; a++)
- f->bisect_table[a] = htole64(offset);
-
- f->header->last_bisect_offset = htole64(offset + le64toh(o->object.size));
-
return 0;
}
-static int journal_file_append_entry_internal(
- JournalFile *f,
- const dual_timestamp *ts,
- uint64_t xor_hash,
- const EntryItem items[], unsigned n_items,
- Object **ret, uint64_t *offset) {
- uint64_t np;
- uint64_t osize;
- Object *o;
- int r;
-
- assert(f);
- assert(items || n_items == 0);
-
- osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
-
- r = journal_file_append_object(f, osize, &o, &np);
- if (r < 0)
- return r;
-
- o->object.type = htole64(OBJECT_ENTRY);
- o->entry.seqnum = htole64(journal_file_seqnum(f));
- memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
- o->entry.realtime = ts ? htole64(ts->realtime) : 0;
- o->entry.monotonic = ts ? htole64(ts->monotonic) : 0;
- o->entry.xor_hash = htole64(xor_hash);
-
- r = journal_file_link_entry(f, o, np);
- if (r < 0)
- return r;
-
- if (ret)
- *ret = o;
-
- if (offset)
- *offset = np;
-
- return 0;
-}
-
-int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset) {
- unsigned i;
- EntryItem *items;
+int sd_journal_iterate_fields(sd_journal *j, const void **data, size_t *size) {
+ JournalFile *f;
+ uint64_t p, l, n, h;
+ size_t t;
int r;
- uint64_t xor_hash = 0;
-
- assert(f);
- assert(iovec || n_iovec == 0);
-
- items = new(EntryItem, n_iovec);
- if (!items)
- return -ENOMEM;
-
- for (i = 0; i < n_iovec; i++) {
- uint64_t p;
- Object *o;
-
- r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
- if (r < 0)
- goto finish;
-
- xor_hash ^= le64toh(o->data.hash);
- items[i].object_offset = htole64(p);
- }
-
- r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, ret, offset);
-
-finish:
- free(items);
-
- return r;
-}
-
-int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset) {
Object *o;
- uint64_t lower, upper, p, n, k;
- int r;
- assert(f);
-
- n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
- k = le64toh(f->header->arena_max_size) / n;
-
- lower = 0;
- upper = le64toh(f->header->last_bisect_offset)/k+1;
-
- while (lower < upper) {
- k = (upper + lower) / 2;
- p = le64toh(f->bisect_table[k]);
-
- if (p == 0) {
- upper = k;
- continue;
- }
-
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
-
- if (o->object.type != htole64(OBJECT_ENTRY))
- return -EBADMSG;
-
- if (o->entry.seqnum == seqnum) {
- if (ret)
- *ret = o;
-
- if (offset)
- *offset = p;
-
- return 1;
- } else if (seqnum < o->entry.seqnum)
- upper = k;
- else if (seqnum > o->entry.seqnum)
- lower = k+1;
- }
-
- assert(lower == upper);
+ assert(j);
+ assert(data);
+ assert(size);
- if (lower <= 0)
+ f = j->current_file;
+ if (!f)
return 0;
- /* The object we are looking for is between
- * bisect_table[lower-1] and bisect_table[lower] */
-
- p = le64toh(f->bisect_table[lower-1]);
-
- for (;;) {
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
-
- if (o->entry.seqnum == seqnum) {
- if (ret)
- *ret = o;
-
- if (offset)
- *offset = p;
-
- return 1;
-
- } if (seqnum < o->entry.seqnum)
- return 0;
-
- if (o->entry.next_entry_offset == 0)
- return 0;
-
- p = le64toh(o->entry.next_entry_offset);
- }
-
- return 0;
-}
-
-int journal_file_next_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
- uint64_t np;
- int r;
-
- assert(f);
-
- if (!o)
- np = le64toh(f->header->head_entry_offset);
- else {
- if (le64toh(o->object.type) != OBJECT_ENTRY)
- return -EINVAL;
-
- np = le64toh(o->entry.next_entry_offset);
- }
-
- if (np == 0)
+ if (f->current_offset <= 0)
return 0;
- r = journal_file_move_to_object(f, np, &o);
+ r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
if (r < 0)
return r;
- if (le64toh(o->object.type) != OBJECT_ENTRY)
- return -EBADMSG;
-
- if (ret)
- *ret = o;
-
- if (offset)
- *offset = np;
-
- return 1;
-}
-
-int journal_file_prev_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
- uint64_t np;
- int r;
-
- assert(f);
-
- if (!o)
- np = le64toh(f->header->tail_entry_offset);
- else {
- if (le64toh(o->object.type) != OBJECT_ENTRY)
- return -EINVAL;
-
- np = le64toh(o->entry.prev_entry_offset);
- }
-
- if (np == 0)
+ n = journal_file_entry_n_items(o);
+ if (j->current_field >= n)
return 0;
- r = journal_file_move_to_object(f, np, &o);
+ p = le64toh(o->entry.items[j->current_field].object_offset);
+ h = o->entry.items[j->current_field].hash;
+ r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
if (r < 0)
return r;
- if (le64toh(o->object.type) != OBJECT_ENTRY)
+ if (h != o->data.hash)
return -EBADMSG;
- if (ret)
- *ret = o;
-
- if (offset)
- *offset = np;
-
- return 1;
-}
+ l = le64toh(o->object.size) - offsetof(Object, data.payload);
+ t = (size_t) l;
-int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
- uint64_t p, osize, hash, h;
- int r;
-
- assert(f);
- assert(data || size == 0);
-
- osize = offsetof(Object, data.payload) + size;
-
- hash = hash64(data, size);
- h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
- p = le64toh(f->hash_table[h].head_hash_offset);
-
- while (p != 0) {
- Object *o;
-
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
-
- if (le64toh(o->object.type) != OBJECT_DATA)
- return -EBADMSG;
-
- if (le64toh(o->object.size) == osize &&
- memcmp(o->data.payload, data, size) == 0) {
-
- if (le64toh(o->data.hash) != hash)
- return -EBADMSG;
-
- if (o->data.head_entry_offset == 0)
- return 0;
-
- p = le64toh(o->data.head_entry_offset);
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
-
- if (le64toh(o->object.type) != OBJECT_ENTRY)
- return -EBADMSG;
-
- if (ret)
- *ret = o;
-
- if (offset)
- *offset = p;
-
- return 1;
- }
-
- p = le64toh(o->data.next_hash_offset);
- }
-
- return 0;
-}
-
-int journal_file_find_last_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
- uint64_t p, osize, hash, h;
- int r;
-
- assert(f);
- assert(data || size == 0);
-
- osize = offsetof(Object, data.payload) + size;
-
- hash = hash64(data, size);
- h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
- p = le64toh(f->hash_table[h].tail_hash_offset);
-
- while (p != 0) {
- Object *o;
-
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
-
- if (le64toh(o->object.type) != OBJECT_DATA)
- return -EBADMSG;
-
- if (le64toh(o->object.size) == osize &&
- memcmp(o->data.payload, data, size) == 0) {
-
- if (le64toh(o->data.hash) != hash)
- return -EBADMSG;
-
- if (o->data.tail_entry_offset == 0)
- return 0;
-
- p = le64toh(o->data.tail_entry_offset);
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
-
- if (le64toh(o->object.type) != OBJECT_ENTRY)
- return -EBADMSG;
-
- if (ret)
- *ret = o;
-
- if (offset)
- *offset = p;
-
- return 1;
- }
-
- p = le64toh(o->data.prev_hash_offset);
- }
-
- return 0;
-}
-
-void journal_file_dump(JournalFile *f) {
- char a[33], b[33], c[33];
- Object *o;
- int r;
- uint64_t p;
-
- assert(f);
-
- printf("File ID: %s\n"
- "Machine ID: %s\n"
- "Boot ID: %s\n"
- "Arena size: %llu\n",
- sd_id128_to_string(f->header->file_id, a),
- sd_id128_to_string(f->header->machine_id, b),
- sd_id128_to_string(f->header->boot_id, c),
- (unsigned long long) le64toh(f->header->arena_size));
-
- p = le64toh(f->header->head_object_offset);
- while (p != 0) {
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- goto fail;
-
- switch (o->object.type) {
-
- case OBJECT_UNUSED:
- printf("Type: OBJECT_UNUSED\n");
- break;
-
- case OBJECT_DATA:
- printf("Type: OBJECT_DATA\n");
- break;
+ /* We can't read objects larger than 4G on a 32bit machine */
+ if ((uint64_t) t != l)
+ return -E2BIG;
- case OBJECT_ENTRY:
- printf("Type: OBJECT_ENTRY %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
- break;
+ *data = o->data.payload;
+ *size = t;
- case OBJECT_HASH_TABLE:
- printf("Type: OBJECT_HASH_TABLE\n");
- break;
+ j->current_field ++;
- case OBJECT_BISECT_TABLE:
- printf("Type: OBJECT_BISECT_TABLE\n");
- break;
- }
-
- if (p == le64toh(f->header->tail_object_offset))
- p = 0;
- else
- p = p + ALIGN64(le64toh(o->object.size));
- }
-
- return;
-fail:
- log_error("File corrupt");
+ return 1;
}
-int journal_file_open(
- sd_journal *j,
- const char *fname,
- int flags,
- mode_t mode,
- JournalFile **ret) {
-
- JournalFile *f;
- int r;
- bool newly_created = false;
-
- assert(fname);
-
- if ((flags & O_ACCMODE) != O_RDONLY &&
- (flags & O_ACCMODE) != O_RDWR)
- return -EINVAL;
-
- f = new0(JournalFile, 1);
- if (!f)
- return -ENOMEM;
-
- f->writable = (flags & O_ACCMODE) != O_RDONLY;
- f->prot = prot_from_flags(flags);
-
- f->fd = open(fname, flags|O_CLOEXEC, mode);
- if (f->fd < 0) {
- r = -errno;
- goto fail;
- }
-
- f->path = strdup(fname);
- if (!f->path) {
- r = -ENOMEM;
- goto fail;
- }
-
- if (fstat(f->fd, &f->last_stat) < 0) {
- r = -errno;
- goto fail;
- }
-
- if (f->last_stat.st_size == 0 && f->writable) {
- newly_created = true;
-
- r = journal_file_init_header(f);
- if (r < 0)
- goto fail;
-
- if (fstat(f->fd, &f->last_stat) < 0) {
- r = -errno;
- goto fail;
- }
- }
-
- if (f->last_stat.st_size < (off_t) sizeof(Header)) {
- r = -EIO;
- goto fail;
- }
-
- f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
- if (f->header == MAP_FAILED) {
- f->header = NULL;
- r = -errno;
- goto fail;
- }
-
- if (!newly_created) {
- r = journal_file_verify_header(f);
- if (r < 0)
- goto fail;
- }
-
- if (f->writable) {
- r = journal_file_refresh_header(f);
- if (r < 0)
- goto fail;
- }
-
- if (newly_created) {
-
- r = journal_file_setup_hash_table(f);
- if (r < 0)
- goto fail;
-
- r = journal_file_setup_bisect_table(f);
- if (r < 0)
- goto fail;
- }
-
- r = journal_file_map_hash_table(f);
- if (r < 0)
- goto fail;
-
- r = journal_file_map_bisect_table(f);
- if (r < 0)
- goto fail;
-
- if (j) {
- LIST_PREPEND(JournalFile, files, j->files, f);
- f->journal = j;
- }
-
- if (ret)
- *ret = f;
-
- return 0;
-
-fail:
- journal_file_close(f);
-
- return r;
+int sd_journal_seek_head(sd_journal *j) {
+ assert(j);
+ return -EINVAL;
}
-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;
-
- 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;
-
- 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;
- }
-
- k = journal_file_open(j, fn, O_RDONLY, 0, NULL);
- if (k < 0 && r == 0)
- r = -k;
-
- free(fn);
- }
- }
-
- if (!j->files) {
- if (r >= 0)
- r = -ENOENT;
-
- goto fail;
- }
-
- *ret = j;
- return 0;
-
-fail:
- sd_journal_close(j);
-
- return r;
-};
-
-void sd_journal_close(sd_journal *j) {
+int sd_journal_seek_tail(sd_journal *j) {
assert(j);
-
- while (j->files)
- journal_file_close(j->files);
-
- free(j);
+ return -EINVAL;
}