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