123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- #include <paths.h>
- #include "busybox.h"
- pid_t spawn(char **argv)
- {
-
- volatile int failed;
- pid_t pid;
-
- failed = 0;
- pid = vfork();
- if (pid < 0)
- return pid;
- if (!pid) {
-
- BB_EXECVP(argv[0], argv);
-
- failed = errno;
- _exit(111);
- }
-
-
- if (failed) {
- errno = failed;
- return -1;
- }
- return pid;
- }
- pid_t xspawn(char **argv)
- {
- pid_t pid = spawn(argv);
- if (pid < 0)
- bb_simple_perror_msg_and_die(*argv);
- return pid;
- }
- int safe_waitpid(int pid, int *wstat, int options)
- {
- int r;
- do
- r = waitpid(pid, wstat, options);
- while ((r == -1) && (errno == EINTR));
- return r;
- }
- int wait_any_nohang(int *wstat)
- {
- return safe_waitpid(-1, wstat, WNOHANG);
- }
- int wait4pid(int pid)
- {
- int status;
- if (pid <= 0) {
-
-
- return -1;
- }
- if (safe_waitpid(pid, &status, 0) == -1)
- return -1;
- if (WIFEXITED(status))
- return WEXITSTATUS(status);
- if (WIFSIGNALED(status))
- return WTERMSIG(status) + 1000;
- return 0;
- }
- #if ENABLE_FEATURE_PREFER_APPLETS
- void save_nofork_data(struct nofork_save_area *save)
- {
- memcpy(&save->die_jmp, &die_jmp, sizeof(die_jmp));
- save->applet_name = applet_name;
- save->xfunc_error_retval = xfunc_error_retval;
- save->option_mask32 = option_mask32;
- save->die_sleep = die_sleep;
- save->saved = 1;
- }
- void restore_nofork_data(struct nofork_save_area *save)
- {
- memcpy(&die_jmp, &save->die_jmp, sizeof(die_jmp));
- applet_name = save->applet_name;
- xfunc_error_retval = save->xfunc_error_retval;
- option_mask32 = save->option_mask32;
- die_sleep = save->die_sleep;
- }
- int run_nofork_applet_prime(struct nofork_save_area *old, int applet_no, char **argv)
- {
- int rc, argc;
- applet_name = APPLET_NAME(applet_no);
- xfunc_error_retval = EXIT_FAILURE;
-
- die_sleep = -1;
-
- argc = 1;
- while (argv[argc])
- argc++;
- rc = setjmp(die_jmp);
- if (!rc) {
-
- char *tmp_argv[argc+1];
- memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0]));
-
- rc = applet_main[applet_no](argc, tmp_argv);
- } else {
-
- if (rc == -2222)
- rc = 0;
- }
-
- restore_nofork_data(old);
- return rc;
- }
- int run_nofork_applet(int applet_no, char **argv)
- {
- struct nofork_save_area old;
-
- save_nofork_data(&old);
- return run_nofork_applet_prime(&old, applet_no, argv);
- }
- #endif
- int spawn_and_wait(char **argv)
- {
- int rc;
- #if ENABLE_FEATURE_PREFER_APPLETS
- int a = find_applet_by_name(argv[0]);
- if (a >= 0 && (APPLET_IS_NOFORK(a)
- #if BB_MMU
- || APPLET_IS_NOEXEC(a)
- #endif
- )) {
- #if BB_MMU
- if (APPLET_IS_NOFORK(a))
- #endif
- {
- return run_nofork_applet(a, argv);
- }
- #if BB_MMU
-
-
- rc = fork();
- if (rc)
- return wait4pid(rc);
-
- xfunc_error_retval = EXIT_FAILURE;
- run_applet_no_and_exit(a, argv);
- #endif
- }
- #endif
- rc = spawn(argv);
- return wait4pid(rc);
- }
- #if !BB_MMU
- void re_exec(char **argv)
- {
-
- argv[0][0] |= 0x80;
- execv(bb_busybox_exec_path, argv);
- bb_perror_msg_and_die("exec %s", bb_busybox_exec_path);
- }
- void forkexit_or_rexec(char **argv)
- {
- pid_t pid;
-
- if (re_execed)
- return;
- pid = vfork();
- if (pid < 0)
- bb_perror_msg_and_die("vfork");
- if (pid)
- exit(0);
-
- re_exec(argv);
- }
- #else
- #undef forkexit_or_rexec
- void forkexit_or_rexec(void)
- {
- pid_t pid;
- pid = fork();
- if (pid < 0)
- bb_perror_msg_and_die("fork");
- if (pid)
- exit(0);
-
- }
- #define forkexit_or_rexec(argv) forkexit_or_rexec()
- #endif
- void bb_daemonize_or_rexec(int flags, char **argv)
- {
- int fd;
- if (flags & DAEMON_CHDIR_ROOT)
- xchdir("/");
- if (flags & DAEMON_DEVNULL_STDIO) {
- close(0);
- close(1);
- close(2);
- }
- fd = xopen(bb_dev_null, O_RDWR);
- while ((unsigned)fd < 2)
- fd = dup(fd);
- if (!(flags & DAEMON_ONLY_SANITIZE)) {
- forkexit_or_rexec(argv);
-
- setsid();
- dup2(fd, 0);
- dup2(fd, 1);
- dup2(fd, 2);
- }
- while (fd > 2) {
- close(fd--);
- if (!(flags & DAEMON_CLOSE_EXTRA_FDS))
- return;
-
- }
- }
- void bb_sanitize_stdio(void)
- {
- bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE, NULL);
- }
|