meta.c revision 1.11 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 static char cwd[MAXPATHLEN];
767 char ldir_vname[64];
768 char latestdir[MAXPATHLEN];
769 char fname[MAXPATHLEN];
770 char fname1[MAXPATHLEN];
771 char fname2[MAXPATHLEN];
772 char *p;
773 char *cp;
774 static size_t cwdlen = 0;
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 meta_name(gn, fname, sizeof(fname), NULL, NULL);
791
792 #ifdef DEBUG_META_MODE
793 if (DEBUG(META))
794 fprintf(debug_file, "meta_oodate: %s\n", fname);
795 #endif
796
797 if ((fp = fopen(fname, "r")) != NULL) {
798 static char *buf = NULL;
799 static size_t bufsz;
800 int lineno = 0;
801 int lastpid = 0;
802 int pid;
803 int f = 0;
804 int x;
805 LstNode ln;
806 struct stat fs;
807
808 if (!buf) {
809 bufsz = 8 * BUFSIZ;
810 buf = bmake_malloc(bufsz);
811 }
812
813 if (!cwdlen) {
814 if (getcwd(cwd, sizeof(cwd)) == NULL)
815 err(1, "Could not get current working directory");
816 cwdlen = strlen(cwd);
817 }
818
819 if (!tmpdir) {
820 tmpdir = getTmpdir();
821 tmplen = strlen(tmpdir);
822 }
823
824 /* we want to track all the .meta we read */
825 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
826
827 ln = Lst_First(gn->commands);
828 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
829 lineno++;
830 if (buf[x - 1] == '\n')
831 buf[x - 1] = '\0';
832 else
833 warnx("%s: %d: line truncated at %u", fname, lineno, x);
834
835 /* Find the start of the build monitor section. */
836 if (!f) {
837 if (strncmp(buf, "-- filemon", 10) == 0) {
838 f = 1;
839 continue;
840 }
841 if (strncmp(buf, "# buildmon", 10) == 0) {
842 f = 1;
843 continue;
844 }
845 }
846
847 /* Delimit the record type. */
848 p = buf;
849 #ifdef DEBUG_META_MODE
850 if (DEBUG(META))
851 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
852 #endif
853 strsep(&p, " ");
854 if (f) {
855 /*
856 * We are in the 'filemon' output section.
857 * Each record from filemon follows the general form:
858 *
859 * <key> <pid> <data>
860 *
861 * Where:
862 * <key> is a single letter, denoting the syscall.
863 * <pid> is the process that made the syscall.
864 * <data> is the arguments (of interest).
865 */
866 switch(buf[0]) {
867 case '#': /* comment */
868 case 'V': /* version */
869 break;
870 default:
871 /*
872 * We need to track pathnames per-process.
873 *
874 * Each process run by make, starts off in the 'CWD'
875 * recorded in the .meta file, if it chdirs ('C')
876 * elsewhere we need to track that - but only for
877 * that process. If it forks ('F'), we initialize
878 * the child to have the same cwd as its parent.
879 *
880 * We also need to track the 'latestdir' of
881 * interest. This is usually the same as cwd, but
882 * not if a process is reading directories.
883 *
884 * Each time we spot a different process ('pid')
885 * we save the current value of 'latestdir' in a
886 * variable qualified by 'lastpid', and
887 * re-initialize 'latestdir' to any pre-saved
888 * value for the current 'pid' and 'CWD' if none.
889 */
890 pid = atoi(p);
891 if (pid > 0 && pid != lastpid) {
892 char *ldir;
893 char *tp;
894
895 if (lastpid > 0) {
896 /* We need to remember this. */
897 Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
898 }
899 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
900 lastpid = pid;
901 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
902 if (ldir) {
903 strlcpy(latestdir, ldir, sizeof(latestdir));
904 if (tp)
905 free(tp);
906 } else
907 strlcpy(latestdir, cwd, sizeof(latestdir));
908 }
909 /* Skip past the pid. */
910 if (strsep(&p, " ") == NULL)
911 continue;
912 #ifdef DEBUG_META_MODE
913 if (DEBUG(META))
914 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, latestdir);
915 #endif
916 break;
917 }
918
919 /* Process according to record type. */
920 switch (buf[0]) {
921 case 'X': /* eXit */
922 Var_Delete(ldir_vname, VAR_GLOBAL);
923 lastpid = 0; /* no need to save ldir_vname */
924 break;
925
926 case 'F': /* [v]Fork */
927 {
928 char cldir[64];
929 int child;
930
931 child = atoi(p);
932 if (child > 0) {
933 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
934 Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
935 }
936 }
937 break;
938
939 case 'C': /* Chdir */
940 /* Update the latest directory. */
941 strlcpy(latestdir, p, sizeof(latestdir));
942 break;
943
944 case 'R': /* Read */
945 case 'E': /* Exec */
946 /*
947 * Check for runtime files that can't
948 * be part of the dependencies because
949 * they are _expected_ to change.
950 */
951 if (strncmp(p, "/tmp/", 5) == 0 ||
952 (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0))
953 break;
954
955 if (strncmp(p, "/var/", 5) == 0)
956 break;
957
958 /* Ignore device files. */
959 if (strncmp(p, "/dev/", 5) == 0)
960 break;
961
962 /* Ignore /etc/ files. */
963 if (strncmp(p, "/etc/", 5) == 0)
964 break;
965
966 /*
967 * The rest of the record is the file name.
968 * Check if it's not an absolute path.
969 */
970 {
971 char *sdirs[4];
972 char **sdp;
973 int sdx = 0;
974 int found = 0;
975
976 if (*p == '/') {
977 sdirs[sdx++] = p; /* done */
978 } else {
979 if (strcmp(".", p) == 0)
980 continue; /* no point */
981
982 /* Check vs latestdir */
983 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
984 sdirs[sdx++] = fname1;
985
986 if (strcmp(latestdir, cwd) != 0) {
987 /* Check vs cwd */
988 snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p);
989 sdirs[sdx++] = fname2;
990 }
991 }
992 sdirs[sdx++] = NULL;
993
994 for (sdp = sdirs; *sdp && !found; sdp++) {
995 #ifdef DEBUG_META_MODE
996 if (DEBUG(META))
997 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
998 #endif
999 if (stat(*sdp, &fs) == 0) {
1000 found = 1;
1001 p = *sdp;
1002 }
1003 }
1004 if (found) {
1005 #ifdef DEBUG_META_MODE
1006 if (DEBUG(META))
1007 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1008 #endif
1009 if (!S_ISDIR(fs.st_mode) &&
1010 fs.st_mtime > gn->mtime) {
1011 if (DEBUG(META))
1012 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1013 oodate = TRUE;
1014 } else if (S_ISDIR(fs.st_mode)) {
1015 /* Update the latest directory. */
1016 realpath(p, latestdir);
1017 }
1018 } else if (errno == ENOENT && *p == '/' &&
1019 strncmp(p, cwd, cwdlen) != 0) {
1020 /*
1021 * A referenced file outside of CWD is missing.
1022 * We cannot catch every eventuality here...
1023 */
1024 if (DEBUG(META))
1025 fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
1026 oodate = TRUE;
1027 }
1028 }
1029 break;
1030 default:
1031 break;
1032 }
1033 } else if (strcmp(buf, "CMD") == 0) {
1034 /*
1035 * Compare the current command with the one in the
1036 * meta data file.
1037 */
1038 if (ln == NULL) {
1039 if (DEBUG(META))
1040 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1041 oodate = TRUE;
1042 } else {
1043 char *cmd = (char *)Lst_Datum(ln);
1044
1045 if (!ignoreOODATE) {
1046 if (strstr(cmd, "$?"))
1047 ignoreOODATE = TRUE;
1048 else if ((cp = strstr(cmd, ".OODATE"))) {
1049 /* check for $[{(].OODATE[)}] */
1050 if (cp > cmd + 2 && cp[-2] == '$')
1051 ignoreOODATE = TRUE;
1052 }
1053 if (ignoreOODATE && DEBUG(META))
1054 fprintf(debug_file, "%s: %d: cannot compare commands using .OODATE\n", fname, lineno);
1055 }
1056 cmd = Var_Subst(NULL, cmd, gn, TRUE);
1057
1058 if ((cp = strchr(cmd, '\n'))) {
1059 int n;
1060
1061 /*
1062 * This command contains newlines, we need to
1063 * fetch more from the .meta file before we
1064 * attempt a comparison.
1065 */
1066 /* first put the newline back at buf[x - 1] */
1067 buf[x - 1] = '\n';
1068 do {
1069 /* now fetch the next line */
1070 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1071 break;
1072 x = n;
1073 lineno++;
1074 if (buf[x - 1] != '\n') {
1075 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1076 break;
1077 }
1078 cp = strchr(++cp, '\n');
1079 } while (cp);
1080 if (buf[x - 1] == '\n')
1081 buf[x - 1] = '\0';
1082 }
1083 if (!ignoreOODATE &&
1084 !(gn->type & OP_NOMETA_CMP) &&
1085 strcmp(p, cmd) != 0) {
1086 if (DEBUG(META))
1087 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1088 if (!metaIgnoreCMDs)
1089 oodate = TRUE;
1090 }
1091 free(cmd);
1092 ln = Lst_Succ(ln);
1093 }
1094 } else if (strcmp(buf, "CWD") == 0) {
1095 if (strcmp(p, cwd) != 0) {
1096 if (DEBUG(META))
1097 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1098 oodate = TRUE;
1099 }
1100 }
1101 }
1102
1103 /*
1104 * Check if there are extra commands now
1105 * that weren't in the meta data file.
1106 */
1107 if (!oodate && ln != NULL) {
1108 if (DEBUG(META))
1109 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1110 oodate = TRUE;
1111 }
1112
1113 fclose(fp);
1114 }
1115 if (oodate && ignoreOODATE) {
1116 /*
1117 * Target uses .OODATE, so we need to re-compute it.
1118 * We need to clean up what Make_DoAllVar() did.
1119 */
1120 Var_Delete(ALLSRC, gn);
1121 Var_Delete(OODATE, gn);
1122 gn->flags &= ~DONE_ALLSRC;
1123 }
1124 return oodate;
1125 }
1126
1127 /* support for compat mode */
1128
1129 static int childPipe[2];
1130
1131 void
1132 meta_compat_start(void)
1133 {
1134 #ifdef USE_FILEMON_ONCE
1135 /*
1136 * We need to re-open filemon for each cmd.
1137 */
1138 BuildMon *pbm = &Mybm;
1139
1140 if (pbm->mfp != NULL && useFilemon) {
1141 filemon_open(pbm);
1142 } else {
1143 pbm->mon_fd = pbm->filemon_fd = -1;
1144 }
1145 #endif
1146 if (pipe(childPipe) < 0)
1147 Punt("Cannot create pipe: %s", strerror(errno));
1148 /* Set close-on-exec flag for both */
1149 (void)fcntl(childPipe[0], F_SETFD, 1);
1150 (void)fcntl(childPipe[1], F_SETFD, 1);
1151 }
1152
1153 void
1154 meta_compat_child(void)
1155 {
1156 meta_job_child(NULL);
1157 if (dup2(childPipe[1], 1) < 0 ||
1158 dup2(1, 2) < 0) {
1159 execError("dup2", "pipe");
1160 _exit(1);
1161 }
1162 }
1163
1164 void
1165 meta_compat_parent(void)
1166 {
1167 FILE *fp;
1168 char buf[BUFSIZ];
1169
1170 close(childPipe[1]); /* child side */
1171 fp = fdopen(childPipe[0], "r");
1172 while (fgets(buf, sizeof(buf), fp)) {
1173 meta_job_output(NULL, buf, "");
1174 printf("%s", buf);
1175 }
1176 fclose(fp);
1177 }
1178
1179 #endif /* USE_META */
1180