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.18 by greg, Mon Jul 22 18:09:26 1996 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/stat.h>
18 +                                        /* select call compatibility stuff */
19 + #ifndef FD_SETSIZE
20 + #include <sys/param.h>
21 + #define FD_SETSIZE      NOFILE          /* maximum # select file descriptors */
22 + #endif
23 + #ifndef FD_SET
24 + #ifndef NFDBITS
25 + #define NFDBITS         (8*sizeof(int)) /* number of bits per fd_mask */
26 + #endif
27 + #define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
28 + #define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
29 + #define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
30 + #define FD_ZERO(p)      bzero((char *)(p), sizeof(*(p)))
31 + #endif
32  
33   #ifndef TIMELIM
34   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
# Line 27 | Line 40 | extern int     headismine;     /* boolean true if header belon
40  
41   extern char     *progname;      /* global program name */
42  
43 + extern char     *errfile;       /* global error file name */
44 +
45   static char     *persistfname = NULL;   /* persist file name */
46   static int      persistfd = -1;         /* persist file descriptor */
47  
48 < static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1];
48 > static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
49  
50  
51   pfdetach()              /* release persist (and header) resources */
# Line 41 | Line 56 | pfdetach()             /* release persist (and header) resources
56          persistfname = NULL;
57          inpname[0] = '\0';
58          outpname[0] = '\0';
59 +        errname[0] = '\0';
60          headismine = 0;
61   }
62  
# Line 55 | Line 71 | pfclean()              /* clean up persist files */
71                  unlink(inpname);
72          if (outpname[0])
73                  unlink(outpname);
74 +        if (errname[0])
75 +                unlink(errname);
76   }
77  
78  
# Line 107 | Line 125 | pfhold()               /* holding pattern for idle rendering proces
125                                  /* close input and output descriptors */
126          close(fileno(stdin));
127          close(fileno(stdout));
128 +        if (errfile == NULL)
129 +                close(fileno(stderr));
130                                  /* create named pipes for input and output */
131          if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
132                  goto createrr;
133          if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
134                  goto createrr;
135 <        sprintf(buf, "%s %d\n%s\n%s\n", progname, getpid(), inpname, outpname);
136 <        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
137 <                error(SYSTEM, "seek/truncate error on persist file");
135 >        if (errfile == NULL &&
136 >                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
137 >                goto createrr;
138 >        sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
139 >                        inpname, outpname, errname);
140          n = strlen(buf);
141          if (write(persistfd, buf, n) < n)
142                  error(SYSTEM, "error writing persist file");
143 +        lseek(persistfd, 0L, 0);
144                                  /* wait TIMELIM for someone to signal us */
145          got_io = 0;
146          signal(SIGIO, sig_io);
147          oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
148          alarm(TIMELIM);
149 <        pflock(0);
149 >        pflock(0);                      /* unlock persist file for attach */
150          while (!got_io)
151 <                pause();
152 <        alarm(0);
151 >                pause();                /* wait for attach */
152 >        alarm(0);                       /* turn off alarm */
153          signal(SIGALRM, oldalrm);
154          signal(SIGIO, SIG_DFL);
155 <        pflock(1);
155 >        pflock(1);                      /* grab persist file back */
156                                  /* someone wants us; reopen stdin and stdout */
157          if (freopen(inpname, "r", stdin) == NULL)
158                  goto openerr;
159          if (freopen(outpname, "w", stdout) == NULL)
160                  goto openerr;
161 +        if (errname[0]) {
162 +                if (freopen(errname, "w", stderr) == NULL)
163 +                        goto openerr;
164 +                unlink(errname);
165 +                errname[0] = '\0';
166 +        }
167          unlink(inpname);
168          inpname[0] = '\0';
169          unlink(outpname);
# Line 147 | Line 176 | openerr:
176   }
177  
178  
179 < io_process()            /* just act as conduits to and from actual process */
179 > io_process()            /* just act as go-between for actual process */
180   {
181          register char   *cp;
182          register int    nr, n;
183 <        char    buf[512], *pfin, *pfout;
184 <        int     pid;
185 <                                /* load and close persist file */
186 <        sleep(5);               /* helps synchronization */
187 <        nr = read(persistfd, buf, sizeof(buf)-1);
188 <        pfdetach();
189 <        if (nr <= 0)
190 <                error(SYSTEM, "cannot read persist file");
191 <        buf[nr] = '\0';
183 >        char    buf[BUFSIZ], *pfin, *pfout, *pferr;
184 >        int     pid, nfds;
185 >        int     fdin, fdout, fderr = -1;
186 >        fd_set  readfds, writefds, excepfds;
187 >                                        /* load persist file */
188 >        n = 40;
189 >        while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
190 >                if (!n--)
191 >                        error(USER, "unattended persist file?");
192 >                pflock(0);
193 >                sleep(15);              /* wait until ready */
194 >                pflock(1);
195 >        }
196 >        if (nr < 0)
197 >                error(SYSTEM, "error reading persist file");
198 >        ftruncate(persistfd, 0L);       /* truncate persist file */
199 >        pfdetach();                     /* close & release persist file */
200 >        buf[nr] = '\0';                 /* parse what we got */
201          if ((cp = index(buf, ' ')) == NULL)
202                  goto formerr;
203          *cp++ = '\0';
# Line 175 | Line 213 | io_process()           /* just act as conduits to and from actu
213          if ((cp = index(cp, '\n')) == NULL)
214                  goto formerr;
215          *cp++ = '\0';
216 +        pferr = cp;
217 +        if ((cp = index(cp, '\n')) == NULL)
218 +                goto formerr;
219 +        *cp++ = '\0';
220          if (cp-buf != nr)
221                  goto formerr;
222          if (strcmp(buf, progname)) {
223                  sprintf(errmsg, "persist file for %s, not %s", buf, progname);
224                  error(USER, errmsg);
225          }
226 <                                /* wake up rendering process */
226 >                                        /* wake up rendering process */
227          if (kill(pid, SIGIO) < 0)
228                  error(SYSTEM, "cannot signal rendering process in io_process");
229 <        pid = fork();           /* fork i/o process */
230 <        if (pid < 0)
231 <                error(SYSTEM, "fork failed in io_process");
232 <                                /* connect to appropriate pipe */
233 <        if (pid) {                      /* parent passes renderer output */
234 <                close(0);
235 <                if (open(pfout, O_RDONLY) != 0)
236 <                        error(SYSTEM, "cannot open input pipe in io_process");
237 <        } else {                        /* child passes renderer input */
238 <                close(1);
239 <                if (open(pfin, O_WRONLY) != 1)
240 <                        error(SYSTEM, "cannot open output pipe in io_process");
229 >                                        /* open named pipes, in order */
230 >        if ((fdin = open(pfin, O_WRONLY)) < 0)
231 >                error(SYSTEM, "cannot open input pipe in io_process");
232 >        if ((fdout = open(pfout, O_RDONLY)) < 0)
233 >                error(SYSTEM, "cannot open output pipe in io_process");
234 >        if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
235 >                error(SYSTEM, "cannot open error pipe in io_process");
236 >        for ( ; ; ) {                   /* loop on select call */
237 >                FD_ZERO(&readfds);
238 >                FD_ZERO(&writefds);
239 >                FD_ZERO(&excepfds);
240 >                nfds = 0;
241 >                if (fdin >= 0) {
242 >                        FD_SET(fdin, &writefds);
243 >                        FD_SET(fdin, &excepfds);
244 >                        nfds = fdin+1;
245 >                }
246 >                if (fdout >= 0) {
247 >                        FD_SET(fdout, &readfds);
248 >                        FD_SET(fdout, &excepfds);
249 >                        nfds = fdout+1;
250 >                }
251 >                if (fderr >= 0) {
252 >                        FD_SET(fderr, &readfds);
253 >                        FD_SET(fderr, &excepfds);
254 >                        nfds = fderr+1;
255 >                }
256 >                if (nfds == 0)
257 >                        break;                  /* all done, exit */
258 >                if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0)
259 >                        error(SYSTEM, "error in select call in io_process");
260 >                                                /* renderer stderr */
261 >                if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
262 >                                FD_ISSET(fderr, &excepfds))) {
263 >                        nr = read(fderr, cp=buf, sizeof(buf));
264 >                        if (nr < 0)
265 >                                goto readerr;
266 >                        if (nr == 0) {
267 >                                close(fderr);
268 >                                /* close(2);    don't close stderr! */
269 >                                fderr = -1;
270 >                        } else
271 >                                do {            /* write it all */
272 >                                        if ((n = write(2, cp, nr)) <= 0)
273 >                                                goto writerr;
274 >                                        cp += n;
275 >                                } while ((nr -= n) > 0);
276 >                }
277 >                                                /* renderer stdout */
278 >                if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
279 >                                FD_ISSET(fdout, &excepfds))) {
280 >                        nr = read(fdout, cp=buf, sizeof(buf));
281 >                        if (nr < 0)
282 >                                goto readerr;
283 >                        if (nr == 0) {          /* EOF */
284 >                                close(fdout);
285 >                                close(1);
286 >                                fdout = -1;
287 >                        } else
288 >                                do {            /* write it all */
289 >                                        if ((n = write(1, cp, nr)) <= 0)
290 >                                                goto writerr;
291 >                                        cp += n;
292 >                                } while ((nr -= n) > 0);
293 >                }
294 >                                                /* renderer stdin */
295 >                if (fdin >= 0 && (FD_ISSET(fdin, &writefds) ||
296 >                                FD_ISSET(fdin, &excepfds))) {
297 >                        nr = read(0, buf, 512); /* use minimal buffer */
298 >                        if (nr < 0)
299 >                                goto readerr;
300 >                        if (nr == 0) {
301 >                                close(0);
302 >                                close(fdin);
303 >                                fdin = -1;
304 >                        } else if (write(fdin, buf, nr) < nr)
305 >                                goto writerr;   /* what else to do? */
306 >                }
307          }
308 <                                /* 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!) */
308 >        _exit(0);               /* we ought to return renderer error status! */
309   formerr:
310          error(USER, "format error in persist file");
311 + readerr:
312 +        error(SYSTEM, "read error in io_process");
313   writerr:
314          error(SYSTEM, "write error in io_process");
315   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines