ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/persist.c
Revision: 2.31
Committed: Sat Aug 30 09:03:31 2003 UTC (20 years, 8 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.30: +1 -5 lines
Log Message:
Eliminated obsolete "freebsd" dependency.

File Contents

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