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.15 by greg, Sat Jul 6 23:19:57 1996 UTC vs.
Revision 2.36 by greg, Sun Sep 19 07:24:37 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines