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