Bernhard Reutner-Fischer | 0a8812b | 2006-05-19 13:12:21 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 3 | * Rexec program for system have fork() as vfork() with foreground option |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) Vladimir N. Oleynik <dzo@simtreas.ru> |
| 6 | * Copyright (C) 2003 Russ Dill <Russ.Dill@asu.edu> |
| 7 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 8 | * daemon() portion taken from uClibc: |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 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 | * |
Bernhard Reutner-Fischer | 0a8812b | 2006-05-19 13:12:21 +0000 | [diff] [blame] | 15 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 18 | #include <paths.h> |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 19 | #include "libbb.h" |
| 20 | |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 21 | #ifdef BB_NOMMU |
Rob Landley | d9a761d | 2006-06-16 16:35:53 +0000 | [diff] [blame] | 22 | void vfork_daemon_rexec(int nochdir, int noclose, |
| 23 | int argc, char **argv, char *foreground_opt) |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 24 | { |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 25 | int fd; |
Rob Landley | d9a761d | 2006-06-16 16:35:53 +0000 | [diff] [blame] | 26 | char **vfork_args; |
| 27 | int a = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 28 | |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 29 | setsid(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 30 | |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 31 | if (!nochdir) |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 32 | xchdir("/"); |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 33 | |
Bernhard Reutner-Fischer | 0a8812b | 2006-05-19 13:12:21 +0000 | [diff] [blame] | 34 | if (!noclose && (fd = open(bb_dev_null, O_RDWR, 0)) != -1) { |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 35 | dup2(fd, STDIN_FILENO); |
| 36 | dup2(fd, STDOUT_FILENO); |
| 37 | dup2(fd, STDERR_FILENO); |
| 38 | if (fd > 2) |
| 39 | close(fd); |
Rob Landley | d9a761d | 2006-06-16 16:35:53 +0000 | [diff] [blame] | 40 | } |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 42 | vfork_args = xzalloc(sizeof(char *) * (argc + 3)); |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 43 | vfork_args[a++] = CONFIG_BUSYBOX_EXEC_PATH; |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 44 | while (*argv) { |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 45 | vfork_args[a++] = *argv; |
| 46 | argv++; |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 47 | } |
| 48 | vfork_args[a] = foreground_opt; |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 49 | switch (vfork()) { |
| 50 | case 0: /* child */ |
| 51 | /* Make certain we are not a session leader, or else we |
| 52 | * might reacquire a controlling terminal */ |
| 53 | if (vfork()) |
| 54 | _exit(0); |
| 55 | execv(vfork_args[0], vfork_args); |
| 56 | bb_perror_msg_and_die("execv %s", vfork_args[0]); |
| 57 | case -1: /* error */ |
| 58 | bb_perror_msg_and_die("vfork"); |
| 59 | default: /* parent */ |
| 60 | exit(0); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 61 | } |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 62 | } |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 63 | #endif /* BB_NOMMU */ |