--- ray/src/common/unix_process.c 2004/09/19 08:42:22 3.6 +++ ray/src/common/unix_process.c 2016/03/04 02:48:14 3.11 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: unix_process.c,v 3.6 2004/09/19 08:42:22 greg Exp $"; +static const char RCSid[] = "$Id: unix_process.c,v 3.11 2016/03/04 02:48:14 greg Exp $"; #endif /* * Routines to communicate with separate process via dual pipes @@ -13,8 +13,10 @@ static const char RCSid[] = "$Id: unix_process.c,v 3.6 #include #include #include +#include #include "rtprocess.h" +#include "rtio.h" int @@ -23,18 +25,19 @@ SUBPROC *pd, char *av[] ) { - extern char *getpath(), *getenv(); char *compath; int p0[2], p1[2]; - pd->running = 0; /* not yet */ - /* find executable */ - compath = getpath(av[0], getenv("PATH"), 1); - if (compath == 0) + pd->pid = 0; + pd->running = 0; /* not going yet */ + + if (av == NULL) /* cloning operation? */ + compath = NULL; + else if ((compath = getpath(av[0], getenv("PATH"), X_OK)) == NULL) return(0); if (pipe(p0) < 0 || pipe(p1) < 0) return(-1); - if ((pd->pid = fork()) == 0) { /* if child */ + if ((pd->pid = fork()) == 0) { /* if child... */ close(p0[1]); close(p1[0]); if (p0[0] != 0) { /* connect p0 to stdin */ @@ -45,7 +48,9 @@ char *av[] dup2(p1[1], 1); close(p1[1]); } - execv(compath, av); /* exec command */ + if (compath == NULL) /* just cloning? */ + return(0); + execv(compath, av); /* else exec command */ perror(compath); _exit(127); } @@ -67,22 +72,44 @@ char *av[] } - int -close_process( /* close pipes and wait for process */ -SUBPROC *pd +close_processes( /* close pipes and wait for processes to finish */ +SUBPROC pd[], +int nproc ) { - int pid, status; + int togo = nproc; + int status, rtn_status = 0; + RT_PID pid; + int i; - if (!pd->running) - return(0); - close(pd->r); - close(pd->w); - pd->running = 0; - if (waitpid(pd->pid, &status, 0) == pd->pid) + for (i = 0; i < nproc; i++) /* close pipes, first */ + if (pd[i].running) { + close(pd[i].w); + close(pd[i].r); + pd[i].running = 0; + } + if (nproc == 1) { /* await specific process? */ + if (waitpid(pd->pid, &status, 0) != pd->pid) + return(-1); + pd->pid = 0; return(status>>8 & 0xff); - return(-1); /* ? unknown status */ + } + /* else unordered wait */ + while (togo > 0 && (pid = wait(&status)) >= 0) { + for (i = nproc; i-- > 0; ) + if (pd[i].pid == pid) { + pd[i].pid = 0; + --togo; + break; + } + if (i < 0) + continue; /* child we don't know? */ + status = status>>8 & 0xff; + if (status) /* record non-zero status */ + rtn_status = status; + } + if (togo) /* child went missing? */ + return(-1); + return(rtn_status); } - -