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.23 by gregl, Tue Nov 11 20:29:25 1997 UTC vs.
Revision 2.31 by schorsch, Sat Aug 30 09:03:31 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 "random.h"
19  
20   #ifdef F_SETLKW
21   #include "paths.h"
22   #include "selcall.h"
16 #include <signal.h>
17 #include <sys/stat.h>
23  
24   #ifndef TIMELIM
25   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
26   #endif
27  
28 < extern char     *strcpy(), *index();
28 > extern void     io_process();
29  
30   extern int      headismine;     /* boolean true if header belongs to me */
31  
# Line 34 | Line 39 | static int     persistfd = -1;         /* persist file descriptor
39   static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
40  
41  
42 + void
43   pfdetach()              /* release persist (and header) resources */
44   {
45          if (persistfd >= 0)
# Line 47 | Line 53 | pfdetach()             /* release persist (and header) resources
53   }
54  
55  
56 + void
57   pfclean()               /* clean up persist files */
58   {
59          if (persistfd >= 0)
# Line 62 | Line 69 | pfclean()              /* clean up persist files */
69   }
70  
71  
72 + void
73   pflock(lf)              /* place or release exclusive lock on file */
74   int     lf;
75   {
# Line 76 | Line 84 | int    lf;
84   }
85  
86  
87 + void
88   persistfile(pfn)        /* open persist file and lock it */
89   char    *pfn;
90   {
# Line 98 | 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 114 | 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, 0L, 0);
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)
# Line 140 | Line 150 | pfhold()               /* holding pattern for idle rendering proces
150          signal(SIGIO, SIG_DFL);
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 163 | Line 183 | openerr:
183   }
184  
185  
186 + void
187   io_process()            /* just act as go-between for actual process */
188   {
189          register char   *cp;
# Line 178 | Line 199 | io_process()           /* just act as go-between for actual pro
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          }
205          if (nr < 0)
206                  error(SYSTEM, "error reading persist file");
207 <        ftruncate(persistfd, 0L);       /* truncate persist file */
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)
212 >        if ((cp = strchr(buf, ' ')) == NULL)
213                  goto formerr;
214          *cp++ = '\0';
215          if ((pid = atoi(cp)) <= 0)
216                  goto formerr;
217 <        if ((cp = index(cp, '\n')) == NULL)
217 >        if ((cp = strchr(cp, '\n')) == NULL)
218                  goto formerr;
219          pfin = ++cp;
220 <        if ((cp = index(cp, '\n')) == NULL)
220 >        if ((cp = strchr(cp, '\n')) == NULL)
221                  goto formerr;
222          *cp++ = '\0';
223          pfout = cp;
224 <        if ((cp = index(cp, '\n')) == NULL)
224 >        if ((cp = strchr(cp, '\n')) == NULL)
225                  goto formerr;
226          *cp++ = '\0';
227          pferr = cp;
228 <        if ((cp = index(cp, '\n')) == NULL)
228 >        if ((cp = strchr(cp, '\n')) == NULL)
229                  goto formerr;
230          *cp++ = '\0';
231          if (cp-buf != nr)
# Line 269 | Line 292 | io_process()           /* just act as go-between for actual pro
292                                  close(fderr);
293                                  /* close(2);    don't close stderr! */
294                                  fderr = -1;
295 <                        } else
295 >                        } else {
296                                  cp[nr] = '\0';  /* deduce status if we can */
297                                  n = strlen(progname);
298                                  if (!strncmp(cp, progname, n) &&
# Line 290 | Line 313 | io_process()           /* just act as go-between for actual pro
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 321 | 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