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