ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/persist.c
Revision: 2.45
Committed: Sat Jun 7 05:09:46 2025 UTC (13 hours, 37 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.44: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: persist.c,v 2.44 2016/03/06 01:13:18 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 "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 (rsighandler_t)(int);
42 static rsighandler_t sig_io;
43 static rsighandler_t sig_alrm;
44
45
46 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 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 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 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 void
122 pfhold(void) /* holding pattern for idle rendering process */
123 {
124 rsighandler_t *oldalrm;
125 char buf[512];
126 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 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 }
182
183
184 void
185 io_process(void) /* just act as go-between for actual process */
186 {
187 char *cp;
188 int nr, n;
189 char buf[BUFSIZ], *pfin, *pfout, *pferr;
190 int pid, nfds;
191 int fdout, fderr = -1;
192 int status = 0;
193 fd_set readfds, excepfds;
194 /* load persist file */
195 n = 40;
196 while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
197 if (!n--)
198 error(USER, "unattended persist file?");
199 pflock(0);
200 sleep(3+(3*getpid()+random())%13); /* wait until ready */
201 pflock(1);
202 }
203 if (nr < 0)
204 error(SYSTEM, "error reading persist file");
205 ftruncate(persistfd, (off_t)0L); /* truncate persist file */
206 pfdetach(); /* close & release persist file */
207 buf[nr] = '\0'; /* parse what we got */
208 if ((cp = strchr(buf, ' ')) == NULL)
209 goto formerr;
210 *cp++ = '\0';
211 if ((pid = atoi(cp)) <= 0)
212 goto formerr;
213 if ((cp = strchr(cp, '\n')) == NULL)
214 goto formerr;
215 pfin = ++cp;
216 if ((cp = strchr(cp, '\n')) == NULL)
217 goto formerr;
218 *cp++ = '\0';
219 pfout = cp;
220 if ((cp = strchr(cp, '\n')) == NULL)
221 goto formerr;
222 *cp++ = '\0';
223 pferr = cp;
224 if ((cp = strchr(cp, '\n')) == NULL)
225 goto formerr;
226 *cp++ = '\0';
227 if (cp-buf != nr)
228 goto formerr;
229 if (strcmp(buf, progname)) {
230 sprintf(errmsg, "persist file for %s, not %s", buf, progname);
231 error(USER, errmsg);
232 }
233 /* wake up rendering process */
234 if (kill(pid, SIGIO) < 0)
235 error(SYSTEM, "cannot signal rendering process in io_process");
236 /* fork child feeder process */
237 pid = fork();
238 if (pid < 0)
239 error(SYSTEM, "fork failed in io_process");
240 if (pid == 0) { /* feeder loop */
241 int fdin;
242 close(1); /* open input pipe */
243 if ((fdin = open(pfin, O_WRONLY)) < 0)
244 error(SYSTEM, "cannot open feed pipe in io_process");
245 /* renderer stdin */
246 while ((nr = read(0, cp=buf, sizeof(buf))) > 0) {
247 do {
248 if ((n = write(fdin, cp, nr)) <= 0)
249 goto writerr;
250 cp += n;
251 } while ((nr -= n) > 0);
252 }
253 if (nr < 0)
254 goto readerr;
255 _exit(0);
256 }
257 close(0);
258 /* open output pipes, in order */
259 if ((fdout = open(pfout, O_RDONLY)) < 0)
260 error(SYSTEM, "cannot open output pipe in io_process");
261 if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
262 error(SYSTEM, "cannot open error pipe in io_process");
263 for ( ; ; ) { /* eater loop */
264 FD_ZERO(&readfds);
265 FD_ZERO(&excepfds);
266 nfds = 0;
267 if (fdout >= 0) {
268 FD_SET(fdout, &readfds);
269 FD_SET(fdout, &excepfds);
270 nfds = fdout+1;
271 }
272 if (fderr >= 0) {
273 FD_SET(fderr, &readfds);
274 FD_SET(fderr, &excepfds);
275 nfds = fderr+1;
276 }
277 if (nfds == 0)
278 break; /* all done, exit */
279 if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0)
280 error(SYSTEM, "error in select call in io_process");
281 /* renderer stderr */
282 if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
283 FD_ISSET(fderr, &excepfds))) {
284 nr = read(fderr, cp=buf, sizeof(buf));
285 if (nr < 0)
286 goto readerr;
287 if (nr == 0) {
288 close(fderr);
289 /* close(2); don't close stderr! */
290 fderr = -1;
291 } else {
292 cp[nr] = '\0'; /* deduce status if we can */
293 n = strlen(progname);
294 if (!strncmp(cp, progname, n) &&
295 cp[n++] == ':' &&
296 cp[n++] == ' ') {
297 struct erract *ep;
298 for (ep = erract; ep < erract+NERRS;
299 ep++)
300 if (ep->pre[0] &&
301 !strncmp(cp+n, ep->pre,
302 strlen(ep->pre))) {
303 status = ep->ec;
304 break;
305 }
306 }
307 do { /* write message */
308 if ((n = write(2, cp, nr)) <= 0)
309 goto writerr;
310 cp += n;
311 } while ((nr -= n) > 0);
312 }
313 }
314 /* renderer stdout */
315 if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
316 FD_ISSET(fdout, &excepfds))) {
317 nr = read(fdout, cp=buf, sizeof(buf));
318 if (nr < 0)
319 goto readerr;
320 if (nr == 0) { /* EOF */
321 close(fdout);
322 close(1);
323 fdout = -1;
324 } else
325 do { /* write it all */
326 if ((n = write(1, cp, nr)) <= 0)
327 goto writerr;
328 cp += n;
329 } while ((nr -= n) > 0);
330 }
331 }
332 kill(pid, SIGTERM); /* no more process to feed, so... */
333 waitpid(pid, 0, 0); /* wait for feeder process */
334 _exit(status);
335 formerr:
336 error(USER, "format error in persist file");
337 readerr:
338 error(SYSTEM, "read error in io_process");
339 writerr:
340 error(SYSTEM, "write error in io_process");
341 }
342
343 #else
344
345 void pfclean(void) {}
346
347 #endif