1 /* $NetBSD: job.c,v 1.53 2001/07/24 05:06:40 matt Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 5 * Copyright (c) 1988, 1989 by Adam de Boor 6 * Copyright (c) 1989 by Berkeley Softworks 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * Adam de Boor. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 */ 40 41 #ifdef MAKE_BOOTSTRAP 42 static char rcsid[] = "$NetBSD: job.c,v 1.53 2001/07/24 05:06:40 matt Exp $"; 43 #else 44 #include <sys/cdefs.h> 45 #ifndef lint 46 #if 0 47 static char sccsid[] = "@(#)job.c 8.2 (Berkeley) 3/19/94"; 48 #else 49 __RCSID("$NetBSD: job.c,v 1.53 2001/07/24 05:06:40 matt Exp $"); 50 #endif 51 #endif /* not lint */ 52 #endif 53 54 /*- 55 * job.c -- 56 * handle the creation etc. of our child processes. 57 * 58 * Interface: 59 * Job_Make Start the creation of the given target. 60 * 61 * Job_CatchChildren Check for and handle the termination of any 62 * children. This must be called reasonably 63 * frequently to keep the whole make going at 64 * a decent clip, since job table entries aren't 65 * removed until their process is caught this way. 66 * Its single argument is TRUE if the function 67 * should block waiting for a child to terminate. 68 * 69 * Job_CatchOutput Print any output our children have produced. 70 * Should also be called fairly frequently to 71 * keep the user informed of what's going on. 72 * If no output is waiting, it will block for 73 * a time given by the SEL_* constants, below, 74 * or until output is ready. 75 * 76 * Job_Init Called to intialize this module. in addition, 77 * any commands attached to the .BEGIN target 78 * are executed before this function returns. 79 * Hence, the makefile must have been parsed 80 * before this function is called. 81 * 82 * Job_End Cleanup any memory used. 83 * 84 * Job_Empty Return TRUE if the job table is completely 85 * empty. 86 * 87 * Job_ParseShell Given the line following a .SHELL target, parse 88 * the line as a shell specification. Returns 89 * FAILURE if the spec was incorrect. 90 * 91 * Job_Finish Perform any final processing which needs doing. 92 * This includes the execution of any commands 93 * which have been/were attached to the .END 94 * target. It should only be called when the 95 * job table is empty. 96 * 97 * Job_AbortAll Abort all currently running jobs. It doesn't 98 * handle output or do anything for the jobs, 99 * just kills them. It should only be called in 100 * an emergency, as it were. 101 * 102 * Job_CheckCommands Verify that the commands for a target are 103 * ok. Provide them if necessary and possible. 104 * 105 * Job_Touch Update a target without really updating it. 106 * 107 * Job_Wait Wait for all currently-running jobs to finish. 108 */ 109 110 #include <sys/types.h> 111 #include <sys/stat.h> 112 #include <sys/file.h> 113 #include <sys/time.h> 114 #include <sys/wait.h> 115 #include <fcntl.h> 116 #include <errno.h> 117 #include <utime.h> 118 #include <stdio.h> 119 #include <string.h> 120 #include <signal.h> 121 #ifndef RMT_WILL_WATCH 122 #ifndef USE_SELECT 123 #include <poll.h> 124 #endif 125 #endif 126 #include "make.h" 127 #include "hash.h" 128 #include "dir.h" 129 #include "job.h" 130 #include "pathnames.h" 131 #include "trace.h" 132 #ifdef REMOTE 133 #include "rmt.h" 134 # define STATIC 135 #else 136 # define STATIC static 137 #endif 138 139 /* 140 * error handling variables 141 */ 142 static int errors = 0; /* number of errors reported */ 143 static int aborting = 0; /* why is the make aborting? */ 144 #define ABORT_ERROR 1 /* Because of an error */ 145 #define ABORT_INTERRUPT 2 /* Because it was interrupted */ 146 #define ABORT_WAIT 3 /* Waiting for jobs to finish */ 147 148 /* 149 * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file 150 * is a char! So when we go above 127 we turn negative! 151 */ 152 #define FILENO(a) ((unsigned) fileno(a)) 153 154 /* 155 * post-make command processing. The node postCommands is really just the 156 * .END target but we keep it around to avoid having to search for it 157 * all the time. 158 */ 159 static GNode *postCommands; /* node containing commands to execute when 160 * everything else is done */ 161 static int numCommands; /* The number of commands actually printed 162 * for a target. Should this number be 163 * 0, no shell will be executed. */ 164 165 /* 166 * Return values from JobStart. 167 */ 168 #define JOB_RUNNING 0 /* Job is running */ 169 #define JOB_ERROR 1 /* Error in starting the job */ 170 #define JOB_FINISHED 2 /* The job is already finished */ 171 #define JOB_STOPPED 3 /* The job is stopped */ 172 173 174 175 /* 176 * Descriptions for various shells. 177 */ 178 static Shell shells[] = { 179 /* 180 * CSH description. The csh can do echo control by playing 181 * with the setting of the 'echo' shell variable. Sadly, 182 * however, it is unable to do error control nicely. 183 */ 184 { 185 "csh", 186 TRUE, "unset verbose", "set verbose", "unset verbose", 10, 187 FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"", 188 "v", "e", 189 }, 190 /* 191 * SH description. Echo control is also possible and, under 192 * sun UNIX anyway, one can even control error checking. 193 */ 194 { 195 "sh", 196 TRUE, "set -", "set -v", "set -", 5, 197 TRUE, "set -e", "set +e", 198 #ifdef OLDBOURNESHELL 199 FALSE, "echo \"%s\"\n", "sh -c '%s || exit 0'\n", 200 #endif 201 #ifdef __NetBSD__ 202 "vq", 203 #else 204 "v", 205 #endif 206 "e", 207 }, 208 /* 209 * UNKNOWN. 210 */ 211 { 212 (char *) 0, 213 FALSE, (char *) 0, (char *) 0, (char *) 0, 0, 214 FALSE, (char *) 0, (char *) 0, 215 (char *) 0, (char *) 0, 216 } 217 }; 218 static Shell *commandShell = &shells[DEFSHELL];/* this is the shell to 219 * which we pass all 220 * commands in the Makefile. 221 * It is set by the 222 * Job_ParseShell function */ 223 static char *shellPath = NULL, /* full pathname of 224 * executable image */ 225 *shellName = NULL, /* last component of shell */ 226 *shellArgv = NULL; /* Custom shell args */ 227 228 229 static int maxJobs; /* The most children we can run at once */ 230 static int maxLocal; /* The most local ones we can have */ 231 STATIC int nJobs; /* The number of children currently running */ 232 STATIC int nLocal; /* The number of local children */ 233 STATIC Lst jobs; /* The structures that describe them */ 234 static Boolean wantToken; /* we want a token */ 235 236 /* 237 * Set of descriptors of pipes connected to 238 * the output channels of children 239 */ 240 #ifndef RMT_WILL_WATCH 241 #ifdef USE_SELECT 242 static fd_set outputs; 243 #else 244 static struct pollfd *fds = NULL; 245 static Job **jobfds = NULL; 246 static int nfds = 0; 247 static int maxfds = 0; 248 static void watchfd __P((Job *)); 249 static void clearfd __P((Job *)); 250 static int readyfd __P((Job *)); 251 #define JBSTART 256 252 #define JBFACTOR 2 253 #endif 254 #endif 255 256 STATIC GNode *lastNode; /* The node for which output was most recently 257 * produced. */ 258 STATIC char *targFmt; /* Format string to use to head output from a 259 * job when it's not the most-recent job heard 260 * from */ 261 static Job tokenWaitJob; /* token wait pseudo-job */ 262 int job_pipe[2] = { -1, -1 }; /* job server pipes. */ 263 264 #ifdef REMOTE 265 # define TARG_FMT "--- %s at %s ---\n" /* Default format */ 266 # define MESSAGE(fp, gn) \ 267 (void) fprintf(fp, targFmt, gn->name, gn->rem.hname) 268 #else 269 # define TARG_FMT "--- %s ---\n" /* Default format */ 270 # define MESSAGE(fp, gn) \ 271 (void) fprintf(fp, targFmt, gn->name) 272 #endif 273 274 /* 275 * When JobStart attempts to run a job remotely but can't, and isn't allowed 276 * to run the job locally, or when Job_CatchChildren detects a job that has 277 * been migrated home, the job is placed on the stoppedJobs queue to be run 278 * when the next job finishes. 279 */ 280 STATIC Lst stoppedJobs; /* Lst of Job structures describing 281 * jobs that were stopped due to concurrency 282 * limits or migration home */ 283 284 285 #if defined(USE_PGRP) && defined(SYSV) 286 # define KILL(pid, sig) kill(-(pid), (sig)) 287 #else 288 # if defined(USE_PGRP) 289 # define KILL(pid, sig) killpg((pid), (sig)) 290 # else 291 # define KILL(pid, sig) kill((pid), (sig)) 292 # endif 293 #endif 294 295 /* 296 * Grmpf... There is no way to set bits of the wait structure 297 * anymore with the stupid W*() macros. I liked the union wait 298 * stuff much more. So, we devise our own macros... This is 299 * really ugly, use dramamine sparingly. You have been warned. 300 */ 301 #ifndef W_STOPCODE 302 #define W_STOPCODE(sig) (((sig) << 8) | 0177) 303 #endif 304 #ifndef W_EXITCODE 305 #define W_EXITCODE(ret, sig) ((ret << 8) | (sig)) 306 #endif 307 308 static int JobCondPassSig __P((ClientData, ClientData)); 309 static void JobPassSig __P((int)); 310 static void JobIgnoreSig __P((int)); 311 #ifdef USE_PGRP 312 static void JobContinueSig __P((int)); 313 #endif 314 static int JobCmpPid __P((ClientData, ClientData)); 315 static int JobPrintCommand __P((ClientData, ClientData)); 316 static int JobSaveCommand __P((ClientData, ClientData)); 317 static void JobClose __P((Job *)); 318 #ifdef REMOTE 319 static int JobCmpRmtID __P((Job *, int)); 320 # ifdef RMT_WILL_WATCH 321 static void JobLocalInput __P((int, Job *)); 322 # endif 323 #else 324 static void JobFinish __P((Job *, int *)); 325 static void JobExec __P((Job *, char **)); 326 #endif 327 static void JobMakeArgv __P((Job *, char **)); 328 static void JobRestart __P((Job *)); 329 static int JobStart __P((GNode *, int, Job *)); 330 static char *JobOutput __P((Job *, char *, char *, int)); 331 static void JobDoOutput __P((Job *, Boolean)); 332 static Shell *JobMatchShell __P((char *)); 333 static void JobInterrupt __P((int, int)); 334 static void JobRestartJobs __P((void)); 335 static void JobTokenAdd __P((void)); 336 337 /*- 338 *----------------------------------------------------------------------- 339 * JobCondPassSig -- 340 * Pass a signal to a job if the job is remote or if USE_PGRP 341 * is defined. 342 * 343 * Results: 344 * === 0 345 * 346 * Side Effects: 347 * None, except the job may bite it. 348 * 349 *----------------------------------------------------------------------- 350 */ 351 static int 352 JobCondPassSig(jobp, signop) 353 ClientData jobp; /* Job to biff */ 354 ClientData signop; /* Signal to send it */ 355 { 356 Job *job = (Job *) jobp; 357 int signo = *(int *) signop; 358 #ifdef RMT_WANTS_SIGNALS 359 if (job->flags & JOB_REMOTE) { 360 (void) Rmt_Signal(job, signo); 361 } else { 362 KILL(job->pid, signo); 363 } 364 #else 365 /* 366 * Assume that sending the signal to job->pid will signal any remote 367 * job as well. 368 */ 369 if (DEBUG(JOB)) { 370 (void) fprintf(stdout, 371 "JobCondPassSig passing signal %d to child %d.\n", 372 signo, job->pid); 373 (void) fflush(stdout); 374 } 375 KILL(job->pid, signo); 376 #endif 377 return 0; 378 } 379 380 /*- 381 *----------------------------------------------------------------------- 382 * JobIgnoreSig -- 383 * No-op signal handler so we wake up from poll. 384 * 385 * Results: 386 * None. 387 * 388 * Side Effects: 389 * None. 390 * 391 *----------------------------------------------------------------------- 392 */ 393 static void 394 JobIgnoreSig(signo) 395 int signo; /* The signal number we've received */ 396 { 397 /* 398 * Do nothing. The mere fact that we've been called will cause 399 * poll/select in Job_CatchOutput() to return early. 400 */ 401 } 402 403 404 #ifdef USE_PGRP 405 /*- 406 *----------------------------------------------------------------------- 407 * JobContinueSig -- 408 * Resume all stopped jobs. 409 * 410 * Results: 411 * None. 412 * 413 * Side Effects: 414 * Jobs start running again. 415 * 416 *----------------------------------------------------------------------- 417 */ 418 static void 419 JobContinueSig(signo) 420 int signo; /* The signal number we've received */ 421 { 422 if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) { 423 (void) signal(SIGTSTP, JobPassSig); 424 } 425 JobRestartJobs(); 426 } 427 #endif 428 429 /*- 430 *----------------------------------------------------------------------- 431 * JobPassSig -- 432 * Pass a signal on to all remote jobs and to all local jobs if 433 * USE_PGRP is defined, then die ourselves. 434 * 435 * Results: 436 * None. 437 * 438 * Side Effects: 439 * We die by the same signal. 440 * 441 *----------------------------------------------------------------------- 442 */ 443 static void 444 JobPassSig(signo) 445 int signo; /* The signal number we've received */ 446 { 447 sigset_t nmask, omask; 448 struct sigaction act; 449 int sigcont; 450 451 if (DEBUG(JOB)) { 452 (void) fprintf(stdout, "JobPassSig(%d) called.\n", signo); 453 (void) fflush(stdout); 454 } 455 Lst_ForEach(jobs, JobCondPassSig, (ClientData) &signo); 456 457 /* 458 * Deal with proper cleanup based on the signal received. We only run 459 * the .INTERRUPT target if the signal was in fact an interrupt. The other 460 * three termination signals are more of a "get out *now*" command. 461 */ 462 if (signo == SIGINT) { 463 JobInterrupt(TRUE, signo); 464 } else if ((signo == SIGHUP) || (signo == SIGTERM) || (signo == SIGQUIT)) { 465 JobInterrupt(FALSE, signo); 466 } 467 468 /* 469 * Leave gracefully if SIGQUIT, rather than core dumping. 470 */ 471 if (signo == SIGQUIT) { 472 Finish(0); 473 } 474 475 if (signo == SIGTSTP) { 476 Job_CatchChildren(FALSE); 477 } 478 /* 479 * Send ourselves the signal now we've given the message to everyone else. 480 * Note we block everything else possible while we're getting the signal. 481 * This ensures that all our jobs get continued when we wake up before 482 * we take any other signal. 483 */ 484 sigfillset(&nmask); 485 sigprocmask(SIG_SETMASK, &nmask, &omask); 486 act.sa_handler = SIG_DFL; 487 sigemptyset(&act.sa_mask); 488 act.sa_flags = 0; 489 sigaction(signo, &act, NULL); 490 491 if (DEBUG(JOB)) { 492 (void) fprintf(stdout, 493 "JobPassSig passing signal %d to self.\n", signo); 494 (void) fflush(stdout); 495 } 496 497 (void) kill(getpid(), signo); 498 if (signo != SIGTSTP) { 499 sigcont = SIGCONT; 500 Lst_ForEach(jobs, JobCondPassSig, (ClientData) &sigcont); 501 } 502 503 (void) sigprocmask(SIG_SETMASK, &omask, NULL); 504 sigprocmask(SIG_SETMASK, &omask, NULL); 505 if (signo != SIGCONT && signo != SIGTSTP) { 506 act.sa_handler = JobPassSig; 507 sigaction(sigcont, &act, NULL); 508 } 509 } 510 511 /*- 512 *----------------------------------------------------------------------- 513 * JobCmpPid -- 514 * Compare the pid of the job with the given pid and return 0 if they 515 * are equal. This function is called from Job_CatchChildren via 516 * Lst_Find to find the job descriptor of the finished job. 517 * 518 * Results: 519 * 0 if the pid's match 520 * 521 * Side Effects: 522 * None 523 *----------------------------------------------------------------------- 524 */ 525 static int 526 JobCmpPid(job, pid) 527 ClientData job; /* job to examine */ 528 ClientData pid; /* process id desired */ 529 { 530 return *(int *) pid - ((Job *) job)->pid; 531 } 532 533 #ifdef REMOTE 534 /*- 535 *----------------------------------------------------------------------- 536 * JobCmpRmtID -- 537 * Compare the rmtID of the job with the given rmtID and return 0 if they 538 * are equal. 539 * 540 * Results: 541 * 0 if the rmtID's match 542 * 543 * Side Effects: 544 * None. 545 *----------------------------------------------------------------------- 546 */ 547 static int 548 JobCmpRmtID(job, rmtID) 549 ClientData job; /* job to examine */ 550 ClientData rmtID; /* remote id desired */ 551 { 552 return(*(int *) rmtID - *(int *) job->rmtID); 553 } 554 #endif 555 556 /*- 557 *----------------------------------------------------------------------- 558 * JobPrintCommand -- 559 * Put out another command for the given job. If the command starts 560 * with an @ or a - we process it specially. In the former case, 561 * so long as the -s and -n flags weren't given to make, we stick 562 * a shell-specific echoOff command in the script. In the latter, 563 * we ignore errors for the entire job, unless the shell has error 564 * control. 565 * If the command is just "..." we take all future commands for this 566 * job to be commands to be executed once the entire graph has been 567 * made and return non-zero to signal that the end of the commands 568 * was reached. These commands are later attached to the postCommands 569 * node and executed by Job_End when all things are done. 570 * This function is called from JobStart via Lst_ForEach. 571 * 572 * Results: 573 * Always 0, unless the command was "..." 574 * 575 * Side Effects: 576 * If the command begins with a '-' and the shell has no error control, 577 * the JOB_IGNERR flag is set in the job descriptor. 578 * If the command is "..." and we're not ignoring such things, 579 * tailCmds is set to the successor node of the cmd. 580 * numCommands is incremented if the command is actually printed. 581 *----------------------------------------------------------------------- 582 */ 583 static int 584 JobPrintCommand(cmdp, jobp) 585 ClientData cmdp; /* command string to print */ 586 ClientData jobp; /* job for which to print it */ 587 { 588 Boolean noSpecials; /* true if we shouldn't worry about 589 * inserting special commands into 590 * the input stream. */ 591 Boolean shutUp = FALSE; /* true if we put a no echo command 592 * into the command file */ 593 Boolean errOff = FALSE; /* true if we turned error checking 594 * off before printing the command 595 * and need to turn it back on */ 596 char *cmdTemplate; /* Template to use when printing the 597 * command */ 598 char *cmdStart; /* Start of expanded command */ 599 LstNode cmdNode; /* Node for replacing the command */ 600 char *cmd = (char *) cmdp; 601 Job *job = (Job *) jobp; 602 char *cp; 603 604 noSpecials = NoExecute(job->node); 605 606 if (strcmp(cmd, "...") == 0) { 607 job->node->type |= OP_SAVE_CMDS; 608 if ((job->flags & JOB_IGNDOTS) == 0) { 609 job->tailCmds = Lst_Succ(Lst_Member(job->node->commands, 610 (ClientData)cmd)); 611 return 1; 612 } 613 return 0; 614 } 615 616 #define DBPRINTF(fmt, arg) if (DEBUG(JOB)) { \ 617 (void) fprintf(stdout, fmt, arg); \ 618 (void) fflush(stdout); \ 619 } \ 620 (void) fprintf(job->cmdFILE, fmt, arg); \ 621 (void) fflush(job->cmdFILE); 622 623 numCommands += 1; 624 625 /* 626 * For debugging, we replace each command with the result of expanding 627 * the variables in the command. 628 */ 629 cmdNode = Lst_Member(job->node->commands, (ClientData)cmd); 630 cmdStart = cmd = Var_Subst(NULL, cmd, job->node, FALSE); 631 Lst_Replace(cmdNode, (ClientData)cmdStart); 632 633 cmdTemplate = "%s\n"; 634 635 /* 636 * Check for leading @' and -'s to control echoing and error checking. 637 */ 638 while (*cmd == '@' || *cmd == '-') { 639 if (*cmd == '@') { 640 shutUp = TRUE; 641 } else { 642 errOff = TRUE; 643 } 644 cmd++; 645 } 646 647 while (isspace((unsigned char) *cmd)) 648 cmd++; 649 650 if (shutUp) { 651 if (!(job->flags & JOB_SILENT) && !noSpecials && 652 commandShell->hasEchoCtl) { 653 DBPRINTF("%s\n", commandShell->echoOff); 654 } else { 655 shutUp = FALSE; 656 } 657 } 658 659 if (errOff) { 660 if ( !(job->flags & JOB_IGNERR) && !noSpecials) { 661 if (commandShell->hasErrCtl) { 662 /* 663 * we don't want the error-control commands showing 664 * up either, so we turn off echoing while executing 665 * them. We could put another field in the shell 666 * structure to tell JobDoOutput to look for this 667 * string too, but why make it any more complex than 668 * it already is? 669 */ 670 if (!(job->flags & JOB_SILENT) && !shutUp && 671 commandShell->hasEchoCtl) { 672 DBPRINTF("%s\n", commandShell->echoOff); 673 DBPRINTF("%s\n", commandShell->ignErr); 674 DBPRINTF("%s\n", commandShell->echoOn); 675 } else { 676 DBPRINTF("%s\n", commandShell->ignErr); 677 } 678 } else if (commandShell->ignErr && 679 (*commandShell->ignErr != '\0')) 680 { 681 /* 682 * The shell has no error control, so we need to be 683 * weird to get it to ignore any errors from the command. 684 * If echoing is turned on, we turn it off and use the 685 * errCheck template to echo the command. Leave echoing 686 * off so the user doesn't see the weirdness we go through 687 * to ignore errors. Set cmdTemplate to use the weirdness 688 * instead of the simple "%s\n" template. 689 */ 690 if (!(job->flags & JOB_SILENT) && !shutUp && 691 commandShell->hasEchoCtl) { 692 DBPRINTF("%s\n", commandShell->echoOff); 693 DBPRINTF(commandShell->errCheck, cmd); 694 shutUp = TRUE; 695 } 696 cmdTemplate = commandShell->ignErr; 697 /* 698 * The error ignoration (hee hee) is already taken care 699 * of by the ignErr template, so pretend error checking 700 * is still on. 701 */ 702 errOff = FALSE; 703 } else { 704 errOff = FALSE; 705 } 706 } else { 707 errOff = FALSE; 708 } 709 } 710 711 if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0 && 712 (job->flags & JOB_TRACED) == 0) { 713 DBPRINTF("set -%s\n", "x"); 714 job->flags |= JOB_TRACED; 715 } 716 717 if ((cp = Check_Cwd_Cmd(cmd)) != NULL) { 718 DBPRINTF("cd %s; ", cp); 719 } 720 DBPRINTF(cmdTemplate, cmd); 721 free(cmdStart); 722 723 if (errOff) { 724 /* 725 * If echoing is already off, there's no point in issuing the 726 * echoOff command. Otherwise we issue it and pretend it was on 727 * for the whole command... 728 */ 729 if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl){ 730 DBPRINTF("%s\n", commandShell->echoOff); 731 shutUp = TRUE; 732 } 733 DBPRINTF("%s\n", commandShell->errCheck); 734 } 735 if (shutUp) { 736 DBPRINTF("%s\n", commandShell->echoOn); 737 } 738 return 0; 739 } 740 741 /*- 742 *----------------------------------------------------------------------- 743 * JobSaveCommand -- 744 * Save a command to be executed when everything else is done. 745 * Callback function for JobFinish... 746 * 747 * Results: 748 * Always returns 0 749 * 750 * Side Effects: 751 * The command is tacked onto the end of postCommands's commands list. 752 * 753 *----------------------------------------------------------------------- 754 */ 755 static int 756 JobSaveCommand(cmd, gn) 757 ClientData cmd; 758 ClientData gn; 759 { 760 cmd = (ClientData) Var_Subst(NULL, (char *) cmd, (GNode *) gn, FALSE); 761 (void) Lst_AtEnd(postCommands->commands, cmd); 762 return(0); 763 } 764 765 766 /*- 767 *----------------------------------------------------------------------- 768 * JobClose -- 769 * Called to close both input and output pipes when a job is finished. 770 * 771 * Results: 772 * Nada 773 * 774 * Side Effects: 775 * The file descriptors associated with the job are closed. 776 * 777 *----------------------------------------------------------------------- 778 */ 779 static void 780 JobClose(job) 781 Job *job; 782 { 783 if (usePipes && (job->flags & JOB_FIRST)) { 784 #ifdef RMT_WILL_WATCH 785 Rmt_Ignore(job->inPipe); 786 #else 787 #ifdef USE_SELECT 788 FD_CLR(job->inPipe, &outputs); 789 #else 790 clearfd(job); 791 #endif 792 #endif 793 if (job->outPipe != job->inPipe) { 794 (void) close(job->outPipe); 795 } 796 JobDoOutput(job, TRUE); 797 (void) close(job->inPipe); 798 } else { 799 (void) close(job->outFd); 800 JobDoOutput(job, TRUE); 801 } 802 } 803 804 /*- 805 *----------------------------------------------------------------------- 806 * JobFinish -- 807 * Do final processing for the given job including updating 808 * parents and starting new jobs as available/necessary. Note 809 * that we pay no attention to the JOB_IGNERR flag here. 810 * This is because when we're called because of a noexecute flag 811 * or something, jstat.w_status is 0 and when called from 812 * Job_CatchChildren, the status is zeroed if it s/b ignored. 813 * 814 * Results: 815 * None 816 * 817 * Side Effects: 818 * Some nodes may be put on the toBeMade queue. 819 * Final commands for the job are placed on postCommands. 820 * 821 * If we got an error and are aborting (aborting == ABORT_ERROR) and 822 * the job list is now empty, we are done for the day. 823 * If we recognized an error (errors !=0), we set the aborting flag 824 * to ABORT_ERROR so no more jobs will be started. 825 *----------------------------------------------------------------------- 826 */ 827 /*ARGSUSED*/ 828 static void 829 JobFinish(job, status) 830 Job *job; /* job to finish */ 831 int *status; /* sub-why job went away */ 832 { 833 Boolean done; 834 835 if ((WIFEXITED(*status) && 836 (((WEXITSTATUS(*status) != 0) && !(job->flags & JOB_IGNERR)))) || 837 WIFSIGNALED(*status)) 838 { 839 /* 840 * If it exited non-zero and either we're doing things our 841 * way or we're not ignoring errors, the job is finished. 842 * Similarly, if the shell died because of a signal 843 * the job is also finished. In these 844 * cases, finish out the job's output before printing the exit 845 * status... 846 */ 847 #ifdef REMOTE 848 KILL(job->pid, SIGCONT); 849 #endif 850 JobClose(job); 851 if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 852 (void) fclose(job->cmdFILE); 853 job->cmdFILE = NULL; 854 } 855 done = TRUE; 856 #ifdef REMOTE 857 if (job->flags & JOB_REMOTE) 858 Rmt_Done(job->rmtID, job->node); 859 #endif 860 } else if (WIFEXITED(*status)) { 861 /* 862 * Deal with ignored errors in -B mode. We need to print a message 863 * telling of the ignored error as well as setting status.w_status 864 * to 0 so the next command gets run. To do this, we set done to be 865 * TRUE if in -B mode and the job exited non-zero. 866 */ 867 done = WEXITSTATUS(*status) != 0; 868 /* 869 * Old comment said: "Note we don't 870 * want to close down any of the streams until we know we're at the 871 * end." 872 * But we do. Otherwise when are we going to print the rest of the 873 * stuff? 874 */ 875 JobClose(job); 876 #ifdef REMOTE 877 if (job->flags & JOB_REMOTE) 878 Rmt_Done(job->rmtID, job->node); 879 #endif /* REMOTE */ 880 } else { 881 /* 882 * No need to close things down or anything. 883 */ 884 done = FALSE; 885 } 886 887 if (done || 888 WIFSTOPPED(*status) || 889 (WIFSIGNALED(*status) && (WTERMSIG(*status) == SIGCONT))) 890 { 891 FILE *out; 892 893 if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) { 894 /* 895 * If output is going to a file and this job is ignoring 896 * errors, arrange to have the exit status sent to the 897 * output file as well. 898 */ 899 out = fdopen(job->outFd, "w"); 900 if (out == NULL) 901 Punt("Cannot fdopen"); 902 } else { 903 out = stdout; 904 } 905 906 if (WIFEXITED(*status)) { 907 if (DEBUG(JOB)) { 908 (void) fprintf(stdout, "Process %d exited.\n", job->pid); 909 (void) fflush(stdout); 910 } 911 if (WEXITSTATUS(*status) != 0) { 912 if (usePipes && job->node != lastNode) { 913 MESSAGE(out, job->node); 914 lastNode = job->node; 915 } 916 (void) fprintf(out, "*** Error code %d%s\n", 917 WEXITSTATUS(*status), 918 (job->flags & JOB_IGNERR) ? "(ignored)" : ""); 919 920 if (job->flags & JOB_IGNERR) { 921 *status = 0; 922 } 923 } else if (DEBUG(JOB)) { 924 if (usePipes && job->node != lastNode) { 925 MESSAGE(out, job->node); 926 lastNode = job->node; 927 } 928 (void) fprintf(out, "*** Completed successfully\n"); 929 } 930 } else if (WIFSTOPPED(*status) && WSTOPSIG(*status) != SIGCONT) { 931 if (DEBUG(JOB)) { 932 (void) fprintf(stdout, "Process %d stopped.\n", job->pid); 933 (void) fflush(stdout); 934 } 935 if (usePipes && job->node != lastNode) { 936 MESSAGE(out, job->node); 937 lastNode = job->node; 938 } 939 if (!(job->flags & JOB_REMIGRATE)) { 940 switch (WSTOPSIG(*status)) { 941 case SIGTSTP: 942 (void) fprintf(out, "*** Suspended\n"); 943 break; 944 case SIGSTOP: 945 (void) fprintf(out, "*** Stopped\n"); 946 break; 947 default: 948 (void) fprintf(out, "*** Stopped -- signal %d\n", 949 WSTOPSIG(*status)); 950 } 951 } 952 job->flags |= JOB_RESUME; 953 (void)Lst_AtEnd(stoppedJobs, (ClientData)job); 954 #ifdef REMOTE 955 if (job->flags & JOB_REMIGRATE) 956 JobRestart(job); 957 #endif 958 (void) fflush(out); 959 return; 960 } else if (WIFSTOPPED(*status) && WSTOPSIG(*status) == SIGCONT) { 961 /* 962 * If the beastie has continued, shift the Job from the stopped 963 * list to the running one (or re-stop it if concurrency is 964 * exceeded) and go and get another child. 965 */ 966 if (job->flags & (JOB_RESUME|JOB_REMIGRATE|JOB_RESTART)) { 967 if (usePipes && job->node != lastNode) { 968 MESSAGE(out, job->node); 969 lastNode = job->node; 970 } 971 (void) fprintf(out, "*** Continued\n"); 972 } 973 if (!(job->flags & JOB_CONTINUING)) { 974 if (DEBUG(JOB)) { 975 (void) fprintf(stdout, 976 "Warning: process %d was not continuing.\n", 977 job->pid); 978 (void) fflush(stdout); 979 } 980 #ifdef notdef 981 /* 982 * We don't really want to restart a job from scratch just 983 * because it continued, especially not without killing the 984 * continuing process! That's why this is ifdef'ed out. 985 * FD - 9/17/90 986 */ 987 JobRestart(job); 988 #endif 989 } 990 job->flags &= ~JOB_CONTINUING; 991 Lst_AtEnd(jobs, (ClientData)job); 992 nJobs += 1; 993 if (!(job->flags & JOB_REMOTE)) { 994 if (DEBUG(JOB)) { 995 (void) fprintf(stdout, 996 "Process %d is continuing locally.\n", 997 job->pid); 998 (void) fflush(stdout); 999 } 1000 nLocal += 1; 1001 } 1002 (void) fflush(out); 1003 return; 1004 } else { 1005 if (usePipes && job->node != lastNode) { 1006 MESSAGE(out, job->node); 1007 lastNode = job->node; 1008 } 1009 (void) fprintf(out, "*** Signal %d\n", WTERMSIG(*status)); 1010 } 1011 1012 (void) fflush(out); 1013 } 1014 1015 /* 1016 * Now handle the -B-mode stuff. If the beast still isn't finished, 1017 * try and restart the job on the next command. If JobStart says it's 1018 * ok, it's ok. If there's an error, this puppy is done. 1019 */ 1020 if (compatMake && (WIFEXITED(*status) && 1021 !Lst_IsAtEnd(job->node->commands))) { 1022 switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) { 1023 case JOB_RUNNING: 1024 done = FALSE; 1025 break; 1026 case JOB_ERROR: 1027 done = TRUE; 1028 *status = W_EXITCODE(1, 0); 1029 break; 1030 case JOB_FINISHED: 1031 /* 1032 * If we got back a JOB_FINISHED code, JobStart has already 1033 * called Make_Update and freed the job descriptor. We set 1034 * done to false here to avoid fake cycles and double frees. 1035 * JobStart needs to do the update so we can proceed up the 1036 * graph when given the -n flag.. 1037 */ 1038 done = FALSE; 1039 break; 1040 } 1041 } else { 1042 done = TRUE; 1043 } 1044 1045 if (done) { 1046 Trace_Log(JOBEND, job); 1047 if (!compatMake && !(job->flags & JOB_SPECIAL)) { 1048 if ((*status != 0) || 1049 (aborting == ABORT_ERROR) || 1050 (aborting == ABORT_INTERRUPT)) 1051 Job_TokenReturn(); 1052 } 1053 1054 } 1055 1056 if (done && 1057 (aborting != ABORT_ERROR) && 1058 (aborting != ABORT_INTERRUPT) && 1059 (*status == 0)) 1060 { 1061 /* 1062 * As long as we aren't aborting and the job didn't return a non-zero 1063 * status that we shouldn't ignore, we call Make_Update to update 1064 * the parents. In addition, any saved commands for the node are placed 1065 * on the .END target. 1066 */ 1067 if (job->tailCmds != NILLNODE) { 1068 Lst_ForEachFrom(job->node->commands, job->tailCmds, 1069 JobSaveCommand, 1070 (ClientData)job->node); 1071 } 1072 job->node->made = MADE; 1073 if (!(job->flags & JOB_SPECIAL)) 1074 Job_TokenReturn(); 1075 Make_Update(job->node); 1076 free((Address)job); 1077 } else if (*status != 0) { 1078 errors += 1; 1079 free((Address)job); 1080 } 1081 JobRestartJobs(); 1082 1083 /* 1084 * Set aborting if any error. 1085 */ 1086 if (errors && !keepgoing && (aborting != ABORT_INTERRUPT)) { 1087 /* 1088 * If we found any errors in this batch of children and the -k flag 1089 * wasn't given, we set the aborting flag so no more jobs get 1090 * started. 1091 */ 1092 aborting = ABORT_ERROR; 1093 } 1094 1095 if ((aborting == ABORT_ERROR) && Job_Empty()) { 1096 /* 1097 * If we are aborting and the job table is now empty, we finish. 1098 */ 1099 Finish(errors); 1100 } 1101 } 1102 1103 /*- 1104 *----------------------------------------------------------------------- 1105 * Job_Touch -- 1106 * Touch the given target. Called by JobStart when the -t flag was 1107 * given 1108 * 1109 * Results: 1110 * None 1111 * 1112 * Side Effects: 1113 * The data modification of the file is changed. In addition, if the 1114 * file did not exist, it is created. 1115 *----------------------------------------------------------------------- 1116 */ 1117 void 1118 Job_Touch(gn, silent) 1119 GNode *gn; /* the node of the file to touch */ 1120 Boolean silent; /* TRUE if should not print messages */ 1121 { 1122 int streamID; /* ID of stream opened to do the touch */ 1123 struct utimbuf times; /* Times for utime() call */ 1124 1125 if (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC|OP_OPTIONAL|OP_PHONY)) { 1126 /* 1127 * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" targets 1128 * and, as such, shouldn't really be created. 1129 */ 1130 return; 1131 } 1132 1133 if (!silent || NoExecute(gn)) { 1134 (void) fprintf(stdout, "touch %s\n", gn->name); 1135 (void) fflush(stdout); 1136 } 1137 1138 if (NoExecute(gn)) { 1139 return; 1140 } 1141 1142 if (gn->type & OP_ARCHV) { 1143 Arch_Touch(gn); 1144 } else if (gn->type & OP_LIB) { 1145 Arch_TouchLib(gn); 1146 } else { 1147 char *file = gn->path ? gn->path : gn->name; 1148 1149 times.actime = times.modtime = now; 1150 if (utime(file, ×) < 0){ 1151 streamID = open(file, O_RDWR | O_CREAT, 0666); 1152 1153 if (streamID >= 0) { 1154 char c; 1155 1156 /* 1157 * Read and write a byte to the file to change the 1158 * modification time, then close the file. 1159 */ 1160 if (read(streamID, &c, 1) == 1) { 1161 (void) lseek(streamID, (off_t)0, SEEK_SET); 1162 (void) write(streamID, &c, 1); 1163 } 1164 1165 (void) close(streamID); 1166 } else { 1167 (void) fprintf(stdout, "*** couldn't touch %s: %s", 1168 file, strerror(errno)); 1169 (void) fflush(stdout); 1170 } 1171 } 1172 } 1173 } 1174 1175 /*- 1176 *----------------------------------------------------------------------- 1177 * Job_CheckCommands -- 1178 * Make sure the given node has all the commands it needs. 1179 * 1180 * Results: 1181 * TRUE if the commands list is/was ok. 1182 * 1183 * Side Effects: 1184 * The node will have commands from the .DEFAULT rule added to it 1185 * if it needs them. 1186 *----------------------------------------------------------------------- 1187 */ 1188 Boolean 1189 Job_CheckCommands(gn, abortProc) 1190 GNode *gn; /* The target whose commands need 1191 * verifying */ 1192 void (*abortProc) __P((char *, ...)); 1193 /* Function to abort with message */ 1194 { 1195 if (OP_NOP(gn->type) && Lst_IsEmpty(gn->commands) && 1196 (gn->type & OP_LIB) == 0) { 1197 /* 1198 * No commands. Look for .DEFAULT rule from which we might infer 1199 * commands 1200 */ 1201 if ((DEFAULT != NILGNODE) && !Lst_IsEmpty(DEFAULT->commands)) { 1202 char *p1; 1203 /* 1204 * Make only looks for a .DEFAULT if the node was never the 1205 * target of an operator, so that's what we do too. If 1206 * a .DEFAULT was given, we substitute its commands for gn's 1207 * commands and set the IMPSRC variable to be the target's name 1208 * The DEFAULT node acts like a transformation rule, in that 1209 * gn also inherits any attributes or sources attached to 1210 * .DEFAULT itself. 1211 */ 1212 Make_HandleUse(DEFAULT, gn); 1213 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), gn, 0); 1214 if (p1) 1215 free(p1); 1216 } else if (Dir_MTime(gn) == 0) { 1217 /* 1218 * The node wasn't the target of an operator we have no .DEFAULT 1219 * rule to go on and the target doesn't already exist. There's 1220 * nothing more we can do for this branch. If the -k flag wasn't 1221 * given, we stop in our tracks, otherwise we just don't update 1222 * this node's parents so they never get examined. 1223 */ 1224 static const char msg[] = ": don't know how to make"; 1225 1226 if (gn->type & OP_OPTIONAL) { 1227 (void) fprintf(stdout, "%s%s %s(ignored)\n", progname, 1228 msg, gn->name); 1229 (void) fflush(stdout); 1230 } else if (keepgoing) { 1231 (void) fprintf(stdout, "%s%s %s(continuing)\n", progname, 1232 msg, gn->name); 1233 (void) fflush(stdout); 1234 return FALSE; 1235 } else { 1236 (*abortProc)("%s%s %s. Stop", progname, msg, gn->name); 1237 return FALSE; 1238 } 1239 } 1240 } 1241 return TRUE; 1242 } 1243 #ifdef RMT_WILL_WATCH 1244 /*- 1245 *----------------------------------------------------------------------- 1246 * JobLocalInput -- 1247 * Handle a pipe becoming readable. Callback function for Rmt_Watch 1248 * 1249 * Results: 1250 * None 1251 * 1252 * Side Effects: 1253 * JobDoOutput is called. 1254 * 1255 *----------------------------------------------------------------------- 1256 */ 1257 /*ARGSUSED*/ 1258 static void 1259 JobLocalInput(stream, job) 1260 int stream; /* Stream that's ready (ignored) */ 1261 Job *job; /* Job to which the stream belongs */ 1262 { 1263 JobDoOutput(job, FALSE); 1264 } 1265 #endif /* RMT_WILL_WATCH */ 1266 1267 /*- 1268 *----------------------------------------------------------------------- 1269 * JobExec -- 1270 * Execute the shell for the given job. Called from JobStart and 1271 * JobRestart. 1272 * 1273 * Results: 1274 * None. 1275 * 1276 * Side Effects: 1277 * A shell is executed, outputs is altered and the Job structure added 1278 * to the job table. 1279 * 1280 *----------------------------------------------------------------------- 1281 */ 1282 static void 1283 JobExec(job, argv) 1284 Job *job; /* Job to execute */ 1285 char **argv; 1286 { 1287 int cpid; /* ID of new child */ 1288 1289 job->flags &= ~JOB_TRACED; 1290 1291 if (DEBUG(JOB)) { 1292 int i; 1293 1294 (void) fprintf(stdout, "Running %s %sly\n", job->node->name, 1295 job->flags&JOB_REMOTE?"remote":"local"); 1296 (void) fprintf(stdout, "\tCommand: "); 1297 for (i = 0; argv[i] != NULL; i++) { 1298 (void) fprintf(stdout, "%s ", argv[i]); 1299 } 1300 (void) fprintf(stdout, "\n"); 1301 (void) fflush(stdout); 1302 } 1303 1304 /* 1305 * Some jobs produce no output and it's disconcerting to have 1306 * no feedback of their running (since they produce no output, the 1307 * banner with their name in it never appears). This is an attempt to 1308 * provide that feedback, even if nothing follows it. 1309 */ 1310 if ((lastNode != job->node) && (job->flags & JOB_FIRST) && 1311 !(job->flags & JOB_SILENT)) { 1312 MESSAGE(stdout, job->node); 1313 lastNode = job->node; 1314 } 1315 1316 #ifdef RMT_NO_EXEC 1317 if (job->flags & JOB_REMOTE) { 1318 goto jobExecFinish; 1319 } 1320 #endif /* RMT_NO_EXEC */ 1321 1322 if ((cpid = vfork()) == -1) { 1323 Punt("Cannot vfork: %s", strerror(errno)); 1324 } else if (cpid == 0) { 1325 1326 /* 1327 * Must duplicate the input stream down to the child's input and 1328 * reset it to the beginning (again). Since the stream was marked 1329 * close-on-exec, we must clear that bit in the new input. 1330 */ 1331 if (dup2(FILENO(job->cmdFILE), 0) == -1) 1332 Punt("Cannot dup2: %s", strerror(errno)); 1333 (void) fcntl(0, F_SETFD, 0); 1334 (void) lseek(0, (off_t)0, SEEK_SET); 1335 1336 if (job->node->type & OP_MAKE) { 1337 /* 1338 * Pass job token pipe to submakes. 1339 */ 1340 fcntl(job_pipe[0], F_SETFD, 0); 1341 fcntl(job_pipe[1], F_SETFD, 0); 1342 } 1343 1344 if (usePipes) { 1345 /* 1346 * Set up the child's output to be routed through the pipe 1347 * we've created for it. 1348 */ 1349 if (dup2(job->outPipe, 1) == -1) 1350 Punt("Cannot dup2: %s", strerror(errno)); 1351 } else { 1352 /* 1353 * We're capturing output in a file, so we duplicate the 1354 * descriptor to the temporary file into the standard 1355 * output. 1356 */ 1357 if (dup2(job->outFd, 1) == -1) 1358 Punt("Cannot dup2: %s", strerror(errno)); 1359 } 1360 /* 1361 * The output channels are marked close on exec. This bit was 1362 * duplicated by the dup2 (on some systems), so we have to clear 1363 * it before routing the shell's error output to the same place as 1364 * its standard output. 1365 */ 1366 (void) fcntl(1, F_SETFD, 0); 1367 if (dup2(1, 2) == -1) 1368 Punt("Cannot dup2: %s", strerror(errno)); 1369 1370 #ifdef USE_PGRP 1371 /* 1372 * We want to switch the child into a different process family so 1373 * we can kill it and all its descendants in one fell swoop, 1374 * by killing its process family, but not commit suicide. 1375 */ 1376 # if defined(SYSV) 1377 (void) setsid(); 1378 # else 1379 (void) setpgid(0, getpid()); 1380 # endif 1381 #endif /* USE_PGRP */ 1382 1383 #ifdef REMOTE 1384 if (job->flags & JOB_REMOTE) { 1385 Rmt_Exec(shellPath, argv, FALSE); 1386 } else 1387 #endif /* REMOTE */ 1388 { 1389 (void) execv(shellPath, argv); 1390 execError(shellPath); 1391 } 1392 _exit(1); 1393 } else { 1394 #ifdef REMOTE 1395 sigset_t nmask, omask; 1396 sigemptyset(&nmask); 1397 sigaddset(&nmask, SIGCHLD); 1398 sigprocmask(SIG_BLOCK, &nmask, &omask); 1399 #endif 1400 job->pid = cpid; 1401 1402 Trace_Log(JOBSTART, job); 1403 1404 if (usePipes && (job->flags & JOB_FIRST)) { 1405 /* 1406 * The first time a job is run for a node, we set the current 1407 * position in the buffer to the beginning and mark another 1408 * stream to watch in the outputs mask 1409 */ 1410 job->curPos = 0; 1411 1412 #ifdef RMT_WILL_WATCH 1413 Rmt_Watch(job->inPipe, JobLocalInput, job); 1414 #else 1415 #ifdef USE_SELECT 1416 FD_SET(job->inPipe, &outputs); 1417 #else 1418 watchfd(job); 1419 #endif 1420 #endif /* RMT_WILL_WATCH */ 1421 } 1422 1423 if (job->flags & JOB_REMOTE) { 1424 #ifndef REMOTE 1425 job->rmtID = 0; 1426 #else 1427 job->rmtID = Rmt_LastID(job->pid); 1428 #endif /* REMOTE */ 1429 } else { 1430 nLocal += 1; 1431 /* 1432 * XXX: Used to not happen if REMOTE. Why? 1433 */ 1434 if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1435 (void) fclose(job->cmdFILE); 1436 job->cmdFILE = NULL; 1437 } 1438 } 1439 #ifdef REMOTE 1440 sigprocmask(SIG_SETMASK, &omask, NULL); 1441 #endif 1442 } 1443 1444 #ifdef RMT_NO_EXEC 1445 jobExecFinish: 1446 #endif 1447 /* 1448 * Now the job is actually running, add it to the table. 1449 */ 1450 nJobs += 1; 1451 (void) Lst_AtEnd(jobs, (ClientData)job); 1452 } 1453 1454 /*- 1455 *----------------------------------------------------------------------- 1456 * JobMakeArgv -- 1457 * Create the argv needed to execute the shell for a given job. 1458 * 1459 * 1460 * Results: 1461 * 1462 * Side Effects: 1463 * 1464 *----------------------------------------------------------------------- 1465 */ 1466 static void 1467 JobMakeArgv(job, argv) 1468 Job *job; 1469 char **argv; 1470 { 1471 int argc; 1472 static char args[10]; /* For merged arguments */ 1473 1474 argv[0] = shellName; 1475 argc = 1; 1476 1477 if ((commandShell->exit && (*commandShell->exit != '-')) || 1478 (commandShell->echo && (*commandShell->echo != '-'))) 1479 { 1480 /* 1481 * At least one of the flags doesn't have a minus before it, so 1482 * merge them together. Have to do this because the *(&(@*#*&#$# 1483 * Bourne shell thinks its second argument is a file to source. 1484 * Grrrr. Note the ten-character limitation on the combined arguments. 1485 */ 1486 (void)snprintf(args, sizeof(args), "-%s%s", 1487 ((job->flags & JOB_IGNERR) ? "" : 1488 (commandShell->exit ? commandShell->exit : "")), 1489 ((job->flags & JOB_SILENT) ? "" : 1490 (commandShell->echo ? commandShell->echo : ""))); 1491 1492 if (args[1]) { 1493 argv[argc] = args; 1494 argc++; 1495 } 1496 } else { 1497 if (!(job->flags & JOB_IGNERR) && commandShell->exit) { 1498 argv[argc] = commandShell->exit; 1499 argc++; 1500 } 1501 if (!(job->flags & JOB_SILENT) && commandShell->echo) { 1502 argv[argc] = commandShell->echo; 1503 argc++; 1504 } 1505 } 1506 argv[argc] = NULL; 1507 } 1508 1509 /*- 1510 *----------------------------------------------------------------------- 1511 * JobRestart -- 1512 * Restart a job that stopped for some reason. 1513 * 1514 * Results: 1515 * None. 1516 * 1517 *----------------------------------------------------------------------- 1518 */ 1519 static void 1520 JobRestart(job) 1521 Job *job; /* Job to restart */ 1522 { 1523 #ifdef REMOTE 1524 int host; 1525 #endif 1526 1527 if (job->flags & JOB_REMIGRATE) { 1528 if ( 1529 #ifdef REMOTE 1530 verboseRemigrates || 1531 #endif 1532 DEBUG(JOB)) { 1533 (void) fprintf(stdout, "*** remigrating %x(%s)\n", 1534 job->pid, job->node->name); 1535 (void) fflush(stdout); 1536 } 1537 1538 #ifdef REMOTE 1539 if (!Rmt_ReExport(job->pid, job->node, &host)) { 1540 if (verboseRemigrates || DEBUG(JOB)) { 1541 (void) fprintf(stdout, "*** couldn't migrate...\n"); 1542 (void) fflush(stdout); 1543 } 1544 #endif 1545 if (nLocal != maxLocal) { 1546 /* 1547 * Job cannot be remigrated, but there's room on the local 1548 * machine, so resume the job and note that another 1549 * local job has started. 1550 */ 1551 if ( 1552 #ifdef REMOTE 1553 verboseRemigrates || 1554 #endif 1555 DEBUG(JOB)) { 1556 (void) fprintf(stdout, "*** resuming on local machine\n"); 1557 (void) fflush(stdout); 1558 } 1559 KILL(job->pid, SIGCONT); 1560 nLocal +=1; 1561 #ifdef REMOTE 1562 job->flags &= ~(JOB_REMIGRATE|JOB_RESUME|JOB_REMOTE); 1563 job->flags |= JOB_CONTINUING; 1564 #else 1565 job->flags &= ~(JOB_REMIGRATE|JOB_RESUME); 1566 #endif 1567 } else { 1568 /* 1569 * Job cannot be restarted. Mark the table as full and 1570 * place the job back on the list of stopped jobs. 1571 */ 1572 if ( 1573 #ifdef REMOTE 1574 verboseRemigrates || 1575 #endif 1576 DEBUG(JOB)) { 1577 (void) fprintf(stdout, "*** holding\n"); 1578 (void) fflush(stdout); 1579 } 1580 (void)Lst_AtFront(stoppedJobs, (ClientData)job); 1581 return; 1582 } 1583 #ifdef REMOTE 1584 } else { 1585 /* 1586 * Clear out the remigrate and resume flags. Set the continuing 1587 * flag so we know later on that the process isn't exiting just 1588 * because of a signal. 1589 */ 1590 job->flags &= ~(JOB_REMIGRATE|JOB_RESUME); 1591 job->flags |= JOB_CONTINUING; 1592 job->rmtID = host; 1593 } 1594 #endif 1595 1596 (void)Lst_AtEnd(jobs, (ClientData)job); 1597 nJobs += 1; 1598 } else if (job->flags & JOB_RESTART) { 1599 /* 1600 * Set up the control arguments to the shell. This is based on the 1601 * flags set earlier for this job. If the JOB_IGNERR flag is clear, 1602 * the 'exit' flag of the commandShell is used to cause it to exit 1603 * upon receiving an error. If the JOB_SILENT flag is clear, the 1604 * 'echo' flag of the commandShell is used to get it to start echoing 1605 * as soon as it starts processing commands. 1606 */ 1607 char *argv[10]; 1608 1609 JobMakeArgv(job, argv); 1610 1611 if (DEBUG(JOB)) { 1612 (void) fprintf(stdout, "Restarting %s...", job->node->name); 1613 (void) fflush(stdout); 1614 } 1615 #ifdef REMOTE 1616 if ((job->node->type&OP_NOEXPORT) || 1617 (nLocal < maxLocal && runLocalFirst) 1618 # ifdef RMT_NO_EXEC 1619 || !Rmt_Export(shellPath, argv, job) 1620 # else 1621 || !Rmt_Begin(shellPath, argv, job->node) 1622 # endif 1623 #endif 1624 { 1625 if (((nLocal >= maxLocal) && !(job->flags & JOB_SPECIAL))) { 1626 /* 1627 * Can't be exported and not allowed to run locally -- put it 1628 * back on the hold queue and mark the table full 1629 */ 1630 if (DEBUG(JOB)) { 1631 (void) fprintf(stdout, "holding\n"); 1632 (void) fflush(stdout); 1633 } 1634 (void)Lst_AtFront(stoppedJobs, (ClientData)job); 1635 return; 1636 } else { 1637 /* 1638 * Job may be run locally. 1639 */ 1640 if (DEBUG(JOB)) { 1641 (void) fprintf(stdout, "running locally\n"); 1642 (void) fflush(stdout); 1643 } 1644 job->flags &= ~JOB_REMOTE; 1645 } 1646 } 1647 #ifdef REMOTE 1648 else { 1649 /* 1650 * Can be exported. Hooray! 1651 */ 1652 if (DEBUG(JOB)) { 1653 (void) fprintf(stdout, "exporting\n"); 1654 (void) fflush(stdout); 1655 } 1656 job->flags |= JOB_REMOTE; 1657 } 1658 #endif 1659 JobExec(job, argv); 1660 } else { 1661 /* 1662 * The job has stopped and needs to be restarted. Why it stopped, 1663 * we don't know... 1664 */ 1665 if (DEBUG(JOB)) { 1666 (void) fprintf(stdout, "Resuming %s...", job->node->name); 1667 (void) fflush(stdout); 1668 } 1669 if (((job->flags & JOB_REMOTE) || 1670 (nLocal < maxLocal) || 1671 #ifdef REMOTE 1672 (((job->flags & JOB_SPECIAL) && 1673 (job->node->type & OP_NOEXPORT)) && 1674 (maxLocal == 0))) && 1675 #else 1676 ((job->flags & JOB_SPECIAL) && 1677 (maxLocal == 0))) && 1678 #endif 1679 (nJobs != maxJobs)) 1680 { 1681 /* 1682 * If the job is remote, it's ok to resume it as long as the 1683 * maximum concurrency won't be exceeded. If it's local and 1684 * we haven't reached the local concurrency limit already (or the 1685 * job must be run locally and maxLocal is 0), it's also ok to 1686 * resume it. 1687 */ 1688 Boolean error; 1689 int status; 1690 1691 #ifdef RMT_WANTS_SIGNALS 1692 if (job->flags & JOB_REMOTE) { 1693 error = !Rmt_Signal(job, SIGCONT); 1694 } else 1695 #endif /* RMT_WANTS_SIGNALS */ 1696 error = (KILL(job->pid, SIGCONT) != 0); 1697 1698 if (!error) { 1699 /* 1700 * Make sure the user knows we've continued the beast and 1701 * actually put the thing in the job table. 1702 */ 1703 job->flags |= JOB_CONTINUING; 1704 status = W_STOPCODE(SIGCONT); 1705 JobFinish(job, &status); 1706 1707 job->flags &= ~(JOB_RESUME|JOB_CONTINUING); 1708 if (DEBUG(JOB)) { 1709 (void) fprintf(stdout, "done\n"); 1710 (void) fflush(stdout); 1711 } 1712 } else { 1713 Error("couldn't resume %s: %s", 1714 job->node->name, strerror(errno)); 1715 status = W_EXITCODE(1, 0); 1716 JobFinish(job, &status); 1717 } 1718 } else { 1719 /* 1720 * Job cannot be restarted. Mark the table as full and 1721 * place the job back on the list of stopped jobs. 1722 */ 1723 if (DEBUG(JOB)) { 1724 (void) fprintf(stdout, "table full\n"); 1725 (void) fflush(stdout); 1726 } 1727 (void) Lst_AtFront(stoppedJobs, (ClientData)job); 1728 } 1729 } 1730 } 1731 1732 /*- 1733 *----------------------------------------------------------------------- 1734 * JobStart -- 1735 * Start a target-creation process going for the target described 1736 * by the graph node gn. 1737 * 1738 * Results: 1739 * JOB_ERROR if there was an error in the commands, JOB_FINISHED 1740 * if there isn't actually anything left to do for the job and 1741 * JOB_RUNNING if the job has been started. 1742 * 1743 * Side Effects: 1744 * A new Job node is created and added to the list of running 1745 * jobs. PMake is forked and a child shell created. 1746 *----------------------------------------------------------------------- 1747 */ 1748 static int 1749 JobStart(gn, flags, previous) 1750 GNode *gn; /* target to create */ 1751 int flags; /* flags for the job to override normal ones. 1752 * e.g. JOB_SPECIAL or JOB_IGNDOTS */ 1753 Job *previous; /* The previous Job structure for this node, 1754 * if any. */ 1755 { 1756 register Job *job; /* new job descriptor */ 1757 char *argv[10]; /* Argument vector to shell */ 1758 Boolean cmdsOK; /* true if the nodes commands were all right */ 1759 Boolean local; /* Set true if the job was run locally */ 1760 Boolean noExec; /* Set true if we decide not to run the job */ 1761 int tfd; /* File descriptor to the temp file */ 1762 1763 if (previous != NULL) { 1764 previous->flags &= ~(JOB_FIRST|JOB_IGNERR|JOB_SILENT|JOB_REMOTE); 1765 job = previous; 1766 } else { 1767 job = (Job *) emalloc(sizeof(Job)); 1768 if (job == NULL) { 1769 Punt("JobStart out of memory"); 1770 } 1771 flags |= JOB_FIRST; 1772 } 1773 1774 job->node = gn; 1775 job->tailCmds = NILLNODE; 1776 1777 /* 1778 * Set the initial value of the flags for this job based on the global 1779 * ones and the node's attributes... Any flags supplied by the caller 1780 * are also added to the field. 1781 */ 1782 job->flags = 0; 1783 if (Targ_Ignore(gn)) { 1784 job->flags |= JOB_IGNERR; 1785 } 1786 if (Targ_Silent(gn)) { 1787 job->flags |= JOB_SILENT; 1788 } 1789 job->flags |= flags; 1790 1791 /* 1792 * Check the commands now so any attributes from .DEFAULT have a chance 1793 * to migrate to the node 1794 */ 1795 if (!compatMake && job->flags & JOB_FIRST) { 1796 cmdsOK = Job_CheckCommands(gn, Error); 1797 } else { 1798 cmdsOK = TRUE; 1799 } 1800 1801 #ifndef RMT_WILL_WATCH 1802 #ifndef USE_SELECT 1803 job->inPollfd = NULL; 1804 #endif 1805 #endif 1806 /* 1807 * If the -n flag wasn't given, we open up OUR (not the child's) 1808 * temporary file to stuff commands in it. The thing is rd/wr so we don't 1809 * need to reopen it to feed it to the shell. If the -n flag *was* given, 1810 * we just set the file to be stdout. Cute, huh? 1811 */ 1812 if (((gn->type & OP_MAKE) && !(noRecursiveExecute)) || 1813 (!noExecute && !touchFlag)) { 1814 /* 1815 * tfile is the name of a file into which all shell commands are 1816 * put. It is used over by removing it before the child shell is 1817 * executed. The XXXXXX in the string are replaced by the pid of 1818 * the make process in a 6-character field with leading zeroes. 1819 */ 1820 char tfile[sizeof(TMPPAT)]; 1821 /* 1822 * We're serious here, but if the commands were bogus, we're 1823 * also dead... 1824 */ 1825 if (!cmdsOK) { 1826 DieHorribly(); 1827 } 1828 1829 (void)strcpy(tfile, TMPPAT); 1830 if ((tfd = mkstemp(tfile)) == -1) 1831 Punt("Could not create temporary file %s", strerror(errno)); 1832 (void) eunlink(tfile); 1833 1834 job->cmdFILE = fdopen(tfd, "w+"); 1835 if (job->cmdFILE == NULL) { 1836 Punt("Could not fdopen %s", tfile); 1837 } 1838 (void) fcntl(FILENO(job->cmdFILE), F_SETFD, 1); 1839 /* 1840 * Send the commands to the command file, flush all its buffers then 1841 * rewind and remove the thing. 1842 */ 1843 noExec = FALSE; 1844 1845 /* 1846 * used to be backwards; replace when start doing multiple commands 1847 * per shell. 1848 */ 1849 if (compatMake) { 1850 /* 1851 * Be compatible: If this is the first time for this node, 1852 * verify its commands are ok and open the commands list for 1853 * sequential access by later invocations of JobStart. 1854 * Once that is done, we take the next command off the list 1855 * and print it to the command file. If the command was an 1856 * ellipsis, note that there's nothing more to execute. 1857 */ 1858 if ((job->flags&JOB_FIRST) && (Lst_Open(gn->commands) != SUCCESS)){ 1859 cmdsOK = FALSE; 1860 } else { 1861 LstNode ln = Lst_Next(gn->commands); 1862 1863 if ((ln == NILLNODE) || 1864 JobPrintCommand((ClientData) Lst_Datum(ln), 1865 (ClientData) job)) 1866 { 1867 noExec = TRUE; 1868 Lst_Close(gn->commands); 1869 } 1870 if (noExec && !(job->flags & JOB_FIRST)) { 1871 /* 1872 * If we're not going to execute anything, the job 1873 * is done and we need to close down the various 1874 * file descriptors we've opened for output, then 1875 * call JobDoOutput to catch the final characters or 1876 * send the file to the screen... Note that the i/o streams 1877 * are only open if this isn't the first job. 1878 * Note also that this could not be done in 1879 * Job_CatchChildren b/c it wasn't clear if there were 1880 * more commands to execute or not... 1881 */ 1882 JobClose(job); 1883 } 1884 } 1885 } else { 1886 /* 1887 * We can do all the commands at once. hooray for sanity 1888 */ 1889 numCommands = 0; 1890 Lst_ForEach(gn->commands, JobPrintCommand, (ClientData)job); 1891 1892 /* 1893 * If we didn't print out any commands to the shell script, 1894 * there's not much point in executing the shell, is there? 1895 */ 1896 if (numCommands == 0) { 1897 noExec = TRUE; 1898 } 1899 } 1900 } else if (NoExecute(gn)) { 1901 /* 1902 * Not executing anything -- just print all the commands to stdout 1903 * in one fell swoop. This will still set up job->tailCmds correctly. 1904 */ 1905 if (lastNode != gn) { 1906 MESSAGE(stdout, gn); 1907 lastNode = gn; 1908 } 1909 job->cmdFILE = stdout; 1910 /* 1911 * Only print the commands if they're ok, but don't die if they're 1912 * not -- just let the user know they're bad and keep going. It 1913 * doesn't do any harm in this case and may do some good. 1914 */ 1915 if (cmdsOK) { 1916 Lst_ForEach(gn->commands, JobPrintCommand, (ClientData)job); 1917 } 1918 /* 1919 * Don't execute the shell, thank you. 1920 */ 1921 noExec = TRUE; 1922 } else { 1923 /* 1924 * Just touch the target and note that no shell should be executed. 1925 * Set cmdFILE to stdout to make life easier. Check the commands, too, 1926 * but don't die if they're no good -- it does no harm to keep working 1927 * up the graph. 1928 */ 1929 job->cmdFILE = stdout; 1930 Job_Touch(gn, job->flags&JOB_SILENT); 1931 noExec = TRUE; 1932 } 1933 1934 /* 1935 * If we're not supposed to execute a shell, don't. 1936 */ 1937 if (noExec) { 1938 /* 1939 * Unlink and close the command file if we opened one 1940 */ 1941 if (job->cmdFILE != stdout) { 1942 if (job->cmdFILE != NULL) { 1943 (void) fclose(job->cmdFILE); 1944 job->cmdFILE = NULL; 1945 } 1946 } else { 1947 (void) fflush(stdout); 1948 } 1949 1950 /* 1951 * We only want to work our way up the graph if we aren't here because 1952 * the commands for the job were no good. 1953 */ 1954 if (cmdsOK) { 1955 if (aborting == 0) { 1956 if (job->tailCmds != NILLNODE) { 1957 Lst_ForEachFrom(job->node->commands, job->tailCmds, 1958 JobSaveCommand, 1959 (ClientData)job->node); 1960 } 1961 if (!(job->flags & JOB_SPECIAL)) 1962 Job_TokenReturn(); 1963 Make_Update(job->node); 1964 } 1965 free((Address)job); 1966 return(JOB_FINISHED); 1967 } else { 1968 free((Address)job); 1969 return(JOB_ERROR); 1970 } 1971 } else { 1972 (void) fflush(job->cmdFILE); 1973 } 1974 1975 /* 1976 * Set up the control arguments to the shell. This is based on the flags 1977 * set earlier for this job. 1978 */ 1979 JobMakeArgv(job, argv); 1980 1981 /* 1982 * If we're using pipes to catch output, create the pipe by which we'll 1983 * get the shell's output. If we're using files, print out that we're 1984 * starting a job and then set up its temporary-file name. 1985 */ 1986 if (!compatMake || (job->flags & JOB_FIRST)) { 1987 if (usePipes) { 1988 int fd[2]; 1989 if (pipe(fd) == -1) 1990 Punt("Cannot create pipe: %s", strerror(errno)); 1991 job->inPipe = fd[0]; 1992 #ifdef USE_SELECT 1993 if (job->inPipe >= FD_SETSIZE) 1994 Punt("Ran out of fd_set slots; " 1995 "recompile with a larger FD_SETSIZE."); 1996 #endif 1997 job->outPipe = fd[1]; 1998 (void) fcntl(job->inPipe, F_SETFD, 1); 1999 (void) fcntl(job->outPipe, F_SETFD, 1); 2000 } else { 2001 (void) fprintf(stdout, "Remaking `%s'\n", gn->name); 2002 (void) fflush(stdout); 2003 (void) strcpy(job->outFile, TMPPAT); 2004 job->outFd = mkstemp(job->outFile); 2005 (void) fcntl(job->outFd, F_SETFD, 1); 2006 } 2007 } 2008 2009 #ifdef REMOTE 2010 if (!(gn->type & OP_NOEXPORT) && !(runLocalFirst && nLocal < maxLocal)) { 2011 #ifdef RMT_NO_EXEC 2012 local = !Rmt_Export(shellPath, argv, job); 2013 #else 2014 local = !Rmt_Begin(shellPath, argv, job->node); 2015 #endif /* RMT_NO_EXEC */ 2016 if (!local) { 2017 job->flags |= JOB_REMOTE; 2018 } 2019 } else 2020 #endif 2021 local = TRUE; 2022 2023 if (local && (((nLocal >= maxLocal) && 2024 !(job->flags & JOB_SPECIAL) && 2025 #ifdef REMOTE 2026 (!(gn->type & OP_NOEXPORT) || (maxLocal != 0)) 2027 #else 2028 (maxLocal != 0) 2029 #endif 2030 ))) 2031 { 2032 /* 2033 * The job can only be run locally, but we've hit the limit of 2034 * local concurrency, so put the job on hold until some other job 2035 * finishes. Note that the special jobs (.BEGIN, .INTERRUPT and .END) 2036 * may be run locally even when the local limit has been reached 2037 * (e.g. when maxLocal == 0), though they will be exported if at 2038 * all possible. In addition, any target marked with .NOEXPORT will 2039 * be run locally if maxLocal is 0. 2040 */ 2041 job->flags |= JOB_RESTART; 2042 (void) Lst_AtEnd(stoppedJobs, (ClientData)job); 2043 } else { 2044 JobExec(job, argv); 2045 } 2046 return(JOB_RUNNING); 2047 } 2048 2049 static char * 2050 JobOutput(job, cp, endp, msg) 2051 register Job *job; 2052 register char *cp, *endp; 2053 int msg; 2054 { 2055 register char *ecp; 2056 2057 if (commandShell->noPrint) { 2058 ecp = Str_FindSubstring(cp, commandShell->noPrint); 2059 while (ecp != NULL) { 2060 if (cp != ecp) { 2061 *ecp = '\0'; 2062 if (msg && job->node != lastNode) { 2063 MESSAGE(stdout, job->node); 2064 lastNode = job->node; 2065 } 2066 /* 2067 * The only way there wouldn't be a newline after 2068 * this line is if it were the last in the buffer. 2069 * however, since the non-printable comes after it, 2070 * there must be a newline, so we don't print one. 2071 */ 2072 (void) fprintf(stdout, "%s", cp); 2073 (void) fflush(stdout); 2074 } 2075 cp = ecp + commandShell->noPLen; 2076 if (cp != endp) { 2077 /* 2078 * Still more to print, look again after skipping 2079 * the whitespace following the non-printable 2080 * command.... 2081 */ 2082 cp++; 2083 while (*cp == ' ' || *cp == '\t' || *cp == '\n') { 2084 cp++; 2085 } 2086 ecp = Str_FindSubstring(cp, commandShell->noPrint); 2087 } else { 2088 return cp; 2089 } 2090 } 2091 } 2092 return cp; 2093 } 2094 2095 /*- 2096 *----------------------------------------------------------------------- 2097 * JobDoOutput -- 2098 * This function is called at different times depending on 2099 * whether the user has specified that output is to be collected 2100 * via pipes or temporary files. In the former case, we are called 2101 * whenever there is something to read on the pipe. We collect more 2102 * output from the given job and store it in the job's outBuf. If 2103 * this makes up a line, we print it tagged by the job's identifier, 2104 * as necessary. 2105 * If output has been collected in a temporary file, we open the 2106 * file and read it line by line, transfering it to our own 2107 * output channel until the file is empty. At which point we 2108 * remove the temporary file. 2109 * In both cases, however, we keep our figurative eye out for the 2110 * 'noPrint' line for the shell from which the output came. If 2111 * we recognize a line, we don't print it. If the command is not 2112 * alone on the line (the character after it is not \0 or \n), we 2113 * do print whatever follows it. 2114 * 2115 * Results: 2116 * None 2117 * 2118 * Side Effects: 2119 * curPos may be shifted as may the contents of outBuf. 2120 *----------------------------------------------------------------------- 2121 */ 2122 STATIC void 2123 JobDoOutput(job, finish) 2124 register Job *job; /* the job whose output needs printing */ 2125 Boolean finish; /* TRUE if this is the last time we'll be 2126 * called for this job */ 2127 { 2128 Boolean gotNL = FALSE; /* true if got a newline */ 2129 Boolean fbuf; /* true if our buffer filled up */ 2130 register int nr; /* number of bytes read */ 2131 register int i; /* auxiliary index into outBuf */ 2132 register int max; /* limit for i (end of current data) */ 2133 int nRead; /* (Temporary) number of bytes read */ 2134 2135 FILE *oFILE; /* Stream pointer to shell's output file */ 2136 char inLine[132]; 2137 2138 2139 if (usePipes) { 2140 /* 2141 * Read as many bytes as will fit in the buffer. 2142 */ 2143 end_loop: 2144 gotNL = FALSE; 2145 fbuf = FALSE; 2146 2147 nRead = read(job->inPipe, &job->outBuf[job->curPos], 2148 JOB_BUFSIZE - job->curPos); 2149 if (nRead < 0) { 2150 if (DEBUG(JOB)) { 2151 perror("JobDoOutput(piperead)"); 2152 } 2153 nr = 0; 2154 } else { 2155 nr = nRead; 2156 } 2157 2158 /* 2159 * If we hit the end-of-file (the job is dead), we must flush its 2160 * remaining output, so pretend we read a newline if there's any 2161 * output remaining in the buffer. 2162 * Also clear the 'finish' flag so we stop looping. 2163 */ 2164 if ((nr == 0) && (job->curPos != 0)) { 2165 job->outBuf[job->curPos] = '\n'; 2166 nr = 1; 2167 finish = FALSE; 2168 } else if (nr == 0) { 2169 finish = FALSE; 2170 } 2171 2172 /* 2173 * Look for the last newline in the bytes we just got. If there is 2174 * one, break out of the loop with 'i' as its index and gotNL set 2175 * TRUE. 2176 */ 2177 max = job->curPos + nr; 2178 for (i = job->curPos + nr - 1; i >= job->curPos; i--) { 2179 if (job->outBuf[i] == '\n') { 2180 gotNL = TRUE; 2181 break; 2182 } else if (job->outBuf[i] == '\0') { 2183 /* 2184 * Why? 2185 */ 2186 job->outBuf[i] = ' '; 2187 } 2188 } 2189 2190 if (!gotNL) { 2191 job->curPos += nr; 2192 if (job->curPos == JOB_BUFSIZE) { 2193 /* 2194 * If we've run out of buffer space, we have no choice 2195 * but to print the stuff. sigh. 2196 */ 2197 fbuf = TRUE; 2198 i = job->curPos; 2199 } 2200 } 2201 if (gotNL || fbuf) { 2202 /* 2203 * Need to send the output to the screen. Null terminate it 2204 * first, overwriting the newline character if there was one. 2205 * So long as the line isn't one we should filter (according 2206 * to the shell description), we print the line, preceeded 2207 * by a target banner if this target isn't the same as the 2208 * one for which we last printed something. 2209 * The rest of the data in the buffer are then shifted down 2210 * to the start of the buffer and curPos is set accordingly. 2211 */ 2212 job->outBuf[i] = '\0'; 2213 if (i >= job->curPos) { 2214 char *cp; 2215 2216 cp = JobOutput(job, job->outBuf, &job->outBuf[i], FALSE); 2217 2218 /* 2219 * There's still more in that thar buffer. This time, though, 2220 * we know there's no newline at the end, so we add one of 2221 * our own free will. 2222 */ 2223 if (*cp != '\0') { 2224 if (job->node != lastNode) { 2225 MESSAGE(stdout, job->node); 2226 lastNode = job->node; 2227 } 2228 (void) fprintf(stdout, "%s%s", cp, gotNL ? "\n" : ""); 2229 (void) fflush(stdout); 2230 } 2231 } 2232 if (i < max - 1) { 2233 /* shift the remaining characters down */ 2234 (void) memcpy(job->outBuf, &job->outBuf[i + 1], max - (i + 1)); 2235 job->curPos = max - (i + 1); 2236 2237 } else { 2238 /* 2239 * We have written everything out, so we just start over 2240 * from the start of the buffer. No copying. No nothing. 2241 */ 2242 job->curPos = 0; 2243 } 2244 } 2245 if (finish) { 2246 /* 2247 * If the finish flag is true, we must loop until we hit 2248 * end-of-file on the pipe. This is guaranteed to happen 2249 * eventually since the other end of the pipe is now closed 2250 * (we closed it explicitly and the child has exited). When 2251 * we do get an EOF, finish will be set FALSE and we'll fall 2252 * through and out. 2253 */ 2254 goto end_loop; 2255 } 2256 } else { 2257 /* 2258 * We've been called to retrieve the output of the job from the 2259 * temporary file where it's been squirreled away. This consists of 2260 * opening the file, reading the output line by line, being sure not 2261 * to print the noPrint line for the shell we used, then close and 2262 * remove the temporary file. Very simple. 2263 * 2264 * Change to read in blocks and do FindSubString type things as for 2265 * pipes? That would allow for "@echo -n..." 2266 */ 2267 oFILE = fopen(job->outFile, "r"); 2268 if (oFILE != NULL) { 2269 (void) fprintf(stdout, "Results of making %s:\n", job->node->name); 2270 (void) fflush(stdout); 2271 while (fgets(inLine, sizeof(inLine), oFILE) != NULL) { 2272 register char *cp, *endp, *oendp; 2273 2274 cp = inLine; 2275 oendp = endp = inLine + strlen(inLine); 2276 if (endp[-1] == '\n') { 2277 *--endp = '\0'; 2278 } 2279 cp = JobOutput(job, inLine, endp, FALSE); 2280 2281 /* 2282 * There's still more in that thar buffer. This time, though, 2283 * we know there's no newline at the end, so we add one of 2284 * our own free will. 2285 */ 2286 (void) fprintf(stdout, "%s", cp); 2287 (void) fflush(stdout); 2288 if (endp != oendp) { 2289 (void) fprintf(stdout, "\n"); 2290 (void) fflush(stdout); 2291 } 2292 } 2293 (void) fclose(oFILE); 2294 (void) eunlink(job->outFile); 2295 } else { 2296 Punt("Cannot open `%s'", job->outFile); 2297 } 2298 } 2299 } 2300 2301 /*- 2302 *----------------------------------------------------------------------- 2303 * Job_CatchChildren -- 2304 * Handle the exit of a child. Called from Make_Make. 2305 * 2306 * Results: 2307 * none. 2308 * 2309 * Side Effects: 2310 * The job descriptor is removed from the list of children. 2311 * 2312 * Notes: 2313 * We do waits, blocking or not, according to the wisdom of our 2314 * caller, until there are no more children to report. For each 2315 * job, call JobFinish to finish things off. This will take care of 2316 * putting jobs on the stoppedJobs queue. 2317 * 2318 *----------------------------------------------------------------------- 2319 */ 2320 void 2321 Job_CatchChildren(block) 2322 Boolean block; /* TRUE if should block on the wait. */ 2323 { 2324 int pid; /* pid of dead child */ 2325 register Job *job; /* job descriptor for dead child */ 2326 LstNode jnode; /* list element for finding job */ 2327 int status; /* Exit/termination status */ 2328 2329 /* 2330 * Don't even bother if we know there's no one around. 2331 */ 2332 if (nLocal == 0) { 2333 return; 2334 } 2335 2336 while ((pid = waitpid((pid_t) -1, &status, 2337 (block?0:WNOHANG)|WUNTRACED)) > 0) 2338 { 2339 if (DEBUG(JOB)) { 2340 (void) fprintf(stdout, "Process %d exited or stopped %x.\n", pid, 2341 status); 2342 (void) fflush(stdout); 2343 } 2344 2345 2346 jnode = Lst_Find(jobs, (ClientData)&pid, JobCmpPid); 2347 2348 if (jnode == NILLNODE) { 2349 if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGCONT)) { 2350 jnode = Lst_Find(stoppedJobs, (ClientData) &pid, JobCmpPid); 2351 if (jnode == NILLNODE) { 2352 Error("Resumed child (%d) not in table", pid); 2353 continue; 2354 } 2355 job = (Job *)Lst_Datum(jnode); 2356 (void) Lst_Remove(stoppedJobs, jnode); 2357 } else { 2358 Error("Child (%d) not in table?", pid); 2359 continue; 2360 } 2361 } else { 2362 job = (Job *) Lst_Datum(jnode); 2363 (void) Lst_Remove(jobs, jnode); 2364 nJobs -= 1; 2365 #ifdef REMOTE 2366 if (!(job->flags & JOB_REMOTE)) { 2367 if (DEBUG(JOB)) { 2368 (void) fprintf(stdout, 2369 "Job queue has one fewer local process.\n"); 2370 (void) fflush(stdout); 2371 } 2372 nLocal -= 1; 2373 } 2374 #else 2375 nLocal -= 1; 2376 #endif 2377 } 2378 2379 JobFinish(job, &status); 2380 } 2381 } 2382 2383 /*- 2384 *----------------------------------------------------------------------- 2385 * Job_CatchOutput -- 2386 * Catch the output from our children, if we're using 2387 * pipes do so. Otherwise just block time until we get a 2388 * signal (most likely a SIGCHLD) since there's no point in 2389 * just spinning when there's nothing to do and the reaping 2390 * of a child can wait for a while. 2391 * 2392 * Results: 2393 * None 2394 * 2395 * Side Effects: 2396 * Output is read from pipes if we're piping. 2397 * ----------------------------------------------------------------------- 2398 */ 2399 void 2400 Job_CatchOutput() 2401 { 2402 int nready; 2403 register LstNode ln; 2404 register Job *job; 2405 #ifdef RMT_WILL_WATCH 2406 int pnJobs; /* Previous nJobs */ 2407 #endif 2408 2409 (void) fflush(stdout); 2410 Job_TokenFlush(); 2411 #ifdef RMT_WILL_WATCH 2412 pnJobs = nJobs; 2413 2414 /* 2415 * It is possible for us to be called with nJobs equal to 0. This happens 2416 * if all the jobs finish and a job that is stopped cannot be run 2417 * locally (eg if maxLocal is 0) and cannot be exported. The job will 2418 * be placed back on the stoppedJobs queue, Job_Empty() will return false, 2419 * Make_Run will call us again when there's nothing for which to wait. 2420 * nJobs never changes, so we loop forever. Hence the check. It could 2421 * be argued that we should sleep for a bit so as not to swamp the 2422 * exportation system with requests. Perhaps we should. 2423 * 2424 * NOTE: IT IS THE RESPONSIBILITY OF Rmt_Wait TO CALL Job_CatchChildren 2425 * IN A TIMELY FASHION TO CATCH ANY LOCALLY RUNNING JOBS THAT EXIT. 2426 * It may use the variable nLocal to determine if it needs to call 2427 * Job_CatchChildren (if nLocal is 0, there's nothing for which to 2428 * wait...) 2429 */ 2430 while (nJobs != 0 && pnJobs == nJobs) { 2431 Rmt_Wait(); 2432 } 2433 #else 2434 if (usePipes) { 2435 #ifdef USE_SELECT 2436 struct timeval timeout; 2437 fd_set readfds; 2438 2439 readfds = outputs; 2440 timeout.tv_sec = SEL_SEC; 2441 timeout.tv_usec = SEL_USEC; 2442 2443 if ((nready = select(FD_SETSIZE, &readfds, (fd_set *) 0, 2444 (fd_set *) 0, &timeout)) <= 0) 2445 return; 2446 #else 2447 if ((nready = poll((wantToken ? fds : (fds + 1)), 2448 (wantToken ? nfds : (nfds - 1)), POLL_MSEC)) <= 0) 2449 return; 2450 #endif 2451 else { 2452 if (Lst_Open(jobs) == FAILURE) { 2453 Punt("Cannot open job table"); 2454 } 2455 while (nready && (ln = Lst_Next(jobs)) != NILLNODE) { 2456 job = (Job *) Lst_Datum(ln); 2457 #ifdef USE_SELECT 2458 if (FD_ISSET(job->inPipe, &readfds)) 2459 #else 2460 if (readyfd(job)) 2461 #endif 2462 { 2463 JobDoOutput(job, FALSE); 2464 nready -= 1; 2465 } 2466 2467 } 2468 Lst_Close(jobs); 2469 } 2470 } 2471 #endif /* RMT_WILL_WATCH */ 2472 } 2473 2474 /*- 2475 *----------------------------------------------------------------------- 2476 * Job_Make -- 2477 * Start the creation of a target. Basically a front-end for 2478 * JobStart used by the Make module. 2479 * 2480 * Results: 2481 * None. 2482 * 2483 * Side Effects: 2484 * Another job is started. 2485 * 2486 *----------------------------------------------------------------------- 2487 */ 2488 void 2489 Job_Make(gn) 2490 GNode *gn; 2491 { 2492 (void) JobStart(gn, 0, NULL); 2493 } 2494 2495 /*- 2496 *----------------------------------------------------------------------- 2497 * Job_Init -- 2498 * Initialize the process module 2499 * 2500 * Results: 2501 * none 2502 * 2503 * Side Effects: 2504 * lists and counters are initialized 2505 *----------------------------------------------------------------------- 2506 */ 2507 void 2508 Job_Init(maxproc, maxlocal) 2509 int maxproc; /* the greatest number of jobs which may be 2510 * running at one time */ 2511 int maxlocal; /* the greatest number of local jobs which may 2512 * be running at once. */ 2513 { 2514 GNode *begin; /* node for commands to do at the very start */ 2515 2516 jobs = Lst_Init(FALSE); 2517 stoppedJobs = Lst_Init(FALSE); 2518 maxJobs = maxproc; 2519 maxLocal = maxlocal; 2520 nJobs = 0; 2521 nLocal = 0; 2522 wantToken = FALSE; 2523 2524 aborting = 0; 2525 errors = 0; 2526 2527 lastNode = NILGNODE; 2528 2529 if (maxJobs == 1 2530 #ifdef REMOTE 2531 || noMessages 2532 #endif 2533 ) { 2534 /* 2535 * If only one job can run at a time, there's no need for a banner, 2536 * is there? 2537 */ 2538 targFmt = ""; 2539 } else { 2540 targFmt = TARG_FMT; 2541 } 2542 2543 if (shellPath == NULL) { 2544 /* 2545 * The user didn't specify a shell to use, so we are using the 2546 * default one... Both the absolute path and the last component 2547 * must be set. The last component is taken from the 'name' field 2548 * of the default shell description pointed-to by commandShell. 2549 * All default shells are located in _PATH_DEFSHELLDIR. 2550 */ 2551 shellName = commandShell->name; 2552 shellPath = str_concat(_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH); 2553 } 2554 2555 if (commandShell->exit == NULL) { 2556 commandShell->exit = ""; 2557 } 2558 if (commandShell->echo == NULL) { 2559 commandShell->echo = ""; 2560 } 2561 2562 /* 2563 * Catch the four signals that POSIX specifies if they aren't ignored. 2564 * JobPassSig will take care of calling JobInterrupt if appropriate. 2565 */ 2566 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 2567 (void) signal(SIGINT, JobPassSig); 2568 } 2569 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 2570 (void) signal(SIGHUP, JobPassSig); 2571 } 2572 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 2573 (void) signal(SIGQUIT, JobPassSig); 2574 } 2575 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 2576 (void) signal(SIGTERM, JobPassSig); 2577 } 2578 /* 2579 * Install a NOOP SIGCHLD handler so we are woken up if we're blocked. 2580 */ 2581 signal(SIGCHLD, JobIgnoreSig); 2582 2583 /* 2584 * There are additional signals that need to be caught and passed if 2585 * either the export system wants to be told directly of signals or if 2586 * we're giving each job its own process group (since then it won't get 2587 * signals from the terminal driver as we own the terminal) 2588 */ 2589 #if defined(RMT_WANTS_SIGNALS) || defined(USE_PGRP) 2590 if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) { 2591 (void) signal(SIGTSTP, JobPassSig); 2592 } 2593 if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) { 2594 (void) signal(SIGTTOU, JobPassSig); 2595 } 2596 if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) { 2597 (void) signal(SIGTTIN, JobPassSig); 2598 } 2599 if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) { 2600 (void) signal(SIGWINCH, JobPassSig); 2601 } 2602 if (signal(SIGCONT, SIG_IGN) != SIG_IGN) { 2603 (void) signal(SIGCONT, JobContinueSig); 2604 } 2605 #endif 2606 2607 begin = Targ_FindNode(".BEGIN", TARG_NOCREATE); 2608 2609 if (begin != NILGNODE) { 2610 JobStart(begin, JOB_SPECIAL, (Job *)0); 2611 while (nJobs) { 2612 Job_CatchOutput(); 2613 #ifndef RMT_WILL_WATCH 2614 Job_CatchChildren(!usePipes); 2615 #endif /* RMT_WILL_WATCH */ 2616 } 2617 } 2618 postCommands = Targ_FindNode(".END", TARG_CREATE); 2619 } 2620 2621 /*- 2622 *----------------------------------------------------------------------- 2623 * Job_Empty -- 2624 * See if the job table is empty. Because the local concurrency may 2625 * be set to 0, it is possible for the job table to become empty, 2626 * while the list of stoppedJobs remains non-empty. In such a case, 2627 * we want to restart as many jobs as we can. 2628 * 2629 * Results: 2630 * TRUE if it is. FALSE if it ain't. 2631 * 2632 * Side Effects: 2633 * None. 2634 * 2635 * ----------------------------------------------------------------------- 2636 */ 2637 Boolean 2638 Job_Empty() 2639 { 2640 if (nJobs == 0) { 2641 if (!Lst_IsEmpty(stoppedJobs) && !aborting) { 2642 /* 2643 * The job table is obviously not full if it has no jobs in 2644 * it...Try and restart the stopped jobs. 2645 */ 2646 JobRestartJobs(); 2647 return(FALSE); 2648 } else { 2649 return(TRUE); 2650 } 2651 } else { 2652 return(FALSE); 2653 } 2654 } 2655 2656 /*- 2657 *----------------------------------------------------------------------- 2658 * JobMatchShell -- 2659 * Find a matching shell in 'shells' given its final component. 2660 * 2661 * Results: 2662 * A pointer to the Shell structure. 2663 * 2664 * Side Effects: 2665 * None. 2666 * 2667 *----------------------------------------------------------------------- 2668 */ 2669 static Shell * 2670 JobMatchShell(name) 2671 char *name; /* Final component of shell path */ 2672 { 2673 register Shell *sh; /* Pointer into shells table */ 2674 Shell *match; /* Longest-matching shell */ 2675 register char *cp1, 2676 *cp2; 2677 char *eoname; 2678 2679 eoname = name + strlen(name); 2680 2681 match = NULL; 2682 2683 for (sh = shells; sh->name != NULL; sh++) { 2684 for (cp1 = eoname - strlen(sh->name), cp2 = sh->name; 2685 *cp1 != '\0' && *cp1 == *cp2; 2686 cp1++, cp2++) { 2687 continue; 2688 } 2689 if (*cp1 != *cp2) { 2690 continue; 2691 } else if (match == NULL || strlen(match->name) < strlen(sh->name)) { 2692 match = sh; 2693 } 2694 } 2695 return(match == NULL ? sh : match); 2696 } 2697 2698 /*- 2699 *----------------------------------------------------------------------- 2700 * Job_ParseShell -- 2701 * Parse a shell specification and set up commandShell, shellPath 2702 * and shellName appropriately. 2703 * 2704 * Results: 2705 * FAILURE if the specification was incorrect. 2706 * 2707 * Side Effects: 2708 * commandShell points to a Shell structure (either predefined or 2709 * created from the shell spec), shellPath is the full path of the 2710 * shell described by commandShell, while shellName is just the 2711 * final component of shellPath. 2712 * 2713 * Notes: 2714 * A shell specification consists of a .SHELL target, with dependency 2715 * operator, followed by a series of blank-separated words. Double 2716 * quotes can be used to use blanks in words. A backslash escapes 2717 * anything (most notably a double-quote and a space) and 2718 * provides the functionality it does in C. Each word consists of 2719 * keyword and value separated by an equal sign. There should be no 2720 * unnecessary spaces in the word. The keywords are as follows: 2721 * name Name of shell. 2722 * path Location of shell. Overrides "name" if given 2723 * quiet Command to turn off echoing. 2724 * echo Command to turn echoing on 2725 * filter Result of turning off echoing that shouldn't be 2726 * printed. 2727 * echoFlag Flag to turn echoing on at the start 2728 * errFlag Flag to turn error checking on at the start 2729 * hasErrCtl True if shell has error checking control 2730 * check Command to turn on error checking if hasErrCtl 2731 * is TRUE or template of command to echo a command 2732 * for which error checking is off if hasErrCtl is 2733 * FALSE. 2734 * ignore Command to turn off error checking if hasErrCtl 2735 * is TRUE or template of command to execute a 2736 * command so as to ignore any errors it returns if 2737 * hasErrCtl is FALSE. 2738 * 2739 *----------------------------------------------------------------------- 2740 */ 2741 ReturnStatus 2742 Job_ParseShell(line) 2743 char *line; /* The shell spec */ 2744 { 2745 char **words; 2746 int wordCount; 2747 register char **argv; 2748 register int argc; 2749 char *path; 2750 Shell newShell; 2751 Boolean fullSpec = FALSE; 2752 2753 while (isspace((unsigned char)*line)) { 2754 line++; 2755 } 2756 2757 if (shellArgv) 2758 free(shellArgv); 2759 2760 words = brk_string(line, &wordCount, TRUE, &shellArgv); 2761 2762 memset((Address)&newShell, 0, sizeof(newShell)); 2763 2764 /* 2765 * Parse the specification by keyword 2766 */ 2767 for (path = NULL, argc = wordCount - 1, argv = words; 2768 argc != 0; 2769 argc--, argv++) { 2770 if (strncmp(*argv, "path=", 5) == 0) { 2771 path = &argv[0][5]; 2772 } else if (strncmp(*argv, "name=", 5) == 0) { 2773 newShell.name = &argv[0][5]; 2774 } else { 2775 if (strncmp(*argv, "quiet=", 6) == 0) { 2776 newShell.echoOff = &argv[0][6]; 2777 } else if (strncmp(*argv, "echo=", 5) == 0) { 2778 newShell.echoOn = &argv[0][5]; 2779 } else if (strncmp(*argv, "filter=", 7) == 0) { 2780 newShell.noPrint = &argv[0][7]; 2781 newShell.noPLen = strlen(newShell.noPrint); 2782 } else if (strncmp(*argv, "echoFlag=", 9) == 0) { 2783 newShell.echo = &argv[0][9]; 2784 } else if (strncmp(*argv, "errFlag=", 8) == 0) { 2785 newShell.exit = &argv[0][8]; 2786 } else if (strncmp(*argv, "hasErrCtl=", 10) == 0) { 2787 char c = argv[0][10]; 2788 newShell.hasErrCtl = !((c != 'Y') && (c != 'y') && 2789 (c != 'T') && (c != 't')); 2790 } else if (strncmp(*argv, "check=", 6) == 0) { 2791 newShell.errCheck = &argv[0][6]; 2792 } else if (strncmp(*argv, "ignore=", 7) == 0) { 2793 newShell.ignErr = &argv[0][7]; 2794 } else { 2795 Parse_Error(PARSE_FATAL, "Unknown keyword \"%s\"", 2796 *argv); 2797 free(words); 2798 return(FAILURE); 2799 } 2800 fullSpec = TRUE; 2801 } 2802 } 2803 2804 if (path == NULL) { 2805 /* 2806 * If no path was given, the user wants one of the pre-defined shells, 2807 * yes? So we find the one s/he wants with the help of JobMatchShell 2808 * and set things up the right way. shellPath will be set up by 2809 * Job_Init. 2810 */ 2811 if (newShell.name == NULL) { 2812 Parse_Error(PARSE_FATAL, "Neither path nor name specified"); 2813 return(FAILURE); 2814 } else { 2815 commandShell = JobMatchShell(newShell.name); 2816 shellName = newShell.name; 2817 } 2818 } else { 2819 /* 2820 * The user provided a path. If s/he gave nothing else (fullSpec is 2821 * FALSE), try and find a matching shell in the ones we know of. 2822 * Else we just take the specification at its word and copy it 2823 * to a new location. In either case, we need to record the 2824 * path the user gave for the shell. 2825 */ 2826 shellPath = path; 2827 path = strrchr(path, '/'); 2828 if (path == NULL) { 2829 path = shellPath; 2830 } else { 2831 path += 1; 2832 } 2833 if (newShell.name != NULL) { 2834 shellName = newShell.name; 2835 } else { 2836 shellName = path; 2837 } 2838 if (!fullSpec) { 2839 commandShell = JobMatchShell(shellName); 2840 } else { 2841 commandShell = (Shell *) emalloc(sizeof(Shell)); 2842 *commandShell = newShell; 2843 } 2844 } 2845 2846 if (commandShell->echoOn && commandShell->echoOff) { 2847 commandShell->hasEchoCtl = TRUE; 2848 } 2849 2850 if (!commandShell->hasErrCtl) { 2851 if (commandShell->errCheck == NULL) { 2852 commandShell->errCheck = ""; 2853 } 2854 if (commandShell->ignErr == NULL) { 2855 commandShell->ignErr = "%s\n"; 2856 } 2857 } 2858 2859 /* 2860 * Do not free up the words themselves, since they might be in use by the 2861 * shell specification. 2862 */ 2863 free(words); 2864 return SUCCESS; 2865 } 2866 2867 /*- 2868 *----------------------------------------------------------------------- 2869 * JobInterrupt -- 2870 * Handle the receipt of an interrupt. 2871 * 2872 * Results: 2873 * None 2874 * 2875 * Side Effects: 2876 * All children are killed. Another job will be started if the 2877 * .INTERRUPT target was given. 2878 *----------------------------------------------------------------------- 2879 */ 2880 static void 2881 JobInterrupt(runINTERRUPT, signo) 2882 int runINTERRUPT; /* Non-zero if commands for the .INTERRUPT 2883 * target should be executed */ 2884 int signo; /* signal received */ 2885 { 2886 LstNode ln; /* element in job table */ 2887 Job *job; /* job descriptor in that element */ 2888 GNode *interrupt; /* the node describing the .INTERRUPT target */ 2889 2890 aborting = ABORT_INTERRUPT; 2891 2892 (void) Lst_Open(jobs); 2893 while ((ln = Lst_Next(jobs)) != NILLNODE) { 2894 job = (Job *) Lst_Datum(ln); 2895 2896 if (!Targ_Precious(job->node)) { 2897 char *file = (job->node->path == NULL ? 2898 job->node->name : 2899 job->node->path); 2900 if (!noExecute && eunlink(file) != -1) { 2901 Error("*** %s removed", file); 2902 } 2903 } 2904 #ifdef RMT_WANTS_SIGNALS 2905 if (job->flags & JOB_REMOTE) { 2906 /* 2907 * If job is remote, let the Rmt module do the killing. 2908 */ 2909 if (!Rmt_Signal(job, signo)) { 2910 /* 2911 * If couldn't kill the thing, finish it out now with an 2912 * error code, since no exit report will come in likely. 2913 */ 2914 int status; 2915 2916 status.w_status = 0; 2917 status.w_retcode = 1; 2918 JobFinish(job, &status); 2919 } 2920 } else if (job->pid) { 2921 KILL(job->pid, signo); 2922 } 2923 #else 2924 if (job->pid) { 2925 if (DEBUG(JOB)) { 2926 (void) fprintf(stdout, 2927 "JobInterrupt passing signal to child %d.\n", 2928 job->pid); 2929 (void) fflush(stdout); 2930 } 2931 KILL(job->pid, signo); 2932 } 2933 #endif /* RMT_WANTS_SIGNALS */ 2934 } 2935 2936 #ifdef REMOTE 2937 (void)Lst_Open(stoppedJobs); 2938 while ((ln = Lst_Next(stoppedJobs)) != NILLNODE) { 2939 job = (Job *) Lst_Datum(ln); 2940 2941 if (job->flags & JOB_RESTART) { 2942 if (DEBUG(JOB)) { 2943 (void) fprintf(stdout, "%s%s", 2944 "JobInterrupt skipping job on stopped queue", 2945 "-- it was waiting to be restarted.\n"); 2946 (void) fflush(stdout); 2947 } 2948 continue; 2949 } 2950 if (!Targ_Precious(job->node)) { 2951 char *file = (job->node->path == NULL ? 2952 job->node->name : 2953 job->node->path); 2954 if (eunlink(file) == 0) { 2955 Error("*** %s removed", file); 2956 } 2957 } 2958 /* 2959 * Resume the thing so it will take the signal. 2960 */ 2961 if (DEBUG(JOB)) { 2962 (void) fprintf(stdout, 2963 "JobInterrupt passing CONT to stopped child %d.\n", 2964 job->pid); 2965 (void) fflush(stdout); 2966 } 2967 KILL(job->pid, SIGCONT); 2968 #ifdef RMT_WANTS_SIGNALS 2969 if (job->flags & JOB_REMOTE) { 2970 /* 2971 * If job is remote, let the Rmt module do the killing. 2972 */ 2973 if (!Rmt_Signal(job, SIGINT)) { 2974 /* 2975 * If couldn't kill the thing, finish it out now with an 2976 * error code, since no exit report will come in likely. 2977 */ 2978 int status; 2979 status.w_status = 0; 2980 status.w_retcode = 1; 2981 JobFinish(job, &status); 2982 } 2983 } else if (job->pid) { 2984 if (DEBUG(JOB)) { 2985 (void) fprintf(stdout, 2986 "JobInterrupt passing interrupt to stopped child %d.\n", 2987 job->pid); 2988 (void) fflush(stdout); 2989 } 2990 KILL(job->pid, SIGINT); 2991 } 2992 #endif /* RMT_WANTS_SIGNALS */ 2993 } 2994 #endif 2995 Lst_Close(stoppedJobs); 2996 2997 if (runINTERRUPT && !touchFlag) { 2998 interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 2999 if (interrupt != NILGNODE) { 3000 ignoreErrors = FALSE; 3001 3002 JobStart(interrupt, JOB_IGNDOTS, (Job *)0); 3003 while (nJobs) { 3004 Job_CatchOutput(); 3005 #ifndef RMT_WILL_WATCH 3006 Job_CatchChildren(!usePipes); 3007 #endif /* RMT_WILL_WATCH */ 3008 } 3009 } 3010 } 3011 Trace_Log(MAKEINTR, 0); 3012 exit(signo); 3013 } 3014 3015 /* 3016 *----------------------------------------------------------------------- 3017 * Job_Finish -- 3018 * Do final processing such as the running of the commands 3019 * attached to the .END target. 3020 * 3021 * Results: 3022 * Number of errors reported. 3023 * 3024 * Side Effects: 3025 * None. 3026 *----------------------------------------------------------------------- 3027 */ 3028 int 3029 Job_Finish() 3030 { 3031 if (postCommands != NILGNODE && !Lst_IsEmpty(postCommands->commands)) { 3032 if (errors) { 3033 Error("Errors reported so .END ignored"); 3034 } else { 3035 JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL); 3036 3037 while (nJobs) { 3038 Job_CatchOutput(); 3039 #ifndef RMT_WILL_WATCH 3040 Job_CatchChildren(!usePipes); 3041 #endif /* RMT_WILL_WATCH */ 3042 } 3043 } 3044 } 3045 Job_TokenFlush(); 3046 return(errors); 3047 } 3048 3049 /*- 3050 *----------------------------------------------------------------------- 3051 * Job_End -- 3052 * Cleanup any memory used by the jobs module 3053 * 3054 * Results: 3055 * None. 3056 * 3057 * Side Effects: 3058 * Memory is freed 3059 *----------------------------------------------------------------------- 3060 */ 3061 void 3062 Job_End() 3063 { 3064 #ifdef CLEANUP 3065 if (shellArgv) 3066 free(shellArgv); 3067 #endif 3068 } 3069 3070 /*- 3071 *----------------------------------------------------------------------- 3072 * Job_Wait -- 3073 * Waits for all running jobs to finish and returns. Sets 'aborting' 3074 * to ABORT_WAIT to prevent other jobs from starting. 3075 * 3076 * Results: 3077 * None. 3078 * 3079 * Side Effects: 3080 * Currently running jobs finish. 3081 * 3082 *----------------------------------------------------------------------- 3083 */ 3084 void 3085 Job_Wait() 3086 { 3087 aborting = ABORT_WAIT; 3088 while (nJobs != 0) { 3089 Job_CatchOutput(); 3090 #ifndef RMT_WILL_WATCH 3091 Job_CatchChildren(!usePipes); 3092 #endif /* RMT_WILL_WATCH */ 3093 } 3094 Job_TokenFlush(); 3095 aborting = 0; 3096 } 3097 3098 /*- 3099 *----------------------------------------------------------------------- 3100 * Job_AbortAll -- 3101 * Abort all currently running jobs without handling output or anything. 3102 * This function is to be called only in the event of a major 3103 * error. Most definitely NOT to be called from JobInterrupt. 3104 * 3105 * Results: 3106 * None 3107 * 3108 * Side Effects: 3109 * All children are killed, not just the firstborn 3110 *----------------------------------------------------------------------- 3111 */ 3112 void 3113 Job_AbortAll() 3114 { 3115 LstNode ln; /* element in job table */ 3116 Job *job; /* the job descriptor in that element */ 3117 int foo; 3118 3119 aborting = ABORT_ERROR; 3120 3121 if (nJobs) { 3122 3123 (void) Lst_Open(jobs); 3124 while ((ln = Lst_Next(jobs)) != NILLNODE) { 3125 job = (Job *) Lst_Datum(ln); 3126 3127 /* 3128 * kill the child process with increasingly drastic signals to make 3129 * darn sure it's dead. 3130 */ 3131 #ifdef RMT_WANTS_SIGNALS 3132 if (job->flags & JOB_REMOTE) { 3133 Rmt_Signal(job, SIGINT); 3134 Rmt_Signal(job, SIGKILL); 3135 } else { 3136 KILL(job->pid, SIGINT); 3137 KILL(job->pid, SIGKILL); 3138 } 3139 #else 3140 KILL(job->pid, SIGINT); 3141 KILL(job->pid, SIGKILL); 3142 #endif /* RMT_WANTS_SIGNALS */ 3143 } 3144 } 3145 3146 /* 3147 * Catch as many children as want to report in at first, then give up 3148 */ 3149 while (waitpid((pid_t) -1, &foo, WNOHANG) > 0) 3150 continue; 3151 } 3152 3153 #ifdef REMOTE 3154 /*- 3155 *----------------------------------------------------------------------- 3156 * JobFlagForMigration -- 3157 * Handle the eviction of a child. Called from RmtStatusChange. 3158 * Flags the child as remigratable and then suspends it. 3159 * 3160 * Results: 3161 * none. 3162 * 3163 * Side Effects: 3164 * The job descriptor is flagged for remigration. 3165 * 3166 *----------------------------------------------------------------------- 3167 */ 3168 void 3169 JobFlagForMigration(hostID) 3170 int hostID; /* ID of host we used, for matching children. */ 3171 { 3172 register Job *job; /* job descriptor for dead child */ 3173 LstNode jnode; /* list element for finding job */ 3174 3175 if (DEBUG(JOB)) { 3176 (void) fprintf(stdout, "JobFlagForMigration(%d) called.\n", hostID); 3177 (void) fflush(stdout); 3178 } 3179 jnode = Lst_Find(jobs, (ClientData)hostID, JobCmpRmtID); 3180 3181 if (jnode == NILLNODE) { 3182 jnode = Lst_Find(stoppedJobs, (ClientData)hostID, JobCmpRmtID); 3183 if (jnode == NILLNODE) { 3184 if (DEBUG(JOB)) { 3185 Error("Evicting host(%d) not in table", hostID); 3186 } 3187 return; 3188 } 3189 } 3190 job = (Job *) Lst_Datum(jnode); 3191 3192 if (DEBUG(JOB)) { 3193 (void) fprintf(stdout, 3194 "JobFlagForMigration(%d) found job '%s'.\n", hostID, 3195 job->node->name); 3196 (void) fflush(stdout); 3197 } 3198 3199 KILL(job->pid, SIGSTOP); 3200 3201 job->flags |= JOB_REMIGRATE; 3202 } 3203 3204 #endif 3205 3206 /*- 3207 *----------------------------------------------------------------------- 3208 * JobRestartJobs -- 3209 * Tries to restart stopped jobs if there are slots available. 3210 * Note that this tries to restart them regardless of pending errors. 3211 * It's not good to leave stopped jobs lying around! 3212 * 3213 * Results: 3214 * None. 3215 * 3216 * Side Effects: 3217 * Resumes(and possibly migrates) jobs. 3218 * 3219 *----------------------------------------------------------------------- 3220 */ 3221 static void 3222 JobRestartJobs() 3223 { 3224 while (!Lst_IsEmpty(stoppedJobs)) { 3225 if (DEBUG(JOB)) { 3226 (void) fprintf(stdout, "Restarting a stopped job.\n"); 3227 (void) fflush(stdout); 3228 } 3229 JobRestart((Job *)Lst_DeQueue(stoppedJobs)); 3230 } 3231 } 3232 3233 #ifndef RMT_WILL_WATCH 3234 #ifndef USE_SELECT 3235 static void 3236 watchfd(job) 3237 Job *job; 3238 { 3239 int i; 3240 if (job->inPollfd != NULL) 3241 Punt("Watching watched job"); 3242 if (fds == NULL) { 3243 maxfds = JBSTART; 3244 fds = emalloc(sizeof(struct pollfd) * maxfds); 3245 jobfds = emalloc(sizeof(Job **) * maxfds); 3246 3247 fds[0].fd = job_pipe[0]; 3248 fds[0].events = POLLIN; 3249 jobfds[0] = &tokenWaitJob; 3250 tokenWaitJob.inPollfd = &fds[0]; 3251 nfds++; 3252 } else if (nfds == maxfds) { 3253 maxfds *= JBFACTOR; 3254 fds = erealloc(fds, sizeof(struct pollfd) * maxfds); 3255 jobfds = erealloc(jobfds, sizeof(Job **) * maxfds); 3256 for (i = 0; i < nfds; i++) 3257 jobfds[i]->inPollfd = &fds[i]; 3258 } 3259 3260 fds[nfds].fd = job->inPipe; 3261 fds[nfds].events = POLLIN; 3262 jobfds[nfds] = job; 3263 job->inPollfd = &fds[nfds]; 3264 nfds++; 3265 } 3266 3267 static void 3268 clearfd(job) 3269 Job *job; 3270 { 3271 int i; 3272 if (job->inPollfd == NULL) 3273 Punt("Unwatching unwatched job"); 3274 i = job->inPollfd - fds; 3275 nfds--; 3276 /* 3277 * Move last job in table into hole made by dead job. 3278 */ 3279 if (nfds != i) { 3280 fds[i] = fds[nfds]; 3281 jobfds[i] = jobfds[nfds]; 3282 jobfds[i]->inPollfd = &fds[i]; 3283 } 3284 job->inPollfd = NULL; 3285 } 3286 3287 static int 3288 readyfd(job) 3289 Job *job; 3290 { 3291 if (job->inPollfd == NULL) 3292 Punt("Polling unwatched job"); 3293 return (job->inPollfd->revents & POLLIN) != 0; 3294 } 3295 #endif 3296 #endif 3297 3298 /*- 3299 *----------------------------------------------------------------------- 3300 * JobTokenAdd -- 3301 * Put a token into the job pipe so that some make process can start 3302 * another job. 3303 * 3304 * Side Effects: 3305 * Allows more build jobs to be spawned somewhere. 3306 * 3307 *----------------------------------------------------------------------- 3308 */ 3309 3310 static void 3311 JobTokenAdd() 3312 { 3313 3314 if (DEBUG(JOB)) 3315 printf("deposit token\n"); 3316 write(job_pipe[1], "+", 1); 3317 } 3318 3319 /*- 3320 *----------------------------------------------------------------------- 3321 * Job_ServerStartTokenAdd -- 3322 * Prep the job token pipe in the root make process. 3323 * 3324 *----------------------------------------------------------------------- 3325 */ 3326 3327 void Job_ServerStart(maxproc) 3328 int maxproc; 3329 { 3330 int i, flags; 3331 char jobarg[64]; 3332 3333 if (pipe(job_pipe) < 0) 3334 Fatal ("error in pipe: %s", strerror(errno)); 3335 3336 /* 3337 * We mark the input side of the pipe non-blocking; we poll(2) the 3338 * pipe when we're waiting for a job token, but we might lose the 3339 * race for the token when a new one becomes available, so the read 3340 * from the pipe should not block. 3341 */ 3342 flags = fcntl(job_pipe[0], F_GETFL, 0); 3343 flags |= O_NONBLOCK; 3344 fcntl(job_pipe[0], F_SETFL, flags); 3345 3346 /* 3347 * Mark job pipes as close-on-exec. 3348 * Note that we will clear this when executing submakes. 3349 */ 3350 fcntl(job_pipe[0], F_SETFD, 1); 3351 fcntl(job_pipe[1], F_SETFD, 1); 3352 3353 snprintf(jobarg, sizeof(jobarg), "%d,%d", job_pipe[0], job_pipe[1]); 3354 3355 Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL); 3356 Var_Append(MAKEFLAGS, jobarg, VAR_GLOBAL); 3357 3358 /* 3359 * Preload job_pipe with one token per job, save the one 3360 * "extra" token for the primary job. 3361 * 3362 * XXX should clip maxJobs against PIPE_BUF -- if maxJobs is 3363 * larger than the write buffer size of the pipe, we will 3364 * deadlock here. 3365 */ 3366 for (i=1; i < maxproc; i++) 3367 JobTokenAdd(); 3368 } 3369 3370 /* 3371 * this tracks the number of tokens currently "out" to build jobs. 3372 */ 3373 int jobTokensRunning = 0; 3374 int jobTokensFree = 0; 3375 /*- 3376 *----------------------------------------------------------------------- 3377 * Job_TokenReturn -- 3378 * Return a withdrawn token to the pool. 3379 * 3380 *----------------------------------------------------------------------- 3381 */ 3382 3383 void 3384 Job_TokenReturn() 3385 { 3386 jobTokensRunning--; 3387 if (jobTokensRunning < 0) 3388 Punt("token botch"); 3389 if (jobTokensRunning) 3390 jobTokensFree++; 3391 } 3392 3393 /*- 3394 *----------------------------------------------------------------------- 3395 * Job_TokenWithdraw -- 3396 * Attempt to withdraw a token from the pool. 3397 * 3398 * Results: 3399 * Returns TRUE if a token was withdrawn, and FALSE if the pool 3400 * is currently empty. 3401 * 3402 * Side Effects: 3403 * If pool is empty, set wantToken so that we wake up 3404 * when a token is released. 3405 * 3406 *----------------------------------------------------------------------- 3407 */ 3408 3409 3410 Boolean 3411 Job_TokenWithdraw() 3412 { 3413 char tok; 3414 int count; 3415 3416 if (aborting) 3417 return FALSE; 3418 3419 if (jobTokensRunning == 0) { 3420 if (DEBUG(JOB)) 3421 printf("first one's free\n"); 3422 jobTokensRunning++; 3423 wantToken = FALSE; 3424 return TRUE; 3425 } 3426 if (jobTokensFree > 0) { 3427 jobTokensFree--; 3428 jobTokensRunning++; 3429 wantToken = FALSE; 3430 return TRUE; 3431 } 3432 count = read(job_pipe[0], &tok, 1); 3433 if (count == 0) 3434 Fatal("eof on job pipe!"); 3435 else if (count < 0) { 3436 if (errno != EAGAIN) { 3437 Fatal("job pipe read: %s", strerror(errno)); 3438 } 3439 if (DEBUG(JOB)) 3440 printf("blocked for token\n"); 3441 wantToken = TRUE; 3442 return FALSE; 3443 } 3444 wantToken = FALSE; 3445 jobTokensRunning++; 3446 if (DEBUG(JOB)) 3447 printf("withdrew token\n"); 3448 return TRUE; 3449 } 3450 3451 /*- 3452 *----------------------------------------------------------------------- 3453 * Job_TokenFlush -- 3454 * Return free tokens to the pool. 3455 * 3456 *----------------------------------------------------------------------- 3457 */ 3458 3459 void 3460 Job_TokenFlush() 3461 { 3462 if (compatMake) return; 3463 3464 while (jobTokensFree > 0) { 3465 JobTokenAdd(); 3466 jobTokensFree--; 3467 } 3468 } 3469 3470