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"; |
11 |
|
#include "standard.h" |
12 |
|
|
13 |
|
#ifdef F_SETLKW |
14 |
– |
|
14 |
|
#include "paths.h" |
15 |
+ |
#include "selcall.h" |
16 |
|
#include <signal.h> |
17 |
– |
#include <sys/types.h> |
17 |
|
#include <sys/stat.h> |
18 |
|
|
19 |
|
#ifndef TIMELIM |
24 |
|
|
25 |
|
extern int headismine; /* boolean true if header belongs to me */ |
26 |
|
|
27 |
+ |
extern char *progname; /* global program name */ |
28 |
+ |
|
29 |
+ |
extern char *errfile; /* global error file name */ |
30 |
+ |
|
31 |
|
static char *persistfname = NULL; /* persist file name */ |
32 |
|
static int persistfd = -1; /* persist file descriptor */ |
33 |
|
|
34 |
< |
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1]; |
34 |
> |
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; |
35 |
|
|
36 |
|
|
37 |
|
pfdetach() /* release persist (and header) resources */ |
42 |
|
persistfname = NULL; |
43 |
|
inpname[0] = '\0'; |
44 |
|
outpname[0] = '\0'; |
45 |
+ |
errname[0] = '\0'; |
46 |
|
headismine = 0; |
47 |
|
} |
48 |
|
|
57 |
|
unlink(inpname); |
58 |
|
if (outpname[0]) |
59 |
|
unlink(outpname); |
60 |
+ |
if (errname[0]) |
61 |
+ |
unlink(errname); |
62 |
|
} |
63 |
|
|
64 |
|
|
79 |
|
persistfile(pfn) /* open persist file and lock it */ |
80 |
|
char *pfn; |
81 |
|
{ |
82 |
< |
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666); |
82 |
> |
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); |
83 |
|
if (persistfd >= 0) { |
84 |
|
persistfname = pfn; |
85 |
|
pflock(1); |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
< |
int sig_noop() {} |
99 |
> |
static int got_io; |
100 |
|
|
101 |
+ |
static int sig_io() { got_io++; } |
102 |
|
|
103 |
+ |
static int sig_alrm() { quit(0); } |
104 |
+ |
|
105 |
+ |
|
106 |
|
pfhold() /* holding pattern for idle rendering process */ |
107 |
|
{ |
108 |
< |
char buf[128]; |
108 |
> |
int (*oldalrm)(); |
109 |
> |
char buf[512]; |
110 |
|
register int n; |
111 |
|
/* close input and output descriptors */ |
112 |
|
close(fileno(stdin)); |
113 |
|
close(fileno(stdout)); |
114 |
+ |
if (errfile == NULL) |
115 |
+ |
close(fileno(stderr)); |
116 |
|
/* create named pipes for input and output */ |
117 |
< |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600) < 0) |
117 |
> |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
118 |
|
goto createrr; |
119 |
< |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0) |
119 |
> |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
120 |
|
goto createrr; |
121 |
< |
sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname); |
122 |
< |
if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0) |
123 |
< |
error(SYSTEM, "seek/truncate error on persist file"); |
121 |
> |
if (errfile == NULL && |
122 |
> |
mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
123 |
> |
goto createrr; |
124 |
> |
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
125 |
> |
inpname, outpname, errname); |
126 |
|
n = strlen(buf); |
127 |
|
if (write(persistfd, buf, n) < n) |
128 |
|
error(SYSTEM, "error writing persist file"); |
129 |
+ |
lseek(persistfd, 0L, 0); |
130 |
|
/* wait TIMELIM for someone to signal us */ |
131 |
< |
signal(SIGIO, sig_noop); |
131 |
> |
got_io = 0; |
132 |
> |
signal(SIGIO, sig_io); |
133 |
> |
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
134 |
|
alarm(TIMELIM); |
135 |
< |
pflock(0); |
136 |
< |
pause(); |
137 |
< |
alarm(0); |
135 |
> |
pflock(0); /* unlock persist file for attach */ |
136 |
> |
while (!got_io) |
137 |
> |
pause(); /* wait for attach */ |
138 |
> |
alarm(0); /* turn off alarm */ |
139 |
> |
signal(SIGALRM, oldalrm); |
140 |
|
signal(SIGIO, SIG_DFL); |
141 |
< |
pflock(1); |
141 |
> |
pflock(1); /* grab persist file back */ |
142 |
|
/* someone wants us; reopen stdin and stdout */ |
143 |
|
if (freopen(inpname, "r", stdin) == NULL) |
144 |
|
goto openerr; |
145 |
|
if (freopen(outpname, "w", stdout) == NULL) |
146 |
|
goto openerr; |
147 |
+ |
if (errname[0]) { |
148 |
+ |
if (freopen(errname, "w", stderr) == NULL) |
149 |
+ |
goto openerr; |
150 |
+ |
unlink(errname); |
151 |
+ |
errname[0] = '\0'; |
152 |
+ |
} |
153 |
|
unlink(inpname); |
154 |
|
inpname[0] = '\0'; |
155 |
|
unlink(outpname); |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
< |
io_process() /* just act as conduits to and from actual process */ |
165 |
> |
io_process() /* just act as go-between for actual process */ |
166 |
|
{ |
167 |
|
register char *cp; |
168 |
|
register int nr, n; |
169 |
< |
char buf[4096], *pfin, *pfout; |
170 |
< |
int pid; |
171 |
< |
/* load and close persist file */ |
172 |
< |
nr = read(persistfd, buf, sizeof(buf)); |
173 |
< |
pfdetach(); |
169 |
> |
char buf[BUFSIZ], *pfin, *pfout, *pferr; |
170 |
> |
int pid, nfds; |
171 |
> |
int fdin, fdout, fderr = -1; |
172 |
> |
fd_set readfds, writefds, excepfds; |
173 |
> |
/* load persist file */ |
174 |
> |
n = 40; |
175 |
> |
while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { |
176 |
> |
if (!n--) |
177 |
> |
error(USER, "unattended persist file?"); |
178 |
> |
pflock(0); |
179 |
> |
sleep(15); /* wait until ready */ |
180 |
> |
pflock(1); |
181 |
> |
} |
182 |
|
if (nr < 0) |
183 |
< |
error(SYSTEM, "cannot read persist file"); |
184 |
< |
buf[nr] = '\0'; |
185 |
< |
if ((cp = index(buf, '\n')) == NULL) |
183 |
> |
error(SYSTEM, "error reading persist file"); |
184 |
> |
ftruncate(persistfd, 0L); /* truncate persist file */ |
185 |
> |
pfdetach(); /* close & release persist file */ |
186 |
> |
buf[nr] = '\0'; /* parse what we got */ |
187 |
> |
if ((cp = index(buf, ' ')) == NULL) |
188 |
|
goto formerr; |
189 |
|
*cp++ = '\0'; |
190 |
< |
if ((pid = atoi(buf)) <= 0) |
190 |
> |
if ((pid = atoi(cp)) <= 0) |
191 |
|
goto formerr; |
156 |
– |
pfin = cp; |
192 |
|
if ((cp = index(cp, '\n')) == NULL) |
193 |
|
goto formerr; |
194 |
+ |
pfin = ++cp; |
195 |
+ |
if ((cp = index(cp, '\n')) == NULL) |
196 |
+ |
goto formerr; |
197 |
|
*cp++ = '\0'; |
198 |
|
pfout = cp; |
199 |
|
if ((cp = index(cp, '\n')) == NULL) |
200 |
|
goto formerr; |
201 |
|
*cp++ = '\0'; |
202 |
+ |
pferr = cp; |
203 |
+ |
if ((cp = index(cp, '\n')) == NULL) |
204 |
+ |
goto formerr; |
205 |
+ |
*cp++ = '\0'; |
206 |
|
if (cp-buf != nr) |
207 |
|
goto formerr; |
208 |
< |
/* wake up rendering process */ |
208 |
> |
if (strcmp(buf, progname)) { |
209 |
> |
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
210 |
> |
error(USER, errmsg); |
211 |
> |
} |
212 |
> |
/* wake up rendering process */ |
213 |
|
if (kill(pid, SIGIO) < 0) |
214 |
|
error(SYSTEM, "cannot signal rendering process in io_process"); |
215 |
< |
pid = fork(); /* fork i/o process */ |
216 |
< |
if (pid < 0) |
217 |
< |
error(SYSTEM, "fork failed in io_process"); |
218 |
< |
/* connect to appropriate pipe */ |
219 |
< |
if (pid) { /* parent passes renderer output */ |
220 |
< |
close(0); |
221 |
< |
if (open(pfout, O_RDONLY) != 0) |
222 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
223 |
< |
} else { /* child passes renderer input */ |
224 |
< |
close(1); |
225 |
< |
if (open(pfin, O_WRONLY) != 1) |
226 |
< |
error(SYSTEM, "cannot open output pipe in io_process"); |
215 |
> |
/* open named pipes, in order */ |
216 |
> |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
217 |
> |
error(SYSTEM, "cannot open input pipe in io_process"); |
218 |
> |
if ((fdout = open(pfout, O_RDONLY)) < 0) |
219 |
> |
error(SYSTEM, "cannot open output pipe in io_process"); |
220 |
> |
if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0) |
221 |
> |
error(SYSTEM, "cannot open error pipe in io_process"); |
222 |
> |
for ( ; ; ) { /* loop on select call */ |
223 |
> |
FD_ZERO(&readfds); |
224 |
> |
FD_ZERO(&writefds); |
225 |
> |
FD_ZERO(&excepfds); |
226 |
> |
nfds = 0; |
227 |
> |
if (fdin >= 0) { |
228 |
> |
FD_SET(fdin, &writefds); |
229 |
> |
FD_SET(fdin, &excepfds); |
230 |
> |
nfds = fdin+1; |
231 |
> |
} |
232 |
> |
if (fdout >= 0) { |
233 |
> |
FD_SET(fdout, &readfds); |
234 |
> |
FD_SET(fdout, &excepfds); |
235 |
> |
nfds = fdout+1; |
236 |
> |
} |
237 |
> |
if (fderr >= 0) { |
238 |
> |
FD_SET(fderr, &readfds); |
239 |
> |
FD_SET(fderr, &excepfds); |
240 |
> |
nfds = fderr+1; |
241 |
> |
} |
242 |
> |
if (nfds == 0) |
243 |
> |
break; /* all done, exit */ |
244 |
> |
if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0) |
245 |
> |
error(SYSTEM, "error in select call in io_process"); |
246 |
> |
/* renderer stderr */ |
247 |
> |
if (fderr >= 0 && (FD_ISSET(fderr, &readfds) || |
248 |
> |
FD_ISSET(fderr, &excepfds))) { |
249 |
> |
nr = read(fderr, cp=buf, sizeof(buf)); |
250 |
> |
if (nr < 0) |
251 |
> |
goto readerr; |
252 |
> |
if (nr == 0) { |
253 |
> |
close(fderr); |
254 |
> |
/* close(2); don't close stderr! */ |
255 |
> |
fderr = -1; |
256 |
> |
} else |
257 |
> |
do { /* write it all */ |
258 |
> |
if ((n = write(2, cp, nr)) <= 0) |
259 |
> |
goto writerr; |
260 |
> |
cp += n; |
261 |
> |
} while ((nr -= n) > 0); |
262 |
> |
} |
263 |
> |
/* renderer stdout */ |
264 |
> |
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
265 |
> |
FD_ISSET(fdout, &excepfds))) { |
266 |
> |
nr = read(fdout, cp=buf, sizeof(buf)); |
267 |
> |
if (nr < 0) |
268 |
> |
goto readerr; |
269 |
> |
if (nr == 0) { /* EOF */ |
270 |
> |
close(fdout); |
271 |
> |
close(1); |
272 |
> |
fdout = -1; |
273 |
> |
} else |
274 |
> |
do { /* write it all */ |
275 |
> |
if ((n = write(1, cp, nr)) <= 0) |
276 |
> |
goto writerr; |
277 |
> |
cp += n; |
278 |
> |
} while ((nr -= n) > 0); |
279 |
> |
} |
280 |
> |
/* renderer stdin */ |
281 |
> |
if (fdin >= 0 && (FD_ISSET(fdin, &writefds) || |
282 |
> |
FD_ISSET(fdin, &excepfds))) { |
283 |
> |
nr = read(0, buf, 512); /* use minimal buffer */ |
284 |
> |
if (nr < 0) |
285 |
> |
goto readerr; |
286 |
> |
if (nr == 0) { |
287 |
> |
close(0); |
288 |
> |
close(fdin); |
289 |
> |
fdin = -1; |
290 |
> |
} else if (write(fdin, buf, nr) < nr) |
291 |
> |
goto writerr; /* what else to do? */ |
292 |
> |
} |
293 |
|
} |
294 |
< |
/* pass input to output */ |
183 |
< |
/* read as much as we can, write all of it */ |
184 |
< |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) |
185 |
< |
do { |
186 |
< |
if ((n = write(1, cp, nr)) <= 0) |
187 |
< |
goto writerr; |
188 |
< |
cp += n; |
189 |
< |
} while ((nr -= n) > 0); |
190 |
< |
if (nr < 0) |
191 |
< |
error(SYSTEM, "read error in io_process"); |
192 |
< |
close(0); /* close input */ |
193 |
< |
close(1); /* close output */ |
194 |
< |
if (pid) /* parent waits for child */ |
195 |
< |
wait(0); |
196 |
< |
exit(0); /* all done, exit (not quit!) */ |
294 |
> |
_exit(0); /* we ought to return renderer error status! */ |
295 |
|
formerr: |
296 |
|
error(USER, "format error in persist file"); |
297 |
+ |
readerr: |
298 |
+ |
error(SYSTEM, "read error in io_process"); |
299 |
|
writerr: |
300 |
|
error(SYSTEM, "write error in io_process"); |
301 |
|
} |