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 |
Mike Frysinger | 6fb5847 | 2006-06-07 21:48:30 +0000 | [diff] [blame^] | 26 | static void vfork_daemon_common(int nochdir, int noclose) |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 27 | { |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 28 | int fd; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 29 | |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 30 | setsid(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 31 | |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 32 | if (!nochdir) |
| 33 | chdir("/"); |
| 34 | |
Bernhard Reutner-Fischer | 0a8812b | 2006-05-19 13:12:21 +0000 | [diff] [blame] | 35 | if (!noclose && (fd = open(bb_dev_null, O_RDWR, 0)) != -1) { |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 36 | dup2(fd, STDIN_FILENO); |
| 37 | dup2(fd, STDOUT_FILENO); |
| 38 | dup2(fd, STDERR_FILENO); |
| 39 | if (fd > 2) |
| 40 | close(fd); |
Mike Frysinger | 6fb5847 | 2006-06-07 21:48:30 +0000 | [diff] [blame^] | 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); |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 59 | |
| 60 | vfork_args = xcalloc(sizeof(char *), argc + 3); |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 61 | vfork_args[a++] = "/bin/busybox"; |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 62 | while(*argv) { |
| 63 | vfork_args[a++] = *argv; |
| 64 | argv++; |
| 65 | } |
| 66 | vfork_args[a] = foreground_opt; |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 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); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 79 | } |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 80 | } |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 81 | #endif /* BB_NOMMU */ |