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