1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
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 "rtprocess.h" |
14 |
#include "vfork.h" |
15 |
|
16 |
|
17 |
int |
18 |
open_process( /* open communication to separate process */ |
19 |
SUBPROC *pd, |
20 |
char *av[] |
21 |
) |
22 |
{ |
23 |
extern char *getpath(), *getenv(); |
24 |
char *compath; |
25 |
int p0[2], p1[2]; |
26 |
|
27 |
pd->running = 0; /* not yet */ |
28 |
/* find executable */ |
29 |
compath = getpath(av[0], getenv("PATH"), 1); |
30 |
if (compath == 0) |
31 |
return(0); |
32 |
if (pipe(p0) < 0 || pipe(p1) < 0) |
33 |
return(-1); |
34 |
if ((pd->pid = vfork()) == 0) { /* if child */ |
35 |
close(p0[1]); |
36 |
close(p1[0]); |
37 |
if (p0[0] != 0) { /* connect p0 to stdin */ |
38 |
dup2(p0[0], 0); |
39 |
close(p0[0]); |
40 |
} |
41 |
if (p1[1] != 1) { /* connect p1 to stdout */ |
42 |
dup2(p1[1], 1); |
43 |
close(p1[1]); |
44 |
} |
45 |
execv(compath, av); /* exec command */ |
46 |
perror(compath); |
47 |
_exit(127); |
48 |
} |
49 |
if (pd->pid == -1) |
50 |
return(-1); |
51 |
close(p0[0]); |
52 |
close(p1[1]); |
53 |
pd->r = p1[0]; |
54 |
pd->w = p0[1]; |
55 |
pd->running = 1; |
56 |
return(PIPE_BUF); |
57 |
} |
58 |
|
59 |
|
60 |
|
61 |
int |
62 |
close_process( /* close pipes and wait for process */ |
63 |
SUBPROC *pd |
64 |
) |
65 |
{ |
66 |
int pid, status; |
67 |
|
68 |
close(pd->r); |
69 |
close(pd->w); |
70 |
pd->running = 0; |
71 |
while ((pid = wait(&status)) != -1) |
72 |
if (pid == pd->pid) |
73 |
return(status>>8 & 0xff); |
74 |
return(-1); /* ? unknown status */ |
75 |
} |
76 |
|
77 |
|