--- ray/src/rt/persist.c 1993/01/21 10:09:04 2.2 +++ ray/src/rt/persist.c 1996/07/04 09:54:59 2.13 @@ -1,4 +1,4 @@ -/* Copyright (c) 1993 Regents of the University of California */ +/* Copyright (c) 1996 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -9,6 +9,9 @@ static char SCCSid[] = "$SunId$ LBL"; */ #include "standard.h" + +#ifdef F_SETLKW + #include "paths.h" #include #include @@ -22,6 +25,8 @@ extern char *strcpy(), *index(); extern int headismine; /* boolean true if header belongs to me */ +extern char *progname; /* global program name */ + static char *persistfname = NULL; /* persist file name */ static int persistfd = -1; /* persist file descriptor */ @@ -70,7 +75,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,34 +92,42 @@ 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)); - fflush(stdout); close(fileno(stdout)); /* 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"); + sprintf(buf, "%s %d\n%s\n%s\n", progname, getpid(), inpname, outpname); + if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0) + error(SYSTEM, "seek/truncate error on persist file"); n = strlen(buf); if (write(persistfd, buf, n) < n) - error(SYSTEM, "error writing persist file in pfhold"); + error(SYSTEM, "error writing persist file"); /* 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(); + while (!got_io) + pause(); alarm(0); + signal(SIGALRM, oldalrm); signal(SIGIO, SIG_DFL); pflock(1); /* someone wants us; reopen stdin and stdout */ @@ -138,22 +151,25 @@ io_process() /* just act as conduits to and from actu { register char *cp; register int nr, n; - char buf[4096], *pfin, *pfout; + char buf[512], *pfin, *pfout; int pid; /* load and close persist file */ - nr = read(persistfd, buf, sizeof(buf)); + sleep(5); /* helps synchronization */ + nr = read(persistfd, buf, sizeof(buf)-1); pfdetach(); - if (nr < 0) + if (nr <= 0) error(SYSTEM, "cannot read persist file"); buf[nr] = '\0'; - if ((cp = index(buf, '\n')) == NULL) + 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) @@ -161,6 +177,10 @@ io_process() /* just act as conduits to and from actu *cp++ = '\0'; if (cp-buf != nr) goto formerr; + 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"); @@ -169,28 +189,37 @@ io_process() /* just act as conduits to and from actu error(SYSTEM, "fork failed in io_process"); /* connect to appropriate pipe */ if (pid) { /* parent passes renderer output */ - if (freopen(pfout, "r", stdin) == NULL) + close(0); + if (open(pfout, O_RDONLY) != 0) 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"); + close(1); + if (open(pfin, O_WRONLY) != 1) + error(SYSTEM, "cannot open output pipe in io_process"); } /* 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); - } - fclose(stdin); - fclose(stdout); + /* read as much as we can, write all of it */ + while ((nr = read(0, cp=buf, sizeof(buf))) > 0) + do { + if ((n = write(1, cp, nr)) <= 0) + goto writerr; + cp += n; + } while ((nr -= n) > 0); + if (nr < 0) + error(SYSTEM, "read error in io_process"); + close(0); /* close input */ + close(1); /* close output */ if (pid) /* parent waits for child */ wait(0); exit(0); /* all done, exit (not quit!) */ formerr: error(USER, "format error in persist file"); +writerr: + error(SYSTEM, "write error in io_process"); } + +#else + +pfclean() {} + +#endif