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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines