ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/persist.c
Revision: 2.32
Committed: Mon Oct 20 16:01:55 2003 UTC (20 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.31: +3 -2 lines
Log Message:
Included "platform.h" wherever lseek() was called

File Contents

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