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.12 by greg, Tue Jan 23 15:51:38 1996 UTC vs.
Revision 2.34 by schorsch, Tue Mar 30 16:13:01 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines