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 */ |
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(); |
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"); |
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; |
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, |
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 nunflushed = 0; |
472 |
> |
static int n2flush = 0; |
473 |
|
register PACKET *p; |
474 |
|
|
475 |
|
while (pl != NULL) { |
480 |
|
p->nr*sizeof(RAYVAL)); |
481 |
|
if (outdev != NULL) /* display it */ |
482 |
|
disp_packet((PACKHEAD *)p); |
483 |
< |
else |
484 |
< |
nunflushed += p->nr; |
483 |
> |
if (hdcachesize <= 0) /* manual flushing */ |
484 |
> |
n2flush += p->nr; |
485 |
|
nraysdone += p->nr; |
486 |
|
npacksdone++; |
487 |
|
} |
489 |
|
p->next = freepacks; |
490 |
|
freepacks = p; |
491 |
|
} |
492 |
< |
if (nunflushed >= 256*RPACKSIZ) { |
492 |
> |
if (n2flush > 512*RPACKSIZ*ncprocs) { |
493 |
|
hdflush(NULL); /* flush holodeck buffers */ |
494 |
< |
nunflushed = 0; |
494 |
> |
n2flush = 0; |
495 |
|
} |
496 |
|
} |
497 |
|
|
598 |
|
{ |
599 |
|
int status = 0; |
600 |
|
|
523 |
– |
if (outdev != NULL) /* close display */ |
524 |
– |
disp_close(); |
601 |
|
if (hdlist[0] != NULL) { /* flush holodeck */ |
602 |
|
if (ncprocs > 0) { |
603 |
|
done_packets(flush_queue()); |
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 |
|
} |