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