free,stat: make NOEXEC
[oweals/busybox.git] / libbb / vfork_daemon_rexec.c
index c192829b55fca8388f3f356a94c15d2314dd6e03..50ecea762ead413e48d4c3d27dacb08ae5982aa9 100644 (file)
@@ -16,6 +16,7 @@
  */
 
 #include "busybox.h" /* uses applet tables */
+#include "NUM_APPLETS.h"
 
 /* This does a fork/exec in one call, using vfork().  Returns PID of new child,
  * -1 for failure.  Runs argv[0], searching path if that has no / in it. */
@@ -91,6 +92,7 @@ struct nofork_save_area {
        void (*die_func)(void);
        const char *applet_name;
        uint32_t option_mask32;
+       smallint logmode;
        uint8_t xfunc_error_retval;
 };
 static void save_nofork_data(struct nofork_save_area *save)
@@ -99,6 +101,7 @@ static void save_nofork_data(struct nofork_save_area *save)
        save->die_func = die_func;
        save->applet_name = applet_name;
        save->option_mask32 = option_mask32;
+       save->logmode = logmode;
        save->xfunc_error_retval = xfunc_error_retval;
 }
 static void restore_nofork_data(struct nofork_save_area *save)
@@ -107,6 +110,7 @@ static void restore_nofork_data(struct nofork_save_area *save)
        die_func = save->die_func;
        applet_name = save->applet_name;
        option_mask32 = save->option_mask32;
+       logmode = save->logmode;
        xfunc_error_retval = save->xfunc_error_retval;
 }
 
@@ -117,32 +121,12 @@ int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
 
        save_nofork_data(&old);
 
+       logmode = LOGMODE_STDIO;
        xfunc_error_retval = EXIT_FAILURE;
-
        /* In case getopt() or getopt32() was already called:
         * reset the libc getopt() function, which keeps internal state.
-        *
-        * BSD-derived getopt() functions require that optind be set to 1 in
-        * order to reset getopt() state.  This used to be generally accepted
-        * way of resetting getopt().  However, glibc's getopt()
-        * has additional getopt() state beyond optind, and requires that
-        * optind be set to zero to reset its state.  So the unfortunate state of
-        * affairs is that BSD-derived versions of getopt() misbehave if
-        * optind is set to 0 in order to reset getopt(), and glibc's getopt()
-        * will core dump if optind is set 1 in order to reset getopt().
-        *
-        * More modern versions of BSD require that optreset be set to 1 in
-        * order to reset getopt().  Sigh.  Standards, anyone?
         */
-#ifdef __GLIBC__
-       optind = 0;
-#else /* BSD style */
-       optind = 1;
-       /* optreset = 1; */
-#endif
-       /* optarg = NULL; opterr = 1; optopt = 63; - do we need this too? */
-       /* (values above are what they initialized to in glibc and uclibc) */
-       /* option_mask32 = 0; - not needed, no applet depends on it being 0 */
+       GETOPT_RESET();
 
        argc = 1;
        while (argv[argc])
@@ -159,19 +143,16 @@ 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 */
        }
 
        /* Restoring some globals */
        restore_nofork_data(&old);
-
        /* Other globals can be simply reset to defaults */
-#ifdef __GLIBC__
-       optind = 0;
-#else /* BSD style */
-       optind = 1;
-#endif
+       GETOPT_RESET();
 
        return rc & 0xff; /* don't confuse people with "exitcodes" >255 */
 }
@@ -180,29 +161,33 @@ int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
 int FAST_FUNC spawn_and_wait(char **argv)
 {
        int rc;
-#if ENABLE_FEATURE_PREFER_APPLETS
+#if ENABLE_FEATURE_PREFER_APPLETS && (NUM_APPLETS > 1)
        int a = find_applet_by_name(argv[0]);
 
-       if (a >= 0 && (APPLET_IS_NOFORK(a)
-# if BB_MMU
-                       || APPLET_IS_NOEXEC(a) /* NOEXEC trick needs fork() */
-# endif
-       )) {
-# if BB_MMU
+       if (a >= 0) {
                if (APPLET_IS_NOFORK(a))
-# endif
-               {
                        return run_nofork_applet(a, argv);
+# if BB_MMU /* NOEXEC needs fork(), thus this is done only on MMU machines: */
+               if (APPLET_IS_NOEXEC(a)) {
+                       fflush_all();
+                       rc = fork();
+                       if (rc) /* parent or error */
+                               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 */
+                       logmode = LOGMODE_STDIO;
+                       /* die_func = NULL; - needed if the caller is a shell,
+                        * init, or a NOFORK applet. But none of those call us
+                        * as of yet (and that should probably always stay true).
+                        */
+                       /* xfunc_error_retval and applet_name are init by: */
+                       run_applet_no_and_exit(a, argv[0], argv);
                }
-# if BB_MMU
-               /* MMU only */
-               /* a->noexec is true */
-               rc = fork();
-               if (rc) /* parent or error */
-                       return wait4pid(rc);
-               /* child */
-               xfunc_error_retval = EXIT_FAILURE;
-               run_applet_no_and_exit(a, argv);
 # endif
        }
 #endif /* FEATURE_PREFER_APPLETS */
@@ -226,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;
@@ -262,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);
@@ -275,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) {