1 /* $NetBSD: compat.c,v 1.63 2006/10/15 08:38:21 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.63 2006/10/15 08:38:21 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.63 2006/10/15 08:38:21 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 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 **av; /* Argument vector for thing to exec */ 218 char **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 *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 #if __GNUC__ 233 (void) &av; 234 (void) &errCheck; 235 (void) &cmd; 236 #endif 237 silent = gn->type & OP_SILENT; 238 errCheck = !(gn->type & OP_IGNORE); 239 doIt = FALSE; 240 241 cmdNode = Lst_Member(gn->commands, (ClientData)cmd); 242 cmdStart = Var_Subst(NULL, cmd, gn, FALSE); 243 244 /* 245 * brk_string will return an argv with a NULL in av[0], thus causing 246 * execvp to choke and die horribly. Besides, how can we execute a null 247 * command? In any case, we warn the user that the command expanded to 248 * nothing (is this the right thing to do?). 249 */ 250 251 if (*cmdStart == '\0') { 252 free(cmdStart); 253 Error("%s expands to empty string", cmd); 254 return(0); 255 } else { 256 cmd = cmdStart; 257 } 258 Lst_Replace(cmdNode, (ClientData)cmdStart); 259 260 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) { 261 (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart); 262 return(0); 263 } else if (strcmp(cmdStart, "...") == 0) { 264 gn->type |= OP_SAVE_CMDS; 265 return(0); 266 } 267 268 while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) { 269 switch (*cmd) { 270 case '@': 271 silent = TRUE; 272 break; 273 case '-': 274 errCheck = FALSE; 275 break; 276 case '+': 277 doIt = TRUE; 278 if (!meta[0]) /* we came here from jobs */ 279 Compat_Init(); 280 break; 281 } 282 cmd++; 283 } 284 285 while (isspace((unsigned char)*cmd)) 286 cmd++; 287 288 #if !defined(MAKE_NATIVE) 289 /* 290 * In a non-native build, the host environment might be weird enough 291 * that it's necessary to go through a shell to get the correct 292 * behaviour. Or perhaps the shell has been replaced with something 293 * that does extra logging, and that should not be bypassed. 294 */ 295 useShell = TRUE; 296 #else 297 /* 298 * Search for meta characters in the command. If there are no meta 299 * characters, there's no need to execute a shell to execute the 300 * command. 301 */ 302 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) { 303 continue; 304 } 305 useShell = (*cp != '\0'); 306 #endif 307 308 /* 309 * Print the command before echoing if we're not supposed to be quiet for 310 * this one. We also print the command if -n given. 311 */ 312 if (!silent || NoExecute(gn)) { 313 printf("%s\n", cmd); 314 fflush(stdout); 315 } 316 317 /* 318 * If we're not supposed to execute any commands, this is as far as 319 * we go... 320 */ 321 if (!doIt && NoExecute(gn)) { 322 return (0); 323 } 324 325 if (useShell) { 326 /* 327 * We need to pass the command off to the shell, typically 328 * because the command contains a "meta" character. 329 */ 330 static const char *shargv[4]; 331 332 shargv[0] = shellPath; 333 /* 334 * The following work for any of the builtin shell specs. 335 */ 336 if (DEBUG(SHELL)) 337 shargv[1] = "-xc"; 338 else 339 shargv[1] = "-c"; 340 shargv[2] = cmd; 341 shargv[3] = NULL; 342 av = shargv; 343 argc = 0; 344 bp = NULL; 345 mav = NULL; 346 } else { 347 /* 348 * No meta-characters, so no need to exec a shell. Break the command 349 * into words to form an argument vector we can execute. 350 */ 351 mav = brk_string(cmd, &argc, TRUE, &bp); 352 av = (const char **)mav; 353 } 354 355 local = TRUE; 356 357 /* 358 * Fork and execute the single command. If the fork fails, we abort. 359 */ 360 cpid = vfork(); 361 if (cpid < 0) { 362 Fatal("Could not fork"); 363 } 364 if (cpid == 0) { 365 Check_Cwd(av); 366 if (local) 367 (void)execvp(av[0], (char *const *)UNCONST(av)); 368 else 369 (void)execv(av[0], (char *const *)UNCONST(av)); 370 execError("exec", av[0]); 371 _exit(1); 372 } 373 if (mav) 374 free(mav); 375 if (bp) 376 free(bp); 377 Lst_Replace(cmdNode, (ClientData) NULL); 378 379 /* 380 * The child is off and running. Now all we can do is wait... 381 */ 382 while (1) { 383 384 while ((retstat = wait(&reason)) != cpid) { 385 if (retstat == -1 && errno != EINTR) { 386 break; 387 } 388 } 389 390 if (retstat > -1) { 391 if (WIFSTOPPED(reason)) { 392 status = WSTOPSIG(reason); /* stopped */ 393 } else if (WIFEXITED(reason)) { 394 status = WEXITSTATUS(reason); /* exited */ 395 if (status != 0) { 396 if (DEBUG(ERROR)) { 397 fprintf(debug_file, "\n*** Failed target: %s\n*** Failed command: ", 398 gn->name); 399 for (cp = cmd; *cp; ) { 400 if (isspace((unsigned char)*cp)) { 401 fprintf(debug_file, " "); 402 while (isspace((unsigned char)*cp)) 403 cp++; 404 } else { 405 fprintf(debug_file, "%c", *cp); 406 cp++; 407 } 408 } 409 fprintf(debug_file, "\n"); 410 } 411 fprintf(debug_file, "*** Error code %d", status); 412 } 413 } else { 414 status = WTERMSIG(reason); /* signaled */ 415 printf("*** Signal %d", status); 416 } 417 418 419 if (!WIFEXITED(reason) || (status != 0)) { 420 if (errCheck) { 421 gn->made = ERROR; 422 if (keepgoing) { 423 /* 424 * Abort the current target, but let others 425 * continue. 426 */ 427 printf(" (continuing)\n"); 428 } 429 } else { 430 /* 431 * Continue executing commands for this target. 432 * If we return 0, this will happen... 433 */ 434 printf(" (ignored)\n"); 435 status = 0; 436 } 437 } 438 break; 439 } else { 440 Fatal("error in wait: %d: %s", retstat, strerror(errno)); 441 /*NOTREACHED*/ 442 } 443 } 444 free(cmdStart); 445 446 return (status); 447 } 448 449 /*- 450 *----------------------------------------------------------------------- 451 * Compat_Make -- 452 * Make a target. 453 * 454 * Input: 455 * gnp The node to make 456 * pgnp Parent to abort if necessary 457 * 458 * Results: 459 * 0 460 * 461 * Side Effects: 462 * If an error is detected and not being ignored, the process exits. 463 * 464 *----------------------------------------------------------------------- 465 */ 466 int 467 Compat_Make(ClientData gnp, ClientData pgnp) 468 { 469 GNode *gn = (GNode *)gnp; 470 GNode *pgn = (GNode *)pgnp; 471 472 if (!meta[0]) /* we came here from jobs */ 473 Compat_Init(); 474 if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) { 475 /* 476 * First mark ourselves to be made, then apply whatever transformations 477 * the suffix module thinks are necessary. Once that's done, we can 478 * descend and make all our children. If any of them has an error 479 * but the -k flag was given, our 'make' field will be set FALSE again. 480 * This is our signal to not attempt to do anything but abort our 481 * parent as well. 482 */ 483 gn->flags |= REMAKE; 484 gn->made = BEINGMADE; 485 if ((gn->type & OP_MADE) == 0) 486 Suff_FindDeps(gn); 487 Lst_ForEach(gn->children, Compat_Make, (ClientData)gn); 488 if ((gn->flags & REMAKE) == 0) { 489 gn->made = ABORTED; 490 pgn->flags &= ~REMAKE; 491 goto cohorts; 492 } 493 494 if (Lst_Member(gn->iParents, pgn) != NILLNODE) { 495 char *p1; 496 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0); 497 if (p1) 498 free(p1); 499 } 500 501 /* 502 * All the children were made ok. Now cmtime contains the modification 503 * time of the newest child, we need to find out if we exist and when 504 * we were modified last. The criteria for datedness are defined by the 505 * Make_OODate function. 506 */ 507 if (DEBUG(MAKE)) { 508 fprintf(debug_file, "Examining %s...", gn->name); 509 } 510 if (! Make_OODate(gn)) { 511 gn->made = UPTODATE; 512 if (DEBUG(MAKE)) { 513 fprintf(debug_file, "up-to-date.\n"); 514 } 515 goto cohorts; 516 } else if (DEBUG(MAKE)) { 517 fprintf(debug_file, "out-of-date.\n"); 518 } 519 520 /* 521 * If the user is just seeing if something is out-of-date, exit now 522 * to tell him/her "yes". 523 */ 524 if (queryFlag) { 525 exit(1); 526 } 527 528 /* 529 * We need to be re-made. We also have to make sure we've got a $? 530 * variable. To be nice, we also define the $> variable using 531 * Make_DoAllVar(). 532 */ 533 Make_DoAllVar(gn); 534 535 /* 536 * Alter our type to tell if errors should be ignored or things 537 * should not be printed so CompatRunCommand knows what to do. 538 */ 539 if (Targ_Ignore(gn)) { 540 gn->type |= OP_IGNORE; 541 } 542 if (Targ_Silent(gn)) { 543 gn->type |= OP_SILENT; 544 } 545 546 if (Job_CheckCommands(gn, Fatal)) { 547 /* 548 * Our commands are ok, but we still have to worry about the -t 549 * flag... 550 */ 551 if (!touchFlag || (gn->type & OP_MAKE)) { 552 curTarg = gn; 553 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn); 554 curTarg = NILGNODE; 555 } else { 556 Job_Touch(gn, gn->type & OP_SILENT); 557 } 558 } else { 559 gn->made = ERROR; 560 } 561 562 if (gn->made != ERROR) { 563 /* 564 * If the node was made successfully, mark it so, update 565 * its modification time and timestamp all its parents. Note 566 * that for .ZEROTIME targets, the timestamping isn't done. 567 * This is to keep its state from affecting that of its parent. 568 */ 569 gn->made = MADE; 570 pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0; 571 if (!(gn->type & OP_EXEC)) { 572 pgn->flags |= CHILDMADE; 573 Make_TimeStamp(pgn, gn); 574 } 575 } else if (keepgoing) { 576 pgn->flags &= ~REMAKE; 577 } else { 578 PrintOnError("\n\nStop."); 579 exit(1); 580 } 581 } else if (gn->made == ERROR) { 582 /* 583 * Already had an error when making this beastie. Tell the parent 584 * to abort. 585 */ 586 pgn->flags &= ~REMAKE; 587 } else { 588 if (Lst_Member(gn->iParents, pgn) != NILLNODE) { 589 char *p1; 590 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0); 591 if (p1) 592 free(p1); 593 } 594 switch(gn->made) { 595 case BEINGMADE: 596 Error("Graph cycles through %s", gn->name); 597 gn->made = ERROR; 598 pgn->flags &= ~REMAKE; 599 break; 600 case MADE: 601 if ((gn->type & OP_EXEC) == 0) { 602 pgn->flags |= CHILDMADE; 603 Make_TimeStamp(pgn, gn); 604 } 605 break; 606 case UPTODATE: 607 if ((gn->type & OP_EXEC) == 0) { 608 Make_TimeStamp(pgn, gn); 609 } 610 break; 611 default: 612 break; 613 } 614 } 615 616 cohorts: 617 Lst_ForEach(gn->cohorts, Compat_Make, pgnp); 618 return (0); 619 } 620 621 /*- 622 *----------------------------------------------------------------------- 623 * Compat_Run -- 624 * Initialize this mode and start making. 625 * 626 * Input: 627 * targs List of target nodes to re-create 628 * 629 * Results: 630 * None. 631 * 632 * Side Effects: 633 * Guess what? 634 * 635 *----------------------------------------------------------------------- 636 */ 637 void 638 Compat_Run(Lst targs) 639 { 640 GNode *gn = NULL;/* Current root target */ 641 int errors; /* Number of targets not remade due to errors */ 642 643 Compat_Init(); 644 645 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 646 signal(SIGINT, CompatInterrupt); 647 } 648 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 649 signal(SIGTERM, CompatInterrupt); 650 } 651 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 652 signal(SIGHUP, CompatInterrupt); 653 } 654 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 655 signal(SIGQUIT, CompatInterrupt); 656 } 657 658 ENDNode = Targ_FindNode(".END", TARG_CREATE); 659 ENDNode->type = OP_SPECIAL; 660 /* 661 * If the user has defined a .BEGIN target, execute the commands attached 662 * to it. 663 */ 664 if (!queryFlag) { 665 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE); 666 if (gn != NILGNODE) { 667 Compat_Make(gn, gn); 668 if (gn->made == ERROR) { 669 PrintOnError("\n\nStop."); 670 exit(1); 671 } 672 } 673 } 674 675 /* 676 * Expand .USE nodes right now, because they can modify the structure 677 * of the tree. 678 */ 679 Lst_Destroy(Make_ExpandUse(targs), NOFREE); 680 681 /* 682 * For each entry in the list of targets to create, call Compat_Make on 683 * it to create the thing. Compat_Make will leave the 'made' field of gn 684 * in one of several states: 685 * UPTODATE gn was already up-to-date 686 * MADE gn was recreated successfully 687 * ERROR An error occurred while gn was being created 688 * ABORTED gn was not remade because one of its inferiors 689 * could not be made due to errors. 690 */ 691 errors = 0; 692 while (!Lst_IsEmpty (targs)) { 693 gn = (GNode *)Lst_DeQueue(targs); 694 Compat_Make(gn, gn); 695 696 if (gn->made == UPTODATE) { 697 printf("`%s' is up to date.\n", gn->name); 698 } else if (gn->made == ABORTED) { 699 printf("`%s' not remade because of errors.\n", gn->name); 700 errors += 1; 701 } 702 } 703 704 /* 705 * If the user has defined a .END target, run its commands. 706 */ 707 if (errors == 0) { 708 Compat_Make(ENDNode, ENDNode); 709 if (gn->made == ERROR) { 710 PrintOnError("\n\nStop."); 711 exit(1); 712 } 713 } 714 } 715