X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=pppd%2Fmain.c;h=7924b5df7dbb583ba4a1385f8ca4f9d436333bc9;hb=cd8692c099106d7acad6fd0215a899dae866792a;hp=b5702f2b07ffabe274661511c67d9ffb4cfda7ee;hpb=ba63d2751b8143c0c9d602e9790eda8e178966ce;p=ppp.git diff --git a/pppd/main.c b/pppd/main.c index b5702f2..7924b5d 100644 --- a/pppd/main.c +++ b/pppd/main.c @@ -66,7 +66,7 @@ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#define RCSID "$Id: main.c,v 1.150 2005/03/21 09:20:16 paulus Exp $" +#define RCSID "$Id: main.c,v 1.154 2006/12/19 07:48:19 paulus Exp $" #include #include @@ -242,6 +242,7 @@ static void toggle_debug __P((int)); static void open_ccp __P((int)); static void bad_signal __P((int)); static void holdoff_end __P((void *)); +static void forget_child __P((int pid, int status)); static int reap_kids __P((void)); static void childwait_end __P((void *)); @@ -534,6 +535,7 @@ main(argc, argv) script_unsetenv("BYTES_RCVD"); lcp_open(0); /* Start protocol */ + start_link(0); while (phase != PHASE_DEAD) { handle_events(); get_input(); @@ -1565,6 +1567,8 @@ safe_fork(int infd, int outfd, int errfd) if (errfd == 0 || errfd == 1) errfd = dup(errfd); + closelog(); + /* dup the in, out, err fds to 0, 1, 2 */ if (infd != 0) dup2(infd, 0); @@ -1573,7 +1577,6 @@ safe_fork(int infd, int outfd, int errfd) if (errfd != 2) dup2(errfd, 2); - closelog(); if (log_to_fd > 2) close(log_to_fd); if (the_channel->close) @@ -1661,7 +1664,7 @@ device_script(program, in, out, dont_wait) /* * run-program - execute a program with given arguments, - * but don't wait for it. + * but don't wait for it unless wait is non-zero. * If the program can't be executed, logs an error unless * must_exist is 0 and the program file doesn't exist. * Returns -1 if it couldn't fork, 0 if the file doesn't exist @@ -1670,14 +1673,15 @@ device_script(program, in, out, dont_wait) * reap_kids) iff the return value is > 0. */ pid_t -run_program(prog, args, must_exist, done, arg) +run_program(prog, args, must_exist, done, arg, wait) char *prog; char **args; int must_exist; void (*done) __P((void *)); void *arg; + int wait; { - int pid; + int pid, status; struct stat sbuf; /* @@ -1703,6 +1707,14 @@ run_program(prog, args, must_exist, done, arg) if (debug) dbglog("Script %s started (pid %d)", prog, pid); record_child(pid, prog, done, arg); + if (wait) { + while (waitpid(pid, &status, 0) < 0) { + if (errno == EINTR) + continue; + fatal("error waiting for script %s: %m", prog); + } + forget_child(pid, status); + } return pid; } @@ -1778,6 +1790,35 @@ childwait_end(arg) childwait_done = 1; } +/* + * forget_child - clean up after a dead child + */ +static void +forget_child(pid, status) + int pid, status; +{ + struct subprocess *chp, **prevp; + + for (prevp = &children; (chp = *prevp) != NULL; prevp = &chp->next) { + if (chp->pid == pid) { + --n_children; + *prevp = chp->next; + break; + } + } + if (WIFSIGNALED(status)) { + warn("Child process %s (pid %d) terminated with signal %d", + (chp? chp->prog: "??"), pid, WTERMSIG(status)); + } else if (debug) + dbglog("Script %s finished (pid %d), status = 0x%x", + (chp? chp->prog: "??"), pid, + WIFEXITED(status) ? WEXITSTATUS(status) : status); + if (chp && chp->done) + (*chp->done)(chp->arg); + if (chp) + free(chp); +} + /* * reap_kids - get status from any dead child processes, * and log a message for abnormal terminations. @@ -1786,29 +1827,11 @@ static int reap_kids() { int pid, status; - struct subprocess *chp, **prevp; if (n_children == 0) return 0; while ((pid = waitpid(-1, &status, WNOHANG)) != -1 && pid != 0) { - for (prevp = &children; (chp = *prevp) != NULL; prevp = &chp->next) { - if (chp->pid == pid) { - --n_children; - *prevp = chp->next; - break; - } - } - if (WIFSIGNALED(status)) { - warn("Child process %s (pid %d) terminated with signal %d", - (chp? chp->prog: "??"), pid, WTERMSIG(status)); - } else if (debug) - dbglog("Script %s finished (pid %d), status = 0x%x", - (chp? chp->prog: "??"), pid, - WIFEXITED(status) ? WEXITSTATUS(status) : status); - if (chp && chp->done) - (*chp->done)(chp->arg); - if (chp) - free(chp); + forget_child(pid, status); } if (pid == -1) { if (errno == ECHILD)