ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.61
Committed: Fri Nov 17 20:36:50 2023 UTC (5 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.60: +13 -1 lines
Log Message:
fix(rpiece): added fortification against rpict -pc, -pXYZ options

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rpiece.c,v 2.60 2023/07/11 02:22:30 greg Exp $";
3 #endif
4 /*
5 * Generate sections of a picture.
6 */
7
8
9 #include <stdio.h>
10 #include <signal.h>
11 #include <sys/types.h>
12
13 #include "platform.h"
14 #ifndef NON_POSIX /* XXX need abstraction for process management */
15 #include <sys/wait.h>
16 #endif
17
18 #include "standard.h"
19 #include "color.h"
20 #include "view.h"
21 #include "rtprocess.h"
22
23 #ifndef F_SETLKW
24
25 int
26 main(
27 int argc,
28 char *argv[]
29 )
30 {
31 fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]);
32 exit(1);
33 }
34
35 #else
36
37 #ifndef NFS
38 #define NFS 1
39 #endif
40
41 #ifndef RHAS_FORK_EXEC
42 #undef MAXFORK
43 #define MAXFORK 0
44 #endif
45 #ifndef MAXFORK
46 #if NFS
47 #define MAXFORK 3 /* allotment of duped processes */
48 #else
49 #define MAXFORK 0
50 #endif
51 #endif
52 /* protection from SYSV signals(!) */
53 #if defined(sgi)
54 #define guard_io() sighold(SIGALRM)
55 #define unguard() sigrelse(SIGALRM)
56 #endif
57 #ifndef guard_io
58 #define guard_io()
59 #define unguard()
60 #endif
61
62 extern char *strerror();
63
64 /* rpict command */
65 char *rpargv[128] = {"rpict", "-S", "1"};
66 int rpargc = 3;
67 FILE *torp, *fromrp;
68 COLR *pbuf;
69 /* our view parameters */
70 VIEW ourview = STDVIEW;
71 double pixaspect = 1.0;
72 int hres = 1024, vres = 1024, hmult = 4, vmult = 4;
73 /* output file */
74 char *outfile = NULL;
75 int outfd;
76 long scanorig;
77 FILE *syncfp = NULL; /* synchronization file pointer */
78 int synclst = F_UNLCK; /* synchronization file lock status */
79 int nforked = 0;
80
81 #define sflock(t) if ((t)!=synclst) dolock(fileno(syncfp),synclst=t)
82
83 char *progname;
84 int verbose = 0;
85 int nowarn = 0;
86 unsigned timelim = 0;
87 int rvrlim = -1;
88
89 int gotalrm = 0;
90 void onalrm(int i) { gotalrm++; }
91
92 static void dolock(int fd, int ltyp);
93 static void init(int ac, char **av);
94 static int nextpiece(int *xp, int *yp);
95 static int rvrpiece(int *xp, int *yp);
96 static int cleanup(int rstat);
97 static void rpiece(void);
98 static int putpiece(int xpos, int ypos);
99 static void filerr(char *t);
100
101
102 int
103 main(
104 int argc,
105 char *argv[]
106 )
107 {
108 int i, rval;
109
110 progname = argv[0];
111 for (i = 1; i < argc; i++) {
112 /* expand arguments */
113 while ((rval = expandarg(&argc, &argv, i)) > 0)
114 ;
115 if (rval < 0) {
116 fprintf(stderr, "%s: cannot expand '%s'\n",
117 argv[0], argv[i]);
118 exit(1);
119 }
120 if (argv[i][0] == '-')
121 switch (argv[i][1]) {
122 case 'w':
123 if (!argv[i][2])
124 nowarn = !nowarn;
125 else if (argv[i][2] == '+')
126 nowarn = 0;
127 else if (argv[i][2] == '-')
128 nowarn = 1;
129 break;
130 case 'v':
131 switch (argv[i][2]) {
132 case '\0': /* verbose option */
133 verbose = !verbose;
134 continue;
135 case 'f': /* view file */
136 if (viewfile(argv[++i], &ourview, NULL) <= 0) {
137 fprintf(stderr,
138 "%s: not a view file\n", argv[i]);
139 exit(1);
140 }
141 continue;
142 default: /* view option? */
143 rval = getviewopt(&ourview, argc-i, argv+i);
144 if (rval >= 0) {
145 i += rval;
146 continue;
147 }
148 break;
149 }
150 break;
151 case 'p': /* pixel aspect ratio? */
152 if (argv[i][2] == 'm') {
153 fprintf(stderr, "%s: -pm unsupported\n",
154 argv[0]);
155 ++i;
156 continue;
157 }
158 if (argv[i][2] == 'X') {
159 fprintf(stderr, "%s: -pXYZ unsupported\n",
160 argv[0]);
161 ++i;
162 continue;
163 }
164 if (argv[i][2] == 'c') {
165 fprintf(stderr, "%s: -pc unsupported\n",
166 argv[0]);
167 i += 9;
168 continue;
169 }
170 if (argv[i][2] != 'a' || argv[i][3])
171 break;
172 pixaspect = atof(argv[++i]);
173 continue;
174 case 'T': /* time limit (hours) */
175 if (argv[i][2])
176 break;
177 timelim = atof(argv[++i])*3600. + .5;
178 continue;
179 case 'x': /* overall x resolution */
180 if (argv[i][2])
181 break;
182 hres = atoi(argv[++i]);
183 continue;
184 case 'y': /* overall y resolution */
185 if (argv[i][2])
186 break;
187 vres = atoi(argv[++i]);
188 continue;
189 case 'X': /* horizontal multiplier */
190 if (argv[i][2])
191 break;
192 hmult = atoi(argv[++i]);
193 continue;
194 case 'Y': /* vertical multiplier */
195 if (argv[i][2])
196 break;
197 vmult = atoi(argv[++i]);
198 continue;
199 case 'R': /* recover */
200 if (argv[i][2])
201 break;
202 rvrlim = 0;
203 /* fall through */
204 case 'F': /* syncronization file */
205 if (argv[i][2])
206 break;
207 if ((syncfp =
208 fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
209 fprintf(stderr, "%s: cannot open\n",
210 argv[i]);
211 exit(1);
212 }
213 continue;
214 case 'z': /* z-file ist verbotten */
215 fprintf(stderr, "%s: -z option not allowed\n",
216 argv[0]);
217 exit(1);
218 case 'o': /* output file */
219 if (argv[i][2])
220 break;
221 outfile = argv[++i];
222 continue;
223 }
224 else if (i >= argc-1)
225 break;
226 rpargv[rpargc++] = argv[i];
227 }
228 if (i >= argc) {
229 fprintf(stderr, "%s: missing octree argument\n", argv[0]);
230 exit(1);
231 }
232 if (outfile == NULL) {
233 fprintf(stderr, "%s: missing output file\n", argv[0]);
234 exit(1);
235 }
236 init(argc, argv);
237 rpiece();
238 exit(cleanup(0));
239 }
240
241
242 static void
243 dolock( /* lock or unlock a file */
244 int fd,
245 int ltyp
246 )
247 {
248 static struct flock fls; /* static so initialized to zeroes */
249
250 fls.l_type = ltyp;
251 if (fcntl(fd, F_SETLKW, &fls) < 0) {
252 fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
253 progname, strerror(errno));
254 _exit(1);
255 }
256 }
257
258
259 static void
260 init( /* set up output file and start rpict */
261 int ac,
262 char **av
263 )
264 {
265 static char hrbuf[16], vrbuf[16];
266 extern char VersionID[];
267 char *err;
268 FILE *fp;
269 int hr, vr;
270 SUBPROC rpd; /* since we don't close_process(), this can be local */
271 /* set up view */
272 if ((err = setview(&ourview)) != NULL) {
273 fprintf(stderr, "%s: %s\n", progname, err);
274 exit(1);
275 }
276 if (syncfp != NULL) {
277 sflock(F_RDLCK);
278 fscanf(syncfp, "%d %d", &hmult, &vmult);
279 sflock(F_UNLCK);
280 }
281 /* compute piece size */
282 hr = hres; vr = vres;
283 if (pixaspect > FTINY)
284 normaspect(viewaspect(&ourview), &pixaspect, &hr, &vr);
285 hres /= hmult;
286 vres /= vmult;
287 if (hres <= 0 || vres <= 0) {
288 fprintf(stderr, "%s: illegal resolution/subdivision\n", progname);
289 exit(1);
290 }
291 normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
292 if (!nowarn && (hr != hres*hmult) | (vr != vres*vmult))
293 fprintf(stderr,
294 "%s: warning - changed resolution from %dx%d to %dx%d\n",
295 progname, hr, vr, hres*hmult, vres*vmult);
296 sprintf(hrbuf, "%d", hres);
297 rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf;
298 sprintf(vrbuf, "%d", vres);
299 rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf;
300 rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0";
301 rpargv[rpargc++] = av[ac-1];
302 rpargv[rpargc] = NULL;
303 /* open output file */
304 if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
305 dolock(outfd, F_WRLCK);
306 if ((fp = fdopen(dup(outfd), "w")) == NULL)
307 goto filerr;
308 newheader("RADIANCE", fp); /* create header */
309 printargs(ac, av, fp);
310 fprintf(fp, "SOFTWARE= %s\n", VersionID);
311 fputs(VIEWSTR, fp);
312 fprintview(&ourview, fp);
313 fputc('\n', fp);
314 fputnow(fp);
315 if (pixaspect < .99 || pixaspect > 1.01)
316 fputaspect(pixaspect, fp);
317 fputprims(stdprims, fp);
318 fputformat(COLRFMT, fp);
319 fputc('\n', fp);
320 fprtresolu(hres*hmult, vres*vmult, fp);
321 } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
322 dolock(outfd, F_RDLCK);
323 if ((fp = fdopen(dup(outfd), "r+")) == NULL)
324 goto filerr;
325 getheader(fp, NULL, NULL); /* skip header */
326 if (!fscnresolu(&hr, &vr, fp) || /* check resolution */
327 hr != hres*hmult || vr != vres*vmult) {
328 fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
329 progname, outfile);
330 exit(1);
331 }
332 } else {
333 fprintf(stderr, "%s: cannot open file \"%s\"\n",
334 progname, outfile);
335 exit(1);
336 }
337 scanorig = ftell(fp); /* record position of first scanline */
338 if (fclose(fp) == -1) /* done with stream i/o */
339 goto filerr;
340 dolock(outfd, F_UNLCK);
341 /* start rpict process */
342 rpd = sp_inactive;
343 if (open_process(&rpd, rpargv) <= 0) {
344 fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
345 exit(1);
346 }
347 if ((fromrp = fdopen(rpd.r, "r")) == NULL ||
348 (torp = fdopen(rpd.w, "w")) == NULL) {
349 fprintf(stderr, "%s: cannot open stream to %s\n",
350 progname, rpargv[0]);
351 exit(1);
352 }
353 if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) {
354 fprintf(stderr, "%s: out of memory\n", progname);
355 exit(1);
356 }
357 signal(SIGALRM, onalrm);
358 if (timelim)
359 alarm(timelim);
360 return;
361 filerr:
362 fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
363 exit(1);
364 }
365
366
367 static int
368 nextpiece( /* get next piece assignment */
369 int *xp,
370 int *yp
371 )
372 {
373 if (gotalrm) /* someone wants us to quit */
374 return(0);
375 if (syncfp != NULL) { /* use sync file */
376 /*
377 * So we don't necessarily have to lock and unlock the file
378 * multiple times (very slow), we establish an exclusive
379 * lock at the beginning on our synchronization file and
380 * maintain it in the subroutine rvrpiece().
381 */
382 sflock(F_WRLCK);
383 fseek(syncfp, 0L, 0); /* read position */
384 if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
385 *xp = hmult-1;
386 *yp = vmult;
387 }
388 if (rvrlim == 0) /* initialize recovery limit */
389 rvrlim = *xp*vmult + *yp;
390 if (rvrpiece(xp, yp)) { /* do stragglers first */
391 sflock(F_UNLCK);
392 return(1);
393 }
394 if (--(*yp) < 0) { /* decrement position */
395 *yp = vmult-1;
396 if (--(*xp) < 0) { /* all done */
397 sflock(F_UNLCK);
398 return(0);
399 }
400 }
401 fseek(syncfp, 0L, 0); /* write new position */
402 fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
403 fflush(syncfp);
404 sflock(F_UNLCK); /* release sync file */
405 return(1);
406 }
407 return(scanf("%d %d", xp, yp) == 2); /* use stdin */
408 }
409
410
411 static int
412 rvrpiece( /* check for recoverable pieces */
413 int *xp,
414 int *yp
415 )
416 {
417 static char *pdone = NULL; /* which pieces are done */
418 static long readpos = -1; /* how far we've read */
419 int px, py, i;
420 /*
421 * This routine is called by nextpiece() with an
422 * exclusive lock on syncfp and the file pointer at the
423 * appropriate position to read in the finished pieces.
424 */
425 if (rvrlim < 0)
426 return(0); /* only check if asked */
427 if (pdone == NULL) /* first call */
428 pdone = (char *)calloc(hmult*vmult, sizeof(char));
429 if (pdone == NULL) {
430 fprintf(stderr, "%s: out of memory\n", progname);
431 exit(1);
432 }
433 if (readpos != -1) /* mark what's been done */
434 fseek(syncfp, readpos, 0);
435 while (fscanf(syncfp, "%d %d", &px, &py) == 2)
436 pdone[px*vmult+py] = 1;
437 if (!feof(syncfp)) {
438 fprintf(stderr, "%s: format error in sync file\n", progname);
439 exit(1);
440 }
441 readpos = ftell(syncfp);
442 i = hmult*vmult; /* find an unaccounted for piece */
443 while (i-- > rvrlim)
444 if (!pdone[i]) {
445 *xp = i / vmult;
446 *yp = i % vmult;
447 pdone[i] = 1; /* consider it done */
448 return(1);
449 }
450 rvrlim = -1; /* nothing left to recover */
451 free(pdone);
452 pdone = NULL;
453 return(0);
454 }
455
456
457 static int
458 cleanup( /* close rpict process and clean up */
459 int rstat
460 )
461 {
462 int status;
463
464 bfree((char *)pbuf, hres*vres*sizeof(COLR));
465 fclose(torp);
466 fclose(fromrp);
467 while (wait(&status) != -1)
468 if (rstat == 0)
469 rstat = status>>8 & 0xff;
470 return(rstat);
471 }
472
473
474 static void
475 rpiece(void) /* render picture piece by piece */
476 {
477 char *err;
478 VIEW pview;
479 int xorg, yorg;
480 /* render each piece */
481 while (nextpiece(&xorg, &yorg)) {
482 pview = ourview;
483 err = cropview(&pview, (double)xorg/hmult, (double)yorg/vmult,
484 (xorg+1.)/hmult, (yorg+1.)/vmult);
485 if (err != NULL) {
486 fprintf(stderr, "%s: %s\n", progname, err);
487 exit(cleanup(1));
488 }
489 fputs(VIEWSTR, torp);
490 fprintview(&pview, torp);
491 fputc('\n', torp);
492 fflush(torp); /* assigns piece to rpict */
493 putpiece(xorg, yorg); /* place piece in output */
494 }
495 }
496
497
498 static int
499 putpiece( /* get next piece from rpict */
500 int xpos,
501 int ypos
502 )
503 {
504 struct flock fls;
505 int pid, status;
506 int hr, vr;
507 int y;
508 /* check bounds */
509 if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) {
510 fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
511 progname, xpos, ypos);
512 exit(cleanup(1));
513 }
514 /* check header from rpict */
515 guard_io();
516 getheader(fromrp, NULL, NULL);
517 if (!fscnresolu(&hr, &vr, fromrp) || (hr != hres) | (vr != vres)) {
518 fprintf(stderr, "%s: resolution mismatch from %s\n",
519 progname, rpargv[0]);
520 exit(cleanup(1));
521 }
522 if (verbose) { /* notify caller */
523 printf("%d %d begun\n", xpos, ypos);
524 fflush(stdout);
525 }
526 unguard();
527 /* load new piece into buffer */
528 for (y = 0; y < vr; y++) {
529 guard_io();
530 if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
531 fprintf(stderr, "%s: read error from %s\n",
532 progname, rpargv[0]);
533 exit(cleanup(1));
534 }
535 unguard();
536 }
537 #if MAXFORK
538 /* fork so we don't slow rpict down */
539 if ((pid = fork()) > 0) {
540 if (++nforked >= MAXFORK) {
541 wait(&status); /* reap a child */
542 if (status)
543 exit(cleanup(status>>8 & 0xff));
544 nforked--;
545 }
546 return(pid);
547 }
548 #else
549 pid = -1; /* no forking */
550 #endif
551 fls.l_start = scanorig +
552 ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
553 #if NFS
554 fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
555 /* lock file section so NFS doesn't mess up */
556 fls.l_whence = 0;
557 fls.l_type = F_WRLCK;
558 #if 0
559 if (fcntl(outfd, F_SETLKW, &fls) < 0)
560 filerr("lock");
561 #else
562 dolock(outfd, F_WRLCK);
563 #endif
564 #endif
565 /* write new piece to file */
566 if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0)
567 filerr("seek");
568 if (hmult == 1) {
569 if (writebuf(outfd, (char *)pbuf,
570 vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
571 filerr("write");
572 } else
573 for (y = 0; y < vr; y++) {
574 if (writebuf(outfd, (char *)(pbuf+y*hr),
575 hr*sizeof(COLR)) != hr*sizeof(COLR))
576 filerr("write");
577 if (y < vr-1 && lseek(outfd,
578 (off_t)(hmult-1)*hr*sizeof(COLR),
579 SEEK_CUR) < 0)
580 filerr("seek");
581 }
582 #if NFS
583 fls.l_type = F_UNLCK; /* release lock */
584 #if 0
585 if (fcntl(outfd, F_SETLKW, &fls) < 0)
586 filerr("lock");
587 #else
588 dolock(outfd, F_UNLCK);
589 #endif
590 #endif
591 if (verbose) { /* notify caller */
592 printf("%d %d done\n", xpos, ypos);
593 fflush(stdout);
594 }
595 if (syncfp != NULL) { /* record what's been done */
596 sflock(F_WRLCK);
597 fseek(syncfp, 0L, 2); /* append index */
598 fprintf(syncfp, "%4d %4d\n", xpos, ypos);
599 fflush(syncfp);
600 /*** Unlock not necessary, since
601 sflock(F_UNLCK); _exit() or nextpiece() is next ***/
602 }
603 if (pid == -1) /* didn't fork or fork failed */
604 return(0);
605 _exit(0); /* else exit child process (releasing locks) */
606 }
607
608
609 static void
610 filerr( /* report file error and exit */
611 char *t
612 )
613 {
614 fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
615 progname, t, outfile, strerror(errno));
616 _exit(1);
617 }
618
619 #endif