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.6 by greg, Fri Jan 29 12:02:29 1993 UTC vs.
Revision 2.16 by greg, Mon Jul 8 19:46:21 1996 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1993 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# 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 25 | Line 38 | extern char    *strcpy(), *index();
38  
39   extern int      headismine;     /* boolean true if header belongs to me */
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 39 | 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 53 | 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 73 | Line 93 | int    lf;
93   persistfile(pfn)        /* open persist file and lock it */
94   char    *pfn;
95   {
96 <        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666);
96 >        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
97          if (persistfd >= 0) {
98                  persistfname = pfn;
99                  pflock(1);
# Line 90 | Line 110 | char   *pfn;
110   }
111  
112  
113 < int sig_noop() {}
113 > static int      got_io;
114  
115 + static int sig_io() { got_io++; }
116  
117 + static int sig_alrm() { quit(0); }
118 +
119 +
120   pfhold()                /* holding pattern for idle rendering process */
121   {
122 <        char    buf[128];
122 >        int     (*oldalrm)();
123 >        char    buf[512];
124          register int    n;
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)
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)
133 >        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
134                  goto createrr;
135 <        sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname);
136 <        if (lseek(persistfd, 0L, 0) < 0)
137 <                error(SYSTEM, "seek error on persist file in pfhold");
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 in pfhold");
142 >                error(SYSTEM, "error writing persist file");
143                                  /* wait TIMELIM for someone to signal us */
144 <        signal(SIGIO, sig_noop);
144 >        got_io = 0;
145 >        signal(SIGIO, sig_io);
146 >        oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
147          alarm(TIMELIM);
148 <        pflock(0);
149 <        pause();
150 <        alarm(0);
148 >        pflock(0);                      /* unlock persist file for attach */
149 >        while (!got_io)
150 >                pause();                /* wait for attach */
151 >        pflock(1);                      /* grab persist file right back! */
152 >        alarm(0);                       /* turn off alarm */
153 >        signal(SIGALRM, oldalrm);
154          signal(SIGIO, SIG_DFL);
155 <        pflock(1);
155 >        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
156 >                error(SYSTEM, "seek/truncate error on persist file");
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 136 | 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[4096], *pfin, *pfout;
185 <        int     pid;
186 <                                /* load and close persist file */
187 <        nr = read(persistfd, buf, sizeof(buf));
188 <        pfdetach();
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 >        while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
190 >                pflock(0);
191 >                sleep(15);              /* wait until ready */
192 >                pflock(1);
193 >        }
194 >        pfdetach();                     /* close persist file */
195          if (nr < 0)
196 <                error(SYSTEM, "cannot read persist file");
196 >                error(SYSTEM, "error reading persist file");
197          buf[nr] = '\0';
198 <        if ((cp = index(buf, '\n')) == NULL)
198 >        if ((cp = index(buf, ' ')) == NULL)
199                  goto formerr;
200          *cp++ = '\0';
201 <        if ((pid = atoi(buf)) <= 0)
201 >        if ((pid = atoi(cp)) <= 0)
202                  goto formerr;
156        pfin = cp;
203          if ((cp = index(cp, '\n')) == NULL)
204                  goto formerr;
205 +        pfin = ++cp;
206 +        if ((cp = index(cp, '\n')) == NULL)
207 +                goto formerr;
208          *cp++ = '\0';
209          pfout = cp;
210          if ((cp = index(cp, '\n')) == NULL)
211                  goto formerr;
212          *cp++ = '\0';
213 +        pferr = cp;
214 +        if ((cp = index(cp, '\n')) == NULL)
215 +                goto formerr;
216 +        *cp++ = '\0';
217          if (cp-buf != nr)
218                  goto formerr;
219 <                                /* wake up rendering process */
219 >        if (strcmp(buf, progname)) {
220 >                sprintf(errmsg, "persist file for %s, not %s", buf, progname);
221 >                error(USER, errmsg);
222 >        }
223 >                                        /* wake up rendering process */
224          if (kill(pid, SIGIO) < 0)
225                  error(SYSTEM, "cannot signal rendering process in io_process");
226 <        pid = fork();           /* fork i/o process */
227 <        if (pid < 0)
228 <                error(SYSTEM, "fork failed in io_process");
229 <                                /* connect to appropriate pipe */
230 <        if (pid) {                      /* parent passes renderer output */
231 <                if (freopen(pfout, "r", stdin) == NULL)
232 <                        error(SYSTEM, "cannot open input pipe in io_process");
233 <        } else {                        /* child passes renderer input */
234 <                if (freopen(pfin, "w", stdout) == NULL)
235 <                        error(SYSTEM, "cannot open input pipe in io_process");
226 >                                        /* open named pipes, in order */
227 >        if ((fdin = open(pfin, O_WRONLY)) < 0)
228 >                error(SYSTEM, "cannot open input pipe in io_process");
229 >        if ((fdout = open(pfout, O_RDONLY)) < 0)
230 >                error(SYSTEM, "cannot open output pipe in io_process");
231 >        if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
232 >                error(SYSTEM, "cannot open error pipe in io_process");
233 >        for ( ; ; ) {                   /* loop on select call */
234 >                FD_ZERO(&readfds);
235 >                FD_ZERO(&writefds);
236 >                FD_ZERO(&excepfds);
237 >                nfds = 0;
238 >                if (fdin >= 0) {
239 >                        FD_SET(fdin, &writefds);
240 >                        FD_SET(fdin, &excepfds);
241 >                        nfds = fdin+1;
242 >                }
243 >                if (fdout >= 0) {
244 >                        FD_SET(fdout, &readfds);
245 >                        FD_SET(fdout, &excepfds);
246 >                        nfds = fdout+1;
247 >                }
248 >                if (fderr >= 0) {
249 >                        FD_SET(fderr, &readfds);
250 >                        FD_SET(fderr, &excepfds);
251 >                        nfds = fderr+1;
252 >                }
253 >                if (nfds == 0)
254 >                        break;                  /* all done, exit */
255 >                if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0)
256 >                        error(SYSTEM, "error in select call in io_process");
257 >                                                /* renderer stderr */
258 >                if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
259 >                                FD_ISSET(fderr, &excepfds))) {
260 >                        nr = read(fderr, cp=buf, sizeof(buf));
261 >                        if (nr < 0)
262 >                                goto readerr;
263 >                        if (nr == 0) {
264 >                                close(fderr);
265 >                                close(2);
266 >                                fderr = -1;
267 >                        } else
268 >                                do {            /* write it all */
269 >                                        if ((n = write(2, cp, nr)) <= 0)
270 >                                                goto writerr;
271 >                                        cp += n;
272 >                                } while ((nr -= n) > 0);
273 >                }
274 >                                                /* renderer stdout */
275 >                if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
276 >                                FD_ISSET(fdout, &excepfds))) {
277 >                        nr = read(fdout, cp=buf, sizeof(buf));
278 >                        if (nr < 0)
279 >                                goto readerr;
280 >                        if (nr == 0) {          /* EOF */
281 >                                close(fdout);
282 >                                close(1);
283 >                                fdout = -1;
284 >                        } else
285 >                                do {            /* write it all */
286 >                                        if ((n = write(1, cp, nr)) <= 0)
287 >                                                goto writerr;
288 >                                        cp += n;
289 >                                } while ((nr -= n) > 0);
290 >                }
291 >                                                /* renderer stdin */
292 >                if (fdin >= 0 && (FD_ISSET(fdin, &writefds) ||
293 >                                FD_ISSET(fdin, &excepfds))) {
294 >                        nr = read(0, buf, 512); /* use minimal buffer */
295 >                        if (nr < 0)
296 >                                goto readerr;
297 >                        if (nr == 0) {
298 >                                close(0);
299 >                                close(fdin);
300 >                                fdin = -1;
301 >                        } else if (write(fdin, buf, nr) < nr)
302 >                                goto writerr;   /* what else to do? */
303 >                }
304          }
305 <                                /* pass input to output */
181 <        cp = buf; n = sizeof(buf);
182 <        while ((nr = read(fileno(stdin), cp, n)) > 0) {
183 <                nr += cp-buf;
184 <                if ((n = write(fileno(stdout), buf, nr)) <= 0)
185 <                        error(SYSTEM, "write error in io_process");
186 <                cp = buf;
187 <                while (n < nr)
188 <                        *cp++ = buf[n++];
189 <                n = sizeof(buf) - (cp-buf);
190 <        }
191 <        fclose(stdin);
192 <        fclose(stdout);
193 <        if (pid)                /* parent waits for child */
194 <                wait(0);
195 <        exit(0);                /* all done, exit (not quit!) */
305 >        _exit(0);               /* we ought to return renderer error status! */
306   formerr:
307          error(USER, "format error in persist file");
308 + readerr:
309 +        error(SYSTEM, "read error in io_process");
310 + writerr:
311 +        error(SYSTEM, "write error in io_process");
312   }
313 +
314 + #else
315 +
316 + pfclean() {}
317  
318   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines