1 |
schorsch |
3.1 |
#ifndef lint |
2 |
greg |
3.11 |
static const char RCSid[] = "$Id: unix_process.c,v 3.10 2012/06/14 05:13:25 greg Exp $"; |
3 |
schorsch |
3.1 |
#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 |
schorsch |
3.2 |
|
13 |
|
|
#include <sys/types.h> |
14 |
|
|
#include <sys/wait.h> |
15 |
greg |
3.6 |
#include <fcntl.h> |
16 |
greg |
3.7 |
#include <stdlib.h> |
17 |
schorsch |
3.1 |
|
18 |
|
|
#include "rtprocess.h" |
19 |
greg |
3.7 |
#include "rtio.h" |
20 |
schorsch |
3.1 |
|
21 |
|
|
|
22 |
|
|
int |
23 |
|
|
open_process( /* open communication to separate process */ |
24 |
|
|
SUBPROC *pd, |
25 |
|
|
char *av[] |
26 |
|
|
) |
27 |
|
|
{ |
28 |
|
|
char *compath; |
29 |
|
|
int p0[2], p1[2]; |
30 |
|
|
|
31 |
greg |
3.11 |
pd->pid = 0; |
32 |
greg |
3.10 |
pd->running = 0; /* not going yet */ |
33 |
|
|
|
34 |
|
|
if (av == NULL) /* cloning operation? */ |
35 |
|
|
compath = NULL; |
36 |
|
|
else if ((compath = getpath(av[0], getenv("PATH"), X_OK)) == NULL) |
37 |
schorsch |
3.1 |
return(0); |
38 |
|
|
if (pipe(p0) < 0 || pipe(p1) < 0) |
39 |
|
|
return(-1); |
40 |
greg |
3.10 |
if ((pd->pid = fork()) == 0) { /* if child... */ |
41 |
schorsch |
3.1 |
close(p0[1]); |
42 |
|
|
close(p1[0]); |
43 |
|
|
if (p0[0] != 0) { /* connect p0 to stdin */ |
44 |
|
|
dup2(p0[0], 0); |
45 |
|
|
close(p0[0]); |
46 |
|
|
} |
47 |
|
|
if (p1[1] != 1) { /* connect p1 to stdout */ |
48 |
|
|
dup2(p1[1], 1); |
49 |
|
|
close(p1[1]); |
50 |
|
|
} |
51 |
greg |
3.10 |
if (compath == NULL) /* just cloning? */ |
52 |
|
|
return(0); |
53 |
|
|
execv(compath, av); /* else exec command */ |
54 |
schorsch |
3.1 |
perror(compath); |
55 |
|
|
_exit(127); |
56 |
|
|
} |
57 |
|
|
if (pd->pid == -1) |
58 |
|
|
return(-1); |
59 |
|
|
close(p0[0]); |
60 |
|
|
close(p1[1]); |
61 |
|
|
pd->r = p1[0]; |
62 |
|
|
pd->w = p0[1]; |
63 |
greg |
3.6 |
/* |
64 |
|
|
* Close write stream on exec to avoid multiprocessing deadlock. |
65 |
|
|
* No use in read stream without it, so set flag there as well. |
66 |
|
|
* GW: This bug took me two days to figure out!! |
67 |
|
|
*/ |
68 |
|
|
fcntl(pd->r, F_SETFD, FD_CLOEXEC); |
69 |
|
|
fcntl(pd->w, F_SETFD, FD_CLOEXEC); |
70 |
schorsch |
3.1 |
pd->running = 1; |
71 |
|
|
return(PIPE_BUF); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
|
75 |
|
|
int |
76 |
greg |
3.11 |
close_processes( /* close pipes and wait for processes to finish */ |
77 |
|
|
SUBPROC pd[], |
78 |
|
|
int nproc |
79 |
schorsch |
3.1 |
) |
80 |
|
|
{ |
81 |
greg |
3.11 |
int togo = nproc; |
82 |
|
|
int status, rtn_status = 0; |
83 |
|
|
RT_PID pid; |
84 |
|
|
int i; |
85 |
|
|
|
86 |
|
|
for (i = 0; i < nproc; i++) /* close pipes, first */ |
87 |
|
|
if (pd[i].running) { |
88 |
|
|
close(pd[i].w); |
89 |
|
|
close(pd[i].r); |
90 |
|
|
pd[i].running = 0; |
91 |
|
|
} |
92 |
|
|
if (nproc == 1) { /* await specific process? */ |
93 |
|
|
if (waitpid(pd->pid, &status, 0) != pd->pid) |
94 |
|
|
return(-1); |
95 |
|
|
pd->pid = 0; |
96 |
greg |
3.5 |
return(status>>8 & 0xff); |
97 |
greg |
3.11 |
} |
98 |
|
|
/* else unordered wait */ |
99 |
|
|
while (togo > 0 && (pid = wait(&status)) >= 0) { |
100 |
|
|
for (i = nproc; i-- > 0; ) |
101 |
|
|
if (pd[i].pid == pid) { |
102 |
|
|
pd[i].pid = 0; |
103 |
|
|
--togo; |
104 |
|
|
break; |
105 |
|
|
} |
106 |
|
|
if (i < 0) |
107 |
|
|
continue; /* child we don't know? */ |
108 |
|
|
status = status>>8 & 0xff; |
109 |
|
|
if (status) /* record non-zero status */ |
110 |
|
|
rtn_status = status; |
111 |
|
|
} |
112 |
|
|
if (togo) /* child went missing? */ |
113 |
|
|
return(-1); |
114 |
|
|
return(rtn_status); |
115 |
schorsch |
3.1 |
} |