--- ray/src/rt/persist.c 1993/01/25 19:17:36 2.5 +++ ray/src/rt/persist.c 1998/01/03 20:07:31 2.25 @@ -1,7 +1,7 @@ -/* Copyright (c) 1993 Regents of the University of California */ +/* Copyright (c) 1997 Silicon Graphics, Inc. */ #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static char SCCSid[] = "$SunId$ SGI"; #endif /* @@ -9,9 +9,12 @@ static char SCCSid[] = "$SunId$ LBL"; */ #include "standard.h" +#include "random.h" + +#ifdef F_SETLKW #include "paths.h" +#include "selcall.h" #include -#include #include #ifndef TIMELIM @@ -22,10 +25,14 @@ extern char *strcpy(), *index(); extern int headismine; /* boolean true if header belongs to me */ +extern char *progname; /* global program name */ + +extern char *errfile; /* global error file name */ + static char *persistfname = NULL; /* persist file name */ static int persistfd = -1; /* persist file descriptor */ -static char inpname[TEMPLEN+1], outpname[TEMPLEN+1]; +static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; pfdetach() /* release persist (and header) resources */ @@ -36,6 +43,7 @@ pfdetach() /* release persist (and header) resources persistfname = NULL; inpname[0] = '\0'; outpname[0] = '\0'; + errname[0] = '\0'; headismine = 0; } @@ -50,6 +58,8 @@ pfclean() /* clean up persist files */ unlink(inpname); if (outpname[0]) unlink(outpname); + if (errname[0]) + unlink(errname); } @@ -70,7 +80,7 @@ int lf; persistfile(pfn) /* open persist file and lock it */ char *pfn; { - persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666); + persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); if (persistfd >= 0) { persistfname = pfn; pflock(1); @@ -87,40 +97,61 @@ char *pfn; } -int sig_noop() {} +static int got_io; +static int sig_io() { got_io++; } +static int sig_alrm() { quit(0); } + + pfhold() /* holding pattern for idle rendering process */ { - char buf[128]; + int (*oldalrm)(); + char buf[512]; register int n; /* close input and output descriptors */ close(fileno(stdin)); close(fileno(stdout)); + if (errfile == NULL) + close(fileno(stderr)); /* create named pipes for input and output */ - if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600) < 0) + if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) goto createrr; - if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0) + if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) goto createrr; - sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname); - if (lseek(persistfd, 0L, 0) < 0) - error(SYSTEM, "seek error on persist file in pfhold"); + if (errfile == NULL && + mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) + goto createrr; + sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), + inpname, outpname, errname); n = strlen(buf); if (write(persistfd, buf, n) < n) - error(SYSTEM, "error writing persist file in pfhold"); + error(SYSTEM, "error writing persist file"); + lseek(persistfd, 0L, 0); /* wait TIMELIM for someone to signal us */ - signal(SIGIO, sig_noop); + got_io = 0; + signal(SIGIO, sig_io); + oldalrm = (int (*)())signal(SIGALRM, sig_alrm); alarm(TIMELIM); - pflock(0); - pause(); - alarm(0); + pflock(0); /* unlock persist file for attach */ + while (!got_io) + pause(); /* wait for attach */ + alarm(0); /* turn off alarm */ + signal(SIGALRM, oldalrm); signal(SIGIO, SIG_DFL); - pflock(1); + 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; + unlink(errname); + errname[0] = '\0'; + } unlink(inpname); inpname[0] = '\0'; unlink(outpname); @@ -133,63 +164,165 @@ openerr: } -io_process() /* just act as conduits to and from actual process */ +io_process() /* just act as go-between for actual process */ { register char *cp; register int nr, n; - char buf[4096], *pfin, *pfout; - int pid; - /* load and close persist file */ - nr = read(persistfd, buf, sizeof(buf)); - pfdetach(); + char buf[BUFSIZ], *pfin, *pfout, *pferr; + int pid, nfds; + 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(3+(3*getpid()+random())%13); /* wait until ready */ + pflock(1); + } if (nr < 0) - error(SYSTEM, "cannot read persist file"); - buf[nr] = '\0'; - if ((cp = index(buf, '\n')) == NULL) + error(SYSTEM, "error reading persist file"); + 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'; - if ((pid = atoi(buf)) <= 0) + if ((pid = atoi(cp)) <= 0) goto formerr; - pfin = cp; if ((cp = index(cp, '\n')) == NULL) goto formerr; + pfin = ++cp; + if ((cp = index(cp, '\n')) == NULL) + goto formerr; *cp++ = '\0'; pfout = cp; if ((cp = index(cp, '\n')) == NULL) goto formerr; *cp++ = '\0'; + pferr = cp; + if ((cp = index(cp, '\n')) == NULL) + goto formerr; + *cp++ = '\0'; if (cp-buf != nr) goto formerr; - /* wake up rendering process */ + if (strcmp(buf, progname)) { + sprintf(errmsg, "persist file for %s, not %s", buf, progname); + error(USER, errmsg); + } + /* wake up rendering process */ if (kill(pid, SIGIO) < 0) error(SYSTEM, "cannot signal rendering process in io_process"); - pid = fork(); /* fork i/o process */ + /* fork child feeder process */ + pid = fork(); if (pid < 0) error(SYSTEM, "fork failed in io_process"); - /* connect to appropriate pipe */ - if (pid) { /* parent passes renderer output */ - if (freopen(pfout, "r", stdin) == NULL) - error(SYSTEM, "cannot open input pipe in io_process"); - } else { /* child passes renderer input */ - if (freopen(pfin, "w", stdout) == NULL) - error(SYSTEM, "cannot open input pipe 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); } - /* pass input to output */ - cp = buf; n = sizeof(buf); - while ((nr = read(fileno(stdin), cp, n)) > 0) { - nr += cp-buf; - if ((n = write(fileno(stdout), buf, nr)) <= 0) - error(SYSTEM, "write error in io_process"); - cp = buf; - while (n < nr) - *cp++ = buf[n++]; - n = sizeof(buf) - (cp-buf); + 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 ( ; ; ) { /* eater loop */ + FD_ZERO(&readfds); + FD_ZERO(&excepfds); + nfds = 0; + if (fdout >= 0) { + FD_SET(fdout, &readfds); + FD_SET(fdout, &excepfds); + nfds = fdout+1; + } + if (fderr >= 0) { + FD_SET(fderr, &readfds); + FD_SET(fderr, &excepfds); + nfds = fderr+1; + } + if (nfds == 0) + break; /* all done, exit */ + 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) || + FD_ISSET(fderr, &excepfds))) { + nr = read(fderr, cp=buf, sizeof(buf)); + if (nr < 0) + goto readerr; + if (nr == 0) { + close(fderr); + /* close(2); don't close stderr! */ + fderr = -1; + } else { + 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; + } while ((nr -= n) > 0); + } + } + /* renderer stdout */ + if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || + FD_ISSET(fdout, &excepfds))) { + nr = read(fdout, cp=buf, sizeof(buf)); + if (nr < 0) + goto readerr; + if (nr == 0) { /* EOF */ + close(fdout); + close(1); + fdout = -1; + } else + do { /* write it all */ + if ((n = write(1, cp, nr)) <= 0) + goto writerr; + cp += n; + } while ((nr -= n) > 0); + } } - fclose(stdin); - fclose(stdout); - if (pid) /* parent waits for child */ - wait(0); - exit(0); /* all done, exit (not quit!) */ + wait(0); /* wait for feeder process */ + _exit(status); formerr: error(USER, "format error in persist file"); +readerr: + error(SYSTEM, "read error in io_process"); +writerr: + error(SYSTEM, "write error in io_process"); } + +#else + +pfclean() {} + +#endif