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.8 by greg, Wed Mar 3 09:07:52 1993 UTC vs.
Revision 2.40 by schorsch, Sun Apr 27 17:22:49 2008 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 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 +
17 + #include "platform.h"
18 + #ifndef NON_POSIX /* XXX need abstraction for process management */
19 + #include <sys/wait.h>
20 + #endif
21 +
22 + #include "rtprocess.h" /* getpid() */
23   #include "standard.h"
24 + #include "random.h"
25 + #include "ray.h"
26  
27   #ifdef F_SETLKW
14
28   #include "paths.h"
29 < #include <signal.h>
17 < #include <sys/types.h>
18 < #include <sys/stat.h>
29 > #include "selcall.h"
30  
31   #ifndef TIMELIM
32   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
33   #endif
34  
24 extern char     *strcpy(), *index();
25
35   extern int      headismine;     /* boolean true if header belongs to me */
36 <
36 > extern char     *progname;      /* global program name */
37 > extern char     *errfile;       /* global error file name */
38   static char     *persistfname = NULL;   /* persist file name */
39   static int      persistfd = -1;         /* persist file descriptor */
40 + static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
41  
42 < static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1];
42 > typedef void (sighandler_t)(int);
43 > static sighandler_t sig_io;
44 > static sighandler_t sig_alrm;
45  
46  
47 < pfdetach()              /* release persist (and header) resources */
47 > extern void
48 > pfdetach(void)          /* release persist (and header) resources */
49   {
50          if (persistfd >= 0)
51                  close(persistfd);
# Line 39 | Line 53 | pfdetach()             /* release persist (and header) resources
53          persistfname = NULL;
54          inpname[0] = '\0';
55          outpname[0] = '\0';
56 +        errname[0] = '\0';
57          headismine = 0;
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 53 | Line 69 | pfclean()              /* clean up persist files */
69                  unlink(inpname);
70          if (outpname[0])
71                  unlink(outpname);
72 +        if (errname[0])
73 +                unlink(errname);
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 70 | 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, 0666);
98 >        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
99          if (persistfd >= 0) {
100                  persistfname = pfn;
101                  pflock(1);
# Line 90 | Line 112 | char   *pfn;
112   }
113  
114  
115 < int sig_noop() {}
115 > static int      got_io;
116  
117 + static void sig_io(int i) { got_io++; }
118  
119 < pfhold()                /* holding pattern for idle rendering process */
119 > static void sig_alrm(int i) { quit(0); }
120 >
121 >
122 > extern void
123 > pfhold(void)            /* holding pattern for idle rendering process */
124   {
125 <        char    buf[128];
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(2);
133                                  /* create named pipes for input and output */
134 <        if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600) < 0)
134 >        if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
135                  goto createrr;
136 <        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0)
136 >        if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
137                  goto createrr;
138 <        sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname);
139 <        if (lseek(persistfd, 0L, 0) < 0)
140 <                error(SYSTEM, "seek error on persist file in pfhold");
138 >        if (errfile == NULL &&
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 in pfhold");
145 >                error(SYSTEM, "error writing persist file");
146 >        lseek(persistfd, (off_t)0, SEEK_SET);
147                                  /* wait TIMELIM for someone to signal us */
148 <        signal(SIGIO, sig_noop);
148 >        got_io = 0;
149 >        signal(SIGIO, sig_io);
150 >        oldalrm = signal(SIGALRM, sig_alrm);
151          alarm(TIMELIM);
152 <        pflock(0);
153 <        pause();
154 <        alarm(0);
152 >        pflock(0);                      /* unlock persist file for attach */
153 >        while (!got_io)
154 >                pause();                /* wait for attach */
155 >        alarm(0);                       /* turn off alarm */
156 >        signal(SIGALRM, oldalrm);
157          signal(SIGIO, SIG_DFL);
158 <        pflock(1);
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 +                close(2);
176 +                if (open(errname, O_WRONLY) != 2)
177 +                        error(INTERNAL, "unexpected stderr file number");
178 +                unlink(errname);
179 +                errname[0] = '\0';
180 +        }
181          unlink(inpname);
182          inpname[0] = '\0';
183          unlink(outpname);
# Line 136 | Line 190 | openerr:
190   }
191  
192  
193 < io_process()            /* just act as conduits to and from 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[4096], *pfin, *pfout;
199 <        int     pid;
200 <                                /* load and close persist file */
201 <        nr = read(persistfd, buf, sizeof(buf));
202 <        pfdetach();
198 >        char    buf[BUFSIZ], *pfin, *pfout, *pferr;
199 >        int     pid, nfds;
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(3+(3*getpid()+random())%13);      /* wait until ready */
210 >                pflock(1);
211 >        }
212          if (nr < 0)
213 <                error(SYSTEM, "cannot read persist file");
214 <        buf[nr] = '\0';
215 <        if ((cp = index(buf, '\n')) == NULL)
213 >                error(SYSTEM, "error reading persist file");
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(buf)) <= 0)
222 >        if ((pid = atoi(cp)) <= 0)
223                  goto formerr;
224 <        pfin = cp;
157 <        if ((cp = index(cp, '\n')) == NULL)
224 >        if ((cp = strchr(cp, '\n')) == NULL)
225                  goto formerr;
226 +        pfin = ++cp;
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 = strchr(cp, '\n')) == NULL)
236 +                goto formerr;
237 +        *cp++ = '\0';
238          if (cp-buf != nr)
239                  goto formerr;
240 <                                /* wake up rendering process */
240 >        if (strcmp(buf, progname)) {
241 >                sprintf(errmsg, "persist file for %s, not %s", buf, progname);
242 >                error(USER, errmsg);
243 >        }
244 >                                        /* wake up rendering process */
245          if (kill(pid, SIGIO) < 0)
246                  error(SYSTEM, "cannot signal rendering process in io_process");
247 <        pid = fork();           /* fork i/o process */
247 >                                        /* fork child feeder process */
248 >        pid = fork();
249          if (pid < 0)
250                  error(SYSTEM, "fork failed in io_process");
251 <                                /* connect to appropriate pipe */
252 <        if (pid) {                      /* parent passes renderer output */
253 <                close(0);
254 <                if (open(pfout, O_RDONLY) != 0)
255 <                        error(SYSTEM, "cannot open input pipe in io_process");
256 <        } else {                        /* child passes renderer input */
257 <                close(1);
258 <                if (open(pfin, O_WRONLY) != 1)
259 <                        error(SYSTEM, "cannot open output pipe 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 <                                /* pass input to output */
269 <        cp = buf; n = sizeof(buf);
270 <                                /* do as much as we can each way */
271 <        while ((nr = read(0, cp, n)) > 0) {
272 <                nr += cp-buf;
273 <                if ((n = write(1, buf, nr)) <= 0)
274 <                        goto writerr;
275 <                cp = buf;
276 <                while (n < nr)
277 <                        *cp++ = buf[n++];
278 <                n = sizeof(buf) - (cp-buf);
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 ( ; ; ) {                   /* eater loop */
275 >                FD_ZERO(&readfds);
276 >                FD_ZERO(&excepfds);
277 >                nfds = 0;
278 >                if (fdout >= 0) {
279 >                        FD_SET(fdout, &readfds);
280 >                        FD_SET(fdout, &excepfds);
281 >                        nfds = fdout+1;
282 >                }
283 >                if (fderr >= 0) {
284 >                        FD_SET(fderr, &readfds);
285 >                        FD_SET(fderr, &excepfds);
286 >                        nfds = fderr+1;
287 >                }
288 >                if (nfds == 0)
289 >                        break;                  /* all done, exit */
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) ||
294 >                                FD_ISSET(fderr, &excepfds))) {
295 >                        nr = read(fderr, cp=buf, sizeof(buf));
296 >                        if (nr < 0)
297 >                                goto readerr;
298 >                        if (nr == 0) {
299 >                                close(fderr);
300 >                                /* close(2);    don't close stderr! */
301 >                                fderr = -1;
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) ||
327 >                                FD_ISSET(fdout, &excepfds))) {
328 >                        nr = read(fdout, cp=buf, sizeof(buf));
329 >                        if (nr < 0)
330 >                                goto readerr;
331 >                        if (nr == 0) {          /* EOF */
332 >                                close(fdout);
333 >                                close(1);
334 >                                fdout = -1;
335 >                        } else
336 >                                do {            /* write it all */
337 >                                        if ((n = write(1, cp, nr)) <= 0)
338 >                                                goto writerr;
339 >                                        cp += n;
340 >                                } while ((nr -= n) > 0);
341 >                }
342          }
343 <        if (nr < 0)
344 <                error(SYSTEM, "read error in io_process");
345 <        close(0);               /* close input */
197 <        nr = cp-buf;            /* write remainder */
198 <        cp = buf;
199 <        while (nr > 0) {
200 <                if ((n = write(1, cp, nr)) <= 0)
201 <                        goto writerr;
202 <                cp += n;
203 <                nr -= n;
204 <        }
205 <        close(1);               /* close output */
206 <        if (pid)                /* parent waits for child */
207 <                wait(0);
208 <        exit(0);                /* all done, exit (not quit!) */
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:
349 +        error(SYSTEM, "read error in io_process");
350   writerr:
351          error(SYSTEM, "write error in io_process");
352   }
353  
354   #else
355  
356 < pfclean() {}
356 > extern void pfclean(void) {}
357  
358   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines