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.16 by greg, Mon Jul 8 19:46:21 1996 UTC vs.
Revision 2.40 by schorsch, Sun Apr 27 17:22:49 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 "standard.h"
10 > #include "copyright.h"
11  
12 < #ifdef F_SETLKW
14 < #include "paths.h"
12 > #include <string.h>
13   #include <signal.h>
16 #include <sys/types.h>
14   #include <sys/stat.h>
15 <                                        /* select call compatibility stuff */
16 < #ifndef FD_SETSIZE
17 < #include <sys/param.h>
18 < #define FD_SETSIZE      NOFILE          /* maximum # select file descriptors */
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
23 #ifndef FD_SET
24 #ifndef NFDBITS
25 #define NFDBITS         (8*sizeof(int)) /* number of bits per fd_mask */
26 #endif
27 #define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
28 #define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
29 #define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
30 #define FD_ZERO(p)      bzero((char *)(p), sizeof(*(p)))
31 #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"
30 +
31   #ifndef TIMELIM
32   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
33   #endif
34  
37 extern char     *strcpy(), *index();
38
35   extern int      headismine;     /* boolean true if header belongs to me */
40
36   extern char     *progname;      /* global program name */
42
37   extern char     *errfile;       /* global error file name */
44
38   static char     *persistfname = NULL;   /* persist file name */
39   static int      persistfd = -1;         /* persist file descriptor */
47
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 61 | 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 76 | 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 90 | 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 112 | 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, (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)
154                  pause();                /* wait for attach */
151        pflock(1);                      /* grab persist file right back! */
155          alarm(0);                       /* turn off alarm */
156          signal(SIGALRM, oldalrm);
157          signal(SIGIO, SIG_DFL);
158 <        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
156 <                error(SYSTEM, "seek/truncate error on persist file");
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 177 | 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;
198          char    buf[BUFSIZ], *pfin, *pfout, *pferr;
199          int     pid, nfds;
200 <        int     fdin, fdout, fderr = -1;
201 <        fd_set  readfds, writefds, excepfds;
200 >        int     fdout, fderr = -1;
201 >        int     status = 0;
202 >        fd_set  readfds, excepfds;
203                                          /* load persist file */
204 +        n = 40;
205          while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
206 +                if (!n--)
207 +                        error(USER, "unattended persist file?");
208                  pflock(0);
209 <                sleep(15);              /* wait until ready */
209 >                sleep(3+(3*getpid()+random())%13);      /* wait until ready */
210                  pflock(1);
211          }
194        pfdetach();                     /* close persist file */
212          if (nr < 0)
213                  error(SYSTEM, "error reading persist file");
214 <        buf[nr] = '\0';
215 <        if ((cp = index(buf, ' ')) == NULL)
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 = 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 223 | Line 244 | io_process()           /* just act as go-between for actual pro
244                                          /* wake up rendering process */
245          if (kill(pid, SIGIO) < 0)
246                  error(SYSTEM, "cannot signal rendering process in io_process");
247 <                                        /* open named pipes, in order */
248 <        if ((fdin = open(pfin, O_WRONLY)) < 0)
249 <                error(SYSTEM, "cannot open input pipe in io_process");
247 >                                        /* fork child feeder process */
248 >        pid = fork();
249 >        if (pid < 0)
250 >                error(SYSTEM, "fork failed in io_process");
251 >        if (pid == 0) {                 /* feeder loop */
252 >                int     fdin;
253 >                close(1);               /* open input pipe */
254 >                if ((fdin = open(pfin, O_WRONLY)) < 0)
255 >                        error(SYSTEM, "cannot open feed pipe in io_process");
256 >                                                /* renderer stdin */
257 >                while ((nr = read(0, cp=buf, sizeof(buf))) > 0) {
258 >                        do {
259 >                                if ((n = write(fdin, cp, nr)) <= 0)
260 >                                        goto writerr;
261 >                                cp += n;
262 >                        } while ((nr -= n) > 0);
263 >                }
264 >                if (nr < 0)
265 >                        goto readerr;
266 >                _exit(0);
267 >        }
268 >        close(0);
269 >                                        /* open output pipes, in order */
270          if ((fdout = open(pfout, O_RDONLY)) < 0)
271                  error(SYSTEM, "cannot open output pipe in io_process");
272          if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
273                  error(SYSTEM, "cannot open error pipe in io_process");
274 <        for ( ; ; ) {                   /* loop on select call */
274 >        for ( ; ; ) {                   /* eater loop */
275                  FD_ZERO(&readfds);
235                FD_ZERO(&writefds);
276                  FD_ZERO(&excepfds);
277                  nfds = 0;
238                if (fdin >= 0) {
239                        FD_SET(fdin, &writefds);
240                        FD_SET(fdin, &excepfds);
241                        nfds = fdin+1;
242                }
278                  if (fdout >= 0) {
279                          FD_SET(fdout, &readfds);
280                          FD_SET(fdout, &excepfds);
# Line 252 | Line 287 | io_process()           /* just act as go-between for actual pro
287                  }
288                  if (nfds == 0)
289                          break;                  /* all done, exit */
290 <                if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0)
290 >                if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0)
291                          error(SYSTEM, "error in select call in io_process");
292                                                  /* renderer stderr */
293                  if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
# Line 262 | Line 297 | io_process()           /* just act as go-between for actual pro
297                                  goto readerr;
298                          if (nr == 0) {
299                                  close(fderr);
300 <                                close(2);
300 >                                /* close(2);    don't close stderr! */
301                                  fderr = -1;
302 <                        } else
303 <                                do {            /* write it all */
302 >                        } else {
303 >                                cp[nr] = '\0';  /* deduce status if we can */
304 >                                n = strlen(progname);
305 >                                if (!strncmp(cp, progname, n) &&
306 >                                                cp[n++] == ':' &&
307 >                                                cp[n++] == ' ') {
308 >                                        register struct erract  *ep;
309 >                                        for (ep = erract; ep < erract+NERRS;
310 >                                                        ep++)
311 >                                                if (ep->pre[0] &&
312 >                                                        !strncmp(cp+n, ep->pre,
313 >                                                            strlen(ep->pre))) {
314 >                                                        status = ep->ec;
315 >                                                        break;
316 >                                                }
317 >                                }
318 >                                do {            /* write message */
319                                          if ((n = write(2, cp, nr)) <= 0)
320                                                  goto writerr;
321                                          cp += n;
322                                  } while ((nr -= n) > 0);
323 +                        }
324                  }
325                                                  /* renderer stdout */
326                  if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
# Line 288 | Line 339 | io_process()           /* just act as go-between for actual pro
339                                          cp += n;
340                                  } while ((nr -= n) > 0);
341                  }
291                                                /* renderer stdin */
292                if (fdin >= 0 && (FD_ISSET(fdin, &writefds) ||
293                                FD_ISSET(fdin, &excepfds))) {
294                        nr = read(0, buf, 512); /* use minimal buffer */
295                        if (nr < 0)
296                                goto readerr;
297                        if (nr == 0) {
298                                close(0);
299                                close(fdin);
300                                fdin = -1;
301                        } else if (write(fdin, buf, nr) < nr)
302                                goto writerr;   /* what else to do? */
303                }
342          }
343 <        _exit(0);               /* we ought to return renderer error status! */
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");
348   readerr:
# Line 313 | 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