X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=runit%2Frunsv.c;h=6d34dc133005de7a4b6022327d5d6deb6f6ad3fc;hb=5a49d284a6a9f6cf2076f23561f95aebdfd44592;hp=1ee3dda019fc75f4efccc97c0d69a945bbb8df54;hpb=b9256054419582dc35917b1cb39c7e09f489dfad;p=oweals%2Fbusybox.git diff --git a/runit/runsv.c b/runit/runsv.c index 1ee3dda01..6d34dc133 100644 --- a/runit/runsv.c +++ b/runit/runsv.c @@ -25,7 +25,7 @@ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* Busyboxed by Denis Vlasenko */ +/* Busyboxed by Denys Vlasenko */ /* TODO: depends on runit_lib.c - review and reduce/eliminate */ #include @@ -90,8 +90,8 @@ struct globals { smallint haslog; smallint sigterm; smallint pidchanged; - int selfpipe[2]; - int logpipe[2]; + struct fd_pair selfpipe; + struct fd_pair logpipe; char *dir; struct svdir svd[2]; }; @@ -103,10 +103,9 @@ struct globals { #define logpipe (G.logpipe ) #define dir (G.dir ) #define svd (G.svd ) -#define INIT_G() \ - do { \ - pidchanged = 1; \ - } while (0) +#define INIT_G() do { \ + pidchanged = 1; \ +} while (0) static void fatal2_cannot(const char *m1, const char *m2) { @@ -128,15 +127,15 @@ static void warn_cannot(const char *m) bb_perror_msg("%s: warning: cannot %s", dir, m); } -static void s_child(int sig_no) +static void s_child(int sig_no UNUSED_PARAM) { - write(selfpipe[1], "", 1); + write(selfpipe.wr, "", 1); } -static void s_term(int sig_no) +static void s_term(int sig_no UNUSED_PARAM) { sigterm = 1; - write(selfpipe[1], "", 1); /* XXX */ + write(selfpipe.wr, "", 1); /* XXX */ } static char *add_str(char *p, const char *to_add) @@ -157,16 +156,6 @@ static int open_trunc_or_warn(const char *name) return fd; } -static int rename_or_warn(const char *old, const char *new) -{ - if (rename(old, new) == -1) { - bb_perror_msg("%s: warning: cannot rename %s to %s", - dir, old, new); - return -1; - } - return 0; -} - static void update_status(struct svdir *s) { ssize_t sz; @@ -258,32 +247,30 @@ static void update_status(struct svdir *s) static unsigned custom(struct svdir *s, char c) { - int pid; + pid_t pid; int w; char a[10]; struct stat st; - char *prog[2]; if (s->islog) return 0; strcpy(a, "control/?"); - a[8] = c; + a[8] = c; /* replace '?' */ if (stat(a, &st) == 0) { if (st.st_mode & S_IXUSR) { - pid = fork(); + pid = vfork(); if (pid == -1) { - warn_cannot("fork for control/?"); + warn_cannot("vfork for control/?"); return 0; } if (!pid) { - if (haslog && dup2(logpipe[1], 1) == -1) + /* child */ + if (haslog && dup2(logpipe.wr, 1) == -1) warn_cannot("setup stdout for control/?"); - prog[0] = a; - prog[1] = NULL; - execve(a, prog, environ); + execl(a, a, (char *) NULL); fatal_cannot("run control/?"); } - while (wait_pid(&w, pid) == -1) { - if (errno == EINTR) continue; + /* parent */ + if (safe_waitpid(pid, &w, 0) == -1) { warn_cannot("wait for child control/?"); return 0; } @@ -317,41 +304,45 @@ static void stopservice(struct svdir *s) static void startservice(struct svdir *s) { int p; - char *run[2]; + const char *run; if (s->state == S_FINISH) - run[0] = (char*)"./finish"; + run = "./finish"; else { - run[0] = (char*)"./run"; + run = "./run"; custom(s, 'u'); } - run[1] = NULL; if (s->pid != 0) stopservice(s); /* should never happen */ - while ((p = fork()) == -1) { - warn_cannot("fork, sleeping"); + while ((p = vfork()) == -1) { + warn_cannot("vfork, sleeping"); sleep(5); } if (p == 0) { /* child */ if (haslog) { + /* NB: bug alert! right order is close, then dup2 */ if (s->islog) { - xdup2(logpipe[0], 0); - close(logpipe[1]); xchdir("./log"); + close(logpipe.wr); + xdup2(logpipe.rd, 0); } else { - xdup2(logpipe[1], 1); - close(logpipe[0]); + close(logpipe.rd); + xdup2(logpipe.wr, 1); } } - signal(SIGCHLD, SIG_DFL); - signal(SIGTERM, SIG_DFL); + /* Non-ignored signals revert to SIG_DFL on exec anyway */ + /*bb_signals(0 + + (1 << SIGCHLD) + + (1 << SIGTERM) + , SIG_DFL);*/ sig_unblock(SIGCHLD); sig_unblock(SIGTERM); - execvp(*run, run); - fatal2_cannot(s->islog ? "start log/" : "start ", *run); + execl(run, run, (char *) NULL); + fatal2_cannot(s->islog ? "start log/" : "start ", run); } + /* parent */ if (s->state != S_FINISH) { gettimeofday_ns(&s->start); s->state = S_RUN; @@ -403,8 +394,7 @@ static int ctrl(struct svdir *s, char c) case 'c': /* sig cont */ if (s->pid && !custom(s, c)) kill(s->pid, SIGCONT); - if (s->ctrl & C_PAUSE) - s->ctrl &= ~C_PAUSE; + s->ctrl &= ~C_PAUSE; update_status(s); break; case 'o': /* once */ @@ -439,8 +429,8 @@ static int ctrl(struct svdir *s, char c) return 1; } -int runsv_main(int argc, char **argv); -int runsv_main(int argc, char **argv) +int runsv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int runsv_main(int argc UNUSED_PARAM, char **argv) { struct stat s; int fd; @@ -453,16 +443,16 @@ int runsv_main(int argc, char **argv) bb_show_usage(); dir = argv[1]; - xpipe(selfpipe); - coe(selfpipe[0]); - coe(selfpipe[1]); - ndelay_on(selfpipe[0]); - ndelay_on(selfpipe[1]); + xpiped_pair(selfpipe); + close_on_exec_on(selfpipe.rd); + close_on_exec_on(selfpipe.wr); + ndelay_on(selfpipe.rd); + ndelay_on(selfpipe.wr); sig_block(SIGCHLD); - sig_catch(SIGCHLD, s_child); + bb_signals_recursive_norestart(1 << SIGCHLD, s_child); sig_block(SIGTERM); - sig_catch(SIGTERM, s_term); + bb_signals_recursive_norestart(1 << SIGTERM, s_term); xchdir(dir); /* bss: svd[0].pid = 0; */ @@ -490,9 +480,9 @@ int runsv_main(int argc, char **argv) gettimeofday_ns(&svd[1].start); if (stat("log/down", &s) != -1) svd[1].want = W_DOWN; - xpipe(logpipe); - coe(logpipe[0]); - coe(logpipe[1]); + xpiped_pair(logpipe); + close_on_exec_on(logpipe.rd); + close_on_exec_on(logpipe.wr); } } @@ -512,7 +502,7 @@ int runsv_main(int argc, char **argv) O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600); if (lock_exnb(svd[0].fdlock) == -1) fatal_cannot("lock supervise/lock"); - coe(svd[0].fdlock); + close_on_exec_on(svd[0].fdlock); if (haslog) { if (mkdir("log/supervise", 0700) == -1) { r = readlink("log/supervise", buf, 256); @@ -536,30 +526,30 @@ int runsv_main(int argc, char **argv) O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600); if (lock_ex(svd[1].fdlock) == -1) fatal_cannot("lock log/supervise/lock"); - coe(svd[1].fdlock); + close_on_exec_on(svd[1].fdlock); } mkfifo("log/supervise/control"+4, 0600); svd[0].fdcontrol = xopen("log/supervise/control"+4, O_RDONLY|O_NDELAY); - coe(svd[0].fdcontrol); + close_on_exec_on(svd[0].fdcontrol); svd[0].fdcontrolwrite = xopen("log/supervise/control"+4, O_WRONLY|O_NDELAY); - coe(svd[0].fdcontrolwrite); + close_on_exec_on(svd[0].fdcontrolwrite); update_status(&svd[0]); if (haslog) { mkfifo("log/supervise/control", 0600); svd[1].fdcontrol = xopen("log/supervise/control", O_RDONLY|O_NDELAY); - coe(svd[1].fdcontrol); + close_on_exec_on(svd[1].fdcontrol); svd[1].fdcontrolwrite = xopen("log/supervise/control", O_WRONLY|O_NDELAY); - coe(svd[1].fdcontrolwrite); + close_on_exec_on(svd[1].fdcontrolwrite); update_status(&svd[1]); } mkfifo("log/supervise/ok"+4, 0600); fd = xopen("log/supervise/ok"+4, O_RDONLY|O_NDELAY); - coe(fd); + close_on_exec_on(fd); if (haslog) { mkfifo("log/supervise/ok", 0600); fd = xopen("log/supervise/ok", O_RDONLY|O_NDELAY); - coe(fd); + close_on_exec_on(fd); } for (;;) { struct pollfd x[3]; @@ -573,7 +563,7 @@ int runsv_main(int argc, char **argv) if (svd[0].want == W_UP || svd[0].state == S_FINISH) startservice(&svd[0]); - x[0].fd = selfpipe[0]; + x[0].fd = selfpipe.rd; x[0].events = POLLIN; x[1].fd = svd[0].fdcontrol; x[1].events = POLLIN; @@ -586,14 +576,14 @@ int runsv_main(int argc, char **argv) sig_block(SIGTERM); sig_block(SIGCHLD); - while (read(selfpipe[0], &ch, 1) == 1) + while (read(selfpipe.rd, &ch, 1) == 1) continue; for (;;) { - int child; + pid_t child; int wstat; - child = wait_nohang(&wstat); + child = wait_any_nohang(&wstat); if (!child) break; if ((child == -1) && (errno != EINTR)) @@ -631,7 +621,7 @@ int runsv_main(int argc, char **argv) sleep(1); } } - } + } /* for (;;) */ if (read(svd[0].fdcontrol, &ch, 1) == 1) ctrl(&svd[0], ch); if (haslog) @@ -645,16 +635,16 @@ int runsv_main(int argc, char **argv) if (svd[0].want == W_EXIT && svd[0].state == S_DOWN) { if (svd[1].pid == 0) - _exit(0); + _exit(EXIT_SUCCESS); if (svd[1].want != W_EXIT) { svd[1].want = W_EXIT; /* stopservice(&svd[1]); */ update_status(&svd[1]); - close(logpipe[1]); - close(logpipe[0]); + close(logpipe.wr); + close(logpipe.rd); } } - } + } /* for (;;) */ /* not reached */ return 0; }