1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Routines to communicate with separate process via dual pipes |
6 |
* |
7 |
* External symbols declared in standard.h |
8 |
*/ |
9 |
|
10 |
#include "copyright.h" |
11 |
|
12 |
/* find pipe buffer limit */ |
13 |
#include <sys/param.h> |
14 |
|
15 |
#ifndef PIPE_BUF |
16 |
#ifdef PIPSIZ |
17 |
#define PIPE_BUF PIPSIZ |
18 |
#else |
19 |
#ifdef PIPE_MAX |
20 |
#define PIPE_BUF PIPE_MAX |
21 |
#else |
22 |
#define PIPE_BUF 512 /* hyperconservative */ |
23 |
#endif |
24 |
#endif |
25 |
#endif |
26 |
|
27 |
#include "vfork.h" |
28 |
|
29 |
#ifndef BSD |
30 |
#include <errno.h> |
31 |
#endif |
32 |
|
33 |
|
34 |
int |
35 |
open_process(pd, av) /* open communication to separate process */ |
36 |
int pd[3]; |
37 |
char *av[]; |
38 |
{ |
39 |
extern char *getpath(), *getenv(); |
40 |
char *compath; |
41 |
int p0[2], p1[2]; |
42 |
/* find executable */ |
43 |
compath = getpath(av[0], getenv("PATH"), 1); |
44 |
if (compath == 0) |
45 |
return(0); |
46 |
if (pipe(p0) < 0 || pipe(p1) < 0) |
47 |
return(-1); |
48 |
if ((pd[2] = vfork()) == 0) { /* if child */ |
49 |
close(p0[1]); |
50 |
close(p1[0]); |
51 |
if (p0[0] != 0) { /* connect p0 to stdin */ |
52 |
dup2(p0[0], 0); |
53 |
close(p0[0]); |
54 |
} |
55 |
if (p1[1] != 1) { /* connect p1 to stdout */ |
56 |
dup2(p1[1], 1); |
57 |
close(p1[1]); |
58 |
} |
59 |
execv(compath, av); /* exec command */ |
60 |
perror(compath); |
61 |
_exit(127); |
62 |
} |
63 |
if (pd[2] == -1) |
64 |
return(-1); |
65 |
close(p0[0]); |
66 |
close(p1[1]); |
67 |
pd[0] = p1[0]; |
68 |
pd[1] = p0[1]; |
69 |
return(PIPE_BUF); |
70 |
} |
71 |
|
72 |
|
73 |
int |
74 |
process(pd, recvbuf, sendbuf, nbr, nbs) /* process data through pd */ |
75 |
int pd[3]; |
76 |
char *recvbuf, *sendbuf; |
77 |
int nbr, nbs; |
78 |
{ |
79 |
if (nbs > PIPE_BUF) |
80 |
return(-1); |
81 |
if (writebuf(pd[1], sendbuf, nbs) < nbs) |
82 |
return(-1); |
83 |
return(readbuf(pd[0], recvbuf, nbr)); |
84 |
} |
85 |
|
86 |
|
87 |
int |
88 |
close_process(pd) /* close pipes and wait for process */ |
89 |
int pd[3]; |
90 |
{ |
91 |
int pid, status; |
92 |
|
93 |
close(pd[1]); |
94 |
close(pd[0]); |
95 |
while ((pid = wait(&status)) != -1) |
96 |
if (pid == pd[2]) |
97 |
return(status>>8 & 0xff); |
98 |
return(-1); /* ? unknown status */ |
99 |
} |
100 |
|
101 |
|
102 |
int |
103 |
readbuf(fd, bpos, siz) /* read all of requested buffer */ |
104 |
int fd; |
105 |
char *bpos; |
106 |
int siz; |
107 |
{ |
108 |
register int cc = 0, nrem = siz; |
109 |
retry: |
110 |
while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) { |
111 |
bpos += cc; |
112 |
nrem -= cc; |
113 |
} |
114 |
if (cc < 0) { |
115 |
#ifndef BSD |
116 |
if (errno == EINTR) /* we were interrupted! */ |
117 |
goto retry; |
118 |
#endif |
119 |
return(cc); |
120 |
} |
121 |
return(siz-nrem); |
122 |
} |
123 |
|
124 |
|
125 |
int |
126 |
writebuf(fd, bpos, siz) /* write all of requested buffer */ |
127 |
int fd; |
128 |
char *bpos; |
129 |
int siz; |
130 |
{ |
131 |
register int cc = 0, nrem = siz; |
132 |
retry: |
133 |
while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) { |
134 |
bpos += cc; |
135 |
nrem -= cc; |
136 |
} |
137 |
if (cc < 0) { |
138 |
#ifndef BSD |
139 |
if (errno == EINTR) /* we were interrupted! */ |
140 |
goto retry; |
141 |
#endif |
142 |
return(cc); |
143 |
} |
144 |
return(siz-nrem); |
145 |
} |