ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/persist.c
(Generate patch)

Comparing ray/src/rt/persist.c (file contents):
Revision 2.2 by greg, Thu Jan 21 10:09:04 1993 UTC vs.
Revision 2.45 by greg, Sat Jun 7 05:09:46 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 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 "standard.h"
11 < #include "paths.h"
10 > #include "copyright.h"
11 >
12 > #include <string.h>
13   #include <signal.h>
14 #include <sys/types.h>
14   #include <sys/stat.h>
15 + #include <sys/types.h>
16  
17 + #include "platform.h"
18 + #ifndef NON_POSIX /* XXX need abstraction for process management */
19 + #include <sys/wait.h>
20 + #endif
21 +
22 + #include "rtprocess.h" /* getpid() */
23 + #include "standard.h"
24 + #include "random.h"
25 + #include "ray.h"
26 +
27 + #ifdef F_SETLKW
28 + #include "selcall.h"
29 +
30   #ifndef TIMELIM
31   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
32   #endif
33  
21 extern char     *strcpy(), *index();
22
34   extern int      headismine;     /* boolean true if header belongs to me */
35 <
35 > extern char     *progname;      /* global program name */
36 > extern char     *errfile;       /* global error file name */
37   static char     *persistfname = NULL;   /* persist file name */
38   static int      persistfd = -1;         /* persist file descriptor */
39 + static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
40  
41 < static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1];
41 > typedef void (rsighandler_t)(int);
42 > static rsighandler_t sig_io;
43 > static rsighandler_t sig_alrm;
44  
45  
46 < pfdetach()              /* release persist (and header) resources */
46 > void
47 > pfdetach(void)          /* release persist (and header) resources */
48   {
49          if (persistfd >= 0)
50                  close(persistfd);
# Line 36 | Line 52 | pfdetach()             /* release persist (and header) resources
52          persistfname = NULL;
53          inpname[0] = '\0';
54          outpname[0] = '\0';
55 +        errname[0] = '\0';
56          headismine = 0;
57   }
58  
59  
60 < pfclean()               /* clean up persist files */
60 > void
61 > pfclean(void)           /* clean up persist files */
62   {
63          if (persistfd >= 0)
64                  close(persistfd);
# Line 50 | Line 68 | pfclean()              /* clean up persist files */
68                  unlink(inpname);
69          if (outpname[0])
70                  unlink(outpname);
71 +        if (errname[0])
72 +                unlink(errname);
73   }
74  
75  
76 < pflock(lf)              /* place or release exclusive lock on file */
77 < int     lf;
76 > void
77 > pflock(         /* place or release exclusive lock on file */
78 >        int     lf
79 > )
80   {
81          struct flock    fls;
82  
# Line 67 | Line 89 | int    lf;
89   }
90  
91  
92 < persistfile(pfn)        /* open persist file and lock it */
93 < char    *pfn;
92 > void
93 > persistfile(    /* open persist file and lock it */
94 >        char    *pfn
95 > )
96   {
97 <        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666);
97 >        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
98          if (persistfd >= 0) {
99                  persistfname = pfn;
100                  pflock(1);
# Line 87 | Line 111 | char   *pfn;
111   }
112  
113  
114 < int sig_noop() {}
114 > static int      got_io;
115  
116 + static void sig_io(int i) { got_io++; }
117  
118 < pfhold()                /* holding pattern for idle rendering process */
118 > static void sig_alrm(int i) { quit(0); }
119 >
120 >
121 > void
122 > pfhold(void)            /* holding pattern for idle rendering process */
123   {
124 <        char    buf[128];
125 <        register int    n;
124 >        rsighandler_t   *oldalrm;
125 >        char    buf[512];
126 >        int     n;
127                                  /* close input and output descriptors */
128 <        close(fileno(stdin));
129 <        fflush(stdout);
130 <        close(fileno(stdout));
128 >        close(0);
129 >        close(1);
130 >        if (errfile == NULL)
131 >                close(2);
132                                  /* create named pipes for input and output */
133 <        if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600) < 0)
133 >        if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
134                  goto createrr;
135 <        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0)
135 >        if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
136                  goto createrr;
137 <        sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname);
138 <        if (lseek(persistfd, 0L, 0) < 0)
139 <                error(SYSTEM, "seek error on persist file in pfhold");
137 >        if (errfile == NULL &&
138 >                mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0)
139 >                goto createrr;
140 >        sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
141 >                        inpname, outpname, errname);
142          n = strlen(buf);
143          if (write(persistfd, buf, n) < n)
144 <                error(SYSTEM, "error writing persist file in pfhold");
144 >                error(SYSTEM, "error writing persist file");
145 >        lseek(persistfd, (off_t)0, SEEK_SET);
146                                  /* wait TIMELIM for someone to signal us */
147 <        signal(SIGIO, sig_noop);
147 >        got_io = 0;
148 >        signal(SIGIO, sig_io);
149 >        oldalrm = signal(SIGALRM, sig_alrm);
150          alarm(TIMELIM);
151 <        pflock(0);
152 <        pause();
153 <        alarm(0);
151 >        pflock(0);                      /* unlock persist file for attach */
152 >        while (!got_io)
153 >                pause();                /* wait for attach */
154 >        alarm(0);                       /* turn off alarm */
155 >        signal(SIGALRM, oldalrm);
156          signal(SIGIO, SIG_DFL);
157 <        pflock(1);
157 >        pflock(1);                      /* grab persist file back */
158                                  /* someone wants us; reopen stdin and stdout */
159 <        if (freopen(inpname, "r", stdin) == NULL)
160 <                goto openerr;
161 <        if (freopen(outpname, "w", stdout) == NULL)
162 <                goto openerr;
159 >        close(0);
160 >        if (open(inpname, O_RDONLY) != 0)
161 >                error(INTERNAL, "unexpected stdin file number");
162 >        clearerr(stdin);
163 >        close(1);
164 >        if (open(outpname, O_WRONLY) != 1)
165 >                error(INTERNAL, "unexpected stdout file number");
166 >        sleep(3);               /* give them a chance to open their pipes */
167 >        if (errname[0]) {
168 >                close(2);
169 >                if (open(errname, O_WRONLY) != 2)
170 >                        error(INTERNAL, "unexpected stderr file number");
171 >                unlink(errname);
172 >                errname[0] = '\0';
173 >        }
174          unlink(inpname);
175          inpname[0] = '\0';
176          unlink(outpname);
# Line 129 | Line 178 | pfhold()               /* holding pattern for idle rendering proces
178          return;
179   createrr:
180          error(SYSTEM, "cannot create named pipes in pfhold");
132 openerr:
133        error(SYSTEM, "cannot open named pipes in pfhold");
181   }
182  
183  
184 < io_process()            /* just act as conduits to and from actual process */
184 > void
185 > io_process(void)                /* just act as go-between for actual process */
186   {
187 <        register char   *cp;
188 <        register int    nr, n;
189 <        char    buf[4096], *pfin, *pfout;
190 <        int     pid;
191 <                                /* load and close persist file */
192 <        nr = read(persistfd, buf, sizeof(buf));
193 <        pfdetach();
187 >        char    *cp;
188 >        int     nr, n;
189 >        char    buf[BUFSIZ], *pfin, *pfout, *pferr;
190 >        int     pid, nfds;
191 >        int     fdout, fderr = -1;
192 >        int     status = 0;
193 >        fd_set  readfds, excepfds;
194 >                                        /* load persist file */
195 >        n = 40;
196 >        while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
197 >                if (!n--)
198 >                        error(USER, "unattended persist file?");
199 >                pflock(0);
200 >                sleep(3+(3*getpid()+random())%13);      /* wait until ready */
201 >                pflock(1);
202 >        }
203          if (nr < 0)
204 <                error(SYSTEM, "cannot read persist file");
205 <        buf[nr] = '\0';
206 <        if ((cp = index(buf, '\n')) == NULL)
204 >                error(SYSTEM, "error reading persist file");
205 >        ftruncate(persistfd, (off_t)0L);        /* truncate persist file */
206 >        pfdetach();                     /* close & release persist file */
207 >        buf[nr] = '\0';                 /* parse what we got */
208 >        if ((cp = strchr(buf, ' ')) == NULL)
209                  goto formerr;
210          *cp++ = '\0';
211 <        if ((pid = atoi(buf)) <= 0)
211 >        if ((pid = atoi(cp)) <= 0)
212                  goto formerr;
213 <        pfin = cp;
155 <        if ((cp = index(cp, '\n')) == NULL)
213 >        if ((cp = strchr(cp, '\n')) == NULL)
214                  goto formerr;
215 +        pfin = ++cp;
216 +        if ((cp = strchr(cp, '\n')) == NULL)
217 +                goto formerr;
218          *cp++ = '\0';
219          pfout = cp;
220 <        if ((cp = index(cp, '\n')) == NULL)
220 >        if ((cp = strchr(cp, '\n')) == NULL)
221                  goto formerr;
222          *cp++ = '\0';
223 +        pferr = cp;
224 +        if ((cp = strchr(cp, '\n')) == NULL)
225 +                goto formerr;
226 +        *cp++ = '\0';
227          if (cp-buf != nr)
228                  goto formerr;
229 <                                /* wake up rendering process */
229 >        if (strcmp(buf, progname)) {
230 >                sprintf(errmsg, "persist file for %s, not %s", buf, progname);
231 >                error(USER, errmsg);
232 >        }
233 >                                        /* wake up rendering process */
234          if (kill(pid, SIGIO) < 0)
235                  error(SYSTEM, "cannot signal rendering process in io_process");
236 <        pid = fork();           /* fork i/o process */
236 >                                        /* fork child feeder process */
237 >        pid = fork();
238          if (pid < 0)
239                  error(SYSTEM, "fork failed in io_process");
240 <                                /* connect to appropriate pipe */
241 <        if (pid) {                      /* parent passes renderer output */
242 <                if (freopen(pfout, "r", stdin) == NULL)
243 <                        error(SYSTEM, "cannot open input pipe in io_process");
244 <        } else {                        /* child passes renderer input */
245 <                if (freopen(pfin, "w", stdout) == NULL)
246 <                        error(SYSTEM, "cannot open input pipe in io_process");
240 >        if (pid == 0) {                 /* feeder loop */
241 >                int     fdin;
242 >                close(1);               /* open input pipe */
243 >                if ((fdin = open(pfin, O_WRONLY)) < 0)
244 >                        error(SYSTEM, "cannot open feed pipe in io_process");
245 >                                                /* renderer stdin */
246 >                while ((nr = read(0, cp=buf, sizeof(buf))) > 0) {
247 >                        do {
248 >                                if ((n = write(fdin, cp, nr)) <= 0)
249 >                                        goto writerr;
250 >                                cp += n;
251 >                        } while ((nr -= n) > 0);
252 >                }
253 >                if (nr < 0)
254 >                        goto readerr;
255 >                _exit(0);
256          }
257 <                                /* pass input to output */
258 <        cp = buf; n = sizeof(buf);
259 <        while ((nr = read(fileno(stdin), cp, n)) > 0) {
260 <                nr += cp-buf;
261 <                if ((n = write(fileno(stdout), buf, nr)) <= 0)
262 <                        error(SYSTEM, "write error in io_process");
263 <                cp = buf;
264 <                while (n < nr)
265 <                        *cp++ = buf[n++];
266 <                n = sizeof(buf) - (cp-buf);
257 >        close(0);
258 >                                        /* open output pipes, in order */
259 >        if ((fdout = open(pfout, O_RDONLY)) < 0)
260 >                error(SYSTEM, "cannot open output pipe in io_process");
261 >        if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
262 >                error(SYSTEM, "cannot open error pipe in io_process");
263 >        for ( ; ; ) {                   /* eater loop */
264 >                FD_ZERO(&readfds);
265 >                FD_ZERO(&excepfds);
266 >                nfds = 0;
267 >                if (fdout >= 0) {
268 >                        FD_SET(fdout, &readfds);
269 >                        FD_SET(fdout, &excepfds);
270 >                        nfds = fdout+1;
271 >                }
272 >                if (fderr >= 0) {
273 >                        FD_SET(fderr, &readfds);
274 >                        FD_SET(fderr, &excepfds);
275 >                        nfds = fderr+1;
276 >                }
277 >                if (nfds == 0)
278 >                        break;                  /* all done, exit */
279 >                if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0)
280 >                        error(SYSTEM, "error in select call in io_process");
281 >                                                /* renderer stderr */
282 >                if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
283 >                                FD_ISSET(fderr, &excepfds))) {
284 >                        nr = read(fderr, cp=buf, sizeof(buf));
285 >                        if (nr < 0)
286 >                                goto readerr;
287 >                        if (nr == 0) {
288 >                                close(fderr);
289 >                                /* close(2);    don't close stderr! */
290 >                                fderr = -1;
291 >                        } else {
292 >                                cp[nr] = '\0';  /* deduce status if we can */
293 >                                n = strlen(progname);
294 >                                if (!strncmp(cp, progname, n) &&
295 >                                                cp[n++] == ':' &&
296 >                                                cp[n++] == ' ') {
297 >                                        struct erract   *ep;
298 >                                        for (ep = erract; ep < erract+NERRS;
299 >                                                        ep++)
300 >                                                if (ep->pre[0] &&
301 >                                                        !strncmp(cp+n, ep->pre,
302 >                                                            strlen(ep->pre))) {
303 >                                                        status = ep->ec;
304 >                                                        break;
305 >                                                }
306 >                                }
307 >                                do {            /* write message */
308 >                                        if ((n = write(2, cp, nr)) <= 0)
309 >                                                goto writerr;
310 >                                        cp += n;
311 >                                } while ((nr -= n) > 0);
312 >                        }
313 >                }
314 >                                                /* renderer stdout */
315 >                if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
316 >                                FD_ISSET(fdout, &excepfds))) {
317 >                        nr = read(fdout, cp=buf, sizeof(buf));
318 >                        if (nr < 0)
319 >                                goto readerr;
320 >                        if (nr == 0) {          /* EOF */
321 >                                close(fdout);
322 >                                close(1);
323 >                                fdout = -1;
324 >                        } else
325 >                                do {            /* write it all */
326 >                                        if ((n = write(1, cp, nr)) <= 0)
327 >                                                goto writerr;
328 >                                        cp += n;
329 >                                } while ((nr -= n) > 0);
330 >                }
331          }
332 <        fclose(stdin);
333 <        fclose(stdout);
334 <        if (pid)                /* parent waits for child */
192 <                wait(0);
193 <        exit(0);                /* all done, exit (not quit!) */
332 >        kill(pid, SIGTERM);     /* no more process to feed, so... */
333 >        waitpid(pid, 0, 0);     /* wait for feeder process */
334 >        _exit(status);
335   formerr:
336          error(USER, "format error in persist file");
337 + readerr:
338 +        error(SYSTEM, "read error in io_process");
339 + writerr:
340 +        error(SYSTEM, "write error in io_process");
341   }
342 +
343 + #else
344 +
345 + void pfclean(void) {}
346 +
347 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines