ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/persist.c
Revision: 2.27
Committed: Tue Feb 25 02:47:23 2003 UTC (21 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.26: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

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 greg 2.27 #include "copyright.h"
11 greg 2.1
12     #include "standard.h"
13 gregl 2.25 #include "random.h"
14 greg 2.6
15     #ifdef F_SETLKW
16 greg 2.1 #include "paths.h"
17 gregl 2.21 #include "selcall.h"
18 greg 2.1 #include <signal.h>
19     #include <sys/stat.h>
20    
21 greg 2.2 #ifndef TIMELIM
22     #define TIMELIM (8*3600) /* time limit for holding pattern */
23     #endif
24    
25 greg 2.26 #ifndef freebsd
26     #define mkfifo(fn,md) mknod(fn, S_IFIFO|(md), 0)
27     #endif
28    
29     extern void io_process();
30 greg 2.1
31     extern int headismine; /* boolean true if header belongs to me */
32    
33 greg 2.12 extern char *progname; /* global program name */
34    
35 greg 2.14 extern char *errfile; /* global error file name */
36    
37 greg 2.1 static char *persistfname = NULL; /* persist file name */
38     static int persistfd = -1; /* persist file descriptor */
39    
40 greg 2.14 static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
41 greg 2.1
42    
43 greg 2.26 void
44 greg 2.1 pfdetach() /* release persist (and header) resources */
45     {
46     if (persistfd >= 0)
47     close(persistfd);
48     persistfd = -1;
49     persistfname = NULL;
50     inpname[0] = '\0';
51     outpname[0] = '\0';
52 greg 2.14 errname[0] = '\0';
53 greg 2.1 headismine = 0;
54     }
55    
56    
57 greg 2.26 void
58 greg 2.1 pfclean() /* clean up persist files */
59     {
60     if (persistfd >= 0)
61     close(persistfd);
62     if (persistfname != NULL)
63     unlink(persistfname);
64     if (inpname[0])
65     unlink(inpname);
66     if (outpname[0])
67     unlink(outpname);
68 greg 2.14 if (errname[0])
69     unlink(errname);
70 greg 2.1 }
71    
72    
73 greg 2.26 void
74 greg 2.1 pflock(lf) /* place or release exclusive lock on file */
75     int lf;
76     {
77     struct flock fls;
78    
79     fls.l_type = lf ? F_WRLCK : F_UNLCK;
80     fls.l_whence = 0;
81     fls.l_start = 0L;
82     fls.l_len = 0L;
83     if (fcntl(persistfd, F_SETLKW, &fls) < 0)
84     error(SYSTEM, "cannot (un)lock persist file");
85     }
86    
87    
88     persistfile(pfn) /* open persist file and lock it */
89     char *pfn;
90     {
91 greg 2.13 persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
92 greg 2.1 if (persistfd >= 0) {
93     persistfname = pfn;
94     pflock(1);
95     return;
96     }
97     /* file exists -- switch to i/o process */
98     persistfd = open(pfn, O_RDWR);
99     if (persistfd < 0) {
100     sprintf(errmsg, "cannot open persist file \"%s\"", pfn);
101     error(SYSTEM, errmsg);
102     }
103     pflock(1);
104     io_process(); /* never returns */
105     }
106    
107    
108 greg 2.13 static int got_io;
109 greg 2.1
110 greg 2.26 static void sig_io() { got_io++; }
111 greg 2.1
112 greg 2.26 static void sig_alrm() { quit(0); }
113 greg 2.13
114    
115 greg 2.26 void
116 greg 2.1 pfhold() /* holding pattern for idle rendering process */
117     {
118 greg 2.26 void (*oldalrm)();
119 greg 2.12 char buf[512];
120 greg 2.1 register int n;
121     /* close input and output descriptors */
122     close(fileno(stdin));
123     close(fileno(stdout));
124 greg 2.16 if (errfile == NULL)
125     close(fileno(stderr));
126 greg 2.1 /* create named pipes for input and output */
127 greg 2.26 if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
128 greg 2.1 goto createrr;
129 greg 2.26 if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
130 greg 2.1 goto createrr;
131 greg 2.14 if (errfile == NULL &&
132 greg 2.26 mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0)
133 greg 2.14 goto createrr;
134     sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
135     inpname, outpname, errname);
136 greg 2.1 n = strlen(buf);
137     if (write(persistfd, buf, n) < n)
138 greg 2.10 error(SYSTEM, "error writing persist file");
139 greg 2.26 lseek(persistfd, (off_t)0L, 0);
140 greg 2.2 /* wait TIMELIM for someone to signal us */
141 greg 2.13 got_io = 0;
142     signal(SIGIO, sig_io);
143 greg 2.26 oldalrm = (void (*)())signal(SIGALRM, sig_alrm);
144 greg 2.2 alarm(TIMELIM);
145 greg 2.15 pflock(0); /* unlock persist file for attach */
146 greg 2.13 while (!got_io)
147 greg 2.15 pause(); /* wait for attach */
148     alarm(0); /* turn off alarm */
149 greg 2.13 signal(SIGALRM, oldalrm);
150 greg 2.2 signal(SIGIO, SIG_DFL);
151 greg 2.17 pflock(1); /* grab persist file back */
152 greg 2.1 /* someone wants us; reopen stdin and stdout */
153 greg 2.26 /*
154 greg 2.1 if (freopen(inpname, "r", stdin) == NULL)
155     goto openerr;
156     if (freopen(outpname, "w", stdout) == NULL)
157     goto openerr;
158 greg 2.26 */
159     close(0);
160     if (open(inpname, O_RDONLY) != 0)
161     error(INTERNAL, "unexpected stdin file number");
162     clearerr(stdin);
163     close(1);
164     if (open(outpname, O_WRONLY) != 1)
165     error(INTERNAL, "unexpected stdout file number");
166 gregl 2.22 sleep(3); /* give them a chance to open their pipes */
167 greg 2.14 if (errname[0]) {
168 greg 2.26 close(2);
169     if (open(errname, O_WRONLY) != 2)
170     error(INTERNAL, "unexpected stderr file number");
171 greg 2.14 unlink(errname);
172     errname[0] = '\0';
173     }
174 greg 2.1 unlink(inpname);
175     inpname[0] = '\0';
176     unlink(outpname);
177     outpname[0] = '\0';
178     return;
179     createrr:
180     error(SYSTEM, "cannot create named pipes in pfhold");
181     openerr:
182     error(SYSTEM, "cannot open named pipes in pfhold");
183     }
184    
185    
186 greg 2.26 void
187 greg 2.16 io_process() /* just act as go-between for actual process */
188 greg 2.1 {
189     register char *cp;
190     register int nr, n;
191 greg 2.16 char buf[BUFSIZ], *pfin, *pfout, *pferr;
192     int pid, nfds;
193 gregl 2.22 int fdout, fderr = -1;
194 gregl 2.23 int status = 0;
195 gregl 2.22 fd_set readfds, excepfds;
196 greg 2.15 /* load persist file */
197 greg 2.17 n = 40;
198 greg 2.15 while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
199 greg 2.17 if (!n--)
200     error(USER, "unattended persist file?");
201 greg 2.15 pflock(0);
202 gregl 2.25 sleep(3+(3*getpid()+random())%13); /* wait until ready */
203 greg 2.15 pflock(1);
204     }
205     if (nr < 0)
206 greg 2.14 error(SYSTEM, "error reading persist file");
207 greg 2.26 ftruncate(persistfd, (off_t)0L); /* truncate persist file */
208 greg 2.17 pfdetach(); /* close & release persist file */
209     buf[nr] = '\0'; /* parse what we got */
210 greg 2.12 if ((cp = index(buf, ' ')) == NULL)
211 greg 2.1 goto formerr;
212     *cp++ = '\0';
213 greg 2.12 if ((pid = atoi(cp)) <= 0)
214 greg 2.1 goto formerr;
215     if ((cp = index(cp, '\n')) == NULL)
216     goto formerr;
217 greg 2.12 pfin = ++cp;
218     if ((cp = index(cp, '\n')) == NULL)
219     goto formerr;
220 greg 2.1 *cp++ = '\0';
221     pfout = cp;
222     if ((cp = index(cp, '\n')) == NULL)
223     goto formerr;
224     *cp++ = '\0';
225 greg 2.14 pferr = cp;
226     if ((cp = index(cp, '\n')) == NULL)
227     goto formerr;
228     *cp++ = '\0';
229 greg 2.1 if (cp-buf != nr)
230     goto formerr;
231 greg 2.12 if (strcmp(buf, progname)) {
232     sprintf(errmsg, "persist file for %s, not %s", buf, progname);
233     error(USER, errmsg);
234     }
235 greg 2.16 /* wake up rendering process */
236 greg 2.2 if (kill(pid, SIGIO) < 0)
237 greg 2.1 error(SYSTEM, "cannot signal rendering process in io_process");
238 gregl 2.22 /* fork child feeder process */
239     pid = fork();
240     if (pid < 0)
241     error(SYSTEM, "fork failed in io_process");
242     if (pid == 0) { /* feeder loop */
243     int fdin;
244     close(1); /* open input pipe */
245     if ((fdin = open(pfin, O_WRONLY)) < 0)
246     error(SYSTEM, "cannot open feed pipe in io_process");
247     /* renderer stdin */
248     while ((nr = read(0, cp=buf, sizeof(buf))) > 0) {
249     do {
250     if ((n = write(fdin, cp, nr)) <= 0)
251     goto writerr;
252     cp += n;
253     } while ((nr -= n) > 0);
254     }
255     if (nr < 0)
256     goto readerr;
257     _exit(0);
258     }
259     close(0);
260     /* open output pipes, in order */
261 greg 2.16 if ((fdout = open(pfout, O_RDONLY)) < 0)
262     error(SYSTEM, "cannot open output pipe in io_process");
263     if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
264     error(SYSTEM, "cannot open error pipe in io_process");
265 gregl 2.22 for ( ; ; ) { /* eater loop */
266 greg 2.16 FD_ZERO(&readfds);
267     FD_ZERO(&excepfds);
268     nfds = 0;
269     if (fdout >= 0) {
270     FD_SET(fdout, &readfds);
271     FD_SET(fdout, &excepfds);
272     nfds = fdout+1;
273 greg 2.14 }
274 greg 2.16 if (fderr >= 0) {
275     FD_SET(fderr, &readfds);
276     FD_SET(fderr, &excepfds);
277     nfds = fderr+1;
278     }
279     if (nfds == 0)
280     break; /* all done, exit */
281 gregl 2.22 if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0)
282 greg 2.16 error(SYSTEM, "error in select call in io_process");
283     /* renderer stderr */
284     if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
285     FD_ISSET(fderr, &excepfds))) {
286     nr = read(fderr, cp=buf, sizeof(buf));
287     if (nr < 0)
288     goto readerr;
289     if (nr == 0) {
290     close(fderr);
291 greg 2.18 /* close(2); don't close stderr! */
292 greg 2.16 fderr = -1;
293 gregl 2.24 } else {
294 gregl 2.23 cp[nr] = '\0'; /* deduce status if we can */
295     n = strlen(progname);
296     if (!strncmp(cp, progname, n) &&
297     cp[n++] == ':' &&
298     cp[n++] == ' ') {
299     register struct erract *ep;
300     for (ep = erract; ep < erract+NERRS;
301     ep++)
302     if (ep->pre[0] &&
303     !strncmp(cp+n, ep->pre,
304     strlen(ep->pre))) {
305     status = ep->ec;
306     break;
307     }
308     }
309     do { /* write message */
310 greg 2.16 if ((n = write(2, cp, nr)) <= 0)
311     goto writerr;
312     cp += n;
313     } while ((nr -= n) > 0);
314 gregl 2.24 }
315 greg 2.16 }
316     /* renderer stdout */
317     if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
318     FD_ISSET(fdout, &excepfds))) {
319     nr = read(fdout, cp=buf, sizeof(buf));
320     if (nr < 0)
321     goto readerr;
322     if (nr == 0) { /* EOF */
323     close(fdout);
324     close(1);
325     fdout = -1;
326     } else
327     do { /* write it all */
328     if ((n = write(1, cp, nr)) <= 0)
329     goto writerr;
330     cp += n;
331     } while ((nr -= n) > 0);
332     }
333 greg 2.1 }
334 gregl 2.23 wait(0); /* wait for feeder process */
335     _exit(status);
336 greg 2.1 formerr:
337     error(USER, "format error in persist file");
338 greg 2.16 readerr:
339     error(SYSTEM, "read error in io_process");
340     writerr:
341     error(SYSTEM, "write error in io_process");
342 greg 2.1 }
343 greg 2.6
344 greg 2.8 #else
345    
346 greg 2.26 void pfclean() {}
347 greg 2.8
348 greg 2.6 #endif