]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
remount-fs: modernize coding style a bit
authorLennart Poettering <lennart@poettering.net>
Mon, 16 Nov 2015 23:27:18 +0000 (00:27 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 16 Nov 2015 23:52:10 +0000 (00:52 +0100)
a) Use _cleanup_ where it makes sense

b) Uniformly use negative errno-style errors internally, convert to
   EXIT_FAILURE/EXIT_SUCCESS only when actually exiting.

c) Use log_oom() where appropriate

d) Fix minor memory leak in hashmap addition error path.

e) Don't pretend we could continue sensibly on OOM or fork() failure

f) Use PR_SET_PDEATHSIG to make sure clients we don't kill on error are
   cleaned up.

g) Make use of STRV_MAKE() where it's pretty to do so.

h) Simplify error paths.

src/remount-fs/remount-fs.c

index 912a42654e8bd39e4a8b55c6e056dc5ce4e6501e..9fc56284d245a12028808a1f11d1a17384234533 100644 (file)
@@ -22,6 +22,7 @@
 #include <errno.h>
 #include <mntent.h>
 #include <string.h>
+#include <sys/prctl.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <unistd.h>
@@ -31,7 +32,9 @@
 #include "mount-setup.h"
 #include "mount-util.h"
 #include "path-util.h"
+#include "process-util.h"
 #include "signal-util.h"
+#include "strv.h"
 #include "util.h"
 
 /* Goes through /etc/fstab and remounts all API file systems, applying
  * respected */
 
 int main(int argc, char *argv[]) {
-        int ret = EXIT_FAILURE;
+        _cleanup_hashmap_free_free_ Hashmap *pids = NULL;
         _cleanup_endmntent_ FILE *f = NULL;
         struct mntent* me;
-        Hashmap *pids = NULL;
+        int r;
 
         if (argc > 1) {
                 log_error("This program takes no argument.");
@@ -57,21 +60,21 @@ int main(int argc, char *argv[]) {
 
         f = setmntent("/etc/fstab", "r");
         if (!f) {
-                if (errno == ENOENT)
-                        return EXIT_SUCCESS;
+                if (errno == ENOENT) {
+                        r = 0;
+                        goto finish;
+                }
 
-                log_error_errno(errno, "Failed to open /etc/fstab: %m");
-                return EXIT_FAILURE;
+                r = log_error_errno(errno, "Failed to open /etc/fstab: %m");
+                goto finish;
         }
 
         pids = hashmap_new(NULL);
         if (!pids) {
-                log_error("Failed to allocate set");
+                r = log_oom();
                 goto finish;
         }
 
-        ret = EXIT_SUCCESS;
-
         while ((me = getmntent(f))) {
                 pid_t pid;
                 int k;
@@ -87,25 +90,18 @@ int main(int argc, char *argv[]) {
 
                 pid = fork();
                 if (pid < 0) {
-                        log_error_errno(errno, "Failed to fork: %m");
-                        ret = EXIT_FAILURE;
-                        continue;
+                        r = log_error_errno(errno, "Failed to fork: %m");
+                        goto finish;
                 }
 
                 if (pid == 0) {
-                        const char *arguments[5];
                         /* Child */
 
                         (void) reset_all_signal_handlers();
                         (void) reset_signal_mask();
+                        (void) prctl(PR_SET_PDEATHSIG, SIGTERM);
 
-                        arguments[0] = MOUNT_PATH;
-                        arguments[1] = me->mnt_dir;
-                        arguments[2] = "-o";
-                        arguments[3] = "remount";
-                        arguments[4] = NULL;
-
-                        execv(MOUNT_PATH, (char **) arguments);
+                        execv(MOUNT_PATH, STRV_MAKE(MOUNT_PATH, me->mnt_dir, "-o", "remount"));
 
                         log_error_errno(errno, "Failed to execute " MOUNT_PATH ": %m");
                         _exit(EXIT_FAILURE);
@@ -115,20 +111,19 @@ int main(int argc, char *argv[]) {
 
                 s = strdup(me->mnt_dir);
                 if (!s) {
-                        log_oom();
-                        ret = EXIT_FAILURE;
-                        continue;
+                        r = log_oom();
+                        goto finish;
                 }
 
-
                 k = hashmap_put(pids, PID_TO_PTR(pid), s);
                 if (k < 0) {
-                        log_error_errno(k, "Failed to add PID to set: %m");
-                        ret = EXIT_FAILURE;
-                        continue;
+                        free(s);
+                        r = log_oom();
+                        goto finish;
                 }
         }
 
+        r = 0;
         while (!hashmap_isempty(pids)) {
                 siginfo_t si = {};
                 char *s;
@@ -138,9 +133,8 @@ int main(int argc, char *argv[]) {
                         if (errno == EINTR)
                                 continue;
 
-                        log_error_errno(errno, "waitid() failed: %m");
-                        ret = EXIT_FAILURE;
-                        break;
+                        r = log_error_errno(errno, "waitid() failed: %m");
+                        goto finish;
                 }
 
                 s = hashmap_remove(pids, PID_TO_PTR(si.si_pid));
@@ -151,7 +145,7 @@ int main(int argc, char *argv[]) {
                                 else
                                         log_error(MOUNT_PATH " for %s terminated by signal %s.", s, signal_to_string(si.si_status));
 
-                                ret = EXIT_FAILURE;
+                                r = -ENOEXEC;
                         }
 
                         free(s);
@@ -159,9 +153,5 @@ int main(int argc, char *argv[]) {
         }
 
 finish:
-
-        if (pids)
-                hashmap_free_free(pids);
-
-        return ret;
+        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }