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.32 by greg, Mon Oct 20 16:01:55 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 <string.h>
13 + #include <signal.h>
14 + #include <sys/stat.h>
15 +
16 + #include "rtprocess.h" /* getpid() */
17   #include "standard.h"
18 + #include "platform.h"
19 + #include "random.h"
20  
21   #ifdef F_SETLKW
22   #include "paths.h"
23 < #include <signal.h>
16 < #include <sys/types.h>
17 < #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
23 > #include "selcall.h"
24  
25   #ifndef TIMELIM
26   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
27   #endif
28  
29 < extern char     *strcpy(), *index();
29 > extern void     io_process();
30  
31   extern int      headismine;     /* boolean true if header belongs to me */
32  
# 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 90 | Line 85 | int    lf;
85   }
86  
87  
88 + void
89   persistfile(pfn)        /* open persist file and lock it */
90   char    *pfn;
91   {
# Line 112 | Line 108 | char   *pfn;
108  
109   static int      got_io;
110  
111 < static int sig_io() { got_io++; }
111 > static void sig_io() { got_io++; }
112  
113 < static int sig_alrm() { quit(0); }
113 > static void sig_alrm() { quit(0); }
114  
115  
116 + void
117   pfhold()                /* holding pattern for idle rendering process */
118   {
119 <        int     (*oldalrm)();
119 >        void    (*oldalrm)();
120          char    buf[512];
121          register int    n;
122                                  /* close input and output descriptors */
# Line 128 | Line 125 | pfhold()               /* holding pattern for idle rendering proces
125          if (errfile == NULL)
126                  close(fileno(stderr));
127                                  /* create named pipes for input and output */
128 <        if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
128 >        if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
129                  goto createrr;
130 <        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
130 >        if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
131                  goto createrr;
132          if (errfile == NULL &&
133 <                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
133 >                mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0)
134                  goto createrr;
135          sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
136                          inpname, outpname, errname);
137          n = strlen(buf);
138          if (write(persistfd, buf, n) < n)
139                  error(SYSTEM, "error writing persist file");
140 +        lseek(persistfd, (off_t)0, SEEK_SET);
141                                  /* wait TIMELIM for someone to signal us */
142          got_io = 0;
143          signal(SIGIO, sig_io);
144 <        oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
144 >        oldalrm = (void (*)())signal(SIGALRM, sig_alrm);
145          alarm(TIMELIM);
146          pflock(0);                      /* unlock persist file for attach */
147          while (!got_io)
148                  pause();                /* wait for attach */
151        pflock(1);                      /* grab persist file right back! */
149          alarm(0);                       /* turn off alarm */
150          signal(SIGALRM, oldalrm);
151          signal(SIGIO, SIG_DFL);
152 <        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
156 <                error(SYSTEM, "seek/truncate error on persist file");
152 >        pflock(1);                      /* grab persist file back */
153                                  /* someone wants us; reopen stdin and stdout */
154 +        /*
155          if (freopen(inpname, "r", stdin) == NULL)
156                  goto openerr;
157          if (freopen(outpname, "w", stdout) == NULL)
158                  goto openerr;
159 +        */
160 +        close(0);
161 +        if (open(inpname, O_RDONLY) != 0)
162 +                error(INTERNAL, "unexpected stdin file number");
163 +        clearerr(stdin);
164 +        close(1);
165 +        if (open(outpname, O_WRONLY) != 1)
166 +                error(INTERNAL, "unexpected stdout file number");
167 +        sleep(3);               /* give them a chance to open their pipes */
168          if (errname[0]) {
169 <                if (freopen(errname, "w", stderr) == NULL)
170 <                        goto openerr;
169 >                close(2);
170 >                if (open(errname, O_WRONLY) != 2)
171 >                        error(INTERNAL, "unexpected stderr file number");
172                  unlink(errname);
173                  errname[0] = '\0';
174          }
# Line 177 | Line 184 | openerr:
184   }
185  
186  
187 + void
188   io_process()            /* just act as go-between for actual process */
189   {
190          register char   *cp;
191          register int    nr, n;
192          char    buf[BUFSIZ], *pfin, *pfout, *pferr;
193          int     pid, nfds;
194 <        int     fdin, fdout, fderr = -1;
195 <        fd_set  readfds, writefds, excepfds;
194 >        int     fdout, fderr = -1;
195 >        int     status = 0;
196 >        fd_set  readfds, excepfds;
197                                          /* load persist file */
198 +        n = 40;
199          while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
200 +                if (!n--)
201 +                        error(USER, "unattended persist file?");
202                  pflock(0);
203 <                sleep(15);              /* wait until ready */
203 >                sleep(3+(3*getpid()+random())%13);      /* wait until ready */
204                  pflock(1);
205          }
194        pfdetach();                     /* close persist file */
206          if (nr < 0)
207                  error(SYSTEM, "error reading persist file");
208 <        buf[nr] = '\0';
209 <        if ((cp = index(buf, ' ')) == NULL)
208 > #ifndef _WIN32 /* XXX we need a replacement for that one */
209 >        ftruncate(persistfd, (off_t)0L);        /* truncate persist file */
210 > #endif
211 >        pfdetach();                     /* close & release persist file */
212 >        buf[nr] = '\0';                 /* parse what we got */
213 >        if ((cp = strchr(buf, ' ')) == NULL)
214                  goto formerr;
215          *cp++ = '\0';
216          if ((pid = atoi(cp)) <= 0)
217                  goto formerr;
218 <        if ((cp = index(cp, '\n')) == NULL)
218 >        if ((cp = strchr(cp, '\n')) == NULL)
219                  goto formerr;
220          pfin = ++cp;
221 <        if ((cp = index(cp, '\n')) == NULL)
221 >        if ((cp = strchr(cp, '\n')) == NULL)
222                  goto formerr;
223          *cp++ = '\0';
224          pfout = cp;
225 <        if ((cp = index(cp, '\n')) == NULL)
225 >        if ((cp = strchr(cp, '\n')) == NULL)
226                  goto formerr;
227          *cp++ = '\0';
228          pferr = cp;
229 <        if ((cp = index(cp, '\n')) == NULL)
229 >        if ((cp = strchr(cp, '\n')) == NULL)
230                  goto formerr;
231          *cp++ = '\0';
232          if (cp-buf != nr)
# Line 223 | Line 238 | io_process()           /* just act as go-between for actual pro
238                                          /* wake up rendering process */
239          if (kill(pid, SIGIO) < 0)
240                  error(SYSTEM, "cannot signal rendering process in io_process");
241 <                                        /* open named pipes, in order */
242 <        if ((fdin = open(pfin, O_WRONLY)) < 0)
243 <                error(SYSTEM, "cannot open input pipe in io_process");
241 >                                        /* fork child feeder process */
242 >        pid = fork();
243 >        if (pid < 0)
244 >                error(SYSTEM, "fork failed in io_process");
245 >        if (pid == 0) {                 /* feeder loop */
246 >                int     fdin;
247 >                close(1);               /* open input pipe */
248 >                if ((fdin = open(pfin, O_WRONLY)) < 0)
249 >                        error(SYSTEM, "cannot open feed pipe in io_process");
250 >                                                /* renderer stdin */
251 >                while ((nr = read(0, cp=buf, sizeof(buf))) > 0) {
252 >                        do {
253 >                                if ((n = write(fdin, cp, nr)) <= 0)
254 >                                        goto writerr;
255 >                                cp += n;
256 >                        } while ((nr -= n) > 0);
257 >                }
258 >                if (nr < 0)
259 >                        goto readerr;
260 >                _exit(0);
261 >        }
262 >        close(0);
263 >                                        /* open output pipes, in order */
264          if ((fdout = open(pfout, O_RDONLY)) < 0)
265                  error(SYSTEM, "cannot open output pipe in io_process");
266          if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
267                  error(SYSTEM, "cannot open error pipe in io_process");
268 <        for ( ; ; ) {                   /* loop on select call */
268 >        for ( ; ; ) {                   /* eater loop */
269                  FD_ZERO(&readfds);
235                FD_ZERO(&writefds);
270                  FD_ZERO(&excepfds);
271                  nfds = 0;
238                if (fdin >= 0) {
239                        FD_SET(fdin, &writefds);
240                        FD_SET(fdin, &excepfds);
241                        nfds = fdin+1;
242                }
272                  if (fdout >= 0) {
273                          FD_SET(fdout, &readfds);
274                          FD_SET(fdout, &excepfds);
# Line 252 | Line 281 | io_process()           /* just act as go-between for actual pro
281                  }
282                  if (nfds == 0)
283                          break;                  /* all done, exit */
284 <                if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0)
284 >                if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0)
285                          error(SYSTEM, "error in select call in io_process");
286                                                  /* renderer stderr */
287                  if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
# Line 262 | Line 291 | io_process()           /* just act as go-between for actual pro
291                                  goto readerr;
292                          if (nr == 0) {
293                                  close(fderr);
294 <                                close(2);
294 >                                /* close(2);    don't close stderr! */
295                                  fderr = -1;
296 <                        } else
297 <                                do {            /* write it all */
296 >                        } else {
297 >                                cp[nr] = '\0';  /* deduce status if we can */
298 >                                n = strlen(progname);
299 >                                if (!strncmp(cp, progname, n) &&
300 >                                                cp[n++] == ':' &&
301 >                                                cp[n++] == ' ') {
302 >                                        register struct erract  *ep;
303 >                                        for (ep = erract; ep < erract+NERRS;
304 >                                                        ep++)
305 >                                                if (ep->pre[0] &&
306 >                                                        !strncmp(cp+n, ep->pre,
307 >                                                            strlen(ep->pre))) {
308 >                                                        status = ep->ec;
309 >                                                        break;
310 >                                                }
311 >                                }
312 >                                do {            /* write message */
313                                          if ((n = write(2, cp, nr)) <= 0)
314                                                  goto writerr;
315                                          cp += n;
316                                  } while ((nr -= n) > 0);
317 +                        }
318                  }
319                                                  /* renderer stdout */
320                  if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
# Line 288 | Line 333 | io_process()           /* just act as go-between for actual pro
333                                          cp += n;
334                                  } while ((nr -= n) > 0);
335                  }
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                }
336          }
337 <        _exit(0);               /* we ought to return renderer error status! */
337 >        wait(0);                /* wait for feeder process */
338 >        _exit(status);
339   formerr:
340          error(USER, "format error in persist file");
341   readerr:
# Line 313 | Line 346 | writerr:
346  
347   #else
348  
349 < pfclean() {}
349 > void pfclean() {}
350  
351   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines