1 |
greg |
2.12 |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
greg |
2.1 |
|
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 |
greg |
2.6 |
|
13 |
|
|
#ifdef F_SETLKW |
14 |
|
|
|
15 |
greg |
2.1 |
#include "paths.h" |
16 |
|
|
#include <signal.h> |
17 |
|
|
#include <sys/types.h> |
18 |
|
|
#include <sys/stat.h> |
19 |
|
|
|
20 |
greg |
2.2 |
#ifndef TIMELIM |
21 |
|
|
#define TIMELIM (8*3600) /* time limit for holding pattern */ |
22 |
|
|
#endif |
23 |
|
|
|
24 |
greg |
2.1 |
extern char *strcpy(), *index(); |
25 |
|
|
|
26 |
|
|
extern int headismine; /* boolean true if header belongs to me */ |
27 |
|
|
|
28 |
greg |
2.12 |
extern char *progname; /* global program name */ |
29 |
|
|
|
30 |
greg |
2.1 |
static char *persistfname = NULL; /* persist file name */ |
31 |
|
|
static int persistfd = -1; /* persist file descriptor */ |
32 |
|
|
|
33 |
|
|
static char inpname[TEMPLEN+1], outpname[TEMPLEN+1]; |
34 |
|
|
|
35 |
|
|
|
36 |
|
|
pfdetach() /* release persist (and header) resources */ |
37 |
|
|
{ |
38 |
|
|
if (persistfd >= 0) |
39 |
|
|
close(persistfd); |
40 |
|
|
persistfd = -1; |
41 |
|
|
persistfname = NULL; |
42 |
|
|
inpname[0] = '\0'; |
43 |
|
|
outpname[0] = '\0'; |
44 |
|
|
headismine = 0; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
|
48 |
|
|
pfclean() /* clean up persist files */ |
49 |
|
|
{ |
50 |
|
|
if (persistfd >= 0) |
51 |
|
|
close(persistfd); |
52 |
|
|
if (persistfname != NULL) |
53 |
|
|
unlink(persistfname); |
54 |
|
|
if (inpname[0]) |
55 |
|
|
unlink(inpname); |
56 |
|
|
if (outpname[0]) |
57 |
|
|
unlink(outpname); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
|
61 |
|
|
pflock(lf) /* place or release exclusive lock on file */ |
62 |
|
|
int lf; |
63 |
|
|
{ |
64 |
|
|
struct flock fls; |
65 |
|
|
|
66 |
|
|
fls.l_type = lf ? F_WRLCK : F_UNLCK; |
67 |
|
|
fls.l_whence = 0; |
68 |
|
|
fls.l_start = 0L; |
69 |
|
|
fls.l_len = 0L; |
70 |
|
|
if (fcntl(persistfd, F_SETLKW, &fls) < 0) |
71 |
|
|
error(SYSTEM, "cannot (un)lock persist file"); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
|
75 |
|
|
persistfile(pfn) /* open persist file and lock it */ |
76 |
|
|
char *pfn; |
77 |
|
|
{ |
78 |
|
|
persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666); |
79 |
|
|
if (persistfd >= 0) { |
80 |
|
|
persistfname = pfn; |
81 |
|
|
pflock(1); |
82 |
|
|
return; |
83 |
|
|
} |
84 |
|
|
/* file exists -- switch to i/o process */ |
85 |
|
|
persistfd = open(pfn, O_RDWR); |
86 |
|
|
if (persistfd < 0) { |
87 |
|
|
sprintf(errmsg, "cannot open persist file \"%s\"", pfn); |
88 |
|
|
error(SYSTEM, errmsg); |
89 |
|
|
} |
90 |
|
|
pflock(1); |
91 |
|
|
io_process(); /* never returns */ |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
|
95 |
greg |
2.2 |
int sig_noop() {} |
96 |
greg |
2.1 |
|
97 |
|
|
|
98 |
|
|
pfhold() /* holding pattern for idle rendering process */ |
99 |
|
|
{ |
100 |
greg |
2.12 |
char buf[512]; |
101 |
greg |
2.1 |
register int n; |
102 |
|
|
/* close input and output descriptors */ |
103 |
|
|
close(fileno(stdin)); |
104 |
|
|
close(fileno(stdout)); |
105 |
|
|
/* create named pipes for input and output */ |
106 |
greg |
2.11 |
if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
107 |
greg |
2.1 |
goto createrr; |
108 |
greg |
2.11 |
if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0) |
109 |
greg |
2.1 |
goto createrr; |
110 |
greg |
2.12 |
sprintf(buf, "%s %d\n%s\n%s\n", progname, getpid(), inpname, outpname); |
111 |
greg |
2.10 |
if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0) |
112 |
|
|
error(SYSTEM, "seek/truncate error on persist file"); |
113 |
greg |
2.1 |
n = strlen(buf); |
114 |
|
|
if (write(persistfd, buf, n) < n) |
115 |
greg |
2.10 |
error(SYSTEM, "error writing persist file"); |
116 |
greg |
2.2 |
/* wait TIMELIM for someone to signal us */ |
117 |
|
|
signal(SIGIO, sig_noop); |
118 |
|
|
alarm(TIMELIM); |
119 |
greg |
2.1 |
pflock(0); |
120 |
|
|
pause(); |
121 |
greg |
2.2 |
alarm(0); |
122 |
|
|
signal(SIGIO, SIG_DFL); |
123 |
greg |
2.1 |
pflock(1); |
124 |
|
|
/* someone wants us; reopen stdin and stdout */ |
125 |
|
|
if (freopen(inpname, "r", stdin) == NULL) |
126 |
|
|
goto openerr; |
127 |
|
|
if (freopen(outpname, "w", stdout) == NULL) |
128 |
|
|
goto openerr; |
129 |
|
|
unlink(inpname); |
130 |
|
|
inpname[0] = '\0'; |
131 |
|
|
unlink(outpname); |
132 |
|
|
outpname[0] = '\0'; |
133 |
|
|
return; |
134 |
|
|
createrr: |
135 |
|
|
error(SYSTEM, "cannot create named pipes in pfhold"); |
136 |
|
|
openerr: |
137 |
|
|
error(SYSTEM, "cannot open named pipes in pfhold"); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
|
141 |
|
|
io_process() /* just act as conduits to and from actual process */ |
142 |
|
|
{ |
143 |
|
|
register char *cp; |
144 |
|
|
register int nr, n; |
145 |
greg |
2.12 |
char buf[512], *pfin, *pfout; |
146 |
greg |
2.1 |
int pid; |
147 |
|
|
/* load and close persist file */ |
148 |
greg |
2.12 |
nr = read(persistfd, buf, sizeof(buf)-1); |
149 |
greg |
2.1 |
pfdetach(); |
150 |
|
|
if (nr < 0) |
151 |
|
|
error(SYSTEM, "cannot read persist file"); |
152 |
|
|
buf[nr] = '\0'; |
153 |
greg |
2.12 |
if ((cp = index(buf, ' ')) == NULL) |
154 |
greg |
2.1 |
goto formerr; |
155 |
|
|
*cp++ = '\0'; |
156 |
greg |
2.12 |
if ((pid = atoi(cp)) <= 0) |
157 |
greg |
2.1 |
goto formerr; |
158 |
|
|
if ((cp = index(cp, '\n')) == NULL) |
159 |
|
|
goto formerr; |
160 |
greg |
2.12 |
pfin = ++cp; |
161 |
|
|
if ((cp = index(cp, '\n')) == NULL) |
162 |
|
|
goto formerr; |
163 |
greg |
2.1 |
*cp++ = '\0'; |
164 |
|
|
pfout = cp; |
165 |
|
|
if ((cp = index(cp, '\n')) == NULL) |
166 |
|
|
goto formerr; |
167 |
|
|
*cp++ = '\0'; |
168 |
|
|
if (cp-buf != nr) |
169 |
|
|
goto formerr; |
170 |
greg |
2.12 |
if (strcmp(buf, progname)) { |
171 |
|
|
sprintf(errmsg, "persist file for %s, not %s", buf, progname); |
172 |
|
|
error(USER, errmsg); |
173 |
|
|
} |
174 |
greg |
2.1 |
/* wake up rendering process */ |
175 |
greg |
2.2 |
if (kill(pid, SIGIO) < 0) |
176 |
greg |
2.1 |
error(SYSTEM, "cannot signal rendering process in io_process"); |
177 |
|
|
pid = fork(); /* fork i/o process */ |
178 |
|
|
if (pid < 0) |
179 |
|
|
error(SYSTEM, "fork failed in io_process"); |
180 |
|
|
/* connect to appropriate pipe */ |
181 |
|
|
if (pid) { /* parent passes renderer output */ |
182 |
greg |
2.7 |
close(0); |
183 |
|
|
if (open(pfout, O_RDONLY) != 0) |
184 |
greg |
2.1 |
error(SYSTEM, "cannot open input pipe in io_process"); |
185 |
|
|
} else { /* child passes renderer input */ |
186 |
greg |
2.7 |
close(1); |
187 |
|
|
if (open(pfin, O_WRONLY) != 1) |
188 |
|
|
error(SYSTEM, "cannot open output pipe in io_process"); |
189 |
greg |
2.1 |
} |
190 |
|
|
/* pass input to output */ |
191 |
greg |
2.9 |
/* read as much as we can, write all of it */ |
192 |
|
|
while ((nr = read(0, cp=buf, sizeof(buf))) > 0) |
193 |
|
|
do { |
194 |
|
|
if ((n = write(1, cp, nr)) <= 0) |
195 |
|
|
goto writerr; |
196 |
|
|
cp += n; |
197 |
|
|
} while ((nr -= n) > 0); |
198 |
greg |
2.7 |
if (nr < 0) |
199 |
|
|
error(SYSTEM, "read error in io_process"); |
200 |
|
|
close(0); /* close input */ |
201 |
|
|
close(1); /* close output */ |
202 |
greg |
2.1 |
if (pid) /* parent waits for child */ |
203 |
|
|
wait(0); |
204 |
|
|
exit(0); /* all done, exit (not quit!) */ |
205 |
|
|
formerr: |
206 |
|
|
error(USER, "format error in persist file"); |
207 |
greg |
2.7 |
writerr: |
208 |
|
|
error(SYSTEM, "write error in io_process"); |
209 |
greg |
2.1 |
} |
210 |
greg |
2.6 |
|
211 |
greg |
2.8 |
#else |
212 |
|
|
|
213 |
|
|
pfclean() {} |
214 |
|
|
|
215 |
greg |
2.6 |
#endif |