--- ray/src/rt/persist.c 2003/06/26 00:58:10 2.28 +++ ray/src/rt/persist.c 2008/05/01 15:50:28 2.41 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: persist.c,v 2.28 2003/06/26 00:58:10 schorsch Exp $"; +static const char RCSid[] = "$Id: persist.c,v 2.41 2008/05/01 15:50:28 greg Exp $"; #endif /* * Routines for persistent rtrace and rpict processes. @@ -9,39 +9,43 @@ static const char RCSid[] = "$Id: persist.c,v 2.28 200 #include "copyright.h" +#include +#include +#include +#include + +#include "platform.h" +#ifndef NON_POSIX /* XXX need abstraction for process management */ + #include +#endif + +#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 -#ifndef freebsd -#define mkfifo(fn,md) mknod(fn, S_IFIFO|(md), 0) -#endif - -extern void io_process(); - 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; -void -pfdetach() /* release persist (and header) resources */ + +extern void +pfdetach(void) /* release persist (and header) resources */ { if (persistfd >= 0) close(persistfd); @@ -54,8 +58,8 @@ pfdetach() /* release persist (and header) resources } -void -pfclean() /* clean up persist files */ +extern void +pfclean(void) /* clean up persist files */ { if (persistfd >= 0) close(persistfd); @@ -70,9 +74,10 @@ pfclean() /* clean up persist files */ } -void -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; @@ -85,8 +90,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) { @@ -107,22 +114,22 @@ char *pfn; static int got_io; -static void sig_io() { got_io++; } +static void sig_io(int i) { got_io++; } -static void sig_alrm() { quit(0); } +static void sig_alrm(int i) { quit(0); } -void -pfhold() /* holding pattern for idle rendering process */ +extern void +pfhold(void) /* holding pattern for idle rendering process */ { - void (*oldalrm)(); + sighandler_t *oldalrm; char buf[512]; register int n; /* close input and output descriptors */ - close(fileno(stdin)); - close(fileno(stdout)); + close(0); + close(1); if (errfile == NULL) - close(fileno(stderr)); + close(2); /* create named pipes for input and output */ if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0) goto createrr; @@ -136,11 +143,11 @@ pfhold() /* holding pattern for idle rendering proces n = strlen(buf); if (write(persistfd, buf, n) < n) error(SYSTEM, "error writing persist file"); - lseek(persistfd, (off_t)0L, 0); + lseek(persistfd, (off_t)0, SEEK_SET); /* wait TIMELIM for someone to signal us */ got_io = 0; signal(SIGIO, sig_io); - oldalrm = (void (*)())signal(SIGALRM, sig_alrm); + oldalrm = signal(SIGALRM, sig_alrm); alarm(TIMELIM); pflock(0); /* unlock persist file for attach */ while (!got_io) @@ -150,12 +157,6 @@ 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"); @@ -178,13 +179,11 @@ pfhold() /* holding pattern for idle rendering proces return; createrr: error(SYSTEM, "cannot create named pipes in pfhold"); -openerr: - error(SYSTEM, "cannot open named pipes in pfhold"); } -void -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; @@ -209,23 +208,23 @@ io_process() /* just act as go-between for actual pro #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) @@ -333,7 +332,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"); @@ -345,6 +345,6 @@ writerr: #else -void pfclean() {} +extern void pfclean(void) {} #endif