--- ray/src/rt/persist.c 1996/07/08 19:46:21 2.16 +++ ray/src/rt/persist.c 1997/11/11 20:29:25 2.23 @@ -12,23 +12,9 @@ static char SCCSid[] = "$SunId$ LBL"; #ifdef F_SETLKW #include "paths.h" +#include "selcall.h" #include -#include #include - /* select call compatibility stuff */ -#ifndef FD_SETSIZE -#include -#define FD_SETSIZE NOFILE /* maximum # select file descriptors */ -#endif -#ifndef FD_SET -#ifndef NFDBITS -#define NFDBITS (8*sizeof(int)) /* number of bits per fd_mask */ -#endif -#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) -#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) -#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) -#define FD_ZERO(p) bzero((char *)(p), sizeof(*(p))) -#endif #ifndef TIMELIM #define TIMELIM (8*3600) /* time limit for holding pattern */ @@ -140,6 +126,7 @@ pfhold() /* holding pattern for idle rendering proces n = strlen(buf); if (write(persistfd, buf, n) < n) error(SYSTEM, "error writing persist file"); + lseek(persistfd, 0L, 0); /* wait TIMELIM for someone to signal us */ got_io = 0; signal(SIGIO, sig_io); @@ -148,17 +135,16 @@ pfhold() /* holding pattern for idle rendering proces pflock(0); /* unlock persist file for attach */ while (!got_io) pause(); /* wait for attach */ - pflock(1); /* grab persist file right back! */ alarm(0); /* turn off alarm */ signal(SIGALRM, oldalrm); signal(SIGIO, SIG_DFL); - if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0) - error(SYSTEM, "seek/truncate error on persist file"); + pflock(1); /* grab persist file back */ /* someone wants us; reopen stdin and stdout */ if (freopen(inpname, "r", stdin) == NULL) goto openerr; if (freopen(outpname, "w", stdout) == NULL) goto openerr; + sleep(3); /* give them a chance to open their pipes */ if (errname[0]) { if (freopen(errname, "w", stderr) == NULL) goto openerr; @@ -183,18 +169,23 @@ io_process() /* just act as go-between for actual pro register int nr, n; char buf[BUFSIZ], *pfin, *pfout, *pferr; int pid, nfds; - int fdin, fdout, fderr = -1; - fd_set readfds, writefds, excepfds; + int fdout, fderr = -1; + int status = 0; + fd_set readfds, excepfds; /* load persist file */ + n = 40; while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { + if (!n--) + error(USER, "unattended persist file?"); pflock(0); sleep(15); /* wait until ready */ pflock(1); } - pfdetach(); /* close persist file */ if (nr < 0) error(SYSTEM, "error reading persist file"); - buf[nr] = '\0'; + ftruncate(persistfd, 0L); /* truncate persist file */ + pfdetach(); /* close & release persist file */ + buf[nr] = '\0'; /* parse what we got */ if ((cp = index(buf, ' ')) == NULL) goto formerr; *cp++ = '\0'; @@ -223,23 +214,37 @@ io_process() /* just act as go-between for actual pro /* wake up rendering process */ if (kill(pid, SIGIO) < 0) error(SYSTEM, "cannot signal rendering process in io_process"); - /* open named pipes, in order */ - if ((fdin = open(pfin, O_WRONLY)) < 0) - error(SYSTEM, "cannot open input pipe in io_process"); + /* fork child feeder process */ + pid = fork(); + if (pid < 0) + error(SYSTEM, "fork failed in io_process"); + if (pid == 0) { /* feeder loop */ + int fdin; + close(1); /* open input pipe */ + if ((fdin = open(pfin, O_WRONLY)) < 0) + error(SYSTEM, "cannot open feed pipe in io_process"); + /* renderer stdin */ + while ((nr = read(0, cp=buf, sizeof(buf))) > 0) { + do { + if ((n = write(fdin, cp, nr)) <= 0) + goto writerr; + cp += n; + } while ((nr -= n) > 0); + } + if (nr < 0) + goto readerr; + _exit(0); + } + close(0); + /* open output pipes, in order */ if ((fdout = open(pfout, O_RDONLY)) < 0) error(SYSTEM, "cannot open output pipe in io_process"); if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0) error(SYSTEM, "cannot open error pipe in io_process"); - for ( ; ; ) { /* loop on select call */ + for ( ; ; ) { /* eater loop */ FD_ZERO(&readfds); - FD_ZERO(&writefds); FD_ZERO(&excepfds); nfds = 0; - if (fdin >= 0) { - FD_SET(fdin, &writefds); - FD_SET(fdin, &excepfds); - nfds = fdin+1; - } if (fdout >= 0) { FD_SET(fdout, &readfds); FD_SET(fdout, &excepfds); @@ -252,7 +257,7 @@ io_process() /* just act as go-between for actual pro } if (nfds == 0) break; /* all done, exit */ - if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0) + if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0) error(SYSTEM, "error in select call in io_process"); /* renderer stderr */ if (fderr >= 0 && (FD_ISSET(fderr, &readfds) || @@ -262,10 +267,25 @@ io_process() /* just act as go-between for actual pro goto readerr; if (nr == 0) { close(fderr); - close(2); + /* close(2); don't close stderr! */ fderr = -1; } else - do { /* write it all */ + cp[nr] = '\0'; /* deduce status if we can */ + n = strlen(progname); + if (!strncmp(cp, progname, n) && + cp[n++] == ':' && + cp[n++] == ' ') { + register struct erract *ep; + for (ep = erract; ep < erract+NERRS; + ep++) + if (ep->pre[0] && + !strncmp(cp+n, ep->pre, + strlen(ep->pre))) { + status = ep->ec; + break; + } + } + do { /* write message */ if ((n = write(2, cp, nr)) <= 0) goto writerr; cp += n; @@ -288,21 +308,9 @@ io_process() /* just act as go-between for actual pro cp += n; } while ((nr -= n) > 0); } - /* renderer stdin */ - if (fdin >= 0 && (FD_ISSET(fdin, &writefds) || - FD_ISSET(fdin, &excepfds))) { - nr = read(0, buf, 512); /* use minimal buffer */ - if (nr < 0) - goto readerr; - if (nr == 0) { - close(0); - close(fdin); - fdin = -1; - } else if (write(fdin, buf, nr) < nr) - goto writerr; /* what else to do? */ - } } - _exit(0); /* we ought to return renderer error status! */ + wait(0); /* wait for feeder process */ + _exit(status); formerr: error(USER, "format error in persist file"); readerr: