1 |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Routines for persistent rtrace and rpict processes. |
9 |
*/ |
10 |
|
11 |
#include "standard.h" |
12 |
|
13 |
#ifdef F_SETLKW |
14 |
#include "paths.h" |
15 |
#include <signal.h> |
16 |
#include <sys/types.h> |
17 |
#ifdef INCL_SEL_H |
18 |
#include <sys/select.h> |
19 |
#endif |
20 |
#include <sys/stat.h> |
21 |
/* select call compatibility stuff */ |
22 |
#ifndef FD_SETSIZE |
23 |
#include <sys/param.h> |
24 |
#define FD_SETSIZE NOFILE /* maximum # select file descriptors */ |
25 |
#endif |
26 |
#ifndef FD_SET |
27 |
#ifndef NFDBITS |
28 |
#define NFDBITS (8*sizeof(int)) /* number of bits per fd_mask */ |
29 |
#endif |
30 |
#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) |
31 |
#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) |
32 |
#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) |
33 |
#define FD_ZERO(p) bzero((char *)(p), sizeof(*(p))) |
34 |
#endif |
35 |
|
36 |
#ifndef TIMELIM |
37 |
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
38 |
#endif |
39 |
|
40 |
extern char *strcpy(), *index(); |
41 |
|
42 |
extern int headismine; /* boolean true if header belongs to me */ |
43 |
|
44 |
extern char *progname; /* global program name */ |
45 |
|
46 |
extern char *errfile; /* global error file name */ |
47 |
|
48 |
static char *persistfname = NULL; /* persist file name */ |
49 |
static int persistfd = -1; /* persist file descriptor */ |
50 |
|
51 |
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; |
52 |
|
53 |
|
54 |
pfdetach() /* release persist (and header) resources */ |
55 |
{ |
56 |
if (persistfd >= 0) |
57 |
close(persistfd); |
58 |
persistfd = -1; |
59 |
persistfname = NULL; |
60 |
inpname[0] = '\0'; |
61 |
outpname[0] = '\0'; |
62 |
errname[0] = '\0'; |
63 |
headismine = 0; |
64 |
} |
65 |
|
66 |
|
67 |
pfclean() /* clean up persist files */ |
68 |
{ |
69 |
if (persistfd >= 0) |
70 |
close(persistfd); |
71 |
if (persistfname != NULL) |
72 |
unlink(persistfname); |
73 |
if (inpname[0]) |
74 |
unlink(inpname); |
75 |
if (outpname[0]) |
76 |
unlink(outpname); |
77 |
if (errname[0]) |
78 |
unlink(errname); |
79 |
} |
80 |
|
81 |
|
82 |
pflock(lf) /* place or release exclusive lock on file */ |
83 |
int lf; |
84 |
{ |
85 |
struct flock fls; |
86 |
|
87 |
fls.l_type = lf ? F_WRLCK : F_UNLCK; |
88 |
fls.l_whence = 0; |
89 |
fls.l_start = 0L; |
90 |
fls.l_len = 0L; |
91 |
if (fcntl(persistfd, F_SETLKW, &fls) < 0) |
92 |
error(SYSTEM, "cannot (un)lock persist file"); |
93 |
} |
94 |
|
95 |
|
96 |
persistfile(pfn) /* open persist file and lock it */ |
97 |
char *pfn; |
98 |
{ |
99 |
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); |
100 |
if (persistfd >= 0) { |
101 |
persistfname = pfn; |
102 |
pflock(1); |
103 |
return; |
104 |
} |
105 |
/* file exists -- switch to i/o process */ |
106 |
persistfd = open(pfn, O_RDWR); |
107 |
if (persistfd < 0) { |
108 |
sprintf(errmsg, "cannot open persist file \"%s\"", pfn); |
109 |
error(SYSTEM, errmsg); |
110 |
} |
111 |
pflock(1); |
112 |
io_process(); /* never returns */ |
113 |
} |
114 |
|
115 |
|
116 |
static int got_io; |
117 |
|
118 |
static int sig_io() { got_io++; } |
119 |
|
120 |
static int sig_alrm() { quit(0); } |
121 |
|
122 |
|
123 |
pfhold() /* holding pattern for idle rendering process */ |
124 |
{ |
125 |
int (*oldalrm)(); |
126 |
char buf[512]; |
127 |
register int n; |
128 |
/* close input and output descriptors */ |
129 |
close(fileno(stdin)); |
130 |
close(fileno(stdout)); |
131 |
if (errfile == NULL) |
132 |
close(fileno(stderr)); |
133 |
/* create named pipes for input and output */ |
134 |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
135 |
goto createrr; |
136 |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
137 |
goto createrr; |
138 |
if (errfile == NULL && |
139 |
mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 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, 0L, 0); |
147 |
/* wait TIMELIM for someone to signal us */ |
148 |
got_io = 0; |
149 |
signal(SIGIO, sig_io); |
150 |
oldalrm = (int (*)())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 |
if (freopen(inpname, "r", stdin) == NULL) |
161 |
goto openerr; |
162 |
if (freopen(outpname, "w", stdout) == NULL) |
163 |
goto openerr; |
164 |
if (errname[0]) { |
165 |
if (freopen(errname, "w", stderr) == NULL) |
166 |
goto openerr; |
167 |
unlink(errname); |
168 |
errname[0] = '\0'; |
169 |
} |
170 |
unlink(inpname); |
171 |
inpname[0] = '\0'; |
172 |
unlink(outpname); |
173 |
outpname[0] = '\0'; |
174 |
return; |
175 |
createrr: |
176 |
error(SYSTEM, "cannot create named pipes in pfhold"); |
177 |
openerr: |
178 |
error(SYSTEM, "cannot open named pipes in pfhold"); |
179 |
} |
180 |
|
181 |
|
182 |
io_process() /* just act as go-between for actual process */ |
183 |
{ |
184 |
register char *cp; |
185 |
register int nr, n; |
186 |
char buf[BUFSIZ], *pfin, *pfout, *pferr; |
187 |
int pid, nfds; |
188 |
int fdin, fdout, fderr = -1; |
189 |
fd_set readfds, writefds, excepfds; |
190 |
/* load persist file */ |
191 |
n = 40; |
192 |
while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { |
193 |
if (!n--) |
194 |
error(USER, "unattended persist file?"); |
195 |
pflock(0); |
196 |
sleep(15); /* wait until ready */ |
197 |
pflock(1); |
198 |
} |
199 |
if (nr < 0) |
200 |
error(SYSTEM, "error reading persist file"); |
201 |
ftruncate(persistfd, 0L); /* truncate persist file */ |
202 |
pfdetach(); /* close & release persist file */ |
203 |
buf[nr] = '\0'; /* parse what we got */ |
204 |
if ((cp = index(buf, ' ')) == NULL) |
205 |
goto formerr; |
206 |
*cp++ = '\0'; |
207 |
if ((pid = atoi(cp)) <= 0) |
208 |
goto formerr; |
209 |
if ((cp = index(cp, '\n')) == NULL) |
210 |
goto formerr; |
211 |
pfin = ++cp; |
212 |
if ((cp = index(cp, '\n')) == NULL) |
213 |
goto formerr; |
214 |
*cp++ = '\0'; |
215 |
pfout = cp; |
216 |
if ((cp = index(cp, '\n')) == NULL) |
217 |
goto formerr; |
218 |
*cp++ = '\0'; |
219 |
pferr = cp; |
220 |
if ((cp = index(cp, '\n')) == NULL) |
221 |
goto formerr; |
222 |
*cp++ = '\0'; |
223 |
if (cp-buf != nr) |
224 |
goto formerr; |
225 |
if (strcmp(buf, progname)) { |
226 |
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
227 |
error(USER, errmsg); |
228 |
} |
229 |
/* wake up rendering process */ |
230 |
if (kill(pid, SIGIO) < 0) |
231 |
error(SYSTEM, "cannot signal rendering process in io_process"); |
232 |
/* open named pipes, in order */ |
233 |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
234 |
error(SYSTEM, "cannot open input pipe in io_process"); |
235 |
if ((fdout = open(pfout, O_RDONLY)) < 0) |
236 |
error(SYSTEM, "cannot open output pipe in io_process"); |
237 |
if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0) |
238 |
error(SYSTEM, "cannot open error pipe in io_process"); |
239 |
for ( ; ; ) { /* loop on select call */ |
240 |
FD_ZERO(&readfds); |
241 |
FD_ZERO(&writefds); |
242 |
FD_ZERO(&excepfds); |
243 |
nfds = 0; |
244 |
if (fdin >= 0) { |
245 |
FD_SET(fdin, &writefds); |
246 |
FD_SET(fdin, &excepfds); |
247 |
nfds = fdin+1; |
248 |
} |
249 |
if (fdout >= 0) { |
250 |
FD_SET(fdout, &readfds); |
251 |
FD_SET(fdout, &excepfds); |
252 |
nfds = fdout+1; |
253 |
} |
254 |
if (fderr >= 0) { |
255 |
FD_SET(fderr, &readfds); |
256 |
FD_SET(fderr, &excepfds); |
257 |
nfds = fderr+1; |
258 |
} |
259 |
if (nfds == 0) |
260 |
break; /* all done, exit */ |
261 |
if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0) |
262 |
error(SYSTEM, "error in select call in io_process"); |
263 |
/* renderer stderr */ |
264 |
if (fderr >= 0 && (FD_ISSET(fderr, &readfds) || |
265 |
FD_ISSET(fderr, &excepfds))) { |
266 |
nr = read(fderr, cp=buf, sizeof(buf)); |
267 |
if (nr < 0) |
268 |
goto readerr; |
269 |
if (nr == 0) { |
270 |
close(fderr); |
271 |
/* close(2); don't close stderr! */ |
272 |
fderr = -1; |
273 |
} else |
274 |
do { /* write it all */ |
275 |
if ((n = write(2, cp, nr)) <= 0) |
276 |
goto writerr; |
277 |
cp += n; |
278 |
} while ((nr -= n) > 0); |
279 |
} |
280 |
/* renderer stdout */ |
281 |
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
282 |
FD_ISSET(fdout, &excepfds))) { |
283 |
nr = read(fdout, cp=buf, sizeof(buf)); |
284 |
if (nr < 0) |
285 |
goto readerr; |
286 |
if (nr == 0) { /* EOF */ |
287 |
close(fdout); |
288 |
close(1); |
289 |
fdout = -1; |
290 |
} else |
291 |
do { /* write it all */ |
292 |
if ((n = write(1, cp, nr)) <= 0) |
293 |
goto writerr; |
294 |
cp += n; |
295 |
} while ((nr -= n) > 0); |
296 |
} |
297 |
/* renderer stdin */ |
298 |
if (fdin >= 0 && (FD_ISSET(fdin, &writefds) || |
299 |
FD_ISSET(fdin, &excepfds))) { |
300 |
nr = read(0, buf, 512); /* use minimal buffer */ |
301 |
if (nr < 0) |
302 |
goto readerr; |
303 |
if (nr == 0) { |
304 |
close(0); |
305 |
close(fdin); |
306 |
fdin = -1; |
307 |
} else if (write(fdin, buf, nr) < nr) |
308 |
goto writerr; /* what else to do? */ |
309 |
} |
310 |
} |
311 |
_exit(0); /* we ought to return renderer error status! */ |
312 |
formerr: |
313 |
error(USER, "format error in persist file"); |
314 |
readerr: |
315 |
error(SYSTEM, "read error in io_process"); |
316 |
writerr: |
317 |
error(SYSTEM, "write error in io_process"); |
318 |
} |
319 |
|
320 |
#else |
321 |
|
322 |
pfclean() {} |
323 |
|
324 |
#endif |