Blame SOURCES/0592-sd-journal-various-clean-ups-and-modernizations.patch

17b0f1
From 6930375c3f0d9681f27b42f1d0406721c3f9a013 Mon Sep 17 00:00:00 2001
17b0f1
From: Lennart Poettering <lennart@poettering.net>
17b0f1
Date: Mon, 2 Nov 2015 23:14:30 +0100
17b0f1
Subject: [PATCH] sd-journal: various clean-ups and modernizations
17b0f1
17b0f1
- Always print a debug log message about files and directories we cannot
17b0f1
  open right when it happens instead of the caller, thus reducing the
17b0f1
  number of places where we need to generate the debug message.
17b0f1
17b0f1
- Always push the errors we encounter immediately into the error set,
17b0f1
  when we run into them, instead of in the caller. Thus, we never forget
17b0f1
  to push them in.
17b0f1
17b0f1
- Use stack instead of heap memory where we can.
17b0f1
17b0f1
- Make remove_file() void, since it cannot fail anyway and always
17b0f1
  returned 0.
17b0f1
17b0f1
- Make local machine check of journal directories explicit in a
17b0f1
  function, to make things more readable.
17b0f1
17b0f1
- Port to all directory listing loops FOREACH_DIRENT_ALL()
17b0f1
17b0f1
- sd-daemon is library code, hence never log at higher log levels than
17b0f1
  LOG_DEBUG.
17b0f1
17b0f1
(cherry picked from commit d617408ecbe69db69aefddfcb10a6c054ea46ba0)
17b0f1
17b0f1
Related: #1465759
17b0f1
---
17b0f1
 src/journal/sd-journal.c | 242 ++++++++++++++++++---------------------
17b0f1
 1 file changed, 111 insertions(+), 131 deletions(-)
17b0f1
17b0f1
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
17b0f1
index 3749f9e895..9895d9608f 100644
17b0f1
--- a/src/journal/sd-journal.c
17b0f1
+++ b/src/journal/sd-journal.c
17b0f1
@@ -1171,6 +1171,8 @@ static bool file_has_type_prefix(const char *prefix, const char *filename) {
17b0f1
 }
17b0f1
 
