vfork_daemon_rexec.c 7.6 KB

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