meta.c revision 1.9 1 /*
2 * Implement 'meta' mode.
3 * Adapted from John Birrell's patches to FreeBSD make.
4 * --sjg
5 */
6 /*
7 * Copyright (c) 2009-2010, Juniper Networks, Inc.
8 * Portions Copyright (c) 2009, John Birrell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #if defined(USE_META)
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36 #include <sys/stat.h>
37 #include <sys/ioctl.h>
38 #include <fcntl.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 HAVE_FILEMON_H
49 # include <filemon.h>
50 #endif
51 #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD)
52 # define USE_FILEMON
53 #endif
54
55 static BuildMon Mybm; /* for compat */
56
57 Boolean useMeta = FALSE;
58 static Boolean useFilemon = FALSE;
59 static Boolean writeMeta = FALSE;
60 static Boolean metaEnv = FALSE; /* don't save env unless asked */
61 static Boolean metaVerbose = FALSE;
62 static Boolean metaIgnoreCMDs = FALSE; /* ignore CMDs in .meta files */
63
64 extern Boolean forceJobs;
65 extern Boolean comatMake;
66
67 #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
68
69 #ifndef N2U
70 # define N2U(n, u) (((n) + ((u) - 1)) / (u))
71 #endif
72 #ifndef ROUNDUP
73 # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
74 #endif
75
76 #if !defined(HAVE_STRSEP)
77 # define strsep(s, d) stresep((s), (d), 0)
78 #endif
79
80 /*
81 * Filemon is a kernel module which snoops certain syscalls.
82 *
83 * C chdir
84 * E exec
85 * F [v]fork
86 * L [sym]link
87 * M rename
88 * R read
89 * W write
90 * S stat
91 *
92 * See meta_oodate below - we mainly care about 'E' and 'R'.
93 *
94 * We can still use meta mode without filemon, but
95 * the benefits are more limited.
96 */
97 #ifdef USE_FILEMON
98 # ifndef _PATH_FILEMON
99 # define _PATH_FILEMON "/dev/filemon"
100 # endif
101
102 /*
103 * Open the filemon device.
104 */
105 static void
106 filemon_open(BuildMon *pbm)
107 {
108 int retry;
109
110 pbm->mon_fd = pbm->filemon_fd = -1;
111 if (!useFilemon)
112 return;
113
114 for (retry = 5; retry >= 0; retry--) {
115 if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0)
116 break;
117 }
118
119 if (pbm->filemon_fd < 0) {
120 useFilemon = FALSE;
121 warn("Could not open %s", _PATH_FILEMON);
122 return;
123 }
124
125 /*
126 * We use a file outside of '.'
127 * to avoid a FreeBSD kernel bug where unlink invalidates
128 * cwd causing getcwd to do a lot more work.
129 * We only care about the descriptor.
130 */
131 pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
132 if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) {
133 err(1, "Could not set filemon file descriptor!");
134 }
135 /* we don't need these once we exec */
136 (void)fcntl(pbm->mon_fd, F_SETFD, 1);
137 (void)fcntl(pbm->filemon_fd, F_SETFD, 1);
138 }
139
140 /*
141 * Read the build monitor output file and write records to the target's
142 * metadata file.
143 */
144 static void
145 filemon_read(FILE *mfp, int fd)
146 {
147 FILE *fp;
148 char buf[BUFSIZ];
149
150 /* Check if we're not writing to a meta data file.*/
151 if (mfp == NULL) {
152 if (fd >= 0)
153 close(fd); /* not interested */
154 return;
155 }
156 /* rewind */
157 lseek(fd, SEEK_SET, 0);
158 if ((fp = fdopen(fd, "r")) == NULL)
159 err(1, "Could not read build monitor file '%d'", fd);
160
161 fprintf(mfp, "-- filemon acquired metadata --\n");
162
163 while (fgets(buf, sizeof(buf), fp)) {
164 fprintf(mfp, "%s", buf);
165 }
166 fflush(mfp);
167 clearerr(fp);
168 fclose(fp);
169 }
170 #endif
171
172 /*
173 * when realpath() fails,
174 * we use this, to clean up ./ and ../
175 */
176 static void
177 eat_dots(char *buf, size_t bufsz, int dots)
178 {
179 char *cp;
180 char *cp2;
181 const char *eat;
182 size_t eatlen;
183
184 switch (dots) {
185 case 1:
186 eat = "/./";
187 eatlen = 2;
188 break;
189 case 2:
190 eat = "/../";
191 eatlen = 3;
192 break;
193 default:
194 return;
195 }
196
197 do {
198 cp = strstr(buf, eat);
199 if (cp) {
200 cp2 = cp + eatlen;
201 if (dots == 2 && cp > buf) {
202 do {
203 cp--;
204 } while (cp > buf && *cp != '/');
205 }
206 if (*cp == '/') {
207 strlcpy(cp, cp2, bufsz - (cp - buf));
208 } else {
209 return; /* can't happen? */
210 }
211 }
212 } while (cp);
213 }
214
215 static char *
216 meta_name(struct GNode *gn, char *mname, size_t mnamelen,
217 const char *dname,
218 const char *tname)
219 {
220 char buf[MAXPATHLEN];
221 char cwd[MAXPATHLEN];
222 char *rp;
223 char *cp;
224 char *tp;
225 char *p[4]; /* >= number of possible uses */
226 int i;
227
228 i = 0;
229 if (!dname)
230 dname = Var_Value(".OBJDIR", gn, &p[i++]);
231 if (!tname)
232 tname = Var_Value(TARGET, gn, &p[i++]);
233
234 if (realpath(dname, cwd))
235 dname = cwd;
236
237 /*
238 * Weed out relative paths from the target file name.
239 * We have to be careful though since if target is a
240 * symlink, the result will be unstable.
241 * So we use realpath() just to get the dirname, and leave the
242 * basename as given to us.
243 */
244 if ((cp = strrchr(tname, '/'))) {
245 if (realpath(tname, buf)) {
246 if ((rp = strrchr(buf, '/'))) {
247 rp++;
248 cp++;
249 if (strcmp(cp, rp) != 0)
250 strlcpy(rp, cp, sizeof(buf) - (rp - buf));
251 }
252 tname = buf;
253 } else {
254 /*
255 * We likely have a directory which is about to be made.
256 * We pretend realpath() succeeded, to have a chance
257 * of generating the same meta file name that we will
258 * next time through.
259 */
260 if (tname[0] == '/') {
261 strlcpy(buf, tname, sizeof(buf));
262 } else {
263 snprintf(buf, sizeof(buf), "%s/%s", cwd, tname);
264 }
265 eat_dots(buf, sizeof(buf), 1); /* ./ */
266 eat_dots(buf, sizeof(buf), 2); /* ../ */
267 tname = buf;
268 }
269 }
270 /* on some systems dirname may modify its arg */
271 tp = bmake_strdup(tname);
272 if (strcmp(dname, dirname(tp)) == 0)
273 snprintf(mname, mnamelen, "%s.meta", tname);
274 else {
275 snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
276
277 /*
278 * Replace path separators in the file name after the
279 * current object directory path.
280 */
281 cp = mname + strlen(dname) + 1;
282
283 while (*cp != '\0') {
284 if (*cp == '/')
285 *cp = '_';
286 cp++;
287 }
288 }
289 free(tp);
290 for (i--; i >= 0; i--) {
291 if (p[i])
292 free(p[i]);
293 }
294 return (mname);
295 }
296
297 /*
298 * Return true if running ${.MAKE}
299 * Bypassed if target is flagged .MAKE
300 */
301 static int
302 is_submake(void *cmdp, void *gnp)
303 {
304 static char *p_make = NULL;
305 static int p_len;
306 char *cmd = cmdp;
307 GNode *gn = gnp;
308 char *mp = NULL;
309 char *cp;
310 char *cp2;
311 int rc = 0; /* keep looking */
312
313 if (!p_make) {
314 p_make = Var_Value(".MAKE", gn, &cp);
315 p_len = strlen(p_make);
316 }
317 cp = strchr(cmd, '$');
318 if ((cp)) {
319 mp = Var_Subst(NULL, cmd, gn, FALSE);
320 cmd = mp;
321 }
322 cp2 = strstr(cmd, p_make);
323 if ((cp2)) {
324 switch (cp2[p_len]) {
325 case '\0':
326 case ' ':
327 case '\t':
328 case '\n':
329 rc = 1;
330 break;
331 }
332 if (cp2 > cmd && rc > 0) {
333 switch (cp2[-1]) {
334 case ' ':
335 case '\t':
336 case '\n':
337 break;
338 default:
339 rc = 0; /* no match */
340 break;
341 }
342 }
343 }
344 if (mp)
345 free(mp);
346 return (rc);
347 }
348
349 typedef struct meta_file_s {
350 FILE *fp;
351 GNode *gn;
352 } meta_file_t;
353
354 static int
355 printCMD(void *cmdp, void *mfpp)
356 {
357 meta_file_t *mfp = mfpp;
358 char *cmd = cmdp;
359 char *cp = NULL;
360
361 if (strchr(cmd, '$')) {
362 cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE);
363 }
364 fprintf(mfp->fp, "CMD %s\n", cmd);
365 if (cp)
366 free(cp);
367 return 0;
368 }
369
370 /*
371 * Certain node types never get a .meta file
372 */
373 #define SKIP_META_TYPE(_type) do { \
374 if ((gn->type & __CONCAT(OP_, _type))) { \
375 if (DEBUG(META)) { \
376 fprintf(debug_file, "Skipping meta for %s: .%s\n", \
377 gn->name, __STRING(_type)); \
378 } \
379 return (NULL); \
380 } \
381 } while (0)
382
383 static FILE *
384 meta_create(BuildMon *pbm, GNode *gn)
385 {
386 extern char **environ;
387 meta_file_t mf;
388 char buf[MAXPATHLEN];
389 char objdir[MAXPATHLEN];
390 char **ptr;
391 const char *dname;
392 const char *tname;
393 char *fname;
394 const char *cp;
395 char *p[4]; /* >= possible uses */
396 int i;
397 struct stat fs;
398
399
400 /* This may be a phony node which we don't want meta data for... */
401 /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
402 /* Or it may be explicitly flagged as .NOMETA */
403 SKIP_META_TYPE(NOMETA);
404 /* Unless it is explicitly flagged as .META */
405 if (!(gn->type & OP_META)) {
406 SKIP_META_TYPE(PHONY);
407 SKIP_META_TYPE(SPECIAL);
408 SKIP_META_TYPE(MAKE);
409 }
410
411 mf.fp = NULL;
412
413 i = 0;
414
415 dname = Var_Value(".OBJDIR", gn, &p[i++]);
416 tname = Var_Value(TARGET, gn, &p[i++]);
417
418 /* The object directory may not exist. Check it.. */
419 if (stat(dname, &fs) != 0) {
420 if (DEBUG(META))
421 fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
422 gn->name);
423 goto out;
424 }
425 /* Check if there are no commands to execute. */
426 if (Lst_IsEmpty(gn->commands)) {
427 if (DEBUG(META))
428 fprintf(debug_file, "Skipping meta for %s: no commands\n",
429 gn->name);
430 goto out;
431 }
432
433 /* make sure these are canonical */
434 if (realpath(dname, objdir))
435 dname = objdir;
436
437 /* If we aren't in the object directory, don't create a meta file. */
438 if (strcmp(curdir, dname) == 0) {
439 if (DEBUG(META))
440 fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
441 gn->name);
442 goto out;
443 }
444 if (!(gn->type & OP_META)) {
445 /* We do not generate .meta files for sub-makes */
446 if (Lst_ForEach(gn->commands, is_submake, gn)) {
447 if (DEBUG(META))
448 fprintf(debug_file, "Skipping meta for %s: .MAKE\n",
449 gn->name);
450 goto out;
451 }
452 }
453
454 if (metaVerbose) {
455 char *mp;
456
457 /* Describe the target we are building */
458 mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, 0);
459 if (*mp)
460 fprintf(stdout, "%s\n", mp);
461 free(mp);
462 }
463 /* Get the basename of the target */
464 if ((cp = strrchr(tname, '/')) == NULL) {
465 cp = tname;
466 } else {
467 cp++;
468 }
469
470 fflush(stdout);
471
472 if (strcmp(cp, makeDependfile) == 0)
473 goto out;
474
475 if (!writeMeta)
476 /* Don't create meta data. */
477 goto out;
478
479 fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
480 dname, tname);
481
482 #ifdef DEBUG_META_MODE
483 if (DEBUG(META))
484 fprintf(debug_file, "meta_create: %s\n", fname);
485 #endif
486
487 if ((mf.fp = fopen(fname, "w")) == NULL)
488 err(1, "Could not open meta file '%s'", fname);
489
490 fprintf(mf.fp, "# Meta data file %s\n", fname);
491
492 mf.gn = gn;
493
494 Lst_ForEach(gn->commands, printCMD, &mf);
495
496 fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
497 fprintf(mf.fp, "TARGET %s\n", tname);
498
499 if (metaEnv) {
500 for (ptr = environ; *ptr != NULL; ptr++)
501 fprintf(mf.fp, "ENV %s\n", *ptr);
502 }
503
504 fprintf(mf.fp, "-- command output --\n");
505 fflush(mf.fp);
506
507 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
508 Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
509
510 out:
511 for (i--; i >= 0; i--) {
512 if (p[i])
513 free(p[i]);
514 }
515
516 return (mf.fp);
517 }
518
519
520 void
521 meta_init(const char *make_mode)
522 {
523 static int once = 0;
524
525 useMeta = TRUE;
526 useFilemon = TRUE;
527 writeMeta = TRUE;
528
529 if (make_mode) {
530 if (strstr(make_mode, "env"))
531 metaEnv = TRUE;
532 if (strstr(make_mode, "verb"))
533 metaVerbose = TRUE;
534 if (strstr(make_mode, "read"))
535 writeMeta = FALSE;
536 if (strstr(make_mode, "nofilemon"))
537 useFilemon = FALSE;
538 if (strstr(make_mode, "ignore-cmd"))
539 metaIgnoreCMDs = TRUE;
540 /* for backwards compatability */
541 Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
542 Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
543 }
544 if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
545 /*
546 * The default value for MAKE_META_PREFIX
547 * prints the absolute path of the target.
548 * This works be cause :H will generate '.' if there is no /
549 * and :tA will resolve that to cwd.
550 */
551 Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
552 }
553 if (once)
554 return;
555 once = 1;
556 memset(&Mybm, 0, sizeof(Mybm));
557 }
558
559 /*
560 * In each case below we allow for job==NULL
561 */
562 void
563 meta_job_start(Job *job, GNode *gn)
564 {
565 BuildMon *pbm;
566
567 if (job != NULL) {
568 pbm = &job->bm;
569 } else {
570 pbm = &Mybm;
571 }
572 pbm->mfp = meta_create(pbm, gn);
573 #ifdef USE_FILEMON_ONCE
574 /* compat mode we open the filemon dev once per command */
575 if (job == NULL)
576 return;
577 #endif
578 #ifdef USE_FILEMON
579 if (pbm->mfp != NULL && useFilemon) {
580 filemon_open(pbm);
581 } else {
582 pbm->mon_fd = pbm->filemon_fd = -1;
583 }
584 #endif
585 }
586
587 /*
588 * The child calls this before doing anything.
589 * It does not disturb our state.
590 */
591 void
592 meta_job_child(Job *job)
593 {
594 #ifdef USE_FILEMON
595 BuildMon *pbm;
596 pid_t pid;
597
598 if (job != NULL) {
599 pbm = &job->bm;
600 } else {
601 pbm = &Mybm;
602 }
603 pid = getpid();
604 if (pbm->mfp != NULL && useFilemon) {
605 if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
606 err(1, "Could not set filemon pid!");
607 }
608 }
609 #endif
610 }
611
612 void
613 meta_job_error(Job *job, GNode *gn, int flags, int status)
614 {
615 char cwd[MAXPATHLEN];
616 BuildMon *pbm;
617
618 if (job != NULL) {
619 pbm = &job->bm;
620 } else {
621 if (!gn)
622 gn = job->node;
623 pbm = &Mybm;
624 }
625 if (pbm->mfp != NULL) {
626 fprintf(pbm->mfp, "*** Error code %d%s\n",
627 status,
628 (flags & JOB_IGNERR) ?
629 "(ignored)" : "");
630 }
631 if (gn) {
632 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
633 }
634 getcwd(cwd, sizeof(cwd));
635 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
636 if (pbm && pbm->meta_fname[0]) {
637 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
638 }
639 }
640
641 void
642 meta_job_output(Job *job, char *cp, const char *nl)
643 {
644 BuildMon *pbm;
645
646 if (job != NULL) {
647 pbm = &job->bm;
648 } else {
649 pbm = &Mybm;
650 }
651 if (pbm->mfp != NULL) {
652 if (metaVerbose) {
653 static char *meta_prefix = NULL;
654 static int meta_prefix_len;
655
656 if (!meta_prefix) {
657 char *cp2;
658
659 meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
660 if ((cp2 = strchr(meta_prefix, '$')))
661 meta_prefix_len = cp2 - meta_prefix;
662 else
663 meta_prefix_len = strlen(meta_prefix);
664 }
665 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
666 cp = strchr(cp+1, '\n');
667 if (!cp++)
668 return;
669 }
670 }
671 fprintf(pbm->mfp, "%s%s", cp, nl);
672 }
673 }
674
675 void
676 meta_cmd_finish(void *pbmp)
677 {
678 #ifdef USE_FILEMON
679 BuildMon *pbm = pbmp;
680
681 if (!pbm)
682 pbm = &Mybm;
683
684 if (pbm->filemon_fd >= 0) {
685 close(pbm->filemon_fd);
686 filemon_read(pbm->mfp, pbm->mon_fd);
687 pbm->filemon_fd = pbm->mon_fd = -1;
688 }
689 #endif
690 }
691
692 void
693 meta_job_finish(Job *job)
694 {
695 BuildMon *pbm;
696
697 if (job != NULL) {
698 pbm = &job->bm;
699 } else {
700 pbm = &Mybm;
701 }
702 if (pbm->mfp != NULL) {
703 meta_cmd_finish(pbm);
704 fclose(pbm->mfp);
705 pbm->mfp = NULL;
706 pbm->meta_fname[0] = '\0';
707 }
708 }
709
710 /*
711 * Fetch a full line from fp - growing bufp if needed
712 * Return length in bufp.
713 */
714 static int
715 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
716 {
717 char *buf = *bufp;
718 size_t bufsz = *szp;
719 struct stat fs;
720 int x;
721
722 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
723 check_newline:
724 x = o + strlen(&buf[o]);
725 if (buf[x - 1] == '\n')
726 return x;
727 /*
728 * We need to grow the buffer.
729 * The meta file can give us a clue.
730 */
731 if (fstat(fileno(fp), &fs) == 0) {
732 size_t newsz;
733 char *p;
734
735 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
736 if (newsz <= bufsz)
737 newsz = ROUNDUP(fs.st_size, BUFSIZ);
738 if (DEBUG(META))
739 fprintf(debug_file, "growing buffer %u -> %u\n",
740 bufsz, newsz);
741 p = bmake_realloc(buf, newsz);
742 if (p) {
743 *bufp = buf = p;
744 *szp = bufsz = newsz;
745 /* fetch the rest */
746 if (!fgets(&buf[x], bufsz - x, fp))
747 return x; /* truncated! */
748 goto check_newline;
749 }
750 }
751 }
752 return 0;
753 }
754
755 /*
756 * When running with 'meta' functionality, a target can be out-of-date
757 * if any of the references in it's meta data file is more recent.
758 * We have to track the latestdir on a per-process basis.
759 */
760 #define LDIR_VNAME_FMT ".meta.%d.ldir"
761
762 Boolean
763 meta_oodate(GNode *gn, Boolean oodate)
764 {
765 static char *tmpdir = NULL;
766 char ldir_vname[64];
767 char cwd[MAXPATHLEN];
768 char latestdir[MAXPATHLEN];
769 char fname[MAXPATHLEN];
770 char fname1[MAXPATHLEN];
771 char fname2[MAXPATHLEN];
772 char *p;
773 char *cp;
774 size_t cwdlen;
775 static size_t tmplen = 0;
776 FILE *fp;
777 Boolean ignoreOODATE = FALSE;
778
779 if (oodate)
780 return oodate; /* we're done */
781
782 /*
783 * We need to check if the target is out-of-date. This includes
784 * checking if the expanded command has changed. This in turn
785 * requires that all variables are set in the same way that they
786 * would be if the target needs to be re-built.
787 */
788 Make_DoAllVar(gn);
789
790 if (getcwd(cwd, sizeof(cwd)) == NULL)
791 err(1, "Could not get current working directory");
792
793 meta_name(gn, fname, sizeof(fname), NULL, NULL);
794
795 #ifdef DEBUG_META_MODE
796 if (DEBUG(META))
797 fprintf(debug_file, "meta_oodate: %s\n", fname);
798 #endif
799
800 if ((fp = fopen(fname, "r")) != NULL) {
801 static char *buf = NULL;
802 static size_t bufsz;
803 int lineno = 0;
804 int lastpid = 0;
805 int pid;
806 int f = 0;
807 int x;
808 LstNode ln;
809 struct stat fs;
810
811 if (!buf) {
812 bufsz = 8 * BUFSIZ;
813 buf = bmake_malloc(bufsz);
814 }
815
816 if (!tmpdir) {
817 tmpdir = getTmpdir();
818 tmplen = strlen(tmpdir);
819 }
820
821 /* we want to track all the .meta we read */
822 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
823
824 cwdlen = strlen(cwd);
825
826 ln = Lst_First(gn->commands);
827 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
828 lineno++;
829 if (buf[x - 1] == '\n')
830 buf[x - 1] = '\0';
831 else
832 warnx("%s: %d: line truncated at %u", fname, lineno, x);
833
834 /* Find the start of the build monitor section. */
835 if (!f) {
836 if (strncmp(buf, "-- filemon", 10) == 0) {
837 f = 1;
838 continue;
839 }
840 if (strncmp(buf, "# buildmon", 10) == 0) {
841 f = 1;
842 continue;
843 }
844 }
845
846 /* Delimit the record type. */
847 p = buf;
848 #ifdef DEBUG_META_MODE
849 if (DEBUG(META))
850 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
851 #endif
852 strsep(&p, " ");
853 if (f) {
854 /*
855 * We are in the 'filemon' output section.
856 * Each record from filemon follows the general form:
857 *
858 * <key> <pid> <data>
859 *
860 * Where:
861 * <key> is a single letter, denoting the syscall.
862 * <pid> is the process that made the syscall.
863 * <data> is the arguments (of interest).
864 */
865 switch(buf[0]) {
866 case '#': /* comment */
867 case 'V': /* version */
868 break;
869 default:
870 /*
871 * We need to track pathnames per-process.
872 *
873 * Each process run by make, starts off in the 'CWD'
874 * recorded in the .meta file, if it chdirs ('C')
875 * elsewhere we need to track that - but only for
876 * that process. If it forks ('F'), we initialize
877 * the child to have the same cwd as its parent.
878 *
879 * We also need to track the 'latestdir' of
880 * interest. This is usually the same as cwd, but
881 * not if a process is reading directories.
882 *
883 * Each time we spot a different process ('pid')
884 * we save the current value of 'latestdir' in a
885 * variable qualified by 'lastpid', and
886 * re-initialize 'latestdir' to any pre-saved
887 * value for the current 'pid' and 'CWD' if none.
888 */
889 pid = atoi(p);
890 if (pid > 0 && pid != lastpid) {
891 char *ldir;
892 char *tp;
893
894 if (lastpid > 0) {
895 /* We need to remember this. */
896 Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
897 }
898 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
899 lastpid = pid;
900 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
901 if (ldir) {
902 strlcpy(latestdir, ldir, sizeof(latestdir));
903 if (tp)
904 free(tp);
905 } else
906 strlcpy(latestdir, cwd, sizeof(latestdir));
907 }
908 /* Skip past the pid. */
909 if (strsep(&p, " ") == NULL)
910 continue;
911 #ifdef DEBUG_META_MODE
912 if (DEBUG(META))
913 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, latestdir);
914 #endif
915 break;
916 }
917
918 /* Process according to record type. */
919 switch (buf[0]) {
920 case 'X': /* eXit */
921 Var_Delete(ldir_vname, VAR_GLOBAL);
922 lastpid = 0; /* no need to save ldir_vname */
923 break;
924
925 case 'F': /* [v]Fork */
926 {
927 char cldir[64];
928 int child;
929
930 child = atoi(p);
931 if (child > 0) {
932 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
933 Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
934 }
935 }
936 break;
937
938 case 'C': /* Chdir */
939 /* Update the latest directory. */
940 strlcpy(latestdir, p, sizeof(latestdir));
941 break;
942
943 case 'R': /* Read */
944 case 'E': /* Exec */
945 /*
946 * Check for runtime files that can't
947 * be part of the dependencies because
948 * they are _expected_ to change.
949 */
950 if (strncmp(p, "/tmp/", 5) == 0 ||
951 (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0))
952 break;
953
954 if (strncmp(p, "/var/", 5) == 0)
955 break;
956
957 /* Ignore device files. */
958 if (strncmp(p, "/dev/", 5) == 0)
959 break;
960
961 /* Ignore /etc/ files. */
962 if (strncmp(p, "/etc/", 5) == 0)
963 break;
964
965 /*
966 * The rest of the record is the file name.
967 * Check if it's not an absolute path.
968 */
969 {
970 char *sdirs[4];
971 char **sdp;
972 int sdx = 0;
973 int found = 0;
974
975 if (*p == '/') {
976 sdirs[sdx++] = p; /* done */
977 } else {
978 if (strcmp(".", p) == 0)
979 continue; /* no point */
980
981 /* Check vs latestdir */
982 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
983 sdirs[sdx++] = fname1;
984
985 if (strcmp(latestdir, cwd) != 0) {
986 /* Check vs cwd */
987 snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p);
988 sdirs[sdx++] = fname2;
989 }
990 }
991 sdirs[sdx++] = NULL;
992
993 for (sdp = sdirs; *sdp && !found; sdp++) {
994 #ifdef DEBUG_META_MODE
995 if (DEBUG(META))
996 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
997 #endif
998 if (stat(*sdp, &fs) == 0) {
999 found = 1;
1000 p = *sdp;
1001 }
1002 }
1003 if (found) {
1004 #ifdef DEBUG_META_MODE
1005 if (DEBUG(META))
1006 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1007 #endif
1008 if (!S_ISDIR(fs.st_mode) &&
1009 fs.st_mtime > gn->mtime) {
1010 if (DEBUG(META))
1011 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1012 oodate = TRUE;
1013 } else if (S_ISDIR(fs.st_mode)) {
1014 /* Update the latest directory. */
1015 realpath(p, latestdir);
1016 }
1017 } else if (errno == ENOENT && *p == '/' &&
1018 strncmp(p, cwd, cwdlen) != 0) {
1019 /*
1020 * A referenced file outside of CWD is missing.
1021 * We cannot catch every eventuality here...
1022 */
1023 if (DEBUG(META))
1024 fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
1025 oodate = TRUE;
1026 }
1027 }
1028 break;
1029 default:
1030 break;
1031 }
1032 } else if (strcmp(buf, "CMD") == 0) {
1033 /*
1034 * Compare the current command with the one in the
1035 * meta data file.
1036 */
1037 if (ln == NULL) {
1038 if (DEBUG(META))
1039 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1040 oodate = TRUE;
1041 } else {
1042 char *cmd = (char *)Lst_Datum(ln);
1043
1044 if (!ignoreOODATE) {
1045 if (strstr(cmd, "$?"))
1046 ignoreOODATE = TRUE;
1047 else if ((cp = strstr(cmd, ".OODATE"))) {
1048 /* check for $[{(].OODATE[)}] */
1049 if (cp > cmd + 2 && cp[-2] == '$')
1050 ignoreOODATE = TRUE;
1051 }
1052 if (ignoreOODATE && DEBUG(META))
1053 fprintf(debug_file, "%s: %d: cannot compare commands using .OODATE\n", fname, lineno);
1054 }
1055 cmd = Var_Subst(NULL, cmd, gn, TRUE);
1056
1057 if ((cp = strchr(cmd, '\n'))) {
1058 int n;
1059
1060 /*
1061 * This command contains newlines, we need to
1062 * fetch more from the .meta file before we
1063 * attempt a comparison.
1064 */
1065 /* first put the newline back at buf[x - 1] */
1066 buf[x - 1] = '\n';
1067 do {
1068 /* now fetch the next line */
1069 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1070 break;
1071 x = n;
1072 lineno++;
1073 if (buf[x - 1] != '\n') {
1074 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1075 break;
1076 }
1077 cp = strchr(++cp, '\n');
1078 } while (cp);
1079 if (buf[x - 1] == '\n')
1080 buf[x - 1] = '\0';
1081 }
1082 if (!ignoreOODATE &&
1083 !(gn->type & OP_NOMETA_CMP) &&
1084 strcmp(p, cmd) != 0) {
1085 if (DEBUG(META))
1086 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1087 if (!metaIgnoreCMDs)
1088 oodate = TRUE;
1089 }
1090 free(cmd);
1091 ln = Lst_Succ(ln);
1092 }
1093 } else if (strcmp(buf, "CWD") == 0) {
1094 if (strcmp(p, curdir) != 0) {
1095 if (DEBUG(META))
1096 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1097 oodate = TRUE;
1098 }
1099 }
1100 }
1101
1102 /*
1103 * Check if there are extra commands now
1104 * that weren't in the meta data file.
1105 */
1106 if (!oodate && ln != NULL) {
1107 if (DEBUG(META))
1108 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1109 oodate = TRUE;
1110 }
1111
1112 fclose(fp);
1113 }
1114 if (oodate && ignoreOODATE) {
1115 /*
1116 * Target uses .OODATE, so we need to re-compute it.
1117 * We need to clean up what Make_DoAllVar() did.
1118 */
1119 Var_Delete(ALLSRC, gn);
1120 Var_Delete(OODATE, gn);
1121 gn->flags &= ~DONE_ALLSRC;
1122 }
1123 return oodate;
1124 }
1125
1126 /* support for compat mode */
1127
1128 static int childPipe[2];
1129
1130 void
1131 meta_compat_start(void)
1132 {
1133 #ifdef USE_FILEMON_ONCE
1134 /*
1135 * We need to re-open filemon for each cmd.
1136 */
1137 BuildMon *pbm = &Mybm;
1138
1139 if (pbm->mfp != NULL && useFilemon) {
1140 filemon_open(pbm);
1141 } else {
1142 pbm->mon_fd = pbm->filemon_fd = -1;
1143 }
1144 #endif
1145 if (pipe(childPipe) < 0)
1146 Punt("Cannot create pipe: %s", strerror(errno));
1147 /* Set close-on-exec flag for both */
1148 (void)fcntl(childPipe[0], F_SETFD, 1);
1149 (void)fcntl(childPipe[1], F_SETFD, 1);
1150 }
1151
1152 void
1153 meta_compat_child(void)
1154 {
1155 meta_job_child(NULL);
1156 if (dup2(childPipe[1], 1) < 0 ||
1157 dup2(1, 2) < 0) {
1158 execError("dup2", "pipe");
1159 _exit(1);
1160 }
1161 }
1162
1163 void
1164 meta_compat_parent(void)
1165 {
1166 FILE *fp;
1167 char buf[BUFSIZ];
1168
1169 close(childPipe[1]); /* child side */
1170 fp = fdopen(childPipe[0], "r");
1171 while (fgets(buf, sizeof(buf), fp)) {
1172 meta_job_output(NULL, buf, "");
1173 printf("%s", buf);
1174 }
1175 fclose(fp);
1176 }
1177
1178 #endif /* USE_META */
1179