17b0f1
 static bool file_type_wanted(int flags, const char *filename) {
17b0f1
+        assert(filename);
17b0f1
+
17b0f1
         if (!endswith(filename, ".journal") && !endswith(filename, ".journal~"))
17b0f1
                 return false;
17b0f1
 
17b0f1
@@ -1195,7 +1197,7 @@ static bool file_type_wanted(int flags, const char *filename) {
17b0f1
 
17b0f1
 static int add_any_file(sd_journal *j, const char *path) {
17b0f1
         JournalFile *f = NULL;
17b0f1
-        int r;
17b0f1
+        int r, k;
17b0f1
 
17b0f1
         assert(j);
17b0f1
         assert(path);
17b0f1
@@ -1204,20 +1206,23 @@ static int add_any_file(sd_journal *j, const char *path) {
17b0f1
                 return 0;
17b0f1
 
17b0f1
         if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
17b0f1
-                log_warning("Too many open journal files, not adding %s.", path);
17b0f1
-                return set_put_error(j, -ETOOMANYREFS);
17b0f1
+                log_debug("Too many open journal files, not adding %s.", path);
17b0f1
+                r = -ETOOMANYREFS;
17b0f1
+                goto fail;
17b0f1
         }
17b0f1
 
17b0f1
         r = journal_file_open(path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, &f);
17b0f1
-        if (r < 0)
17b0f1
-                return r;
17b0f1
+        if (r < 0) {
17b0f1
+                log_debug_errno(r, "Failed to open journal file %s: %m", path);
17b0f1
+                goto fail;
17b0f1
+        }
17b0f1
 
17b0f1
         /* journal_file_dump(f); */
17b0f1
 
17b0f1
         r = ordered_hashmap_put(j->files, f->path, f);
17b0f1
         if (r < 0) {
17b0f1
                 journal_file_close(f);
17b0f1
-                return r;
17b0f1
+                goto fail;
17b0f1
         }
17b0f1
 
17b0f1
         log_debug("File %s added.", f->path);
17b0f1
@@ -1227,10 +1232,17 @@ static int add_any_file(sd_journal *j, const char *path) {
17b0f1
         j->current_invalidate_counter ++;
17b0f1
 
17b0f1
         return 0;
17b0f1
+
17b0f1
+fail:
17b0f1
+        k = set_put_error(j, r);
17b0f1
+        if (k < 0)
17b0f1
+                return k;
17b0f1
+
17b0f1
+        return r;
17b0f1
 }
17b0f1
 
17b0f1
 static int add_file(sd_journal *j, const char *prefix, const char *filename) {
17b0f1
-        char *path = NULL;
17b0f1
+        const char *path;
17b0f1
 
17b0f1
         assert(j);
17b0f1
         assert(prefix);
17b0f1
@@ -1250,24 +1262,20 @@ static int add_file(sd_journal *j, const char *prefix, const char *filename) {
17b0f1
         return add_any_file(j, path);
17b0f1
 }
17b0f1
 
17b0f1
-static int remove_file(sd_journal *j, const char *prefix, const char *filename) {
17b0f1
-        _cleanup_free_ char *path;
17b0f1
+static void remove_file(sd_journal *j, const char *prefix, const char *filename) {
17b0f1
+        const char *path;
17b0f1
         JournalFile *f;
17b0f1
 
17b0f1
         assert(j);
17b0f1
         assert(prefix);
17b0f1
         assert(filename);
17b0f1
 
17b0f1
-        path = strjoin(prefix, "/", filename, NULL);
17b0f1
-        if (!path)
17b0f1
-                return -ENOMEM;
17b0f1
-
17b0f1
+        path = strjoina(prefix, "/", filename);
17b0f1
         f = ordered_hashmap_get(j->files, path);
17b0f1
         if (!f)
17b0f1
-                return 0;
17b0f1
+                return;
17b0f1
 
17b0f1
         remove_file_real(j, f);
17b0f1
-        return 0;
17b0f1
 }
17b0f1
 
17b0f1
 static void remove_file_real(sd_journal *j, JournalFile *f) {
17b0f1
@@ -1296,12 +1304,27 @@ static void remove_file_real(sd_journal *j, JournalFile *f) {
17b0f1
         j->current_invalidate_counter ++;
17b0f1
 }
17b0f1
 
17b0f1
+static int dirname_is_machine_id(const char *fn) {
17b0f1
+        sd_id128_t id, machine;
17b0f1
+        int r;
17b0f1
+
17b0f1
+        r = sd_id128_get_machine(&machine);
17b0f1
+        if (r < 0)
17b0f1
+                return r;
17b0f1
+
17b0f1
+        r = sd_id128_from_string(fn, &id;;
17b0f1
+        if (r < 0)
17b0f1
+                return r;
17b0f1
+
17b0f1
+        return sd_id128_equal(id, machine);
17b0f1
+}
17b0f1
+
17b0f1
 static int add_directory(sd_journal *j, const char *prefix, const char *dirname) {
17b0f1
         _cleanup_free_ char *path = NULL;
17b0f1
-        int r;
17b0f1
         _cleanup_closedir_ DIR *d = NULL;
17b0f1
-        sd_id128_t id, mid;
17b0f1
+        struct dirent *de = NULL;
17b0f1
         Directory *m;
17b0f1
+        int r, k;
17b0f1
 
17b0f1
         assert(j);
17b0f1
         assert(prefix);
17b0f1
@@ -1310,35 +1333,36 @@ static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
17b0f1
         log_debug("Considering %s/%s.", prefix, dirname);
17b0f1
 
17b0f1
         if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
17b0f1
-            (sd_id128_from_string(dirname, &id) < 0 ||
17b0f1
-             sd_id128_get_machine(&mid) < 0 ||
17b0f1
-             !(sd_id128_equal(id, mid) || path_startswith(prefix, "/run"))))
17b0f1
+            !(dirname_is_machine_id(dirname) > 0 || path_startswith(prefix, "/run")))
17b0f1
             return 0;
17b0f1
 
17b0f1
         path = strjoin(prefix, "/", dirname, NULL);
17b0f1
-        if (!path)
17b0f1
-                return -ENOMEM;
17b0f1
+        if (!path) {
17b0f1
+                r = -ENOMEM;
17b0f1
+                goto fail;
17b0f1
+        }
17b0f1
 
17b0f1
         d = opendir(path);
17b0f1
         if (!d) {
17b0f1
-                log_debug_errno(errno, "Failed to open %s: %m", path);
17b0f1
-                if (errno == ENOENT)
17b0f1
-                        return 0;
17b0f1
-                return -errno;
17b0f1
+                r = log_debug_errno(errno, "Failed to open directory %s: %m", path);
17b0f1
+                goto fail;
17b0f1
         }
17b0f1
 
17b0f1
         m = hashmap_get(j->directories_by_path, path);
17b0f1
         if (!m) {
17b0f1
                 m = new0(Directory, 1);
17b0f1
-                if (!m)
17b0f1
-                        return -ENOMEM;
17b0f1
+                if (!m) {
17b0f1
+                        r = -ENOMEM;
17b0f1
+                        goto fail;
17b0f1
+                }
17b0f1
 
17b0f1
                 m->is_root = false;
17b0f1
                 m->path = path;
17b0f1
 
17b0f1
                 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
17b0f1
                         free(m);
17b0f1
-                        return -ENOMEM;
17b0f1
+                        r = -ENOMEM;
17b0f1
+                        goto fail;
17b0f1
                 }
17b0f1
 
17b0f1
                 path = NULL; /* avoid freeing in cleanup */
17b0f1
@@ -1360,41 +1384,30 @@ static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
17b0f1
                         inotify_rm_watch(j->inotify_fd, m->wd);
17b0f1
         }
17b0f1
 
17b0f1
-        for (;;) {
17b0f1
-                struct dirent *de;
17b0f1
-
17b0f1
-                errno = 0;
17b0f1
-                de = readdir(d);
17b0f1
-                if (!de && errno != 0) {
17b0f1
-                        r = -errno;
17b0f1
-                        log_debug_errno(errno, "Failed to read directory %s: %m", m->path);
17b0f1
-                        return r;
17b0f1
-                }
17b0f1
-                if (!de)
17b0f1
-                        break;
17b0f1
+        FOREACH_DIRENT_ALL(de, d, return log_debug_errno(errno, "Failed to read directory %s: %m", m->path)) {
17b0f1
 
17b0f1
                 if (dirent_is_file_with_suffix(de, ".journal") ||
17b0f1
-                    dirent_is_file_with_suffix(de, ".journal~")) {
17b0f1
-                        r = add_file(j, m->path, de->d_name);
17b0f1
-                        if (r < 0) {
17b0f1
-                                log_debug_errno(r, "Failed to add file %s/%s: %m",
17b0f1
-                                                m->path, de->d_name);
17b0f1
-                                r = set_put_error(j, r);
17b0f1
-                                if (r < 0)
17b0f1
-                                        return r;
17b0f1
-                        }
17b0f1
-                }
17b0f1
+                    dirent_is_file_with_suffix(de, ".journal~"))
17b0f1
+                        (void) add_file(j, m->path, de->d_name);
17b0f1
         }
17b0f1
 
17b0f1
         check_network(j, dirfd(d));
17b0f1
 
17b0f1
         return 0;
17b0f1
+
17b0f1
+fail:
17b0f1
+        k = set_put_error(j, r);
17b0f1
+        if (k < 0)
17b0f1
+                return k;
17b0f1
+
17b0f1
+        return r;
17b0f1
 }
17b0f1
 
17b0f1
-static int add_root_directory(sd_journal *j, const char *p) {
17b0f1
+static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) {
17b0f1
         _cleanup_closedir_ DIR *d = NULL;
17b0f1
+        struct dirent *de;
17b0f1
         Directory *m;
17b0f1
-        int r;
17b0f1
+        int r, k;
17b0f1
 
17b0f1
         assert(j);
17b0f1
         assert(p);
17b0f1
@@ -1407,26 +1420,35 @@ static int add_root_directory(sd_journal *j, const char *p) {
17b0f1
                 p = strjoina(j->prefix, p);
17b0f1
 
17b0f1
         d = opendir(p);
17b0f1
-        if (!d)
17b0f1
-                return -errno;
17b0f1
+        if (!d) {
17b0f1
+                if (errno == ENOENT && missing_ok)
17b0f1
+                        return 0;
17b0f1
+
17b0f1
+                r = log_debug_errno(errno, "Failed to open root directory %s: %m", p);
17b0f1
+                goto fail;
17b0f1
+        }
17b0f1
 
17b0f1
         m = hashmap_get(j->directories_by_path, p);
17b0f1
         if (!m) {
17b0f1
                 m = new0(Directory, 1);
17b0f1
-                if (!m)
17b0f1
-                        return -ENOMEM;
17b0f1
+                if (!m) {
17b0f1
+                        r = -ENOMEM;
17b0f1
+                        goto fail;
17b0f1
+                }
17b0f1
 
17b0f1
                 m->is_root = true;
17b0f1
                 m->path = strdup(p);
17b0f1
                 if (!m->path) {
17b0f1
                         free(m);
17b0f1
-                        return -ENOMEM;
17b0f1
+                        r = -ENOMEM;
17b0f1
+                        goto fail;
17b0f1
                 }
17b0f1
 
17b0f1
                 if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
17b0f1
                         free(m->path);
17b0f1
                         free(m);
17b0f1
-                        return -ENOMEM;
17b0f1
+                        r = -ENOMEM;
17b0f1
+                        goto fail;
17b0f1
                 }
17b0f1
 
17b0f1
                 j->current_invalidate_counter ++;
17b0f1
@@ -1449,42 +1471,27 @@ static int add_root_directory(sd_journal *j, const char *p) {
17b0f1
         if (j->no_new_files)
17b0f1
                 return 0;
17b0f1
 
17b0f1
-        for (;;) {
17b0f1
-                struct dirent *de;
17b0f1
+        FOREACH_DIRENT_ALL(de, d, return log_debug_errno(errno, "Failed to read directory %s: %m", m->path)) {
17b0f1
                 sd_id128_t id;
17b0f1
 
17b0f1
-                errno = 0;
17b0f1
-                de = readdir(d);
17b0f1
-                if (!de && errno != 0) {
17b0f1
-                        r = -errno;
17b0f1
-                        log_debug_errno(errno, "Failed to read directory %s: %m", m->path);
17b0f1
-                        return r;
17b0f1
-                }
17b0f1
-                if (!de)
17b0f1
-                        break;
17b0f1
-
17b0f1
                 if (dirent_is_file_with_suffix(de, ".journal") ||
17b0f1
-                    dirent_is_file_with_suffix(de, ".journal~")) {
17b0f1
-                        r = add_file(j, m->path, de->d_name);
17b0f1
-                        if (r < 0) {
17b0f1
-                                log_debug_errno(r, "Failed to add file %s/%s: %m",
17b0f1
-                                                m->path, de->d_name);
17b0f1
-                                r = set_put_error(j, r);
17b0f1
-                                if (r < 0)
17b0f1
-                                        return r;
17b0f1
-                        }
17b0f1
-                } else if ((de->d_type == DT_DIR || de->d_type == DT_LNK || de->d_type == DT_UNKNOWN) &&
17b0f1
-                           sd_id128_from_string(de->d_name, &id) >= 0) {
17b0f1
-
17b0f1
-                        r = add_directory(j, m->path, de->d_name);
17b0f1
-                        if (r < 0)
17b0f1
-                                log_debug_errno(r, "Failed to add directory %s/%s: %m", m->path, de->d_name);
17b0f1
-                }
17b0f1
+                    dirent_is_file_with_suffix(de, ".journal~"))
17b0f1
+                        (void) add_file(j, m->path, de->d_name);
17b0f1
+                else if (IN_SET(de->d_type, DT_DIR, DT_LNK, DT_UNKNOWN) &&
17b0f1
+                         sd_id128_from_string(de->d_name, &id) >= 0)
17b0f1
+                        (void) add_directory(j, m->path, de->d_name);
17b0f1
         }
17b0f1
 
17b0f1
         check_network(j, dirfd(d));
17b0f1
 
17b0f1
         return 0;
17b0f1
+
17b0f1
+fail:
17b0f1
+        k = set_put_error(j, r);
17b0f1
+        if (k < 0)
17b0f1
+                return k;
17b0f1
+
17b0f1
+        return r;
17b0f1
 }
17b0f1
 
17b0f1
 static void remove_directory(sd_journal *j, Directory *d) {
17b0f1
@@ -1509,8 +1516,8 @@ static void remove_directory(sd_journal *j, Directory *d) {
17b0f1
 }
17b0f1
 
17b0f1
 static int add_search_paths(sd_journal *j) {
17b0f1
-        int r;
17b0f1
-        const char search_paths[] =
17b0f1
+
17b0f1
+        static const char search_paths[] =
17b0f1
                 "/run/log/journal\0"
17b0f1
                 "/var/log/journal\0";
17b0f1
         const char *p;
17b0f1
@@ -1520,14 +1527,8 @@ static int add_search_paths(sd_journal *j) {
17b0f1
         /* We ignore most errors here, since the idea is to only open
17b0f1
          * what's actually accessible, and ignore the rest. */
17b0f1
 
17b0f1
-        NULSTR_FOREACH(p, search_paths) {
17b0f1
-                r = add_root_directory(j, p);
17b0f1
-                if (r < 0 && r != -ENOENT) {
17b0f1
-                        r = set_put_error(j, r);
17b0f1
-                        if (r < 0)
17b0f1
-                                return r;
17b0f1
-                }
17b0f1
-        }
17b0f1
+        NULSTR_FOREACH(p, search_paths)
17b0f1
+                (void) add_root_directory(j, p, true);
17b0f1
 
17b0f1
         return 0;
17b0f1
 }
17b0f1
@@ -1551,17 +1552,14 @@ static int add_current_paths(sd_journal *j) {
17b0f1
                 if (!dir)
17b0f1
                         return -ENOMEM;
17b0f1
 
17b0f1
-                r = add_root_directory(j, dir);
17b0f1
-                if (r < 0) {
17b0f1
-                        set_put_error(j, r);
17b0f1
+                r = add_root_directory(j, dir, true);
17b0f1
+                if (r < 0)
17b0f1
                         return r;
17b0f1
-                }
17b0f1
         }
17b0f1
 
17b0f1
         return 0;
17b0f1
 }
17b0f1
 
17b0f1
-
17b0f1
 static int allocate_inotify(sd_journal *j) {
17b0f1
         assert(j);
17b0f1
 
17b0f1
@@ -1689,11 +1687,9 @@ _public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int f
17b0f1
         if (!j)
17b0f1
                 return -ENOMEM;
17b0f1
 
17b0f1
-        r = add_root_directory(j, path);
17b0f1
-        if (r < 0) {
17b0f1
-                set_put_error(j, r);
17b0f1
+        r = add_root_directory(j, path, false);
17b0f1
+        if (r < 0)
17b0f1
                 goto fail;
17b0f1
-        }
17b0f1
 
17b0f1
         *ret = j;
17b0f1
         return 0;
17b0f1
@@ -1718,10 +1714,8 @@ _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int fla
17b0f1
 
17b0f1
         STRV_FOREACH(path, paths) {
17b0f1
                 r = add_any_file(j, *path);
17b0f1
-                if (r < 0) {
17b0f1
-                        log_error_errno(r, "Failed to open %s: %m", *path);
17b0f1
+                if (r < 0)
17b0f1
                         goto fail;
17b0f1
-                }
17b0f1
         }
17b0f1
 
17b0f1
         j->no_new_files = true;
17b0f1
@@ -2061,7 +2055,7 @@ _public_ int sd_journal_get_fd(sd_journal *j) {
17b0f1
         if (j->no_new_files)
17b0f1
                 r = add_current_paths(j);
17b0f1
         else if (j->path)
17b0f1
-                r = add_root_directory(j, j->path);
17b0f1
+                r = add_root_directory(j, j->path, true);
17b0f1
         else
17b0f1
                 r = add_search_paths(j);
17b0f1
         if (r < 0)
17b0f1
@@ -2108,7 +2102,6 @@ _public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
17b0f1
 
17b0f1
 static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
17b0f1
         Directory *d;
17b0f1
-        int r;
17b0f1
 
17b0f1
         assert(j);
17b0f1
         assert(e);
17b0f1
@@ -2124,20 +2117,10 @@ static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
17b0f1
 
17b0f1
                         /* Event for a journal file */
17b0f1
 
17b0f1
-                        if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
17b0f1
-                                r = add_file(j, d->path, e->name);
17b0f1
-                                if (r < 0) {
17b0f1
-                                        log_debug_errno(r, "Failed to add file %s/%s: %m",
17b0f1
-                                                        d->path, e->name);
17b0f1
-                                        set_put_error(j, r);
17b0f1
-                                }
17b0f1
-
17b0f1
-                        } else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT)) {
17b0f1
-
17b0f1
-                                r = remove_file(j, d->path, e->name);
17b0f1
-                                if (r < 0)
17b0f1
-                                        log_debug_errno(r, "Failed to remove file %s/%s: %m", d->path, e->name);
17b0f1
-                        }
17b0f1
+                        if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
17b0f1
+                                (void) add_file(j, d->path, e->name);
17b0f1
+                        else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT))
17b0f1
+                                remove_file(j, d->path, e->name);
17b0f1
 
17b0f1
                 } else if (!d->is_root && e->len == 0) {
17b0f1
 
17b0f1
@@ -2150,11 +2133,8 @@ static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
17b0f1
 
17b0f1
                         /* Event for root directory */
17b0f1
 
17b0f1
-                        if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
17b0f1
-                                r = add_directory(j, d->path, e->name);
17b0f1
-                                if (r < 0)
17b0f1
-                                        log_debug_errno(r, "Failed to add directory %s/%s: %m", d->path, e->name);
17b0f1
-                        }
17b0f1
+                        if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
17b0f1
+                                (void) add_directory(j, d->path, e->name);
17b0f1
                 }
17b0f1
 
17b0f1
                 return;
17b0f1
@@ -2163,7 +2143,7 @@ static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
17b0f1
         if (e->mask & IN_IGNORED)
17b0f1
                 return;
17b0f1
 
17b0f1
-        log_warning("Unknown inotify event.");
17b0f1
+        log_debug("Unknown inotify event.");
17b0f1
 }
17b0f1
 
17b0f1
 static int determine_change(sd_journal *j) {