| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: unix_process.c,v 3.19 2024/06/04 21:47:55 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* Routines to communicate with separate process via dual pipes
|
| 6 |
* Unix version
|
| 7 |
*
|
| 8 |
* External symbols declared in standard.h
|
| 9 |
*/
|
| 10 |
|
| 11 |
#include "copyright.h"
|
| 12 |
|
| 13 |
#include <sys/wait.h>
|
| 14 |
#include <stdlib.h>
|
| 15 |
|
| 16 |
#include "rtprocess.h"
|
| 17 |
#include "rtio.h"
|
| 18 |
|
| 19 |
|
| 20 |
SUBPROC sp_inactive = SP_INACTIVE;
|
| 21 |
|
| 22 |
|
| 23 |
int
|
| 24 |
open_process( /* open communication to separate process */
|
| 25 |
SUBPROC *pd,
|
| 26 |
char *av[]
|
| 27 |
)
|
| 28 |
{
|
| 29 |
char *compath;
|
| 30 |
int p0[2], p1[2];
|
| 31 |
|
| 32 |
if (pd->pid > 0)
|
| 33 |
return(-1); /* need to close, first */
|
| 34 |
|
| 35 |
if ((pd->flags&(PF_FILT_INP|PF_FILT_OUT)) == (PF_FILT_INP|PF_FILT_OUT))
|
| 36 |
return(-1); /* circular process not supported */
|
| 37 |
|
| 38 |
pd->flags &= ~PF_RUNNING; /* not running for sure */
|
| 39 |
pd->pid = -1;
|
| 40 |
|
| 41 |
if (av == NULL) /* cloning operation? */
|
| 42 |
compath = NULL;
|
| 43 |
else if ((compath = getpath(av[0], getenv("PATH"), X_OK)) == NULL)
|
| 44 |
return(0);
|
| 45 |
|
| 46 |
if (pd->flags & PF_FILT_INP) { /* filterning input stream? */
|
| 47 |
if ((pd->r < 0) | (pd->r == 1))
|
| 48 |
return(-1);
|
| 49 |
p0[0] = pd->r;
|
| 50 |
p0[1] = -1;
|
| 51 |
} else if (pipe(p0) < 0)
|
| 52 |
return(-1);
|
| 53 |
|
| 54 |
if (pd->flags & PF_FILT_OUT) { /* filtering output stream? */
|
| 55 |
if (pd->w < 1)
|
| 56 |
return(-1);
|
| 57 |
p1[0] = -1;
|
| 58 |
p1[1] = pd->w;
|
| 59 |
} else if (pipe(p1) < 0)
|
| 60 |
return(-1);
|
| 61 |
pd->pid = fork();
|
| 62 |
if (pd->pid == 0) { /* if child... */
|
| 63 |
close(p0[1]);
|
| 64 |
close(p1[0]);
|
| 65 |
if (p0[0] != 0) { /* connect p0 to stdin */
|
| 66 |
if (dup2(p0[0], 0) < 0)
|
| 67 |
return(-1);
|
| 68 |
close(p0[0]);
|
| 69 |
}
|
| 70 |
if (p1[1] != 1) { /* connect p1 to stdout */
|
| 71 |
if (dup2(p1[1], 1) < 0)
|
| 72 |
return(-1);
|
| 73 |
close(p1[1]);
|
| 74 |
}
|
| 75 |
if (compath == NULL) /* just cloning? */
|
| 76 |
return(0);
|
| 77 |
/* else clear streams' FD_CLOEXEC */
|
| 78 |
if (p0[0] == 0)
|
| 79 |
fcntl(0, F_SETFD, 0);
|
| 80 |
if (p1[1] == 1)
|
| 81 |
fcntl(1, F_SETFD, 0);
|
| 82 |
execv(compath, av); /* exec command */
|
| 83 |
perror(compath);
|
| 84 |
_exit(127);
|
| 85 |
}
|
| 86 |
if (pd->pid == -1)
|
| 87 |
return(-1);
|
| 88 |
/* connect parent's streams */
|
| 89 |
if (!(pd->flags & PF_FILT_INP)) {
|
| 90 |
close(p0[0]);
|
| 91 |
pd->r = p1[0];
|
| 92 |
} else if (p1[0] != pd->r) {
|
| 93 |
if (dup2(p1[0], pd->r) < 0)
|
| 94 |
return(-1);
|
| 95 |
close(p1[0]);
|
| 96 |
}
|
| 97 |
if (!(pd->flags & PF_FILT_OUT)) {
|
| 98 |
close(p1[1]);
|
| 99 |
pd->w = p0[1];
|
| 100 |
} else if (p0[1] != pd->w) {
|
| 101 |
if (dup2(p0[1], pd->w) < 0)
|
| 102 |
return(-1);
|
| 103 |
close(p0[1]);
|
| 104 |
}
|
| 105 |
/*
|
| 106 |
* Close write stream on exec to avoid multiprocessing deadlock.
|
| 107 |
* No use in read stream without it, so set flag there as well.
|
| 108 |
* GW: This bug took me two days to figure out!!
|
| 109 |
*/
|
| 110 |
if (pd->r > 0)
|
| 111 |
fcntl(pd->r, F_SETFD, FD_CLOEXEC);
|
| 112 |
if (pd->w > 1)
|
| 113 |
fcntl(pd->w, F_SETFD, FD_CLOEXEC);
|
| 114 |
pd->flags |= PF_RUNNING;
|
| 115 |
return(PIPE_BUF);
|
| 116 |
}
|
| 117 |
|
| 118 |
|
| 119 |
int
|
| 120 |
close_processes( /* close pipes and wait for processes to finish */
|
| 121 |
SUBPROC pd[],
|
| 122 |
int nproc
|
| 123 |
)
|
| 124 |
{
|
| 125 |
int togo = nproc;
|
| 126 |
int status = 0, rtn_status = 0;
|
| 127 |
RT_PID pid;
|
| 128 |
int i;
|
| 129 |
|
| 130 |
for (i = 0; i < nproc; i++) /* close pipes, first */
|
| 131 |
if (pd[i].flags & PF_RUNNING) {
|
| 132 |
if (pd[i].w >= 0) close(pd[i].w);
|
| 133 |
if (pd[i].r >= 0) close(pd[i].r);
|
| 134 |
pd[i].flags &= ~PF_RUNNING;
|
| 135 |
} else
|
| 136 |
togo -= (pd[i].pid < 0);
|
| 137 |
if (nproc == 1) { /* await specific process? */
|
| 138 |
if (waitpid(pd->pid, &status, 0) != pd->pid)
|
| 139 |
return(-1);
|
| 140 |
*pd = sp_inactive;
|
| 141 |
return(status>>8 & 0xff);
|
| 142 |
}
|
| 143 |
/* else unordered wait */
|
| 144 |
while (togo > 0 && (pid = wait(&status)) >= 0) {
|
| 145 |
for (i = nproc; i-- > 0; )
|
| 146 |
if (pd[i].pid == pid)
|
| 147 |
break; /* found it! */
|
| 148 |
if (i < 0)
|
| 149 |
continue; /* or child not in our list */
|
| 150 |
pd[i] = sp_inactive;
|
| 151 |
--togo;
|
| 152 |
status = status>>8 & 0xff;
|
| 153 |
if (status) /* record non-zero status */
|
| 154 |
rtn_status = status;
|
| 155 |
}
|
| 156 |
if (togo) /* child went missing? */
|
| 157 |
return(-1);
|
| 158 |
return(rtn_status);
|
| 159 |
}
|