ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/src/rt/persist.c
(Generate patch)

Comparing src/rt/persist.c (file contents):
Revision 2.15 by greg, Sat Jul 6 23:19:57 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 <
15 < #include "paths.h"
12 > #include <string.h>
13   #include <signal.h>
17 #include <sys/types.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
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 */
27
35   extern char     *progname;      /* global program name */
29
36   extern char     *errfile;       /* global error file name */
31
37   static char     *persistfname = NULL;   /* persist file name */
38   static int      persistfd = -1;         /* persist file descriptor */
34
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 48 | 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 63 | 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 77 | 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 99 | 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(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 */
136        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)
141 <                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 157 | Line 178 | pfhold()               /* holding pattern for idle rendering proces
178          return;
179   createrr:
180          error(SYSTEM, "cannot create named pipes in pfhold");
160 openerr:
161        error(SYSTEM, "cannot open named pipes in pfhold");
181   }
182  
183  
184 < io_process()            /* just act as conduits to and from 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;
189 <        char    buf[512], *pfin, *pfout, *pferr;
190 <        int     pid, pid2 = -1;
187 >        char    *cp;
188 >        int     nr, n;
189 >        char    buf[BUFSIZ], *pfin, *pfout, *pferr;
190 >        int     pid, nfds;
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          }
177        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 203 | Line 230 | io_process()           /* just act as conduits to and from actu
230                  sprintf(errmsg, "persist file for %s, not %s", buf, progname);
231                  error(USER, errmsg);
232          }
233 <                                /* wake up rendering process */
233 >                                        /* wake up rendering process */
234          if (kill(pid, SIGIO) < 0)
235                  error(SYSTEM, "cannot signal rendering process in io_process");
236 <        pid = fork();           /* fork i/o process */
236 >                                        /* fork child feeder process */
237 >        pid = fork();
238          if (pid < 0)
239 <                goto forkerr;
240 <                                /* connect to appropriate pipe */
241 <        if (pid) {                      /* parent passes renderer output */
242 <                close(0);
243 <                if (pferr[0]) {
244 <                        pid2 = fork();          /* fork another for stderr */
245 <                        if (pid2 < 0)
246 <                                goto forkerr;
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 (pid2) {                     /* parent is still stdout */
254 <                        if (open(pfout, O_RDONLY) != 0)
255 <                                error(SYSTEM,
256 <                                "cannot open output pipe in io_process");
257 <                } else {                        /* second child is stderr */
258 <                        if (open(pferr, O_RDONLY) != 0)
259 <                                error(SYSTEM,
260 <                                "cannot open error pipe in io_process");
261 <                        dup2(2, 1);             /* attach stdout to stderr */
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 ( ; ; ) {                   /* eater loop */
264 >                FD_ZERO(&readfds);
265 >                FD_ZERO(&excepfds);
266 >                nfds = 0;
267 >                if (fdout >= 0) {
268 >                        FD_SET(fdout, &readfds);
269 >                        FD_SET(fdout, &excepfds);
270 >                        nfds = fdout+1;
271                  }
272 <        } else {                        /* child passes renderer input */
273 <                close(1);
274 <                if (open(pfin, O_WRONLY) != 1)
275 <                        error(SYSTEM, "cannot open input pipe in io_process");
272 >                if (fderr >= 0) {
273 >                        FD_SET(fderr, &readfds);
274 >                        FD_SET(fderr, &excepfds);
275 >                        nfds = fderr+1;
276 >                }
277 >                if (nfds == 0)
278 >                        break;                  /* all done, exit */
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) ||
283 >                                FD_ISSET(fderr, &excepfds))) {
284 >                        nr = read(fderr, cp=buf, sizeof(buf));
285 >                        if (nr < 0)
286 >                                goto readerr;
287 >                        if (nr == 0) {
288 >                                close(fderr);
289 >                                /* close(2);    don't close stderr! */
290 >                                fderr = -1;
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) ||
316 >                                FD_ISSET(fdout, &excepfds))) {
317 >                        nr = read(fdout, cp=buf, sizeof(buf));
318 >                        if (nr < 0)
319 >                                goto readerr;
320 >                        if (nr == 0) {          /* EOF */
321 >                                close(fdout);
322 >                                close(1);
323 >                                fdout = -1;
324 >                        } else
325 >                                do {            /* write it all */
326 >                                        if ((n = write(1, cp, nr)) <= 0)
327 >                                                goto writerr;
328 >                                        cp += n;
329 >                                } while ((nr -= n) > 0);
330 >                }
331          }
332 <                                /* pass input to output */
333 <                                /* read as much as we can, write all of it */
334 <        while ((nr = read(0, cp=buf, sizeof(buf))) > 0)
238 <                do {
239 <                        if ((n = write(1, cp, nr)) <= 0)
240 <                                error(SYSTEM, "write error in io_process");
241 <                        cp += n;
242 <                } while ((nr -= n) > 0);
243 <        if (nr < 0)
244 <                error(SYSTEM, "read error in io_process");
245 <        close(0);               /* close input */
246 <        close(1);               /* close output */
247 <        if (pid)                /* parent waits for stdin child */
248 <                wait(0);
249 <        if (pid2 > 0)           /* wait for stderr child also */
250 <                wait(0);
251 <        _exit(0);               /* all done, exit (not quit!) */
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 < forkerr:
338 <        error(SYSTEM, "fork failed in io_process");
337 > readerr:
338 >        error(SYSTEM, "read error in io_process");
339 > writerr:
340 >        error(SYSTEM, "write error in io_process");
341   }
342  
343   #else
344  
345 < pfclean() {}
345 > void pfclean(void) {}
346  
347   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)