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