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.24 by gregl, Mon Nov 24 15:13:33 1997 UTC vs.
Revision 2.30 by schorsch, Mon Jul 14 20:02:30 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
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 > #ifndef freebsd
29 > #define mkfifo(fn,md)   mknod(fn, S_IFIFO|(md), 0)
30 > #endif
31  
32 + extern void     io_process();
33 +
34   extern int      headismine;     /* boolean true if header belongs to me */
35  
36   extern char     *progname;      /* global program name */
# Line 34 | Line 43 | static int     persistfd = -1;         /* persist file descriptor
43   static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
44  
45  
46 + void
47   pfdetach()              /* release persist (and header) resources */
48   {
49          if (persistfd >= 0)
# Line 47 | Line 57 | pfdetach()             /* release persist (and header) resources
57   }
58  
59  
60 + void
61   pfclean()               /* clean up persist files */
62   {
63          if (persistfd >= 0)
# Line 62 | Line 73 | pfclean()              /* clean up persist files */
73   }
74  
75  
76 + void
77   pflock(lf)              /* place or release exclusive lock on file */
78   int     lf;
79   {
# Line 76 | Line 88 | int    lf;
88   }
89  
90  
91 + void
92   persistfile(pfn)        /* open persist file and lock it */
93   char    *pfn;
94   {
# Line 98 | Line 111 | char   *pfn;
111  
112   static int      got_io;
113  
114 < static int sig_io() { got_io++; }
114 > static void sig_io() { got_io++; }
115  
116 < static int sig_alrm() { quit(0); }
116 > static void sig_alrm() { quit(0); }
117  
118  
119 + void
120   pfhold()                /* holding pattern for idle rendering process */
121   {
122 <        int     (*oldalrm)();
122 >        void    (*oldalrm)();
123          char    buf[512];
124          register int    n;
125                                  /* close input and output descriptors */
# Line 114 | Line 128 | pfhold()               /* holding pattern for idle rendering proces
128          if (errfile == NULL)
129                  close(fileno(stderr));
130                                  /* create named pipes for input and output */
131 <        if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
131 >        if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
132                  goto createrr;
133 <        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
133 >        if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
134                  goto createrr;
135          if (errfile == NULL &&
136 <                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
136 >                mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0)
137                  goto createrr;
138          sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
139                          inpname, outpname, errname);
140          n = strlen(buf);
141          if (write(persistfd, buf, n) < n)
142                  error(SYSTEM, "error writing persist file");
143 <        lseek(persistfd, 0L, 0);
143 >        lseek(persistfd, (off_t)0L, 0);
144                                  /* wait TIMELIM for someone to signal us */
145          got_io = 0;
146          signal(SIGIO, sig_io);
147 <        oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
147 >        oldalrm = (void (*)())signal(SIGALRM, sig_alrm);
148          alarm(TIMELIM);
149          pflock(0);                      /* unlock persist file for attach */
150          while (!got_io)
# Line 140 | Line 154 | pfhold()               /* holding pattern for idle rendering proces
154          signal(SIGIO, SIG_DFL);
155          pflock(1);                      /* grab persist file back */
156                                  /* someone wants us; reopen stdin and stdout */
157 +        /*
158          if (freopen(inpname, "r", stdin) == NULL)
159                  goto openerr;
160          if (freopen(outpname, "w", stdout) == NULL)
161                  goto openerr;
162 +        */
163 +        close(0);
164 +        if (open(inpname, O_RDONLY) != 0)
165 +                error(INTERNAL, "unexpected stdin file number");
166 +        clearerr(stdin);
167 +        close(1);
168 +        if (open(outpname, O_WRONLY) != 1)
169 +                error(INTERNAL, "unexpected stdout file number");
170          sleep(3);               /* give them a chance to open their pipes */
171          if (errname[0]) {
172 <                if (freopen(errname, "w", stderr) == NULL)
173 <                        goto openerr;
172 >                close(2);
173 >                if (open(errname, O_WRONLY) != 2)
174 >                        error(INTERNAL, "unexpected stderr file number");
175                  unlink(errname);
176                  errname[0] = '\0';
177          }
# Line 163 | Line 187 | openerr:
187   }
188  
189  
190 + void
191   io_process()            /* just act as go-between for actual process */
192   {
193          register char   *cp;
# Line 178 | Line 203 | io_process()           /* just act as go-between for actual pro
203                  if (!n--)
204                          error(USER, "unattended persist file?");
205                  pflock(0);
206 <                sleep(15);              /* wait until ready */
206 >                sleep(3+(3*getpid()+random())%13);      /* wait until ready */
207                  pflock(1);
208          }
209          if (nr < 0)
210                  error(SYSTEM, "error reading persist file");
211 <        ftruncate(persistfd, 0L);       /* truncate persist file */
211 > #ifndef _WIN32 /* XXX we need a replacement for that one */
212 >        ftruncate(persistfd, (off_t)0L);        /* truncate persist file */
213 > #endif
214          pfdetach();                     /* close & release persist file */
215          buf[nr] = '\0';                 /* parse what we got */
216 <        if ((cp = index(buf, ' ')) == NULL)
216 >        if ((cp = strchr(buf, ' ')) == NULL)
217                  goto formerr;
218          *cp++ = '\0';
219          if ((pid = atoi(cp)) <= 0)
220                  goto formerr;
221 <        if ((cp = index(cp, '\n')) == NULL)
221 >        if ((cp = strchr(cp, '\n')) == NULL)
222                  goto formerr;
223          pfin = ++cp;
224 <        if ((cp = index(cp, '\n')) == NULL)
224 >        if ((cp = strchr(cp, '\n')) == NULL)
225                  goto formerr;
226          *cp++ = '\0';
227          pfout = cp;
228 <        if ((cp = index(cp, '\n')) == NULL)
228 >        if ((cp = strchr(cp, '\n')) == NULL)
229                  goto formerr;
230          *cp++ = '\0';
231          pferr = cp;
232 <        if ((cp = index(cp, '\n')) == NULL)
232 >        if ((cp = strchr(cp, '\n')) == NULL)
233                  goto formerr;
234          *cp++ = '\0';
235          if (cp-buf != nr)
# Line 322 | Line 349 | writerr:
349  
350   #else
351  
352 < pfclean() {}
352 > void pfclean() {}
353  
354   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines