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