meta.c revision 1.111 1 /* $NetBSD: meta.c,v 1.111 2020/08/29 13:16:54 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 "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 const 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(cmd, gn, VARE_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 *cmd_freeIt = NULL;
372
373 if (strchr(cmd, '$')) {
374 cmd = cmd_freeIt = Var_Subst(cmd, mfp->gn, VARE_WANTRES);
375 }
376 fprintf(mfp->fp, "CMD %s\n", cmd);
377 free(cmd_freeIt);
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("${" MAKE_META_PREFIX "}", gn, VARE_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 bmake_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);
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);
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();
629 metaBailiwickStr = Var_Subst("${.MAKE.META.BAILIWICK:O:u:tA}",
630 VAR_GLOBAL, VARE_WANTRES);
631 str2Lst_Append(metaBailiwick, metaBailiwickStr, NULL);
632 /*
633 * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
634 */
635 metaIgnorePaths = Lst_Init();
636 Var_Append(MAKE_META_IGNORE_PATHS,
637 "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}", VAR_GLOBAL);
638 metaIgnorePathsStr = Var_Subst("${" MAKE_META_IGNORE_PATHS ":O:u:tA}",
639 VAR_GLOBAL, VARE_WANTRES);
640 str2Lst_Append(metaIgnorePaths, metaIgnorePathsStr, NULL);
641
642 /*
643 * We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS}
644 */
645 cp = NULL;
646 if (Var_Value(MAKE_META_IGNORE_PATTERNS, VAR_GLOBAL, &cp)) {
647 metaIgnorePatterns = TRUE;
648 bmake_free(cp);
649 }
650 cp = NULL;
651 if (Var_Value(MAKE_META_IGNORE_FILTER, VAR_GLOBAL, &cp)) {
652 metaIgnoreFilter = TRUE;
653 bmake_free(cp);
654 }
655 }
656
657 /*
658 * In each case below we allow for job==NULL
659 */
660 void
661 meta_job_start(Job *job, GNode *gn)
662 {
663 BuildMon *pbm;
664
665 if (job != NULL) {
666 pbm = &job->bm;
667 } else {
668 pbm = &Mybm;
669 }
670 pbm->mfp = meta_create(pbm, gn);
671 #ifdef USE_FILEMON_ONCE
672 /* compat mode we open the filemon dev once per command */
673 if (job == NULL)
674 return;
675 #endif
676 #ifdef USE_FILEMON
677 if (pbm->mfp != NULL && useFilemon) {
678 meta_open_filemon(pbm);
679 } else {
680 pbm->mon_fd = -1;
681 pbm->filemon = NULL;
682 }
683 #endif
684 }
685
686 /*
687 * The child calls this before doing anything.
688 * It does not disturb our state.
689 */
690 void
691 meta_job_child(Job *job)
692 {
693 #ifdef USE_FILEMON
694 BuildMon *pbm;
695
696 if (job != NULL) {
697 pbm = &job->bm;
698 } else {
699 pbm = &Mybm;
700 }
701 if (pbm->mfp != NULL) {
702 close(fileno(pbm->mfp));
703 if (useFilemon && pbm->filemon) {
704 pid_t pid;
705
706 pid = getpid();
707 if (filemon_setpid_child(pbm->filemon, pid) == -1) {
708 err(1, "Could not set filemon pid!");
709 }
710 }
711 }
712 #endif
713 }
714
715 void
716 meta_job_parent(Job *job, pid_t pid)
717 {
718 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
719 BuildMon *pbm;
720
721 if (job != NULL) {
722 pbm = &job->bm;
723 } else {
724 pbm = &Mybm;
725 }
726 if (useFilemon && pbm->filemon) {
727 filemon_setpid_parent(pbm->filemon, pid);
728 }
729 #endif
730 }
731
732 int
733 meta_job_fd(Job *job)
734 {
735 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
736 BuildMon *pbm;
737
738 if (job != NULL) {
739 pbm = &job->bm;
740 } else {
741 pbm = &Mybm;
742 }
743 if (useFilemon && pbm->filemon) {
744 return filemon_readfd(pbm->filemon);
745 }
746 #endif
747 return -1;
748 }
749
750 int
751 meta_job_event(Job *job)
752 {
753 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
754 BuildMon *pbm;
755
756 if (job != NULL) {
757 pbm = &job->bm;
758 } else {
759 pbm = &Mybm;
760 }
761 if (useFilemon && pbm->filemon) {
762 return filemon_process(pbm->filemon);
763 }
764 #endif
765 return 0;
766 }
767
768 void
769 meta_job_error(Job *job, GNode *gn, int flags, int status)
770 {
771 char cwd[MAXPATHLEN];
772 BuildMon *pbm;
773
774 if (job != NULL) {
775 pbm = &job->bm;
776 if (!gn)
777 gn = job->node;
778 } else {
779 pbm = &Mybm;
780 }
781 if (pbm->mfp != NULL) {
782 fprintf(pbm->mfp, "\n*** Error code %d%s\n",
783 status,
784 (flags & JOB_IGNERR) ?
785 "(ignored)" : "");
786 }
787 if (gn) {
788 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL);
789 }
790 getcwd(cwd, sizeof(cwd));
791 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL);
792 if (pbm->meta_fname[0]) {
793 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL);
794 }
795 meta_job_finish(job);
796 }
797
798 void
799 meta_job_output(Job *job, char *cp, const char *nl)
800 {
801 BuildMon *pbm;
802
803 if (job != NULL) {
804 pbm = &job->bm;
805 } else {
806 pbm = &Mybm;
807 }
808 if (pbm->mfp != NULL) {
809 if (metaVerbose) {
810 static char *meta_prefix = NULL;
811 static int meta_prefix_len;
812
813 if (!meta_prefix) {
814 char *cp2;
815
816 meta_prefix = Var_Subst("${" MAKE_META_PREFIX "}",
817 VAR_GLOBAL, VARE_WANTRES);
818 if ((cp2 = strchr(meta_prefix, '$')))
819 meta_prefix_len = cp2 - meta_prefix;
820 else
821 meta_prefix_len = strlen(meta_prefix);
822 }
823 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
824 cp = strchr(cp+1, '\n');
825 if (!cp++)
826 return;
827 }
828 }
829 fprintf(pbm->mfp, "%s%s", cp, nl);
830 }
831 }
832
833 int
834 meta_cmd_finish(void *pbmp)
835 {
836 int error = 0;
837 BuildMon *pbm = pbmp;
838 #ifdef USE_FILEMON
839 int x;
840 #endif
841
842 if (!pbm)
843 pbm = &Mybm;
844
845 #ifdef USE_FILEMON
846 if (pbm->filemon) {
847 while (filemon_process(pbm->filemon) > 0)
848 continue;
849 if (filemon_close(pbm->filemon) == -1)
850 error = errno;
851 x = filemon_read(pbm->mfp, pbm->mon_fd);
852 if (error == 0 && x != 0)
853 error = x;
854 pbm->mon_fd = -1;
855 pbm->filemon = NULL;
856 } else
857 #endif
858 fprintf(pbm->mfp, "\n"); /* ensure end with newline */
859 return error;
860 }
861
862 int
863 meta_job_finish(Job *job)
864 {
865 BuildMon *pbm;
866 int error = 0;
867 int x;
868
869 if (job != NULL) {
870 pbm = &job->bm;
871 } else {
872 pbm = &Mybm;
873 }
874 if (pbm->mfp != NULL) {
875 error = meta_cmd_finish(pbm);
876 x = fclose(pbm->mfp);
877 if (error == 0 && x != 0)
878 error = errno;
879 pbm->mfp = NULL;
880 pbm->meta_fname[0] = '\0';
881 }
882 return error;
883 }
884
885 void
886 meta_finish(void)
887 {
888 if (metaBailiwick != NULL)
889 Lst_Free(metaBailiwick);
890 free(metaBailiwickStr);
891 if (metaIgnorePaths != NULL)
892 Lst_Free(metaIgnorePaths);
893 free(metaIgnorePathsStr);
894 }
895
896 /*
897 * Fetch a full line from fp - growing bufp if needed
898 * Return length in bufp.
899 */
900 static int
901 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
902 {
903 char *buf = *bufp;
904 size_t bufsz = *szp;
905 struct stat fs;
906 int x;
907
908 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
909 check_newline:
910 x = o + strlen(&buf[o]);
911 if (buf[x - 1] == '\n')
912 return x;
913 /*
914 * We need to grow the buffer.
915 * The meta file can give us a clue.
916 */
917 if (fstat(fileno(fp), &fs) == 0) {
918 size_t newsz;
919 char *p;
920
921 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
922 if (newsz <= bufsz)
923 newsz = ROUNDUP(fs.st_size, BUFSIZ);
924 if (newsz <= bufsz)
925 return x; /* truncated */
926 if (DEBUG(META))
927 fprintf(debug_file, "growing buffer %zu -> %zu\n",
928 bufsz, newsz);
929 p = bmake_realloc(buf, newsz);
930 if (p) {
931 *bufp = buf = p;
932 *szp = bufsz = newsz;
933 /* fetch the rest */
934 if (!fgets(&buf[x], bufsz - x, fp))
935 return x; /* truncated! */
936 goto check_newline;
937 }
938 }
939 }
940 return 0;
941 }
942
943 /* Lst_ForEach wants 1 to stop search */
944 static int
945 prefix_match(void *p, void *q)
946 {
947 const char *prefix = p;
948 const char *path = q;
949 size_t n = strlen(prefix);
950
951 return strncmp(path, prefix, n) == 0;
952 }
953
954 /* See if the path equals prefix or starts with "prefix/". */
955 static Boolean
956 path_match(const void *p, const void *q)
957 {
958 const char *path = p;
959 const char *prefix = q;
960 size_t n = strlen(prefix);
961
962 if (strncmp(path, prefix, n) != 0)
963 return FALSE;
964 return path[n] == '\0' || path[n] == '/';
965 }
966
967 static Boolean
968 string_match(const void *p, const void *q)
969 {
970 return strcmp(p, q) == 0;
971 }
972
973
974 static int
975 meta_ignore(GNode *gn, const char *p)
976 {
977 char fname[MAXPATHLEN];
978
979 if (p == NULL)
980 return TRUE;
981
982 if (*p == '/') {
983 cached_realpath(p, fname); /* clean it up */
984 if (Lst_ForEach(metaIgnorePaths, prefix_match, fname)) {
985 #ifdef DEBUG_META_MODE
986 if (DEBUG(META))
987 fprintf(debug_file, "meta_oodate: ignoring path: %s\n",
988 p);
989 #endif
990 return TRUE;
991 }
992 }
993
994 if (metaIgnorePatterns) {
995 const char *expr;
996 char *pm;
997
998 Var_Set(".p.", p, gn);
999 expr = "${" MAKE_META_IGNORE_PATTERNS ":@m@${.p.:M$m}@}";
1000 pm = Var_Subst(expr, gn, VARE_WANTRES);
1001 if (*pm) {
1002 #ifdef DEBUG_META_MODE
1003 if (DEBUG(META))
1004 fprintf(debug_file, "meta_oodate: ignoring pattern: %s\n",
1005 p);
1006 #endif
1007 free(pm);
1008 return TRUE;
1009 }
1010 free(pm);
1011 }
1012
1013 if (metaIgnoreFilter) {
1014 char *fm;
1015
1016 /* skip if filter result is empty */
1017 snprintf(fname, sizeof(fname),
1018 "${%s:L:${%s:ts:}}",
1019 p, MAKE_META_IGNORE_FILTER);
1020 fm = Var_Subst(fname, gn, VARE_WANTRES);
1021 if (*fm == '\0') {
1022 #ifdef DEBUG_META_MODE
1023 if (DEBUG(META))
1024 fprintf(debug_file, "meta_oodate: ignoring filtered: %s\n",
1025 p);
1026 #endif
1027 free(fm);
1028 return TRUE;
1029 }
1030 free(fm);
1031 }
1032 return FALSE;
1033 }
1034
1035 /*
1036 * When running with 'meta' functionality, a target can be out-of-date
1037 * if any of the references in its meta data file is more recent.
1038 * We have to track the latestdir on a per-process basis.
1039 */
1040 #define LCWD_VNAME_FMT ".meta.%d.lcwd"
1041 #define LDIR_VNAME_FMT ".meta.%d.ldir"
1042
1043 /*
1044 * It is possible that a .meta file is corrupted,
1045 * if we detect this we want to reproduce it.
1046 * Setting oodate TRUE will have that effect.
1047 */
1048 #define CHECK_VALID_META(p) if (!(p && *p)) { \
1049 warnx("%s: %d: malformed", fname, lineno); \
1050 oodate = TRUE; \
1051 continue; \
1052 }
1053
1054 #define DEQUOTE(p) if (*p == '\'') { \
1055 char *ep; \
1056 p++; \
1057 if ((ep = strchr(p, '\''))) \
1058 *ep = '\0'; \
1059 }
1060
1061 Boolean
1062 meta_oodate(GNode *gn, Boolean oodate)
1063 {
1064 static char *tmpdir = NULL;
1065 static char cwd[MAXPATHLEN];
1066 char lcwd_vname[64];
1067 char ldir_vname[64];
1068 char lcwd[MAXPATHLEN];
1069 char latestdir[MAXPATHLEN];
1070 char fname[MAXPATHLEN];
1071 char fname1[MAXPATHLEN];
1072 char fname2[MAXPATHLEN];
1073 char fname3[MAXPATHLEN];
1074 const char *dname;
1075 const char *tname;
1076 char *p;
1077 char *cp;
1078 char *link_src;
1079 char *move_target;
1080 static size_t cwdlen = 0;
1081 static size_t tmplen = 0;
1082 FILE *fp;
1083 Boolean needOODATE = FALSE;
1084 Lst missingFiles;
1085 char *pa[4]; /* >= possible uses */
1086 int i;
1087 int have_filemon = FALSE;
1088
1089 if (oodate)
1090 return oodate; /* we're done */
1091
1092 i = 0;
1093
1094 dname = Var_Value(".OBJDIR", gn, &pa[i++]);
1095 tname = Var_Value(TARGET, gn, &pa[i++]);
1096
1097 /* if this succeeds fname3 is realpath of dname */
1098 if (!meta_needed(gn, dname, tname, fname3, FALSE))
1099 goto oodate_out;
1100 dname = fname3;
1101
1102 missingFiles = Lst_Init();
1103
1104 /*
1105 * We need to check if the target is out-of-date. This includes
1106 * checking if the expanded command has changed. This in turn
1107 * requires that all variables are set in the same way that they
1108 * would be if the target needs to be re-built.
1109 */
1110 Make_DoAllVar(gn);
1111
1112 meta_name(gn, fname, sizeof(fname), dname, tname, dname);
1113
1114 #ifdef DEBUG_META_MODE
1115 if (DEBUG(META))
1116 fprintf(debug_file, "meta_oodate: %s\n", fname);
1117 #endif
1118
1119 if ((fp = fopen(fname, "r")) != NULL) {
1120 static char *buf = NULL;
1121 static size_t bufsz;
1122 int lineno = 0;
1123 int lastpid = 0;
1124 int pid;
1125 int x;
1126 LstNode ln;
1127 struct stat fs;
1128
1129 if (!buf) {
1130 bufsz = 8 * BUFSIZ;
1131 buf = bmake_malloc(bufsz);
1132 }
1133
1134 if (!cwdlen) {
1135 if (getcwd(cwd, sizeof(cwd)) == NULL)
1136 err(1, "Could not get current working directory");
1137 cwdlen = strlen(cwd);
1138 }
1139 strlcpy(lcwd, cwd, sizeof(lcwd));
1140 strlcpy(latestdir, cwd, sizeof(latestdir));
1141
1142 if (!tmpdir) {
1143 tmpdir = getTmpdir();
1144 tmplen = strlen(tmpdir);
1145 }
1146
1147 /* we want to track all the .meta we read */
1148 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
1149
1150 ln = Lst_First(gn->commands);
1151 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
1152 lineno++;
1153 if (buf[x - 1] == '\n')
1154 buf[x - 1] = '\0';
1155 else {
1156 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1157 oodate = TRUE;
1158 break;
1159 }
1160 link_src = NULL;
1161 move_target = NULL;
1162 /* Find the start of the build monitor section. */
1163 if (!have_filemon) {
1164 if (strncmp(buf, "-- filemon", 10) == 0) {
1165 have_filemon = TRUE;
1166 continue;
1167 }
1168 if (strncmp(buf, "# buildmon", 10) == 0) {
1169 have_filemon = TRUE;
1170 continue;
1171 }
1172 }
1173
1174 /* Delimit the record type. */
1175 p = buf;
1176 #ifdef DEBUG_META_MODE
1177 if (DEBUG(META))
1178 fprintf(debug_file, "%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 char *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 fprintf(debug_file, "%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 fprintf(debug_file, "%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n",
1278 fname, lineno,
1279 child, cwd, lcwd, latestdir);
1280 #endif
1281 }
1282 }
1283 break;
1284
1285 case 'C': /* Chdir */
1286 /* Update lcwd and latest directory. */
1287 strlcpy(latestdir, p, sizeof(latestdir));
1288 strlcpy(lcwd, p, sizeof(lcwd));
1289 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL);
1290 Var_Set(ldir_vname, lcwd, VAR_GLOBAL);
1291 #ifdef DEBUG_META_MODE
1292 if (DEBUG(META))
1293 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, lcwd);
1294 #endif
1295 break;
1296
1297 case 'M': /* renaMe */
1298 /*
1299 * For 'M'oves we want to check
1300 * the src as for 'R'ead
1301 * and the target as for 'W'rite.
1302 */
1303 cp = p; /* save this for a second */
1304 /* now get target */
1305 if (strsep(&p, " ") == NULL)
1306 continue;
1307 CHECK_VALID_META(p);
1308 move_target = p;
1309 p = cp;
1310 /* 'L' and 'M' put single quotes around the args */
1311 DEQUOTE(p);
1312 DEQUOTE(move_target);
1313 /* FALLTHROUGH */
1314 case 'D': /* unlink */
1315 if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1316 /* remove any missingFiles entries that match p */
1317 ln = Lst_Find(missingFiles, path_match, p);
1318 if (ln != NULL) {
1319 LstNode nln;
1320 char *tp;
1321
1322 do {
1323 nln = Lst_FindFrom(missingFiles,
1324 LstNode_Next(ln),
1325 path_match, p);
1326 tp = Lst_Datum(ln);
1327 Lst_Remove(missingFiles, ln);
1328 free(tp);
1329 } while ((ln = nln) != NULL);
1330 }
1331 }
1332 if (buf[0] == 'M') {
1333 /* the target of the mv is a file 'W'ritten */
1334 #ifdef DEBUG_META_MODE
1335 if (DEBUG(META))
1336 fprintf(debug_file, "meta_oodate: M %s -> %s\n",
1337 p, move_target);
1338 #endif
1339 p = move_target;
1340 goto check_write;
1341 }
1342 break;
1343 case 'L': /* Link */
1344 /*
1345 * For 'L'inks check
1346 * the src as for 'R'ead
1347 * and the target as for 'W'rite.
1348 */
1349 link_src = p;
1350 /* now get target */
1351 if (strsep(&p, " ") == NULL)
1352 continue;
1353 CHECK_VALID_META(p);
1354 /* 'L' and 'M' put single quotes around the args */
1355 DEQUOTE(p);
1356 DEQUOTE(link_src);
1357 #ifdef DEBUG_META_MODE
1358 if (DEBUG(META))
1359 fprintf(debug_file, "meta_oodate: L %s -> %s\n",
1360 link_src, p);
1361 #endif
1362 /* FALLTHROUGH */
1363 case 'W': /* Write */
1364 check_write:
1365 /*
1366 * If a file we generated within our bailiwick
1367 * but outside of .OBJDIR is missing,
1368 * we need to do it again.
1369 */
1370 /* ignore non-absolute paths */
1371 if (*p != '/')
1372 break;
1373
1374 if (Lst_IsEmpty(metaBailiwick))
1375 break;
1376
1377 /* ignore cwd - normal dependencies handle those */
1378 if (strncmp(p, cwd, cwdlen) == 0)
1379 break;
1380
1381 if (!Lst_ForEach(metaBailiwick, prefix_match, p))
1382 break;
1383
1384 /* tmpdir might be within */
1385 if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1386 break;
1387
1388 /* ignore anything containing the string "tmp" */
1389 if ((strstr("tmp", p)))
1390 break;
1391
1392 if ((link_src != NULL && cached_lstat(p, &fs) < 0) ||
1393 (link_src == NULL && cached_stat(p, &fs) < 0)) {
1394 if (!meta_ignore(gn, p)) {
1395 if (Lst_Find(missingFiles, string_match, p) == NULL)
1396 Lst_Append(missingFiles, bmake_strdup(p));
1397 }
1398 }
1399 break;
1400 check_link_src:
1401 p = link_src;
1402 link_src = NULL;
1403 #ifdef DEBUG_META_MODE
1404 if (DEBUG(META))
1405 fprintf(debug_file, "meta_oodate: L src %s\n", p);
1406 #endif
1407 /* FALLTHROUGH */
1408 case 'R': /* Read */
1409 case 'E': /* Exec */
1410 /*
1411 * Check for runtime files that can't
1412 * be part of the dependencies because
1413 * they are _expected_ to change.
1414 */
1415 if (meta_ignore(gn, p))
1416 break;
1417
1418 /*
1419 * The rest of the record is the file name.
1420 * Check if it's not an absolute path.
1421 */
1422 {
1423 char *sdirs[4];
1424 char **sdp;
1425 int sdx = 0;
1426 int found = 0;
1427
1428 if (*p == '/') {
1429 sdirs[sdx++] = p; /* done */
1430 } else {
1431 if (strcmp(".", p) == 0)
1432 continue; /* no point */
1433
1434 /* Check vs latestdir */
1435 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1436 sdirs[sdx++] = fname1;
1437
1438 if (strcmp(latestdir, lcwd) != 0) {
1439 /* Check vs lcwd */
1440 snprintf(fname2, sizeof(fname2), "%s/%s", lcwd, p);
1441 sdirs[sdx++] = fname2;
1442 }
1443 if (strcmp(lcwd, cwd) != 0) {
1444 /* Check vs cwd */
1445 snprintf(fname3, sizeof(fname3), "%s/%s", cwd, p);
1446 sdirs[sdx++] = fname3;
1447 }
1448 }
1449 sdirs[sdx++] = NULL;
1450
1451 for (sdp = sdirs; *sdp && !found; sdp++) {
1452 #ifdef DEBUG_META_MODE
1453 if (DEBUG(META))
1454 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1455 #endif
1456 if (cached_stat(*sdp, &fs) == 0) {
1457 found = 1;
1458 p = *sdp;
1459 }
1460 }
1461 if (found) {
1462 #ifdef DEBUG_META_MODE
1463 if (DEBUG(META))
1464 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1465 #endif
1466 if (!S_ISDIR(fs.st_mode) &&
1467 fs.st_mtime > gn->mtime) {
1468 if (DEBUG(META))
1469 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1470 oodate = TRUE;
1471 } else if (S_ISDIR(fs.st_mode)) {
1472 /* Update the latest directory. */
1473 cached_realpath(p, latestdir);
1474 }
1475 } else if (errno == ENOENT && *p == '/' &&
1476 strncmp(p, cwd, cwdlen) != 0) {
1477 /*
1478 * A referenced file outside of CWD is missing.
1479 * We cannot catch every eventuality here...
1480 */
1481 if (Lst_Find(missingFiles, string_match, p) == NULL)
1482 Lst_Append(missingFiles, bmake_strdup(p));
1483 }
1484 }
1485 if (buf[0] == 'E') {
1486 /* previous latestdir is no longer relevant */
1487 strlcpy(latestdir, lcwd, sizeof(latestdir));
1488 }
1489 break;
1490 default:
1491 break;
1492 }
1493 if (!oodate && buf[0] == 'L' && link_src != NULL)
1494 goto check_link_src;
1495 } else if (strcmp(buf, "CMD") == 0) {
1496 /*
1497 * Compare the current command with the one in the
1498 * meta data file.
1499 */
1500 if (ln == NULL) {
1501 if (DEBUG(META))
1502 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1503 oodate = TRUE;
1504 } else {
1505 char *cmd = Lst_Datum(ln);
1506 Boolean hasOODATE = FALSE;
1507
1508 if (strstr(cmd, "$?"))
1509 hasOODATE = TRUE;
1510 else if ((cp = strstr(cmd, ".OODATE"))) {
1511 /* check for $[{(].OODATE[:)}] */
1512 if (cp > cmd + 2 && cp[-2] == '$')
1513 hasOODATE = TRUE;
1514 }
1515 if (hasOODATE) {
1516 needOODATE = TRUE;
1517 if (DEBUG(META))
1518 fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno);
1519 }
1520 cmd = Var_Subst(cmd, gn, VARE_WANTRES|VARE_UNDEFERR);
1521
1522 if ((cp = strchr(cmd, '\n'))) {
1523 int n;
1524
1525 /*
1526 * This command contains newlines, we need to
1527 * fetch more from the .meta file before we
1528 * attempt a comparison.
1529 */
1530 /* first put the newline back at buf[x - 1] */
1531 buf[x - 1] = '\n';
1532 do {
1533 /* now fetch the next line */
1534 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1535 break;
1536 x = n;
1537 lineno++;
1538 if (buf[x - 1] != '\n') {
1539 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1540 break;
1541 }
1542 cp = strchr(++cp, '\n');
1543 } while (cp);
1544 if (buf[x - 1] == '\n')
1545 buf[x - 1] = '\0';
1546 }
1547 if (p &&
1548 !hasOODATE &&
1549 !(gn->type & OP_NOMETA_CMP) &&
1550 strcmp(p, cmd) != 0) {
1551 if (DEBUG(META))
1552 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1553 if (!metaIgnoreCMDs)
1554 oodate = TRUE;
1555 }
1556 free(cmd);
1557 ln = LstNode_Next(ln);
1558 }
1559 } else if (strcmp(buf, "CWD") == 0) {
1560 /*
1561 * Check if there are extra commands now
1562 * that weren't in the meta data file.
1563 */
1564 if (!oodate && ln != NULL) {
1565 if (DEBUG(META))
1566 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1567 oodate = TRUE;
1568 }
1569 CHECK_VALID_META(p);
1570 if (strcmp(p, cwd) != 0) {
1571 if (DEBUG(META))
1572 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1573 oodate = TRUE;
1574 }
1575 }
1576 }
1577
1578 fclose(fp);
1579 if (!Lst_IsEmpty(missingFiles)) {
1580 if (DEBUG(META))
1581 fprintf(debug_file, "%s: missing files: %s...\n",
1582 fname, (char *)Lst_Datum(Lst_First(missingFiles)));
1583 oodate = TRUE;
1584 }
1585 if (!oodate && !have_filemon && filemonMissing) {
1586 if (DEBUG(META))
1587 fprintf(debug_file, "%s: missing filemon data\n", fname);
1588 oodate = TRUE;
1589 }
1590 } else {
1591 if (writeMeta && (metaMissing || (gn->type & OP_META))) {
1592 cp = NULL;
1593
1594 /* if target is in .CURDIR we do not need a meta file */
1595 if (gn->path && (cp = strrchr(gn->path, '/')) && cp > gn->path) {
1596 if (strncmp(curdir, gn->path, (cp - gn->path)) != 0) {
1597 cp = NULL; /* not in .CURDIR */
1598 }
1599 }
1600 if (!cp) {
1601 if (DEBUG(META))
1602 fprintf(debug_file, "%s: required but missing\n", fname);
1603 oodate = TRUE;
1604 needOODATE = TRUE; /* assume the worst */
1605 }
1606 }
1607 }
1608
1609 Lst_Destroy(missingFiles, free);
1610
1611 if (oodate && needOODATE) {
1612 /*
1613 * Target uses .OODATE which is empty; or we wouldn't be here.
1614 * We have decided it is oodate, so .OODATE needs to be set.
1615 * All we can sanely do is set it to .ALLSRC.
1616 */
1617 Var_Delete(OODATE, gn);
1618 Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn);
1619 bmake_free(cp);
1620 }
1621
1622 oodate_out:
1623 for (i--; i >= 0; i--) {
1624 bmake_free(pa[i]);
1625 }
1626 return oodate;
1627 }
1628
1629 /* support for compat mode */
1630
1631 static int childPipe[2];
1632
1633 void
1634 meta_compat_start(void)
1635 {
1636 #ifdef USE_FILEMON_ONCE
1637 /*
1638 * We need to re-open filemon for each cmd.
1639 */
1640 BuildMon *pbm = &Mybm;
1641
1642 if (pbm->mfp != NULL && useFilemon) {
1643 meta_open_filemon(pbm);
1644 } else {
1645 pbm->mon_fd = -1;
1646 pbm->filemon = NULL;
1647 }
1648 #endif
1649 if (pipe(childPipe) < 0)
1650 Punt("Cannot create pipe: %s", strerror(errno));
1651 /* Set close-on-exec flag for both */
1652 (void)fcntl(childPipe[0], F_SETFD, FD_CLOEXEC);
1653 (void)fcntl(childPipe[1], F_SETFD, FD_CLOEXEC);
1654 }
1655
1656 void
1657 meta_compat_child(void)
1658 {
1659 meta_job_child(NULL);
1660 if (dup2(childPipe[1], 1) < 0 ||
1661 dup2(1, 2) < 0) {
1662 execError("dup2", "pipe");
1663 _exit(1);
1664 }
1665 }
1666
1667 void
1668 meta_compat_parent(pid_t child)
1669 {
1670 int outfd, metafd, maxfd, nfds;
1671 char buf[BUFSIZ+1];
1672 fd_set readfds;
1673
1674 meta_job_parent(NULL, child);
1675 close(childPipe[1]); /* child side */
1676 outfd = childPipe[0];
1677 #ifdef USE_FILEMON
1678 metafd = Mybm.filemon ? filemon_readfd(Mybm.filemon) : -1;
1679 #else
1680 metafd = -1;
1681 #endif
1682 maxfd = -1;
1683 if (outfd > maxfd)
1684 maxfd = outfd;
1685 if (metafd > maxfd)
1686 maxfd = metafd;
1687
1688 while (outfd != -1 || metafd != -1) {
1689 FD_ZERO(&readfds);
1690 if (outfd != -1) {
1691 FD_SET(outfd, &readfds);
1692 }
1693 if (metafd != -1) {
1694 FD_SET(metafd, &readfds);
1695 }
1696 nfds = select(maxfd + 1, &readfds, NULL, NULL, NULL);
1697 if (nfds == -1) {
1698 if (errno == EINTR)
1699 continue;
1700 err(1, "select");
1701 }
1702
1703 if (outfd != -1 && FD_ISSET(outfd, &readfds)) do {
1704 /* XXX this is not line-buffered */
1705 ssize_t nread = read(outfd, buf, sizeof(buf) - 1);
1706 if (nread == -1)
1707 err(1, "read");
1708 if (nread == 0) {
1709 close(outfd);
1710 outfd = -1;
1711 break;
1712 }
1713 fwrite(buf, 1, (size_t)nread, stdout);
1714 fflush(stdout);
1715 buf[nread] = '\0';
1716 meta_job_output(NULL, buf, "");
1717 } while (0);
1718 if (metafd != -1 && FD_ISSET(metafd, &readfds)) {
1719 if (meta_job_event(NULL) <= 0)
1720 metafd = -1;
1721 }
1722 }
1723 }
1724
1725 #endif /* USE_META */
1726