openvt,getty,vfork_daemon_rexec,mount: tighten up fd cleanup code
[oweals/busybox.git] / libbb / vfork_daemon_rexec.c
index 59a2287b023d20cc076acabcdfdd7489cb884665..26d1826e015518b0e750ccb8d8a389accd5b39b6 100644 (file)
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
-#include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
 #include <paths.h>
 #include "libbb.h"
 
-
 #ifdef BB_NOMMU
 void vfork_daemon_rexec(int nochdir, int noclose,
                int argc, char **argv, char *foreground_opt)
@@ -33,21 +29,21 @@ void vfork_daemon_rexec(int nochdir, int noclose,
        setsid();
 
        if (!nochdir)
-               chdir("/");
+               xchdir("/");
 
        if (!noclose && (fd = open(bb_dev_null, O_RDWR, 0)) != -1) {
                dup2(fd, STDIN_FILENO);
                dup2(fd, STDOUT_FILENO);
                dup2(fd, STDERR_FILENO);
-               if (fd > 2)
-                       close(fd);
+               while (fd > 2)
+                       close(fd--);
        }
 
-       vfork_args = xcalloc(sizeof(char *), argc + 3);
-       vfork_args[a++] = "/bin/busybox";
-       while(*argv) {
-           vfork_args[a++] = *argv;
-           argv++;
+       vfork_args = xzalloc(sizeof(char *) * (argc + 3));
+       vfork_args[a++] = CONFIG_BUSYBOX_EXEC_PATH;
+       while (*argv) {
+               vfork_args[a++] = *argv;
+               argv++;
        }
        vfork_args[a] = foreground_opt;
        switch (vfork()) {