ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
(Generate patch)

Comparing ray/src/hd/rholo.c (file contents):
Revision 3.8 by gregl, Mon Nov 10 18:05:24 1997 UTC vs.
Revision 3.14 by gregl, Mon Dec 1 16:34:21 1997 UTC

# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ SGI";
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 */
# Line 38 | Line 41 | long   maxdisk;                /* maximum file space (bytes) */
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  
# Line 55 | Line 62 | char   *argv[];
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]) {
# Line 125 | Line 133 | userr:
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();
# Line 138 | Line 193 | initrholo()                    /* get our holodeck running */
193          if (!vdef(DISKSPACE))
194                  maxdisk = 0;
195          else
196 <                maxdisk = 1024.*1024.*vflt(DISKSPACE));
196 >                maxdisk = 1024.*1024.*vflt(DISKSPACE);
197                                                  /* record end time */
198          if (!vdef(TIME) || vflt(TIME) <= FTINY)
199                  endtime = 0;
# Line 184 | Line 239 | initrholo()                    /* get our holodeck running */
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");
# Line 205 | Line 272 | rholo()                                /* holodeck main loop */
272          if (ncprocs <= 0)
273                  return(1);
274                                          /* check file size */
275 <        if (maxdisk > 0 && hdfiluse(hdlist[0]->fd,0)+hdmemuse(0) >= maxdisk) {
275 >        if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
276                  error(WARNING, "file limit exceeded");
277                  return(0);
278          }
# Line 304 | Line 371 | register HDGRID        *gp;
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)++;
# Line 317 | Line 388 | register HDGRID        *gp;
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) {
# Line 326 | Line 399 | HDGRID *gp;
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  
# Line 363 | Line 437 | char   *s;
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,
# Line 379 | Line 455 | loadholo()                     /* start loading a holodeck from fname */
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) {
# Line 402 | Line 480 | PACKET *pl;
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                  }
# Line 411 | Line 489 | PACKET *pl;
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  
# Line 421 | Line 499 | PACKET *pl;
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];
# Line 520 | Line 598 | int    ec;
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());
# Line 530 | Line 606 | int    ec;
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",
# Line 540 | Line 616 | int    ec;
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines