3
0

vfork_daemon_rexec.c 6.9 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 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 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 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. int safe_waitpid(int pid, int *wstat, int options)
  63. {
  64. int r;
  65. do
  66. r = waitpid(pid, wstat, options);
  67. while ((r == -1) && (errno == EINTR));
  68. return r;
  69. }
  70. int 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 wait4pid(int 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 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 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 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. /* option_mask32 = 0; - not needed */
  119. argc = 1;
  120. while (argv[argc])
  121. argc++;
  122. rc = setjmp(die_jmp);
  123. if (!rc) {
  124. /* Some callers (xargs)
  125. * need argv untouched because they free argv[i]! */
  126. char *tmp_argv[argc+1];
  127. memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0]));
  128. /* Finally we can call NOFORK applet's main() */
  129. rc = applet_main[applet_no](argc, tmp_argv);
  130. /* The whole reason behind nofork_save_area is that <applet>_main
  131. * may exit non-locally! For example, in hush Ctrl-Z tries
  132. * (modulo bugs) to dynamically create a child (backgrounded task)
  133. * if it detects that Ctrl-Z was pressed when a NOFORK was running.
  134. * Testcase: interactive "rm -i".
  135. * Don't fool yourself into thinking "and <applet>_main() returns
  136. * quickly here" and removing "useless" nofork_save_area code. */
  137. } else { /* xfunc died in NOFORK applet */
  138. /* in case they meant to return 0... */
  139. if (rc == -2222)
  140. rc = 0;
  141. }
  142. /* Restoring globals */
  143. restore_nofork_data(old);
  144. return rc & 0xff; /* don't confuse people with "exitcodes" >255 */
  145. }
  146. int run_nofork_applet(int applet_no, char **argv)
  147. {
  148. struct nofork_save_area old;
  149. /* Saving globals */
  150. save_nofork_data(&old);
  151. return run_nofork_applet_prime(&old, applet_no, argv);
  152. }
  153. #endif /* FEATURE_PREFER_APPLETS */
  154. int spawn_and_wait(char **argv)
  155. {
  156. int rc;
  157. #if ENABLE_FEATURE_PREFER_APPLETS
  158. int a = find_applet_by_name(argv[0]);
  159. if (a >= 0 && (APPLET_IS_NOFORK(a)
  160. #if BB_MMU
  161. || APPLET_IS_NOEXEC(a) /* NOEXEC trick needs fork() */
  162. #endif
  163. )) {
  164. #if BB_MMU
  165. if (APPLET_IS_NOFORK(a))
  166. #endif
  167. {
  168. return run_nofork_applet(a, argv);
  169. }
  170. #if BB_MMU
  171. /* MMU only */
  172. /* a->noexec is true */
  173. rc = fork();
  174. if (rc) /* parent or error */
  175. return wait4pid(rc);
  176. /* child */
  177. xfunc_error_retval = EXIT_FAILURE;
  178. run_applet_no_and_exit(a, argv);
  179. #endif
  180. }
  181. #endif /* FEATURE_PREFER_APPLETS */
  182. rc = spawn(argv);
  183. return wait4pid(rc);
  184. }
  185. #if !BB_MMU
  186. void re_exec(char **argv)
  187. {
  188. /* high-order bit of first char in argv[0] is a hidden
  189. * "we have (already) re-execed, don't do it again" flag */
  190. argv[0][0] |= 0x80;
  191. execv(bb_busybox_exec_path, argv);
  192. bb_perror_msg_and_die("exec %s", bb_busybox_exec_path);
  193. }
  194. void forkexit_or_rexec(char **argv)
  195. {
  196. pid_t pid;
  197. /* Maybe we are already re-execed and come here again? */
  198. if (re_execed)
  199. return;
  200. pid = vfork();
  201. if (pid < 0) /* wtf? */
  202. bb_perror_msg_and_die("vfork");
  203. if (pid) /* parent */
  204. exit(EXIT_SUCCESS);
  205. /* child - re-exec ourself */
  206. re_exec(argv);
  207. }
  208. #else
  209. /* Dance around (void)...*/
  210. #undef forkexit_or_rexec
  211. void forkexit_or_rexec(void)
  212. {
  213. pid_t pid;
  214. pid = fork();
  215. if (pid < 0) /* wtf? */
  216. bb_perror_msg_and_die("fork");
  217. if (pid) /* parent */
  218. exit(EXIT_SUCCESS);
  219. /* child */
  220. }
  221. #define forkexit_or_rexec(argv) forkexit_or_rexec()
  222. #endif
  223. /* Due to a #define in libbb.h on MMU systems we actually have 1 argument -
  224. * char **argv "vanishes" */
  225. void bb_daemonize_or_rexec(int flags, char **argv)
  226. {
  227. int fd;
  228. if (flags & DAEMON_CHDIR_ROOT)
  229. xchdir("/");
  230. if (flags & DAEMON_DEVNULL_STDIO) {
  231. close(0);
  232. close(1);
  233. close(2);
  234. }
  235. fd = xopen(bb_dev_null, O_RDWR);
  236. while ((unsigned)fd < 2)
  237. fd = dup(fd); /* have 0,1,2 open at least to /dev/null */
  238. if (!(flags & DAEMON_ONLY_SANITIZE)) {
  239. forkexit_or_rexec(argv);
  240. /* if daemonizing, make sure we detach from stdio & ctty */
  241. setsid();
  242. dup2(fd, 0);
  243. dup2(fd, 1);
  244. dup2(fd, 2);
  245. }
  246. while (fd > 2) {
  247. close(fd--);
  248. if (!(flags & DAEMON_CLOSE_EXTRA_FDS))
  249. return;
  250. /* else close everything after fd#2 */
  251. }
  252. }
  253. void bb_sanitize_stdio(void)
  254. {
  255. bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE, NULL);
  256. }