| 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 |
| 82 |
|
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); |
| 83 |
|
if (persistfd >= 0) { |
| 84 |
|
persistfname = pfn; |
| 86 |
– |
/* put something there for faulty lock daemons */ |
| 87 |
– |
write(persistfd, "Initializing...\n", 16); |
| 85 |
|
pflock(1); |
| 86 |
|
return; |
| 87 |
|
} |
| 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) < 0) |
| 118 |
|
goto createrr; |
| 123 |
|
goto createrr; |
| 124 |
|
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
| 125 |
|
inpname, outpname, errname); |
| 127 |
– |
if (lseek(persistfd, 0L, 0) < 0) |
| 128 |
– |
error(SYSTEM, "seek error on persist file"); |
| 126 |
|
n = strlen(buf); |
| 127 |
|
if (write(persistfd, buf, n) < n) |
| 128 |
|
error(SYSTEM, "error writing persist file"); |
| 129 |
< |
ftruncate(persistfd, (long)n); |
| 133 |
< |
fsync(persistfd); /* shouldn't be necessary, but.... */ |
| 129 |
> |
lseek(persistfd, 0L, 0); |
| 130 |
|
/* wait TIMELIM for someone to signal us */ |
| 131 |
|
got_io = 0; |
| 132 |
|
signal(SIGIO, sig_io); |
| 133 |
|
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
| 134 |
|
alarm(TIMELIM); |
| 135 |
< |
pflock(0); |
| 135 |
> |
pflock(0); /* unlock persist file for attach */ |
| 136 |
|
while (!got_io) |
| 137 |
< |
pause(); |
| 138 |
< |
alarm(0); |
| 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 |
+ |
sleep(3); /* give them a chance to open their pipes */ |
| 148 |
|
if (errname[0]) { |
| 149 |
|
if (freopen(errname, "w", stderr) == NULL) |
| 150 |
|
goto openerr; |
| 163 |
|
} |
| 164 |
|
|
| 165 |
|
|
| 166 |
< |
io_process() /* just act as conduits to and from actual process */ |
| 166 |
> |
io_process() /* just act as go-between for actual process */ |
| 167 |
|
{ |
| 168 |
|
register char *cp; |
| 169 |
|
register int nr, n; |
| 170 |
< |
char buf[512], *pfin, *pfout, *pferr; |
| 171 |
< |
int pid, pid2 = -1; |
| 172 |
< |
/* load and close persist file */ |
| 173 |
< |
errno = 0; |
| 174 |
< |
nr = read(persistfd, buf, sizeof(buf)-1); |
| 175 |
< |
pfdetach(); |
| 176 |
< |
if (nr <= 0) |
| 170 |
> |
char buf[BUFSIZ], *pfin, *pfout, *pferr; |
| 171 |
> |
int pid, nfds; |
| 172 |
> |
int fdout, fderr = -1; |
| 173 |
> |
fd_set readfds, excepfds; |
| 174 |
> |
/* load persist file */ |
| 175 |
> |
n = 40; |
| 176 |
> |
while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { |
| 177 |
> |
if (!n--) |
| 178 |
> |
error(USER, "unattended persist file?"); |
| 179 |
> |
pflock(0); |
| 180 |
> |
sleep(15); /* wait until ready */ |
| 181 |
> |
pflock(1); |
| 182 |
> |
} |
| 183 |
> |
if (nr < 0) |
| 184 |
|
error(SYSTEM, "error reading persist file"); |
| 185 |
< |
buf[nr] = '\0'; |
| 185 |
> |
ftruncate(persistfd, 0L); /* truncate persist file */ |
| 186 |
> |
pfdetach(); /* close & release persist file */ |
| 187 |
> |
buf[nr] = '\0'; /* parse what we got */ |
| 188 |
|
if ((cp = index(buf, ' ')) == NULL) |
| 189 |
|
goto formerr; |
| 190 |
|
*cp++ = '\0'; |
| 210 |
|
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
| 211 |
|
error(USER, errmsg); |
| 212 |
|
} |
| 213 |
< |
/* wake up rendering process */ |
| 213 |
> |
/* wake up rendering process */ |
| 214 |
|
if (kill(pid, SIGIO) < 0) |
| 215 |
|
error(SYSTEM, "cannot signal rendering process in io_process"); |
| 216 |
< |
pid = fork(); /* fork i/o process */ |
| 216 |
> |
/* fork child feeder process */ |
| 217 |
> |
pid = fork(); |
| 218 |
|
if (pid < 0) |
| 219 |
< |
goto forkerr; |
| 220 |
< |
/* connect to appropriate pipe */ |
| 221 |
< |
if (pid) { /* parent passes renderer output */ |
| 222 |
< |
close(0); |
| 223 |
< |
if (pferr[0]) { |
| 224 |
< |
pid2 = fork(); /* fork another for stderr */ |
| 225 |
< |
if (pid2 < 0) |
| 226 |
< |
goto forkerr; |
| 219 |
> |
error(SYSTEM, "fork failed in io_process"); |
| 220 |
> |
if (pid == 0) { /* feeder loop */ |
| 221 |
> |
int fdin; |
| 222 |
> |
close(1); /* open input pipe */ |
| 223 |
> |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
| 224 |
> |
error(SYSTEM, "cannot open feed pipe in io_process"); |
| 225 |
> |
/* renderer stdin */ |
| 226 |
> |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) { |
| 227 |
> |
do { |
| 228 |
> |
if ((n = write(fdin, cp, nr)) <= 0) |
| 229 |
> |
goto writerr; |
| 230 |
> |
cp += n; |
| 231 |
> |
} while ((nr -= n) > 0); |
| 232 |
|
} |
| 233 |
< |
if (pid2) { /* parent is still stdout */ |
| 234 |
< |
if (open(pfout, O_RDONLY) != 0) |
| 235 |
< |
error(SYSTEM, |
| 236 |
< |
"cannot open output pipe in io_process"); |
| 237 |
< |
} else { /* second child is stderr */ |
| 238 |
< |
if (open(pferr, O_RDONLY) != 0) |
| 239 |
< |
error(SYSTEM, |
| 240 |
< |
"cannot open error pipe in io_process"); |
| 241 |
< |
dup2(2, 1); /* attach stdout to stderr */ |
| 233 |
> |
if (nr < 0) |
| 234 |
> |
goto readerr; |
| 235 |
> |
_exit(0); |
| 236 |
> |
} |
| 237 |
> |
close(0); |
| 238 |
> |
/* open output pipes, in order */ |
| 239 |
> |
if ((fdout = open(pfout, O_RDONLY)) < 0) |
| 240 |
> |
error(SYSTEM, "cannot open output pipe in io_process"); |
| 241 |
> |
if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0) |
| 242 |
> |
error(SYSTEM, "cannot open error pipe in io_process"); |
| 243 |
> |
for ( ; ; ) { /* eater loop */ |
| 244 |
> |
FD_ZERO(&readfds); |
| 245 |
> |
FD_ZERO(&excepfds); |
| 246 |
> |
nfds = 0; |
| 247 |
> |
if (fdout >= 0) { |
| 248 |
> |
FD_SET(fdout, &readfds); |
| 249 |
> |
FD_SET(fdout, &excepfds); |
| 250 |
> |
nfds = fdout+1; |
| 251 |
|
} |
| 252 |
< |
} else { /* child passes renderer input */ |
| 253 |
< |
close(1); |
| 254 |
< |
if (open(pfin, O_WRONLY) != 1) |
| 255 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
| 252 |
> |
if (fderr >= 0) { |
| 253 |
> |
FD_SET(fderr, &readfds); |
| 254 |
> |
FD_SET(fderr, &excepfds); |
| 255 |
> |
nfds = fderr+1; |
| 256 |
> |
} |
| 257 |
> |
if (nfds == 0) |
| 258 |
> |
break; /* all done, exit */ |
| 259 |
> |
if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0) |
| 260 |
> |
error(SYSTEM, "error in select call in io_process"); |
| 261 |
> |
/* renderer stderr */ |
| 262 |
> |
if (fderr >= 0 && (FD_ISSET(fderr, &readfds) || |
| 263 |
> |
FD_ISSET(fderr, &excepfds))) { |
| 264 |
> |
nr = read(fderr, cp=buf, sizeof(buf)); |
| 265 |
> |
if (nr < 0) |
| 266 |
> |
goto readerr; |
| 267 |
> |
if (nr == 0) { |
| 268 |
> |
close(fderr); |
| 269 |
> |
/* close(2); don't close stderr! */ |
| 270 |
> |
fderr = -1; |
| 271 |
> |
} else |
| 272 |
> |
do { /* write it all */ |
| 273 |
> |
if ((n = write(2, cp, nr)) <= 0) |
| 274 |
> |
goto writerr; |
| 275 |
> |
cp += n; |
| 276 |
> |
} while ((nr -= n) > 0); |
| 277 |
> |
} |
| 278 |
> |
/* renderer stdout */ |
| 279 |
> |
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
| 280 |
> |
FD_ISSET(fdout, &excepfds))) { |
| 281 |
> |
nr = read(fdout, cp=buf, sizeof(buf)); |
| 282 |
> |
if (nr < 0) |
| 283 |
> |
goto readerr; |
| 284 |
> |
if (nr == 0) { /* EOF */ |
| 285 |
> |
close(fdout); |
| 286 |
> |
close(1); |
| 287 |
> |
fdout = -1; |
| 288 |
> |
} else |
| 289 |
> |
do { /* write it all */ |
| 290 |
> |
if ((n = write(1, cp, nr)) <= 0) |
| 291 |
> |
goto writerr; |
| 292 |
> |
cp += n; |
| 293 |
> |
} while ((nr -= n) > 0); |
| 294 |
> |
} |
| 295 |
|
} |
| 296 |
< |
/* 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!) */ |
| 296 |
> |
_exit(0); /* we ought to return renderer error status! */ |
| 297 |
|
formerr: |
| 298 |
|
error(USER, "format error in persist file"); |
| 299 |
< |
forkerr: |
| 300 |
< |
error(SYSTEM, "fork failed in io_process"); |
| 299 |
> |
readerr: |
| 300 |
> |
error(SYSTEM, "read error in io_process"); |
| 301 |
> |
writerr: |
| 302 |
> |
error(SYSTEM, "write error in io_process"); |
| 303 |
|
} |
| 304 |
|
|
| 305 |
|
#else |