ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/persist.c
Revision: 2.12
Committed: Tue Jan 23 15:51:38 1996 UTC (28 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.11: +16 -8 lines
Log Message:
added program name to beginning of persist file for safety reasons

File Contents

# Content
1 /* Copyright (c) 1995 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 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 int sig_noop() {}
96
97
98 pfhold() /* holding pattern for idle rendering process */
99 {
100 char buf[512];
101 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 if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
107 goto createrr;
108 if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
109 goto createrr;
110 sprintf(buf, "%s %d\n%s\n%s\n", progname, getpid(), inpname, outpname);
111 if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
112 error(SYSTEM, "seek/truncate error on persist file");
113 n = strlen(buf);
114 if (write(persistfd, buf, n) < n)
115 error(SYSTEM, "error writing persist file");
116 /* wait TIMELIM for someone to signal us */
117 signal(SIGIO, sig_noop);
118 alarm(TIMELIM);
119 pflock(0);
120 pause();
121 alarm(0);
122 signal(SIGIO, SIG_DFL);
123 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 char buf[512], *pfin, *pfout;
146 int pid;
147 /* load and close persist file */
148 nr = read(persistfd, buf, sizeof(buf)-1);
149 pfdetach();
150 if (nr < 0)
151 error(SYSTEM, "cannot read persist file");
152 buf[nr] = '\0';
153 if ((cp = index(buf, ' ')) == NULL)
154 goto formerr;
155 *cp++ = '\0';
156 if ((pid = atoi(cp)) <= 0)
157 goto formerr;
158 if ((cp = index(cp, '\n')) == NULL)
159 goto formerr;
160 pfin = ++cp;
161 if ((cp = index(cp, '\n')) == NULL)
162 goto formerr;
163 *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 if (strcmp(buf, progname)) {
171 sprintf(errmsg, "persist file for %s, not %s", buf, progname);
172 error(USER, errmsg);
173 }
174 /* wake up rendering process */
175 if (kill(pid, SIGIO) < 0)
176 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 close(0);
183 if (open(pfout, O_RDONLY) != 0)
184 error(SYSTEM, "cannot open input pipe in io_process");
185 } else { /* child passes renderer input */
186 close(1);
187 if (open(pfin, O_WRONLY) != 1)
188 error(SYSTEM, "cannot open output pipe in io_process");
189 }
190 /* pass input to output */
191 /* 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 if (nr < 0)
199 error(SYSTEM, "read error in io_process");
200 close(0); /* close input */
201 close(1); /* close output */
202 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 writerr:
208 error(SYSTEM, "write error in io_process");
209 }
210
211 #else
212
213 pfclean() {}
214
215 #endif