meta.c revision 1.1 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
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35 #include <sys/stat.h>
36 #include <sys/ioctl.h>
37 #include <fcntl.h>
38 #include <libgen.h>
39 #include <errno.h>
40 #include <err.h>
41
42 #include "make.h"
43 #include "job.h"
44
45 #ifdef HAVE_FILEMON_H
46 # include <filemon.h>
47 #endif
48 #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD)
49 # define USE_FILEMON
50 #endif
51
52 static BuildMon Mybm; /* for compat */
53
54 Boolean useMeta = FALSE;
55 static Boolean useFilemon = FALSE;
56 static Boolean writeMeta = FALSE;
57 static Boolean metaEnv = FALSE; /* don't save env unless asked */
58 static Boolean metaVerbose = FALSE;
59 static Boolean metaIgnoreCMDs = FALSE; /* ignore CMDs in .meta files */
60
61 extern Boolean forceJobs;
62 extern Boolean comatMake;
63
64 #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
65
66 #ifndef N2U
67 # define N2U(n, u) (((n) + ((u) - 1)) / (u))
68 #endif
69 #ifndef ROUNDUP
70 # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
71 #endif
72
73 /*
74 * Filemon is a kernel module which snoops certain syscalls.
75 *
76 * C chdir
77 * E exec
78 * F [v]fork
79 * L [sym]link
80 * M rename
81 * R read
82 * W write
83 * S stat
84 *
85 * See meta_oodate below - we mainly care about 'E' and 'R'.
86 *
87 * We can still use meta mode without filemon, but
88 * the benefits are more limited.
89 */
90 #ifdef USE_FILEMON
91 # ifndef _PATH_FILEMON
92 # define _PATH_FILEMON "/dev/filemon"
93 # endif
94
95 /*
96 * Open the filemon device.
97 */
98 static void
99 filemon_open(BuildMon *pbm)
100 {
101 int retry;
102
103 pbm->mon_fd = pbm->filemon_fd = -1;
104 if (!useFilemon)
105 return;
106
107 for (retry = 5; retry >= 0; retry--) {
108 if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0)
109 break;
110 }
111
112 if (pbm->filemon_fd < 0) {
113 useFilemon = FALSE;
114 warn("Could not open %s", _PATH_FILEMON);
115 return;
116 }
117
118 /*
119 * We use a file outside of '.'
120 * to avoid a FreeBSD kernel bug where unlink invalidates
121 * cwd causing getcwd to do a lot more work.
122 * We only care about the descriptor.
123 */
124 pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
125 if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) {
126 err(1, "Could not set filemon file descriptor!");
127 }
128 /* we don't need these once we exec */
129 (void)fcntl(pbm->mon_fd, F_SETFD, 1);
130 (void)fcntl(pbm->filemon_fd, F_SETFD, 1);
131 }
132
133 /*
134 * Read the build monitor output file and write records to the target's
135 * metadata file.
136 */
137 static void
138 filemon_read(FILE *mfp, int fd)
139 {
140 FILE *fp;
141 char buf[BUFSIZ];
142
143 /* Check if we're not writing to a meta data file.*/
144 if (mfp == NULL) {
145 if (fd >= 0)
146 close(fd); /* not interested */
147 return;
148 }
149 /* rewind */
150 lseek(fd, SEEK_SET, 0);
151 if ((fp = fdopen(fd, "r")) == NULL)
152 err(1, "Could not read build monitor file '%d'", fd);
153
154 fprintf(mfp, "-- filemon acquired metadata --\n");
155
156 while (fgets(buf, sizeof(buf), fp)) {
157 fprintf(mfp, "%s", buf);
158 }
159 fflush(mfp);
160 clearerr(fp);
161 fclose(fp);
162 }
163 #endif
164
165 static char *
166 meta_name(struct GNode *gn, char *mname, size_t mnamelen,
167 const char *dname,
168 const char *tname)
169 {
170 char buf[MAXPATHLEN];
171 char cwd[MAXPATHLEN];
172 char *rp;
173 char *cp;
174 char *tp;
175 char *p[4]; /* >= number of possible uses */
176 int i;
177
178 i = 0;
179 if (!dname)
180 dname = Var_Value(".OBJDIR", gn, &p[i++]);
181 if (!tname)
182 tname = Var_Value(TARGET, gn, &p[i++]);
183
184 /*
185 * Weed out relative paths from the target file name.
186 * We have to be careful though since if target is a
187 * symlink, the result will be unstable.
188 * So we use realpath() just to get the dirname, and leave the
189 * basename as given to us.
190 */
191 if ((cp = strrchr(tname, '/'))) {
192 if (realpath(tname, buf)) {
193 if ((rp = strrchr(buf, '/'))) {
194 rp++;
195 cp++;
196 if (strcmp(cp, rp) != 0)
197 strlcpy(rp, cp, sizeof(buf) - (rp - buf));
198 }
199 tname = buf;
200 }
201 }
202 if (realpath(dname, cwd))
203 dname = cwd;
204 /* on some systems dirname may modify its arg */
205 tp = bmake_strdup(tname);
206 if (strcmp(dname, dirname(tp)) == 0)
207 snprintf(mname, mnamelen, "%s.meta", tname);
208 else {
209 snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
210
211 /*
212 * Replace path separators in the file name after the
213 * current object directory path.
214 */
215 cp = mname + strlen(dname) + 1;
216
217 while (*cp != '\0') {
218 if (*cp == '/')
219 *cp = '_';
220 cp++;
221 }
222 }
223 free(tp);
224 for (i--; i >= 0; i--) {
225 if (p[i])
226 free(p[i]);
227 }
228 return (mname);
229 }
230
231 /*
232 * Return true if running ${.MAKE}
233 * Bypassed if target is flagged .MAKE
234 */
235 static int
236 is_submake(void *cmdp, void *gnp)
237 {
238 static char *p_make = NULL;
239 static int p_len;
240 char *cmd = cmdp;
241 GNode *gn = gnp;
242 char *mp = NULL;
243 char *cp;
244 char *cp2;
245 int rc = 0; /* keep looking */
246
247 if (!p_make) {
248 p_make = Var_Value(".MAKE", gn, &cp);
249 p_len = strlen(p_make);
250 }
251 cp = strchr(cmd, '$');
252 if ((cp)) {
253 mp = Var_Subst(NULL, cmd, gn, FALSE);
254 cmd = mp;
255 }
256 cp2 = strstr(cmd, p_make);
257 if ((cp2)) {
258 switch (cp2[p_len]) {
259 case '\0':
260 case ' ':
261 case '\t':
262 case '\n':
263 rc = 1;
264 break;
265 }
266 if (cp2 > cmd && rc > 0) {
267 switch (cp2[-1]) {
268 case ' ':
269 case '\t':
270 case '\n':
271 break;
272 default:
273 rc = 0; /* no match */
274 break;
275 }
276 }
277 }
278 if (mp)
279 free(mp);
280 return (rc);
281 }
282
283 typedef struct meta_file_s {
284 FILE *fp;
285 GNode *gn;
286 } meta_file_t;
287
288 static int
289 printCMD(void *cmdp, void *mfpp)
290 {
291 meta_file_t *mfp = mfpp;
292 char *cmd = cmdp;
293 char *cp = NULL;
294
295 if (strchr(cmd, '$')) {
296 cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE);
297 }
298 fprintf(mfp->fp, "CMD %s\n", cmd);
299 if (cp)
300 free(cp);
301 return 0;
302 }
303
304 /*
305 * Certain node types never get a .meta file
306 */
307 #define SKIP_META_TYPE(_type) do { \
308 if ((gn->type & __CONCAT(OP_, _type))) { \
309 if (DEBUG(META)) { \
310 fprintf(debug_file, "Skipping meta for %s: .%s\n", \
311 gn->name, __STRING(_type)); \
312 } \
313 return (NULL); \
314 } \
315 } while (0)
316
317 static FILE *
318 meta_create(BuildMon *pbm, GNode *gn)
319 {
320 extern char **environ;
321 meta_file_t mf;
322 char buf[MAXPATHLEN];
323 char curdir[MAXPATHLEN];
324 char objdir[MAXPATHLEN];
325 char **ptr;
326 const char *cname;
327 const char *dname;
328 const char *tname;
329 char *fname;
330 const char *cp;
331 char *p[4]; /* >= possible uses */
332 int i;
333 struct stat fs;
334
335
336 /* This may be a phony node which we don't want meta data for... */
337 /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
338 /* Or it may be explicitly flagged as .NOMETA */
339 SKIP_META_TYPE(NOMETA);
340 /* Unless it is explicitly flagged as .META */
341 if (!(gn->type & OP_META)) {
342 SKIP_META_TYPE(PHONY);
343 SKIP_META_TYPE(SPECIAL);
344 SKIP_META_TYPE(MAKE);
345 }
346
347 mf.fp = NULL;
348
349 i = 0;
350
351 dname = Var_Value(".OBJDIR", gn, &p[i++]);
352 cname = Var_Value(".CURDIR", gn, &p[i++]);
353 tname = Var_Value(TARGET, gn, &p[i++]);
354
355 /* The object directory may not exist. Check it.. */
356 if (stat(dname, &fs) != 0) {
357 if (DEBUG(META))
358 fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
359 gn->name);
360 goto out;
361 }
362 /* Check if there are no commands to execute. */
363 if (Lst_IsEmpty(gn->commands)) {
364 if (DEBUG(META))
365 fprintf(debug_file, "Skipping meta for %s: no commands\n",
366 gn->name);
367 goto out;
368 }
369
370 /* make sure these are canonical */
371 if (realpath(dname, objdir))
372 dname = objdir;
373 if (realpath(cname, curdir))
374 cname = curdir;
375
376 /* If we aren't in the object directory, don't create a meta file. */
377 if (strcmp(cname, dname) == 0) {
378 if (DEBUG(META))
379 fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
380 gn->name);
381 goto out;
382 }
383 if (!(gn->type & OP_META)) {
384 /* We do not generate .meta files for sub-makes */
385 if (Lst_ForEach(gn->commands, is_submake, gn)) {
386 if (DEBUG(META))
387 fprintf(debug_file, "Skipping meta for %s: .MAKE\n",
388 gn->name);
389 goto out;
390 }
391 }
392
393 if (metaVerbose) {
394 char *mp;
395
396 /* Describe the target we are building */
397 mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, 0);
398 if (*mp)
399 fprintf(stdout, "%s\n", mp);
400 free(mp);
401 }
402 /* Get the basename of the target */
403 if ((cp = strrchr(tname, '/')) == NULL) {
404 cp = tname;
405 } else {
406 cp++;
407 }
408
409 fflush(stdout);
410
411 if (strcmp(cp, makeDependfile) == 0)
412 goto out;
413
414 if (!writeMeta)
415 /* Don't create meta data. */
416 goto out;
417
418 fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
419 dname, tname);
420
421 if ((mf.fp = fopen(fname, "w")) == NULL)
422 err(1, "Could not open meta file '%s'", fname);
423
424 fprintf(mf.fp, "# Meta data file %s\n", fname);
425
426 mf.gn = gn;
427
428 Lst_ForEach(gn->commands, printCMD, &mf);
429
430 fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
431 fprintf(mf.fp, "TARGET %s\n", tname);
432
433 if (metaEnv) {
434 for (ptr = environ; *ptr != NULL; ptr++)
435 fprintf(mf.fp, "ENV %s\n", *ptr);
436 }
437
438 fprintf(mf.fp, "-- command output --\n");
439 fflush(mf.fp);
440
441 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
442 Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
443
444 out:
445 for (i--; i >= 0; i--) {
446 if (p[i])
447 free(p[i]);
448 }
449
450 return (mf.fp);
451 }
452
453
454 void
455 meta_init(const char *make_mode)
456 {
457 static int once = 0;
458
459 useMeta = TRUE;
460 useFilemon = TRUE;
461 writeMeta = TRUE;
462
463 if (make_mode) {
464 if (strstr(make_mode, "env"))
465 metaEnv = TRUE;
466 if (strstr(make_mode, "verb"))
467 metaVerbose = TRUE;
468 if (strstr(make_mode, "read"))
469 writeMeta = FALSE;
470 if (strstr(make_mode, "nofilemon"))
471 useFilemon = FALSE;
472 if (strstr(make_mode, "ignore-cmd"))
473 metaIgnoreCMDs = TRUE;
474 /* for backwards compatability */
475 Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
476 Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
477 }
478 if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
479 /*
480 * The default value for MAKE_META_PREFIX
481 * prints the absolute path of the target.
482 * This works be cause :H will generate '.' if there is no /
483 * and :tA will resolve that to cwd.
484 */
485 Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
486 }
487 if (once)
488 return;
489 once = 1;
490 memset(&Mybm, 0, sizeof(Mybm));
491 }
492
493 /*
494 * In each case below we allow for job==NULL
495 */
496 void
497 meta_job_start(Job *job, GNode *gn)
498 {
499 BuildMon *pbm;
500
501 if (job != NULL) {
502 pbm = &job->bm;
503 } else {
504 pbm = &Mybm;
505 }
506 pbm->mfp = meta_create(pbm, gn);
507 #ifdef USE_FILEMON_ONCE
508 /* compat mode we open the filemon dev once per command */
509 if (job == NULL)
510 return;
511 #endif
512 #ifdef USE_FILEMON
513 if (pbm->mfp != NULL && useFilemon) {
514 filemon_open(pbm);
515 } else {
516 pbm->mon_fd = pbm->filemon_fd = -1;
517 }
518 #endif
519 }
520
521 /*
522 * The child calls this before doing anything.
523 * It does not disturb our state.
524 */
525 void
526 meta_job_child(Job *job)
527 {
528 #ifdef USE_FILEMON
529 BuildMon *pbm;
530 pid_t pid;
531
532 if (job != NULL) {
533 pbm = &job->bm;
534 } else {
535 pbm = &Mybm;
536 }
537 pid = getpid();
538 if (pbm->mfp != NULL && useFilemon) {
539 if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
540 err(1, "Could not set filemon pid!");
541 }
542 }
543 #endif
544 }
545
546 void
547 meta_job_error(Job *job, GNode *gn, int flags, int status)
548 {
549 char cwd[MAXPATHLEN];
550 BuildMon *pbm;
551
552 if (job != NULL) {
553 pbm = &job->bm;
554 } else {
555 if (!gn)
556 gn = job->node;
557 pbm = &Mybm;
558 }
559 if (pbm->mfp != NULL) {
560 fprintf(pbm->mfp, "*** Error code %d%s\n",
561 status,
562 (flags & JOB_IGNERR) ?
563 "(ignored)" : "");
564 }
565 if (gn) {
566 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
567 }
568 getcwd(cwd, sizeof(cwd));
569 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
570 #ifdef USE_FILEMON
571 if (pbm) {
572 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
573 }
574 #endif
575 }
576
577 void
578 meta_job_output(Job *job, char *cp, const char *nl)
579 {
580 BuildMon *pbm;
581
582 if (job != NULL) {
583 pbm = &job->bm;
584 } else {
585 pbm = &Mybm;
586 }
587 if (pbm->mfp != NULL) {
588 if (metaVerbose) {
589 static char *meta_prefix = NULL;
590 static int meta_prefix_len;
591
592 if (!meta_prefix) {
593 char *cp2;
594
595 meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
596 if ((cp2 = strchr(meta_prefix, '$')))
597 meta_prefix_len = cp2 - meta_prefix;
598 else
599 meta_prefix_len = strlen(meta_prefix);
600 }
601 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
602 cp = strchr(cp+1, '\n');
603 if (!cp++)
604 return;
605 }
606 }
607 fprintf(pbm->mfp, "%s%s", cp, nl);
608 }
609 }
610
611 void
612 meta_cmd_finish(void *pbmp)
613 {
614 #ifdef USE_FILEMON
615 BuildMon *pbm = pbmp;
616
617 if (!pbm)
618 pbm = &Mybm;
619
620 if (pbm->filemon_fd >= 0) {
621 close(pbm->filemon_fd);
622 filemon_read(pbm->mfp, pbm->mon_fd);
623 pbm->filemon_fd = pbm->mon_fd = -1;
624 }
625 #endif
626 }
627
628 void
629 meta_job_finish(Job *job)
630 {
631 BuildMon *pbm;
632
633 if (job != NULL) {
634 pbm = &job->bm;
635 } else {
636 pbm = &Mybm;
637 }
638 if (pbm->mfp != NULL) {
639 meta_cmd_finish(pbm);
640 fclose(pbm->mfp);
641 pbm->mfp = NULL;
642 }
643 }
644
645 /*
646 * Fetch a full line from fp - growing bufp if needed
647 * Return length in bufp.
648 */
649 static int
650 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
651 {
652 char *buf = *bufp;
653 size_t bufsz = *szp;
654 struct stat fs;
655 int x;
656
657 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
658 check_newline:
659 x = o + strlen(&buf[o]);
660 if (buf[x - 1] == '\n')
661 return x;
662 /*
663 * We need to grow the buffer.
664 * The meta file can give us a clue.
665 */
666 if (fstat(fileno(fp), &fs) == 0) {
667 size_t newsz;
668 char *p;
669
670 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
671 if (newsz <= bufsz)
672 newsz = ROUNDUP(fs.st_size, BUFSIZ);
673 if (DEBUG(META))
674 fprintf(debug_file, "growing buffer %u -> %u\n",
675 bufsz, newsz);
676 p = bmake_realloc(buf, newsz);
677 if (p) {
678 *bufp = buf = p;
679 *szp = bufsz = newsz;
680 /* fetch the rest */
681 if (!fgets(&buf[x], bufsz - x, fp))
682 return x; /* truncated! */
683 goto check_newline;
684 }
685 }
686 }
687 return 0;
688 }
689
690 /*
691 * When running with 'meta' functionality, a target can be out-of-date
692 * if any of the references in it's meta data file is more recent.
693 */
694 Boolean
695 meta_oodate(GNode *gn, Boolean oodate)
696 {
697 char latestdir[MAXPATHLEN];
698 char fname[MAXPATHLEN];
699 char fname1[MAXPATHLEN];
700 char *p;
701 char *cp;
702 FILE *fp;
703 Boolean ignoreOODATE = FALSE;
704
705 /*
706 * We need to check if the target is out-of-date. This includes
707 * checking if the expanded command has changed. This in turn
708 * requires that all variables are set in the same way that they
709 * would be if the target needs to be re-built.
710 */
711 Make_DoAllVar(gn);
712
713 if (oodate)
714 return oodate; /* we're done */
715
716 if (getcwd(latestdir, sizeof(latestdir)) == NULL)
717 err(1, "Could not get current working directory");
718
719 meta_name(gn, fname, sizeof(fname), NULL, NULL);
720
721 if ((fp = fopen(fname, "r")) != NULL) {
722 static char *buf = NULL;
723 static size_t bufsz;
724 int lineno = 0;
725 int f = 0;
726 int x;
727 LstNode ln;
728 struct stat fs;
729
730 if (!buf) {
731 bufsz = 8 * BUFSIZ;
732 buf = bmake_malloc(bufsz);
733 }
734
735 /* we want to track all the .meta we read */
736 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
737
738 ln = Lst_First(gn->commands);
739 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
740 lineno++;
741 if (buf[x - 1] == '\n')
742 buf[x - 1] = '\0';
743 else
744 warnx("%s: %d: line truncated at %u", fname, lineno, x);
745
746 /* Find the start of the build monitor section. */
747 if (!f) {
748 if (strncmp(buf, "-- filemon", 10) == 0) {
749 f = 1;
750 continue;
751 }
752 if (strncmp(buf, "# buildmon", 10) == 0) {
753 f = 1;
754 continue;
755 }
756 }
757
758 /* Delimit the record type. */
759 p = buf;
760 strsep(&p, " ");
761 if (f) {
762 /* Process according to record type. */
763 switch (buf[0]) {
764 case 'C':
765 /* Skip the pid. */
766 if (strsep(&p, " ") == NULL)
767 break;
768
769 /* Update the latest directory. */
770 strlcpy(latestdir, p, sizeof(latestdir));
771 break;
772
773 case 'R':
774 case 'E':
775 /* Skip the pid. */
776 if (strsep(&p, " ") == NULL)
777 break;
778
779 /*
780 * Check for runtime files that can't
781 * be part of the dependencies because
782 * they are _expected_ to change.
783 */
784 if (strncmp(p, "/var/", 5) == 0)
785 break;
786
787 /* Ignore device files. */
788 if (strncmp(p, "/dev/", 5) == 0)
789 break;
790
791 /* Ignore /etc/ files. */
792 if (strncmp(p, "/etc/", 5) == 0)
793 break;
794
795 /*
796 * The rest of the record is the
797 * file name.
798 * Check if it's not an absolute
799 * path.
800 */
801 if (*p != '/') {
802 /* Use the latest path seen. */
803 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
804 p = fname1;
805 }
806
807 if (stat(p, &fs) == 0 &&
808 !S_ISDIR(fs.st_mode) &&
809 fs.st_mtime > gn->mtime) {
810 if (DEBUG(META))
811 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
812 oodate = TRUE;
813 }
814 break;
815 default:
816 break;
817 }
818
819 /*
820 * Compare the current command with the one in the
821 * meta data file.
822 */
823 } else if (strcmp(buf, "CMD") == 0) {
824 if (ln == NULL) {
825 if (DEBUG(META))
826 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
827 oodate = TRUE;
828 } else {
829 char *cmd = (char *)Lst_Datum(ln);
830
831 if (!ignoreOODATE) {
832 if (strstr(cmd, "$?"))
833 ignoreOODATE = TRUE;
834 else if ((cp = strstr(cmd, ".OODATE"))) {
835 /* check for $[{(].OODATE[)}] */
836 if (cp > cmd + 2 && cp[-2] == '$')
837 ignoreOODATE = TRUE;
838 }
839 if (ignoreOODATE && DEBUG(META))
840 fprintf(debug_file, "%s: %d: cannot compare commands using .OODATE\n", fname, lineno);
841 }
842 cmd = Var_Subst(NULL, cmd, gn, TRUE);
843
844 if ((cp = strchr(cmd, '\n'))) {
845 int n;
846
847 /*
848 * This command contains newlines, we need to
849 * fetch more from the .meta file before we
850 * attempt a comparison.
851 */
852 /* first put the newline back at buf[x - 1] */
853 buf[x - 1] = '\n';
854 do {
855 /* now fetch the next line */
856 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
857 break;
858 x = n;
859 lineno++;
860 if (buf[x - 1] != '\n') {
861 warnx("%s: %d: line truncated at %u", fname, lineno, x);
862 break;
863 }
864 cp = strchr(++cp, '\n');
865 } while (cp);
866 if (buf[x - 1] == '\n')
867 buf[x - 1] = '\0';
868 }
869 if (!ignoreOODATE &&
870 !(gn->type & OP_NOMETA_CMP) &&
871 strcmp(p, cmd) != 0) {
872 if (DEBUG(META))
873 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
874 if (!metaIgnoreCMDs)
875 oodate = TRUE;
876 }
877 free(cmd);
878 ln = Lst_Succ(ln);
879 }
880 } else if (strcmp(buf, "CWD") == 0) {
881 char curdir[MAXPATHLEN];
882 if (strcmp(p, getcwd(curdir, sizeof(curdir))) != 0) {
883 if (DEBUG(META))
884 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
885 oodate = TRUE;
886 }
887 }
888 }
889
890 /*
891 * Check if there are extra commands now
892 * that weren't in the meta data file.
893 */
894 if (!oodate && ln != NULL) {
895 if (DEBUG(META))
896 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
897 oodate = TRUE;
898 }
899
900 fclose(fp);
901 }
902 return oodate;
903 }
904
905 /* support for compat mode */
906
907 static int childPipe[2];
908
909 void
910 meta_compat_start(void)
911 {
912 #ifdef USE_FILEMON_ONCE
913 /*
914 * We need to re-open filemon for each cmd.
915 */
916 BuildMon *pbm = &Mybm;
917
918 if (pbm->mfp != NULL && useFilemon) {
919 filemon_open(pbm);
920 } else {
921 pbm->mon_fd = pbm->filemon_fd = -1;
922 }
923 #endif
924 if (pipe(childPipe) < 0)
925 Punt("Cannot create pipe: %s", strerror(errno));
926 /* Set close-on-exec flag for both */
927 (void)fcntl(childPipe[0], F_SETFD, 1);
928 (void)fcntl(childPipe[1], F_SETFD, 1);
929 }
930
931 void
932 meta_compat_child(void)
933 {
934 meta_job_child(NULL);
935 if (dup2(childPipe[1], 1) < 0 ||
936 dup2(1, 2) < 0) {
937 execError("dup2", "pipe");
938 _exit(1);
939 }
940 }
941
942 void
943 meta_compat_parent(void)
944 {
945 FILE *fp;
946 char buf[BUFSIZ];
947
948 close(childPipe[1]); /* child side */
949 fp = fdopen(childPipe[0], "r");
950 while (fgets(buf, sizeof(buf), fp)) {
951 meta_job_output(NULL, buf, "");
952 printf("%s", buf);
953 }
954 fclose(fp);
955 }
956