1 |
< |
/* Copyright (c) 1996 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
4 |
> |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
|
#endif |
6 |
|
|
7 |
|
/* |
9 |
|
*/ |
10 |
|
|
11 |
|
#include "standard.h" |
12 |
+ |
#include "random.h" |
13 |
|
|
14 |
|
#ifdef F_SETLKW |
15 |
|
#include "paths.h" |
16 |
+ |
#include "selcall.h" |
17 |
|
#include <signal.h> |
16 |
– |
#include <sys/types.h> |
18 |
|
#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 |
19 |
|
|
20 |
|
#ifndef TIMELIM |
21 |
|
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
125 |
|
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
126 |
|
inpname, outpname, errname); |
127 |
|
n = strlen(buf); |
141 |
– |
lseek(persistfd, 0L, 0); |
128 |
|
if (write(persistfd, buf, n) < n) |
129 |
|
error(SYSTEM, "error writing persist file"); |
130 |
+ |
lseek(persistfd, 0L, 0); |
131 |
|
/* wait TIMELIM for someone to signal us */ |
132 |
|
got_io = 0; |
133 |
|
signal(SIGIO, sig_io); |
145 |
|
goto openerr; |
146 |
|
if (freopen(outpname, "w", stdout) == NULL) |
147 |
|
goto openerr; |
148 |
+ |
sleep(3); /* give them a chance to open their pipes */ |
149 |
|
if (errname[0]) { |
150 |
|
if (freopen(errname, "w", stderr) == NULL) |
151 |
|
goto openerr; |
170 |
|
register int nr, n; |
171 |
|
char buf[BUFSIZ], *pfin, *pfout, *pferr; |
172 |
|
int pid, nfds; |
173 |
< |
int fdin, fdout, fderr = -1; |
174 |
< |
fd_set readfds, writefds, excepfds; |
173 |
> |
int fdout, fderr = -1; |
174 |
> |
int status = 0; |
175 |
> |
fd_set readfds, excepfds; |
176 |
|
/* load persist file */ |
177 |
|
n = 40; |
178 |
|
while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { |
179 |
|
if (!n--) |
180 |
|
error(USER, "unattended persist file?"); |
181 |
|
pflock(0); |
182 |
< |
sleep(15); /* wait until ready */ |
182 |
> |
sleep(3+(3*getpid()+random())%13); /* wait until ready */ |
183 |
|
pflock(1); |
184 |
|
} |
185 |
|
if (nr < 0) |
215 |
|
/* wake up rendering process */ |
216 |
|
if (kill(pid, SIGIO) < 0) |
217 |
|
error(SYSTEM, "cannot signal rendering process in io_process"); |
218 |
< |
/* open named pipes, in order */ |
219 |
< |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
220 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
218 |
> |
/* fork child feeder process */ |
219 |
> |
pid = fork(); |
220 |
> |
if (pid < 0) |
221 |
> |
error(SYSTEM, "fork failed in io_process"); |
222 |
> |
if (pid == 0) { /* feeder loop */ |
223 |
> |
int fdin; |
224 |
> |
close(1); /* open input pipe */ |
225 |
> |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
226 |
> |
error(SYSTEM, "cannot open feed pipe in io_process"); |
227 |
> |
/* renderer stdin */ |
228 |
> |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) { |
229 |
> |
do { |
230 |
> |
if ((n = write(fdin, cp, nr)) <= 0) |
231 |
> |
goto writerr; |
232 |
> |
cp += n; |
233 |
> |
} while ((nr -= n) > 0); |
234 |
> |
} |
235 |
> |
if (nr < 0) |
236 |
> |
goto readerr; |
237 |
> |
_exit(0); |
238 |
> |
} |
239 |
> |
close(0); |
240 |
> |
/* open output pipes, in order */ |
241 |
|
if ((fdout = open(pfout, O_RDONLY)) < 0) |
242 |
|
error(SYSTEM, "cannot open output pipe in io_process"); |
243 |
|
if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0) |
244 |
|
error(SYSTEM, "cannot open error pipe in io_process"); |
245 |
< |
for ( ; ; ) { /* loop on select call */ |
245 |
> |
for ( ; ; ) { /* eater loop */ |
246 |
|
FD_ZERO(&readfds); |
238 |
– |
FD_ZERO(&writefds); |
247 |
|
FD_ZERO(&excepfds); |
248 |
|
nfds = 0; |
241 |
– |
if (fdin >= 0) { |
242 |
– |
FD_SET(fdin, &writefds); |
243 |
– |
FD_SET(fdin, &excepfds); |
244 |
– |
nfds = fdin+1; |
245 |
– |
} |
249 |
|
if (fdout >= 0) { |
250 |
|
FD_SET(fdout, &readfds); |
251 |
|
FD_SET(fdout, &excepfds); |
258 |
|
} |
259 |
|
if (nfds == 0) |
260 |
|
break; /* all done, exit */ |
261 |
< |
if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0) |
261 |
> |
if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0) |
262 |
|
error(SYSTEM, "error in select call in io_process"); |
263 |
|
/* renderer stderr */ |
264 |
|
if (fderr >= 0 && (FD_ISSET(fderr, &readfds) || |
268 |
|
goto readerr; |
269 |
|
if (nr == 0) { |
270 |
|
close(fderr); |
271 |
< |
close(2); |
271 |
> |
/* close(2); don't close stderr! */ |
272 |
|
fderr = -1; |
273 |
< |
} else |
274 |
< |
do { /* write it all */ |
273 |
> |
} else { |
274 |
> |
cp[nr] = '\0'; /* deduce status if we can */ |
275 |
> |
n = strlen(progname); |
276 |
> |
if (!strncmp(cp, progname, n) && |
277 |
> |
cp[n++] == ':' && |
278 |
> |
cp[n++] == ' ') { |
279 |
> |
register struct erract *ep; |
280 |
> |
for (ep = erract; ep < erract+NERRS; |
281 |
> |
ep++) |
282 |
> |
if (ep->pre[0] && |
283 |
> |
!strncmp(cp+n, ep->pre, |
284 |
> |
strlen(ep->pre))) { |
285 |
> |
status = ep->ec; |
286 |
> |
break; |
287 |
> |
} |
288 |
> |
} |
289 |
> |
do { /* write message */ |
290 |
|
if ((n = write(2, cp, nr)) <= 0) |
291 |
|
goto writerr; |
292 |
|
cp += n; |
293 |
|
} while ((nr -= n) > 0); |
294 |
+ |
} |
295 |
|
} |
296 |
|
/* renderer stdout */ |
297 |
|
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
310 |
|
cp += n; |
311 |
|
} while ((nr -= n) > 0); |
312 |
|
} |
294 |
– |
/* renderer stdin */ |
295 |
– |
if (fdin >= 0 && (FD_ISSET(fdin, &writefds) || |
296 |
– |
FD_ISSET(fdin, &excepfds))) { |
297 |
– |
nr = read(0, buf, 512); /* use minimal buffer */ |
298 |
– |
if (nr < 0) |
299 |
– |
goto readerr; |
300 |
– |
if (nr == 0) { |
301 |
– |
close(0); |
302 |
– |
close(fdin); |
303 |
– |
fdin = -1; |
304 |
– |
} else if (write(fdin, buf, nr) < nr) |
305 |
– |
goto writerr; /* what else to do? */ |
306 |
– |
} |
313 |
|
} |
314 |
< |
_exit(0); /* we ought to return renderer error status! */ |
314 |
> |
wait(0); /* wait for feeder process */ |
315 |
> |
_exit(status); |
316 |
|
formerr: |
317 |
|
error(USER, "format error in persist file"); |
318 |
|
readerr: |