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