--- ray/src/rt/persist.c 1998/01/03 20:07:31 2.25 +++ ray/src/rt/persist.c 2004/09/19 07:24:37 2.36 @@ -1,41 +1,46 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: persist.c,v 2.36 2004/09/19 07:24:37 greg Exp $"; #endif - /* * Routines for persistent rtrace and rpict processes. + * + * External symbols declared in ray.h */ +#include "copyright.h" + +#include +#include +#include + +#include "platform.h" +#include "rtprocess.h" /* getpid() */ #include "standard.h" #include "random.h" +#include "ray.h" #ifdef F_SETLKW #include "paths.h" #include "selcall.h" -#include -#include #ifndef TIMELIM #define TIMELIM (8*3600) /* time limit for holding pattern */ #endif -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], errname[TEMPLEN+1]; +typedef void (sighandler_t)(int); +static sighandler_t sig_io; +static sighandler_t sig_alrm; -pfdetach() /* release persist (and header) resources */ + +extern void +pfdetach(void) /* release persist (and header) resources */ { if (persistfd >= 0) close(persistfd); @@ -48,7 +53,8 @@ pfdetach() /* release persist (and header) resources } -pfclean() /* clean up persist files */ +extern void +pfclean(void) /* clean up persist files */ { if (persistfd >= 0) close(persistfd); @@ -63,8 +69,10 @@ pfclean() /* clean up persist files */ } -pflock(lf) /* place or release exclusive lock on file */ -int lf; +extern void +pflock( /* place or release exclusive lock on file */ + int lf +) { struct flock fls; @@ -77,8 +85,10 @@ int lf; } -persistfile(pfn) /* open persist file and lock it */ -char *pfn; +extern void +persistfile( /* open persist file and lock it */ + char *pfn +) { persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); if (persistfd >= 0) { @@ -99,14 +109,15 @@ char *pfn; static int got_io; -static int sig_io() { got_io++; } +static void sig_io(int i) { got_io++; } -static int sig_alrm() { quit(0); } +static void sig_alrm(int i) { quit(0); } -pfhold() /* holding pattern for idle rendering process */ +extern void +pfhold(void) /* holding pattern for idle rendering process */ { - int (*oldalrm)(); + sighandler_t *oldalrm; char buf[512]; register int n; /* close input and output descriptors */ @@ -115,23 +126,23 @@ pfhold() /* holding pattern for idle rendering proces if (errfile == NULL) close(fileno(stderr)); /* create named pipes for input and output */ - if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) + if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0) goto createrr; - if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) + if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0) goto createrr; if (errfile == NULL && - mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) + mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 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"); - lseek(persistfd, 0L, 0); + lseek(persistfd, (off_t)0, SEEK_SET); /* wait TIMELIM for someone to signal us */ got_io = 0; signal(SIGIO, sig_io); - oldalrm = (int (*)())signal(SIGALRM, sig_alrm); + oldalrm = signal(SIGALRM, sig_alrm); alarm(TIMELIM); pflock(0); /* unlock persist file for attach */ while (!got_io) @@ -141,14 +152,24 @@ pfhold() /* holding pattern for idle rendering proces signal(SIGIO, SIG_DFL); 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; + */ + close(0); + if (open(inpname, O_RDONLY) != 0) + error(INTERNAL, "unexpected stdin file number"); + clearerr(stdin); + close(1); + if (open(outpname, O_WRONLY) != 1) + error(INTERNAL, "unexpected stdout file number"); sleep(3); /* give them a chance to open their pipes */ if (errname[0]) { - if (freopen(errname, "w", stderr) == NULL) - goto openerr; + close(2); + if (open(errname, O_WRONLY) != 2) + error(INTERNAL, "unexpected stderr file number"); unlink(errname); errname[0] = '\0'; } @@ -164,7 +185,8 @@ openerr: } -io_process() /* just act as go-between for actual process */ +extern void +io_process(void) /* just act as go-between for actual process */ { register char *cp; register int nr, n; @@ -184,26 +206,28 @@ io_process() /* just act as go-between for actual pro } if (nr < 0) error(SYSTEM, "error reading persist file"); - ftruncate(persistfd, 0L); /* truncate persist file */ +#ifndef _WIN32 /* XXX we need a replacement for that one */ + ftruncate(persistfd, (off_t)0L); /* truncate persist file */ +#endif pfdetach(); /* close & release persist file */ buf[nr] = '\0'; /* parse what we got */ - if ((cp = index(buf, ' ')) == NULL) + if ((cp = strchr(buf, ' ')) == NULL) goto formerr; *cp++ = '\0'; if ((pid = atoi(cp)) <= 0) goto formerr; - if ((cp = index(cp, '\n')) == NULL) + if ((cp = strchr(cp, '\n')) == NULL) goto formerr; pfin = ++cp; - if ((cp = index(cp, '\n')) == NULL) + if ((cp = strchr(cp, '\n')) == NULL) goto formerr; *cp++ = '\0'; pfout = cp; - if ((cp = index(cp, '\n')) == NULL) + if ((cp = strchr(cp, '\n')) == NULL) goto formerr; *cp++ = '\0'; pferr = cp; - if ((cp = index(cp, '\n')) == NULL) + if ((cp = strchr(cp, '\n')) == NULL) goto formerr; *cp++ = '\0'; if (cp-buf != nr) @@ -311,7 +335,8 @@ io_process() /* just act as go-between for actual pro } while ((nr -= n) > 0); } } - wait(0); /* wait for feeder process */ + kill(pid, SIGTERM); /* no more process to feed, so... */ + waitpid(pid, 0, 0); /* wait for feeder process */ _exit(status); formerr: error(USER, "format error in persist file"); @@ -323,6 +348,6 @@ writerr: #else -pfclean() {} +extern void pfclean(void) {} #endif