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.40 by schorsch, Sun Apr 27 17:22:49 2008 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 + #include <sys/types.h>
16 +
17 + #include "platform.h"
18 + #ifndef NON_POSIX /* XXX need abstraction for process management */
19 + #include <sys/wait.h>
20 + #endif
21 +
22 + #include "rtprocess.h" /* getpid() */
23   #include "standard.h"
24   #include "random.h"
25 + #include "ray.h"
26  
27   #ifdef F_SETLKW
28   #include "paths.h"
29   #include "selcall.h"
17 #include <signal.h>
18 #include <sys/stat.h>
30  
31   #ifndef TIMELIM
32   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
33   #endif
34  
24 extern char     *strcpy(), *index();
25
35   extern int      headismine;     /* boolean true if header belongs to me */
27
36   extern char     *progname;      /* global program name */
29
37   extern char     *errfile;       /* global error file name */
31
38   static char     *persistfname = NULL;   /* persist file name */
39   static int      persistfd = -1;         /* persist file descriptor */
34
40   static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
41  
42 + typedef void (sighandler_t)(int);
43 + static sighandler_t sig_io;
44 + static sighandler_t sig_alrm;
45  
46 < pfdetach()              /* release persist (and header) resources */
46 >
47 > extern void
48 > pfdetach(void)          /* release persist (and header) resources */
49   {
50          if (persistfd >= 0)
51                  close(persistfd);
# Line 48 | Line 58 | pfdetach()             /* release persist (and header) resources
58   }
59  
60  
61 < pfclean()               /* clean up persist files */
61 > extern void
62 > pfclean(void)           /* clean up persist files */
63   {
64          if (persistfd >= 0)
65                  close(persistfd);
# Line 63 | Line 74 | pfclean()              /* clean up persist files */
74   }
75  
76  
77 < pflock(lf)              /* place or release exclusive lock on file */
78 < int     lf;
77 > extern void
78 > pflock(         /* place or release exclusive lock on file */
79 >        int     lf
80 > )
81   {
82          struct flock    fls;
83  
# Line 77 | Line 90 | int    lf;
90   }
91  
92  
93 < persistfile(pfn)        /* open persist file and lock it */
94 < char    *pfn;
93 > extern void
94 > persistfile(    /* open persist file and lock it */
95 >        char    *pfn
96 > )
97   {
98          persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
99          if (persistfd >= 0) {
# Line 99 | Line 114 | char   *pfn;
114  
115   static int      got_io;
116  
117 < static int sig_io() { got_io++; }
117 > static void sig_io(int i) { got_io++; }
118  
119 < static int sig_alrm() { quit(0); }
119 > static void sig_alrm(int i) { quit(0); }
120  
121  
122 < pfhold()                /* holding pattern for idle rendering process */
122 > extern void
123 > pfhold(void)            /* holding pattern for idle rendering process */
124   {
125 <        int     (*oldalrm)();
125 >        sighandler_t    *oldalrm;
126          char    buf[512];
127          register int    n;
128                                  /* close input and output descriptors */
129 <        close(fileno(stdin));
130 <        close(fileno(stdout));
129 >        close(0);
130 >        close(1);
131          if (errfile == NULL)
132 <                close(fileno(stderr));
132 >                close(2);
133                                  /* create named pipes for input and output */
134 <        if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
134 >        if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
135                  goto createrr;
136 <        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
136 >        if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
137                  goto createrr;
138          if (errfile == NULL &&
139 <                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
139 >                mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0)
140                  goto createrr;
141          sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
142                          inpname, outpname, errname);
143          n = strlen(buf);
144          if (write(persistfd, buf, n) < n)
145                  error(SYSTEM, "error writing persist file");
146 <        lseek(persistfd, 0L, 0);
146 >        lseek(persistfd, (off_t)0, SEEK_SET);
147                                  /* wait TIMELIM for someone to signal us */
148          got_io = 0;
149          signal(SIGIO, sig_io);
150 <        oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
150 >        oldalrm = signal(SIGALRM, sig_alrm);
151          alarm(TIMELIM);
152          pflock(0);                      /* unlock persist file for attach */
153          while (!got_io)
# Line 141 | Line 157 | pfhold()               /* holding pattern for idle rendering proces
157          signal(SIGIO, SIG_DFL);
158          pflock(1);                      /* grab persist file back */
159                                  /* someone wants us; reopen stdin and stdout */
160 +        /*
161          if (freopen(inpname, "r", stdin) == NULL)
162                  goto openerr;
163          if (freopen(outpname, "w", stdout) == NULL)
164                  goto openerr;
165 +        */
166 +        close(0);
167 +        if (open(inpname, O_RDONLY) != 0)
168 +                error(INTERNAL, "unexpected stdin file number");
169 +        clearerr(stdin);
170 +        close(1);
171 +        if (open(outpname, O_WRONLY) != 1)
172 +                error(INTERNAL, "unexpected stdout file number");
173          sleep(3);               /* give them a chance to open their pipes */
174          if (errname[0]) {
175 <                if (freopen(errname, "w", stderr) == NULL)
176 <                        goto openerr;
175 >                close(2);
176 >                if (open(errname, O_WRONLY) != 2)
177 >                        error(INTERNAL, "unexpected stderr file number");
178                  unlink(errname);
179                  errname[0] = '\0';
180          }
# Line 164 | Line 190 | openerr:
190   }
191  
192  
193 < io_process()            /* just act as go-between for actual process */
193 > extern void
194 > io_process(void)                /* just act as go-between for actual process */
195   {
196          register char   *cp;
197          register int    nr, n;
# Line 184 | Line 211 | io_process()           /* just act as go-between for actual pro
211          }
212          if (nr < 0)
213                  error(SYSTEM, "error reading persist file");
214 <        ftruncate(persistfd, 0L);       /* truncate persist file */
214 > #ifndef _WIN32 /* XXX we need a replacement for that one */
215 >        ftruncate(persistfd, (off_t)0L);        /* truncate persist file */
216 > #endif
217          pfdetach();                     /* close & release persist file */
218          buf[nr] = '\0';                 /* parse what we got */
219 <        if ((cp = index(buf, ' ')) == NULL)
219 >        if ((cp = strchr(buf, ' ')) == NULL)
220                  goto formerr;
221          *cp++ = '\0';
222          if ((pid = atoi(cp)) <= 0)
223                  goto formerr;
224 <        if ((cp = index(cp, '\n')) == NULL)
224 >        if ((cp = strchr(cp, '\n')) == NULL)
225                  goto formerr;
226          pfin = ++cp;
227 <        if ((cp = index(cp, '\n')) == NULL)
227 >        if ((cp = strchr(cp, '\n')) == NULL)
228                  goto formerr;
229          *cp++ = '\0';
230          pfout = cp;
231 <        if ((cp = index(cp, '\n')) == NULL)
231 >        if ((cp = strchr(cp, '\n')) == NULL)
232                  goto formerr;
233          *cp++ = '\0';
234          pferr = cp;
235 <        if ((cp = index(cp, '\n')) == NULL)
235 >        if ((cp = strchr(cp, '\n')) == NULL)
236                  goto formerr;
237          *cp++ = '\0';
238          if (cp-buf != nr)
# Line 311 | Line 340 | io_process()           /* just act as go-between for actual pro
340                                  } while ((nr -= n) > 0);
341                  }
342          }
343 <        wait(0);                /* wait for feeder process */
343 >        kill(pid, SIGTERM);     /* no more process to feed, so... */
344 >        waitpid(pid, 0, 0);     /* wait for feeder process */
345          _exit(status);
346   formerr:
347          error(USER, "format error in persist file");
# Line 323 | Line 353 | writerr:
353  
354   #else
355  
356 < pfclean() {}
356 > extern void pfclean(void) {}
357  
358   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines