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