vfork_daemon_rexec.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. || ENABLE_FEATURE_SH_NOFORK
  66. static jmp_buf die_jmp;
  67. static void jump(void)
  68. {
  69. /* Special case. We arrive here if NOFORK applet
  70. * calls xfunc, which then decides to die.
  71. * We don't die, but jump instead back to caller.
  72. * NOFORK applets still cannot carelessly call xfuncs:
  73. * p = xmalloc(10);
  74. * q = xmalloc(10); // BUG! if this dies, we leak p!
  75. */
  76. /* | 0x100 allows to pass zero exitcode (longjmp can't pass 0).
  77. * This works because exitcodes are bytes,
  78. * run_nofork_applet() ensures that by "& 0xff" */
  79. longjmp(die_jmp, xfunc_error_retval | 0x100);
  80. }
  81. struct nofork_save_area {
  82. jmp_buf die_jmp;
  83. void (*die_func)(void);
  84. const char *applet_name;
  85. uint32_t option_mask32;
  86. uint8_t xfunc_error_retval;
  87. };
  88. static void save_nofork_data(struct nofork_save_area *save)
  89. {
  90. memcpy(&save->die_jmp, &die_jmp, sizeof(die_jmp));
  91. save->die_func = die_func;
  92. save->applet_name = applet_name;
  93. save->option_mask32 = option_mask32;
  94. save->xfunc_error_retval = xfunc_error_retval;
  95. }
  96. static void restore_nofork_data(struct nofork_save_area *save)
  97. {
  98. memcpy(&die_jmp, &save->die_jmp, sizeof(die_jmp));
  99. die_func = save->die_func;
  100. applet_name = save->applet_name;
  101. option_mask32 = save->option_mask32;
  102. xfunc_error_retval = save->xfunc_error_retval;
  103. }
  104. int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
  105. {
  106. int rc, argc;
  107. struct nofork_save_area old;
  108. save_nofork_data(&old);
  109. xfunc_error_retval = EXIT_FAILURE;
  110. /* In case getopt() or getopt32() was already called:
  111. * reset the libc getopt() function, which keeps internal state.
  112. *
  113. * BSD-derived getopt() functions require that optind be set to 1 in
  114. * order to reset getopt() state. This used to be generally accepted
  115. * way of resetting getopt(). However, glibc's getopt()
  116. * has additional getopt() state beyond optind, and requires that
  117. * optind be set to zero to reset its state. So the unfortunate state of
  118. * affairs is that BSD-derived versions of getopt() misbehave if
  119. * optind is set to 0 in order to reset getopt(), and glibc's getopt()
  120. * will core dump if optind is set 1 in order to reset getopt().
  121. *
  122. * More modern versions of BSD require that optreset be set to 1 in
  123. * order to reset getopt(). Sigh. Standards, anyone?
  124. */
  125. #ifdef __GLIBC__
  126. optind = 0;
  127. #else /* BSD style */
  128. optind = 1;
  129. /* optreset = 1; */
  130. #endif
  131. /* optarg = NULL; opterr = 1; optopt = 63; - do we need this too? */
  132. /* (values above are what they initialized to in glibc and uclibc) */
  133. /* option_mask32 = 0; - not needed, no applet depends on it being 0 */
  134. argc = 1;
  135. while (argv[argc])
  136. argc++;
  137. /* If xfunc "dies" in NOFORK applet, die_func longjmp's here instead */
  138. die_func = jump;
  139. rc = setjmp(die_jmp);
  140. if (!rc) {
  141. /* Some callers (xargs)
  142. * need argv untouched because they free argv[i]! */
  143. char *tmp_argv[argc+1];
  144. memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0]));
  145. applet_name = tmp_argv[0];
  146. /* Finally we can call NOFORK applet's main() */
  147. rc = applet_main[applet_no](argc, tmp_argv);
  148. } else {
  149. /* xfunc died in NOFORK applet */
  150. }
  151. /* Restoring some globals */
  152. restore_nofork_data(&old);
  153. /* Other globals can be simply reset to defaults */
  154. #ifdef __GLIBC__
  155. optind = 0;
  156. #else /* BSD style */
  157. optind = 1;
  158. #endif
  159. return rc & 0xff; /* don't confuse people with "exitcodes" >255 */
  160. }
  161. #endif /* FEATURE_PREFER_APPLETS || FEATURE_SH_NOFORK */
  162. int FAST_FUNC spawn_and_wait(char **argv)
  163. {
  164. int rc;
  165. #if ENABLE_FEATURE_PREFER_APPLETS
  166. int a = find_applet_by_name(argv[0]);
  167. if (a >= 0 && (APPLET_IS_NOFORK(a)
  168. # if BB_MMU
  169. || APPLET_IS_NOEXEC(a) /* NOEXEC trick needs fork() */
  170. # endif
  171. )) {
  172. # if BB_MMU
  173. if (APPLET_IS_NOFORK(a))
  174. # endif
  175. {
  176. return run_nofork_applet(a, argv);
  177. }
  178. # if BB_MMU
  179. /* MMU only */
  180. /* a->noexec is true */
  181. rc = fork();
  182. if (rc) /* parent or error */
  183. return wait4pid(rc);
  184. /* child */
  185. xfunc_error_retval = EXIT_FAILURE;
  186. run_applet_no_and_exit(a, argv);
  187. # endif
  188. }
  189. #endif /* FEATURE_PREFER_APPLETS */
  190. rc = spawn(argv);
  191. return wait4pid(rc);
  192. }
  193. #if !BB_MMU
  194. void FAST_FUNC re_exec(char **argv)
  195. {
  196. /* high-order bit of first char in argv[0] is a hidden
  197. * "we have (already) re-execed, don't do it again" flag */
  198. argv[0][0] |= 0x80;
  199. execv(bb_busybox_exec_path, argv);
  200. bb_perror_msg_and_die("can't execute '%s'", bb_busybox_exec_path);
  201. }
  202. pid_t FAST_FUNC fork_or_rexec(char **argv)
  203. {
  204. pid_t pid;
  205. /* Maybe we are already re-execed and come here again? */
  206. if (re_execed)
  207. return 0;
  208. pid = xvfork();
  209. if (pid) /* parent */
  210. return pid;
  211. /* child - re-exec ourself */
  212. re_exec(argv);
  213. }
  214. #endif
  215. /* Due to a #define in libbb.h on MMU systems we actually have 1 argument -
  216. * char **argv "vanishes" */
  217. void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
  218. {
  219. int fd;
  220. if (flags & DAEMON_CHDIR_ROOT)
  221. xchdir("/");
  222. if (flags & DAEMON_DEVNULL_STDIO) {
  223. close(0);
  224. close(1);
  225. close(2);
  226. }
  227. fd = open(bb_dev_null, O_RDWR);
  228. if (fd < 0) {
  229. /* NB: we can be called as bb_sanitize_stdio() from init
  230. * or mdev, and there /dev/null may legitimately not (yet) exist!
  231. * Do not use xopen above, but obtain _ANY_ open descriptor,
  232. * even bogus one as below. */
  233. fd = xopen("/", O_RDONLY); /* don't believe this can fail */
  234. }
  235. while ((unsigned)fd < 2)
  236. fd = dup(fd); /* have 0,1,2 open at least to /dev/null */
  237. if (!(flags & DAEMON_ONLY_SANITIZE)) {
  238. if (fork_or_rexec(argv))
  239. exit(EXIT_SUCCESS); /* parent */
  240. /* if daemonizing, detach from stdio & ctty */
  241. setsid();
  242. dup2(fd, 0);
  243. dup2(fd, 1);
  244. dup2(fd, 2);
  245. if (flags & DAEMON_DOUBLE_FORK) {
  246. /* On Linux, session leader can acquire ctty
  247. * unknowingly, by opening a tty.
  248. * Prevent this: stop being a session leader.
  249. */
  250. if (fork_or_rexec(argv))
  251. exit(EXIT_SUCCESS); /* parent */
  252. }
  253. }
  254. while (fd > 2) {
  255. close(fd--);
  256. if (!(flags & DAEMON_CLOSE_EXTRA_FDS))
  257. return;
  258. /* else close everything after fd#2 */
  259. }
  260. }
  261. void FAST_FUNC bb_sanitize_stdio(void)
  262. {
  263. bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE, NULL);
  264. }