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.22 by gregl, Fri Nov 7 12:12:53 1997 UTC vs.
Revision 2.26 by greg, Sat Feb 22 02:07:29 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 + /* ====================================================================
11 + * The Radiance Software License, Version 1.0
12 + *
13 + * Copyright (c) 1990 - 2002 The Regents of the University of California,
14 + * through Lawrence Berkeley National Laboratory.   All rights reserved.
15 + *
16 + * Redistribution and use in source and binary forms, with or without
17 + * modification, are permitted provided that the following conditions
18 + * are met:
19 + *
20 + * 1. Redistributions of source code must retain the above copyright
21 + *         notice, this list of conditions and the following disclaimer.
22 + *
23 + * 2. Redistributions in binary form must reproduce the above copyright
24 + *       notice, this list of conditions and the following disclaimer in
25 + *       the documentation and/or other materials provided with the
26 + *       distribution.
27 + *
28 + * 3. The end-user documentation included with the redistribution,
29 + *           if any, must include the following acknowledgment:
30 + *             "This product includes Radiance software
31 + *                 (http://radsite.lbl.gov/)
32 + *                 developed by the Lawrence Berkeley National Laboratory
33 + *               (http://www.lbl.gov/)."
34 + *       Alternately, this acknowledgment may appear in the software itself,
35 + *       if and wherever such third-party acknowledgments normally appear.
36 + *
37 + * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38 + *       and "The Regents of the University of California" must
39 + *       not be used to endorse or promote products derived from this
40 + *       software without prior written permission. For written
41 + *       permission, please contact [email protected].
42 + *
43 + * 5. Products derived from this software may not be called "Radiance",
44 + *       nor may "Radiance" appear in their name, without prior written
45 + *       permission of Lawrence Berkeley National Laboratory.
46 + *
47 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 + * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 + * SUCH DAMAGE.
59 + * ====================================================================
60 + *
61 + * This software consists of voluntary contributions made by many
62 + * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
63 + * information on Lawrence Berkeley National Laboratory, please see
64 + * <http://www.lbl.gov/>.
65 + */
66 +
67   #include "standard.h"
68 + #include "random.h"
69  
70   #ifdef F_SETLKW
71   #include "paths.h"
# Line 20 | Line 77 | static char SCCSid[] = "$SunId$ LBL";
77   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
78   #endif
79  
80 < extern char     *strcpy(), *index();
80 > #ifndef freebsd
81 > #define mkfifo(fn,md)   mknod(fn, S_IFIFO|(md), 0)
82 > #endif
83  
84 + extern void     io_process();
85 +
86   extern int      headismine;     /* boolean true if header belongs to me */
87  
88   extern char     *progname;      /* global program name */
# Line 34 | Line 95 | static int     persistfd = -1;         /* persist file descriptor
95   static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
96  
97  
98 + void
99   pfdetach()              /* release persist (and header) resources */
100   {
101          if (persistfd >= 0)
# Line 47 | Line 109 | pfdetach()             /* release persist (and header) resources
109   }
110  
111  
112 + void
113   pfclean()               /* clean up persist files */
114   {
115          if (persistfd >= 0)
# Line 62 | Line 125 | pfclean()              /* clean up persist files */
125   }
126  
127  
128 + void
129   pflock(lf)              /* place or release exclusive lock on file */
130   int     lf;
131   {
# Line 98 | Line 162 | char   *pfn;
162  
163   static int      got_io;
164  
165 < static int sig_io() { got_io++; }
165 > static void sig_io() { got_io++; }
166  
167 < static int sig_alrm() { quit(0); }
167 > static void sig_alrm() { quit(0); }
168  
169  
170 + void
171   pfhold()                /* holding pattern for idle rendering process */
172   {
173 <        int     (*oldalrm)();
173 >        void    (*oldalrm)();
174          char    buf[512];
175          register int    n;
176                                  /* close input and output descriptors */
# Line 114 | Line 179 | pfhold()               /* holding pattern for idle rendering proces
179          if (errfile == NULL)
180                  close(fileno(stderr));
181                                  /* create named pipes for input and output */
182 <        if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
182 >        if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
183                  goto createrr;
184 <        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
184 >        if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
185                  goto createrr;
186          if (errfile == NULL &&
187 <                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
187 >                mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0)
188                  goto createrr;
189          sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
190                          inpname, outpname, errname);
191          n = strlen(buf);
192          if (write(persistfd, buf, n) < n)
193                  error(SYSTEM, "error writing persist file");
194 <        lseek(persistfd, 0L, 0);
194 >        lseek(persistfd, (off_t)0L, 0);
195                                  /* wait TIMELIM for someone to signal us */
196          got_io = 0;
197          signal(SIGIO, sig_io);
198 <        oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
198 >        oldalrm = (void (*)())signal(SIGALRM, sig_alrm);
199          alarm(TIMELIM);
200          pflock(0);                      /* unlock persist file for attach */
201          while (!got_io)
# Line 140 | Line 205 | pfhold()               /* holding pattern for idle rendering proces
205          signal(SIGIO, SIG_DFL);
206          pflock(1);                      /* grab persist file back */
207                                  /* someone wants us; reopen stdin and stdout */
208 +        /*
209          if (freopen(inpname, "r", stdin) == NULL)
210                  goto openerr;
211          if (freopen(outpname, "w", stdout) == NULL)
212                  goto openerr;
213 +        */
214 +        close(0);
215 +        if (open(inpname, O_RDONLY) != 0)
216 +                error(INTERNAL, "unexpected stdin file number");
217 +        clearerr(stdin);
218 +        close(1);
219 +        if (open(outpname, O_WRONLY) != 1)
220 +                error(INTERNAL, "unexpected stdout file number");
221          sleep(3);               /* give them a chance to open their pipes */
222          if (errname[0]) {
223 <                if (freopen(errname, "w", stderr) == NULL)
224 <                        goto openerr;
223 >                close(2);
224 >                if (open(errname, O_WRONLY) != 2)
225 >                        error(INTERNAL, "unexpected stderr file number");
226                  unlink(errname);
227                  errname[0] = '\0';
228          }
# Line 163 | Line 238 | openerr:
238   }
239  
240  
241 + void
242   io_process()            /* just act as go-between for actual process */
243   {
244          register char   *cp;
# Line 170 | Line 246 | io_process()           /* just act as go-between for actual pro
246          char    buf[BUFSIZ], *pfin, *pfout, *pferr;
247          int     pid, nfds;
248          int     fdout, fderr = -1;
249 +        int     status = 0;
250          fd_set  readfds, excepfds;
251                                          /* load persist file */
252          n = 40;
# Line 177 | Line 254 | io_process()           /* just act as go-between for actual pro
254                  if (!n--)
255                          error(USER, "unattended persist file?");
256                  pflock(0);
257 <                sleep(15);              /* wait until ready */
257 >                sleep(3+(3*getpid()+random())%13);      /* wait until ready */
258                  pflock(1);
259          }
260          if (nr < 0)
261                  error(SYSTEM, "error reading persist file");
262 <        ftruncate(persistfd, 0L);       /* truncate persist file */
262 >        ftruncate(persistfd, (off_t)0L);        /* truncate persist file */
263          pfdetach();                     /* close & release persist file */
264          buf[nr] = '\0';                 /* parse what we got */
265          if ((cp = index(buf, ' ')) == NULL)
# Line 268 | Line 345 | io_process()           /* just act as go-between for actual pro
345                                  close(fderr);
346                                  /* close(2);    don't close stderr! */
347                                  fderr = -1;
348 <                        } else
349 <                                do {            /* write it all */
348 >                        } else {
349 >                                cp[nr] = '\0';  /* deduce status if we can */
350 >                                n = strlen(progname);
351 >                                if (!strncmp(cp, progname, n) &&
352 >                                                cp[n++] == ':' &&
353 >                                                cp[n++] == ' ') {
354 >                                        register struct erract  *ep;
355 >                                        for (ep = erract; ep < erract+NERRS;
356 >                                                        ep++)
357 >                                                if (ep->pre[0] &&
358 >                                                        !strncmp(cp+n, ep->pre,
359 >                                                            strlen(ep->pre))) {
360 >                                                        status = ep->ec;
361 >                                                        break;
362 >                                                }
363 >                                }
364 >                                do {            /* write message */
365                                          if ((n = write(2, cp, nr)) <= 0)
366                                                  goto writerr;
367                                          cp += n;
368                                  } while ((nr -= n) > 0);
369 +                        }
370                  }
371                                                  /* renderer stdout */
372                  if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
# Line 293 | Line 386 | io_process()           /* just act as go-between for actual pro
386                                  } while ((nr -= n) > 0);
387                  }
388          }
389 <        _exit(0);               /* we ought to return renderer error status! */
389 >        wait(0);                /* wait for feeder process */
390 >        _exit(status);
391   formerr:
392          error(USER, "format error in persist file");
393   readerr:
# Line 304 | Line 398 | writerr:
398  
399   #else
400  
401 < pfclean() {}
401 > void pfclean() {}
402  
403   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines