| 1 |
< |
/* Copyright (c) 1993 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
| 4 |
> |
static char SCCSid[] = "$SunId$ SGI"; |
| 5 |
|
#endif |
| 6 |
|
|
| 7 |
|
/* |
| 9 |
|
*/ |
| 10 |
|
|
| 11 |
|
#include "standard.h" |
| 12 |
+ |
#include "random.h" |
| 13 |
+ |
|
| 14 |
+ |
#ifdef F_SETLKW |
| 15 |
|
#include "paths.h" |
| 16 |
+ |
#include "selcall.h" |
| 17 |
|
#include <signal.h> |
| 14 |
– |
#include <sys/types.h> |
| 18 |
|
#include <sys/stat.h> |
| 19 |
|
|
| 20 |
|
#ifndef TIMELIM |
| 25 |
|
|
| 26 |
|
extern int headismine; /* boolean true if header belongs to me */ |
| 27 |
|
|
| 28 |
+ |
extern char *progname; /* global program name */ |
| 29 |
+ |
|
| 30 |
+ |
extern char *errfile; /* global error file name */ |
| 31 |
+ |
|
| 32 |
|
static char *persistfname = NULL; /* persist file name */ |
| 33 |
|
static int persistfd = -1; /* persist file descriptor */ |
| 34 |
|
|
| 35 |
< |
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1]; |
| 35 |
> |
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
pfdetach() /* release persist (and header) resources */ |
| 43 |
|
persistfname = NULL; |
| 44 |
|
inpname[0] = '\0'; |
| 45 |
|
outpname[0] = '\0'; |
| 46 |
+ |
errname[0] = '\0'; |
| 47 |
|
headismine = 0; |
| 48 |
|
} |
| 49 |
|
|
| 58 |
|
unlink(inpname); |
| 59 |
|
if (outpname[0]) |
| 60 |
|
unlink(outpname); |
| 61 |
+ |
if (errname[0]) |
| 62 |
+ |
unlink(errname); |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
|
| 80 |
|
persistfile(pfn) /* open persist file and lock it */ |
| 81 |
|
char *pfn; |
| 82 |
|
{ |
| 83 |
< |
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 83 |
> |
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); |
| 84 |
|
if (persistfd >= 0) { |
| 85 |
|
persistfname = pfn; |
| 86 |
|
pflock(1); |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
|
| 100 |
< |
int sig_noop() {} |
| 100 |
> |
static int got_io; |
| 101 |
|
|
| 102 |
+ |
static int sig_io() { got_io++; } |
| 103 |
|
|
| 104 |
+ |
static int sig_alrm() { quit(0); } |
| 105 |
+ |
|
| 106 |
+ |
|
| 107 |
|
pfhold() /* holding pattern for idle rendering process */ |
| 108 |
|
{ |
| 109 |
< |
char buf[128]; |
| 109 |
> |
int (*oldalrm)(); |
| 110 |
> |
char buf[512]; |
| 111 |
|
register int n; |
| 112 |
|
/* close input and output descriptors */ |
| 113 |
|
close(fileno(stdin)); |
| 99 |
– |
fflush(stdout); |
| 114 |
|
close(fileno(stdout)); |
| 115 |
+ |
if (errfile == NULL) |
| 116 |
+ |
close(fileno(stderr)); |
| 117 |
|
/* create named pipes for input and output */ |
| 118 |
< |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600) < 0) |
| 118 |
> |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 119 |
|
goto createrr; |
| 120 |
< |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0) |
| 120 |
> |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 121 |
|
goto createrr; |
| 122 |
< |
sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname); |
| 123 |
< |
if (lseek(persistfd, 0L, 0) < 0) |
| 124 |
< |
error(SYSTEM, "seek error on persist file in pfhold"); |
| 122 |
> |
if (errfile == NULL && |
| 123 |
> |
mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 124 |
> |
goto createrr; |
| 125 |
> |
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
| 126 |
> |
inpname, outpname, errname); |
| 127 |
|
n = strlen(buf); |
| 128 |
|
if (write(persistfd, buf, n) < n) |
| 129 |
< |
error(SYSTEM, "error writing persist file in pfhold"); |
| 129 |
> |
error(SYSTEM, "error writing persist file"); |
| 130 |
> |
lseek(persistfd, 0L, 0); |
| 131 |
|
/* wait TIMELIM for someone to signal us */ |
| 132 |
< |
signal(SIGIO, sig_noop); |
| 132 |
> |
got_io = 0; |
| 133 |
> |
signal(SIGIO, sig_io); |
| 134 |
> |
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
| 135 |
|
alarm(TIMELIM); |
| 136 |
< |
pflock(0); |
| 137 |
< |
pause(); |
| 138 |
< |
alarm(0); |
| 136 |
> |
pflock(0); /* unlock persist file for attach */ |
| 137 |
> |
while (!got_io) |
| 138 |
> |
pause(); /* wait for attach */ |
| 139 |
> |
alarm(0); /* turn off alarm */ |
| 140 |
> |
signal(SIGALRM, oldalrm); |
| 141 |
|
signal(SIGIO, SIG_DFL); |
| 142 |
< |
pflock(1); |
| 142 |
> |
pflock(1); /* grab persist file back */ |
| 143 |
|
/* someone wants us; reopen stdin and stdout */ |
| 144 |
|
if (freopen(inpname, "r", stdin) == NULL) |
| 145 |
|
goto openerr; |
| 146 |
|
if (freopen(outpname, "w", stdout) == NULL) |
| 147 |
|
goto openerr; |
| 148 |
+ |
sleep(3); /* give them a chance to open their pipes */ |
| 149 |
+ |
if (errname[0]) { |
| 150 |
+ |
if (freopen(errname, "w", stderr) == NULL) |
| 151 |
+ |
goto openerr; |
| 152 |
+ |
unlink(errname); |
| 153 |
+ |
errname[0] = '\0'; |
| 154 |
+ |
} |
| 155 |
|
unlink(inpname); |
| 156 |
|
inpname[0] = '\0'; |
| 157 |
|
unlink(outpname); |
| 164 |
|
} |
| 165 |
|
|
| 166 |
|
|
| 167 |
< |
io_process() /* just act as conduits to and from actual process */ |
| 167 |
> |
io_process() /* just act as go-between for actual process */ |
| 168 |
|
{ |
| 169 |
|
register char *cp; |
| 170 |
|
register int nr, n; |
| 171 |
< |
char buf[4096], *pfin, *pfout; |
| 172 |
< |
int pid; |
| 173 |
< |
/* load and close persist file */ |
| 174 |
< |
nr = read(persistfd, buf, sizeof(buf)); |
| 175 |
< |
pfdetach(); |
| 171 |
> |
char buf[BUFSIZ], *pfin, *pfout, *pferr; |
| 172 |
> |
int pid, nfds; |
| 173 |
> |
int fdout, fderr = -1; |
| 174 |
> |
int status = 0; |
| 175 |
> |
fd_set readfds, excepfds; |
| 176 |
> |
/* load persist file */ |
| 177 |
> |
n = 40; |
| 178 |
> |
while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { |
| 179 |
> |
if (!n--) |
| 180 |
> |
error(USER, "unattended persist file?"); |
| 181 |
> |
pflock(0); |
| 182 |
> |
sleep(3+(3*getpid()+random())%13); /* wait until ready */ |
| 183 |
> |
pflock(1); |
| 184 |
> |
} |
| 185 |
|
if (nr < 0) |
| 186 |
< |
error(SYSTEM, "cannot read persist file"); |
| 187 |
< |
buf[nr] = '\0'; |
| 188 |
< |
if ((cp = index(buf, '\n')) == NULL) |
| 186 |
> |
error(SYSTEM, "error reading persist file"); |
| 187 |
> |
ftruncate(persistfd, 0L); /* truncate persist file */ |
| 188 |
> |
pfdetach(); /* close & release persist file */ |
| 189 |
> |
buf[nr] = '\0'; /* parse what we got */ |
| 190 |
> |
if ((cp = index(buf, ' ')) == NULL) |
| 191 |
|
goto formerr; |
| 192 |
|
*cp++ = '\0'; |
| 193 |
< |
if ((pid = atoi(buf)) <= 0) |
| 193 |
> |
if ((pid = atoi(cp)) <= 0) |
| 194 |
|
goto formerr; |
| 154 |
– |
pfin = cp; |
| 195 |
|
if ((cp = index(cp, '\n')) == NULL) |
| 196 |
|
goto formerr; |
| 197 |
+ |
pfin = ++cp; |
| 198 |
+ |
if ((cp = index(cp, '\n')) == NULL) |
| 199 |
+ |
goto formerr; |
| 200 |
|
*cp++ = '\0'; |
| 201 |
|
pfout = cp; |
| 202 |
|
if ((cp = index(cp, '\n')) == NULL) |
| 203 |
|
goto formerr; |
| 204 |
|
*cp++ = '\0'; |
| 205 |
+ |
pferr = cp; |
| 206 |
+ |
if ((cp = index(cp, '\n')) == NULL) |
| 207 |
+ |
goto formerr; |
| 208 |
+ |
*cp++ = '\0'; |
| 209 |
|
if (cp-buf != nr) |
| 210 |
|
goto formerr; |
| 211 |
< |
/* wake up rendering process */ |
| 211 |
> |
if (strcmp(buf, progname)) { |
| 212 |
> |
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
| 213 |
> |
error(USER, errmsg); |
| 214 |
> |
} |
| 215 |
> |
/* wake up rendering process */ |
| 216 |
|
if (kill(pid, SIGIO) < 0) |
| 217 |
|
error(SYSTEM, "cannot signal rendering process in io_process"); |
| 218 |
< |
pid = fork(); /* fork i/o process */ |
| 218 |
> |
/* fork child feeder process */ |
| 219 |
> |
pid = fork(); |
| 220 |
|
if (pid < 0) |
| 221 |
|
error(SYSTEM, "fork failed in io_process"); |
| 222 |
< |
/* connect to appropriate pipe */ |
| 223 |
< |
if (pid) { /* parent passes renderer output */ |
| 224 |
< |
if (freopen(pfout, "r", stdin) == NULL) |
| 225 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
| 226 |
< |
} else { /* child passes renderer input */ |
| 227 |
< |
if (freopen(pfin, "w", stdout) == NULL) |
| 228 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
| 222 |
> |
if (pid == 0) { /* feeder loop */ |
| 223 |
> |
int fdin; |
| 224 |
> |
close(1); /* open input pipe */ |
| 225 |
> |
if ((fdin = open(pfin, O_WRONLY)) < 0) |
| 226 |
> |
error(SYSTEM, "cannot open feed pipe in io_process"); |
| 227 |
> |
/* renderer stdin */ |
| 228 |
> |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) { |
| 229 |
> |
do { |
| 230 |
> |
if ((n = write(fdin, cp, nr)) <= 0) |
| 231 |
> |
goto writerr; |
| 232 |
> |
cp += n; |
| 233 |
> |
} while ((nr -= n) > 0); |
| 234 |
> |
} |
| 235 |
> |
if (nr < 0) |
| 236 |
> |
goto readerr; |
| 237 |
> |
_exit(0); |
| 238 |
|
} |
| 239 |
< |
/* pass input to output */ |
| 240 |
< |
cp = buf; n = sizeof(buf); |
| 241 |
< |
while ((nr = read(fileno(stdin), cp, n)) > 0) { |
| 242 |
< |
nr += cp-buf; |
| 243 |
< |
if ((n = write(fileno(stdout), buf, nr)) <= 0) |
| 244 |
< |
error(SYSTEM, "write error in io_process"); |
| 245 |
< |
cp = buf; |
| 246 |
< |
while (n < nr) |
| 247 |
< |
*cp++ = buf[n++]; |
| 248 |
< |
n = sizeof(buf) - (cp-buf); |
| 239 |
> |
close(0); |
| 240 |
> |
/* open output pipes, in order */ |
| 241 |
> |
if ((fdout = open(pfout, O_RDONLY)) < 0) |
| 242 |
> |
error(SYSTEM, "cannot open output pipe in io_process"); |
| 243 |
> |
if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0) |
| 244 |
> |
error(SYSTEM, "cannot open error pipe in io_process"); |
| 245 |
> |
for ( ; ; ) { /* eater loop */ |
| 246 |
> |
FD_ZERO(&readfds); |
| 247 |
> |
FD_ZERO(&excepfds); |
| 248 |
> |
nfds = 0; |
| 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, NULL, &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 |
> |
cp[nr] = '\0'; /* deduce status if we can */ |
| 275 |
> |
n = strlen(progname); |
| 276 |
> |
if (!strncmp(cp, progname, n) && |
| 277 |
> |
cp[n++] == ':' && |
| 278 |
> |
cp[n++] == ' ') { |
| 279 |
> |
register struct erract *ep; |
| 280 |
> |
for (ep = erract; ep < erract+NERRS; |
| 281 |
> |
ep++) |
| 282 |
> |
if (ep->pre[0] && |
| 283 |
> |
!strncmp(cp+n, ep->pre, |
| 284 |
> |
strlen(ep->pre))) { |
| 285 |
> |
status = ep->ec; |
| 286 |
> |
break; |
| 287 |
> |
} |
| 288 |
> |
} |
| 289 |
> |
do { /* write message */ |
| 290 |
> |
if ((n = write(2, cp, nr)) <= 0) |
| 291 |
> |
goto writerr; |
| 292 |
> |
cp += n; |
| 293 |
> |
} while ((nr -= n) > 0); |
| 294 |
> |
} |
| 295 |
> |
} |
| 296 |
> |
/* renderer stdout */ |
| 297 |
> |
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
| 298 |
> |
FD_ISSET(fdout, &excepfds))) { |
| 299 |
> |
nr = read(fdout, cp=buf, sizeof(buf)); |
| 300 |
> |
if (nr < 0) |
| 301 |
> |
goto readerr; |
| 302 |
> |
if (nr == 0) { /* EOF */ |
| 303 |
> |
close(fdout); |
| 304 |
> |
close(1); |
| 305 |
> |
fdout = -1; |
| 306 |
> |
} else |
| 307 |
> |
do { /* write it all */ |
| 308 |
> |
if ((n = write(1, cp, nr)) <= 0) |
| 309 |
> |
goto writerr; |
| 310 |
> |
cp += n; |
| 311 |
> |
} while ((nr -= n) > 0); |
| 312 |
> |
} |
| 313 |
|
} |
| 314 |
< |
fclose(stdin); |
| 315 |
< |
fclose(stdout); |
| 191 |
< |
if (pid) /* parent waits for child */ |
| 192 |
< |
wait(0); |
| 193 |
< |
exit(0); /* all done, exit (not quit!) */ |
| 314 |
> |
wait(0); /* wait for feeder process */ |
| 315 |
> |
_exit(status); |
| 316 |
|
formerr: |
| 317 |
|
error(USER, "format error in persist file"); |
| 318 |
+ |
readerr: |
| 319 |
+ |
error(SYSTEM, "read error in io_process"); |
| 320 |
+ |
writerr: |
| 321 |
+ |
error(SYSTEM, "write error in io_process"); |
| 322 |
|
} |
| 323 |
+ |
|
| 324 |
+ |
#else |
| 325 |
+ |
|
| 326 |
+ |
pfclean() {} |
| 327 |
+ |
|
| 328 |
+ |
#endif |