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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines