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.25 by gregl, Sat Jan 3 20:07:31 1998 UTC vs.
Revision 2.36 by greg, Sun Sep 19 07:24:37 2004 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 "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 "selcall.h"
17 #include <signal.h>
18 #include <sys/stat.h>
25  
26   #ifndef TIMELIM
27   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
28   #endif
29  
24 extern char     *strcpy(), *index();
25
30   extern int      headismine;     /* boolean true if header belongs to me */
27
31   extern char     *progname;      /* global program name */
29
32   extern char     *errfile;       /* global error file name */
31
33   static char     *persistfname = NULL;   /* persist file name */
34   static int      persistfd = -1;         /* persist file descriptor */
34
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 48 | 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 63 | 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 77 | 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 99 | 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 115 | 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);
139          if (write(persistfd, buf, n) < n)
140                  error(SYSTEM, "error writing persist file");
141 <        lseek(persistfd, 0L, 0);
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 141 | 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 164 | 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;
# Line 184 | Line 206 | io_process()           /* just act as go-between for actual pro
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 311 | Line 335 | io_process()           /* just act as go-between for actual pro
335                                  } while ((nr -= n) > 0);
336                  }
337          }
338 <        wait(0);                /* wait for feeder process */
338 >        kill(pid, SIGTERM);     /* no more process to feed, so... */
339 >        waitpid(pid, 0, 0);     /* wait for feeder process */
340          _exit(status);
341   formerr:
342          error(USER, "format error in persist file");
# Line 323 | Line 348 | writerr:
348  
349   #else
350  
351 < pfclean() {}
351 > extern void pfclean(void) {}
352  
353   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines