10 |
|
|
11 |
|
#include "rholo.h" |
12 |
|
#include "paths.h" |
13 |
+ |
#include <signal.h> |
14 |
|
#include <sys/types.h> |
15 |
+ |
#include <sys/stat.h> |
16 |
|
|
17 |
|
/* the following must be consistent with rholo.h */ |
18 |
|
int NVARS = NRHVARS; /* total number of variables */ |
35 |
|
time_t endtime; /* time we should end by */ |
36 |
|
time_t reporttime; /* time for next report */ |
37 |
|
|
38 |
+ |
long maxdisk; /* maximum file space (bytes) */ |
39 |
+ |
|
40 |
|
int rtargc = 1; /* rtrace command */ |
41 |
|
char *rtargv[128] = {"rtrace", NULL}; |
42 |
|
|
43 |
+ |
int orig_mode = -1; /* original file mode (-1 if unchanged) */ |
44 |
+ |
|
45 |
|
long nraysdone = 0L; /* number of rays done */ |
46 |
|
long npacksdone = 0L; /* number of packets done */ |
47 |
|
|
48 |
|
PACKET *freepacks; /* available packets */ |
49 |
|
|
50 |
+ |
char *sigerr[NSIG]; /* signal error messages */ |
51 |
+ |
|
52 |
|
extern time_t time(); |
53 |
|
|
54 |
|
|
59 |
|
HDGRID hdg; |
60 |
|
int i; |
61 |
|
int force = 0; |
62 |
< |
|
62 |
> |
/* mark start time */ |
63 |
> |
starttime = time(NULL); |
64 |
|
progname = argv[0]; /* get arguments */ |
65 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
66 |
|
switch (argv[i][1]) { |
111 |
|
"holodeck file exists -- use -f to overwrite"); |
112 |
|
/* create holodeck */ |
113 |
|
creatholo(&hdg); |
114 |
< |
} else /* else load holodeck */ |
114 |
> |
} else { /* else load holodeck */ |
115 |
|
loadholo(); |
116 |
+ |
if (vdef(RIF)) /* load RIF if any */ |
117 |
+ |
getradfile(vval(RIF)); |
118 |
+ |
} |
119 |
|
/* initialize */ |
120 |
|
initrholo(); |
121 |
|
/* run */ |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
+ |
onsig(signo) /* fatal signal */ |
135 |
+ |
int signo; |
136 |
+ |
{ |
137 |
+ |
static int gotsig = 0; |
138 |
+ |
|
139 |
+ |
if (gotsig++) /* two signals and we're gone! */ |
140 |
+ |
_exit(signo); |
141 |
+ |
|
142 |
+ |
alarm(30); /* allow 30 seconds to clean up */ |
143 |
+ |
signal(SIGALRM, SIG_DFL); /* make certain we do die */ |
144 |
+ |
eputs("signal - "); |
145 |
+ |
eputs(sigerr[signo]); |
146 |
+ |
eputs("\n"); |
147 |
+ |
quit(3); |
148 |
+ |
} |
149 |
+ |
|
150 |
+ |
|
151 |
+ |
sigdie(signo, msg) /* set fatal signal */ |
152 |
+ |
int signo; |
153 |
+ |
char *msg; |
154 |
+ |
{ |
155 |
+ |
if (signal(signo, onsig) == SIG_IGN) |
156 |
+ |
signal(signo, SIG_IGN); |
157 |
+ |
sigerr[signo] = msg; |
158 |
+ |
} |
159 |
+ |
|
160 |
+ |
|
161 |
+ |
int |
162 |
+ |
resfmode(fd, mod) /* restrict open file access mode */ |
163 |
+ |
int fd, mod; |
164 |
+ |
{ |
165 |
+ |
struct stat stbuf; |
166 |
+ |
/* get original mode */ |
167 |
+ |
if (fstat(fd, &stbuf) < 0) |
168 |
+ |
error(SYSTEM, "cannot stat open holodeck file"); |
169 |
+ |
mod &= stbuf.st_mode; /* always more restrictive */ |
170 |
+ |
if (mod == stbuf.st_mode) |
171 |
+ |
return(-1); /* already set */ |
172 |
+ |
/* else change it */ |
173 |
+ |
if (fchmod(fd, mod) < 0) { |
174 |
+ |
error(WARNING, "cannot change holodeck file access mode"); |
175 |
+ |
return(-1); |
176 |
+ |
} |
177 |
+ |
return(stbuf.st_mode); /* return original mode */ |
178 |
+ |
} |
179 |
+ |
|
180 |
+ |
|
181 |
|
initrholo() /* get our holodeck running */ |
182 |
|
{ |
183 |
|
extern int global_packet(); |
184 |
|
register int i; |
185 |
< |
/* check output device */ |
186 |
< |
if (outdev != NULL) |
187 |
< |
open_display(outdev); |
185 |
> |
|
186 |
> |
if (outdev != NULL) /* open output device */ |
187 |
> |
disp_open(outdev); |
188 |
|
else if (ncprocs > 0) /* else use global ray feed */ |
189 |
|
init_global(); |
190 |
< |
/* record the time */ |
191 |
< |
starttime = time(NULL); |
190 |
> |
/* record disk space limit */ |
191 |
> |
if (!vdef(DISKSPACE)) |
192 |
> |
maxdisk = 0; |
193 |
> |
else |
194 |
> |
maxdisk = 1024.*1024.*vflt(DISKSPACE); |
195 |
> |
/* record end time */ |
196 |
|
if (!vdef(TIME) || vflt(TIME) <= FTINY) |
197 |
|
endtime = 0; |
198 |
|
else |
199 |
|
endtime = starttime + vflt(TIME)*3600.; |
200 |
|
/* set up memory cache */ |
201 |
< |
hdcachesize = 1024.*1024.*vflt(CACHE); |
201 |
> |
if (outdev == NULL) |
202 |
> |
hdcachesize = 0; /* manual flushing */ |
203 |
> |
else if (vdef(CACHE)) |
204 |
> |
hdcachesize = 1024.*1024.*vflt(CACHE); |
205 |
|
/* open report file */ |
206 |
|
if (vdef(REPORT)) { |
207 |
|
register char *s = sskip2(vval(REPORT), 1); |
237 |
|
freepacks[i].next = &freepacks[i+1]; |
238 |
|
} |
239 |
|
} |
240 |
+ |
/* set up signal handling */ |
241 |
+ |
sigdie(SIGINT, "Interrupt"); |
242 |
+ |
sigdie(SIGHUP, "Hangup"); |
243 |
+ |
sigdie(SIGTERM, "Terminate"); |
244 |
+ |
sigdie(SIGPIPE, "Broken pipe"); |
245 |
+ |
sigdie(SIGALRM, "Alarm clock"); |
246 |
+ |
#ifdef SIGXCPU |
247 |
+ |
sigdie(SIGXCPU, "CPU limit exceeded"); |
248 |
+ |
sigdie(SIGXFSZ, "File size exceeded"); |
249 |
+ |
#endif |
250 |
+ |
/* protect holodeck file */ |
251 |
+ |
orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444); |
252 |
|
return; |
253 |
|
memerr: |
254 |
|
error(SYSTEM, "out of memory in initrholo"); |
262 |
|
register PACKET *p; |
263 |
|
time_t t; |
264 |
|
long l; |
265 |
< |
/* check display */ |
266 |
< |
if (outdev != NULL && !disp_check(idle)) |
267 |
< |
return(0); |
265 |
> |
|
266 |
> |
if (outdev != NULL) /* check display */ |
267 |
> |
if (!disp_check(idle)) |
268 |
> |
return(0); |
269 |
|
/* display only? */ |
270 |
|
if (ncprocs <= 0) |
271 |
|
return(1); |
272 |
|
/* check file size */ |
273 |
< |
if ((l = 1024.*1024.*vflt(DISKSPACE)) > 0 && |
274 |
< |
hdfiluse(hdlist[0]->fd, 0) + hdmemuse(0) >= l) |
273 |
> |
if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) { |
274 |
> |
error(WARNING, "file limit exceeded"); |
275 |
|
return(0); |
276 |
+ |
} |
277 |
|
/* check time */ |
278 |
< |
if (endtime > 0 || vdef(REPORT)) |
278 |
> |
if (endtime > 0 || reporttime > 0) |
279 |
|
t = time(NULL); |
280 |
< |
if (endtime > 0 && t >= endtime) |
280 |
> |
if (endtime > 0 && t >= endtime) { |
281 |
> |
error(WARNING, "time limit exceeded"); |
282 |
|
return(0); |
283 |
< |
if (vdef(REPORT) && t >= reporttime) |
283 |
> |
} |
284 |
> |
if (reporttime > 0 && t >= reporttime) |
285 |
|
report(t); |
286 |
|
/* get packets to process */ |
287 |
|
while (freepacks != NULL) { |
312 |
|
fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n", |
313 |
|
progname, npacksdone, nraysdone, (t-starttime)/3600.); |
314 |
|
fflush(stderr); |
315 |
< |
reporttime = t + (time_t)(vflt(REPORT)*60.); |
315 |
> |
if (vdef(REPORT)) |
316 |
> |
reporttime = t + (time_t)(vflt(REPORT)*60.+.5); |
317 |
|
} |
318 |
|
|
319 |
|
|
365 |
|
sprintf(vval(OCTREE), "%s.oct", froot); |
366 |
|
vdef(OCTREE)++; |
367 |
|
} |
285 |
– |
if (!vdef(DISKSPACE)) { |
286 |
– |
sprintf(errmsg, |
287 |
– |
"no %s setting, assuming 100 Mbytes available", |
288 |
– |
vnam(DISKSPACE)); |
289 |
– |
error(WARNING, errmsg); |
290 |
– |
vval(DISKSPACE) = "100"; |
291 |
– |
vdef(DISKSPACE)++; |
292 |
– |
} |
293 |
– |
if (!vdef(CACHE)) { |
294 |
– |
sprintf(errmsg, |
295 |
– |
"no %s setting, assuming 10 Mbytes available", |
296 |
– |
vnam(CACHE)); |
297 |
– |
error(WARNING, errmsg); |
298 |
– |
vval(CACHE) = "10"; |
299 |
– |
vdef(CACHE)++; |
300 |
– |
} |
368 |
|
if (!vdef(OBSTRUCTIONS)) { |
369 |
|
vval(OBSTRUCTIONS) = "T"; |
370 |
|
vdef(OBSTRUCTIONS)++; |
382 |
|
creatholo(gp) /* create a holodeck output file */ |
383 |
|
HDGRID *gp; |
384 |
|
{ |
385 |
+ |
extern char VersionID[]; |
386 |
|
long endloc = 0; |
387 |
+ |
int fd; |
388 |
|
FILE *fp; |
389 |
|
/* open & truncate file */ |
390 |
|
if ((fp = fopen(hdkfile, "w+")) == NULL) { |
393 |
|
} |
394 |
|
/* write information header */ |
395 |
|
newheader("RADIANCE", fp); |
396 |
+ |
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
397 |
|
printvars(fp); |
398 |
|
fputformat(HOLOFMT, fp); |
399 |
|
fputc('\n', fp); |
400 |
|
putw(HOLOMAGIC, fp); /* put magic number & terminus */ |
401 |
|
fwrite(&endloc, sizeof(long), 1, fp); |
402 |
< |
fflush(fp); /* flush buffer */ |
403 |
< |
hdinit(fileno(fp), gp); /* allocate and initialize index */ |
404 |
< |
/* we're dropping fp here.... */ |
402 |
> |
fd = dup(fileno(fp)); |
403 |
> |
fclose(fp); /* flush and close stdio stream */ |
404 |
> |
hdinit(fd, gp); /* allocate and initialize index */ |
405 |
|
} |
406 |
|
|
407 |
|
|
431 |
|
|
432 |
|
loadholo() /* start loading a holodeck from fname */ |
433 |
|
{ |
434 |
+ |
extern long ftell(); |
435 |
|
FILE *fp; |
436 |
< |
long endloc; |
437 |
< |
/* open input file */ |
438 |
< |
if ((fp = fopen(hdkfile, "r+")) == NULL) { |
439 |
< |
sprintf(errmsg, "cannot open \"%s\" for appending", hdkfile); |
436 |
> |
int fd; |
437 |
> |
long fpos; |
438 |
> |
/* open holodeck file */ |
439 |
> |
if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { |
440 |
> |
sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile, |
441 |
> |
ncprocs>0 ? "appending" : "reading"); |
442 |
|
error(SYSTEM, errmsg); |
443 |
|
} |
444 |
|
/* load variables from header */ |
449 |
|
hdkfile); |
450 |
|
error(USER, errmsg); |
451 |
|
} |
452 |
< |
fread(&endloc, sizeof(long), 1, fp); |
453 |
< |
if (endloc != 0) |
452 |
> |
fread(&fpos, sizeof(long), 1, fp); |
453 |
> |
if (fpos != 0) |
454 |
|
error(WARNING, "ignoring multiple sections in holodeck file"); |
455 |
< |
fseek(fp, 0L, 1); /* align system file pointer */ |
456 |
< |
hdinit(fileno(fp), NULL); /* allocate and load index */ |
457 |
< |
/* we're dropping fp here.... */ |
455 |
> |
fpos = ftell(fp); /* get stdio position */ |
456 |
> |
fd = dup(fileno(fp)); |
457 |
> |
fclose(fp); /* done with stdio */ |
458 |
> |
lseek(fd, fpos, 0); /* align system file pointer */ |
459 |
> |
hdinit(fd, NULL); /* allocate and load index */ |
460 |
|
} |
461 |
|
|
462 |
|
|
463 |
|
done_packets(pl) /* handle finished packets */ |
464 |
|
PACKET *pl; |
465 |
|
{ |
466 |
+ |
static int nunflushed = 0; |
467 |
|
register PACKET *p; |
468 |
|
|
469 |
|
while (pl != NULL) { |
473 |
|
(char *)hdnewrays(hdlist[p->hd],p->bi,p->nr), |
474 |
|
p->nr*sizeof(RAYVAL)); |
475 |
|
if (outdev != NULL) /* display it */ |
476 |
< |
disp_packet(p); |
476 |
> |
disp_packet((PACKHEAD *)p); |
477 |
> |
else |
478 |
> |
nunflushed += p->nr; |
479 |
> |
nraysdone += p->nr; |
480 |
> |
npacksdone++; |
481 |
|
} |
402 |
– |
nraysdone += p->nr; |
403 |
– |
npacksdone++; |
482 |
|
p->nr = 0; /* push onto free list */ |
483 |
|
p->next = freepacks; |
484 |
|
freepacks = p; |
485 |
|
} |
486 |
+ |
if (nunflushed >= 256*RPACKSIZ) { |
487 |
+ |
hdflush(NULL); /* flush holodeck buffers */ |
488 |
+ |
nunflushed = 0; |
489 |
+ |
} |
490 |
|
} |
491 |
|
|
492 |
|
|
493 |
|
getradfile(rfargs) /* run rad and get needed variables */ |
494 |
|
char *rfargs; |
495 |
|
{ |
496 |
< |
static short mvar[] = {VIEW,OCTREE,EXPOSURE,-1}; |
496 |
> |
static short mvar[] = {OCTREE,EXPOSURE,-1}; |
497 |
|
static char tf1[] = TEMPLATE; |
498 |
|
char tf2[64]; |
499 |
|
char combuf[256]; |
500 |
+ |
char *pippt; |
501 |
|
register int i; |
502 |
|
register char *cp; |
503 |
|
/* create rad command */ |
507 |
|
"rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH", |
508 |
|
rfargs, tf1); |
509 |
|
cp = combuf; |
510 |
< |
while (*cp) cp++; /* match unset variables */ |
510 |
> |
while (*cp){ |
511 |
> |
if (*cp == '|') pippt = cp; |
512 |
> |
cp++; |
513 |
> |
} /* match unset variables */ |
514 |
|
for (i = 0; mvar[i] >= 0; i++) |
515 |
|
if (!vdef(mvar[i])) { |
516 |
|
*cp++ = '|'; |
517 |
|
strcpy(cp, vnam(mvar[i])); |
518 |
|
while (*cp) cp++; |
519 |
+ |
pippt = NULL; |
520 |
|
} |
521 |
< |
sprintf(cp, ")[ \t]*=' > %s", tf2); |
521 |
> |
if (pippt != NULL) |
522 |
> |
strcpy(pippt, "> /dev/null"); /* nothing to match */ |
523 |
> |
else |
524 |
> |
sprintf(cp, ")[ \t]*=' > %s", tf2); |
525 |
|
if (system(combuf)) { |
526 |
|
error(SYSTEM, "cannot execute rad command"); |
527 |
|
unlink(tf2); /* clean up */ |
528 |
|
unlink(tf1); |
529 |
|
quit(1); |
530 |
|
} |
531 |
< |
loadvars(tf2); /* load variables */ |
531 |
> |
if (pippt == NULL) { |
532 |
> |
loadvars(tf2); /* load variables */ |
533 |
> |
unlink(tf2); |
534 |
> |
} |
535 |
|
rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */ |
536 |
< |
unlink(tf2); /* clean up */ |
444 |
< |
unlink(tf1); |
536 |
> |
unlink(tf1); /* clean up */ |
537 |
|
} |
538 |
|
|
539 |
|
|
600 |
|
if (vdef(REPORT)) { |
601 |
|
long fsiz, fuse; |
602 |
|
report(0); |
603 |
< |
fsiz = lseek(hdlist[0]->fd, 0L, 2); |
603 |
> |
fsiz = hdfilen(hdlist[0]->fd); |
604 |
|
fuse = hdfiluse(hdlist[0]->fd, 1); |
605 |
|
fprintf(stderr, |
606 |
< |
"%s: %.1f Mbyte holodeck file, %.2f%% fragmentation\n", |
606 |
> |
"%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", |
607 |
|
hdkfile, fsiz/(1024.*1024.), |
608 |
|
100.*(fsiz-fuse)/fsiz); |
609 |
|
} |
610 |
|
} else |
611 |
|
hdflush(NULL); |
612 |
|
} |
613 |
+ |
if (orig_mode >= 0) /* reset holodeck access mode */ |
614 |
+ |
fchmod(hdlist[0]->fd, orig_mode); |
615 |
+ |
if (outdev != NULL) /* close display */ |
616 |
+ |
disp_close(); |
617 |
|
exit(ec ? ec : status); /* exit */ |
618 |
|
} |