1 /* 2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 3 * Copyright (c) 1988, 1989 by Adam de Boor 4 * Copyright (c) 1989 by Berkeley Softworks 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE 39 * -------------------- ----- ---------------------- 40 * CURRENT PATCH LEVEL: 1 00039 41 * -------------------- ----- ---------------------- 42 * 43 * 10 Aug 92 David Dawes Fixed "remove directory" bug 44 */ 45 46 #ifndef lint 47 static char sccsid[] = "@(#)job.c 5.15 (Berkeley) 3/1/91"; 48 #endif /* not lint */ 49 50 /*- 51 * job.c -- 52 * handle the creation etc. of our child processes. 53 * 54 * Interface: 55 * Job_Make Start the creation of the given target. 56 * 57 * Job_CatchChildren Check for and handle the termination of any 58 * children. This must be called reasonably 59 * frequently to keep the whole make going at 60 * a decent clip, since job table entries aren't 61 * removed until their process is caught this way. 62 * Its single argument is TRUE if the function 63 * should block waiting for a child to terminate. 64 * 65 * Job_CatchOutput Print any output our children have produced. 66 * Should also be called fairly frequently to 67 * keep the user informed of what's going on. 68 * If no output is waiting, it will block for 69 * a time given by the SEL_* constants, below, 70 * or until output is ready. 71 * 72 * Job_Init Called to intialize this module. in addition, 73 * any commands attached to the .BEGIN target 74 * are executed before this function returns. 75 * Hence, the makefile must have been parsed 76 * before this function is called. 77 * 78 * Job_Full Return TRUE if the job table is filled. 79 * 80 * Job_Empty Return TRUE if the job table is completely 81 * empty. 82 * 83 * Job_ParseShell Given the line following a .SHELL target, parse 84 * the line as a shell specification. Returns 85 * FAILURE if the spec was incorrect. 86 * 87 * Job_End Perform any final processing which needs doing. 88 * This includes the execution of any commands 89 * which have been/were attached to the .END 90 * target. It should only be called when the 91 * job table is empty. 92 * 93 * Job_AbortAll Abort all currently running jobs. It doesn't 94 * handle output or do anything for the jobs, 95 * just kills them. It should only be called in 96 * an emergency, as it were. 97 * 98 * Job_CheckCommands Verify that the commands for a target are 99 * ok. Provide them if necessary and possible. 100 * 101 * Job_Touch Update a target without really updating it. 102 * 103 * Job_Wait Wait for all currently-running jobs to finish. 104 */ 105 106 #include "make.h" 107 #include <sys/signal.h> 108 #include <sys/stat.h> 109 #include <sys/file.h> 110 #include <sys/time.h> 111 #include <sys/wait.h> 112 #include <fcntl.h> 113 #include <errno.h> 114 #include <stdio.h> 115 #include <string.h> 116 #include <ctype.h> 117 #include "job.h" 118 #include "pathnames.h" 119 120 extern int errno; 121 122 /* 123 * error handling variables 124 */ 125 int errors = 0; /* number of errors reported */ 126 int aborting = 0; /* why is the make aborting? */ 127 #define ABORT_ERROR 1 /* Because of an error */ 128 #define ABORT_INTERRUPT 2 /* Because it was interrupted */ 129 #define ABORT_WAIT 3 /* Waiting for jobs to finish */ 130 131 132 /* 133 * post-make command processing. The node postCommands is really just the 134 * .END target but we keep it around to avoid having to search for it 135 * all the time. 136 */ 137 static GNode *postCommands; /* node containing commands to execute when 138 * everything else is done */ 139 static int numCommands; /* The number of commands actually printed 140 * for a target. Should this number be 141 * 0, no shell will be executed. */ 142 143 144 /* 145 * Return values from JobStart. 146 */ 147 #define JOB_RUNNING 0 /* Job is running */ 148 #define JOB_ERROR 1 /* Error in starting the job */ 149 #define JOB_FINISHED 2 /* The job is already finished */ 150 #define JOB_STOPPED 3 /* The job is stopped */ 151 152 /* 153 * tfile is the name of a file into which all shell commands are put. It is 154 * used over by removing it before the child shell is executed. The XXXXX in 155 * the string are replaced by the pid of the make process in a 5-character 156 * field with leading zeroes. 157 */ 158 static char tfile[] = TMPPAT; 159 160 161 /* 162 * Descriptions for various shells. 163 */ 164 static Shell shells[] = { 165 /* 166 * CSH description. The csh can do echo control by playing 167 * with the setting of the 'echo' shell variable. Sadly, 168 * however, it is unable to do error control nicely. 169 */ 170 { 171 "csh", 172 TRUE, "unset verbose", "set verbose", "unset verbose", 10, 173 FALSE, "echo \"%s\"\n", "csh -c \"%s || exit 0\"", 174 "v", "e", 175 }, 176 /* 177 * SH description. Echo control is also possible and, under 178 * sun UNIX anyway, one can even control error checking. 179 */ 180 { 181 "sh", 182 TRUE, "set -", "set -v", "set -", 5, 183 FALSE, "echo \"%s\"\n", "sh -c '%s || exit 0'\n", 184 "v", "e", 185 }, 186 /* 187 * UNKNOWN. 188 */ 189 { 190 (char *)0, 191 FALSE, (char *)0, (char *)0, (char *)0, 0, 192 FALSE, (char *)0, (char *)0, 193 (char *)0, (char *)0, 194 } 195 }; 196 Shell *commandShell = &shells[DEFSHELL]; /* this is the shell to 197 * which we pass all 198 * commands in the Makefile. 199 * It is set by the 200 * Job_ParseShell function */ 201 char *shellPath = (char *) NULL, /* full pathname of 202 * executable image */ 203 *shellName; /* last component of shell */ 204 205 206 static int maxJobs; /* The most children we can run at once */ 207 static int maxLocal; /* The most local ones we can have */ 208 int nJobs; /* The number of children currently running */ 209 int nLocal; /* The number of local children */ 210 Lst jobs; /* The structures that describe them */ 211 Boolean jobFull; /* Flag to tell when the job table is full. It 212 * is set TRUE when (1) the total number of 213 * running jobs equals the maximum allowed or 214 * (2) a job can only be run locally, but 215 * nLocal equals maxLocal */ 216 #ifndef RMT_WILL_WATCH 217 static fd_set outputs; /* Set of descriptors of pipes connected to 218 * the output channels of children */ 219 #endif 220 221 GNode *lastNode; /* The node for which output was most recently 222 * produced. */ 223 char *targFmt; /* Format string to use to head output from a 224 * job when it's not the most-recent job heard 225 * from */ 226 #define TARG_FMT "--- %s ---\n" /* Default format */ 227 228 /* 229 * When JobStart attempts to run a job remotely but can't, and isn't allowed 230 * to run the job locally, or when Job_CatchChildren detects a job that has 231 * been migrated home, the job is placed on the stoppedJobs queue to be run 232 * when the next job finishes. 233 */ 234 Lst stoppedJobs; /* Lst of Job structures describing 235 * jobs that were stopped due to concurrency 236 * limits or migration home */ 237 238 239 # if defined(USE_PGRP) 240 #define KILL(pid,sig) killpg((pid),(sig)) 241 # else 242 #define KILL(pid,sig) kill((pid),(sig)) 243 # endif 244 245 static void JobRestart(); 246 static int JobStart(); 247 static void JobInterrupt(); 248 249 /*- 250 *----------------------------------------------------------------------- 251 * JobCondPassSig -- 252 * Pass a signal to a job if the job is remote or if USE_PGRP 253 * is defined. 254 * 255 * Results: 256 * === 0 257 * 258 * Side Effects: 259 * None, except the job may bite it. 260 * 261 *----------------------------------------------------------------------- 262 */ 263 static int 264 JobCondPassSig(job, signo) 265 Job *job; /* Job to biff */ 266 int signo; /* Signal to send it */ 267 { 268 #ifdef RMT_WANTS_SIGNALS 269 if (job->flags & JOB_REMOTE) { 270 (void)Rmt_Signal(job, signo); 271 } else { 272 KILL(job->pid, signo); 273 } 274 #else 275 /* 276 * Assume that sending the signal to job->pid will signal any remote 277 * job as well. 278 */ 279 KILL(job->pid, signo); 280 #endif 281 return(0); 282 } 283 284 /*- 285 *----------------------------------------------------------------------- 286 * JobPassSig -- 287 * Pass a signal on to all remote jobs and to all local jobs if 288 * USE_PGRP is defined, then die ourselves. 289 * 290 * Results: 291 * None. 292 * 293 * Side Effects: 294 * We die by the same signal. 295 * 296 *----------------------------------------------------------------------- 297 */ 298 static void 299 JobPassSig(signo) 300 int signo; /* The signal number we've received */ 301 { 302 int mask; 303 304 Lst_ForEach(jobs, JobCondPassSig, (ClientData)signo); 305 306 /* 307 * Deal with proper cleanup based on the signal received. We only run 308 * the .INTERRUPT target if the signal was in fact an interrupt. The other 309 * three termination signals are more of a "get out *now*" command. 310 */ 311 if (signo == SIGINT) { 312 JobInterrupt(TRUE); 313 } else if ((signo == SIGHUP) || (signo == SIGTERM) || (signo == SIGQUIT)) { 314 JobInterrupt(FALSE); 315 } 316 317 /* 318 * Leave gracefully if SIGQUIT, rather than core dumping. 319 */ 320 if (signo == SIGQUIT) { 321 Finish(); 322 } 323 324 /* 325 * Send ourselves the signal now we've given the message to everyone else. 326 * Note we block everything else possible while we're getting the signal. 327 * This ensures that all our jobs get continued when we wake up before 328 * we take any other signal. 329 */ 330 mask = sigblock(0); 331 (void) sigsetmask(~0 & ~(1 << (signo-1))); 332 signal(signo, SIG_DFL); 333 334 kill(getpid(), signo); 335 336 Lst_ForEach(jobs, JobCondPassSig, (ClientData)SIGCONT); 337 338 sigsetmask(mask); 339 signal(signo, JobPassSig); 340 341 } 342 343 /*- 344 *----------------------------------------------------------------------- 345 * JobCmpPid -- 346 * Compare the pid of the job with the given pid and return 0 if they 347 * are equal. This function is called from Job_CatchChildren via 348 * Lst_Find to find the job descriptor of the finished job. 349 * 350 * Results: 351 * 0 if the pid's match 352 * 353 * Side Effects: 354 * None 355 *----------------------------------------------------------------------- 356 */ 357 static int 358 JobCmpPid (job, pid) 359 int pid; /* process id desired */ 360 Job *job; /* job to examine */ 361 { 362 return (pid - job->pid); 363 } 364 365 /*- 366 *----------------------------------------------------------------------- 367 * JobPrintCommand -- 368 * Put out another command for the given job. If the command starts 369 * with an @ or a - we process it specially. In the former case, 370 * so long as the -s and -n flags weren't given to make, we stick 371 * a shell-specific echoOff command in the script. In the latter, 372 * we ignore errors for the entire job, unless the shell has error 373 * control. 374 * If the command is just "..." we take all future commands for this 375 * job to be commands to be executed once the entire graph has been 376 * made and return non-zero to signal that the end of the commands 377 * was reached. These commands are later attached to the postCommands 378 * node and executed by Job_End when all things are done. 379 * This function is called from JobStart via Lst_ForEach. 380 * 381 * Results: 382 * Always 0, unless the command was "..." 383 * 384 * Side Effects: 385 * If the command begins with a '-' and the shell has no error control, 386 * the JOB_IGNERR flag is set in the job descriptor. 387 * If the command is "..." and we're not ignoring such things, 388 * tailCmds is set to the successor node of the cmd. 389 * numCommands is incremented if the command is actually printed. 390 *----------------------------------------------------------------------- 391 */ 392 static int 393 JobPrintCommand (cmd, job) 394 char *cmd; /* command string to print */ 395 Job *job; /* job for which to print it */ 396 { 397 Boolean noSpecials; /* true if we shouldn't worry about 398 * inserting special commands into 399 * the input stream. */ 400 Boolean shutUp = FALSE; /* true if we put a no echo command 401 * into the command file */ 402 Boolean errOff = FALSE; /* true if we turned error checking 403 * off before printing the command 404 * and need to turn it back on */ 405 char *cmdTemplate; /* Template to use when printing the 406 * command */ 407 char *cmdStart; /* Start of expanded command */ 408 LstNode cmdNode; /* Node for replacing the command */ 409 410 noSpecials = (noExecute && ! (job->node->type & OP_MAKE)); 411 412 if (strcmp (cmd, "...") == 0) { 413 if ((job->flags & JOB_IGNDOTS) == 0) { 414 job->tailCmds = Lst_Succ (Lst_Member (job->node->commands, 415 (ClientData)cmd)); 416 return (1); 417 } 418 return (0); 419 } 420 421 #define DBPRINTF(fmt, arg) if (DEBUG(JOB)) printf (fmt, arg); fprintf (job->cmdFILE, fmt, arg) 422 423 numCommands += 1; 424 425 /* 426 * For debugging, we replace each command with the result of expanding 427 * the variables in the command. 428 */ 429 cmdNode = Lst_Member (job->node->commands, (ClientData)cmd); 430 cmdStart = cmd = Var_Subst (cmd, job->node, FALSE); 431 Lst_Replace (cmdNode, (ClientData)cmdStart); 432 433 cmdTemplate = "%s\n"; 434 435 /* 436 * Check for leading @' and -'s to control echoing and error checking. 437 */ 438 while (*cmd == '@' || *cmd == '-') { 439 if (*cmd == '@') { 440 shutUp = TRUE; 441 } else { 442 errOff = TRUE; 443 } 444 cmd++; 445 } 446 447 while (isspace(*cmd)) cmd++; 448 449 if (shutUp) { 450 if (! (job->flags & JOB_SILENT) && !noSpecials && 451 commandShell->hasEchoCtl) { 452 DBPRINTF ("%s\n", commandShell->echoOff); 453 } else { 454 shutUp = FALSE; 455 } 456 } 457 458 if (errOff) { 459 if ( ! (job->flags & JOB_IGNERR) && !noSpecials) { 460 if (commandShell->hasErrCtl) { 461 /* 462 * we don't want the error-control commands showing 463 * up either, so we turn off echoing while executing 464 * them. We could put another field in the shell 465 * structure to tell JobDoOutput to look for this 466 * string too, but why make it any more complex than 467 * it already is? 468 */ 469 if (! (job->flags & JOB_SILENT) && !shutUp && 470 commandShell->hasEchoCtl) { 471 DBPRINTF ("%s\n", commandShell->echoOff); 472 DBPRINTF ("%s\n", commandShell->ignErr); 473 DBPRINTF ("%s\n", commandShell->echoOn); 474 } else { 475 DBPRINTF ("%s\n", commandShell->ignErr); 476 } 477 } else if (commandShell->ignErr && 478 (*commandShell->ignErr != '\0')) 479 { 480 /* 481 * The shell has no error control, so we need to be 482 * weird to get it to ignore any errors from the command. 483 * If echoing is turned on, we turn it off and use the 484 * errCheck template to echo the command. Leave echoing 485 * off so the user doesn't see the weirdness we go through 486 * to ignore errors. Set cmdTemplate to use the weirdness 487 * instead of the simple "%s\n" template. 488 */ 489 if (! (job->flags & JOB_SILENT) && !shutUp && 490 commandShell->hasEchoCtl) { 491 DBPRINTF ("%s\n", commandShell->echoOff); 492 DBPRINTF (commandShell->errCheck, cmd); 493 shutUp = TRUE; 494 } 495 cmdTemplate = commandShell->ignErr; 496 /* 497 * The error ignoration (hee hee) is already taken care 498 * of by the ignErr template, so pretend error checking 499 * is still on. 500 */ 501 errOff = FALSE; 502 } else { 503 errOff = FALSE; 504 } 505 } else { 506 errOff = FALSE; 507 } 508 } 509 510 DBPRINTF (cmdTemplate, cmd); 511 512 if (errOff) { 513 /* 514 * If echoing is already off, there's no point in issuing the 515 * echoOff command. Otherwise we issue it and pretend it was on 516 * for the whole command... 517 */ 518 if (!shutUp && !(job->flags & JOB_SILENT) && commandShell->hasEchoCtl){ 519 DBPRINTF ("%s\n", commandShell->echoOff); 520 shutUp = TRUE; 521 } 522 DBPRINTF ("%s\n", commandShell->errCheck); 523 } 524 if (shutUp) { 525 DBPRINTF ("%s\n", commandShell->echoOn); 526 } 527 return (0); 528 } 529 530 /*- 531 *----------------------------------------------------------------------- 532 * JobSaveCommand -- 533 * Save a command to be executed when everything else is done. 534 * Callback function for JobFinish... 535 * 536 * Results: 537 * Always returns 0 538 * 539 * Side Effects: 540 * The command is tacked onto the end of postCommands's commands list. 541 * 542 *----------------------------------------------------------------------- 543 */ 544 static int 545 JobSaveCommand (cmd, gn) 546 char *cmd; 547 GNode *gn; 548 { 549 cmd = Var_Subst (cmd, gn, FALSE); 550 (void)Lst_AtEnd (postCommands->commands, (ClientData)cmd); 551 return (0); 552 } 553 554 /*- 555 *----------------------------------------------------------------------- 556 * JobFinish -- 557 * Do final processing for the given job including updating 558 * parents and starting new jobs as available/necessary. Note 559 * that we pay no attention to the JOB_IGNERR flag here. 560 * This is because when we're called because of a noexecute flag 561 * or something, jstat.w_status is 0 and when called from 562 * Job_CatchChildren, the status is zeroed if it s/b ignored. 563 * 564 * Results: 565 * None 566 * 567 * Side Effects: 568 * Some nodes may be put on the toBeMade queue. 569 * Final commands for the job are placed on postCommands. 570 * 571 * If we got an error and are aborting (aborting == ABORT_ERROR) and 572 * the job list is now empty, we are done for the day. 573 * If we recognized an error (errors !=0), we set the aborting flag 574 * to ABORT_ERROR so no more jobs will be started. 575 *----------------------------------------------------------------------- 576 */ 577 /*ARGSUSED*/ 578 void 579 JobFinish (job, status) 580 Job *job; /* job to finish */ 581 union wait status; /* sub-why job went away */ 582 { 583 Boolean done; 584 585 if ((WIFEXITED(status) && 586 (((status.w_retcode != 0) && !(job->flags & JOB_IGNERR)))) || 587 (WIFSIGNALED(status) && (status.w_termsig != SIGCONT))) 588 { 589 /* 590 * If it exited non-zero and either we're doing things our 591 * way or we're not ignoring errors, the job is finished. 592 * Similarly, if the shell died because of a signal 593 * the job is also finished. In these 594 * cases, finish out the job's output before printing the exit 595 * status... 596 */ 597 if (usePipes) { 598 #ifdef RMT_WILL_WATCH 599 Rmt_Ignore(job->inPipe); 600 #else 601 FD_CLR(job->inPipe, &outputs); 602 #endif /* RMT_WILL_WATCH */ 603 if (job->outPipe != job->inPipe) { 604 (void)close (job->outPipe); 605 } 606 JobDoOutput (job, TRUE); 607 (void)close (job->inPipe); 608 } else { 609 (void)close (job->outFd); 610 JobDoOutput (job, TRUE); 611 } 612 613 if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 614 fclose(job->cmdFILE); 615 } 616 done = TRUE; 617 } else if (WIFEXITED(status) && status.w_retcode != 0) { 618 /* 619 * Deal with ignored errors in -B mode. We need to print a message 620 * telling of the ignored error as well as setting status.w_status 621 * to 0 so the next command gets run. To do this, we set done to be 622 * TRUE if in -B mode and the job exited non-zero. Note we don't 623 * want to close down any of the streams until we know we're at the 624 * end. 625 */ 626 done = TRUE; 627 } else { 628 /* 629 * No need to close things down or anything. 630 */ 631 done = FALSE; 632 } 633 634 if (done || 635 WIFSTOPPED(status) || 636 (WIFSIGNALED(status) && (status.w_termsig == SIGCONT)) || 637 DEBUG(JOB)) 638 { 639 FILE *out; 640 641 if (!usePipes && (job->flags & JOB_IGNERR)) { 642 /* 643 * If output is going to a file and this job is ignoring 644 * errors, arrange to have the exit status sent to the 645 * output file as well. 646 */ 647 out = fdopen (job->outFd, "w"); 648 } else { 649 out = stdout; 650 } 651 652 if (WIFEXITED(status)) { 653 if (status.w_retcode != 0) { 654 if (usePipes && job->node != lastNode) { 655 fprintf (out, targFmt, job->node->name); 656 lastNode = job->node; 657 } 658 fprintf (out, "*** Error code %d%s\n", status.w_retcode, 659 (job->flags & JOB_IGNERR) ? " (ignored)" : ""); 660 661 if (job->flags & JOB_IGNERR) { 662 status.w_status = 0; 663 } 664 } else if (DEBUG(JOB)) { 665 if (usePipes && job->node != lastNode) { 666 fprintf (out, targFmt, job->node->name); 667 lastNode = job->node; 668 } 669 fprintf (out, "*** Completed successfully\n"); 670 } 671 } else if (WIFSTOPPED(status)) { 672 if (usePipes && job->node != lastNode) { 673 fprintf (out, targFmt, job->node->name); 674 lastNode = job->node; 675 } 676 if (! (job->flags & JOB_REMIGRATE)) { 677 fprintf (out, "*** Stopped -- signal %d\n", status.w_stopsig); 678 } 679 job->flags |= JOB_RESUME; 680 (void)Lst_AtEnd(stoppedJobs, (ClientData)job); 681 fflush(out); 682 return; 683 } else if (status.w_termsig == SIGCONT) { 684 /* 685 * If the beastie has continued, shift the Job from the stopped 686 * list to the running one (or re-stop it if concurrency is 687 * exceeded) and go and get another child. 688 */ 689 if (job->flags & (JOB_RESUME|JOB_REMIGRATE|JOB_RESTART)) { 690 if (usePipes && job->node != lastNode) { 691 fprintf (out, targFmt, job->node->name); 692 lastNode = job->node; 693 } 694 fprintf (out, "*** Continued\n"); 695 } 696 if (! (job->flags & JOB_CONTINUING)) { 697 JobRestart(job); 698 } else { 699 Lst_AtEnd(jobs, (ClientData)job); 700 nJobs += 1; 701 if (! (job->flags & JOB_REMOTE)) { 702 nLocal += 1; 703 } 704 if (nJobs == maxJobs) { 705 jobFull = TRUE; 706 if (DEBUG(JOB)) { 707 printf("Job queue is full.\n"); 708 } 709 } 710 } 711 fflush(out); 712 return; 713 } else { 714 if (usePipes && job->node != lastNode) { 715 fprintf (out, targFmt, job->node->name); 716 lastNode = job->node; 717 } 718 fprintf (out, "*** Signal %d\n", status.w_termsig); 719 } 720 721 fflush (out); 722 } 723 724 /* 725 * Now handle the -B-mode stuff. If the beast still isn't finished, 726 * try and restart the job on the next command. If JobStart says it's 727 * ok, it's ok. If there's an error, this puppy is done. 728 */ 729 if ((status.w_status == 0) && 730 !Lst_IsAtEnd (job->node->commands)) 731 { 732 switch (JobStart (job->node, 733 job->flags & JOB_IGNDOTS, 734 job)) 735 { 736 case JOB_RUNNING: 737 done = FALSE; 738 break; 739 case JOB_ERROR: 740 done = TRUE; 741 status.w_retcode = 1; 742 break; 743 case JOB_FINISHED: 744 /* 745 * If we got back a JOB_FINISHED code, JobStart has already 746 * called Make_Update and freed the job descriptor. We set 747 * done to false here to avoid fake cycles and double frees. 748 * JobStart needs to do the update so we can proceed up the 749 * graph when given the -n flag.. 750 */ 751 done = FALSE; 752 break; 753 } 754 } else { 755 done = TRUE; 756 } 757 758 759 if (done && 760 (aborting != ABORT_ERROR) && 761 (aborting != ABORT_INTERRUPT) && 762 (status.w_status == 0)) 763 { 764 /* 765 * As long as we aren't aborting and the job didn't return a non-zero 766 * status that we shouldn't ignore, we call Make_Update to update 767 * the parents. In addition, any saved commands for the node are placed 768 * on the .END target. 769 */ 770 if (job->tailCmds != NILLNODE) { 771 Lst_ForEachFrom (job->node->commands, job->tailCmds, 772 JobSaveCommand, 773 (ClientData)job->node); 774 } 775 job->node->made = MADE; 776 Make_Update (job->node); 777 free((Address)job); 778 } else if (status.w_status) { 779 errors += 1; 780 free((Address)job); 781 } 782 783 while (!errors && !jobFull && !Lst_IsEmpty(stoppedJobs)) { 784 JobRestart((Job *)Lst_DeQueue(stoppedJobs)); 785 } 786 787 /* 788 * Set aborting if any error. 789 */ 790 if (errors && !keepgoing && (aborting != ABORT_INTERRUPT)) { 791 /* 792 * If we found any errors in this batch of children and the -k flag 793 * wasn't given, we set the aborting flag so no more jobs get 794 * started. 795 */ 796 aborting = ABORT_ERROR; 797 } 798 799 if ((aborting == ABORT_ERROR) && Job_Empty()) { 800 /* 801 * If we are aborting and the job table is now empty, we finish. 802 */ 803 (void) unlink (tfile); 804 Finish (errors); 805 } 806 } 807 808 /*- 809 *----------------------------------------------------------------------- 810 * Job_Touch -- 811 * Touch the given target. Called by JobStart when the -t flag was 812 * given 813 * 814 * Results: 815 * None 816 * 817 * Side Effects: 818 * The data modification of the file is changed. In addition, if the 819 * file did not exist, it is created. 820 *----------------------------------------------------------------------- 821 */ 822 void 823 Job_Touch (gn, silent) 824 GNode *gn; /* the node of the file to touch */ 825 Boolean silent; /* TRUE if should not print messages */ 826 { 827 int streamID; /* ID of stream opened to do the touch */ 828 struct timeval times[2]; /* Times for utimes() call */ 829 struct stat attr; /* Attributes of the file */ 830 831 if (gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_OPTIONAL)) { 832 /* 833 * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" targets 834 * and, as such, shouldn't really be created. 835 */ 836 return; 837 } 838 839 if (!silent) { 840 printf ("touch %s\n", gn->name); 841 } 842 843 if (noExecute) { 844 return; 845 } 846 847 if (gn->type & OP_ARCHV) { 848 Arch_Touch (gn); 849 } else if (gn->type & OP_LIB) { 850 Arch_TouchLib (gn); 851 } else { 852 char *file = gn->path ? gn->path : gn->name; 853 854 times[0].tv_sec = times[1].tv_sec = now; 855 times[0].tv_usec = times[1].tv_usec = 0; 856 if (utimes(file, times) < 0){ 857 streamID = open (file, O_RDWR | O_CREAT, 0666); 858 859 if (streamID >= 0) { 860 char c; 861 862 /* 863 * Read and write a byte to the file to change the 864 * modification time, then close the file. 865 */ 866 if (read(streamID, &c, 1) == 1) { 867 lseek(streamID, 0L, L_SET); 868 write(streamID, &c, 1); 869 } 870 871 (void)close (streamID); 872 } else 873 printf("*** couldn't touch %s: %s", file, strerror(errno)); 874 } 875 } 876 } 877 878 /*- 879 *----------------------------------------------------------------------- 880 * Job_CheckCommands -- 881 * Make sure the given node has all the commands it needs. 882 * 883 * Results: 884 * TRUE if the commands list is/was ok. 885 * 886 * Side Effects: 887 * The node will have commands from the .DEFAULT rule added to it 888 * if it needs them. 889 *----------------------------------------------------------------------- 890 */ 891 Boolean 892 Job_CheckCommands (gn, abortProc) 893 GNode *gn; /* The target whose commands need 894 * verifying */ 895 void (*abortProc)(); /* Function to abort with message */ 896 { 897 if (OP_NOP(gn->type) && Lst_IsEmpty (gn->commands) && 898 (gn->type & OP_LIB) == 0) { 899 /* 900 * No commands. Look for .DEFAULT rule from which we might infer 901 * commands 902 */ 903 if ((DEFAULT != NILGNODE) && !Lst_IsEmpty(DEFAULT->commands)) { 904 /* 905 * Make only looks for a .DEFAULT if the node was never the 906 * target of an operator, so that's what we do too. If 907 * a .DEFAULT was given, we substitute its commands for gn's 908 * commands and set the IMPSRC variable to be the target's name 909 * The DEFAULT node acts like a transformation rule, in that 910 * gn also inherits any attributes or sources attached to 911 * .DEFAULT itself. 912 */ 913 Make_HandleUse(DEFAULT, gn); 914 Var_Set (IMPSRC, Var_Value (TARGET, gn), gn); 915 } else if (Dir_MTime (gn) == 0) { 916 /* 917 * The node wasn't the target of an operator we have no .DEFAULT 918 * rule to go on and the target doesn't already exist. There's 919 * nothing more we can do for this branch. If the -k flag wasn't 920 * given, we stop in our tracks, otherwise we just don't update 921 * this node's parents so they never get examined. 922 */ 923 if (gn->type & OP_OPTIONAL) { 924 printf ("make: don't know how to make %s (ignored)\n", 925 gn->name); 926 } else if (keepgoing) { 927 printf ("make: don't know how to make %s (continuing)\n", 928 gn->name); 929 return (FALSE); 930 } else { 931 (*abortProc) ("make: don't know how to make %s. Stop", 932 gn->name); 933 return(FALSE); 934 } 935 } 936 } 937 return (TRUE); 938 } 939 #ifdef RMT_WILL_WATCH 940 /*- 941 *----------------------------------------------------------------------- 942 * JobLocalInput -- 943 * Handle a pipe becoming readable. Callback function for Rmt_Watch 944 * 945 * Results: 946 * None 947 * 948 * Side Effects: 949 * JobDoOutput is called. 950 * 951 *----------------------------------------------------------------------- 952 */ 953 /*ARGSUSED*/ 954 static void 955 JobLocalInput(stream, job) 956 int stream; /* Stream that's ready (ignored) */ 957 Job *job; /* Job to which the stream belongs */ 958 { 959 JobDoOutput(job, FALSE); 960 } 961 #endif /* RMT_WILL_WATCH */ 962 963 /*- 964 *----------------------------------------------------------------------- 965 * JobExec -- 966 * Execute the shell for the given job. Called from JobStart and 967 * JobRestart. 968 * 969 * Results: 970 * None. 971 * 972 * Side Effects: 973 * A shell is executed, outputs is altered and the Job structure added 974 * to the job table. 975 * 976 *----------------------------------------------------------------------- 977 */ 978 static void 979 JobExec(job, argv) 980 Job *job; /* Job to execute */ 981 char **argv; 982 { 983 int cpid; /* ID of new child */ 984 985 if (DEBUG(JOB)) { 986 int i; 987 988 printf("Running %s %sly\n", job->node->name, 989 job->flags&JOB_REMOTE?"remote":"local"); 990 printf("\tCommand: "); 991 for (i = 0; argv[i] != (char *)NULL; i++) { 992 printf("%s ", argv[i]); 993 } 994 printf("\n"); 995 } 996 997 /* 998 * Some jobs produce no output and it's disconcerting to have 999 * no feedback of their running (since they produce no output, the 1000 * banner with their name in it never appears). This is an attempt to 1001 * provide that feedback, even if nothing follows it. 1002 */ 1003 if ((lastNode != job->node) && (job->flags & JOB_FIRST) && 1004 !(job->flags & JOB_SILENT)) 1005 { 1006 printf(targFmt, job->node->name); 1007 lastNode = job->node; 1008 } 1009 1010 #ifdef RMT_NO_EXEC 1011 if (job->flags & JOB_REMOTE) { 1012 goto jobExecFinish; 1013 } 1014 #endif /* RMT_NO_EXEC */ 1015 1016 if ((cpid = vfork()) == -1) { 1017 Punt ("Cannot fork"); 1018 } else if (cpid == 0) { 1019 1020 /* 1021 * Must duplicate the input stream down to the child's input and 1022 * reset it to the beginning (again). Since the stream was marked 1023 * close-on-exec, we must clear that bit in the new input. 1024 */ 1025 (void) dup2(fileno(job->cmdFILE), 0); 1026 fcntl(0, F_SETFD, 0); 1027 lseek(0, 0, L_SET); 1028 1029 if (usePipes) { 1030 /* 1031 * Set up the child's output to be routed through the pipe 1032 * we've created for it. 1033 */ 1034 (void) dup2 (job->outPipe, 1); 1035 } else { 1036 /* 1037 * We're capturing output in a file, so we duplicate the 1038 * descriptor to the temporary file into the standard 1039 * output. 1040 */ 1041 (void) dup2 (job->outFd, 1); 1042 } 1043 /* 1044 * The output channels are marked close on exec. This bit was 1045 * duplicated by the dup2 (on some systems), so we have to clear 1046 * it before routing the shell's error output to the same place as 1047 * its standard output. 1048 */ 1049 fcntl(1, F_SETFD, 0); 1050 (void) dup2 (1, 2); 1051 1052 #ifdef USE_PGRP 1053 /* 1054 * We want to switch the child into a different process family so 1055 * we can kill it and all its descendants in one fell swoop, 1056 * by killing its process family, but not commit suicide. 1057 */ 1058 1059 (void) setpgrp(0, getpid()); 1060 #endif USE_PGRP 1061 1062 (void) execv (shellPath, argv); 1063 (void) write (2, "Could not execute shell\n", 1064 sizeof ("Could not execute shell")); 1065 _exit (1); 1066 } else { 1067 job->pid = cpid; 1068 1069 if (usePipes && (job->flags & JOB_FIRST) ) { 1070 /* 1071 * The first time a job is run for a node, we set the current 1072 * position in the buffer to the beginning and mark another 1073 * stream to watch in the outputs mask 1074 */ 1075 job->curPos = 0; 1076 1077 #ifdef RMT_WILL_WATCH 1078 Rmt_Watch(job->inPipe, JobLocalInput, job); 1079 #else 1080 FD_SET(job->inPipe, &outputs); 1081 #endif /* RMT_WILL_WATCH */ 1082 } 1083 1084 if (job->flags & JOB_REMOTE) { 1085 job->rmtID = (char *)0; 1086 } else { 1087 nLocal += 1; 1088 /* 1089 * XXX: Used to not happen if CUSTOMS. Why? 1090 */ 1091 if (job->cmdFILE != stdout) { 1092 fclose(job->cmdFILE); 1093 job->cmdFILE = NULL; 1094 } 1095 } 1096 } 1097 1098 jobExecFinish: 1099 /* 1100 * Now the job is actually running, add it to the table. 1101 */ 1102 nJobs += 1; 1103 (void)Lst_AtEnd (jobs, (ClientData)job); 1104 if (nJobs == maxJobs) { 1105 jobFull = TRUE; 1106 } 1107 } 1108 1109 /*- 1110 *----------------------------------------------------------------------- 1111 * JobMakeArgv -- 1112 * Create the argv needed to execute the shell for a given job. 1113 * 1114 * 1115 * Results: 1116 * 1117 * Side Effects: 1118 * 1119 *----------------------------------------------------------------------- 1120 */ 1121 static void 1122 JobMakeArgv(job, argv) 1123 Job *job; 1124 char **argv; 1125 { 1126 int argc; 1127 static char args[10]; /* For merged arguments */ 1128 1129 argv[0] = shellName; 1130 argc = 1; 1131 1132 if ((commandShell->exit && (*commandShell->exit != '-')) || 1133 (commandShell->echo && (*commandShell->echo != '-'))) 1134 { 1135 /* 1136 * At least one of the flags doesn't have a minus before it, so 1137 * merge them together. Have to do this because the *(&(@*#*&#$# 1138 * Bourne shell thinks its second argument is a file to source. 1139 * Grrrr. Note the ten-character limitation on the combined arguments. 1140 */ 1141 (void)sprintf(args, "-%s%s", 1142 ((job->flags & JOB_IGNERR) ? "" : 1143 (commandShell->exit ? commandShell->exit : "")), 1144 ((job->flags & JOB_SILENT) ? "" : 1145 (commandShell->echo ? commandShell->echo : ""))); 1146 1147 if (args[1]) { 1148 argv[argc] = args; 1149 argc++; 1150 } 1151 } else { 1152 if (!(job->flags & JOB_IGNERR) && commandShell->exit) { 1153 argv[argc] = commandShell->exit; 1154 argc++; 1155 } 1156 if (!(job->flags & JOB_SILENT) && commandShell->echo) { 1157 argv[argc] = commandShell->echo; 1158 argc++; 1159 } 1160 } 1161 argv[argc] = (char *)NULL; 1162 } 1163 1164 /*- 1165 *----------------------------------------------------------------------- 1166 * JobRestart -- 1167 * Restart a job that stopped for some reason. 1168 * 1169 * Results: 1170 * None. 1171 * 1172 * Side Effects: 1173 * jobFull will be set if the job couldn't be run. 1174 * 1175 *----------------------------------------------------------------------- 1176 */ 1177 static void 1178 JobRestart(job) 1179 Job *job; /* Job to restart */ 1180 { 1181 if (job->flags & JOB_REMIGRATE) { 1182 if (DEBUG(JOB)) { 1183 printf("Remigrating %x\n", job->pid); 1184 } 1185 if (nLocal != maxLocal) { 1186 /* 1187 * Job cannot be remigrated, but there's room on the local 1188 * machine, so resume the job and note that another 1189 * local job has started. 1190 */ 1191 if (DEBUG(JOB)) { 1192 printf("resuming on local machine\n"); 1193 } 1194 KILL(job->pid, SIGCONT); 1195 nLocal +=1; 1196 job->flags &= ~(JOB_REMIGRATE|JOB_RESUME); 1197 } else { 1198 /* 1199 * Job cannot be restarted. Mark the table as full and 1200 * place the job back on the list of stopped jobs. 1201 */ 1202 if (DEBUG(JOB)) { 1203 printf("holding\n"); 1204 } 1205 (void)Lst_AtFront(stoppedJobs, (ClientData)job); 1206 jobFull = TRUE; 1207 if (DEBUG(JOB)) { 1208 printf("Job queue is full.\n"); 1209 } 1210 return; 1211 } 1212 1213 (void)Lst_AtEnd(jobs, (ClientData)job); 1214 nJobs += 1; 1215 if (nJobs == maxJobs) { 1216 jobFull = TRUE; 1217 if (DEBUG(JOB)) { 1218 printf("Job queue is full.\n"); 1219 } 1220 } 1221 } else if (job->flags & JOB_RESTART) { 1222 /* 1223 * Set up the control arguments to the shell. This is based on the 1224 * flags set earlier for this job. If the JOB_IGNERR flag is clear, 1225 * the 'exit' flag of the commandShell is used to cause it to exit 1226 * upon receiving an error. If the JOB_SILENT flag is clear, the 1227 * 'echo' flag of the commandShell is used to get it to start echoing 1228 * as soon as it starts processing commands. 1229 */ 1230 char *argv[4]; 1231 1232 JobMakeArgv(job, argv); 1233 1234 if (DEBUG(JOB)) { 1235 printf("Restarting %s...", job->node->name); 1236 } 1237 if (((nLocal >= maxLocal) && ! (job->flags & JOB_SPECIAL))) { 1238 /* 1239 * Can't be exported and not allowed to run locally -- put it 1240 * back on the hold queue and mark the table full 1241 */ 1242 if (DEBUG(JOB)) { 1243 printf("holding\n"); 1244 } 1245 (void)Lst_AtFront(stoppedJobs, (ClientData)job); 1246 jobFull = TRUE; 1247 if (DEBUG(JOB)) { 1248 printf("Job queue is full.\n"); 1249 } 1250 return; 1251 } else { 1252 /* 1253 * Job may be run locally. 1254 */ 1255 if (DEBUG(JOB)) { 1256 printf("running locally\n"); 1257 } 1258 job->flags &= ~JOB_REMOTE; 1259 } 1260 JobExec(job, argv); 1261 } else { 1262 /* 1263 * The job has stopped and needs to be restarted. Why it stopped, 1264 * we don't know... 1265 */ 1266 if (DEBUG(JOB)) { 1267 printf("Resuming %s...", job->node->name); 1268 } 1269 if (((job->flags & JOB_REMOTE) || 1270 (nLocal < maxLocal) || 1271 (((job->flags & JOB_SPECIAL)) && 1272 (maxLocal == 0))) && 1273 (nJobs != maxJobs)) 1274 { 1275 /* 1276 * If the job is remote, it's ok to resume it as long as the 1277 * maximum concurrency won't be exceeded. If it's local and 1278 * we haven't reached the local concurrency limit already (or the 1279 * job must be run locally and maxLocal is 0), it's also ok to 1280 * resume it. 1281 */ 1282 Boolean error; 1283 extern int errno; 1284 union wait status; 1285 1286 #ifdef RMT_WANTS_SIGNALS 1287 if (job->flags & JOB_REMOTE) { 1288 error = !Rmt_Signal(job, SIGCONT); 1289 } else 1290 #endif /* RMT_WANTS_SIGNALS */ 1291 error = (KILL(job->pid, SIGCONT) != 0); 1292 1293 if (!error) { 1294 /* 1295 * Make sure the user knows we've continued the beast and 1296 * actually put the thing in the job table. 1297 */ 1298 job->flags |= JOB_CONTINUING; 1299 status.w_termsig = SIGCONT; 1300 JobFinish(job, status); 1301 1302 job->flags &= ~(JOB_RESUME|JOB_CONTINUING); 1303 if (DEBUG(JOB)) { 1304 printf("done\n"); 1305 } 1306 } else { 1307 Error("couldn't resume %s: %s", 1308 job->node->name, strerror(errno)); 1309 status.w_status = 0; 1310 status.w_retcode = 1; 1311 JobFinish(job, status); 1312 } 1313 } else { 1314 /* 1315 * Job cannot be restarted. Mark the table as full and 1316 * place the job back on the list of stopped jobs. 1317 */ 1318 if (DEBUG(JOB)) { 1319 printf("table full\n"); 1320 } 1321 (void)Lst_AtFront(stoppedJobs, (ClientData)job); 1322 jobFull = TRUE; 1323 if (DEBUG(JOB)) { 1324 printf("Job queue is full.\n"); 1325 } 1326 } 1327 } 1328 } 1329 1330 /*- 1331 *----------------------------------------------------------------------- 1332 * JobStart -- 1333 * Start a target-creation process going for the target described 1334 * by the graph node gn. 1335 * 1336 * Results: 1337 * JOB_ERROR if there was an error in the commands, JOB_FINISHED 1338 * if there isn't actually anything left to do for the job and 1339 * JOB_RUNNING if the job has been started. 1340 * 1341 * Side Effects: 1342 * A new Job node is created and added to the list of running 1343 * jobs. PMake is forked and a child shell created. 1344 *----------------------------------------------------------------------- 1345 */ 1346 static int 1347 JobStart (gn, flags, previous) 1348 GNode *gn; /* target to create */ 1349 short flags; /* flags for the job to override normal ones. 1350 * e.g. JOB_SPECIAL or JOB_IGNDOTS */ 1351 Job *previous; /* The previous Job structure for this node, 1352 * if any. */ 1353 { 1354 register Job *job; /* new job descriptor */ 1355 char *argv[4]; /* Argument vector to shell */ 1356 char args[5]; /* arguments to shell */ 1357 static int jobno = 0; /* job number of catching output in a file */ 1358 Boolean cmdsOK; /* true if the nodes commands were all right */ 1359 Boolean local; /* Set true if the job was run locally */ 1360 Boolean noExec; /* Set true if we decide not to run the job */ 1361 1362 if (previous != (Job *)NULL) { 1363 previous->flags &= ~ (JOB_FIRST|JOB_IGNERR|JOB_SILENT|JOB_REMOTE); 1364 job = previous; 1365 } else { 1366 job = (Job *) emalloc (sizeof (Job)); 1367 if (job == (Job *)NULL) { 1368 Punt("JobStart out of memory"); 1369 } 1370 flags |= JOB_FIRST; 1371 } 1372 1373 job->node = gn; 1374 job->tailCmds = NILLNODE; 1375 1376 /* 1377 * Set the initial value of the flags for this job based on the global 1378 * ones and the node's attributes... Any flags supplied by the caller 1379 * are also added to the field. 1380 */ 1381 job->flags = 0; 1382 if (Targ_Ignore (gn)) { 1383 job->flags |= JOB_IGNERR; 1384 } 1385 if (Targ_Silent (gn)) { 1386 job->flags |= JOB_SILENT; 1387 } 1388 job->flags |= flags; 1389 1390 /* 1391 * Check the commands now so any attributes from .DEFAULT have a chance 1392 * to migrate to the node 1393 */ 1394 if (job->flags & JOB_FIRST) { 1395 cmdsOK = Job_CheckCommands(gn, Error); 1396 } else { 1397 cmdsOK = TRUE; 1398 } 1399 1400 /* 1401 * If the -n flag wasn't given, we open up OUR (not the child's) 1402 * temporary file to stuff commands in it. The thing is rd/wr so we don't 1403 * need to reopen it to feed it to the shell. If the -n flag *was* given, 1404 * we just set the file to be stdout. Cute, huh? 1405 */ 1406 if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) { 1407 /* 1408 * We're serious here, but if the commands were bogus, we're 1409 * also dead... 1410 */ 1411 if (!cmdsOK) { 1412 DieHorribly(); 1413 } 1414 1415 job->cmdFILE = fopen (tfile, "w+"); 1416 if (job->cmdFILE == (FILE *) NULL) { 1417 Punt ("Could not open %s", tfile); 1418 } 1419 fcntl(fileno(job->cmdFILE), F_SETFD, 1); 1420 /* 1421 * Send the commands to the command file, flush all its buffers then 1422 * rewind and remove the thing. 1423 */ 1424 noExec = FALSE; 1425 1426 /* 1427 * used to be backwards; replace when start doing multiple commands 1428 * per shell. 1429 */ 1430 if (1) { 1431 /* 1432 * Be compatible: If this is the first time for this node, 1433 * verify its commands are ok and open the commands list for 1434 * sequential access by later invocations of JobStart. 1435 * Once that is done, we take the next command off the list 1436 * and print it to the command file. If the command was an 1437 * ellipsis, note that there's nothing more to execute. 1438 */ 1439 if ((job->flags&JOB_FIRST) && (Lst_Open(gn->commands) != SUCCESS)){ 1440 cmdsOK = FALSE; 1441 } else { 1442 LstNode ln = Lst_Next (gn->commands); 1443 1444 if ((ln == NILLNODE) || 1445 JobPrintCommand ((char *)Lst_Datum (ln), job)) 1446 { 1447 noExec = TRUE; 1448 Lst_Close (gn->commands); 1449 } 1450 if (noExec && !(job->flags & JOB_FIRST)) { 1451 /* 1452 * If we're not going to execute anything, the job 1453 * is done and we need to close down the various 1454 * file descriptors we've opened for output, then 1455 * call JobDoOutput to catch the final characters or 1456 * send the file to the screen... Note that the i/o streams 1457 * are only open if this isn't the first job. 1458 * Note also that this could not be done in 1459 * Job_CatchChildren b/c it wasn't clear if there were 1460 * more commands to execute or not... 1461 */ 1462 if (usePipes) { 1463 #ifdef RMT_WILL_WATCH 1464 Rmt_Ignore(job->inPipe); 1465 #else 1466 FD_CLR(job->inPipe, &outputs); 1467 #endif 1468 if (job->outPipe != job->inPipe) { 1469 (void)close (job->outPipe); 1470 } 1471 JobDoOutput (job, TRUE); 1472 (void)close (job->inPipe); 1473 } else { 1474 (void)close (job->outFd); 1475 JobDoOutput (job, TRUE); 1476 } 1477 } 1478 } 1479 } else { 1480 /* 1481 * We can do all the commands at once. hooray for sanity 1482 */ 1483 numCommands = 0; 1484 Lst_ForEach (gn->commands, JobPrintCommand, (ClientData)job); 1485 1486 /* 1487 * If we didn't print out any commands to the shell script, 1488 * there's not much point in executing the shell, is there? 1489 */ 1490 if (numCommands == 0) { 1491 noExec = TRUE; 1492 } 1493 } 1494 } else if (noExecute) { 1495 /* 1496 * Not executing anything -- just print all the commands to stdout 1497 * in one fell swoop. This will still set up job->tailCmds correctly. 1498 */ 1499 if (lastNode != gn) { 1500 printf (targFmt, gn->name); 1501 lastNode = gn; 1502 } 1503 job->cmdFILE = stdout; 1504 /* 1505 * Only print the commands if they're ok, but don't die if they're 1506 * not -- just let the user know they're bad and keep going. It 1507 * doesn't do any harm in this case and may do some good. 1508 */ 1509 if (cmdsOK) { 1510 Lst_ForEach(gn->commands, JobPrintCommand, (ClientData)job); 1511 } 1512 /* 1513 * Don't execute the shell, thank you. 1514 */ 1515 noExec = TRUE; 1516 } else { 1517 /* 1518 * Just touch the target and note that no shell should be executed. 1519 * Set cmdFILE to stdout to make life easier. Check the commands, too, 1520 * but don't die if they're no good -- it does no harm to keep working 1521 * up the graph. 1522 */ 1523 job->cmdFILE = stdout; 1524 Job_Touch (gn, job->flags&JOB_SILENT); 1525 noExec = TRUE; 1526 } 1527 1528 /* 1529 * If we're not supposed to execute a shell, don't. 1530 */ 1531 if (noExec) { 1532 /* 1533 * Unlink and close the command file if we opened one 1534 */ 1535 if (job->cmdFILE != stdout) { 1536 (void) unlink (tfile); 1537 fclose(job->cmdFILE); 1538 } else { 1539 fflush (stdout); 1540 } 1541 1542 /* 1543 * We only want to work our way up the graph if we aren't here because 1544 * the commands for the job were no good. 1545 */ 1546 if (cmdsOK) { 1547 if (aborting == 0) { 1548 if (job->tailCmds != NILLNODE) { 1549 Lst_ForEachFrom(job->node->commands, job->tailCmds, 1550 JobSaveCommand, 1551 (ClientData)job->node); 1552 } 1553 Make_Update(job->node); 1554 } 1555 free((Address)job); 1556 return(JOB_FINISHED); 1557 } else { 1558 free((Address)job); 1559 return(JOB_ERROR); 1560 } 1561 } else { 1562 fflush (job->cmdFILE); 1563 (void) unlink (tfile); 1564 } 1565 1566 /* 1567 * Set up the control arguments to the shell. This is based on the flags 1568 * set earlier for this job. 1569 */ 1570 JobMakeArgv(job, argv); 1571 1572 /* 1573 * If we're using pipes to catch output, create the pipe by which we'll 1574 * get the shell's output. If we're using files, print out that we're 1575 * starting a job and then set up its temporary-file name. This is just 1576 * tfile with two extra digits tacked on -- jobno. 1577 */ 1578 if (job->flags & JOB_FIRST) { 1579 if (usePipes) { 1580 int fd[2]; 1581 (void) pipe(fd); 1582 job->inPipe = fd[0]; 1583 job->outPipe = fd[1]; 1584 (void)fcntl (job->inPipe, F_SETFD, 1); 1585 (void)fcntl (job->outPipe, F_SETFD, 1); 1586 } else { 1587 printf ("Remaking `%s'\n", gn->name); 1588 fflush (stdout); 1589 sprintf (job->outFile, "%s%02d", tfile, jobno); 1590 jobno = (jobno + 1) % 100; 1591 job->outFd = open(job->outFile,O_WRONLY|O_CREAT|O_APPEND,0600); 1592 (void)fcntl (job->outFd, F_SETFD, 1); 1593 } 1594 } 1595 1596 local = TRUE; 1597 1598 if (local && (((nLocal >= maxLocal) && 1599 !(job->flags & JOB_SPECIAL) && 1600 (maxLocal != 0)))) 1601 { 1602 /* 1603 * The job can only be run locally, but we've hit the limit of 1604 * local concurrency, so put the job on hold until some other job 1605 * finishes. Note that the special jobs (.BEGIN, .INTERRUPT and .END) 1606 * may be run locally even when the local limit has been reached 1607 * (e.g. when maxLocal == 0), though they will be exported if at 1608 * all possible. 1609 */ 1610 jobFull = TRUE; 1611 1612 if (DEBUG(JOB)) { 1613 printf("Can only run job locally.\n"); 1614 } 1615 job->flags |= JOB_RESTART; 1616 (void)Lst_AtEnd(stoppedJobs, (ClientData)job); 1617 } else { 1618 if ((nLocal >= maxLocal) && local) { 1619 /* 1620 * If we're running this job locally as a special case (see above), 1621 * at least say the table is full. 1622 */ 1623 jobFull = TRUE; 1624 if (DEBUG(JOB)) { 1625 printf("Local job queue is full.\n"); 1626 } 1627 } 1628 JobExec(job, argv); 1629 } 1630 return(JOB_RUNNING); 1631 } 1632 1633 /*- 1634 *----------------------------------------------------------------------- 1635 * JobDoOutput -- 1636 * This function is called at different times depending on 1637 * whether the user has specified that output is to be collected 1638 * via pipes or temporary files. In the former case, we are called 1639 * whenever there is something to read on the pipe. We collect more 1640 * output from the given job and store it in the job's outBuf. If 1641 * this makes up a line, we print it tagged by the job's identifier, 1642 * as necessary. 1643 * If output has been collected in a temporary file, we open the 1644 * file and read it line by line, transfering it to our own 1645 * output channel until the file is empty. At which point we 1646 * remove the temporary file. 1647 * In both cases, however, we keep our figurative eye out for the 1648 * 'noPrint' line for the shell from which the output came. If 1649 * we recognize a line, we don't print it. If the command is not 1650 * alone on the line (the character after it is not \0 or \n), we 1651 * do print whatever follows it. 1652 * 1653 * Results: 1654 * None 1655 * 1656 * Side Effects: 1657 * curPos may be shifted as may the contents of outBuf. 1658 *----------------------------------------------------------------------- 1659 */ 1660 void 1661 JobDoOutput (job, finish) 1662 register Job *job; /* the job whose output needs printing */ 1663 Boolean finish; /* TRUE if this is the last time we'll be 1664 * called for this job */ 1665 { 1666 Boolean gotNL = FALSE; /* true if got a newline */ 1667 register int nr; /* number of bytes read */ 1668 register int i; /* auxiliary index into outBuf */ 1669 register int max; /* limit for i (end of current data) */ 1670 int nRead; /* (Temporary) number of bytes read */ 1671 1672 char c; /* character after noPrint string */ 1673 FILE *oFILE; /* Stream pointer to shell's output file */ 1674 char inLine[132]; 1675 1676 1677 if (usePipes) { 1678 /* 1679 * Read as many bytes as will fit in the buffer. 1680 */ 1681 end_loop: 1682 1683 nRead = read (job->inPipe, &job->outBuf[job->curPos], 1684 JOB_BUFSIZE - job->curPos); 1685 if (nRead < 0) { 1686 if (DEBUG(JOB)) { 1687 perror("JobDoOutput(piperead)"); 1688 } 1689 nr = 0; 1690 } else { 1691 nr = nRead; 1692 } 1693 1694 /* 1695 * If we hit the end-of-file (the job is dead), we must flush its 1696 * remaining output, so pretend we read a newline if there's any 1697 * output remaining in the buffer. 1698 * Also clear the 'finish' flag so we stop looping. 1699 */ 1700 if ((nr == 0) && (job->curPos != 0)) { 1701 job->outBuf[job->curPos] = '\n'; 1702 nr = 1; 1703 finish = FALSE; 1704 } else if (nr == 0) { 1705 finish = FALSE; 1706 } 1707 1708 /* 1709 * Look for the last newline in the bytes we just got. If there is 1710 * one, break out of the loop with 'i' as its index and gotNL set 1711 * TRUE. 1712 */ 1713 max = job->curPos + nr; 1714 for (i = job->curPos + nr - 1; i >= job->curPos; i--) { 1715 if (job->outBuf[i] == '\n') { 1716 gotNL = TRUE; 1717 break; 1718 } else if (job->outBuf[i] == '\0') { 1719 /* 1720 * Why? 1721 */ 1722 job->outBuf[i] = ' '; 1723 } 1724 } 1725 1726 if (!gotNL) { 1727 job->curPos += nr; 1728 if (job->curPos == JOB_BUFSIZE) { 1729 /* 1730 * If we've run out of buffer space, we have no choice 1731 * but to print the stuff. sigh. 1732 */ 1733 gotNL = TRUE; 1734 i = job->curPos; 1735 } 1736 } 1737 if (gotNL) { 1738 /* 1739 * Need to send the output to the screen. Null terminate it 1740 * first, overwriting the newline character if there was one. 1741 * So long as the line isn't one we should filter (according 1742 * to the shell description), we print the line, preceeded 1743 * by a target banner if this target isn't the same as the 1744 * one for which we last printed something. 1745 * The rest of the data in the buffer are then shifted down 1746 * to the start of the buffer and curPos is set accordingly. 1747 */ 1748 job->outBuf[i] = '\0'; 1749 if (i >= job->curPos) { 1750 register char *cp, *ecp; 1751 1752 cp = job->outBuf; 1753 if (commandShell->noPrint) { 1754 ecp = Str_FindSubstring(job->outBuf, 1755 commandShell->noPrint); 1756 while (ecp != (char *)NULL) { 1757 if (cp != ecp) { 1758 *ecp = '\0'; 1759 if (job->node != lastNode) { 1760 printf (targFmt, job->node->name); 1761 lastNode = job->node; 1762 } 1763 /* 1764 * The only way there wouldn't be a newline after 1765 * this line is if it were the last in the buffer. 1766 * however, since the non-printable comes after it, 1767 * there must be a newline, so we don't print one. 1768 */ 1769 printf ("%s", cp); 1770 } 1771 cp = ecp + commandShell->noPLen; 1772 if (cp != &job->outBuf[i]) { 1773 /* 1774 * Still more to print, look again after skipping 1775 * the whitespace following the non-printable 1776 * command.... 1777 */ 1778 cp++; 1779 while (*cp == ' ' || *cp == '\t' || *cp == '\n') { 1780 cp++; 1781 } 1782 ecp = Str_FindSubstring (cp, 1783 commandShell->noPrint); 1784 } else { 1785 break; 1786 } 1787 } 1788 } 1789 1790 /* 1791 * There's still more in that thar buffer. This time, though, 1792 * we know there's no newline at the end, so we add one of 1793 * our own free will. 1794 */ 1795 if (*cp != '\0') { 1796 if (job->node != lastNode) { 1797 printf (targFmt, job->node->name); 1798 lastNode = job->node; 1799 } 1800 printf ("%s\n", cp); 1801 } 1802 1803 fflush (stdout); 1804 } 1805 if (i < max - 1) { 1806 bcopy (&job->outBuf[i + 1], /* shift the remaining */ 1807 job->outBuf, /* characters down */ 1808 max - (i + 1)); 1809 job->curPos = max - (i + 1); 1810 1811 } else { 1812 /* 1813 * We have written everything out, so we just start over 1814 * from the start of the buffer. No copying. No nothing. 1815 */ 1816 job->curPos = 0; 1817 } 1818 } 1819 if (finish) { 1820 /* 1821 * If the finish flag is true, we must loop until we hit 1822 * end-of-file on the pipe. This is guaranteed to happen eventually 1823 * since the other end of the pipe is now closed (we closed it 1824 * explicitly and the child has exited). When we do get an EOF, 1825 * finish will be set FALSE and we'll fall through and out. 1826 */ 1827 goto end_loop; 1828 } 1829 } else { 1830 /* 1831 * We've been called to retrieve the output of the job from the 1832 * temporary file where it's been squirreled away. This consists of 1833 * opening the file, reading the output line by line, being sure not 1834 * to print the noPrint line for the shell we used, then close and 1835 * remove the temporary file. Very simple. 1836 * 1837 * Change to read in blocks and do FindSubString type things as for 1838 * pipes? That would allow for "@echo -n..." 1839 */ 1840 oFILE = fopen (job->outFile, "r"); 1841 if (oFILE != (FILE *) NULL) { 1842 printf ("Results of making %s:\n", job->node->name); 1843 while (fgets (inLine, sizeof(inLine), oFILE) != NULL) { 1844 register char *cp, *ecp, *endp; 1845 1846 cp = inLine; 1847 endp = inLine + strlen(inLine); 1848 if (endp[-1] == '\n') { 1849 *--endp = '\0'; 1850 } 1851 if (commandShell->noPrint) { 1852 ecp = Str_FindSubstring(cp, commandShell->noPrint); 1853 while (ecp != (char *)NULL) { 1854 if (cp != ecp) { 1855 *ecp = '\0'; 1856 /* 1857 * The only way there wouldn't be a newline after 1858 * this line is if it were the last in the buffer. 1859 * however, since the non-printable comes after it, 1860 * there must be a newline, so we don't print one. 1861 */ 1862 printf ("%s", cp); 1863 } 1864 cp = ecp + commandShell->noPLen; 1865 if (cp != endp) { 1866 /* 1867 * Still more to print, look again after skipping 1868 * the whitespace following the non-printable 1869 * command.... 1870 */ 1871 cp++; 1872 while (*cp == ' ' || *cp == '\t' || *cp == '\n') { 1873 cp++; 1874 } 1875 ecp = Str_FindSubstring(cp, commandShell->noPrint); 1876 } else { 1877 break; 1878 } 1879 } 1880 } 1881 1882 /* 1883 * There's still more in that thar buffer. This time, though, 1884 * we know there's no newline at the end, so we add one of 1885 * our own free will. 1886 */ 1887 if (*cp != '\0') { 1888 printf ("%s\n", cp); 1889 } 1890 } 1891 fclose (oFILE); 1892 (void) unlink (job->outFile); 1893 } 1894 } 1895 fflush(stdout); 1896 } 1897 1898 /*- 1899 *----------------------------------------------------------------------- 1900 * Job_CatchChildren -- 1901 * Handle the exit of a child. Called from Make_Make. 1902 * 1903 * Results: 1904 * none. 1905 * 1906 * Side Effects: 1907 * The job descriptor is removed from the list of children. 1908 * 1909 * Notes: 1910 * We do waits, blocking or not, according to the wisdom of our 1911 * caller, until there are no more children to report. For each 1912 * job, call JobFinish to finish things off. This will take care of 1913 * putting jobs on the stoppedJobs queue. 1914 * 1915 *----------------------------------------------------------------------- 1916 */ 1917 void 1918 Job_CatchChildren (block) 1919 Boolean block; /* TRUE if should block on the wait. */ 1920 { 1921 int pid; /* pid of dead child */ 1922 register Job *job; /* job descriptor for dead child */ 1923 LstNode jnode; /* list element for finding job */ 1924 union wait status; /* Exit/termination status */ 1925 1926 /* 1927 * Don't even bother if we know there's no one around. 1928 */ 1929 if (nLocal == 0) { 1930 return; 1931 } 1932 1933 while ((pid = wait3((int *)&status, (block?0:WNOHANG)|WUNTRACED, 1934 (struct rusage *)0)) > 0) 1935 { 1936 if (DEBUG(JOB)) 1937 printf("Process %d exited or stopped.\n", pid); 1938 1939 1940 jnode = Lst_Find (jobs, (ClientData)pid, JobCmpPid); 1941 1942 if (jnode == NILLNODE) { 1943 if (WIFSIGNALED(status) && (status.w_termsig == SIGCONT)) { 1944 jnode = Lst_Find(stoppedJobs, (ClientData)pid, JobCmpPid); 1945 if (jnode == NILLNODE) { 1946 Error("Resumed child (%d) not in table", pid); 1947 continue; 1948 } 1949 job = (Job *)Lst_Datum(jnode); 1950 (void)Lst_Remove(stoppedJobs, jnode); 1951 } else { 1952 Error ("Child (%d) not in table?", pid); 1953 continue; 1954 } 1955 } else { 1956 job = (Job *) Lst_Datum (jnode); 1957 (void)Lst_Remove (jobs, jnode); 1958 nJobs -= 1; 1959 if (jobFull && DEBUG(JOB)) { 1960 printf("Job queue is no longer full.\n"); 1961 } 1962 jobFull = FALSE; 1963 nLocal -= 1; 1964 } 1965 1966 JobFinish (job, status); 1967 } 1968 } 1969 1970 /*- 1971 *----------------------------------------------------------------------- 1972 * Job_CatchOutput -- 1973 * Catch the output from our children, if we're using 1974 * pipes do so. Otherwise just block time until we get a 1975 * signal (most likely a SIGCHLD) since there's no point in 1976 * just spinning when there's nothing to do and the reaping 1977 * of a child can wait for a while. 1978 * 1979 * Results: 1980 * None 1981 * 1982 * Side Effects: 1983 * Output is read from pipes if we're piping. 1984 * ----------------------------------------------------------------------- 1985 */ 1986 void 1987 Job_CatchOutput () 1988 { 1989 int nfds; 1990 struct timeval timeout; 1991 fd_set readfds; 1992 register LstNode ln; 1993 register Job *job; 1994 int pnJobs; /* Previous nJobs */ 1995 1996 fflush(stdout); 1997 #ifdef RMT_WILL_WATCH 1998 pnJobs = nJobs; 1999 2000 /* 2001 * It is possible for us to be called with nJobs equal to 0. This happens 2002 * if all the jobs finish and a job that is stopped cannot be run 2003 * locally (eg if maxLocal is 0) and cannot be exported. The job will 2004 * be placed back on the stoppedJobs queue, Job_Empty() will return false, 2005 * Make_Run will call us again when there's nothing for which to wait. 2006 * nJobs never changes, so we loop forever. Hence the check. It could 2007 * be argued that we should sleep for a bit so as not to swamp the 2008 * exportation system with requests. Perhaps we should. 2009 * 2010 * NOTE: IT IS THE RESPONSIBILITY OF Rmt_Wait TO CALL Job_CatchChildren 2011 * IN A TIMELY FASHION TO CATCH ANY LOCALLY RUNNING JOBS THAT EXIT. 2012 * It may use the variable nLocal to determine if it needs to call 2013 * Job_CatchChildren (if nLocal is 0, there's nothing for which to 2014 * wait...) 2015 */ 2016 while (nJobs != 0 && pnJobs == nJobs) { 2017 Rmt_Wait(); 2018 } 2019 #else 2020 if (usePipes) { 2021 readfds = outputs; 2022 timeout.tv_sec = SEL_SEC; 2023 timeout.tv_usec = SEL_USEC; 2024 2025 if ((nfds = select (FD_SETSIZE, &readfds, (int *) 0, (int *) 0, &timeout)) < 0) 2026 { 2027 return; 2028 } else { 2029 if (Lst_Open (jobs) == FAILURE) { 2030 Punt ("Cannot open job table"); 2031 } 2032 while (nfds && (ln = Lst_Next (jobs)) != NILLNODE) { 2033 job = (Job *) Lst_Datum (ln); 2034 if (FD_ISSET(job->inPipe, &readfds)) { 2035 JobDoOutput (job, FALSE); 2036 nfds -= 1; 2037 } 2038 } 2039 Lst_Close (jobs); 2040 } 2041 } 2042 #endif /* RMT_WILL_WATCH */ 2043 } 2044 2045 /*- 2046 *----------------------------------------------------------------------- 2047 * Job_Make -- 2048 * Start the creation of a target. Basically a front-end for 2049 * JobStart used by the Make module. 2050 * 2051 * Results: 2052 * None. 2053 * 2054 * Side Effects: 2055 * Another job is started. 2056 * 2057 *----------------------------------------------------------------------- 2058 */ 2059 void 2060 Job_Make (gn) 2061 GNode *gn; 2062 { 2063 (void)JobStart (gn, 0, (Job *)NULL); 2064 } 2065 2066 /*- 2067 *----------------------------------------------------------------------- 2068 * Job_Init -- 2069 * Initialize the process module 2070 * 2071 * Results: 2072 * none 2073 * 2074 * Side Effects: 2075 * lists and counters are initialized 2076 *----------------------------------------------------------------------- 2077 */ 2078 void 2079 Job_Init (maxproc, maxlocal) 2080 int maxproc; /* the greatest number of jobs which may be 2081 * running at one time */ 2082 int maxlocal; /* the greatest number of local jobs which may 2083 * be running at once. */ 2084 { 2085 GNode *begin; /* node for commands to do at the very start */ 2086 2087 sprintf (tfile, "/tmp/make%05d", getpid()); 2088 2089 jobs = Lst_Init (FALSE); 2090 stoppedJobs = Lst_Init(FALSE); 2091 maxJobs = maxproc; 2092 maxLocal = maxlocal; 2093 nJobs = 0; 2094 nLocal = 0; 2095 jobFull = FALSE; 2096 2097 aborting = 0; 2098 errors = 0; 2099 2100 lastNode = NILGNODE; 2101 2102 if (maxJobs == 1) { 2103 /* 2104 * If only one job can run at a time, there's no need for a banner, 2105 * no is there? 2106 */ 2107 targFmt = ""; 2108 } else { 2109 targFmt = TARG_FMT; 2110 } 2111 2112 if (shellPath == (char *) NULL) { 2113 /* 2114 * The user didn't specify a shell to use, so we are using the 2115 * default one... Both the absolute path and the last component 2116 * must be set. The last component is taken from the 'name' field 2117 * of the default shell description pointed-to by commandShell. 2118 * All default shells are located in _PATH_DEFSHELLDIR. 2119 */ 2120 shellName = commandShell->name; 2121 shellPath = str_concat (_PATH_DEFSHELLDIR, shellName, STR_ADDSLASH); 2122 } 2123 2124 if (commandShell->exit == (char *)NULL) { 2125 commandShell->exit = ""; 2126 } 2127 if (commandShell->echo == (char *)NULL) { 2128 commandShell->echo = ""; 2129 } 2130 2131 /* 2132 * Catch the four signals that POSIX specifies if they aren't ignored. 2133 * JobPassSig will take care of calling JobInterrupt if appropriate. 2134 */ 2135 if (signal (SIGINT, SIG_IGN) != SIG_IGN) { 2136 signal (SIGINT, JobPassSig); 2137 } 2138 if (signal (SIGHUP, SIG_IGN) != SIG_IGN) { 2139 signal (SIGHUP, JobPassSig); 2140 } 2141 if (signal (SIGQUIT, SIG_IGN) != SIG_IGN) { 2142 signal (SIGQUIT, JobPassSig); 2143 } 2144 if (signal (SIGTERM, SIG_IGN) != SIG_IGN) { 2145 signal (SIGTERM, JobPassSig); 2146 } 2147 /* 2148 * There are additional signals that need to be caught and passed if 2149 * either the export system wants to be told directly of signals or if 2150 * we're giving each job its own process group (since then it won't get 2151 * signals from the terminal driver as we own the terminal) 2152 */ 2153 #if defined(RMT_WANTS_SIGNALS) || defined(USE_PGRP) 2154 if (signal (SIGTSTP, SIG_IGN) != SIG_IGN) { 2155 signal (SIGTSTP, JobPassSig); 2156 } 2157 if (signal (SIGTTOU, SIG_IGN) != SIG_IGN) { 2158 signal (SIGTTOU, JobPassSig); 2159 } 2160 if (signal (SIGTTIN, SIG_IGN) != SIG_IGN) { 2161 signal (SIGTTIN, JobPassSig); 2162 } 2163 if (signal (SIGWINCH, SIG_IGN) != SIG_IGN) { 2164 signal (SIGWINCH, JobPassSig); 2165 } 2166 #endif 2167 2168 begin = Targ_FindNode (".BEGIN", TARG_NOCREATE); 2169 2170 if (begin != NILGNODE) { 2171 JobStart (begin, JOB_SPECIAL, (Job *)0); 2172 while (nJobs) { 2173 Job_CatchOutput(); 2174 #ifndef RMT_WILL_WATCH 2175 Job_CatchChildren (!usePipes); 2176 #endif /* RMT_WILL_WATCH */ 2177 } 2178 } 2179 postCommands = Targ_FindNode (".END", TARG_CREATE); 2180 } 2181 2182 /*- 2183 *----------------------------------------------------------------------- 2184 * Job_Full -- 2185 * See if the job table is full. It is considered full if it is OR 2186 * if we are in the process of aborting OR if we have 2187 * reached/exceeded our local quota. This prevents any more jobs 2188 * from starting up. 2189 * 2190 * Results: 2191 * TRUE if the job table is full, FALSE otherwise 2192 * Side Effects: 2193 * None. 2194 *----------------------------------------------------------------------- 2195 */ 2196 Boolean 2197 Job_Full () 2198 { 2199 return (aborting || jobFull); 2200 } 2201 2202 /*- 2203 *----------------------------------------------------------------------- 2204 * Job_Empty -- 2205 * See if the job table is empty. Because the local concurrency may 2206 * be set to 0, it is possible for the job table to become empty, 2207 * while the list of stoppedJobs remains non-empty. In such a case, 2208 * we want to restart as many jobs as we can. 2209 * 2210 * Results: 2211 * TRUE if it is. FALSE if it ain't. 2212 * 2213 * Side Effects: 2214 * None. 2215 * 2216 * ----------------------------------------------------------------------- 2217 */ 2218 Boolean 2219 Job_Empty () 2220 { 2221 if (nJobs == 0) { 2222 if (!Lst_IsEmpty(stoppedJobs) && !aborting) { 2223 /* 2224 * The job table is obviously not full if it has no jobs in 2225 * it...Try and restart the stopped jobs. 2226 */ 2227 jobFull = FALSE; 2228 while (!jobFull && !Lst_IsEmpty(stoppedJobs)) { 2229 JobRestart((Job *)Lst_DeQueue(stoppedJobs)); 2230 } 2231 return(FALSE); 2232 } else { 2233 return(TRUE); 2234 } 2235 } else { 2236 return(FALSE); 2237 } 2238 } 2239 2240 /*- 2241 *----------------------------------------------------------------------- 2242 * JobMatchShell -- 2243 * Find a matching shell in 'shells' given its final component. 2244 * 2245 * Results: 2246 * A pointer to the Shell structure. 2247 * 2248 * Side Effects: 2249 * None. 2250 * 2251 *----------------------------------------------------------------------- 2252 */ 2253 static Shell * 2254 JobMatchShell (name) 2255 char *name; /* Final component of shell path */ 2256 { 2257 register Shell *sh; /* Pointer into shells table */ 2258 Shell *match; /* Longest-matching shell */ 2259 register char *cp1, 2260 *cp2; 2261 char *eoname; 2262 2263 eoname = name + strlen (name); 2264 2265 match = (Shell *) NULL; 2266 2267 for (sh = shells; sh->name != NULL; sh++) { 2268 for (cp1 = eoname - strlen (sh->name), cp2 = sh->name; 2269 *cp1 != '\0' && *cp1 == *cp2; 2270 cp1++, cp2++) { 2271 continue; 2272 } 2273 if (*cp1 != *cp2) { 2274 continue; 2275 } else if (match == (Shell *) NULL || 2276 strlen (match->name) < strlen (sh->name)) { 2277 match = sh; 2278 } 2279 } 2280 return (match == (Shell *) NULL ? sh : match); 2281 } 2282 2283 /*- 2284 *----------------------------------------------------------------------- 2285 * Job_ParseShell -- 2286 * Parse a shell specification and set up commandShell, shellPath 2287 * and shellName appropriately. 2288 * 2289 * Results: 2290 * FAILURE if the specification was incorrect. 2291 * 2292 * Side Effects: 2293 * commandShell points to a Shell structure (either predefined or 2294 * created from the shell spec), shellPath is the full path of the 2295 * shell described by commandShell, while shellName is just the 2296 * final component of shellPath. 2297 * 2298 * Notes: 2299 * A shell specification consists of a .SHELL target, with dependency 2300 * operator, followed by a series of blank-separated words. Double 2301 * quotes can be used to use blanks in words. A backslash escapes 2302 * anything (most notably a double-quote and a space) and 2303 * provides the functionality it does in C. Each word consists of 2304 * keyword and value separated by an equal sign. There should be no 2305 * unnecessary spaces in the word. The keywords are as follows: 2306 * name Name of shell. 2307 * path Location of shell. Overrides "name" if given 2308 * quiet Command to turn off echoing. 2309 * echo Command to turn echoing on 2310 * filter Result of turning off echoing that shouldn't be 2311 * printed. 2312 * echoFlag Flag to turn echoing on at the start 2313 * errFlag Flag to turn error checking on at the start 2314 * hasErrCtl True if shell has error checking control 2315 * check Command to turn on error checking if hasErrCtl 2316 * is TRUE or template of command to echo a command 2317 * for which error checking is off if hasErrCtl is 2318 * FALSE. 2319 * ignore Command to turn off error checking if hasErrCtl 2320 * is TRUE or template of command to execute a 2321 * command so as to ignore any errors it returns if 2322 * hasErrCtl is FALSE. 2323 * 2324 *----------------------------------------------------------------------- 2325 */ 2326 ReturnStatus 2327 Job_ParseShell (line) 2328 char *line; /* The shell spec */ 2329 { 2330 char **words; 2331 int wordCount; 2332 register char **argv; 2333 register int argc; 2334 char *path; 2335 Shell newShell; 2336 Boolean fullSpec = FALSE; 2337 2338 while (isspace (*line)) { 2339 line++; 2340 } 2341 words = brk_string (line, &wordCount); 2342 2343 bzero ((Address)&newShell, sizeof(newShell)); 2344 2345 /* 2346 * Parse the specification by keyword 2347 */ 2348 for (path = (char *)NULL, argc = wordCount - 1, argv = words + 1; 2349 argc != 0; 2350 argc--, argv++) { 2351 if (strncmp (*argv, "path=", 5) == 0) { 2352 path = &argv[0][5]; 2353 } else if (strncmp (*argv, "name=", 5) == 0) { 2354 newShell.name = &argv[0][5]; 2355 } else { 2356 if (strncmp (*argv, "quiet=", 6) == 0) { 2357 newShell.echoOff = &argv[0][6]; 2358 } else if (strncmp (*argv, "echo=", 5) == 0) { 2359 newShell.echoOn = &argv[0][5]; 2360 } else if (strncmp (*argv, "filter=", 7) == 0) { 2361 newShell.noPrint = &argv[0][7]; 2362 newShell.noPLen = strlen(newShell.noPrint); 2363 } else if (strncmp (*argv, "echoFlag=", 9) == 0) { 2364 newShell.echo = &argv[0][9]; 2365 } else if (strncmp (*argv, "errFlag=", 8) == 0) { 2366 newShell.exit = &argv[0][8]; 2367 } else if (strncmp (*argv, "hasErrCtl=", 10) == 0) { 2368 char c = argv[0][10]; 2369 newShell.hasErrCtl = !((c != 'Y') && (c != 'y') && 2370 (c != 'T') && (c != 't')); 2371 } else if (strncmp (*argv, "check=", 6) == 0) { 2372 newShell.errCheck = &argv[0][6]; 2373 } else if (strncmp (*argv, "ignore=", 7) == 0) { 2374 newShell.ignErr = &argv[0][7]; 2375 } else { 2376 Parse_Error (PARSE_FATAL, "Unknown keyword \"%s\"", 2377 *argv); 2378 return (FAILURE); 2379 } 2380 fullSpec = TRUE; 2381 } 2382 } 2383 2384 if (path == (char *)NULL) { 2385 /* 2386 * If no path was given, the user wants one of the pre-defined shells, 2387 * yes? So we find the one s/he wants with the help of JobMatchShell 2388 * and set things up the right way. shellPath will be set up by 2389 * Job_Init. 2390 */ 2391 if (newShell.name == (char *)NULL) { 2392 Parse_Error (PARSE_FATAL, "Neither path nor name specified"); 2393 return (FAILURE); 2394 } else { 2395 commandShell = JobMatchShell (newShell.name); 2396 shellName = newShell.name; 2397 } 2398 } else { 2399 /* 2400 * The user provided a path. If s/he gave nothing else (fullSpec is 2401 * FALSE), try and find a matching shell in the ones we know of. 2402 * Else we just take the specification at its word and copy it 2403 * to a new location. In either case, we need to record the 2404 * path the user gave for the shell. 2405 */ 2406 shellPath = path; 2407 path = rindex (path, '/'); 2408 if (path == (char *)NULL) { 2409 path = shellPath; 2410 } else { 2411 path += 1; 2412 } 2413 if (newShell.name != (char *)NULL) { 2414 shellName = newShell.name; 2415 } else { 2416 shellName = path; 2417 } 2418 if (!fullSpec) { 2419 commandShell = JobMatchShell (shellName); 2420 } else { 2421 commandShell = (Shell *) emalloc(sizeof(Shell)); 2422 *commandShell = newShell; 2423 } 2424 } 2425 2426 if (commandShell->echoOn && commandShell->echoOff) { 2427 commandShell->hasEchoCtl = TRUE; 2428 } 2429 2430 if (!commandShell->hasErrCtl) { 2431 if (commandShell->errCheck == (char *)NULL) { 2432 commandShell->errCheck = ""; 2433 } 2434 if (commandShell->ignErr == (char *)NULL) { 2435 commandShell->ignErr = "%s\n"; 2436 } 2437 } 2438 2439 /* 2440 * Do not free up the words themselves, since they might be in use by the 2441 * shell specification... 2442 */ 2443 free (words); 2444 return SUCCESS; 2445 } 2446 2447 /*- 2448 *----------------------------------------------------------------------- 2449 * JobInterrupt -- 2450 * Handle the receipt of an interrupt. 2451 * 2452 * Results: 2453 * None 2454 * 2455 * Side Effects: 2456 * All children are killed. Another job will be started if the 2457 * .INTERRUPT target was given. 2458 *----------------------------------------------------------------------- 2459 */ 2460 static void 2461 JobInterrupt (runINTERRUPT) 2462 int runINTERRUPT; /* Non-zero if commands for the .INTERRUPT 2463 * target should be executed */ 2464 { 2465 LstNode ln; /* element in job table */ 2466 Job *job; /* job descriptor in that element */ 2467 GNode *interrupt; /* the node describing the .INTERRUPT target */ 2468 2469 aborting = ABORT_INTERRUPT; 2470 2471 (void)Lst_Open (jobs); 2472 while ((ln = Lst_Next (jobs)) != NILLNODE) { 2473 job = (Job *) Lst_Datum (ln); 2474 2475 if (!Targ_Precious (job->node)) { 2476 char *file = (job->node->path == (char *)NULL ? 2477 job->node->name : 2478 job->node->path); 2479 /* Don't unlink directories */ /* 10 Aug 92*/ 2480 struct stat sbuf; 2481 stat (file, &sbuf); 2482 if (!(sbuf.st_mode & S_IFDIR)) { 2483 if (unlink (file) == 0) { 2484 Error ("*** %s removed", file); 2485 } 2486 } 2487 } 2488 #ifdef RMT_WANTS_SIGNALS 2489 if (job->flags & JOB_REMOTE) { 2490 /* 2491 * If job is remote, let the Rmt module do the killing. 2492 */ 2493 if (!Rmt_Signal(job, SIGINT)) { 2494 /* 2495 * If couldn't kill the thing, finish it out now with an 2496 * error code, since no exit report will come in likely. 2497 */ 2498 union wait status; 2499 2500 status.w_status = 0; 2501 status.w_retcode = 1; 2502 JobFinish(job, status); 2503 } 2504 } else if (job->pid) { 2505 KILL(job->pid, SIGINT); 2506 } 2507 #else 2508 if (job->pid) { 2509 KILL(job->pid, SIGINT); 2510 } 2511 #endif /* RMT_WANTS_SIGNALS */ 2512 } 2513 Lst_Close (jobs); 2514 2515 if (runINTERRUPT && !touchFlag) { 2516 interrupt = Targ_FindNode (".INTERRUPT", TARG_NOCREATE); 2517 if (interrupt != NILGNODE) { 2518 ignoreErrors = FALSE; 2519 2520 JobStart (interrupt, JOB_IGNDOTS, (Job *)0); 2521 while (nJobs) { 2522 Job_CatchOutput(); 2523 #ifndef RMT_WILL_WATCH 2524 Job_CatchChildren (!usePipes); 2525 #endif /* RMT_WILL_WATCH */ 2526 } 2527 } 2528 } 2529 (void) unlink (tfile); 2530 exit (0); 2531 } 2532 2533 /* 2534 *----------------------------------------------------------------------- 2535 * Job_End -- 2536 * Do final processing such as the running of the commands 2537 * attached to the .END target. 2538 * 2539 * Results: 2540 * Number of errors reported. 2541 * 2542 * Side Effects: 2543 * The process' temporary file (tfile) is removed if it still 2544 * existed. 2545 *----------------------------------------------------------------------- 2546 */ 2547 int 2548 Job_End () 2549 { 2550 if (postCommands != NILGNODE && !Lst_IsEmpty (postCommands->commands)) { 2551 if (errors) { 2552 Error ("Errors reported so .END ignored"); 2553 } else { 2554 JobStart (postCommands, JOB_SPECIAL | JOB_IGNDOTS, 2555 (Job *)0); 2556 2557 while (nJobs) { 2558 Job_CatchOutput(); 2559 #ifndef RMT_WILL_WATCH 2560 Job_CatchChildren (!usePipes); 2561 #endif /* RMT_WILL_WATCH */ 2562 } 2563 } 2564 } 2565 (void) unlink (tfile); 2566 return(errors); 2567 } 2568 2569 /*- 2570 *----------------------------------------------------------------------- 2571 * Job_Wait -- 2572 * Waits for all running jobs to finish and returns. Sets 'aborting' 2573 * to ABORT_WAIT to prevent other jobs from starting. 2574 * 2575 * Results: 2576 * None. 2577 * 2578 * Side Effects: 2579 * Currently running jobs finish. 2580 * 2581 *----------------------------------------------------------------------- 2582 */ 2583 void 2584 Job_Wait() 2585 { 2586 aborting = ABORT_WAIT; 2587 while (nJobs != 0) { 2588 Job_CatchOutput(); 2589 #ifndef RMT_WILL_WATCH 2590 Job_CatchChildren(!usePipes); 2591 #endif /* RMT_WILL_WATCH */ 2592 } 2593 aborting = 0; 2594 } 2595 2596 /*- 2597 *----------------------------------------------------------------------- 2598 * Job_AbortAll -- 2599 * Abort all currently running jobs without handling output or anything. 2600 * This function is to be called only in the event of a major 2601 * error. Most definitely NOT to be called from JobInterrupt. 2602 * 2603 * Results: 2604 * None 2605 * 2606 * Side Effects: 2607 * All children are killed, not just the firstborn 2608 *----------------------------------------------------------------------- 2609 */ 2610 void 2611 Job_AbortAll () 2612 { 2613 LstNode ln; /* element in job table */ 2614 Job *job; /* the job descriptor in that element */ 2615 int foo; 2616 2617 aborting = ABORT_ERROR; 2618 2619 if (nJobs) { 2620 2621 (void)Lst_Open (jobs); 2622 while ((ln = Lst_Next (jobs)) != NILLNODE) { 2623 job = (Job *) Lst_Datum (ln); 2624 2625 /* 2626 * kill the child process with increasingly drastic signals to make 2627 * darn sure it's dead. 2628 */ 2629 #ifdef RMT_WANTS_SIGNALS 2630 if (job->flags & JOB_REMOTE) { 2631 Rmt_Signal(job, SIGINT); 2632 Rmt_Signal(job, SIGKILL); 2633 } else { 2634 KILL(job->pid, SIGINT); 2635 KILL(job->pid, SIGKILL); 2636 } 2637 #else 2638 KILL(job->pid, SIGINT); 2639 KILL(job->pid, SIGKILL); 2640 #endif /* RMT_WANTS_SIGNALS */ 2641 } 2642 } 2643 2644 /* 2645 * Catch as many children as want to report in at first, then give up 2646 */ 2647 while (wait3(&foo, WNOHANG, (struct rusage *)0) > 0) { 2648 ; 2649 } 2650 (void) unlink (tfile); 2651 } 2652