1 /* 2 * Implement 'meta' mode. 3 * Adapted from John Birrell's patches to FreeBSD make. 4 * --sjg 5 */ 6 /* 7 * Copyright (c) 2009-2010, Juniper Networks, Inc. 8 * Portions Copyright (c) 2009, John Birrell. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #if defined(USE_META) 32 33 #ifdef HAVE_CONFIG_H 34 # include "config.h" 35 #endif 36 #include <sys/stat.h> 37 #include <sys/ioctl.h> 38 #include <fcntl.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 "job.h" 47 48 #ifdef HAVE_FILEMON_H 49 # include <filemon.h> 50 #endif 51 #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD) 52 # define USE_FILEMON 53 #endif 54 55 static BuildMon Mybm; /* for compat */ 56 57 Boolean useMeta = FALSE; 58 static Boolean useFilemon = FALSE; 59 static Boolean writeMeta = FALSE; 60 static Boolean metaEnv = FALSE; /* don't save env unless asked */ 61 static Boolean metaVerbose = FALSE; 62 static Boolean metaIgnoreCMDs = FALSE; /* ignore CMDs in .meta files */ 63 64 extern Boolean forceJobs; 65 extern Boolean comatMake; 66 67 #define MAKE_META_PREFIX ".MAKE.META.PREFIX" 68 69 #ifndef N2U 70 # define N2U(n, u) (((n) + ((u) - 1)) / (u)) 71 #endif 72 #ifndef ROUNDUP 73 # define ROUNDUP(n, u) (N2U((n), (u)) * (u)) 74 #endif 75 76 #if !defined(HAVE_STRSEP) 77 # define strsep(s, d) stresep((s), (d), 0) 78 #endif 79 80 /* 81 * Filemon is a kernel module which snoops certain syscalls. 82 * 83 * C chdir 84 * E exec 85 * F [v]fork 86 * L [sym]link 87 * M rename 88 * R read 89 * W write 90 * S stat 91 * 92 * See meta_oodate below - we mainly care about 'E' and 'R'. 93 * 94 * We can still use meta mode without filemon, but 95 * the benefits are more limited. 96 */ 97 #ifdef USE_FILEMON 98 # ifndef _PATH_FILEMON 99 # define _PATH_FILEMON "/dev/filemon" 100 # endif 101 102 /* 103 * Open the filemon device. 104 */ 105 static void 106 filemon_open(BuildMon *pbm) 107 { 108 int retry; 109 110 pbm->mon_fd = pbm->filemon_fd = -1; 111 if (!useFilemon) 112 return; 113 114 for (retry = 5; retry >= 0; retry--) { 115 if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0) 116 break; 117 } 118 119 if (pbm->filemon_fd < 0) { 120 useFilemon = FALSE; 121 warn("Could not open %s", _PATH_FILEMON); 122 return; 123 } 124 125 /* 126 * We use a file outside of '.' 127 * to avoid a FreeBSD kernel bug where unlink invalidates 128 * cwd causing getcwd to do a lot more work. 129 * We only care about the descriptor. 130 */ 131 pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL); 132 if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) { 133 err(1, "Could not set filemon file descriptor!"); 134 } 135 /* we don't need these once we exec */ 136 (void)fcntl(pbm->mon_fd, F_SETFD, 1); 137 (void)fcntl(pbm->filemon_fd, F_SETFD, 1); 138 } 139 140 /* 141 * Read the build monitor output file and write records to the target's 142 * metadata file. 143 */ 144 static void 145 filemon_read(FILE *mfp, int fd) 146 { 147 FILE *fp; 148 char buf[BUFSIZ]; 149 150 /* Check if we're not writing to a meta data file.*/ 151 if (mfp == NULL) { 152 if (fd >= 0) 153 close(fd); /* not interested */ 154 return; 155 } 156 /* rewind */ 157 lseek(fd, SEEK_SET, 0); 158 if ((fp = fdopen(fd, "r")) == NULL) 159 err(1, "Could not read build monitor file '%d'", fd); 160 161 fprintf(mfp, "-- filemon acquired metadata --\n"); 162 163 while (fgets(buf, sizeof(buf), fp)) { 164 fprintf(mfp, "%s", buf); 165 } 166 fflush(mfp); 167 clearerr(fp); 168 fclose(fp); 169 } 170 #endif 171 172 /* 173 * when realpath() fails, 174 * we use this, to clean up ./ and ../ 175 */ 176 static void 177 eat_dots(char *buf, size_t bufsz, int dots) 178 { 179 char *cp; 180 char *cp2; 181 const char *eat; 182 size_t eatlen; 183 184 switch (dots) { 185 case 1: 186 eat = "/./"; 187 eatlen = 2; 188 break; 189 case 2: 190 eat = "/../"; 191 eatlen = 3; 192 break; 193 default: 194 return; 195 } 196 197 do { 198 cp = strstr(buf, eat); 199 if (cp) { 200 cp2 = cp + eatlen; 201 if (dots == 2 && cp > buf) { 202 do { 203 cp--; 204 } while (cp > buf && *cp != '/'); 205 } 206 if (*cp == '/') { 207 strlcpy(cp, cp2, bufsz - (cp - buf)); 208 } else { 209 return; /* can't happen? */ 210 } 211 } 212 } while (cp); 213 } 214 215 static char * 216 meta_name(struct GNode *gn, char *mname, size_t mnamelen, 217 const char *dname, 218 const char *tname) 219 { 220 char buf[MAXPATHLEN]; 221 char cwd[MAXPATHLEN]; 222 char *rp; 223 char *cp; 224 char *tp; 225 char *p[4]; /* >= number of possible uses */ 226 int i; 227 228 i = 0; 229 if (!dname) 230 dname = Var_Value(".OBJDIR", gn, &p[i++]); 231 if (!tname) 232 tname = Var_Value(TARGET, gn, &p[i++]); 233 234 if (realpath(dname, cwd)) 235 dname = cwd; 236 237 /* 238 * Weed out relative paths from the target file name. 239 * We have to be careful though since if target is a 240 * symlink, the result will be unstable. 241 * So we use realpath() just to get the dirname, and leave the 242 * basename as given to us. 243 */ 244 if ((cp = strrchr(tname, '/'))) { 245 if (realpath(tname, buf)) { 246 if ((rp = strrchr(buf, '/'))) { 247 rp++; 248 cp++; 249 if (strcmp(cp, rp) != 0) 250 strlcpy(rp, cp, sizeof(buf) - (rp - buf)); 251 } 252 tname = buf; 253 } else { 254 /* 255 * We likely have a directory which is about to be made. 256 * We pretend realpath() succeeded, to have a chance 257 * of generating the same meta file name that we will 258 * next time through. 259 */ 260 if (tname[0] == '/') { 261 strlcpy(buf, tname, sizeof(buf)); 262 } else { 263 snprintf(buf, sizeof(buf), "%s/%s", cwd, tname); 264 } 265 eat_dots(buf, sizeof(buf), 1); /* ./ */ 266 eat_dots(buf, sizeof(buf), 2); /* ../ */ 267 tname = buf; 268 } 269 } 270 /* on some systems dirname may modify its arg */ 271 tp = bmake_strdup(tname); 272 if (strcmp(dname, dirname(tp)) == 0) 273 snprintf(mname, mnamelen, "%s.meta", tname); 274 else { 275 snprintf(mname, mnamelen, "%s/%s.meta", dname, tname); 276 277 /* 278 * Replace path separators in the file name after the 279 * current object directory path. 280 */ 281 cp = mname + strlen(dname) + 1; 282 283 while (*cp != '\0') { 284 if (*cp == '/') 285 *cp = '_'; 286 cp++; 287 } 288 } 289 free(tp); 290 for (i--; i >= 0; i--) { 291 if (p[i]) 292 free(p[i]); 293 } 294 return (mname); 295 } 296 297 /* 298 * Return true if running ${.MAKE} 299 * Bypassed if target is flagged .MAKE 300 */ 301 static int 302 is_submake(void *cmdp, void *gnp) 303 { 304 static char *p_make = NULL; 305 static int p_len; 306 char *cmd = cmdp; 307 GNode *gn = gnp; 308 char *mp = NULL; 309 char *cp; 310 char *cp2; 311 int rc = 0; /* keep looking */ 312 313 if (!p_make) { 314 p_make = Var_Value(".MAKE", gn, &cp); 315 p_len = strlen(p_make); 316 } 317 cp = strchr(cmd, '$'); 318 if ((cp)) { 319 mp = Var_Subst(NULL, cmd, gn, FALSE); 320 cmd = mp; 321 } 322 cp2 = strstr(cmd, p_make); 323 if ((cp2)) { 324 switch (cp2[p_len]) { 325 case '\0': 326 case ' ': 327 case '\t': 328 case '\n': 329 rc = 1; 330 break; 331 } 332 if (cp2 > cmd && rc > 0) { 333 switch (cp2[-1]) { 334 case ' ': 335 case '\t': 336 case '\n': 337 break; 338 default: 339 rc = 0; /* no match */ 340 break; 341 } 342 } 343 } 344 if (mp) 345 free(mp); 346 return (rc); 347 } 348 349 typedef struct meta_file_s { 350 FILE *fp; 351 GNode *gn; 352 } meta_file_t; 353 354 static int 355 printCMD(void *cmdp, void *mfpp) 356 { 357 meta_file_t *mfp = mfpp; 358 char *cmd = cmdp; 359 char *cp = NULL; 360 361 if (strchr(cmd, '$')) { 362 cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE); 363 } 364 fprintf(mfp->fp, "CMD %s\n", cmd); 365 if (cp) 366 free(cp); 367 return 0; 368 } 369 370 /* 371 * Certain node types never get a .meta file 372 */ 373 #define SKIP_META_TYPE(_type) do { \ 374 if ((gn->type & __CONCAT(OP_, _type))) { \ 375 if (DEBUG(META)) { \ 376 fprintf(debug_file, "Skipping meta for %s: .%s\n", \ 377 gn->name, __STRING(_type)); \ 378 } \ 379 return (NULL); \ 380 } \ 381 } while (0) 382 383 static FILE * 384 meta_create(BuildMon *pbm, GNode *gn) 385 { 386 extern char **environ; 387 meta_file_t mf; 388 char buf[MAXPATHLEN]; 389 char curdir[MAXPATHLEN]; 390 char objdir[MAXPATHLEN]; 391 char **ptr; 392 const char *cname; 393 const char *dname; 394 const char *tname; 395 char *fname; 396 const char *cp; 397 char *p[4]; /* >= possible uses */ 398 int i; 399 struct stat fs; 400 401 402 /* This may be a phony node which we don't want meta data for... */ 403 /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */ 404 /* Or it may be explicitly flagged as .NOMETA */ 405 SKIP_META_TYPE(NOMETA); 406 /* Unless it is explicitly flagged as .META */ 407 if (!(gn->type & OP_META)) { 408 SKIP_META_TYPE(PHONY); 409 SKIP_META_TYPE(SPECIAL); 410 SKIP_META_TYPE(MAKE); 411 } 412 413 mf.fp = NULL; 414 415 i = 0; 416 417 dname = Var_Value(".OBJDIR", gn, &p[i++]); 418 cname = Var_Value(".CURDIR", gn, &p[i++]); 419 tname = Var_Value(TARGET, gn, &p[i++]); 420 421 /* The object directory may not exist. Check it.. */ 422 if (stat(dname, &fs) != 0) { 423 if (DEBUG(META)) 424 fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n", 425 gn->name); 426 goto out; 427 } 428 /* Check if there are no commands to execute. */ 429 if (Lst_IsEmpty(gn->commands)) { 430 if (DEBUG(META)) 431 fprintf(debug_file, "Skipping meta for %s: no commands\n", 432 gn->name); 433 goto out; 434 } 435 436 /* make sure these are canonical */ 437 if (realpath(dname, objdir)) 438 dname = objdir; 439 if (realpath(cname, curdir)) 440 cname = curdir; 441 442 /* If we aren't in the object directory, don't create a meta file. */ 443 if (strcmp(cname, dname) == 0) { 444 if (DEBUG(META)) 445 fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n", 446 gn->name); 447 goto out; 448 } 449 if (!(gn->type & OP_META)) { 450 /* We do not generate .meta files for sub-makes */ 451 if (Lst_ForEach(gn->commands, is_submake, gn)) { 452 if (DEBUG(META)) 453 fprintf(debug_file, "Skipping meta for %s: .MAKE\n", 454 gn->name); 455 goto out; 456 } 457 } 458 459 if (metaVerbose) { 460 char *mp; 461 462 /* Describe the target we are building */ 463 mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, 0); 464 if (*mp) 465 fprintf(stdout, "%s\n", mp); 466 free(mp); 467 } 468 /* Get the basename of the target */ 469 if ((cp = strrchr(tname, '/')) == NULL) { 470 cp = tname; 471 } else { 472 cp++; 473 } 474 475 fflush(stdout); 476 477 if (strcmp(cp, makeDependfile) == 0) 478 goto out; 479 480 if (!writeMeta) 481 /* Don't create meta data. */ 482 goto out; 483 484 fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname), 485 dname, tname); 486 487 #ifdef DEBUG_META_MODE 488 if (DEBUG(META)) 489 fprintf(debug_file, "meta_create: %s\n", fname); 490 #endif 491 492 if ((mf.fp = fopen(fname, "w")) == NULL) 493 err(1, "Could not open meta file '%s'", fname); 494 495 fprintf(mf.fp, "# Meta data file %s\n", fname); 496 497 mf.gn = gn; 498 499 Lst_ForEach(gn->commands, printCMD, &mf); 500 501 fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf))); 502 fprintf(mf.fp, "TARGET %s\n", tname); 503 504 if (metaEnv) { 505 for (ptr = environ; *ptr != NULL; ptr++) 506 fprintf(mf.fp, "ENV %s\n", *ptr); 507 } 508 509 fprintf(mf.fp, "-- command output --\n"); 510 fflush(mf.fp); 511 512 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL); 513 Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL); 514 515 out: 516 for (i--; i >= 0; i--) { 517 if (p[i]) 518 free(p[i]); 519 } 520 521 return (mf.fp); 522 } 523 524 525 void 526 meta_init(const char *make_mode) 527 { 528 static int once = 0; 529 530 useMeta = TRUE; 531 useFilemon = TRUE; 532 writeMeta = TRUE; 533 534 if (make_mode) { 535 if (strstr(make_mode, "env")) 536 metaEnv = TRUE; 537 if (strstr(make_mode, "verb")) 538 metaVerbose = TRUE; 539 if (strstr(make_mode, "read")) 540 writeMeta = FALSE; 541 if (strstr(make_mode, "nofilemon")) 542 useFilemon = FALSE; 543 if (strstr(make_mode, "ignore-cmd")) 544 metaIgnoreCMDs = TRUE; 545 /* for backwards compatability */ 546 Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0); 547 Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0); 548 } 549 if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) { 550 /* 551 * The default value for MAKE_META_PREFIX 552 * prints the absolute path of the target. 553 * This works be cause :H will generate '.' if there is no / 554 * and :tA will resolve that to cwd. 555 */ 556 Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0); 557 } 558 if (once) 559 return; 560 once = 1; 561 memset(&Mybm, 0, sizeof(Mybm)); 562 } 563 564 /* 565 * In each case below we allow for job==NULL 566 */ 567 void 568 meta_job_start(Job *job, GNode *gn) 569 { 570 BuildMon *pbm; 571 572 if (job != NULL) { 573 pbm = &job->bm; 574 } else { 575 pbm = &Mybm; 576 } 577 pbm->mfp = meta_create(pbm, gn); 578 #ifdef USE_FILEMON_ONCE 579 /* compat mode we open the filemon dev once per command */ 580 if (job == NULL) 581 return; 582 #endif 583 #ifdef USE_FILEMON 584 if (pbm->mfp != NULL && useFilemon) { 585 filemon_open(pbm); 586 } else { 587 pbm->mon_fd = pbm->filemon_fd = -1; 588 } 589 #endif 590 } 591 592 /* 593 * The child calls this before doing anything. 594 * It does not disturb our state. 595 */ 596 void 597 meta_job_child(Job *job) 598 { 599 #ifdef USE_FILEMON 600 BuildMon *pbm; 601 pid_t pid; 602 603 if (job != NULL) { 604 pbm = &job->bm; 605 } else { 606 pbm = &Mybm; 607 } 608 pid = getpid(); 609 if (pbm->mfp != NULL && useFilemon) { 610 if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) { 611 err(1, "Could not set filemon pid!"); 612 } 613 } 614 #endif 615 } 616 617 void 618 meta_job_error(Job *job, GNode *gn, int flags, int status) 619 { 620 char cwd[MAXPATHLEN]; 621 BuildMon *pbm; 622 623 if (job != NULL) { 624 pbm = &job->bm; 625 } else { 626 if (!gn) 627 gn = job->node; 628 pbm = &Mybm; 629 } 630 if (pbm->mfp != NULL) { 631 fprintf(pbm->mfp, "*** Error code %d%s\n", 632 status, 633 (flags & JOB_IGNERR) ? 634 "(ignored)" : ""); 635 } 636 if (gn) { 637 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0); 638 } 639 getcwd(cwd, sizeof(cwd)); 640 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0); 641 if (pbm && pbm->meta_fname[0]) { 642 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0); 643 } 644 } 645 646 void 647 meta_job_output(Job *job, char *cp, const char *nl) 648 { 649 BuildMon *pbm; 650 651 if (job != NULL) { 652 pbm = &job->bm; 653 } else { 654 pbm = &Mybm; 655 } 656 if (pbm->mfp != NULL) { 657 if (metaVerbose) { 658 static char *meta_prefix = NULL; 659 static int meta_prefix_len; 660 661 if (!meta_prefix) { 662 char *cp2; 663 664 meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0); 665 if ((cp2 = strchr(meta_prefix, '$'))) 666 meta_prefix_len = cp2 - meta_prefix; 667 else 668 meta_prefix_len = strlen(meta_prefix); 669 } 670 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) { 671 cp = strchr(cp+1, '\n'); 672 if (!cp++) 673 return; 674 } 675 } 676 fprintf(pbm->mfp, "%s%s", cp, nl); 677 } 678 } 679 680 void 681 meta_cmd_finish(void *pbmp) 682 { 683 #ifdef USE_FILEMON 684 BuildMon *pbm = pbmp; 685 686 if (!pbm) 687 pbm = &Mybm; 688 689 if (pbm->filemon_fd >= 0) { 690 close(pbm->filemon_fd); 691 filemon_read(pbm->mfp, pbm->mon_fd); 692 pbm->filemon_fd = pbm->mon_fd = -1; 693 } 694 #endif 695 } 696 697 void 698 meta_job_finish(Job *job) 699 { 700 BuildMon *pbm; 701 702 if (job != NULL) { 703 pbm = &job->bm; 704 } else { 705 pbm = &Mybm; 706 } 707 if (pbm->mfp != NULL) { 708 meta_cmd_finish(pbm); 709 fclose(pbm->mfp); 710 pbm->mfp = NULL; 711 pbm->meta_fname[0] = '\0'; 712 } 713 } 714 715 /* 716 * Fetch a full line from fp - growing bufp if needed 717 * Return length in bufp. 718 */ 719 static int 720 fgetLine(char **bufp, size_t *szp, int o, FILE *fp) 721 { 722 char *buf = *bufp; 723 size_t bufsz = *szp; 724 struct stat fs; 725 int x; 726 727 if (fgets(&buf[o], bufsz - o, fp) != NULL) { 728 check_newline: 729 x = o + strlen(&buf[o]); 730 if (buf[x - 1] == '\n') 731 return x; 732 /* 733 * We need to grow the buffer. 734 * The meta file can give us a clue. 735 */ 736 if (fstat(fileno(fp), &fs) == 0) { 737 size_t newsz; 738 char *p; 739 740 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ); 741 if (newsz <= bufsz) 742 newsz = ROUNDUP(fs.st_size, BUFSIZ); 743 if (DEBUG(META)) 744 fprintf(debug_file, "growing buffer %u -> %u\n", 745 bufsz, newsz); 746 p = bmake_realloc(buf, newsz); 747 if (p) { 748 *bufp = buf = p; 749 *szp = bufsz = newsz; 750 /* fetch the rest */ 751 if (!fgets(&buf[x], bufsz - x, fp)) 752 return x; /* truncated! */ 753 goto check_newline; 754 } 755 } 756 } 757 return 0; 758 } 759 760 /* 761 * When running with 'meta' functionality, a target can be out-of-date 762 * if any of the references in it's meta data file is more recent. 763 * We have to track the latestdir on a per-process basis. 764 */ 765 #define LDIR_VNAME_FMT ".meta.%d.ldir" 766 767 Boolean 768 meta_oodate(GNode *gn, Boolean oodate) 769 { 770 static char *tmpdir = NULL; 771 char ldir_vname[64]; 772 char cwd[MAXPATHLEN]; 773 char latestdir[MAXPATHLEN]; 774 char fname[MAXPATHLEN]; 775 char fname1[MAXPATHLEN]; 776 char fname2[MAXPATHLEN]; 777 char *p; 778 char *cp; 779 size_t cwdlen; 780 static size_t tmplen = 0; 781 FILE *fp; 782 Boolean ignoreOODATE = FALSE; 783 784 if (oodate) 785 return oodate; /* we're done */ 786 787 /* 788 * We need to check if the target is out-of-date. This includes 789 * checking if the expanded command has changed. This in turn 790 * requires that all variables are set in the same way that they 791 * would be if the target needs to be re-built. 792 */ 793 Make_DoAllVar(gn); 794 795 if (getcwd(cwd, sizeof(cwd)) == NULL) 796 err(1, "Could not get current working directory"); 797 798 meta_name(gn, fname, sizeof(fname), NULL, NULL); 799 800 #ifdef DEBUG_META_MODE 801 if (DEBUG(META)) 802 fprintf(debug_file, "meta_oodate: %s\n", fname); 803 #endif 804 805 if ((fp = fopen(fname, "r")) != NULL) { 806 static char *buf = NULL; 807 static size_t bufsz; 808 int lineno = 0; 809 int lastpid = 0; 810 int pid; 811 int f = 0; 812 int x; 813 LstNode ln; 814 struct stat fs; 815 816 if (!buf) { 817 bufsz = 8 * BUFSIZ; 818 buf = bmake_malloc(bufsz); 819 } 820 821 if (!tmpdir) { 822 tmpdir = getTmpdir(); 823 tmplen = strlen(tmpdir); 824 } 825 826 /* we want to track all the .meta we read */ 827 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL); 828 829 cwdlen = strlen(cwd); 830 831 ln = Lst_First(gn->commands); 832 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) { 833 lineno++; 834 if (buf[x - 1] == '\n') 835 buf[x - 1] = '\0'; 836 else 837 warnx("%s: %d: line truncated at %u", fname, lineno, x); 838 839 /* Find the start of the build monitor section. */ 840 if (!f) { 841 if (strncmp(buf, "-- filemon", 10) == 0) { 842 f = 1; 843 continue; 844 } 845 if (strncmp(buf, "# buildmon", 10) == 0) { 846 f = 1; 847 continue; 848 } 849 } 850 851 /* Delimit the record type. */ 852 p = buf; 853 #ifdef DEBUG_META_MODE 854 if (DEBUG(META)) 855 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf); 856 #endif 857 strsep(&p, " "); 858 if (f) { 859 /* 860 * We are in the 'filemon' output section. 861 * Each record from filemon follows the general form: 862 * 863 * <key> <pid> <data> 864 * 865 * Where: 866 * <key> is a single letter, denoting the syscall. 867 * <pid> is the process that made the syscall. 868 * <data> is the arguments (of interest). 869 */ 870 switch(buf[0]) { 871 case '#': /* comment */ 872 case 'V': /* version */ 873 break; 874 default: 875 /* 876 * We need to track pathnames per-process. 877 * 878 * Each process run by make, starts off in the 'CWD' 879 * recorded in the .meta file, if it chdirs ('C') 880 * elsewhere we need to track that - but only for 881 * that process. If it forks ('F'), we initialize 882 * the child to have the same cwd as its parent. 883 * 884 * We also need to track the 'latestdir' of 885 * interest. This is usually the same as cwd, but 886 * not if a process is reading directories. 887 * 888 * Each time we spot a different process ('pid') 889 * we save the current value of 'latestdir' in a 890 * variable qualified by 'lastpid', and 891 * re-initialize 'latestdir' to any pre-saved 892 * value for the current 'pid' and 'CWD' if none. 893 */ 894 pid = atoi(p); 895 if (pid > 0 && pid != lastpid) { 896 char *ldir; 897 char *tp; 898 899 if (lastpid > 0) { 900 /* We need to remember this. */ 901 Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0); 902 } 903 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid); 904 lastpid = pid; 905 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp); 906 if (ldir) { 907 strlcpy(latestdir, ldir, sizeof(latestdir)); 908 if (tp) 909 free(tp); 910 } else 911 strlcpy(latestdir, cwd, sizeof(latestdir)); 912 } 913 /* Skip past the pid. */ 914 if (strsep(&p, " ") == NULL) 915 continue; 916 #ifdef DEBUG_META_MODE 917 if (DEBUG(META)) 918 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, latestdir); 919 #endif 920 break; 921 } 922 923 /* Process according to record type. */ 924 switch (buf[0]) { 925 case 'X': /* eXit */ 926 Var_Delete(ldir_vname, VAR_GLOBAL); 927 lastpid = 0; /* no need to save ldir_vname */ 928 break; 929 930 case 'F': /* [v]Fork */ 931 { 932 char cldir[64]; 933 int child; 934 935 child = atoi(p); 936 if (child > 0) { 937 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child); 938 Var_Set(cldir, latestdir, VAR_GLOBAL, 0); 939 } 940 } 941 break; 942 943 case 'C': /* Chdir */ 944 /* Update the latest directory. */ 945 strlcpy(latestdir, p, sizeof(latestdir)); 946 break; 947 948 case 'R': /* Read */ 949 case 'E': /* Exec */ 950 /* 951 * Check for runtime files that can't 952 * be part of the dependencies because 953 * they are _expected_ to change. 954 */ 955 if (strncmp(p, "/tmp/", 5) == 0 || 956 (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)) 957 break; 958 959 if (strncmp(p, "/var/", 5) == 0) 960 break; 961 962 /* Ignore device files. */ 963 if (strncmp(p, "/dev/", 5) == 0) 964 break; 965 966 /* Ignore /etc/ files. */ 967 if (strncmp(p, "/etc/", 5) == 0) 968 break; 969 970 /* 971 * The rest of the record is the file name. 972 * Check if it's not an absolute path. 973 */ 974 { 975 char *sdirs[4]; 976 char **sdp; 977 int sdx = 0; 978 int found = 0; 979 980 if (*p == '/') { 981 sdirs[sdx++] = p; /* done */ 982 } else { 983 if (strcmp(".", p) == 0) 984 continue; /* no point */ 985 986 /* Check vs latestdir */ 987 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p); 988 sdirs[sdx++] = fname1; 989 990 if (strcmp(latestdir, cwd) != 0) { 991 /* Check vs cwd */ 992 snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p); 993 sdirs[sdx++] = fname2; 994 } 995 } 996 sdirs[sdx++] = NULL; 997 998 for (sdp = sdirs; *sdp && !found; sdp++) { 999 #ifdef DEBUG_META_MODE 1000 if (DEBUG(META)) 1001 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp); 1002 #endif 1003 if (stat(*sdp, &fs) == 0) { 1004 found = 1; 1005 p = *sdp; 1006 } 1007 } 1008 if (found) { 1009 #ifdef DEBUG_META_MODE 1010 if (DEBUG(META)) 1011 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p); 1012 #endif 1013 if (!S_ISDIR(fs.st_mode) && 1014 fs.st_mtime > gn->mtime) { 1015 if (DEBUG(META)) 1016 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p); 1017 oodate = TRUE; 1018 } else if (S_ISDIR(fs.st_mode)) { 1019 /* Update the latest directory. */ 1020 realpath(p, latestdir); 1021 } 1022 } else if (errno == ENOENT && *p == '/' && 1023 strncmp(p, cwd, cwdlen) != 0) { 1024 /* 1025 * A referenced file outside of CWD is missing. 1026 * We cannot catch every eventuality here... 1027 */ 1028 if (DEBUG(META)) 1029 fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p); 1030 oodate = TRUE; 1031 } 1032 } 1033 break; 1034 default: 1035 break; 1036 } 1037 } else if (strcmp(buf, "CMD") == 0) { 1038 /* 1039 * Compare the current command with the one in the 1040 * meta data file. 1041 */ 1042 if (ln == NULL) { 1043 if (DEBUG(META)) 1044 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno); 1045 oodate = TRUE; 1046 } else { 1047 char *cmd = (char *)Lst_Datum(ln); 1048 1049 if (!ignoreOODATE) { 1050 if (strstr(cmd, "$?")) 1051 ignoreOODATE = TRUE; 1052 else if ((cp = strstr(cmd, ".OODATE"))) { 1053 /* check for $[{(].OODATE[)}] */ 1054 if (cp > cmd + 2 && cp[-2] == '$') 1055 ignoreOODATE = TRUE; 1056 } 1057 if (ignoreOODATE && DEBUG(META)) 1058 fprintf(debug_file, "%s: %d: cannot compare commands using .OODATE\n", fname, lineno); 1059 } 1060 cmd = Var_Subst(NULL, cmd, gn, TRUE); 1061 1062 if ((cp = strchr(cmd, '\n'))) { 1063 int n; 1064 1065 /* 1066 * This command contains newlines, we need to 1067 * fetch more from the .meta file before we 1068 * attempt a comparison. 1069 */ 1070 /* first put the newline back at buf[x - 1] */ 1071 buf[x - 1] = '\n'; 1072 do { 1073 /* now fetch the next line */ 1074 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0) 1075 break; 1076 x = n; 1077 lineno++; 1078 if (buf[x - 1] != '\n') { 1079 warnx("%s: %d: line truncated at %u", fname, lineno, x); 1080 break; 1081 } 1082 cp = strchr(++cp, '\n'); 1083 } while (cp); 1084 if (buf[x - 1] == '\n') 1085 buf[x - 1] = '\0'; 1086 } 1087 if (!ignoreOODATE && 1088 !(gn->type & OP_NOMETA_CMP) && 1089 strcmp(p, cmd) != 0) { 1090 if (DEBUG(META)) 1091 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd); 1092 if (!metaIgnoreCMDs) 1093 oodate = TRUE; 1094 } 1095 free(cmd); 1096 ln = Lst_Succ(ln); 1097 } 1098 } else if (strcmp(buf, "CWD") == 0) { 1099 char curdir[MAXPATHLEN]; 1100 if (strcmp(p, getcwd(curdir, sizeof(curdir))) != 0) { 1101 if (DEBUG(META)) 1102 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir); 1103 oodate = TRUE; 1104 } 1105 } 1106 } 1107 1108 /* 1109 * Check if there are extra commands now 1110 * that weren't in the meta data file. 1111 */ 1112 if (!oodate && ln != NULL) { 1113 if (DEBUG(META)) 1114 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno); 1115 oodate = TRUE; 1116 } 1117 1118 fclose(fp); 1119 } 1120 if (oodate && ignoreOODATE) { 1121 /* 1122 * Target uses .OODATE, so we need to re-compute it. 1123 * We need to clean up what Make_DoAllVar() did. 1124 */ 1125 Var_Delete(ALLSRC, gn); 1126 Var_Delete(OODATE, gn); 1127 gn->flags &= ~DONE_ALLSRC; 1128 } 1129 return oodate; 1130 } 1131 1132 /* support for compat mode */ 1133 1134 static int childPipe[2]; 1135 1136 void 1137 meta_compat_start(void) 1138 { 1139 #ifdef USE_FILEMON_ONCE 1140 /* 1141 * We need to re-open filemon for each cmd. 1142 */ 1143 BuildMon *pbm = &Mybm; 1144 1145 if (pbm->mfp != NULL && useFilemon) { 1146 filemon_open(pbm); 1147 } else { 1148 pbm->mon_fd = pbm->filemon_fd = -1; 1149 } 1150 #endif 1151 if (pipe(childPipe) < 0) 1152 Punt("Cannot create pipe: %s", strerror(errno)); 1153 /* Set close-on-exec flag for both */ 1154 (void)fcntl(childPipe[0], F_SETFD, 1); 1155 (void)fcntl(childPipe[1], F_SETFD, 1); 1156 } 1157 1158 void 1159 meta_compat_child(void) 1160 { 1161 meta_job_child(NULL); 1162 if (dup2(childPipe[1], 1) < 0 || 1163 dup2(1, 2) < 0) { 1164 execError("dup2", "pipe"); 1165 _exit(1); 1166 } 1167 } 1168 1169 void 1170 meta_compat_parent(void) 1171 { 1172 FILE *fp; 1173 char buf[BUFSIZ]; 1174 1175 close(childPipe[1]); /* child side */ 1176 fp = fdopen(childPipe[0], "r"); 1177 while (fgets(buf, sizeof(buf), fp)) { 1178 meta_job_output(NULL, buf, ""); 1179 printf("%s", buf); 1180 } 1181 fclose(fp); 1182 } 1183 1184 #endif /* USE_META */ 1185