#include <sys/types.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "client.h"
typedef enum _PROCSTATE {
PSrunning,
PSdead
} PROCSTATE;
struct _child {
char *c_name;
int c_readfd;
pid_t c_pid;
PROCSTATE c_state;
struct _child *c_next;
};
typedef struct _child CHILD;
static CHILD *childlist = NULL;
int activechildren = 0;
static int needscan = FALSE;
static void removechild(CHILD *);
static CHILD *copychild(CHILD *);
static void addchild(CHILD *);
static void readchild(CHILD *);
static pid_t waitproc(int *, int);
static void reap(int);
static void childscan(void);
static void
removechild(CHILD *child)
{
CHILD *pc, *prevpc;
debugmsg(DM_CALL, "removechild(%s, %d, %d) start",
child->c_name, child->c_pid, child->c_readfd);
for (pc = childlist, prevpc = NULL; pc != NULL;
prevpc = pc, pc = pc->c_next)
if (pc == child)
break;
if (pc == NULL)
error("RemoveChild called with bad child %s %d %d",
child->c_name, child->c_pid, child->c_readfd);
else {
sigset_t set, oset;
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigprocmask(SIG_BLOCK, &set, &oset);
if (prevpc != NULL)
prevpc->c_next = pc->c_next;
else
childlist = pc->c_next;
sigprocmask(SIG_SETMASK, &oset, NULL);
(void) free(child->c_name);
--activechildren;
(void) close(child->c_readfd);
(void) free(pc);
}
debugmsg(DM_CALL, "removechild() end");
}
static CHILD *
copychild(CHILD *child)
{
CHILD *newc;
newc = xmalloc(sizeof *newc);
newc->c_name = xstrdup(child->c_name);
newc->c_readfd = child->c_readfd;
newc->c_pid = child->c_pid;
newc->c_state = child->c_state;
newc->c_next = NULL;
return(newc);
}
static void
addchild(CHILD *child)
{
CHILD *pc;
debugmsg(DM_CALL, "addchild() start\n");
pc = copychild(child);
pc->c_next = childlist;
childlist = pc;
++activechildren;
debugmsg(DM_MISC,
"addchild() created '%s' pid %d fd %d (active=%d)\n",
child->c_name, child->c_pid, child->c_readfd, activechildren);
}
static void
readchild(CHILD *child)
{
char rbuf[BUFSIZ];
ssize_t amt;
debugmsg(DM_CALL, "[readchild(%s, %d, %d) start]",
child->c_name, child->c_pid, child->c_readfd);
if (child->c_name == NULL || child->c_readfd <= 0) {
debugmsg(DM_MISC, "[readchild(%s, %d, %d) bad child]",
child->c_name, child->c_pid, child->c_readfd);
return;
}
while ((amt = read(child->c_readfd, rbuf, sizeof(rbuf))) > 0) {
debugmsg(DM_MISC, "[readchild(%s, %d, %d) got %zd bytes]",
child->c_name, child->c_pid, child->c_readfd, amt);
(void) xwrite(fileno(stdout), rbuf, amt);
debugmsg(DM_MISC, "[readchild(%s, %d, %d) write done]",
child->c_name, child->c_pid, child->c_readfd);
}
debugmsg(DM_MISC, "readchild(%s, %d, %d) done: amt = %zd errno = %d\n",
child->c_name, child->c_pid, child->c_readfd, amt, errno);
if (amt == 0)
debugmsg(DM_MISC, "readchild(%s, %d, %d) at EOF\n",
child->c_name, child->c_pid, child->c_readfd);
}
static pid_t
waitproc(int *statval, int block)
{
int status;
pid_t pid;
int exitval;
debugmsg(DM_CALL, "waitproc() %s, active children = %d...\n",
(block) ? "blocking" : "nonblocking", activechildren);
pid = waitpid(-1, &status, (block) ? 0 : WNOHANG);
exitval = WEXITSTATUS(status);
if (pid > 0 && exitval != 0) {
nerrs++;
debugmsg(DM_MISC,
"Child process %d exited with status %d.\n",
pid, exitval);
}
if (statval)
*statval = exitval;
debugmsg(DM_CALL, "waitproc() done (activechildren = %d)\n",
activechildren);
return(pid);
}
static void
reap(int dummy)
{
CHILD *pc;
int save_errno = errno;
int status = 0;
pid_t pid;
debugmsg(DM_CALL, "reap() called\n");
for ( ; ; ) {
pid = waitproc(&status, FALSE);
debugmsg(DM_MISC,
"reap() pid = %d status = %d activechildren=%d\n",
pid, status, activechildren);
if (pid == 0)
break;
if (pid < 0) {
if (errno != ECHILD)
error("Wait failed: %s", SYSERR);
break;
}
for (pc = childlist; pc; pc = pc->c_next)
if (pc->c_pid == pid) {
needscan = TRUE;
pc->c_state = PSdead;
}
}
(void) signal(SIGCHLD, reap);
debugmsg(DM_CALL, "reap() done\n");
errno = save_errno;
}
static void
childscan(void)
{
CHILD *pc, *nextpc;
debugmsg(DM_CALL, "childscan() start");
for (pc = childlist; pc; pc = nextpc) {
nextpc = pc->c_next;
if (pc->c_state == PSdead) {
readchild(pc);
removechild(pc);
}
}
needscan = FALSE;
debugmsg(DM_CALL, "childscan() end");
}
void
waitup(void)
{
int count;
CHILD *pc;
fd_set *rchildfdsp = NULL;
int rchildfdsn = 0;
debugmsg(DM_CALL, "waitup() start\n");
if (needscan)
childscan();
if (activechildren <= 0)
return;
for (pc = childlist; pc; pc = pc->c_next)
if (pc->c_readfd > rchildfdsn)
rchildfdsn = pc->c_readfd;
rchildfdsp = xcalloc(howmany(rchildfdsn+1, NFDBITS), sizeof(fd_mask));
for (pc = childlist; pc; pc = pc->c_next)
if (pc->c_readfd > 0) {
debugmsg(DM_MISC, "waitup() select on %d (%s)\n",
pc->c_readfd, pc->c_name);
FD_SET(pc->c_readfd, rchildfdsp);
}
debugmsg(DM_MISC, "waitup() Call select(), activechildren=%d\n",
activechildren);
count = select(rchildfdsn+1, rchildfdsp, NULL, NULL, NULL);
debugmsg(DM_MISC, "waitup() select returned %d activechildren = %d\n",
count, activechildren);
if (count < 0) {
if (errno != EINTR)
error("Select failed reading children input: %s",
SYSERR);
free(rchildfdsp);
return;
}
if (count == 0) {
error("Select returned an unexpected count of 0.");
free(rchildfdsp);
return;
}
for (pc = childlist; pc && count > 0; pc = pc->c_next) {
if (pc->c_name && kill(pc->c_pid, 0) == -1 &&
errno == ESRCH) {
debugmsg(DM_MISC,
"waitup() proc %d (%s) died unexpectedly!",
pc->c_pid, pc->c_name);
pc->c_state = PSdead;
needscan = TRUE;
}
if (pc->c_name == NULL ||
!FD_ISSET(pc->c_readfd, rchildfdsp))
continue;
readchild(pc);
--count;
}
free(rchildfdsp);
debugmsg(DM_CALL, "waitup() end\n");
}
static int
setnonblocking(int fd)
{
int flags;
if ((flags = fcntl(fd, F_GETFL)) == -1)
return (-1);
if (flags & O_NONBLOCK)
return (0);
return (fcntl(fd, F_SETFL, flags | O_NONBLOCK));
}
int
spawn(struct cmd *cmd, struct cmd *cmdlist)
{
pid_t pid;
int fildes[2];
char *childname = cmd->c_name;
if (pipe(fildes) == -1) {
error("Cannot create pipe for %s: %s", childname, SYSERR);
return(-1);
}
pid = fork();
if (pid == (pid_t)-1) {
error("Cannot spawn child for %s: fork failed: %s",
childname, SYSERR);
return(-1);
} else if (pid > 0) {
static CHILD newchild;
(void) signal(SIGCHLD, reap);
newchild.c_next = NULL;
newchild.c_name = childname;
newchild.c_readfd = fildes[PIPE_READ];
newchild.c_pid = pid;
newchild.c_state = PSrunning;
(void) close(fildes[PIPE_WRITE]);
if (setnonblocking(newchild.c_readfd) < 0) {
error("Set nonblocking I/O failed: %s", SYSERR);
return(-1);
}
addchild(&newchild);
markassigned(cmd, cmdlist);
debugmsg(DM_CALL,
"spawn() Forked child %d for host %s active = %d\n",
pid, childname, activechildren);
return(pid);
} else {
(void) close(fildes[PIPE_READ]);
if (dup2(fildes[PIPE_WRITE], (int)fileno(stdout)) == -1) {
error("Cannot duplicate stdout file descriptor: %s",
SYSERR);
return(-1);
}
if (dup2(fildes[PIPE_WRITE], (int)fileno(stderr)) == -1) {
error("Cannot duplicate stderr file descriptor: %s",
SYSERR);
return(-1);
}
return(0);
}
}