chiark / gitweb /
systemctl: limit logs in status to current boot
[elogind.git] / src / journal / sd-journal.c
index 7e06a70344ac55eb5e70bfd4e0e311badb605f8f..cf60ebcee2756e4fe096c31c565d3cdb336fbc8c 100644 (file)
@@ -109,6 +109,9 @@ static void set_location(sd_journal *j, LocationType type, JournalFile *f, Objec
 
         init_location(&j->current_location, type, f, o);
 
+        if (j->current_file)
+                j->current_file->current_offset = 0;
+
         j->current_file = f;
         j->current_field = 0;
 
@@ -203,7 +206,7 @@ static void match_free_if_empty(Match *m) {
 }
 
 _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
-        Match *l2, *l3, *add_here = NULL, *m;
+        Match *l3, *l4, *add_here = NULL, *m;
         le64_t le_hash;
 
         if (!j)
@@ -218,44 +221,52 @@ _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size)
         if (!match_is_valid(data, size))
                 return -EINVAL;
 
-        /* level 0: OR term
-         * level 1: AND terms
-         * level 2: OR terms
-         * level 3: concrete matches */
+        /* level 0: AND term
+         * level 1: OR terms
+         * level 2: AND terms
+         * level 3: OR terms
+         * level 4: concrete matches */
 
         if (!j->level0) {
-                j->level0 = match_new(NULL, MATCH_OR_TERM);
+                j->level0 = match_new(NULL, MATCH_AND_TERM);
                 if (!j->level0)
                         return -ENOMEM;
         }
 
         if (!j->level1) {
-                j->level1 = match_new(j->level0, MATCH_AND_TERM);
+                j->level1 = match_new(j->level0, MATCH_OR_TERM);
                 if (!j->level1)
                         return -ENOMEM;
         }
 
-        assert(j->level0->type == MATCH_OR_TERM);
-        assert(j->level1->type == MATCH_AND_TERM);
+        if (!j->level2) {
+                j->level2 = match_new(j->level1, MATCH_AND_TERM);
+                if (!j->level2)
+                        return -ENOMEM;
+        }
+
+        assert(j->level0->type == MATCH_AND_TERM);
+        assert(j->level1->type == MATCH_OR_TERM);
+        assert(j->level2->type == MATCH_AND_TERM);
 
         le_hash = htole64(hash64(data, size));
 
