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