| 1 |
greg |
2.1 |
#ifndef lint |
| 2 |
schorsch |
2.40 |
static const char RCSid[] = "$Id: persist.c,v 2.39 2008/04/21 07:31:30 schorsch Exp $"; |
| 3 |
greg |
2.1 |
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* Routines for persistent rtrace and rpict processes. |
| 6 |
greg |
2.26 |
* |
| 7 |
|
|
* External symbols declared in ray.h |
| 8 |
|
|
*/ |
| 9 |
|
|
|
| 10 |
greg |
2.27 |
#include "copyright.h" |
| 11 |
greg |
2.1 |
|
| 12 |
schorsch |
2.29 |
#include <string.h> |
| 13 |
|
|
#include <signal.h> |
| 14 |
|
|
#include <sys/stat.h> |
| 15 |
schorsch |
2.38 |
#include <sys/types.h> |
| 16 |
schorsch |
2.40 |
|
| 17 |
|
|
#include "platform.h" |
| 18 |
schorsch |
2.39 |
#ifndef NON_POSIX /* XXX need abstraction for process management */ |
| 19 |
|
|
#include <sys/wait.h> |
| 20 |
|
|
#endif |
| 21 |
schorsch |
2.29 |
|
| 22 |
schorsch |
2.30 |
#include "rtprocess.h" /* getpid() */ |
| 23 |
greg |
2.1 |
#include "standard.h" |
| 24 |
gregl |
2.25 |
#include "random.h" |
| 25 |
schorsch |
2.34 |
#include "ray.h" |
| 26 |
greg |
2.6 |
|
| 27 |
|
|
#ifdef F_SETLKW |
| 28 |
greg |
2.1 |
#include "paths.h" |
| 29 |
gregl |
2.21 |
#include "selcall.h" |
| 30 |
greg |
2.1 |
|
| 31 |
greg |
2.2 |
#ifndef TIMELIM |
| 32 |
|
|
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
| 33 |
greg |
2.26 |
#endif |
| 34 |
|
|
|
| 35 |
greg |
2.1 |
extern int headismine; /* boolean true if header belongs to me */ |
| 36 |
greg |
2.12 |
extern char *progname; /* global program name */ |
| 37 |
greg |
2.14 |
extern char *errfile; /* global error file name */ |
| 38 |
greg |
2.1 |
static char *persistfname = NULL; /* persist file name */ |
| 39 |
|
|
static int persistfd = -1; /* persist file descriptor */ |
| 40 |
schorsch |
2.34 |
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; |
| 41 |
greg |
2.1 |
|
| 42 |
schorsch |
2.34 |
typedef void (sighandler_t)(int); |
| 43 |
|
|
static sighandler_t sig_io; |
| 44 |
|
|
static sighandler_t sig_alrm; |
| 45 |
greg |
2.1 |
|
| 46 |
|
|
|
| 47 |
schorsch |
2.34 |
extern void |
| 48 |
|
|
pfdetach(void) /* release persist (and header) resources */ |
| 49 |
greg |
2.1 |
{ |
| 50 |
|
|
if (persistfd >= 0) |
| 51 |
|
|
close(persistfd); |
| 52 |
|
|
persistfd = -1; |
| 53 |
|
|
persistfname = NULL; |
| 54 |
|
|
inpname[0] = '\0'; |
| 55 |
|
|
outpname[0] = '\0'; |
| 56 |
greg |
2.14 |
errname[0] = '\0'; |
| 57 |
greg |
2.1 |
headismine = 0; |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
|
| 61 |
schorsch |
2.34 |
extern void |
| 62 |
|
|
pfclean(void) /* clean up persist files */ |
| 63 |
greg |
2.1 |
{ |
| 64 |
|
|
if (persistfd >= 0) |
| 65 |
|
|
close(persistfd); |
| 66 |
|
|
if (persistfname != NULL) |
| 67 |
|
|
unlink(persistfname); |
| 68 |
|
|
if (inpname[0]) |
| 69 |
|
|
unlink(inpname); |
| 70 |
|
|
if (outpname[0]) |
| 71 |
|
|
unlink(outpname); |
| 72 |
greg |
2.14 |
if (errname[0]) |
| 73 |
|
|
unlink(errname); |
| 74 |
greg |
2.1 |
} |
| 75 |
|
|
|
| 76 |
|
|
|
| 77 |
schorsch |
2.34 |
extern void |
| 78 |
|
|
pflock( /* place or release exclusive lock on file */ |
| 79 |
|
|
int lf |
| 80 |
|
|
) |
| 81 |
greg |
2.1 |
{ |
| 82 |
|
|
struct flock fls; |
| 83 |
|
|
|
| 84 |
|
|
fls.l_type = lf ? F_WRLCK : F_UNLCK; |
| 85 |
|
|
fls.l_whence = 0; |
| 86 |
|
|
fls.l_start = 0L; |
| 87 |
|
|
fls.l_len = 0L; |
| 88 |
|
|
if (fcntl(persistfd, F_SETLKW, &fls) < 0) |
| 89 |
|
|
error(SYSTEM, "cannot (un)lock persist file"); |
| 90 |
|
|
} |
| 91 |
|
|
|
| 92 |
|
|
|
| 93 |
schorsch |
2.34 |
extern void |
| 94 |
|
|
persistfile( /* open persist file and lock it */ |
| 95 |
|
|
char *pfn |
| 96 |
|
|
) |
| 97 |
greg |
2.1 |
{ |
| 98 |
greg |
2.13 |
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); |
| 99 |
greg |
2.1 |
if (persistfd >= 0) { |
| 100 |
|
|
persistfname = pfn; |
| 101 |
|
|
pflock(1); |
| 102 |
|
|
return; |
| 103 |
|
|
} |
| 104 |
|
|
/* file exists -- switch to i/o process */ |
| 105 |
|
|
persistfd = open(pfn, O_RDWR); |
| 106 |
|
|
if (persistfd < 0) { |
| 107 |
|
|
sprintf(errmsg, "cannot open persist file \"%s\"", pfn); |
| 108 |
|
|
error(SYSTEM, errmsg); |
| 109 |
|
|
} |
| 110 |
|
|
pflock(1); |
| 111 |
|
|
io_process(); /* never returns */ |
| 112 |
|
|
} |
| 113 |
|
|
|
| 114 |
|
|
|
| 115 |
greg |
2.13 |
static int got_io; |
| 116 |
greg |
2.1 |
|
| 117 |
schorsch |
2.34 |
static void sig_io(int i) { got_io++; } |
| 118 |
greg |
2.1 |
|
| 119 |
schorsch |
2.34 |
static void sig_alrm(int i) { quit(0); } |
| 120 |
greg |
2.13 |
|
| 121 |
|
|
|
| 122 |
schorsch |
2.34 |
extern void |
| 123 |
|
|
pfhold(void) /* holding pattern for idle rendering process */ |
| 124 |
greg |
2.1 |
{ |
| 125 |
schorsch |
2.34 |
sighandler_t *oldalrm; |
| 126 |
greg |
2.12 |
char buf[512]; |
| 127 |
greg |
2.1 |
register int n; |
| 128 |
|
|
/* close input and output descriptors */ |
| 129 |
greg |
2.37 |
close(0); |
| 130 |
|
|
close(1); |
| 131 |
greg |
2.16 |
if (errfile == NULL) |
| 132 |
greg |
2.37 |
close(2); |
| 133 |
greg |
2.1 |
/* create named pipes for input and output */ |
| 134 |
greg |
2.26 |
if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0) |
| 135 |
greg |
2.1 |
goto createrr; |
| 136 |
greg |
2.26 |
if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0) |
| 137 |
greg |
2.1 |
goto createrr; |
| 138 |
greg |
2.14 |
if (errfile == NULL && |
| 139 |
greg |
2.26 |
mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0) |
| 140 |
greg |
2.14 |
goto createrr; |
| 141 |
|
|
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
| 142 |
|
|
inpname, outpname, errname); |
| 143 |
greg |
2.1 |
n = strlen(buf); |
| 144 |
|
|
if (write(persistfd, buf, n) < n) |
| 145 |
greg |
2.10 |
error(SYSTEM, "error writing persist file"); |
| 146 |
greg |
2.32 |
lseek(persistfd, (off_t)0, SEEK_SET); |
| 147 |
greg |
2.2 |
/* wait TIMELIM for someone to signal us */ |
| 148 |
greg |
2.13 |
got_io = 0; |
| 149 |
|
|
signal(SIGIO, sig_io); |
| 150 |
schorsch |
2.34 |
oldalrm = signal(SIGALRM, sig_alrm); |
| 151 |
greg |
2.2 |
alarm(TIMELIM); |
| 152 |
greg |
2.15 |
pflock(0); /* unlock persist file for attach */ |
| 153 |
greg |
2.13 |
while (!got_io) |
| 154 |
greg |
2.15 |
pause(); /* wait for attach */ |
| 155 |
|
|
alarm(0); /* turn off alarm */ |
| 156 |
greg |
2.13 |
signal(SIGALRM, oldalrm); |
| 157 |
greg |
2.2 |
signal(SIGIO, SIG_DFL); |
| 158 |
greg |
2.17 |
pflock(1); /* grab persist file back */ |
| 159 |
greg |
2.1 |
/* someone wants us; reopen stdin and stdout */ |
| 160 |
greg |
2.26 |
/* |
| 161 |
greg |
2.1 |
if (freopen(inpname, "r", stdin) == NULL) |
| 162 |
|
|
goto openerr; |
| 163 |
|
|
if (freopen(outpname, "w", stdout) == NULL) |
| 164 |
|
|
goto openerr; |
| 165 |
greg |
2.26 |
*/ |
| 166 |
|
|
close(0); |
| 167 |
|
|
if (open(inpname, O_RDONLY) != 0) |
| 168 |
|
|
error(INTERNAL, "unexpected stdin file number"); |
| 169 |
|
|
clearerr(stdin); |
| 170 |
|
|
close(1); |
| 171 |
|
|
if (open(outpname, O_WRONLY) != 1) |
| 172 |
|
|
error(INTERNAL, "unexpected stdout file number"); |
| 173 |
gregl |
2.22 |
sleep(3); /* give them a chance to open their pipes */ |
| 174 |
greg |
2.14 |
if (errname[0]) { |
| 175 |
greg |
2.26 |
close(2); |
| 176 |
|
|
if (open(errname, O_WRONLY) != 2) |
| 177 |
|
|
error(INTERNAL, "unexpected stderr file number"); |
| 178 |
greg |
2.14 |
unlink(errname); |
| 179 |
|
|
errname[0] = '\0'; |
| 180 |
|
|
} |
| 181 |
greg |
2.1 |
unlink(inpname); |
| 182 |
|
|
inpname[0] = '\0'; |
| 183 |
|
|
unlink(outpname); |
| 184 |
|
|
outpname[0] = '\0'; |
| 185 |
|
|
return; |
| 186 |
|
|
createrr: |
| 187 |
|
|
error(SYSTEM, "cannot create named pipes in pfhold"); |
| 188 |
|
|
openerr: |
| 189 |
|
|
error(SYSTEM, "cannot open named pipes in pfhold"); |
| 190 |
|
|
} |
| 191 |
|
|
|
| 192 |
|
|
|
| 193 |
schorsch |
2.34 |
extern void |
| 194 |
|
|
io_process(void) /* just act as go-between for actual process */ |
| 195 |
greg |
2.1 |
{ |
| 196 |
|
|
register char *cp; |
| 197 |
|
|
register int nr, n; |
| 198 |
greg |
2.16 |
char buf[BUFSIZ], *pfin, *pfout, *pferr; |
| 199 |
|
|
int pid, nfds; |
| 200 |
gregl |
2.22 |
int fdout, fderr = -1; |
| 201 |
gregl |
2.23 |
int status = 0; |
| 202 |
gregl |
2.22 |
fd_set readfds, excepfds; |
| 203 |
greg |
2.15 |
/* load persist file */ |
| 204 |
greg |
2.17 |
n = 40; |
| 205 |
greg |
2.15 |
while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) { |
| 206 |
greg |
2.17 |
if (!n--) |
| 207 |
|
|
error(USER, "unattended persist file?"); |
| 208 |
greg |
2.15 |
pflock(0); |
| 209 |
gregl |
2.25 |
sleep(3+(3*getpid()+random())%13); /* wait until ready */ |
| 210 |
greg |
2.15 |
pflock(1); |
| 211 |
|
|
} |
| 212 |
|
|
if (nr < 0) |
| 213 |
greg |
2.14 |
error(SYSTEM, "error reading persist file"); |
| 214 |
schorsch |
2.28 |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
| 215 |
greg |
2.26 |
ftruncate(persistfd, (off_t)0L); /* truncate persist file */ |
| 216 |
schorsch |
2.28 |
#endif |
| 217 |
greg |
2.17 |
pfdetach(); /* close & release persist file */ |
| 218 |
|
|
buf[nr] = '\0'; /* parse what we got */ |
| 219 |
schorsch |
2.29 |
if ((cp = strchr(buf, ' ')) == NULL) |
| 220 |
greg |
2.1 |
goto formerr; |
| 221 |
|
|
*cp++ = '\0'; |
| 222 |
greg |
2.12 |
if ((pid = atoi(cp)) <= 0) |
| 223 |
greg |
2.1 |
goto formerr; |
| 224 |
schorsch |
2.29 |
if ((cp = strchr(cp, '\n')) == NULL) |
| 225 |
greg |
2.1 |
goto formerr; |
| 226 |
greg |
2.12 |
pfin = ++cp; |
| 227 |
schorsch |
2.29 |
if ((cp = strchr(cp, '\n')) == NULL) |
| 228 |
greg |
2.12 |
goto formerr; |
| 229 |
greg |
2.1 |
*cp++ = '\0'; |
| 230 |
|
|
pfout = cp; |
| 231 |
schorsch |
2.29 |
if ((cp = strchr(cp, '\n')) == NULL) |
| 232 |
greg |
2.1 |
goto formerr; |
| 233 |
|
|
*cp++ = '\0'; |
| 234 |
greg |
2.14 |
pferr = cp; |
| 235 |
schorsch |
2.29 |
if ((cp = strchr(cp, '\n')) == NULL) |
| 236 |
greg |
2.14 |
goto formerr; |
| 237 |
|
|
*cp++ = '\0'; |
| 238 |
greg |
2.1 |
if (cp-buf != nr) |
| 239 |
|
|
goto formerr; |
| 240 |
greg |
2.12 |
if (strcmp(buf, progname)) { |
| 241 |
|
|
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
| 242 |
|
|
error(USER, errmsg); |
| 243 |
|
|
} |
| 244 |
greg |
2.16 |
/* wake up rendering process */ |
| 245 |
greg |
2.2 |
if (kill(pid, SIGIO) < 0) |
| 246 |
greg |
2.1 |
error(SYSTEM, "cannot signal rendering process in io_process"); |
| 247 |
gregl |
2.22 |
/* fork child feeder process */ |
| 248 |
|
|
pid = fork(); |
| 249 |
|
|
if (pid < 0) |
| 250 |
|
|
error(SYSTEM, "fork failed in io_process"); |
| 251 |
|
|
if (pid == 0) { /* feeder loop */ |
| 252 |
|
|
int fdin; |
| 253 |
|
|
close(1); /* open input pipe */ |
| 254 |
|
|
if ((fdin = open(pfin, O_WRONLY)) < 0) |
| 255 |
|
|
error(SYSTEM, "cannot open feed pipe in io_process"); |
| 256 |
|
|
/* renderer stdin */ |
| 257 |
|
|
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) { |
| 258 |
|
|
do { |
| 259 |
|
|
if ((n = write(fdin, cp, nr)) <= 0) |
| 260 |
|
|
goto writerr; |
| 261 |
|
|
cp += n; |
| 262 |
|
|
} while ((nr -= n) > 0); |
| 263 |
|
|
} |
| 264 |
|
|
if (nr < 0) |
| 265 |
|
|
goto readerr; |
| 266 |
|
|
_exit(0); |
| 267 |
|
|
} |
| 268 |
|
|
close(0); |
| 269 |
|
|
/* open output pipes, in order */ |
| 270 |
greg |
2.16 |
if ((fdout = open(pfout, O_RDONLY)) < 0) |
| 271 |
|
|
error(SYSTEM, "cannot open output pipe in io_process"); |
| 272 |
|
|
if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0) |
| 273 |
|
|
error(SYSTEM, "cannot open error pipe in io_process"); |
| 274 |
gregl |
2.22 |
for ( ; ; ) { /* eater loop */ |
| 275 |
greg |
2.16 |
FD_ZERO(&readfds); |
| 276 |
|
|
FD_ZERO(&excepfds); |
| 277 |
|
|
nfds = 0; |
| 278 |
|
|
if (fdout >= 0) { |
| 279 |
|
|
FD_SET(fdout, &readfds); |
| 280 |
|
|
FD_SET(fdout, &excepfds); |
| 281 |
|
|
nfds = fdout+1; |
| 282 |
greg |
2.14 |
} |
| 283 |
greg |
2.16 |
if (fderr >= 0) { |
| 284 |
|
|
FD_SET(fderr, &readfds); |
| 285 |
|
|
FD_SET(fderr, &excepfds); |
| 286 |
|
|
nfds = fderr+1; |
| 287 |
|
|
} |
| 288 |
|
|
if (nfds == 0) |
| 289 |
|
|
break; /* all done, exit */ |
| 290 |
gregl |
2.22 |
if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0) |
| 291 |
greg |
2.16 |
error(SYSTEM, "error in select call in io_process"); |
| 292 |
|
|
/* renderer stderr */ |
| 293 |
|
|
if (fderr >= 0 && (FD_ISSET(fderr, &readfds) || |
| 294 |
|
|
FD_ISSET(fderr, &excepfds))) { |
| 295 |
|
|
nr = read(fderr, cp=buf, sizeof(buf)); |
| 296 |
|
|
if (nr < 0) |
| 297 |
|
|
goto readerr; |
| 298 |
|
|
if (nr == 0) { |
| 299 |
|
|
close(fderr); |
| 300 |
greg |
2.18 |
/* close(2); don't close stderr! */ |
| 301 |
greg |
2.16 |
fderr = -1; |
| 302 |
gregl |
2.24 |
} else { |
| 303 |
gregl |
2.23 |
cp[nr] = '\0'; /* deduce status if we can */ |
| 304 |
|
|
n = strlen(progname); |
| 305 |
|
|
if (!strncmp(cp, progname, n) && |
| 306 |
|
|
cp[n++] == ':' && |
| 307 |
|
|
cp[n++] == ' ') { |
| 308 |
|
|
register struct erract *ep; |
| 309 |
|
|
for (ep = erract; ep < erract+NERRS; |
| 310 |
|
|
ep++) |
| 311 |
|
|
if (ep->pre[0] && |
| 312 |
|
|
!strncmp(cp+n, ep->pre, |
| 313 |
|
|
strlen(ep->pre))) { |
| 314 |
|
|
status = ep->ec; |
| 315 |
|
|
break; |
| 316 |
|
|
} |
| 317 |
|
|
} |
| 318 |
|
|
do { /* write message */ |
| 319 |
greg |
2.16 |
if ((n = write(2, cp, nr)) <= 0) |
| 320 |
|
|
goto writerr; |
| 321 |
|
|
cp += n; |
| 322 |
|
|
} while ((nr -= n) > 0); |
| 323 |
gregl |
2.24 |
} |
| 324 |
greg |
2.16 |
} |
| 325 |
|
|
/* renderer stdout */ |
| 326 |
|
|
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
| 327 |
|
|
FD_ISSET(fdout, &excepfds))) { |
| 328 |
|
|
nr = read(fdout, cp=buf, sizeof(buf)); |
| 329 |
|
|
if (nr < 0) |
| 330 |
|
|
goto readerr; |
| 331 |
|
|
if (nr == 0) { /* EOF */ |
| 332 |
|
|
close(fdout); |
| 333 |
|
|
close(1); |
| 334 |
|
|
fdout = -1; |
| 335 |
|
|
} else |
| 336 |
|
|
do { /* write it all */ |
| 337 |
|
|
if ((n = write(1, cp, nr)) <= 0) |
| 338 |
|
|
goto writerr; |
| 339 |
|
|
cp += n; |
| 340 |
|
|
} while ((nr -= n) > 0); |
| 341 |
|
|
} |
| 342 |
greg |
2.1 |
} |
| 343 |
greg |
2.36 |
kill(pid, SIGTERM); /* no more process to feed, so... */ |
| 344 |
greg |
2.35 |
waitpid(pid, 0, 0); /* wait for feeder process */ |
| 345 |
gregl |
2.23 |
_exit(status); |
| 346 |
greg |
2.1 |
formerr: |
| 347 |
|
|
error(USER, "format error in persist file"); |
| 348 |
greg |
2.16 |
readerr: |
| 349 |
|
|
error(SYSTEM, "read error in io_process"); |
| 350 |
|
|
writerr: |
| 351 |
|
|
error(SYSTEM, "write error in io_process"); |
| 352 |
greg |
2.1 |
} |
| 353 |
greg |
2.6 |
|
| 354 |
greg |
2.8 |
#else |
| 355 |
|
|
|
| 356 |
schorsch |
2.34 |
extern void pfclean(void) {} |
| 357 |
greg |
2.8 |
|
| 358 |
greg |
2.6 |
#endif |