free,stat: make NOEXEC
[oweals/busybox.git] / libbb / vfork_daemon_rexec.c
index 487ecb0e4ffeb4b03e45bb01efe644b5765b04e5..50ecea762ead413e48d4c3d27dacb08ae5982aa9 100644 (file)
@@ -143,6 +143,8 @@ int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
                applet_name = tmp_argv[0];
                /* Finally we can call NOFORK applet's main() */
                rc = applet_main[applet_no](argc, tmp_argv);
+               /* Important for shells: `which CMD` was failing */
+               fflush_all();
        } else {
                /* xfunc died in NOFORK applet */
        }
@@ -173,6 +175,8 @@ int FAST_FUNC spawn_and_wait(char **argv)
                                return wait4pid(rc);
 
                        /* child */
+//TODO: prctl(PR_SET_NAME, (long)argv[0], 0, 0, 0);? [think pidof, pgrep, pkill]
+//Rewrite /proc/PID/cmdline? (need to save argv0 and length at init for this to work!)
                        /* reset some state and run without execing */
 
                        /* msg_eol = "\n"; - no caller needs this reinited yet */
@@ -207,6 +211,9 @@ pid_t FAST_FUNC fork_or_rexec(char **argv)
        /* Maybe we are already re-execed and come here again? */
        if (re_execed)
                return 0;
+
+       /* fflush_all(); ? - so far all callers had no buffered output to flush */
+
        pid = xvfork();
        if (pid) /* parent */
                return pid;
@@ -243,8 +250,11 @@ void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
                fd = dup(fd); /* have 0,1,2 open at least to /dev/null */
 
        if (!(flags & DAEMON_ONLY_SANITIZE)) {
+
+               /* fflush_all(); - add it in fork_or_rexec() if necessary */
+
                if (fork_or_rexec(argv))
-                       exit(EXIT_SUCCESS); /* parent */
+                       _exit(EXIT_SUCCESS); /* parent */
                /* if daemonizing, detach from stdio & ctty */
                setsid();
                dup2(fd, 0);
@@ -256,7 +266,7 @@ void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
                         * Prevent this: stop being a session leader.
                         */
                        if (fork_or_rexec(argv))
-                               exit(EXIT_SUCCESS); /* parent */
+                               _exit(EXIT_SUCCESS); /* parent */
                }
        }
        while (fd > 2) {