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