3
0

vfork_daemon_rexec.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #include "NUM_APPLETS.h"
  19. #define NOFORK_SUPPORT ((NUM_APPLETS > 1) && (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_NOFORK))
  20. #define NOEXEC_SUPPORT ((NUM_APPLETS > 1) && (ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE))
  21. #if defined(__linux__) && (NUM_APPLETS > 1)
  22. # include <sys/prctl.h>
  23. # ifndef PR_SET_NAME
  24. # define PR_SET_NAME 15
  25. # endif
  26. # ifndef PR_GET_NAME
  27. # define PR_GET_NAME 16
  28. # endif
  29. void FAST_FUNC set_task_comm(const char *comm)
  30. {
  31. /* okay if too long (truncates) */
  32. prctl(PR_SET_NAME, (long)comm, 0, 0, 0);
  33. }
  34. #endif
  35. /*
  36. * NOFORK/NOEXEC support
  37. */
  38. #if NOFORK_SUPPORT
  39. static jmp_buf die_jmp;
  40. static void jump(void)
  41. {
  42. /* Special case. We arrive here if NOFORK applet
  43. * calls xfunc, which then decides to die.
  44. * We don't die, but instead jump back to caller.
  45. * NOFORK applets still cannot carelessly call xfuncs:
  46. * p = xmalloc(10);
  47. * q = xmalloc(10); // BUG! if this dies, we leak p!
  48. */
  49. /* | 0x100 allows to pass zero exitcode (longjmp can't pass 0).
  50. * This works because exitcodes are bytes,
  51. * run_nofork_applet() ensures that by "& 0xff"
  52. */
  53. longjmp(die_jmp, xfunc_error_retval | 0x100);
  54. }
  55. struct nofork_save_area {
  56. jmp_buf die_jmp;
  57. void (*die_func)(void);
  58. const char *applet_name;
  59. uint32_t option_mask32;
  60. smallint logmode;
  61. uint8_t xfunc_error_retval;
  62. };
  63. static void save_nofork_data(struct nofork_save_area *save)
  64. {
  65. memcpy(&save->die_jmp, &die_jmp, sizeof(die_jmp));
  66. save->die_func = die_func;
  67. save->applet_name = applet_name;
  68. save->option_mask32 = option_mask32;
  69. save->logmode = logmode;
  70. save->xfunc_error_retval = xfunc_error_retval;
  71. }
  72. static void restore_nofork_data(struct nofork_save_area *save)
  73. {
  74. memcpy(&die_jmp, &save->die_jmp, sizeof(die_jmp));
  75. die_func = save->die_func;
  76. applet_name = save->applet_name;
  77. option_mask32 = save->option_mask32;
  78. logmode = save->logmode;
  79. xfunc_error_retval = save->xfunc_error_retval;
  80. }
  81. int FAST_FUNC run_nofork_applet(int applet_no, char **argv)
  82. {
  83. int rc, argc;
  84. struct nofork_save_area old;
  85. save_nofork_data(&old);
  86. logmode = LOGMODE_STDIO;
  87. xfunc_error_retval = EXIT_FAILURE;
  88. /* In case getopt() was already called:
  89. * reset the libc getopt() function, which keeps internal state.
  90. * (getopt32() does it itself, but getopt() doesn't (and can't))
  91. */
  92. GETOPT_RESET();
  93. argc = string_array_len(argv);
  94. /* If xfunc "dies" in NOFORK applet, die_func longjmp's here instead */
  95. die_func = jump;
  96. rc = setjmp(die_jmp);
  97. if (!rc) {
  98. /* Some callers (xargs)
  99. * need argv untouched because they free argv[i]! */
  100. char *tmp_argv[argc+1];
  101. memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0]));
  102. applet_name = tmp_argv[0];
  103. /* longjmp's (instead of returning) if --help is seen */
  104. show_usage_if_dash_dash_help(applet_no, argv);
  105. /* Finally we can call NOFORK applet's main() */
  106. rc = applet_main[applet_no](argc, tmp_argv);
  107. /* Important for shells: `which CMD` was failing */
  108. fflush_all();
  109. } else {
  110. /* xfunc died in NOFORK applet */
  111. }
  112. /* Restoring some globals */
  113. restore_nofork_data(&old);
  114. /* Other globals can be simply reset to defaults */
  115. GETOPT_RESET();
  116. return rc & 0xff; /* don't confuse people with "exitcodes" >255 */
  117. }
  118. #endif
  119. #if NOEXEC_SUPPORT
  120. void FAST_FUNC run_noexec_applet_and_exit(int a, const char *name, char **argv)
  121. {
  122. /* reset some state and run without execing */
  123. /* msg_eol = "\n"; - no caller needs this reinited yet */
  124. logmode = LOGMODE_STDIO;
  125. xfunc_error_retval = EXIT_FAILURE;
  126. die_func = NULL;
  127. GETOPT_RESET();
  128. //TODO: think pidof, pgrep, pkill!
  129. //set_task_comm() makes our pidof find NOEXECs (e.g. "yes >/dev/null"),
  130. //but one from procps-ng-3.3.10 needs more!
  131. //Rewrite /proc/PID/cmdline? (need to save argv0 and length at init for this to work!)
  132. set_task_comm(name);
  133. /* applet_name is set by this function: */
  134. run_applet_no_and_exit(a, name, argv);
  135. }
  136. #endif
  137. /*
  138. * Higher-level code, hiding optional NOFORK/NOEXEC trickery.
  139. */
  140. /* This does a fork/exec in one call, using vfork(). Returns PID of new child,
  141. * -1 for failure. Runs argv[0], searching path if that has no / in it. */
  142. pid_t FAST_FUNC spawn(char **argv)
  143. {
  144. /* Compiler should not optimize stores here */
  145. volatile int failed;
  146. pid_t pid;
  147. fflush_all();
  148. /* Be nice to nommu machines. */
  149. failed = 0;
  150. pid = vfork();
  151. if (pid < 0) /* error */
  152. return pid;
  153. if (!pid) { /* child */
  154. /* This macro is ok - it doesn't do NOEXEC/NOFORK tricks */
  155. BB_EXECVP(argv[0], argv);
  156. /* We are (maybe) sharing a stack with blocked parent,
  157. * let parent know we failed and then exit to unblock parent
  158. * (but don't run atexit() stuff, which would screw up parent.)
  159. */
  160. failed = errno;
  161. /* mount, for example, does not want the message */
  162. /*bb_perror_msg("can't execute '%s'", argv[0]);*/
  163. _exit(111);
  164. }
  165. /* parent */
  166. /* Unfortunately, this is not reliable: according to standards
  167. * vfork() can be equivalent to fork() and we won't see value
  168. * of 'failed'.
  169. * Interested party can wait on pid and learn exit code.
  170. * If 111 - then it (most probably) failed to exec */
  171. if (failed) {
  172. safe_waitpid(pid, NULL, 0); /* prevent zombie */
  173. errno = failed;
  174. return -1;
  175. }
  176. return pid;
  177. }
  178. /* Die with an error message if we can't spawn a child process. */
  179. pid_t FAST_FUNC xspawn(char **argv)
  180. {
  181. pid_t pid = spawn(argv);
  182. if (pid < 0)
  183. bb_simple_perror_msg_and_die(*argv);
  184. return pid;
  185. }
  186. int FAST_FUNC spawn_and_wait(char **argv)
  187. {
  188. int rc;
  189. #if ENABLE_FEATURE_PREFER_APPLETS && (NUM_APPLETS > 1)
  190. int a = find_applet_by_name(argv[0]);
  191. if (a >= 0) {
  192. if (APPLET_IS_NOFORK(a))
  193. return run_nofork_applet(a, argv);
  194. # if BB_MMU /* NOEXEC needs fork(), thus this is done only on MMU machines: */
  195. if (APPLET_IS_NOEXEC(a)) {
  196. fflush_all();
  197. rc = fork();
  198. if (rc) /* parent or error */
  199. return wait4pid(rc);
  200. /* child */
  201. run_noexec_applet_and_exit(a, argv[0], argv);
  202. }
  203. # endif
  204. }
  205. #endif
  206. rc = spawn(argv);
  207. return wait4pid(rc);
  208. }
  209. #if !BB_MMU
  210. void FAST_FUNC re_exec(char **argv)
  211. {
  212. /* high-order bit of first char in argv[0] is a hidden
  213. * "we have (already) re-execed, don't do it again" flag */
  214. argv[0][0] |= 0x80;
  215. execv(bb_busybox_exec_path, argv);
  216. bb_perror_msg_and_die("can't execute '%s'", bb_busybox_exec_path);
  217. }
  218. pid_t FAST_FUNC fork_or_rexec(char **argv)
  219. {
  220. pid_t pid;
  221. /* Maybe we are already re-execed and come here again? */
  222. if (re_execed)
  223. return 0;
  224. /* fflush_all(); ? - so far all callers had no buffered output to flush */
  225. pid = xvfork();
  226. if (pid) /* parent */
  227. return pid;
  228. /* child - re-exec ourself */
  229. re_exec(argv);
  230. }
  231. #endif
  232. /* Due to a #define in libbb.h on MMU systems we actually have 1 argument -
  233. * char **argv "vanishes" */
  234. void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
  235. {
  236. int fd;
  237. if (flags & DAEMON_CHDIR_ROOT)
  238. xchdir("/");
  239. fd = open(bb_dev_null, O_RDWR);
  240. if (fd < 0) {
  241. /* NB: we can be called as bb_sanitize_stdio() from init
  242. * or mdev, and there /dev/null may legitimately not (yet) exist!
  243. * Do not use xopen above, but obtain _ANY_ open descriptor,
  244. * even bogus one as below. */
  245. fd = xopen("/", O_RDONLY); /* don't believe this can fail */
  246. }
  247. if (flags & DAEMON_DEVNULL_STDIO) {
  248. xdup2(fd, 0);
  249. xdup2(fd, 1);
  250. xdup2(fd, 2);
  251. } else {
  252. /* have 0,1,2 open at least to /dev/null */
  253. while ((unsigned)fd < 2)
  254. fd = dup(fd);
  255. }
  256. if (!(flags & DAEMON_ONLY_SANITIZE)) {
  257. /* fflush_all(); - add it in fork_or_rexec() if necessary */
  258. if (fork_or_rexec(argv))
  259. _exit(EXIT_SUCCESS); /* parent */
  260. /* if daemonizing, detach from stdio & ctty */
  261. setsid();
  262. dup2(fd, 0);
  263. dup2(fd, 1);
  264. dup2(fd, 2);
  265. // if (flags & DAEMON_DOUBLE_FORK) {
  266. // /* On Linux, session leader can acquire ctty
  267. // * unknowingly, by opening a tty.
  268. // * Prevent this: stop being a session leader.
  269. // */
  270. // if (fork_or_rexec(argv))
  271. // _exit(EXIT_SUCCESS); /* parent */
  272. // }
  273. }
  274. while (fd > 2) {
  275. close(fd--);
  276. if (!(flags & DAEMON_CLOSE_EXTRA_FDS))
  277. return;
  278. /* else close everything after fd#2 */
  279. }
  280. }
  281. void FAST_FUNC bb_sanitize_stdio(void)
  282. {
  283. bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE, NULL);
  284. }