-        LIST_FOREACH(matches, l2, j->level1->matches) {
-                assert(l2->type == MATCH_OR_TERM);
+        LIST_FOREACH(matches, l3, j->level2->matches) {
+                assert(l3->type == MATCH_OR_TERM);
 
-                LIST_FOREACH(matches, l3, l2->matches) {
-                        assert(l3->type == MATCH_DISCRETE);
+                LIST_FOREACH(matches, l4, l3->matches) {
+                        assert(l4->type == MATCH_DISCRETE);
 
                         /* Exactly the same match already? Then ignore
                          * this addition */
-                        if (l3->le_hash == le_hash &&
-                            l3->size == size &&
-                            memcmp(l3->data, data, size) == 0)
+                        if (l4->le_hash == le_hash &&
+                            l4->size == size &&
+                            memcmp(l4->data, data, size) == 0)
                                 return 0;
 
                         /* Same field? Then let's add this to this OR term */
-                        if (same_field(data, size, l3->data, l3->size)) {
-                                add_here = l2;
+                        if (same_field(data, size, l4->data, l4->size)) {
+                                add_here = l3;
                                 break;
                         }
                 }
@@ -265,7 +276,7 @@ _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size)
         }
 
         if (!add_here) {
-                add_here = match_new(j->level1, MATCH_OR_TERM);
+                add_here = match_new(j->level2, MATCH_OR_TERM);
                 if (!add_here)
                         goto fail;
         }
@@ -288,6 +299,9 @@ fail:
         if (add_here)
                 match_free_if_empty(add_here);
 
+        if (j->level2)
+                match_free_if_empty(j->level2);
+
         if (j->level1)
                 match_free_if_empty(j->level1);
 
@@ -297,9 +311,7 @@ fail:
         return -ENOMEM;
 }
 
-_public_ int sd_journal_add_disjunction(sd_journal *j) {
-        Match *m;
-
+_public_ int sd_journal_add_conjunction(sd_journal *j) {
         assert(j);
 
         if (!j->level0)
@@ -311,11 +323,28 @@ _public_ int sd_journal_add_disjunction(sd_journal *j) {
         if (!j->level1->matches)
                 return 0;
 
-        m = match_new(j->level0, MATCH_AND_TERM);
-        if (!m)
-                return -ENOMEM;
+        j->level1 = NULL;
+        j->level2 = NULL;
+
+        return 0;
+}
+
+_public_ int sd_journal_add_disjunction(sd_journal *j) {
+        assert(j);
+
+        if (!j->level0)
+                return 0;
+
+        if (!j->level1)
+                return 0;
+
+        if (!j->level2)
+                return 0;
 
-        j->level1 = m;
+        if (!j->level2->matches)
+                return 0;
+
+        j->level2 = NULL;
         return 0;
 }
 
@@ -380,13 +409,13 @@ _public_ void sd_journal_flush_matches(sd_journal *j) {
         if (j->level0)
                 match_free(j->level0);
 
-        j->level0 = j->level1 = NULL;
+        j->level0 = j->level1 = j->level2 = NULL;
 
         detach_location(j);
 }
 
 static int compare_entry_order(JournalFile *af, Object *_ao,
-                         JournalFile *bf, uint64_t bp) {
+                               JournalFile *bf, uint64_t bp) {
 
         uint64_t a, b;
         Object *ao, *bo;
@@ -469,7 +498,7 @@ static int compare_entry_order(JournalFile *af, Object *_ao,
         return 0;
 }
 
-static int compare_with_location(JournalFile *af, Object *ao, Location *l) {
+_pure_ static int compare_with_location(JournalFile *af, Object *ao, Location *l) {
         uint64_t a;
 
         assert(af);
@@ -1222,15 +1251,15 @@ static void check_network(sd_journal *j, int fd) {
                 return;
 
         j->on_network =
-                (long)sfs.f_type == (long)CIFS_MAGIC_NUMBER ||
-                sfs.f_type == CODA_SUPER_MAGIC ||
-                sfs.f_type == NCP_SUPER_MAGIC ||
-                sfs.f_type == NFS_SUPER_MAGIC ||
-                sfs.f_type == SMB_SUPER_MAGIC;
+                F_TYPE_CMP(sfs.f_type, CIFS_MAGIC_NUMBER) ||
+                F_TYPE_CMP(sfs.f_type, CODA_SUPER_MAGIC) ||
+                F_TYPE_CMP(sfs.f_type, NCP_SUPER_MAGIC) ||
+                F_TYPE_CMP(sfs.f_type, NFS_SUPER_MAGIC) ||
+                F_TYPE_CMP(sfs.f_type, SMB_SUPER_MAGIC);
 }
 
 static int add_file(sd_journal *j, const char *prefix, const char *filename) {
-        char _cleanup_free_ *path = NULL;
+        _cleanup_free_ char *path = NULL;
         int r;
         JournalFile *f;
 
@@ -1273,7 +1302,7 @@ static int add_file(sd_journal *j, const char *prefix, const char *filename) {
                 return r;
         }
 
-        log_debug("File %s got added.", f->path);
+        log_debug("File %s added.", f->path);
 
         check_network(j, f->fd);
 
@@ -1301,7 +1330,7 @@ static int remove_file(sd_journal *j, const char *prefix, const char *filename)
 
         hashmap_remove(j->files, f->path);
 
-        log_debug("File %s got removed.", f->path);
+        log_debug("File %s removed.", f->path);
 
         if (j->current_file == f) {
                 j->current_file = NULL;
@@ -1321,9 +1350,9 @@ static int remove_file(sd_journal *j, const char *prefix, const char *filename)
 }
 
 static int add_directory(sd_journal *j, const char *prefix, const char *dirname) {
-        char _cleanup_free_ *path = NULL;
+        _cleanup_free_ char *path = NULL;
         int r;
-        DIR _cleanup_closedir_ *d = NULL;
+        _cleanup_closedir_ DIR *d = NULL;
         sd_id128_t id, mid;
         Directory *m;
 
@@ -1368,7 +1397,7 @@ static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
                 path = NULL; /* avoid freeing in cleanup */
                 j->current_invalidate_counter ++;
 
-                log_debug("Directory %s got added.", m->path);
+                log_debug("Directory %s added.", m->path);
 
         } else if (m->is_root)
                 return 0;
@@ -1411,7 +1440,7 @@ static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
 }
 
 static int add_root_directory(sd_journal *j, const char *p) {
-        DIR _cleanup_closedir_ *d = NULL;
+        _cleanup_closedir_ DIR *d = NULL;
         Directory *m;
         int r;
 
@@ -1447,7 +1476,7 @@ static int add_root_directory(sd_journal *j, const char *p) {
 
                 j->current_invalidate_counter ++;
 
-                log_debug("Root directory %s got added.", m->path);
+                log_debug("Root directory %s added.", m->path);
 
         } else if (!m->is_root)
                 return 0;
@@ -1508,9 +1537,9 @@ static int remove_directory(sd_journal *j, Directory *d) {
         hashmap_remove(j->directories_by_path, d->path);
 
         if (d->is_root)
-                log_debug("Root directory %s got removed.", d->path);
+                log_debug("Root directory %s removed.", d->path);
         else
-                log_debug("Directory %s got removed.", d->path);
+                log_debug("Directory %s removed.", d->path);
 
         free(d->path);
         free(d);
@@ -2188,6 +2217,8 @@ _public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from,
                 return -EINVAL;
         if (!from && !to)
                 return -EINVAL;
+        if (from == to)
+                return -EINVAL;
 
         HASHMAP_FOREACH(f, j->files, i) {
                 usec_t fr, t;
@@ -2227,6 +2258,8 @@ _public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot
                 return -EINVAL;
         if (!from && !to)
                 return -EINVAL;
+        if (from == to)
+                return -EINVAL;
 
         HASHMAP_FOREACH(f, j->files, i) {
                 usec_t fr, t;