--- ray/src/common/unix_process.c 2003/07/17 09:21:29 3.2 +++ ray/src/common/unix_process.c 2009/12/12 23:08:13 3.9 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: unix_process.c,v 3.2 2003/07/17 09:21:29 schorsch Exp $"; +static const char RCSid[] = "$Id: unix_process.c,v 3.9 2009/12/12 23:08:13 greg Exp $"; #endif /* * Routines to communicate with separate process via dual pipes @@ -12,9 +12,11 @@ static const char RCSid[] = "$Id: unix_process.c,v 3.2 #include #include +#include +#include #include "rtprocess.h" -#include "vfork.h" +#include "rtio.h" int @@ -23,7 +25,6 @@ SUBPROC *pd, char *av[] ) { - extern char *getpath(), *getenv(); char *compath; int p0[2], p1[2]; @@ -34,7 +35,7 @@ char *av[] return(0); if (pipe(p0) < 0 || pipe(p1) < 0) return(-1); - if ((pd->pid = vfork()) == 0) { /* if child */ + if ((pd->pid = fork()) == 0) { /* if child */ close(p0[1]); close(p1[0]); if (p0[0] != 0) { /* connect p0 to stdin */ @@ -55,6 +56,13 @@ char *av[] close(p1[1]); pd->r = p1[0]; pd->w = p0[1]; + /* + * Close write stream on exec to avoid multiprocessing deadlock. + * No use in read stream without it, so set flag there as well. + * GW: This bug took me two days to figure out!! + */ + fcntl(pd->r, F_SETFD, FD_CLOEXEC); + fcntl(pd->w, F_SETFD, FD_CLOEXEC); pd->running = 1; return(PIPE_BUF); } @@ -66,14 +74,16 @@ close_process( /* close pipes and wait for process */ SUBPROC *pd ) { - int pid, status; + int status; - close(pd->r); + if (!pd->running) + return(0); close(pd->w); pd->running = 0; - while ((pid = wait(&status)) != -1) - if (pid == pd->pid) - return(status>>8 & 0xff); + if (waitpid(pd->pid, &status, 0) == pd->pid) { + close(pd->r); + return(status>>8 & 0xff); + } return(-1); /* ? unknown status */ }