1 |
– |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
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 |
+ |
#ifdef _WIN32 |
16 |
+ |
#include <process.h> /* getpid() */ |
17 |
+ |
#endif |
18 |
+ |
|
19 |
|
#include "standard.h" |
20 |
+ |
#include "random.h" |
21 |
|
|
22 |
|
#ifdef F_SETLKW |
23 |
|
#include "paths.h" |
24 |
|
#include "selcall.h" |
16 |
– |
#include <signal.h> |
17 |
– |
#include <sys/stat.h> |
25 |
|
|
26 |
|
#ifndef TIMELIM |
27 |
|
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
28 |
|
#endif |
29 |
|
|
30 |
< |
extern char *strcpy(), *index(); |
30 |
> |
#ifndef freebsd |
31 |
> |
#define mkfifo(fn,md) mknod(fn, S_IFIFO|(md), 0) |
32 |
> |
#endif |
33 |
|
|
34 |
+ |
extern void io_process(); |
35 |
+ |
|
36 |
|
extern int headismine; /* boolean true if header belongs to me */ |
37 |
|
|
38 |
|
extern char *progname; /* global program name */ |
45 |
|
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; |
46 |
|
|
47 |
|
|
48 |
+ |
void |
49 |
|
pfdetach() /* release persist (and header) resources */ |
50 |
|
{ |
51 |
|
if (persistfd >= 0) |
59 |
|
} |
60 |
|
|
61 |
|
|
62 |
+ |
void |
63 |
|
pfclean() /* clean up persist files */ |
64 |
|
{ |
65 |
|
if (persistfd >= 0) |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
+ |
void |
79 |
|
pflock(lf) /* place or release exclusive lock on file */ |
80 |
|
int lf; |
81 |
|
{ |
112 |
|
|
113 |
|
static int got_io; |
114 |
|
|
115 |
< |
static int sig_io() { got_io++; } |
115 |
> |
static void sig_io() { got_io++; } |
116 |
|
|
117 |
< |
static int sig_alrm() { quit(0); } |
117 |
> |
static void sig_alrm() { quit(0); } |
118 |
|
|
119 |
|
|
120 |
+ |
void |
121 |
|
pfhold() /* holding pattern for idle rendering process */ |
122 |
|
{ |
123 |
< |
int (*oldalrm)(); |
123 |
> |
void (*oldalrm)(); |
124 |
|
char buf[512]; |
125 |
|
register int n; |
126 |
|
/* close input and output descriptors */ |
129 |
|
if (errfile == NULL) |
130 |
|
close(fileno(stderr)); |
131 |
|
/* create named pipes for input and output */ |
132 |
< |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
132 |
> |
if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0) |
133 |
|
goto createrr; |
134 |
< |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
134 |
> |
if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0) |
135 |
|
goto createrr; |
136 |
|
if (errfile == NULL && |
137 |
< |
mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
137 |
> |
mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0) |
138 |
|
goto createrr; |
139 |
|
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
140 |
|
inpname, outpname, errname); |
141 |
|
n = strlen(buf); |
142 |
|
if (write(persistfd, buf, n) < n) |
143 |
|
error(SYSTEM, "error writing persist file"); |
144 |
< |
lseek(persistfd, 0L, 0); |
144 |
> |
lseek(persistfd, (off_t)0L, 0); |
145 |
|
/* wait TIMELIM for someone to signal us */ |
146 |
|
got_io = 0; |
147 |
|
signal(SIGIO, sig_io); |
148 |
< |
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
148 |
> |
oldalrm = (void (*)())signal(SIGALRM, sig_alrm); |
149 |
|
alarm(TIMELIM); |
150 |
|
pflock(0); /* unlock persist file for attach */ |
151 |
|
while (!got_io) |
155 |
|
signal(SIGIO, SIG_DFL); |
156 |
|
pflock(1); /* grab persist file back */ |
157 |
|
/* someone wants us; reopen stdin and stdout */ |
158 |
+ |
/* |
159 |
|
if (freopen(inpname, "r", stdin) == NULL) |
160 |
|
goto openerr; |
161 |
|
if (freopen(outpname, "w", stdout) == NULL) |
162 |
|
goto openerr; |
163 |
+ |
*/ |
164 |
+ |
close(0); |
165 |
+ |
if (open(inpname, O_RDONLY) != 0) |
166 |
+ |
error(INTERNAL, "unexpected stdin file number"); |
167 |
+ |
clearerr(stdin); |
168 |
+ |
close(1); |
169 |
+ |
if (open(outpname, O_WRONLY) != 1) |
170 |
+ |
error(INTERNAL, "unexpected stdout file number"); |
171 |
|
sleep(3); /* give them a chance to open their pipes */ |
172 |
|
if (errname[0]) { |
173 |
< |
if (freopen(errname, "w", stderr) == NULL) |
174 |
< |
goto openerr; |
173 |
> |
close(2); |
174 |
> |
if (open(errname, O_WRONLY) != 2) |
175 |
> |
error(INTERNAL, "unexpected stderr file number"); |
176 |
|
unlink(errname); |
177 |
|
errname[0] = '\0'; |
178 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
+ |
void |
192 |
|
io_process() /* just act as go-between for actual process */ |
193 |
|
{ |
194 |
|
register char *cp; |
204 |
|
if (!n--) |
205 |
|
error(USER, "unattended persist file?"); |
206 |
|
pflock(0); |
207 |
< |
sleep(15); /* wait until ready */ |
207 |
> |
sleep(3+(3*getpid()+random())%13); /* wait until ready */ |
208 |
|
pflock(1); |
209 |
|
} |
210 |
|
if (nr < 0) |
211 |
|
error(SYSTEM, "error reading persist file"); |
212 |
< |
ftruncate(persistfd, 0L); /* truncate persist file */ |
212 |
> |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
213 |
> |
ftruncate(persistfd, (off_t)0L); /* truncate persist file */ |
214 |
> |
#endif |
215 |
|
pfdetach(); /* close & release persist file */ |
216 |
|
buf[nr] = '\0'; /* parse what we got */ |
217 |
< |
if ((cp = index(buf, ' ')) == NULL) |
217 |
> |
if ((cp = strchr(buf, ' ')) == NULL) |
218 |
|
goto formerr; |
219 |
|
*cp++ = '\0'; |
220 |
|
if ((pid = atoi(cp)) <= 0) |
221 |
|
goto formerr; |
222 |
< |
if ((cp = index(cp, '\n')) == NULL) |
222 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
223 |
|
goto formerr; |
224 |
|
pfin = ++cp; |
225 |
< |
if ((cp = index(cp, '\n')) == NULL) |
225 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
226 |
|
goto formerr; |
227 |
|
*cp++ = '\0'; |
228 |
|
pfout = cp; |
229 |
< |
if ((cp = index(cp, '\n')) == NULL) |
229 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
230 |
|
goto formerr; |
231 |
|
*cp++ = '\0'; |
232 |
|
pferr = cp; |
233 |
< |
if ((cp = index(cp, '\n')) == NULL) |
233 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
234 |
|
goto formerr; |
235 |
|
*cp++ = '\0'; |
236 |
|
if (cp-buf != nr) |
297 |
|
close(fderr); |
298 |
|
/* close(2); don't close stderr! */ |
299 |
|
fderr = -1; |
300 |
< |
} else |
300 |
> |
} else { |
301 |
|
cp[nr] = '\0'; /* deduce status if we can */ |
302 |
|
n = strlen(progname); |
303 |
|
if (!strncmp(cp, progname, n) && |
318 |
|
goto writerr; |
319 |
|
cp += n; |
320 |
|
} while ((nr -= n) > 0); |
321 |
+ |
} |
322 |
|
} |
323 |
|
/* renderer stdout */ |
324 |
|
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
350 |
|
|
351 |
|
#else |
352 |
|
|
353 |
< |
pfclean() {} |
353 |
> |
void pfclean() {} |
354 |
|
|
355 |
|
#endif |