openvt,getty,vfork_daemon_rexec,mount: tighten up fd cleanup code
[oweals/busybox.git] / libbb / vfork_daemon_rexec.c
index 7e4b0c09703b1195aa758a02de4a230b96f9cd1b..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)
@@ -39,15 +35,15 @@ void vfork_daemon_rexec(int nochdir, int noclose,
                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 = xzalloc(sizeof(char *) * (argc + 3));
        vfork_args[a++] = CONFIG_BUSYBOX_EXEC_PATH;
-       while(*argv) {
-           vfork_args[a++] = *argv;
-           argv++;
+       while (*argv) {
+               vfork_args[a++] = *argv;
+               argv++;
        }
        vfork_args[a] = foreground_opt;
        switch (vfork()) {