vfork_daemon_rexec.c 8.5 KB

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