meta.c revision 1.117 1 /* $NetBSD: meta.c,v 1.117 2020/09/22 20:19:46 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 int
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 return 0;
383 }
384
385 /*
386 * Certain node types never get a .meta file
387 */
388 #define SKIP_META_TYPE(_type) do { \
389 if ((gn->type & __CONCAT(OP_, _type))) { \
390 if (verbose) { \
391 fprintf(debug_file, "Skipping meta for %s: .%s\n", \
392 gn->name, __STRING(_type)); \
393 } \
394 return FALSE; \
395 } \
396 } while (0)
397
398
399 /*
400 * Do we need/want a .meta file ?
401 */
402 static Boolean
403 meta_needed(GNode *gn, const char *dname, const char *tname,
404 char *objdir, int verbose)
405 {
406 struct make_stat mst;
407
408 if (verbose)
409 verbose = DEBUG(META);
410
411 /* This may be a phony node which we don't want meta data for... */
412 /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
413 /* Or it may be explicitly flagged as .NOMETA */
414 SKIP_META_TYPE(NOMETA);
415 /* Unless it is explicitly flagged as .META */
416 if (!(gn->type & OP_META)) {
417 SKIP_META_TYPE(PHONY);
418 SKIP_META_TYPE(SPECIAL);
419 SKIP_META_TYPE(MAKE);
420 }
421
422 /* Check if there are no commands to execute. */
423 if (Lst_IsEmpty(gn->commands)) {
424 if (verbose)
425 fprintf(debug_file, "Skipping meta for %s: no commands\n",
426 gn->name);
427 return FALSE;
428 }
429 if ((gn->type & (OP_META|OP_SUBMAKE)) == OP_SUBMAKE) {
430 /* OP_SUBMAKE is a bit too aggressive */
431 if (Lst_ForEach(gn->commands, is_submake, gn)) {
432 if (DEBUG(META))
433 fprintf(debug_file, "Skipping meta for %s: .SUBMAKE\n",
434 gn->name);
435 return FALSE;
436 }
437 }
438
439 /* The object directory may not exist. Check it.. */
440 if (cached_stat(dname, &mst) != 0) {
441 if (verbose)
442 fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
443 gn->name);
444 return FALSE;
445 }
446
447 /* make sure these are canonical */
448 if (cached_realpath(dname, objdir))
449 dname = objdir;
450
451 /* If we aren't in the object directory, don't create a meta file. */
452 if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
453 if (verbose)
454 fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
455 gn->name);
456 return FALSE;
457 }
458 return TRUE;
459 }
460
461
462 static FILE *
463 meta_create(BuildMon *pbm, GNode *gn)
464 {
465 meta_file_t mf;
466 char buf[MAXPATHLEN];
467 char objdir[MAXPATHLEN];
468 char **ptr;
469 const char *dname;
470 const char *tname;
471 char *fname;
472 const char *cp;
473 char *p[5]; /* >= possible uses */
474 int i;
475
476 mf.fp = NULL;
477 i = 0;
478
479 dname = Var_Value(".OBJDIR", gn, &p[i++]);
480 tname = Var_Value(TARGET, gn, &p[i++]);
481
482 /* if this succeeds objdir is realpath of dname */
483 if (!meta_needed(gn, dname, tname, objdir, TRUE))
484 goto out;
485 dname = objdir;
486
487 if (metaVerbose) {
488 char *mp;
489
490 /* Describe the target we are building */
491 (void)Var_Subst("${" MAKE_META_PREFIX "}", gn, VARE_WANTRES, &mp);
492 /* TODO: handle errors */
493 if (*mp)
494 fprintf(stdout, "%s\n", mp);
495 free(mp);
496 }
497 /* Get the basename of the target */
498 if ((cp = strrchr(tname, '/')) == NULL) {
499 cp = tname;
500 } else {
501 cp++;
502 }
503
504 fflush(stdout);
505
506 if (!writeMeta)
507 /* Don't create meta data. */
508 goto out;
509
510 fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
511 dname, tname, objdir);
512
513 #ifdef DEBUG_META_MODE
514 if (DEBUG(META))
515 fprintf(debug_file, "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 Lst_ForEach(gn->commands, printCMD, &mf);
526
527 fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
528 fprintf(mf.fp, "TARGET %s\n", tname);
529 cp = Var_Value(".OODATE", gn, &p[i++]);
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 for (i--; i >= 0; i--) {
550 bmake_free(p[i]);
551 }
552
553 return mf.fp;
554 }
555
556 static Boolean
557 boolValue(char *s)
558 {
559 switch(*s) {
560 case '0':
561 case 'N':
562 case 'n':
563 case 'F':
564 case 'f':
565 return FALSE;
566 }
567 return TRUE;
568 }
569
570 /*
571 * Initialization we need before reading makefiles.
572 */
573 void
574 meta_init(void)
575 {
576 #ifdef USE_FILEMON
577 /* this allows makefiles to test if we have filemon support */
578 Var_Set(".MAKE.PATH_FILEMON", filemon_path(), VAR_GLOBAL);
579 #endif
580 }
581
582
583 #define get_mode_bf(bf, token) \
584 if ((cp = strstr(make_mode, token))) \
585 bf = boolValue(&cp[sizeof(token) - 1])
586
587 /*
588 * Initialization we need after reading makefiles.
589 */
590 void
591 meta_mode_init(const char *make_mode)
592 {
593 static int once = 0;
594 char *cp;
595
596 useMeta = TRUE;
597 useFilemon = TRUE;
598 writeMeta = TRUE;
599
600 if (make_mode) {
601 if (strstr(make_mode, "env"))
602 metaEnv = TRUE;
603 if (strstr(make_mode, "verb"))
604 metaVerbose = TRUE;
605 if (strstr(make_mode, "read"))
606 writeMeta = FALSE;
607 if (strstr(make_mode, "nofilemon"))
608 useFilemon = FALSE;
609 if (strstr(make_mode, "ignore-cmd"))
610 metaIgnoreCMDs = TRUE;
611 if (useFilemon)
612 get_mode_bf(filemonMissing, "missing-filemon=");
613 get_mode_bf(metaCurdirOk, "curdirok=");
614 get_mode_bf(metaMissing, "missing-meta=");
615 get_mode_bf(metaSilent, "silent=");
616 }
617 if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
618 /*
619 * The default value for MAKE_META_PREFIX
620 * prints the absolute path of the target.
621 * This works be cause :H will generate '.' if there is no /
622 * and :tA will resolve that to cwd.
623 */
624 Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL);
625 }
626 if (once)
627 return;
628 once = 1;
629 memset(&Mybm, 0, sizeof(Mybm));
630 /*
631 * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
632 */
633 metaBailiwick = Lst_Init();
634 (void)Var_Subst("${.MAKE.META.BAILIWICK:O:u:tA}",
635 VAR_GLOBAL, VARE_WANTRES, &metaBailiwickStr);
636 /* TODO: handle errors */
637 str2Lst_Append(metaBailiwick, metaBailiwickStr, NULL);
638 /*
639 * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
640 */
641 metaIgnorePaths = Lst_Init();
642 Var_Append(MAKE_META_IGNORE_PATHS,
643 "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}", VAR_GLOBAL);
644 (void)Var_Subst("${" MAKE_META_IGNORE_PATHS ":O:u:tA}",
645 VAR_GLOBAL, VARE_WANTRES, &metaIgnorePathsStr);
646 /* TODO: handle errors */
647 str2Lst_Append(metaIgnorePaths, metaIgnorePathsStr, NULL);
648
649 /*
650 * We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS}
651 */
652 cp = NULL;
653 if (Var_Value(MAKE_META_IGNORE_PATTERNS, VAR_GLOBAL, &cp)) {
654 metaIgnorePatterns = TRUE;
655 bmake_free(cp);
656 }
657 cp = NULL;
658 if (Var_Value(MAKE_META_IGNORE_FILTER, VAR_GLOBAL, &cp)) {
659 metaIgnoreFilter = TRUE;
660 bmake_free(cp);
661 }
662 }
663
664 /*
665 * In each case below we allow for job==NULL
666 */
667 void
668 meta_job_start(Job *job, GNode *gn)
669 {
670 BuildMon *pbm;
671
672 if (job != NULL) {
673 pbm = &job->bm;
674 } else {
675 pbm = &Mybm;
676 }
677 pbm->mfp = meta_create(pbm, gn);
678 #ifdef USE_FILEMON_ONCE
679 /* compat mode we open the filemon dev once per command */
680 if (job == NULL)
681 return;
682 #endif
683 #ifdef USE_FILEMON
684 if (pbm->mfp != NULL && useFilemon) {
685 meta_open_filemon(pbm);
686 } else {
687 pbm->mon_fd = -1;
688 pbm->filemon = NULL;
689 }
690 #endif
691 }
692
693 /*
694 * The child calls this before doing anything.
695 * It does not disturb our state.
696 */
697 void
698 meta_job_child(Job *job)
699 {
700 #ifdef USE_FILEMON
701 BuildMon *pbm;
702
703 if (job != NULL) {
704 pbm = &job->bm;
705 } else {
706 pbm = &Mybm;
707 }
708 if (pbm->mfp != NULL) {
709 close(fileno(pbm->mfp));
710 if (useFilemon && pbm->filemon) {
711 pid_t pid;
712
713 pid = getpid();
714 if (filemon_setpid_child(pbm->filemon, pid) == -1) {
715 err(1, "Could not set filemon pid!");
716 }
717 }
718 }
719 #endif
720 }
721
722 void
723 meta_job_parent(Job *job, pid_t pid)
724 {
725 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
726 BuildMon *pbm;
727
728 if (job != NULL) {
729 pbm = &job->bm;
730 } else {
731 pbm = &Mybm;
732 }
733 if (useFilemon && pbm->filemon) {
734 filemon_setpid_parent(pbm->filemon, pid);
735 }
736 #endif
737 }
738
739 int
740 meta_job_fd(Job *job)
741 {
742 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
743 BuildMon *pbm;
744
745 if (job != NULL) {
746 pbm = &job->bm;
747 } else {
748 pbm = &Mybm;
749 }
750 if (useFilemon && pbm->filemon) {
751 return filemon_readfd(pbm->filemon);
752 }
753 #endif
754 return -1;
755 }
756
757 int
758 meta_job_event(Job *job)
759 {
760 #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
761 BuildMon *pbm;
762
763 if (job != NULL) {
764 pbm = &job->bm;
765 } else {
766 pbm = &Mybm;
767 }
768 if (useFilemon && pbm->filemon) {
769 return filemon_process(pbm->filemon);
770 }
771 #endif
772 return 0;
773 }
774
775 void
776 meta_job_error(Job *job, GNode *gn, int flags, int status)
777 {
778 char cwd[MAXPATHLEN];
779 BuildMon *pbm;
780
781 if (job != NULL) {
782 pbm = &job->bm;
783 if (!gn)
784 gn = job->node;
785 } else {
786 pbm = &Mybm;
787 }
788 if (pbm->mfp != NULL) {
789 fprintf(pbm->mfp, "\n*** Error code %d%s\n",
790 status,
791 (flags & JOB_IGNERR) ?
792 "(ignored)" : "");
793 }
794 if (gn) {
795 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL);
796 }
797 getcwd(cwd, sizeof(cwd));
798 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL);
799 if (pbm->meta_fname[0]) {
800 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL);
801 }
802 meta_job_finish(job);
803 }
804
805 void
806 meta_job_output(Job *job, char *cp, const char *nl)
807 {
808 BuildMon *pbm;
809
810 if (job != NULL) {
811 pbm = &job->bm;
812 } else {
813 pbm = &Mybm;
814 }
815 if (pbm->mfp != NULL) {
816 if (metaVerbose) {
817 static char *meta_prefix = NULL;
818 static int meta_prefix_len;
819
820 if (!meta_prefix) {
821 char *cp2;
822
823 (void)Var_Subst("${" MAKE_META_PREFIX "}",
824 VAR_GLOBAL, VARE_WANTRES, &meta_prefix);
825 /* TODO: handle errors */
826 if ((cp2 = strchr(meta_prefix, '$')))
827 meta_prefix_len = cp2 - meta_prefix;
828 else
829 meta_prefix_len = strlen(meta_prefix);
830 }
831 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
832 cp = strchr(cp+1, '\n');
833 if (!cp++)
834 return;
835 }
836 }
837 fprintf(pbm->mfp, "%s%s", cp, nl);
838 }
839 }
840
841 int
842 meta_cmd_finish(void *pbmp)
843 {
844 int error = 0;
845 BuildMon *pbm = pbmp;
846 #ifdef USE_FILEMON
847 int x;
848 #endif
849
850 if (!pbm)
851 pbm = &Mybm;
852
853 #ifdef USE_FILEMON
854 if (pbm->filemon) {
855 while (filemon_process(pbm->filemon) > 0)
856 continue;
857 if (filemon_close(pbm->filemon) == -1)
858 error = errno;
859 x = filemon_read(pbm->mfp, pbm->mon_fd);
860 if (error == 0 && x != 0)
861 error = x;
862 pbm->mon_fd = -1;
863 pbm->filemon = NULL;
864 } else
865 #endif
866 fprintf(pbm->mfp, "\n"); /* ensure end with newline */
867 return error;
868 }
869
870 int
871 meta_job_finish(Job *job)
872 {
873 BuildMon *pbm;
874 int error = 0;
875 int x;
876
877 if (job != NULL) {
878 pbm = &job->bm;
879 } else {
880 pbm = &Mybm;
881 }
882 if (pbm->mfp != NULL) {
883 error = meta_cmd_finish(pbm);
884 x = fclose(pbm->mfp);
885 if (error == 0 && x != 0)
886 error = errno;
887 pbm->mfp = NULL;
888 pbm->meta_fname[0] = '\0';
889 }
890 return error;
891 }
892
893 void
894 meta_finish(void)
895 {
896 if (metaBailiwick != NULL)
897 Lst_Free(metaBailiwick);
898 free(metaBailiwickStr);
899 if (metaIgnorePaths != NULL)
900 Lst_Free(metaIgnorePaths);
901 free(metaIgnorePathsStr);
902 }
903
904 /*
905 * Fetch a full line from fp - growing bufp if needed
906 * Return length in bufp.
907 */
908 static int
909 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
910 {
911 char *buf = *bufp;
912 size_t bufsz = *szp;
913 struct stat fs;
914 int x;
915
916 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
917 check_newline:
918 x = o + strlen(&buf[o]);
919 if (buf[x - 1] == '\n')
920 return x;
921 /*
922 * We need to grow the buffer.
923 * The meta file can give us a clue.
924 */
925 if (fstat(fileno(fp), &fs) == 0) {
926 size_t newsz;
927 char *p;
928
929 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
930 if (newsz <= bufsz)
931 newsz = ROUNDUP(fs.st_size, BUFSIZ);
932 if (newsz <= bufsz)
933 return x; /* truncated */
934 if (DEBUG(META))
935 fprintf(debug_file, "growing buffer %zu -> %zu\n",
936 bufsz, newsz);
937 p = bmake_realloc(buf, newsz);
938 if (p) {
939 *bufp = buf = p;
940 *szp = bufsz = newsz;
941 /* fetch the rest */
942 if (!fgets(&buf[x], bufsz - x, fp))
943 return x; /* truncated! */
944 goto check_newline;
945 }
946 }
947 }
948 return 0;
949 }
950
951 /* Lst_ForEach wants 1 to stop search */
952 static int
953 prefix_match(void *p, void *q)
954 {
955 const char *prefix = p;
956 const char *path = q;
957 size_t n = strlen(prefix);
958
959 return strncmp(path, prefix, n) == 0;
960 }
961
962 /* See if the path equals prefix or starts with "prefix/". */
963 static Boolean
964 path_match(const void *p, const void *q)
965 {
966 const char *path = p;
967 const char *prefix = q;
968 size_t n = strlen(prefix);
969
970 if (strncmp(path, prefix, n) != 0)
971 return FALSE;
972 return path[n] == '\0' || path[n] == '/';
973 }
974
975 static Boolean
976 string_match(const void *p, const void *q)
977 {
978 return strcmp(p, q) == 0;
979 }
980
981
982 static int
983 meta_ignore(GNode *gn, const char *p)
984 {
985 char fname[MAXPATHLEN];
986
987 if (p == NULL)
988 return TRUE;
989
990 if (*p == '/') {
991 cached_realpath(p, fname); /* clean it up */
992 if (Lst_ForEach(metaIgnorePaths, prefix_match, fname)) {
993 #ifdef DEBUG_META_MODE
994 if (DEBUG(META))
995 fprintf(debug_file, "meta_oodate: ignoring path: %s\n",
996 p);
997 #endif
998 return TRUE;
999 }
1000 }
1001
1002 if (metaIgnorePatterns) {
1003 const char *expr;
1004 char *pm;
1005
1006 Var_Set(".p.", p, gn);
1007 expr = "${" MAKE_META_IGNORE_PATTERNS ":@m@${.p.:M$m}@}";
1008 (void)Var_Subst(expr, gn, VARE_WANTRES, &pm);
1009 /* TODO: handle errors */
1010 if (*pm) {
1011 #ifdef DEBUG_META_MODE
1012 if (DEBUG(META))
1013 fprintf(debug_file, "meta_oodate: ignoring pattern: %s\n",
1014 p);
1015 #endif
1016 free(pm);
1017 return TRUE;
1018 }
1019 free(pm);
1020 }
1021
1022 if (metaIgnoreFilter) {
1023 char *fm;
1024
1025 /* skip if filter result is empty */
1026 snprintf(fname, sizeof(fname),
1027 "${%s:L:${%s:ts:}}",
1028 p, MAKE_META_IGNORE_FILTER);
1029 (void)Var_Subst(fname, gn, VARE_WANTRES, &fm);
1030 /* TODO: handle errors */
1031 if (*fm == '\0') {
1032 #ifdef DEBUG_META_MODE
1033 if (DEBUG(META))
1034 fprintf(debug_file, "meta_oodate: ignoring filtered: %s\n",
1035 p);
1036 #endif
1037 free(fm);
1038 return TRUE;
1039 }
1040 free(fm);
1041 }
1042 return FALSE;
1043 }
1044
1045 /*
1046 * When running with 'meta' functionality, a target can be out-of-date
1047 * if any of the references in its meta data file is more recent.
1048 * We have to track the latestdir on a per-process basis.
1049 */
1050 #define LCWD_VNAME_FMT ".meta.%d.lcwd"
1051 #define LDIR_VNAME_FMT ".meta.%d.ldir"
1052
1053 /*
1054 * It is possible that a .meta file is corrupted,
1055 * if we detect this we want to reproduce it.
1056 * Setting oodate TRUE will have that effect.
1057 */
1058 #define CHECK_VALID_META(p) if (!(p && *p)) { \
1059 warnx("%s: %d: malformed", fname, lineno); \
1060 oodate = TRUE; \
1061 continue; \
1062 }
1063
1064 #define DEQUOTE(p) if (*p == '\'') { \
1065 char *ep; \
1066 p++; \
1067 if ((ep = strchr(p, '\''))) \
1068 *ep = '\0'; \
1069 }
1070
1071 Boolean
1072 meta_oodate(GNode *gn, Boolean oodate)
1073 {
1074 static char *tmpdir = NULL;
1075 static char cwd[MAXPATHLEN];
1076 char lcwd_vname[64];
1077 char ldir_vname[64];
1078 char lcwd[MAXPATHLEN];
1079 char latestdir[MAXPATHLEN];
1080 char fname[MAXPATHLEN];
1081 char fname1[MAXPATHLEN];
1082 char fname2[MAXPATHLEN];
1083 char fname3[MAXPATHLEN];
1084 const char *dname;
1085 const char *tname;
1086 char *p;
1087 char *cp;
1088 char *link_src;
1089 char *move_target;
1090 static size_t cwdlen = 0;
1091 static size_t tmplen = 0;
1092 FILE *fp;
1093 Boolean needOODATE = FALSE;
1094 StringList *missingFiles;
1095 char *pa[4]; /* >= possible uses */
1096 int i;
1097 int have_filemon = FALSE;
1098
1099 if (oodate)
1100 return oodate; /* we're done */
1101
1102 i = 0;
1103
1104 dname = Var_Value(".OBJDIR", gn, &pa[i++]);
1105 tname = Var_Value(TARGET, gn, &pa[i++]);
1106
1107 /* if this succeeds fname3 is realpath of dname */
1108 if (!meta_needed(gn, dname, tname, fname3, FALSE))
1109 goto oodate_out;
1110 dname = fname3;
1111
1112 missingFiles = Lst_Init();
1113
1114 /*
1115 * We need to check if the target is out-of-date. This includes
1116 * checking if the expanded command has changed. This in turn
1117 * requires that all variables are set in the same way that they
1118 * would be if the target needs to be re-built.
1119 */
1120 Make_DoAllVar(gn);
1121
1122 meta_name(gn, fname, sizeof(fname), dname, tname, dname);
1123
1124 #ifdef DEBUG_META_MODE
1125 if (DEBUG(META))
1126 fprintf(debug_file, "meta_oodate: %s\n", fname);
1127 #endif
1128
1129 if ((fp = fopen(fname, "r")) != NULL) {
1130 static char *buf = NULL;
1131 static size_t bufsz;
1132 int lineno = 0;
1133 int lastpid = 0;
1134 int pid;
1135 int x;
1136 StringListNode *cmdNode;
1137 struct make_stat mst;
1138
1139 if (!buf) {
1140 bufsz = 8 * BUFSIZ;
1141 buf = bmake_malloc(bufsz);
1142 }
1143
1144 if (!cwdlen) {
1145 if (getcwd(cwd, sizeof(cwd)) == NULL)
1146 err(1, "Could not get current working directory");
1147 cwdlen = strlen(cwd);
1148 }
1149 strlcpy(lcwd, cwd, sizeof(lcwd));
1150 strlcpy(latestdir, cwd, sizeof(latestdir));
1151
1152 if (!tmpdir) {
1153 tmpdir = getTmpdir();
1154 tmplen = strlen(tmpdir);
1155 }
1156
1157 /* we want to track all the .meta we read */
1158 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
1159
1160 cmdNode = Lst_First(gn->commands);
1161 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
1162 lineno++;
1163 if (buf[x - 1] == '\n')
1164 buf[x - 1] = '\0';
1165 else {
1166 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1167 oodate = TRUE;
1168 break;
1169 }
1170 link_src = NULL;
1171 move_target = NULL;
1172 /* Find the start of the build monitor section. */
1173 if (!have_filemon) {
1174 if (strncmp(buf, "-- filemon", 10) == 0) {
1175 have_filemon = TRUE;
1176 continue;
1177 }
1178 if (strncmp(buf, "# buildmon", 10) == 0) {
1179 have_filemon = TRUE;
1180 continue;
1181 }
1182 }
1183
1184 /* Delimit the record type. */
1185 p = buf;
1186 #ifdef DEBUG_META_MODE
1187 if (DEBUG(META))
1188 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
1189 #endif
1190 strsep(&p, " ");
1191 if (have_filemon) {
1192 /*
1193 * We are in the 'filemon' output section.
1194 * Each record from filemon follows the general form:
1195 *
1196 * <key> <pid> <data>
1197 *
1198 * Where:
1199 * <key> is a single letter, denoting the syscall.
1200 * <pid> is the process that made the syscall.
1201 * <data> is the arguments (of interest).
1202 */
1203 switch(buf[0]) {
1204 case '#': /* comment */
1205 case 'V': /* version */
1206 break;
1207 default:
1208 /*
1209 * We need to track pathnames per-process.
1210 *
1211 * Each process run by make, starts off in the 'CWD'
1212 * recorded in the .meta file, if it chdirs ('C')
1213 * elsewhere we need to track that - but only for
1214 * that process. If it forks ('F'), we initialize
1215 * the child to have the same cwd as its parent.
1216 *
1217 * We also need to track the 'latestdir' of
1218 * interest. This is usually the same as cwd, but
1219 * not if a process is reading directories.
1220 *
1221 * Each time we spot a different process ('pid')
1222 * we save the current value of 'latestdir' in a
1223 * variable qualified by 'lastpid', and
1224 * re-initialize 'latestdir' to any pre-saved
1225 * value for the current 'pid' and 'CWD' if none.
1226 */
1227 CHECK_VALID_META(p);
1228 pid = atoi(p);
1229 if (pid > 0 && pid != lastpid) {
1230 const char *ldir;
1231 char *tp;
1232
1233 if (lastpid > 0) {
1234 /* We need to remember these. */
1235 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL);
1236 Var_Set(ldir_vname, latestdir, VAR_GLOBAL);
1237 }
1238 snprintf(lcwd_vname, sizeof(lcwd_vname), LCWD_VNAME_FMT, pid);
1239 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
1240 lastpid = pid;
1241 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
1242 if (ldir) {
1243 strlcpy(latestdir, ldir, sizeof(latestdir));
1244 bmake_free(tp);
1245 }
1246 ldir = Var_Value(lcwd_vname, VAR_GLOBAL, &tp);
1247 if (ldir) {
1248 strlcpy(lcwd, ldir, sizeof(lcwd));
1249 bmake_free(tp);
1250 }
1251 }
1252 /* Skip past the pid. */
1253 if (strsep(&p, " ") == NULL)
1254 continue;
1255 #ifdef DEBUG_META_MODE
1256 if (DEBUG(META))
1257 fprintf(debug_file, "%s: %d: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
1258 fname, lineno,
1259 pid, buf[0], cwd, lcwd, latestdir);
1260 #endif
1261 break;
1262 }
1263
1264 CHECK_VALID_META(p);
1265
1266 /* Process according to record type. */
1267 switch (buf[0]) {
1268 case 'X': /* eXit */
1269 Var_Delete(lcwd_vname, VAR_GLOBAL);
1270 Var_Delete(ldir_vname, VAR_GLOBAL);
1271 lastpid = 0; /* no need to save ldir_vname */
1272 break;
1273
1274 case 'F': /* [v]Fork */
1275 {
1276 char cldir[64];
1277 int child;
1278
1279 child = atoi(p);
1280 if (child > 0) {
1281 snprintf(cldir, sizeof(cldir), LCWD_VNAME_FMT, child);
1282 Var_Set(cldir, lcwd, VAR_GLOBAL);
1283 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
1284 Var_Set(cldir, latestdir, VAR_GLOBAL);
1285 #ifdef DEBUG_META_MODE
1286 if (DEBUG(META))
1287 fprintf(debug_file, "%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n",
1288 fname, lineno,
1289 child, cwd, lcwd, latestdir);
1290 #endif
1291 }
1292 }
1293 break;
1294
1295 case 'C': /* Chdir */
1296 /* Update lcwd and latest directory. */
1297 strlcpy(latestdir, p, sizeof(latestdir));
1298 strlcpy(lcwd, p, sizeof(lcwd));
1299 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL);
1300 Var_Set(ldir_vname, lcwd, VAR_GLOBAL);
1301 #ifdef DEBUG_META_MODE
1302 if (DEBUG(META))
1303 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, lcwd);
1304 #endif
1305 break;
1306
1307 case 'M': /* renaMe */
1308 /*
1309 * For 'M'oves we want to check
1310 * the src as for 'R'ead
1311 * and the target as for 'W'rite.
1312 */
1313 cp = p; /* save this for a second */
1314 /* now get target */
1315 if (strsep(&p, " ") == NULL)
1316 continue;
1317 CHECK_VALID_META(p);
1318 move_target = p;
1319 p = cp;
1320 /* 'L' and 'M' put single quotes around the args */
1321 DEQUOTE(p);
1322 DEQUOTE(move_target);
1323 /* FALLTHROUGH */
1324 case 'D': /* unlink */
1325 if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1326 /* remove any missingFiles entries that match p */
1327 StringListNode *missingNode =
1328 Lst_Find(missingFiles, path_match, p);
1329 if (missingNode != NULL) {
1330 StringListNode *nln;
1331
1332 do {
1333 char *tp;
1334 nln = Lst_FindFrom(missingFiles,
1335 LstNode_Next(missingNode),
1336 path_match, p);
1337 tp = LstNode_Datum(missingNode);
1338 Lst_Remove(missingFiles, missingNode);
1339 free(tp);
1340 } while ((missingNode = nln) != NULL);
1341 }
1342 }
1343 if (buf[0] == 'M') {
1344 /* the target of the mv is a file 'W'ritten */
1345 #ifdef DEBUG_META_MODE
1346 if (DEBUG(META))
1347 fprintf(debug_file, "meta_oodate: M %s -> %s\n",
1348 p, move_target);
1349 #endif
1350 p = move_target;
1351 goto check_write;
1352 }
1353 break;
1354 case 'L': /* Link */
1355 /*
1356 * For 'L'inks check
1357 * the src as for 'R'ead
1358 * and the target as for 'W'rite.
1359 */
1360 link_src = p;
1361 /* now get target */
1362 if (strsep(&p, " ") == NULL)
1363 continue;
1364 CHECK_VALID_META(p);
1365 /* 'L' and 'M' put single quotes around the args */
1366 DEQUOTE(p);
1367 DEQUOTE(link_src);
1368 #ifdef DEBUG_META_MODE
1369 if (DEBUG(META))
1370 fprintf(debug_file, "meta_oodate: L %s -> %s\n",
1371 link_src, p);
1372 #endif
1373 /* FALLTHROUGH */
1374 case 'W': /* Write */
1375 check_write:
1376 /*
1377 * If a file we generated within our bailiwick
1378 * but outside of .OBJDIR is missing,
1379 * we need to do it again.
1380 */
1381 /* ignore non-absolute paths */
1382 if (*p != '/')
1383 break;
1384
1385 if (Lst_IsEmpty(metaBailiwick))
1386 break;
1387
1388 /* ignore cwd - normal dependencies handle those */
1389 if (strncmp(p, cwd, cwdlen) == 0)
1390 break;
1391
1392 if (!Lst_ForEach(metaBailiwick, prefix_match, p))
1393 break;
1394
1395 /* tmpdir might be within */
1396 if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1397 break;
1398
1399 /* ignore anything containing the string "tmp" */
1400 if ((strstr("tmp", p)))
1401 break;
1402
1403 if ((link_src != NULL && cached_lstat(p, &mst) < 0) ||
1404 (link_src == NULL && cached_stat(p, &mst) < 0)) {
1405 if (!meta_ignore(gn, p)) {
1406 if (Lst_Find(missingFiles, string_match, p) == NULL)
1407 Lst_Append(missingFiles, bmake_strdup(p));
1408 }
1409 }
1410 break;
1411 check_link_src:
1412 p = link_src;
1413 link_src = NULL;
1414 #ifdef DEBUG_META_MODE
1415 if (DEBUG(META))
1416 fprintf(debug_file, "meta_oodate: L src %s\n", p);
1417 #endif
1418 /* FALLTHROUGH */
1419 case 'R': /* Read */
1420 case 'E': /* Exec */
1421 /*
1422 * Check for runtime files that can't
1423 * be part of the dependencies because
1424 * they are _expected_ to change.
1425 */
1426 if (meta_ignore(gn, p))
1427 break;
1428
1429 /*
1430 * The rest of the record is the file name.
1431 * Check if it's not an absolute path.
1432 */
1433 {
1434 char *sdirs[4];
1435 char **sdp;
1436 int sdx = 0;
1437 int found = 0;
1438
1439 if (*p == '/') {
1440 sdirs[sdx++] = p; /* done */
1441 } else {
1442 if (strcmp(".", p) == 0)
1443 continue; /* no point */
1444
1445 /* Check vs latestdir */
1446 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1447 sdirs[sdx++] = fname1;
1448
1449 if (strcmp(latestdir, lcwd) != 0) {
1450 /* Check vs lcwd */
1451 snprintf(fname2, sizeof(fname2), "%s/%s", lcwd, p);
1452 sdirs[sdx++] = fname2;
1453 }
1454 if (strcmp(lcwd, cwd) != 0) {
1455 /* Check vs cwd */
1456 snprintf(fname3, sizeof(fname3), "%s/%s", cwd, p);
1457 sdirs[sdx++] = fname3;
1458 }
1459 }
1460 sdirs[sdx++] = NULL;
1461
1462 for (sdp = sdirs; *sdp && !found; sdp++) {
1463 #ifdef DEBUG_META_MODE
1464 if (DEBUG(META))
1465 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1466 #endif
1467 if (cached_stat(*sdp, &mst) == 0) {
1468 found = 1;
1469 p = *sdp;
1470 }
1471 }
1472 if (found) {
1473 #ifdef DEBUG_META_MODE
1474 if (DEBUG(META))
1475 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1476 #endif
1477 if (!S_ISDIR(mst.mst_mode) &&
1478 mst.mst_mtime > gn->mtime) {
1479 if (DEBUG(META))
1480 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1481 oodate = TRUE;
1482 } else if (S_ISDIR(mst.mst_mode)) {
1483 /* Update the latest directory. */
1484 cached_realpath(p, latestdir);
1485 }
1486 } else if (errno == ENOENT && *p == '/' &&
1487 strncmp(p, cwd, cwdlen) != 0) {
1488 /*
1489 * A referenced file outside of CWD is missing.
1490 * We cannot catch every eventuality here...
1491 */
1492 if (Lst_Find(missingFiles, string_match, p) == NULL)
1493 Lst_Append(missingFiles, bmake_strdup(p));
1494 }
1495 }
1496 if (buf[0] == 'E') {
1497 /* previous latestdir is no longer relevant */
1498 strlcpy(latestdir, lcwd, sizeof(latestdir));
1499 }
1500 break;
1501 default:
1502 break;
1503 }
1504 if (!oodate && buf[0] == 'L' && link_src != NULL)
1505 goto check_link_src;
1506 } else if (strcmp(buf, "CMD") == 0) {
1507 /*
1508 * Compare the current command with the one in the
1509 * meta data file.
1510 */
1511 if (cmdNode == NULL) {
1512 if (DEBUG(META))
1513 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1514 oodate = TRUE;
1515 } else {
1516 char *cmd = LstNode_Datum(cmdNode);
1517 Boolean hasOODATE = FALSE;
1518
1519 if (strstr(cmd, "$?"))
1520 hasOODATE = TRUE;
1521 else if ((cp = strstr(cmd, ".OODATE"))) {
1522 /* check for $[{(].OODATE[:)}] */
1523 if (cp > cmd + 2 && cp[-2] == '$')
1524 hasOODATE = TRUE;
1525 }
1526 if (hasOODATE) {
1527 needOODATE = TRUE;
1528 if (DEBUG(META))
1529 fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno);
1530 }
1531 (void)Var_Subst(cmd, gn, VARE_WANTRES|VARE_UNDEFERR, &cmd);
1532 /* TODO: handle errors */
1533
1534 if ((cp = strchr(cmd, '\n'))) {
1535 int n;
1536
1537 /*
1538 * This command contains newlines, we need to
1539 * fetch more from the .meta file before we
1540 * attempt a comparison.
1541 */
1542 /* first put the newline back at buf[x - 1] */
1543 buf[x - 1] = '\n';
1544 do {
1545 /* now fetch the next line */
1546 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1547 break;
1548 x = n;
1549 lineno++;
1550 if (buf[x - 1] != '\n') {
1551 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1552 break;
1553 }
1554 cp = strchr(++cp, '\n');
1555 } while (cp);
1556 if (buf[x - 1] == '\n')
1557 buf[x - 1] = '\0';
1558 }
1559 if (p &&
1560 !hasOODATE &&
1561 !(gn->type & OP_NOMETA_CMP) &&
1562 strcmp(p, cmd) != 0) {
1563 if (DEBUG(META))
1564 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1565 if (!metaIgnoreCMDs)
1566 oodate = TRUE;
1567 }
1568 free(cmd);
1569 cmdNode = LstNode_Next(cmdNode);
1570 }
1571 } else if (strcmp(buf, "CWD") == 0) {
1572 /*
1573 * Check if there are extra commands now
1574 * that weren't in the meta data file.
1575 */
1576 if (!oodate && cmdNode != NULL) {
1577 if (DEBUG(META))
1578 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1579 oodate = TRUE;
1580 }
1581 CHECK_VALID_META(p);
1582 if (strcmp(p, cwd) != 0) {
1583 if (DEBUG(META))
1584 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1585 oodate = TRUE;
1586 }
1587 }
1588 }
1589
1590 fclose(fp);
1591 if (!Lst_IsEmpty(missingFiles)) {
1592 if (DEBUG(META))
1593 fprintf(debug_file, "%s: missing files: %s...\n",
1594 fname, (char *)LstNode_Datum(Lst_First(missingFiles)));
1595 oodate = TRUE;
1596 }
1597 if (!oodate && !have_filemon && filemonMissing) {
1598 if (DEBUG(META))
1599 fprintf(debug_file, "%s: missing filemon data\n", fname);
1600 oodate = TRUE;
1601 }
1602 } else {
1603 if (writeMeta && (metaMissing || (gn->type & OP_META))) {
1604 cp = NULL;
1605
1606 /* if target is in .CURDIR we do not need a meta file */
1607 if (gn->path && (cp = strrchr(gn->path, '/')) && cp > gn->path) {
1608 if (strncmp(curdir, gn->path, (cp - gn->path)) != 0) {
1609 cp = NULL; /* not in .CURDIR */
1610 }
1611 }
1612 if (!cp) {
1613 if (DEBUG(META))
1614 fprintf(debug_file, "%s: required but missing\n", fname);
1615 oodate = TRUE;
1616 needOODATE = TRUE; /* assume the worst */
1617 }
1618 }
1619 }
1620
1621 Lst_Destroy(missingFiles, free);
1622
1623 if (oodate && needOODATE) {
1624 /*
1625 * Target uses .OODATE which is empty; or we wouldn't be here.
1626 * We have decided it is oodate, so .OODATE needs to be set.
1627 * All we can sanely do is set it to .ALLSRC.
1628 */
1629 Var_Delete(OODATE, gn);
1630 Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn);
1631 bmake_free(cp);
1632 }
1633
1634 oodate_out:
1635 for (i--; i >= 0; i--) {
1636 bmake_free(pa[i]);
1637 }
1638 return oodate;
1639 }
1640
1641 /* support for compat mode */
1642
1643 static int childPipe[2];
1644
1645 void
1646 meta_compat_start(void)
1647 {
1648 #ifdef USE_FILEMON_ONCE
1649 /*
1650 * We need to re-open filemon for each cmd.
1651 */
1652 BuildMon *pbm = &Mybm;
1653
1654 if (pbm->mfp != NULL && useFilemon) {
1655 meta_open_filemon(pbm);
1656 } else {
1657 pbm->mon_fd = -1;
1658 pbm->filemon = NULL;
1659 }
1660 #endif
1661 if (pipe(childPipe) < 0)
1662 Punt("Cannot create pipe: %s", strerror(errno));
1663 /* Set close-on-exec flag for both */
1664 (void)fcntl(childPipe[0], F_SETFD, FD_CLOEXEC);
1665 (void)fcntl(childPipe[1], F_SETFD, FD_CLOEXEC);
1666 }
1667
1668 void
1669 meta_compat_child(void)
1670 {
1671 meta_job_child(NULL);
1672 if (dup2(childPipe[1], 1) < 0 ||
1673 dup2(1, 2) < 0) {
1674 execError("dup2", "pipe");
1675 _exit(1);
1676 }
1677 }
1678
1679 void
1680 meta_compat_parent(pid_t child)
1681 {
1682 int outfd, metafd, maxfd, nfds;
1683 char buf[BUFSIZ+1];
1684 fd_set readfds;
1685
1686 meta_job_parent(NULL, child);
1687 close(childPipe[1]); /* child side */
1688 outfd = childPipe[0];
1689 #ifdef USE_FILEMON
1690 metafd = Mybm.filemon ? filemon_readfd(Mybm.filemon) : -1;
1691 #else
1692 metafd = -1;
1693 #endif
1694 maxfd = -1;
1695 if (outfd > maxfd)
1696 maxfd = outfd;
1697 if (metafd > maxfd)
1698 maxfd = metafd;
1699
1700 while (outfd != -1 || metafd != -1) {
1701 FD_ZERO(&readfds);
1702 if (outfd != -1) {
1703 FD_SET(outfd, &readfds);
1704 }
1705 if (metafd != -1) {
1706 FD_SET(metafd, &readfds);
1707 }
1708 nfds = select(maxfd + 1, &readfds, NULL, NULL, NULL);
1709 if (nfds == -1) {
1710 if (errno == EINTR)
1711 continue;
1712 err(1, "select");
1713 }
1714
1715 if (outfd != -1 && FD_ISSET(outfd, &readfds)) do {
1716 /* XXX this is not line-buffered */
1717 ssize_t nread = read(outfd, buf, sizeof(buf) - 1);
1718 if (nread == -1)
1719 err(1, "read");
1720 if (nread == 0) {
1721 close(outfd);
1722 outfd = -1;
1723 break;
1724 }
1725 fwrite(buf, 1, (size_t)nread, stdout);
1726 fflush(stdout);
1727 buf[nread] = '\0';
1728 meta_job_output(NULL, buf, "");
1729 } while (0);
1730 if (metafd != -1 && FD_ISSET(metafd, &readfds)) {
1731 if (meta_job_event(NULL) <= 0)
1732 metafd = -1;
1733 }
1734 }
1735 }
1736
1737 #endif /* USE_META */
1738