vfork_daemon_rexec.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "busybox.h" /* uses applet tables */
  18. #include "NUM_APPLETS.h"
  19. /* This does a fork/exec in one call, using vfork(). Returns PID of new child,
  20. * -1 for failure. Runs argv[0], searching path if that has no / in it. */
  21. pid_t FAST_FUNC spawn(char **argv)
  22. {
  23. /* Compiler should not optimize stores here */
  24. volatile int failed;
  25. pid_t pid;
  26. fflush_all();
  27. /* Be nice to nommu machines. */
  28. failed = 0;
  29. pid = vfork();
  30. if (pid < 0) /* error */
  31. return pid;
  32. if (!pid) { /* child */
  33. /* This macro is ok - it doesn't do NOEXEC/NOFORK tricks */
  34. BB_EXECVP(argv[0], argv);
  35. /* We are (maybe) sharing a stack with blocked parent,
  36. * let parent know we failed and then exit to unblock parent
  37. * (but don't run atexit() stuff, which would screw up parent.)
  38. */
  39. failed = errno;
  40. /* mount, for example, does not want the message */
  41. /*bb_perror_msg("can't execute '%s'", argv[0]);*/
  42. _exit(111);
  43. }
  44. /* parent */
  45. /* Unfortunately, this is not reliable: according to standards
  46. * vfork() can be equivalent to fork() and we won't see value
  47. * of 'failed'.
  48. * Interested party can wait on pid and learn exit code.
  49. * If 111 - then it (most probably) failed to exec */
  50. if (failed) {
  51. safe_waitpid(pid, NULL, 0); /* prevent zombie */
  52. errno = failed;
  53. return -1;
  54. }
  55. return pid;
  56. }
  57. /* Die with an error message if we can't spawn a child process. */
  58. pid_t FAST_FUNC xspawn(char **argv)
  59. {
  60. pid_t pid = spawn(argv);
  61. if (pid < 0)
  62. bb_simple_perror_msg_and_die(*argv);
  63. return pid;
  64. }
  65. #if ENABLE_FEATURE_PREFER_APPLETS \
  66. || ENABLE_FEATURE_SH_NOFORK
  67. static jmp_buf die_jmp;
  68. static void jump(void)
  69. {
  70. /* Special case. We arrive here if NOFORK applet
  71. * calls xfunc, which then decides to die.
  72. * We don't die, but jump instead back to caller.
  73. * NOFORK applets still cannot carelessly call xfuncs:
  74. * p = xmalloc(10);
  75. * q = xmalloc(10); // BUG! if this dies, we leak p!
  76. */
  77. /* | 0x100 allows to pass zero exitcode (longjmp can't pass 0).
  78. * This works because exitcodes are bytes,
  79. * run_nofork_applet() ensures that by "& 0xff" */
  80. longjmp(die_jmp, xfunc_error_retval | 0x100);
  81. }
  82. struct nofork_save_area {
  83. jmp_buf die_jmp;
  84. void (*die_func)(void);
  85. const char *applet_name;
  86. uint32_t option_mask32;
  87. uint8_t xfunc_error_retval;
  88. };
  89. static void save_nofork_data(struct nofork_save_area *save)
  90. {
  91. memcpy(&save->die_jmp, &die_jmp, sizeof(die_jmp));
  92. save->die_func = die_func;
  93. save->applet_name = applet_name;
  94. save->option_mask32 = option_mask32;
  95. save->xfunc_error_retval = xfunc_error_retval;
  96. }
  97. static void restore_nofork_data(struct nofork_save_area *save)
  98. {
  99. memcpy(&die_jmp, &save->die_jmp, sizeof(die_jmp));
  100. die_func = save->die_func;
  101. applet_name = save->applet_name;
  102. option_mask32 = save->option_mask32;
  103. xfunc_error_retval = save->xfunc_error_retval;
  104. }
  105. int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
  106. {
  107. int rc, argc;
  108. struct nofork_save_area old;
  109. save_nofork_data(&old);
  110. xfunc_error_retval = EXIT_FAILURE;
  111. /* In case getopt() or getopt32() was already called:
  112. * reset the libc getopt() function, which keeps internal state.
  113. */
  114. GETOPT_RESET();
  115. argc = 1;
  116. while (argv[argc])
  117. argc++;
  118. /* If xfunc "dies" in NOFORK applet, die_func longjmp's here instead */
  119. die_func = jump;
  120. rc = setjmp(die_jmp);
  121. if (!rc) {
  122. /* Some callers (xargs)
  123. * need argv untouched because they free argv[i]! */
  124. char *tmp_argv[argc+1];
  125. memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0]));
  126. applet_name = tmp_argv[0];
  127. /* Finally we can call NOFORK applet's main() */
  128. rc = applet_main[applet_no](argc, tmp_argv);
  129. } else {
  130. /* xfunc died in NOFORK applet */
  131. }
  132. /* Restoring some globals */
  133. restore_nofork_data(&old);
  134. /* Other globals can be simply reset to defaults */
  135. GETOPT_RESET();
  136. return rc & 0xff; /* don't confuse people with "exitcodes" >255 */
  137. }
  138. #endif /* FEATURE_PREFER_APPLETS || FEATURE_SH_NOFORK */
  139. int FAST_FUNC spawn_and_wait(char **argv)
  140. {
  141. int rc;
  142. #if ENABLE_FEATURE_PREFER_APPLETS && (NUM_APPLETS > 1)
  143. int a = find_applet_by_name(argv[0]);
  144. if (a >= 0) {
  145. if (APPLET_IS_NOFORK(a))
  146. return run_nofork_applet(a, argv);
  147. # if BB_MMU /* NOEXEC needs fork(), thus this is done only on MMU machines: */
  148. if (APPLET_IS_NOEXEC(a)) {
  149. fflush_all();
  150. rc = fork();
  151. if (rc) /* parent or error */
  152. return wait4pid(rc);
  153. /* child */
  154. /* reset some state and run without execing */
  155. /* msg_eol = "\n"; - no caller needs this reinited yet */
  156. logmode = LOGMODE_STDIO;
  157. /* die_func = NULL; - needed if the caller is a shell,
  158. * init, or a NOFORK applet. But none of those call us
  159. * as of yet (and that should probably always stay true).
  160. */
  161. /* xfunc_error_retval and applet_name are init by: */
  162. run_applet_no_and_exit(a, argv[0], argv);
  163. }
  164. # endif
  165. }
  166. #endif /* FEATURE_PREFER_APPLETS */
  167. rc = spawn(argv);
  168. return wait4pid(rc);
  169. }
  170. #if !BB_MMU
  171. void FAST_FUNC re_exec(char **argv)
  172. {
  173. /* high-order bit of first char in argv[0] is a hidden
  174. * "we have (already) re-execed, don't do it again" flag */
  175. argv[0][0] |= 0x80;
  176. execv(bb_busybox_exec_path, argv);
  177. bb_perror_msg_and_die("can't execute '%s'", bb_busybox_exec_path);
  178. }
  179. pid_t FAST_FUNC fork_or_rexec(char **argv)
  180. {
  181. pid_t pid;
  182. /* Maybe we are already re-execed and come here again? */
  183. if (re_execed)
  184. return 0;
  185. pid = xvfork();
  186. if (pid) /* parent */
  187. return pid;
  188. /* child - re-exec ourself */
  189. re_exec(argv);
  190. }
  191. #endif
  192. /* Due to a #define in libbb.h on MMU systems we actually have 1 argument -
  193. * char **argv "vanishes" */
  194. void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
  195. {
  196. int fd;
  197. if (flags & DAEMON_CHDIR_ROOT)
  198. xchdir("/");
  199. if (flags & DAEMON_DEVNULL_STDIO) {
  200. close(0);
  201. close(1);
  202. close(2);
  203. }
  204. fd = open(bb_dev_null, O_RDWR);
  205. if (fd < 0) {
  206. /* NB: we can be called as bb_sanitize_stdio() from init
  207. * or mdev, and there /dev/null may legitimately not (yet) exist!
  208. * Do not use xopen above, but obtain _ANY_ open descriptor,
  209. * even bogus one as below. */
  210. fd = xopen("/", O_RDONLY); /* don't believe this can fail */
  211. }
  212. while ((unsigned)fd < 2)
  213. fd = dup(fd); /* have 0,1,2 open at least to /dev/null */
  214. if (!(flags & DAEMON_ONLY_SANITIZE)) {
  215. if (fork_or_rexec(argv))
  216. exit(EXIT_SUCCESS); /* parent */
  217. /* if daemonizing, detach from stdio & ctty */
  218. setsid();
  219. dup2(fd, 0);
  220. dup2(fd, 1);
  221. dup2(fd, 2);
  222. if (flags & DAEMON_DOUBLE_FORK) {
  223. /* On Linux, session leader can acquire ctty
  224. * unknowingly, by opening a tty.
  225. * Prevent this: stop being a session leader.
  226. */
  227. if (fork_or_rexec(argv))
  228. exit(EXIT_SUCCESS); /* parent */
  229. }
  230. }
  231. while (fd > 2) {
  232. close(fd--);
  233. if (!(flags & DAEMON_CLOSE_EXTRA_FDS))
  234. return;
  235. /* else close everything after fd#2 */
  236. }
  237. }
  238. void FAST_FUNC bb_sanitize_stdio(void)
  239. {
  240. bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE, NULL);
  241. }