| 5 |  | * Radiance holodeck generation controller | 
| 6 |  | */ | 
| 7 |  |  | 
| 8 | + | #include <stdio.h> | 
| 9 | + | #include <time.h> | 
| 10 |  | #include <signal.h> | 
| 11 |  | #include <sys/stat.h> | 
| 12 |  | #include <string.h> | 
| 13 |  |  | 
| 12 | – | #include "rholo.h" | 
| 14 |  | #include "platform.h" | 
| 15 | + | #include "rterror.h" | 
| 16 | + | #include "resolu.h" | 
| 17 | + | #include "rholo.h" | 
| 18 |  | #include "random.h" | 
| 19 |  |  | 
| 20 |  | #ifndef FRAGWARN | 
| 21 |  | #define FRAGWARN        20              /* fragmentation for warning (%) */ | 
| 22 |  | #endif | 
| 23 |  | #ifndef MAXQTIME | 
| 24 | < | #define MAXQTIME        5               /* target maximum seconds in queue */ | 
| 24 | > | #define MAXQTIME        3               /* target maximum seconds in queue */ | 
| 25 |  | #endif | 
| 26 |  | /* manual cache flushing frequency */ | 
| 27 |  | #ifndef RTFLUSH | 
| 55 |  | off_t   maxdisk;                /* maximum file space (bytes) */ | 
| 56 |  |  | 
| 57 |  | int     rtargc = 1;             /* rtrace command */ | 
| 58 | < | char    *rtargv[128] = {"rtrace", NULL}; | 
| 58 | > | char    *rtargv[MAXRTARGC] = {"rtrace", NULL}; | 
| 59 |  |  | 
| 60 |  | int     orig_mode = -1;         /* original file mode (-1 if unchanged) */ | 
| 61 |  |  | 
| 69 |  |  | 
| 70 |  | extern int      nowarn;         /* turn warnings off? */ | 
| 71 |  |  | 
| 72 | < | extern time_t   time(); | 
| 72 | > | static void onsig(int  signo); | 
| 73 | > | static void sigdie(int  signo, char  *msg); | 
| 74 | > | static int resfmode(int fd, int mod); | 
| 75 | > | static void initrholo(void); | 
| 76 | > | static int rholo(void); | 
| 77 | > | static void setdefaults(HDGRID  *gp); | 
| 78 | > | static void creatholo(HDGRID    *gp); | 
| 79 | > | static gethfunc headline; | 
| 80 | > | static void loadholo(void); | 
| 81 | > | static void rootname(char       *rn, char       *fn); | 
| 82 | > | static void badvalue(int        vc); | 
| 83 |  |  | 
| 84 |  |  | 
| 85 | < | main(argc, argv) | 
| 86 | < | int     argc; | 
| 87 | < | char    *argv[]; | 
| 85 | > | int | 
| 86 | > | main( | 
| 87 | > | int     argc, | 
| 88 | > | char    *argv[] | 
| 89 | > | ) | 
| 90 |  | { | 
| 91 |  | int     i; | 
| 92 |  |  | 
| 77 | – | initurand(16384);                       /* initialize urand */ | 
| 93 |  | progname = argv[0];                     /* get arguments */ | 
| 94 |  | for (i = 1; i < argc && argv[i][0] == '-'; i++) | 
| 95 |  | switch (argv[i][1]) { | 
| 164 |  | quit(0); | 
| 165 |  | userr: | 
| 166 |  | fprintf(stderr, | 
| 167 | < | "Usage: %s [-n nprocs][-o disp][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", | 
| 167 | > | "Usage: %s [-n nprocs][-o disp][-i][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", | 
| 168 |  | progname); | 
| 169 |  | quit(1); | 
| 170 | + | return 1; /* pro forma return */ | 
| 171 |  | } | 
| 172 |  |  | 
| 173 |  |  | 
| 174 | < | void | 
| 175 | < | onsig(signo)                            /* fatal signal */ | 
| 176 | < | int  signo; | 
| 174 | > | static void | 
| 175 | > | onsig(                          /* fatal signal */ | 
| 176 | > | int  signo | 
| 177 | > | ) | 
| 178 |  | { | 
| 179 |  | static int  gotsig = 0; | 
| 180 |  |  | 
| 184 |  | hdsync(NULL, 0);        /* don't leave w/o saying goodbye */ | 
| 185 |  | _exit(signo); | 
| 186 |  | } | 
| 187 | < | alarm(300);                     /* allow 5 minutes to clean up */ | 
| 187 | > | alarm(300);                     /* allow 10 minutes to clean up */ | 
| 188 |  | eputs("signal - "); | 
| 189 |  | eputs(sigerr[signo]); | 
| 190 |  | eputs("\n"); | 
| 192 |  | } | 
| 193 |  |  | 
| 194 |  |  | 
| 195 | < | sigdie(signo, msg)                      /* set fatal signal */ | 
| 196 | < | int  signo; | 
| 197 | < | char  *msg; | 
| 195 | > | static void | 
| 196 | > | sigdie(                 /* set fatal signal */ | 
| 197 | > | int  signo, | 
| 198 | > | char  *msg | 
| 199 | > | ) | 
| 200 |  | { | 
| 201 |  | if (signal(signo, onsig) == SIG_IGN) | 
| 202 |  | signal(signo, SIG_IGN); | 
| 204 |  | } | 
| 205 |  |  | 
| 206 |  |  | 
| 207 | < | int | 
| 208 | < | resfmode(fd, mod)               /* restrict open file access mode */ | 
| 209 | < | int     fd, mod; | 
| 207 | > | static int | 
| 208 | > | resfmode(               /* restrict open file access mode */ | 
| 209 | > | int     fd, | 
| 210 | > | int     mod | 
| 211 | > | ) | 
| 212 |  | { | 
| 213 |  | struct stat     stbuf; | 
| 214 |  | /* get original mode */ | 
| 226 |  | } | 
| 227 |  |  | 
| 228 |  |  | 
| 229 | < | initrholo()                     /* get our holodeck running */ | 
| 229 | > | static void | 
| 230 | > | initrholo(void)                 /* get our holodeck running */ | 
| 231 |  | { | 
| 232 |  | extern int      global_packet(); | 
| 233 | < | register int    i; | 
| 233 | > | int     i; | 
| 234 |  | /* close holodeck on exec() */ | 
| 235 |  | fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC); | 
| 236 |  |  | 
| 250 |  | hdcachesize = 1024.*1024.*vflt(CACHE); | 
| 251 |  | /* open report file */ | 
| 252 |  | if (vdef(REPORT)) { | 
| 253 | < | register char   *s = sskip2(vval(REPORT), 1); | 
| 253 | > | char    *s = sskip2(vval(REPORT), 1); | 
| 254 |  | if (*s && freopen(s, "a", stderr) == NULL) | 
| 255 |  | quit(2); | 
| 256 |  | } | 
| 292 |  | } | 
| 293 |  | /* set up signal handling */ | 
| 294 |  | sigdie(SIGINT, "Interrupt"); | 
| 273 | – | sigdie(SIGHUP, "Hangup"); | 
| 295 |  | sigdie(SIGTERM, "Terminate"); | 
| 296 | + | #ifdef SIGHUP | 
| 297 | + | sigdie(SIGHUP, "Hangup"); | 
| 298 | + | #endif | 
| 299 | + | #ifdef SIGPIPE | 
| 300 |  | sigdie(SIGPIPE, "Broken pipe"); | 
| 301 | + | #endif | 
| 302 | + | #ifdef SIGALRM | 
| 303 |  | sigdie(SIGALRM, "Alarm clock"); | 
| 304 | + | #endif | 
| 305 |  | #ifdef  SIGXCPU | 
| 306 |  | sigdie(SIGXCPU, "CPU limit exceeded"); | 
| 307 | + | #endif | 
| 308 | + | #ifdef  SIGXFSZ | 
| 309 |  | sigdie(SIGXFSZ, "File size exceeded"); | 
| 310 |  | #endif | 
| 311 |  | /* protect holodeck file */ | 
| 316 |  | } | 
| 317 |  |  | 
| 318 |  |  | 
| 319 | < | rholo()                         /* holodeck main loop */ | 
| 319 | > | static int | 
| 320 | > | rholo(void)                             /* holodeck main loop */ | 
| 321 |  | { | 
| 322 | < | static long     nextfragwarn = 100*(1L<<20); | 
| 322 | > | static off_t    nextfragwarn = 100L<<20; | 
| 323 |  | static int      idle = 0; | 
| 324 |  | PACKET  *pl = NULL, *plend; | 
| 325 |  | off_t   fsiz; | 
| 326 |  | int     pksiz; | 
| 327 | < | register PACKET *p; | 
| 327 | > | PACKET  *p; | 
| 328 |  | time_t  t; | 
| 329 |  | /* check display */ | 
| 330 |  | if (nprocs <= 0) | 
| 338 |  | return(0);              /* all done */ | 
| 339 |  | fsiz = hdfilen(hdlist[0]->fd);  /* check file size */ | 
| 340 |  | if (maxdisk > 0 && fsiz >= maxdisk) { | 
| 341 | < | error(WARNING, "file limit exceeded"); | 
| 341 | > | error(USER, "file limit exceeded"); | 
| 342 |  | done_rtrace(); | 
| 343 |  | return(1);      /* comes back */ | 
| 344 |  | } | 
| 345 |  | #if FRAGWARN | 
| 346 | < | if (fsiz >= nextfragwarn && | 
| 347 | < | (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) { | 
| 348 | < | sprintf(errmsg, "holodeck file fragmentation is %.0f%%", | 
| 349 | < | 100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz); | 
| 350 | < | error(WARNING, errmsg); | 
| 351 | < | nextfragwarn = fsiz + (fsiz>>2);        /* decent interval */ | 
| 346 | > | if (fsiz >= nextfragwarn) { | 
| 347 | > | double  pctfrag = 100.*(fsiz-hdfiluse(hdlist[0]->fd))/fsiz; | 
| 348 | > | if (pctfrag >= (double)FRAGWARN) { | 
| 349 | > | sprintf(errmsg, "holodeck file fragmentation is %.0f%%", | 
| 350 | > | pctfrag); | 
| 351 | > | error(WARNING, errmsg); | 
| 352 | > | nextfragwarn = fsiz + (fsiz>>2); | 
| 353 | > | } else | 
| 354 | > | nextfragwarn = fsiz + (10L<<20); | 
| 355 |  | } | 
| 356 |  | #endif | 
| 357 |  | t = time(NULL);                 /* check time */ | 
| 358 |  | if (endtime > 0 && t >= endtime) { | 
| 359 | < | error(WARNING, "time limit exceeded"); | 
| 359 | > | error(USER, "time limit exceeded"); | 
| 360 |  | done_rtrace(); | 
| 361 |  | return(1);      /* comes back */ | 
| 362 |  | } | 
| 389 |  | } | 
| 390 |  |  | 
| 391 |  |  | 
| 392 | < | setdefaults(gp)                 /* set default values */ | 
| 393 | < | register HDGRID *gp; | 
| 392 | > | static void | 
| 393 | > | setdefaults(                    /* set default values */ | 
| 394 | > | HDGRID  *gp | 
| 395 | > | ) | 
| 396 |  | { | 
| 397 |  | extern char     *atos(); | 
| 398 | < | register int    i; | 
| 398 | > | int     i; | 
| 399 |  | int     n; | 
| 400 |  | double  len[3], d; | 
| 365 | – | char    buf[64]; | 
| 401 |  |  | 
| 402 |  | if (!vdef(SECTION)) { | 
| 403 |  | sprintf(errmsg, "%s must be defined", vnam(SECTION)); | 
| 415 |  | } | 
| 416 |  | /* append rendering options */ | 
| 417 |  | if (vdef(RENDER)) | 
| 418 | < | rtargc += wordstring(rtargv+rtargc, vval(RENDER)); | 
| 418 | > | rtargc += wordstring(rtargv+rtargc, MAXRTARGC-rtargc, vval(RENDER)); | 
| 419 |  |  | 
| 420 |  | if (gp == NULL)         /* already initialized? */ | 
| 421 |  | return; | 
| 447 |  | } | 
| 448 |  |  | 
| 449 |  |  | 
| 450 | < | creatholo(gp)                   /* create a holodeck output file */ | 
| 451 | < | HDGRID  *gp; | 
| 450 | > | static void | 
| 451 | > | creatholo(                      /* create a holodeck output file */ | 
| 452 | > | HDGRID  *gp | 
| 453 | > | ) | 
| 454 |  | { | 
| 455 |  | extern char     VersionID[]; | 
| 456 | < | int32   lastloc, nextloc; | 
| 456 | > | off_t   lastloc, nextloc; | 
| 457 |  | int     n; | 
| 458 |  | int     fd; | 
| 459 |  | FILE    *fp; | 
| 460 |  | /* open & truncate file */ | 
| 461 | < | if ((fp = fopen(hdkfile, "w+")) == NULL) { | 
| 461 | > | if ((fp = fopen(hdkfile, "wb+")) == NULL) { | 
| 462 |  | sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile); | 
| 463 |  | error(SYSTEM, errmsg); | 
| 464 |  | } | 
| 479 |  | if (!n) | 
| 480 |  | break; | 
| 481 |  | nextloc = hdfilen(fd);          /* write section pointer */ | 
| 482 | < | if (lseek(fd, (off_t)lastloc, SEEK_SET) < 0) | 
| 482 | > | if (lseek(fd, lastloc, SEEK_SET) < 0) | 
| 483 |  | error(SYSTEM, | 
| 484 |  | "cannot seek on holodeck file in creatholo"); | 
| 485 |  | write(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 486 | < | lseek(fd, (off_t)(lastloc=nextloc), SEEK_SET); | 
| 486 | > | lseek(fd, (lastloc=nextloc), SEEK_SET); | 
| 487 |  | } | 
| 488 |  | } | 
| 489 |  |  | 
| 490 |  |  | 
| 491 | < | int | 
| 492 | < | headline(s)                     /* process information header line */ | 
| 493 | < | char    *s; | 
| 491 | > | static int | 
| 492 | > | headline(                       /* process information header line */ | 
| 493 | > | char    *s, | 
| 494 | > | void    *p | 
| 495 | > | ) | 
| 496 |  | { | 
| 497 |  | extern char     FMTSTR[]; | 
| 498 | < | register char   *cp; | 
| 499 | < | char    fmt[32]; | 
| 498 | > | char    *cp; | 
| 499 | > | char    fmt[MAXFMTLEN]; | 
| 500 |  |  | 
| 501 |  | if (formatval(fmt, s)) { | 
| 502 |  | if (strcmp(fmt, HOLOFMT)) { | 
| 516 |  | } | 
| 517 |  |  | 
| 518 |  |  | 
| 519 | < | loadholo()                      /* start loading a holodeck from fname */ | 
| 519 | > | static void | 
| 520 | > | loadholo(void)                  /* start loading a holodeck from fname */ | 
| 521 |  | { | 
| 522 |  | FILE    *fp; | 
| 523 |  | int     fd; | 
| 524 |  | int     n; | 
| 525 | < | int32   nextloc; | 
| 525 | > | off_t   nextloc; | 
| 526 |  |  | 
| 527 |  | if ((ncprocs > 0) & (force >= 0)) | 
| 528 | < | fp = fopen(hdkfile, "r+"); | 
| 528 | > | fp = fopen(hdkfile, "rb+"); | 
| 529 |  | else | 
| 530 |  | fp = NULL; | 
| 531 |  | if (fp == NULL) { | 
| 532 | < | if ((fp = fopen(hdkfile, "r")) == NULL) { | 
| 532 | > | if ((fp = fopen(hdkfile, "rb")) == NULL) { | 
| 533 |  | sprintf(errmsg, "cannot open \"%s\"", hdkfile); | 
| 534 |  | error(SYSTEM, errmsg); | 
| 535 |  | } | 
| 536 |  | if (ncprocs > 0) { | 
| 537 | < | sprintf(errmsg, | 
| 538 | < | "\"%s\" opened read-only; new rays will be discarded", | 
| 539 | < | hdkfile); | 
| 537 | > | sprintf(errmsg, "\"%s\" is read-only", hdkfile); | 
| 538 | > | if (outdev == NULL) | 
| 539 | > | error(USER, errmsg); | 
| 540 | > | strcat(errmsg, "; new rays will be discarded"); | 
| 541 |  | error(WARNING, errmsg); | 
| 542 |  | force = -1; | 
| 543 |  | } | 
| 554 |  | fd = dup(fileno(fp)); | 
| 555 |  | fclose(fp);                             /* done with stdio */ | 
| 556 |  | for (n = 0; nextloc > 0L; n++) {        /* initialize each section */ | 
| 557 | < | lseek(fd, (off_t)nextloc, SEEK_SET); | 
| 557 | > | lseek(fd, nextloc, SEEK_SET); | 
| 558 |  | read(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 559 |  | hdinit(fd, NULL); | 
| 560 |  | } | 
| 566 |  | } | 
| 567 |  |  | 
| 568 |  |  | 
| 569 | < | done_packets(pl)                /* handle finished packets */ | 
| 570 | < | PACKET  *pl; | 
| 569 | > | void | 
| 570 | > | done_packets(           /* handle finished packets */ | 
| 571 | > | PACKET  *pl | 
| 572 | > | ) | 
| 573 |  | { | 
| 574 |  | static int      n2flush = 0; | 
| 575 | < | register PACKET *p; | 
| 575 | > | PACKET  *p; | 
| 576 |  |  | 
| 577 |  | while (pl != NULL) { | 
| 578 |  | p = pl; pl = p->next; p->next = NULL; | 
| 601 |  | } | 
| 602 |  |  | 
| 603 |  |  | 
| 604 | < | rootname(rn, fn)                /* remove tail from end of fn */ | 
| 605 | < | register char   *rn, *fn; | 
| 604 | > | static void | 
| 605 | > | rootname(               /* remove tail from end of fn */ | 
| 606 | > | char    *rn, | 
| 607 | > | char    *fn | 
| 608 | > | ) | 
| 609 |  | { | 
| 610 |  | char    *tp, *dp; | 
| 611 |  |  | 
| 620 |  | } | 
| 621 |  |  | 
| 622 |  |  | 
| 623 | < | badvalue(vc)                    /* report bad variable value and exit */ | 
| 624 | < | int     vc; | 
| 623 | > | static void | 
| 624 | > | badvalue(                       /* report bad variable value and exit */ | 
| 625 | > | int     vc | 
| 626 | > | ) | 
| 627 |  | { | 
| 628 |  | sprintf(errmsg, "bad value for variable '%s'", vnam(vc)); | 
| 629 |  | error(USER, errmsg); | 
| 632 |  |  | 
| 633 |  | void | 
| 634 |  | eputs(s)                        /* put error message to stderr */ | 
| 635 | < | register char  *s; | 
| 635 | > | char  *s; | 
| 636 |  | { | 
| 637 |  | static int  midline = 0; | 
| 638 |  |  | 
| 662 |  | if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) { | 
| 663 |  | off_t   fsiz, fuse; | 
| 664 |  | fsiz = hdfilen(hdlist[0]->fd); | 
| 665 | < | fuse = hdfiluse(hdlist[0]->fd, 1); | 
| 665 | > | fuse = hdfiluse(hdlist[0]->fd); | 
| 666 |  | fprintf(stderr, | 
| 667 |  | "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", | 
| 668 |  | hdkfile, fsiz/(1024.*1024.), |