ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/persist.c
Revision: 2.40
Committed: Sun Apr 27 17:22:49 2008 UTC (16 years ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R9
Changes since 2.39: +3 -2 lines
Log Message:
Yet some mingw related changes.

File Contents

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