| 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> |
| 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 |
|
|
| 75 |
|
persistfile(pfn) /* open persist file and lock it */ |
| 76 |
|
char *pfn; |
| 77 |
|
{ |
| 78 |
< |
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 78 |
> |
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); |
| 79 |
|
if (persistfd >= 0) { |
| 80 |
|
persistfname = pfn; |
| 81 |
|
pflock(1); |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
< |
int sig_noop() {} |
| 95 |
> |
static int got_io; |
| 96 |
|
|
| 97 |
+ |
static int sig_io() { got_io++; } |
| 98 |
|
|
| 99 |
+ |
static int sig_alrm() { quit(0); } |
| 100 |
+ |
|
| 101 |
+ |
|
| 102 |
|
pfhold() /* holding pattern for idle rendering process */ |
| 103 |
|
{ |
| 104 |
< |
char buf[128]; |
| 104 |
> |
int (*oldalrm)(); |
| 105 |
> |
char buf[512]; |
| 106 |
|
register int n; |
| 107 |
|
/* close input and output descriptors */ |
| 108 |
|
close(fileno(stdin)); |
| 99 |
– |
fflush(stdout); |
| 109 |
|
close(fileno(stdout)); |
| 110 |
|
/* create named pipes for input and output */ |
| 111 |
< |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600) < 0) |
| 111 |
> |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 112 |
|
goto createrr; |
| 113 |
< |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0) |
| 113 |
> |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 114 |
|
goto createrr; |
| 115 |
< |
sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname); |
| 116 |
< |
if (lseek(persistfd, 0L, 0) < 0) |
| 117 |
< |
error(SYSTEM, "seek error on persist file in pfhold"); |
| 115 |
> |
sprintf(buf, "%s %d\n%s\n%s\n", progname, getpid(), inpname, outpname); |
| 116 |
> |
if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0) |
| 117 |
> |
error(SYSTEM, "seek/truncate error on persist file"); |
| 118 |
|
n = strlen(buf); |
| 119 |
|
if (write(persistfd, buf, n) < n) |
| 120 |
< |
error(SYSTEM, "error writing persist file in pfhold"); |
| 120 |
> |
error(SYSTEM, "error writing persist file"); |
| 121 |
|
/* wait TIMELIM for someone to signal us */ |
| 122 |
< |
signal(SIGIO, sig_noop); |
| 122 |
> |
got_io = 0; |
| 123 |
> |
signal(SIGIO, sig_io); |
| 124 |
> |
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
| 125 |
|
alarm(TIMELIM); |
| 126 |
|
pflock(0); |
| 127 |
< |
pause(); |
| 127 |
> |
while (!got_io) |
| 128 |
> |
pause(); |
| 129 |
|
alarm(0); |
| 130 |
+ |
signal(SIGALRM, oldalrm); |
| 131 |
|
signal(SIGIO, SIG_DFL); |
| 132 |
|
pflock(1); |
| 133 |
|
/* someone wants us; reopen stdin and stdout */ |
| 151 |
|
{ |
| 152 |
|
register char *cp; |
| 153 |
|
register int nr, n; |
| 154 |
< |
char buf[4096], *pfin, *pfout; |
| 154 |
> |
char buf[512], *pfin, *pfout; |
| 155 |
|
int pid; |
| 156 |
|
/* load and close persist file */ |
| 157 |
< |
nr = read(persistfd, buf, sizeof(buf)); |
| 157 |
> |
sleep(5); /* helps synchronization */ |
| 158 |
> |
nr = read(persistfd, buf, sizeof(buf)-1); |
| 159 |
|
pfdetach(); |
| 160 |
< |
if (nr < 0) |
| 160 |
> |
if (nr <= 0) |
| 161 |
|
error(SYSTEM, "cannot read persist file"); |
| 162 |
|
buf[nr] = '\0'; |
| 163 |
< |
if ((cp = index(buf, '\n')) == NULL) |
| 163 |
> |
if ((cp = index(buf, ' ')) == NULL) |
| 164 |
|
goto formerr; |
| 165 |
|
*cp++ = '\0'; |
| 166 |
< |
if ((pid = atoi(buf)) <= 0) |
| 166 |
> |
if ((pid = atoi(cp)) <= 0) |
| 167 |
|
goto formerr; |
| 154 |
– |
pfin = cp; |
| 168 |
|
if ((cp = index(cp, '\n')) == NULL) |
| 169 |
|
goto formerr; |
| 170 |
+ |
pfin = ++cp; |
| 171 |
+ |
if ((cp = index(cp, '\n')) == NULL) |
| 172 |
+ |
goto formerr; |
| 173 |
|
*cp++ = '\0'; |
| 174 |
|
pfout = cp; |
| 175 |
|
if ((cp = index(cp, '\n')) == NULL) |
| 177 |
|
*cp++ = '\0'; |
| 178 |
|
if (cp-buf != nr) |
| 179 |
|
goto formerr; |
| 180 |
+ |
if (strcmp(buf, progname)) { |
| 181 |
+ |
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
| 182 |
+ |
error(USER, errmsg); |
| 183 |
+ |
} |
| 184 |
|
/* wake up rendering process */ |
| 185 |
|
if (kill(pid, SIGIO) < 0) |
| 186 |
|
error(SYSTEM, "cannot signal rendering process in io_process"); |
| 189 |
|
error(SYSTEM, "fork failed in io_process"); |
| 190 |
|
/* connect to appropriate pipe */ |
| 191 |
|
if (pid) { /* parent passes renderer output */ |
| 192 |
< |
if (freopen(pfout, "r", stdin) == NULL) |
| 192 |
> |
close(0); |
| 193 |
> |
if (open(pfout, O_RDONLY) != 0) |
| 194 |
|
error(SYSTEM, "cannot open input pipe in io_process"); |
| 195 |
|
} else { /* child passes renderer input */ |
| 196 |
< |
if (freopen(pfin, "w", stdout) == NULL) |
| 197 |
< |
error(SYSTEM, "cannot open input pipe in io_process"); |
| 196 |
> |
close(1); |
| 197 |
> |
if (open(pfin, O_WRONLY) != 1) |
| 198 |
> |
error(SYSTEM, "cannot open output pipe in io_process"); |
| 199 |
|
} |
| 200 |
|
/* pass input to output */ |
| 201 |
< |
cp = buf; n = sizeof(buf); |
| 202 |
< |
while ((nr = read(fileno(stdin), cp, n)) > 0) { |
| 203 |
< |
nr += cp-buf; |
| 204 |
< |
if ((n = write(fileno(stdout), buf, nr)) <= 0) |
| 205 |
< |
error(SYSTEM, "write error in io_process"); |
| 206 |
< |
cp = buf; |
| 207 |
< |
while (n < nr) |
| 208 |
< |
*cp++ = buf[n++]; |
| 209 |
< |
n = sizeof(buf) - (cp-buf); |
| 210 |
< |
} |
| 211 |
< |
fclose(stdin); |
| 190 |
< |
fclose(stdout); |
| 201 |
> |
/* read as much as we can, write all of it */ |
| 202 |
> |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) |
| 203 |
> |
do { |
| 204 |
> |
if ((n = write(1, cp, nr)) <= 0) |
| 205 |
> |
goto writerr; |
| 206 |
> |
cp += n; |
| 207 |
> |
} while ((nr -= n) > 0); |
| 208 |
> |
if (nr < 0) |
| 209 |
> |
error(SYSTEM, "read error in io_process"); |
| 210 |
> |
close(0); /* close input */ |
| 211 |
> |
close(1); /* close output */ |
| 212 |
|
if (pid) /* parent waits for child */ |
| 213 |
|
wait(0); |
| 214 |
|
exit(0); /* all done, exit (not quit!) */ |
| 215 |
|
formerr: |
| 216 |
|
error(USER, "format error in persist file"); |
| 217 |
+ |
writerr: |
| 218 |
+ |
error(SYSTEM, "write error in io_process"); |
| 219 |
|
} |
| 220 |
+ |
|
| 221 |
+ |
#else |
| 222 |
+ |
|
| 223 |
+ |
pfclean() {} |
| 224 |
+ |
|
| 225 |
+ |
#endif |