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