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 (19 hours, 34 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

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.45 static const char RCSid[] = "$Id: persist.c,v 2.44 2016/03/06 01:13:18 schorsch Exp $";
3 greg 2.1 #endif
4     /*
5     * Routines for persistent rtrace and rpict processes.
6 greg 2.26 *
7     * External symbols declared in ray.h
8     */
9    
10 greg 2.27 #include "copyright.h"
11 greg 2.1
12 schorsch 2.29 #include <string.h>
13     #include <signal.h>
14     #include <sys/stat.h>
15 schorsch 2.38 #include <sys/types.h>
16 schorsch 2.40
17     #include "platform.h"
18 schorsch 2.39 #ifndef NON_POSIX /* XXX need abstraction for process management */
19     #include <sys/wait.h>
20     #endif
21 schorsch 2.29
22 schorsch 2.30 #include "rtprocess.h" /* getpid() */
23 greg 2.1 #include "standard.h"
24 gregl 2.25 #include "random.h"
25 schorsch 2.34 #include "ray.h"
26 greg 2.6
27     #ifdef F_SETLKW
28 gregl 2.21 #include "selcall.h"
29 greg 2.1
30 greg 2.2 #ifndef TIMELIM
31     #define TIMELIM (8*3600) /* time limit for holding pattern */
32 greg 2.26 #endif
33    
34 greg 2.1 extern int headismine; /* boolean true if header belongs to me */
35 greg 2.12 extern char *progname; /* global program name */
36 greg 2.14 extern char *errfile; /* global error file name */
37 greg 2.1 static char *persistfname = NULL; /* persist file name */
38     static int persistfd = -1; /* persist file descriptor */
39 schorsch 2.34 static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
40 greg 2.1
41 greg 2.42 typedef void (rsighandler_t)(int);
42     static rsighandler_t sig_io;
43     static rsighandler_t sig_alrm;
44 greg 2.1
45    
46 greg 2.43 void
47 schorsch 2.34 pfdetach(void) /* release persist (and header) resources */
48 greg 2.1 {
49     if (persistfd >= 0)
50     close(persistfd);
51     persistfd = -1;
52     persistfname = NULL;
53     inpname[0] = '\0';
54     outpname[0] = '\0';
55 greg 2.14 errname[0] = '\0';
56 greg 2.1 headismine = 0;
57     }
58    
59    
60 greg 2.43 void
61 schorsch 2.34 pfclean(void) /* clean up persist files */
62 greg 2.1 {
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 greg 2.14 if (errname[0])
72     unlink(errname);
73 greg 2.1 }
74    
75    
76 greg 2.43 void
77 schorsch 2.34 pflock( /* place or release exclusive lock on file */
78     int lf
79     )
80 greg 2.1 {
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 greg 2.43 void
93 schorsch 2.34 persistfile( /* open persist file and lock it */
94     char *pfn
95     )
96 greg 2.1 {
97 greg 2.13 persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
98 greg 2.1 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 greg 2.13 static int got_io;
115 greg 2.1
116 schorsch 2.34 static void sig_io(int i) { got_io++; }
117 greg 2.1
118 schorsch 2.34 static void sig_alrm(int i) { quit(0); }
119 greg 2.13
120    
121 greg 2.43 void
122 schorsch 2.34 pfhold(void) /* holding pattern for idle rendering process */
123 greg 2.1 {
124 greg 2.42 rsighandler_t *oldalrm;
125 greg 2.12 char buf[512];
126 greg 2.43 int n;
127 greg 2.1 /* close input and output descriptors */
128 greg 2.37 close(0);
129     close(1);
130 greg 2.16 if (errfile == NULL)
131 greg 2.37 close(2);
132 greg 2.1 /* create named pipes for input and output */
133 greg 2.26 if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
134 greg 2.1 goto createrr;
135 greg 2.26 if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
136 greg 2.1 goto createrr;
137 greg 2.14 if (errfile == NULL &&
138 greg 2.26 mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0)
139 greg 2.14 goto createrr;
140     sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
141     inpname, outpname, errname);
142 greg 2.1 n = strlen(buf);
143     if (write(persistfd, buf, n) < n)
144 greg 2.10 error(SYSTEM, "error writing persist file");
145 greg 2.32 lseek(persistfd, (off_t)0, SEEK_SET);
146 greg 2.2 /* wait TIMELIM for someone to signal us */
147 greg 2.13 got_io = 0;
148     signal(SIGIO, sig_io);
149 schorsch 2.34 oldalrm = signal(SIGALRM, sig_alrm);
150 greg 2.2 alarm(TIMELIM);
151 greg 2.15 pflock(0); /* unlock persist file for attach */
152 greg 2.13 while (!got_io)
153 greg 2.15 pause(); /* wait for attach */
154     alarm(0); /* turn off alarm */
155 greg 2.13 signal(SIGALRM, oldalrm);
156 greg 2.2 signal(SIGIO, SIG_DFL);
157 greg 2.17 pflock(1); /* grab persist file back */
158 greg 2.1 /* someone wants us; reopen stdin and stdout */
159 greg 2.26 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 gregl 2.22 sleep(3); /* give them a chance to open their pipes */
167 greg 2.14 if (errname[0]) {
168 greg 2.26 close(2);
169     if (open(errname, O_WRONLY) != 2)
170     error(INTERNAL, "unexpected stderr file number");
171 greg 2.14 unlink(errname);
172     errname[0] = '\0';
173     }
174 greg 2.1 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 greg 2.43 void
185 schorsch 2.34 io_process(void) /* just act as go-between for actual process */
186 greg 2.1 {
187 greg 2.43 char *cp;
188     int nr, n;
189 greg 2.16 char buf[BUFSIZ], *pfin, *pfout, *pferr;
190     int pid, nfds;
191 gregl 2.22 int fdout, fderr = -1;
192 gregl 2.23 int status = 0;
193 gregl 2.22 fd_set readfds, excepfds;
194 greg 2.15 /* load persist file */
195 greg 2.17 n = 40;
196 greg 2.15 while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
197 greg 2.17 if (!n--)
198     error(USER, "unattended persist file?");
199 greg 2.15 pflock(0);
200 gregl 2.25 sleep(3+(3*getpid()+random())%13); /* wait until ready */
201 greg 2.15 pflock(1);
202     }
203     if (nr < 0)
204 greg 2.14 error(SYSTEM, "error reading persist file");
205 greg 2.26 ftruncate(persistfd, (off_t)0L); /* truncate persist file */
206 greg 2.17 pfdetach(); /* close & release persist file */
207     buf[nr] = '\0'; /* parse what we got */
208 schorsch 2.29 if ((cp = strchr(buf, ' ')) == NULL)
209 greg 2.1 goto formerr;
210     *cp++ = '\0';
211 greg 2.12 if ((pid = atoi(cp)) <= 0)
212 greg 2.1 goto formerr;
213 schorsch 2.29 if ((cp = strchr(cp, '\n')) == NULL)
214 greg 2.1 goto formerr;
215 greg 2.12 pfin = ++cp;
216 schorsch 2.29 if ((cp = strchr(cp, '\n')) == NULL)
217 greg 2.12 goto formerr;
218 greg 2.1 *cp++ = '\0';
219     pfout = cp;
220 schorsch 2.29 if ((cp = strchr(cp, '\n')) == NULL)
221 greg 2.1 goto formerr;
222     *cp++ = '\0';
223 greg 2.14 pferr = cp;
224 schorsch 2.29 if ((cp = strchr(cp, '\n')) == NULL)
225 greg 2.14 goto formerr;
226     *cp++ = '\0';
227 greg 2.1 if (cp-buf != nr)
228     goto formerr;
229 greg 2.12 if (strcmp(buf, progname)) {
230     sprintf(errmsg, "persist file for %s, not %s", buf, progname);
231     error(USER, errmsg);
232     }
233 greg 2.16 /* wake up rendering process */
234 greg 2.2 if (kill(pid, SIGIO) < 0)
235 greg 2.1 error(SYSTEM, "cannot signal rendering process in io_process");
236 gregl 2.22 /* 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 greg 2.16 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 gregl 2.22 for ( ; ; ) { /* eater loop */
264 greg 2.16 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 greg 2.14 }
272 greg 2.16 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 gregl 2.22 if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0)
280 greg 2.16 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 greg 2.18 /* close(2); don't close stderr! */
290 greg 2.16 fderr = -1;
291 gregl 2.24 } else {
292 gregl 2.23 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 greg 2.43 struct erract *ep;
298 gregl 2.23 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 greg 2.16 if ((n = write(2, cp, nr)) <= 0)
309     goto writerr;
310     cp += n;
311     } while ((nr -= n) > 0);
312 gregl 2.24 }
313 greg 2.16 }
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 greg 2.1 }
332 greg 2.36 kill(pid, SIGTERM); /* no more process to feed, so... */
333 greg 2.35 waitpid(pid, 0, 0); /* wait for feeder process */
334 gregl 2.23 _exit(status);
335 greg 2.1 formerr:
336     error(USER, "format error in persist file");
337 greg 2.16 readerr:
338     error(SYSTEM, "read error in io_process");
339     writerr:
340     error(SYSTEM, "write error in io_process");
341 greg 2.1 }
342 greg 2.6
343 greg 2.8 #else
344    
345 greg 2.43 void pfclean(void) {}
346 greg 2.8
347 greg 2.6 #endif