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