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 | |
| 18 | #include <unistd.h> |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <paths.h> |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 22 | #include "libbb.h" |
| 23 | |
| 24 | |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 25 | #ifdef BB_NOMMU |
Rob Landley | d9a761d | 2006-06-16 16:35:53 +0000 | [diff] [blame] | 26 | void vfork_daemon_rexec(int nochdir, int noclose, |
| 27 | int argc, char **argv, char *foreground_opt) |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 28 | { |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 29 | int fd; |
Rob Landley | d9a761d | 2006-06-16 16:35:53 +0000 | [diff] [blame] | 30 | char **vfork_args; |
| 31 | int a = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 32 | |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 33 | setsid(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 34 | |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 35 | if (!nochdir) |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 36 | xchdir("/"); |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 37 | |
Bernhard Reutner-Fischer | 0a8812b | 2006-05-19 13:12:21 +0000 | [diff] [blame] | 38 | if (!noclose && (fd = open(bb_dev_null, O_RDWR, 0)) != -1) { |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 39 | dup2(fd, STDIN_FILENO); |
| 40 | dup2(fd, STDOUT_FILENO); |
| 41 | dup2(fd, STDERR_FILENO); |
| 42 | if (fd > 2) |
| 43 | close(fd); |
Rob Landley | d9a761d | 2006-06-16 16:35:53 +0000 | [diff] [blame] | 44 | } |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 45 | |
| 46 | vfork_args = xcalloc(sizeof(char *), argc + 3); |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 47 | vfork_args[a++] = CONFIG_BUSYBOX_EXEC_PATH; |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 48 | while(*argv) { |
| 49 | vfork_args[a++] = *argv; |
| 50 | argv++; |
| 51 | } |
| 52 | vfork_args[a] = foreground_opt; |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 53 | switch (vfork()) { |
| 54 | case 0: /* child */ |
| 55 | /* Make certain we are not a session leader, or else we |
| 56 | * might reacquire a controlling terminal */ |
| 57 | if (vfork()) |
| 58 | _exit(0); |
| 59 | execv(vfork_args[0], vfork_args); |
| 60 | bb_perror_msg_and_die("execv %s", vfork_args[0]); |
| 61 | case -1: /* error */ |
| 62 | bb_perror_msg_and_die("vfork"); |
| 63 | default: /* parent */ |
| 64 | exit(0); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 65 | } |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 66 | } |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 67 | #endif /* BB_NOMMU */ |