3
0

vfork_daemon_rexec.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Rexec program for system have fork() as vfork() with foreground option
  4. *
  5. * Copyright (C) Vladimir N. Oleynik <dzo@simtreas.ru>
  6. * Copyright (C) 2003 Russ Dill <Russ.Dill@asu.edu>
  7. *
  8. * daemon() portion taken from uClibc:
  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. *
  15. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  16. */
  17. #include <sys/prctl.h>
  18. #ifndef PR_SET_NAME
  19. #define PR_SET_NAME 15
  20. #endif
  21. #ifndef PR_GET_NAME
  22. #define PR_GET_NAME 16
  23. #endif
  24. #include "busybox.h" /* uses applet tables */
  25. #include "NUM_APPLETS.h"
  26. #define NOFORK_SUPPORT ((NUM_APPLETS > 1) && (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_NOFORK))
  27. #define NOEXEC_SUPPORT ((NUM_APPLETS > 1) && (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE))
  28. #if defined(__linux__) && (NUM_APPLETS > 1)
  29. void FAST_FUNC set_task_comm(const char *comm)
  30. {
  31. /* okay if too long (truncates) */
  32. prctl(PR_SET_NAME, (long)comm, 0, 0, 0);
  33. }
  34. #endif
  35. /*
  36. * NOFORK/NOEXEC support
  37. */
  38. #if NOFORK_SUPPORT
  39. static jmp_buf die_jmp;
  40. static void jump(void)
  41. {
  42. /* Special case. We arrive here if NOFORK applet
  43. * calls xfunc, which then decides to die.
  44. * We don't die, but instead jump back to caller.
  45. * NOFORK applets still cannot carelessly call xfuncs:
  46. * p = xmalloc(10);
  47. * q = xmalloc(10); // BUG! if this dies, we leak p!
  48. */
  49. /* | 0x100 allows to pass zero exitcode (longjmp can't pass 0).
  50. * This works because exitcodes are bytes,
  51. * run_nofork_applet() ensures that by "& 0xff"
  52. */
  53. longjmp(die_jmp, xfunc_error_retval | 0x100);
  54. }
  55. struct nofork_save_area {
  56. jmp_buf die_jmp;
  57. void (*die_func)(void);
  58. const char *applet_name;
  59. uint32_t option_mask32;
  60. smallint logmode;
  61. uint8_t xfunc_error_retval;
  62. };
  63. static void save_nofork_data(struct nofork_save_area *save)
  64. {
  65. memcpy(&save->die_jmp, &die_jmp, sizeof(die_jmp));
  66. save->die_func = die_func;
  67. save->applet_name = applet_name;
  68. save->option_mask32 = option_mask32;
  69. save->logmode = logmode;
  70. save->xfunc_error_retval = xfunc_error_retval;
  71. }
  72. static void restore_nofork_data(struct nofork_save_area *save)
  73. {
  74. memcpy(&die_jmp, &save->die_jmp, sizeof(die_jmp));
  75. die_func = save->die_func;
  76. applet_name = save->applet_name;
  77. option_mask32 = save->option_mask32;
  78. logmode = save->logmode;
  79. xfunc_error_retval = save->xfunc_error_retval;
  80. }
  81. int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
  82. {
  83. int rc, argc;
  84. struct nofork_save_area old;
  85. save_nofork_data(&old);
  86. logmode = LOGMODE_STDIO;
  87. xfunc_error_retval = EXIT_FAILURE;
  88. /* In case getopt() was already called:
  89. * reset the libc getopt() function, which keeps internal state.
  90. * (getopt32() does it itself, but getopt() doesn't (and can't))
  91. */
  92. GETOPT_RESET();
  93. argc = string_array_len(argv);
  94. /* If xfunc "dies" in NOFORK applet, die_func longjmp's here instead */
  95. die_func = jump;
  96. rc = setjmp(die_jmp);
  97. if (!rc) {
  98. /* Some callers (xargs)
  99. * need argv untouched because they free argv[i]! */
  100. char *tmp_argv[argc+1];
  101. memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0]));
  102. applet_name = tmp_argv[0];
  103. /* Finally we can call NOFORK applet's main() */
  104. rc = applet_main[applet_no](argc, tmp_argv);
  105. /* Important for shells: `which CMD` was failing */
  106. fflush_all();
  107. } else {
  108. /* xfunc died in NOFORK applet */
  109. }
  110. /* Restoring some globals */
  111. restore_nofork_data(&old);
  112. /* Other globals can be simply reset to defaults */
  113. GETOPT_RESET();
  114. return rc & 0xff; /* don't confuse people with "exitcodes" >255 */
  115. }
  116. #endif
  117. #if NOEXEC_SUPPORT
  118. void 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();
  126. //TODO: think pidof, pgrep, pkill!
  127. //set_task_comm() makes our pidof find NOEXECs (e.g. "yes >/dev/null"),
  128. //but one from procps-ng-3.3.10 needs more!
  129. //Rewrite /proc/PID/cmdline? (need to save argv0 and length at init for this to work!)
  130. set_task_comm(name);
  131. /* applet_name is set by this function: */
  132. run_applet_no_and_exit(a, name, argv);
  133. }
  134. #endif
  135. /*
  136. * Higher-level code, hiding optional NOFORK/NOEXEC trickery.
  137. */
  138. /* This does a fork/exec in one call, using vfork(). Returns PID of new child,
  139. * -1 for failure. Runs argv[0], searching path if that has no / in it. */
  140. pid_t FAST_FUNC spawn(char **argv)
  141. {
  142. /* Compiler should not optimize stores here */
  143. volatile int failed;
  144. pid_t pid;
  145. fflush_all();
  146. /* Be nice to nommu machines. */
  147. failed = 0;
  148. pid = vfork();
  149. if (pid < 0) /* error */
  150. return pid;
  151. if (!pid) { /* child */
  152. /* This macro is ok - it doesn't do NOEXEC/NOFORK tricks */
  153. BB_EXECVP(argv[0], argv);
  154. /* We are (maybe) sharing a stack with blocked parent,
  155. * let parent know we failed and then exit to unblock parent
  156. * (but don't run atexit() stuff, which would screw up parent.)
  157. */
  158. failed = errno;
  159. /* mount, for example, does not want the message */
  160. /*bb_perror_msg("can't execute '%s'", argv[0]);*/
  161. _exit(111);
  162. }
  163. /* parent */
  164. /* Unfortunately, this is not reliable: according to standards
  165. * vfork() can be equivalent to fork() and we won't see value
  166. * of 'failed'.
  167. * Interested party can wait on pid and learn exit code.
  168. * If 111 - then it (most probably) failed to exec */
  169. if (failed) {
  170. safe_waitpid(pid, NULL, 0); /* prevent zombie */
  171. errno = failed;
  172. return -1;
  173. }
  174. return pid;
  175. }
  176. /* Die with an error message if we can't spawn a child process. */
  177. pid_t FAST_FUNC xspawn(char **argv)
  178. {
  179. pid_t pid = spawn(argv);
  180. if (pid < 0)
  181. bb_simple_perror_msg_and_die(*argv);
  182. return pid;
  183. }
  184. int FAST_FUNC spawn_and_wait(char **argv)
  185. {
  186. int rc;
  187. #if ENABLE_FEATURE_PREFER_APPLETS && (NUM_APPLETS > 1)
  188. int a = find_applet_by_name(argv[0]);
  189. if (a >= 0) {
  190. if (APPLET_IS_NOFORK(a))
  191. return run_nofork_applet(a, argv);
  192. # if BB_MMU /* NOEXEC needs fork(), thus this is done only on MMU machines: */
  193. if (APPLET_IS_NOEXEC(a)) {
  194. fflush_all();
  195. rc = fork();
  196. if (rc) /* parent or error */
  197. return wait4pid(rc);
  198. /* child */
  199. run_noexec_applet_and_exit(a, argv[0], argv);
  200. }
  201. # endif
  202. }
  203. #endif
  204. rc = spawn(argv);
  205. return wait4pid(rc);
  206. }
  207. #if !BB_MMU
  208. void FAST_FUNC re_exec(char **argv)
  209. {
  210. /* high-order bit of first char in argv[0] is a hidden
  211. * "we have (already) re-execed, don't do it again" flag */
  212. argv[0][0] |= 0x80;
  213. execv(bb_busybox_exec_path, argv);
  214. bb_perror_msg_and_die("can't execute '%s'", bb_busybox_exec_path);
  215. }
  216. pid_t FAST_FUNC fork_or_rexec(char **argv)
  217. {
  218. pid_t pid;
  219. /* Maybe we are already re-execed and come here again? */
  220. if (re_execed)
  221. return 0;
  222. /* fflush_all(); ? - so far all callers had no buffered output to flush */
  223. pid = xvfork();
  224. if (pid) /* parent */
  225. return pid;
  226. /* child - re-exec ourself */
  227. re_exec(argv);
  228. }
  229. #endif
  230. /* Due to a #define in libbb.h on MMU systems we actually have 1 argument -
  231. * char **argv "vanishes" */
  232. void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
  233. {
  234. int fd;
  235. if (flags & DAEMON_CHDIR_ROOT)
  236. xchdir("/");
  237. if (flags & DAEMON_DEVNULL_STDIO) {
  238. close(0);
  239. close(1);
  240. close(2);
  241. }
  242. fd = open(bb_dev_null, O_RDWR);
  243. if (fd < 0) {
  244. /* NB: we can be called as bb_sanitize_stdio() from init
  245. * or mdev, and there /dev/null may legitimately not (yet) exist!
  246. * Do not use xopen above, but obtain _ANY_ open descriptor,
  247. * even bogus one as below. */
  248. fd = xopen("/", O_RDONLY); /* don't believe this can fail */
  249. }
  250. while ((unsigned)fd < 2)
  251. fd = dup(fd); /* have 0,1,2 open at least to /dev/null */
  252. if (!(flags & DAEMON_ONLY_SANITIZE)) {
  253. /* fflush_all(); - add it in fork_or_rexec() if necessary */
  254. if (fork_or_rexec(argv))
  255. _exit(EXIT_SUCCESS); /* parent */
  256. /* if daemonizing, detach from stdio & ctty */
  257. setsid();
  258. dup2(fd, 0);
  259. dup2(fd, 1);
  260. dup2(fd, 2);
  261. if (flags & DAEMON_DOUBLE_FORK) {
  262. /* On Linux, session leader can acquire ctty
  263. * unknowingly, by opening a tty.
  264. * Prevent this: stop being a session leader.
  265. */
  266. if (fork_or_rexec(argv))
  267. _exit(EXIT_SUCCESS); /* parent */
  268. }
  269. }
  270. while (fd > 2) {
  271. close(fd--);
  272. if (!(flags & DAEMON_CLOSE_EXTRA_FDS))
  273. return;
  274. /* else close everything after fd#2 */
  275. }
  276. }
  277. void FAST_FUNC bb_sanitize_stdio(void)
  278. {
  279. bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE, NULL);
  280. }