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.16 by greg, Mon Jul 8 19:46:21 1996 UTC vs.
Revision 2.45 by greg, Sat Jun 7 05:09:46 2025 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 "standard.h"
10 > #include "copyright.h"
11  
12 < #ifdef F_SETLKW
14 < #include "paths.h"
12 > #include <string.h>
13   #include <signal.h>
16 #include <sys/types.h>
14   #include <sys/stat.h>
15 <                                        /* select call compatibility stuff */
16 < #ifndef FD_SETSIZE
17 < #include <sys/param.h>
18 < #define FD_SETSIZE      NOFILE          /* maximum # select file descriptors */
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
23 #ifndef FD_SET
24 #ifndef NFDBITS
25 #define NFDBITS         (8*sizeof(int)) /* number of bits per fd_mask */
26 #endif
27 #define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
28 #define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
29 #define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
30 #define FD_ZERO(p)      bzero((char *)(p), sizeof(*(p)))
31 #endif
21  
22 + #include "rtprocess.h" /* getpid() */
23 + #include "standard.h"
24 + #include "random.h"
25 + #include "ray.h"
26 +
27 + #ifdef F_SETLKW
28 + #include "selcall.h"
29 +
30   #ifndef TIMELIM
31   #define TIMELIM         (8*3600)        /* time limit for holding pattern */
32   #endif
33  
37 extern char     *strcpy(), *index();
38
34   extern int      headismine;     /* boolean true if header belongs to me */
40
35   extern char     *progname;      /* global program name */
42
36   extern char     *errfile;       /* global error file name */
44
37   static char     *persistfname = NULL;   /* persist file name */
38   static int      persistfd = -1;         /* persist file descriptor */
47
39   static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
40  
41 + typedef void (rsighandler_t)(int);
42 + static rsighandler_t sig_io;
43 + static rsighandler_t sig_alrm;
44  
45 < pfdetach()              /* release persist (and header) resources */
45 >
46 > void
47 > pfdetach(void)          /* release persist (and header) resources */
48   {
49          if (persistfd >= 0)
50                  close(persistfd);
# Line 61 | Line 57 | pfdetach()             /* release persist (and header) resources
57   }
58  
59  
60 < pfclean()               /* clean up persist files */
60 > void
61 > pfclean(void)           /* clean up persist files */
62   {
63          if (persistfd >= 0)
64                  close(persistfd);
# Line 76 | Line 73 | pfclean()              /* clean up persist files */
73   }
74  
75  
76 < pflock(lf)              /* place or release exclusive lock on file */
77 < int     lf;
76 > void
77 > pflock(         /* place or release exclusive lock on file */
78 >        int     lf
79 > )
80   {
81          struct flock    fls;
82  
# Line 90 | Line 89 | int    lf;
89   }
90  
91  
92 < persistfile(pfn)        /* open persist file and lock it */
93 < char    *pfn;
92 > void
93 > persistfile(    /* open persist file and lock it */
94 >        char    *pfn
95 > )
96   {
97          persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
98          if (persistfd >= 0) {
# Line 112 | Line 113 | char   *pfn;
113  
114   static int      got_io;
115  
116 < static int sig_io() { got_io++; }
116 > static void sig_io(int i) { got_io++; }
117  
118 < static int sig_alrm() { quit(0); }
118 > static void sig_alrm(int i) { quit(0); }
119  
120  
121 < pfhold()                /* holding pattern for idle rendering process */
121 > void
122 > pfhold(void)            /* holding pattern for idle rendering process */
123   {
124 <        int     (*oldalrm)();
124 >        rsighandler_t   *oldalrm;
125          char    buf[512];
126 <        register int    n;
126 >        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(fileno(stderr));
131 >                close(2);
132                                  /* create named pipes for input and output */
133 <        if (mknod(mktemp(strcpy(inpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
133 >        if (mkfifo(mktemp(strcpy(inpname,TEMPLATE)), 0600) < 0)
134                  goto createrr;
135 <        if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
135 >        if (mkfifo(mktemp(strcpy(outpname,TEMPLATE)), 0600) < 0)
136                  goto createrr;
137          if (errfile == NULL &&
138 <                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
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");
145 +        lseek(persistfd, (off_t)0, SEEK_SET);
146                                  /* wait TIMELIM for someone to signal us */
147          got_io = 0;
148          signal(SIGIO, sig_io);
149 <        oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
149 >        oldalrm = signal(SIGALRM, sig_alrm);
150          alarm(TIMELIM);
151          pflock(0);                      /* unlock persist file for attach */
152          while (!got_io)
153                  pause();                /* wait for attach */
151        pflock(1);                      /* grab persist file right back! */
154          alarm(0);                       /* turn off alarm */
155          signal(SIGALRM, oldalrm);
156          signal(SIGIO, SIG_DFL);
157 <        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
156 <                error(SYSTEM, "seek/truncate error on persist file");
157 >        pflock(1);                      /* grab persist file back */
158                                  /* someone wants us; reopen stdin and stdout */
159 <        if (freopen(inpname, "r", stdin) == NULL)
160 <                goto openerr;
161 <        if (freopen(outpname, "w", stdout) == NULL)
162 <                goto openerr;
159 >        close(0);
160 >        if (open(inpname, O_RDONLY) != 0)
161 >                error(INTERNAL, "unexpected stdin file number");
162 >        clearerr(stdin);
163 >        close(1);
164 >        if (open(outpname, O_WRONLY) != 1)
165 >                error(INTERNAL, "unexpected stdout file number");
166 >        sleep(3);               /* give them a chance to open their pipes */
167          if (errname[0]) {
168 <                if (freopen(errname, "w", stderr) == NULL)
169 <                        goto openerr;
168 >                close(2);
169 >                if (open(errname, O_WRONLY) != 2)
170 >                        error(INTERNAL, "unexpected stderr file number");
171                  unlink(errname);
172                  errname[0] = '\0';
173          }
# Line 172 | Line 178 | pfhold()               /* holding pattern for idle rendering proces
178          return;
179   createrr:
180          error(SYSTEM, "cannot create named pipes in pfhold");
175 openerr:
176        error(SYSTEM, "cannot open named pipes in pfhold");
181   }
182  
183  
184 < io_process()            /* just act as go-between for actual process */
184 > void
185 > io_process(void)                /* just act as go-between for actual process */
186   {
187 <        register char   *cp;
188 <        register int    nr, n;
187 >        char    *cp;
188 >        int     nr, n;
189          char    buf[BUFSIZ], *pfin, *pfout, *pferr;
190          int     pid, nfds;
191 <        int     fdin, fdout, fderr = -1;
192 <        fd_set  readfds, writefds, excepfds;
191 >        int     fdout, fderr = -1;
192 >        int     status = 0;
193 >        fd_set  readfds, excepfds;
194                                          /* load persist file */
195 +        n = 40;
196          while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
197 +                if (!n--)
198 +                        error(USER, "unattended persist file?");
199                  pflock(0);
200 <                sleep(15);              /* wait until ready */
200 >                sleep(3+(3*getpid()+random())%13);      /* wait until ready */
201                  pflock(1);
202          }
194        pfdetach();                     /* close persist file */
203          if (nr < 0)
204                  error(SYSTEM, "error reading persist file");
205 <        buf[nr] = '\0';
206 <        if ((cp = index(buf, ' ')) == NULL)
205 >        ftruncate(persistfd, (off_t)0L);        /* truncate persist file */
206 >        pfdetach();                     /* close & release persist file */
207 >        buf[nr] = '\0';                 /* parse what we got */
208 >        if ((cp = strchr(buf, ' ')) == NULL)
209                  goto formerr;
210          *cp++ = '\0';
211          if ((pid = atoi(cp)) <= 0)
212                  goto formerr;
213 <        if ((cp = index(cp, '\n')) == NULL)
213 >        if ((cp = strchr(cp, '\n')) == NULL)
214                  goto formerr;
215          pfin = ++cp;
216 <        if ((cp = index(cp, '\n')) == NULL)
216 >        if ((cp = strchr(cp, '\n')) == NULL)
217                  goto formerr;
218          *cp++ = '\0';
219          pfout = cp;
220 <        if ((cp = index(cp, '\n')) == NULL)
220 >        if ((cp = strchr(cp, '\n')) == NULL)
221                  goto formerr;
222          *cp++ = '\0';
223          pferr = cp;
224 <        if ((cp = index(cp, '\n')) == NULL)
224 >        if ((cp = strchr(cp, '\n')) == NULL)
225                  goto formerr;
226          *cp++ = '\0';
227          if (cp-buf != nr)
# Line 223 | Line 233 | io_process()           /* just act as go-between for actual pro
233                                          /* wake up rendering process */
234          if (kill(pid, SIGIO) < 0)
235                  error(SYSTEM, "cannot signal rendering process in io_process");
236 <                                        /* open named pipes, in order */
237 <        if ((fdin = open(pfin, O_WRONLY)) < 0)
238 <                error(SYSTEM, "cannot open input pipe in io_process");
236 >                                        /* fork child feeder process */
237 >        pid = fork();
238 >        if (pid < 0)
239 >                error(SYSTEM, "fork failed in io_process");
240 >        if (pid == 0) {                 /* feeder loop */
241 >                int     fdin;
242 >                close(1);               /* open input pipe */
243 >                if ((fdin = open(pfin, O_WRONLY)) < 0)
244 >                        error(SYSTEM, "cannot open feed pipe in io_process");
245 >                                                /* renderer stdin */
246 >                while ((nr = read(0, cp=buf, sizeof(buf))) > 0) {
247 >                        do {
248 >                                if ((n = write(fdin, cp, nr)) <= 0)
249 >                                        goto writerr;
250 >                                cp += n;
251 >                        } while ((nr -= n) > 0);
252 >                }
253 >                if (nr < 0)
254 >                        goto readerr;
255 >                _exit(0);
256 >        }
257 >        close(0);
258 >                                        /* open output pipes, in order */
259          if ((fdout = open(pfout, O_RDONLY)) < 0)
260                  error(SYSTEM, "cannot open output pipe in io_process");
261          if (pferr[0] && (fderr = open(pferr, O_RDONLY)) < 0)
262                  error(SYSTEM, "cannot open error pipe in io_process");
263 <        for ( ; ; ) {                   /* loop on select call */
263 >        for ( ; ; ) {                   /* eater loop */
264                  FD_ZERO(&readfds);
235                FD_ZERO(&writefds);
265                  FD_ZERO(&excepfds);
266                  nfds = 0;
238                if (fdin >= 0) {
239                        FD_SET(fdin, &writefds);
240                        FD_SET(fdin, &excepfds);
241                        nfds = fdin+1;
242                }
267                  if (fdout >= 0) {
268                          FD_SET(fdout, &readfds);
269                          FD_SET(fdout, &excepfds);
# Line 252 | Line 276 | io_process()           /* just act as go-between for actual pro
276                  }
277                  if (nfds == 0)
278                          break;                  /* all done, exit */
279 <                if (select(nfds, &readfds, &writefds, &excepfds, NULL) < 0)
279 >                if (select(nfds, &readfds, NULL, &excepfds, NULL) < 0)
280                          error(SYSTEM, "error in select call in io_process");
281                                                  /* renderer stderr */
282                  if (fderr >= 0 && (FD_ISSET(fderr, &readfds) ||
# Line 262 | Line 286 | io_process()           /* just act as go-between for actual pro
286                                  goto readerr;
287                          if (nr == 0) {
288                                  close(fderr);
289 <                                close(2);
289 >                                /* close(2);    don't close stderr! */
290                                  fderr = -1;
291 <                        } else
292 <                                do {            /* write it all */
291 >                        } else {
292 >                                cp[nr] = '\0';  /* deduce status if we can */
293 >                                n = strlen(progname);
294 >                                if (!strncmp(cp, progname, n) &&
295 >                                                cp[n++] == ':' &&
296 >                                                cp[n++] == ' ') {
297 >                                        struct erract   *ep;
298 >                                        for (ep = erract; ep < erract+NERRS;
299 >                                                        ep++)
300 >                                                if (ep->pre[0] &&
301 >                                                        !strncmp(cp+n, ep->pre,
302 >                                                            strlen(ep->pre))) {
303 >                                                        status = ep->ec;
304 >                                                        break;
305 >                                                }
306 >                                }
307 >                                do {            /* write message */
308                                          if ((n = write(2, cp, nr)) <= 0)
309                                                  goto writerr;
310                                          cp += n;
311                                  } while ((nr -= n) > 0);
312 +                        }
313                  }
314                                                  /* renderer stdout */
315                  if (fdout >= 0 && (FD_ISSET(fdout, &readfds) ||
# Line 288 | Line 328 | io_process()           /* just act as go-between for actual pro
328                                          cp += n;
329                                  } while ((nr -= n) > 0);
330                  }
291                                                /* renderer stdin */
292                if (fdin >= 0 && (FD_ISSET(fdin, &writefds) ||
293                                FD_ISSET(fdin, &excepfds))) {
294                        nr = read(0, buf, 512); /* use minimal buffer */
295                        if (nr < 0)
296                                goto readerr;
297                        if (nr == 0) {
298                                close(0);
299                                close(fdin);
300                                fdin = -1;
301                        } else if (write(fdin, buf, nr) < nr)
302                                goto writerr;   /* what else to do? */
303                }
331          }
332 <        _exit(0);               /* we ought to return renderer error status! */
332 >        kill(pid, SIGTERM);     /* no more process to feed, so... */
333 >        waitpid(pid, 0, 0);     /* wait for feeder process */
334 >        _exit(status);
335   formerr:
336          error(USER, "format error in persist file");
337   readerr:
# Line 313 | Line 342 | writerr:
342  
343   #else
344  
345 < pfclean() {}
345 > void pfclean(void) {}
346  
347   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines