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.15 by greg, Sat Jul 6 23:19:57 1996 UTC vs.
Revision 2.20 by gregl, Thu Oct 16 13:49:22 1997 UTC

# Line 11 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11   #include "standard.h"
12  
13   #ifdef F_SETLKW
14
14   #include "paths.h"
15   #include <signal.h>
16   #include <sys/types.h>
17 + #ifdef INCL_SEL_H
18 + #include <sys/select.h>
19 + #endif
20   #include <sys/stat.h>
21 +                                        /* select call compatibility stuff */
22 + #ifndef FD_SETSIZE
23 + #include <sys/param.h>
24 + #define FD_SETSIZE      NOFILE          /* maximum # select file descriptors */
25 + #endif
26 + #ifndef FD_SET
27 + #ifndef NFDBITS
28 + #define NFDBITS         (8*sizeof(int)) /* number of bits per fd_mask */
29 + #endif
30 + #define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
31 + #define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
32 + #define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
33 + #define FD_ZERO(p)      bzero((char *)(p), sizeof(*(p)))
34 + #endif
35  
36   #ifndef TIMELIM
37   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
# Line 112 | Line 128 | pfhold()               /* holding pattern for idle rendering proces
128                                  /* close input and output descriptors */
129          close(fileno(stdin));
130          close(fileno(stdout));
131 +        if (errfile == NULL)
132 +                close(fileno(stderr));
133                                  /* create named pipes for input and output */
134          if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
135                  goto createrr;
# Line 125 | 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, 0L, 0);
147                                  /* wait TIMELIM for someone to signal us */
148          got_io = 0;
149          signal(SIGIO, sig_io);
# Line 133 | Line 152 | pfhold()               /* holding pattern for idle rendering proces
152          pflock(0);                      /* unlock persist file for attach */
153          while (!got_io)
154                  pause();                /* wait for attach */
136        pflock(1);                      /* grab persist file right back! */
155          alarm(0);                       /* turn off alarm */
156          signal(SIGALRM, oldalrm);
157          signal(SIGIO, SIG_DFL);
158 <        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
141 <                error(SYSTEM, "seek/truncate error on persist file");
158 >        pflock(1);                      /* grab persist file back */
159                                  /* someone wants us; reopen stdin and stdout */
160          if (freopen(inpname, "r", stdin) == NULL)
161                  goto openerr;
# Line 162 | Line 179 | openerr:
179   }
180  
181  
182 < io_process()            /* just act as conduits to and from actual process */
182 > io_process()            /* just act as go-between for actual process */
183   {
184          register char   *cp;
185          register int    nr, n;
186 <        char    buf[512], *pfin, *pfout, *pferr;
187 <        int     pid, pid2 = -1;
186 >        char    buf[BUFSIZ], *pfin, *pfout, *pferr;
187 >        int     pid, nfds;
188 >        int     fdin, fdout, fderr = -1;
189 >        fd_set  readfds, writefds, excepfds;
190                                          /* load persist file */
191 +        n = 40;
192          while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
193 +                if (!n--)
194 +                        error(USER, "unattended persist file?");
195                  pflock(0);
196                  sleep(15);              /* wait until ready */
197                  pflock(1);
198          }
177        pfdetach();                     /* close persist file */
199          if (nr < 0)
200                  error(SYSTEM, "error reading persist file");
201 <        buf[nr] = '\0';
201 >        ftruncate(persistfd, 0L);       /* truncate persist file */
202 >        pfdetach();                     /* close & release persist file */
203 >        buf[nr] = '\0';                 /* parse what we got */
204          if ((cp = index(buf, ' ')) == NULL)
205                  goto formerr;
206          *cp++ = '\0';
# Line 203 | Line 226 | io_process()           /* just act as conduits to and from actu
226                  sprintf(errmsg, "persist file for %s, not %s", buf, progname);
227                  error(USER, errmsg);
228          }
229 <                                /* wake up rendering process */
229 >                                        /* wake up rendering process */
230          if (kill(pid, SIGIO) < 0)
231                  error(SYSTEM, "cannot signal rendering process in io_process");
232 <        pid = fork();           /* fork i/o process */
233 <        if (pid < 0)
234 <                goto forkerr;
235 <                                /* connect to appropriate pipe */
236 <        if (pid) {                      /* parent passes renderer output */
237 <                close(0);
238 <                if (pferr[0]) {
239 <                        pid2 = fork();          /* fork another for stderr */
240 <                        if (pid2 < 0)
241 <                                goto forkerr;
232 >                                        /* open named pipes, in order */
233 >        if ((fdin = open(pfin, O_WRONLY)) < 0)
234 >                error(SYSTEM, "cannot open input pipe in io_process");
235 >        if ((fdout = open(pfout, O_RDONLY)) < 0)
236 >                error(SYSTEM, "cannot open output pipe in io_process");
237 >        if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
238 >                error(SYSTEM, "cannot open error pipe in io_process");
239 >        for ( ; ; ) {                   /* loop on select call */
240 >                FD_ZERO(&readfds);
241 >                FD_ZERO(&writefds);
242 >                FD_ZERO(&excepfds);
243 >                nfds = 0;
244 >                if (fdin >= 0) {
245 >                        FD_SET(fdin, &writefds);
246 >                        FD_SET(fdin, &excepfds);
247 >                        nfds = fdin+1;
248                  }
249 <                if (pid2) {                     /* parent is still stdout */
250 <                        if (open(pfout, O_RDONLY) != 0)
251 <                                error(SYSTEM,
252 <                                "cannot open output pipe in io_process");
224 <                } else {                        /* second child is stderr */
225 <                        if (open(pferr, O_RDONLY) != 0)
226 <                                error(SYSTEM,
227 <                                "cannot open error pipe in io_process");
228 <                        dup2(2, 1);             /* attach stdout to stderr */
249 >                if (fdout >= 0) {
250 >                        FD_SET(fdout, &readfds);
251 >                        FD_SET(fdout, &excepfds);
252 >                        nfds = fdout+1;
253                  }
254 <        } else {                        /* child passes renderer input */
255 <                close(1);
256 <                if (open(pfin, O_WRONLY) != 1)
257 <                        error(SYSTEM, "cannot open input pipe in io_process");
254 >                if (fderr >= 0) {
255 >                        FD_SET(fderr, &readfds);
256 >                        FD_SET(fderr, &excepfds);
257 >                        nfds = fderr+1;
258 >                }
259 >                if (nfds == 0)
260 >                        break;                  /* all done, exit */
261 >                if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0)
262 >                        error(SYSTEM, "error in select call in io_process");
263 >                                                /* renderer stderr */
264 >                if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
265 >                                FD_ISSET(fderr, &excepfds))) {
266 >                        nr = read(fderr, cp=buf, sizeof(buf));
267 >                        if (nr < 0)
268 >                                goto readerr;
269 >                        if (nr == 0) {
270 >                                close(fderr);
271 >                                /* close(2);    don't close stderr! */
272 >                                fderr = -1;
273 >                        } else
274 >                                do {            /* write it all */
275 >                                        if ((n = write(2, cp, nr)) <= 0)
276 >                                                goto writerr;
277 >                                        cp += n;
278 >                                } while ((nr -= n) > 0);
279 >                }
280 >                                                /* renderer stdout */
281 >                if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
282 >                                FD_ISSET(fdout, &excepfds))) {
283 >                        nr = read(fdout, cp=buf, sizeof(buf));
284 >                        if (nr < 0)
285 >                                goto readerr;
286 >                        if (nr == 0) {          /* EOF */
287 >                                close(fdout);
288 >                                close(1);
289 >                                fdout = -1;
290 >                        } else
291 >                                do {            /* write it all */
292 >                                        if ((n = write(1, cp, nr)) <= 0)
293 >                                                goto writerr;
294 >                                        cp += n;
295 >                                } while ((nr -= n) > 0);
296 >                }
297 >                                                /* renderer stdin */
298 >                if (fdin >= 0 && (FD_ISSET(fdin, &writefds) ||
299 >                                FD_ISSET(fdin, &excepfds))) {
300 >                        nr = read(0, buf, 512); /* use minimal buffer */
301 >                        if (nr < 0)
302 >                                goto readerr;
303 >                        if (nr == 0) {
304 >                                close(0);
305 >                                close(fdin);
306 >                                fdin = -1;
307 >                        } else if (write(fdin, buf, nr) < nr)
308 >                                goto writerr;   /* what else to do? */
309 >                }
310          }
311 <                                /* pass input to output */
236 <                                /* read as much as we can, write all of it */
237 <        while ((nr = read(0, cp=buf, sizeof(buf))) > 0)
238 <                do {
239 <                        if ((n = write(1, cp, nr)) <= 0)
240 <                                error(SYSTEM, "write error in io_process");
241 <                        cp += n;
242 <                } while ((nr -= n) > 0);
243 <        if (nr < 0)
244 <                error(SYSTEM, "read error in io_process");
245 <        close(0);               /* close input */
246 <        close(1);               /* close output */
247 <        if (pid)                /* parent waits for stdin child */
248 <                wait(0);
249 <        if (pid2 > 0)           /* wait for stderr child also */
250 <                wait(0);
251 <        _exit(0);               /* all done, exit (not quit!) */
311 >        _exit(0);               /* we ought to return renderer error status! */
312   formerr:
313          error(USER, "format error in persist file");
314 < forkerr:
315 <        error(SYSTEM, "fork failed in io_process");
314 > readerr:
315 >        error(SYSTEM, "read error in io_process");
316 > writerr:
317 >        error(SYSTEM, "write error in io_process");
318   }
319  
320   #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines