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