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.21 by gregl, Tue Oct 28 14:00:02 1997 UTC vs.
Revision 2.39 by schorsch, Mon Apr 21 07:31:30 2008 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 + #include <sys/types.h>
16 + #ifndef NON_POSIX /* XXX need abstraction for process management */
17 + #include <sys/wait.h>
18 + #endif
19 +
20 + #include "platform.h"
21 + #include "rtprocess.h" /* getpid() */
22   #include "standard.h"
23 + #include "random.h"
24 + #include "ray.h"
25  
26   #ifdef F_SETLKW
27   #include "paths.h"
28   #include "selcall.h"
16 #include <signal.h>
17 #include <sys/stat.h>
29  
30   #ifndef TIMELIM
31   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
32   #endif
33  
23 extern char     *strcpy(), *index();
24
34   extern int      headismine;     /* boolean true if header belongs to me */
26
35   extern char     *progname;      /* global program name */
28
36   extern char     *errfile;       /* global error file name */
30
37   static char     *persistfname = NULL;   /* persist file name */
38   static int      persistfd = -1;         /* persist file descriptor */
33
39   static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
40  
41 + typedef void (sighandler_t)(int);
42 + static sighandler_t sig_io;
43 + static sighandler_t sig_alrm;
44  
45 < pfdetach()              /* release persist (and header) resources */
45 >
46 > extern void
47 > pfdetach(void)          /* release persist (and header) resources */
48   {
49          if (persistfd >= 0)
50                  close(persistfd);
# Line 47 | Line 57 | pfdetach()             /* release persist (and header) resources
57   }
58  
59  
60 < pfclean()               /* clean up persist files */
60 > extern void
61 > pfclean(void)           /* clean up persist files */
62   {
63          if (persistfd >= 0)
64                  close(persistfd);
# Line 62 | Line 73 | pfclean()              /* clean up persist files */
73   }
74  
75  
76 < pflock(lf)              /* place or release exclusive lock on file */
77 < int     lf;
76 > extern void
77 > pflock(         /* place or release exclusive lock on file */
78 >        int     lf
79 > )
80   {
81          struct flock    fls;
82  
# Line 76 | Line 89 | int    lf;
89   }
90  
91  
92 < persistfile(pfn)        /* open persist file and lock it */
93 < char    *pfn;
92 > extern void
93 > persistfile(    /* open persist file and lock it */
94 >        char    *pfn
95 > )
96   {
97          persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
98          if (persistfd >= 0) {
# Line 98 | Line 113 | char   *pfn;
113  
114   static int      got_io;
115  
116 < static int sig_io() { got_io++; }
116 > static void sig_io(int i) { got_io++; }
117  
118 < static int sig_alrm() { quit(0); }
118 > static void sig_alrm(int i) { quit(0); }
119  
120  
121 < pfhold()                /* holding pattern for idle rendering process */
121 > extern void
122 > pfhold(void)            /* holding pattern for idle rendering process */
123   {
124 <        int     (*oldalrm)();
124 >        sighandler_t    *oldalrm;
125          char    buf[512];
126          register int    n;
127                                  /* close input and output descriptors */
128 <        close(fileno(stdin));
129 <        close(fileno(stdout));
128 >        close(0);
129 >        close(1);
130          if (errfile == NULL)
131 <                close(fileno(stderr));
131 >                close(2);
132                                  /* create named pipes for input and output */
133 <        if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
133 >        if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
134                  goto createrr;
135 <        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
135 >        if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
136                  goto createrr;
137          if (errfile == NULL &&
138 <                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
138 >                mkfifo(mktemp(strcpy(errname,TEMPLATE)), 0600) < 0)
139                  goto createrr;
140          sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
141                          inpname, outpname, errname);
142          n = strlen(buf);
143          if (write(persistfd, buf, n) < n)
144                  error(SYSTEM, "error writing persist file");
145 <        lseek(persistfd, 0L, 0);
145 >        lseek(persistfd, (off_t)0, SEEK_SET);
146                                  /* wait TIMELIM for someone to signal us */
147          got_io = 0;
148          signal(SIGIO, sig_io);
149 <        oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
149 >        oldalrm = signal(SIGALRM, sig_alrm);
150          alarm(TIMELIM);
151          pflock(0);                      /* unlock persist file for attach */
152          while (!got_io)
# Line 140 | Line 156 | pfhold()               /* holding pattern for idle rendering proces
156          signal(SIGIO, SIG_DFL);
157          pflock(1);                      /* grab persist file back */
158                                  /* someone wants us; reopen stdin and stdout */
159 +        /*
160          if (freopen(inpname, "r", stdin) == NULL)
161                  goto openerr;
162          if (freopen(outpname, "w", stdout) == NULL)
163                  goto openerr;
164 +        */
165 +        close(0);
166 +        if (open(inpname, O_RDONLY) != 0)
167 +                error(INTERNAL, "unexpected stdin file number");
168 +        clearerr(stdin);
169 +        close(1);
170 +        if (open(outpname, O_WRONLY) != 1)
171 +                error(INTERNAL, "unexpected stdout file number");
172 +        sleep(3);               /* give them a chance to open their pipes */
173          if (errname[0]) {
174 <                if (freopen(errname, "w", stderr) == NULL)
175 <                        goto openerr;
174 >                close(2);
175 >                if (open(errname, O_WRONLY) != 2)
176 >                        error(INTERNAL, "unexpected stderr file number");
177                  unlink(errname);
178                  errname[0] = '\0';
179          }
# Line 162 | Line 189 | openerr:
189   }
190  
191  
192 < io_process()            /* just act as go-between for actual process */
192 > extern void
193 > io_process(void)                /* just act as go-between for actual process */
194   {
195          register char   *cp;
196          register int    nr, n;
197          char    buf[BUFSIZ], *pfin, *pfout, *pferr;
198          int     pid, nfds;
199 <        int     fdin, fdout, fderr = -1;
200 <        fd_set  readfds, writefds, excepfds;
199 >        int     fdout, fderr = -1;
200 >        int     status = 0;
201 >        fd_set  readfds, excepfds;
202                                          /* load persist file */
203          n = 40;
204          while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
205                  if (!n--)
206                          error(USER, "unattended persist file?");
207                  pflock(0);
208 <                sleep(15);              /* wait until ready */
208 >                sleep(3+(3*getpid()+random())%13);      /* wait until ready */
209                  pflock(1);
210          }
211          if (nr < 0)
212                  error(SYSTEM, "error reading persist file");
213 <        ftruncate(persistfd, 0L);       /* truncate persist file */
213 > #ifndef _WIN32 /* XXX we need a replacement for that one */
214 >        ftruncate(persistfd, (off_t)0L);        /* truncate persist file */
215 > #endif
216          pfdetach();                     /* close & release persist file */
217          buf[nr] = '\0';                 /* parse what we got */
218 <        if ((cp = index(buf, ' ')) == NULL)
218 >        if ((cp = strchr(buf, ' ')) == NULL)
219                  goto formerr;
220          *cp++ = '\0';
221          if ((pid = atoi(cp)) <= 0)
222                  goto formerr;
223 <        if ((cp = index(cp, '\n')) == NULL)
223 >        if ((cp = strchr(cp, '\n')) == NULL)
224                  goto formerr;
225          pfin = ++cp;
226 <        if ((cp = index(cp, '\n')) == NULL)
226 >        if ((cp = strchr(cp, '\n')) == NULL)
227                  goto formerr;
228          *cp++ = '\0';
229          pfout = cp;
230 <        if ((cp = index(cp, '\n')) == NULL)
230 >        if ((cp = strchr(cp, '\n')) == NULL)
231                  goto formerr;
232          *cp++ = '\0';
233          pferr = cp;
234 <        if ((cp = index(cp, '\n')) == NULL)
234 >        if ((cp = strchr(cp, '\n')) == NULL)
235                  goto formerr;
236          *cp++ = '\0';
237          if (cp-buf != nr)
# Line 212 | Line 243 | io_process()           /* just act as go-between for actual pro
243                                          /* wake up rendering process */
244          if (kill(pid, SIGIO) < 0)
245                  error(SYSTEM, "cannot signal rendering process in io_process");
246 <                                        /* open named pipes, in order */
247 <        if ((fdin = open(pfin, O_WRONLY)) < 0)
248 <                error(SYSTEM, "cannot open input pipe in io_process");
246 >                                        /* fork child feeder process */
247 >        pid = fork();
248 >        if (pid < 0)
249 >                error(SYSTEM, "fork failed in io_process");
250 >        if (pid == 0) {                 /* feeder loop */
251 >                int     fdin;
252 >                close(1);               /* open input pipe */
253 >                if ((fdin = open(pfin, O_WRONLY)) < 0)
254 >                        error(SYSTEM, "cannot open feed pipe in io_process");
255 >                                                /* renderer stdin */
256 >                while ((nr = read(0, cp=buf, sizeof(buf))) > 0) {
257 >                        do {
258 >                                if ((n = write(fdin, cp, nr)) <= 0)
259 >                                        goto writerr;
260 >                                cp += n;
261 >                        } while ((nr -= n) > 0);
262 >                }
263 >                if (nr < 0)
264 >                        goto readerr;
265 >                _exit(0);
266 >        }
267 >        close(0);
268 >                                        /* open output pipes, in order */
269          if ((fdout = open(pfout, O_RDONLY)) < 0)
270                  error(SYSTEM, "cannot open output pipe in io_process");
271          if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
272                  error(SYSTEM, "cannot open error pipe in io_process");
273 <        for ( ; ; ) {                   /* loop on select call */
273 >        for ( ; ; ) {                   /* eater loop */
274                  FD_ZERO(&readfds);
224                FD_ZERO(&writefds);
275                  FD_ZERO(&excepfds);
276                  nfds = 0;
227                if (fdin >= 0) {
228                        FD_SET(fdin, &writefds);
229                        FD_SET(fdin, &excepfds);
230                        nfds = fdin+1;
231                }
277                  if (fdout >= 0) {
278                          FD_SET(fdout, &readfds);
279                          FD_SET(fdout, &excepfds);
# Line 241 | Line 286 | io_process()           /* just act as go-between for actual pro
286                  }
287                  if (nfds == 0)
288                          break;                  /* all done, exit */
289 <                if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0)
289 >                if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0)
290                          error(SYSTEM, "error in select call in io_process");
291                                                  /* renderer stderr */
292                  if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
# Line 253 | Line 298 | io_process()           /* just act as go-between for actual pro
298                                  close(fderr);
299                                  /* close(2);    don't close stderr! */
300                                  fderr = -1;
301 <                        } else
302 <                                do {            /* write it all */
301 >                        } else {
302 >                                cp[nr] = '\0';  /* deduce status if we can */
303 >                                n = strlen(progname);
304 >                                if (!strncmp(cp, progname, n) &&
305 >                                                cp[n++] == ':' &&
306 >                                                cp[n++] == ' ') {
307 >                                        register struct erract  *ep;
308 >                                        for (ep = erract; ep < erract+NERRS;
309 >                                                        ep++)
310 >                                                if (ep->pre[0] &&
311 >                                                        !strncmp(cp+n, ep->pre,
312 >                                                            strlen(ep->pre))) {
313 >                                                        status = ep->ec;
314 >                                                        break;
315 >                                                }
316 >                                }
317 >                                do {            /* write message */
318                                          if ((n = write(2, cp, nr)) <= 0)
319                                                  goto writerr;
320                                          cp += n;
321                                  } while ((nr -= n) > 0);
322 +                        }
323                  }
324                                                  /* renderer stdout */
325                  if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
# Line 277 | Line 338 | io_process()           /* just act as go-between for actual pro
338                                          cp += n;
339                                  } while ((nr -= n) > 0);
340                  }
280                                                /* renderer stdin */
281                if (fdin >= 0 && (FD_ISSET(fdin, &writefds) ||
282                                FD_ISSET(fdin, &excepfds))) {
283                        nr = read(0, buf, 512); /* use minimal buffer */
284                        if (nr < 0)
285                                goto readerr;
286                        if (nr == 0) {
287                                close(0);
288                                close(fdin);
289                                fdin = -1;
290                        } else if (write(fdin, buf, nr) < nr)
291                                goto writerr;   /* what else to do? */
292                }
341          }
342 <        _exit(0);               /* we ought to return renderer error status! */
342 >        kill(pid, SIGTERM);     /* no more process to feed, so... */
343 >        waitpid(pid, 0, 0);     /* wait for feeder process */
344 >        _exit(status);
345   formerr:
346          error(USER, "format error in persist file");
347   readerr:
# Line 302 | Line 352 | writerr:
352  
353   #else
354  
355 < pfclean() {}
355 > extern void pfclean(void) {}
356  
357   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines