blob: c96cd61a5b3b827846c2a454964b7931490a58bc [file] [log] [blame]
Bernhard Reutner-Fischer0a8812b2006-05-19 13:12:21 +00001/* vi: set sw=4 ts=4: */
Eric Andersen35e643b2003-07-28 07:40:39 +00002/*
Eric Andersenaff114c2004-04-14 17:51:38 +00003 * Rexec program for system have fork() as vfork() with foreground option
Russ Dilla1fece22003-12-15 21:57:44 +00004 *
5 * Copyright (C) Vladimir N. Oleynik <dzo@simtreas.ru>
6 * Copyright (C) 2003 Russ Dill <Russ.Dill@asu.edu>
7 *
Eric Andersenaff114c2004-04-14 17:51:38 +00008 * daemon() portion taken from uClibc:
Russ Dilla1fece22003-12-15 21:57:44 +00009 *
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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020015 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen35e643b2003-07-28 07:40:39 +000016 */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000017#include "busybox.h" /* uses applet tables */
Denys Vlasenkod4e4fdb2017-07-03 21:31:16 +020018#include "NUM_APPLETS.h"
Eric Andersen35e643b2003-07-28 07:40:39 +000019
Denys Vlasenko8858a982017-08-08 01:21:49 +020020/*
21 * NOFORK support
22 */
Denys Vlasenko0fb00452016-07-22 18:48:38 +020023#if ENABLE_FEATURE_PREFER_APPLETS \
24 || ENABLE_FEATURE_SH_NOFORK
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020025static jmp_buf die_jmp;
26static void jump(void)
27{
28 /* Special case. We arrive here if NOFORK applet
29 * calls xfunc, which then decides to die.
30 * We don't die, but jump instead back to caller.
31 * NOFORK applets still cannot carelessly call xfuncs:
32 * p = xmalloc(10);
33 * q = xmalloc(10); // BUG! if this dies, we leak p!
34 */
35 /* | 0x100 allows to pass zero exitcode (longjmp can't pass 0).
36 * This works because exitcodes are bytes,
37 * run_nofork_applet() ensures that by "& 0xff" */
38 longjmp(die_jmp, xfunc_error_retval | 0x100);
39}
40
Denys Vlasenko63073572011-02-02 19:05:25 +010041struct nofork_save_area {
42 jmp_buf die_jmp;
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020043 void (*die_func)(void);
Denys Vlasenko63073572011-02-02 19:05:25 +010044 const char *applet_name;
45 uint32_t option_mask32;
Denys Vlasenko39194f02017-08-03 19:00:01 +020046 smallint logmode;
Denys Vlasenko63073572011-02-02 19:05:25 +010047 uint8_t xfunc_error_retval;
48};
49static void save_nofork_data(struct nofork_save_area *save)
Denis Vlasenko18e19f22007-04-28 16:43:18 +000050{
Denis Vlasenkob0550012007-05-24 12:18:16 +000051 memcpy(&save->die_jmp, &die_jmp, sizeof(die_jmp));
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020052 save->die_func = die_func;
Denis Vlasenko82d38da2007-10-10 14:38:47 +000053 save->applet_name = applet_name;
Denis Vlasenko18e19f22007-04-28 16:43:18 +000054 save->option_mask32 = option_mask32;
Denys Vlasenko39194f02017-08-03 19:00:01 +020055 save->logmode = logmode;
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020056 save->xfunc_error_retval = xfunc_error_retval;
Denis Vlasenko18e19f22007-04-28 16:43:18 +000057}
Denys Vlasenko63073572011-02-02 19:05:25 +010058static void restore_nofork_data(struct nofork_save_area *save)
Denis Vlasenko18e19f22007-04-28 16:43:18 +000059{
Denis Vlasenkob0550012007-05-24 12:18:16 +000060 memcpy(&die_jmp, &save->die_jmp, sizeof(die_jmp));
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020061 die_func = save->die_func;
Denis Vlasenko82d38da2007-10-10 14:38:47 +000062 applet_name = save->applet_name;
Denis Vlasenko18e19f22007-04-28 16:43:18 +000063 option_mask32 = save->option_mask32;
Denys Vlasenko39194f02017-08-03 19:00:01 +020064 logmode = save->logmode;
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020065 xfunc_error_retval = save->xfunc_error_retval;
Denis Vlasenko18e19f22007-04-28 16:43:18 +000066}
67
Denys Vlasenko63073572011-02-02 19:05:25 +010068int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
Denis Vlasenkof5294e12007-04-14 10:09:57 +000069{
70 int rc, argc;
Denys Vlasenko63073572011-02-02 19:05:25 +010071 struct nofork_save_area old;
72
73 save_nofork_data(&old);
Denis Vlasenkof5294e12007-04-14 10:09:57 +000074
Denys Vlasenko39194f02017-08-03 19:00:01 +020075 logmode = LOGMODE_STDIO;
Denis Vlasenkof5294e12007-04-14 10:09:57 +000076 xfunc_error_retval = EXIT_FAILURE;
Denis Vlasenkod1660cb2008-10-20 07:52:33 +000077 /* In case getopt() or getopt32() was already called:
78 * reset the libc getopt() function, which keeps internal state.
Denis Vlasenkod1660cb2008-10-20 07:52:33 +000079 */
Kaarle Ritvanen835ad3a2017-04-12 00:58:46 +030080 GETOPT_RESET();
Denys Vlasenko8858a982017-08-08 01:21:49 +020081//? applet_long_options = NULL;
82//? opt_complementary = NULL;
Denis Vlasenkoc4e4be92008-01-27 22:39:55 +000083
Denys Vlasenko8858a982017-08-08 01:21:49 +020084 argc = string_array_len(argv);
Denis Vlasenkof5294e12007-04-14 10:09:57 +000085
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020086 /* If xfunc "dies" in NOFORK applet, die_func longjmp's here instead */
87 die_func = jump;
Denis Vlasenkof5294e12007-04-14 10:09:57 +000088 rc = setjmp(die_jmp);
89 if (!rc) {
90 /* Some callers (xargs)
91 * need argv untouched because they free argv[i]! */
92 char *tmp_argv[argc+1];
93 memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0]));
Ron Yorston610c4c32016-03-30 00:42:05 +020094 applet_name = tmp_argv[0];
Denis Vlasenkof5294e12007-04-14 10:09:57 +000095 /* Finally we can call NOFORK applet's main() */
Denis Vlasenko745cd172007-11-29 03:31:20 +000096 rc = applet_main[applet_no](argc, tmp_argv);
Denys Vlasenko19c9f312017-08-03 19:52:47 +020097 /* Important for shells: `which CMD` was failing */
98 fflush_all();
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020099 } else {
100 /* xfunc died in NOFORK applet */
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000101 }
102
Denis Vlasenkod1660cb2008-10-20 07:52:33 +0000103 /* Restoring some globals */
Denys Vlasenko63073572011-02-02 19:05:25 +0100104 restore_nofork_data(&old);
Denis Vlasenkod1660cb2008-10-20 07:52:33 +0000105 /* Other globals can be simply reset to defaults */
Kaarle Ritvanen835ad3a2017-04-12 00:58:46 +0300106 GETOPT_RESET();
Denys Vlasenko8858a982017-08-08 01:21:49 +0200107//? applet_long_options = NULL;
108//? opt_complementary = NULL;
Denis Vlasenkod1660cb2008-10-20 07:52:33 +0000109
Denis Vlasenko7465dbc2008-04-13 02:25:53 +0000110 return rc & 0xff; /* don't confuse people with "exitcodes" >255 */
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000111}
Denys Vlasenko0fb00452016-07-22 18:48:38 +0200112#endif /* FEATURE_PREFER_APPLETS || FEATURE_SH_NOFORK */
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000113
Denys Vlasenko8858a982017-08-08 01:21:49 +0200114/*
115 * NOEXEC support
116 */
Denys Vlasenko80e8e3c2017-08-07 19:24:57 +0200117#if (NUM_APPLETS > 1) && (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE)
118void FAST_FUNC run_noexec_applet_and_exit(int a, const char *name, char **argv)
119{
120 /* reset some state and run without execing */
121 /* msg_eol = "\n"; - no caller needs this reinited yet */
122 logmode = LOGMODE_STDIO;
123 xfunc_error_retval = EXIT_FAILURE;
124 die_func = NULL;
125 GETOPT_RESET();
Denys Vlasenko8858a982017-08-08 01:21:49 +0200126//? applet_long_options = NULL;
127//? opt_complementary = NULL;
Denys Vlasenko80e8e3c2017-08-07 19:24:57 +0200128
129//TODO: think pidof, pgrep, pkill!
130//set_task_comm() makes our pidof find NOEXECs (e.g. "yes >/dev/null"),
131//but one from procps-ng-3.3.10 needs more!
132//Rewrite /proc/PID/cmdline? (need to save argv0 and length at init for this to work!)
133 set_task_comm(name);
Denys Vlasenko8858a982017-08-08 01:21:49 +0200134 /* applet_name is set by this function: */
Denys Vlasenko80e8e3c2017-08-07 19:24:57 +0200135 run_applet_no_and_exit(a, name, argv);
136}
137#endif
138
Denys Vlasenko8858a982017-08-08 01:21:49 +0200139/*
140 * Higher-level code, hiding optional NOFORK/NOEXEC trickery.
141 */
142
143/* This does a fork/exec in one call, using vfork(). Returns PID of new child,
144 * -1 for failure. Runs argv[0], searching path if that has no / in it. */
145pid_t FAST_FUNC spawn(char **argv)
146{
147 /* Compiler should not optimize stores here */
148 volatile int failed;
149 pid_t pid;
150
151 fflush_all();
152
153 /* Be nice to nommu machines. */
154 failed = 0;
155 pid = vfork();
156 if (pid < 0) /* error */
157 return pid;
158 if (!pid) { /* child */
159 /* This macro is ok - it doesn't do NOEXEC/NOFORK tricks */
160 BB_EXECVP(argv[0], argv);
161
162 /* We are (maybe) sharing a stack with blocked parent,
163 * let parent know we failed and then exit to unblock parent
164 * (but don't run atexit() stuff, which would screw up parent.)
165 */
166 failed = errno;
167 /* mount, for example, does not want the message */
168 /*bb_perror_msg("can't execute '%s'", argv[0]);*/
169 _exit(111);
170 }
171 /* parent */
172 /* Unfortunately, this is not reliable: according to standards
173 * vfork() can be equivalent to fork() and we won't see value
174 * of 'failed'.
175 * Interested party can wait on pid and learn exit code.
176 * If 111 - then it (most probably) failed to exec */
177 if (failed) {
178 safe_waitpid(pid, NULL, 0); /* prevent zombie */
179 errno = failed;
180 return -1;
181 }
182 return pid;
183}
184
185/* Die with an error message if we can't spawn a child process. */
186pid_t FAST_FUNC xspawn(char **argv)
187{
188 pid_t pid = spawn(argv);
189 if (pid < 0)
190 bb_simple_perror_msg_and_die(*argv);
191 return pid;
192}
193
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000194int FAST_FUNC spawn_and_wait(char **argv)
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000195{
Denis Vlasenkoc6758a02007-04-10 21:40:19 +0000196 int rc;
Denys Vlasenkod4e4fdb2017-07-03 21:31:16 +0200197#if ENABLE_FEATURE_PREFER_APPLETS && (NUM_APPLETS > 1)
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000198 int a = find_applet_by_name(argv[0]);
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000199
Denys Vlasenko9967c992017-01-26 01:13:58 +0100200 if (a >= 0) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000201 if (APPLET_IS_NOFORK(a))
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000202 return run_nofork_applet(a, argv);
Denys Vlasenko9967c992017-01-26 01:13:58 +0100203# if BB_MMU /* NOEXEC needs fork(), thus this is done only on MMU machines: */
204 if (APPLET_IS_NOEXEC(a)) {
205 fflush_all();
206 rc = fork();
207 if (rc) /* parent or error */
208 return wait4pid(rc);
209
210 /* child */
Denys Vlasenko80e8e3c2017-08-07 19:24:57 +0200211 run_noexec_applet_and_exit(a, argv[0], argv);
Denis Vlasenkoc6758a02007-04-10 21:40:19 +0000212 }
Denys Vlasenkoc71b4692011-02-02 03:28:56 +0100213# endif
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000214 }
Denis Vlasenko473dae02007-04-11 07:04:23 +0000215#endif /* FEATURE_PREFER_APPLETS */
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000216 rc = spawn(argv);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000217 return wait4pid(rc);
Eric Andersen35e643b2003-07-28 07:40:39 +0000218}
Denis Vlasenkoc6758a02007-04-10 21:40:19 +0000219
Denis Vlasenko473dae02007-04-11 07:04:23 +0000220#if !BB_MMU
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000221void FAST_FUNC re_exec(char **argv)
Denis Vlasenko367960b2007-08-18 14:20:21 +0000222{
223 /* high-order bit of first char in argv[0] is a hidden
224 * "we have (already) re-execed, don't do it again" flag */
225 argv[0][0] |= 0x80;
226 execv(bb_busybox_exec_path, argv);
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200227 bb_perror_msg_and_die("can't execute '%s'", bb_busybox_exec_path);
Denis Vlasenko367960b2007-08-18 14:20:21 +0000228}
229
Denis Vlasenko83518d12009-03-20 22:17:13 +0000230pid_t FAST_FUNC fork_or_rexec(char **argv)
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000231{
232 pid_t pid;
233 /* Maybe we are already re-execed and come here again? */
234 if (re_execed)
Denys Vlasenko8f24f982009-06-07 16:02:00 +0200235 return 0;
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200236
237 /* fflush_all(); ? - so far all callers had no buffered output to flush */
238
Pascal Bellard926031b2010-07-04 15:32:38 +0200239 pid = xvfork();
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000240 if (pid) /* parent */
Denis Vlasenko83518d12009-03-20 22:17:13 +0000241 return pid;
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000242 /* child - re-exec ourself */
Denis Vlasenko367960b2007-08-18 14:20:21 +0000243 re_exec(argv);
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000244}
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000245#endif
246
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000247/* Due to a #define in libbb.h on MMU systems we actually have 1 argument -
248 * char **argv "vanishes" */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000249void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000250{
251 int fd;
252
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000253 if (flags & DAEMON_CHDIR_ROOT)
254 xchdir("/");
255
256 if (flags & DAEMON_DEVNULL_STDIO) {
257 close(0);
258 close(1);
259 close(2);
260 }
261
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000262 fd = open(bb_dev_null, O_RDWR);
263 if (fd < 0) {
264 /* NB: we can be called as bb_sanitize_stdio() from init
265 * or mdev, and there /dev/null may legitimately not (yet) exist!
266 * Do not use xopen above, but obtain _ANY_ open descriptor,
267 * even bogus one as below. */
268 fd = xopen("/", O_RDONLY); /* don't believe this can fail */
269 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000270
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000271 while ((unsigned)fd < 2)
272 fd = dup(fd); /* have 0,1,2 open at least to /dev/null */
273
274 if (!(flags & DAEMON_ONLY_SANITIZE)) {
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200275
276 /* fflush_all(); - add it in fork_or_rexec() if necessary */
277
Denis Vlasenko83518d12009-03-20 22:17:13 +0000278 if (fork_or_rexec(argv))
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200279 _exit(EXIT_SUCCESS); /* parent */
Peter Korsgaard743edac2011-11-09 19:44:37 +0100280 /* if daemonizing, detach from stdio & ctty */
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000281 setsid();
282 dup2(fd, 0);
283 dup2(fd, 1);
284 dup2(fd, 2);
Peter Korsgaard743edac2011-11-09 19:44:37 +0100285 if (flags & DAEMON_DOUBLE_FORK) {
286 /* On Linux, session leader can acquire ctty
287 * unknowingly, by opening a tty.
288 * Prevent this: stop being a session leader.
289 */
290 if (fork_or_rexec(argv))
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200291 _exit(EXIT_SUCCESS); /* parent */
Peter Korsgaard743edac2011-11-09 19:44:37 +0100292 }
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000293 }
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000294 while (fd > 2) {
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000295 close(fd--);
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000296 if (!(flags & DAEMON_CLOSE_EXTRA_FDS))
297 return;
298 /* else close everything after fd#2 */
299 }
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000300}
301
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000302void FAST_FUNC bb_sanitize_stdio(void)
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000303{
304 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE, NULL);
305}