| 1 |
< |
/* Copyright (c) 1993 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1995 Regents of the University of California */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
| 25 |
|
|
| 26 |
|
extern int headismine; /* boolean true if header belongs to me */ |
| 27 |
|
|
| 28 |
+ |
extern char *progname; /* global program name */ |
| 29 |
+ |
|
| 30 |
|
static char *persistfname = NULL; /* persist file name */ |
| 31 |
|
static int persistfd = -1; /* persist file descriptor */ |
| 32 |
|
|
| 97 |
|
|
| 98 |
|
pfhold() /* holding pattern for idle rendering process */ |
| 99 |
|
{ |
| 100 |
< |
char buf[128]; |
| 100 |
> |
char buf[512]; |
| 101 |
|
register int n; |
| 102 |
|
/* close input and output descriptors */ |
| 103 |
|
close(fileno(stdin)); |
| 104 |
|
close(fileno(stdout)); |
| 105 |
|
/* create named pipes for input and output */ |
| 106 |
< |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600) < 0) |
| 106 |
> |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 107 |
|
goto createrr; |
| 108 |
< |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0) |
| 108 |
> |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 109 |
|
goto createrr; |
| 110 |
< |
sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname); |
| 111 |
< |
if (lseek(persistfd, 0L, 0) < 0) |
| 112 |
< |
error(SYSTEM, "seek error on persist file in pfhold"); |
| 110 |
> |
sprintf(buf, "%s %d\n%s\n%s\n", progname, getpid(), inpname, outpname); |
| 111 |
> |
if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0) |
| 112 |
> |
error(SYSTEM, "seek/truncate error on persist file"); |
| 113 |
|
n = strlen(buf); |
| 114 |
|
if (write(persistfd, buf, n) < n) |
| 115 |
< |
error(SYSTEM, "error writing persist file in pfhold"); |
| 115 |
> |
error(SYSTEM, "error writing persist file"); |
| 116 |
|
/* wait TIMELIM for someone to signal us */ |
| 117 |
|
signal(SIGIO, sig_noop); |
| 118 |
|
alarm(TIMELIM); |
| 142 |
|
{ |
| 143 |
|
register char *cp; |
| 144 |
|
register int nr, n; |
| 145 |
< |
char buf[4096], *pfin, *pfout; |
| 145 |
> |
char buf[512], *pfin, *pfout; |
| 146 |
|
int pid; |
| 147 |
|
/* load and close persist file */ |
| 148 |
< |
nr = read(persistfd, buf, sizeof(buf)); |
| 148 |
> |
nr = read(persistfd, buf, sizeof(buf)-1); |
| 149 |
|
pfdetach(); |
| 150 |
|
if (nr < 0) |
| 151 |
|
error(SYSTEM, "cannot read persist file"); |
| 152 |
|
buf[nr] = '\0'; |
| 153 |
< |
if ((cp = index(buf, '\n')) == NULL) |
| 153 |
> |
if ((cp = index(buf, ' ')) == NULL) |
| 154 |
|
goto formerr; |
| 155 |
|
*cp++ = '\0'; |
| 156 |
< |
if ((pid = atoi(buf)) <= 0) |
| 156 |
> |
if ((pid = atoi(cp)) <= 0) |
| 157 |
|
goto formerr; |
| 156 |
– |
pfin = cp; |
| 158 |
|
if ((cp = index(cp, '\n')) == NULL) |
| 159 |
|
goto formerr; |
| 160 |
+ |
pfin = ++cp; |
| 161 |
+ |
if ((cp = index(cp, '\n')) == NULL) |
| 162 |
+ |
goto formerr; |
| 163 |
|
*cp++ = '\0'; |
| 164 |
|
pfout = cp; |
| 165 |
|
if ((cp = index(cp, '\n')) == NULL) |
| 167 |
|
*cp++ = '\0'; |
| 168 |
|
if (cp-buf != nr) |
| 169 |
|
goto formerr; |
| 170 |
+ |
if (strcmp(buf, progname)) { |
| 171 |
+ |
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
| 172 |
+ |
error(USER, errmsg); |
| 173 |
+ |
} |
| 174 |
|
/* wake up rendering process */ |
| 175 |
|
if (kill(pid, SIGIO) < 0) |
| 176 |
|
error(SYSTEM, "cannot signal rendering process in io_process"); |
| 179 |
|
error(SYSTEM, "fork failed in io_process"); |
| 180 |
|
/* connect to appropriate pipe */ |
| 181 |
|
if (pid) { /* parent passes renderer output */ |
| 182 |
< |
if (freopen(pfout, "r", stdin) == NULL) |
| 182 |
> |
close(0); |
| 183 |
> |
if (open(pfout, O_RDONLY) != 0) |
| 184 |
|
error(SYSTEM, "cannot open input pipe in io_process"); |
| 185 |
|
} else { /* child passes renderer input */ |
| 186 |
< |
if (freopen(pfin, "w", stdout) == NULL) |
| 187 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
| 186 |
> |
close(1); |
| 187 |
> |
if (open(pfin, O_WRONLY) != 1) |
| 188 |
> |
error(SYSTEM, "cannot open output pipe in io_process"); |
| 189 |
|
} |
| 190 |
|
/* pass input to output */ |
| 191 |
< |
cp = buf; n = sizeof(buf); |
| 192 |
< |
while ((nr = read(fileno(stdin), cp, n)) > 0) { |
| 193 |
< |
nr += cp-buf; |
| 194 |
< |
if ((n = write(fileno(stdout), buf, nr)) <= 0) |
| 195 |
< |
error(SYSTEM, "write error in io_process"); |
| 196 |
< |
cp = buf; |
| 197 |
< |
while (n < nr) |
| 198 |
< |
*cp++ = buf[n++]; |
| 199 |
< |
n = sizeof(buf) - (cp-buf); |
| 200 |
< |
} |
| 201 |
< |
fclose(stdin); |
| 192 |
< |
fclose(stdout); |
| 191 |
> |
/* read as much as we can, write all of it */ |
| 192 |
> |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) |
| 193 |
> |
do { |
| 194 |
> |
if ((n = write(1, cp, nr)) <= 0) |
| 195 |
> |
goto writerr; |
| 196 |
> |
cp += n; |
| 197 |
> |
} while ((nr -= n) > 0); |
| 198 |
> |
if (nr < 0) |
| 199 |
> |
error(SYSTEM, "read error in io_process"); |
| 200 |
> |
close(0); /* close input */ |
| 201 |
> |
close(1); /* close output */ |
| 202 |
|
if (pid) /* parent waits for child */ |
| 203 |
|
wait(0); |
| 204 |
|
exit(0); /* all done, exit (not quit!) */ |
| 205 |
|
formerr: |
| 206 |
|
error(USER, "format error in persist file"); |
| 207 |
+ |
writerr: |
| 208 |
+ |
error(SYSTEM, "write error in io_process"); |
| 209 |
|
} |
| 210 |
+ |
|
| 211 |
+ |
#else |
| 212 |
+ |
|
| 213 |
+ |
pfclean() {} |
| 214 |
|
|
| 215 |
|
#endif |