ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/persist.c
Revision: 2.17
Committed: Fri Jul 19 10:16:00 1996 UTC (28 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.16: +8 -5 lines
Log Message:
final(?) fix to process synchronization problems

File Contents

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