1 |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Routines for persistent rtrace and rpict processes. |
9 |
*/ |
10 |
|
11 |
#include "standard.h" |
12 |
|
13 |
#ifdef F_SETLKW |
14 |
|
15 |
#include "paths.h" |
16 |
#include <signal.h> |
17 |
#include <sys/types.h> |
18 |
#include <sys/stat.h> |
19 |
|
20 |
#ifndef TIMELIM |
21 |
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
22 |
#endif |
23 |
|
24 |
extern char *strcpy(), *index(); |
25 |
|
26 |
extern int headismine; /* boolean true if header belongs to me */ |
27 |
|
28 |
extern char *progname; /* global program name */ |
29 |
|
30 |
extern char *errfile; /* global error file name */ |
31 |
|
32 |
static char *persistfname = NULL; /* persist file name */ |
33 |
static int persistfd = -1; /* persist file descriptor */ |
34 |
|
35 |
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1]; |
36 |
|
37 |
|
38 |
pfdetach() /* release persist (and header) resources */ |
39 |
{ |
40 |
if (persistfd >= 0) |
41 |
close(persistfd); |
42 |
persistfd = -1; |
43 |
persistfname = NULL; |
44 |
inpname[0] = '\0'; |
45 |
outpname[0] = '\0'; |
46 |
errname[0] = '\0'; |
47 |
headismine = 0; |
48 |
} |
49 |
|
50 |
|
51 |
pfclean() /* clean up persist files */ |
52 |
{ |
53 |
if (persistfd >= 0) |
54 |
close(persistfd); |
55 |
if (persistfname != NULL) |
56 |
unlink(persistfname); |
57 |
if (inpname[0]) |
58 |
unlink(inpname); |
59 |
if (outpname[0]) |
60 |
unlink(outpname); |
61 |
if (errname[0]) |
62 |
unlink(errname); |
63 |
} |
64 |
|
65 |
|
66 |
pflock(lf) /* place or release exclusive lock on file */ |
67 |
int lf; |
68 |
{ |
69 |
struct flock fls; |
70 |
|
71 |
fls.l_type = lf ? F_WRLCK : F_UNLCK; |
72 |
fls.l_whence = 0; |
73 |
fls.l_start = 0L; |
74 |
fls.l_len = 0L; |
75 |
if (fcntl(persistfd, F_SETLKW, &fls) < 0) |
76 |
error(SYSTEM, "cannot (un)lock persist file"); |
77 |
} |
78 |
|
79 |
|
80 |
persistfile(pfn) /* open persist file and lock it */ |
81 |
char *pfn; |
82 |
{ |
83 |
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644); |
84 |
if (persistfd >= 0) { |
85 |
persistfname = pfn; |
86 |
/* put something there for faulty lock daemons */ |
87 |
write(persistfd, "Initializing...\n", 16); |
88 |
pflock(1); |
89 |
return; |
90 |
} |
91 |
/* file exists -- switch to i/o process */ |
92 |
persistfd = open(pfn, O_RDWR); |
93 |
if (persistfd < 0) { |
94 |
sprintf(errmsg, "cannot open persist file \"%s\"", pfn); |
95 |
error(SYSTEM, errmsg); |
96 |
} |
97 |
pflock(1); |
98 |
io_process(); /* never returns */ |
99 |
} |
100 |
|
101 |
|
102 |
static int got_io; |
103 |
|
104 |
static int sig_io() { got_io++; } |
105 |
|
106 |
static int sig_alrm() { quit(0); } |
107 |
|
108 |
|
109 |
pfhold() /* holding pattern for idle rendering process */ |
110 |
{ |
111 |
int (*oldalrm)(); |
112 |
char buf[512]; |
113 |
register int n; |
114 |
/* close input and output descriptors */ |
115 |
close(fileno(stdin)); |
116 |
close(fileno(stdout)); |
117 |
/* create named pipes for input and output */ |
118 |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
119 |
goto createrr; |
120 |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
121 |
goto createrr; |
122 |
if (errfile == NULL && |
123 |
mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
124 |
goto createrr; |
125 |
sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(), |
126 |
inpname, outpname, errname); |
127 |
if (lseek(persistfd, 0L, 0) < 0) |
128 |
error(SYSTEM, "seek error on persist file"); |
129 |
n = strlen(buf); |
130 |
if (write(persistfd, buf, n) < n) |
131 |
error(SYSTEM, "error writing persist file"); |
132 |
ftruncate(persistfd, (long)n); |
133 |
fsync(persistfd); /* shouldn't be necessary, but.... */ |
134 |
/* wait TIMELIM for someone to signal us */ |
135 |
got_io = 0; |
136 |
signal(SIGIO, sig_io); |
137 |
oldalrm = (int (*)())signal(SIGALRM, sig_alrm); |
138 |
alarm(TIMELIM); |
139 |
pflock(0); |
140 |
while (!got_io) |
141 |
pause(); |
142 |
alarm(0); |
143 |
signal(SIGALRM, oldalrm); |
144 |
signal(SIGIO, SIG_DFL); |
145 |
pflock(1); |
146 |
/* someone wants us; reopen stdin and stdout */ |
147 |
if (freopen(inpname, "r", stdin) == NULL) |
148 |
goto openerr; |
149 |
if (freopen(outpname, "w", stdout) == NULL) |
150 |
goto openerr; |
151 |
if (errname[0]) { |
152 |
if (freopen(errname, "w", stderr) == NULL) |
153 |
goto openerr; |
154 |
unlink(errname); |
155 |
errname[0] = '\0'; |
156 |
} |
157 |
unlink(inpname); |
158 |
inpname[0] = '\0'; |
159 |
unlink(outpname); |
160 |
outpname[0] = '\0'; |
161 |
return; |
162 |
createrr: |
163 |
error(SYSTEM, "cannot create named pipes in pfhold"); |
164 |
openerr: |
165 |
error(SYSTEM, "cannot open named pipes in pfhold"); |
166 |
} |
167 |
|
168 |
|
169 |
io_process() /* just act as conduits to and from actual process */ |
170 |
{ |
171 |
register char *cp; |
172 |
register int nr, n; |
173 |
char buf[512], *pfin, *pfout, *pferr; |
174 |
int pid, pid2 = -1; |
175 |
/* load and close persist file */ |
176 |
errno = 0; |
177 |
nr = read(persistfd, buf, sizeof(buf)-1); |
178 |
pfdetach(); |
179 |
if (nr <= 0) |
180 |
error(SYSTEM, "error reading persist file"); |
181 |
buf[nr] = '\0'; |
182 |
if ((cp = index(buf, ' ')) == NULL) |
183 |
goto formerr; |
184 |
*cp++ = '\0'; |
185 |
if ((pid = atoi(cp)) <= 0) |
186 |
goto formerr; |
187 |
if ((cp = index(cp, '\n')) == NULL) |
188 |
goto formerr; |
189 |
pfin = ++cp; |
190 |
if ((cp = index(cp, '\n')) == NULL) |
191 |
goto formerr; |
192 |
*cp++ = '\0'; |
193 |
pfout = cp; |
194 |
if ((cp = index(cp, '\n')) == NULL) |
195 |
goto formerr; |
196 |
*cp++ = '\0'; |
197 |
pferr = cp; |
198 |
if ((cp = index(cp, '\n')) == NULL) |
199 |
goto formerr; |
200 |
*cp++ = '\0'; |
201 |
if (cp-buf != nr) |
202 |
goto formerr; |
203 |
if (strcmp(buf, progname)) { |
204 |
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
205 |
error(USER, errmsg); |
206 |
} |
207 |
/* wake up rendering process */ |
208 |
if (kill(pid, SIGIO) < 0) |
209 |
error(SYSTEM, "cannot signal rendering process in io_process"); |
210 |
pid = fork(); /* fork i/o process */ |
211 |
if (pid < 0) |
212 |
goto forkerr; |
213 |
/* connect to appropriate pipe */ |
214 |
if (pid) { /* parent passes renderer output */ |
215 |
close(0); |
216 |
if (pferr[0]) { |
217 |
pid2 = fork(); /* fork another for stderr */ |
218 |
if (pid2 < 0) |
219 |
goto forkerr; |
220 |
} |
221 |
if (pid2) { /* parent is still stdout */ |
222 |
if (open(pfout, O_RDONLY) != 0) |
223 |
error(SYSTEM, |
224 |
"cannot open output pipe in io_process"); |
225 |
} else { /* second child is stderr */ |
226 |
if (open(pferr, O_RDONLY) != 0) |
227 |
error(SYSTEM, |
228 |
"cannot open error pipe in io_process"); |
229 |
dup2(2, 1); /* attach stdout to stderr */ |
230 |
} |
231 |
} else { /* child passes renderer input */ |
232 |
close(1); |
233 |
if (open(pfin, O_WRONLY) != 1) |
234 |
error(SYSTEM, "cannot open input pipe in io_process"); |
235 |
} |
236 |
/* pass input to output */ |
237 |
/* read as much as we can, write all of it */ |
238 |
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) |
239 |
do { |
240 |
if ((n = write(1, cp, nr)) <= 0) |
241 |
error(SYSTEM, "write error in io_process"); |
242 |
cp += n; |
243 |
} while ((nr -= n) > 0); |
244 |
if (nr < 0) |
245 |
error(SYSTEM, "read error in io_process"); |
246 |
close(0); /* close input */ |
247 |
close(1); /* close output */ |
248 |
if (pid) /* parent waits for stdin child */ |
249 |
wait(0); |
250 |
if (pid2 > 0) /* wait for stderr child also */ |
251 |
wait(0); |
252 |
_exit(0); /* all done, exit (not quit!) */ |
253 |
formerr: |
254 |
error(USER, "format error in persist file"); |
255 |
forkerr: |
256 |
error(SYSTEM, "fork failed in io_process"); |
257 |
} |
258 |
|
259 |
#else |
260 |
|
261 |
pfclean() {} |
262 |
|
263 |
#endif |