| 11 |
|
#include "standard.h" |
| 12 |
|
|
| 13 |
|
#ifdef F_SETLKW |
| 14 |
– |
|
| 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 */ |
| 96 |
|
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); |
| 97 |
|
if (persistfd >= 0) { |
| 98 |
|
persistfname = pfn; |
| 86 |
– |
/* put something there for faulty lock daemons */ |
| 87 |
– |
write(persistfd, "Initializing...\n", 16); |
| 99 |
|
pflock(1); |
| 100 |
|
return; |
| 101 |
|
} |
| 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) < 0) |
| 132 |
|
goto createrr; |
| 137 |
|
goto createrr; |
| 138 |
|
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
| 139 |
|
inpname, outpname, errname); |
| 127 |
– |
if (lseek(persistfd, 0L, 0) < 0) |
| 128 |
– |
error(SYSTEM, "seek error on persist file"); |
| 140 |
|
n = strlen(buf); |
| 141 |
+ |
lseek(persistfd, 0L, 0); |
| 142 |
|
if (write(persistfd, buf, n) < n) |
| 143 |
|
error(SYSTEM, "error writing persist file"); |
| 132 |
– |
ftruncate(persistfd, (long)n); |
| 133 |
– |
fsync(persistfd); /* shouldn't be necessary, but.... */ |
| 144 |
|
/* wait TIMELIM for someone to signal us */ |
| 145 |
|
got_io = 0; |
| 146 |
|
signal(SIGIO, sig_io); |
| 147 |
|
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
| 148 |
|
alarm(TIMELIM); |
| 149 |
< |
pflock(0); |
| 149 |
> |
pflock(0); /* unlock persist file for attach */ |
| 150 |
|
while (!got_io) |
| 151 |
< |
pause(); |
| 152 |
< |
alarm(0); |
| 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; |
| 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[512], *pfin, *pfout, *pferr; |
| 184 |
< |
int pid, pid2 = -1; |
| 185 |
< |
/* load and close persist file */ |
| 186 |
< |
errno = 0; |
| 187 |
< |
nr = read(persistfd, buf, sizeof(buf)-1); |
| 188 |
< |
pfdetach(); |
| 189 |
< |
if (nr <= 0) |
| 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, "error reading persist file"); |
| 198 |
< |
buf[nr] = '\0'; |
| 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'; |
| 223 |
|
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
| 224 |
|
error(USER, errmsg); |
| 225 |
|
} |
| 226 |
< |
/* wake up rendering process */ |
| 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 |
< |
goto forkerr; |
| 232 |
< |
/* connect to appropriate pipe */ |
| 233 |
< |
if (pid) { /* parent passes renderer output */ |
| 234 |
< |
close(0); |
| 235 |
< |
if (pferr[0]) { |
| 236 |
< |
pid2 = fork(); /* fork another for stderr */ |
| 237 |
< |
if (pid2 < 0) |
| 238 |
< |
goto forkerr; |
| 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 (pid2) { /* parent is still stdout */ |
| 247 |
< |
if (open(pfout, O_RDONLY) != 0) |
| 248 |
< |
error(SYSTEM, |
| 249 |
< |
"cannot open output pipe in io_process"); |
| 225 |
< |
} else { /* second child is stderr */ |
| 226 |
< |
if (open(pferr, O_RDONLY) != 0) |
| 227 |
< |
error(SYSTEM, |
| 228 |
< |
"cannot open error pipe in io_process"); |
| 229 |
< |
dup2(2, 1); /* attach stdout to stderr */ |
| 246 |
> |
if (fdout >= 0) { |
| 247 |
> |
FD_SET(fdout, &readfds); |
| 248 |
> |
FD_SET(fdout, &excepfds); |
| 249 |
> |
nfds = fdout+1; |
| 250 |
|
} |
| 251 |
< |
} else { /* child passes renderer input */ |
| 252 |
< |
close(1); |
| 253 |
< |
if (open(pfin, O_WRONLY) != 1) |
| 254 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
| 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); |
| 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 */ |
| 237 |
< |
/* read as much as we can, write all of it */ |
| 238 |
< |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) |
| 239 |
< |
do { |
| 240 |
< |
if ((n = write(1, cp, nr)) <= 0) |
| 241 |
< |
error(SYSTEM, "write error in io_process"); |
| 242 |
< |
cp += n; |
| 243 |
< |
} while ((nr -= n) > 0); |
| 244 |
< |
if (nr < 0) |
| 245 |
< |
error(SYSTEM, "read error in io_process"); |
| 246 |
< |
close(0); /* close input */ |
| 247 |
< |
close(1); /* close output */ |
| 248 |
< |
if (pid) /* parent waits for stdin child */ |
| 249 |
< |
wait(0); |
| 250 |
< |
if (pid2 > 0) /* wait for stderr child also */ |
| 251 |
< |
wait(0); |
| 252 |
< |
_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 |
< |
forkerr: |
| 312 |
< |
error(SYSTEM, "fork failed in io_process"); |
| 311 |
> |
readerr: |
| 312 |
> |
error(SYSTEM, "read error in io_process"); |
| 313 |
> |
writerr: |
| 314 |
> |
error(SYSTEM, "write error in io_process"); |
| 315 |
|
} |
| 316 |
|
|
| 317 |
|
#else |