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.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 27 | Line 41 | extern int     headismine;     /* boolean true if header belon
41  
42   extern char     *progname;      /* global program name */
43  
44 + extern char     *errfile;       /* global error file name */
45 +
46   static char     *persistfname = NULL;   /* persist file name */
47   static int      persistfd = -1;         /* persist file descriptor */
48  
49 < static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1];
49 > static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
50  
51  
52   pfdetach()              /* release persist (and header) resources */
# Line 41 | Line 57 | pfdetach()             /* release persist (and header) resources
57          persistfname = NULL;
58          inpname[0] = '\0';
59          outpname[0] = '\0';
60 +        errname[0] = '\0';
61          headismine = 0;
62   }
63  
# Line 55 | Line 72 | pfclean()              /* clean up persist files */
72                  unlink(inpname);
73          if (outpname[0])
74                  unlink(outpname);
75 +        if (errname[0])
76 +                unlink(errname);
77   }
78  
79  
# Line 107 | 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;
134          if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
135                  goto createrr;
136 <        sprintf(buf, "%s %d\n%s\n%s\n", progname, getpid(), inpname, outpname);
137 <        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
138 <                error(SYSTEM, "seek/truncate error on persist file");
136 >        if (errfile == NULL &&
137 >                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
138 >                goto createrr;
139 >        sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
140 >                        inpname, outpname, errname);
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);
148          oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
149          alarm(TIMELIM);
150 <        pflock(0);
150 >        pflock(0);                      /* unlock persist file for attach */
151          while (!got_io)
152 <                pause();
153 <        alarm(0);
152 >                pause();                /* wait for attach */
153 >        alarm(0);                       /* turn off alarm */
154          signal(SIGALRM, oldalrm);
155          signal(SIGIO, SIG_DFL);
156 <        pflock(1);
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;
160          if (freopen(outpname, "w", stdout) == NULL)
161                  goto openerr;
162 +        if (errname[0]) {
163 +                if (freopen(errname, "w", stderr) == NULL)
164 +                        goto openerr;
165 +                unlink(errname);
166 +                errname[0] = '\0';
167 +        }
168          unlink(inpname);
169          inpname[0] = '\0';
170          unlink(outpname);
# Line 147 | 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;
185 <        int     pid;
186 <                                /* load and close persist file */
187 <        sleep(5);               /* helps synchronization */
188 <        nr = read(persistfd, buf, sizeof(buf)-1);
189 <        pfdetach();
190 <        if (nr <= 0)
191 <                error(SYSTEM, "cannot read persist file");
192 <        buf[nr] = '\0';
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 >        }
197 >        if (nr < 0)
198 >                error(SYSTEM, "error reading persist file");
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 175 | Line 214 | io_process()           /* just act as conduits to and from actu
214          if ((cp = index(cp, '\n')) == NULL)
215                  goto formerr;
216          *cp++ = '\0';
217 +        pferr = cp;
218 +        if ((cp = index(cp, '\n')) == NULL)
219 +                goto formerr;
220 +        *cp++ = '\0';
221          if (cp-buf != nr)
222                  goto formerr;
223          if (strcmp(buf, progname)) {
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 <                error(SYSTEM, "fork failed in io_process");
233 <                                /* connect to appropriate pipe */
234 <        if (pid) {                      /* parent passes renderer output */
235 <                close(0);
236 <                if (open(pfout, O_RDONLY) != 0)
237 <                        error(SYSTEM, "cannot open input pipe in io_process");
238 <        } else {                        /* child passes renderer input */
239 <                close(1);
240 <                if (open(pfin, O_WRONLY) != 1)
241 <                        error(SYSTEM, "cannot open output pipe in io_process");
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 (fdout >= 0) {
248 >                        FD_SET(fdout, &readfds);
249 >                        FD_SET(fdout, &excepfds);
250 >                        nfds = fdout+1;
251 >                }
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 */
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!) */
309 >        _exit(0);               /* we ought to return renderer error status! */
310   formerr:
311          error(USER, "format error in persist file");
312 + readerr:
313 +        error(SYSTEM, "read error in io_process");
314   writerr:
315          error(SYSTEM, "write error in io_process");
316   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines