X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fjournal%2Fsd-journal.c;h=74abac88af74ad262d4649518fea1e8109ca570d;hp=8426b3bf9e232fe098f671ad3292bb8d10c13e3e;hb=7210bfb3706f8cbb2b35403f916dace1824c516c;hpb=cec736d21ff86c4ac81b4d306ddba2120333818c diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c index 8426b3bf9..74abac88a 100644 --- a/src/journal/sd-journal.c +++ b/src/journal/sd-journal.c @@ -21,6 +21,7 @@ #include #include +#include #include "sd-journal.h" #include "journal-def.h" @@ -42,6 +43,7 @@ struct sd_journal { Hashmap *files; JournalFile *current_file; + uint64_t current_field; LIST_HEAD(Match, matches); }; @@ -86,10 +88,13 @@ void sd_journal_flush_matches(sd_journal *j) { } static int compare_order(JournalFile *af, Object *ao, uint64_t ap, - JournalFile *bf, Object *bo, uint64_t bp) { + JournalFile *bf, Object *bo, uint64_t bp) { uint64_t a, b; + /* We operate on two different files here, hence we can access + * two objects at the same time, which we normally can't */ + if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) { /* If this is from the same seqnum source, compare @@ -97,23 +102,43 @@ static int compare_order(JournalFile *af, Object *ao, uint64_t ap, a = le64toh(ao->entry.seqnum); b = le64toh(bo->entry.seqnum); + if (a < b) + return -1; + if (a > b) + return 1; + } - } else if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) { + if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) { /* If the boot id matches compare monotonic time */ a = le64toh(ao->entry.monotonic); b = le64toh(bo->entry.monotonic); - } else { - - /* Otherwise compare UTC time */ - a = le64toh(ao->entry.realtime); - b = le64toh(ao->entry.realtime); + if (a < b) + return -1; + if (a > b) + return 1; } - return - a < b ? -1 : - a > b ? +1 : 0; + /* Otherwise compare UTC time */ + a = le64toh(ao->entry.realtime); + b = le64toh(ao->entry.realtime); + + 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; + + return 0; } int sd_journal_next(sd_journal *j) { @@ -142,7 +167,8 @@ int sd_journal_next(sd_journal *j) { else if (r == 0) continue; - if (!new_current || compare_order(new_current, new_entry, new_offset, f, o, p) > 0) { + if (!new_current || + compare_order(new_current, new_entry, new_offset, f, o, p) > 0) { new_current = f; new_entry = o; new_offset = p; @@ -151,7 +177,35 @@ int sd_journal_next(sd_journal *j) { if (new_current) { j->current_file = new_current; - f->current_offset = new_offset; + j->current_file->current_offset = new_offset; + j->current_field = 0; + + /* Skip over any identical entries in the other files too */ + + HASHMAP_FOREACH(f, j->files, i) { + Object *o; + uint64_t p; + + if (j->current_file == f) + continue; + + 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; + + r = journal_file_next_entry(f, o, &o, &p); + if (r < 0) + return r; + else if (r == 0) + continue; + + if (compare_order(new_current, new_entry, new_offset, f, o, p) == 0) + f->current_offset = p; + } + return 1; } @@ -193,59 +247,161 @@ int sd_journal_previous(sd_journal *j) { if (new_current) { j->current_file = new_current; - f->current_offset = new_offset; + j->current_file->current_offset = new_offset; + j->current_field = 0; + + /* Skip over any identical entries in the other files too */ + + HASHMAP_FOREACH(f, j->files, i) { + Object *o; + uint64_t p; + + if (j->current_file == f) + continue; + + 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; + + r = journal_file_prev_entry(f, o, &o, &p); + if (r < 0) + return r; + else if (r == 0) + continue; + + if (compare_order(new_current, new_entry, new_offset, f, o, p) == 0) + f->current_offset = p; + } + return 1; } return 0; } -int sd_journal_get_cursor(sd_journal *j, void **cursor, size_t *size) { - JournalCursor *c; +int sd_journal_get_cursor(sd_journal *j, char **cursor) { Object *o; int r; + char bid[33], sid[33]; assert(j); assert(cursor); - assert(size); - if (!j->current_file || !j->current_file->current_offset <= 0) - return 0; + if (!j->current_file || j->current_file->current_offset <= 0) + return -EADDRNOTAVAIL; r = journal_file_move_to_object(j->current_file, j->current_file->current_offset, OBJECT_ENTRY, &o); if (r < 0) return r; - c = new0(JournalCursor, 1); - if (!c) - return -ENOMEM; - - 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; + sd_id128_to_string(j->current_file->header->seqnum_id, sid); + sd_id128_to_string(o->entry.boot_id, bid); - *cursor = c; - *size = sizeof(JournalCursor); + 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; return 1; } -int sd_journal_set_cursor(sd_journal *j, const void *cursor, size_t size) { +int sd_journal_set_cursor(sd_journal *j, const char *cursor) { return -EINVAL; } +static int add_file(sd_journal *j, const char *prefix, const char *dir, const char *filename) { + char *fn; + int r; + JournalFile *f; + + assert(j); + assert(prefix); + assert(filename); + + if (dir) + fn = join(prefix, "/", dir, "/", filename, NULL); + else + fn = join(prefix, "/", filename, NULL); + + if (!fn) + return -ENOMEM; + + r = journal_file_open(fn, O_RDONLY, 0, NULL, &f); + free(fn); + + if (r < 0) { + if (errno == ENOENT) + return 0; + + return r; + } + + r = hashmap_put(j->files, f->path, f); + if (r < 0) { + journal_file_close(f); + return r; + } + + return 0; +} + +static int add_directory(sd_journal *j, const char *prefix, const char *dir) { + char *fn; + int r; + DIR *d; + + assert(j); + assert(prefix); + assert(dir); + + fn = join(prefix, "/", dir, NULL); + if (!fn) + return -ENOMEM; + + d = opendir(fn); + free(fn); + + if (!d) { + if (errno == ENOENT) + return 0; + + return -errno; + } + + for (;;) { + struct dirent buf, *de; + + r = readdir_r(d, &buf, &de); + if (r != 0 || !de) + break; + + if (!dirent_is_file_with_suffix(de, ".journal")) + continue; + + 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)); + } + + closedir(d); + + return 0; +} + 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"; + int r; assert(ret); @@ -254,64 +410,47 @@ int sd_journal_open(sd_journal **ret) { return -ENOMEM; j->files = hashmap_new(string_hash_func, string_compare_func); - if (!j->files) + if (!j->files) { + r = -ENOMEM; goto fail; + } + + /* We ignore most errors here, since the idea is to only open + * what's actually accessible, and ignore the rest. */ NULSTR_FOREACH(p, search_paths) { DIR *d; d = opendir(p); if (!d) { - if (errno != ENOENT && r == 0) - r = -errno; - + if (errno != ENOENT) + log_debug("Failed to open %s: %m", p); continue; } for (;;) { struct dirent buf, *de; - int k; - JournalFile *f; - - k = readdir_r(d, &buf, &de); - if (k != 0) { - if (r == 0) - r = -k; + sd_id128_t id; + r = readdir_r(d, &buf, &de); + if (r != 0 || !de) break; - } - if (!de) - break; + 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)); - 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(fn, O_RDONLY, 0, &f); - free(fn); - - if (k < 0) { - - if (r == 0) - r = -k; - } else { - k = hashmap_put(j->files, f->path, f); - if (k < 0) { - journal_file_close(f); - closedir(d); + } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) && + sd_id128_from_string(de->d_name, &id) >= 0) { - r = k; - goto fail; - } + 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)); } } + + closedir(d); } *ret = j; @@ -337,3 +476,177 @@ void sd_journal_close(sd_journal *j) { free(j); } + +int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) { + Object *o; + JournalFile *f; + int r; + + assert(j); + assert(ret); + + f = j->current_file; + if (!f) + return 0; + + if (f->current_offset <= 0) + return 0; + + r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o); + if (r < 0) + return r; + + *ret = le64toh(o->entry.realtime); + return 1; +} + +int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret) { + Object *o; + JournalFile *f; + int r; + sd_id128_t id; + + assert(j); + assert(ret); + + f = j->current_file; + if (!f) + return 0; + + if (f->current_offset <= 0) + return 0; + + r = sd_id128_get_machine(&id); + if (r < 0) + return r; + + r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o); + if (r < 0) + return r; + + if (!sd_id128_equal(id, o->entry.boot_id)) + return 0; + + *ret = le64toh(o->entry.monotonic); + return 1; + +} + +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(j); + assert(field); + assert(data); + assert(size); + + if (isempty(field) || strchr(field, '=')) + return -EINVAL; + + f = j->current_file; + if (!f) + return 0; + + if (f->current_offset <= 0) + return 0; + + r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o); + if (r < 0) + return r; + + field_length = strlen(field); + + n = journal_file_entry_n_items(o); + for (i = 0; i < n; i++) { + uint64_t p, l; + size_t t; + + p = le64toh(o->entry.items[i].object_offset); + r = journal_file_move_to_object(f, p, OBJECT_DATA, &o); + if (r < 0) + return r; + + l = le64toh(o->object.size) - offsetof(Object, data.payload); + + if (l >= field_length+1 && + memcmp(o->data.payload, field, field_length) == 0 && + o->data.payload[field_length] == '=') { + + t = (size_t) l; + + if ((uint64_t) t != l) + return -E2BIG; + + *data = o->data.payload; + *size = t; + + return 1; + } + + r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o); + if (r < 0) + return r; + } + + return 0; +} + +int sd_journal_iterate_fields(sd_journal *j, const void **data, size_t *size) { + JournalFile *f; + uint64_t p, l, n; + size_t t; + int r; + Object *o; + + assert(j); + assert(data); + assert(size); + + f = j->current_file; + if (!f) + return 0; + + if (f->current_offset <= 0) + return 0; + + r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o); + if (r < 0) + return r; + + n = journal_file_entry_n_items(o); + if (j->current_field >= n) + return 0; + + p = le64toh(o->entry.items[j->current_field].object_offset); + r = journal_file_move_to_object(f, p, OBJECT_DATA, &o); + if (r < 0) + return r; + + l = le64toh(o->object.size) - offsetof(Object, data.payload); + t = (size_t) l; + + /* We can't read objects larger than 4G on a 32bit machine */ + if ((uint64_t) t != l) + return -E2BIG; + + *data = o->data.payload; + *size = t; + + j->current_field ++; + + return 1; +} + +int sd_journal_seek_head(sd_journal *j) { + assert(j); + return -EINVAL; +} + +int sd_journal_seek_tail(sd_journal *j) { + assert(j); + return -EINVAL; +}