summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2011-10-14 04:44:50 +0200
committerLennart Poettering <lennart@poettering.net>2011-10-14 04:44:50 +0200
commit3fbf9cbb02690e40cd65802e777519f3f3c8d88a (patch)
treeac17b4bfe762003e2681246356848938c000e787 /src
parent0ac38b707212e9aa40e25d65ffbae648cc9116f5 (diff)
journal: implement parallel file traversal
Diffstat (limited to 'src')
-rw-r--r--src/journal/journal-file.c9
-rw-r--r--src/journal/journal-file.h12
-rw-r--r--src/journal/journalctl.c103
-rw-r--r--src/journal/journald.c17
-rw-r--r--src/journal/sd-journal.c346
-rw-r--r--src/journal/sd-journal.h20
6 files changed, 340 insertions, 167 deletions
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
index 934c043aff..6c8d712dbd 100644
--- a/src/journal/journal-file.c
+++ b/src/journal/journal-file.c
@@ -751,8 +751,8 @@ static int journal_file_append_entry_internal(
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.realtime = htole64(ts ? ts->realtime : now(CLOCK_REALTIME));
+ o->entry.monotonic = htole64(ts ? ts->monotonic : now(CLOCK_MONOTONIC));
o->entry.xor_hash = htole64(xor_hash);
o->entry.boot_id = f->header->boot_id;
@@ -1072,7 +1072,10 @@ void journal_file_dump(JournalFile *f) {
break;
case OBJECT_ENTRY:
- printf("Type: OBJECT_ENTRY %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
+ printf("Type: OBJECT_ENTRY %llu %llu %llu\n",
+ (unsigned long long) le64toh(o->entry.seqnum),
+ (unsigned long long) le64toh(o->entry.monotonic),
+ (unsigned long long) le64toh(o->entry.realtime));
break;
case OBJECT_HASH_TABLE:
diff --git a/src/journal/journal-file.h b/src/journal/journal-file.h
index 4665f4da38..c51504d11b 100644
--- a/src/journal/journal-file.h
+++ b/src/journal/journal-file.h
@@ -52,19 +52,9 @@ typedef struct JournalFile {
uint64_t window_size;
uint64_t current_offset;
+ uint64_t current_field;
} JournalFile;
-typedef struct JournalCursor {
- uint8_t version;
- uint8_t reserved[7];
- uint64_t seqnum;
- sd_id128_t seqnum_id;
- sd_id128_t boot_id;
- uint64_t monotonic;
- uint64_t realtime;
- uint64_t xor_hash;
-} JournalCursor;
-
int journal_file_open(const char *fname, int flags, mode_t mode, JournalFile *template, JournalFile **ret);
void journal_file_close(JournalFile *j);
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 4645f9ebb0..bb1f18a21f 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -22,104 +22,51 @@
#include <fcntl.h>
#include <errno.h>
#include <stddef.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
-#include "journal-file.h"
-
-static int system_journal_open(JournalFile **f) {
- int r;
- char *fn;
- sd_id128_t machine;
- char ids[33];
-
- assert(f);
-
- r = sd_id128_get_machine(&machine);
- if (r < 0)
- return r;
-
- fn = join("/var/log/journal/", sd_id128_to_string(machine, ids), "/system.journal", NULL);
- if (!fn)
- return -ENOMEM;
-
- r = journal_file_open(fn, O_RDONLY, 0640, NULL, f);
- free(fn);
-
- if (r >= 0)
- return r;
-
- if (r < 0 && r != -ENOENT) {
- log_error("Failed to open system journal: %s", strerror(-r));
- return r;
- }
-
- fn = join("/run/log/journal/", ids, "/system.journal", NULL);
- if (!fn)
- return -ENOMEM;
-
- r = journal_file_open(fn, O_RDONLY, 0640, NULL, f);
- free(fn);
-
- if (r < 0) {
- log_error("Failed to open system journal: %s", strerror(-r));
- return r;
- }
-
- return r;
-}
+#include "sd-journal.h"
+#include "log.h"
int main(int argc, char *argv[]) {
int r;
- JournalFile *f;
- Object *o = NULL;
+ sd_journal *j = NULL;
+
+ log_set_max_level(LOG_DEBUG);
+ log_set_target(LOG_TARGET_CONSOLE);
log_parse_environment();
log_open();
- r = system_journal_open(&f);
+ r = sd_journal_open(&j);
if (r < 0) {
log_error("Failed to open journal: %s", strerror(-r));
- return EXIT_FAILURE;
+ goto finish;
}
- for (;;) {
- uint64_t offset;
- uint64_t n, i;
+ SD_JOURNAL_FOREACH(j) {
+ const void *data;
+ size_t length;
+ char *cursor;
- r = journal_file_next_entry(f, o, &o, &offset);
+ r = sd_journal_get_cursor(j, &cursor);
if (r < 0) {
- log_error("Failed to read journal: %s", strerror(-r));
+ log_error("Failed to get cursor: %s", strerror(-r));
goto finish;
}
- if (r == 0)
- break;
+ printf("entry: %s\n", cursor);
+ free(cursor);
- printf("entry: %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
-
- n = journal_file_entry_n_items(o);
- for (i = 0; i < n; i++) {
- uint64_t p, l;
-
- p = le64toh(o->entry.items[i].object_offset);
- r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
- if (r < 0) {
- log_error("Failed to move to data: %s", strerror(-r));
- goto finish;
- }
-
- l = o->object.size - offsetof(Object, data.payload);
- printf("\t[%.*s]\n", (int) l, o->data.payload);
-
- r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
- if (r < 0) {
- log_error("Failed to move back to entry: %s", strerror(-r));
- goto finish;
- }
- }
+ SD_JOURNAL_FOREACH_FIELD(j, data, length)
+ printf("\t%.*s\n", (int) length, (const char*) data);
}
finish:
- journal_file_close(f);
+ if (j)
+ sd_journal_close(j);
- return 0;
+ return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
diff --git a/src/journal/journald.c b/src/journal/journald.c
index 7a2b50b017..1143d81abc 100644
--- a/src/journal/journald.c
+++ b/src/journal/journald.c
@@ -95,6 +95,8 @@ static JournalFile* find_journal(Server *s, uid_t uid) {
char *p;
int r;
JournalFile *f;
+ char ids[33];
+ sd_id128_t machine;
assert(s);
@@ -105,11 +107,15 @@ static JournalFile* find_journal(Server *s, uid_t uid) {
if (uid <= 0)
return s->system_journal;
+ r = sd_id128_get_machine(&machine);
+ if (r < 0)
+ return s->system_journal;
+
f = hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
if (f)
return f;
- if (asprintf(&p, "/var/log/journal/user-%lu.journal", (unsigned long) uid) < 0)
+ if (asprintf(&p, "/var/log/journal/%s/user-%lu.journal", sd_id128_to_string(machine, ids), (unsigned long) uid) < 0)
return s->system_journal;
r = journal_file_open(p, O_RDWR|O_CREAT, 0640, NULL, &f);
@@ -401,16 +407,11 @@ static int system_journal_open(Server *s) {
/* /var didn't work, so try /run, but this time we
* create the prefix too */
- fn = strappend("/run/log/journal/", ids);
- if (!fn)
- return -ENOMEM;
- (void) mkdir_p(fn, 0755);
- free(fn);
-
- /* Then create the runtime journal file */
fn = join("/run/log/journal/", ids, "/system.journal", NULL);
if (!fn)
return -ENOMEM;
+
+ (void) mkdir_parents(fn, 0755);
r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
free(fn);
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index 9f5f1e858b..6f47520217 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -21,6 +21,7 @@
#include <errno.h>
#include <fcntl.h>
+#include <stddef.h>
#include "sd-journal.h"
#include "journal-def.h"
@@ -151,7 +152,8 @@ 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_file->current_field = 0;
return 1;
}
@@ -193,59 +195,134 @@ 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_file->current_field = 0;
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;
+ sd_id128_to_string(j->current_file->header->seqnum_id, sid);
+ sd_id128_to_string(o->entry.boot_id, bid);
- 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;
-
- *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 +331,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;
-
- break;
- }
+ sd_id128_t id;
- if (!de)
+ r = readdir_r(d, &buf, &de);
+ if (r != 0 || !de)
break;
- if (!dirent_is_file_with_suffix(de, ".journal"))
- continue;
+ 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));
- fn = join(p, "/", de->d_name, NULL);
- if (!fn) {
- r = -ENOMEM;
- closedir(d);
- goto fail;
- }
-
- k = journal_file_open(fn, O_RDONLY, 0, NULL, &f);
- free(fn);
-
- if (k < 0) {
+ } else if ((de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) &&
+ sd_id128_from_string(de->d_name, &id) >= 0) {
- 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;
- }
+ 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 +397,165 @@ 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 (field_length+1 > l)
+ continue;
+
+ if (memcmp(o->data.payload, field, field_length) ||
+ o->data.payload[field_length] != '=')
+ continue;
+
+ t = (size_t) l;
+
+ if ((uint64_t) t != l)
+ return -E2BIG;
+
+ *data = o->data.payload;
+ *size = t;
+
+ return 1;
+ }
+
+ 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 (f->current_field >= n)
+ return 0;
+
+ p = le64toh(o->entry.items[f->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;
+
+ f->current_field ++;
+
+ return 1;
+}
diff --git a/src/journal/sd-journal.h b/src/journal/sd-journal.h
index 55e58601fe..bbfcda6f2a 100644
--- a/src/journal/sd-journal.h
+++ b/src/journal/sd-journal.h
@@ -27,13 +27,14 @@
/* TODO:
*
- * - implement rotation
* - check LE/BE conversion for 8bit, 16bit, 32bit values
* - implement parallel traversal
* - implement inotify usage on client
* - implement audit gateway
* - implement native gateway
+ * - implement stdout gateway
* - extend hash table/bisect table as we go
+ * - accelerate looking for "all hostnames" and suchlike.
*/
typedef struct sd_journal sd_journal;
@@ -44,10 +45,10 @@ void sd_journal_close(sd_journal *j);
int sd_journal_previous(sd_journal *j);
int sd_journal_next(sd_journal *j);
-int sd_journal_get(sd_journal *j, const char *field, const void **data, size_t *size);
-int sd_journal_get_seqnum(sd_journal *j, uint64_t *ret);
int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret);
int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret);
+int sd_journal_get_field(sd_journal *j, const char *field, const void **data, size_t *l);
+int sd_journal_iterate_fields(sd_journal *j, const void **data, size_t *l);
int sd_journal_add_match(sd_journal *j, const char *field, const void *data, size_t size);
void sd_journal_flush_matches(sd_journal *j);
@@ -59,9 +60,18 @@ int sd_journal_seek_seqnum(sd_journal *j, uint64_t seqnum);
int sd_journal_seek_monotonic_usec(sd_journal *j, uint64_t usec);
int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec);
-int sd_journal_get_cursor(sd_journal *j, void **cursor, size_t *size);
-int sd_journal_set_cursor(sd_journal *j, const void *cursor, size_t size);
+int sd_journal_get_cursor(sd_journal *j, char **cursor);
+int sd_journal_set_cursor(sd_journal *j, const char *cursor);
int sd_journal_get_fd(sd_journal *j);
+#define SD_JOURNAL_FOREACH(j) \
+ while (sd_journal_next(j) > 0)
+
+#define SD_JOURNAL_FOREACH_BACKWARDS(j) \
+ while (sd_journal_previous(j) > 0)
+
+#define SD_JOURNAL_FOREACH_FIELD(j, data, l) \
+ while (sd_journal_iterate_fields((j), &(data), &(l)) > 0)
+
#endif