chiark / gitweb /
nspawn: use automatic cleanup for umask
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 16 Sep 2012 14:14:11 +0000 (16:14 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 16 Sep 2012 14:20:09 +0000 (16:20 +0200)
src/nspawn/nspawn.c
src/shared/macro.h
src/shared/util.c
src/shared/util.h

index 0220ec4b255475bd5fd7d62bf069945f3b253a86..b494045b8853212a188f9d5ba25ca25aa93ccd75 100644 (file)
@@ -435,7 +435,7 @@ static int copy_devnodes(const char *dest) {
 
         const char *d;
         int r = 0;
-        mode_t u;
+        mode_t _cleanup_umask_ u;
 
         assert(dest);
 
@@ -479,8 +479,6 @@ static int copy_devnodes(const char *dest) {
                 }
         }
 
-        umask(u);
-
         return r;
 }
 
@@ -488,7 +486,7 @@ static int setup_dev_console(const char *dest, const char *console) {
         struct stat st;
         char _cleanup_free_ *to = NULL;
         int r;
-        mode_t u;
+        mode_t _cleanup_umask_ u;
 
         assert(dest);
         assert(console);
@@ -497,25 +495,21 @@ static int setup_dev_console(const char *dest, const char *console) {
 
         if (stat(console, &st) < 0) {
                 log_error("Failed to stat %s: %m", console);
-                r = -errno;
-                goto finish;
+                return -errno;
 
         } else if (!S_ISCHR(st.st_mode)) {
-                log_error("/dev/console is not a char device.");
-                r = -EIO;
-                goto finish;
+                log_error("/dev/console is not a char device");
+                return -EIO;
         }
 
         r = chmod_and_chown(console, 0600, 0, 0);
         if (r < 0) {
                 log_error("Failed to correct access mode for TTY: %s", strerror(-r));
-                goto finish;
+                return r;
         }
 
-        if (asprintf(&to, "%s/dev/console", dest) < 0) {
-                r = log_oom();
-                goto finish;
-        }
+        if (asprintf(&to, "%s/dev/console", dest) < 0)
+                return log_oom();
 
         /* We need to bind mount the right tty to /dev/console since
          * ptys can only exist on pts file systems. To have something
@@ -526,26 +520,21 @@ static int setup_dev_console(const char *dest, const char *console) {
 
         if (mknod(to, (st.st_mode & ~07777) | 0600, st.st_rdev) < 0) {
                 log_error("mknod() for /dev/console failed: %m");
-                r = -errno;
-                goto finish;
+                return -errno;
         }
 
         if (mount(console, to, "bind", MS_BIND, NULL) < 0) {
                 log_error("Bind mount for /dev/console failed: %m");
-                r = -errno;
-                goto finish;
+                return -errno;
         }
 
-finish:
-        umask(u);
-
-        return r;
+        return 0;
 }
 
 static int setup_kmsg(const char *dest, int kmsg_socket) {
         char _cleanup_free_ *from = NULL, *to = NULL;
         int r, fd, k;
-        mode_t u;
+        mode_t _cleanup_umask_ u;
         union {
                 struct cmsghdr cmsghdr;
                 uint8_t buf[CMSG_SPACE(sizeof(int))];
@@ -565,39 +554,30 @@ static int setup_kmsg(const char *dest, int kmsg_socket) {
          * that writing blocks when nothing is reading. In order to
          * avoid any problems with containers deadlocking due to this
          * we simply make /dev/kmsg unavailable to the container. */
-        if (asprintf(&from, "%s/dev/kmsg", dest) < 0) {
-                r = log_oom();
-                goto finish;
-        }
-
-        if (asprintf(&to, "%s/proc/kmsg", dest) < 0) {
-                r = log_oom();
-                goto finish;
-        }
+        if (asprintf(&from, "%s/dev/kmsg", dest) < 0 ||
+            asprintf(&to, "%s/proc/kmsg", dest) < 0)
+                return log_oom();
 
         if (mkfifo(from, 0600) < 0) {
                 log_error("mkfifo() for /dev/kmsg failed: %m");
-                r = -errno;
-                goto finish;
+                return -errno;
         }
 
         r = chmod_and_chown(from, 0600, 0, 0);
         if (r < 0) {
                 log_error("Failed to correct access mode for /dev/kmsg: %s", strerror(-r));
-                goto finish;
+                return r;
         }
 
         if (mount(from, to, "bind", MS_BIND, NULL) < 0) {
                 log_error("Bind mount for /proc/kmsg failed: %m");
-                r = -errno;
-                goto finish;
+                return -errno;
         }
 
         fd = open(from, O_RDWR|O_NDELAY|O_CLOEXEC);
         if (fd < 0) {
                 log_error("Failed to open fifo: %m");
-                r = -errno;
-                goto finish;
+                return -errno;
         }
 
         zero(mh);
@@ -621,17 +601,12 @@ static int setup_kmsg(const char *dest, int kmsg_socket) {
 
         if (k < 0) {
                 log_error("Failed to send FIFO fd: %m");
-                r = -errno;
-                goto finish;
+                return -errno;
         }
 
         /* And now make the FIFO unavailable as /dev/kmsg... */
         unlink(from);
-
-finish:
-        umask(u);
-
-        return r;
+        return 0;
 }
 
 static int setup_hostname(void) {
index f8c5656538ac9c41f341f4dabaffd73cca74feec..c7ce7c87d096079fdc1ff0f6c3a60582a2407197 100644 (file)
@@ -191,5 +191,6 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
 #define _cleanup_fclose_ __attribute__((cleanup(fclosep)))
 #define _cleanup_close_ __attribute__((cleanup(closep)))
 #define _cleanup_closedir_ __attribute__((cleanup(closedirp)))
+#define _cleanup_umask_ __attribute__((cleanup(umaskp)))
 
 #include "log.h"
index add3fdce53906bc89441facbbfdcc248f4c0785a..27b6683393c3fb257a9237b0996af16bd1a8074d 100644 (file)
@@ -5827,3 +5827,7 @@ void closedirp(DIR **d) {
         if (*d)
                 closedir(*d);
 }
+
+void umaskp(mode_t *u) {
+        umask(*u);
+}
index 2b75ba62f97f459e422f7ae8e9e372548e70ac83..1a607cf37c3599bc5729df984a1a55edd7badde3 100644 (file)
@@ -31,6 +31,7 @@
 #include <signal.h>
 #include <sched.h>
 #include <limits.h>
+#include <sys/types.h>
 #include <sys/stat.h>
 #include <dirent.h>
 #include <sys/resource.h>
@@ -533,3 +534,4 @@ void freep(void *p);
 void fclosep(FILE **f);
 void closep(int *fd);
 void closedirp(DIR **d);
+void umaskp(mode_t *u);