1 |
schorsch |
3.1 |
#ifndef lint |
2 |
greg |
3.9 |
static const char RCSid[] = "$Id: unix_process.c,v 3.8 2006/06/07 17:52:03 schorsch 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 |
|
|
pd->running = 0; /* not yet */ |
32 |
|
|
/* find executable */ |
33 |
|
|
compath = getpath(av[0], getenv("PATH"), 1); |
34 |
|
|
if (compath == 0) |
35 |
|
|
return(0); |
36 |
|
|
if (pipe(p0) < 0 || pipe(p1) < 0) |
37 |
|
|
return(-1); |
38 |
greg |
3.4 |
if ((pd->pid = fork()) == 0) { /* if child */ |
39 |
schorsch |
3.1 |
close(p0[1]); |
40 |
|
|
close(p1[0]); |
41 |
|
|
if (p0[0] != 0) { /* connect p0 to stdin */ |
42 |
|
|
dup2(p0[0], 0); |
43 |
|
|
close(p0[0]); |
44 |
|
|
} |
45 |
|
|
if (p1[1] != 1) { /* connect p1 to stdout */ |
46 |
|
|
dup2(p1[1], 1); |
47 |
|
|
close(p1[1]); |
48 |
|
|
} |
49 |
|
|
execv(compath, av); /* exec command */ |
50 |
|
|
perror(compath); |
51 |
|
|
_exit(127); |
52 |
|
|
} |
53 |
|
|
if (pd->pid == -1) |
54 |
|
|
return(-1); |
55 |
|
|
close(p0[0]); |
56 |
|
|
close(p1[1]); |
57 |
|
|
pd->r = p1[0]; |
58 |
|
|
pd->w = p0[1]; |
59 |
greg |
3.6 |
/* |
60 |
|
|
* Close write stream on exec to avoid multiprocessing deadlock. |
61 |
|
|
* No use in read stream without it, so set flag there as well. |
62 |
|
|
* GW: This bug took me two days to figure out!! |
63 |
|
|
*/ |
64 |
|
|
fcntl(pd->r, F_SETFD, FD_CLOEXEC); |
65 |
|
|
fcntl(pd->w, F_SETFD, FD_CLOEXEC); |
66 |
schorsch |
3.1 |
pd->running = 1; |
67 |
|
|
return(PIPE_BUF); |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
int |
73 |
|
|
close_process( /* close pipes and wait for process */ |
74 |
|
|
SUBPROC *pd |
75 |
|
|
) |
76 |
|
|
{ |
77 |
schorsch |
3.8 |
int status; |
78 |
schorsch |
3.1 |
|
79 |
greg |
3.5 |
if (!pd->running) |
80 |
|
|
return(0); |
81 |
schorsch |
3.1 |
close(pd->w); |
82 |
|
|
pd->running = 0; |
83 |
greg |
3.9 |
if (waitpid(pd->pid, &status, 0) == pd->pid) { |
84 |
|
|
close(pd->r); |
85 |
greg |
3.5 |
return(status>>8 & 0xff); |
86 |
greg |
3.9 |
} |
87 |
schorsch |
3.1 |
return(-1); /* ? unknown status */ |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
|