]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
uniformly nullify std fds
authorTycho Andersen <tycho.andersen@canonical.com>
Wed, 10 Jun 2015 21:57:50 +0000 (21:57 +0000)
committerStéphane Graber <stgraber@ubuntu.com>
Fri, 28 Aug 2015 22:02:16 +0000 (18:02 -0400)
In various places throughout the code, we want to "nullify" the std fds,
opening them to /dev/null or zero or so. Instead, let's unify this code and do
it in such a way that Coverity (probably) won't complain.

v2: use /dev/null for stdin as well
v3: add a comment about use of C's short circuiting
v4: axe comment, check errors on dup2, s/quiet/need_null_stdfds

Reported-by: Coverity
Signed-off-by: Tycho Andersen <tycho.andersen@canonical.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
src/lxc/bdev.c
src/lxc/lxccontainer.c
src/lxc/monitor.c
src/lxc/utils.c
src/lxc/utils.h

index f9891c3aa5374ed459eeeb32566e07f3b601cd9d..c02bf863197faf2a2b8ec4faac0e9ffee804f5a3 100644 (file)
@@ -227,12 +227,8 @@ static int do_mkfs(const char *path, const char *fstype)
 
        // If the file is not a block device, we don't want mkfs to ask
        // us about whether to proceed.
-       close(0);
-       close(1);
-       close(2);
-       open("/dev/zero", O_RDONLY);
-       open("/dev/null", O_RDWR);
-       open("/dev/null", O_RDWR);
+       if (null_stdfds() < 0)
+               exit(1);
        execlp("mkfs", "mkfs", "-t", fstype, path, NULL);
        exit(1);
 }
index dfc255334996d3651932a3053fd6b6cae2c34256..c2c8b12d7553cb85e033620b6e267bc77505b25c 100644 (file)
@@ -623,12 +623,10 @@ static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv
                        return false;
                }
                lxc_check_inherited(conf, -1);
-               close(0);
-               close(1);
-               close(2);
-               open("/dev/zero", O_RDONLY);
-               open("/dev/null", O_RDWR);
-               open("/dev/null", O_RDWR);
+               if (null_stdfds() < 0) {
+                       ERROR("failed to close fds");
+                       return false;
+               }
                setsid();
        } else {
                if (!am_single_threaded()) {
@@ -838,7 +836,7 @@ static char *lxcbasename(char *path)
        return p;
 }
 
-static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet,
+static bool create_run_template(struct lxc_container *c, char *tpath, bool need_null_stdfds,
                                char *const argv[])
 {
        pid_t pid;
@@ -860,13 +858,8 @@ static bool create_run_template(struct lxc_container *c, char *tpath, bool quiet
                char **newargv;
                struct lxc_conf *conf = c->lxc_conf;
 
-               if (quiet) {
-                       close(0);
-                       close(1);
-                       close(2);
-                       open("/dev/zero", O_RDONLY);
-                       open("/dev/null", O_RDWR);
-                       open("/dev/null", O_RDWR);
+               if (need_null_stdfds && null_stdfds() < 0) {
+                       exit(1);
                }
 
                src = c->lxc_conf->rootfs.path;
index ada9a0b4978a63fcc93d0d7ff9e0ffe6c1d58087..700aaa88f22df96d7f06772716ea4a18bbdd9bc0 100644 (file)
@@ -329,12 +329,8 @@ int lxc_monitord_spawn(const char *lxcpath)
                exit(EXIT_FAILURE);
        }
        lxc_check_inherited(NULL, pipefd[1]);
-       close(0);
-       close(1);
-       close(2);
-       open("/dev/null", O_RDONLY);
-       open("/dev/null", O_RDWR);
-       open("/dev/null", O_RDWR);
+       if (null_stdfds() < 0)
+               exit(EXIT_FAILURE);
        close(pipefd[0]);
        sprintf(pipefd_str, "%d", pipefd[1]);
        execvp(args[0], args);
index e921c63e43a1b72a255688607a892d405a1d9c31..d6bb882d99530a0d6aad9e462bdb31ad318077f2 100644 (file)
@@ -1122,3 +1122,24 @@ char *get_template_path(const char *t)
 
        return tpath;
 }
+
+int null_stdfds(void)
+{
+       int fd, ret = -1;
+
+       fd = open("/dev/null", O_RDWR);
+       if (fd < 0)
+               return -1;
+
+       if (dup2(fd, 0) < 0)
+               goto err;
+       if (dup2(fd, 1) < 0)
+               goto err;
+       if (dup2(fd, 2) < 0)
+               goto err;
+
+       ret = 0;
+err:
+       close(fd);
+       return ret;
+}
index d6de78a13a3b444fad00aa5ea5c09a2957364918..378c9d5691e66a73765c57110d1236ad2842a6c9 100644 (file)
@@ -273,4 +273,5 @@ int detect_shared_rootfs(void);
 int detect_ramfs_rootfs(void);
 char *on_path(char *cmd);
 char *get_template_path(const char *t);
+int null_stdfds(void);
 #endif /* __LXC_UTILS_H */