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