netifd: fix process stdin/stdout/stderr when the pipe fds overlap with standard fds
authorFelix Fietkau <nbd@openwrt.org>
Sun, 1 Dec 2013 09:28:52 +0000 (10:28 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 1 Dec 2013 09:28:52 +0000 (10:28 +0100)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
main.c

diff --git a/main.c b/main.c
index 92c6f4969695d696fbb5952efac552bd7b20b516..d8cb17f40658f8c7a6b777270a0eb08c4c239df9 100644 (file)
--- a/main.c
+++ b/main.c
@@ -143,6 +143,8 @@ netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
                goto error;
 
        if (!pid) {
+               int i;
+
                if (env) {
                        while (*env) {
                                putenv(*env);
@@ -152,12 +154,17 @@ netifd_start_process(const char **argv, char **env, struct netifd_process *proc)
                if (proc->dir_fd >= 0)
                        fchdir(proc->dir_fd);
 
-               dup2(pfds[1], 0);
-               dup2(pfds[1], 1);
-               dup2(pfds[1], 2);
-
                close(pfds[0]);
-               close(pfds[1]);
+
+               for (i = 0; i <= 2; i++) {
+                       if (pfds[1] == i)
+                               continue;
+
+                       dup2(pfds[1], i);
+               }
+
+               if (pfds[1] > 2)
+                       close(pfds[1]);
 
                execvp(argv[0], (char **) argv);
                exit(127);