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.39 by schorsch, Mon Apr 21 07:31:30 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 + #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 */
35 <
35 > extern char     *progname;      /* global program name */
36 > extern char     *errfile;       /* global error file name */
37   static char     *persistfname = NULL;   /* persist file name */
38   static int      persistfd = -1;         /* persist file descriptor */
39 + static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
40  
41 < static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1];
41 > typedef void (sighandler_t)(int);
42 > static sighandler_t sig_io;
43 > static sighandler_t sig_alrm;
44  
45  
46 < pfdetach()              /* release persist (and header) resources */
46 > extern void
47 > pfdetach(void)          /* release persist (and header) resources */
48   {
49          if (persistfd >= 0)
50                  close(persistfd);
# Line 39 | Line 52 | pfdetach()             /* release persist (and header) resources
52          persistfname = NULL;
53          inpname[0] = '\0';
54          outpname[0] = '\0';
55 +        errname[0] = '\0';
56          headismine = 0;
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 53 | Line 68 | pfclean()              /* clean up persist files */
68                  unlink(inpname);
69          if (outpname[0])
70                  unlink(outpname);
71 +        if (errname[0])
72 +                unlink(errname);
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 70 | 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, 0666);
97 >        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
98          if (persistfd >= 0) {
99                  persistfname = pfn;
100                  pflock(1);
# Line 90 | Line 111 | char   *pfn;
111   }
112  
113  
114 < int sig_noop() {}
114 > static int      got_io;
115  
116 + static void sig_io(int i) { got_io++; }
117  
118 < pfhold()                /* holding pattern for idle rendering process */
118 > static void sig_alrm(int i) { quit(0); }
119 >
120 >
121 > extern void
122 > pfhold(void)            /* holding pattern for idle rendering process */
123   {
124 <        char    buf[128];
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)
133 >        if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
134                  goto createrr;
135 <        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0)
135 >        if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
136                  goto createrr;
137 <        sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname);
138 <        if (lseek(persistfd, 0L, 0) < 0)
139 <                error(SYSTEM, "seek error on persist file in pfhold");
137 >        if (errfile == NULL &&
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 in pfhold");
144 >                error(SYSTEM, "error writing persist file");
145 >        lseek(persistfd, (off_t)0, SEEK_SET);
146                                  /* wait TIMELIM for someone to signal us */
147 <        signal(SIGIO, sig_noop);
147 >        got_io = 0;
148 >        signal(SIGIO, sig_io);
149 >        oldalrm = signal(SIGALRM, sig_alrm);
150          alarm(TIMELIM);
151 <        pflock(0);
152 <        pause();
153 <        alarm(0);
151 >        pflock(0);                      /* unlock persist file for attach */
152 >        while (!got_io)
153 >                pause();                /* wait for attach */
154 >        alarm(0);                       /* turn off alarm */
155 >        signal(SIGALRM, oldalrm);
156          signal(SIGIO, SIG_DFL);
157 <        pflock(1);
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 +                close(2);
175 +                if (open(errname, O_WRONLY) != 2)
176 +                        error(INTERNAL, "unexpected stderr file number");
177 +                unlink(errname);
178 +                errname[0] = '\0';
179 +        }
180          unlink(inpname);
181          inpname[0] = '\0';
182          unlink(outpname);
# Line 136 | 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[4096], *pfin, *pfout;
198 <        int     pid;
199 <                                /* load and close persist file */
200 <        nr = read(persistfd, buf, sizeof(buf));
201 <        pfdetach();
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(3+(3*getpid()+random())%13);      /* wait until ready */
209 >                pflock(1);
210 >        }
211          if (nr < 0)
212 <                error(SYSTEM, "cannot read persist file");
213 <        buf[nr] = '\0';
214 <        if ((cp = index(buf, '\n')) == NULL)
212 >                error(SYSTEM, "error reading persist file");
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(buf)) <= 0)
221 >        if ((pid = atoi(cp)) <= 0)
222                  goto formerr;
223 <        pfin = cp;
157 <        if ((cp = index(cp, '\n')) == NULL)
223 >        if ((cp = strchr(cp, '\n')) == NULL)
224                  goto formerr;
225 +        pfin = ++cp;
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 = strchr(cp, '\n')) == NULL)
235 +                goto formerr;
236 +        *cp++ = '\0';
237          if (cp-buf != nr)
238                  goto formerr;
239 <                                /* wake up rendering process */
239 >        if (strcmp(buf, progname)) {
240 >                sprintf(errmsg, "persist file for %s, not %s", buf, progname);
241 >                error(USER, errmsg);
242 >        }
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                  error(SYSTEM, "fork failed in io_process");
250 <                                /* connect to appropriate pipe */
251 <        if (pid) {                      /* parent passes renderer output */
252 <                close(0);
253 <                if (open(pfout, O_RDONLY) != 0)
254 <                        error(SYSTEM, "cannot open input pipe in io_process");
255 <        } else {                        /* child passes renderer input */
256 <                close(1);
257 <                if (open(pfin, O_WRONLY) != 1)
258 <                        error(SYSTEM, "cannot open output pipe 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 (nr < 0)
264 >                        goto readerr;
265 >                _exit(0);
266          }
267 <                                /* pass input to output */
268 <        cp = buf; n = sizeof(buf);
269 <                                /* do as much as we can each way */
270 <        while ((nr = read(0, cp, n)) > 0) {
271 <                nr += cp-buf;
272 <                if ((n = write(1, buf, nr)) <= 0)
273 <                        goto writerr;
274 <                cp = buf;
275 <                while (n < nr)
276 <                        *cp++ = buf[n++];
277 <                n = sizeof(buf) - (cp-buf);
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 >                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 <        if (nr < 0)
343 <                error(SYSTEM, "read error in io_process");
344 <        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!) */
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 + 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