chiark / gitweb /
remove unused variable
[elogind.git] / src / journal / sd-journal.c
index a83c0c25bfed032cc4bb1b5e7c7f75a81f1fadd5..bb116df0474a568163303010ee06c63a4b2192e8 100644 (file)
@@ -41,6 +41,7 @@
 #include "missing.h"
 #include "catalog.h"
 #include "replace-var.h"
+#include "fileio.h"
 
 #define JOURNAL_FILES_MAX 1024
 
@@ -186,7 +187,7 @@ static Match *match_new(Match *p, MatchType t) {
 
         if (p) {
                 m->parent = p;
-                LIST_PREPEND(Match, matches, p->matches, m);
+                LIST_PREPEND(matches, p->matches, m);
         }
 
         return m;
@@ -199,7 +200,7 @@ static void match_free(Match *m) {
                 match_free(m->matches);
 
         if (m->parent)
-                LIST_REMOVE(Match, matches, m->parent->matches, m);
+                LIST_REMOVE(matches, m->parent->matches, m);
 
         free(m->data);
         free(m);
@@ -216,19 +217,14 @@ _public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size)
         Match *l3, *l4, *add_here = NULL, *m;
         le64_t le_hash;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-
-        if (!data)
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(data, -EINVAL);
 
         if (size == 0)
                 size = strlen(data);
 
-        if (!match_is_valid(data, size))
-                return -EINVAL;
+        assert_return(match_is_valid(data, size), -EINVAL);
 
         /* level 0: AND term
          * level 1: OR terms
@@ -314,10 +310,8 @@ fail:
 }
 
 _public_ int sd_journal_add_conjunction(sd_journal *j) {
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         if (!j->level0)
                 return 0;
@@ -335,10 +329,8 @@ _public_ int sd_journal_add_conjunction(sd_journal *j) {
 }
 
 _public_ int sd_journal_add_disjunction(sd_journal *j) {
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         if (!j->level0)
                 return 0;
@@ -884,10 +876,8 @@ static int real_journal_next(sd_journal *j, direction_t direction) {
         Iterator i;
         int r;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         HASHMAP_FOREACH(f, j->files, i) {
                 bool found;
@@ -938,10 +928,8 @@ _public_ int sd_journal_previous(sd_journal *j) {
 static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
         int c = 0, r;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         if (skip == 0) {
                 /* If this is not a discrete skip, then at least
@@ -980,12 +968,9 @@ _public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
         int r;
         char bid[33], sid[33];
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (!cursor)
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(cursor, -EINVAL);
 
         if (!j->current_file || j->current_file->current_offset <= 0)
                 return -EADDRNOTAVAIL;
@@ -1021,12 +1006,9 @@ _public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
                 xor_hash_set = false;
         sd_id128_t seqnum_id, boot_id;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (isempty(cursor))
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(!isempty(cursor), -EINVAL);
 
         FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) {
                 char *item;
@@ -1122,12 +1104,9 @@ _public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
         size_t l;
         Object *o;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (isempty(cursor))
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(!isempty(cursor), -EINVAL);
 
         if (!j->current_file || j->current_file->current_offset <= 0)
                 return -EADDRNOTAVAIL;
@@ -1202,10 +1181,8 @@ _public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
 
 
 _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         reset_location(j);
         j->current_location.type = LOCATION_SEEK;
@@ -1217,10 +1194,8 @@ _public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, u
 }
 
 _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         reset_location(j);
         j->current_location.type = LOCATION_SEEK;
@@ -1231,10 +1206,8 @@ _public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
 }
 
 _public_ int sd_journal_seek_head(sd_journal *j) {
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         reset_location(j);
         j->current_location.type = LOCATION_HEAD;
@@ -1243,10 +1216,8 @@ _public_ int sd_journal_seek_head(sd_journal *j) {
 }
 
 _public_ int sd_journal_seek_tail(sd_journal *j) {
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         reset_location(j);
         j->current_location.type = LOCATION_TAIL;
@@ -1266,17 +1237,17 @@ static void check_network(sd_journal *j, int fd) {
                 return;
 
         j->on_network =
-                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);
+                F_TYPE_EQUAL(sfs.f_type, CIFS_MAGIC_NUMBER) ||
+                F_TYPE_EQUAL(sfs.f_type, CODA_SUPER_MAGIC) ||
+                F_TYPE_EQUAL(sfs.f_type, NCP_SUPER_MAGIC) ||
+                F_TYPE_EQUAL(sfs.f_type, NFS_SUPER_MAGIC) ||
+                F_TYPE_EQUAL(sfs.f_type, SMB_SUPER_MAGIC);
 }
 
 static bool file_has_type_prefix(const char *prefix, const char *filename) {
         const char *full, *tilded, *atted;
 
-        full = strappend(prefix, ".journal");
+        full = strappenda(prefix, ".journal");
         tilded = strappenda(full, "~");
         atted = strappenda(prefix, "@");
 
@@ -1507,6 +1478,9 @@ static int add_root_directory(sd_journal *j, const char *p) {
             !path_startswith(p, "/run"))
                 return -EINVAL;
 
+        if (j->prefix)
+                p = strappenda(j->prefix, p);
+
         d = opendir(p);
         if (!d)
                 return -errno;
@@ -1642,8 +1616,8 @@ static int add_current_paths(sd_journal *j) {
          * treat them as fatal. */
 
         HASHMAP_FOREACH(f, j->files, i) {
-                int r;
                 _cleanup_free_ char *dir;
+                int r;
 
                 dir = dirname_malloc(f->path);
                 if (!dir)
@@ -1713,14 +1687,8 @@ _public_ int sd_journal_open(sd_journal **ret, int flags) {
         sd_journal *j;
         int r;
 
-        if (!ret)
-                return -EINVAL;
-
-        if (flags & ~(SD_JOURNAL_LOCAL_ONLY|
-                      SD_JOURNAL_RUNTIME_ONLY|
-                      SD_JOURNAL_SYSTEM|
-                      SD_JOURNAL_CURRENT_USER))
-                return -EINVAL;
+        assert_return(ret, -EINVAL);
+        assert_return((flags & ~(SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_RUNTIME_ONLY|SD_JOURNAL_SYSTEM|SD_JOURNAL_CURRENT_USER)) == 0, -EINVAL);
 
         j = journal_new(flags, NULL);
         if (!j)
@@ -1739,18 +1707,55 @@ fail:
         return r;
 }
 
-_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
+_public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) {
+        _cleanup_free_ char *root = NULL, *class = NULL;
         sd_journal *j;
+        char *p;
         int r;
 
-        if (!ret)
-                return -EINVAL;
+        assert_return(machine, -EINVAL);
+        assert_return(ret, -EINVAL);
+        assert_return((flags & ~(SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM)) == 0, -EINVAL);
+        assert_return(filename_is_safe(machine), -EINVAL);
 
-        if (!path)
-                return -EINVAL;
+        p = strappenda("/run/systemd/machines/", machine);
+        r = parse_env_file(p, NEWLINE, "ROOT", &root, "CLASS", &class, NULL);
+        if (r == -ENOENT)
+                return -EHOSTDOWN;
+        if (r < 0)
+                return r;
+        if (!root)
+                return -ENODATA;
 
-        if (flags != 0)
-                return -EINVAL;
+        if (!streq_ptr(class, "container"))
+                return -EIO;
+
+        j = journal_new(flags, NULL);
+        if (!j)
+                return -ENOMEM;
+
+        j->prefix = root;
+        root = NULL;
+
+        r = add_search_paths(j);
+        if (r < 0)
+                goto fail;
+
+        *ret = j;
+        return 0;
+
+fail:
+        sd_journal_close(j);
+        return r;
+}
+
+_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
+        sd_journal *j;
+        int r;
+
+        assert_return(ret, -EINVAL);
+        assert_return(path, -EINVAL);
+        assert_return(flags == 0, -EINVAL);
 
         j = journal_new(flags, path);
         if (!j)
@@ -1776,11 +1781,8 @@ _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int fla
         const char **path;
         int r;
 
-        if (!ret)
-                return -EINVAL;
-
-        if (flags != 0)
-                return -EINVAL;
+        assert_return(ret, -EINVAL);
+        assert_return(flags == 0, -EINVAL);
 
         j = journal_new(flags, NULL);
         if (!j)
@@ -1831,10 +1833,13 @@ _public_ void sd_journal_close(sd_journal *j) {
         if (j->inotify_fd >= 0)
                 close_nointr_nofail(j->inotify_fd);
 
-        if (j->mmap)
+        if (j->mmap) {
+                log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap));
                 mmap_cache_unref(j->mmap);
+        }
 
         free(j->path);
