ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/persist.c
(Generate patch)

Comparing ray/src/rt/persist.c (file contents):
Revision 2.28 by schorsch, Thu Jun 26 00:58:10 2003 UTC vs.
Revision 2.41 by greg, Thu May 1 15:50:28 2008 UTC

# Line 9 | Line 9 | static const char      RCSid[] = "$Id$";
9  
10   #include "copyright.h"
11  
12 + #include <string.h>
13 + #include <signal.h>
14 + #include <sys/stat.h>
15 + #include <sys/types.h>
16 +
17 + #include "platform.h"
18 + #ifndef NON_POSIX /* XXX need abstraction for process management */
19 + #include <sys/wait.h>
20 + #endif
21 +
22 + #include "rtprocess.h" /* getpid() */
23   #include "standard.h"
24   #include "random.h"
25 + #include "ray.h"
26  
27   #ifdef F_SETLKW
28   #include "paths.h"
29   #include "selcall.h"
18 #include <signal.h>
19 #include <sys/stat.h>
30  
31   #ifndef TIMELIM
32   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
33   #endif
34  
25 #ifndef freebsd
26 #define mkfifo(fn,md)   mknod(fn, S_IFIFO|(md), 0)
27 #endif
28
29 extern void     io_process();
30
35   extern int      headismine;     /* boolean true if header belongs to me */
32
36   extern char     *progname;      /* global program name */
34
37   extern char     *errfile;       /* global error file name */
36
38   static char     *persistfname = NULL;   /* persist file name */
39   static int      persistfd = -1;         /* persist file descriptor */
39
40   static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
41  
42 + typedef void (sighandler_t)(int);
43 + static sighandler_t sig_io;
44 + static sighandler_t sig_alrm;
45  
46 < void
47 < pfdetach()              /* release persist (and header) resources */
46 >
47 > extern void
48 > pfdetach(void)          /* release persist (and header) resources */
49   {
50          if (persistfd >= 0)
51                  close(persistfd);
# Line 54 | Line 58 | pfdetach()             /* release persist (and header) resources
58   }
59  
60  
61 < void
62 < pfclean()               /* clean up persist files */
61 > extern void
62 > pfclean(void)           /* clean up persist files */
63   {
64          if (persistfd >= 0)
65                  close(persistfd);
# Line 70 | Line 74 | pfclean()              /* clean up persist files */
74   }
75  
76  
77 < void
78 < pflock(lf)              /* place or release exclusive lock on file */
79 < int     lf;
77 > extern void
78 > pflock(         /* place or release exclusive lock on file */
79 >        int     lf
80 > )
81   {
82          struct flock    fls;
83  
# Line 85 | Line 90 | int    lf;
90   }
91  
92  
93 < persistfile(pfn)        /* open persist file and lock it */
94 < char    *pfn;
93 > extern void
94 > persistfile(    /* open persist file and lock it */
95 >        char    *pfn
96 > )
97   {
98          persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
99          if (persistfd >= 0) {
# Line 107 | Line 114 | char   *pfn;
114  
115   static int      got_io;
116  
117 < static void sig_io() { got_io++; }
117 > static void sig_io(int i) { got_io++; }
118  
119 < static void sig_alrm() { quit(0); }
119 > static void sig_alrm(int i) { quit(0); }
120  
121  
122 < void
123 < pfhold()                /* holding pattern for idle rendering process */
122 > extern void
123 > pfhold(void)            /* holding pattern for idle rendering process */
124   {
125 <        void    (*oldalrm)();
125 >        sighandler_t    *oldalrm;
126          char    buf[512];
127          register int    n;
128                                  /* close input and output descriptors */
129 <        close(fileno(stdin));
130 <        close(fileno(stdout));
129 >        close(0);
130 >        close(1);
131          if (errfile == NULL)
132 <                close(fileno(stderr));
132 >                close(2);
133                                  /* create named pipes for input and output */
134          if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
135                  goto createrr;
# Line 136 | Line 143 | pfhold()               /* holding pattern for idle rendering proces
143          n = strlen(buf);
144          if (write(persistfd, buf, n) < n)
145                  error(SYSTEM, "error writing persist file");
146 <        lseek(persistfd, (off_t)0L, 0);
146 >        lseek(persistfd, (off_t)0, SEEK_SET);
147                                  /* wait TIMELIM for someone to signal us */
148          got_io = 0;
149          signal(SIGIO, sig_io);
150 <        oldalrm = (void (*)())signal(SIGALRM, sig_alrm);
150 >        oldalrm = signal(SIGALRM, sig_alrm);
151          alarm(TIMELIM);
152          pflock(0);                      /* unlock persist file for attach */
153          while (!got_io)
# Line 150 | Line 157 | pfhold()               /* holding pattern for idle rendering proces
157          signal(SIGIO, SIG_DFL);
158          pflock(1);                      /* grab persist file back */
159                                  /* someone wants us; reopen stdin and stdout */
153        /*
154        if (freopen(inpname, "r", stdin) == NULL)
155                goto openerr;
156        if (freopen(outpname, "w", stdout) == NULL)
157                goto openerr;
158        */
160          close(0);
161          if (open(inpname, O_RDONLY) != 0)
162                  error(INTERNAL, "unexpected stdin file number");
# Line 178 | Line 179 | pfhold()               /* holding pattern for idle rendering proces
179          return;
180   createrr:
181          error(SYSTEM, "cannot create named pipes in pfhold");
181 openerr:
182        error(SYSTEM, "cannot open named pipes in pfhold");
182   }
183  
184  
185 < void
186 < io_process()            /* just act as go-between for actual process */
185 > extern void
186 > io_process(void)                /* just act as go-between for actual process */
187   {
188          register char   *cp;
189          register int    nr, n;
# Line 209 | Line 208 | io_process()           /* just act as go-between for actual pro
208   #endif
209          pfdetach();                     /* close & release persist file */
210          buf[nr] = '\0';                 /* parse what we got */
211 <        if ((cp = index(buf, ' ')) == NULL)
211 >        if ((cp = strchr(buf, ' ')) == NULL)
212                  goto formerr;
213          *cp++ = '\0';
214          if ((pid = atoi(cp)) <= 0)
215                  goto formerr;
216 <        if ((cp = index(cp, '\n')) == NULL)
216 >        if ((cp = strchr(cp, '\n')) == NULL)
217                  goto formerr;
218          pfin = ++cp;
219 <        if ((cp = index(cp, '\n')) == NULL)
219 >        if ((cp = strchr(cp, '\n')) == NULL)
220                  goto formerr;
221          *cp++ = '\0';
222          pfout = cp;
223 <        if ((cp = index(cp, '\n')) == NULL)
223 >        if ((cp = strchr(cp, '\n')) == NULL)
224                  goto formerr;
225          *cp++ = '\0';
226          pferr = cp;
227 <        if ((cp = index(cp, '\n')) == NULL)
227 >        if ((cp = strchr(cp, '\n')) == NULL)
228                  goto formerr;
229          *cp++ = '\0';
230          if (cp-buf != nr)
# Line 333 | Line 332 | io_process()           /* just act as go-between for actual pro
332                                  } while ((nr -= n) > 0);
333                  }
334          }
335 <        wait(0);                /* wait for feeder process */
335 >        kill(pid, SIGTERM);     /* no more process to feed, so... */
336 >        waitpid(pid, 0, 0);     /* wait for feeder process */
337          _exit(status);
338   formerr:
339          error(USER, "format error in persist file");
# Line 345 | Line 345 | writerr:
345  
346   #else
347  
348 < void pfclean() {}
348 > extern void pfclean(void) {}
349  
350   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines