meta.c revision 1.119 1 /* $NetBSD: meta.c,v 1.119 2020/09/24 07:53:32 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 Boolean comatMake;
84 extern char **environ;
85
86 #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
87
88 #ifndef N2U
89 # define N2U(n, u) (((n) + ((u) - 1)) / (u))
90 #endif
91 #ifndef ROUNDUP
92 # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
93 #endif
94
95 #if !defined(HAVE_STRSEP)
96 # define strsep(s, d) stresep((s), (d), 0)
97 #endif
98
99 /*
100 * Filemon is a kernel module which snoops certain syscalls.
101 *
102 * C chdir
103 * E exec
104 * F [v]fork
105 * L [sym]link
106 * M rename
107 * R read
108 * W write
109 * S stat
110 *
111 * See meta_oodate below - we mainly care about 'E' and 'R'.
112 *
113 * We can still use meta mode without filemon, but
114 * the benefits are more limited.
115 */
116 #ifdef USE_FILEMON
117
118 /*
119 * Open the filemon device.
120 */
121 static void
122 meta_open_filemon(BuildMon *pbm)
123 {
124 int dupfd;
125
126 pbm->mon_fd = -1;
127 pbm->filemon = NULL;
128 if (!useFilemon || !pbm->mfp)
129 return;
130
131 pbm->filemon = filemon_open();
132 if (pbm->filemon == NULL) {
133 useFilemon = FALSE;
134 warn("Could not open filemon %s", filemon_path());
135 return;
136 }
137
138 /*
139 * We use a file outside of '.'
140 * to avoid a FreeBSD kernel bug where unlink invalidates
141 * cwd causing getcwd to do a lot more work.
142 * We only care about the descriptor.
143 */
144 pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
145 if ((dupfd = dup(pbm->mon_fd)) == -1) {
146 err(1, "Could not dup filemon output!");
147 }
148 (void)fcntl(dupfd, F_SETFD, FD_CLOEXEC);
149 if (filemon_setfd(pbm->filemon, dupfd) == -1) {
150 err(1, "Could not set filemon file descriptor!");
151 }
152 /* we don't need these once we exec */
153 (void)fcntl(pbm->mon_fd, F_SETFD, FD_CLOEXEC);
154 }
155
156 /*
157 * Read the build monitor output file and write records to the target's
158 * metadata file.
159 */
160 static int
161 filemon_read(FILE *mfp, int fd)
162 {
163 char buf[BUFSIZ];
164 int n;
165 int error;
166
167 /* Check if we're not writing to a meta data file.*/
168 if (mfp == NULL) {
169 if (fd >= 0)
170 close(fd); /* not interested */
171 return 0;
172 }
173 /* rewind */
174 if (lseek(fd, (off_t)0, SEEK_SET) < 0) {
175 error = errno;
176 warn("Could not rewind filemon");
177 fprintf(mfp, "\n");
178 } else {
179 error = 0;
180 fprintf(mfp, "\n-- filemon acquired metadata --\n");
181
182 while ((n = read(fd, buf, sizeof(buf))) > 0) {
183 if ((int)fwrite(buf, 1, 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 - (cp - buf));
230 } else {
231 return; /* can't happen? */
232 }
233 }
234 } while (cp);
235 }
236
237 static char *
238 meta_name(struct GNode *gn, 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) - (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 int 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) {
328 p_make = Var_Value(".MAKE", gn, &cp);
329 p_len = strlen(p_make);
330 }
331 cp = strchr(cmd, '$');
332 if ((cp)) {
333 (void)Var_Subst(cmd, gn, VARE_WANTRES, &mp);
334 /* TODO: handle errors */
335 cmd = mp;
336 }
337 cp2 = strstr(cmd, p_make);
338 if ((cp2)) {
339 switch (cp2[p_len]) {
340 case '\0':
341 case ' ':
342 case '\t':
343 case '\n':
344 rc = 1;
345 break;
346 }
347 if (cp2 > cmd && rc > 0) {
348 switch (cp2[-1]) {
349 case ' ':
350 case '\t':
351 case '\n':
352 break;
353 default:
354 rc = 0; /* no match */
355 break;
356 }
357 }
358 }
359 free(mp);
360 return rc;
361 }
362
363 typedef struct meta_file_s {
364 FILE *fp;
365 GNode *gn;
366 } meta_file_t;
367
368 static void
369 printCMD(void *cmdp, void *mfpp)
370 {
371 meta_file_t *mfp = mfpp;
372 char *cmd = cmdp;
373 char *cmd_freeIt = NULL;
374
375 if (strchr(cmd, '$')) {
376 (void)Var_Subst(cmd, mfp->gn, VARE_WANTRES, &cmd_freeIt);
377 /* TODO: handle errors */
378 cmd = cmd_freeIt;
379 }
380 fprintf(mfp->fp, "CMD %s\n", cmd);
381 free(cmd_freeIt);
382 }
383
384 /*
385 * Certain node types never get a .meta file
386 */
387 #define SKIP_META_TYPE(_type) do { \
388 if ((gn->type & __CONCAT(OP_, _type))) { \
389 if (verbose) { \
390 fprintf(debug_file, "Skipping meta for %s: .%s\n", \
391 gn->name, __STRING(_type)); \
392 } \
393 return FALSE; \
394 } \
395 } while (0)
396
397
398 /*
399 * Do we need/want a .meta file ?
400 */
401 static Boolean
402 meta_needed(GNode *gn, const char *dname, const char *tname,
403 char *objdir, int verbose)
404 {
405 struct make_stat mst;
406
407 if (verbose)
408 verbose = DEBUG(META);
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 /* Check if there are no commands to execute. */
422 if (Lst_IsEmpty(gn->commands)) {
423 if (verbose)
424 fprintf(debug_file, "Skipping meta for %s: no commands\n",
425 gn->name);
426 return FALSE;
427 }
428 if ((gn->type & (OP_META|OP_SUBMAKE)) == OP_SUBMAKE) {
429 /* OP_SUBMAKE is a bit too aggressive */
430 if (Lst_ForEachUntil(gn->commands, is_submake, gn)) {
431 if (DEBUG(META))
432 fprintf(debug_file, "Skipping meta for %s: .SUBMAKE\n",
433 gn->name);
434 return FALSE;
435 }
436 }
437
438 /* The object directory may not exist. Check it.. */
439 if (cached_stat(dname, &mst) != 0) {
440 if (verbose)
441 fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
442 gn->name);
443 return FALSE;
444 }
445
446 /* make sure these are canonical */
447 if (cached_realpath(dname, objdir))
448 dname = objdir;
449
450 /* If we aren't in the object directory, don't create a meta file. */
451 if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
452 if (verbose)
453 fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
454 gn->name);
455 return FALSE;
456 }
457 return TRUE;
458 }
459
460
461 static FILE *
462 meta_create(BuildMon *pbm, GNode *gn)
463 {
464 meta_file_t mf;
465 char buf[MAXPATHLEN];
466 char objdir[MAXPATHLEN];
467 char **ptr;
468 const char *dname;
469 const char *tname;
470 char *fname;
471 const char *cp;
472 char *p[5]; /* >= possible uses */
473 int i;
474
475 mf.fp = NULL;
476 i = 0;
477
478 dname = Var_Value(".OBJDIR", gn, &p[i++]);
479 tname = Var_Value(TARGET, gn, &p[i++]);
480
481 /* if this succeeds objdir is realpath of dname */
482 if (!meta_needed(gn, dname, tname, objdir, TRUE))
483 goto out;
484 dname = objdir;
485
486 if (metaVerbose) {
487 char *mp;
488
489 /* Describe the target we are building */
490 (void)Var_Subst("${" MAKE_META_PREFIX "}", gn, VARE_WANTRES, &mp);
491 /* TODO: handle errors */
492 if (*mp)
493 fprintf(stdout, "%s\n", mp);
494 free(mp);
495 }
496 /* Get the basename of the target */
497 if ((cp = strrchr(tname, '/')) == NULL) {
498 cp = tname;
499 } else {
500 cp++;
501 }
502
503 fflush(stdout);
504
505 if (!writeMeta)
506 /* Don't create meta data. */
507 goto out;
508
509 fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
510 dname, tname, objdir);
511
512 #ifdef DEBUG_META_MODE
513 if (DEBUG(META))
514 fprintf(debug_file, "meta_create: %s\n", fname);
515 #endif
516
517 if ((mf.fp = fopen(fname, "w")) == NULL)
518 err(1, "Could not open meta file '%s'", fname);
519
520 fprintf(mf.fp, "# Meta data file %s\n", fname);
521
522 mf.gn = gn;
523
524 Lst_ForEach(gn->commands, printCMD, &mf);
525
526 fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
527 fprintf(mf.fp, "TARGET %s\n", tname);
528 cp = Var_Value(".OODATE", gn, &p[i++]);
529 if (cp && *cp) {
530 fprintf(mf.fp, "OODATE %s\n", cp);
531 }
532 if (metaEnv) {
533 for (ptr = environ; *ptr != NULL; ptr++)
534 fprintf(mf.fp, "ENV %s\n", *ptr);
535 }
536
537 fprintf(mf.fp, "-- command output --\n");
538 fflush(mf.fp);
539
540 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
541 Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
542
543 gn->type |= OP_META; /* in case anyone wants to know */
544 if (metaSilent) {
545 gn->type |= OP_SILENT;
546 }
547 out:
548 for (i--; i >= 0; i--) {
549 bmake_free(p[i]);
550 }
551
552 return mf.fp;
553 }
554
555 static Boolean
556 boolValue(char *s)
557 {
558 switch(*s) {
559 case '0':
560 case 'N':
561 case 'n':
562 case 'F':
563 case 'f':
564 return FALSE;
565 }
566 return TRUE;
567 }
568
569 /*
570 * Initialization we need before reading makefiles.
571 */
572 void
573 meta_init(void)
574 {
575 #ifdef USE_FILEMON
576 /* this allows makefiles to test if we have filemon support */
577 Var_Set(".MAKE.PATH_FILEMON", filemon_path(), VAR_GLOBAL);
578 #endif
579 }
580
581
582 #define get_mode_bf(bf, token) \
583 if ((cp = strstr(make_mode, token))) \
584 bf = boolValue(&cp[sizeof(token) - 1])
585
586 /*
587 * Initialization we need after reading makefiles.
588 */
589 void
590 meta_mode_init(const char *make_mode)
591 {
592 static int once = 0;
593 char *cp;
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_Init();
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, NULL);
637 /*
638 * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
639 */
640 metaIgnorePaths = Lst_Init();
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, NULL);
647
648 /*
649 * We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS}
650 */
651 cp = NULL;
652 if (Var_Value(MAKE_META_IGNORE_PATTERNS, VAR_GLOBAL, &cp)) {
653 metaIgnorePatterns = TRUE;
654 bmake_free(cp);
655 }
656 cp = NULL;
657 if (Var_Value(MAKE_META_IGNORE_FILTER, VAR_GLOBAL, &cp)) {
658 metaIgnoreFilter = TRUE;
659 bmake_free(cp);
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)
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", gn->path ? gn->path : gn->name, VAR_GLOBAL);
795 }
796 getcwd(cwd, sizeof(cwd));
797 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL);
798 if (pbm->meta_fname[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 int meta_prefix_len;
818
819 if (!meta_prefix) {
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 = 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)
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 } else
864 #endif
865 fprintf(pbm->mfp, "\n"); /* ensure end with newline */
866 return error;
867 }
868
869 int
870 meta_job_finish(Job *job)
871 {
872 BuildMon *pbm;
873 int error = 0;
874 int x;
875
876 if (job != NULL) {
877 pbm = &job->bm;
878 } else {
879 pbm = &Mybm;
880 }
881 if (pbm->mfp != NULL) {
882 error = meta_cmd_finish(pbm);
883 x = fclose(pbm->mfp);
884 if (error == 0 && x != 0)
885 error = errno;
886 pbm->mfp = NULL;
887 pbm->meta_fname[0] = '\0';
888 }
889 return error;
890 }
891
892 void
893 meta_finish(void)
894 {
895 if (metaBailiwick != NULL)
896 Lst_Free(metaBailiwick);
897 free(metaBailiwickStr);
898 if (metaIgnorePaths != NULL)
899 Lst_Free(metaIgnorePaths);
900 free(metaIgnorePathsStr);
901 }
902
903 /*
904 * Fetch a full line from fp - growing bufp if needed
905 * Return length in bufp.
906 */
907 static int
908 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
909 {
910 char *buf = *bufp;
911 size_t bufsz = *szp;
912 struct stat fs;
913 int x;
914
915 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
916 check_newline:
917 x = o + strlen(&buf[o]);
918 if (buf[x - 1] == '\n')
919 return x;
920 /*
921 * We need to grow the buffer.
922 * The meta file can give us a clue.
923 */
924 if (fstat(fileno(fp), &fs) == 0) {
925 size_t newsz;
926 char *p;
927
928 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
929 if (newsz <= bufsz)
930 newsz = ROUNDUP(fs.st_size, BUFSIZ);
931 if (newsz <= bufsz)
932 return x; /* truncated */
933 if (DEBUG(META))
934 fprintf(debug_file, "growing buffer %zu -> %zu\n",
935 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], bufsz - x, fp))
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_match(const void *p, const void *q)
964 {
965 const char *path = p;
966 const char *prefix = q;
967 size_t n = strlen(prefix);
968
969 if (strncmp(path, prefix, n) != 0)
970 return FALSE;
971 return path[n] == '\0' || path[n] == '/';
972 }
973
974 static Boolean
975 string_match(const void *p, const void *q)
976 {
977 return strcmp(p, q) == 0;
978 }
979
980
981 static int
982 meta_ignore(GNode *gn, const char *p)
983 {
984 char fname[MAXPATHLEN];
985
986 if (p == NULL)
987 return TRUE;
988
989 if (*p == '/') {
990 cached_realpath(p, fname); /* clean it up */
991 if (Lst_ForEachUntil(metaIgnorePaths, prefix_match, fname)) {
992 #ifdef DEBUG_META_MODE
993 if (DEBUG(META))
994 fprintf(debug_file, "meta_oodate: ignoring path: %s\n",
995 p);
996 #endif
997 return TRUE;
998 }
999 }
1000
1001 if (metaIgnorePatterns) {
1002 const char *expr;
1003 char *pm;
1004
1005 Var_Set(".p.", p, gn);
1006 expr = "${" MAKE_META_IGNORE_PATTERNS ":@m@${.p.:M$m}@}";
1007 (void)Var_Subst(expr, gn, VARE_WANTRES, &pm);
1008 /* TODO: handle errors */
1009 if (*pm) {
1010 #ifdef DEBUG_META_MODE
1011 if (DEBUG(META))
1012 fprintf(debug_file, "meta_oodate: ignoring pattern: %s\n",
1013 p);
1014 #endif
1015 free(pm);
1016 return TRUE;
1017 }
1018 free(pm);
1019 }
1020
1021 if (metaIgnoreFilter) {
1022 char *fm;
1023
1024 /* skip if filter result is empty */
1025 snprintf(fname, sizeof(fname),
1026 "${%s:L:${%s:ts:}}",
1027 p, MAKE_META_IGNORE_FILTER);
1028 (void)Var_Subst(fname, gn, VARE_WANTRES, &fm);
1029 /* TODO: handle errors */
1030 if (*fm == '\0') {
1031 #ifdef DEBUG_META_MODE
1032 if (DEBUG(META))
1033 fprintf(debug_file, "meta_oodate: ignoring filtered: %s\n",
1034 p);
1035 #endif
1036 free(fm);
1037 return TRUE;
1038 }
1039 free(fm);
1040 }
1041 return FALSE;
1042 }
1043
1044 /*
1045 * When running with 'meta' functionality, a target can be out-of-date
1046 * if any of the references in its meta data file is more recent.
1047 * We have to track the latestdir on a per-process basis.
1048 */
1049 #define LCWD_VNAME_FMT ".meta.%d.lcwd"
1050 #define LDIR_VNAME_FMT ".meta.%d.ldir"
1051
1052 /*
1053 * It is possible that a .meta file is corrupted,
1054 * if we detect this we want to reproduce it.
1055 * Setting oodate TRUE will have that effect.
1056 */
1057 #define CHECK_VALID_META(p) if (!(p && *p)) { \
1058 warnx("%s: %d: malformed", fname, lineno); \
1059 oodate = TRUE; \
1060 continue; \
1061 }
1062
1063 #define DEQUOTE(p) if (*p == '\'') { \
1064 char *ep; \
1065 p++; \
1066 if ((ep = strchr(p, '\''))) \
1067 *ep = '\0'; \
1068 }
1069
1070 Boolean
1071 meta_oodate(GNode *gn, Boolean oodate)
1072 {
1073 static char *tmpdir = NULL;
1074 static char cwd[MAXPATHLEN];
1075 char lcwd_vname[64];
1076 char ldir_vname[64];
1077 char lcwd[MAXPATHLEN];
1078 char latestdir[MAXPATHLEN];
1079 char fname[MAXPATHLEN];
1080 char fname1[MAXPATHLEN];
1081 char fname2[MAXPATHLEN];
1082 char fname3[MAXPATHLEN];
1083 const char *dname;
1084 const char *tname;
1085 char *p;
1086 char *cp;
1087 char *link_src;
1088 char *move_target;
1089 static size_t cwdlen = 0;
1090 static size_t tmplen = 0;
1091 FILE *fp;
1092 Boolean needOODATE = FALSE;
1093 StringList *missingFiles;
1094 char *pa[4]; /* >= possible uses */
1095 int i;
1096 int have_filemon = FALSE;
1097
1098 if (oodate)
1099 return oodate; /* we're done */
1100
1101 i = 0;
1102
1103 dname = Var_Value(".OBJDIR", gn, &pa[i++]);
1104 tname = Var_Value(TARGET, gn, &pa[i++]);
1105
1106 /* if this succeeds fname3 is realpath of dname */
1107 if (!meta_needed(gn, dname, tname, fname3, FALSE))
1108 goto oodate_out;
1109 dname = fname3;
1110
1111 missingFiles = Lst_Init();
1112
1113 /*
1114 * We need to check if the target is out-of-date. This includes
1115 * checking if the expanded command has changed. This in turn
1116 * requires that all variables are set in the same way that they
1117 * would be if the target needs to be re-built.
1118 */
1119 Make_DoAllVar(gn);
1120
1121 meta_name(gn, fname, sizeof(fname), dname, tname, dname);
1122
1123 #ifdef DEBUG_META_MODE
1124 if (DEBUG(META))
1125 fprintf(debug_file, "meta_oodate: %s\n", fname);
1126 #endif
1127
1128 if ((fp = fopen(fname, "r")) != NULL) {
1129 static char *buf = NULL;
1130 static size_t bufsz;
1131 int lineno = 0;
1132 int lastpid = 0;
1133 int pid;
1134 int x;
1135 StringListNode *cmdNode;
1136 struct make_stat mst;
1137
1138 if (!buf) {
1139 bufsz = 8 * BUFSIZ;
1140 buf = bmake_malloc(bufsz);
1141 }
1142
1143 if (!cwdlen) {
1144 if (getcwd(cwd, sizeof(cwd)) == NULL)
1145 err(1, "Could not get current working directory");
1146 cwdlen = strlen(cwd);
1147 }
1148 strlcpy(lcwd, cwd, sizeof(lcwd));
1149 strlcpy(latestdir, cwd, sizeof(latestdir));
1150
1151 if (!tmpdir) {
1152 tmpdir = getTmpdir();
1153 tmplen = strlen(tmpdir);
1154 }
1155
1156 /* we want to track all the .meta we read */
1157 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
1158
1159 cmdNode = Lst_First(gn->commands);
1160 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
1161 lineno++;
1162 if (buf[x - 1] == '\n')
1163 buf[x - 1] = '\0';
1164 else {
1165 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1166 oodate = TRUE;
1167 break;
1168 }
1169 link_src = NULL;
1170 move_target = NULL;
1171 /* Find the start of the build monitor section. */
1172 if (!have_filemon) {
1173 if (strncmp(buf, "-- filemon", 10) == 0) {
1174 have_filemon = TRUE;
1175 continue;
1176 }
1177 if (strncmp(buf, "# buildmon", 10) == 0) {
1178 have_filemon = TRUE;
1179 continue;
1180 }
1181 }
1182
1183 /* Delimit the record type. */
1184 p = buf;
1185 #ifdef DEBUG_META_MODE
1186 if (DEBUG(META))
1187 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
1188 #endif
1189 strsep(&p, " ");
1190 if (have_filemon) {
1191 /*
1192 * We are in the 'filemon' output section.
1193 * Each record from filemon follows the general form:
1194 *
1195 * <key> <pid> <data>
1196 *
1197 * Where:
1198 * <key> is a single letter, denoting the syscall.
1199 * <pid> is the process that made the syscall.
1200 * <data> is the arguments (of interest).
1201 */
1202 switch(buf[0]) {
1203 case '#': /* comment */
1204 case 'V': /* version */
1205 break;
1206 default:
1207 /*
1208 * We need to track pathnames per-process.
1209 *
1210 * Each process run by make, starts off in the 'CWD'
1211 * recorded in the .meta file, if it chdirs ('C')
1212 * elsewhere we need to track that - but only for
1213 * that process. If it forks ('F'), we initialize
1214 * the child to have the same cwd as its parent.
1215 *
1216 * We also need to track the 'latestdir' of
1217 * interest. This is usually the same as cwd, but
1218 * not if a process is reading directories.
1219 *
1220 * Each time we spot a different process ('pid')
1221 * we save the current value of 'latestdir' in a
1222 * variable qualified by 'lastpid', and
1223 * re-initialize 'latestdir' to any pre-saved
1224 * value for the current 'pid' and 'CWD' if none.
1225 */
1226 CHECK_VALID_META(p);
1227 pid = atoi(p);
1228 if (pid > 0 && pid != lastpid) {
1229 const char *ldir;
1230 char *tp;
1231
1232 if (lastpid > 0) {
1233 /* We need to remember these. */
1234 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL);
1235 Var_Set(ldir_vname, latestdir, VAR_GLOBAL);
1236 }
1237 snprintf(lcwd_vname, sizeof(lcwd_vname), LCWD_VNAME_FMT, pid);
1238 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
1239 lastpid = pid;
1240 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
1241 if (ldir) {
1242 strlcpy(latestdir, ldir, sizeof(latestdir));
1243 bmake_free(tp);
1244 }
1245 ldir = Var_Value(lcwd_vname, VAR_GLOBAL, &tp);
1246 if (ldir) {
1247 strlcpy(lcwd, ldir, sizeof(lcwd));
1248 bmake_free(tp);
1249 }
1250 }
1251 /* Skip past the pid. */
1252 if (strsep(&p, " ") == NULL)
1253 continue;
1254 #ifdef DEBUG_META_MODE
1255 if (DEBUG(META))
1256 fprintf(debug_file, "%s: %d: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
1257 fname, lineno,
1258 pid, buf[0], cwd, lcwd, latestdir);
1259 #endif
1260 break;
1261 }
1262
1263 CHECK_VALID_META(p);
1264
1265 /* Process according to record type. */
1266 switch (buf[0]) {
1267 case 'X': /* eXit */
1268 Var_Delete(lcwd_vname, VAR_GLOBAL);
1269 Var_Delete(ldir_vname, VAR_GLOBAL);
1270 lastpid = 0; /* no need to save ldir_vname */
1271 break;
1272
1273 case 'F': /* [v]Fork */
1274 {
1275 char cldir[64];
1276 int child;
1277
1278 child = atoi(p);
1279 if (child > 0) {
1280 snprintf(cldir, sizeof(cldir), LCWD_VNAME_FMT, child);
1281 Var_Set(cldir, lcwd, VAR_GLOBAL);
1282 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
1283 Var_Set(cldir, latestdir, VAR_GLOBAL);
1284 #ifdef DEBUG_META_MODE
1285 if (DEBUG(META))
1286 fprintf(debug_file, "%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n",
1287 fname, lineno,
1288 child, cwd, lcwd, latestdir);
1289 #endif
1290 }
1291 }
1292 break;
1293
1294 case 'C': /* Chdir */
1295 /* Update lcwd and latest directory. */
1296 strlcpy(latestdir, p, sizeof(latestdir));
1297 strlcpy(lcwd, p, sizeof(lcwd));
1298 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL);
1299 Var_Set(ldir_vname, lcwd, VAR_GLOBAL);
1300 #ifdef DEBUG_META_MODE
1301 if (DEBUG(META))
1302 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, lcwd);
1303 #endif
1304 break;
1305
1306 case 'M': /* renaMe */
1307 /*
1308 * For 'M'oves we want to check
1309 * the src as for 'R'ead
1310 * and the target as for 'W'rite.
1311 */
1312 cp = p; /* save this for a second */
1313 /* now get target */
1314 if (strsep(&p, " ") == NULL)
1315 continue;
1316 CHECK_VALID_META(p);
1317 move_target = p;
1318 p = cp;
1319 /* 'L' and 'M' put single quotes around the args */
1320 DEQUOTE(p);
1321 DEQUOTE(move_target);
1322 /* FALLTHROUGH */
1323 case 'D': /* unlink */
1324 if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1325 /* remove any missingFiles entries that match p */
1326 StringListNode *missingNode =
1327 Lst_Find(missingFiles, path_match, p);
1328 if (missingNode != NULL) {
1329 StringListNode *nln;
1330
1331 do {
1332 char *tp;
1333 nln = Lst_FindFrom(missingFiles,
1334 LstNode_Next(missingNode),
1335 path_match, p);
1336 tp = LstNode_Datum(missingNode);
1337 Lst_Remove(missingFiles, missingNode);
1338 free(tp);
1339 } while ((missingNode = nln) != NULL);
1340 }
1341 }
1342 if (buf[0] == 'M') {
1343 /* the target of the mv is a file 'W'ritten */
1344 #ifdef DEBUG_META_MODE
1345 if (DEBUG(META))
1346 fprintf(debug_file, "meta_oodate: M %s -> %s\n",
1347 p, move_target);
1348 #endif
1349 p = move_target;
1350 goto check_write;
1351 }
1352 break;
1353 case 'L': /* Link */
1354 /*
1355 * For 'L'inks check
1356 * the src as for 'R'ead
1357 * and the target as for 'W'rite.
1358 */
1359 link_src = p;
1360 /* now get target */
1361 if (strsep(&p, " ") == NULL)
1362 continue;
1363 CHECK_VALID_META(p);
1364 /* 'L' and 'M' put single quotes around the args */
1365 DEQUOTE(p);
1366 DEQUOTE(link_src);
1367 #ifdef DEBUG_META_MODE
1368 if (DEBUG(META))
1369 fprintf(debug_file, "meta_oodate: L %s -> %s\n",
1370 link_src, p);
1371 #endif
1372 /* FALLTHROUGH */
1373 case 'W': /* Write */
1374 check_write:
1375 /*
1376 * If a file we generated within our bailiwick
1377 * but outside of .OBJDIR is missing,
1378 * we need to do it again.
1379 */
1380 /* ignore non-absolute paths */
1381 if (*p != '/')
1382 break;
1383
1384 if (Lst_IsEmpty(metaBailiwick))
1385 break;
1386
1387 /* ignore cwd - normal dependencies handle those */
1388 if (strncmp(p, cwd, cwdlen) == 0)
1389 break;
1390
1391 if (!Lst_ForEachUntil(metaBailiwick, prefix_match, p))
1392 break;
1393
1394 /* tmpdir might be within */
1395 if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1396 break;
1397
1398 /* ignore anything containing the string "tmp" */
1399 if ((strstr("tmp", p)))
1400 break;
1401
1402 if ((link_src != NULL && cached_lstat(p, &mst) < 0) ||
1403 (link_src == NULL && cached_stat(p, &mst) < 0)) {
1404 if (!meta_ignore(gn, p)) {
1405 if (Lst_Find(missingFiles, string_match, p) == NULL)
1406 Lst_Append(missingFiles, bmake_strdup(p));
1407 }
1408 }
1409 break;
1410 check_link_src:
1411 p = link_src;
1412 link_src = NULL;
1413 #ifdef DEBUG_META_MODE
1414 if (DEBUG(META))
1415 fprintf(debug_file, "meta_oodate: L src %s\n", p);
1416 #endif
1417 /* FALLTHROUGH */
1418 case 'R': /* Read */
1419 case 'E': /* Exec */
1420 /*
1421 * Check for runtime files that can't
1422 * be part of the dependencies because
1423 * they are _expected_ to change.
1424 */
1425 if (meta_ignore(gn, p))
1426 break;
1427
1428 /*
1429 * The rest of the record is the file name.
1430 * Check if it's not an absolute path.
1431 */
1432 {
1433 char *sdirs[4];
1434 char **sdp;
1435 int sdx = 0;
1436 int found = 0;
1437
1438 if (*p == '/') {
1439 sdirs[sdx++] = p; /* done */
1440 } else {
1441 if (strcmp(".", p) == 0)
1442 continue; /* no point */
1443
1444 /* Check vs latestdir */
1445 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1446 sdirs[sdx++] = fname1;
1447
1448 if (strcmp(latestdir, lcwd) != 0) {
1449 /* Check vs lcwd */
1450 snprintf(fname2, sizeof(fname2), "%s/%s", lcwd, p);
1451 sdirs[sdx++] = fname2;
1452 }
1453 if (strcmp(lcwd, cwd) != 0) {
1454 /* Check vs cwd */
1455 snprintf(fname3, sizeof(fname3), "%s/%s", cwd, p);
1456 sdirs[sdx++] = fname3;
1457 }
1458 }
1459 sdirs[sdx++] = NULL;
1460
1461 for (sdp = sdirs; *sdp && !found; sdp++) {
1462 #ifdef DEBUG_META_MODE
1463 if (DEBUG(META))
1464 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1465 #endif
1466 if (cached_stat(*sdp, &mst) == 0) {
1467 found = 1;
1468 p = *sdp;
1469 }
1470 }
1471 if (found) {
1472 #ifdef DEBUG_META_MODE
1473 if (DEBUG(META))
1474 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1475 #endif
1476 if (!S_ISDIR(mst.mst_mode) &&
1477 mst.mst_mtime > gn->mtime) {
1478 if (DEBUG(META))
1479 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1480 oodate = TRUE;
1481 } else if (S_ISDIR(mst.mst_mode)) {
1482 /* Update the latest directory. */
1483 cached_realpath(p, latestdir);
1484 }
1485 } else if (errno == ENOENT && *p == '/' &&
1486 strncmp(p, cwd, cwdlen) != 0) {
1487 /*
1488 * A referenced file outside of CWD is missing.
1489 * We cannot catch every eventuality here...
1490 */
1491 if (Lst_Find(missingFiles, string_match, p) == NULL)
1492 Lst_Append(missingFiles, bmake_strdup(p));
1493 }
1494 }
1495 if (buf[0] == 'E') {
1496 /* previous latestdir is no longer relevant */
1497 strlcpy(latestdir, lcwd, sizeof(latestdir));
1498 }
1499 break;
1500 default:
1501 break;
1502 }
1503 if (!oodate && buf[0] == 'L' && link_src != NULL)
1504 goto check_link_src;
1505 } else if (strcmp(buf, "CMD") == 0) {
1506 /*
1507 * Compare the current command with the one in the
1508 * meta data file.
1509 */
1510 if (cmdNode == NULL) {
1511 if (DEBUG(META))
1512 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1513 oodate = TRUE;
1514 } else {
1515 char *cmd = LstNode_Datum(cmdNode);
1516 Boolean hasOODATE = FALSE;
1517
1518 if (strstr(cmd, "$?"))
1519 hasOODATE = TRUE;
1520 else if ((cp = strstr(cmd, ".OODATE"))) {
1521 /* check for $[{(].OODATE[:)}] */
1522 if (cp > cmd + 2 && cp[-2] == '$')
1523 hasOODATE = TRUE;
1524 }
1525 if (hasOODATE) {
1526 needOODATE = TRUE;
1527 if (DEBUG(META))
1528 fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno);
1529 }
1530 (void)Var_Subst(cmd, gn, VARE_WANTRES|VARE_UNDEFERR, &cmd);
1531 /* TODO: handle errors */
1532
1533 if ((cp = strchr(cmd, '\n'))) {
1534 int n;
1535
1536 /*
1537 * This command contains newlines, we need to
1538 * fetch more from the .meta file before we
1539 * attempt a comparison.
1540 */
1541 /* first put the newline back at buf[x - 1] */
1542 buf[x - 1] = '\n';
1543 do {
1544 /* now fetch the next line */
1545 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1546 break;
1547 x = n;
1548 lineno++;
1549 if (buf[x - 1] != '\n') {
1550 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1551 break;
1552 }
1553 cp = strchr(++cp, '\n');
1554 } while (cp);
1555 if (buf[x - 1] == '\n')
1556 buf[x - 1] = '\0';
1557 }
1558 if (p &&
1559 !hasOODATE &&
1560 !(gn->type & OP_NOMETA_CMP) &&
1561 strcmp(p, cmd) != 0) {
1562 if (DEBUG(META))
1563 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1564 if (!metaIgnoreCMDs)
1565 oodate = TRUE;
1566 }
1567 free(cmd);
1568 cmdNode = LstNode_Next(cmdNode);
1569 }
1570 } else if (strcmp(buf, "CWD") == 0) {
1571 /*
1572 * Check if there are extra commands now
1573 * that weren't in the meta data file.
1574 */
1575 if (!oodate && cmdNode != NULL) {
1576 if (DEBUG(META))
1577 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1578 oodate = TRUE;
1579 }
1580 CHECK_VALID_META(p);
1581 if (strcmp(p, cwd) != 0) {
1582 if (DEBUG(META))
1583 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1584 oodate = TRUE;
1585 }
1586 }
1587 }
1588
1589 fclose(fp);
1590 if (!Lst_IsEmpty(missingFiles)) {
1591 if (DEBUG(META))
1592 fprintf(debug_file, "%s: missing files: %s...\n",
1593 fname, (char *)LstNode_Datum(Lst_First(missingFiles)));
1594 oodate = TRUE;
1595 }
1596 if (!oodate && !have_filemon && filemonMissing) {
1597 if (DEBUG(META))
1598 fprintf(debug_file, "%s: missing filemon data\n", fname);
1599 oodate = TRUE;
1600 }
1601 } else {
1602 if (writeMeta && (metaMissing || (gn->type & OP_META))) {
1603 cp = NULL;
1604
1605 /* if target is in .CURDIR we do not need a meta file */
1606 if (gn->path && (cp = strrchr(gn->path, '/')) && cp > gn->path) {
1607 if (strncmp(curdir, gn->path, (cp - gn->path)) != 0) {
1608 cp = NULL; /* not in .CURDIR */
1609 }
1610 }
1611 if (!cp) {
1612 if (DEBUG(META))
1613 fprintf(debug_file, "%s: required but missing\n", fname);
1614 oodate = TRUE;
1615 needOODATE = TRUE; /* assume the worst */
1616 }
1617 }
1618 }
1619
1620 Lst_Destroy(missingFiles, free);
1621
1622 if (oodate && needOODATE) {
1623 /*
1624 * Target uses .OODATE which is empty; or we wouldn't be here.
1625 * We have decided it is oodate, so .OODATE needs to be set.
1626 * All we can sanely do is set it to .ALLSRC.
1627 */
1628 Var_Delete(OODATE, gn);
1629 Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn);
1630 bmake_free(cp);
1631 }
1632
1633 oodate_out:
1634 for (i--; i >= 0; i--) {
1635 bmake_free(pa[i]);
1636 }
1637 return oodate;
1638 }
1639
1640 /* support for compat mode */
1641
1642 static int childPipe[2];
1643
1644 void
1645 meta_compat_start(void)
1646 {
1647 #ifdef USE_FILEMON_ONCE
1648 /*
1649 * We need to re-open filemon for each cmd.
1650 */
1651 BuildMon *pbm = &Mybm;
1652
1653 if (pbm->mfp != NULL && useFilemon) {
1654 meta_open_filemon(pbm);
1655 } else {
1656 pbm->mon_fd = -1;
1657 pbm->filemon = NULL;
1658 }
1659 #endif
1660 if (pipe(childPipe) < 0)
1661 Punt("Cannot create pipe: %s", strerror(errno));
1662 /* Set close-on-exec flag for both */
1663 (void)fcntl(childPipe[0], F_SETFD, FD_CLOEXEC);
1664 (void)fcntl(childPipe[1], F_SETFD, FD_CLOEXEC);
1665 }
1666
1667 void
1668 meta_compat_child(void)
1669 {
1670 meta_job_child(NULL);
1671 if (dup2(childPipe[1], 1) < 0 ||
1672 dup2(1, 2) < 0) {
1673 execError("dup2", "pipe");
1674 _exit(1);
1675 }
1676 }
1677
1678 void
1679 meta_compat_parent(pid_t child)
1680 {
1681 int outfd, metafd, maxfd, nfds;
1682 char buf[BUFSIZ+1];
1683 fd_set readfds;
1684
1685 meta_job_parent(NULL, child);
1686 close(childPipe[1]); /* child side */
1687 outfd = childPipe[0];
1688 #ifdef USE_FILEMON
1689 metafd = Mybm.filemon ? filemon_readfd(Mybm.filemon) : -1;
1690 #else
1691 metafd = -1;
1692 #endif
1693 maxfd = -1;
1694 if (outfd > maxfd)
1695 maxfd = outfd;
1696 if (metafd > maxfd)
1697 maxfd = metafd;
1698
1699 while (outfd != -1 || metafd != -1) {
1700 FD_ZERO(&readfds);
1701 if (outfd != -1) {
1702 FD_SET(outfd, &readfds);
1703 }
1704 if (metafd != -1) {
1705 FD_SET(metafd, &readfds);
1706 }
1707 nfds = select(maxfd + 1, &readfds, NULL, NULL, NULL);
1708 if (nfds == -1) {
1709 if (errno == EINTR)
1710 continue;
1711 err(1, "select");
1712 }
1713
1714 if (outfd != -1 && FD_ISSET(outfd, &readfds)) do {
1715 /* XXX this is not line-buffered */
1716 ssize_t nread = read(outfd, buf, sizeof(buf) - 1);
1717 if (nread == -1)
1718 err(1, "read");
1719 if (nread == 0) {
1720 close(outfd);
1721 outfd = -1;
1722 break;
1723 }
1724 fwrite(buf, 1, (size_t)nread, stdout);
1725 fflush(stdout);
1726 buf[nread] = '\0';
1727 meta_job_output(NULL, buf, "");
1728 } while (0);
1729 if (metafd != -1 && FD_ISSET(metafd, &readfds)) {
1730 if (meta_job_event(NULL) <= 0)
1731 metafd = -1;
1732 }
1733 }
1734 }
1735
1736 #endif /* USE_META */
1737