vfork_daemon_rexec.c 8.5 KB

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