vfork_daemon_rexec.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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" /* for struct bb_applet */
  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_perror_msg_and_die("%s", *argv);
  60. return pid;
  61. }
  62. // Wait for the specified child PID to exit, returning child's error return.
  63. int wait4pid(int pid)
  64. {
  65. int status;
  66. if (pid <= 0) {
  67. /*errno = ECHILD; -- wrong. */
  68. /* we expect errno to be already set from failed [v]fork/exec */
  69. return -1;
  70. }
  71. if (waitpid(pid, &status, 0) == -1)
  72. return -1;
  73. if (WIFEXITED(status))
  74. return WEXITSTATUS(status);
  75. if (WIFSIGNALED(status))
  76. return WTERMSIG(status) + 1000;
  77. return 0;
  78. }
  79. int wait_nohang(int *wstat)
  80. {
  81. return waitpid(-1, wstat, WNOHANG);
  82. }
  83. int wait_pid(int *wstat, int pid)
  84. {
  85. int r;
  86. do
  87. r = waitpid(pid, wstat, 0);
  88. while ((r == -1) && (errno == EINTR));
  89. return r;
  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->current_applet = current_applet;
  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. current_applet = save->current_applet;
  105. xfunc_error_retval = save->xfunc_error_retval;
  106. option_mask32 = save->option_mask32;
  107. die_sleep = save->die_sleep;
  108. applet_name = current_applet->name;
  109. }
  110. int run_nofork_applet_prime(struct nofork_save_area *old, const struct bb_applet *a, char **argv)
  111. {
  112. int rc, argc;
  113. current_applet = a;
  114. applet_name = a->name;
  115. xfunc_error_retval = EXIT_FAILURE;
  116. /*option_mask32 = 0; - not needed */
  117. /* special flag for xfunc_die(). If xfunc will "die"
  118. * in NOFORK applet, xfunc_die() sees negative
  119. * die_sleep and longjmp here instead. */
  120. die_sleep = -1;
  121. argc = 1;
  122. while (argv[argc])
  123. argc++;
  124. rc = setjmp(die_jmp);
  125. if (!rc) {
  126. /* Some callers (xargs)
  127. * need argv untouched because they free argv[i]! */
  128. char *tmp_argv[argc+1];
  129. memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0]));
  130. /* Finally we can call NOFORK applet's main() */
  131. rc = a->main(argc, tmp_argv);
  132. } else { /* xfunc died in NOFORK applet */
  133. /* in case they meant to return 0... */
  134. if (rc == -2222)
  135. rc = 0;
  136. }
  137. /* Restoring globals */
  138. restore_nofork_data(old);
  139. return rc;
  140. }
  141. int run_nofork_applet(const struct bb_applet *a, char **argv)
  142. {
  143. struct nofork_save_area old;
  144. /* Saving globals */
  145. save_nofork_data(&old);
  146. return run_nofork_applet_prime(&old, a, argv);
  147. }
  148. #endif /* FEATURE_PREFER_APPLETS */
  149. int spawn_and_wait(char **argv)
  150. {
  151. int rc;
  152. #if ENABLE_FEATURE_PREFER_APPLETS
  153. const struct bb_applet *a = find_applet_by_name(argv[0]);
  154. if (a && (a->nofork
  155. #if BB_MMU
  156. || a->noexec /* NOEXEC trick needs fork() */
  157. #endif
  158. )) {
  159. #if BB_MMU
  160. if (a->nofork)
  161. #endif
  162. {
  163. return run_nofork_applet(a, argv);
  164. }
  165. #if BB_MMU
  166. /* MMU only */
  167. /* a->noexec is true */
  168. rc = fork();
  169. if (rc) /* parent or error */
  170. return wait4pid(rc);
  171. /* child */
  172. xfunc_error_retval = EXIT_FAILURE;
  173. current_applet = a;
  174. run_current_applet_and_exit(argv);
  175. #endif
  176. }
  177. #endif /* FEATURE_PREFER_APPLETS */
  178. rc = spawn(argv);
  179. return wait4pid(rc);
  180. }
  181. #if !BB_MMU
  182. void 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("exec %s", bb_busybox_exec_path);
  189. }
  190. void forkexit_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;
  196. pid = vfork();
  197. if (pid < 0) /* wtf? */
  198. bb_perror_msg_and_die("vfork");
  199. if (pid) /* parent */
  200. exit(0);
  201. /* child - re-exec ourself */
  202. re_exec(argv);
  203. }
  204. #else
  205. /* Dance around (void)...*/
  206. #undef forkexit_or_rexec
  207. void forkexit_or_rexec(void)
  208. {
  209. pid_t pid;
  210. pid = fork();
  211. if (pid < 0) /* wtf? */
  212. bb_perror_msg_and_die("fork");
  213. if (pid) /* parent */
  214. exit(0);
  215. /* child */
  216. }
  217. #define forkexit_or_rexec(argv) forkexit_or_rexec()
  218. #endif
  219. /* Due to a #define in libbb.h on MMU systems we actually have 1 argument -
  220. * char **argv "vanishes" */
  221. void bb_daemonize_or_rexec(int flags, char **argv)
  222. {
  223. int fd;
  224. if (flags & DAEMON_CHDIR_ROOT)
  225. xchdir("/");
  226. if (flags & DAEMON_DEVNULL_STDIO) {
  227. close(0);
  228. close(1);
  229. close(2);
  230. }
  231. fd = xopen(bb_dev_null, O_RDWR);
  232. while ((unsigned)fd < 2)
  233. fd = dup(fd); /* have 0,1,2 open at least to /dev/null */
  234. if (!(flags & DAEMON_ONLY_SANITIZE)) {
  235. forkexit_or_rexec(argv);
  236. /* if daemonizing, make sure we detach from stdio & ctty */
  237. setsid();
  238. dup2(fd, 0);
  239. dup2(fd, 1);
  240. dup2(fd, 2);
  241. }
  242. while (fd > 2) {
  243. close(fd--);
  244. if (!(flags & DAEMON_CLOSE_EXTRA_FDS))
  245. return;
  246. /* else close everything after fd#2 */
  247. }
  248. }
  249. void bb_sanitize_stdio(void)
  250. {
  251. bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE, NULL);
  252. }