udhcpc: fix a problem with binary-encoded options #2
[oweals/busybox.git] / libbb / vfork_daemon_rexec.c
index af938bed3b732bea371640894ac3342505197849..ed1f86f0c5748383ba63b9c95253de72532e631e 100644 (file)
@@ -52,6 +52,7 @@ pid_t FAST_FUNC spawn(char **argv)
         * Interested party can wait on pid and learn exit code.
         * If 111 - then it (most probably) failed to exec */
        if (failed) {
+               safe_waitpid(pid, NULL, 0); /* prevent zombie */
                errno = failed;
                return -1;
        }
@@ -252,11 +253,19 @@ void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
        if (!(flags & DAEMON_ONLY_SANITIZE)) {
                if (fork_or_rexec(argv))
                        exit(EXIT_SUCCESS); /* parent */
-               /* if daemonizing, make sure we detach from stdio & ctty */
+               /* if daemonizing, detach from stdio & ctty */
                setsid();
                dup2(fd, 0);
                dup2(fd, 1);
                dup2(fd, 2);
+               if (flags & DAEMON_DOUBLE_FORK) {
+                       /* On Linux, session leader can acquire ctty
+                        * unknowingly, by opening a tty.
+                        * Prevent this: stop being a session leader.
+                        */
+                       if (fork_or_rexec(argv))
+                               exit(EXIT_SUCCESS); /* parent */
+               }
        }
        while (fd > 2) {
                close(fd--);