]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: voidify mkdir_p() call and unify two similar code paths 12119/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 27 Mar 2019 08:30:35 +0000 (09:30 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 27 Mar 2019 12:01:44 +0000 (13:01 +0100)
Let's unify the two similar code paths to watch /run/systemd/journal.
The code in manager.c is similar, but it uses mkdir_p_label(), and unifying
that would be too much trouble, so let's just adjust the error messages to
be the same.

CID #1400224.

src/core/manager.c
src/journal/journalctl.c

index 7f4f71b9e95d0132a371db77f50b2b6f464c13b9..0e5dad5116c1f83e148daed18ce21779a594e619 100644 (file)
@@ -292,10 +292,10 @@ static int manager_check_ask_password(Manager *m) {
 
                 m->ask_password_inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
                 if (m->ask_password_inotify_fd < 0)
-                        return log_error_errno(errno, "inotify_init1() failed: %m");
+                        return log_error_errno(errno, "Failed to create inotify object: %m");
 
                 if (inotify_add_watch(m->ask_password_inotify_fd, "/run/systemd/ask-password", IN_CREATE|IN_DELETE|IN_MOVE) < 0) {
-                        log_error_errno(errno, "Failed to add watch on /run/systemd/ask-password: %m");
+                        log_error_errno(errno, "Failed to watch \"/run/systemd/ask-password\": %m");
                         manager_close_ask_password(m);
                         return -errno;
                 }
index f2386762b2268c222c676cc787fb8bc7c940247a..42fb25cc4683e2f9b19538332e71478084ea28b8 100644 (file)
@@ -1901,6 +1901,21 @@ static int verify(sd_journal *j) {
         return r;
 }
 
+static int watch_run_systemd_journal(uint32_t mask) {
+        _cleanup_close_ int watch_fd = -1;
+
+        (void) mkdir_p("/run/systemd/journal", 0755);
+
+        watch_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
+        if (watch_fd < 0)
+                return log_error_errno(errno, "Failed to create inotify object: %m");
+
+        if (inotify_add_watch(watch_fd, "/run/systemd/journal", mask) < 0)
+                return log_error_errno(errno, "Failed to watch \"/run/systemd/journal\": %m");
+
+        return TAKE_FD(watch_fd);
+}
+
 static int flush_to_var(void) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
@@ -1934,19 +1949,13 @@ static int flush_to_var(void) {
         if (r < 0)
                 return log_error_errno(r, "Failed to kill journal service: %s", bus_error_message(&error, r));
 
-        mkdir_p("/run/systemd/journal", 0755);
-
-        watch_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
+        watch_fd = watch_run_systemd_journal(IN_CREATE|IN_DONT_FOLLOW|IN_ONLYDIR);
         if (watch_fd < 0)
-                return log_error_errno(errno, "Failed to create inotify watch: %m");
-
-        r = inotify_add_watch(watch_fd, "/run/systemd/journal", IN_CREATE|IN_DONT_FOLLOW|IN_ONLYDIR);
-        if (r < 0)
-                return log_error_errno(errno, "Failed to watch journal directory: %m");
+                return watch_fd;
 
         for (;;) {
                 if (access("/run/systemd/journal/flushed", F_OK) >= 0)
-                        break;
+                        return 0;
 
                 if (errno != ENOENT)
                         return log_error_errno(errno, "Failed to check for existence of /run/systemd/journal/flushed: %m");
@@ -1959,8 +1968,6 @@ static int flush_to_var(void) {
                 if (r < 0)
                         return log_error_errno(r, "Failed to flush inotify events: %m");
         }
-
-        return 0;
 }
 
 static int send_signal_and_wait(int sig, const char *watch_path) {
@@ -2016,23 +2023,15 @@ static int send_signal_and_wait(int sig, const char *watch_path) {
 
                 /* Let's install the inotify watch, if we didn't do that yet. */
                 if (watch_fd < 0) {
-
-                        mkdir_p("/run/systemd/journal", 0755);
-
-                        watch_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
+                        watch_fd = watch_run_systemd_journal(IN_MOVED_TO|IN_DONT_FOLLOW|IN_ONLYDIR);
                         if (watch_fd < 0)
-                                return log_error_errno(errno, "Failed to create inotify watch: %m");
-
-                        r = inotify_add_watch(watch_fd, "/run/systemd/journal", IN_MOVED_TO|IN_DONT_FOLLOW|IN_ONLYDIR);
-                        if (r < 0)
-                                return log_error_errno(errno, "Failed to watch journal directory: %m");
+                                return watch_fd;
 
                         /* Recheck the flag file immediately, so that we don't miss any event since the last check. */
                         continue;
                 }
 
-                /* OK, all preparatory steps done, let's wait until
-                 * inotify reports an event. */
+                /* OK, all preparatory steps done, let's wait until inotify reports an event. */
 
                 r = fd_wait_for_event(watch_fd, POLLIN, USEC_INFINITY);
                 if (r < 0)