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.11 by greg, Mon Nov 8 09:35:32 1993 UTC vs.
Revision 2.14 by greg, Sat Jul 6 22:30:05 1996 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1993 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 25 | Line 25 | extern char    *strcpy(), *index();
25  
26   extern int      headismine;     /* boolean true if header belongs to me */
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 39 | 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 53 | 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 73 | Line 80 | int    lf;
80   persistfile(pfn)        /* open persist file and lock it */
81   char    *pfn;
82   {
83 <        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0666);
83 >        persistfd = open(pfn, O_WRONLY|O_CREAT|O_EXCL, 0644);
84          if (persistfd >= 0) {
85                  persistfname = pfn;
86 +                        /* put something there for faulty lock daemons */
87 +                write(persistfd, "Initializing...\n", 16);
88                  pflock(1);
89                  return;
90          }
# Line 90 | Line 99 | char   *pfn;
99   }
100  
101  
102 < int sig_noop() {}
102 > static int      got_io;
103  
104 + static int sig_io() { got_io++; }
105  
106 + static int sig_alrm() { quit(0); }
107 +
108 +
109   pfhold()                /* holding pattern for idle rendering process */
110   {
111 <        char    buf[128];
111 >        int     (*oldalrm)();
112 >        char    buf[512];
113          register int    n;
114                                  /* close input and output descriptors */
115          close(fileno(stdin));
# Line 105 | Line 119 | pfhold()               /* holding pattern for idle rendering proces
119                  goto createrr;
120          if (mknod(mktemp(strcpy(outpname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
121                  goto createrr;
122 <        sprintf(buf, "%d\n%s\n%s\n", getpid(), inpname, outpname);
123 <        if (lseek(persistfd, 0L, 0) < 0 || ftruncate(persistfd, 0L) < 0)
124 <                error(SYSTEM, "seek/truncate error on persist file");
122 >        if (errfile == NULL &&
123 >                mknod(mktemp(strcpy(errname,TEMPLATE)), S_IFIFO|0600, 0) < 0)
124 >                goto createrr;
125 >        sprintf(buf, "%s %d\n%s\n%s\n%s\n", progname, getpid(),
126 >                        inpname, outpname, errname);
127 >        if (lseek(persistfd, 0L, 0) < 0)
128 >                error(SYSTEM, "seek error on persist file");
129          n = strlen(buf);
130          if (write(persistfd, buf, n) < n)
131                  error(SYSTEM, "error writing persist file");
132 +        ftruncate(persistfd, (long)n);
133 +        fsync(persistfd);       /* shouldn't be necessary, but.... */
134                                  /* wait TIMELIM for someone to signal us */
135 <        signal(SIGIO, sig_noop);
135 >        got_io = 0;
136 >        signal(SIGIO, sig_io);
137 >        oldalrm = (int (*)())signal(SIGALRM, sig_alrm);
138          alarm(TIMELIM);
139          pflock(0);
140 <        pause();
140 >        while (!got_io)
141 >                pause();
142          alarm(0);
143 +        signal(SIGALRM, oldalrm);
144          signal(SIGIO, SIG_DFL);
145          pflock(1);
146                                  /* someone wants us; reopen stdin and stdout */
# Line 124 | Line 148 | pfhold()               /* holding pattern for idle rendering proces
148                  goto openerr;
149          if (freopen(outpname, "w", stdout) == NULL)
150                  goto openerr;
151 +        if (errname[0]) {
152 +                if (freopen(errname, "w", stderr) == NULL)
153 +                        goto openerr;
154 +                unlink(errname);
155 +                errname[0] = '\0';
156 +        }
157          unlink(inpname);
158          inpname[0] = '\0';
159          unlink(outpname);
# Line 140 | Line 170 | io_process()           /* just act as conduits to and from actu
170   {
171          register char   *cp;
172          register int    nr, n;
173 <        char    buf[4096], *pfin, *pfout;
174 <        int     pid;
173 >        char    buf[512], *pfin, *pfout, *pferr;
174 >        int     pid, pid2 = -1;
175                                  /* load and close persist file */
176 <        nr = read(persistfd, buf, sizeof(buf));
176 >        errno = 0;
177 >        nr = read(persistfd, buf, sizeof(buf)-1);
178          pfdetach();
179 <        if (nr < 0)
180 <                error(SYSTEM, "cannot read persist file");
179 >        if (nr <= 0)
180 >                error(SYSTEM, "error reading persist file");
181          buf[nr] = '\0';
182 <        if ((cp = index(buf, '\n')) == NULL)
182 >        if ((cp = index(buf, ' ')) == NULL)
183                  goto formerr;
184          *cp++ = '\0';
185 <        if ((pid = atoi(buf)) <= 0)
185 >        if ((pid = atoi(cp)) <= 0)
186                  goto formerr;
156        pfin = cp;
187          if ((cp = index(cp, '\n')) == NULL)
188                  goto formerr;
189 +        pfin = ++cp;
190 +        if ((cp = index(cp, '\n')) == NULL)
191 +                goto formerr;
192          *cp++ = '\0';
193          pfout = cp;
194          if ((cp = index(cp, '\n')) == NULL)
195                  goto formerr;
196          *cp++ = '\0';
197 +        pferr = cp;
198 +        if ((cp = index(cp, '\n')) == NULL)
199 +                goto formerr;
200 +        *cp++ = '\0';
201          if (cp-buf != nr)
202                  goto formerr;
203 +        if (strcmp(buf, progname)) {
204 +                sprintf(errmsg, "persist file for %s, not %s", buf, progname);
205 +                error(USER, errmsg);
206 +        }
207                                  /* wake up rendering process */
208          if (kill(pid, SIGIO) < 0)
209                  error(SYSTEM, "cannot signal rendering process in io_process");
210          pid = fork();           /* fork i/o process */
211          if (pid < 0)
212 <                error(SYSTEM, "fork failed in io_process");
212 >                goto forkerr;
213                                  /* connect to appropriate pipe */
214          if (pid) {                      /* parent passes renderer output */
215                  close(0);
216 <                if (open(pfout, O_RDONLY) != 0)
217 <                        error(SYSTEM, "cannot open input pipe in io_process");
216 >                if (pferr[0]) {
217 >                        pid2 = fork();          /* fork another for stderr */
218 >                        if (pid2 < 0)
219 >                                goto forkerr;
220 >                }
221 >                if (pid2) {                     /* parent is still stdout */
222 >                        if (open(pfout, O_RDONLY) != 0)
223 >                                error(SYSTEM,
224 >                                "cannot open output pipe in io_process");
225 >                } else {                        /* second child is stderr */
226 >                        if (open(pferr, O_RDONLY) != 0)
227 >                                error(SYSTEM,
228 >                                "cannot open error pipe in io_process");
229 >                        dup2(2, 1);             /* attach stdout to stderr */
230 >                }
231          } else {                        /* child passes renderer input */
232                  close(1);
233                  if (open(pfin, O_WRONLY) != 1)
234 <                        error(SYSTEM, "cannot open output pipe in io_process");
234 >                        error(SYSTEM, "cannot open input pipe in io_process");
235          }
236                                  /* pass input to output */
237                                  /* read as much as we can, write all of it */
238          while ((nr = read(0, cp=buf, sizeof(buf))) > 0)
239                  do {
240                          if ((n = write(1, cp, nr)) <= 0)
241 <                                goto writerr;
241 >                                error(SYSTEM, "write error in io_process");
242                          cp += n;
243                  } while ((nr -= n) > 0);
244          if (nr < 0)
245                  error(SYSTEM, "read error in io_process");
246          close(0);               /* close input */
247          close(1);               /* close output */
248 <        if (pid)                /* parent waits for child */
248 >        if (pid)                /* parent waits for stdin child */
249                  wait(0);
250 <        exit(0);                /* all done, exit (not quit!) */
250 >        if (pid2 > 0)           /* wait for stderr child also */
251 >                wait(0);
252 >        _exit(0);               /* all done, exit (not quit!) */
253   formerr:
254          error(USER, "format error in persist file");
255 < writerr:
256 <        error(SYSTEM, "write error in io_process");
255 > forkerr:
256 >        error(SYSTEM, "fork failed in io_process");
257   }
258  
259   #else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines