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