1 /* $NetBSD: meta.c,v 1.61 2016/06/07 00:40:00 sjg Exp $ */ 2 3 /* 4 * Implement 'meta' mode. 5 * Adapted from John Birrell's patches to FreeBSD make. 6 * --sjg 7 */ 8 /* 9 * Copyright (c) 2009-2016, Juniper Networks, Inc. 10 * Portions Copyright (c) 2009, John Birrell. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 #if defined(USE_META) 34 35 #ifdef HAVE_CONFIG_H 36 # include "config.h" 37 #endif 38 #include <sys/stat.h> 39 #include <sys/ioctl.h> 40 #ifdef HAVE_LIBGEN_H 41 #include <libgen.h> 42 #elif !defined(HAVE_DIRNAME) 43 char * dirname(char *); 44 #endif 45 #include <errno.h> 46 #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H) 47 #include <err.h> 48 #endif 49 50 #include "make.h" 51 #include "job.h" 52 53 #ifdef HAVE_FILEMON_H 54 # include <filemon.h> 55 #endif 56 #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD) 57 # define USE_FILEMON 58 #endif 59 60 static BuildMon Mybm; /* for compat */ 61 static Lst metaBailiwick; /* our scope of control */ 62 static char *metaBailiwickStr; /* string storage for the list */ 63 static Lst metaIgnorePaths; /* paths we deliberately ignore */ 64 static char *metaIgnorePathsStr; /* string storage for the list */ 65 66 #ifndef MAKE_META_IGNORE_PATHS 67 #define MAKE_META_IGNORE_PATHS ".MAKE.META.IGNORE_PATHS" 68 #endif 69 #ifndef MAKE_META_IGNORE_PATTERNS 70 #define MAKE_META_IGNORE_PATTERNS ".MAKE.META.IGNORE_PATTERNS" 71 #endif 72 73 Boolean useMeta = FALSE; 74 static Boolean useFilemon = FALSE; 75 static Boolean writeMeta = FALSE; 76 static Boolean metaMissing = FALSE; /* oodate if missing */ 77 static Boolean filemonMissing = FALSE; /* oodate if missing */ 78 static Boolean metaEnv = FALSE; /* don't save env unless asked */ 79 static Boolean metaVerbose = FALSE; 80 static Boolean metaIgnoreCMDs = FALSE; /* ignore CMDs in .meta files */ 81 static Boolean metaIgnorePatterns = FALSE; /* do we need to do pattern matches */ 82 static Boolean metaCurdirOk = FALSE; /* write .meta in .CURDIR Ok? */ 83 static Boolean metaSilent = FALSE; /* if we have a .meta be SILENT */ 84 85 extern Boolean forceJobs; 86 extern Boolean comatMake; 87 extern char **environ; 88 89 #define MAKE_META_PREFIX ".MAKE.META.PREFIX" 90 91 #ifndef N2U 92 # define N2U(n, u) (((n) + ((u) - 1)) / (u)) 93 #endif 94 #ifndef ROUNDUP 95 # define ROUNDUP(n, u) (N2U((n), (u)) * (u)) 96 #endif 97 98 #if !defined(HAVE_STRSEP) 99 # define strsep(s, d) stresep((s), (d), 0) 100 #endif 101 102 /* 103 * Filemon is a kernel module which snoops certain syscalls. 104 * 105 * C chdir 106 * E exec 107 * F [v]fork 108 * L [sym]link 109 * M rename 110 * R read 111 * W write 112 * S stat 113 * 114 * See meta_oodate below - we mainly care about 'E' and 'R'. 115 * 116 * We can still use meta mode without filemon, but 117 * the benefits are more limited. 118 */ 119 #ifdef USE_FILEMON 120 # ifndef _PATH_FILEMON 121 # define _PATH_FILEMON "/dev/filemon" 122 # endif 123 124 /* 125 * Open the filemon device. 126 */ 127 static void 128 filemon_open(BuildMon *pbm) 129 { 130 int retry; 131 132 pbm->mon_fd = pbm->filemon_fd = -1; 133 if (!useFilemon) 134 return; 135 136 for (retry = 5; retry >= 0; retry--) { 137 if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0) 138 break; 139 } 140 141 if (pbm->filemon_fd < 0) { 142 useFilemon = FALSE; 143 warn("Could not open %s", _PATH_FILEMON); 144 return; 145 } 146 147 /* 148 * We use a file outside of '.' 149 * to avoid a FreeBSD kernel bug where unlink invalidates 150 * cwd causing getcwd to do a lot more work. 151 * We only care about the descriptor. 152 */ 153 pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL); 154 if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) { 155 err(1, "Could not set filemon file descriptor!"); 156 } 157 /* we don't need these once we exec */ 158 (void)fcntl(pbm->mon_fd, F_SETFD, FD_CLOEXEC); 159 (void)fcntl(pbm->filemon_fd, F_SETFD, FD_CLOEXEC); 160 } 161 162 /* 163 * Read the build monitor output file and write records to the target's 164 * metadata file. 165 */ 166 static int 167 filemon_read(FILE *mfp, int fd) 168 { 169 char buf[BUFSIZ]; 170 int n; 171 int error; 172 173 /* Check if we're not writing to a meta data file.*/ 174 if (mfp == NULL) { 175 if (fd >= 0) 176 close(fd); /* not interested */ 177 return 0; 178 } 179 /* rewind */ 180 (void)lseek(fd, (off_t)0, SEEK_SET); 181 182 error = 0; 183 fprintf(mfp, "\n-- filemon acquired metadata --\n"); 184 185 while ((n = read(fd, buf, sizeof(buf))) > 0) { 186 if ((int)fwrite(buf, 1, n, mfp) < n) 187 error = EIO; 188 } 189 fflush(mfp); 190 if (close(fd) < 0) 191 error = errno; 192 return error; 193 } 194 #endif 195 196 /* 197 * when realpath() fails, 198 * we use this, to clean up ./ and ../ 199 */ 200 static void 201 eat_dots(char *buf, size_t bufsz, int dots) 202 { 203 char *cp; 204 char *cp2; 205 const char *eat; 206 size_t eatlen; 207 208 switch (dots) { 209 case 1: 210 eat = "/./"; 211 eatlen = 2; 212 break; 213 case 2: 214 eat = "/../"; 215 eatlen = 3; 216 break; 217 default: 218 return; 219 } 220 221 do { 222 cp = strstr(buf, eat); 223 if (cp) { 224 cp2 = cp + eatlen; 225 if (dots == 2 && cp > buf) { 226 do { 227 cp--; 228 } while (cp > buf && *cp != '/'); 229 } 230 if (*cp == '/') { 231 strlcpy(cp, cp2, bufsz - (cp - buf)); 232 } else { 233 return; /* can't happen? */ 234 } 235 } 236 } while (cp); 237 } 238 239 static char * 240 meta_name(struct GNode *gn, char *mname, size_t mnamelen, 241 const char *dname, 242 const char *tname, 243 const char *cwd) 244 { 245 char buf[MAXPATHLEN]; 246 char *rp; 247 char *cp; 248 char *tp; 249 250 /* 251 * Weed out relative paths from the target file name. 252 * We have to be careful though since if target is a 253 * symlink, the result will be unstable. 254 * So we use realpath() just to get the dirname, and leave the 255 * basename as given to us. 256 */ 257 if ((cp = strrchr(tname, '/'))) { 258 if (cached_realpath(tname, buf)) { 259 if ((rp = strrchr(buf, '/'))) { 260 rp++; 261 cp++; 262 if (strcmp(cp, rp) != 0) 263 strlcpy(rp, cp, sizeof(buf) - (rp - buf)); 264 } 265 tname = buf; 266 } else { 267 /* 268 * We likely have a directory which is about to be made. 269 * We pretend realpath() succeeded, to have a chance 270 * of generating the same meta file name that we will 271 * next time through. 272 */ 273 if (tname[0] == '/') { 274 strlcpy(buf, tname, sizeof(buf)); 275 } else { 276 snprintf(buf, sizeof(buf), "%s/%s", cwd, tname); 277 } 278 eat_dots(buf, sizeof(buf), 1); /* ./ */ 279 eat_dots(buf, sizeof(buf), 2); /* ../ */ 280 tname = buf; 281 } 282 } 283 /* on some systems dirname may modify its arg */ 284 tp = bmake_strdup(tname); 285 if (strcmp(dname, dirname(tp)) == 0) 286 snprintf(mname, mnamelen, "%s.meta", tname); 287 else { 288 snprintf(mname, mnamelen, "%s/%s.meta", dname, tname); 289 290 /* 291 * Replace path separators in the file name after the 292 * current object directory path. 293 */ 294 cp = mname + strlen(dname) + 1; 295 296 while (*cp != '\0') { 297 if (*cp == '/') 298 *cp = '_'; 299 cp++; 300 } 301 } 302 free(tp); 303 return (mname); 304 } 305 306 /* 307 * Return true if running ${.MAKE} 308 * Bypassed if target is flagged .MAKE 309 */ 310 static int 311 is_submake(void *cmdp, void *gnp) 312 { 313 static char *p_make = NULL; 314 static int p_len; 315 char *cmd = cmdp; 316 GNode *gn = gnp; 317 char *mp = NULL; 318 char *cp; 319 char *cp2; 320 int rc = 0; /* keep looking */ 321 322 if (!p_make) { 323 p_make = Var_Value(".MAKE", gn, &cp); 324 p_len = strlen(p_make); 325 } 326 cp = strchr(cmd, '$'); 327 if ((cp)) { 328 mp = Var_Subst(NULL, cmd, gn, VARF_WANTRES); 329 cmd = mp; 330 } 331 cp2 = strstr(cmd, p_make); 332 if ((cp2)) { 333 switch (cp2[p_len]) { 334 case '\0': 335 case ' ': 336 case '\t': 337 case '\n': 338 rc = 1; 339 break; 340 } 341 if (cp2 > cmd && rc > 0) { 342 switch (cp2[-1]) { 343 case ' ': 344 case '\t': 345 case '\n': 346 break; 347 default: 348 rc = 0; /* no match */ 349 break; 350 } 351 } 352 } 353 free(mp); 354 return (rc); 355 } 356 357 typedef struct meta_file_s { 358 FILE *fp; 359 GNode *gn; 360 } meta_file_t; 361 362 static int 363 printCMD(void *cmdp, void *mfpp) 364 { 365 meta_file_t *mfp = mfpp; 366 char *cmd = cmdp; 367 char *cp = NULL; 368 369 if (strchr(cmd, '$')) { 370 cmd = cp = Var_Subst(NULL, cmd, mfp->gn, VARF_WANTRES); 371 } 372 fprintf(mfp->fp, "CMD %s\n", cmd); 373 free(cp); 374 return 0; 375 } 376 377 /* 378 * Certain node types never get a .meta file 379 */ 380 #define SKIP_META_TYPE(_type) do { \ 381 if ((gn->type & __CONCAT(OP_, _type))) { \ 382 if (verbose) { \ 383 fprintf(debug_file, "Skipping meta for %s: .%s\n", \ 384 gn->name, __STRING(_type)); \ 385 } \ 386 return FALSE; \ 387 } \ 388 } while (0) 389 390 391 /* 392 * Do we need/want a .meta file ? 393 */ 394 static Boolean 395 meta_needed(GNode *gn, const char *dname, const char *tname, 396 char *objdir, int verbose) 397 { 398 struct stat fs; 399 400 if (verbose) 401 verbose = DEBUG(META); 402 403 /* This may be a phony node which we don't want meta data for... */ 404 /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */ 405 /* Or it may be explicitly flagged as .NOMETA */ 406 SKIP_META_TYPE(NOMETA); 407 /* Unless it is explicitly flagged as .META */ 408 if (!(gn->type & OP_META)) { 409 SKIP_META_TYPE(PHONY); 410 SKIP_META_TYPE(SPECIAL); 411 SKIP_META_TYPE(MAKE); 412 } 413 414 /* Check if there are no commands to execute. */ 415 if (Lst_IsEmpty(gn->commands)) { 416 if (verbose) 417 fprintf(debug_file, "Skipping meta for %s: no commands\n", 418 gn->name); 419 return FALSE; 420 } 421 if ((gn->type & (OP_META|OP_SUBMAKE)) == OP_SUBMAKE) { 422 /* OP_SUBMAKE is a bit too aggressive */ 423 if (Lst_ForEach(gn->commands, is_submake, gn)) { 424 if (DEBUG(META)) 425 fprintf(debug_file, "Skipping meta for %s: .SUBMAKE\n", 426 gn->name); 427 return FALSE; 428 } 429 } 430 431 /* The object directory may not exist. Check it.. */ 432 if (cached_stat(dname, &fs) != 0) { 433 if (verbose) 434 fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n", 435 gn->name); 436 return FALSE; 437 } 438 439 /* make sure these are canonical */ 440 if (cached_realpath(dname, objdir)) 441 dname = objdir; 442 443 /* If we aren't in the object directory, don't create a meta file. */ 444 if (!metaCurdirOk && strcmp(curdir, dname) == 0) { 445 if (verbose) 446 fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n", 447 gn->name); 448 return FALSE; 449 } 450 return TRUE; 451 } 452 453 454 static FILE * 455 meta_create(BuildMon *pbm, GNode *gn) 456 { 457 meta_file_t mf; 458 char buf[MAXPATHLEN]; 459 char objdir[MAXPATHLEN]; 460 char **ptr; 461 const char *dname; 462 const char *tname; 463 char *fname; 464 const char *cp; 465 char *p[4]; /* >= possible uses */ 466 int i; 467 468 mf.fp = NULL; 469 i = 0; 470 471 dname = Var_Value(".OBJDIR", gn, &p[i++]); 472 tname = Var_Value(TARGET, gn, &p[i++]); 473 474 /* if this succeeds objdir is realpath of dname */ 475 if (!meta_needed(gn, dname, tname, objdir, TRUE)) 476 goto out; 477 dname = objdir; 478 479 if (metaVerbose) { 480 char *mp; 481 482 /* Describe the target we are building */ 483 mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, VARF_WANTRES); 484 if (*mp) 485 fprintf(stdout, "%s\n", mp); 486 free(mp); 487 } 488 /* Get the basename of the target */ 489 if ((cp = strrchr(tname, '/')) == NULL) { 490 cp = tname; 491 } else { 492 cp++; 493 } 494 495 fflush(stdout); 496 497 if (!writeMeta) 498 /* Don't create meta data. */ 499 goto out; 500 501 fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname), 502 dname, tname, objdir); 503 504 #ifdef DEBUG_META_MODE 505 if (DEBUG(META)) 506 fprintf(debug_file, "meta_create: %s\n", fname); 507 #endif 508 509 if ((mf.fp = fopen(fname, "w")) == NULL) 510 err(1, "Could not open meta file '%s'", fname); 511 512 fprintf(mf.fp, "# Meta data file %s\n", fname); 513 514 mf.gn = gn; 515 516 Lst_ForEach(gn->commands, printCMD, &mf); 517 518 fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf))); 519 fprintf(mf.fp, "TARGET %s\n", tname); 520 521 if (metaEnv) { 522 for (ptr = environ; *ptr != NULL; ptr++) 523 fprintf(mf.fp, "ENV %s\n", *ptr); 524 } 525 526 fprintf(mf.fp, "-- command output --\n"); 527 fflush(mf.fp); 528 529 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL); 530 Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL); 531 532 gn->type |= OP_META; /* in case anyone wants to know */ 533 if (metaSilent) { 534 gn->type |= OP_SILENT; 535 } 536 out: 537 for (i--; i >= 0; i--) { 538 free(p[i]); 539 } 540 541 return (mf.fp); 542 } 543 544 static Boolean 545 boolValue(char *s) 546 { 547 switch(*s) { 548 case '0': 549 case 'N': 550 case 'n': 551 case 'F': 552 case 'f': 553 return FALSE; 554 } 555 return TRUE; 556 } 557 558 /* 559 * Initialization we need before reading makefiles. 560 */ 561 void 562 meta_init(void) 563 { 564 #ifdef USE_FILEMON 565 /* this allows makefiles to test if we have filemon support */ 566 Var_Set(".MAKE.PATH_FILEMON", _PATH_FILEMON, VAR_GLOBAL, 0); 567 #endif 568 } 569 570 571 #define get_mode_bf(bf, token) \ 572 if ((cp = strstr(make_mode, token))) \ 573 bf = boolValue(&cp[sizeof(token) - 1]) 574 575 /* 576 * Initialization we need after reading makefiles. 577 */ 578 void 579 meta_mode_init(const char *make_mode) 580 { 581 static int once = 0; 582 char *cp; 583 584 useMeta = TRUE; 585 useFilemon = TRUE; 586 writeMeta = TRUE; 587 588 if (make_mode) { 589 if (strstr(make_mode, "env")) 590 metaEnv = TRUE; 591 if (strstr(make_mode, "verb")) 592 metaVerbose = TRUE; 593 if (strstr(make_mode, "read")) 594 writeMeta = FALSE; 595 if (strstr(make_mode, "nofilemon")) 596 useFilemon = FALSE; 597 if (strstr(make_mode, "ignore-cmd")) 598 metaIgnoreCMDs = TRUE; 599 if (useFilemon) 600 get_mode_bf(filemonMissing, "missing-filemon="); 601 get_mode_bf(metaCurdirOk, "curdirok="); 602 get_mode_bf(metaMissing, "missing-meta="); 603 get_mode_bf(metaSilent, "silent="); 604 } 605 if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) { 606 /* 607 * The default value for MAKE_META_PREFIX 608 * prints the absolute path of the target. 609 * This works be cause :H will generate '.' if there is no / 610 * and :tA will resolve that to cwd. 611 */ 612 Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0); 613 } 614 if (once) 615 return; 616 once = 1; 617 memset(&Mybm, 0, sizeof(Mybm)); 618 /* 619 * We consider ourselves master of all within ${.MAKE.META.BAILIWICK} 620 */ 621 metaBailiwick = Lst_Init(FALSE); 622 metaBailiwickStr = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}", 623 VAR_GLOBAL, VARF_WANTRES); 624 if (metaBailiwickStr) { 625 str2Lst_Append(metaBailiwick, metaBailiwickStr, NULL); 626 } 627 /* 628 * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS} 629 */ 630 metaIgnorePaths = Lst_Init(FALSE); 631 Var_Append(MAKE_META_IGNORE_PATHS, 632 "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}", VAR_GLOBAL); 633 metaIgnorePathsStr = Var_Subst(NULL, 634 "${" MAKE_META_IGNORE_PATHS ":O:u:tA}", VAR_GLOBAL, 635 VARF_WANTRES); 636 if (metaIgnorePathsStr) { 637 str2Lst_Append(metaIgnorePaths, metaIgnorePathsStr, NULL); 638 } 639 640 /* 641 * We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS} 642 */ 643 cp = NULL; 644 if (Var_Value(MAKE_META_IGNORE_PATTERNS, VAR_GLOBAL, &cp)) { 645 metaIgnorePatterns = TRUE; 646 free(cp); 647 } 648 } 649 650 /* 651 * In each case below we allow for job==NULL 652 */ 653 void 654 meta_job_start(Job *job, GNode *gn) 655 { 656 BuildMon *pbm; 657 658 if (job != NULL) { 659 pbm = &job->bm; 660 } else { 661 pbm = &Mybm; 662 } 663 pbm->mfp = meta_create(pbm, gn); 664 #ifdef USE_FILEMON_ONCE 665 /* compat mode we open the filemon dev once per command */ 666 if (job == NULL) 667 return; 668 #endif 669 #ifdef USE_FILEMON 670 if (pbm->mfp != NULL && useFilemon) { 671 filemon_open(pbm); 672 } else { 673 pbm->mon_fd = pbm->filemon_fd = -1; 674 } 675 #endif 676 } 677 678 /* 679 * The child calls this before doing anything. 680 * It does not disturb our state. 681 */ 682 void 683 meta_job_child(Job *job) 684 { 685 #ifdef USE_FILEMON 686 BuildMon *pbm; 687 688 if (job != NULL) { 689 pbm = &job->bm; 690 } else { 691 pbm = &Mybm; 692 } 693 if (pbm->mfp != NULL) { 694 close(fileno(pbm->mfp)); 695 if (useFilemon) { 696 pid_t pid; 697 698 pid = getpid(); 699 if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) { 700 err(1, "Could not set filemon pid!"); 701 } 702 } 703 } 704 #endif 705 } 706 707 void 708 meta_job_error(Job *job, GNode *gn, int flags, int status) 709 { 710 char cwd[MAXPATHLEN]; 711 BuildMon *pbm; 712 713 if (job != NULL) { 714 pbm = &job->bm; 715 if (!gn) 716 gn = job->node; 717 } else { 718 pbm = &Mybm; 719 } 720 if (pbm->mfp != NULL) { 721 fprintf(pbm->mfp, "*** Error code %d%s\n", 722 status, 723 (flags & JOB_IGNERR) ? 724 "(ignored)" : ""); 725 } 726 if (gn) { 727 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0); 728 } 729 getcwd(cwd, sizeof(cwd)); 730 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0); 731 if (pbm->meta_fname[0]) { 732 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0); 733 } 734 meta_job_finish(job); 735 } 736 737 void 738 meta_job_output(Job *job, char *cp, const char *nl) 739 { 740 BuildMon *pbm; 741 742 if (job != NULL) { 743 pbm = &job->bm; 744 } else { 745 pbm = &Mybm; 746 } 747 if (pbm->mfp != NULL) { 748 if (metaVerbose) { 749 static char *meta_prefix = NULL; 750 static int meta_prefix_len; 751 752 if (!meta_prefix) { 753 char *cp2; 754 755 meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", 756 VAR_GLOBAL, VARF_WANTRES); 757 if ((cp2 = strchr(meta_prefix, '$'))) 758 meta_prefix_len = cp2 - meta_prefix; 759 else 760 meta_prefix_len = strlen(meta_prefix); 761 } 762 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) { 763 cp = strchr(cp+1, '\n'); 764 if (!cp++) 765 return; 766 } 767 } 768 fprintf(pbm->mfp, "%s%s", cp, nl); 769 } 770 } 771 772 int 773 meta_cmd_finish(void *pbmp) 774 { 775 int error = 0; 776 #ifdef USE_FILEMON 777 BuildMon *pbm = pbmp; 778 int x; 779 780 if (!pbm) 781 pbm = &Mybm; 782 783 if (pbm->filemon_fd >= 0) { 784 if (close(pbm->filemon_fd) < 0) 785 error = errno; 786 x = filemon_read(pbm->mfp, pbm->mon_fd); 787 if (error == 0 && x != 0) 788 error = x; 789 pbm->filemon_fd = pbm->mon_fd = -1; 790 } 791 #endif 792 return error; 793 } 794 795 int 796 meta_job_finish(Job *job) 797 { 798 BuildMon *pbm; 799 int error = 0; 800 int x; 801 802 if (job != NULL) { 803 pbm = &job->bm; 804 } else { 805 pbm = &Mybm; 806 } 807 if (pbm->mfp != NULL) { 808 error = meta_cmd_finish(pbm); 809 x = fclose(pbm->mfp); 810 if (error == 0 && x != 0) 811 error = errno; 812 pbm->mfp = NULL; 813 pbm->meta_fname[0] = '\0'; 814 } 815 return error; 816 } 817 818 void 819 meta_finish(void) 820 { 821 Lst_Destroy(metaBailiwick, NULL); 822 free(metaBailiwickStr); 823 Lst_Destroy(metaIgnorePaths, NULL); 824 free(metaIgnorePathsStr); 825 } 826 827 /* 828 * Fetch a full line from fp - growing bufp if needed 829 * Return length in bufp. 830 */ 831 static int 832 fgetLine(char **bufp, size_t *szp, int o, FILE *fp) 833 { 834 char *buf = *bufp; 835 size_t bufsz = *szp; 836 struct stat fs; 837 int x; 838 839 if (fgets(&buf[o], bufsz - o, fp) != NULL) { 840 check_newline: 841 x = o + strlen(&buf[o]); 842 if (buf[x - 1] == '\n') 843 return x; 844 /* 845 * We need to grow the buffer. 846 * The meta file can give us a clue. 847 */ 848 if (fstat(fileno(fp), &fs) == 0) { 849 size_t newsz; 850 char *p; 851 852 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ); 853 if (newsz <= bufsz) 854 newsz = ROUNDUP(fs.st_size, BUFSIZ); 855 if (DEBUG(META)) 856 fprintf(debug_file, "growing buffer %u -> %u\n", 857 (unsigned)bufsz, (unsigned)newsz); 858 p = bmake_realloc(buf, newsz); 859 if (p) { 860 *bufp = buf = p; 861 *szp = bufsz = newsz; 862 /* fetch the rest */ 863 if (!fgets(&buf[x], bufsz - x, fp)) 864 return x; /* truncated! */ 865 goto check_newline; 866 } 867 } 868 } 869 return 0; 870 } 871 872 static int 873 prefix_match(void *p, void *q) 874 { 875 const char *prefix = p; 876 const char *path = q; 877 size_t n = strlen(prefix); 878 879 return (0 == strncmp(path, prefix, n)); 880 } 881 882 static int 883 string_match(const void *p, const void *q) 884 { 885 const char *p1 = p; 886 const char *p2 = q; 887 888 return strcmp(p1, p2); 889 } 890 891 892 /* 893 * When running with 'meta' functionality, a target can be out-of-date 894 * if any of the references in its meta data file is more recent. 895 * We have to track the latestdir on a per-process basis. 896 */ 897 #define LCWD_VNAME_FMT ".meta.%d.lcwd" 898 #define LDIR_VNAME_FMT ".meta.%d.ldir" 899 900 /* 901 * It is possible that a .meta file is corrupted, 902 * if we detect this we want to reproduce it. 903 * Setting oodate TRUE will have that effect. 904 */ 905 #define CHECK_VALID_META(p) if (!(p && *p)) { \ 906 warnx("%s: %d: malformed", fname, lineno); \ 907 oodate = TRUE; \ 908 continue; \ 909 } 910 911 #define DEQUOTE(p) if (*p == '\'') { \ 912 char *ep; \ 913 p++; \ 914 if ((ep = strchr(p, '\''))) \ 915 *ep = '\0'; \ 916 } 917 918 Boolean 919 meta_oodate(GNode *gn, Boolean oodate) 920 { 921 static char *tmpdir = NULL; 922 static char cwd[MAXPATHLEN]; 923 char lcwd_vname[64]; 924 char ldir_vname[64]; 925 char lcwd[MAXPATHLEN]; 926 char latestdir[MAXPATHLEN]; 927 char fname[MAXPATHLEN]; 928 char fname1[MAXPATHLEN]; 929 char fname2[MAXPATHLEN]; 930 char fname3[MAXPATHLEN]; 931 const char *dname; 932 const char *tname; 933 char *p; 934 char *cp; 935 char *link_src; 936 char *move_target; 937 static size_t cwdlen = 0; 938 static size_t tmplen = 0; 939 FILE *fp; 940 Boolean needOODATE = FALSE; 941 Lst missingFiles; 942 char *pa[4]; /* >= possible uses */ 943 int i; 944 int have_filemon = FALSE; 945 946 if (oodate) 947 return oodate; /* we're done */ 948 949 i = 0; 950 951 dname = Var_Value(".OBJDIR", gn, &pa[i++]); 952 tname = Var_Value(TARGET, gn, &pa[i++]); 953 954 /* if this succeeds fname3 is realpath of dname */ 955 if (!meta_needed(gn, dname, tname, fname3, FALSE)) 956 goto oodate_out; 957 dname = fname3; 958 959 missingFiles = Lst_Init(FALSE); 960 961 /* 962 * We need to check if the target is out-of-date. This includes 963 * checking if the expanded command has changed. This in turn 964 * requires that all variables are set in the same way that they 965 * would be if the target needs to be re-built. 966 */ 967 Make_DoAllVar(gn); 968 969 meta_name(gn, fname, sizeof(fname), dname, tname, dname); 970 971 #ifdef DEBUG_META_MODE 972 if (DEBUG(META)) 973 fprintf(debug_file, "meta_oodate: %s\n", fname); 974 #endif 975 976 if ((fp = fopen(fname, "r")) != NULL) { 977 static char *buf = NULL; 978 static size_t bufsz; 979 int lineno = 0; 980 int lastpid = 0; 981 int pid; 982 int x; 983 LstNode ln; 984 struct stat fs; 985 986 if (!buf) { 987 bufsz = 8 * BUFSIZ; 988 buf = bmake_malloc(bufsz); 989 } 990 991 if (!cwdlen) { 992 if (getcwd(cwd, sizeof(cwd)) == NULL) 993 err(1, "Could not get current working directory"); 994 cwdlen = strlen(cwd); 995 } 996 strlcpy(lcwd, cwd, sizeof(lcwd)); 997 strlcpy(latestdir, cwd, sizeof(latestdir)); 998 999 if (!tmpdir) { 1000 tmpdir = getTmpdir(); 1001 tmplen = strlen(tmpdir); 1002 } 1003 1004 /* we want to track all the .meta we read */ 1005 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL); 1006 1007 ln = Lst_First(gn->commands); 1008 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) { 1009 lineno++; 1010 if (buf[x - 1] == '\n') 1011 buf[x - 1] = '\0'; 1012 else { 1013 warnx("%s: %d: line truncated at %u", fname, lineno, x); 1014 oodate = TRUE; 1015 break; 1016 } 1017 link_src = NULL; 1018 move_target = NULL; 1019 /* Find the start of the build monitor section. */ 1020 if (!have_filemon) { 1021 if (strncmp(buf, "-- filemon", 10) == 0) { 1022 have_filemon = TRUE; 1023 continue; 1024 } 1025 if (strncmp(buf, "# buildmon", 10) == 0) { 1026 have_filemon = TRUE; 1027 continue; 1028 } 1029 } 1030 1031 /* Delimit the record type. */ 1032 p = buf; 1033 #ifdef DEBUG_META_MODE 1034 if (DEBUG(META)) 1035 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf); 1036 #endif 1037 strsep(&p, " "); 1038 if (have_filemon) { 1039 /* 1040 * We are in the 'filemon' output section. 1041 * Each record from filemon follows the general form: 1042 * 1043 * <key> <pid> <data> 1044 * 1045 * Where: 1046 * <key> is a single letter, denoting the syscall. 1047 * <pid> is the process that made the syscall. 1048 * <data> is the arguments (of interest). 1049 */ 1050 switch(buf[0]) { 1051 case '#': /* comment */ 1052 case 'V': /* version */ 1053 break; 1054 default: 1055 /* 1056 * We need to track pathnames per-process. 1057 * 1058 * Each process run by make, starts off in the 'CWD' 1059 * recorded in the .meta file, if it chdirs ('C') 1060 * elsewhere we need to track that - but only for 1061 * that process. If it forks ('F'), we initialize 1062 * the child to have the same cwd as its parent. 1063 * 1064 * We also need to track the 'latestdir' of 1065 * interest. This is usually the same as cwd, but 1066 * not if a process is reading directories. 1067 * 1068 * Each time we spot a different process ('pid') 1069 * we save the current value of 'latestdir' in a 1070 * variable qualified by 'lastpid', and 1071 * re-initialize 'latestdir' to any pre-saved 1072 * value for the current 'pid' and 'CWD' if none. 1073 */ 1074 CHECK_VALID_META(p); 1075 pid = atoi(p); 1076 if (pid > 0 && pid != lastpid) { 1077 char *ldir; 1078 char *tp; 1079 1080 if (lastpid > 0) { 1081 /* We need to remember these. */ 1082 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL, 0); 1083 Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0); 1084 } 1085 snprintf(lcwd_vname, sizeof(lcwd_vname), LCWD_VNAME_FMT, pid); 1086 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid); 1087 lastpid = pid; 1088 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp); 1089 if (ldir) { 1090 strlcpy(latestdir, ldir, sizeof(latestdir)); 1091 free(tp); 1092 } 1093 ldir = Var_Value(lcwd_vname, VAR_GLOBAL, &tp); 1094 if (ldir) { 1095 strlcpy(lcwd, ldir, sizeof(lcwd)); 1096 free(tp); 1097 } 1098 } 1099 /* Skip past the pid. */ 1100 if (strsep(&p, " ") == NULL) 1101 continue; 1102 #ifdef DEBUG_META_MODE 1103 if (DEBUG(META)) 1104 fprintf(debug_file, "%s: %d: %d: %c: cwd=%s lcwd=%s ldir=%s\n", 1105 fname, lineno, 1106 pid, buf[0], cwd, lcwd, latestdir); 1107 #endif 1108 break; 1109 } 1110 1111 CHECK_VALID_META(p); 1112 1113 /* Process according to record type. */ 1114 switch (buf[0]) { 1115 case 'X': /* eXit */ 1116 Var_Delete(lcwd_vname, VAR_GLOBAL); 1117 Var_Delete(ldir_vname, VAR_GLOBAL); 1118 lastpid = 0; /* no need to save ldir_vname */ 1119 break; 1120 1121 case 'F': /* [v]Fork */ 1122 { 1123 char cldir[64]; 1124 int child; 1125 1126 child = atoi(p); 1127 if (child > 0) { 1128 snprintf(cldir, sizeof(cldir), LCWD_VNAME_FMT, child); 1129 Var_Set(cldir, lcwd, VAR_GLOBAL, 0); 1130 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child); 1131 Var_Set(cldir, latestdir, VAR_GLOBAL, 0); 1132 #ifdef DEBUG_META_MODE 1133 if (DEBUG(META)) 1134 fprintf(debug_file, "%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n", 1135 fname, lineno, 1136 child, cwd, lcwd, latestdir); 1137 #endif 1138 } 1139 } 1140 break; 1141 1142 case 'C': /* Chdir */ 1143 /* Update lcwd and latest directory. */ 1144 strlcpy(latestdir, p, sizeof(latestdir)); 1145 strlcpy(lcwd, p, sizeof(lcwd)); 1146 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL, 0); 1147 Var_Set(ldir_vname, lcwd, VAR_GLOBAL, 0); 1148 #ifdef DEBUG_META_MODE 1149 if (DEBUG(META)) 1150 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, lcwd); 1151 #endif 1152 break; 1153 1154 case 'M': /* renaMe */ 1155 /* 1156 * For 'M'oves we want to check 1157 * the src as for 'R'ead 1158 * and the target as for 'W'rite. 1159 */ 1160 cp = p; /* save this for a second */ 1161 /* now get target */ 1162 if (strsep(&p, " ") == NULL) 1163 continue; 1164 CHECK_VALID_META(p); 1165 move_target = p; 1166 p = cp; 1167 /* 'L' and 'M' put single quotes around the args */ 1168 DEQUOTE(p); 1169 DEQUOTE(move_target); 1170 /* FALLTHROUGH */ 1171 case 'D': /* unlink */ 1172 if (*p == '/' && !Lst_IsEmpty(missingFiles)) { 1173 /* remove p from the missingFiles list if present */ 1174 if ((ln = Lst_Find(missingFiles, p, string_match)) != NULL) { 1175 char *tp = Lst_Datum(ln); 1176 Lst_Remove(missingFiles, ln); 1177 free(tp); 1178 ln = NULL; /* we're done with it */ 1179 } 1180 } 1181 if (buf[0] == 'M') { 1182 /* the target of the mv is a file 'W'ritten */ 1183 #ifdef DEBUG_META_MODE 1184 if (DEBUG(META)) 1185 fprintf(debug_file, "meta_oodate: M %s -> %s\n", 1186 p, move_target); 1187 #endif 1188 p = move_target; 1189 goto check_write; 1190 } 1191 break; 1192 case 'L': /* Link */ 1193 /* 1194 * For 'L'inks check 1195 * the src as for 'R'ead 1196 * and the target as for 'W'rite. 1197 */ 1198 link_src = p; 1199 /* now get target */ 1200 if (strsep(&p, " ") == NULL) 1201 continue; 1202 CHECK_VALID_META(p); 1203 /* 'L' and 'M' put single quotes around the args */ 1204 DEQUOTE(p); 1205 DEQUOTE(link_src); 1206 #ifdef DEBUG_META_MODE 1207 if (DEBUG(META)) 1208 fprintf(debug_file, "meta_oodate: L %s -> %s\n", 1209 link_src, p); 1210 #endif 1211 /* FALLTHROUGH */ 1212 case 'W': /* Write */ 1213 check_write: 1214 /* 1215 * If a file we generated within our bailiwick 1216 * but outside of .OBJDIR is missing, 1217 * we need to do it again. 1218 */ 1219 /* ignore non-absolute paths */ 1220 if (*p != '/') 1221 break; 1222 1223 if (Lst_IsEmpty(metaBailiwick)) 1224 break; 1225 1226 /* ignore cwd - normal dependencies handle those */ 1227 if (strncmp(p, cwd, cwdlen) == 0) 1228 break; 1229 1230 if (!Lst_ForEach(metaBailiwick, prefix_match, p)) 1231 break; 1232 1233 /* tmpdir might be within */ 1234 if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0) 1235 break; 1236 1237 /* ignore anything containing the string "tmp" */ 1238 if ((strstr("tmp", p))) 1239 break; 1240 1241 if ((link_src != NULL && cached_lstat(p, &fs) < 0) || 1242 (link_src == NULL && cached_stat(p, &fs) < 0)) { 1243 if (Lst_Find(missingFiles, p, string_match) == NULL) 1244 Lst_AtEnd(missingFiles, bmake_strdup(p)); 1245 } 1246 break; 1247 check_link_src: 1248 p = link_src; 1249 link_src = NULL; 1250 #ifdef DEBUG_META_MODE 1251 if (DEBUG(META)) 1252 fprintf(debug_file, "meta_oodate: L src %s\n", p); 1253 #endif 1254 /* FALLTHROUGH */ 1255 case 'R': /* Read */ 1256 case 'E': /* Exec */ 1257 /* 1258 * Check for runtime files that can't 1259 * be part of the dependencies because 1260 * they are _expected_ to change. 1261 */ 1262 if (*p == '/') { 1263 cached_realpath(p, fname1); /* clean it up */ 1264 if (Lst_ForEach(metaIgnorePaths, prefix_match, fname1)) { 1265 #ifdef DEBUG_META_MODE 1266 if (DEBUG(META)) 1267 fprintf(debug_file, "meta_oodate: ignoring path: %s\n", 1268 p); 1269 #endif 1270 break; 1271 } 1272 } 1273 1274 if (metaIgnorePatterns) { 1275 char *pm; 1276 1277 snprintf(fname1, sizeof(fname1), 1278 "${%s:@m@${%s:L:M$m}@}", 1279 MAKE_META_IGNORE_PATTERNS, p); 1280 pm = Var_Subst(NULL, fname1, gn, VARF_WANTRES); 1281 if (*pm) { 1282 #ifdef DEBUG_META_MODE 1283 if (DEBUG(META)) 1284 fprintf(debug_file, "meta_oodate: ignoring pattern: %s\n", 1285 p); 1286 #endif 1287 free(pm); 1288 break; 1289 } 1290 free(pm); 1291 } 1292 1293 /* 1294 * The rest of the record is the file name. 1295 * Check if it's not an absolute path. 1296 */ 1297 { 1298 char *sdirs[4]; 1299 char **sdp; 1300 int sdx = 0; 1301 int found = 0; 1302 1303 if (*p == '/') { 1304 sdirs[sdx++] = p; /* done */ 1305 } else { 1306 if (strcmp(".", p) == 0) 1307 continue; /* no point */ 1308 1309 /* Check vs latestdir */ 1310 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p); 1311 sdirs[sdx++] = fname1; 1312 1313 if (strcmp(latestdir, lcwd) != 0) { 1314 /* Check vs lcwd */ 1315 snprintf(fname2, sizeof(fname2), "%s/%s", lcwd, p); 1316 sdirs[sdx++] = fname2; 1317 } 1318 if (strcmp(lcwd, cwd) != 0) { 1319 /* Check vs cwd */ 1320 snprintf(fname3, sizeof(fname3), "%s/%s", cwd, p); 1321 sdirs[sdx++] = fname3; 1322 } 1323 } 1324 sdirs[sdx++] = NULL; 1325 1326 for (sdp = sdirs; *sdp && !found; sdp++) { 1327 #ifdef DEBUG_META_MODE 1328 if (DEBUG(META)) 1329 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp); 1330 #endif 1331 if (cached_stat(*sdp, &fs) == 0) { 1332 found = 1; 1333 p = *sdp; 1334 } 1335 } 1336 if (found) { 1337 #ifdef DEBUG_META_MODE 1338 if (DEBUG(META)) 1339 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p); 1340 #endif 1341 if (!S_ISDIR(fs.st_mode) && 1342 fs.st_mtime > gn->mtime) { 1343 if (DEBUG(META)) 1344 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p); 1345 oodate = TRUE; 1346 } else if (S_ISDIR(fs.st_mode)) { 1347 /* Update the latest directory. */ 1348 cached_realpath(p, latestdir); 1349 } 1350 } else if (errno == ENOENT && *p == '/' && 1351 strncmp(p, cwd, cwdlen) != 0) { 1352 /* 1353 * A referenced file outside of CWD is missing. 1354 * We cannot catch every eventuality here... 1355 */ 1356 if (Lst_Find(missingFiles, p, string_match) == NULL) 1357 Lst_AtEnd(missingFiles, bmake_strdup(p)); 1358 } 1359 } 1360 if (buf[0] == 'E') { 1361 /* previous latestdir is no longer relevant */ 1362 strlcpy(latestdir, lcwd, sizeof(latestdir)); 1363 } 1364 break; 1365 default: 1366 break; 1367 } 1368 if (!oodate && buf[0] == 'L' && link_src != NULL) 1369 goto check_link_src; 1370 } else if (strcmp(buf, "CMD") == 0) { 1371 /* 1372 * Compare the current command with the one in the 1373 * meta data file. 1374 */ 1375 if (ln == NULL) { 1376 if (DEBUG(META)) 1377 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno); 1378 oodate = TRUE; 1379 } else { 1380 char *cmd = (char *)Lst_Datum(ln); 1381 Boolean hasOODATE = FALSE; 1382 1383 if (strstr(cmd, "$?")) 1384 hasOODATE = TRUE; 1385 else if ((cp = strstr(cmd, ".OODATE"))) { 1386 /* check for $[{(].OODATE[:)}] */ 1387 if (cp > cmd + 2 && cp[-2] == '$') 1388 hasOODATE = TRUE; 1389 } 1390 if (hasOODATE) { 1391 needOODATE = TRUE; 1392 if (DEBUG(META)) 1393 fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno); 1394 } 1395 cmd = Var_Subst(NULL, cmd, gn, VARF_WANTRES|VARF_UNDEFERR); 1396 1397 if ((cp = strchr(cmd, '\n'))) { 1398 int n; 1399 1400 /* 1401 * This command contains newlines, we need to 1402 * fetch more from the .meta file before we 1403 * attempt a comparison. 1404 */ 1405 /* first put the newline back at buf[x - 1] */ 1406 buf[x - 1] = '\n'; 1407 do { 1408 /* now fetch the next line */ 1409 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0) 1410 break; 1411 x = n; 1412 lineno++; 1413 if (buf[x - 1] != '\n') { 1414 warnx("%s: %d: line truncated at %u", fname, lineno, x); 1415 break; 1416 } 1417 cp = strchr(++cp, '\n'); 1418 } while (cp); 1419 if (buf[x - 1] == '\n') 1420 buf[x - 1] = '\0'; 1421 } 1422 if (!hasOODATE && 1423 !(gn->type & OP_NOMETA_CMP) && 1424 strcmp(p, cmd) != 0) { 1425 if (DEBUG(META)) 1426 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd); 1427 if (!metaIgnoreCMDs) 1428 oodate = TRUE; 1429 } 1430 free(cmd); 1431 ln = Lst_Succ(ln); 1432 } 1433 } else if (strcmp(buf, "CWD") == 0) { 1434 /* 1435 * Check if there are extra commands now 1436 * that weren't in the meta data file. 1437 */ 1438 if (!oodate && ln != NULL) { 1439 if (DEBUG(META)) 1440 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno); 1441 oodate = TRUE; 1442 } 1443 if (strcmp(p, cwd) != 0) { 1444 if (DEBUG(META)) 1445 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir); 1446 oodate = TRUE; 1447 } 1448 } 1449 } 1450 1451 fclose(fp); 1452 if (!Lst_IsEmpty(missingFiles)) { 1453 if (DEBUG(META)) 1454 fprintf(debug_file, "%s: missing files: %s...\n", 1455 fname, (char *)Lst_Datum(Lst_First(missingFiles))); 1456 oodate = TRUE; 1457 } 1458 if (!oodate && !have_filemon && filemonMissing) { 1459 if (DEBUG(META)) 1460 fprintf(debug_file, "%s: missing filemon data\n", fname); 1461 oodate = TRUE; 1462 } 1463 } else { 1464 if (writeMeta && metaMissing) { 1465 cp = NULL; 1466 1467 /* if target is in .CURDIR we do not need a meta file */ 1468 if (gn->path && (cp = strrchr(gn->path, '/')) && cp > gn->path) { 1469 if (strncmp(curdir, gn->path, (cp - gn->path)) != 0) { 1470 cp = NULL; /* not in .CURDIR */ 1471 } 1472 } 1473 if (!cp) { 1474 if (DEBUG(META)) 1475 fprintf(debug_file, "%s: required but missing\n", fname); 1476 oodate = TRUE; 1477 needOODATE = TRUE; /* assume the worst */ 1478 } 1479 } 1480 } 1481 1482 Lst_Destroy(missingFiles, (FreeProc *)free); 1483 1484 if (oodate && needOODATE) { 1485 /* 1486 * Target uses .OODATE which is empty; or we wouldn't be here. 1487 * We have decided it is oodate, so .OODATE needs to be set. 1488 * All we can sanely do is set it to .ALLSRC. 1489 */ 1490 Var_Delete(OODATE, gn); 1491 Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn, 0); 1492 free(cp); 1493 } 1494 1495 oodate_out: 1496 for (i--; i >= 0; i--) { 1497 free(pa[i]); 1498 } 1499 return oodate; 1500 } 1501 1502 /* support for compat mode */ 1503 1504 static int childPipe[2]; 1505 1506 void 1507 meta_compat_start(void) 1508 { 1509 #ifdef USE_FILEMON_ONCE 1510 /* 1511 * We need to re-open filemon for each cmd. 1512 */ 1513 BuildMon *pbm = &Mybm; 1514 1515 if (pbm->mfp != NULL && useFilemon) { 1516 filemon_open(pbm); 1517 } else { 1518 pbm->mon_fd = pbm->filemon_fd = -1; 1519 } 1520 #endif 1521 if (pipe(childPipe) < 0) 1522 Punt("Cannot create pipe: %s", strerror(errno)); 1523 /* Set close-on-exec flag for both */ 1524 (void)fcntl(childPipe[0], F_SETFD, FD_CLOEXEC); 1525 (void)fcntl(childPipe[1], F_SETFD, FD_CLOEXEC); 1526 } 1527 1528 void 1529 meta_compat_child(void) 1530 { 1531 meta_job_child(NULL); 1532 if (dup2(childPipe[1], 1) < 0 || 1533 dup2(1, 2) < 0) { 1534 execError("dup2", "pipe"); 1535 _exit(1); 1536 } 1537 } 1538 1539 void 1540 meta_compat_parent(void) 1541 { 1542 FILE *fp; 1543 char buf[BUFSIZ]; 1544 1545 close(childPipe[1]); /* child side */ 1546 fp = fdopen(childPipe[0], "r"); 1547 while (fgets(buf, sizeof(buf), fp)) { 1548 meta_job_output(NULL, buf, ""); 1549 printf("%s", buf); 1550 (void)fflush(stdout); 1551 } 1552 fclose(fp); 1553 } 1554 1555 #endif /* USE_META */ 1556