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