sometimes daemonizing and vfork()/exit() is ok
[platform/upstream/busybox.git] / libbb / vfork_daemon_rexec.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Rexec program for system have fork() as vfork() with foreground option
4  *
5  * Copyright (C) Vladimir N. Oleynik <dzo@simtreas.ru>
6  * Copyright (C) 2003 Russ Dill <Russ.Dill@asu.edu>
7  *
8  * daemon() portion taken from uClibc:
9  *
10  * Copyright (c) 1991, 1993
11  *      The Regents of the University of California.  All rights reserved.
12  *
13  * Modified for uClibc by Erik Andersen <andersee@debian.org>
14  *
15  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
16  */
17
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <paths.h>
22 #include "libbb.h"
23
24
25 #ifdef BB_NOMMU
26 static void vfork_daemon_common(int nochdir, int noclose)
27 {
28         int fd;
29
30         setsid();
31
32         if (!nochdir)
33                 chdir("/");
34
35         if (!noclose && (fd = open(bb_dev_null, O_RDWR, 0)) != -1) {
36                 dup2(fd, STDIN_FILENO);
37                 dup2(fd, STDOUT_FILENO);
38                 dup2(fd, STDERR_FILENO);
39                 if (fd > 2)
40                         close(fd);
41         }       
42 }
43
44 void vfork_daemon(int nochdir, int noclose)
45 {
46         vfork_daemon_common(nochdir, noclose);
47
48         if (vfork())
49                 exit(0);
50 }
51
52 void vfork_daemon_rexec(int nochdir, int noclose,
53                 int argc, char **argv, char *foreground_opt)
54 {
55         char **vfork_args;
56         int a = 0;
57
58         vfork_daemon_common(nochdir, noclose);
59
60         vfork_args = xcalloc(sizeof(char *), argc + 3);
61         vfork_args[a++] = "/bin/busybox";
62         while(*argv) {
63             vfork_args[a++] = *argv;
64             argv++;
65         }
66         vfork_args[a] = foreground_opt;
67         switch (vfork()) {
68         case 0: /* child */
69                 /* Make certain we are not a session leader, or else we
70                  * might reacquire a controlling terminal */
71                 if (vfork())
72                         _exit(0);
73                 execv(vfork_args[0], vfork_args);
74                 bb_perror_msg_and_die("execv %s", vfork_args[0]);
75         case -1: /* error */
76                 bb_perror_msg_and_die("vfork");
77         default: /* parent */
78                 exit(0);
79         }
80 }
81 #endif /* BB_NOMMU */