11 |
|
#include "standard.h" |
12 |
|
|
13 |
|
#ifdef F_SETLKW |
14 |
– |
|
14 |
|
#include "paths.h" |
15 |
|
#include <signal.h> |
16 |
|
#include <sys/types.h> |
17 |
|
#include <sys/stat.h> |
18 |
+ |
/* select call compatibility stuff */ |
19 |
+ |
#ifndef FD_SETSIZE |
20 |
+ |
#include <sys/param.h> |
21 |
+ |
#define FD_SETSIZE NOFILE /* maximum # select file descriptors */ |
22 |
+ |
#endif |
23 |
+ |
#ifndef FD_SET |
24 |
+ |
#ifndef NFDBITS |
25 |
+ |
#define NFDBITS (8*sizeof(int)) /* number of bits per fd_mask */ |
26 |
+ |
#endif |
27 |
+ |
#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) |
28 |
+ |
#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) |
29 |
+ |
#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) |
30 |
+ |
#define FD_ZERO(p) bzero((char *)(p), sizeof(*(p))) |
31 |
+ |
#endif |
32 |
|
|
33 |
|
#ifndef TIMELIM |
34 |
|
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
125 |
|
/* close input and output descriptors */ |
126 |
|
close(fileno(stdin)); |
127 |
|
close(fileno(stdout)); |
128 |
+ |
if (errfile == NULL) |
129 |
+ |
close(fileno(stderr)); |
130 |
|
/* create named pipes for input and output */ |
131 |
|
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
132 |
|
goto createrr; |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
< |
io_process() /* just act as conduits to and from actual process */ |
180 |
> |
io_process() /* just act as go-between for actual process */ |
181 |
|
{ |
182 |
|
register char *cp; |
183 |
|
register int nr, n; |
184 |
< |
char buf[512], *pfin, *pfout, *pferr; |
185 |
< |
int pid, pid2 = -1; |
184 |
> |
char buf[BUFSIZ], *pfin, *pfout, *pferr; |
185 |
> |
int pid, nfds; |
186 |
> |
int fdin, fdout, fderr = -1; |
187 |
> |
fd_set readfds, writefds, excepfds; |
188 |
|
/* load persist file */ |
189 |
|
while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { |
190 |
|
pflock(0); |
220 |
|
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
221 |
|
error(USER, errmsg); |
222 |
|
} |
223 |
< |
/* wake up rendering process */ |
223 |
> |
/* wake up rendering process */ |
224 |
|
if (kill(pid, SIGIO) < 0) |
225 |
|
error(SYSTEM, "cannot signal rendering process in io_process"); |
226 |
< |
pid = fork(); /* fork i/o process */ |
227 |
< |
if (pid < 0) |
228 |
< |
goto forkerr; |
229 |
< |
/* connect to appropriate pipe */ |
230 |
< |
if (pid) { /* parent passes renderer output */ |
231 |
< |
close(0); |
232 |
< |
if (pferr[0]) { |
233 |
< |
pid2 = fork(); /* fork another for stderr */ |
234 |
< |
if (pid2 < 0) |
235 |
< |
goto forkerr; |
226 |
> |
/* open named pipes, in order */ |
227 |
> |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
228 |
> |
error(SYSTEM, "cannot open input pipe in io_process"); |
229 |
> |
if ((fdout = open(pfout, O_RDONLY)) < 0) |
230 |
> |
error(SYSTEM, "cannot open output pipe in io_process"); |
231 |
> |
if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0) |
232 |
> |
error(SYSTEM, "cannot open error pipe in io_process"); |
233 |
> |
for ( ; ; ) { /* loop on select call */ |
234 |
> |
FD_ZERO(&readfds); |
235 |
> |
FD_ZERO(&writefds); |
236 |
> |
FD_ZERO(&excepfds); |
237 |
> |
nfds = 0; |
238 |
> |
if (fdin >= 0) { |
239 |
> |
FD_SET(fdin, &writefds); |
240 |
> |
FD_SET(fdin, &excepfds); |
241 |
> |
nfds = fdin+1; |
242 |
|
} |
243 |
< |
if (pid2) { /* parent is still stdout */ |
244 |
< |
if (open(pfout, O_RDONLY) != 0) |
245 |
< |
error(SYSTEM, |
246 |
< |
"cannot open output pipe in io_process"); |
224 |
< |
} else { /* second child is stderr */ |
225 |
< |
if (open(pferr, O_RDONLY) != 0) |
226 |
< |
error(SYSTEM, |
227 |
< |
"cannot open error pipe in io_process"); |
228 |
< |
dup2(2, 1); /* attach stdout to stderr */ |
243 |
> |
if (fdout >= 0) { |
244 |
> |
FD_SET(fdout, &readfds); |
245 |
> |
FD_SET(fdout, &excepfds); |
246 |
> |
nfds = fdout+1; |
247 |
|
} |
248 |
< |
} else { /* child passes renderer input */ |
249 |
< |
close(1); |
250 |
< |
if (open(pfin, O_WRONLY) != 1) |
251 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
248 |
> |
if (fderr >= 0) { |
249 |
> |
FD_SET(fderr, &readfds); |
250 |
> |
FD_SET(fderr, &excepfds); |
251 |
> |
nfds = fderr+1; |
252 |
> |
} |
253 |
> |
if (nfds == 0) |
254 |
> |
break; /* all done, exit */ |
255 |
> |
if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0) |
256 |
> |
error(SYSTEM, "error in select call in io_process"); |
257 |
> |
/* renderer stderr */ |
258 |
> |
if (fderr >= 0 && (FD_ISSET(fderr, &readfds) || |
259 |
> |
FD_ISSET(fderr, &excepfds))) { |
260 |
> |
nr = read(fderr, cp=buf, sizeof(buf)); |
261 |
> |
if (nr < 0) |
262 |
> |
goto readerr; |
263 |
> |
if (nr == 0) { |
264 |
> |
close(fderr); |
265 |
> |
close(2); |
266 |
> |
fderr = -1; |
267 |
> |
} else |
268 |
> |
do { /* write it all */ |
269 |
> |
if ((n = write(2, cp, nr)) <= 0) |
270 |
> |
goto writerr; |
271 |
> |
cp += n; |
272 |
> |
} while ((nr -= n) > 0); |
273 |
> |
} |
274 |
> |
/* renderer stdout */ |
275 |
> |
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
276 |
> |
FD_ISSET(fdout, &excepfds))) { |
277 |
> |
nr = read(fdout, cp=buf, sizeof(buf)); |
278 |
> |
if (nr < 0) |
279 |
> |
goto readerr; |
280 |
> |
if (nr == 0) { /* EOF */ |
281 |
> |
close(fdout); |
282 |
> |
close(1); |
283 |
> |
fdout = -1; |
284 |
> |
} else |
285 |
> |
do { /* write it all */ |
286 |
> |
if ((n = write(1, cp, nr)) <= 0) |
287 |
> |
goto writerr; |
288 |
> |
cp += n; |
289 |
> |
} while ((nr -= n) > 0); |
290 |
> |
} |
291 |
> |
/* renderer stdin */ |
292 |
> |
if (fdin >= 0 && (FD_ISSET(fdin, &writefds) || |
293 |
> |
FD_ISSET(fdin, &excepfds))) { |
294 |
> |
nr = read(0, buf, 512); /* use minimal buffer */ |
295 |
> |
if (nr < 0) |
296 |
> |
goto readerr; |
297 |
> |
if (nr == 0) { |
298 |
> |
close(0); |
299 |
> |
close(fdin); |
300 |
> |
fdin = -1; |
301 |
> |
} else if (write(fdin, buf, nr) < nr) |
302 |
> |
goto writerr; /* what else to do? */ |
303 |
> |
} |
304 |
|
} |
305 |
< |
/* pass input to output */ |
236 |
< |
/* read as much as we can, write all of it */ |
237 |
< |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) |
238 |
< |
do { |
239 |
< |
if ((n = write(1, cp, nr)) <= 0) |
240 |
< |
error(SYSTEM, "write error in io_process"); |
241 |
< |
cp += n; |
242 |
< |
} while ((nr -= n) > 0); |
243 |
< |
if (nr < 0) |
244 |
< |
error(SYSTEM, "read error in io_process"); |
245 |
< |
close(0); /* close input */ |
246 |
< |
close(1); /* close output */ |
247 |
< |
if (pid) /* parent waits for stdin child */ |
248 |
< |
wait(0); |
249 |
< |
if (pid2 > 0) /* wait for stderr child also */ |
250 |
< |
wait(0); |
251 |
< |
_exit(0); /* all done, exit (not quit!) */ |
305 |
> |
_exit(0); /* we ought to return renderer error status! */ |
306 |
|
formerr: |
307 |
|
error(USER, "format error in persist file"); |
308 |
< |
forkerr: |
309 |
< |
error(SYSTEM, "fork failed in io_process"); |
308 |
> |
readerr: |
309 |
> |
error(SYSTEM, "read error in io_process"); |
310 |
> |
writerr: |
311 |
> |
error(SYSTEM, "write error in io_process"); |
312 |
|
} |
313 |
|
|
314 |
|
#else |