ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/persist.c
Revision: 2.15
Committed: Sat Jul 6 23:19:57 1996 UTC (27 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.14: +14 -15 lines
Log Message:
finally fixed synchronization problem caused by break in lock during fork

File Contents

# Content
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 pflock(1);
87 return;
88 }
89 /* file exists -- switch to i/o process */
90 persistfd = open(pfn, O_RDWR);
91 if (persistfd < 0) {
92 sprintf(errmsg, "cannot open persist file \"%s\"", pfn);
93 error(SYSTEM, errmsg);
94 }
95 pflock(1);
96 io_process(); /* never returns */
97 }
98
99
100 static int got_io;
101
102 static int sig_io() { got_io++; }
103
104 static int sig_alrm() { quit(0); }
105
106
107 pfhold() /* holding pattern for idle rendering process */
108 {
109 int (*oldalrm)();
110 char buf[512];
111 register int n;
112 /* close input and output descriptors */
113 close(fileno(stdin));
114 close(fileno(stdout));
115 /* create named pipes for input and output */
116 if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
117 goto createrr;
118 if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
119 goto createrr;
120 if (errfile == NULL &&
121 mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
122 goto createrr;
123 sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
124 inpname, outpname, errname);
125 n = strlen(buf);
126 if (write(persistfd, buf, n) < n)
127 error(SYSTEM, "error writing persist file");
128 /* wait TIMELIM for someone to signal us */
129 got_io = 0;
130 signal(SIGIO, sig_io);
131 oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
132 alarm(TIMELIM);
133 pflock(0); /* unlock persist file for attach */
134 while (!got_io)
135 pause(); /* wait for attach */
136 pflock(1); /* grab persist file right back! */
137 alarm(0); /* turn off alarm */
138 signal(SIGALRM, oldalrm);
139 signal(SIGIO, SIG_DFL);
140 if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
141 error(SYSTEM, "seek/truncate error on persist file");
142 /* someone wants us; reopen stdin and stdout */
143 if (freopen(inpname, "r", stdin) == NULL)
144 goto openerr;
145 if (freopen(outpname, "w", stdout) == NULL)
146 goto openerr;
147 if (errname[0]) {
148 if (freopen(errname, "w", stderr) == NULL)
149 goto openerr;
150 unlink(errname);
151 errname[0] = '\0';
152 }
153 unlink(inpname);
154 inpname[0] = '\0';
155 unlink(outpname);
156 outpname[0] = '\0';
157 return;
158 createrr:
159 error(SYSTEM, "cannot create named pipes in pfhold");
160 openerr:
161 error(SYSTEM, "cannot open named pipes in pfhold");
162 }
163
164
165 io_process() /* just act as conduits to and from actual process */
166 {
167 register char *cp;
168 register int nr, n;
169 char buf[512], *pfin, *pfout, *pferr;
170 int pid, pid2 = -1;
171 /* load persist file */
172 while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
173 pflock(0);
174 sleep(15); /* wait until ready */
175 pflock(1);
176 }
177 pfdetach(); /* close persist file */
178 if (nr < 0)
179 error(SYSTEM, "error reading persist file");
180 buf[nr] = '\0';
181 if ((cp = index(buf, ' ')) == NULL)
182 goto formerr;
183 *cp++ = '\0';
184 if ((pid = atoi(cp)) <= 0)
185 goto formerr;
186 if ((cp = index(cp, '\n')) == NULL)
187 goto formerr;
188 pfin = ++cp;
189 if ((cp = index(cp, '\n')) == NULL)
190 goto formerr;
191 *cp++ = '\0';
192 pfout = cp;
193 if ((cp = index(cp, '\n')) == NULL)
194 goto formerr;
195 *cp++ = '\0';
196 pferr = cp;
197 if ((cp = index(cp, '\n')) == NULL)
198 goto formerr;
199 *cp++ = '\0';
200 if (cp-buf != nr)
201 goto formerr;
202 if (strcmp(buf, progname)) {
203 sprintf(errmsg, "persist file for %s, not %s", buf, progname);
204 error(USER, errmsg);
205 }
206 /* wake up rendering process */
207 if (kill(pid, SIGIO) < 0)
208 error(SYSTEM, "cannot signal rendering process in io_process");
209 pid = fork(); /* fork i/o process */
210 if (pid < 0)
211 goto forkerr;
212 /* connect to appropriate pipe */
213 if (pid) { /* parent passes renderer output */
214 close(0);
215 if (pferr[0]) {
216 pid2 = fork(); /* fork another for stderr */
217 if (pid2 < 0)
218 goto forkerr;
219 }
220 if (pid2) { /* parent is still stdout */
221 if (open(pfout, O_RDONLY) != 0)
222 error(SYSTEM,
223 "cannot open output pipe in io_process");
224 } else { /* second child is stderr */
225 if (open(pferr, O_RDONLY) != 0)
226 error(SYSTEM,
227 "cannot open error pipe in io_process");
228 dup2(2, 1); /* attach stdout to stderr */
229 }
230 } else { /* child passes renderer input */
231 close(1);
232 if (open(pfin, O_WRONLY) != 1)
233 error(SYSTEM, "cannot open input pipe in io_process");
234 }
235 /* pass input to output */
236 /* read as much as we can, write all of it */
237 while ((nr = read(0, cp=buf, sizeof(buf))) > 0)
238 do {
239 if ((n = write(1, cp, nr)) <= 0)
240 error(SYSTEM, "write error in io_process");
241 cp += n;
242 } while ((nr -= n) > 0);
243 if (nr < 0)
244 error(SYSTEM, "read error in io_process");
245 close(0); /* close input */
246 close(1); /* close output */
247 if (pid) /* parent waits for stdin child */
248 wait(0);
249 if (pid2 > 0) /* wait for stderr child also */
250 wait(0);
251 _exit(0); /* all done, exit (not quit!) */
252 formerr:
253 error(USER, "format error in persist file");
254 forkerr:
255 error(SYSTEM, "fork failed in io_process");
256 }
257
258 #else
259
260 pfclean() {}
261
262 #endif