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.3 by greg, Thu Jan 21 17:01:24 1993 UTC vs.
Revision 2.29 by schorsch, Mon Jun 30 14:59:12 2003 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 "standard.h"
11 < #include "paths.h"
10 > #include "copyright.h"
11 >
12 > #include <string.h>
13   #include <signal.h>
14 #include <sys/types.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
23 + #include "paths.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 */
39 +
40 + extern char     *errfile;       /* global error file name */
41 +
42   static char     *persistfname = NULL;   /* persist file name */
43   static int      persistfd = -1;         /* persist file descriptor */
44  
45 < static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1];
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 36 | Line 54 | pfdetach()             /* release persist (and header) resources
54          persistfname = NULL;
55          inpname[0] = '\0';
56          outpname[0] = '\0';
57 +        errname[0] = '\0';
58          headismine = 0;
59   }
60  
61  
62 + void
63   pfclean()               /* clean up persist files */
64   {
65          if (persistfd >= 0)
# Line 50 | Line 70 | pfclean()              /* clean up persist files */
70                  unlink(inpname);
71          if (outpname[0])
72                  unlink(outpname);
73 +        if (errname[0])
74 +                unlink(errname);
75   }
76  
77  
78 + void
79   pflock(lf)              /* place or release exclusive lock on file */
80   int     lf;
81   {
# Line 70 | Line 93 | int    lf;
93   persistfile(pfn)        /* open persist file and lock it */
94   char    *pfn;
95   {
96 <        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666);
96 >        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
97          if (persistfd >= 0) {
98                  persistfname = pfn;
99                  pflock(1);
# Line 87 | Line 110 | char   *pfn;
110   }
111  
112  
113 < int sig_noop() {}
113 > static int      got_io;
114  
115 + static void sig_io() { got_io++; }
116  
117 + static void sig_alrm() { quit(0); }
118 +
119 +
120 + void
121   pfhold()                /* holding pattern for idle rendering process */
122   {
123 <        char    buf[128];
123 >        void    (*oldalrm)();
124 >        char    buf[512];
125          register int    n;
126                                  /* close input and output descriptors */
127          close(fileno(stdin));
99        fflush(stdout);
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)
132 >        if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
133                  goto createrr;
134 <        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600) < 0)
134 >        if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
135                  goto createrr;
136 <        sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname);
137 <        if (lseek(persistfd, 0L, 0) < 0)
138 <                error(SYSTEM, "seek error on persist file in pfhold");
136 >        if (errfile == NULL &&
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 in pfhold");
143 >                error(SYSTEM, "error writing persist file");
144 >        lseek(persistfd, (off_t)0L, 0);
145                                  /* wait TIMELIM for someone to signal us */
146 <        signal(SIGIO, sig_noop);
146 >        got_io = 0;
147 >        signal(SIGIO, sig_io);
148 >        oldalrm = (void (*)())signal(SIGALRM, sig_alrm);
149          alarm(TIMELIM);
150 <        pflock(0);
151 <        pause();
152 <        alarm(0);
150 >        pflock(0);                      /* unlock persist file for attach */
151 >        while (!got_io)
152 >                pause();                /* wait for attach */
153 >        alarm(0);                       /* turn off alarm */
154 >        signal(SIGALRM, oldalrm);
155          signal(SIGIO, SIG_DFL);
156 <        pflock(1);
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 +                close(2);
174 +                if (open(errname, O_WRONLY) != 2)
175 +                        error(INTERNAL, "unexpected stderr file number");
176 +                unlink(errname);
177 +                errname[0] = '\0';
178 +        }
179          unlink(inpname);
180          inpname[0] = '\0';
181          unlink(outpname);
182          outpname[0] = '\0';
129        clean_slate();          /* reset time and ray counters */
183          return;
184   createrr:
185          error(SYSTEM, "cannot create named pipes in pfhold");
# Line 135 | 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[4096], *pfin, *pfout;
197 <        int     pid;
198 <                                /* load and close persist file */
199 <        nr = read(persistfd, buf, sizeof(buf));
200 <        pfdetach();
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(3+(3*getpid()+random())%13);      /* wait until ready */
208 >                pflock(1);
209 >        }
210          if (nr < 0)
211 <                error(SYSTEM, "cannot read persist file");
212 <        buf[nr] = '\0';
213 <        if ((cp = index(buf, '\n')) == NULL)
211 >                error(SYSTEM, "error reading persist file");
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(buf)) <= 0)
220 >        if ((pid = atoi(cp)) <= 0)
221                  goto formerr;
222 <        pfin = cp;
156 <        if ((cp = index(cp, '\n')) == NULL)
222 >        if ((cp = strchr(cp, '\n')) == NULL)
223                  goto formerr;
224 +        pfin = ++cp;
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 = strchr(cp, '\n')) == NULL)
234 +                goto formerr;
235 +        *cp++ = '\0';
236          if (cp-buf != nr)
237                  goto formerr;
238 <                                /* wake up rendering process */
238 >        if (strcmp(buf, progname)) {
239 >                sprintf(errmsg, "persist file for %s, not %s", buf, progname);
240 >                error(USER, errmsg);
241 >        }
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                  error(SYSTEM, "fork failed in io_process");
249 <                                /* connect to appropriate pipe */
250 <        if (pid) {                      /* parent passes renderer output */
251 <                if (freopen(pfout, "r", stdin) == NULL)
252 <                        error(SYSTEM, "cannot open input pipe in io_process");
253 <        } else {                        /* child passes renderer input */
254 <                if (freopen(pfin, "w", stdout) == NULL)
255 <                        error(SYSTEM, "cannot open input pipe 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 (nr < 0)
263 >                        goto readerr;
264 >                _exit(0);
265          }
266 <                                /* pass input to output */
267 <        cp = buf; n = sizeof(buf);
268 <        while ((nr = read(fileno(stdin), cp, n)) > 0) {
269 <                nr += cp-buf;
270 <                if ((n = write(fileno(stdout), buf, nr)) <= 0)
271 <                        error(SYSTEM, "write error in io_process");
272 <                cp = buf;
273 <                while (n < nr)
274 <                        *cp++ = buf[n++];
275 <                n = sizeof(buf) - (cp-buf);
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 >                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 <        fclose(stdin);
342 <        fclose(stdout);
192 <        if (pid)                /* parent waits for child */
193 <                wait(0);
194 <        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 + 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 + void pfclean() {}
354 +
355 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines