| 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 */ |
| 26 |
|
|
| 27 |
|
int nowarn = 0; /* turn warnings off? */ |
| 28 |
|
|
| 26 |
– |
double expval = 1.; /* global exposure value */ |
| 27 |
– |
|
| 29 |
|
int ncprocs = 0; /* desired number of compute processes */ |
| 30 |
|
|
| 31 |
|
char *outdev = NULL; /* output device name */ |
| 39 |
|
int rtargc = 1; /* rtrace command */ |
| 40 |
|
char *rtargv[128] = {"rtrace", NULL}; |
| 41 |
|
|
| 42 |
+ |
int orig_mode = -1; /* original file mode (-1 if unchanged) */ |
| 43 |
+ |
|
| 44 |
|
long nraysdone = 0L; /* number of rays done */ |
| 45 |
|
long npacksdone = 0L; /* number of packets done */ |
| 46 |
|
|
| 47 |
|
PACKET *freepacks; /* available packets */ |
| 48 |
|
|
| 49 |
+ |
char *sigerr[NSIG]; /* signal error messages */ |
| 50 |
+ |
|
| 51 |
|
extern time_t time(); |
| 52 |
|
|
| 53 |
|
|
| 60 |
|
int force = 0; |
| 61 |
|
/* mark start time */ |
| 62 |
|
starttime = time(NULL); |
| 63 |
+ |
initurand(10240); /* initialize urand */ |
| 64 |
|
progname = argv[0]; /* get arguments */ |
| 65 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
| 66 |
|
switch (argv[i][1]) { |
| 113 |
|
creatholo(&hdg); |
| 114 |
|
} else { /* else load holodeck */ |
| 115 |
|
loadholo(); |
| 116 |
< |
if (vdef(RIF)) /* load RIF if any */ |
| 116 |
> |
/* check settings */ |
| 117 |
> |
checkvalues(); |
| 118 |
> |
/* load RIF if any */ |
| 119 |
> |
if (vdef(RIF)) |
| 120 |
|
getradfile(vval(RIF)); |
| 121 |
+ |
/* set defaults */ |
| 122 |
+ |
setdefaults(NULL); |
| 123 |
|
} |
| 124 |
|
/* initialize */ |
| 125 |
|
initrholo(); |
| 126 |
< |
/* run */ |
| 126 |
> |
/* main loop */ |
| 127 |
|
while (rholo()) |
| 128 |
|
; |
| 129 |
|
/* done */ |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
|
| 139 |
+ |
onsig(signo) /* fatal signal */ |
| 140 |
+ |
int signo; |
| 141 |
+ |
{ |
| 142 |
+ |
static int gotsig = 0; |
| 143 |
+ |
|
| 144 |
+ |
if (gotsig++) /* two signals and we're gone! */ |
| 145 |
+ |
_exit(signo); |
| 146 |
+ |
|
| 147 |
+ |
alarm(30); /* allow 30 seconds to clean up */ |
| 148 |
+ |
signal(SIGALRM, SIG_DFL); /* make certain we do die */ |
| 149 |
+ |
eputs("signal - "); |
| 150 |
+ |
eputs(sigerr[signo]); |
| 151 |
+ |
eputs("\n"); |
| 152 |
+ |
quit(3); |
| 153 |
+ |
} |
| 154 |
+ |
|
| 155 |
+ |
|
| 156 |
+ |
sigdie(signo, msg) /* set fatal signal */ |
| 157 |
+ |
int signo; |
| 158 |
+ |
char *msg; |
| 159 |
+ |
{ |
| 160 |
+ |
if (signal(signo, onsig) == SIG_IGN) |
| 161 |
+ |
signal(signo, SIG_IGN); |
| 162 |
+ |
sigerr[signo] = msg; |
| 163 |
+ |
} |
| 164 |
+ |
|
| 165 |
+ |
|
| 166 |
+ |
int |
| 167 |
+ |
resfmode(fd, mod) /* restrict open file access mode */ |
| 168 |
+ |
int fd, mod; |
| 169 |
+ |
{ |
| 170 |
+ |
struct stat stbuf; |
| 171 |
+ |
/* get original mode */ |
| 172 |
+ |
if (fstat(fd, &stbuf) < 0) |
| 173 |
+ |
error(SYSTEM, "cannot stat open holodeck file"); |
| 174 |
+ |
mod &= stbuf.st_mode; /* always more restrictive */ |
| 175 |
+ |
if (mod == stbuf.st_mode) |
| 176 |
+ |
return(-1); /* already set */ |
| 177 |
+ |
/* else change it */ |
| 178 |
+ |
if (fchmod(fd, mod) < 0) { |
| 179 |
+ |
error(WARNING, "cannot change holodeck file access mode"); |
| 180 |
+ |
return(-1); |
| 181 |
+ |
} |
| 182 |
+ |
return(stbuf.st_mode); /* return original mode */ |
| 183 |
+ |
} |
| 184 |
+ |
|
| 185 |
+ |
|
| 186 |
|
initrholo() /* get our holodeck running */ |
| 187 |
|
{ |
| 188 |
|
extern int global_packet(); |
| 196 |
|
if (!vdef(DISKSPACE)) |
| 197 |
|
maxdisk = 0; |
| 198 |
|
else |
| 199 |
< |
maxdisk = 1024.*1024.*vflt(DISKSPACE)); |
| 199 |
> |
maxdisk = 1024.*1024.*vflt(DISKSPACE); |
| 200 |
|
/* record end time */ |
| 201 |
|
if (!vdef(TIME) || vflt(TIME) <= FTINY) |
| 202 |
|
endtime = 0; |
| 242 |
|
freepacks[i].next = &freepacks[i+1]; |
| 243 |
|
} |
| 244 |
|
} |
| 245 |
+ |
/* set up signal handling */ |
| 246 |
+ |
sigdie(SIGINT, "Interrupt"); |
| 247 |
+ |
sigdie(SIGHUP, "Hangup"); |
| 248 |
+ |
sigdie(SIGTERM, "Terminate"); |
| 249 |
+ |
sigdie(SIGPIPE, "Broken pipe"); |
| 250 |
+ |
sigdie(SIGALRM, "Alarm clock"); |
| 251 |
+ |
#ifdef SIGXCPU |
| 252 |
+ |
sigdie(SIGXCPU, "CPU limit exceeded"); |
| 253 |
+ |
sigdie(SIGXFSZ, "File size exceeded"); |
| 254 |
+ |
#endif |
| 255 |
+ |
/* protect holodeck file */ |
| 256 |
+ |
orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444); |
| 257 |
|
return; |
| 258 |
|
memerr: |
| 259 |
|
error(SYSTEM, "out of memory in initrholo"); |
| 275 |
|
if (ncprocs <= 0) |
| 276 |
|
return(1); |
| 277 |
|
/* check file size */ |
| 278 |
< |
if (maxdisk > 0 && hdfiluse(hdlist[0]->fd,0)+hdmemuse(0) >= maxdisk) { |
| 278 |
> |
if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) { |
| 279 |
|
error(WARNING, "file limit exceeded"); |
| 280 |
|
return(0); |
| 281 |
|
} |
| 338 |
|
sprintf(errmsg, "ignoring all but first %s", vnam(SECTION)); |
| 339 |
|
error(WARNING, errmsg); |
| 340 |
|
} |
| 341 |
+ |
if (!vdef(OCTREE)) { |
| 342 |
+ |
if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL) |
| 343 |
+ |
error(SYSTEM, "out of memory"); |
| 344 |
+ |
sprintf(vval(OCTREE), "%s.oct", froot); |
| 345 |
+ |
vdef(OCTREE)++; |
| 346 |
+ |
} |
| 347 |
+ |
if (!vdef(OBSTRUCTIONS)) { |
| 348 |
+ |
vval(OBSTRUCTIONS) = "T"; |
| 349 |
+ |
vdef(OBSTRUCTIONS)++; |
| 350 |
+ |
} |
| 351 |
+ |
if (!vdef(VDIST)) { |
| 352 |
+ |
vval(VDIST) = "F"; |
| 353 |
+ |
vdef(VDIST)++; |
| 354 |
+ |
} |
| 355 |
+ |
if (!vdef(OCCUPANCY)) { |
| 356 |
+ |
vval(OCCUPANCY) = "U"; |
| 357 |
+ |
vdef(OCCUPANCY)++; |
| 358 |
+ |
} |
| 359 |
+ |
/* append rendering options */ |
| 360 |
+ |
if (vdef(RENDER)) |
| 361 |
+ |
rtargc += wordstring(rtargv+rtargc, vval(RENDER)); |
| 362 |
+ |
|
| 363 |
+ |
if (gp == NULL) /* already initialized? */ |
| 364 |
+ |
return; |
| 365 |
+ |
/* set grid parameters */ |
| 366 |
|
if (sscanf(vval(SECTION), |
| 367 |
|
"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", |
| 368 |
|
&gp->orig[0], &gp->orig[1], &gp->orig[2], |
| 383 |
|
badvalue(GRID); |
| 384 |
|
for (i = 0; i < 3; i++) |
| 385 |
|
gp->grid[i] = len[i]/d + (1.-FTINY); |
| 291 |
– |
if (!vdef(EXPOSURE)) { |
| 292 |
– |
sprintf(errmsg, "%s must be defined", vnam(EXPOSURE)); |
| 293 |
– |
error(USER, errmsg); |
| 294 |
– |
} |
| 295 |
– |
expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ? |
| 296 |
– |
pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE); |
| 297 |
– |
if (!vdef(OCTREE)) { |
| 298 |
– |
if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL) |
| 299 |
– |
error(SYSTEM, "out of memory"); |
| 300 |
– |
sprintf(vval(OCTREE), "%s.oct", froot); |
| 301 |
– |
vdef(OCTREE)++; |
| 302 |
– |
} |
| 303 |
– |
if (!vdef(OBSTRUCTIONS)) { |
| 304 |
– |
vval(OBSTRUCTIONS) = "T"; |
| 305 |
– |
vdef(OBSTRUCTIONS)++; |
| 306 |
– |
} |
| 307 |
– |
if (!vdef(OCCUPANCY)) { |
| 308 |
– |
vval(OCCUPANCY) = "U"; |
| 309 |
– |
vdef(OCCUPANCY)++; |
| 310 |
– |
} |
| 311 |
– |
/* append rendering options */ |
| 312 |
– |
if (vdef(RENDER)) |
| 313 |
– |
rtargc += wordstring(rtargv+rtargc, vval(RENDER)); |
| 386 |
|
} |
| 387 |
|
|
| 388 |
|
|
| 389 |
|
creatholo(gp) /* create a holodeck output file */ |
| 390 |
|
HDGRID *gp; |
| 391 |
|
{ |
| 392 |
+ |
extern char VersionID[]; |
| 393 |
|
long endloc = 0; |
| 394 |
+ |
int fd; |
| 395 |
|
FILE *fp; |
| 396 |
|
/* open & truncate file */ |
| 397 |
|
if ((fp = fopen(hdkfile, "w+")) == NULL) { |
| 400 |
|
} |
| 401 |
|
/* write information header */ |
| 402 |
|
newheader("RADIANCE", fp); |
| 403 |
+ |
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
| 404 |
|
printvars(fp); |
| 405 |
|
fputformat(HOLOFMT, fp); |
| 406 |
|
fputc('\n', fp); |
| 407 |
|
putw(HOLOMAGIC, fp); /* put magic number & terminus */ |
| 408 |
|
fwrite(&endloc, sizeof(long), 1, fp); |
| 409 |
< |
fflush(fp); /* flush buffer */ |
| 410 |
< |
hdinit(fileno(fp), gp); /* allocate and initialize index */ |
| 411 |
< |
/* we're dropping fp here.... */ |
| 409 |
> |
fd = dup(fileno(fp)); |
| 410 |
> |
fclose(fp); /* flush and close stdio stream */ |
| 411 |
> |
hdinit(fd, gp); /* allocate and initialize index */ |
| 412 |
|
} |
| 413 |
|
|
| 414 |
|
|
| 438 |
|
|
| 439 |
|
loadholo() /* start loading a holodeck from fname */ |
| 440 |
|
{ |
| 441 |
+ |
extern long ftell(); |
| 442 |
|
FILE *fp; |
| 443 |
< |
long endloc; |
| 443 |
> |
int fd; |
| 444 |
> |
long fpos; |
| 445 |
|
/* open holodeck file */ |
| 446 |
|
if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { |
| 447 |
|
sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile, |
| 456 |
|
hdkfile); |
| 457 |
|
error(USER, errmsg); |
| 458 |
|
} |
| 459 |
< |
fread(&endloc, sizeof(long), 1, fp); |
| 460 |
< |
if (endloc != 0) |
| 459 |
> |
fread(&fpos, sizeof(long), 1, fp); |
| 460 |
> |
if (fpos != 0) |
| 461 |
|
error(WARNING, "ignoring multiple sections in holodeck file"); |
| 462 |
< |
fseek(fp, 0L, 1); /* align system file pointer */ |
| 463 |
< |
hdinit(fileno(fp), NULL); /* allocate and load index */ |
| 464 |
< |
/* we're dropping fp here.... */ |
| 462 |
> |
fpos = ftell(fp); /* get stdio position */ |
| 463 |
> |
fd = dup(fileno(fp)); |
| 464 |
> |
fclose(fp); /* done with stdio */ |
| 465 |
> |
lseek(fd, fpos, 0); /* align system file pointer */ |
| 466 |
> |
hdinit(fd, NULL); /* allocate and load index */ |
| 467 |
|
} |
| 468 |
|
|
| 469 |
|
|
| 470 |
|
done_packets(pl) /* handle finished packets */ |
| 471 |
|
PACKET *pl; |
| 472 |
|
{ |
| 473 |
< |
static int nunflushed = 0; |
| 473 |
> |
static int n2flush = 0; |
| 474 |
|
register PACKET *p; |
| 475 |
|
|
| 476 |
|
while (pl != NULL) { |
| 481 |
|
p->nr*sizeof(RAYVAL)); |
| 482 |
|
if (outdev != NULL) /* display it */ |
| 483 |
|
disp_packet((PACKHEAD *)p); |
| 484 |
< |
else |
| 485 |
< |
nunflushed += p->nr; |
| 484 |
> |
if (hdcachesize <= 0) /* manual flushing */ |
| 485 |
> |
n2flush += p->nr; |
| 486 |
|
nraysdone += p->nr; |
| 487 |
|
npacksdone++; |
| 488 |
|
} |
| 490 |
|
p->next = freepacks; |
| 491 |
|
freepacks = p; |
| 492 |
|
} |
| 493 |
< |
if (nunflushed >= 256*RPACKSIZ) { |
| 493 |
> |
if (n2flush > 512*RPACKSIZ*ncprocs) { |
| 494 |
|
hdflush(NULL); /* flush holodeck buffers */ |
| 495 |
< |
nunflushed = 0; |
| 495 |
> |
n2flush = 0; |
| 496 |
|
} |
| 497 |
|
} |
| 498 |
|
|
| 500 |
|
getradfile(rfargs) /* run rad and get needed variables */ |
| 501 |
|
char *rfargs; |
| 502 |
|
{ |
| 503 |
< |
static short mvar[] = {VIEW,OCTREE,EXPOSURE,-1}; |
| 503 |
> |
static short mvar[] = {OCTREE,-1}; |
| 504 |
|
static char tf1[] = TEMPLATE; |
| 505 |
|
char tf2[64]; |
| 506 |
|
char combuf[256]; |
| 599 |
|
{ |
| 600 |
|
int status = 0; |
| 601 |
|
|
| 523 |
– |
if (outdev != NULL) /* close display */ |
| 524 |
– |
disp_close(); |
| 602 |
|
if (hdlist[0] != NULL) { /* flush holodeck */ |
| 603 |
|
if (ncprocs > 0) { |
| 604 |
|
done_packets(flush_queue()); |
| 607 |
|
if (vdef(REPORT)) { |
| 608 |
|
long fsiz, fuse; |
| 609 |
|
report(0); |
| 610 |
< |
fsiz = lseek(hdlist[0]->fd, 0L, 2); |
| 610 |
> |
fsiz = hdfilen(hdlist[0]->fd); |
| 611 |
|
fuse = hdfiluse(hdlist[0]->fd, 1); |
| 612 |
|
fprintf(stderr, |
| 613 |
|
"%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", |
| 617 |
|
} else |
| 618 |
|
hdflush(NULL); |
| 619 |
|
} |
| 620 |
+ |
if (orig_mode >= 0) /* reset holodeck access mode */ |
| 621 |
+ |
fchmod(hdlist[0]->fd, orig_mode); |
| 622 |
+ |
if (outdev != NULL) /* close display */ |
| 623 |
+ |
disp_close(); |
| 624 |
|
exit(ec ? ec : status); /* exit */ |
| 625 |
|
} |