--- ray/src/hd/rholo.c 1997/11/03 18:33:12 3.4 +++ ray/src/hd/rholo.c 1997/11/20 11:39:24 3.12 @@ -10,7 +10,9 @@ static char SCCSid[] = "$SunId$ SGI"; #include "rholo.h" #include "paths.h" +#include #include +#include /* the following must be consistent with rholo.h */ int NVARS = NRHVARS; /* total number of variables */ @@ -33,14 +35,20 @@ time_t starttime; /* time we got started */ time_t endtime; /* time we should end by */ time_t reporttime; /* time for next report */ +long maxdisk; /* maximum file space (bytes) */ + int rtargc = 1; /* rtrace command */ char *rtargv[128] = {"rtrace", NULL}; +int orig_mode = -1; /* original file mode (-1 if unchanged) */ + long nraysdone = 0L; /* number of rays done */ long npacksdone = 0L; /* number of packets done */ PACKET *freepacks; /* available packets */ +char *sigerr[NSIG]; /* signal error messages */ + extern time_t time(); @@ -123,6 +131,53 @@ userr: } +onsig(signo) /* fatal signal */ +int signo; +{ + static int gotsig = 0; + + if (gotsig++) /* two signals and we're gone! */ + _exit(signo); + + alarm(30); /* allow 30 seconds to clean up */ + signal(SIGALRM, SIG_DFL); /* make certain we do die */ + eputs("signal - "); + eputs(sigerr[signo]); + eputs("\n"); + quit(3); +} + + +sigdie(signo, msg) /* set fatal signal */ +int signo; +char *msg; +{ + if (signal(signo, onsig) == SIG_IGN) + signal(signo, SIG_IGN); + sigerr[signo] = msg; +} + + +int +resfmode(fd, mod) /* restrict open file access mode */ +int fd, mod; +{ + struct stat stbuf; + /* get original mode */ + if (fstat(fd, &stbuf) < 0) + error(SYSTEM, "cannot stat open holodeck file"); + mod &= stbuf.st_mode; /* always more restrictive */ + if (mod == stbuf.st_mode) + return(-1); /* already set */ + /* else change it */ + if (fchmod(fd, mod) < 0) { + error(WARNING, "cannot change holodeck file access mode"); + return(-1); + } + return(stbuf.st_mode); /* return original mode */ +} + + initrholo() /* get our holodeck running */ { extern int global_packet(); @@ -132,13 +187,21 @@ initrholo() /* get our holodeck running */ disp_open(outdev); else if (ncprocs > 0) /* else use global ray feed */ init_global(); + /* record disk space limit */ + if (!vdef(DISKSPACE)) + maxdisk = 0; + else + maxdisk = 1024.*1024.*vflt(DISKSPACE); /* record end time */ if (!vdef(TIME) || vflt(TIME) <= FTINY) endtime = 0; else endtime = starttime + vflt(TIME)*3600.; /* set up memory cache */ - hdcachesize = 1024.*1024.*vflt(CACHE); + if (outdev == NULL) + hdcachesize = 0; /* manual flushing */ + else if (vdef(CACHE)) + hdcachesize = 1024.*1024.*vflt(CACHE); /* open report file */ if (vdef(REPORT)) { register char *s = sskip2(vval(REPORT), 1); @@ -174,6 +237,18 @@ initrholo() /* get our holodeck running */ freepacks[i].next = &freepacks[i+1]; } } + /* set up signal handling */ + sigdie(SIGINT, "Interrupt"); + sigdie(SIGHUP, "Hangup"); + sigdie(SIGTERM, "Terminate"); + sigdie(SIGPIPE, "Broken pipe"); + sigdie(SIGALRM, "Alarm clock"); +#ifdef SIGXCPU + sigdie(SIGXCPU, "CPU limit exceeded"); + sigdie(SIGXFSZ, "File size exceeded"); +#endif + /* protect holodeck file */ + orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444); return; memerr: error(SYSTEM, "out of memory in initrholo"); @@ -195,14 +270,17 @@ rholo() /* holodeck main loop */ if (ncprocs <= 0) return(1); /* check file size */ - if ((l = 1024.*1024.*vflt(DISKSPACE)) > 0 && - hdfiluse(hdlist[0]->fd, 0) + hdmemuse(0) >= l) + if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) { + error(WARNING, "file limit exceeded"); return(0); + } /* check time */ if (endtime > 0 || reporttime > 0) t = time(NULL); - if (endtime > 0 && t >= endtime) + if (endtime > 0 && t >= endtime) { + error(WARNING, "time limit exceeded"); return(0); + } if (reporttime > 0 && t >= reporttime) report(t); /* get packets to process */ @@ -287,22 +365,6 @@ register HDGRID *gp; sprintf(vval(OCTREE), "%s.oct", froot); vdef(OCTREE)++; } - if (!vdef(DISKSPACE)) { - sprintf(errmsg, - "no %s setting, assuming 100 Mbytes available", - vnam(DISKSPACE)); - error(WARNING, errmsg); - vval(DISKSPACE) = "100"; - vdef(DISKSPACE)++; - } - if (!vdef(CACHE)) { - sprintf(errmsg, - "no %s setting, assuming 10 Mbytes available", - vnam(CACHE)); - error(WARNING, errmsg); - vval(CACHE) = "10"; - vdef(CACHE)++; - } if (!vdef(OBSTRUCTIONS)) { vval(OBSTRUCTIONS) = "T"; vdef(OBSTRUCTIONS)++; @@ -320,7 +382,9 @@ register HDGRID *gp; creatholo(gp) /* create a holodeck output file */ HDGRID *gp; { + extern char VersionID[]; long endloc = 0; + int fd; FILE *fp; /* open & truncate file */ if ((fp = fopen(hdkfile, "w+")) == NULL) { @@ -329,14 +393,15 @@ HDGRID *gp; } /* write information header */ newheader("RADIANCE", fp); + fprintf(fp, "SOFTWARE= %s\n", VersionID); printvars(fp); fputformat(HOLOFMT, fp); fputc('\n', fp); putw(HOLOMAGIC, fp); /* put magic number & terminus */ fwrite(&endloc, sizeof(long), 1, fp); - fflush(fp); /* flush buffer */ - hdinit(fileno(fp), gp); /* allocate and initialize index */ - /* we're dropping fp here.... */ + fd = dup(fileno(fp)); + fclose(fp); /* flush and close stdio stream */ + hdinit(fd, gp); /* allocate and initialize index */ } @@ -366,8 +431,10 @@ char *s; loadholo() /* start loading a holodeck from fname */ { + extern long ftell(); FILE *fp; - long endloc; + int fd; + long fpos; /* open holodeck file */ if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile, @@ -382,18 +449,21 @@ loadholo() /* start loading a holodeck from fname */ hdkfile); error(USER, errmsg); } - fread(&endloc, sizeof(long), 1, fp); - if (endloc != 0) + fread(&fpos, sizeof(long), 1, fp); + if (fpos != 0) error(WARNING, "ignoring multiple sections in holodeck file"); - fseek(fp, 0L, 1); /* align system file pointer */ - hdinit(fileno(fp), NULL); /* allocate and load index */ - /* we're dropping fp here.... */ + fpos = ftell(fp); /* get stdio position */ + fd = dup(fileno(fp)); + fclose(fp); /* done with stdio */ + lseek(fd, fpos, 0); /* align system file pointer */ + hdinit(fd, NULL); /* allocate and load index */ } done_packets(pl) /* handle finished packets */ PACKET *pl; { + static int nunflushed = 0; register PACKET *p; while (pl != NULL) { @@ -403,21 +473,27 @@ PACKET *pl; (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr), p->nr*sizeof(RAYVAL)); if (outdev != NULL) /* display it */ - disp_packet(p); + disp_packet((PACKHEAD *)p); + else + nunflushed += p->nr; + nraysdone += p->nr; + npacksdone++; } - nraysdone += p->nr; - npacksdone++; p->nr = 0; /* push onto free list */ p->next = freepacks; freepacks = p; } + if (nunflushed >= 256*RPACKSIZ) { + hdflush(NULL); /* flush holodeck buffers */ + nunflushed = 0; + } } getradfile(rfargs) /* run rad and get needed variables */ char *rfargs; { - static short mvar[] = {VIEW,OCTREE,EXPOSURE,-1}; + static short mvar[] = {OCTREE,EXPOSURE,-1}; static char tf1[] = TEMPLATE; char tf2[64]; char combuf[256]; @@ -516,8 +592,6 @@ int ec; { int status = 0; - if (outdev != NULL) /* close display */ - disp_close(); if (hdlist[0] != NULL) { /* flush holodeck */ if (ncprocs > 0) { done_packets(flush_queue()); @@ -526,7 +600,7 @@ int ec; if (vdef(REPORT)) { long fsiz, fuse; report(0); - fsiz = lseek(hdlist[0]->fd, 0L, 2); + fsiz = hdfilen(hdlist[0]->fd); fuse = hdfiluse(hdlist[0]->fd, 1); fprintf(stderr, "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", @@ -536,5 +610,9 @@ int ec; } else hdflush(NULL); } + if (orig_mode >= 0) /* reset holodeck access mode */ + fchmod(hdlist[0]->fd, orig_mode); + if (outdev != NULL) /* close display */ + disp_close(); exit(ec ? ec : status); /* exit */ }