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