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