ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/persist.c
Revision: 2.26
Committed: Sat Feb 22 02:07:29 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.25: +92 -17 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.26 static const char RCSid[] = "$Id$";
3 greg 2.1 #endif
4     /*
5     * Routines for persistent rtrace and rpict processes.
6 greg 2.26 *
7     * External symbols declared in ray.h
8     */
9    
10     /* ====================================================================
11     * The Radiance Software License, Version 1.0
12     *
13     * Copyright (c) 1990 - 2002 The Regents of the University of California,
14     * through Lawrence Berkeley National Laboratory. All rights reserved.
15     *
16     * Redistribution and use in source and binary forms, with or without
17     * modification, are permitted provided that the following conditions
18     * are met:
19     *
20     * 1. Redistributions of source code must retain the above copyright
21     * notice, this list of conditions and the following disclaimer.
22     *
23     * 2. Redistributions in binary form must reproduce the above copyright
24     * notice, this list of conditions and the following disclaimer in
25     * the documentation and/or other materials provided with the
26     * distribution.
27     *
28     * 3. The end-user documentation included with the redistribution,
29     * if any, must include the following acknowledgment:
30     * "This product includes Radiance software
31     * (http://radsite.lbl.gov/)
32     * developed by the Lawrence Berkeley National Laboratory
33     * (http://www.lbl.gov/)."
34     * Alternately, this acknowledgment may appear in the software itself,
35     * if and wherever such third-party acknowledgments normally appear.
36     *
37     * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38     * and "The Regents of the University of California" must
39     * not be used to endorse or promote products derived from this
40     * software without prior written permission. For written
41     * permission, please contact [email protected].
42     *
43     * 5. Products derived from this software may not be called "Radiance",
44     * nor may "Radiance" appear in their name, without prior written
45     * permission of Lawrence Berkeley National Laboratory.
46     *
47     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50     * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58     * SUCH DAMAGE.
59     * ====================================================================
60     *
61     * This software consists of voluntary contributions made by many
62     * individuals on behalf of Lawrence Berkeley National Laboratory. For more
63     * information on Lawrence Berkeley National Laboratory, please see
64     * <http://www.lbl.gov/>.
65 greg 2.1 */
66    
67     #include "standard.h"
68 gregl 2.25 #include "random.h"
69 greg 2.6
70     #ifdef F_SETLKW
71 greg 2.1 #include "paths.h"
72 gregl 2.21 #include "selcall.h"
73 greg 2.1 #include <signal.h>
74     #include <sys/stat.h>
75    
76 greg 2.2 #ifndef TIMELIM
77     #define TIMELIM (8*3600) /* time limit for holding pattern */
78     #endif
79    
80 greg 2.26 #ifndef freebsd
81     #define mkfifo(fn,md) mknod(fn, S_IFIFO|(md), 0)
82     #endif
83    
84     extern void io_process();
85 greg 2.1
86     extern int headismine; /* boolean true if header belongs to me */
87    
88 greg 2.12 extern char *progname; /* global program name */
89    
90 greg 2.14 extern char *errfile; /* global error file name */
91    
92 greg 2.1 static char *persistfname = NULL; /* persist file name */
93     static int persistfd = -1; /* persist file descriptor */
94    
95 greg 2.14 static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
96 greg 2.1
97    
98 greg 2.26 void
99 greg 2.1 pfdetach() /* release persist (and header) resources */
100     {
101     if (persistfd >= 0)
102     close(persistfd);
103     persistfd = -1;
104     persistfname = NULL;
105     inpname[0] = '\0';
106     outpname[0] = '\0';
107 greg 2.14 errname[0] = '\0';
108 greg 2.1 headismine = 0;
109     }
110    
111    
112 greg 2.26 void
113 greg 2.1 pfclean() /* clean up persist files */
114     {
115     if (persistfd >= 0)
116     close(persistfd);
117     if (persistfname != NULL)
118     unlink(persistfname);
119     if (inpname[0])
120     unlink(inpname);
121     if (outpname[0])
122     unlink(outpname);
123 greg 2.14 if (errname[0])
124     unlink(errname);
125 greg 2.1 }
126    
127    
128 greg 2.26 void
129 greg 2.1 pflock(lf) /* place or release exclusive lock on file */
130     int lf;
131     {
132     struct flock fls;
133    
134     fls.l_type = lf ? F_WRLCK : F_UNLCK;
135     fls.l_whence = 0;
136     fls.l_start = 0L;
137     fls.l_len = 0L;
138     if (fcntl(persistfd, F_SETLKW, &fls) < 0)
139     error(SYSTEM, "cannot (un)lock persist file");
140     }
141    
142    
143     persistfile(pfn) /* open persist file and lock it */
144     char *pfn;
145     {
146 greg 2.13 persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
147 greg 2.1 if (persistfd >= 0) {
148     persistfname = pfn;
149     pflock(1);
150     return;
151     }
152     /* file exists -- switch to i/o process */
153     persistfd = open(pfn, O_RDWR);
154     if (persistfd < 0) {
155     sprintf(errmsg, "cannot open persist file \"%s\"", pfn);
156     error(SYSTEM, errmsg);
157     }
158     pflock(1);
159     io_process(); /* never returns */
160     }
161    
162    
163 greg 2.13 static int got_io;
164 greg 2.1
165 greg 2.26 static void sig_io() { got_io++; }
166 greg 2.1
167 greg 2.26 static void sig_alrm() { quit(0); }
168 greg 2.13
169    
170 greg 2.26 void
171 greg 2.1 pfhold() /* holding pattern for idle rendering process */
172     {
173 greg 2.26 void (*oldalrm)();
174 greg 2.12 char buf[512];
175 greg 2.1 register int n;
176     /* close input and output descriptors */
177     close(fileno(stdin));
178     close(fileno(stdout));
179 greg 2.16 if (errfile == NULL)
180     close(fileno(stderr));
181 greg 2.1 /* create named pipes for input and output */
182 greg 2.26 if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
183 greg 2.1 goto createrr;
184 greg 2.26 if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
185 greg 2.1 goto createrr;
186 greg 2.14 if (errfile == NULL &&
187 greg 2.26 mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0)
188 greg 2.14 goto createrr;
189     sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
190     inpname, outpname, errname);
191 greg 2.1 n = strlen(buf);
192     if (write(persistfd, buf, n) < n)
193 greg 2.10 error(SYSTEM, "error writing persist file");
194 greg 2.26 lseek(persistfd, (off_t)0L, 0);
195 greg 2.2 /* wait TIMELIM for someone to signal us */
196 greg 2.13 got_io = 0;
197     signal(SIGIO, sig_io);
198 greg 2.26 oldalrm = (void (*)())signal(SIGALRM, sig_alrm);
199 greg 2.2 alarm(TIMELIM);
200 greg 2.15 pflock(0); /* unlock persist file for attach */
201 greg 2.13 while (!got_io)
202 greg 2.15 pause(); /* wait for attach */
203     alarm(0); /* turn off alarm */
204 greg 2.13 signal(SIGALRM, oldalrm);
205 greg 2.2 signal(SIGIO, SIG_DFL);
206 greg 2.17 pflock(1); /* grab persist file back */
207 greg 2.1 /* someone wants us; reopen stdin and stdout */
208 greg 2.26 /*
209 greg 2.1 if (freopen(inpname, "r", stdin) == NULL)
210     goto openerr;
211     if (freopen(outpname, "w", stdout) == NULL)
212     goto openerr;
213 greg 2.26 */
214     close(0);
215     if (open(inpname, O_RDONLY) != 0)
216     error(INTERNAL, "unexpected stdin file number");
217     clearerr(stdin);
218     close(1);
219     if (open(outpname, O_WRONLY) != 1)
220     error(INTERNAL, "unexpected stdout file number");
221 gregl 2.22 sleep(3); /* give them a chance to open their pipes */
222 greg 2.14 if (errname[0]) {
223 greg 2.26 close(2);
224     if (open(errname, O_WRONLY) != 2)
225     error(INTERNAL, "unexpected stderr file number");
226 greg 2.14 unlink(errname);
227     errname[0] = '\0';
228     }
229 greg 2.1 unlink(inpname);
230     inpname[0] = '\0';
231     unlink(outpname);
232     outpname[0] = '\0';
233     return;
234     createrr:
235     error(SYSTEM, "cannot create named pipes in pfhold");
236     openerr:
237     error(SYSTEM, "cannot open named pipes in pfhold");
238     }
239    
240    
241 greg 2.26 void
242 greg 2.16 io_process() /* just act as go-between for actual process */
243 greg 2.1 {
244     register char *cp;
245     register int nr, n;
246 greg 2.16 char buf[BUFSIZ], *pfin, *pfout, *pferr;
247     int pid, nfds;
248 gregl 2.22 int fdout, fderr = -1;
249 gregl 2.23 int status = 0;
250 gregl 2.22 fd_set readfds, excepfds;
251 greg 2.15 /* load persist file */
252 greg 2.17 n = 40;
253 greg 2.15 while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
254 greg 2.17 if (!n--)
255     error(USER, "unattended persist file?");
256 greg 2.15 pflock(0);
257 gregl 2.25 sleep(3+(3*getpid()+random())%13); /* wait until ready */
258 greg 2.15 pflock(1);
259     }
260     if (nr < 0)
261 greg 2.14 error(SYSTEM, "error reading persist file");
262 greg 2.26 ftruncate(persistfd, (off_t)0L); /* truncate persist file */
263 greg 2.17 pfdetach(); /* close & release persist file */
264     buf[nr] = '\0'; /* parse what we got */
265 greg 2.12 if ((cp = index(buf, ' ')) == NULL)
266 greg 2.1 goto formerr;
267     *cp++ = '\0';
268 greg 2.12 if ((pid = atoi(cp)) <= 0)
269 greg 2.1 goto formerr;
270     if ((cp = index(cp, '\n')) == NULL)
271     goto formerr;
272 greg 2.12 pfin = ++cp;
273     if ((cp = index(cp, '\n')) == NULL)
274     goto formerr;
275 greg 2.1 *cp++ = '\0';
276     pfout = cp;
277     if ((cp = index(cp, '\n')) == NULL)
278     goto formerr;
279     *cp++ = '\0';
280 greg 2.14 pferr = cp;
281     if ((cp = index(cp, '\n')) == NULL)
282     goto formerr;
283     *cp++ = '\0';
284 greg 2.1 if (cp-buf != nr)
285     goto formerr;
286 greg 2.12 if (strcmp(buf, progname)) {
287     sprintf(errmsg, "persist file for %s, not %s", buf, progname);
288     error(USER, errmsg);
289     }
290 greg 2.16 /* wake up rendering process */
291 greg 2.2 if (kill(pid, SIGIO) < 0)
292 greg 2.1 error(SYSTEM, "cannot signal rendering process in io_process");
293 gregl 2.22 /* fork child feeder process */
294     pid = fork();
295     if (pid < 0)
296     error(SYSTEM, "fork failed in io_process");
297     if (pid == 0) { /* feeder loop */
298     int fdin;
299     close(1); /* open input pipe */
300     if ((fdin = open(pfin, O_WRONLY)) < 0)
301     error(SYSTEM, "cannot open feed pipe in io_process");
302     /* renderer stdin */
303     while ((nr = read(0, cp=buf, sizeof(buf))) > 0) {
304     do {
305     if ((n = write(fdin, cp, nr)) <= 0)
306     goto writerr;
307     cp += n;
308     } while ((nr -= n) > 0);
309     }
310     if (nr < 0)
311     goto readerr;
312     _exit(0);
313     }
314     close(0);
315     /* open output pipes, in order */
316 greg 2.16 if ((fdout = open(pfout, O_RDONLY)) < 0)
317     error(SYSTEM, "cannot open output pipe in io_process");
318     if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
319     error(SYSTEM, "cannot open error pipe in io_process");
320 gregl 2.22 for ( ; ; ) { /* eater loop */
321 greg 2.16 FD_ZERO(&readfds);
322     FD_ZERO(&excepfds);
323     nfds = 0;
324     if (fdout >= 0) {
325     FD_SET(fdout, &readfds);
326     FD_SET(fdout, &excepfds);
327     nfds = fdout+1;
328 greg 2.14 }
329 greg 2.16 if (fderr >= 0) {
330     FD_SET(fderr, &readfds);
331     FD_SET(fderr, &excepfds);
332     nfds = fderr+1;
333     }
334     if (nfds == 0)
335     break; /* all done, exit */
336 gregl 2.22 if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0)
337 greg 2.16 error(SYSTEM, "error in select call in io_process");
338     /* renderer stderr */
339     if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
340     FD_ISSET(fderr, &excepfds))) {
341     nr = read(fderr, cp=buf, sizeof(buf));
342     if (nr < 0)
343     goto readerr;
344     if (nr == 0) {
345     close(fderr);
346 greg 2.18 /* close(2); don't close stderr! */
347 greg 2.16 fderr = -1;
348 gregl 2.24 } else {
349 gregl 2.23 cp[nr] = '\0'; /* deduce status if we can */
350     n = strlen(progname);
351     if (!strncmp(cp, progname, n) &&
352     cp[n++] == ':' &&
353     cp[n++] == ' ') {
354     register struct erract *ep;
355     for (ep = erract; ep < erract+NERRS;
356     ep++)
357     if (ep->pre[0] &&
358     !strncmp(cp+n, ep->pre,
359     strlen(ep->pre))) {
360     status = ep->ec;
361     break;
362     }
363     }
364     do { /* write message */
365 greg 2.16 if ((n = write(2, cp, nr)) <= 0)
366     goto writerr;
367     cp += n;
368     } while ((nr -= n) > 0);
369 gregl 2.24 }
370 greg 2.16 }
371     /* renderer stdout */
372     if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
373     FD_ISSET(fdout, &excepfds))) {
374     nr = read(fdout, cp=buf, sizeof(buf));
375     if (nr < 0)
376     goto readerr;
377     if (nr == 0) { /* EOF */
378     close(fdout);
379     close(1);
380     fdout = -1;
381     } else
382     do { /* write it all */
383     if ((n = write(1, cp, nr)) <= 0)
384     goto writerr;
385     cp += n;
386     } while ((nr -= n) > 0);
387     }
388 greg 2.1 }
389 gregl 2.23 wait(0); /* wait for feeder process */
390     _exit(status);
391 greg 2.1 formerr:
392     error(USER, "format error in persist file");
393 greg 2.16 readerr:
394     error(SYSTEM, "read error in io_process");
395     writerr:
396     error(SYSTEM, "write error in io_process");
397 greg 2.1 }
398 greg 2.6
399 greg 2.8 #else
400    
401 greg 2.26 void pfclean() {}
402 greg 2.8
403 greg 2.6 #endif