* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#define RCSID "$Id: main.c,v 1.152 2005/08/25 23:59:34 paulus Exp $"
+#define RCSID "$Id: main.c,v 1.156 2008/06/23 11:47:18 paulus Exp $"
#include <stdio.h>
#include <ctype.h>
char *prog;
void (*done) __P((void *));
void *arg;
+ int killable;
struct subprocess *next;
};
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 *));
int sig;
{
struct sigaction act, oldact;
+ struct subprocess *chp;
+ if (!detached) {
+ /*
+ * There might be other things in our process group that we
+ * didn't start that would get hit if we did a kill(0), so
+ * just send the signal individually to our children.
+ */
+ for (chp = children; chp != NULL; chp = chp->next)
+ if (chp->killable)
+ kill(chp->pid, sig);
+ return;
+ }
+
+ /* We've done a setsid(), so we can just use a kill(0) */
sigemptyset(&act.sa_mask); /* unnecessary in fact */
act.sa_handler = SIG_IGN;
act.sa_flags = 0;
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);
if (errfd != 2)
dup2(errfd, 2);
- closelog();
if (log_to_fd > 2)
close(log_to_fd);
if (the_channel->close)
}
if (pid != 0) {
- if (dont_wait) {
- record_child(pid, program, NULL, NULL);
- status = 0;
- } else {
+ record_child(pid, program, NULL, NULL, 1);
+ status = 0;
+ if (!dont_wait) {
while (waitpid(pid, &status, 0) < 0) {
if (errno == EINTR)
continue;
fatal("error waiting for (dis)connection process: %m");
}
+ forget_child(pid, status);
--conn_running;
}
return (status == 0 ? 0 : -1);
/*
- * run-program - execute a program with given arguments,
+ * run_program - execute a program with given arguments,
* 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.
if (pid != 0) {
if (debug)
dbglog("Script %s started (pid %d)", prog, pid);
- record_child(pid, prog, done, arg);
+ record_child(pid, prog, done, arg, 0);
if (wait) {
while (waitpid(pid, &status, 0) < 0) {
if (errno == EINTR)
continue;
fatal("error waiting for script %s: %m", prog);
}
- reap_kids();
+ forget_child(pid, status);
}
return pid;
}
* to use.
*/
void
-record_child(pid, prog, done, arg)
+record_child(pid, prog, done, arg, killable)
int pid;
char *prog;
void (*done) __P((void *));
void *arg;
+ int killable;
{
struct subprocess *chp;
chp->done = done;
chp->arg = arg;
chp->next = children;
+ chp->killable = killable;
children = chp;
}
}
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.
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)
dbuf.dptr = vbuf;
dbuf.dsize = vlen;
if (tdb_store(pppdb, key, dbuf, TDB_REPLACE))
- error("tdb_store failed: %s", tdb_error(pppdb));
+ error("tdb_store failed: %s", tdb_errorstr(pppdb));
if (vbuf)
free(vbuf);
dbuf.dptr = db_key;
dbuf.dsize = strlen(db_key);
if (tdb_store(pppdb, key, dbuf, TDB_REPLACE))
- error("tdb_store key failed: %s", tdb_error(pppdb));
+ error("tdb_store key failed: %s", tdb_errorstr(pppdb));
}
/*