| 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 |
+ |
|
| 15 |
|
#include "paths.h" |
| 16 |
|
#include <signal.h> |
| 17 |
|
#include <sys/types.h> |
| 18 |
|
#include <sys/stat.h> |
| 19 |
|
|
| 20 |
+ |
#ifndef TIMELIM |
| 21 |
+ |
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
| 22 |
+ |
#endif |
| 23 |
+ |
|
| 24 |
|
extern char *strcpy(), *index(); |
| 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 |
< |
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)); |
| 95 |
– |
fflush(stdout); |
| 114 |
|
close(fileno(stdout)); |
| 115 |
|
/* create named pipes for input and output */ |
| 116 |
< |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600) < 0) |
| 116 |
> |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 117 |
|
goto createrr; |
| 118 |
< |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0) |
| 118 |
> |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 119 |
|
goto createrr; |
| 120 |
< |
sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname); |
| 121 |
< |
if (lseek(persistfd, 0L, 0) < 0) |
| 122 |
< |
error(SYSTEM, "seek error on persist file in pfhold"); |
| 120 |
> |
if (errfile == NULL && |
| 121 |
> |
mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 122 |
> |
goto createrr; |
| 123 |
> |
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
| 124 |
> |
inpname, outpname, errname); |
| 125 |
|
n = strlen(buf); |
| 126 |
|
if (write(persistfd, buf, n) < n) |
| 127 |
< |
error(SYSTEM, "error writing persist file in pfhold"); |
| 128 |
< |
/* wait for someone to signal us */ |
| 129 |
< |
signal(SIGALRM, sig_noop); |
| 130 |
< |
pflock(0); |
| 131 |
< |
pause(); |
| 132 |
< |
pflock(1); |
| 127 |
> |
error(SYSTEM, "error writing persist file"); |
| 128 |
> |
/* wait TIMELIM for someone to signal us */ |
| 129 |
> |
got_io = 0; |
| 130 |
> |
signal(SIGIO, sig_io); |
| 131 |
> |
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
| 132 |
> |
alarm(TIMELIM); |
| 133 |
> |
pflock(0); /* unlock persist file for attach */ |
| 134 |
> |
while (!got_io) |
| 135 |
> |
pause(); /* wait for attach */ |
| 136 |
> |
pflock(1); /* grab persist file right back! */ |
| 137 |
> |
alarm(0); /* turn off alarm */ |
| 138 |
> |
signal(SIGALRM, oldalrm); |
| 139 |
> |
signal(SIGIO, SIG_DFL); |
| 140 |
> |
if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0) |
| 141 |
> |
error(SYSTEM, "seek/truncate error on persist file"); |
| 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); |
| 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[512], *pfin, *pfout, *pferr; |
| 170 |
> |
int pid, pid2 = -1; |
| 171 |
> |
/* load persist file */ |
| 172 |
> |
while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { |
| 173 |
> |
pflock(0); |
| 174 |
> |
sleep(15); /* wait until ready */ |
| 175 |
> |
pflock(1); |
| 176 |
> |
} |
| 177 |
> |
pfdetach(); /* close persist file */ |
| 178 |
|
if (nr < 0) |
| 179 |
< |
error(SYSTEM, "cannot read persist file"); |
| 179 |
> |
error(SYSTEM, "error reading persist file"); |
| 180 |
|
buf[nr] = '\0'; |
| 181 |
< |
if ((cp = index(buf, '\n')) == NULL) |
| 181 |
> |
if ((cp = index(buf, ' ')) == NULL) |
| 182 |
|
goto formerr; |
| 183 |
|
*cp++ = '\0'; |
| 184 |
< |
if ((pid = atoi(buf)) <= 0) |
| 184 |
> |
if ((pid = atoi(cp)) <= 0) |
| 185 |
|
goto formerr; |
| 147 |
– |
pfin = cp; |
| 186 |
|
if ((cp = index(cp, '\n')) == NULL) |
| 187 |
|
goto formerr; |
| 188 |
+ |
pfin = ++cp; |
| 189 |
+ |
if ((cp = index(cp, '\n')) == NULL) |
| 190 |
+ |
goto formerr; |
| 191 |
|
*cp++ = '\0'; |
| 192 |
|
pfout = cp; |
| 193 |
|
if ((cp = index(cp, '\n')) == NULL) |
| 194 |
|
goto formerr; |
| 195 |
|
*cp++ = '\0'; |
| 196 |
+ |
pferr = cp; |
| 197 |
+ |
if ((cp = index(cp, '\n')) == NULL) |
| 198 |
+ |
goto formerr; |
| 199 |
+ |
*cp++ = '\0'; |
| 200 |
|
if (cp-buf != nr) |
| 201 |
|
goto formerr; |
| 202 |
+ |
if (strcmp(buf, progname)) { |
| 203 |
+ |
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
| 204 |
+ |
error(USER, errmsg); |
| 205 |
+ |
} |
| 206 |
|
/* wake up rendering process */ |
| 207 |
< |
if (kill(pid, SIGALRM) < 0) |
| 207 |
> |
if (kill(pid, SIGIO) < 0) |
| 208 |
|
error(SYSTEM, "cannot signal rendering process in io_process"); |
| 209 |
|
pid = fork(); /* fork i/o process */ |
| 210 |
|
if (pid < 0) |
| 211 |
< |
error(SYSTEM, "fork failed in io_process"); |
| 211 |
> |
goto forkerr; |
| 212 |
|
/* connect to appropriate pipe */ |
| 213 |
|
if (pid) { /* parent passes renderer output */ |
| 214 |
< |
if (freopen(pfout, "r", stdin) == NULL) |
| 215 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
| 214 |
> |
close(0); |
| 215 |
> |
if (pferr[0]) { |
| 216 |
> |
pid2 = fork(); /* fork another for stderr */ |
| 217 |
> |
if (pid2 < 0) |
| 218 |
> |
goto forkerr; |
| 219 |
> |
} |
| 220 |
> |
if (pid2) { /* parent is still stdout */ |
| 221 |
> |
if (open(pfout, O_RDONLY) != 0) |
| 222 |
> |
error(SYSTEM, |
| 223 |
> |
"cannot open output pipe in io_process"); |
| 224 |
> |
} else { /* second child is stderr */ |
| 225 |
> |
if (open(pferr, O_RDONLY) != 0) |
| 226 |
> |
error(SYSTEM, |
| 227 |
> |
"cannot open error pipe in io_process"); |
| 228 |
> |
dup2(2, 1); /* attach stdout to stderr */ |
| 229 |
> |
} |
| 230 |
|
} else { /* child passes renderer input */ |
| 231 |
< |
if (freopen(pfin, "w", stdout) == NULL) |
| 231 |
> |
close(1); |
| 232 |
> |
if (open(pfin, O_WRONLY) != 1) |
| 233 |
|
error(SYSTEM, "cannot open input pipe in io_process"); |
| 234 |
|
} |
| 235 |
|
/* pass input to output */ |
| 236 |
< |
cp = buf; n = sizeof(buf); |
| 237 |
< |
while ((nr = read(fileno(stdin), cp, n)) > 0) { |
| 238 |
< |
nr += cp-buf; |
| 239 |
< |
if ((n = write(fileno(stdout), buf, nr)) <= 0) |
| 240 |
< |
error(SYSTEM, "write error in io_process"); |
| 241 |
< |
cp = buf; |
| 242 |
< |
while (n < nr) |
| 243 |
< |
*cp++ = buf[n++]; |
| 244 |
< |
n = sizeof(buf) - (cp-buf); |
| 245 |
< |
} |
| 246 |
< |
fclose(stdin); |
| 247 |
< |
fclose(stdout); |
| 184 |
< |
if (pid) /* parent waits for child */ |
| 236 |
> |
/* read as much as we can, write all of it */ |
| 237 |
> |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) |
| 238 |
> |
do { |
| 239 |
> |
if ((n = write(1, cp, nr)) <= 0) |
| 240 |
> |
error(SYSTEM, "write error in io_process"); |
| 241 |
> |
cp += n; |
| 242 |
> |
} while ((nr -= n) > 0); |
| 243 |
> |
if (nr < 0) |
| 244 |
> |
error(SYSTEM, "read error in io_process"); |
| 245 |
> |
close(0); /* close input */ |
| 246 |
> |
close(1); /* close output */ |
| 247 |
> |
if (pid) /* parent waits for stdin child */ |
| 248 |
|
wait(0); |
| 249 |
< |
exit(0); /* all done, exit (not quit!) */ |
| 249 |
> |
if (pid2 > 0) /* wait for stderr child also */ |
| 250 |
> |
wait(0); |
| 251 |
> |
_exit(0); /* all done, exit (not quit!) */ |
| 252 |
|
formerr: |
| 253 |
|
error(USER, "format error in persist file"); |
| 254 |
+ |
forkerr: |
| 255 |
+ |
error(SYSTEM, "fork failed in io_process"); |
| 256 |
|
} |
| 257 |
+ |
|
| 258 |
+ |
#else |
| 259 |
+ |
|
| 260 |
+ |
pfclean() {} |
| 261 |
+ |
|
| 262 |
+ |
#endif |