1 |
< |
/* Copyright (c) 1993 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
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 |
|
#include <sys/stat.h> |
18 |
+ |
/* select call compatibility stuff */ |
19 |
+ |
#ifndef FD_SETSIZE |
20 |
+ |
#include <sys/param.h> |
21 |
+ |
#define FD_SETSIZE NOFILE /* maximum # select file descriptors */ |
22 |
+ |
#endif |
23 |
+ |
#ifndef FD_SET |
24 |
+ |
#ifndef NFDBITS |
25 |
+ |
#define NFDBITS (8*sizeof(int)) /* number of bits per fd_mask */ |
26 |
+ |
#endif |
27 |
+ |
#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) |
28 |
+ |
#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) |
29 |
+ |
#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) |
30 |
+ |
#define FD_ZERO(p) bzero((char *)(p), sizeof(*(p))) |
31 |
+ |
#endif |
32 |
|
|
33 |
|
#ifndef TIMELIM |
34 |
|
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
38 |
|
|
39 |
|
extern int headismine; /* boolean true if header belongs to me */ |
40 |
|
|
41 |
+ |
extern char *progname; /* global program name */ |
42 |
+ |
|
43 |
+ |
extern char *errfile; /* global error file name */ |
44 |
+ |
|
45 |
|
static char *persistfname = NULL; /* persist file name */ |
46 |
|
static int persistfd = -1; /* persist file descriptor */ |
47 |
|
|
48 |
< |
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1]; |
48 |
> |
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; |
49 |
|
|
50 |
|
|
51 |
|
pfdetach() /* release persist (and header) resources */ |
56 |
|
persistfname = NULL; |
57 |
|
inpname[0] = '\0'; |
58 |
|
outpname[0] = '\0'; |
59 |
+ |
errname[0] = '\0'; |
60 |
|
headismine = 0; |
61 |
|
} |
62 |
|
|
71 |
|
unlink(inpname); |
72 |
|
if (outpname[0]) |
73 |
|
unlink(outpname); |
74 |
+ |
if (errname[0]) |
75 |
+ |
unlink(errname); |
76 |
|
} |
77 |
|
|
78 |
|
|
93 |
|
persistfile(pfn) /* open persist file and lock it */ |
94 |
|
char *pfn; |
95 |
|
{ |
96 |
< |
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666); |
96 |
> |
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); |
97 |
|
if (persistfd >= 0) { |
98 |
|
persistfname = pfn; |
99 |
|
pflock(1); |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
< |
int sig_noop() {} |
113 |
> |
static int got_io; |
114 |
|
|
115 |
+ |
static int sig_io() { got_io++; } |
116 |
|
|
117 |
+ |
static int sig_alrm() { quit(0); } |
118 |
+ |
|
119 |
+ |
|
120 |
|
pfhold() /* holding pattern for idle rendering process */ |
121 |
|
{ |
122 |
< |
char buf[128]; |
122 |
> |
int (*oldalrm)(); |
123 |
> |
char buf[512]; |
124 |
|
register int n; |
125 |
|
/* close input and output descriptors */ |
126 |
|
close(fileno(stdin)); |
127 |
|
close(fileno(stdout)); |
128 |
+ |
if (errfile == NULL) |
129 |
+ |
close(fileno(stderr)); |
130 |
|
/* create named pipes for input and output */ |
131 |
< |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600) < 0) |
131 |
> |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
132 |
|
goto createrr; |
133 |
< |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0) |
133 |
> |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
134 |
|
goto createrr; |
135 |
< |
sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname); |
136 |
< |
if (lseek(persistfd, 0L, 0) < 0) |
137 |
< |
error(SYSTEM, "seek error on persist file in pfhold"); |
135 |
> |
if (errfile == NULL && |
136 |
> |
mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
137 |
> |
goto createrr; |
138 |
> |
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
139 |
> |
inpname, outpname, errname); |
140 |
|
n = strlen(buf); |
141 |
|
if (write(persistfd, buf, n) < n) |
142 |
< |
error(SYSTEM, "error writing persist file in pfhold"); |
142 |
> |
error(SYSTEM, "error writing persist file"); |
143 |
> |
lseek(persistfd, 0L, 0); |
144 |
|
/* wait TIMELIM for someone to signal us */ |
145 |
< |
signal(SIGIO, sig_noop); |
145 |
> |
got_io = 0; |
146 |
> |
signal(SIGIO, sig_io); |
147 |
> |
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
148 |
|
alarm(TIMELIM); |
149 |
< |
pflock(0); |
150 |
< |
pause(); |
151 |
< |
alarm(0); |
149 |
> |
pflock(0); /* unlock persist file for attach */ |
150 |
> |
while (!got_io) |
151 |
> |
pause(); /* wait for attach */ |
152 |
> |
alarm(0); /* turn off alarm */ |
153 |
> |
signal(SIGALRM, oldalrm); |
154 |
|
signal(SIGIO, SIG_DFL); |
155 |
< |
pflock(1); |
155 |
> |
pflock(1); /* grab persist file back */ |
156 |
|
/* someone wants us; reopen stdin and stdout */ |
157 |
|
if (freopen(inpname, "r", stdin) == NULL) |
158 |
|
goto openerr; |
159 |
|
if (freopen(outpname, "w", stdout) == NULL) |
160 |
|
goto openerr; |
161 |
+ |
if (errname[0]) { |
162 |
+ |
if (freopen(errname, "w", stderr) == NULL) |
163 |
+ |
goto openerr; |
164 |
+ |
unlink(errname); |
165 |
+ |
errname[0] = '\0'; |
166 |
+ |
} |
167 |
|
unlink(inpname); |
168 |
|
inpname[0] = '\0'; |
169 |
|
unlink(outpname); |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
< |
io_process() /* just act as conduits to and from actual process */ |
179 |
> |
io_process() /* just act as go-between for actual process */ |
180 |
|
{ |
181 |
|
register char *cp; |
182 |
|
register int nr, n; |
183 |
< |
char buf[4096], *pfin, *pfout; |
184 |
< |
int pid; |
185 |
< |
/* load and close persist file */ |
186 |
< |
nr = read(persistfd, buf, sizeof(buf)); |
187 |
< |
pfdetach(); |
183 |
> |
char buf[BUFSIZ], *pfin, *pfout, *pferr; |
184 |
> |
int pid, nfds; |
185 |
> |
int fdin, fdout, fderr = -1; |
186 |
> |
fd_set readfds, writefds, excepfds; |
187 |
> |
/* load persist file */ |
188 |
> |
n = 40; |
189 |
> |
while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { |
190 |
> |
if (!n--) |
191 |
> |
error(USER, "unattended persist file?"); |
192 |
> |
pflock(0); |
193 |
> |
sleep(15); /* wait until ready */ |
194 |
> |
pflock(1); |
195 |
> |
} |
196 |
|
if (nr < 0) |
197 |
< |
error(SYSTEM, "cannot read persist file"); |
198 |
< |
buf[nr] = '\0'; |
199 |
< |
if ((cp = index(buf, '\n')) == NULL) |
197 |
> |
error(SYSTEM, "error reading persist file"); |
198 |
> |
ftruncate(persistfd, 0L); /* truncate persist file */ |
199 |
> |
pfdetach(); /* close & release persist file */ |
200 |
> |
buf[nr] = '\0'; /* parse what we got */ |
201 |
> |
if ((cp = index(buf, ' ')) == NULL) |
202 |
|
goto formerr; |
203 |
|
*cp++ = '\0'; |
204 |
< |
if ((pid = atoi(buf)) <= 0) |
204 |
> |
if ((pid = atoi(cp)) <= 0) |
205 |
|
goto formerr; |
153 |
– |
pfin = cp; |
206 |
|
if ((cp = index(cp, '\n')) == NULL) |
207 |
|
goto formerr; |
208 |
+ |
pfin = ++cp; |
209 |
+ |
if ((cp = index(cp, '\n')) == NULL) |
210 |
+ |
goto formerr; |
211 |
|
*cp++ = '\0'; |
212 |
|
pfout = cp; |
213 |
|
if ((cp = index(cp, '\n')) == NULL) |
214 |
|
goto formerr; |
215 |
|
*cp++ = '\0'; |
216 |
+ |
pferr = cp; |
217 |
+ |
if ((cp = index(cp, '\n')) == NULL) |
218 |
+ |
goto formerr; |
219 |
+ |
*cp++ = '\0'; |
220 |
|
if (cp-buf != nr) |
221 |
|
goto formerr; |
222 |
< |
/* wake up rendering process */ |
222 |
> |
if (strcmp(buf, progname)) { |
223 |
> |
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
224 |
> |
error(USER, errmsg); |
225 |
> |
} |
226 |
> |
/* wake up rendering process */ |
227 |
|
if (kill(pid, SIGIO) < 0) |
228 |
|
error(SYSTEM, "cannot signal rendering process in io_process"); |
229 |
< |
pid = fork(); /* fork i/o process */ |
230 |
< |
if (pid < 0) |
231 |
< |
error(SYSTEM, "fork failed in io_process"); |
232 |
< |
/* connect to appropriate pipe */ |
233 |
< |
if (pid) { /* parent passes renderer output */ |
234 |
< |
if (freopen(pfout, "r", stdin) == NULL) |
235 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
236 |
< |
} else { /* child passes renderer input */ |
237 |
< |
if (freopen(pfin, "w", stdout) == NULL) |
238 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
229 |
> |
/* open named pipes, in order */ |
230 |
> |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
231 |
> |
error(SYSTEM, "cannot open input pipe in io_process"); |
232 |
> |
if ((fdout = open(pfout, O_RDONLY)) < 0) |
233 |
> |
error(SYSTEM, "cannot open output pipe in io_process"); |
234 |
> |
if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0) |
235 |
> |
error(SYSTEM, "cannot open error pipe in io_process"); |
236 |
> |
for ( ; ; ) { /* loop on select call */ |
237 |
> |
FD_ZERO(&readfds); |
238 |
> |
FD_ZERO(&writefds); |
239 |
> |
FD_ZERO(&excepfds); |
240 |
> |
nfds = 0; |
241 |
> |
if (fdin >= 0) { |
242 |
> |
FD_SET(fdin, &writefds); |
243 |
> |
FD_SET(fdin, &excepfds); |
244 |
> |
nfds = fdin+1; |
245 |
> |
} |
246 |
> |
if (fdout >= 0) { |
247 |
> |
FD_SET(fdout, &readfds); |
248 |
> |
FD_SET(fdout, &excepfds); |
249 |
> |
nfds = fdout+1; |
250 |
> |
} |
251 |
> |
if (fderr >= 0) { |
252 |
> |
FD_SET(fderr, &readfds); |
253 |
> |
FD_SET(fderr, &excepfds); |
254 |
> |
nfds = fderr+1; |
255 |
> |
} |
256 |
> |
if (nfds == 0) |
257 |
> |
break; /* all done, exit */ |
258 |
> |
if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0) |
259 |
> |
error(SYSTEM, "error in select call in io_process"); |
260 |
> |
/* renderer stderr */ |
261 |
> |
if (fderr >= 0 && (FD_ISSET(fderr, &readfds) || |
262 |
> |
FD_ISSET(fderr, &excepfds))) { |
263 |
> |
nr = read(fderr, cp=buf, sizeof(buf)); |
264 |
> |
if (nr < 0) |
265 |
> |
goto readerr; |
266 |
> |
if (nr == 0) { |
267 |
> |
close(fderr); |
268 |
> |
/* close(2); don't close stderr! */ |
269 |
> |
fderr = -1; |
270 |
> |
} else |
271 |
> |
do { /* write it all */ |
272 |
> |
if ((n = write(2, cp, nr)) <= 0) |
273 |
> |
goto writerr; |
274 |
> |
cp += n; |
275 |
> |
} while ((nr -= n) > 0); |
276 |
> |
} |
277 |
> |
/* renderer stdout */ |
278 |
> |
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
279 |
> |
FD_ISSET(fdout, &excepfds))) { |
280 |
> |
nr = read(fdout, cp=buf, sizeof(buf)); |
281 |
> |
if (nr < 0) |
282 |
> |
goto readerr; |
283 |
> |
if (nr == 0) { /* EOF */ |
284 |
> |
close(fdout); |
285 |
> |
close(1); |
286 |
> |
fdout = -1; |
287 |
> |
} else |
288 |
> |
do { /* write it all */ |
289 |
> |
if ((n = write(1, cp, nr)) <= 0) |
290 |
> |
goto writerr; |
291 |
> |
cp += n; |
292 |
> |
} while ((nr -= n) > 0); |
293 |
> |
} |
294 |
> |
/* renderer stdin */ |
295 |
> |
if (fdin >= 0 && (FD_ISSET(fdin, &writefds) || |
296 |
> |
FD_ISSET(fdin, &excepfds))) { |
297 |
> |
nr = read(0, buf, 512); /* use minimal buffer */ |
298 |
> |
if (nr < 0) |
299 |
> |
goto readerr; |
300 |
> |
if (nr == 0) { |
301 |
> |
close(0); |
302 |
> |
close(fdin); |
303 |
> |
fdin = -1; |
304 |
> |
} else if (write(fdin, buf, nr) < nr) |
305 |
> |
goto writerr; /* what else to do? */ |
306 |
> |
} |
307 |
|
} |
308 |
< |
/* pass input to output */ |
178 |
< |
cp = buf; n = sizeof(buf); |
179 |
< |
while ((nr = read(fileno(stdin), cp, n)) > 0) { |
180 |
< |
nr += cp-buf; |
181 |
< |
if ((n = write(fileno(stdout), buf, nr)) <= 0) |
182 |
< |
error(SYSTEM, "write error in io_process"); |
183 |
< |
cp = buf; |
184 |
< |
while (n < nr) |
185 |
< |
*cp++ = buf[n++]; |
186 |
< |
n = sizeof(buf) - (cp-buf); |
187 |
< |
} |
188 |
< |
fclose(stdin); |
189 |
< |
fclose(stdout); |
190 |
< |
if (pid) /* parent waits for child */ |
191 |
< |
wait(0); |
192 |
< |
exit(0); /* all done, exit (not quit!) */ |
308 |
> |
_exit(0); /* we ought to return renderer error status! */ |
309 |
|
formerr: |
310 |
|
error(USER, "format error in persist file"); |
311 |
+ |
readerr: |
312 |
+ |
error(SYSTEM, "read error in io_process"); |
313 |
+ |
writerr: |
314 |
+ |
error(SYSTEM, "write error in io_process"); |
315 |
|
} |
316 |
+ |
|
317 |
+ |
#else |
318 |
+ |
|
319 |
+ |
pfclean() {} |
320 |
+ |
|
321 |
+ |
#endif |