chiark / gitweb /
journal: properly implement matching with multiple matches
[elogind.git] / src / journal / sd-journal.c
index 35f911f2ba8e8000b50b29473a6f2a9204571a3c..bc6d255662adf21a98f63796ab6182673cc2b649 100644 (file)
 #include "journal-file.h"
 #include "hashmap.h"
 #include "list.h"
+#include "lookup3.h"
 
 typedef struct Match Match;
 
 struct Match {
         char *data;
         size_t size;
-        uint64_t hash;
+        uint64_t le_hash;
 
         LIST_FIELDS(Match, matches);
 };
@@ -43,34 +44,40 @@ struct sd_journal {
         Hashmap *files;
 
         JournalFile *current_file;
+        uint64_t current_field;
 
         LIST_HEAD(Match, matches);
+        unsigned n_matches;
 };
 
-int sd_journal_add_match(sd_journal *j, const char *field, const void *data, size_t size) {
+int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
         Match *m;
-        char *e;
 
         assert(j);
-        assert(field);
-        assert(data || size == 0);
+
+        if (size <= 0)
+                return -EINVAL;
+
+        assert(data);
 
         m = new0(Match, 1);
         if (!m)
                 return -ENOMEM;
 
-        m->size = strlen(field) + 1 + size;
+        m->size = size;
+
         m->data = malloc(m->size);
         if (!m->data) {
                 free(m);
                 return -ENOMEM;
         }
 
-        e = stpcpy(m->data, field);
-        *(e++) = '=';
-        memcpy(e, data, size);
+        memcpy(m->data, data, size);
+        m->le_hash = hash64(m->data, size);
 
         LIST_PREPEND(Match, matches, j->matches, m);
+        j->n_matches ++;
+
         return 0;
 }
 
@@ -84,6 +91,8 @@ void sd_journal_flush_matches(sd_journal *j) {
                 free(m->data);
                 free(m);
         }
+
+        j->n_matches = 0;
 }
 
 static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
@@ -92,7 +101,16 @@ static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
         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 */
+         * 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 */
+
+        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;
 
         if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
 
@@ -105,6 +123,10 @@ static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
                         return -1;
                 if (a > b)
                         return 1;
+
+                /* 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. */
         }
 
         if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
@@ -140,6 +162,103 @@ static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
         return 0;
 }
 
