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.13 by greg, Thu Jul 4 09:54:59 1996 UTC vs.
Revision 2.15 by greg, Sat Jul 6 23:19:57 1996 UTC

# Line 27 | Line 27 | extern int     headismine;     /* boolean true if header belon
27  
28   extern char     *progname;      /* global program name */
29  
30 + extern char     *errfile;       /* global error file name */
31 +
32   static char     *persistfname = NULL;   /* persist file name */
33   static int      persistfd = -1;         /* persist file descriptor */
34  
35 < static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1];
35 > static char     inpname[TEMPLEN+1], outpname[TEMPLEN+1], errname[TEMPLEN+1];
36  
37  
38   pfdetach()              /* release persist (and header) resources */
# Line 41 | Line 43 | pfdetach()             /* release persist (and header) resources
43          persistfname = NULL;
44          inpname[0] = '\0';
45          outpname[0] = '\0';
46 +        errname[0] = '\0';
47          headismine = 0;
48   }
49  
# Line 55 | Line 58 | pfclean()              /* clean up persist files */
58                  unlink(inpname);
59          if (outpname[0])
60                  unlink(outpname);
61 +        if (errname[0])
62 +                unlink(errname);
63   }
64  
65  
# Line 112 | Line 117 | pfhold()               /* holding pattern for idle rendering proces
117                  goto createrr;
118          if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
119                  goto createrr;
120 <        sprintf(buf, "%s %d\n%s\n%s\n", progname, getpid(), inpname, outpname);
121 <        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
122 <                error(SYSTEM, "seek/truncate error on persist file");
120 >        if (errfile == NULL &&
121 >                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
122 >                goto createrr;
123 >        sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
124 >                        inpname, outpname, errname);
125          n = strlen(buf);
126          if (write(persistfd, buf, n) < n)
127                  error(SYSTEM, "error writing persist file");
# Line 123 | Line 130 | pfhold()               /* holding pattern for idle rendering proces
130          signal(SIGIO, sig_io);
131          oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
132          alarm(TIMELIM);
133 <        pflock(0);
133 >        pflock(0);                      /* unlock persist file for attach */
134          while (!got_io)
135 <                pause();
136 <        alarm(0);
135 >                pause();                /* wait for attach */
136 >        pflock(1);                      /* grab persist file right back! */
137 >        alarm(0);                       /* turn off alarm */
138          signal(SIGALRM, oldalrm);
139          signal(SIGIO, SIG_DFL);
140 <        pflock(1);
140 >        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
141 >                error(SYSTEM, "seek/truncate error on persist file");
142                                  /* someone wants us; reopen stdin and stdout */
143          if (freopen(inpname, "r", stdin) == NULL)
144                  goto openerr;
145          if (freopen(outpname, "w", stdout) == NULL)
146                  goto openerr;
147 +        if (errname[0]) {
148 +                if (freopen(errname, "w", stderr) == NULL)
149 +                        goto openerr;
150 +                unlink(errname);
151 +                errname[0] = '\0';
152 +        }
153          unlink(inpname);
154          inpname[0] = '\0';
155          unlink(outpname);
# Line 151 | Line 166 | io_process()           /* just act as conduits to and from actu
166   {
167          register char   *cp;
168          register int    nr, n;
169 <        char    buf[512], *pfin, *pfout;
170 <        int     pid;
171 <                                /* load and close persist file */
172 <        sleep(5);               /* helps synchronization */
173 <        nr = read(persistfd, buf, sizeof(buf)-1);
174 <        pfdetach();
175 <        if (nr <= 0)
176 <                error(SYSTEM, "cannot read persist file");
169 >        char    buf[512], *pfin, *pfout, *pferr;
170 >        int     pid, pid2 = -1;
171 >                                        /* load persist file */
172 >        while ((nr = read(persistfd, buf, sizeof(buf)-1)) == 0) {
173 >                pflock(0);
174 >                sleep(15);              /* wait until ready */
175 >                pflock(1);
176 >        }
177 >        pfdetach();                     /* close persist file */
178 >        if (nr < 0)
179 >                error(SYSTEM, "error reading persist file");
180          buf[nr] = '\0';
181          if ((cp = index(buf, ' ')) == NULL)
182                  goto formerr;
# Line 175 | Line 193 | io_process()           /* just act as conduits to and from actu
193          if ((cp = index(cp, '\n')) == NULL)
194                  goto formerr;
195          *cp++ = '\0';
196 +        pferr = cp;
197 +        if ((cp = index(cp, '\n')) == NULL)
198 +                goto formerr;
199 +        *cp++ = '\0';
200          if (cp-buf != nr)
201                  goto formerr;
202          if (strcmp(buf, progname)) {
# Line 186 | Line 208 | io_process()           /* just act as conduits to and from actu
208                  error(SYSTEM, "cannot signal rendering process in io_process");
209          pid = fork();           /* fork i/o process */
210          if (pid < 0)
211 <                error(SYSTEM, "fork failed in io_process");
211 >                goto forkerr;
212                                  /* connect to appropriate pipe */
213          if (pid) {                      /* parent passes renderer output */
214                  close(0);
215 <                if (open(pfout, O_RDONLY) != 0)
216 <                        error(SYSTEM, "cannot open input pipe in io_process");
215 >                if (pferr[0]) {
216 >                        pid2 = fork();          /* fork another for stderr */
217 >                        if (pid2 < 0)
218 >                                goto forkerr;
219 >                }
220 >                if (pid2) {                     /* parent is still stdout */
221 >                        if (open(pfout, O_RDONLY) != 0)
222 >                                error(SYSTEM,
223 >                                "cannot open output pipe in io_process");
224 >                } else {                        /* second child is stderr */
225 >                        if (open(pferr, O_RDONLY) != 0)
226 >                                error(SYSTEM,
227 >                                "cannot open error pipe in io_process");
228 >                        dup2(2, 1);             /* attach stdout to stderr */
229 >                }
230          } else {                        /* child passes renderer input */
231                  close(1);
232                  if (open(pfin, O_WRONLY) != 1)
233 <                        error(SYSTEM, "cannot open output pipe in io_process");
233 >                        error(SYSTEM, "cannot open input pipe in io_process");
234          }
235                                  /* pass input to output */
236                                  /* read as much as we can, write all of it */
237          while ((nr = read(0, cp=buf, sizeof(buf))) > 0)
238                  do {
239                          if ((n = write(1, cp, nr)) <= 0)
240 <                                goto writerr;
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 child */
247 >        if (pid)                /* parent waits for stdin child */
248                  wait(0);
249 <        exit(0);                /* all done, exit (not quit!) */
249 >        if (pid2 > 0)           /* wait for stderr child also */
250 >                wait(0);
251 >        _exit(0);               /* all done, exit (not quit!) */
252   formerr:
253          error(USER, "format error in persist file");
254 < writerr:
255 <        error(SYSTEM, "write error in io_process");
254 > forkerr:
255 >        error(SYSTEM, "fork failed in io_process");
256   }
257  
258   #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines