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