+static int move_to_next_with_matches(sd_journal *j, JournalFile *f, Object **o, uint64_t *p) {
+        int r;
+        uint64_t cp;
+        Object *c;
+
+        assert(j);
+        assert(f);
+        assert(o);
+        assert(p);
+
+        if (!j->matches) {
+                /* No matches is easy, just go on to the next entry */
+
+                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;
+
+                return journal_file_next_entry(f, c, o, p);
+        }
+
+        /* So there are matches we have to adhere to, let's find the
+         * first entry that matches all of them */
+
+        if (f->current_offset > 0)
+                cp = f->current_offset;
+        else {
+                r = journal_file_find_first_entry(f, j->matches->data, j->matches->size, &c, &cp);
+                if (r <= 0)
+                        return r;
+
+                /* We can shortcut this if there's only one match */
+                if (j->n_matches == 1) {
+                        *o = c;
+                        *p = cp;
+                        return r;
+                }
+        }
+
+        for (;;) {
+                uint64_t np, n;
+                bool found;
+                Match *m;
+
+                r = journal_file_move_to_object(f, cp, OBJECT_ENTRY, &c);
+                if (r < 0)
+                        return r;
+
+                n = journal_file_entry_n_items(c);
+
+                /* Make sure we don't match the entry we are starting
+                 * from. */
+                found = f->current_offset != cp;
+
+                np = 0;
+                LIST_FOREACH(matches, m, j->matches) {
+                        uint64_t q, k;
+
+                        for (k = 0; k < n; k++)
+                                if (c->entry.items[k].hash == m->le_hash)
+                                        break;
+
+                        if (k >= n) {
+                                /* Hmm, didn't find any field that matched, so ignore
+                                 * this match. Go on with next match */
+
+                                found = false;
+                                continue;
+                        }
+
+                        /* Hmm, so, this field matched, let's remember
+                         * where we'd have to try next, in case the other
+                         * matches are not OK */
+                        q = le64toh(c->entry.items[k].next_entry_offset);
+                        if (q > np)
+                                np = q;
+                }
+
+                /* Did this entry match against all matches? */
+                if (found) {
+                        *o = c;
+                        *p = cp;
+                        return 1;
+                }
+
+                /* Did we find a subsequent entry? */
+                if (np == 0)
+                        return 0;
+
+                /* Hmm, ok, this entry only matched partially, so
+                 * let's try another one */
+                cp = np;
+        }
+}
+
 int sd_journal_next(sd_journal *j) {
         JournalFile *f, *new_current = NULL;
         Iterator i;
@@ -153,14 +272,7 @@ int sd_journal_next(sd_journal *j) {
                 Object *o;
                 uint64_t p;
 
-                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);
+                r = move_to_next_with_matches(j, f, &o, &p);
                 if (r < 0)
                         return r;
                 else if (r == 0)
@@ -177,7 +289,7 @@ int sd_journal_next(sd_journal *j) {
         if (new_current) {
                 j->current_file = new_current;
                 j->current_file->current_offset = new_offset;
-                j->current_file->current_field = 0;
+                j->current_field = 0;
 
                 /* Skip over any identical entries in the other files too */
 
@@ -188,23 +300,14 @@ int sd_journal_next(sd_journal *j) {
                         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);
+                        r = move_to_next_with_matches(j, f, &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) {
+                        if (compare_order(new_current, new_entry, new_offset, f, o, p) == 0)
                                 f->current_offset = p;
-                                f->current_field = 0;
-                        }
                 }
 
                 return 1;
@@ -249,7 +352,34 @@ int sd_journal_previous(sd_journal *j) {
         if (new_current) {
                 j->current_file = new_current;
                 j->current_file->current_offset = new_offset;
-                j->current_file->current_field = 0;
+                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;
         }
 
@@ -448,6 +578,8 @@ void sd_journal_close(sd_journal *j) {
                 hashmap_free(j->files);
         }
 
+        sd_journal_flush_matches(j);
+
         free(j);
 }
 
@@ -490,7 +622,7 @@ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret) {
         if (f->current_offset <= 0)
                 return 0;
 
-        r = sd_id128_get_machine(&id);
+        r = sd_id128_get_boot(&id);
         if (r < 0)
                 return r;
 
@@ -536,14 +668,18 @@ int sd_journal_get_field(sd_journal *j, const char *field, const void **data, si
 
         n = journal_file_entry_n_items(o);
         for (i = 0; i < n; i++) {
-                uint64_t p, l;
+                uint64_t p, l, h;
                 size_t t;
 
                 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;
 
+                if (h != o->data.hash)
+                        return -EBADMSG;
+
                 l = le64toh(o->object.size) - offsetof(Object, data.payload);
 
                 if (l >= field_length+1 &&
@@ -571,7 +707,7 @@ int sd_journal_get_field(sd_journal *j, const char *field, const void **data, si
 
 int sd_journal_iterate_fields(sd_journal *j, const void **data, size_t *size) {
         JournalFile *f;
-        uint64_t p, l, n;
+        uint64_t p, l, n, h;
         size_t t;
         int r;
         Object *o;
@@ -592,14 +728,18 @@ int sd_journal_iterate_fields(sd_journal *j, const void **data, size_t *size) {
                 return r;
 
         n = journal_file_entry_n_items(o);
-        if (f->current_field >= n)
+        if (j->current_field >= n)
                 return 0;
 
-        p = le64toh(o->entry.items[f->current_field].object_offset);
+        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 (h != o->data.hash)
+                return -EBADMSG;
+
         l = le64toh(o->object.size) - offsetof(Object, data.payload);
         t = (size_t) l;
 
@@ -610,7 +750,7 @@ int sd_journal_iterate_fields(sd_journal *j, const void **data, size_t *size) {
         *data = o->data.payload;
         *size = t;
 
-        f->current_field ++;
+        j->current_field ++;
 
         return 1;
 }