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