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 |
+ |
|
16 |
+ |
#include "platform.h" |
17 |
+ |
#include "rtprocess.h" /* getpid() */ |
18 |
|
#include "standard.h" |
19 |
+ |
#include "random.h" |
20 |
|
|
21 |
|
#ifdef F_SETLKW |
22 |
|
#include "paths.h" |
23 |
|
#include "selcall.h" |
16 |
– |
#include <signal.h> |
17 |
– |
#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; |
192 |
|
char buf[BUFSIZ], *pfin, *pfout, *pferr; |
193 |
|
int pid, nfds; |
194 |
|
int fdout, fderr = -1; |
195 |
+ |
int status = 0; |
196 |
|
fd_set readfds, excepfds; |
197 |
|
/* load persist file */ |
198 |
|
n = 40; |
200 |
|
if (!n--) |
201 |
|
error(USER, "unattended persist file?"); |
202 |
|
pflock(0); |
203 |
< |
sleep(15); /* wait until ready */ |
203 |
> |
sleep(3+(3*getpid()+random())%13); /* wait until ready */ |
204 |
|
pflock(1); |
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) |
293 |
|
close(fderr); |
294 |
|
/* close(2); don't close stderr! */ |
295 |
|
fderr = -1; |
296 |
< |
} else |
297 |
< |
do { /* write it all */ |
296 |
> |
} else { |
297 |
> |
cp[nr] = '\0'; /* deduce status if we can */ |
298 |
> |
n = strlen(progname); |
299 |
> |
if (!strncmp(cp, progname, n) && |
300 |
> |
cp[n++] == ':' && |
301 |
> |
cp[n++] == ' ') { |
302 |
> |
register struct erract *ep; |
303 |
> |
for (ep = erract; ep < erract+NERRS; |
304 |
> |
ep++) |
305 |
> |
if (ep->pre[0] && |
306 |
> |
!strncmp(cp+n, ep->pre, |
307 |
> |
strlen(ep->pre))) { |
308 |
> |
status = ep->ec; |
309 |
> |
break; |
310 |
> |
} |
311 |
> |
} |
312 |
> |
do { /* write message */ |
313 |
|
if ((n = write(2, cp, nr)) <= 0) |
314 |
|
goto writerr; |
315 |
|
cp += n; |
316 |
|
} while ((nr -= n) > 0); |
317 |
+ |
} |
318 |
|
} |
319 |
|
/* renderer stdout */ |
320 |
|
if (fdout >= 0 && (FD_ISSET(fdout, &readfds) || |
334 |
|
} while ((nr -= n) > 0); |
335 |
|
} |
336 |
|
} |
337 |
< |
_exit(0); /* we ought to return renderer error status! */ |
337 |
> |
wait(0); /* wait for feeder process */ |
338 |
> |
_exit(status); |
339 |
|
formerr: |
340 |
|
error(USER, "format error in persist file"); |
341 |
|
readerr: |
346 |
|
|
347 |
|
#else |
348 |
|
|
349 |
< |
pfclean() {} |
349 |
> |
void pfclean() {} |
350 |
|
|
351 |
|
#endif |