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