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 |
+ |
|
16 |
+ |
#include "rtprocess.h" /* getpid() */ |
17 |
|
#include "standard.h" |
18 |
+ |
#include "random.h" |
19 |
|
|
20 |
|
#ifdef F_SETLKW |
14 |
– |
|
21 |
|
#include "paths.h" |
22 |
< |
#include <signal.h> |
17 |
< |
#include <sys/types.h> |
18 |
< |
#include <sys/stat.h> |
22 |
> |
#include "selcall.h" |
23 |
|
|
24 |
|
#ifndef TIMELIM |
25 |
|
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
26 |
|
#endif |
27 |
|
|
28 |
< |
extern char *strcpy(), *index(); |
28 |
> |
#ifndef freebsd |
29 |
> |
#define mkfifo(fn,md) mknod(fn, S_IFIFO|(md), 0) |
30 |
> |
#endif |
31 |
|
|
32 |
+ |
extern void io_process(); |
33 |
+ |
|
34 |
|
extern int headismine; /* boolean true if header belongs to me */ |
35 |
|
|
36 |
|
extern char *progname; /* global program name */ |
43 |
|
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; |
44 |
|
|
45 |
|
|
46 |
+ |
void |
47 |
|
pfdetach() /* release persist (and header) resources */ |
48 |
|
{ |
49 |
|
if (persistfd >= 0) |
57 |
|
} |
58 |
|
|
59 |
|
|
60 |
+ |
void |
61 |
|
pfclean() /* clean up persist files */ |
62 |
|
{ |
63 |
|
if (persistfd >= 0) |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
+ |
void |
77 |
|
pflock(lf) /* place or release exclusive lock on file */ |
78 |
|
int lf; |
79 |
|
{ |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
+ |
void |
92 |
|
persistfile(pfn) /* open persist file and lock it */ |
93 |
|
char *pfn; |
94 |
|
{ |
111 |
|
|
112 |
|
static int got_io; |
113 |
|
|
114 |
< |
static int sig_io() { got_io++; } |
114 |
> |
static void sig_io() { got_io++; } |
115 |
|
|
116 |
< |
static int sig_alrm() { quit(0); } |
116 |
> |
static void sig_alrm() { quit(0); } |
117 |
|
|
118 |
|
|
119 |
+ |
void |
120 |
|
pfhold() /* holding pattern for idle rendering process */ |
121 |
|
{ |
122 |
< |
int (*oldalrm)(); |
122 |
> |
void (*oldalrm)(); |
123 |
|
char buf[512]; |
124 |
|
register int n; |
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) |
131 |
> |
if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0) |
132 |
|
goto createrr; |
133 |
< |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
133 |
> |
if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0) |
134 |
|
goto createrr; |
135 |
|
if (errfile == NULL && |
136 |
< |
mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
136 |
> |
mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0) |
137 |
|
goto createrr; |
138 |
|
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
139 |
|
inpname, outpname, errname); |
140 |
|
n = strlen(buf); |
141 |
|
if (write(persistfd, buf, n) < n) |
142 |
|
error(SYSTEM, "error writing persist file"); |
143 |
+ |
lseek(persistfd, (off_t)0L, 0); |
144 |
|
/* wait TIMELIM for someone to signal us */ |
145 |
|
got_io = 0; |
146 |
|
signal(SIGIO, sig_io); |
147 |
< |
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
147 |
> |
oldalrm = (void (*)())signal(SIGALRM, sig_alrm); |
148 |
|
alarm(TIMELIM); |
149 |
|
pflock(0); /* unlock persist file for attach */ |
150 |
|
while (!got_io) |
151 |
|
pause(); /* wait for attach */ |
136 |
– |
pflock(1); /* grab persist file right back! */ |
152 |
|
alarm(0); /* turn off alarm */ |
153 |
|
signal(SIGALRM, oldalrm); |
154 |
|
signal(SIGIO, SIG_DFL); |
155 |
< |
if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0) |
141 |
< |
error(SYSTEM, "seek/truncate error on persist file"); |
155 |
> |
pflock(1); /* grab persist file back */ |
156 |
|
/* someone wants us; reopen stdin and stdout */ |
157 |
+ |
/* |
158 |
|
if (freopen(inpname, "r", stdin) == NULL) |
159 |
|
goto openerr; |
160 |
|
if (freopen(outpname, "w", stdout) == NULL) |
161 |
|
goto openerr; |
162 |
+ |
*/ |
163 |
+ |
close(0); |
164 |
+ |
if (open(inpname, O_RDONLY) != 0) |
165 |
+ |
error(INTERNAL, "unexpected stdin file number"); |
166 |
+ |
clearerr(stdin); |
167 |
+ |
close(1); |
168 |
+ |
if (open(outpname, O_WRONLY) != 1) |
169 |
+ |
error(INTERNAL, "unexpected stdout file number"); |
170 |
+ |
sleep(3); /* give them a chance to open their pipes */ |
171 |
|
if (errname[0]) { |
172 |
< |
if (freopen(errname, "w", stderr) == NULL) |
173 |
< |
goto openerr; |
172 |
> |
close(2); |
173 |
> |
if (open(errname, O_WRONLY) != 2) |
174 |
> |
error(INTERNAL, "unexpected stderr file number"); |
175 |
|
unlink(errname); |
176 |
|
errname[0] = '\0'; |
177 |
|
} |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
< |
io_process() /* just act as conduits to and from actual process */ |
190 |
> |
void |
191 |
> |
io_process() /* just act as go-between for actual process */ |
192 |
|
{ |
193 |
|
register char *cp; |
194 |
|
register int nr, n; |
195 |
< |
char buf[512], *pfin, *pfout, *pferr; |
196 |
< |
int pid, pid2 = -1; |
195 |
> |
char buf[BUFSIZ], *pfin, *pfout, *pferr; |
196 |
> |
int pid, nfds; |
197 |
> |
int fdout, fderr = -1; |
198 |
> |
int status = 0; |
199 |
> |
fd_set readfds, excepfds; |
200 |
|
/* load persist file */ |
201 |
+ |
n = 40; |
202 |
|
while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { |
203 |
+ |
if (!n--) |
204 |
+ |
error(USER, "unattended persist file?"); |
205 |
|
pflock(0); |
206 |
< |
sleep(15); /* wait until ready */ |
206 |
> |
sleep(3+(3*getpid()+random())%13); /* wait until ready */ |
207 |
|
pflock(1); |
208 |
|
} |
177 |
– |
pfdetach(); /* close persist file */ |
209 |
|
if (nr < 0) |
210 |
|
error(SYSTEM, "error reading persist file"); |
211 |
< |
buf[nr] = '\0'; |
212 |
< |
if ((cp = index(buf, ' ')) == NULL) |
211 |
> |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
212 |
> |
ftruncate(persistfd, (off_t)0L); /* truncate persist file */ |
213 |
> |
#endif |
214 |
> |
pfdetach(); /* close & release persist file */ |
215 |
> |
buf[nr] = '\0'; /* parse what we got */ |
216 |
> |
if ((cp = strchr(buf, ' ')) == NULL) |
217 |
|
goto formerr; |
218 |
|
*cp++ = '\0'; |
219 |
|
if ((pid = atoi(cp)) <= 0) |
220 |
|
goto formerr; |
221 |
< |
if ((cp = index(cp, '\n')) == NULL) |
221 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
222 |
|
goto formerr; |
223 |
|
pfin = ++cp; |
224 |
< |
if ((cp = index(cp, '\n')) == NULL) |
224 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
225 |
|
goto formerr; |
226 |
|
*cp++ = '\0'; |
227 |
|
pfout = cp; |
228 |
< |
if ((cp = index(cp, '\n')) == NULL) |
228 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
229 |
|
goto formerr; |
230 |
|
*cp++ = '\0'; |
231 |
|
pferr = cp; |
232 |
< |
if ((cp = index(cp, '\n')) == NULL) |
232 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
233 |
|
goto formerr; |
234 |
|
*cp++ = '\0'; |
235 |
|
if (cp-buf != nr) |
238 |
|
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
239 |
|
error(USER, errmsg); |
240 |
|
} |
241 |
< |
/* wake up rendering process */ |
241 |
> |
/* wake up rendering process */ |
242 |
|
if (kill(pid, SIGIO) < 0) |
243 |
|
error(SYSTEM, "cannot signal rendering process in io_process"); |
244 |
< |
pid = fork(); /* fork i/o process */ |
244 |
> |
/* fork child feeder process */ |
245 |
> |
pid = fork(); |
246 |
|
if (pid < 0) |
247 |
< |
goto forkerr; |
248 |
< |
/* connect to appropriate pipe */ |
249 |
< |
if (pid) { /* parent passes renderer output */ |
250 |
< |
close(0); |
251 |
< |
if (pferr[0]) { |
252 |
< |
pid2 = fork(); /* fork another for stderr */ |
253 |
< |
if (pid2 < 0) |
254 |
< |
goto forkerr; |
247 |
> |
error(SYSTEM, "fork failed in io_process"); |
248 |
> |
if (pid == 0) { /* feeder loop */ |
249 |
> |
int fdin; |
250 |
> |
close(1); /* open input pipe */ |
251 |
> |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
252 |
> |
error(SYSTEM, "cannot open feed pipe in io_process"); |
253 |
> |
/* renderer stdin */ |
254 |
> |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) { |
255 |
> |
do { |
256 |
> |
if ((n = write(fdin, cp, nr)) <= 0) |
257 |
> |
goto writerr; |
258 |
> |
cp += n; |
259 |
> |
} while ((nr -= n) > 0); |
260 |
|
} |
261 |
< |
if (pid2) { /* parent is still stdout */ |
262 |
< |
if (open(pfout, O_RDONLY) != 0) |
263 |
< |
error(SYSTEM, |
264 |
< |
"cannot open output pipe in io_process"); |
265 |
< |
} else { /* second child is stderr */ |
266 |
< |
if (open(pferr, O_RDONLY) != 0) |
267 |
< |
error(SYSTEM, |
268 |
< |
"cannot open error pipe in io_process"); |
269 |
< |
dup2(2, 1); /* attach stdout to stderr */ |
261 |
> |
if (nr < 0) |
262 |
> |
goto readerr; |
263 |
> |
_exit(0); |
264 |
> |
} |
265 |
> |
close(0); |
266 |
> |
/* open output pipes, in order */ |
267 |
> |
if ((fdout = open(pfout, O_RDONLY)) < 0) |
268 |
> |
error(SYSTEM, "cannot open output pipe in io_process"); |
269 |
> |
if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0) |
270 |
> |
error(SYSTEM, "cannot open error pipe in io_process"); |
271 |
> |
for ( ; ; ) { /* eater loop */ |
272 |
> |
FD_ZERO(&readfds); |
273 |
> |
FD_ZERO(&excepfds); |
274 |
> |
nfds = 0; |
275 |
> |
if (fdout >= 0) { |
276 |
> |
FD_SET(fdout, &readfds); |
277 |
> |
FD_SET(fdout, &excepfds); |
278 |
> |
nfds = fdout+1; |
279 |
|
} |
280 |
< |
} else { /* child passes renderer input */ |
281 |
< |
close(1); |
282 |
< |
if (open(pfin, O_WRONLY) != 1) |
283 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
280 |
> |
if (fderr >= 0) { |
281 |
> |
FD_SET(fderr, &readfds); |
282 |
> |
FD_SET(fderr, &excepfds); |
283 |
> |
nfds = fderr+1; |
284 |
> |
} |
285 |
> |
if (nfds == 0) |
286 |
> |
break; /* all done, exit */ |
287 |
> |
if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0) |
288 |
> |
error(SYSTEM, "error in select call in io_process"); |
289 |
> |
/* renderer stderr */ |
290 |
> |
if (fderr >= 0 && (FD_ISSET(fderr, &readfds) || |
291 |
> |
FD_ISSET(fderr, &excepfds))) { |
292 |
> |
nr = read(fderr, cp=buf, sizeof(buf)); |
293 |
> |
if (nr < 0) |
294 |
> |
goto readerr; |
295 |
> |
if (nr == 0) { |
296 |
> |
close(fderr); |
297 |
> |
/* close(2); don't close stderr! */ |
298 |
> |
fderr = -1; |
299 |
> |
} else { |
300 |
> |
cp[nr] = '\0'; /* deduce status if we can */ |
301 |
> |
n = strlen(progname); |
302 |
> |
if (!strncmp(cp, progname, n) && |
303 |
> |
cp[n++] == ':' && |
304 |
> |
cp[n++] == ' ') { |
305 |
> |
register struct erract *ep; |
306 |
> |
for (ep = erract; ep < erract+NERRS; |
307 |
> |
ep++) |
308 |
> |
if (ep->pre[0] && |
309 |
> |
!strncmp(cp+n, ep->pre, |
310 |
> |
strlen(ep->pre))) { |
311 |
> |
status = ep->ec; |
312 |
> |
break; |
313 |
> |
} |
314 |
> |
} |
315 |
> |
do { /* write message */ |
316 |
> |
if ((n = write(2, cp, nr)) <= 0) |
317 |
> |
goto writerr; |
318 |
> |
cp += n; |
319 |
> |
} while ((nr -= n) > 0); |
320 |
> |
} |
321 |
> |
} |
322 |
> |
/* renderer stdout */ |
323 |
> |
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
324 |
> |
FD_ISSET(fdout, &excepfds))) { |
325 |
> |
nr = read(fdout, cp=buf, sizeof(buf)); |
326 |
> |
if (nr < 0) |
327 |
> |
goto readerr; |
328 |
> |
if (nr == 0) { /* EOF */ |
329 |
> |
close(fdout); |
330 |
> |
close(1); |
331 |
> |
fdout = -1; |
332 |
> |
} else |
333 |
> |
do { /* write it all */ |
334 |
> |
if ((n = write(1, cp, nr)) <= 0) |
335 |
> |
goto writerr; |
336 |
> |
cp += n; |
337 |
> |
} while ((nr -= n) > 0); |
338 |
> |
} |
339 |
|
} |
340 |
< |
/* pass input to output */ |
341 |
< |
/* 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!) */ |
340 |
> |
wait(0); /* wait for feeder process */ |
341 |
> |
_exit(status); |
342 |
|
formerr: |
343 |
|
error(USER, "format error in persist file"); |
344 |
< |
forkerr: |
345 |
< |
error(SYSTEM, "fork failed in io_process"); |
344 |
> |
readerr: |
345 |
> |
error(SYSTEM, "read error in io_process"); |
346 |
> |
writerr: |
347 |
> |
error(SYSTEM, "write error in io_process"); |
348 |
|
} |
349 |
|
|
350 |
|
#else |
351 |
|
|
352 |
< |
pfclean() {} |
352 |
> |
void pfclean() {} |
353 |
|
|
354 |
|
#endif |