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.16 by greg, Mon Jul 8 19:46:21 1996 UTC vs.
Revision 2.28 by schorsch, Thu Jun 26 00:58:10 2003 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 "standard.h"
13 + #include "random.h"
14  
15   #ifdef F_SETLKW
16   #include "paths.h"
17 + #include "selcall.h"
18   #include <signal.h>
16 #include <sys/types.h>
19   #include <sys/stat.h>
18                                        /* select call compatibility stuff */
19 #ifndef FD_SETSIZE
20 #include <sys/param.h>
21 #define FD_SETSIZE      NOFILE          /* maximum # select file descriptors */
22 #endif
23 #ifndef FD_SET
24 #ifndef NFDBITS
25 #define NFDBITS         (8*sizeof(int)) /* number of bits per fd_mask */
26 #endif
27 #define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
28 #define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
29 #define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
30 #define FD_ZERO(p)      bzero((char *)(p), sizeof(*(p)))
31 #endif
20  
21   #ifndef TIMELIM
22   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
23   #endif
24  
25 < extern char     *strcpy(), *index();
25 > #ifndef freebsd
26 > #define mkfifo(fn,md)   mknod(fn, S_IFIFO|(md), 0)
27 > #endif
28  
29 + extern void     io_process();
30 +
31   extern int      headismine;     /* boolean true if header belongs to me */
32  
33   extern char     *progname;      /* global program name */
# Line 48 | Line 40 | static int     persistfd = -1;         /* persist file descriptor
40   static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
41  
42  
43 + void
44   pfdetach()              /* release persist (and header) resources */
45   {
46          if (persistfd >= 0)
# Line 61 | Line 54 | pfdetach()             /* release persist (and header) resources
54   }
55  
56  
57 + void
58   pfclean()               /* clean up persist files */
59   {
60          if (persistfd >= 0)
# Line 76 | Line 70 | pfclean()              /* clean up persist files */
70   }
71  
72  
73 + void
74   pflock(lf)              /* place or release exclusive lock on file */
75   int     lf;
76   {
# Line 112 | Line 107 | char   *pfn;
107  
108   static int      got_io;
109  
110 < static int sig_io() { got_io++; }
110 > static void sig_io() { got_io++; }
111  
112 < static int sig_alrm() { quit(0); }
112 > static void sig_alrm() { quit(0); }
113  
114  
115 + void
116   pfhold()                /* holding pattern for idle rendering process */
117   {
118 <        int     (*oldalrm)();
118 >        void    (*oldalrm)();
119          char    buf[512];
120          register int    n;
121                                  /* close input and output descriptors */
# Line 128 | Line 124 | pfhold()               /* holding pattern for idle rendering proces
124          if (errfile == NULL)
125                  close(fileno(stderr));
126                                  /* create named pipes for input and output */
127 <        if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
127 >        if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
128                  goto createrr;
129 <        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
129 >        if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
130                  goto createrr;
131          if (errfile == NULL &&
132 <                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
132 >                mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0)
133                  goto createrr;
134          sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
135                          inpname, outpname, errname);
136          n = strlen(buf);
137          if (write(persistfd, buf, n) < n)
138                  error(SYSTEM, "error writing persist file");
139 +        lseek(persistfd, (off_t)0L, 0);
140                                  /* wait TIMELIM for someone to signal us */
141          got_io = 0;
142          signal(SIGIO, sig_io);
143 <        oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
143 >        oldalrm = (void (*)())signal(SIGALRM, sig_alrm);
144          alarm(TIMELIM);
145          pflock(0);                      /* unlock persist file for attach */
146          while (!got_io)
147                  pause();                /* wait for attach */
151        pflock(1);                      /* grab persist file right back! */
148          alarm(0);                       /* turn off alarm */
149          signal(SIGALRM, oldalrm);
150          signal(SIGIO, SIG_DFL);
151 <        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
156 <                error(SYSTEM, "seek/truncate error on persist file");
151 >        pflock(1);                      /* grab persist file back */
152                                  /* someone wants us; reopen stdin and stdout */
153 +        /*
154          if (freopen(inpname, "r", stdin) == NULL)
155                  goto openerr;
156          if (freopen(outpname, "w", stdout) == NULL)
157                  goto openerr;
158 +        */
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 <                if (freopen(errname, "w", stderr) == NULL)
169 <                        goto openerr;
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          }
# Line 177 | Line 183 | openerr:
183   }
184  
185  
186 + void
187   io_process()            /* just act as go-between for actual process */
188   {
189          register char   *cp;
190          register int    nr, n;
191          char    buf[BUFSIZ], *pfin, *pfout, *pferr;
192          int     pid, nfds;
193 <        int     fdin, fdout, fderr = -1;
194 <        fd_set  readfds, writefds, excepfds;
193 >        int     fdout, fderr = -1;
194 >        int     status = 0;
195 >        fd_set  readfds, excepfds;
196                                          /* load persist file */
197 +        n = 40;
198          while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
199 +                if (!n--)
200 +                        error(USER, "unattended persist file?");
201                  pflock(0);
202 <                sleep(15);              /* wait until ready */
202 >                sleep(3+(3*getpid()+random())%13);      /* wait until ready */
203                  pflock(1);
204          }
194        pfdetach();                     /* close persist file */
205          if (nr < 0)
206                  error(SYSTEM, "error reading persist file");
207 <        buf[nr] = '\0';
207 > #ifndef _WIN32 /* XXX we need a replacement for that one */
208 >        ftruncate(persistfd, (off_t)0L);        /* truncate persist file */
209 > #endif
210 >        pfdetach();                     /* close & release persist file */
211 >        buf[nr] = '\0';                 /* parse what we got */
212          if ((cp = index(buf, ' ')) == NULL)
213                  goto formerr;
214          *cp++ = '\0';
# Line 223 | Line 237 | io_process()           /* just act as go-between for actual pro
237                                          /* wake up rendering process */
238          if (kill(pid, SIGIO) < 0)
239                  error(SYSTEM, "cannot signal rendering process in io_process");
240 <                                        /* open named pipes, in order */
241 <        if ((fdin = open(pfin, O_WRONLY)) < 0)
242 <                error(SYSTEM, "cannot open input pipe in io_process");
240 >                                        /* fork child feeder process */
241 >        pid = fork();
242 >        if (pid < 0)
243 >                error(SYSTEM, "fork failed in io_process");
244 >        if (pid == 0) {                 /* feeder loop */
245 >                int     fdin;
246 >                close(1);               /* open input pipe */
247 >                if ((fdin = open(pfin, O_WRONLY)) < 0)
248 >                        error(SYSTEM, "cannot open feed pipe in io_process");
249 >                                                /* renderer stdin */
250 >                while ((nr = read(0, cp=buf, sizeof(buf))) > 0) {
251 >                        do {
252 >                                if ((n = write(fdin, cp, nr)) <= 0)
253 >                                        goto writerr;
254 >                                cp += n;
255 >                        } while ((nr -= n) > 0);
256 >                }
257 >                if (nr < 0)
258 >                        goto readerr;
259 >                _exit(0);
260 >        }
261 >        close(0);
262 >                                        /* open output pipes, in order */
263          if ((fdout = open(pfout, O_RDONLY)) < 0)
264                  error(SYSTEM, "cannot open output pipe in io_process");
265          if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
266                  error(SYSTEM, "cannot open error pipe in io_process");
267 <        for ( ; ; ) {                   /* loop on select call */
267 >        for ( ; ; ) {                   /* eater loop */
268                  FD_ZERO(&readfds);
235                FD_ZERO(&writefds);
269                  FD_ZERO(&excepfds);
270                  nfds = 0;
238                if (fdin >= 0) {
239                        FD_SET(fdin, &writefds);
240                        FD_SET(fdin, &excepfds);
241                        nfds = fdin+1;
242                }
271                  if (fdout >= 0) {
272                          FD_SET(fdout, &readfds);
273                          FD_SET(fdout, &excepfds);
# Line 252 | Line 280 | io_process()           /* just act as go-between for actual pro
280                  }
281                  if (nfds == 0)
282                          break;                  /* all done, exit */
283 <                if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0)
283 >                if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0)
284                          error(SYSTEM, "error in select call in io_process");
285                                                  /* renderer stderr */
286                  if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
# Line 262 | Line 290 | io_process()           /* just act as go-between for actual pro
290                                  goto readerr;
291                          if (nr == 0) {
292                                  close(fderr);
293 <                                close(2);
293 >                                /* close(2);    don't close stderr! */
294                                  fderr = -1;
295 <                        } else
296 <                                do {            /* write it all */
295 >                        } else {
296 >                                cp[nr] = '\0';  /* deduce status if we can */
297 >                                n = strlen(progname);
298 >                                if (!strncmp(cp, progname, n) &&
299 >                                                cp[n++] == ':' &&
300 >                                                cp[n++] == ' ') {
301 >                                        register struct erract  *ep;
302 >                                        for (ep = erract; ep < erract+NERRS;
303 >                                                        ep++)
304 >                                                if (ep->pre[0] &&
305 >                                                        !strncmp(cp+n, ep->pre,
306 >                                                            strlen(ep->pre))) {
307 >                                                        status = ep->ec;
308 >                                                        break;
309 >                                                }
310 >                                }
311 >                                do {            /* write message */
312                                          if ((n = write(2, cp, nr)) <= 0)
313                                                  goto writerr;
314                                          cp += n;
315                                  } while ((nr -= n) > 0);
316 +                        }
317                  }
318                                                  /* renderer stdout */
319                  if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
# Line 288 | Line 332 | io_process()           /* just act as go-between for actual pro
332                                          cp += n;
333                                  } while ((nr -= n) > 0);
334                  }
291                                                /* renderer stdin */
292                if (fdin >= 0 && (FD_ISSET(fdin, &writefds) ||
293                                FD_ISSET(fdin, &excepfds))) {
294                        nr = read(0, buf, 512); /* use minimal buffer */
295                        if (nr < 0)
296                                goto readerr;
297                        if (nr == 0) {
298                                close(0);
299                                close(fdin);
300                                fdin = -1;
301                        } else if (write(fdin, buf, nr) < nr)
302                                goto writerr;   /* what else to do? */
303                }
335          }
336 <        _exit(0);               /* we ought to return renderer error status! */
336 >        wait(0);                /* wait for feeder process */
337 >        _exit(status);
338   formerr:
339          error(USER, "format error in persist file");
340   readerr:
# Line 313 | Line 345 | writerr:
345  
346   #else
347  
348 < pfclean() {}
348 > void pfclean() {}
349  
350   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines