1 /* $NetBSD: compat.c,v 1.66 2006/11/17 22:07:39 dsl Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 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 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1988, 1989 by Adam de Boor 37 * Copyright (c) 1989 by Berkeley Softworks 38 * All rights reserved. 39 * 40 * This code is derived from software contributed to Berkeley by 41 * Adam de Boor. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by the University of 54 * California, Berkeley and its contributors. 55 * 4. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 */ 71 72 #ifndef MAKE_NATIVE 73 static char rcsid[] = "$NetBSD: compat.c,v 1.66 2006/11/17 22:07:39 dsl Exp $"; 74 #else 75 #include <sys/cdefs.h> 76 #ifndef lint 77 #if 0 78 static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94"; 79 #else 80 __RCSID("$NetBSD: compat.c,v 1.66 2006/11/17 22:07:39 dsl Exp $"); 81 #endif 82 #endif /* not lint */ 83 #endif 84 85 /*- 86 * compat.c -- 87 * The routines in this file implement the full-compatibility 88 * mode of PMake. Most of the special functionality of PMake 89 * is available in this mode. Things not supported: 90 * - different shells. 91 * - friendly variable substitution. 92 * 93 * Interface: 94 * Compat_Run Initialize things for this module and recreate 95 * thems as need creatin' 96 */ 97 98 #include <sys/types.h> 99 #include <sys/stat.h> 100 #include <sys/wait.h> 101 102 #include <ctype.h> 103 #include <errno.h> 104 #include <signal.h> 105 #include <stdio.h> 106 107 #include "make.h" 108 #include "hash.h" 109 #include "dir.h" 110 #include "job.h" 111 #include "pathnames.h" 112 113 /* 114 * The following array is used to make a fast determination of which 115 * characters are interpreted specially by the shell. If a command 116 * contains any of these characters, it is executed by the shell, not 117 * directly by us. 118 */ 119 120 static char meta[256]; 121 122 static GNode *curTarg = NILGNODE; 123 static GNode *ENDNode; 124 static void CompatInterrupt(int); 125 126 static void 127 Compat_Init(void) 128 { 129 const char *cp; 130 131 Shell_Init(); /* setup default shell */ 132 133 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) { 134 meta[(unsigned char) *cp] = 1; 135 } 136 /* 137 * The null character serves as a sentinel in the string. 138 */ 139 meta[0] = 1; 140 } 141 142 /*- 143 *----------------------------------------------------------------------- 144 * CompatInterrupt -- 145 * Interrupt the creation of the current target and remove it if 146 * it ain't precious. 147 * 148 * Results: 149 * None. 150 * 151 * Side Effects: 152 * The target is removed and the process exits. If .INTERRUPT exists, 153 * its commands are run first WITH INTERRUPTS IGNORED.. 154 * 155 *----------------------------------------------------------------------- 156 */ 157 static void 158 CompatInterrupt(int signo) 159 { 160 GNode *gn; 161 162 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) { 163 char *p1; 164 char *file = Var_Value(TARGET, curTarg, &p1); 165 166 if (!noExecute && eunlink(file) != -1) { 167 Error("*** %s removed", file); 168 } 169 if (p1) 170 free(p1); 171 172 /* 173 * Run .INTERRUPT only if hit with interrupt signal 174 */ 175 if (signo == SIGINT) { 176 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 177 if (gn != NILGNODE) { 178 Compat_Make(gn, gn); 179 } 180 } 181 182 } 183 exit(signo); 184 } 185 186 /*- 187 *----------------------------------------------------------------------- 188 * CompatRunCommand -- 189 * Execute the next command for a target. If the command returns an 190 * error, the node's made field is set to ERROR and creation stops. 191 * 192 * Input: 193 * cmdp Command to execute 194 * gnp Node from which the command came 195 * 196 * Results: 197 * 0 if the command succeeded, 1 if an error occurred. 198 * 199 * Side Effects: 200 * The node's 'made' field may be set to ERROR. 201 * 202 *----------------------------------------------------------------------- 203 */ 204 int 205 CompatRunCommand(ClientData cmdp, ClientData gnp) 206 { 207 char *cmdStart; /* Start of expanded command */ 208 char *cp, *bp; 209 Boolean silent, /* Don't print command */ 210 doIt; /* Execute even if -n */ 211 volatile Boolean errCheck; /* Check errors */ 212 int reason; /* Reason for child's death */ 213 int status; /* Description of child's death */ 214 int cpid; /* Child actually found */ 215 ReturnStatus retstat; /* Status of fork */ 216 LstNode cmdNode; /* Node where current command is located */ 217 const char ** volatile av; /* Argument vector for thing to exec */ 218 char ** volatile mav;/* Copy of the argument vector for freeing */ 219 int argc; /* Number of arguments in av or 0 if not 220 * dynamically allocated */ 221 Boolean local; /* TRUE if command should be executed 222 * locally */ 223 Boolean useShell; /* TRUE if command should be executed 224 * using a shell */ 225 char * volatile cmd = (char *)cmdp; 226 GNode *gn = (GNode *)gnp; 227 228 /* 229 * Avoid clobbered variable warnings by forcing the compiler 230 * to ``unregister'' variables 231 */ 232 silent = gn->type & OP_SILENT; 233 errCheck = !(gn->type & OP_IGNORE); 234 doIt = FALSE; 235 236 cmdNode = Lst_Member(gn->commands, cmd); 237 cmdStart = Var_Subst(NULL, cmd, gn, FALSE); 238 239 /* 240 * brk_string will return an argv with a NULL in av[0], thus causing 241 * execvp to choke and die horribly. Besides, how can we execute a null 242 * command? In any case, we warn the user that the command expanded to 243 * nothing (is this the right thing to do?). 244 */ 245 246 if (*cmdStart == '\0') { 247 free(cmdStart); 248 Error("%s expands to empty string", cmd); 249 return(0); 250 } else { 251 cmd = cmdStart; 252 } 253 Lst_Replace(cmdNode, cmdStart); 254 255 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) { 256 (void)Lst_AtEnd(ENDNode->commands, cmdStart); 257 return(0); 258 } else if (strcmp(cmdStart, "...") == 0) { 259 gn->type |= OP_SAVE_CMDS; 260 return(0); 261 } 262 263 while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) { 264 switch (*cmd) { 265 case '@': 266 silent = TRUE; 267 break; 268 case '-': 269 errCheck = FALSE; 270 break; 271 case '+': 272 doIt = TRUE; 273 if (!meta[0]) /* we came here from jobs */ 274 Compat_Init(); 275 break; 276 } 277 cmd++; 278 } 279 280 while (isspace((unsigned char)*cmd)) 281 cmd++; 282 283 #if !defined(MAKE_NATIVE) 284 /* 285 * In a non-native build, the host environment might be weird enough 286 * that it's necessary to go through a shell to get the correct 287 * behaviour. Or perhaps the shell has been replaced with something 288 * that does extra logging, and that should not be bypassed. 289 */ 290 useShell = TRUE; 291 #else 292 /* 293 * Search for meta characters in the command. If there are no meta 294 * characters, there's no need to execute a shell to execute the 295 * command. 296 */ 297 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) { 298 continue; 299 } 300 useShell = (*cp != '\0'); 301 #endif 302 303 /* 304 * Print the command before echoing if we're not supposed to be quiet for 305 * this one. We also print the command if -n given. 306 */ 307 if (!silent || NoExecute(gn)) { 308 printf("%s\n", cmd); 309 fflush(stdout); 310 } 311 312 /* 313 * If we're not supposed to execute any commands, this is as far as 314 * we go... 315 */ 316 if (!doIt && NoExecute(gn)) { 317 return (0); 318 } 319 320 if (useShell) { 321 /* 322 * We need to pass the command off to the shell, typically 323 * because the command contains a "meta" character. 324 */ 325 static const char *shargv[4]; 326 327 shargv[0] = shellPath; 328 /* 329 * The following work for any of the builtin shell specs. 330 */ 331 if (DEBUG(SHELL)) 332 shargv[1] = "-xc"; 333 else 334 shargv[1] = "-c"; 335 shargv[2] = cmd; 336 shargv[3] = NULL; 337 av = shargv; 338 argc = 0; 339 bp = NULL; 340 mav = NULL; 341 } else { 342 /* 343 * No meta-characters, so no need to exec a shell. Break the command 344 * into words to form an argument vector we can execute. 345 */ 346 mav = brk_string(cmd, &argc, TRUE, &bp); 347 av = (const char **)mav; 348 } 349 350 local = TRUE; 351 352 /* 353 * Fork and execute the single command. If the fork fails, we abort. 354 */ 355 cpid = vfork(); 356 if (cpid < 0) { 357 Fatal("Could not fork"); 358 } 359 if (cpid == 0) { 360 Check_Cwd(av); 361 if (local) 362 (void)execvp(av[0], (char *const *)UNCONST(av)); 363 else 364 (void)execv(av[0], (char *const *)UNCONST(av)); 365 execError("exec", av[0]); 366 _exit(1); 367 } 368 if (mav) 369 free(mav); 370 if (bp) 371 free(bp); 372 Lst_Replace(cmdNode, NULL); 373 374 /* 375 * The child is off and running. Now all we can do is wait... 376 */ 377 while (1) { 378 379 while ((retstat = wait(&reason)) != cpid) { 380 if (retstat == -1 && errno != EINTR) { 381 break; 382 } 383 } 384 385 if (retstat > -1) { 386 if (WIFSTOPPED(reason)) { 387 status = WSTOPSIG(reason); /* stopped */ 388 } else if (WIFEXITED(reason)) { 389 status = WEXITSTATUS(reason); /* exited */ 390 if (status != 0) { 391 if (DEBUG(ERROR)) { 392 fprintf(debug_file, "\n*** Failed target: %s\n*** Failed command: ", 393 gn->name); 394 for (cp = cmd; *cp; ) { 395 if (isspace((unsigned char)*cp)) { 396 fprintf(debug_file, " "); 397 while (isspace((unsigned char)*cp)) 398 cp++; 399 } else { 400 fprintf(debug_file, "%c", *cp); 401 cp++; 402 } 403 } 404 fprintf(debug_file, "\n"); 405 } 406 fprintf(debug_file, "*** Error code %d", status); 407 } 408 } else { 409 status = WTERMSIG(reason); /* signaled */ 410 printf("*** Signal %d", status); 411 } 412 413 414 if (!WIFEXITED(reason) || (status != 0)) { 415 if (errCheck) { 416 gn->made = ERROR; 417 if (keepgoing) { 418 /* 419 * Abort the current target, but let others 420 * continue. 421 */ 422 printf(" (continuing)\n"); 423 } 424 } else { 425 /* 426 * Continue executing commands for this target. 427 * If we return 0, this will happen... 428 */ 429 printf(" (ignored)\n"); 430 status = 0; 431 } 432 } 433 break; 434 } else { 435 Fatal("error in wait: %d: %s", retstat, strerror(errno)); 436 /*NOTREACHED*/ 437 } 438 } 439 free(cmdStart); 440 441 return (status); 442 } 443 444 /*- 445 *----------------------------------------------------------------------- 446 * Compat_Make -- 447 * Make a target. 448 * 449 * Input: 450 * gnp The node to make 451 * pgnp Parent to abort if necessary 452 * 453 * Results: 454 * 0 455 * 456 * Side Effects: 457 * If an error is detected and not being ignored, the process exits. 458 * 459 *----------------------------------------------------------------------- 460 */ 461 int 462 Compat_Make(ClientData gnp, ClientData pgnp) 463 { 464 GNode *gn = (GNode *)gnp; 465 GNode *pgn = (GNode *)pgnp; 466 467 if (!meta[0]) /* we came here from jobs */ 468 Compat_Init(); 469 if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) { 470 /* 471 * First mark ourselves to be made, then apply whatever transformations 472 * the suffix module thinks are necessary. Once that's done, we can 473 * descend and make all our children. If any of them has an error 474 * but the -k flag was given, our 'make' field will be set FALSE again. 475 * This is our signal to not attempt to do anything but abort our 476 * parent as well. 477 */ 478 gn->flags |= REMAKE; 479 gn->made = BEINGMADE; 480 if ((gn->type & OP_MADE) == 0) 481 Suff_FindDeps(gn); 482 Lst_ForEach(gn->children, Compat_Make, gn); 483 if ((gn->flags & REMAKE) == 0) { 484 gn->made = ABORTED; 485 pgn->flags &= ~REMAKE; 486 goto cohorts; 487 } 488 489 if (Lst_Member(gn->iParents, pgn) != NILLNODE) { 490 char *p1; 491 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0); 492 if (p1) 493 free(p1); 494 } 495 496 /* 497 * All the children were made ok. Now cmtime contains the modification 498 * time of the newest child, we need to find out if we exist and when 499 * we were modified last. The criteria for datedness are defined by the 500 * Make_OODate function. 501 */ 502 if (DEBUG(MAKE)) { 503 fprintf(debug_file, "Examining %s...", gn->name); 504 } 505 if (! Make_OODate(gn)) { 506 gn->made = UPTODATE; 507 if (DEBUG(MAKE)) { 508 fprintf(debug_file, "up-to-date.\n"); 509 } 510 goto cohorts; 511 } else if (DEBUG(MAKE)) { 512 fprintf(debug_file, "out-of-date.\n"); 513 } 514 515 /* 516 * If the user is just seeing if something is out-of-date, exit now 517 * to tell him/her "yes". 518 */ 519 if (queryFlag) { 520 exit(1); 521 } 522 523 /* 524 * We need to be re-made. We also have to make sure we've got a $? 525 * variable. To be nice, we also define the $> variable using 526 * Make_DoAllVar(). 527 */ 528 Make_DoAllVar(gn); 529 530 /* 531 * Alter our type to tell if errors should be ignored or things 532 * should not be printed so CompatRunCommand knows what to do. 533 */ 534 if (Targ_Ignore(gn)) { 535 gn->type |= OP_IGNORE; 536 } 537 if (Targ_Silent(gn)) { 538 gn->type |= OP_SILENT; 539 } 540 541 if (Job_CheckCommands(gn, Fatal)) { 542 /* 543 * Our commands are ok, but we still have to worry about the -t 544 * flag... 545 */ 546 if (!touchFlag || (gn->type & OP_MAKE)) { 547 curTarg = gn; 548 Lst_ForEach(gn->commands, CompatRunCommand, gn); 549 curTarg = NILGNODE; 550 } else { 551 Job_Touch(gn, gn->type & OP_SILENT); 552 } 553 } else { 554 gn->made = ERROR; 555 } 556 557 if (gn->made != ERROR) { 558 /* 559 * If the node was made successfully, mark it so, update 560 * its modification time and timestamp all its parents. Note 561 * that for .ZEROTIME targets, the timestamping isn't done. 562 * This is to keep its state from affecting that of its parent. 563 */ 564 gn->made = MADE; 565 pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0; 566 if (!(gn->type & OP_EXEC)) { 567 pgn->flags |= CHILDMADE; 568 Make_TimeStamp(pgn, gn); 569 } 570 } else if (keepgoing) { 571 pgn->flags &= ~REMAKE; 572 } else { 573 PrintOnError("\n\nStop."); 574 exit(1); 575 } 576 } else if (gn->made == ERROR) { 577 /* 578 * Already had an error when making this beastie. Tell the parent 579 * to abort. 580 */ 581 pgn->flags &= ~REMAKE; 582 } else { 583 if (Lst_Member(gn->iParents, pgn) != NILLNODE) { 584 char *p1; 585 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0); 586 if (p1) 587 free(p1); 588 } 589 switch(gn->made) { 590 case BEINGMADE: 591 Error("Graph cycles through %s", gn->name); 592 gn->made = ERROR; 593 pgn->flags &= ~REMAKE; 594 break; 595 case MADE: 596 if ((gn->type & OP_EXEC) == 0) { 597 pgn->flags |= CHILDMADE; 598 Make_TimeStamp(pgn, gn); 599 } 600 break; 601 case UPTODATE: 602 if ((gn->type & OP_EXEC) == 0) { 603 Make_TimeStamp(pgn, gn); 604 } 605 break; 606 default: 607 break; 608 } 609 } 610 611 cohorts: 612 Lst_ForEach(gn->cohorts, Compat_Make, pgnp); 613 return (0); 614 } 615 616 /*- 617 *----------------------------------------------------------------------- 618 * Compat_Run -- 619 * Initialize this mode and start making. 620 * 621 * Input: 622 * targs List of target nodes to re-create 623 * 624 * Results: 625 * None. 626 * 627 * Side Effects: 628 * Guess what? 629 * 630 *----------------------------------------------------------------------- 631 */ 632 void 633 Compat_Run(Lst targs) 634 { 635 GNode *gn = NULL;/* Current root target */ 636 int errors; /* Number of targets not remade due to errors */ 637 638 Compat_Init(); 639 640 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 641 signal(SIGINT, CompatInterrupt); 642 } 643 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 644 signal(SIGTERM, CompatInterrupt); 645 } 646 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 647 signal(SIGHUP, CompatInterrupt); 648 } 649 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 650 signal(SIGQUIT, CompatInterrupt); 651 } 652 653 ENDNode = Targ_FindNode(".END", TARG_CREATE); 654 ENDNode->type = OP_SPECIAL; 655 /* 656 * If the user has defined a .BEGIN target, execute the commands attached 657 * to it. 658 */ 659 if (!queryFlag) { 660 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE); 661 if (gn != NILGNODE) { 662 Compat_Make(gn, gn); 663 if (gn->made == ERROR) { 664 PrintOnError("\n\nStop."); 665 exit(1); 666 } 667 } 668 } 669 670 /* 671 * Expand .USE nodes right now, because they can modify the structure 672 * of the tree. 673 */ 674 Make_ExpandUse(targs); 675 676 /* 677 * For each entry in the list of targets to create, call Compat_Make on 678 * it to create the thing. Compat_Make will leave the 'made' field of gn 679 * in one of several states: 680 * UPTODATE gn was already up-to-date 681 * MADE gn was recreated successfully 682 * ERROR An error occurred while gn was being created 683 * ABORTED gn was not remade because one of its inferiors 684 * could not be made due to errors. 685 */ 686 errors = 0; 687 while (!Lst_IsEmpty (targs)) { 688 gn = (GNode *)Lst_DeQueue(targs); 689 Compat_Make(gn, gn); 690 691 if (gn->made == UPTODATE) { 692 printf("`%s' is up to date.\n", gn->name); 693 } else if (gn->made == ABORTED) { 694 printf("`%s' not remade because of errors.\n", gn->name); 695 errors += 1; 696 } 697 } 698 699 /* 700 * If the user has defined a .END target, run its commands. 701 */ 702 if (errors == 0) { 703 Compat_Make(ENDNode, ENDNode); 704 if (gn->made == ERROR) { 705 PrintOnError("\n\nStop."); 706 exit(1); 707 } 708 } 709 } 710