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.3 by greg, Thu Jan 21 17:01:24 1993 UTC vs.
Revision 2.20 by gregl, Thu Oct 16 13:49:22 1997 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 9 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   */
10  
11   #include "standard.h"
12 +
13 + #ifdef F_SETLKW
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 22 | Line 41 | extern char    *strcpy(), *index();
41  
42   extern int      headismine;     /* boolean true if header belongs to me */
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 36 | 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 50 | 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 70 | Line 96 | int    lf;
96   persistfile(pfn)        /* open persist file and lock it */
97   char    *pfn;
98   {
99 <        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666);
99 >        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
100          if (persistfd >= 0) {
101                  persistfname = pfn;
102                  pflock(1);
# Line 87 | Line 113 | char   *pfn;
113   }
114  
115  
116 < int sig_noop() {}
116 > static int      got_io;
117  
118 + static int sig_io() { got_io++; }
119  
120 + static int sig_alrm() { quit(0); }
121 +
122 +
123   pfhold()                /* holding pattern for idle rendering process */
124   {
125 <        char    buf[128];
125 >        int     (*oldalrm)();
126 >        char    buf[512];
127          register int    n;
128                                  /* close input and output descriptors */
129          close(fileno(stdin));
99        fflush(stdout);
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)
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)
136 >        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
137                  goto createrr;
138 <        sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname);
139 <        if (lseek(persistfd, 0L, 0) < 0)
140 <                error(SYSTEM, "seek error on persist file in pfhold");
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 in pfhold");
145 >                error(SYSTEM, "error writing persist file");
146 >        lseek(persistfd, 0L, 0);
147                                  /* wait TIMELIM for someone to signal us */
148 <        signal(SIGIO, sig_noop);
148 >        got_io = 0;
149 >        signal(SIGIO, sig_io);
150 >        oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
151          alarm(TIMELIM);
152 <        pflock(0);
153 <        pause();
154 <        alarm(0);
152 >        pflock(0);                      /* unlock persist file for attach */
153 >        while (!got_io)
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);
173          outpname[0] = '\0';
129        clean_slate();          /* reset time and ray counters */
174          return;
175   createrr:
176          error(SYSTEM, "cannot create named pipes in pfhold");
# Line 135 | 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[4096], *pfin, *pfout;
187 <        int     pid;
188 <                                /* load and close persist file */
189 <        nr = read(persistfd, buf, sizeof(buf));
190 <        pfdetach();
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, "cannot read persist file");
201 <        buf[nr] = '\0';
202 <        if ((cp = index(buf, '\n')) == NULL)
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';
207 <        if ((pid = atoi(buf)) <= 0)
207 >        if ((pid = atoi(cp)) <= 0)
208                  goto formerr;
155        pfin = cp;
209          if ((cp = index(cp, '\n')) == NULL)
210                  goto formerr;
211 +        pfin = ++cp;
212 +        if ((cp = index(cp, '\n')) == NULL)
213 +                goto formerr;
214          *cp++ = '\0';
215          pfout = cp;
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 <                                /* wake up rendering process */
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 */
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 <                if (freopen(pfout, "r", stdin) == NULL)
238 <                        error(SYSTEM, "cannot open input pipe in io_process");
239 <        } else {                        /* child passes renderer input */
240 <                if (freopen(pfin, "w", stdout) == NULL)
241 <                        error(SYSTEM, "cannot open input 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 */
180 <        cp = buf; n = sizeof(buf);
181 <        while ((nr = read(fileno(stdin), cp, n)) > 0) {
182 <                nr += cp-buf;
183 <                if ((n = write(fileno(stdout), buf, nr)) <= 0)
184 <                        error(SYSTEM, "write error in io_process");
185 <                cp = buf;
186 <                while (n < nr)
187 <                        *cp++ = buf[n++];
188 <                n = sizeof(buf) - (cp-buf);
189 <        }
190 <        fclose(stdin);
191 <        fclose(stdout);
192 <        if (pid)                /* parent waits for child */
193 <                wait(0);
194 <        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   }
319 +
320 + #else
321 +
322 + pfclean() {}
323 +
324 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines