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.13 by greg, Thu Jul 4 09:54:59 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 27 | Line 43 | extern int     headismine;     /* boolean true if header belon
43  
44   extern char     *progname;      /* global program name */
45  
46 + extern char     *errfile;       /* global error file name */
47 +
48   static char     *persistfname = NULL;   /* persist file name */
49   static int      persistfd = -1;         /* persist file descriptor */
50  
51 < static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1];
51 > static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
52  
53  
54   pfdetach()              /* release persist (and header) resources */
# Line 41 | Line 59 | pfdetach()             /* release persist (and header) resources
59          persistfname = NULL;
60          inpname[0] = '\0';
61          outpname[0] = '\0';
62 +        errname[0] = '\0';
63          headismine = 0;
64   }
65  
# Line 55 | Line 74 | pfclean()              /* clean up persist files */
74                  unlink(inpname);
75          if (outpname[0])
76                  unlink(outpname);
77 +        if (errname[0])
78 +                unlink(errname);
79   }
80  
81  
# Line 107 | 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;
136          if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
137                  goto createrr;
138 <        sprintf(buf, "%s %d\n%s\n%s\n", progname, getpid(), inpname, outpname);
139 <        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
140 <                error(SYSTEM, "seek/truncate error on persist file");
138 >        if (errfile == NULL &&
139 >                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
140 >                goto createrr;
141 >        sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
142 >                        inpname, outpname, errname);
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);
150          oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
151          alarm(TIMELIM);
152 <        pflock(0);
152 >        pflock(0);                      /* unlock persist file for attach */
153          while (!got_io)
154 <                pause();
155 <        alarm(0);
154 >                pause();                /* wait for attach */
155 >        alarm(0);                       /* turn off alarm */
156          signal(SIGALRM, oldalrm);
157          signal(SIGIO, SIG_DFL);
158 <        pflock(1);
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;
162          if (freopen(outpname, "w", stdout) == NULL)
163                  goto openerr;
164 +        if (errname[0]) {
165 +                if (freopen(errname, "w", stderr) == NULL)
166 +                        goto openerr;
167 +                unlink(errname);
168 +                errname[0] = '\0';
169 +        }
170          unlink(inpname);
171          inpname[0] = '\0';
172          unlink(outpname);
# Line 147 | 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;
187 <        int     pid;
188 <                                /* load and close persist file */
189 <        sleep(5);               /* helps synchronization */
190 <        nr = read(persistfd, buf, sizeof(buf)-1);
191 <        pfdetach();
192 <        if (nr <= 0)
193 <                error(SYSTEM, "cannot read persist file");
194 <        buf[nr] = '\0';
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 >        }
199 >        if (nr < 0)
200 >                error(SYSTEM, "error reading persist file");
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 175 | Line 216 | io_process()           /* just act as conduits to and from actu
216          if ((cp = index(cp, '\n')) == NULL)
217                  goto formerr;
218          *cp++ = '\0';
219 +        pferr = cp;
220 +        if ((cp = index(cp, '\n')) == NULL)
221 +                goto formerr;
222 +        *cp++ = '\0';
223          if (cp-buf != nr)
224                  goto formerr;
225          if (strcmp(buf, progname)) {
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 <                error(SYSTEM, "fork failed in io_process");
235 <                                /* connect to appropriate pipe */
236 <        if (pid) {                      /* parent passes renderer output */
237 <                close(0);
238 <                if (open(pfout, O_RDONLY) != 0)
239 <                        error(SYSTEM, "cannot open input pipe in io_process");
240 <        } else {                        /* child passes renderer input */
241 <                close(1);
242 <                if (open(pfin, O_WRONLY) != 1)
243 <                        error(SYSTEM, "cannot open output pipe in io_process");
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 (fdout >= 0) {
250 >                        FD_SET(fdout, &readfds);
251 >                        FD_SET(fdout, &excepfds);
252 >                        nfds = fdout+1;
253 >                }
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 */
201 <                                /* read as much as we can, write all of it */
202 <        while ((nr = read(0, cp=buf, sizeof(buf))) > 0)
203 <                do {
204 <                        if ((n = write(1, cp, nr)) <= 0)
205 <                                goto writerr;
206 <                        cp += n;
207 <                } while ((nr -= n) > 0);
208 <        if (nr < 0)
209 <                error(SYSTEM, "read error in io_process");
210 <        close(0);               /* close input */
211 <        close(1);               /* close output */
212 <        if (pid)                /* parent waits for child */
213 <                wait(0);
214 <        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 + readerr:
315 +        error(SYSTEM, "read error in io_process");
316   writerr:
317          error(SYSTEM, "write error in io_process");
318   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines