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