| 1 |
greg |
2.1 |
/* Copyright (c) 1993 Regents of the University of California */ |
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint |
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
| 5 |
|
|
#endif |
| 6 |
|
|
|
| 7 |
|
|
/* |
| 8 |
|
|
* Routines for persistent rtrace and rpict processes. |
| 9 |
|
|
*/ |
| 10 |
|
|
|
| 11 |
|
|
#include "standard.h" |
| 12 |
|
|
#include "paths.h" |
| 13 |
|
|
#include <signal.h> |
| 14 |
|
|
#include <sys/types.h> |
| 15 |
|
|
#include <sys/stat.h> |
| 16 |
|
|
|
| 17 |
greg |
2.2 |
#ifndef TIMELIM |
| 18 |
|
|
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
| 19 |
|
|
#endif |
| 20 |
|
|
|
| 21 |
greg |
2.1 |
extern char *strcpy(), *index(); |
| 22 |
|
|
|
| 23 |
|
|
extern int headismine; /* boolean true if header belongs to me */ |
| 24 |
|
|
|
| 25 |
|
|
static char *persistfname = NULL; /* persist file name */ |
| 26 |
|
|
static int persistfd = -1; /* persist file descriptor */ |
| 27 |
|
|
|
| 28 |
|
|
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1]; |
| 29 |
|
|
|
| 30 |
|
|
|
| 31 |
|
|
pfdetach() /* release persist (and header) resources */ |
| 32 |
|
|
{ |
| 33 |
|
|
if (persistfd >= 0) |
| 34 |
|
|
close(persistfd); |
| 35 |
|
|
persistfd = -1; |
| 36 |
|
|
persistfname = NULL; |
| 37 |
|
|
inpname[0] = '\0'; |
| 38 |
|
|
outpname[0] = '\0'; |
| 39 |
|
|
headismine = 0; |
| 40 |
|
|
} |
| 41 |
|
|
|
| 42 |
|
|
|
| 43 |
|
|
pfclean() /* clean up persist files */ |
| 44 |
|
|
{ |
| 45 |
|
|
if (persistfd >= 0) |
| 46 |
|
|
close(persistfd); |
| 47 |
|
|
if (persistfname != NULL) |
| 48 |
|
|
unlink(persistfname); |
| 49 |
|
|
if (inpname[0]) |
| 50 |
|
|
unlink(inpname); |
| 51 |
|
|
if (outpname[0]) |
| 52 |
|
|
unlink(outpname); |
| 53 |
|
|
} |
| 54 |
|
|
|
| 55 |
|
|
|
| 56 |
|
|
pflock(lf) /* place or release exclusive lock on file */ |
| 57 |
|
|
int lf; |
| 58 |
|
|
{ |
| 59 |
|
|
struct flock fls; |
| 60 |
|
|
|
| 61 |
|
|
fls.l_type = lf ? F_WRLCK : F_UNLCK; |
| 62 |
|
|
fls.l_whence = 0; |
| 63 |
|
|
fls.l_start = 0L; |
| 64 |
|
|
fls.l_len = 0L; |
| 65 |
|
|
if (fcntl(persistfd, F_SETLKW, &fls) < 0) |
| 66 |
|
|
error(SYSTEM, "cannot (un)lock persist file"); |
| 67 |
|
|
} |
| 68 |
|
|
|
| 69 |
|
|
|
| 70 |
|
|
persistfile(pfn) /* open persist file and lock it */ |
| 71 |
|
|
char *pfn; |
| 72 |
|
|
{ |
| 73 |
|
|
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 74 |
|
|
if (persistfd >= 0) { |
| 75 |
|
|
persistfname = pfn; |
| 76 |
|
|
pflock(1); |
| 77 |
|
|
return; |
| 78 |
|
|
} |
| 79 |
|
|
/* file exists -- switch to i/o process */ |
| 80 |
|
|
persistfd = open(pfn, O_RDWR); |
| 81 |
|
|
if (persistfd < 0) { |
| 82 |
|
|
sprintf(errmsg, "cannot open persist file \"%s\"", pfn); |
| 83 |
|
|
error(SYSTEM, errmsg); |
| 84 |
|
|
} |
| 85 |
|
|
pflock(1); |
| 86 |
|
|
io_process(); /* never returns */ |
| 87 |
|
|
} |
| 88 |
|
|
|
| 89 |
|
|
|
| 90 |
greg |
2.2 |
int sig_noop() {} |
| 91 |
greg |
2.1 |
|
| 92 |
|
|
|
| 93 |
|
|
pfhold() /* holding pattern for idle rendering process */ |
| 94 |
|
|
{ |
| 95 |
|
|
char buf[128]; |
| 96 |
|
|
register int n; |
| 97 |
|
|
/* close input and output descriptors */ |
| 98 |
|
|
close(fileno(stdin)); |
| 99 |
|
|
close(fileno(stdout)); |
| 100 |
|
|
/* create named pipes for input and output */ |
| 101 |
|
|
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600) < 0) |
| 102 |
|
|
goto createrr; |
| 103 |
|
|
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0) |
| 104 |
|
|
goto createrr; |
| 105 |
|
|
sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname); |
| 106 |
|
|
if (lseek(persistfd, 0L, 0) < 0) |
| 107 |
|
|
error(SYSTEM, "seek error on persist file in pfhold"); |
| 108 |
|
|
n = strlen(buf); |
| 109 |
|
|
if (write(persistfd, buf, n) < n) |
| 110 |
|
|
error(SYSTEM, "error writing persist file in pfhold"); |
| 111 |
greg |
2.2 |
/* wait TIMELIM for someone to signal us */ |
| 112 |
|
|
signal(SIGIO, sig_noop); |
| 113 |
|
|
alarm(TIMELIM); |
| 114 |
greg |
2.1 |
pflock(0); |
| 115 |
|
|
pause(); |
| 116 |
greg |
2.2 |
alarm(0); |
| 117 |
|
|
signal(SIGIO, SIG_DFL); |
| 118 |
greg |
2.1 |
pflock(1); |
| 119 |
|
|
/* someone wants us; reopen stdin and stdout */ |
| 120 |
|
|
if (freopen(inpname, "r", stdin) == NULL) |
| 121 |
|
|
goto openerr; |
| 122 |
|
|
if (freopen(outpname, "w", stdout) == NULL) |
| 123 |
|
|
goto openerr; |
| 124 |
|
|
unlink(inpname); |
| 125 |
|
|
inpname[0] = '\0'; |
| 126 |
|
|
unlink(outpname); |
| 127 |
|
|
outpname[0] = '\0'; |
| 128 |
greg |
2.3 |
clean_slate(); /* reset time and ray counters */ |
| 129 |
greg |
2.1 |
return; |
| 130 |
|
|
createrr: |
| 131 |
|
|
error(SYSTEM, "cannot create named pipes in pfhold"); |
| 132 |
|
|
openerr: |
| 133 |
|
|
error(SYSTEM, "cannot open named pipes in pfhold"); |
| 134 |
|
|
} |
| 135 |
|
|
|
| 136 |
|
|
|
| 137 |
|
|
io_process() /* just act as conduits to and from actual process */ |
| 138 |
|
|
{ |
| 139 |
|
|
register char *cp; |
| 140 |
|
|
register int nr, n; |
| 141 |
|
|
char buf[4096], *pfin, *pfout; |
| 142 |
|
|
int pid; |
| 143 |
|
|
/* load and close persist file */ |
| 144 |
|
|
nr = read(persistfd, buf, sizeof(buf)); |
| 145 |
|
|
pfdetach(); |
| 146 |
|
|
if (nr < 0) |
| 147 |
|
|
error(SYSTEM, "cannot read persist file"); |
| 148 |
|
|
buf[nr] = '\0'; |
| 149 |
|
|
if ((cp = index(buf, '\n')) == NULL) |
| 150 |
|
|
goto formerr; |
| 151 |
|
|
*cp++ = '\0'; |
| 152 |
|
|
if ((pid = atoi(buf)) <= 0) |
| 153 |
|
|
goto formerr; |
| 154 |
|
|
pfin = cp; |
| 155 |
|
|
if ((cp = index(cp, '\n')) == NULL) |
| 156 |
|
|
goto formerr; |
| 157 |
|
|
*cp++ = '\0'; |
| 158 |
|
|
pfout = cp; |
| 159 |
|
|
if ((cp = index(cp, '\n')) == NULL) |
| 160 |
|
|
goto formerr; |
| 161 |
|
|
*cp++ = '\0'; |
| 162 |
|
|
if (cp-buf != nr) |
| 163 |
|
|
goto formerr; |
| 164 |
|
|
/* wake up rendering process */ |
| 165 |
greg |
2.2 |
if (kill(pid, SIGIO) < 0) |
| 166 |
greg |
2.1 |
error(SYSTEM, "cannot signal rendering process in io_process"); |
| 167 |
|
|
pid = fork(); /* fork i/o process */ |
| 168 |
|
|
if (pid < 0) |
| 169 |
|
|
error(SYSTEM, "fork failed in io_process"); |
| 170 |
|
|
/* connect to appropriate pipe */ |
| 171 |
|
|
if (pid) { /* parent passes renderer output */ |
| 172 |
|
|
if (freopen(pfout, "r", stdin) == NULL) |
| 173 |
|
|
error(SYSTEM, "cannot open input pipe in io_process"); |
| 174 |
|
|
} else { /* child passes renderer input */ |
| 175 |
|
|
if (freopen(pfin, "w", stdout) == NULL) |
| 176 |
|
|
error(SYSTEM, "cannot open input pipe in io_process"); |
| 177 |
|
|
} |
| 178 |
|
|
/* pass input to output */ |
| 179 |
|
|
cp = buf; n = sizeof(buf); |
| 180 |
|
|
while ((nr = read(fileno(stdin), cp, n)) > 0) { |
| 181 |
|
|
nr += cp-buf; |
| 182 |
|
|
if ((n = write(fileno(stdout), buf, nr)) <= 0) |
| 183 |
|
|
error(SYSTEM, "write error in io_process"); |
| 184 |
|
|
cp = buf; |
| 185 |
|
|
while (n < nr) |
| 186 |
|
|
*cp++ = buf[n++]; |
| 187 |
|
|
n = sizeof(buf) - (cp-buf); |
| 188 |
|
|
} |
| 189 |
|
|
fclose(stdin); |
| 190 |
|
|
fclose(stdout); |
| 191 |
|
|
if (pid) /* parent waits for child */ |
| 192 |
|
|
wait(0); |
| 193 |
|
|
exit(0); /* all done, exit (not quit!) */ |
| 194 |
|
|
formerr: |
| 195 |
|
|
error(USER, "format error in persist file"); |
| 196 |
|
|
} |