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.23 by gregl, Tue Nov 11 20:29:25 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 11 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11   #include "standard.h"
12  
13   #ifdef F_SETLKW
14
14   #include "paths.h"
15 + #include "selcall.h"
16   #include <signal.h>
17 #include <sys/types.h>
17   #include <sys/stat.h>
18  
19   #ifndef TIMELIM
# Line 25 | Line 24 | extern char    *strcpy(), *index();
24  
25   extern int      headismine;     /* boolean true if header belongs to me */
26  
27 + extern char     *progname;      /* global program name */
28 +
29 + extern char     *errfile;       /* global error file name */
30 +
31   static char     *persistfname = NULL;   /* persist file name */
32   static int      persistfd = -1;         /* persist file descriptor */
33  
34 < static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1];
34 > static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
35  
36  
37   pfdetach()              /* release persist (and header) resources */
# Line 39 | Line 42 | pfdetach()             /* release persist (and header) resources
42          persistfname = NULL;
43          inpname[0] = '\0';
44          outpname[0] = '\0';
45 +        errname[0] = '\0';
46          headismine = 0;
47   }
48  
# Line 53 | Line 57 | pfclean()              /* clean up persist files */
57                  unlink(inpname);
58          if (outpname[0])
59                  unlink(outpname);
60 +        if (errname[0])
61 +                unlink(errname);
62   }
63  
64  
# Line 73 | Line 79 | int    lf;
79   persistfile(pfn)        /* open persist file and lock it */
80   char    *pfn;
81   {
82 <        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666);
82 >        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
83          if (persistfd >= 0) {
84                  persistfname = pfn;
85                  pflock(1);
# Line 90 | Line 96 | char   *pfn;
96   }
97  
98  
99 < int sig_noop() {}
99 > static int      got_io;
100  
101 + static int sig_io() { got_io++; }
102  
103 + static int sig_alrm() { quit(0); }
104 +
105 +
106   pfhold()                /* holding pattern for idle rendering process */
107   {
108 <        char    buf[128];
108 >        int     (*oldalrm)();
109 >        char    buf[512];
110          register int    n;
111                                  /* close input and output descriptors */
112          close(fileno(stdin));
113          close(fileno(stdout));
114 +        if (errfile == NULL)
115 +                close(fileno(stderr));
116                                  /* create named pipes for input and output */
117 <        if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600) < 0)
117 >        if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
118                  goto createrr;
119 <        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0)
119 >        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
120                  goto createrr;
121 <        sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname);
122 <        if (lseek(persistfd, 0L, 0) < 0)
123 <                error(SYSTEM, "seek error on persist file in pfhold");
121 >        if (errfile == NULL &&
122 >                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
123 >                goto createrr;
124 >        sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
125 >                        inpname, outpname, errname);
126          n = strlen(buf);
127          if (write(persistfd, buf, n) < n)
128 <                error(SYSTEM, "error writing persist file in pfhold");
128 >                error(SYSTEM, "error writing persist file");
129 >        lseek(persistfd, 0L, 0);
130                                  /* wait TIMELIM for someone to signal us */
131 <        signal(SIGIO, sig_noop);
131 >        got_io = 0;
132 >        signal(SIGIO, sig_io);
133 >        oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
134          alarm(TIMELIM);
135 <        pflock(0);
136 <        pause();
137 <        alarm(0);
135 >        pflock(0);                      /* unlock persist file for attach */
136 >        while (!got_io)
137 >                pause();                /* wait for attach */
138 >        alarm(0);                       /* turn off alarm */
139 >        signal(SIGALRM, oldalrm);
140          signal(SIGIO, SIG_DFL);
141 <        pflock(1);
141 >        pflock(1);                      /* grab persist file back */
142                                  /* someone wants us; reopen stdin and stdout */
143          if (freopen(inpname, "r", stdin) == NULL)
144                  goto openerr;
145          if (freopen(outpname, "w", stdout) == NULL)
146                  goto openerr;
147 +        sleep(3);               /* give them a chance to open their pipes */
148 +        if (errname[0]) {
149 +                if (freopen(errname, "w", stderr) == NULL)
150 +                        goto openerr;
151 +                unlink(errname);
152 +                errname[0] = '\0';
153 +        }
154          unlink(inpname);
155          inpname[0] = '\0';
156          unlink(outpname);
# Line 136 | Line 163 | openerr:
163   }
164  
165  
166 < io_process()            /* just act as conduits to and from actual process */
166 > io_process()            /* just act as go-between for actual process */
167   {
168          register char   *cp;
169          register int    nr, n;
170 <        char    buf[4096], *pfin, *pfout;
171 <        int     pid;
172 <                                /* load and close persist file */
173 <        nr = read(persistfd, buf, sizeof(buf));
174 <        pfdetach();
170 >        char    buf[BUFSIZ], *pfin, *pfout, *pferr;
171 >        int     pid, nfds;
172 >        int     fdout, fderr = -1;
173 >        int     status = 0;
174 >        fd_set  readfds, excepfds;
175 >                                        /* load persist file */
176 >        n = 40;
177 >        while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
178 >                if (!n--)
179 >                        error(USER, "unattended persist file?");
180 >                pflock(0);
181 >                sleep(15);              /* wait until ready */
182 >                pflock(1);
183 >        }
184          if (nr < 0)
185 <                error(SYSTEM, "cannot read persist file");
186 <        buf[nr] = '\0';
187 <        if ((cp = index(buf, '\n')) == NULL)
185 >                error(SYSTEM, "error reading persist file");
186 >        ftruncate(persistfd, 0L);       /* truncate persist file */
187 >        pfdetach();                     /* close & release persist file */
188 >        buf[nr] = '\0';                 /* parse what we got */
189 >        if ((cp = index(buf, ' ')) == NULL)
190                  goto formerr;
191          *cp++ = '\0';
192 <        if ((pid = atoi(buf)) <= 0)
192 >        if ((pid = atoi(cp)) <= 0)
193                  goto formerr;
156        pfin = cp;
194          if ((cp = index(cp, '\n')) == NULL)
195                  goto formerr;
196 +        pfin = ++cp;
197 +        if ((cp = index(cp, '\n')) == NULL)
198 +                goto formerr;
199          *cp++ = '\0';
200          pfout = cp;
201          if ((cp = index(cp, '\n')) == NULL)
202                  goto formerr;
203          *cp++ = '\0';
204 +        pferr = cp;
205 +        if ((cp = index(cp, '\n')) == NULL)
206 +                goto formerr;
207 +        *cp++ = '\0';
208          if (cp-buf != nr)
209                  goto formerr;
210 <                                /* wake up rendering process */
210 >        if (strcmp(buf, progname)) {
211 >                sprintf(errmsg, "persist file for %s, not %s", buf, progname);
212 >                error(USER, errmsg);
213 >        }
214 >                                        /* wake up rendering process */
215          if (kill(pid, SIGIO) < 0)
216                  error(SYSTEM, "cannot signal rendering process in io_process");
217 <        pid = fork();           /* fork i/o process */
217 >                                        /* fork child feeder process */
218 >        pid = fork();
219          if (pid < 0)
220                  error(SYSTEM, "fork failed in io_process");
221 <                                /* connect to appropriate pipe */
222 <        if (pid) {                      /* parent passes renderer output */
223 <                if (freopen(pfout, "r", stdin) == NULL)
224 <                        error(SYSTEM, "cannot open input pipe in io_process");
225 <        } else {                        /* child passes renderer input */
226 <                if (freopen(pfin, "w", stdout) == NULL)
227 <                        error(SYSTEM, "cannot open input pipe in io_process");
221 >        if (pid == 0) {                 /* feeder loop */
222 >                int     fdin;
223 >                close(1);               /* open input pipe */
224 >                if ((fdin = open(pfin, O_WRONLY)) < 0)
225 >                        error(SYSTEM, "cannot open feed pipe in io_process");
226 >                                                /* renderer stdin */
227 >                while ((nr = read(0, cp=buf, sizeof(buf))) > 0) {
228 >                        do {
229 >                                if ((n = write(fdin, cp, nr)) <= 0)
230 >                                        goto writerr;
231 >                                cp += n;
232 >                        } while ((nr -= n) > 0);
233 >                }
234 >                if (nr < 0)
235 >                        goto readerr;
236 >                _exit(0);
237          }
238 <                                /* pass input to output */
239 <        cp = buf; n = sizeof(buf);
240 <        while ((nr = read(fileno(stdin), cp, n)) > 0) {
241 <                nr += cp-buf;
242 <                if ((n = write(fileno(stdout), buf, nr)) <= 0)
243 <                        error(SYSTEM, "write error in io_process");
244 <                cp = buf;
245 <                while (n < nr)
246 <                        *cp++ = buf[n++];
247 <                n = sizeof(buf) - (cp-buf);
238 >        close(0);
239 >                                        /* open output pipes, in order */
240 >        if ((fdout = open(pfout, O_RDONLY)) < 0)
241 >                error(SYSTEM, "cannot open output pipe in io_process");
242 >        if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
243 >                error(SYSTEM, "cannot open error pipe in io_process");
244 >        for ( ; ; ) {                   /* eater loop */
245 >                FD_ZERO(&readfds);
246 >                FD_ZERO(&excepfds);
247 >                nfds = 0;
248 >                if (fdout >= 0) {
249 >                        FD_SET(fdout, &readfds);
250 >                        FD_SET(fdout, &excepfds);
251 >                        nfds = fdout+1;
252 >                }
253 >                if (fderr >= 0) {
254 >                        FD_SET(fderr, &readfds);
255 >                        FD_SET(fderr, &excepfds);
256 >                        nfds = fderr+1;
257 >                }
258 >                if (nfds == 0)
259 >                        break;                  /* all done, exit */
260 >                if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0)
261 >                        error(SYSTEM, "error in select call in io_process");
262 >                                                /* renderer stderr */
263 >                if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
264 >                                FD_ISSET(fderr, &excepfds))) {
265 >                        nr = read(fderr, cp=buf, sizeof(buf));
266 >                        if (nr < 0)
267 >                                goto readerr;
268 >                        if (nr == 0) {
269 >                                close(fderr);
270 >                                /* close(2);    don't close stderr! */
271 >                                fderr = -1;
272 >                        } else
273 >                                cp[nr] = '\0';  /* deduce status if we can */
274 >                                n = strlen(progname);
275 >                                if (!strncmp(cp, progname, n) &&
276 >                                                cp[n++] == ':' &&
277 >                                                cp[n++] == ' ') {
278 >                                        register struct erract  *ep;
279 >                                        for (ep = erract; ep < erract+NERRS;
280 >                                                        ep++)
281 >                                                if (ep->pre[0] &&
282 >                                                        !strncmp(cp+n, ep->pre,
283 >                                                            strlen(ep->pre))) {
284 >                                                        status = ep->ec;
285 >                                                        break;
286 >                                                }
287 >                                }
288 >                                do {            /* write message */
289 >                                        if ((n = write(2, cp, nr)) <= 0)
290 >                                                goto writerr;
291 >                                        cp += n;
292 >                                } while ((nr -= n) > 0);
293 >                }
294 >                                                /* renderer stdout */
295 >                if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
296 >                                FD_ISSET(fdout, &excepfds))) {
297 >                        nr = read(fdout, cp=buf, sizeof(buf));
298 >                        if (nr < 0)
299 >                                goto readerr;
300 >                        if (nr == 0) {          /* EOF */
301 >                                close(fdout);
302 >                                close(1);
303 >                                fdout = -1;
304 >                        } else
305 >                                do {            /* write it all */
306 >                                        if ((n = write(1, cp, nr)) <= 0)
307 >                                                goto writerr;
308 >                                        cp += n;
309 >                                } while ((nr -= n) > 0);
310 >                }
311          }
312 <        fclose(stdin);
313 <        fclose(stdout);
193 <        if (pid)                /* parent waits for child */
194 <                wait(0);
195 <        exit(0);                /* all done, exit (not quit!) */
312 >        wait(0);                /* wait for feeder process */
313 >        _exit(status);
314   formerr:
315          error(USER, "format error in persist file");
316 + readerr:
317 +        error(SYSTEM, "read error in io_process");
318 + writerr:
319 +        error(SYSTEM, "write error in io_process");
320   }
321 +
322 + #else
323 +
324 + pfclean() {}
325  
326   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines