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.15 by greg, Sat Jul 6 23:19:57 1996 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
14
27   #include "paths.h"
28 < #include <signal.h>
17 < #include <sys/types.h>
18 < #include <sys/stat.h>
28 > #include "selcall.h"
29  
30   #ifndef TIMELIM
31   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
32   #endif
33  
24 extern char     *strcpy(), *index();
25
34   extern int      headismine;     /* boolean true if header belongs to me */
27
35   extern char     *progname;      /* global program name */
29
36   extern char     *errfile;       /* global error file name */
31
37   static char     *persistfname = NULL;   /* persist file name */
38   static int      persistfd = -1;         /* persist file descriptor */
34
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 48 | 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 63 | 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 77 | 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 99 | 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(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, (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)
153                  pause();                /* wait for attach */
136        pflock(1);                      /* grab persist file right back! */
154          alarm(0);                       /* turn off alarm */
155          signal(SIGALRM, oldalrm);
156          signal(SIGIO, SIG_DFL);
157 <        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
141 <                error(SYSTEM, "seek/truncate error on persist file");
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 conduits to and from 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[512], *pfin, *pfout, *pferr;
198 <        int     pid, pid2 = -1;
197 >        char    buf[BUFSIZ], *pfin, *pfout, *pferr;
198 >        int     pid, nfds;
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          }
177        pfdetach();                     /* close persist file */
211          if (nr < 0)
212                  error(SYSTEM, "error reading persist file");
213 <        buf[nr] = '\0';
214 <        if ((cp = index(buf, ' ')) == NULL)
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 = 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 203 | Line 240 | io_process()           /* just act as conduits to and from actu
240                  sprintf(errmsg, "persist file for %s, not %s", buf, progname);
241                  error(USER, errmsg);
242          }
243 <                                /* wake up rendering process */
243 >                                        /* wake up rendering process */
244          if (kill(pid, SIGIO) < 0)
245                  error(SYSTEM, "cannot signal rendering process in io_process");
246 <        pid = fork();           /* fork i/o process */
246 >                                        /* fork child feeder process */
247 >        pid = fork();
248          if (pid < 0)
249 <                goto forkerr;
250 <                                /* connect to appropriate pipe */
251 <        if (pid) {                      /* parent passes renderer output */
252 <                close(0);
253 <                if (pferr[0]) {
254 <                        pid2 = fork();          /* fork another for stderr */
255 <                        if (pid2 < 0)
256 <                                goto forkerr;
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 (pid2) {                     /* parent is still stdout */
264 <                        if (open(pfout, O_RDONLY) != 0)
265 <                                error(SYSTEM,
266 <                                "cannot open output pipe in io_process");
267 <                } else {                        /* second child is stderr */
268 <                        if (open(pferr, O_RDONLY) != 0)
269 <                                error(SYSTEM,
270 <                                "cannot open error pipe in io_process");
271 <                        dup2(2, 1);             /* attach stdout to stderr */
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 ( ; ; ) {                   /* eater loop */
274 >                FD_ZERO(&readfds);
275 >                FD_ZERO(&excepfds);
276 >                nfds = 0;
277 >                if (fdout >= 0) {
278 >                        FD_SET(fdout, &readfds);
279 >                        FD_SET(fdout, &excepfds);
280 >                        nfds = fdout+1;
281                  }
282 <        } else {                        /* child passes renderer input */
283 <                close(1);
284 <                if (open(pfin, O_WRONLY) != 1)
285 <                        error(SYSTEM, "cannot open input pipe in io_process");
282 >                if (fderr >= 0) {
283 >                        FD_SET(fderr, &readfds);
284 >                        FD_SET(fderr, &excepfds);
285 >                        nfds = fderr+1;
286 >                }
287 >                if (nfds == 0)
288 >                        break;                  /* all done, exit */
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) ||
293 >                                FD_ISSET(fderr, &excepfds))) {
294 >                        nr = read(fderr, cp=buf, sizeof(buf));
295 >                        if (nr < 0)
296 >                                goto readerr;
297 >                        if (nr == 0) {
298 >                                close(fderr);
299 >                                /* close(2);    don't close stderr! */
300 >                                fderr = -1;
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) ||
326 >                                FD_ISSET(fdout, &excepfds))) {
327 >                        nr = read(fdout, cp=buf, sizeof(buf));
328 >                        if (nr < 0)
329 >                                goto readerr;
330 >                        if (nr == 0) {          /* EOF */
331 >                                close(fdout);
332 >                                close(1);
333 >                                fdout = -1;
334 >                        } else
335 >                                do {            /* write it all */
336 >                                        if ((n = write(1, cp, nr)) <= 0)
337 >                                                goto writerr;
338 >                                        cp += n;
339 >                                } while ((nr -= n) > 0);
340 >                }
341          }
342 <                                /* pass input to output */
343 <                                /* read as much as we can, write all of it */
344 <        while ((nr = read(0, cp=buf, sizeof(buf))) > 0)
238 <                do {
239 <                        if ((n = write(1, cp, nr)) <= 0)
240 <                                error(SYSTEM, "write error in io_process");
241 <                        cp += n;
242 <                } while ((nr -= n) > 0);
243 <        if (nr < 0)
244 <                error(SYSTEM, "read error in io_process");
245 <        close(0);               /* close input */
246 <        close(1);               /* close output */
247 <        if (pid)                /* parent waits for stdin child */
248 <                wait(0);
249 <        if (pid2 > 0)           /* wait for stderr child also */
250 <                wait(0);
251 <        _exit(0);               /* all done, exit (not quit!) */
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 < forkerr:
348 <        error(SYSTEM, "fork failed in io_process");
347 > readerr:
348 >        error(SYSTEM, "read error in io_process");
349 > writerr:
350 >        error(SYSTEM, "write error in io_process");
351   }
352  
353   #else
354  
355 < pfclean() {}
355 > extern void pfclean(void) {}
356  
357   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines