ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/persist.c
Revision: 2.39
Committed: Mon Apr 21 07:31:30 2008 UTC (16 years ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.38: +4 -2 lines
Log Message:
Scons update for mingw.

File Contents

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