X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=runit%2Frunsv.c;h=5b221e90ae4e51617af8198937d85aeeed29040f;hb=6c5bf0d347faded028e15d523c26a0d64c6d3920;hp=20f849528ed6e57119dd7e7d7449ec417377980b;hpb=b6adbf1be29841501cc49917249e85f273e1df7c;p=oweals%2Fbusybox.git diff --git a/runit/runsv.c b/runit/runsv.c index 20f849528..5b221e90a 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 @@ -33,7 +33,33 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "libbb.h" #include "runit_lib.h" -static int selfpipe[2]; +#if ENABLE_MONOTONIC_SYSCALL +#include + +/* libc has incredibly messy way of doing this, + * typically requiring -lrt. We just skip all this mess */ +static void gettimeofday_ns(struct timespec *ts) +{ + syscall(__NR_clock_gettime, CLOCK_REALTIME, ts); +} +#else +static void gettimeofday_ns(struct timespec *ts) +{ + if (sizeof(struct timeval) == sizeof(struct timespec) + && sizeof(((struct timeval*)ts)->tv_usec) == sizeof(ts->tv_nsec) + ) { + /* Cheat */ + gettimeofday((void*)ts, NULL); + ts->tv_nsec *= 1000; + } else { + extern void BUG_need_to_implement_gettimeofday_ns(void); + BUG_need_to_implement_gettimeofday_ns(); + } +} +#endif + +/* Compare possibly overflowing unsigned counters */ +#define LESS(a,b) ((int)((unsigned)(b) - (unsigned)(a)) > 0) /* state */ #define S_DOWN 0 @@ -52,22 +78,35 @@ struct svdir { int pid; smallint state; smallint ctrl; - smallint want; + smallint sd_want; smallint islog; - struct taia start; + struct timespec start; int fdlock; int fdcontrol; int fdcontrolwrite; + int wstat; }; -static struct svdir svd[2]; -static smallint sigterm; -static smallint haslog; -static smallint pidchanged = 1; -static int logpipe[2]; -static char *dir; - -#define usage() bb_show_usage() +struct globals { + smallint haslog; + smallint sigterm; + smallint pidchanged; + struct fd_pair selfpipe; + struct fd_pair logpipe; + char *dir; + struct svdir svd[2]; +} FIX_ALIASING; +#define G (*(struct globals*)&bb_common_bufsiz1) +#define haslog (G.haslog ) +#define sigterm (G.sigterm ) +#define pidchanged (G.pidchanged ) +#define selfpipe (G.selfpipe ) +#define logpipe (G.logpipe ) +#define dir (G.dir ) +#define svd (G.svd ) +#define INIT_G() do { \ + pidchanged = 1; \ +} while (0) static void fatal2_cannot(const char *m1, const char *m2) { @@ -88,23 +127,20 @@ static void warn_cannot(const char *m) { bb_perror_msg("%s: warning: cannot %s", dir, m); } -static void warnx_cannot(const char *m) -{ - bb_error_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) +/* libbb candidate */ +static char *bb_stpcpy(char *p, const char *to_add) { while ((*p = *to_add) != '\0') { p++; @@ -122,21 +158,11 @@ 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) { - unsigned long l; + ssize_t sz; int fd; - char status[20]; + svstatus_t status; /* pid */ if (pidchanged) { @@ -165,24 +191,26 @@ static void update_status(struct svdir *s) char *p = stat_buf; switch (s->state) { case S_DOWN: - p = add_str(p, "down"); + p = bb_stpcpy(p, "down"); break; case S_RUN: - p = add_str(p, "run"); + p = bb_stpcpy(p, "run"); break; case S_FINISH: - p = add_str(p, "finish"); + p = bb_stpcpy(p, "finish"); break; } - if (s->ctrl & C_PAUSE) p = add_str(p, ", paused"); - if (s->ctrl & C_TERM) p = add_str(p, ", got TERM"); + if (s->ctrl & C_PAUSE) + p = bb_stpcpy(p, ", paused"); + if (s->ctrl & C_TERM) + p = bb_stpcpy(p, ", got TERM"); if (s->state != S_DOWN) - switch (s->want) { + switch (s->sd_want) { case W_DOWN: - p = add_str(p, ", want down"); + p = bb_stpcpy(p, ", want down"); break; case W_EXIT: - p = add_str(p, ", want exit"); + p = bb_stpcpy(p, ", want exit"); break; } *p++ = '\n'; @@ -194,76 +222,64 @@ static void update_status(struct svdir *s) s->islog ? "log/supervise/stat" : "log/supervise/stat"+4); /* supervise compatibility */ - taia_pack(status, &s->start); - l = (unsigned long)s->pid; - status[12] = l; l >>=8; - status[13] = l; l >>=8; - status[14] = l; l >>=8; - status[15] = l; + memset(&status, 0, sizeof(status)); + status.time_be64 = SWAP_BE64(s->start.tv_sec + 0x400000000000000aULL); + status.time_nsec_be32 = SWAP_BE32(s->start.tv_nsec); + status.pid_le32 = SWAP_LE32(s->pid); if (s->ctrl & C_PAUSE) - status[16] = 1; - else - status[16] = 0; - if (s->want == W_UP) - status[17] = 'u'; + status.paused = 1; + if (s->sd_want == W_UP) + status.want = 'u'; else - status[17] = 'd'; + status.want = 'd'; if (s->ctrl & C_TERM) - status[18] = 1; - else - status[18] = 0; - status[19] = s->state; + status.got_term = 1; + status.run_or_finish = s->state; fd = open_trunc_or_warn("supervise/status.new"); if (fd < 0) return; - l = write(fd, status, sizeof(status)); - if (l < 0) { + sz = write(fd, &status, sizeof(status)); + close(fd); + if (sz != sizeof(status)) { warn_cannot("write supervise/status.new"); - close(fd); unlink("supervise/status.new"); return; } - close(fd); - if (l < sizeof(status)) { - warnx_cannot("write supervise/status.new: partial write"); - return; - } rename_or_warn("supervise/status.new", s->islog ? "log/supervise/status" : "log/supervise/status"+4); } 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; + 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) + if (pid == 0) { + /* 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; } - return !wait_exitcode(w); + return WEXITSTATUS(w) == 0; } } else { if (errno != ENOENT) @@ -279,12 +295,12 @@ static void stopservice(struct svdir *s) s->ctrl |= C_TERM; update_status(s); } - if (s->want == W_DOWN) { + if (s->sd_want == W_DOWN) { kill(s->pid, SIGCONT); custom(s, 'd'); return; } - if (s->want == W_EXIT) { + if (s->sd_want == W_EXIT) { kill(s->pid, SIGCONT); custom(s, 'x'); } @@ -293,46 +309,66 @@ static void stopservice(struct svdir *s) static void startservice(struct svdir *s) { int p; - char *run[2]; - - if (s->state == S_FINISH) - run[0] = (char*)"./finish"; - else { - run[0] = (char*)"./run"; + const char *arg[4]; + char exitcode[sizeof(int)*3 + 2]; + + if (s->state == S_FINISH) { +/* Two arguments are given to ./finish. The first one is ./run exit code, + * or -1 if ./run didnt exit normally. The second one is + * the least significant byte of the exit status as determined by waitpid; + * for instance it is 0 if ./run exited normally, and the signal number + * if ./run was terminated by a signal. If runsv cannot start ./run + * for some reason, the exit code is 111 and the status is 0. + */ + arg[0] = "./finish"; + arg[1] = "-1"; + if (WIFEXITED(s->wstat)) { + *utoa_to_buf(WEXITSTATUS(s->wstat), exitcode, sizeof(exitcode)) = '\0'; + arg[1] = exitcode; + } + //arg[2] = "0"; + //if (WIFSIGNALED(s->wstat)) { + arg[2] = utoa(WTERMSIG(s->wstat)); + //} + arg[3] = NULL; + } else { + arg[0] = "./run"; + arg[1] = NULL; 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) { - if (dup2(logpipe[0], 0) == -1) - fatal_cannot("setup filedescriptor for ./log/run"); - close(logpipe[1]); - if (chdir("./log") == -1) - fatal_cannot("change directory to ./log"); + xchdir("./log"); + close(logpipe.wr); + xdup2(logpipe.rd, 0); } else { - if (dup2(logpipe[1], 1) == -1) - fatal_cannot("setup filedescriptor for ./run"); - 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); + execv(arg[0], (char**) arg); + fatal2_cannot(s->islog ? "start log/" : "start ", arg[0]); } + /* parent */ if (s->state != S_FINISH) { - taia_now(&s->start); + gettimeofday_ns(&s->start); s->state = S_RUN; } s->pid = p; @@ -347,41 +383,49 @@ static int ctrl(struct svdir *s, char c) switch (c) { case 'd': /* down */ - s->want = W_DOWN; + s->sd_want = W_DOWN; update_status(s); - if (s->pid && s->state != S_FINISH) stopservice(s); + if (s->pid && s->state != S_FINISH) + stopservice(s); break; case 'u': /* up */ - s->want = W_UP; + s->sd_want = W_UP; update_status(s); - if (s->pid == 0) startservice(s); + if (s->pid == 0) + startservice(s); break; case 'x': /* exit */ - if (s->islog) break; - s->want = W_EXIT; + if (s->islog) + break; + s->sd_want = W_EXIT; update_status(s); /* FALLTHROUGH */ case 't': /* sig term */ - if (s->pid && s->state != S_FINISH) stopservice(s); + if (s->pid && s->state != S_FINISH) + stopservice(s); break; case 'k': /* sig kill */ - if (s->pid && !custom(s, c)) kill(s->pid, SIGKILL); + if (s->pid && !custom(s, c)) + kill(s->pid, SIGKILL); s->state = S_DOWN; break; case 'p': /* sig pause */ - if (s->pid && !custom(s, c)) kill(s->pid, SIGSTOP); + if (s->pid && !custom(s, c)) + kill(s->pid, SIGSTOP); s->ctrl |= C_PAUSE; update_status(s); break; case 'c': /* sig cont */ - if (s->pid && !custom(s, c)) kill(s->pid, SIGCONT); - if (s->ctrl & C_PAUSE) s->ctrl &= ~C_PAUSE; + if (s->pid && !custom(s, c)) + kill(s->pid, SIGCONT); + s->ctrl &= ~C_PAUSE; update_status(s); break; case 'o': /* once */ - s->want = W_DOWN; + s->sd_want = W_DOWN; update_status(s); - if (!s->pid) startservice(s); + if (!s->pid) + startservice(s); break; case 'a': /* sig alarm */ sig = SIGALRM; @@ -409,56 +453,59 @@ 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; int r; char buf[256]; - if (!argv[1] || argv[2]) usage(); - dir = argv[1]; + INIT_G(); + + dir = single_argv(argv); - 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; */ if (S_DOWN) svd[0].state = S_DOWN; /* otherwise already 0 (bss) */ if (C_NOOP) svd[0].ctrl = C_NOOP; - if (W_UP) svd[0].want = W_UP; + if (W_UP) svd[0].sd_want = W_UP; /* bss: svd[0].islog = 0; */ /* bss: svd[1].pid = 0; */ - taia_now(&svd[0].start); - if (stat("down", &s) != -1) svd[0].want = W_DOWN; + gettimeofday_ns(&svd[0].start); + if (stat("down", &s) != -1) + svd[0].sd_want = W_DOWN; if (stat("log", &s) == -1) { if (errno != ENOENT) warn_cannot("stat ./log"); } else { - if (!S_ISDIR(s.st_mode)) - warnx_cannot("stat log/down: log is not a directory"); - else { + if (!S_ISDIR(s.st_mode)) { + errno = 0; + warn_cannot("stat log/down: log is not a directory"); + } else { haslog = 1; svd[1].state = S_DOWN; svd[1].ctrl = C_NOOP; - svd[1].want = W_UP; + svd[1].sd_want = W_UP; svd[1].islog = 1; - taia_now(&svd[1].start); + gettimeofday_ns(&svd[1].start); if (stat("log/down", &s) != -1) - svd[1].want = W_DOWN; - xpipe(logpipe); - coe(logpipe[0]); - coe(logpipe[1]); + svd[1].sd_want = W_DOWN; + xpiped_pair(logpipe); + close_on_exec_on(logpipe.rd); + close_on_exec_on(logpipe.wr); } } @@ -478,7 +525,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); @@ -502,75 +549,73 @@ 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 (;;) { - iopause_fd x[3]; - struct taia deadline; - struct taia now; + struct pollfd x[3]; + unsigned deadline; char ch; if (haslog) - if (!svd[1].pid && svd[1].want == W_UP) + if (!svd[1].pid && svd[1].sd_want == W_UP) startservice(&svd[1]); if (!svd[0].pid) - if (svd[0].want == W_UP || svd[0].state == S_FINISH) + if (svd[0].sd_want == W_UP || svd[0].state == S_FINISH) startservice(&svd[0]); - x[0].fd = selfpipe[0]; - x[0].events = IOPAUSE_READ; + x[0].fd = selfpipe.rd; + x[0].events = POLLIN; x[1].fd = svd[0].fdcontrol; - x[1].events = IOPAUSE_READ; - if (haslog) { - x[2].fd = svd[1].fdcontrol; - x[2].events = IOPAUSE_READ; - } - taia_now(&now); - taia_uint(&deadline, 3600); - taia_add(&deadline, &now, &deadline); - + x[1].events = POLLIN; + /* x[2] is used only if haslog == 1 */ + x[2].fd = svd[1].fdcontrol; + x[2].events = POLLIN; sig_unblock(SIGTERM); sig_unblock(SIGCHLD); - iopause(x, 2+haslog, &deadline, &now); + poll(x, 2 + haslog, 3600*1000); 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); - if (!child) break; - if ((child == -1) && (errno != EINTR)) break; + child = wait_any_nohang(&wstat); + if (!child) + break; + if ((child == -1) && (errno != EINTR)) + break; if (child == svd[0].pid) { + svd[0].wstat = wstat; svd[0].pid = 0; pidchanged = 1; - svd[0].ctrl &=~ C_TERM; + svd[0].ctrl &= ~C_TERM; if (svd[0].state != S_FINISH) { fd = open_read("finish"); if (fd != -1) { @@ -581,26 +626,27 @@ int runsv_main(int argc, char **argv) } } svd[0].state = S_DOWN; - taia_uint(&deadline, 1); - taia_add(&deadline, &svd[0].start, &deadline); - taia_now(&svd[0].start); + deadline = svd[0].start.tv_sec + 1; + gettimeofday_ns(&svd[0].start); update_status(&svd[0]); - if (taia_less(&svd[0].start, &deadline)) sleep(1); + if (LESS(svd[0].start.tv_sec, deadline)) + sleep(1); } if (haslog) { if (child == svd[1].pid) { + svd[0].wstat = wstat; svd[1].pid = 0; pidchanged = 1; svd[1].state = S_DOWN; svd[1].ctrl &= ~C_TERM; - taia_uint(&deadline, 1); - taia_add(&deadline, &svd[1].start, &deadline); - taia_now(&svd[1].start); + deadline = svd[1].start.tv_sec + 1; + gettimeofday_ns(&svd[1].start); update_status(&svd[1]); - if (taia_less(&svd[1].start, &deadline)) sleep(1); + if (LESS(svd[1].start.tv_sec, deadline)) + sleep(1); } } - } + } /* for (;;) */ if (read(svd[0].fdcontrol, &ch, 1) == 1) ctrl(&svd[0], ch); if (haslog) @@ -612,22 +658,18 @@ int runsv_main(int argc, char **argv) sigterm = 0; } - if (svd[0].want == W_EXIT && svd[0].state == S_DOWN) { + if (svd[0].sd_want == W_EXIT && svd[0].state == S_DOWN) { if (svd[1].pid == 0) - _exit(0); - if (svd[1].want != W_EXIT) { - svd[1].want = W_EXIT; + _exit(EXIT_SUCCESS); + if (svd[1].sd_want != W_EXIT) { + svd[1].sd_want = W_EXIT; /* stopservice(&svd[1]); */ update_status(&svd[1]); - close(logpipe[1]); - close(logpipe[0]); - //if (close(logpipe[1]) == -1) - // warn_cannot("close logpipe[1]"); - //if (close(logpipe[0]) == -1) - // warn_cannot("close logpipe[0]"); + close(logpipe.wr); + close(logpipe.rd); } } - } + } /* for (;;) */ /* not reached */ return 0; }