3
0

vfork_daemon_rexec.c 7.1 KB

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