blob: 79141936aac7551dee46a5d15375ccb598596f20 [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 Vlasenkoddd1ee42017-08-08 14:09:23 +020020#define NOFORK_SUPPORT ((NUM_APPLETS > 1) && (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_NOFORK))
21#define NOEXEC_SUPPORT ((NUM_APPLETS > 1) && (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE))
22
23#if defined(__linux__) && (NUM_APPLETS > 1)
Sean MacLennand9aabfe2018-03-04 23:15:59 +010024# include <sys/prctl.h>
25# ifndef PR_SET_NAME
26# define PR_SET_NAME 15
27# endif
28# ifndef PR_GET_NAME
29# define PR_GET_NAME 16
30# endif
Denys Vlasenko5acf5e12021-10-11 18:39:25 +020031# if ENABLE_FEATURE_SH_STANDALONE || ENABLE_FEATURE_PREFER_APPLETS || !BB_MMU
32int FAST_FUNC re_execed_comm(void)
33{
34 const char *e, *expected_comm;
35 char comm[16];
36
37 BUILD_BUG_ON(CONFIG_BUSYBOX_EXEC_PATH[0] != '/');
38 e = CONFIG_BUSYBOX_EXEC_PATH;
39 /* Hopefully (strrchr(e) - e) evaluates to constant at compile time: */
40 expected_comm = bb_busybox_exec_path + (strrchr(e, '/') - e) + 1;
41
42 prctl(PR_GET_NAME, (long)comm, 0, 0, 0);
43 //bb_error_msg("comm:'%.*s' expected:'%s'", 16, comm, expected_comm);
44 return strcmp(comm, expected_comm) == 0;
45}
46# endif
Denys Vlasenkoddd1ee42017-08-08 14:09:23 +020047void FAST_FUNC set_task_comm(const char *comm)
48{
49 /* okay if too long (truncates) */
50 prctl(PR_SET_NAME, (long)comm, 0, 0, 0);
51}
52#endif
53
Denys Vlasenko8858a982017-08-08 01:21:49 +020054/*
Denys Vlasenkoddd1ee42017-08-08 14:09:23 +020055 * NOFORK/NOEXEC support
Denys Vlasenko8858a982017-08-08 01:21:49 +020056 */
Denys Vlasenkoddd1ee42017-08-08 14:09:23 +020057#if NOFORK_SUPPORT
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020058static jmp_buf die_jmp;
59static void jump(void)
60{
61 /* Special case. We arrive here if NOFORK applet
62 * calls xfunc, which then decides to die.
Denys Vlasenko036585a2017-08-08 16:38:18 +020063 * We don't die, but instead jump back to caller.
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020064 * NOFORK applets still cannot carelessly call xfuncs:
65 * p = xmalloc(10);
66 * q = xmalloc(10); // BUG! if this dies, we leak p!
67 */
68 /* | 0x100 allows to pass zero exitcode (longjmp can't pass 0).
69 * This works because exitcodes are bytes,
Denys Vlasenko036585a2017-08-08 16:38:18 +020070 * run_nofork_applet() ensures that by "& 0xff"
71 */
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020072 longjmp(die_jmp, xfunc_error_retval | 0x100);
73}
74
Denys Vlasenko63073572011-02-02 19:05:25 +010075struct nofork_save_area {
76 jmp_buf die_jmp;
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020077 void (*die_func)(void);
Denys Vlasenko63073572011-02-02 19:05:25 +010078 const char *applet_name;
79 uint32_t option_mask32;
Denys Vlasenko39194f02017-08-03 19:00:01 +020080 smallint logmode;
Denys Vlasenko63073572011-02-02 19:05:25 +010081 uint8_t xfunc_error_retval;
82};
83static void save_nofork_data(struct nofork_save_area *save)
Denis Vlasenko18e19f22007-04-28 16:43:18 +000084{
Denis Vlasenkob0550012007-05-24 12:18:16 +000085 memcpy(&save->die_jmp, &die_jmp, sizeof(die_jmp));
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020086 save->die_func = die_func;
Denis Vlasenko82d38da2007-10-10 14:38:47 +000087 save->applet_name = applet_name;
Denis Vlasenko18e19f22007-04-28 16:43:18 +000088 save->option_mask32 = option_mask32;
Denys Vlasenko39194f02017-08-03 19:00:01 +020089 save->logmode = logmode;
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020090 save->xfunc_error_retval = xfunc_error_retval;
Denis Vlasenko18e19f22007-04-28 16:43:18 +000091}
Denys Vlasenko63073572011-02-02 19:05:25 +010092static void restore_nofork_data(struct nofork_save_area *save)
Denis Vlasenko18e19f22007-04-28 16:43:18 +000093{
Denis Vlasenkob0550012007-05-24 12:18:16 +000094 memcpy(&die_jmp, &save->die_jmp, sizeof(die_jmp));
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020095 die_func = save->die_func;
Denis Vlasenko82d38da2007-10-10 14:38:47 +000096 applet_name = save->applet_name;
Denis Vlasenko18e19f22007-04-28 16:43:18 +000097 option_mask32 = save->option_mask32;
Denys Vlasenko39194f02017-08-03 19:00:01 +020098 logmode = save->logmode;
Denys Vlasenko550bf5b2015-10-09 16:42:57 +020099 xfunc_error_retval = save->xfunc_error_retval;
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000100}
101
Denys Vlasenko63073572011-02-02 19:05:25 +0100102int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000103{
104 int rc, argc;
Denys Vlasenko63073572011-02-02 19:05:25 +0100105 struct nofork_save_area old;
106
107 save_nofork_data(&old);
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000108
Denys Vlasenko39194f02017-08-03 19:00:01 +0200109 logmode = LOGMODE_STDIO;
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000110 xfunc_error_retval = EXIT_FAILURE;
Denys Vlasenko036585a2017-08-08 16:38:18 +0200111 /* In case getopt() was already called:
Denis Vlasenkod1660cb2008-10-20 07:52:33 +0000112 * reset the libc getopt() function, which keeps internal state.
Denys Vlasenko036585a2017-08-08 16:38:18 +0200113 * (getopt32() does it itself, but getopt() doesn't (and can't))
Denis Vlasenkod1660cb2008-10-20 07:52:33 +0000114 */
Kaarle Ritvanen835ad3a2017-04-12 00:58:46 +0300115 GETOPT_RESET();
Denis Vlasenkoc4e4be92008-01-27 22:39:55 +0000116
Denys Vlasenko8858a982017-08-08 01:21:49 +0200117 argc = string_array_len(argv);
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000118
Denys Vlasenko550bf5b2015-10-09 16:42:57 +0200119 /* If xfunc "dies" in NOFORK applet, die_func longjmp's here instead */
120 die_func = jump;
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000121 rc = setjmp(die_jmp);
122 if (!rc) {
123 /* Some callers (xargs)
124 * need argv untouched because they free argv[i]! */
125 char *tmp_argv[argc+1];
126 memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0]));
Ron Yorston610c4c32016-03-30 00:42:05 +0200127 applet_name = tmp_argv[0];
Denys Vlasenko6bdfbc42021-02-18 23:30:24 +0100128
129 /* longjmp's (instead of returning) if --help is seen */
130 show_usage_if_dash_dash_help(applet_no, argv);
131
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000132 /* Finally we can call NOFORK applet's main() */
Denis Vlasenko745cd172007-11-29 03:31:20 +0000133 rc = applet_main[applet_no](argc, tmp_argv);
Denys Vlasenko6bdfbc42021-02-18 23:30:24 +0100134
Denys Vlasenko19c9f312017-08-03 19:52:47 +0200135 /* Important for shells: `which CMD` was failing */
136 fflush_all();
Denys Vlasenko550bf5b2015-10-09 16:42:57 +0200137 } else {
138 /* xfunc died in NOFORK applet */
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000139 }
140
Denis Vlasenkod1660cb2008-10-20 07:52:33 +0000141 /* Restoring some globals */
Denys Vlasenko63073572011-02-02 19:05:25 +0100142 restore_nofork_data(&old);
Denis Vlasenkod1660cb2008-10-20 07:52:33 +0000143 /* Other globals can be simply reset to defaults */
Kaarle Ritvanen835ad3a2017-04-12 00:58:46 +0300144 GETOPT_RESET();
Denis Vlasenkod1660cb2008-10-20 07:52:33 +0000145
Denis Vlasenko7465dbc2008-04-13 02:25:53 +0000146 return rc & 0xff; /* don't confuse people with "exitcodes" >255 */
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000147}
Denys Vlasenkoddd1ee42017-08-08 14:09:23 +0200148#endif
Denis Vlasenko18e19f22007-04-28 16:43:18 +0000149
Denys Vlasenkoddd1ee42017-08-08 14:09:23 +0200150#if NOEXEC_SUPPORT
Denys Vlasenko80e8e3c2017-08-07 19:24:57 +0200151void FAST_FUNC run_noexec_applet_and_exit(int a, const char *name, char **argv)
152{
153 /* reset some state and run without execing */
154 /* msg_eol = "\n"; - no caller needs this reinited yet */
155 logmode = LOGMODE_STDIO;
156 xfunc_error_retval = EXIT_FAILURE;
157 die_func = NULL;
158 GETOPT_RESET();
159
160//TODO: think pidof, pgrep, pkill!
161//set_task_comm() makes our pidof find NOEXECs (e.g. "yes >/dev/null"),
162//but one from procps-ng-3.3.10 needs more!
163//Rewrite /proc/PID/cmdline? (need to save argv0 and length at init for this to work!)
164 set_task_comm(name);
Denys Vlasenko8858a982017-08-08 01:21:49 +0200165 /* applet_name is set by this function: */
Denys Vlasenko80e8e3c2017-08-07 19:24:57 +0200166 run_applet_no_and_exit(a, name, argv);
167}
168#endif
169
Denys Vlasenko8858a982017-08-08 01:21:49 +0200170/*
171 * Higher-level code, hiding optional NOFORK/NOEXEC trickery.
172 */
173
174/* This does a fork/exec in one call, using vfork(). Returns PID of new child,
175 * -1 for failure. Runs argv[0], searching path if that has no / in it. */
176pid_t FAST_FUNC spawn(char **argv)
177{
178 /* Compiler should not optimize stores here */
179 volatile int failed;
180 pid_t pid;
181
182 fflush_all();
183
184 /* Be nice to nommu machines. */
185 failed = 0;
186 pid = vfork();
187 if (pid < 0) /* error */
188 return pid;
189 if (!pid) { /* child */
190 /* This macro is ok - it doesn't do NOEXEC/NOFORK tricks */
191 BB_EXECVP(argv[0], argv);
192
193 /* We are (maybe) sharing a stack with blocked parent,
194 * let parent know we failed and then exit to unblock parent
195 * (but don't run atexit() stuff, which would screw up parent.)
196 */
197 failed = errno;
198 /* mount, for example, does not want the message */
199 /*bb_perror_msg("can't execute '%s'", argv[0]);*/
200 _exit(111);
201 }
202 /* parent */
203 /* Unfortunately, this is not reliable: according to standards
204 * vfork() can be equivalent to fork() and we won't see value
205 * of 'failed'.
206 * Interested party can wait on pid and learn exit code.
207 * If 111 - then it (most probably) failed to exec */
208 if (failed) {
209 safe_waitpid(pid, NULL, 0); /* prevent zombie */
210 errno = failed;
211 return -1;
212 }
213 return pid;
214}
215
216/* Die with an error message if we can't spawn a child process. */
217pid_t FAST_FUNC xspawn(char **argv)
218{
219 pid_t pid = spawn(argv);
220 if (pid < 0)
221 bb_simple_perror_msg_and_die(*argv);
222 return pid;
223}
224
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000225int FAST_FUNC spawn_and_wait(char **argv)
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000226{
Denis Vlasenkoc6758a02007-04-10 21:40:19 +0000227 int rc;
Denys Vlasenkod4e4fdb2017-07-03 21:31:16 +0200228#if ENABLE_FEATURE_PREFER_APPLETS && (NUM_APPLETS > 1)
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000229 int a = find_applet_by_name(argv[0]);
Denis Vlasenko335b63d2007-04-10 21:38:30 +0000230
Denys Vlasenko9967c992017-01-26 01:13:58 +0100231 if (a >= 0) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000232 if (APPLET_IS_NOFORK(a))
Denis Vlasenkof5294e12007-04-14 10:09:57 +0000233 return run_nofork_applet(a, argv);
Denys Vlasenko9967c992017-01-26 01:13:58 +0100234# if BB_MMU /* NOEXEC needs fork(), thus this is done only on MMU machines: */
235 if (APPLET_IS_NOEXEC(a)) {
236 fflush_all();
237 rc = fork();
238 if (rc) /* parent or error */
239 return wait4pid(rc);
240
241 /* child */
Denys Vlasenko80e8e3c2017-08-07 19:24:57 +0200242 run_noexec_applet_and_exit(a, argv[0], argv);
Denis Vlasenkoc6758a02007-04-10 21:40:19 +0000243 }
Denys Vlasenkoc71b4692011-02-02 03:28:56 +0100244# endif
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000245 }
Denys Vlasenkoddd1ee42017-08-08 14:09:23 +0200246#endif
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000247 rc = spawn(argv);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000248 return wait4pid(rc);
Eric Andersen35e643b2003-07-28 07:40:39 +0000249}
Denis Vlasenkoc6758a02007-04-10 21:40:19 +0000250
Denis Vlasenko473dae02007-04-11 07:04:23 +0000251#if !BB_MMU
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000252void FAST_FUNC re_exec(char **argv)
Denis Vlasenko367960b2007-08-18 14:20:21 +0000253{
254 /* high-order bit of first char in argv[0] is a hidden
255 * "we have (already) re-execed, don't do it again" flag */
256 argv[0][0] |= 0x80;
257 execv(bb_busybox_exec_path, argv);
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +0200258 bb_perror_msg_and_die("can't execute '%s'", bb_busybox_exec_path);
Denis Vlasenko367960b2007-08-18 14:20:21 +0000259}
260
Denis Vlasenko83518d12009-03-20 22:17:13 +0000261pid_t FAST_FUNC fork_or_rexec(char **argv)
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000262{
263 pid_t pid;
264 /* Maybe we are already re-execed and come here again? */
265 if (re_execed)
Denys Vlasenko8f24f982009-06-07 16:02:00 +0200266 return 0;
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200267
268 /* fflush_all(); ? - so far all callers had no buffered output to flush */
269
Pascal Bellard926031b2010-07-04 15:32:38 +0200270 pid = xvfork();
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000271 if (pid) /* parent */
Denis Vlasenko83518d12009-03-20 22:17:13 +0000272 return pid;
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000273 /* child - re-exec ourself */
Denis Vlasenko367960b2007-08-18 14:20:21 +0000274 re_exec(argv);
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000275}
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000276#endif
277
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000278/* Due to a #define in libbb.h on MMU systems we actually have 1 argument -
279 * char **argv "vanishes" */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000280void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000281{
282 int fd;
283
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000284 if (flags & DAEMON_CHDIR_ROOT)
285 xchdir("/");
286
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000287 fd = open(bb_dev_null, O_RDWR);
288 if (fd < 0) {
289 /* NB: we can be called as bb_sanitize_stdio() from init
290 * or mdev, and there /dev/null may legitimately not (yet) exist!
291 * Do not use xopen above, but obtain _ANY_ open descriptor,
292 * even bogus one as below. */
293 fd = xopen("/", O_RDONLY); /* don't believe this can fail */
294 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000295
Denys Vlasenkofa8878b2019-04-29 14:24:07 +0200296 if (flags & DAEMON_DEVNULL_STDIO) {
297 xdup2(fd, 0);
298 xdup2(fd, 1);
299 xdup2(fd, 2);
300 } else {
301 /* have 0,1,2 open at least to /dev/null */
302 while ((unsigned)fd < 2)
303 fd = dup(fd);
304 }
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000305
306 if (!(flags & DAEMON_ONLY_SANITIZE)) {
Denys Vlasenkob182e9a2017-08-04 23:04:17 +0200307
308 /* fflush_all(); - add it in fork_or_rexec() if necessary */
309
Denis Vlasenko83518d12009-03-20 22:17:13 +0000310 if (fork_or_rexec(argv))
Denys Vlasenkodb5546c2022-01-05 22:16:06 +0100311 _exit_SUCCESS(); /* parent */
Peter Korsgaard743edac2011-11-09 19:44:37 +0100312 /* if daemonizing, detach from stdio & ctty */
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000313 setsid();
314 dup2(fd, 0);
315 dup2(fd, 1);
316 dup2(fd, 2);
Denys Vlasenko088fec32019-01-14 14:45:18 +0100317// if (flags & DAEMON_DOUBLE_FORK) {
318// /* On Linux, session leader can acquire ctty
319// * unknowingly, by opening a tty.
320// * Prevent this: stop being a session leader.
321// */
322// if (fork_or_rexec(argv))
Denys Vlasenkodb5546c2022-01-05 22:16:06 +0100323// _exit_SUCCESS(); /* parent */
Denys Vlasenko088fec32019-01-14 14:45:18 +0100324// }
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000325 }
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000326 while (fd > 2) {
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000327 close(fd--);
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000328 if (!(flags & DAEMON_CLOSE_EXTRA_FDS))
329 return;
330 /* else close everything after fd#2 */
331 }
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000332}
333
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000334void FAST_FUNC bb_sanitize_stdio(void)
Denis Vlasenkobb7fcb42007-03-26 13:20:04 +0000335{
336 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE, NULL);
337}