]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
utils: duplicate stderr as well in lxc_popen()
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 13 Sep 2017 03:25:22 +0000 (05:25 +0200)
committerStéphane Graber <stgraber@ubuntu.com>
Wed, 4 Oct 2017 23:17:11 +0000 (19:17 -0400)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/utils.c

index 922595c40883e291c498b38c63e68dfef92b3947..a9d39cbf8b827a9616932656d0cb1cb036010d59 100644 (file)
@@ -417,6 +417,7 @@ const char** lxc_va_arg_list_to_argv_const(va_list ap, size_t skip)
 
 extern struct lxc_popen_FILE *lxc_popen(const char *command)
 {
+       int ret;
        struct lxc_popen_FILE *fp = NULL;
        int parent_end = -1, child_end = -1;
        int pipe_fds[2];
@@ -436,24 +437,40 @@ extern struct lxc_popen_FILE *lxc_popen(const char *command)
 
        if (child_pid == 0) {
                /* child */
-               int child_std_end = STDOUT_FILENO;
 
                close(parent_end);
 
-               if (child_end != child_std_end) {
+               if (child_end != STDOUT_FILENO) {
                        /* dup2() doesn't dup close-on-exec flag */
-                       dup2(child_end, child_std_end);
-
-                       /* it's safe not to close child_end here
-                        * as it's marked close-on-exec anyway
+                       ret = dup2(child_end, STDOUT_FILENO);
+                       if (ret < 0)
+                               WARN("Failed to duplicate stdout fd");
+               } else {
+                       /*
+                        * The descriptor is already the one we will use.
+                        * But it must not be marked close-on-exec.
+                        * Undo the effects.
                         */
+                       ret = fcntl(child_end, F_SETFD, 0);
+                       if (ret < 0) {
+                               SYSERROR("Failed to remove FD_CLOEXEC from fd.");
+                               exit(127);
+                       }
+               }
+
+               if (child_end != STDERR_FILENO) {
+                       /* dup2() doesn't dup close-on-exec flag */
+                       ret = dup2(child_end, STDERR_FILENO);
+                       if (ret < 0)
+                               WARN("Failed to duplicate stdout fd");
                } else {
                        /*
                         * The descriptor is already the one we will use.
                         * But it must not be marked close-on-exec.
                         * Undo the effects.
                         */
-                       if (fcntl(child_end, F_SETFD, 0) != 0) {
+                       ret = fcntl(child_end, F_SETFD, 0);
+                       if (ret < 0) {
                                SYSERROR("Failed to remove FD_CLOEXEC from fd.");
                                exit(127);
                        }