runsv: random code savings, ~300 bytes

This commit is contained in:
Denis Vlasenko 2007-03-09 22:46:06 +00:00
parent bebbd8c9ba
commit 3aba666514

View File

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