+        free(j->prefix);
         free(j->unique_field);
         set_free(j->errors);
         free(j);
@@ -1845,12 +1850,9 @@ _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
         JournalFile *f;
         int r;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (!ret)
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(ret, -EINVAL);
 
         f = j->current_file;
         if (!f)
@@ -1873,10 +1875,8 @@ _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id12
         int r;
         sd_id128_t id;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         f = j->current_file;
         if (!f)
@@ -1941,19 +1941,12 @@ _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **
         int r;
         Object *o;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (!field)
-                return -EINVAL;
-        if (!data)
-                return -EINVAL;
-        if (!size)
-                return -EINVAL;
-
-        if (!field_is_valid(field))
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(field, -EINVAL);
+        assert_return(data, -EINVAL);
+        assert_return(size, -EINVAL);
+        assert_return(field_is_valid(field), -EINVAL);
 
         f = j->current_file;
         if (!f)
@@ -2069,14 +2062,10 @@ _public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t
         int r;
         Object *o;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (!data)
-                return -EINVAL;
-        if (!size)
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(data, -EINVAL);
+        assert_return(size, -EINVAL);
 
         f = j->current_file;
         if (!f)
@@ -2121,10 +2110,8 @@ _public_ void sd_journal_restart_data(sd_journal *j) {
 _public_ int sd_journal_get_fd(sd_journal *j) {
         int r;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         if (j->inotify_fd >= 0)
                 return j->inotify_fd;
@@ -2150,10 +2137,8 @@ _public_ int sd_journal_get_fd(sd_journal *j) {
 _public_ int sd_journal_get_events(sd_journal *j) {
         int fd;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         fd = sd_journal_get_fd(j);
         if (fd < 0)
@@ -2165,12 +2150,9 @@ _public_ int sd_journal_get_events(sd_journal *j) {
 _public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
         int fd;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (!timeout_usec)
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(timeout_usec, -EINVAL);
 
         fd = sd_journal_get_fd(j);
         if (fd < 0)
@@ -2267,10 +2249,8 @@ _public_ int sd_journal_process(sd_journal *j) {
         uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX] _alignas_(struct inotify_event);
         bool got_something = false;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         j->last_process_usec = now(CLOCK_MONOTONIC);
 
@@ -2309,10 +2289,8 @@ _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
         int r;
         uint64_t t;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         if (j->inotify_fd < 0) {
 
@@ -2359,14 +2337,10 @@ _public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from,
         bool first = true;
         int r;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (!from && !to)
-                return -EINVAL;
-        if (from == to)
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(from || to, -EINVAL);
+        assert_return(from != to, -EINVAL);
 
         HASHMAP_FOREACH(f, j->files, i) {
                 usec_t fr, t;
@@ -2402,14 +2376,10 @@ _public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot
         bool first = true;
         int r;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (!from && !to)
-                return -EINVAL;
-        if (from == to)
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(from || to, -EINVAL);
+        assert_return(from != to, -EINVAL);
 
         HASHMAP_FOREACH(f, j->files, i) {
                 usec_t fr, t;
@@ -2461,12 +2431,9 @@ _public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
         JournalFile *f;
         uint64_t sum = 0;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (!bytes)
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(bytes, -EINVAL);
 
         HASHMAP_FOREACH(f, j->files, i) {
                 struct stat st;
@@ -2484,14 +2451,10 @@ _public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
 _public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
         char *f;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (isempty(field))
-                return -EINVAL;
-        if (!field_is_valid(field))
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(!isempty(field), -EINVAL);
+        assert_return(field_is_valid(field), -EINVAL);
 
         f = strdup(field);
         if (!f)
@@ -2510,16 +2473,11 @@ _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
         size_t k;
         int r;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (!data)
-                return -EINVAL;
-        if (!l)
-                return -EINVAL;
-        if (!j->unique_field)
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(data, -EINVAL);
+        assert_return(l, -EINVAL);
+        assert_return(j->unique_field, -EINVAL);
 
         k = strlen(j->unique_field);
 
@@ -2624,10 +2582,8 @@ _public_ void sd_journal_restart_unique(sd_journal *j) {
 }
 
 _public_ int sd_journal_reliable_fd(sd_journal *j) {
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         return !j->on_network;
 }
@@ -2659,12 +2615,9 @@ _public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
         char *t;
         int r;
 
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (!ret)
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(ret, -EINVAL);
 
         r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size);
         if (r < 0)
@@ -2691,29 +2644,23 @@ _public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
 }
 
 _public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) {
-        if (!ret)
-                return -EINVAL;
+        assert_return(ret, -EINVAL);
 
         return catalog_get(CATALOG_DATABASE, id, ret);
 }
 
 _public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
 
         j->data_threshold = sz;
         return 0;
 }
 
 _public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) {
-        if (!j)
-                return -EINVAL;
-        if (journal_pid_changed(j))
-                return -ECHILD;
-        if (!sz)
-                return -EINVAL;
+        assert_return(j, -EINVAL);
+        assert_return(!journal_pid_changed(j), -ECHILD);
+        assert_return(sz, -EINVAL);
 
         *sz = j->data_threshold;
         return 0;