runsv: run ./finish with parameters (runit compat)

function                                             old     new   delta
startservice                                         288     377     +89
runsv_main                                          1763    1783     +20

Signed-off-by: Earl Chew <earl_chew@agilent.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Earl Chew 2009-08-02 02:23:27 +02:00 committed by Denys Vlasenko
parent aebb742939
commit 80a3418b8c

View File

@ -84,6 +84,7 @@ struct svdir {
int fdlock; int fdlock;
int fdcontrol; int fdcontrol;
int fdcontrolwrite; int fdcontrolwrite;
int wstat;
}; };
struct globals { struct globals {
@ -138,7 +139,8 @@ static void s_term(int sig_no UNUSED_PARAM)
write(selfpipe.wr, "", 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') { while ((*p = *to_add) != '\0') {
p++; p++;
@ -189,24 +191,26 @@ static void update_status(struct svdir *s)
char *p = stat_buf; char *p = stat_buf;
switch (s->state) { switch (s->state) {
case S_DOWN: case S_DOWN:
p = add_str(p, "down"); p = bb_stpcpy(p, "down");
break; break;
case S_RUN: case S_RUN:
p = add_str(p, "run"); p = bb_stpcpy(p, "run");
break; break;
case S_FINISH: case S_FINISH:
p = add_str(p, "finish"); p = bb_stpcpy(p, "finish");
break; break;
} }
if (s->ctrl & C_PAUSE) p = add_str(p, ", paused"); if (s->ctrl & C_PAUSE)
if (s->ctrl & C_TERM) p = add_str(p, ", got TERM"); p = bb_stpcpy(p, ", paused");
if (s->ctrl & C_TERM)
p = bb_stpcpy(p, ", got TERM");
if (s->state != S_DOWN) if (s->state != S_DOWN)
switch (s->sd_want) { switch (s->sd_want) {
case W_DOWN: case W_DOWN:
p = add_str(p, ", want down"); p = bb_stpcpy(p, ", want down");
break; break;
case W_EXIT: case W_EXIT:
p = add_str(p, ", want exit"); p = bb_stpcpy(p, ", want exit");
break; break;
} }
*p++ = '\n'; *p++ = '\n';
@ -263,7 +267,7 @@ static unsigned custom(struct svdir *s, char c)
warn_cannot("vfork for control/?"); warn_cannot("vfork for control/?");
return 0; return 0;
} }
if (!pid) { if (pid == 0) {
/* child */ /* child */
if (haslog && dup2(logpipe.wr, 1) == -1) if (haslog && dup2(logpipe.wr, 1) == -1)
warn_cannot("setup stdout for control/?"); warn_cannot("setup stdout for control/?");
@ -305,12 +309,33 @@ static void stopservice(struct svdir *s)
static void startservice(struct svdir *s) static void startservice(struct svdir *s)
{ {
int p; int p;
const char *run; const char *arg[4];
char exitcode[sizeof(int)*3 + 2];
char sigcode[sizeof(int)*3 + 2];
if (s->state == S_FINISH) if (s->state == S_FINISH) {
run = "./finish"; /* Two arguments are given to ./finish. The first one is ./run exit code,
else { * or -1 if ./run didnt exit normally. The second one is
run = "./run"; * 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)) {
sprintf(exitcode, "%u", (int) WEXITSTATUS(s->wstat));
arg[1] = exitcode;
}
//arg[2] = "0";
//if (WIFSIGNALED(s->wstat)) {
sprintf(sigcode, "%u", (int) WTERMSIG(s->wstat));
arg[2] = sigcode;
//}
arg[3] = NULL;
} else {
arg[0] = "./run";
arg[1] = NULL;
custom(s, 'u'); custom(s, 'u');
} }
@ -340,8 +365,8 @@ static void startservice(struct svdir *s)
, SIG_DFL);*/ , SIG_DFL);*/
sig_unblock(SIGCHLD); sig_unblock(SIGCHLD);
sig_unblock(SIGTERM); sig_unblock(SIGTERM);
execl(run, run, (char *) NULL); execv(arg[0], (char**) arg);
fatal2_cannot(s->islog ? "start log/" : "start ", run); fatal2_cannot(s->islog ? "start log/" : "start ", arg[0]);
} }
/* parent */ /* parent */
if (s->state != S_FINISH) { if (s->state != S_FINISH) {
@ -591,9 +616,10 @@ int runsv_main(int argc UNUSED_PARAM, char **argv)
if ((child == -1) && (errno != EINTR)) if ((child == -1) && (errno != EINTR))
break; break;
if (child == svd[0].pid) { if (child == svd[0].pid) {
svd[0].wstat = wstat;
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) {
@ -612,6 +638,7 @@ int runsv_main(int argc UNUSED_PARAM, char **argv)
} }
if (haslog) { if (haslog) {
if (child == svd[1].pid) { if (child == svd[1].pid) {
svd[0].wstat = wstat;
svd[1].pid = 0; svd[1].pid = 0;
pidchanged = 1; pidchanged = 1;
svd[1].state = S_DOWN; svd[1].state = S_DOWN;