Annotation of src/usr.bin/make/compat.c, Revision 1.59
1.36 espie 1: /* $OpenPackages$ */
1.59 ! espie 2: /* $OpenBSD: compat.c,v 1.58 2007/09/17 08:36:57 espie Exp $ */
1.8 millert 3: /* $NetBSD: compat.c,v 1.14 1996/11/06 17:59:01 christos Exp $ */
1.1 deraadt 4:
5: /*
6: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
7: * Copyright (c) 1988, 1989 by Adam de Boor
8: * Copyright (c) 1989 by Berkeley Softworks
9: * All rights reserved.
10: *
11: * This code is derived from software contributed to Berkeley by
12: * Adam de Boor.
13: *
14: * Redistribution and use in source and binary forms, with or without
15: * modification, are permitted provided that the following conditions
16: * are met:
17: * 1. Redistributions of source code must retain the above copyright
18: * notice, this list of conditions and the following disclaimer.
19: * 2. Redistributions in binary form must reproduce the above copyright
20: * notice, this list of conditions and the following disclaimer in the
21: * documentation and/or other materials provided with the distribution.
1.49 millert 22: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
1.37 espie 39: #include <sys/types.h>
40: #include <sys/stat.h>
41: #include <sys/wait.h>
42: #include <ctype.h>
43: #include <errno.h>
1.39 espie 44: #include <limits.h>
1.37 espie 45: #include <signal.h>
46: #include <stddef.h>
47: #include <stdio.h>
1.39 espie 48: #include <stdlib.h>
1.38 espie 49: #include <string.h>
1.37 espie 50: #include <unistd.h>
51: #include "config.h"
52: #include "defines.h"
53: #include "dir.h"
1.55 espie 54: #include "engine.h"
1.37 espie 55: #include "compat.h"
56: #include "suff.h"
57: #include "var.h"
58: #include "targ.h"
59: #include "error.h"
60: #include "str.h"
61: #include "extern.h"
62: #include "memory.h"
63: #include "gnode.h"
64: #include "make.h"
65: #include "timestamp.h"
66: #include "lst.h"
67: #include "pathnames.h"
1.1 deraadt 68:
1.36 espie 69: /* The following array is used to make a fast determination of which
1.1 deraadt 70: * characters are interpreted specially by the shell. If a command
71: * contains any of these characters, it is executed by the shell, not
1.36 espie 72: * directly by us. */
1.1 deraadt 73:
1.57 espie 74: static char meta[256];
1.1 deraadt 75:
1.57 espie 76: static GNode *ENDNode;
1.36 espie 77: static void CompatInterrupt(int);
1.51 espie 78: static int CompatRunCommand(LstNode, void *);
1.36 espie 79: static void CompatMake(void *, void *);
80: static int shellneed(char **);
1.1 deraadt 81:
1.45 espie 82: static volatile sig_atomic_t interrupted;
83:
1.59 ! espie 84: static void
1.50 espie 85: CompatInterrupt(int signo)
1.1 deraadt 86: {
1.57 espie 87: if (interrupted != SIGINT)
88: interrupted = signo;
1.1 deraadt 89: }
1.36 espie 90:
1.1 deraadt 91: /*-
92: *-----------------------------------------------------------------------
1.10 deraadt 93: * shellneed --
1.36 espie 94: *
1.10 deraadt 95: * Results:
1.36 espie 96: * Returns 1 if a specified set of arguments
97: * must be executed by the shell,
1.12 espie 98: * 0 if it can be run via execve, and -1 if the command can be
1.36 espie 99: * handled internally
1.10 deraadt 100: *
101: * Side Effects:
1.12 espie 102: * May modify the process umask
1.10 deraadt 103: *-----------------------------------------------------------------------
104: */
105: static int
1.50 espie 106: shellneed(char **av)
1.10 deraadt 107: {
1.36 espie 108: char *runsh[] = {
1.11 deraadt 109: "alias", "cd", "eval", "exec", "exit", "read", "set", "ulimit",
1.36 espie 110: "unalias", "unset", "wait",
1.11 deraadt 111: NULL
112: };
113:
1.12 espie 114: char **p;
1.11 deraadt 115:
1.12 espie 116: /* FIXME most of these ARE actual no-ops */
1.11 deraadt 117: for (p = runsh; *p; p++)
1.12 espie 118: if (strcmp(av[0], *p) == 0)
1.36 espie 119: return 1;
1.11 deraadt 120:
1.12 espie 121: if (strcmp(av[0], "umask") == 0) {
1.10 deraadt 122: long umi;
123: char *ep = NULL;
124: mode_t um;
125:
1.12 espie 126: if (av[1] != NULL) {
127: umi = strtol(av[1], &ep, 8);
1.10 deraadt 128: if (ep == NULL)
1.36 espie 129: return 1;
1.10 deraadt 130: um = umi;
131: }
1.12 espie 132: else {
133: um = umask(0);
134: printf("%o\n", um);
135: }
1.36 espie 136: (void)umask(um);
137: return -1;
1.10 deraadt 138: }
139:
1.36 espie 140: return 0;
1.10 deraadt 141: }
1.36 espie 142:
1.10 deraadt 143: /*-
144: *-----------------------------------------------------------------------
1.1 deraadt 145: * CompatRunCommand --
146: * Execute the next command for a target. If the command returns an
147: * error, the node's made field is set to ERROR and creation stops.
148: *
149: * Results:
1.36 espie 150: * 0 in case of error, 1 if ok.
1.1 deraadt 151: *
152: * Side Effects:
153: * The node's 'made' field may be set to ERROR.
154: *-----------------------------------------------------------------------
155: */
156: static int
1.51 espie 157: CompatRunCommand(LstNode cmdNode,/* Command to execute */
1.50 espie 158: void *gnp) /* Node from which the command came */
1.1 deraadt 159: {
1.57 espie 160: char *cmdStart; /* Start of expanded command */
161: char *cp, *bp = NULL;
162: bool silent; /* Don't print command */
163: bool doExecute; /* Execute the command */
164: volatile bool errCheck; /* Check errors */
165: int reason; /* Reason for child's death */
166: int status; /* Description of child's death */
167: pid_t cpid; /* Child actually found */
168: pid_t stat; /* Status of fork */
169: char **volatile av; /* Argument vector for thing to exec */
170: int argc; /* Number of arguments in av or 0 if not
1.1 deraadt 171: * dynamically allocated */
1.57 espie 172: char *cmd = (char *)Lst_Datum(cmdNode);
173: GNode *gn = (GNode *)gnp;
174: static char *shargv[4] = { _PATH_BSHELL };
175:
176: silent = gn->type & OP_SILENT;
177: errCheck = !(gn->type & OP_IGNORE);
178: doExecute = !noExecute;
179:
180: cmdStart = Var_Subst(cmd, &gn->context, false);
181:
182: /* brk_string will return an argv with a NULL in av[0], thus causing
183: * execvp to choke and die horribly. Besides, how can we execute a null
184: * command? In any case, we warn the user that the command expanded to
185: * nothing (is this the right thing to do?). */
186:
187: if (*cmdStart == '\0') {
188: free(cmdStart);
189: Error("%s expands to empty string", cmd);
190: return 1;
191: } else
192: cmd = cmdStart;
193: Lst_Replace(cmdNode, cmdStart);
194:
195: if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
196: Lst_AtEnd(&ENDNode->commands, cmdStart);
197: return 1;
198: } else if (strcmp(cmdStart, "...") == 0) {
199: gn->type |= OP_SAVE_CMDS;
200: return 1;
201: }
202:
203: for (;; cmd++) {
204: if (*cmd == '@')
205: silent = DEBUG(LOUD) ? false : true;
206: else if (*cmd == '-')
207: errCheck = false;
208: else if (*cmd == '+')
209: doExecute = true;
210: else
211: break;
212: }
1.4 millert 213:
1.57 espie 214: while (isspace(*cmd))
215: cmd++;
216:
217: /* Search for meta characters in the command. If there are no meta
218: * characters, there's no need to execute a shell to execute the
219: * command. */
220: for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
221: continue;
222: }
223:
224: /* Print the command before echoing if we're not supposed to be quiet
225: * for this one. We also print the command if -n given. */
226: if (!silent || noExecute) {
227: printf("%s\n", cmd);
228: fflush(stdout);
229: }
230:
231: /* If we're not supposed to execute any commands, this is as far as
232: * we go... */
233: if (!doExecute)
1.25 espie 234: return 1;
1.57 espie 235:
236: if (*cp != '\0') {
237: /* If *cp isn't the null character, we hit a "meta" character
238: * and need to pass the command off to the shell. We give the
239: * shell the -e flag as well as -c if it's supposed to exit
240: * when it hits an error. */
241:
1.36 espie 242: shargv[1] = errCheck ? "-ec" : "-c";
1.12 espie 243: shargv[2] = cmd;
1.36 espie 244: shargv[3] = NULL;
1.57 espie 245: av = shargv;
246: argc = 0;
247: } else {
248: /* No meta-characters, so probably no need to exec a shell.
249: * Break the command into words to form an argument vector
250: * we can execute. */
251: av = brk_string(cmd, &argc, &bp);
252: switch (shellneed(av)) {
253: case -1: /* handled internally */
254: free(bp);
255: free(av);
256: return 1;
257: case 1:
258: shargv[1] = errCheck ? "-ec" : "-c";
259: shargv[2] = cmd;
260: shargv[3] = NULL;
261: free(av);
262: free(bp);
263: bp = NULL;
264: av = shargv;
265: argc = 0;
266: break;
267: default: /* nothing needed */
268: break;
269: }
270: }
271:
272: /* Fork and execute the single command. If the fork fails, we abort. */
273: cpid = fork();
274: if (cpid == -1)
275: Fatal("Could not fork");
276: if (cpid == 0) {
277: execvp(av[0], av);
278: if (errno == ENOENT)
279: fprintf(stderr, "%s: not found\n", av[0]);
280: else
281: perror(av[0]);
282: _exit(1);
283: }
284: if (bp) {
1.36 espie 285: free(av);
1.14 espie 286: free(bp);
1.57 espie 287: }
288: free(cmdStart);
289: Lst_Replace(cmdNode, NULL);
290:
291: /* The child is off and running. Now all we can do is wait... */
292: while (1) {
293:
294: while ((stat = wait(&reason)) != cpid) {
295: if (stat == -1 && errno != EINTR)
296: break;
1.1 deraadt 297: }
1.57 espie 298:
299: if (interrupted)
300: break;
301:
302: if (stat != -1) {
303: if (WIFSTOPPED(reason))
304: status = WSTOPSIG(reason); /* stopped */
305: else if (WIFEXITED(reason)) {
306: status = WEXITSTATUS(reason); /* exited */
307: if (status != 0)
308: printf("*** Error code %d", status);
309: } else {
310: status = WTERMSIG(reason); /* signaled */
311: printf("*** Signal %d", status);
312: }
313:
314:
315: if (!WIFEXITED(reason) || status != 0) {
316: if (errCheck) {
317: gn->made = ERROR;
318: if (keepgoing)
319: /* Abort the current target,
320: * but let others continue. */
321: printf(" (continuing)\n");
322: } else {
323: /* Continue executing commands for this
324: * target. If we return 0, this will
325: * happen... */
326: printf(" (ignored)\n");
327: status = 0;
328: }
329: }
330: return !status;
331: } else
332: Fatal("error in wait: %d", stat);
333: /*NOTREACHED*/
334: }
335:
336: /* This is reached only if interrupted */
337: if (!Targ_Precious(gn)) {
338: char *file = Varq_Value(TARGET_INDEX, gn);
339:
340: if (!noExecute && eunlink(file) != -1)
341: Error("*** %s removed\n", file);
342: }
343: if (interrupted == SIGINT) {
344: GNode *i = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
345: signal(SIGINT, SIG_IGN);
346: signal(SIGTERM, SIG_IGN);
347: signal(SIGHUP, SIG_IGN);
348: signal(SIGQUIT, SIG_IGN);
349: interrupted = 0;
350: if (i != NULL)
351: Lst_ForEachNodeWhile(&i->commands, CompatRunCommand, i);
352: exit(SIGINT);
353: }
354: exit(interrupted);
1.1 deraadt 355: }
1.36 espie 356:
1.1 deraadt 357: /*-
358: *-----------------------------------------------------------------------
359: * CompatMake --
360: * Make a target.
361: *
362: * Side Effects:
363: * If an error is detected and not being ignored, the process exits.
364: *-----------------------------------------------------------------------
365: */
1.26 espie 366: static void
1.50 espie 367: CompatMake(void *gnp, /* The node to make */
368: void *pgnp) /* Parent to abort if necessary */
1.1 deraadt 369: {
1.57 espie 370: GNode *gn = (GNode *)gnp;
371: GNode *pgn = (GNode *)pgnp;
372:
373: if (pgn->type & OP_MADE) {
374: (void)Dir_MTime(gn);
375: gn->made = UPTODATE;
376: }
377:
378: if (gn->type & OP_USE) {
379: Make_HandleUse(gn, pgn);
380: } else if (gn->made == UNMADE) {
381: /* First mark ourselves to be made, then apply whatever
382: * transformations the suffix module thinks are necessary. Once
383: * that's done, we can descend and make all our children. If
384: * any of them has an error but the -k flag was given, our
385: * 'make' field will be set false again. This is our signal to
386: * not attempt to do anything but abort our parent as well. */
387: gn->make = true;
388: gn->made = BEINGMADE;
389: Suff_FindDeps(gn);
390: Lst_ForEach(&gn->children, CompatMake, gn);
391: if (!gn->make) {
392: gn->made = ABORTED;
393: pgn->make = false;
394: return;
395: }
1.5 millert 396:
1.57 espie 397: if (Lst_Member(&gn->iParents, pgn) != NULL) {
1.59 ! espie 398: Varq_Set(IMPSRC_INDEX, Varq_Value(TARGET_INDEX, gn),
1.57 espie 399: pgn);
400: }
1.1 deraadt 401:
1.57 espie 402: /* All the children were made ok. Now cmtime contains the
403: * modification time of the newest child, we need to find out
404: * if we exist and when we were modified last. The criteria for
405: * datedness are defined by the Make_OODate function. */
406: if (DEBUG(MAKE))
407: printf("Examining %s...", gn->name);
408: if (! Make_OODate(gn)) {
409: gn->made = UPTODATE;
410: if (DEBUG(MAKE))
411: printf("up-to-date.\n");
412: return;
413: } else if (DEBUG(MAKE))
414: printf("out-of-date.\n");
415:
416: /* If the user is just seeing if something is out-of-date, exit
417: * now to tell him/her "yes". */
418: if (queryFlag)
419: exit(-1);
420:
421: /* We need to be re-made. We also have to make sure we've got a
422: * $? variable. To be nice, we also define the $> variable
423: * using Make_DoAllVar(). */
424: Make_DoAllVar(gn);
425:
426: /* Alter our type to tell if errors should be ignored or things
427: * should not be printed so CompatRunCommand knows what to do.
428: * */
429: if (Targ_Ignore(gn))
430: gn->type |= OP_IGNORE;
431: if (Targ_Silent(gn))
432: gn->type |= OP_SILENT;
433:
434: if (Job_CheckCommands(gn, Fatal)) {
435: /* Our commands are ok, but we still have to worry
436: * about the -t flag... */
437: if (!touchFlag)
1.59 ! espie 438: Lst_ForEachNodeWhile(&gn->commands,
1.57 espie 439: CompatRunCommand, gn);
440: else
441: Job_Touch(gn, gn->type & OP_SILENT);
442: } else
443: gn->made = ERROR;
444:
445: if (gn->made != ERROR) {
446: /* If the node was made successfully, mark it so,
447: * update its modification time and timestamp all its
448: * parents. Note that for .ZEROTIME targets, the
449: * timestamping isn't done. This is to keep its state
450: * from affecting that of its parent. */
451: gn->made = MADE;
452: /* This is what Make does and it's actually a good
453: * thing, as it allows rules like
454: *
455: * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
456: *
457: * to function as intended. Unfortunately, thanks to
458: * the stateless nature of NFS (and the speed of this
459: * program), there are times when the modification time
460: * of a file created on a remote machine will not be
461: * modified before the stat() implied by the Dir_MTime
462: * occurs, thus leading us to believe that the file is
463: * unchanged, wreaking havoc with files that depend on
464: * this one.
465: *
466: * I have decided it is better to make too much than to
467: * make too little, so this stuff is commented out
468: * unless you're sure it's ok. -- ardeb 1/12/88
469: */
470: if (noExecute || is_out_of_date(Dir_MTime(gn)))
471: gn->mtime = now;
472: if (is_strictly_before(gn->mtime, gn->cmtime))
473: gn->mtime = gn->cmtime;
474: if (DEBUG(MAKE))
1.59 ! espie 475: printf("update time: %s\n",
1.57 espie 476: time_to_string(gn->mtime));
477: if (!(gn->type & OP_EXEC)) {
478: pgn->childMade = true;
479: Make_TimeStamp(pgn, gn);
480: }
481: } else if (keepgoing)
482: pgn->make = false;
483: else {
1.36 espie 484:
1.57 espie 485: if (gn->lineno)
486: printf("\n\nStop in %s (line %lu of %s).\n",
487: Var_Value(".CURDIR"),
488: (unsigned long)gn->lineno,
489: gn->fname);
490: else
1.59 ! espie 491: printf("\n\nStop in %s.\n",
1.57 espie 492: Var_Value(".CURDIR"));
493: exit(1);
494: }
495: } else if (gn->made == ERROR)
496: /* Already had an error when making this beastie. Tell the
497: * parent to abort. */
1.37 espie 498: pgn->make = false;
1.57 espie 499: else {
500: if (Lst_Member(&gn->iParents, pgn) != NULL) {
1.59 ! espie 501: Varq_Set(IMPSRC_INDEX, Varq_Value(TARGET_INDEX, gn),
1.57 espie 502: pgn);
503: }
504: switch (gn->made) {
505: case BEINGMADE:
506: Error("Graph cycles through %s\n", gn->name);
507: gn->made = ERROR;
508: pgn->make = false;
509: break;
510: case MADE:
511: if ((gn->type & OP_EXEC) == 0) {
512: pgn->childMade = true;
513: Make_TimeStamp(pgn, gn);
514: }
515: break;
516: case UPTODATE:
517: if ((gn->type & OP_EXEC) == 0)
518: Make_TimeStamp(pgn, gn);
519: break;
520: default:
521: break;
522: }
1.1 deraadt 523: }
524: }
1.36 espie 525:
1.1 deraadt 526: void
1.50 espie 527: Compat_Run(Lst targs) /* List of target nodes to re-create */
1.1 deraadt 528: {
1.57 espie 529: char *cp; /* Pointer to string of shell meta-characters */
530: GNode *gn = NULL; /* Current root target */
531: int errors; /* Number of targets not remade due to errors */
532:
533: signal(SIGINT, CompatInterrupt);
534: signal(SIGTERM, CompatInterrupt);
535: signal(SIGHUP, CompatInterrupt);
536: signal(SIGQUIT, CompatInterrupt);
537:
538: for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++)
539: meta[(unsigned char) *cp] = 1;
540: /* The null character serves as a sentinel in the string. */
541: meta[0] = 1;
542:
543: ENDNode = Targ_FindNode(".END", TARG_CREATE);
544: /* If the user has defined a .BEGIN target, execute the commands
545: * attached to it. */
546: if (!queryFlag) {
547: gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
548: if (gn != NULL) {
1.59 ! espie 549: Lst_ForEachNodeWhile(&gn->commands, CompatRunCommand,
1.57 espie 550: gn);
551: if (gn->made == ERROR) {
552: printf("\n\nStop.\n");
553: exit(1);
554: }
555: }
556: }
557:
558: /* For each entry in the list of targets to create, call CompatMake on
559: * it to create the thing. CompatMake will leave the 'made' field of gn
560: * in one of several states:
561: * UPTODATE gn was already up-to-date
562: * MADE gn was recreated successfully
563: * ERROR An error occurred while gn was being created
1.59 ! espie 564: * ABORTED gn was not remade because one of its
1.57 espie 565: * inferiors could not be made due to errors.
566: */
567: errors = 0;
568: while ((gn = (GNode *)Lst_DeQueue(targs)) != NULL) {
569: CompatMake(gn, gn);
570:
571: if (gn->made == UPTODATE)
572: printf("`%s' is up to date.\n", gn->name);
573: else if (gn->made == ABORTED) {
1.59 ! espie 574: printf("`%s' not remade because of errors.\n",
1.57 espie 575: gn->name);
1.58 espie 576: errors++;
1.57 espie 577: }
578: }
579:
580: /* If the user has defined a .END target, run its commands. */
581: if (errors == 0)
1.59 ! espie 582: Lst_ForEachNodeWhile(&ENDNode->commands, CompatRunCommand,
1.57 espie 583: ENDNode);
1.1 deraadt 584: }