openvt,getty,vfork_daemon_rexec,mount: tighten up fd cleanup code
[oweals/busybox.git] / libbb / vfork_daemon_rexec.c
index 0601121ad10d0df22fc64ab5b29a6193075cc7b1..26d1826e015518b0e750ccb8d8a389accd5b39b6 100644 (file)
@@ -1,39 +1,24 @@
+/* vi: set sw=4 ts=4: */
 /*
- * Rexec program for system have fork() as vfork() with foregound option
+ * Rexec program for system have fork() as vfork() with foreground option
  *
  * Copyright (C) Vladimir N. Oleynik <dzo@simtreas.ru>
  * Copyright (C) 2003 Russ Dill <Russ.Dill@asu.edu>
  *
- * daemon() portion taken from uclibc:
+ * daemon() portion taken from uClibc:
  *
  * Copyright (c) 1991, 1993
  *      The Regents of the University of California.  All rights reserved.
  *
  * Modified for uClibc by Erik Andersen <andersee@debian.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * 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"
 
-
-#if defined(__uClinux__)
+#ifdef BB_NOMMU
 void vfork_daemon_rexec(int nochdir, int noclose,
                int argc, char **argv, char *foreground_opt)
 {
@@ -44,21 +29,21 @@ void vfork_daemon_rexec(int nochdir, int noclose,
        setsid();
 
        if (!nochdir)
-               chdir("/");
+               xchdir("/");
 
-       if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
+       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()) {
@@ -75,4 +60,4 @@ void vfork_daemon_rexec(int nochdir, int noclose,
                exit(0);
        }
 }
-#endif /* uClinux */
+#endif /* BB_NOMMU */