diff --git a/runit/runsv.c b/runit/runsv.c index 4a71ae966..813709d41 100644 --- a/runit/runsv.c +++ b/runit/runsv.c @@ -23,20 +23,20 @@ static int selfpipe[2]; struct svdir { int pid; - int state; - int ctrl; - int want; + smallint state; + smallint ctrl; + smallint want; + smallint islog; struct taia start; int fdlock; int fdcontrol; int fdcontrolwrite; - int islog; }; static struct svdir svd[2]; -static int sigterm = 0; -static int haslog = 0; -static int pidchanged = 1; +static smallint sigterm; +static smallint haslog; +static smallint pidchanged = 1; static int logpipe[2]; static char *dir; @@ -66,8 +66,6 @@ static void warnx_cannot(const char *m) bb_error_msg("%s: warning: cannot %s", dir, m); } -static void stopservice(struct svdir *); - static void s_child(int sig_no) { write(selfpipe[1], "", 1); @@ -119,17 +117,14 @@ static void update_status(struct svdir *s) if (fd < 0) return; if (s->pid) { - char spid[sizeof(s->pid)*3 + 2]; - int size = sprintf(spid, "%d\n", s->pid); + char spid[sizeof(int)*3 + 2]; + int size = sprintf(spid, "%u\n", (unsigned)s->pid); write(fd, spid, size); } close(fd); - if (s->islog) { - if (rename_or_warn("supervise/pid.new", "log/supervise/pid")) - return; - } else if (rename_or_warn("supervise/pid.new", "supervise/pid")) { + if (rename_or_warn("supervise/pid.new", + s->islog ? "log/supervise/pid" : "log/supervise/pid"+4)) return; - } pidchanged = 0; } @@ -168,11 +163,8 @@ static void update_status(struct svdir *s) close(fd); } - if (s->islog) { - rename_or_warn("supervise/stat.new", "log/supervise/stat"); - } else { - rename_or_warn("supervise/stat.new", "log/supervise/stat"+4); - } + rename_or_warn("supervise/stat.new", + s->islog ? "log/supervise/stat" : "log/supervise/stat"+4); /* supervise compatibility */ taia_pack(status, &s->start); @@ -197,7 +189,7 @@ static void update_status(struct svdir *s) fd = open_trunc_or_warn("supervise/status.new"); if (fd < 0) return; - l = write(fd, status, sizeof status); + l = write(fd, status, sizeof(status)); if (l < 0) { warn_cannot("write supervise/status.new"); close(fd); @@ -205,15 +197,12 @@ static void update_status(struct svdir *s) return; } close(fd); - if (l < sizeof status) { + if (l < sizeof(status)) { warnx_cannot("write supervise/status.new: partial write"); return; } - if (s->islog) { - rename_or_warn("supervise/status.new", "log/supervise/status"); - } else { - rename_or_warn("supervise/status.new", "log/supervise/status"+4); - } + rename_or_warn("supervise/status.new", + s->islog ? "log/supervise/status" : "log/supervise/status"+4); } static unsigned custom(struct svdir *s, char c) @@ -225,7 +214,7 @@ static unsigned custom(struct svdir *s, char c) char *prog[2]; if (s->islog) return 0; - memcpy(a, "control/?", 10); + strcpy(a, "control/?"); a[8] = c; if (stat(a, &st) == 0) { if (st.st_mode & S_IXUSR) { @@ -238,7 +227,7 @@ static unsigned custom(struct svdir *s, char c) if (haslog && fd_copy(1, logpipe[1]) == -1) warn_cannot("setup stdout for control/?"); prog[0] = a; - prog[1] = 0; + prog[1] = NULL; execve(a, prog, environ); fatal_cannot("run control/?"); } @@ -249,24 +238,24 @@ static unsigned custom(struct svdir *s, char c) } return !wait_exitcode(w); } - } - else { - if (errno == ENOENT) return 0; - warn_cannot("stat control/?"); + } else { + if (errno != ENOENT) + warn_cannot("stat control/?"); } return 0; } static void stopservice(struct svdir *s) { - if (s->pid && ! custom(s, 't')) { + if (s->pid && !custom(s, 't')) { kill(s->pid, SIGTERM); - s->ctrl |=C_TERM; + s->ctrl |= C_TERM; update_status(s); } if (s->want == W_DOWN) { kill(s->pid, SIGCONT); - custom(s, 'd'); return; + custom(s, 'd'); + return; } if (s->want == W_EXIT) { kill(s->pid, SIGCONT); @@ -312,10 +301,7 @@ static void startservice(struct svdir *s) sig_uncatch(SIGTERM); sig_unblock(SIGTERM); execve(*run, run, environ); - if (s->islog) - fatal2_cannot("start log/", *run); - else - fatal2_cannot("start ", *run); + fatal2_cannot(s->islog ? "start log/" : "start ", *run); } if (s->state != S_FINISH) { taia_now(&s->start); @@ -329,6 +315,8 @@ static void startservice(struct svdir *s) static int ctrl(struct svdir *s, char c) { + int sig; + switch (c) { case 'd': /* down */ s->want = W_DOWN; @@ -344,23 +332,22 @@ static int ctrl(struct svdir *s, char c) if (s->islog) break; s->want = W_EXIT; update_status(s); - if (s->pid && s->state != S_FINISH) stopservice(s); - break; + /* FALLTHROUGH */ case 't': /* sig term */ 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); - s->ctrl |=C_PAUSE; + 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); + if (s->ctrl & C_PAUSE) s->ctrl &= ~C_PAUSE; update_status(s); break; case 'o': /* once */ @@ -369,25 +356,29 @@ static int ctrl(struct svdir *s, char c) if (!s->pid) startservice(s); break; case 'a': /* sig alarm */ - if (s->pid && ! custom(s, c)) kill(s->pid, SIGALRM); - break; + sig = SIGALRM; + goto sendsig; case 'h': /* sig hup */ - if (s->pid && ! custom(s, c)) kill(s->pid, SIGHUP); - break; + sig = SIGHUP; + goto sendsig; case 'i': /* sig int */ - if (s->pid && ! custom(s, c)) kill(s->pid, SIGINT); - break; + sig = SIGINT; + goto sendsig; case 'q': /* sig quit */ - if (s->pid && ! custom(s, c)) kill(s->pid, SIGQUIT); - break; + sig = SIGQUIT; + goto sendsig; case '1': /* sig usr1 */ - if (s->pid && ! custom(s, c)) kill(s->pid, SIGUSR1); - break; + sig = SIGUSR1; + goto sendsig; case '2': /* sig usr2 */ - if (s->pid && ! custom(s, c)) kill(s->pid, SIGUSR2); - break; + sig = SIGUSR2; + goto sendsig; } return 1; + sendsig: + if (s->pid && !custom(s, c)) + kill(s->pid, sig); + return 1; } int runsv_main(int argc, char **argv); @@ -413,12 +404,12 @@ int runsv_main(int argc, char **argv) sig_catch(SIGTERM, s_term); xchdir(dir); - svd[0].pid = 0; - svd[0].state = S_DOWN; - svd[0].ctrl = C_NOOP; - svd[0].want = W_UP; - svd[0].islog = 0; - svd[1].pid = 0; + /* 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; + /* 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; @@ -445,10 +436,10 @@ int runsv_main(int argc, char **argv) } if (mkdir("supervise", 0700) == -1) { - r = readlink("supervise", buf, 256); + r = readlink("supervise", buf, sizeof(buf)); if (r != -1) { - if (r == 256) - fatal2x_cannot("readlink ./supervise: ", "name too long"); + if (r == sizeof(buf)) + fatal2x_cannot("readlink ./supervise", ": name too long"); buf[r] = 0; mkdir(buf, 0700); } else { @@ -466,7 +457,7 @@ int runsv_main(int argc, char **argv) r = readlink("log/supervise", buf, 256); if (r != -1) { if (r == 256) - fatal2x_cannot("readlink ./log/supervise: ", "name too long"); + fatal2x_cannot("readlink ./log/supervise", ": name too long"); buf[r] = 0; fd = xopen(".", O_RDONLY|O_NDELAY); xchdir("./log"); @@ -552,7 +543,7 @@ int runsv_main(int argc, char **argv) if (child == svd[0].pid) { 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) { @@ -574,7 +565,7 @@ int runsv_main(int argc, char **argv) svd[1].pid = 0; pidchanged = 1; svd[1].state = S_DOWN; - svd[1].ctrl &=~C_TERM; + svd[1].ctrl &= ~C_TERM; taia_uint(&deadline, 1); taia_add(&deadline, &svd[1].start, &deadline); taia_now(&svd[1].start);