*: more readable handling of pipe fds. No code changes.

This commit is contained in:
Denis Vlasenko
2008-02-16 13:20:56 +00:00
parent 1e18f1bab3
commit 3718832a15
12 changed files with 88 additions and 82 deletions

View File

@@ -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];
};
@@ -130,13 +130,13 @@ static void warn_cannot(const char *m)
static void s_child(int sig_no)
{
write(selfpipe[1], "", 1);
write(selfpipe.wr, "", 1);
}
static void s_term(int sig_no)
{
sigterm = 1;
write(selfpipe[1], "", 1); /* XXX */
write(selfpipe.wr, "", 1); /* XXX */
}
static char *add_str(char *p, const char *to_add)
@@ -275,7 +275,7 @@ static unsigned custom(struct svdir *s, char c)
return 0;
}
if (!pid) {
if (haslog && dup2(logpipe[1], 1) == -1)
if (haslog && dup2(logpipe.wr, 1) == -1)
warn_cannot("setup stdout for control/?");
prog[0] = a;
prog[1] = NULL;
@@ -335,13 +335,14 @@ static void startservice(struct svdir *s)
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);
@@ -452,11 +453,11 @@ int runsv_main(int argc, char **argv)
bb_show_usage();
dir = argv[1];
xpipe(selfpipe);
close_on_exec_on(selfpipe[0]);
close_on_exec_on(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);
@@ -489,9 +490,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);
close_on_exec_on(logpipe[0]);
close_on_exec_on(logpipe[1]);
xpiped_pair(logpipe);
close_on_exec_on(logpipe.rd);
close_on_exec_on(logpipe.wr);
}
}
@@ -572,7 +573,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;
@@ -585,7 +586,7 @@ 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 (;;) {
@@ -630,7 +631,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)
@@ -649,11 +650,11 @@ int runsv_main(int argc, char **argv)
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;
}

View File

@@ -47,7 +47,7 @@ static char *svdir;
static int svnum;
static char *rplog;
static int rploglen;
static int logpipe[2];
static struct fd_pair logpipe;
static struct pollfd pfd[1];
static unsigned stamplog;
static smallint check = 1;
@@ -186,19 +186,19 @@ static int setup_log(void)
warnx("log must have at least seven characters");
return 0;
}
if (pipe(logpipe)) {
if (piped_pair(logpipe)) {
warnx("cannot create pipe for log");
return -1;
}
close_on_exec_on(logpipe[1]);
close_on_exec_on(logpipe[0]);
ndelay_on(logpipe[0]);
ndelay_on(logpipe[1]);
if (dup2(logpipe[1], 2) == -1) {
close_on_exec_on(logpipe.rd);
close_on_exec_on(logpipe.wr);
ndelay_on(logpipe.rd);
ndelay_on(logpipe.wr);
if (dup2(logpipe.wr, 2) == -1) {
warnx("cannot set filedescriptor for log");
return -1;
}
pfd[0].fd = logpipe[0];
pfd[0].fd = logpipe.rd;
pfd[0].events = POLLIN;
stamplog = monotonic_sec();
return 1;
@@ -296,7 +296,7 @@ int runsvdir_main(int argc, char **argv)
if (rplog) {
if ((int)(now - stamplog) >= 0) {
write(logpipe[1], ".", 1);
write(logpipe.wr, ".", 1);
stamplog = now + 900;
}
}
@@ -311,7 +311,7 @@ int runsvdir_main(int argc, char **argv)
sig_unblock(SIGCHLD);
if (pfd[0].revents & POLLIN) {
while (read(logpipe[0], &ch, 1) > 0) {
while (read(logpipe.rd, &ch, 1) > 0) {
if (ch) {
for (i = 6; i < rploglen; i++)
rplog[i-1] = rplog[i];