1 |
– |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Routines for persistent rtrace and rpict processes. |
6 |
+ |
* |
7 |
+ |
* External symbols declared in ray.h |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#include "copyright.h" |
11 |
+ |
|
12 |
+ |
#include <string.h> |
13 |
+ |
#include <signal.h> |
14 |
+ |
#include <sys/stat.h> |
15 |
+ |
#ifdef _WIN32 |
16 |
+ |
#include <process.h> /* getpid() */ |
17 |
+ |
#endif |
18 |
+ |
|
19 |
|
#include "standard.h" |
20 |
+ |
#include "random.h" |
21 |
|
|
22 |
|
#ifdef F_SETLKW |
23 |
|
#include "paths.h" |
24 |
|
#include "selcall.h" |
16 |
– |
#include <signal.h> |
17 |
– |
#include <sys/stat.h> |
25 |
|
|
26 |
|
#ifndef TIMELIM |
27 |
|
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
28 |
|
#endif |
29 |
|
|
30 |
< |
extern char *strcpy(), *index(); |
30 |
> |
#ifndef freebsd |
31 |
> |
#define mkfifo(fn,md) mknod(fn, S_IFIFO|(md), 0) |
32 |
> |
#endif |
33 |
|
|
34 |
+ |
extern void io_process(); |
35 |
+ |
|
36 |
|
extern int headismine; /* boolean true if header belongs to me */ |
37 |
|
|
38 |
|
extern char *progname; /* global program name */ |
45 |
|
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; |
46 |
|
|
47 |
|
|
48 |
+ |
void |
49 |
|
pfdetach() /* release persist (and header) resources */ |
50 |
|
{ |
51 |
|
if (persistfd >= 0) |
59 |
|
} |
60 |
|
|
61 |
|
|
62 |
+ |
void |
63 |
|
pfclean() /* clean up persist files */ |
64 |
|
{ |
65 |
|
if (persistfd >= 0) |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
+ |
void |
79 |
|
pflock(lf) /* place or release exclusive lock on file */ |
80 |
|
int lf; |
81 |
|
{ |
112 |
|
|
113 |
|
static int got_io; |
114 |
|
|
115 |
< |
static int sig_io() { got_io++; } |
115 |
> |
static void sig_io() { got_io++; } |
116 |
|
|
117 |
< |
static int sig_alrm() { quit(0); } |
117 |
> |
static void sig_alrm() { quit(0); } |
118 |
|
|
119 |
|
|
120 |
+ |
void |
121 |
|
pfhold() /* holding pattern for idle rendering process */ |
122 |
|
{ |
123 |
< |
int (*oldalrm)(); |
123 |
> |
void (*oldalrm)(); |
124 |
|
char buf[512]; |
125 |
|
register int n; |
126 |
|
/* close input and output descriptors */ |
129 |
|
if (errfile == NULL) |
130 |
|
close(fileno(stderr)); |
131 |
|
/* create named pipes for input and output */ |
132 |
< |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
132 |
> |
if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0) |
133 |
|
goto createrr; |
134 |
< |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
134 |
> |
if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0) |
135 |
|
goto createrr; |
136 |
|
if (errfile == NULL && |
137 |
< |
mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
137 |
> |
mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0) |
138 |
|
goto createrr; |
139 |
|
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
140 |
|
inpname, outpname, errname); |
141 |
|
n = strlen(buf); |
142 |
|
if (write(persistfd, buf, n) < n) |
143 |
|
error(SYSTEM, "error writing persist file"); |
144 |
< |
lseek(persistfd, 0L, 0); |
144 |
> |
lseek(persistfd, (off_t)0L, 0); |
145 |
|
/* wait TIMELIM for someone to signal us */ |
146 |
|
got_io = 0; |
147 |
|
signal(SIGIO, sig_io); |
148 |
< |
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
148 |
> |
oldalrm = (void (*)())signal(SIGALRM, sig_alrm); |
149 |
|
alarm(TIMELIM); |
150 |
|
pflock(0); /* unlock persist file for attach */ |
151 |
|
while (!got_io) |
155 |
|
signal(SIGIO, SIG_DFL); |
156 |
|
pflock(1); /* grab persist file back */ |
157 |
|
/* someone wants us; reopen stdin and stdout */ |
158 |
+ |
/* |
159 |
|
if (freopen(inpname, "r", stdin) == NULL) |
160 |
|
goto openerr; |
161 |
|
if (freopen(outpname, "w", stdout) == NULL) |
162 |
|
goto openerr; |
163 |
+ |
*/ |
164 |
+ |
close(0); |
165 |
+ |
if (open(inpname, O_RDONLY) != 0) |
166 |
+ |
error(INTERNAL, "unexpected stdin file number"); |
167 |
+ |
clearerr(stdin); |
168 |
+ |
close(1); |
169 |
+ |
if (open(outpname, O_WRONLY) != 1) |
170 |
+ |
error(INTERNAL, "unexpected stdout file number"); |
171 |
+ |
sleep(3); /* give them a chance to open their pipes */ |
172 |
|
if (errname[0]) { |
173 |
< |
if (freopen(errname, "w", stderr) == NULL) |
174 |
< |
goto openerr; |
173 |
> |
close(2); |
174 |
> |
if (open(errname, O_WRONLY) != 2) |
175 |
> |
error(INTERNAL, "unexpected stderr file number"); |
176 |
|
unlink(errname); |
177 |
|
errname[0] = '\0'; |
178 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
+ |
void |
192 |
|
io_process() /* just act as go-between for actual process */ |
193 |
|
{ |
194 |
|
register char *cp; |
195 |
|
register int nr, n; |
196 |
|
char buf[BUFSIZ], *pfin, *pfout, *pferr; |
197 |
|
int pid, nfds; |
198 |
< |
int fdin, fdout, fderr = -1; |
199 |
< |
fd_set readfds, writefds, excepfds; |
198 |
> |
int fdout, fderr = -1; |
199 |
> |
int status = 0; |
200 |
> |
fd_set readfds, excepfds; |
201 |
|
/* load persist file */ |
202 |
|
n = 40; |
203 |
|
while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { |
204 |
|
if (!n--) |
205 |
|
error(USER, "unattended persist file?"); |
206 |
|
pflock(0); |
207 |
< |
sleep(15); /* wait until ready */ |
207 |
> |
sleep(3+(3*getpid()+random())%13); /* wait until ready */ |
208 |
|
pflock(1); |
209 |
|
} |
210 |
|
if (nr < 0) |
211 |
|
error(SYSTEM, "error reading persist file"); |
212 |
< |
ftruncate(persistfd, 0L); /* truncate persist file */ |
212 |
> |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
213 |
> |
ftruncate(persistfd, (off_t)0L); /* truncate persist file */ |
214 |
> |
#endif |
215 |
|
pfdetach(); /* close & release persist file */ |
216 |
|
buf[nr] = '\0'; /* parse what we got */ |
217 |
< |
if ((cp = index(buf, ' ')) == NULL) |
217 |
> |
if ((cp = strchr(buf, ' ')) == NULL) |
218 |
|
goto formerr; |
219 |
|
*cp++ = '\0'; |
220 |
|
if ((pid = atoi(cp)) <= 0) |
221 |
|
goto formerr; |
222 |
< |
if ((cp = index(cp, '\n')) == NULL) |
222 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
223 |
|
goto formerr; |
224 |
|
pfin = ++cp; |
225 |
< |
if ((cp = index(cp, '\n')) == NULL) |
225 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
226 |
|
goto formerr; |
227 |
|
*cp++ = '\0'; |
228 |
|
pfout = cp; |
229 |
< |
if ((cp = index(cp, '\n')) == NULL) |
229 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
230 |
|
goto formerr; |
231 |
|
*cp++ = '\0'; |
232 |
|
pferr = cp; |
233 |
< |
if ((cp = index(cp, '\n')) == NULL) |
233 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
234 |
|
goto formerr; |
235 |
|
*cp++ = '\0'; |
236 |
|
if (cp-buf != nr) |
242 |
|
/* wake up rendering process */ |
243 |
|
if (kill(pid, SIGIO) < 0) |
244 |
|
error(SYSTEM, "cannot signal rendering process in io_process"); |
245 |
< |
/* open named pipes, in order */ |
246 |
< |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
247 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
245 |
> |
/* fork child feeder process */ |
246 |
> |
pid = fork(); |
247 |
> |
if (pid < 0) |
248 |
> |
error(SYSTEM, "fork failed in io_process"); |
249 |
> |
if (pid == 0) { /* feeder loop */ |
250 |
> |
int fdin; |
251 |
> |
close(1); /* open input pipe */ |
252 |
> |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
253 |
> |
error(SYSTEM, "cannot open feed pipe in io_process"); |
254 |
> |
/* renderer stdin */ |
255 |
> |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) { |
256 |
> |
do { |
257 |
> |
if ((n = write(fdin, cp, nr)) <= 0) |
258 |
> |
goto writerr; |
259 |
> |
cp += n; |
260 |
> |
} while ((nr -= n) > 0); |
261 |
> |
} |
262 |
> |
if (nr < 0) |
263 |
> |
goto readerr; |
264 |
> |
_exit(0); |
265 |
> |
} |
266 |
> |
close(0); |
267 |
> |
/* open output pipes, in order */ |
268 |
|
if ((fdout = open(pfout, O_RDONLY)) < 0) |
269 |
|
error(SYSTEM, "cannot open output pipe in io_process"); |
270 |
|
if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0) |
271 |
|
error(SYSTEM, "cannot open error pipe in io_process"); |
272 |
< |
for ( ; ; ) { /* loop on select call */ |
272 |
> |
for ( ; ; ) { /* eater loop */ |
273 |
|
FD_ZERO(&readfds); |
224 |
– |
FD_ZERO(&writefds); |
274 |
|
FD_ZERO(&excepfds); |
275 |
|
nfds = 0; |
227 |
– |
if (fdin >= 0) { |
228 |
– |
FD_SET(fdin, &writefds); |
229 |
– |
FD_SET(fdin, &excepfds); |
230 |
– |
nfds = fdin+1; |
231 |
– |
} |
276 |
|
if (fdout >= 0) { |
277 |
|
FD_SET(fdout, &readfds); |
278 |
|
FD_SET(fdout, &excepfds); |
285 |
|
} |
286 |
|
if (nfds == 0) |
287 |
|
break; /* all done, exit */ |
288 |
< |
if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0) |
288 |
> |
if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0) |
289 |
|
error(SYSTEM, "error in select call in io_process"); |
290 |
|
/* renderer stderr */ |
291 |
|
if (fderr >= 0 && (FD_ISSET(fderr, &readfds) || |
297 |
|
close(fderr); |
298 |
|
/* close(2); don't close stderr! */ |
299 |
|
fderr = -1; |
300 |
< |
} else |
301 |
< |
do { /* write it all */ |
300 |
> |
} else { |
301 |
> |
cp[nr] = '\0'; /* deduce status if we can */ |
302 |
> |
n = strlen(progname); |
303 |
> |
if (!strncmp(cp, progname, n) && |
304 |
> |
cp[n++] == ':' && |
305 |
> |
cp[n++] == ' ') { |
306 |
> |
register struct erract *ep; |
307 |
> |
for (ep = erract; ep < erract+NERRS; |
308 |
> |
ep++) |
309 |
> |
if (ep->pre[0] && |
310 |
> |
!strncmp(cp+n, ep->pre, |
311 |
> |
strlen(ep->pre))) { |
312 |
> |
status = ep->ec; |
313 |
> |
break; |
314 |
> |
} |
315 |
> |
} |
316 |
> |
do { /* write message */ |
317 |
|
if ((n = write(2, cp, nr)) <= 0) |
318 |
|
goto writerr; |
319 |
|
cp += n; |
320 |
|
} while ((nr -= n) > 0); |
321 |
+ |
} |
322 |
|
} |
323 |
|
/* renderer stdout */ |
324 |
|
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
337 |
|
cp += n; |
338 |
|
} while ((nr -= n) > 0); |
339 |
|
} |
280 |
– |
/* renderer stdin */ |
281 |
– |
if (fdin >= 0 && (FD_ISSET(fdin, &writefds) || |
282 |
– |
FD_ISSET(fdin, &excepfds))) { |
283 |
– |
nr = read(0, buf, 512); /* use minimal buffer */ |
284 |
– |
if (nr < 0) |
285 |
– |
goto readerr; |
286 |
– |
if (nr == 0) { |
287 |
– |
close(0); |
288 |
– |
close(fdin); |
289 |
– |
fdin = -1; |
290 |
– |
} else if (write(fdin, buf, nr) < nr) |
291 |
– |
goto writerr; /* what else to do? */ |
292 |
– |
} |
340 |
|
} |
341 |
< |
_exit(0); /* we ought to return renderer error status! */ |
341 |
> |
wait(0); /* wait for feeder process */ |
342 |
> |
_exit(status); |
343 |
|
formerr: |
344 |
|
error(USER, "format error in persist file"); |
345 |
|
readerr: |
350 |
|
|
351 |
|
#else |
352 |
|
|
353 |
< |
pfclean() {} |
353 |
> |
void pfclean() {} |
354 |
|
|
355 |
|
#endif |