| 1 |
– |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
|
* Routines for persistent rtrace and rpict processes. |
| 6 |
+ |
* |
| 7 |
+ |
* External symbols declared in ray.h |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
+ |
#include "copyright.h" |
| 11 |
+ |
|
| 12 |
+ |
#include <string.h> |
| 13 |
+ |
#include <signal.h> |
| 14 |
+ |
#include <sys/stat.h> |
| 15 |
+ |
|
| 16 |
+ |
#include "rtprocess.h" /* getpid() */ |
| 17 |
|
#include "standard.h" |
| 18 |
+ |
#include "platform.h" |
| 19 |
|
#include "random.h" |
| 20 |
|
|
| 21 |
|
#ifdef F_SETLKW |
| 22 |
|
#include "paths.h" |
| 23 |
|
#include "selcall.h" |
| 17 |
– |
#include <signal.h> |
| 18 |
– |
#include <sys/stat.h> |
| 24 |
|
|
| 25 |
|
#ifndef TIMELIM |
| 26 |
|
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
| 27 |
|
#endif |
| 28 |
|
|
| 29 |
< |
extern char *strcpy(), *index(); |
| 29 |
> |
extern void io_process(); |
| 30 |
|
|
| 31 |
|
extern int headismine; /* boolean true if header belongs to me */ |
| 32 |
|
|
| 40 |
|
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; |
| 41 |
|
|
| 42 |
|
|
| 43 |
+ |
void |
| 44 |
|
pfdetach() /* release persist (and header) resources */ |
| 45 |
|
{ |
| 46 |
|
if (persistfd >= 0) |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
|
| 57 |
+ |
void |
| 58 |
|
pfclean() /* clean up persist files */ |
| 59 |
|
{ |
| 60 |
|
if (persistfd >= 0) |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
|
| 73 |
+ |
void |
| 74 |
|
pflock(lf) /* place or release exclusive lock on file */ |
| 75 |
|
int lf; |
| 76 |
|
{ |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
|
| 88 |
+ |
void |
| 89 |
|
persistfile(pfn) /* open persist file and lock it */ |
| 90 |
|
char *pfn; |
| 91 |
|
{ |
| 108 |
|
|
| 109 |
|
static int got_io; |
| 110 |
|
|
| 111 |
< |
static int sig_io() { got_io++; } |
| 111 |
> |
static void sig_io() { got_io++; } |
| 112 |
|
|
| 113 |
< |
static int sig_alrm() { quit(0); } |
| 113 |
> |
static void sig_alrm() { quit(0); } |
| 114 |
|
|
| 115 |
|
|
| 116 |
+ |
void |
| 117 |
|
pfhold() /* holding pattern for idle rendering process */ |
| 118 |
|
{ |
| 119 |
< |
int (*oldalrm)(); |
| 119 |
> |
void (*oldalrm)(); |
| 120 |
|
char buf[512]; |
| 121 |
|
register int n; |
| 122 |
|
/* close input and output descriptors */ |
| 125 |
|
if (errfile == NULL) |
| 126 |
|
close(fileno(stderr)); |
| 127 |
|
/* create named pipes for input and output */ |
| 128 |
< |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 128 |
> |
if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0) |
| 129 |
|
goto createrr; |
| 130 |
< |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 130 |
> |
if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0) |
| 131 |
|
goto createrr; |
| 132 |
|
if (errfile == NULL && |
| 133 |
< |
mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
| 133 |
> |
mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0) |
| 134 |
|
goto createrr; |
| 135 |
|
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
| 136 |
|
inpname, outpname, errname); |
| 137 |
|
n = strlen(buf); |
| 138 |
|
if (write(persistfd, buf, n) < n) |
| 139 |
|
error(SYSTEM, "error writing persist file"); |
| 140 |
< |
lseek(persistfd, 0L, 0); |
| 140 |
> |
lseek(persistfd, (off_t)0, SEEK_SET); |
| 141 |
|
/* wait TIMELIM for someone to signal us */ |
| 142 |
|
got_io = 0; |
| 143 |
|
signal(SIGIO, sig_io); |
| 144 |
< |
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
| 144 |
> |
oldalrm = (void (*)())signal(SIGALRM, sig_alrm); |
| 145 |
|
alarm(TIMELIM); |
| 146 |
|
pflock(0); /* unlock persist file for attach */ |
| 147 |
|
while (!got_io) |
| 151 |
|
signal(SIGIO, SIG_DFL); |
| 152 |
|
pflock(1); /* grab persist file back */ |
| 153 |
|
/* someone wants us; reopen stdin and stdout */ |
| 154 |
+ |
/* |
| 155 |
|
if (freopen(inpname, "r", stdin) == NULL) |
| 156 |
|
goto openerr; |
| 157 |
|
if (freopen(outpname, "w", stdout) == NULL) |
| 158 |
|
goto openerr; |
| 159 |
+ |
*/ |
| 160 |
+ |
close(0); |
| 161 |
+ |
if (open(inpname, O_RDONLY) != 0) |
| 162 |
+ |
error(INTERNAL, "unexpected stdin file number"); |
| 163 |
+ |
clearerr(stdin); |
| 164 |
+ |
close(1); |
| 165 |
+ |
if (open(outpname, O_WRONLY) != 1) |
| 166 |
+ |
error(INTERNAL, "unexpected stdout file number"); |
| 167 |
|
sleep(3); /* give them a chance to open their pipes */ |
| 168 |
|
if (errname[0]) { |
| 169 |
< |
if (freopen(errname, "w", stderr) == NULL) |
| 170 |
< |
goto openerr; |
| 169 |
> |
close(2); |
| 170 |
> |
if (open(errname, O_WRONLY) != 2) |
| 171 |
> |
error(INTERNAL, "unexpected stderr file number"); |
| 172 |
|
unlink(errname); |
| 173 |
|
errname[0] = '\0'; |
| 174 |
|
} |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
|
| 187 |
+ |
void |
| 188 |
|
io_process() /* just act as go-between for actual process */ |
| 189 |
|
{ |
| 190 |
|
register char *cp; |
| 205 |
|
} |
| 206 |
|
if (nr < 0) |
| 207 |
|
error(SYSTEM, "error reading persist file"); |
| 208 |
< |
ftruncate(persistfd, 0L); /* truncate persist file */ |
| 208 |
> |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
| 209 |
> |
ftruncate(persistfd, (off_t)0L); /* truncate persist file */ |
| 210 |
> |
#endif |
| 211 |
|
pfdetach(); /* close & release persist file */ |
| 212 |
|
buf[nr] = '\0'; /* parse what we got */ |
| 213 |
< |
if ((cp = index(buf, ' ')) == NULL) |
| 213 |
> |
if ((cp = strchr(buf, ' ')) == NULL) |
| 214 |
|
goto formerr; |
| 215 |
|
*cp++ = '\0'; |
| 216 |
|
if ((pid = atoi(cp)) <= 0) |
| 217 |
|
goto formerr; |
| 218 |
< |
if ((cp = index(cp, '\n')) == NULL) |
| 218 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
| 219 |
|
goto formerr; |
| 220 |
|
pfin = ++cp; |
| 221 |
< |
if ((cp = index(cp, '\n')) == NULL) |
| 221 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
| 222 |
|
goto formerr; |
| 223 |
|
*cp++ = '\0'; |
| 224 |
|
pfout = cp; |
| 225 |
< |
if ((cp = index(cp, '\n')) == NULL) |
| 225 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
| 226 |
|
goto formerr; |
| 227 |
|
*cp++ = '\0'; |
| 228 |
|
pferr = cp; |
| 229 |
< |
if ((cp = index(cp, '\n')) == NULL) |
| 229 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
| 230 |
|
goto formerr; |
| 231 |
|
*cp++ = '\0'; |
| 232 |
|
if (cp-buf != nr) |
| 346 |
|
|
| 347 |
|
#else |
| 348 |
|
|
| 349 |
< |
pfclean() {} |
| 349 |
> |
void pfclean() {} |
| 350 |
|
|
| 351 |
|
#endif |