9 |
|
*/ |
10 |
|
|
11 |
|
#include "rholo.h" |
12 |
+ |
#include "random.h" |
13 |
|
#include "paths.h" |
14 |
+ |
#include <signal.h> |
15 |
|
#include <sys/types.h> |
16 |
+ |
#include <sys/stat.h> |
17 |
|
|
18 |
|
/* the following must be consistent with rholo.h */ |
19 |
|
int NVARS = NRHVARS; /* total number of variables */ |
36 |
|
time_t endtime; /* time we should end by */ |
37 |
|
time_t reporttime; /* time for next report */ |
38 |
|
|
39 |
+ |
long maxdisk; /* maximum file space (bytes) */ |
40 |
+ |
|
41 |
|
int rtargc = 1; /* rtrace command */ |
42 |
|
char *rtargv[128] = {"rtrace", NULL}; |
43 |
|
|
44 |
+ |
int orig_mode = -1; /* original file mode (-1 if unchanged) */ |
45 |
+ |
|
46 |
|
long nraysdone = 0L; /* number of rays done */ |
47 |
|
long npacksdone = 0L; /* number of packets done */ |
48 |
|
|
49 |
|
PACKET *freepacks; /* available packets */ |
50 |
|
|
51 |
+ |
char *sigerr[NSIG]; /* signal error messages */ |
52 |
+ |
|
53 |
|
extern time_t time(); |
54 |
|
|
55 |
|
|
62 |
|
int force = 0; |
63 |
|
/* mark start time */ |
64 |
|
starttime = time(NULL); |
65 |
+ |
initurand(10240); /* initialize urand */ |
66 |
|
progname = argv[0]; /* get arguments */ |
67 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
68 |
|
switch (argv[i][1]) { |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
+ |
onsig(signo) /* fatal signal */ |
137 |
+ |
int signo; |
138 |
+ |
{ |
139 |
+ |
static int gotsig = 0; |
140 |
+ |
|
141 |
+ |
if (gotsig++) /* two signals and we're gone! */ |
142 |
+ |
_exit(signo); |
143 |
+ |
|
144 |
+ |
alarm(30); /* allow 30 seconds to clean up */ |
145 |
+ |
signal(SIGALRM, SIG_DFL); /* make certain we do die */ |
146 |
+ |
eputs("signal - "); |
147 |
+ |
eputs(sigerr[signo]); |
148 |
+ |
eputs("\n"); |
149 |
+ |
quit(3); |
150 |
+ |
} |
151 |
+ |
|
152 |
+ |
|
153 |
+ |
sigdie(signo, msg) /* set fatal signal */ |
154 |
+ |
int signo; |
155 |
+ |
char *msg; |
156 |
+ |
{ |
157 |
+ |
if (signal(signo, onsig) == SIG_IGN) |
158 |
+ |
signal(signo, SIG_IGN); |
159 |
+ |
sigerr[signo] = msg; |
160 |
+ |
} |
161 |
+ |
|
162 |
+ |
|
163 |
+ |
int |
164 |
+ |
resfmode(fd, mod) /* restrict open file access mode */ |
165 |
+ |
int fd, mod; |
166 |
+ |
{ |
167 |
+ |
struct stat stbuf; |
168 |
+ |
/* get original mode */ |
169 |
+ |
if (fstat(fd, &stbuf) < 0) |
170 |
+ |
error(SYSTEM, "cannot stat open holodeck file"); |
171 |
+ |
mod &= stbuf.st_mode; /* always more restrictive */ |
172 |
+ |
if (mod == stbuf.st_mode) |
173 |
+ |
return(-1); /* already set */ |
174 |
+ |
/* else change it */ |
175 |
+ |
if (fchmod(fd, mod) < 0) { |
176 |
+ |
error(WARNING, "cannot change holodeck file access mode"); |
177 |
+ |
return(-1); |
178 |
+ |
} |
179 |
+ |
return(stbuf.st_mode); /* return original mode */ |
180 |
+ |
} |
181 |
+ |
|
182 |
+ |
|
183 |
|
initrholo() /* get our holodeck running */ |
184 |
|
{ |
185 |
|
extern int global_packet(); |
186 |
|
register int i; |
187 |
< |
/* check output device */ |
188 |
< |
if (outdev != NULL) |
189 |
< |
open_display(outdev); |
187 |
> |
|
188 |
> |
if (outdev != NULL) /* open output device */ |
189 |
> |
disp_open(outdev); |
190 |
|
else if (ncprocs > 0) /* else use global ray feed */ |
191 |
|
init_global(); |
192 |
+ |
/* record disk space limit */ |
193 |
+ |
if (!vdef(DISKSPACE)) |
194 |
+ |
maxdisk = 0; |
195 |
+ |
else |
196 |
+ |
maxdisk = 1024.*1024.*vflt(DISKSPACE); |
197 |
|
/* record end time */ |
198 |
|
if (!vdef(TIME) || vflt(TIME) <= FTINY) |
199 |
|
endtime = 0; |
200 |
|
else |
201 |
|
endtime = starttime + vflt(TIME)*3600.; |
202 |
|
/* set up memory cache */ |
203 |
< |
hdcachesize = 1024.*1024.*vflt(CACHE); |
203 |
> |
if (outdev == NULL) |
204 |
> |
hdcachesize = 0; /* manual flushing */ |
205 |
> |
else if (vdef(CACHE)) |
206 |
> |
hdcachesize = 1024.*1024.*vflt(CACHE); |
207 |
|
/* open report file */ |
208 |
|
if (vdef(REPORT)) { |
209 |
|
register char *s = sskip2(vval(REPORT), 1); |
239 |
|
freepacks[i].next = &freepacks[i+1]; |
240 |
|
} |
241 |
|
} |
242 |
+ |
/* set up signal handling */ |
243 |
+ |
sigdie(SIGINT, "Interrupt"); |
244 |
+ |
sigdie(SIGHUP, "Hangup"); |
245 |
+ |
sigdie(SIGTERM, "Terminate"); |
246 |
+ |
sigdie(SIGPIPE, "Broken pipe"); |
247 |
+ |
sigdie(SIGALRM, "Alarm clock"); |
248 |
+ |
#ifdef SIGXCPU |
249 |
+ |
sigdie(SIGXCPU, "CPU limit exceeded"); |
250 |
+ |
sigdie(SIGXFSZ, "File size exceeded"); |
251 |
+ |
#endif |
252 |
+ |
/* protect holodeck file */ |
253 |
+ |
orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444); |
254 |
|
return; |
255 |
|
memerr: |
256 |
|
error(SYSTEM, "out of memory in initrholo"); |
264 |
|
register PACKET *p; |
265 |
|
time_t t; |
266 |
|
long l; |
267 |
< |
/* check display */ |
268 |
< |
if (outdev != NULL && !disp_check(idle)) |
269 |
< |
return(0); |
267 |
> |
|
268 |
> |
if (outdev != NULL) /* check display */ |
269 |
> |
if (!disp_check(idle)) |
270 |
> |
return(0); |
271 |
|
/* display only? */ |
272 |
|
if (ncprocs <= 0) |
273 |
|
return(1); |
274 |
|
/* check file size */ |
275 |
< |
if ((l = 1024.*1024.*vflt(DISKSPACE)) > 0 && |
276 |
< |
hdfiluse(hdlist[0]->fd, 0) + hdmemuse(0) >= l) |
275 |
> |
if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) { |
276 |
> |
error(WARNING, "file limit exceeded"); |
277 |
|
return(0); |
278 |
+ |
} |
279 |
|
/* check time */ |
280 |
|
if (endtime > 0 || reporttime > 0) |
281 |
|
t = time(NULL); |
282 |
< |
if (endtime > 0 && t >= endtime) |
282 |
> |
if (endtime > 0 && t >= endtime) { |
283 |
> |
error(WARNING, "time limit exceeded"); |
284 |
|
return(0); |
285 |
+ |
} |
286 |
|
if (reporttime > 0 && t >= reporttime) |
287 |
|
report(t); |
288 |
|
/* get packets to process */ |
367 |
|
sprintf(vval(OCTREE), "%s.oct", froot); |
368 |
|
vdef(OCTREE)++; |
369 |
|
} |
289 |
– |
if (!vdef(DISKSPACE)) { |
290 |
– |
sprintf(errmsg, |
291 |
– |
"no %s setting, assuming 100 Mbytes available", |
292 |
– |
vnam(DISKSPACE)); |
293 |
– |
error(WARNING, errmsg); |
294 |
– |
vval(DISKSPACE) = "100"; |
295 |
– |
vdef(DISKSPACE)++; |
296 |
– |
} |
297 |
– |
if (!vdef(CACHE)) { |
298 |
– |
sprintf(errmsg, |
299 |
– |
"no %s setting, assuming 10 Mbytes available", |
300 |
– |
vnam(CACHE)); |
301 |
– |
error(WARNING, errmsg); |
302 |
– |
vval(CACHE) = "10"; |
303 |
– |
vdef(CACHE)++; |
304 |
– |
} |
370 |
|
if (!vdef(OBSTRUCTIONS)) { |
371 |
|
vval(OBSTRUCTIONS) = "T"; |
372 |
|
vdef(OBSTRUCTIONS)++; |
373 |
|
} |
374 |
+ |
if (!vdef(VDIST)) { |
375 |
+ |
vval(VDIST) = "F"; |
376 |
+ |
vdef(VDIST)++; |
377 |
+ |
} |
378 |
|
if (!vdef(OCCUPANCY)) { |
379 |
|
vval(OCCUPANCY) = "U"; |
380 |
|
vdef(OCCUPANCY)++; |
388 |
|
creatholo(gp) /* create a holodeck output file */ |
389 |
|
HDGRID *gp; |
390 |
|
{ |
391 |
+ |
extern char VersionID[]; |
392 |
|
long endloc = 0; |
393 |
+ |
int fd; |
394 |
|
FILE *fp; |
395 |
|
/* open & truncate file */ |
396 |
|
if ((fp = fopen(hdkfile, "w+")) == NULL) { |
399 |
|
} |
400 |
|
/* write information header */ |
401 |
|
newheader("RADIANCE", fp); |
402 |
+ |
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
403 |
|
printvars(fp); |
404 |
|
fputformat(HOLOFMT, fp); |
405 |
|
fputc('\n', fp); |
406 |
|
putw(HOLOMAGIC, fp); /* put magic number & terminus */ |
407 |
|
fwrite(&endloc, sizeof(long), 1, fp); |
408 |
< |
fflush(fp); /* flush buffer */ |
409 |
< |
hdinit(fileno(fp), gp); /* allocate and initialize index */ |
410 |
< |
/* we're dropping fp here.... */ |
408 |
> |
fd = dup(fileno(fp)); |
409 |
> |
fclose(fp); /* flush and close stdio stream */ |
410 |
> |
hdinit(fd, gp); /* allocate and initialize index */ |
411 |
|
} |
412 |
|
|
413 |
|
|
437 |
|
|
438 |
|
loadholo() /* start loading a holodeck from fname */ |
439 |
|
{ |
440 |
+ |
extern long ftell(); |
441 |
|
FILE *fp; |
442 |
< |
long endloc; |
443 |
< |
/* open input file */ |
444 |
< |
if ((fp = fopen(hdkfile, "r+")) == NULL) { |
445 |
< |
sprintf(errmsg, "cannot open \"%s\" for appending", hdkfile); |
442 |
> |
int fd; |
443 |
> |
long fpos; |
444 |
> |
/* open holodeck file */ |
445 |
> |
if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { |
446 |
> |
sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile, |
447 |
> |
ncprocs>0 ? "appending" : "reading"); |
448 |
|
error(SYSTEM, errmsg); |
449 |
|
} |
450 |
|
/* load variables from header */ |
455 |
|
hdkfile); |
456 |
|
error(USER, errmsg); |
457 |
|
} |
458 |
< |
fread(&endloc, sizeof(long), 1, fp); |
459 |
< |
if (endloc != 0) |
458 |
> |
fread(&fpos, sizeof(long), 1, fp); |
459 |
> |
if (fpos != 0) |
460 |
|
error(WARNING, "ignoring multiple sections in holodeck file"); |
461 |
< |
fseek(fp, 0L, 1); /* align system file pointer */ |
462 |
< |
hdinit(fileno(fp), NULL); /* allocate and load index */ |
463 |
< |
/* we're dropping fp here.... */ |
461 |
> |
fpos = ftell(fp); /* get stdio position */ |
462 |
> |
fd = dup(fileno(fp)); |
463 |
> |
fclose(fp); /* done with stdio */ |
464 |
> |
lseek(fd, fpos, 0); /* align system file pointer */ |
465 |
> |
hdinit(fd, NULL); /* allocate and load index */ |
466 |
|
} |
467 |
|
|
468 |
|
|
469 |
|
done_packets(pl) /* handle finished packets */ |
470 |
|
PACKET *pl; |
471 |
|
{ |
472 |
+ |
static int n2flush = 0; |
473 |
|
register PACKET *p; |
474 |
|
|
475 |
|
while (pl != NULL) { |
479 |
|
(char *)hdnewrays(hdlist[p->hd],p->bi,p->nr), |
480 |
|
p->nr*sizeof(RAYVAL)); |
481 |
|
if (outdev != NULL) /* display it */ |
482 |
< |
disp_packet(p); |
482 |
> |
disp_packet((PACKHEAD *)p); |
483 |
> |
if (hdcachesize <= 0) /* manual flushing */ |
484 |
> |
n2flush += p->nr; |
485 |
> |
nraysdone += p->nr; |
486 |
> |
npacksdone++; |
487 |
|
} |
406 |
– |
nraysdone += p->nr; |
407 |
– |
npacksdone++; |
488 |
|
p->nr = 0; /* push onto free list */ |
489 |
|
p->next = freepacks; |
490 |
|
freepacks = p; |
491 |
|
} |
492 |
+ |
if (n2flush > 512*RPACKSIZ*ncprocs) { |
493 |
+ |
hdflush(NULL); /* flush holodeck buffers */ |
494 |
+ |
n2flush = 0; |
495 |
+ |
} |
496 |
|
} |
497 |
|
|
498 |
|
|
499 |
|
getradfile(rfargs) /* run rad and get needed variables */ |
500 |
|
char *rfargs; |
501 |
|
{ |
502 |
< |
static short mvar[] = {VIEW,OCTREE,EXPOSURE,-1}; |
502 |
> |
static short mvar[] = {OCTREE,EXPOSURE,-1}; |
503 |
|
static char tf1[] = TEMPLATE; |
504 |
|
char tf2[64]; |
505 |
|
char combuf[256]; |
606 |
|
if (vdef(REPORT)) { |
607 |
|
long fsiz, fuse; |
608 |
|
report(0); |
609 |
< |
fsiz = lseek(hdlist[0]->fd, 0L, 2); |
609 |
> |
fsiz = hdfilen(hdlist[0]->fd); |
610 |
|
fuse = hdfiluse(hdlist[0]->fd, 1); |
611 |
|
fprintf(stderr, |
612 |
|
"%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", |
616 |
|
} else |
617 |
|
hdflush(NULL); |
618 |
|
} |
619 |
+ |
if (orig_mode >= 0) /* reset holodeck access mode */ |
620 |
+ |
fchmod(hdlist[0]->fd, orig_mode); |
621 |
+ |
if (outdev != NULL) /* close display */ |
622 |
+ |
disp_close(); |
623 |
|
exit(ec ? ec : status); /* exit */ |
624 |
|
} |