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 "standard.h" |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#ifdef F_SETLKW |
14 |
< |
#include "paths.h" |
12 |
> |
#include <string.h> |
13 |
|
#include <signal.h> |
16 |
– |
#include <sys/types.h> |
17 |
– |
#include <sys/select.h> |
14 |
|
#include <sys/stat.h> |
15 |
< |
/* select call compatibility stuff */ |
16 |
< |
#ifndef FD_SETSIZE |
17 |
< |
#include <sys/param.h> |
18 |
< |
#define FD_SETSIZE NOFILE /* maximum # select file descriptors */ |
15 |
> |
#include <sys/types.h> |
16 |
> |
|
17 |
> |
#include "platform.h" |
18 |
> |
#ifndef NON_POSIX /* XXX need abstraction for process management */ |
19 |
> |
#include <sys/wait.h> |
20 |
|
#endif |
24 |
– |
#ifndef FD_SET |
25 |
– |
#ifndef NFDBITS |
26 |
– |
#define NFDBITS (8*sizeof(int)) /* number of bits per fd_mask */ |
27 |
– |
#endif |
28 |
– |
#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) |
29 |
– |
#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) |
30 |
– |
#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) |
31 |
– |
#define FD_ZERO(p) bzero((char *)(p), sizeof(*(p))) |
32 |
– |
#endif |
21 |
|
|
22 |
+ |
#include "rtprocess.h" /* getpid() */ |
23 |
+ |
#include "standard.h" |
24 |
+ |
#include "random.h" |
25 |
+ |
#include "ray.h" |
26 |
+ |
|
27 |
+ |
#ifdef F_SETLKW |
28 |
+ |
#include "paths.h" |
29 |
+ |
#include "selcall.h" |
30 |
+ |
|
31 |
|
#ifndef TIMELIM |
32 |
|
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
33 |
|
#endif |
34 |
|
|
38 |
– |
extern char *strcpy(), *index(); |
39 |
– |
|
35 |
|
extern int headismine; /* boolean true if header belongs to me */ |
41 |
– |
|
36 |
|
extern char *progname; /* global program name */ |
43 |
– |
|
37 |
|
extern char *errfile; /* global error file name */ |
45 |
– |
|
38 |
|
static char *persistfname = NULL; /* persist file name */ |
39 |
|
static int persistfd = -1; /* persist file descriptor */ |
48 |
– |
|
40 |
|
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; |
41 |
|
|
42 |
+ |
typedef void (sighandler_t)(int); |
43 |
+ |
static sighandler_t sig_io; |
44 |
+ |
static sighandler_t sig_alrm; |
45 |
|
|
46 |
< |
pfdetach() /* release persist (and header) resources */ |
46 |
> |
|
47 |
> |
extern void |
48 |
> |
pfdetach(void) /* release persist (and header) resources */ |
49 |
|
{ |
50 |
|
if (persistfd >= 0) |
51 |
|
close(persistfd); |
58 |
|
} |
59 |
|
|
60 |
|
|
61 |
< |
pfclean() /* clean up persist files */ |
61 |
> |
extern void |
62 |
> |
pfclean(void) /* clean up persist files */ |
63 |
|
{ |
64 |
|
if (persistfd >= 0) |
65 |
|
close(persistfd); |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
< |
pflock(lf) /* place or release exclusive lock on file */ |
78 |
< |
int lf; |
77 |
> |
extern void |
78 |
> |
pflock( /* place or release exclusive lock on file */ |
79 |
> |
int lf |
80 |
> |
) |
81 |
|
{ |
82 |
|
struct flock fls; |
83 |
|
|
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
< |
persistfile(pfn) /* open persist file and lock it */ |
94 |
< |
char *pfn; |
93 |
> |
extern void |
94 |
> |
persistfile( /* open persist file and lock it */ |
95 |
> |
char *pfn |
96 |
> |
) |
97 |
|
{ |
98 |
|
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); |
99 |
|
if (persistfd >= 0) { |
114 |
|
|
115 |
|
static int got_io; |
116 |
|
|
117 |
< |
static int sig_io() { got_io++; } |
117 |
> |
static void sig_io(int i) { got_io++; } |
118 |
|
|
119 |
< |
static int sig_alrm() { quit(0); } |
119 |
> |
static void sig_alrm(int i) { quit(0); } |
120 |
|
|
121 |
|
|
122 |
< |
pfhold() /* holding pattern for idle rendering process */ |
122 |
> |
extern void |
123 |
> |
pfhold(void) /* holding pattern for idle rendering process */ |
124 |
|
{ |
125 |
< |
int (*oldalrm)(); |
125 |
> |
sighandler_t *oldalrm; |
126 |
|
char buf[512]; |
127 |
|
register int n; |
128 |
|
/* close input and output descriptors */ |
129 |
< |
close(fileno(stdin)); |
130 |
< |
close(fileno(stdout)); |
129 |
> |
close(0); |
130 |
> |
close(1); |
131 |
|
if (errfile == NULL) |
132 |
< |
close(fileno(stderr)); |
132 |
> |
close(2); |
133 |
|
/* create named pipes for input and output */ |
134 |
< |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
134 |
> |
if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0) |
135 |
|
goto createrr; |
136 |
< |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
136 |
> |
if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0) |
137 |
|
goto createrr; |
138 |
|
if (errfile == NULL && |
139 |
< |
mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
139 |
> |
mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0) |
140 |
|
goto createrr; |
141 |
|
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
142 |
|
inpname, outpname, errname); |
143 |
|
n = strlen(buf); |
144 |
|
if (write(persistfd, buf, n) < n) |
145 |
|
error(SYSTEM, "error writing persist file"); |
146 |
< |
lseek(persistfd, 0L, 0); |
146 |
> |
lseek(persistfd, (off_t)0, SEEK_SET); |
147 |
|
/* wait TIMELIM for someone to signal us */ |
148 |
|
got_io = 0; |
149 |
|
signal(SIGIO, sig_io); |
150 |
< |
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
150 |
> |
oldalrm = signal(SIGALRM, sig_alrm); |
151 |
|
alarm(TIMELIM); |
152 |
|
pflock(0); /* unlock persist file for attach */ |
153 |
|
while (!got_io) |
157 |
|
signal(SIGIO, SIG_DFL); |
158 |
|
pflock(1); /* grab persist file back */ |
159 |
|
/* someone wants us; reopen stdin and stdout */ |
160 |
< |
if (freopen(inpname, "r", stdin) == NULL) |
161 |
< |
goto openerr; |
162 |
< |
if (freopen(outpname, "w", stdout) == NULL) |
163 |
< |
goto openerr; |
160 |
> |
close(0); |
161 |
> |
if (open(inpname, O_RDONLY) != 0) |
162 |
> |
error(INTERNAL, "unexpected stdin file number"); |
163 |
> |
clearerr(stdin); |
164 |
> |
close(1); |
165 |
> |
if (open(outpname, O_WRONLY) != 1) |
166 |
> |
error(INTERNAL, "unexpected stdout file number"); |
167 |
> |
sleep(3); /* give them a chance to open their pipes */ |
168 |
|
if (errname[0]) { |
169 |
< |
if (freopen(errname, "w", stderr) == NULL) |
170 |
< |
goto openerr; |
169 |
> |
close(2); |
170 |
> |
if (open(errname, O_WRONLY) != 2) |
171 |
> |
error(INTERNAL, "unexpected stderr file number"); |
172 |
|
unlink(errname); |
173 |
|
errname[0] = '\0'; |
174 |
|
} |
179 |
|
return; |
180 |
|
createrr: |
181 |
|
error(SYSTEM, "cannot create named pipes in pfhold"); |
175 |
– |
openerr: |
176 |
– |
error(SYSTEM, "cannot open named pipes in pfhold"); |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
< |
io_process() /* just act as go-between for actual process */ |
185 |
> |
extern void |
186 |
> |
io_process(void) /* just act as go-between for actual process */ |
187 |
|
{ |
188 |
|
register char *cp; |
189 |
|
register int nr, n; |
190 |
|
char buf[BUFSIZ], *pfin, *pfout, *pferr; |
191 |
|
int pid, nfds; |
192 |
< |
int fdin, fdout, fderr = -1; |
193 |
< |
fd_set readfds, writefds, excepfds; |
192 |
> |
int fdout, fderr = -1; |
193 |
> |
int status = 0; |
194 |
> |
fd_set readfds, excepfds; |
195 |
|
/* load persist file */ |
196 |
|
n = 40; |
197 |
|
while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { |
198 |
|
if (!n--) |
199 |
|
error(USER, "unattended persist file?"); |
200 |
|
pflock(0); |
201 |
< |
sleep(15); /* wait until ready */ |
201 |
> |
sleep(3+(3*getpid()+random())%13); /* wait until ready */ |
202 |
|
pflock(1); |
203 |
|
} |
204 |
|
if (nr < 0) |
205 |
|
error(SYSTEM, "error reading persist file"); |
206 |
< |
ftruncate(persistfd, 0L); /* truncate persist file */ |
206 |
> |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
207 |
> |
ftruncate(persistfd, (off_t)0L); /* truncate persist file */ |
208 |
> |
#endif |
209 |
|
pfdetach(); /* close & release persist file */ |
210 |
|
buf[nr] = '\0'; /* parse what we got */ |
211 |
< |
if ((cp = index(buf, ' ')) == NULL) |
211 |
> |
if ((cp = strchr(buf, ' ')) == NULL) |
212 |
|
goto formerr; |
213 |
|
*cp++ = '\0'; |
214 |
|
if ((pid = atoi(cp)) <= 0) |
215 |
|
goto formerr; |
216 |
< |
if ((cp = index(cp, '\n')) == NULL) |
216 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
217 |
|
goto formerr; |
218 |
|
pfin = ++cp; |
219 |
< |
if ((cp = index(cp, '\n')) == NULL) |
219 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
220 |
|
goto formerr; |
221 |
|
*cp++ = '\0'; |
222 |
|
pfout = cp; |
223 |
< |
if ((cp = index(cp, '\n')) == NULL) |
223 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
224 |
|
goto formerr; |
225 |
|
*cp++ = '\0'; |
226 |
|
pferr = cp; |
227 |
< |
if ((cp = index(cp, '\n')) == NULL) |
227 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
228 |
|
goto formerr; |
229 |
|
*cp++ = '\0'; |
230 |
|
if (cp-buf != nr) |
236 |
|
/* wake up rendering process */ |
237 |
|
if (kill(pid, SIGIO) < 0) |
238 |
|
error(SYSTEM, "cannot signal rendering process in io_process"); |
239 |
< |
/* open named pipes, in order */ |
240 |
< |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
241 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
239 |
> |
/* fork child feeder process */ |
240 |
> |
pid = fork(); |
241 |
> |
if (pid < 0) |
242 |
> |
error(SYSTEM, "fork failed in io_process"); |
243 |
> |
if (pid == 0) { /* feeder loop */ |
244 |
> |
int fdin; |
245 |
> |
close(1); /* open input pipe */ |
246 |
> |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
247 |
> |
error(SYSTEM, "cannot open feed pipe in io_process"); |
248 |
> |
/* renderer stdin */ |
249 |
> |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) { |
250 |
> |
do { |
251 |
> |
if ((n = write(fdin, cp, nr)) <= 0) |
252 |
> |
goto writerr; |
253 |
> |
cp += n; |
254 |
> |
} while ((nr -= n) > 0); |
255 |
> |
} |
256 |
> |
if (nr < 0) |
257 |
> |
goto readerr; |
258 |
> |
_exit(0); |
259 |
> |
} |
260 |
> |
close(0); |
261 |
> |
/* open output pipes, in order */ |
262 |
|
if ((fdout = open(pfout, O_RDONLY)) < 0) |
263 |
|
error(SYSTEM, "cannot open output pipe in io_process"); |
264 |
|
if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0) |
265 |
|
error(SYSTEM, "cannot open error pipe in io_process"); |
266 |
< |
for ( ; ; ) { /* loop on select call */ |
266 |
> |
for ( ; ; ) { /* eater loop */ |
267 |
|
FD_ZERO(&readfds); |
239 |
– |
FD_ZERO(&writefds); |
268 |
|
FD_ZERO(&excepfds); |
269 |
|
nfds = 0; |
242 |
– |
if (fdin >= 0) { |
243 |
– |
FD_SET(fdin, &writefds); |
244 |
– |
FD_SET(fdin, &excepfds); |
245 |
– |
nfds = fdin+1; |
246 |
– |
} |
270 |
|
if (fdout >= 0) { |
271 |
|
FD_SET(fdout, &readfds); |
272 |
|
FD_SET(fdout, &excepfds); |
279 |
|
} |
280 |
|
if (nfds == 0) |
281 |
|
break; /* all done, exit */ |
282 |
< |
if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0) |
282 |
> |
if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0) |
283 |
|
error(SYSTEM, "error in select call in io_process"); |
284 |
|
/* renderer stderr */ |
285 |
|
if (fderr >= 0 && (FD_ISSET(fderr, &readfds) || |
291 |
|
close(fderr); |
292 |
|
/* close(2); don't close stderr! */ |
293 |
|
fderr = -1; |
294 |
< |
} else |
295 |
< |
do { /* write it all */ |
294 |
> |
} else { |
295 |
> |
cp[nr] = '\0'; /* deduce status if we can */ |
296 |
> |
n = strlen(progname); |
297 |
> |
if (!strncmp(cp, progname, n) && |
298 |
> |
cp[n++] == ':' && |
299 |
> |
cp[n++] == ' ') { |
300 |
> |
register struct erract *ep; |
301 |
> |
for (ep = erract; ep < erract+NERRS; |
302 |
> |
ep++) |
303 |
> |
if (ep->pre[0] && |
304 |
> |
!strncmp(cp+n, ep->pre, |
305 |
> |
strlen(ep->pre))) { |
306 |
> |
status = ep->ec; |
307 |
> |
break; |
308 |
> |
} |
309 |
> |
} |
310 |
> |
do { /* write message */ |
311 |
|
if ((n = write(2, cp, nr)) <= 0) |
312 |
|
goto writerr; |
313 |
|
cp += n; |
314 |
|
} while ((nr -= n) > 0); |
315 |
+ |
} |
316 |
|
} |
317 |
|
/* renderer stdout */ |
318 |
|
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
331 |
|
cp += n; |
332 |
|
} while ((nr -= n) > 0); |
333 |
|
} |
295 |
– |
/* renderer stdin */ |
296 |
– |
if (fdin >= 0 && (FD_ISSET(fdin, &writefds) || |
297 |
– |
FD_ISSET(fdin, &excepfds))) { |
298 |
– |
nr = read(0, buf, 512); /* use minimal buffer */ |
299 |
– |
if (nr < 0) |
300 |
– |
goto readerr; |
301 |
– |
if (nr == 0) { |
302 |
– |
close(0); |
303 |
– |
close(fdin); |
304 |
– |
fdin = -1; |
305 |
– |
} else if (write(fdin, buf, nr) < nr) |
306 |
– |
goto writerr; /* what else to do? */ |
307 |
– |
} |
334 |
|
} |
335 |
< |
_exit(0); /* we ought to return renderer error status! */ |
335 |
> |
kill(pid, SIGTERM); /* no more process to feed, so... */ |
336 |
> |
waitpid(pid, 0, 0); /* wait for feeder process */ |
337 |
> |
_exit(status); |
338 |
|
formerr: |
339 |
|
error(USER, "format error in persist file"); |
340 |
|
readerr: |
345 |
|
|
346 |
|
#else |
347 |
|
|
348 |
< |
pfclean() {} |
348 |
> |
extern void pfclean(void) {} |
349 |
|
|
350 |
|
#endif |