--- ray/src/rt/persist.c 2003/10/20 16:01:55 2.32 +++ ray/src/rt/persist.c 2016/03/06 01:13:18 2.44 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: persist.c,v 2.32 2003/10/20 16:01:55 greg Exp $"; +static const char RCSid[] = "$Id: persist.c,v 2.44 2016/03/06 01:13:18 schorsch Exp $"; #endif /* * Routines for persistent rtrace and rpict processes. @@ -12,11 +12,17 @@ static const char RCSid[] = "$Id: persist.c,v 2.32 200 #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 "platform.h" #include "random.h" +#include "ray.h" #ifdef F_SETLKW #include "paths.h" @@ -26,22 +32,20 @@ static const char RCSid[] = "$Id: persist.c,v 2.32 200 #define TIMELIM (8*3600) /* time limit for holding pattern */ #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 (rsighandler_t)(int); +static rsighandler_t sig_io; +static rsighandler_t sig_alrm; + void -pfdetach() /* release persist (and header) resources */ +pfdetach(void) /* release persist (and header) resources */ { if (persistfd >= 0) close(persistfd); @@ -55,7 +59,7 @@ pfdetach() /* release persist (and header) resources void -pfclean() /* clean up persist files */ +pfclean(void) /* clean up persist files */ { if (persistfd >= 0) close(persistfd); @@ -71,8 +75,9 @@ pfclean() /* clean up persist files */ void -pflock(lf) /* place or release exclusive lock on file */ -int lf; +pflock( /* place or release exclusive lock on file */ + int lf +) { struct flock fls; @@ -86,8 +91,9 @@ int lf; void -persistfile(pfn) /* open persist file and lock it */ -char *pfn; +persistfile( /* open persist file and lock it */ + char *pfn +) { persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); if (persistfd >= 0) { @@ -108,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 */ +pfhold(void) /* holding pattern for idle rendering process */ { - void (*oldalrm)(); + rsighandler_t *oldalrm; char buf[512]; - register int n; + 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; @@ -141,7 +147,7 @@ pfhold() /* holding pattern for idle rendering proces /* 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) @@ -151,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"); @@ -179,16 +179,14 @@ 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 */ +io_process(void) /* just act as go-between for actual process */ { - register char *cp; - register int nr, n; + char *cp; + int nr, n; char buf[BUFSIZ], *pfin, *pfout, *pferr; int pid, nfds; int fdout, fderr = -1; @@ -205,9 +203,7 @@ io_process() /* just act as go-between for actual pro } if (nr < 0) error(SYSTEM, "error reading 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 = strchr(buf, ' ')) == NULL) @@ -299,7 +295,7 @@ io_process() /* just act as go-between for actual pro if (!strncmp(cp, progname, n) && cp[n++] == ':' && cp[n++] == ' ') { - register struct erract *ep; + struct erract *ep; for (ep = erract; ep < erract+NERRS; ep++) if (ep->pre[0] && @@ -334,7 +330,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"); @@ -346,6 +343,6 @@ writerr: #else -void pfclean() {} +void pfclean(void) {} #endif