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.17 by greg, Fri Jul 19 10:16:00 1996 UTC vs.
Revision 2.34 by schorsch, Tue Mar 30 16:13:01 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
23   #include "paths.h"
24 < #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
24 > #include "selcall.h"
25  
26   #ifndef TIMELIM
27   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
28   #endif
29  
37 extern char     *strcpy(), *index();
38
30   extern int      headismine;     /* boolean true if header belongs to me */
40
31   extern char     *progname;      /* global program name */
42
32   extern char     *errfile;       /* global error file name */
44
33   static char     *persistfname = NULL;   /* persist file name */
34   static int      persistfd = -1;         /* persist file descriptor */
47
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 61 | 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 76 | 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 90 | 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 112 | 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 */
# Line 128 | Line 126 | pfhold()               /* holding pattern for idle rendering proces
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);
141        lseek(persistfd, 0L, 0);
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)
# Line 154 | Line 152 | pfhold()               /* holding pattern for idle rendering proces
152          signal(SIGIO, SIG_DFL);
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 176 | Line 185 | openerr:
185   }
186  
187  
188 < io_process()            /* just act as go-between for 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[BUFSIZ], *pfin, *pfout, *pferr;
194          int     pid, nfds;
195 <        int     fdin, fdout, fderr = -1;
196 <        fd_set  readfds, writefds, excepfds;
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          }
207          if (nr < 0)
208                  error(SYSTEM, "error reading persist file");
209 <        ftruncate(persistfd, 0L);       /* truncate persist file */
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 = index(buf, ' ')) == NULL)
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 226 | Line 239 | io_process()           /* just act as go-between for actual pro
239                                          /* wake up rendering process */
240          if (kill(pid, SIGIO) < 0)
241                  error(SYSTEM, "cannot signal rendering process in io_process");
242 <                                        /* open named pipes, in order */
243 <        if ((fdin = open(pfin, O_WRONLY)) < 0)
244 <                error(SYSTEM, "cannot open input pipe in io_process");
242 >                                        /* fork child feeder process */
243 >        pid = fork();
244 >        if (pid < 0)
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 (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 ( ; ; ) {                   /* loop on select call */
269 >        for ( ; ; ) {                   /* eater loop */
270                  FD_ZERO(&readfds);
238                FD_ZERO(&writefds);
271                  FD_ZERO(&excepfds);
272                  nfds = 0;
241                if (fdin >= 0) {
242                        FD_SET(fdin, &writefds);
243                        FD_SET(fdin, &excepfds);
244                        nfds = fdin+1;
245                }
273                  if (fdout >= 0) {
274                          FD_SET(fdout, &readfds);
275                          FD_SET(fdout, &excepfds);
# Line 255 | Line 282 | io_process()           /* just act as go-between for actual pro
282                  }
283                  if (nfds == 0)
284                          break;                  /* all done, exit */
285 <                if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0)
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) ||
# Line 265 | Line 292 | io_process()           /* just act as go-between for actual pro
292                                  goto readerr;
293                          if (nr == 0) {
294                                  close(fderr);
295 <                                close(2);
295 >                                /* close(2);    don't close stderr! */
296                                  fderr = -1;
297 <                        } else
298 <                                do {            /* write it all */
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) ||
# Line 291 | Line 334 | io_process()           /* just act as go-between for actual pro
334                                          cp += n;
335                                  } while ((nr -= n) > 0);
336                  }
294                                                /* renderer stdin */
295                if (fdin >= 0 && (FD_ISSET(fdin, &writefds) ||
296                                FD_ISSET(fdin, &excepfds))) {
297                        nr = read(0, buf, 512); /* use minimal buffer */
298                        if (nr < 0)
299                                goto readerr;
300                        if (nr == 0) {
301                                close(0);
302                                close(fdin);
303                                fdin = -1;
304                        } else if (write(fdin, buf, nr) < nr)
305                                goto writerr;   /* what else to do? */
306                }
337          }
338 <        _exit(0);               /* we ought to return renderer error status! */
338 >        wait(0);                /* wait for feeder process */ /* XXX platform */
339 >        _exit(status);
340   formerr:
341          error(USER, "format error in persist file");
342   readerr:
# Line 316 | Line 347 | writerr:
347  
348   #else
349  
350 < pfclean() {}
350 > extern void pfclean(void) {}
351  
352   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines