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 |
+ |
#include <sys/types.h> |
16 |
+ |
#ifndef NON_POSIX /* XXX need abstraction for process management */ |
17 |
+ |
#include <sys/wait.h> |
18 |
+ |
#endif |
19 |
+ |
|
20 |
+ |
#include "platform.h" |
21 |
+ |
#include "rtprocess.h" /* getpid() */ |
22 |
|
#include "standard.h" |
23 |
+ |
#include "random.h" |
24 |
+ |
#include "ray.h" |
25 |
|
|
26 |
|
#ifdef F_SETLKW |
27 |
|
#include "paths.h" |
28 |
|
#include "selcall.h" |
16 |
– |
#include <signal.h> |
17 |
– |
#include <sys/stat.h> |
29 |
|
|
30 |
|
#ifndef TIMELIM |
31 |
|
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
32 |
|
#endif |
33 |
|
|
23 |
– |
extern char *strcpy(), *index(); |
24 |
– |
|
34 |
|
extern int headismine; /* boolean true if header belongs to me */ |
26 |
– |
|
35 |
|
extern char *progname; /* global program name */ |
28 |
– |
|
36 |
|
extern char *errfile; /* global error file name */ |
30 |
– |
|
37 |
|
static char *persistfname = NULL; /* persist file name */ |
38 |
|
static int persistfd = -1; /* persist file descriptor */ |
33 |
– |
|
39 |
|
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; |
40 |
|
|
41 |
+ |
typedef void (sighandler_t)(int); |
42 |
+ |
static sighandler_t sig_io; |
43 |
+ |
static sighandler_t sig_alrm; |
44 |
|
|
45 |
< |
pfdetach() /* release persist (and header) resources */ |
45 |
> |
|
46 |
> |
extern void |
47 |
> |
pfdetach(void) /* release persist (and header) resources */ |
48 |
|
{ |
49 |
|
if (persistfd >= 0) |
50 |
|
close(persistfd); |
57 |
|
} |
58 |
|
|
59 |
|
|
60 |
< |
pfclean() /* clean up persist files */ |
60 |
> |
extern void |
61 |
> |
pfclean(void) /* clean up persist files */ |
62 |
|
{ |
63 |
|
if (persistfd >= 0) |
64 |
|
close(persistfd); |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
< |
pflock(lf) /* place or release exclusive lock on file */ |
77 |
< |
int lf; |
76 |
> |
extern void |
77 |
> |
pflock( /* place or release exclusive lock on file */ |
78 |
> |
int lf |
79 |
> |
) |
80 |
|
{ |
81 |
|
struct flock fls; |
82 |
|
|
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
< |
persistfile(pfn) /* open persist file and lock it */ |
93 |
< |
char *pfn; |
92 |
> |
extern void |
93 |
> |
persistfile( /* open persist file and lock it */ |
94 |
> |
char *pfn |
95 |
> |
) |
96 |
|
{ |
97 |
|
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); |
98 |
|
if (persistfd >= 0) { |
113 |
|
|
114 |
|
static int got_io; |
115 |
|
|
116 |
< |
static int sig_io() { got_io++; } |
116 |
> |
static void sig_io(int i) { got_io++; } |
117 |
|
|
118 |
< |
static int sig_alrm() { quit(0); } |
118 |
> |
static void sig_alrm(int i) { quit(0); } |
119 |
|
|
120 |
|
|
121 |
< |
pfhold() /* holding pattern for idle rendering process */ |
121 |
> |
extern void |
122 |
> |
pfhold(void) /* holding pattern for idle rendering process */ |
123 |
|
{ |
124 |
< |
int (*oldalrm)(); |
124 |
> |
sighandler_t *oldalrm; |
125 |
|
char buf[512]; |
126 |
|
register int n; |
127 |
|
/* close input and output descriptors */ |
128 |
< |
close(fileno(stdin)); |
129 |
< |
close(fileno(stdout)); |
128 |
> |
close(0); |
129 |
> |
close(1); |
130 |
|
if (errfile == NULL) |
131 |
< |
close(fileno(stderr)); |
131 |
> |
close(2); |
132 |
|
/* create named pipes for input and output */ |
133 |
< |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
133 |
> |
if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0) |
134 |
|
goto createrr; |
135 |
< |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
135 |
> |
if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0) |
136 |
|
goto createrr; |
137 |
|
if (errfile == NULL && |
138 |
< |
mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
138 |
> |
mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0) |
139 |
|
goto createrr; |
140 |
|
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
141 |
|
inpname, outpname, errname); |
142 |
|
n = strlen(buf); |
143 |
|
if (write(persistfd, buf, n) < n) |
144 |
|
error(SYSTEM, "error writing persist file"); |
145 |
< |
lseek(persistfd, 0L, 0); |
145 |
> |
lseek(persistfd, (off_t)0, SEEK_SET); |
146 |
|
/* wait TIMELIM for someone to signal us */ |
147 |
|
got_io = 0; |
148 |
|
signal(SIGIO, sig_io); |
149 |
< |
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
149 |
> |
oldalrm = signal(SIGALRM, sig_alrm); |
150 |
|
alarm(TIMELIM); |
151 |
|
pflock(0); /* unlock persist file for attach */ |
152 |
|
while (!got_io) |
156 |
|
signal(SIGIO, SIG_DFL); |
157 |
|
pflock(1); /* grab persist file back */ |
158 |
|
/* someone wants us; reopen stdin and stdout */ |
159 |
+ |
/* |
160 |
|
if (freopen(inpname, "r", stdin) == NULL) |
161 |
|
goto openerr; |
162 |
|
if (freopen(outpname, "w", stdout) == NULL) |
163 |
|
goto openerr; |
164 |
+ |
*/ |
165 |
+ |
close(0); |
166 |
+ |
if (open(inpname, O_RDONLY) != 0) |
167 |
+ |
error(INTERNAL, "unexpected stdin file number"); |
168 |
+ |
clearerr(stdin); |
169 |
+ |
close(1); |
170 |
+ |
if (open(outpname, O_WRONLY) != 1) |
171 |
+ |
error(INTERNAL, "unexpected stdout file number"); |
172 |
|
sleep(3); /* give them a chance to open their pipes */ |
173 |
|
if (errname[0]) { |
174 |
< |
if (freopen(errname, "w", stderr) == NULL) |
175 |
< |
goto openerr; |
174 |
> |
close(2); |
175 |
> |
if (open(errname, O_WRONLY) != 2) |
176 |
> |
error(INTERNAL, "unexpected stderr file number"); |
177 |
|
unlink(errname); |
178 |
|
errname[0] = '\0'; |
179 |
|
} |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
< |
io_process() /* just act as go-between for actual process */ |
192 |
> |
extern void |
193 |
> |
io_process(void) /* just act as go-between for actual process */ |
194 |
|
{ |
195 |
|
register char *cp; |
196 |
|
register int nr, n; |
205 |
|
if (!n--) |
206 |
|
error(USER, "unattended persist file?"); |
207 |
|
pflock(0); |
208 |
< |
sleep(15); /* wait until ready */ |
208 |
> |
sleep(3+(3*getpid()+random())%13); /* wait until ready */ |
209 |
|
pflock(1); |
210 |
|
} |
211 |
|
if (nr < 0) |
212 |
|
error(SYSTEM, "error reading persist file"); |
213 |
< |
ftruncate(persistfd, 0L); /* truncate persist file */ |
213 |
> |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
214 |
> |
ftruncate(persistfd, (off_t)0L); /* truncate persist file */ |
215 |
> |
#endif |
216 |
|
pfdetach(); /* close & release persist file */ |
217 |
|
buf[nr] = '\0'; /* parse what we got */ |
218 |
< |
if ((cp = index(buf, ' ')) == NULL) |
218 |
> |
if ((cp = strchr(buf, ' ')) == NULL) |
219 |
|
goto formerr; |
220 |
|
*cp++ = '\0'; |
221 |
|
if ((pid = atoi(cp)) <= 0) |
222 |
|
goto formerr; |
223 |
< |
if ((cp = index(cp, '\n')) == NULL) |
223 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
224 |
|
goto formerr; |
225 |
|
pfin = ++cp; |
226 |
< |
if ((cp = index(cp, '\n')) == NULL) |
226 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
227 |
|
goto formerr; |
228 |
|
*cp++ = '\0'; |
229 |
|
pfout = cp; |
230 |
< |
if ((cp = index(cp, '\n')) == NULL) |
230 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
231 |
|
goto formerr; |
232 |
|
*cp++ = '\0'; |
233 |
|
pferr = cp; |
234 |
< |
if ((cp = index(cp, '\n')) == NULL) |
234 |
> |
if ((cp = strchr(cp, '\n')) == NULL) |
235 |
|
goto formerr; |
236 |
|
*cp++ = '\0'; |
237 |
|
if (cp-buf != nr) |
339 |
|
} while ((nr -= n) > 0); |
340 |
|
} |
341 |
|
} |
342 |
< |
wait(0); /* wait for feeder process */ |
342 |
> |
kill(pid, SIGTERM); /* no more process to feed, so... */ |
343 |
> |
waitpid(pid, 0, 0); /* wait for feeder process */ |
344 |
|
_exit(status); |
345 |
|
formerr: |
346 |
|
error(USER, "format error in persist file"); |
352 |
|
|
353 |
|
#else |
354 |
|
|
355 |
< |
pfclean() {} |
355 |
> |
extern void pfclean(void) {} |
356 |
|
|
357 |
|
#endif |