blob: 80022b39047da3b24f523927aabe863c10af533c [file] [log] [blame]
Eric Andersen35e643b2003-07-28 07:40:39 +00001/*
Eric Andersenaff114c2004-04-14 17:51:38 +00002 * Rexec program for system have fork() as vfork() with foreground option
Russ Dilla1fece22003-12-15 21:57:44 +00003 *
4 * Copyright (C) Vladimir N. Oleynik <dzo@simtreas.ru>
5 * Copyright (C) 2003 Russ Dill <Russ.Dill@asu.edu>
6 *
Eric Andersenaff114c2004-04-14 17:51:38 +00007 * daemon() portion taken from uClibc:
Russ Dilla1fece22003-12-15 21:57:44 +00008 *
9 * Copyright (c) 1991, 1993
10 * The Regents of the University of California. All rights reserved.
11 *
12 * Modified for uClibc by Erik Andersen <andersee@debian.org>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Eric Andersen35e643b2003-07-28 07:40:39 +000027 */
28
29#include <unistd.h>
Russ Dilla1fece22003-12-15 21:57:44 +000030#include <stdio.h>
31#include <fcntl.h>
32#include <paths.h>
Eric Andersen35e643b2003-07-28 07:40:39 +000033#include "libbb.h"
34
35
36#if defined(__uClinux__)
Russ Dilla1fece22003-12-15 21:57:44 +000037void vfork_daemon_rexec(int nochdir, int noclose,
38 int argc, char **argv, char *foreground_opt)
Eric Andersen35e643b2003-07-28 07:40:39 +000039{
Russ Dilla1fece22003-12-15 21:57:44 +000040 int fd;
Eric Andersen35e643b2003-07-28 07:40:39 +000041 char **vfork_args;
42 int a = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000043
Russ Dilla1fece22003-12-15 21:57:44 +000044 setsid();
Eric Andersenc7bda1c2004-03-15 08:29:22 +000045
Russ Dilla1fece22003-12-15 21:57:44 +000046 if (!nochdir)
47 chdir("/");
48
49 if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
50 dup2(fd, STDIN_FILENO);
51 dup2(fd, STDOUT_FILENO);
52 dup2(fd, STDERR_FILENO);
53 if (fd > 2)
54 close(fd);
55 }
Eric Andersen35e643b2003-07-28 07:40:39 +000056
57 vfork_args = xcalloc(sizeof(char *), argc + 3);
Russ Dilla1fece22003-12-15 21:57:44 +000058 vfork_args[a++] = "/bin/busybox";
Eric Andersen35e643b2003-07-28 07:40:39 +000059 while(*argv) {
60 vfork_args[a++] = *argv;
61 argv++;
62 }
63 vfork_args[a] = foreground_opt;
Russ Dilla1fece22003-12-15 21:57:44 +000064 switch (vfork()) {
65 case 0: /* child */
66 /* Make certain we are not a session leader, or else we
67 * might reacquire a controlling terminal */
68 if (vfork())
69 _exit(0);
70 execv(vfork_args[0], vfork_args);
71 bb_perror_msg_and_die("execv %s", vfork_args[0]);
72 case -1: /* error */
73 bb_perror_msg_and_die("vfork");
74 default: /* parent */
75 exit(0);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000076 }
Eric Andersen35e643b2003-07-28 07:40:39 +000077}
78#endif /* uClinux */