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