vfork_daemon_rexec.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. /* This does a fork/exec in one call, using vfork(). Returns PID of new child,
  19. * -1 for failure. Runs argv[0], searching path if that has no / in it. */
  20. pid_t FAST_FUNC spawn(char **argv)
  21. {
  22. /* Compiler should not optimize stores here */
  23. volatile int failed;
  24. pid_t pid;
  25. fflush_all();
  26. /* Be nice to nommu machines. */
  27. failed = 0;
  28. pid = vfork();
  29. if (pid < 0) /* error */
  30. return pid;
  31. if (!pid) { /* child */
  32. /* This macro is ok - it doesn't do NOEXEC/NOFORK tricks */
  33. BB_EXECVP(argv[0], argv);
  34. /* We are (maybe) sharing a stack with blocked parent,
  35. * let parent know we failed and then exit to unblock parent
  36. * (but don't run atexit() stuff, which would screw up parent.)
  37. */
  38. failed = errno;
  39. /* mount, for example, does not want the message */
  40. /*bb_perror_msg("can't execute '%s'", argv[0]);*/
  41. _exit(111);
  42. }
  43. /* parent */
  44. /* Unfortunately, this is not reliable: according to standards
  45. * vfork() can be equivalent to fork() and we won't see value
  46. * of 'failed'.
  47. * Interested party can wait on pid and learn exit code.
  48. * If 111 - then it (most probably) failed to exec */
  49. if (failed) {
  50. safe_waitpid(pid, NULL, 0); /* prevent zombie */
  51. errno = failed;
  52. return -1;
  53. }
  54. return pid;
  55. }
  56. /* Die with an error message if we can't spawn a child process. */
  57. pid_t FAST_FUNC xspawn(char **argv)
  58. {
  59. pid_t pid = spawn(argv);
  60. if (pid < 0)
  61. bb_simple_perror_msg_and_die(*argv);
  62. return pid;
  63. }
  64. #if ENABLE_FEATURE_PREFER_APPLETS
  65. void FAST_FUNC save_nofork_data(struct nofork_save_area *save)
  66. {
  67. memcpy(&save->die_jmp, &die_jmp, sizeof(die_jmp));
  68. save->applet_name = applet_name;
  69. save->xfunc_error_retval = xfunc_error_retval;
  70. save->option_mask32 = option_mask32;
  71. save->die_sleep = die_sleep;
  72. save->saved = 1;
  73. }
  74. void FAST_FUNC restore_nofork_data(struct nofork_save_area *save)
  75. {
  76. memcpy(&die_jmp, &save->die_jmp, sizeof(die_jmp));
  77. applet_name = save->applet_name;
  78. xfunc_error_retval = save->xfunc_error_retval;
  79. option_mask32 = save->option_mask32;
  80. die_sleep = save->die_sleep;
  81. }
  82. int FAST_FUNC run_nofork_applet_prime(struct nofork_save_area *old, int applet_no, char **argv)
  83. {
  84. int rc, argc;
  85. applet_name = APPLET_NAME(applet_no);
  86. xfunc_error_retval = EXIT_FAILURE;
  87. /* Special flag for xfunc_die(). If xfunc will "die"
  88. * in NOFORK applet, xfunc_die() sees negative
  89. * die_sleep and longjmp here instead. */
  90. die_sleep = -1;
  91. /* In case getopt() or getopt32() was already called:
  92. * reset the libc getopt() function, which keeps internal state.
  93. *
  94. * BSD-derived getopt() functions require that optind be set to 1 in
  95. * order to reset getopt() state. This used to be generally accepted
  96. * way of resetting getopt(). However, glibc's getopt()
  97. * has additional getopt() state beyond optind, and requires that
  98. * optind be set to zero to reset its state. So the unfortunate state of
  99. * affairs is that BSD-derived versions of getopt() misbehave if
  100. * optind is set to 0 in order to reset getopt(), and glibc's getopt()
  101. * will core dump if optind is set 1 in order to reset getopt().
  102. *
  103. * More modern versions of BSD require that optreset be set to 1 in
  104. * order to reset getopt(). Sigh. Standards, anyone?
  105. */
  106. #ifdef __GLIBC__
  107. optind = 0;
  108. #else /* BSD style */
  109. optind = 1;
  110. /* optreset = 1; */
  111. #endif
  112. /* optarg = NULL; opterr = 1; optopt = 63; - do we need this too? */
  113. /* (values above are what they initialized to in glibc and uclibc) */
  114. /* option_mask32 = 0; - not needed, no applet depends on it being 0 */
  115. argc = 1;
  116. while (argv[argc])
  117. argc++;
  118. rc = setjmp(die_jmp);
  119. if (!rc) {
  120. /* Some callers (xargs)
  121. * need argv untouched because they free argv[i]! */
  122. char *tmp_argv[argc+1];
  123. memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0]));
  124. /* Finally we can call NOFORK applet's main() */
  125. rc = applet_main[applet_no](argc, tmp_argv);
  126. /* The whole reason behind nofork_save_area is that <applet>_main
  127. * may exit non-locally! For example, in hush Ctrl-Z tries
  128. * (modulo bugs) to dynamically create a child (backgrounded task)
  129. * if it detects that Ctrl-Z was pressed when a NOFORK was running.
  130. * Testcase: interactive "rm -i".
  131. * Don't fool yourself into thinking "and <applet>_main() returns
  132. * quickly here" and removing "useless" nofork_save_area code. */
  133. } else { /* xfunc died in NOFORK applet */
  134. /* in case they meant to return 0... */
  135. if (rc == -2222)
  136. rc = 0;
  137. }
  138. /* Restoring some globals */
  139. restore_nofork_data(old);
  140. /* Other globals can be simply reset to defaults */
  141. #ifdef __GLIBC__
  142. optind = 0;
  143. #else /* BSD style */
  144. optind = 1;
  145. #endif
  146. return rc & 0xff; /* don't confuse people with "exitcodes" >255 */
  147. }
  148. int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
  149. {
  150. struct nofork_save_area old;
  151. /* Saving globals */
  152. save_nofork_data(&old);
  153. return run_nofork_applet_prime(&old, applet_no, argv);
  154. }
  155. #endif /* FEATURE_PREFER_APPLETS */
  156. int FAST_FUNC spawn_and_wait(char **argv)
  157. {
  158. int rc;
  159. #if ENABLE_FEATURE_PREFER_APPLETS
  160. int a = find_applet_by_name(argv[0]);
  161. if (a >= 0 && (APPLET_IS_NOFORK(a)
  162. #if BB_MMU
  163. || APPLET_IS_NOEXEC(a) /* NOEXEC trick needs fork() */
  164. #endif
  165. )) {
  166. #if BB_MMU
  167. if (APPLET_IS_NOFORK(a))
  168. #endif
  169. {
  170. return run_nofork_applet(a, argv);
  171. }
  172. #if BB_MMU
  173. /* MMU only */
  174. /* a->noexec is true */
  175. rc = fork();
  176. if (rc) /* parent or error */
  177. return wait4pid(rc);
  178. /* child */
  179. xfunc_error_retval = EXIT_FAILURE;
  180. run_applet_no_and_exit(a, argv);
  181. #endif
  182. }
  183. #endif /* FEATURE_PREFER_APPLETS */
  184. rc = spawn(argv);
  185. return wait4pid(rc);
  186. }
  187. #if !BB_MMU
  188. void FAST_FUNC re_exec(char **argv)
  189. {
  190. /* high-order bit of first char in argv[0] is a hidden
  191. * "we have (already) re-execed, don't do it again" flag */
  192. argv[0][0] |= 0x80;
  193. execv(bb_busybox_exec_path, argv);
  194. bb_perror_msg_and_die("can't execute '%s'", bb_busybox_exec_path);
  195. }
  196. pid_t FAST_FUNC fork_or_rexec(char **argv)
  197. {
  198. pid_t pid;
  199. /* Maybe we are already re-execed and come here again? */
  200. if (re_execed)
  201. return 0;
  202. pid = xvfork();
  203. if (pid) /* parent */
  204. return pid;
  205. /* child - re-exec ourself */
  206. re_exec(argv);
  207. }
  208. #endif
  209. /* Due to a #define in libbb.h on MMU systems we actually have 1 argument -
  210. * char **argv "vanishes" */
  211. void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
  212. {
  213. int fd;
  214. if (flags & DAEMON_CHDIR_ROOT)
  215. xchdir("/");
  216. if (flags & DAEMON_DEVNULL_STDIO) {
  217. close(0);
  218. close(1);
  219. close(2);
  220. }
  221. fd = open(bb_dev_null, O_RDWR);
  222. if (fd < 0) {
  223. /* NB: we can be called as bb_sanitize_stdio() from init
  224. * or mdev, and there /dev/null may legitimately not (yet) exist!
  225. * Do not use xopen above, but obtain _ANY_ open descriptor,
  226. * even bogus one as below. */
  227. fd = xopen("/", O_RDONLY); /* don't believe this can fail */
  228. }
  229. while ((unsigned)fd < 2)
  230. fd = dup(fd); /* have 0,1,2 open at least to /dev/null */
  231. if (!(flags & DAEMON_ONLY_SANITIZE)) {
  232. if (fork_or_rexec(argv))
  233. exit(EXIT_SUCCESS); /* parent */
  234. /* if daemonizing, make sure we detach from stdio & ctty */
  235. setsid();
  236. dup2(fd, 0);
  237. dup2(fd, 1);
  238. dup2(fd, 2);
  239. }
  240. while (fd > 2) {
  241. close(fd--);
  242. if (!(flags & DAEMON_CLOSE_EXTRA_FDS))
  243. return;
  244. /* else close everything after fd#2 */
  245. }
  246. }
  247. void FAST_FUNC bb_sanitize_stdio(void)
  248. {
  249. bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE, NULL);
  250. }