Annotation of src/usr.bin/make/job.c, Revision 1.98
1.40 espie 1: /* $OpenPackages$ */
1.93 espie 2: /* $OpenBSD$ */
1.6 millert 3: /* $NetBSD: job.c,v 1.16 1996/11/06 17:59:08 christos Exp $ */
1.1 deraadt 4:
5: /*
6: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
7: * Copyright (c) 1988, 1989 by Adam de Boor
8: * Copyright (c) 1989 by Berkeley Softworks
9: * All rights reserved.
10: *
11: * This code is derived from software contributed to Berkeley by
12: * Adam de Boor.
13: *
14: * Redistribution and use in source and binary forms, with or without
15: * modification, are permitted provided that the following conditions
16: * are met:
17: * 1. Redistributions of source code must retain the above copyright
18: * notice, this list of conditions and the following disclaimer.
19: * 2. Redistributions in binary form must reproduce the above copyright
20: * notice, this list of conditions and the following disclaimer in the
21: * documentation and/or other materials provided with the distribution.
1.55 millert 22: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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: /*-
40: * job.c --
41: * handle the creation etc. of our child processes.
42: *
43: * Interface:
1.40 espie 44: * Job_Make Start the creation of the given target.
1.1 deraadt 45: *
1.40 espie 46: * Job_CatchChildren Check for and handle the termination of any
47: * children. This must be called reasonably
48: * frequently to keep the whole make going at
49: * a decent clip, since job table entries aren't
50: * removed until their process is caught this way.
51: *
52: * Job_CatchOutput Print any output our children have produced.
53: * Should also be called fairly frequently to
54: * keep the user informed of what's going on.
55: * If no output is waiting, it will block for
56: * a time given by the SEL_* constants, below,
57: * or until output is ready.
58: *
1.53 jmc 59: * Job_Init Called to initialize this module. in addition,
1.40 espie 60: * any commands attached to the .BEGIN target
61: * are executed before this function returns.
62: * Hence, the makefile must have been parsed
63: * before this function is called.
64: *
65: * Job_End Cleanup any memory used.
66: *
1.41 espie 67: * Job_Full Return true if the job table is filled.
1.40 espie 68: *
1.41 espie 69: * Job_Empty Return true if the job table is completely
1.40 espie 70: * empty.
71: *
72: * Job_Finish Perform any final processing which needs doing.
73: * This includes the execution of any commands
74: * which have been/were attached to the .END
75: * target. It should only be called when the
76: * job table is empty.
77: *
78: * Job_AbortAll Abort all currently running jobs. It doesn't
79: * handle output or do anything for the jobs,
80: * just kills them. It should only be called in
81: * an emergency, as it were.
1.1 deraadt 82: *
1.40 espie 83: * Job_Wait Wait for all currently-running jobs to finish.
1.1 deraadt 84: */
85:
86: #include <sys/types.h>
87: #include <sys/wait.h>
1.41 espie 88: #include <ctype.h>
89: #include <errno.h>
1.1 deraadt 90: #include <fcntl.h>
1.41 espie 91: #include <signal.h>
1.69 espie 92: #include <stdarg.h>
1.1 deraadt 93: #include <stdio.h>
1.42 espie 94: #include <stdlib.h>
1.1 deraadt 95: #include <string.h>
1.41 espie 96: #include <unistd.h>
97: #include "config.h"
98: #include "defines.h"
1.1 deraadt 99: #include "job.h"
1.63 espie 100: #include "engine.h"
1.1 deraadt 101: #include "pathnames.h"
1.41 espie 102: #include "var.h"
103: #include "targ.h"
104: #include "error.h"
105: #include "lst.h"
106: #include "extern.h"
107: #include "gnode.h"
108: #include "memory.h"
109: #include "make.h"
110:
1.50 espie 111: /*
112: * The SEL_ constants determine the maximum amount of time spent in select
113: * before coming out to see if a child has finished. SEL_SEC is the number of
114: * seconds and SEL_USEC is the number of micro-seconds
115: */
116: #define SEL_SEC 0
117: #define SEL_USEC 500000
118:
119:
120: /*-
121: * Job Table definitions.
122: *
123: * Each job has several things associated with it:
124: * 1) The process id of the child shell
125: * 2) The graph node describing the target being made by this job
1.93 espie 126: * 3) An FILE* for writing out the commands. This is only
1.50 espie 127: * used before the job is actually started.
1.93 espie 128: * 4) Things used for handling the shell's output.
1.76 espie 129: * the output is being caught via a pipe and
1.50 espie 130: * the descriptors of our pipe, an array in which output is line
131: * buffered and the current position in that buffer are all
1.78 espie 132: * maintained for each job.
1.93 espie 133: * 5) A word of flags which determine how the module handles errors,
1.50 espie 134: * echoing, etc. for the job
135: *
136: * The job "table" is kept as a linked Lst in 'jobs', with the number of
137: * active jobs maintained in the 'nJobs' variable. At no time will this
138: * exceed the value of 'maxJobs', initialized by the Job_Init function.
139: *
140: * When a job is finished, the Make_Update function is called on each of the
141: * parents of the node which was just remade. This takes care of the upward
142: * traversal of the dependency graph.
143: */
144: #define JOB_BUFSIZE 1024
145: typedef struct Job_ {
1.51 mpech 146: pid_t pid; /* The child's process ID */
1.50 espie 147: GNode *node; /* The target the child is making */
148: LstNode tailCmds; /* The node of the first command to be
149: * saved when the job has been run */
150: FILE *cmdFILE; /* When creating the shell script, this is
151: * where the commands go */
152: int rmtID; /* ID returned from Rmt module */
153: short flags; /* Flags to control treatment of job */
154: #define JOB_IGNERR 0x001 /* Ignore non-zero exits */
155: #define JOB_SILENT 0x002 /* no output */
1.83 espie 156: #define JOB_SPECIAL 0x004 /* Target is a special one. */
1.79 espie 157: #define JOB_IGNDOTS 0x008 /* Ignore "..." lines when processing
158: * commands */
1.50 espie 159: #define JOB_RESTART 0x080 /* Job needs to be completely restarted */
1.79 espie 160: #define JOB_RESUME 0x100 /* Job needs to be resumed b/c it stopped,
161: * for some reason */
162: #define JOB_CONTINUING 0x200 /* We are in the process of resuming this job.
163: * Used to avoid infinite recursion between
164: * JobFinish and JobRestart */
1.76 espie 165: int inPipe; /* Input side of pipe associated
166: * with job's output channel */
167: int outPipe; /* Output side of pipe associated with
168: * job's output channel */
169: char outBuf[JOB_BUFSIZE + 1];
170: /* Buffer for storing the output of the
171: * job, line by line */
172: int curPos; /* Current position in op_outBuf */
1.50 espie 173: } Job;
174:
1.78 espie 175:
1.1 deraadt 176: /*
1.6 millert 177: * error handling variables
1.1 deraadt 178: */
1.40 espie 179: static int errors = 0; /* number of errors reported */
180: static int aborting = 0; /* why is the make aborting? */
181: #define ABORT_ERROR 1 /* Because of an error */
182: #define ABORT_INTERRUPT 2 /* Because it was interrupted */
183: #define ABORT_WAIT 3 /* Waiting for jobs to finish */
1.1 deraadt 184:
1.40 espie 185: static int numCommands; /* The number of commands actually printed
1.1 deraadt 186: * for a target. Should this number be
187: * 0, no shell will be executed. */
188:
1.70 espie 189: #define SHELL_ECHO_OFF "set -"
190: #define SHELL_ECHO_ON "set -v"
191: #define SHELL_ERROR_ON "set -e"
192: #define SHELL_ERROR_OFF "set +e"
193: #define SHELL_ECHO_FLAG "v"
194: #define SHELL_ERROR_FLAG "e"
1.1 deraadt 195:
1.70 espie 196: static const char *shellPath = _PATH_BSHELL;
197: static const char *shellName = "sh";
1.1 deraadt 198:
1.78 espie 199:
1.40 espie 200: static int maxJobs; /* The most children we can run at once */
1.83 espie 201: static int nJobs; /* The number of children currently running */
1.95 espie 202: static LIST runningJobs; /* The structures that describe them */
1.79 espie 203: static bool jobFull; /* Flag to tell when the job table is full. It
1.83 espie 204: * is set true when nJobs equals maxJobs */
1.40 espie 205: static fd_set *outputsp; /* Set of descriptors of pipes connected to
1.1 deraadt 206: * the output channels of children */
1.8 deraadt 207: static int outputsn;
1.48 espie 208: static GNode *lastNode; /* The node for which output was most recently
1.1 deraadt 209: * produced. */
1.48 espie 210: static char *targFmt; /* Format string to use to head output from a
1.1 deraadt 211: * job when it's not the most-recent job heard
212: * from */
1.2 deraadt 213:
214: # define TARG_FMT "--- %s ---\n" /* Default format */
215: # define MESSAGE(fp, gn) \
1.40 espie 216: (void)fprintf(fp, targFmt, gn->name);
1.1 deraadt 217:
218: /*
1.50 espie 219: * When JobStart attempts to run a job but isn't allowed to,
1.96 espie 220: * the job is placed on the queuedJobs queue to be run
1.6 millert 221: * when the next job finishes.
1.1 deraadt 222: */
1.96 espie 223: static LIST stoppedJobs;
224: static LIST queuedJobs;
1.1 deraadt 225:
226:
227: #if defined(USE_PGRP) && defined(SYSV)
1.40 espie 228: # define KILL(pid, sig) killpg(-(pid), (sig))
1.1 deraadt 229: #else
230: # if defined(USE_PGRP)
1.2 deraadt 231: # define KILL(pid, sig) killpg((pid), (sig))
1.1 deraadt 232: # else
1.2 deraadt 233: # define KILL(pid, sig) kill((pid), (sig))
1.1 deraadt 234: # endif
235: #endif
236:
1.6 millert 237: /*
1.2 deraadt 238: * Grmpf... There is no way to set bits of the wait structure
239: * anymore with the stupid W*() macros. I liked the union wait
240: * stuff much more. So, we devise our own macros... This is
241: * really ugly, use dramamine sparingly. You have been warned.
242: */
243: #define W_SETMASKED(st, val, fun) \
244: { \
245: int sh = (int) ~0; \
246: int mask = fun(sh); \
247: \
248: for (sh = 0; ((mask >> sh) & 1) == 0; sh++) \
249: continue; \
250: *(st) = (*(st) & ~mask) | ((val) << sh); \
251: }
252:
253: #define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG)
254: #define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS)
255:
256:
1.93 espie 257: static void pass_signal_to_job(void *, void *);
1.57 espie 258: static void SigHandler(int);
1.93 espie 259: static void handle_all_signals(void);
260: static void handle_signal(int);
1.40 espie 261: static int JobCmpPid(void *, void *);
1.60 espie 262: static int JobPrintCommand(LstNode, void *);
1.40 espie 263: static void JobSaveCommand(void *, void *);
264: static void JobClose(Job *);
265: static void JobFinish(Job *, int *);
266: static void JobExec(Job *, char **);
267: static void JobMakeArgv(Job *, char **);
268: static void JobRestart(Job *);
1.97 espie 269: static void JobStart(GNode *, int);
1.40 espie 270: static char *JobOutput(Job *, char *, char *, int);
1.41 espie 271: static void JobDoOutput(Job *, bool);
1.40 espie 272: static void JobInterrupt(int, int);
273: static void JobRestartJobs(void);
1.69 espie 274: static void DBPRINTF(Job *, const char *, ...);
1.84 espie 275: static void debug_printf(const char *, ...);
1.88 espie 276: static FILE *new_command_file(void);
1.93 espie 277: static void setup_signal(int);
1.94 espie 278: static void setup_all_signals(void);
1.98 ! espie 279: static Job *prepare_job(GNode *, int);
1.96 espie 280: static void start_queued_job(Job *);
1.93 espie 281:
282: static volatile sig_atomic_t got_signal;
1.1 deraadt 283:
1.57 espie 284: static volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT,
1.93 espie 285: got_SIGTERM, got_SIGTSTP, got_SIGTTOU, got_SIGTTIN, got_SIGWINCH;
286:
1.57 espie 287:
1.88 espie 288: #define TMPPAT "/tmp/makeXXXXXXXXXX"
289:
290: static FILE *
291: new_command_file()
292: {
293: int fd;
294: FILE *f;
295: char tmp[] = TMPPAT;
296:
297: fd = mkstemp(tmp);
298: if (fd == -1)
299: return NULL;
300: f = fdopen(fd, "w");
301: if (f == NULL)
302: close(fd);
303: eunlink(tmp);
304: return f;
305: }
306:
1.57 espie 307: static void
308: SigHandler(int sig)
309: {
310: switch(sig) {
311: case SIGINT:
312: got_SIGINT++;
1.93 espie 313: got_signal = 1;
1.57 espie 314: break;
315: case SIGHUP:
316: got_SIGHUP++;
1.93 espie 317: got_signal = 1;
1.57 espie 318: break;
319: case SIGQUIT:
320: got_SIGQUIT++;
1.93 espie 321: got_signal = 1;
1.57 espie 322: break;
323: case SIGTERM:
324: got_SIGTERM++;
1.93 espie 325: got_signal = 1;
1.57 espie 326: break;
1.93 espie 327: #ifdef USE_PGRP
1.57 espie 328: case SIGTSTP:
329: got_SIGTSTP++;
1.93 espie 330: got_signal = 1;
1.57 espie 331: break;
332: case SIGTTOU:
333: got_SIGTTOU++;
1.93 espie 334: got_signal = 1;
1.57 espie 335: break;
336: case SIGTTIN:
337: got_SIGTTIN++;
1.93 espie 338: got_signal = 1;
1.57 espie 339: break;
340: case SIGWINCH:
341: got_SIGWINCH++;
1.93 espie 342: got_signal = 1;
1.57 espie 343: break;
344: #endif
345: }
346: }
347:
348: static void
1.93 espie 349: handle_all_signals()
1.57 espie 350: {
1.93 espie 351: if (got_signal)
352: got_signal = 0;
353: else
354: return;
355:
1.57 espie 356: if (got_SIGINT) {
357: got_SIGINT=0;
1.93 espie 358: handle_signal(SIGINT);
1.57 espie 359: }
360: if (got_SIGHUP) {
361: got_SIGHUP=0;
1.93 espie 362: handle_signal(SIGHUP);
1.57 espie 363: }
364: if (got_SIGQUIT) {
365: got_SIGQUIT=0;
1.93 espie 366: handle_signal(SIGQUIT);
1.57 espie 367: }
368: if (got_SIGTERM) {
369: got_SIGTERM=0;
1.93 espie 370: handle_signal(SIGTERM);
1.57 espie 371: }
372: if (got_SIGTSTP) {
373: got_SIGTSTP=0;
1.93 espie 374: handle_signal(SIGTSTP);
1.57 espie 375: }
376: if (got_SIGTTOU) {
377: got_SIGTTOU=0;
1.93 espie 378: handle_signal(SIGTTOU);
1.57 espie 379: }
380: if (got_SIGTTIN) {
381: got_SIGTTIN=0;
1.93 espie 382: handle_signal(SIGTTIN);
1.57 espie 383: }
384: if (got_SIGWINCH) {
385: got_SIGWINCH=0;
1.93 espie 386: handle_signal(SIGWINCH);
1.57 espie 387: }
388: }
389:
1.1 deraadt 390: /*-
391: *-----------------------------------------------------------------------
392: * JobCondPassSig --
1.50 espie 393: * Pass a signal to a job if USE_PGRP
1.1 deraadt 394: * is defined.
395: *
396: * Side Effects:
397: * None, except the job may bite it.
398: *-----------------------------------------------------------------------
399: */
1.27 espie 400: static void
1.93 espie 401: pass_signal_to_job(void *jobp, /* Job to biff */
1.56 espie 402: void *signop) /* Signal to send it */
1.1 deraadt 403: {
1.66 espie 404: Job *job = (Job *)jobp;
405: int signo = *(int *)signop;
406: if (DEBUG(JOB)) {
407: (void)fprintf(stdout,
1.93 espie 408: "pass_signal_to_job passing signal %d to child %ld.\n",
1.66 espie 409: signo, (long)job->pid);
410: (void)fflush(stdout);
411: }
412: KILL(job->pid, signo);
1.1 deraadt 413: }
414:
415: /*-
416: *-----------------------------------------------------------------------
1.94 espie 417: * handle_signal --
1.68 espie 418: * Pass a signal to all local jobs if USE_PGRP is defined,
1.50 espie 419: * then die ourselves.
1.1 deraadt 420: *
421: * Side Effects:
422: * We die by the same signal.
423: *-----------------------------------------------------------------------
424: */
425: static void
1.93 espie 426: handle_signal(int signo) /* The signal number we've received */
1.1 deraadt 427: {
1.66 espie 428: sigset_t nmask, omask;
429: struct sigaction act;
1.6 millert 430:
1.66 espie 431: if (DEBUG(JOB)) {
1.93 espie 432: (void)fprintf(stdout, "handle_signal(%d) called.\n", signo);
1.66 espie 433: (void)fflush(stdout);
434: }
1.95 espie 435: Lst_ForEach(&runningJobs, pass_signal_to_job, &signo);
1.66 espie 436:
437: /*
438: * Deal with proper cleanup based on the signal received. We only run
439: * the .INTERRUPT target if the signal was in fact an interrupt. The
440: * other three termination signals are more of a "get out *now*"
441: * command.
442: */
443: if (signo == SIGINT) {
444: JobInterrupt(true, signo);
445: } else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT) {
446: JobInterrupt(false, signo);
447: }
1.1 deraadt 448:
1.66 espie 449: /*
450: * Leave gracefully if SIGQUIT, rather than core dumping.
451: */
452: if (signo == SIGQUIT) {
453: Finish(0);
454: }
1.6 millert 455:
1.66 espie 456: /*
457: * Send ourselves the signal now we've given the message to everyone
458: * else. Note we block everything else possible while we're getting
459: * the signal. This ensures that all our jobs get continued when we
460: * wake up before we take any other signal.
461: */
462: sigemptyset(&nmask);
463: sigaddset(&nmask, signo);
464: sigprocmask(SIG_SETMASK, &nmask, &omask);
465: memset(&act, 0, sizeof act);
466: act.sa_handler = SIG_DFL;
467: sigemptyset(&act.sa_mask);
468: act.sa_flags = 0;
469: sigaction(signo, &act, NULL);
1.6 millert 470:
1.66 espie 471: if (DEBUG(JOB)) {
472: (void)fprintf(stdout,
1.93 espie 473: "handle_signal passing signal to self, mask = %x.\n",
1.66 espie 474: ~0 & ~(1 << (signo-1)));
475: (void)fflush(stdout);
476: }
477: (void)signal(signo, SIG_DFL);
1.1 deraadt 478:
1.66 espie 479: (void)KILL(getpid(), signo);
1.1 deraadt 480:
1.66 espie 481: signo = SIGCONT;
1.95 espie 482: Lst_ForEach(&runningJobs, pass_signal_to_job, &signo);
1.1 deraadt 483:
1.66 espie 484: (void)sigprocmask(SIG_SETMASK, &omask, NULL);
485: sigprocmask(SIG_SETMASK, &omask, NULL);
486: act.sa_handler = SigHandler;
487: sigaction(signo, &act, NULL);
1.1 deraadt 488: }
489:
490: /*-
491: *-----------------------------------------------------------------------
492: * JobCmpPid --
493: * Compare the pid of the job with the given pid and return 0 if they
494: * are equal. This function is called from Job_CatchChildren via
495: * Lst_Find to find the job descriptor of the finished job.
496: *
497: * Results:
498: * 0 if the pid's match
499: *-----------------------------------------------------------------------
500: */
501: static int
1.56 espie 502: JobCmpPid(void *job, /* job to examine */
503: void *pid) /* process id desired */
1.1 deraadt 504: {
1.66 espie 505: return *(pid_t *)pid - ((Job *)job)->pid;
1.1 deraadt 506: }
507:
1.69 espie 508: static void
509: DBPRINTF(Job *job, const char *fmt, ...)
510: {
511: va_list va;
512: va_start(va, fmt);
513: if (DEBUG(JOB)) {
514: (void)vfprintf(stdout, fmt, va);
515: fflush(stdout);
516: }
517: vfprintf(job->cmdFILE, fmt, va);
518: va_end(va);
519: }
520:
1.84 espie 521: static void
522: debug_printf(const char *fmt, ...)
523: {
524: if (DEBUG(JOB)) {
525: va_list va;
526:
527: va_start(va, fmt);
528: (void)vfprintf(stdout, fmt, va);
529: fflush(stdout);
530: va_end(va);
531: }
532: }
533:
1.1 deraadt 534: /*-
535: *-----------------------------------------------------------------------
536: * JobPrintCommand --
537: * Put out another command for the given job. If the command starts
538: * with an @ or a - we process it specially. In the former case,
539: * so long as the -s and -n flags weren't given to make, we stick
540: * a shell-specific echoOff command in the script. In the latter,
541: * we ignore errors for the entire job, unless the shell has error
542: * control.
543: * If the command is just "..." we take all future commands for this
544: * job to be commands to be executed once the entire graph has been
545: * made and return non-zero to signal that the end of the commands
1.78 espie 546: * was reached. These commands are later attached to the end_node
1.1 deraadt 547: * node and executed by Job_End when all things are done.
1.40 espie 548: * This function is called from JobStart via Lst_Find
1.1 deraadt 549: *
550: * Results:
1.26 espie 551: * Always 1, unless the command was "..."
1.1 deraadt 552: *
553: * Side Effects:
554: * If the command begins with a '-' and the shell has no error control,
555: * the JOB_IGNERR flag is set in the job descriptor.
556: * If the command is "..." and we're not ignoring such things,
557: * tailCmds is set to the successor node of the cmd.
558: * numCommands is incremented if the command is actually printed.
559: *-----------------------------------------------------------------------
560: */
561: static int
1.78 espie 562: JobPrintCommand(LstNode cmdNode, /* command string to print */
563: void *jobp) /* job for which to print it */
1.1 deraadt 564: {
1.78 espie 565: bool noSpecials; /* true if we shouldn't worry about
566: * inserting special commands into
567: * the input stream. */
568: bool shutUp = false; /* true if we put a no echo command
569: * into the command file */
570: bool errOff = false; /* true if we turned error checking
571: * off before printing the command
572: * and need to turn it back on */
573: char *cmdTemplate; /* Template to use when printing the
574: * command */
575: char *cmdStart; /* Start of expanded command */
1.66 espie 576: char *cmd = (char *)Lst_Datum(cmdNode);
577: Job *job = (Job *)jobp;
578:
579: noSpecials = (noExecute && !(job->node->type & OP_MAKE));
580:
581: if (strcmp(cmd, "...") == 0) {
582: job->node->type |= OP_SAVE_CMDS;
583: if ((job->flags & JOB_IGNDOTS) == 0) {
584: job->tailCmds = Lst_Succ(cmdNode);
585: return 0;
586: }
587: return 1;
1.2 deraadt 588: }
1.1 deraadt 589:
1.78 espie 590:
1.67 espie 591: numCommands++;
1.1 deraadt 592:
1.66 espie 593: /* For debugging, we replace each command with the result of expanding
594: * the variables in the command. */
595: cmdStart = cmd = Var_Subst(cmd, &job->node->context, false);
596: Lst_Replace(cmdNode, cmdStart);
1.1 deraadt 597:
1.66 espie 598: cmdTemplate = "%s\n";
1.1 deraadt 599:
1.66 espie 600: /*
601: * Check for leading @' and -'s to control echoing and error checking.
602: */
603: for (;; cmd++) {
604: if (*cmd == '@')
605: shutUp = DEBUG(LOUD) ? false : true;
606: else if (*cmd == '-')
607: errOff = true;
608: else if (*cmd != '+')
609: break;
1.1 deraadt 610: }
611:
1.66 espie 612: while (isspace(*cmd))
613: cmd++;
614:
615: if (shutUp) {
1.72 espie 616: if (!(job->flags & JOB_SILENT) && !noSpecials) {
1.71 espie 617: DBPRINTF(job, "%s\n", SHELL_ECHO_OFF);
1.1 deraadt 618: } else {
1.66 espie 619: shutUp = false;
620: }
621: }
622:
623: if (errOff) {
624: if ( !(job->flags & JOB_IGNERR) && !noSpecials) {
1.72 espie 625: /*
1.78 espie 626: * we don't want the error-control commands showing
627: * up either, so we turn off echoing while executing
628: * them. We could put another field in the shell
629: * structure to tell JobDoOutput to look for this
630: * string too, but why make it any more complex than
631: * it already is?
1.72 espie 632: */
633: if (!(job->flags & JOB_SILENT) && !shutUp) {
1.75 espie 634: DBPRINTF(job, "%s; %s; %s\n", SHELL_ECHO_OFF,
635: SHELL_ERROR_OFF, SHELL_ECHO_ON);
1.66 espie 636: } else {
1.72 espie 637: DBPRINTF(job, "%s\n", SHELL_ERROR_OFF);
1.66 espie 638: }
639: } else {
640: errOff = false;
1.1 deraadt 641: }
1.66 espie 642: }
643:
1.69 espie 644: DBPRINTF(job, cmdTemplate, cmd);
1.66 espie 645:
646: if (errOff) {
647: /*
648: * If echoing is already off, there's no point in issuing the
649: * echoOff command. Otherwise we issue it and pretend it was on
650: * for the whole command...
1.1 deraadt 651: */
1.72 espie 652: if (!shutUp && !(job->flags & JOB_SILENT)) {
1.71 espie 653: DBPRINTF(job, "%s\n", SHELL_ECHO_OFF);
1.41 espie 654: shutUp = true;
1.1 deraadt 655: }
1.71 espie 656: DBPRINTF(job, "%s\n", SHELL_ERROR_ON);
1.66 espie 657: }
658: if (shutUp) {
1.71 espie 659: DBPRINTF(job, "%s\n", SHELL_ECHO_ON);
1.1 deraadt 660: }
1.66 espie 661: return 1;
1.1 deraadt 662: }
663:
664: /*-
665: *-----------------------------------------------------------------------
666: * JobSaveCommand --
667: * Save a command to be executed when everything else is done.
668: * Callback function for JobFinish...
669: *
670: * Side Effects:
1.78 espie 671: * The command is tacked onto the end of end_node's commands list.
1.1 deraadt 672: *-----------------------------------------------------------------------
673: */
1.27 espie 674: static void
1.56 espie 675: JobSaveCommand(void *cmd, void *gn)
1.1 deraadt 676: {
1.66 espie 677: GNode *g = (GNode *)gn;
678: char *result;
1.28 espie 679:
1.66 espie 680: result = Var_Subst((char *)cmd, &g->context, false);
1.78 espie 681: Lst_AtEnd(&end_node->commands, result);
1.2 deraadt 682: }
683:
684:
685: /*-
686: *-----------------------------------------------------------------------
687: * JobClose --
688: * Called to close both input and output pipes when a job is finished.
689: *
690: * Side Effects:
691: * The file descriptors associated with the job are closed.
692: *-----------------------------------------------------------------------
693: */
694: static void
1.56 espie 695: JobClose(Job *job)
1.2 deraadt 696: {
1.76 espie 697: FD_CLR(job->inPipe, outputsp);
698: if (job->outPipe != job->inPipe) {
699: (void)close(job->outPipe);
1.66 espie 700: }
1.76 espie 701: JobDoOutput(job, true);
702: (void)close(job->inPipe);
1.1 deraadt 703: }
704:
705: /*-
706: *-----------------------------------------------------------------------
707: * JobFinish --
708: * Do final processing for the given job including updating
709: * parents and starting new jobs as available/necessary. Note
710: * that we pay no attention to the JOB_IGNERR flag here.
711: * This is because when we're called because of a noexecute flag
712: * or something, jstat.w_status is 0 and when called from
713: * Job_CatchChildren, the status is zeroed if it s/b ignored.
714: *
715: * Side Effects:
716: * Some nodes may be put on the toBeMade queue.
1.78 espie 717: * Final commands for the job are placed on end_node.
1.1 deraadt 718: *
1.6 millert 719: * If we got an error and are aborting (aborting == ABORT_ERROR) and
1.1 deraadt 720: * the job list is now empty, we are done for the day.
1.6 millert 721: * If we recognized an error (errors !=0), we set the aborting flag
1.1 deraadt 722: * to ABORT_ERROR so no more jobs will be started.
723: *-----------------------------------------------------------------------
724: */
725: /*ARGSUSED*/
726: static void
1.56 espie 727: JobFinish(Job *job, /* job to finish */
728: int *status) /* sub-why job went away */
1.2 deraadt 729: {
1.78 espie 730: bool done;
1.2 deraadt 731:
1.66 espie 732: if ((WIFEXITED(*status) &&
733: WEXITSTATUS(*status) != 0 && !(job->flags & JOB_IGNERR)) ||
734: (WIFSIGNALED(*status) && WTERMSIG(*status) != SIGCONT)) {
735: /*
736: * If it exited non-zero and either we're doing things our
737: * way or we're not ignoring errors, the job is finished.
738: * Similarly, if the shell died because of a signal
739: * the job is also finished. In these
740: * cases, finish out the job's output before printing the exit
741: * status...
742: */
743: JobClose(job);
744: if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
745: (void)fclose(job->cmdFILE);
746: }
747: done = true;
748: } else if (WIFEXITED(*status)) {
749: /*
750: * Deal with ignored errors in -B mode. We need to print a
751: * message telling of the ignored error as well as setting
752: * status.w_status to 0 so the next command gets run. To do
753: * this, we set done to be true if in -B mode and the job
754: * exited non-zero.
755: */
756: done = WEXITSTATUS(*status) != 0;
757: /*
758: * Old comment said: "Note we don't want to close down any of
759: * the streams until we know we're at the end." But we do.
760: * Otherwise when are we going to print the rest of the stuff?
761: */
762: JobClose(job);
763: } else {
764: /*
765: * No need to close things down or anything.
766: */
767: done = false;
1.1 deraadt 768: }
1.6 millert 769:
1.66 espie 770: if (done ||
771: WIFSTOPPED(*status) ||
772: (WIFSIGNALED(*status) && WTERMSIG(*status) == SIGCONT) ||
773: DEBUG(JOB)) {
774: FILE *out;
775:
1.76 espie 776: out = stdout;
1.1 deraadt 777:
1.66 espie 778: if (WIFEXITED(*status)) {
1.84 espie 779: debug_printf("Process %ld exited.\n", (long)job->pid);
1.66 espie 780: if (WEXITSTATUS(*status) != 0) {
1.76 espie 781: if (job->node != lastNode) {
1.66 espie 782: MESSAGE(out, job->node);
783: lastNode = job->node;
784: }
785: (void)fprintf(out, "*** Error code %d%s\n",
786: WEXITSTATUS(*status),
1.68 espie 787: (job->flags & JOB_IGNERR) ? "(ignored)" :
1.66 espie 788: "");
789:
790: if (job->flags & JOB_IGNERR) {
791: *status = 0;
792: }
793: } else if (DEBUG(JOB)) {
1.76 espie 794: if (job->node != lastNode) {
1.66 espie 795: MESSAGE(out, job->node);
796: lastNode = job->node;
797: }
1.68 espie 798: (void)fprintf(out,
1.66 espie 799: "*** Completed successfully\n");
800: }
801: } else if (WIFSTOPPED(*status)) {
1.84 espie 802: debug_printf("Process %ld stopped.\n", (long)job->pid);
1.76 espie 803: if (job->node != lastNode) {
1.66 espie 804: MESSAGE(out, job->node);
805: lastNode = job->node;
806: }
807: (void)fprintf(out, "*** Stopped -- signal %d\n",
808: WSTOPSIG(*status));
809: job->flags |= JOB_RESUME;
810: Lst_AtEnd(&stoppedJobs, job);
811: (void)fflush(out);
812: return;
813: } else if (WTERMSIG(*status) == SIGCONT) {
814: /*
815: * If the beastie has continued, shift the Job from the
816: * stopped list to the running one (or re-stop it if
817: * concurrency is exceeded) and go and get another
818: * child.
819: */
820: if (job->flags & (JOB_RESUME|JOB_RESTART)) {
1.76 espie 821: if (job->node != lastNode) {
1.66 espie 822: MESSAGE(out, job->node);
823: lastNode = job->node;
824: }
825: (void)fprintf(out, "*** Continued\n");
826: }
827: if (!(job->flags & JOB_CONTINUING)) {
1.84 espie 828: debug_printf(
829: "Warning: "
830: "process %ld was not continuing.\n",
831: (long)job->pid);
1.66 espie 832: #if 0
833: /*
834: * We don't really want to restart a job from
835: * scratch just because it continued,
836: * especially not without killing the
837: * continuing process! That's why this is
838: * ifdef'ed out. FD - 9/17/90
839: */
840: JobRestart(job);
1.2 deraadt 841: #endif
1.66 espie 842: }
843: job->flags &= ~JOB_CONTINUING;
1.95 espie 844: Lst_AtEnd(&runningJobs, job);
1.67 espie 845: nJobs++;
1.84 espie 846: debug_printf("Process %ld is continuing locally.\n",
847: (long)job->pid);
1.66 espie 848: if (nJobs == maxJobs) {
849: jobFull = true;
1.84 espie 850: debug_printf("Job queue is full.\n");
1.66 espie 851: }
852: (void)fflush(out);
853: return;
854: } else {
1.76 espie 855: if (job->node != lastNode) {
1.66 espie 856: MESSAGE(out, job->node);
857: lastNode = job->node;
858: }
1.68 espie 859: (void)fprintf(out, "*** Signal %d\n",
1.66 espie 860: WTERMSIG(*status));
1.40 espie 861: }
1.66 espie 862:
863: (void)fflush(out);
1.1 deraadt 864: }
865:
1.85 espie 866: done = true;
1.1 deraadt 867:
1.66 espie 868: if (done &&
869: aborting != ABORT_ERROR &&
870: aborting != ABORT_INTERRUPT &&
871: *status == 0) {
872: /* As long as we aren't aborting and the job didn't return a
873: * non-zero status that we shouldn't ignore, we call
874: * Make_Update to update the parents. In addition, any saved
875: * commands for the node are placed on the .END target. */
876: Lst_ForEachFrom(job->tailCmds, JobSaveCommand, job->node);
877: job->node->made = MADE;
878: Make_Update(job->node);
879: free(job);
880: } else if (*status != 0) {
1.67 espie 881: errors++;
1.66 espie 882: free(job);
883: }
1.1 deraadt 884:
1.66 espie 885: JobRestartJobs();
1.1 deraadt 886:
887: /*
1.66 espie 888: * Set aborting if any error.
1.1 deraadt 889: */
1.66 espie 890: if (errors && !keepgoing && aborting != ABORT_INTERRUPT) {
891: /*
892: * If we found any errors in this batch of children and the -k
893: * flag wasn't given, we set the aborting flag so no more jobs
894: * get started.
895: */
896: aborting = ABORT_ERROR;
897: }
1.6 millert 898:
1.66 espie 899: if (aborting == ABORT_ERROR && Job_Empty()) {
900: /*
901: * If we are aborting and the job table is now empty, we finish.
902: */
903: Finish(errors);
904: }
1.1 deraadt 905: }
906:
907: /*-
908: *-----------------------------------------------------------------------
909: * JobExec --
910: * Execute the shell for the given job. Called from JobStart and
911: * JobRestart.
912: *
913: * Side Effects:
914: * A shell is executed, outputs is altered and the Job structure added
915: * to the job table.
916: *-----------------------------------------------------------------------
917: */
918: static void
1.56 espie 919: JobExec(Job *job, char **argv)
1.1 deraadt 920: {
1.66 espie 921: pid_t cpid; /* ID of new child */
1.94 espie 922: static int signals_caught = 0;
1.6 millert 923:
1.66 espie 924: if (DEBUG(JOB)) {
1.78 espie 925: int i;
1.6 millert 926:
1.66 espie 927: (void)fprintf(stdout, "Running %s\n", job->node->name);
928: (void)fprintf(stdout, "\tCommand: ");
929: for (i = 0; argv[i] != NULL; i++) {
930: (void)fprintf(stdout, "%s ", argv[i]);
931: }
932: (void)fprintf(stdout, "\n");
933: (void)fflush(stdout);
1.1 deraadt 934: }
1.6 millert 935:
1.66 espie 936: /*
937: * Some jobs produce no output and it's disconcerting to have
938: * no feedback of their running (since they produce no output, the
939: * banner with their name in it never appears). This is an attempt to
940: * provide that feedback, even if nothing follows it.
941: */
1.87 espie 942: if (lastNode != job->node && !(job->flags & JOB_SILENT)) {
1.66 espie 943: MESSAGE(stdout, job->node);
944: lastNode = job->node;
945: }
1.1 deraadt 946:
1.94 espie 947: if (!signals_caught) {
948: signals_caught = 1;
949: setup_all_signals();
950: }
951:
1.66 espie 952: if ((cpid = fork()) == -1) {
953: Punt("Cannot fork");
954: } else if (cpid == 0) {
1.6 millert 955:
1.66 espie 956: /*
957: * Must duplicate the input stream down to the child's input
958: * and reset it to the beginning (again). Since the stream was
959: * marked close-on-exec, we must clear that bit in the new
960: * input.
961: */
1.81 espie 962: if (dup2(fileno(job->cmdFILE), 0) == -1)
1.82 espie 963: Punt("Cannot dup2(job->cmdFile): %s", strerror(errno));
1.66 espie 964: (void)fcntl(0, F_SETFD, 0);
965: (void)lseek(0, 0, SEEK_SET);
966:
1.76 espie 967: /*
1.78 espie 968: * Set up the child's output to be routed through the pipe
969: * we've created for it.
1.76 espie 970: */
971: if (dup2(job->outPipe, 1) == -1)
1.82 espie 972: Punt("Cannot dup2(job->outPipe): %s", strerror(errno));
1.66 espie 973: /*
974: * The output channels are marked close on exec. This bit was
975: * duplicated by the dup2 (on some systems), so we have to
976: * clear it before routing the shell's error output to the same
977: * place as its standard output.
978: */
979: (void)fcntl(1, F_SETFD, 0);
980: if (dup2(1, 2) == -1)
1.82 espie 981: Punt("Cannot dup2(stdout): %s", strerror(errno));
1.1 deraadt 982:
983: #ifdef USE_PGRP
1.66 espie 984: /*
985: * We want to switch the child into a different process family
986: * so we can kill it and all its descendants in one fell swoop,
987: * by killing its process family, but not commit suicide.
988: */
1.2 deraadt 989: # if defined(SYSV)
1.66 espie 990: (void)setsid();
1.2 deraadt 991: # else
1.66 espie 992: (void)setpgid(0, getpid());
1.2 deraadt 993: # endif
994: #endif /* USE_PGRP */
1.1 deraadt 995:
1.66 espie 996: (void)execv(shellPath, argv);
997:
998: (void)write(STDERR_FILENO, "Could not execute shell\n",
999: sizeof("Could not execute shell"));
1000: _exit(1);
1001: } else {
1002: job->pid = cpid;
1003:
1.87 espie 1004: /* we set the current position in the buffer to the beginning
1005: * and mark another stream to watch in the outputs mask
1006: */
1007: job->curPos = 0;
1.66 espie 1008:
1.87 espie 1009: if (outputsp == NULL || job->inPipe > outputsn) {
1.92 cnst 1010: int fdn, ofdn;
1011: fd_set *tmp;
1.87 espie 1012:
1.92 cnst 1013: fdn = howmany(job->inPipe+1, NFDBITS);
1014: ofdn = outputsn ? howmany(outputsn+1, NFDBITS) : 0;
1.87 espie 1015:
1.92 cnst 1016: if (fdn != ofdn) {
1017: tmp = recalloc(outputsp, fdn, sizeof(fd_mask));
1.87 espie 1018: if (tmp == NULL)
1019: return;
1.92 cnst 1020: outputsp = tmp;
1.66 espie 1021: }
1.87 espie 1022: outputsn = job->inPipe;
1.66 espie 1023: }
1.87 espie 1024: FD_SET(job->inPipe, outputsp);
1.2 deraadt 1025:
1.66 espie 1026: /*
1027: * XXX: Used to not happen if REMOTE. Why?
1028: */
1029: if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1030: (void)fclose(job->cmdFILE);
1031: job->cmdFILE = NULL;
1032: }
1.1 deraadt 1033: }
1034:
1.48 espie 1035: /*
1.66 espie 1036: * Now the job is actually running, add it to the table.
1.48 espie 1037: */
1.67 espie 1038: nJobs++;
1.95 espie 1039: Lst_AtEnd(&runningJobs, job);
1.66 espie 1040: if (nJobs == maxJobs) {
1041: jobFull = true;
1.1 deraadt 1042: }
1043: }
1044:
1045: /*-
1046: *-----------------------------------------------------------------------
1047: * JobMakeArgv --
1048: * Create the argv needed to execute the shell for a given job.
1049: *-----------------------------------------------------------------------
1050: */
1051: static void
1.56 espie 1052: JobMakeArgv(Job *job, char **argv)
1.1 deraadt 1053: {
1.66 espie 1054: int argc;
1055: static char args[10]; /* For merged arguments */
1.6 millert 1056:
1.70 espie 1057: argv[0] = (char *)shellName;
1.66 espie 1058: argc = 1;
1.1 deraadt 1059:
1.73 espie 1060: (void)snprintf(args, sizeof(args), "-%s%s",
1061: (job->flags & JOB_IGNERR) ? "" : SHELL_ERROR_FLAG,
1062: (job->flags & JOB_SILENT) ? "" : SHELL_ECHO_FLAG);
1063:
1064: if (args[1]) {
1065: argv[argc] = args;
1066: argc++;
1.78 espie 1067: }
1.66 espie 1068: argv[argc] = NULL;
1.1 deraadt 1069: }
1070:
1.96 espie 1071: static void
1072: start_queued_job(Job *job)
1073: {
1074: /*
1075: * Set up the control arguments to the shell. This is based on
1076: * the flags set earlier for this job. If the JOB_IGNERR flag
1077: * is clear, the 'exit' flag of the commandShell is used to
1078: * cause it to exit upon receiving an error. If the JOB_SILENT
1079: * flag is clear, the 'echo' flag of the commandShell is used
1080: * to get it to start echoing as soon as it starts processing
1081: * commands.
1082: */
1083: char *argv[4];
1084:
1085: JobMakeArgv(job, argv);
1086:
1087: if (DEBUG(JOB)) {
1088: (void)fprintf(stdout, "Restarting %s...",
1089: job->node->name);
1090: (void)fflush(stdout);
1091: }
1092: if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) {
1093: /*
1094: * Can't be exported and not allowed to run locally --
1095: * put it back on the hold queue and mark the table
1096: * full
1097: */
1098: debug_printf("holding\n");
1099: Lst_AtFront(&stoppedJobs, job);
1100: jobFull = true;
1101: debug_printf("Job queue is full.\n");
1102: return;
1103: } else {
1104: /*
1105: * Job may be run locally.
1106: */
1107: debug_printf("running locally\n");
1108: }
1109: JobExec(job, argv);
1110: }
1111:
1.1 deraadt 1112: /*-
1113: *-----------------------------------------------------------------------
1114: * JobRestart --
1.6 millert 1115: * Restart a job that stopped for some reason.
1.1 deraadt 1116: *
1117: * Side Effects:
1118: * jobFull will be set if the job couldn't be run.
1119: *-----------------------------------------------------------------------
1120: */
1121: static void
1.56 espie 1122: JobRestart(Job *job)
1.1 deraadt 1123: {
1.66 espie 1124: if (job->flags & JOB_RESTART) {
1.96 espie 1125: start_queued_job(job);
1.66 espie 1126: } else {
1.1 deraadt 1127: /*
1.66 espie 1128: * The job has stopped and needs to be restarted. Why it
1129: * stopped, we don't know...
1.1 deraadt 1130: */
1.84 espie 1131: debug_printf("Resuming %s...", job->node->name);
1.83 espie 1132: if ((nJobs < maxJobs || ((job->flags & JOB_SPECIAL) &&
1133: maxJobs == 0)) && nJobs != maxJobs) {
1.66 espie 1134: /*
1.79 espie 1135: * If we haven't reached the concurrency limit already
1.83 espie 1136: * (or maxJobs is 0), it's ok to resume the job.
1.66 espie 1137: */
1138: bool error;
1139: int status;
1140:
1141: error = KILL(job->pid, SIGCONT) != 0;
1142:
1143: if (!error) {
1144: /*
1145: * Make sure the user knows we've continued the
1146: * beast and actually put the thing in the job
1147: * table.
1148: */
1149: job->flags |= JOB_CONTINUING;
1150: W_SETTERMSIG(&status, SIGCONT);
1151: JobFinish(job, &status);
1152:
1153: job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1.84 espie 1154: debug_printf("done\n");
1.66 espie 1155: } else {
1156: Error("couldn't resume %s: %s",
1157: job->node->name, strerror(errno));
1158: status = 0;
1159: W_SETEXITSTATUS(&status, 1);
1160: JobFinish(job, &status);
1161: }
1162: } else {
1163: /*
1164: * Job cannot be restarted. Mark the table as full and
1165: * place the job back on the list of stopped jobs.
1166: */
1.84 espie 1167: debug_printf("table full\n");
1.66 espie 1168: Lst_AtFront(&stoppedJobs, job);
1169: jobFull = true;
1.84 espie 1170: debug_printf("Job queue is full.\n");
1.1 deraadt 1171: }
1172: }
1173: }
1174:
1.98 ! espie 1175: static Job *
! 1176: prepare_job(GNode *gn, int flags)
1.78 espie 1177: {
1178: Job *job; /* new job descriptor */
1179: bool cmdsOK; /* true if the nodes commands were all right */
1180: bool noExec; /* Set true if we decide not to run the job */
1.66 espie 1181:
1.86 espie 1182: job = emalloc(sizeof(Job));
1183: if (job == NULL) {
1184: Punt("JobStart out of memory");
1.1 deraadt 1185: }
1186:
1.66 espie 1187: job->node = gn;
1188: job->tailCmds = NULL;
1.1 deraadt 1189:
1190: /*
1.66 espie 1191: * Set the initial value of the flags for this job based on the global
1192: * ones and the node's attributes... Any flags supplied by the caller
1193: * are also added to the field.
1.1 deraadt 1194: */
1.87 espie 1195: job->flags = flags;
1.66 espie 1196: if (Targ_Ignore(gn)) {
1197: job->flags |= JOB_IGNERR;
1198: }
1199: if (Targ_Silent(gn)) {
1200: job->flags |= JOB_SILENT;
1.1 deraadt 1201: }
1.6 millert 1202:
1.1 deraadt 1203: /*
1.66 espie 1204: * Check the commands now so any attributes from .DEFAULT have a chance
1205: * to migrate to the node
1.1 deraadt 1206: */
1.87 espie 1207: cmdsOK = Job_CheckCommands(gn, Error);
1.1 deraadt 1208:
1209: /*
1.66 espie 1210: * If the -n flag wasn't given, we open up OUR (not the child's)
1211: * temporary file to stuff commands in it. The thing is rd/wr so we
1212: * don't need to reopen it to feed it to the shell. If the -n flag
1213: * *was* given, we just set the file to be stdout. Cute, huh?
1.1 deraadt 1214: */
1.66 espie 1215: if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
1216: /*
1217: * We're serious here, but if the commands were bogus, we're
1218: * also dead...
1219: */
1220: if (!cmdsOK) {
1221: DieHorribly();
1222: }
1.6 millert 1223:
1.88 espie 1224: job->cmdFILE = new_command_file();
1.66 espie 1225: if (job->cmdFILE == NULL) {
1.88 espie 1226: Punt("Error creating command file");
1.66 espie 1227: }
1.90 espie 1228: (void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC);
1.66 espie 1229: /*
1230: * Send the commands to the command file, flush all its buffers
1231: * then rewind and remove the thing.
1232: */
1233: noExec = false;
1234:
1235: /*
1.85 espie 1236: * We can do all the commands at once. hooray for
1237: * sanity
1.66 espie 1238: */
1.85 espie 1239: numCommands = 0;
1240: Lst_ForEachNodeWhile(&gn->commands, JobPrintCommand,
1241: job);
1.66 espie 1242:
1.85 espie 1243: /*
1244: * If we didn't print out any commands to the shell
1245: * script, there's not much point in executing the
1246: * shell, is there?
1247: */
1248: if (numCommands == 0) {
1249: noExec = true;
1.66 espie 1250: }
1251: } else if (noExecute) {
1252: /*
1253: * Not executing anything -- just print all the commands to
1254: * stdout in one fell swoop. This will still set up
1255: * job->tailCmds correctly.
1256: */
1257: if (lastNode != gn) {
1258: MESSAGE(stdout, gn);
1259: lastNode = gn;
1260: }
1261: job->cmdFILE = stdout;
1262: /*
1263: * Only print the commands if they're ok, but don't die if
1264: * they're not -- just let the user know they're bad and keep
1265: * going. It doesn't do any harm in this case and may do some
1266: * good.
1267: */
1268: if (cmdsOK) {
1.68 espie 1269: Lst_ForEachNodeWhile(&gn->commands, JobPrintCommand,
1.66 espie 1270: job);
1271: }
1272: /*
1273: * Don't execute the shell, thank you.
1274: */
1.41 espie 1275: noExec = true;
1.66 espie 1276: } else {
1277: /*
1278: * Just touch the target and note that no shell should be
1279: * executed. Set cmdFILE to stdout to make life easier. Check
1280: * the commands, too, but don't die if they're no good -- it
1281: * does no harm to keep working up the graph.
1.30 espie 1282: */
1.66 espie 1283: job->cmdFILE = stdout;
1.87 espie 1284: Job_Touch(gn, job->flags & JOB_SILENT);
1.41 espie 1285: noExec = true;
1.1 deraadt 1286: }
1.66 espie 1287:
1.1 deraadt 1288: /*
1.66 espie 1289: * If we're not supposed to execute a shell, don't.
1.1 deraadt 1290: */
1.66 espie 1291: if (noExec) {
1292: /*
1293: * Unlink and close the command file if we opened one
1294: */
1295: if (job->cmdFILE != stdout) {
1296: if (job->cmdFILE != NULL)
1297: (void)fclose(job->cmdFILE);
1298: } else {
1299: (void)fflush(stdout);
1300: }
1.1 deraadt 1301:
1.66 espie 1302: /*
1303: * We only want to work our way up the graph if we aren't here
1304: * because the commands for the job were no good.
1305: */
1306: if (cmdsOK) {
1307: if (aborting == 0) {
1.68 espie 1308: Lst_ForEachFrom(job->tailCmds, JobSaveCommand,
1.66 espie 1309: job->node);
1310: Make_Update(job->node);
1311: }
1312: free(job);
1.98 ! espie 1313: return NULL;
1.66 espie 1314: } else {
1315: free(job);
1.98 ! espie 1316: return NULL;
1.66 espie 1317: }
1.1 deraadt 1318: } else {
1.66 espie 1319: (void)fflush(job->cmdFILE);
1.98 ! espie 1320: return job;
1.1 deraadt 1321: }
1.98 ! espie 1322: }
1.1 deraadt 1323:
1.98 ! espie 1324: /*-
! 1325: *-----------------------------------------------------------------------
! 1326: * JobStart --
! 1327: * Start a target-creation process going for the target described
! 1328: * by the graph node gn.
! 1329: *
! 1330: * Side Effects:
! 1331: * A new Job node is created and added to the list of running
! 1332: * jobs. PMake is forked and a child shell created.
! 1333: *-----------------------------------------------------------------------
! 1334: */
! 1335: static void
! 1336: JobStart(GNode *gn, /* target to create */
! 1337: int flags) /* flags for the job to override normal ones.
! 1338: * e.g. JOB_SPECIAL */
! 1339: {
! 1340: Job *job;
! 1341: char *argv[4]; /* Argument vector to shell */
! 1342: int fd[2];
! 1343: job = prepare_job(gn, flags);
! 1344: if (!job)
! 1345: return;
1.90 espie 1346: /* Create the pipe by which we'll get the shell's output.
1.66 espie 1347: */
1.87 espie 1348: if (pipe(fd) == -1)
1349: Punt("Cannot create pipe: %s", strerror(errno));
1350: job->inPipe = fd[0];
1351: job->outPipe = fd[1];
1.90 espie 1352: (void)fcntl(job->inPipe, F_SETFD, FD_CLOEXEC);
1353: (void)fcntl(job->outPipe, F_SETFD, FD_CLOEXEC);
1.1 deraadt 1354:
1.96 espie 1355: /*
1356: * Set up the control arguments to the shell. This is based on the flags
1357: * set earlier for this job.
1358: */
1359: JobMakeArgv(job, argv);
1360:
1.83 espie 1361: if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) &&
1362: maxJobs != 0) {
1.79 espie 1363: /*
1364: * The job can only be run locally, but we've hit the limit of
1365: * local concurrency, so put the job on hold until some other
1366: * job finishes. Note that the special jobs (.BEGIN, .INTERRUPT
1367: * and .END) may be run locally even when the local limit has
1.83 espie 1368: * been reached (e.g. when maxJobs == 0), though they will be
1.79 espie 1369: * exported if at all possible. In addition, any target marked
1.83 espie 1370: * with .NOEXPORT will be run locally if maxJobs is 0.
1.66 espie 1371: */
1372: jobFull = true;
1.6 millert 1373:
1.84 espie 1374: debug_printf("Can only run job locally.\n");
1.66 espie 1375: job->flags |= JOB_RESTART;
1376: Lst_AtEnd(&stoppedJobs, job);
1.79 espie 1377: } else {
1.83 espie 1378: if (nJobs >= maxJobs) {
1.79 espie 1379: /*
1380: * If we're running this job locally as a special case
1381: * (see above), at least say the table is full.
1382: */
1383: jobFull = true;
1.84 espie 1384: debug_printf("Local job queue is full.\n");
1.79 espie 1385: }
1386: JobExec(job, argv);
1.1 deraadt 1387: }
1388: }
1389:
1.6 millert 1390: static char *
1.56 espie 1391: JobOutput(Job *job, char *cp, char *endp, int msg)
1.2 deraadt 1392: {
1.66 espie 1393: char *ecp;
1.2 deraadt 1394:
1.74 espie 1395: ecp = strstr(cp, SHELL_ECHO_OFF);
1396: while (ecp != NULL) {
1397: if (cp != ecp) {
1398: *ecp = '\0';
1399: if (msg && job->node != lastNode) {
1400: MESSAGE(stdout, job->node);
1401: lastNode = job->node;
1.66 espie 1402: }
1.74 espie 1403: /*
1.78 espie 1404: * The only way there wouldn't be a newline after
1405: * this line is if it were the last in the buffer.
1406: * however, since the non-printable comes after it,
1407: * there must be a newline, so we don't print one.
1.74 espie 1408: */
1409: (void)fprintf(stdout, "%s", cp);
1410: (void)fflush(stdout);
1411: }
1412: cp = ecp + strlen(SHELL_ECHO_OFF);
1413: if (cp != endp) {
1414: /*
1.78 espie 1415: * Still more to print, look again after skipping
1416: * the whitespace following the non-printable
1417: * command....
1.74 espie 1418: */
1419: cp++;
1.78 espie 1420: while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
1.66 espie 1421: cp++;
1422: }
1.74 espie 1423: ecp = strstr(cp, SHELL_ECHO_OFF);
1424: } else {
1425: return cp;
1.2 deraadt 1426: }
1427: }
1.66 espie 1428: return cp;
1.2 deraadt 1429: }
1430:
1.1 deraadt 1431: /*-
1432: *-----------------------------------------------------------------------
1.40 espie 1433: * JobDoOutput --
1.89 espie 1434: * This functions is called whenever there is something to read on the
1435: * pipe. We collect more output from the given job and store it in the
1436: * job's outBuf. If this makes up a line, we print it tagged by the job's
1437: * identifier, as necessary.
1438: * We also keep our figurative eye out for the
1.1 deraadt 1439: * 'noPrint' line for the shell from which the output came. If
1440: * we recognize a line, we don't print it. If the command is not
1.6 millert 1441: * alone on the line (the character after it is not \0 or \n), we
1.1 deraadt 1442: * do print whatever follows it.
1443: *
1444: * Side Effects:
1445: * curPos may be shifted as may the contents of outBuf.
1446: *-----------------------------------------------------------------------
1447: */
1.48 espie 1448: static void
1.66 espie 1449: JobDoOutput(Job *job, /* the job whose output needs printing */
1450: bool finish) /* true if this is the last time we'll be
1451: * called for this job */
1452: {
1.78 espie 1453: bool gotNL = false; /* true if got a newline */
1.66 espie 1454: bool fbuf; /* true if our buffer filled up */
1455: int nr; /* number of bytes read */
1456: int i; /* auxiliary index into outBuf */
1.78 espie 1457: int max; /* limit for i (end of current data) */
1458: int nRead; /* (Temporary) number of bytes read */
1.1 deraadt 1459:
1.76 espie 1460: /*
1461: * Read as many bytes as will fit in the buffer.
1462: */
1.1 deraadt 1463: end_loop:
1.76 espie 1464: gotNL = false;
1465: fbuf = false;
1.6 millert 1466:
1.76 espie 1467: nRead = read(job->inPipe, &job->outBuf[job->curPos],
1468: JOB_BUFSIZE - job->curPos);
1469: if (nRead == -1) {
1470: if (DEBUG(JOB)) {
1471: perror("JobDoOutput(piperead)");
1.66 espie 1472: }
1.76 espie 1473: nr = 0;
1474: } else {
1475: nr = nRead;
1476: }
1.1 deraadt 1477:
1.76 espie 1478: /*
1.78 espie 1479: * If we hit the end-of-file (the job is dead), we must flush its
1480: * remaining output, so pretend we read a newline if there's any
1481: * output remaining in the buffer.
1482: * Also clear the 'finish' flag so we stop looping.
1.76 espie 1483: */
1484: if (nr == 0 && job->curPos != 0) {
1485: job->outBuf[job->curPos] = '\n';
1486: nr = 1;
1487: finish = false;
1488: } else if (nr == 0) {
1489: finish = false;
1490: }
1.6 millert 1491:
1.76 espie 1492: /*
1.78 espie 1493: * Look for the last newline in the bytes we just got. If there is
1494: * one, break out of the loop with 'i' as its index and gotNL set
1495: * true.
1.76 espie 1496: */
1497: max = job->curPos + nr;
1498: for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
1499: if (job->outBuf[i] == '\n') {
1500: gotNL = true;
1501: break;
1502: } else if (job->outBuf[i] == '\0') {
1503: /*
1.89 espie 1504: * To be fixed: don't use printf, it stops at NUL bytes.
1.76 espie 1505: */
1506: job->outBuf[i] = ' ';
1.66 espie 1507: }
1.76 espie 1508: }
1.6 millert 1509:
1.76 espie 1510: if (!gotNL) {
1511: job->curPos += nr;
1512: if (job->curPos == JOB_BUFSIZE) {
1.66 espie 1513: /*
1.78 espie 1514: * If we've run out of buffer space, we have no choice
1515: * but to print the stuff. sigh.
1.66 espie 1516: */
1.76 espie 1517: fbuf = true;
1518: i = job->curPos;
1519: }
1520: }
1521: if (gotNL || fbuf) {
1522: /*
1.78 espie 1523: * Need to send the output to the screen. Null terminate it
1524: * first, overwriting the newline character if there was one.
1525: * So long as the line isn't one we should filter (according
1526: * to the shell description), we print the line, preceded
1527: * by a target banner if this target isn't the same as the
1528: * one for which we last printed something.
1529: * The rest of the data in the buffer are then shifted down
1530: * to the start of the buffer and curPos is set accordingly.
1.76 espie 1531: */
1532: job->outBuf[i] = '\0';
1533: if (i >= job->curPos) {
1534: char *cp;
1.66 espie 1535:
1.78 espie 1536: cp = JobOutput(job, job->outBuf, &job->outBuf[i],
1537: false);
1.66 espie 1538:
1.76 espie 1539: /*
1.78 espie 1540: * There's still more in that thar buffer. This time,
1541: * though, we know there's no newline at the end, so we
1542: * add one of our own free will.
1.76 espie 1543: */
1544: if (*cp != '\0') {
1545: if (job->node != lastNode) {
1546: MESSAGE(stdout, job->node);
1547: lastNode = job->node;
1.66 espie 1548: }
1.76 espie 1549: (void)fprintf(stdout, "%s%s", cp,
1550: gotNL ? "\n" : "");
1551: (void)fflush(stdout);
1.66 espie 1552: }
1.76 espie 1553: }
1554: if (i < max - 1) {
1555: /* shift the remaining characters down */
1556: (void)memcpy(job->outBuf, &job->outBuf[i + 1],
1557: max - (i + 1));
1558: job->curPos = max - (i + 1);
1.66 espie 1559:
1.76 espie 1560: } else {
1.66 espie 1561: /*
1.78 espie 1562: * We have written everything out, so we just start over
1563: * from the start of the buffer. No copying. No nothing.
1.66 espie 1564: */
1.76 espie 1565: job->curPos = 0;
1.66 espie 1566: }
1.76 espie 1567: }
1568: if (finish) {
1.66 espie 1569: /*
1.78 espie 1570: * If the finish flag is true, we must loop until we hit
1571: * end-of-file on the pipe. This is guaranteed to happen
1572: * eventually since the other end of the pipe is now closed
1573: * (we closed it explicitly and the child has exited). When
1574: * we do get an EOF, finish will be set false and we'll fall
1575: * through and out.
1.76 espie 1576: */
1577: goto end_loop;
1.1 deraadt 1578: }
1579: }
1580:
1581: /*-
1582: *-----------------------------------------------------------------------
1583: * Job_CatchChildren --
1584: * Handle the exit of a child. Called from Make_Make.
1585: *
1586: * Side Effects:
1587: * The job descriptor is removed from the list of children.
1588: *
1589: * Notes:
1590: * We do waits, blocking or not, according to the wisdom of our
1591: * caller, until there are no more children to report. For each
1592: * job, call JobFinish to finish things off. This will take care of
1593: * putting jobs on the stoppedJobs queue.
1594: *-----------------------------------------------------------------------
1595: */
1596: void
1.76 espie 1597: Job_CatchChildren()
1.1 deraadt 1598: {
1.78 espie 1599: pid_t pid; /* pid of dead child */
1600: Job *job; /* job descriptor for dead child */
1601: LstNode jnode; /* list element for finding job */
1602: int status; /* Exit/termination status */
1.1 deraadt 1603:
1.66 espie 1604: /*
1605: * Don't even bother if we know there's no one around.
1606: */
1.83 espie 1607: if (nJobs == 0) {
1.66 espie 1608: return;
1.2 deraadt 1609: }
1.6 millert 1610:
1.76 espie 1611: while ((pid = waitpid((pid_t) -1, &status, WNOHANG|WUNTRACED)) > 0) {
1.93 espie 1612: handle_all_signals();
1.84 espie 1613: debug_printf("Process %ld exited or stopped.\n", (long)pid);
1.1 deraadt 1614:
1.95 espie 1615: jnode = Lst_Find(&runningJobs, JobCmpPid, &pid);
1.1 deraadt 1616:
1.18 espie 1617: if (jnode == NULL) {
1.68 espie 1618: if (WIFSIGNALED(status) &&
1.66 espie 1619: (WTERMSIG(status) == SIGCONT)) {
1620: jnode = Lst_Find(&stoppedJobs, JobCmpPid, &pid);
1621: if (jnode == NULL) {
1622: Error("Resumed child (%ld) not in table", (long)pid);
1623: continue;
1624: }
1625: job = (Job *)Lst_Datum(jnode);
1626: Lst_Remove(&stoppedJobs, jnode);
1627: } else {
1628: Error("Child (%ld) not in table?", (long)pid);
1629: continue;
1630: }
1631: } else {
1632: job = (Job *)Lst_Datum(jnode);
1.95 espie 1633: Lst_Remove(&runningJobs, jnode);
1.67 espie 1634: nJobs--;
1.84 espie 1635: if (jobFull)
1636: debug_printf("Job queue is no longer full.\n");
1.66 espie 1637: jobFull = false;
1.1 deraadt 1638: }
1.66 espie 1639:
1640: JobFinish(job, &status);
1.1 deraadt 1641: }
1642: }
1643:
1644: /*-
1645: *-----------------------------------------------------------------------
1646: * Job_CatchOutput --
1647: * Catch the output from our children, if we're using
1648: * pipes do so. Otherwise just block time until we get a
1.6 millert 1649: * signal (most likely a SIGCHLD) since there's no point in
1.1 deraadt 1650: * just spinning when there's nothing to do and the reaping
1.6 millert 1651: * of a child can wait for a while.
1.1 deraadt 1652: *
1653: * Side Effects:
1654: * Output is read from pipes if we're piping.
1655: * -----------------------------------------------------------------------
1656: */
1657: void
1.56 espie 1658: Job_CatchOutput(void)
1.1 deraadt 1659: {
1.66 espie 1660: int nfds;
1661: struct timeval timeout;
1662: LstNode ln;
1663: Job *job;
1664:
1.76 espie 1665: int count = howmany(outputsn+1, NFDBITS) * sizeof(fd_mask);
1666: fd_set *readfdsp = malloc(count);
1.91 espie 1667:
1.77 espie 1668: (void)fflush(stdout);
1.76 espie 1669: if (readfdsp == NULL)
1670: return;
1671:
1672: memcpy(readfdsp, outputsp, count);
1673: timeout.tv_sec = SEL_SEC;
1674: timeout.tv_usec = SEL_USEC;
1.66 espie 1675:
1.91 espie 1676: nfds = select(outputsn+1, readfdsp, NULL, NULL, &timeout);
1.93 espie 1677: handle_all_signals();
1.91 espie 1678: if (nfds > 0) {
1.95 espie 1679: for (ln = Lst_First(&runningJobs); nfds && ln != NULL;
1.76 espie 1680: ln = Lst_Adv(ln)) {
1681: job = (Job *)Lst_Datum(ln);
1682: if (FD_ISSET(job->inPipe, readfdsp)) {
1683: JobDoOutput(job, false);
1684: nfds--;
1.66 espie 1685: }
1.1 deraadt 1686: }
1687: }
1.76 espie 1688: free(readfdsp);
1.1 deraadt 1689: }
1690:
1691: /*-
1692: *-----------------------------------------------------------------------
1693: * Job_Make --
1694: * Start the creation of a target. Basically a front-end for
1695: * JobStart used by the Make module.
1696: *
1697: * Side Effects:
1698: * Another job is started.
1699: *-----------------------------------------------------------------------
1700: */
1701: void
1.56 espie 1702: Job_Make(GNode *gn)
1.1 deraadt 1703: {
1.86 espie 1704: (void)JobStart(gn, 0);
1.1 deraadt 1705: }
1706:
1.93 espie 1707: static void
1708: setup_signal(int sig)
1709: {
1710: if (signal(sig, SIG_IGN) != SIG_IGN) {
1711: (void)signal(sig, SigHandler);
1712: }
1713: }
1714:
1.94 espie 1715: static void
1716: setup_all_signals()
1717: {
1718: /*
1719: * Catch the four signals that POSIX specifies if they aren't ignored.
1720: * handle_signal will take care of calling JobInterrupt if appropriate.
1721: */
1722: setup_signal(SIGINT);
1723: setup_signal(SIGHUP);
1724: setup_signal(SIGQUIT);
1725: setup_signal(SIGTERM);
1726: /*
1727: * There are additional signals that need to be caught and passed if
1728: * either the export system wants to be told directly of signals or if
1729: * we're giving each job its own process group (since then it won't get
1730: * signals from the terminal driver as we own the terminal)
1731: */
1732: #if defined(USE_PGRP)
1733: setup_signal(SIGTSTP);
1734: setup_signal(SIGTTOU);
1735: setup_signal(SIGTTIN);
1736: setup_signal(SIGWINCH);
1737: #endif
1738: }
1739:
1.1 deraadt 1740: /*-
1741: *-----------------------------------------------------------------------
1742: * Job_Init --
1743: * Initialize the process module
1744: *
1745: * Side Effects:
1746: * lists and counters are initialized
1747: *-----------------------------------------------------------------------
1748: */
1749: void
1.83 espie 1750: Job_Init(int maxproc)
1.1 deraadt 1751: {
1.95 espie 1752: Static_Lst_Init(&runningJobs);
1.66 espie 1753: Static_Lst_Init(&stoppedJobs);
1.96 espie 1754: Static_Lst_Init(&queuedJobs);
1.66 espie 1755: maxJobs = maxproc;
1.79 espie 1756: nJobs = 0;
1.66 espie 1757: jobFull = false;
1758:
1759: aborting = 0;
1760: errors = 0;
1.40 espie 1761:
1.66 espie 1762: lastNode = NULL;
1.1 deraadt 1763:
1.66 espie 1764: if (maxJobs == 1) {
1765: /*
1766: * If only one job can run at a time, there's no need for a
1767: * banner, no is there?
1768: */
1769: targFmt = "";
1770: } else {
1771: targFmt = TARG_FMT;
1772: }
1.6 millert 1773:
1.78 espie 1774: if ((begin_node->type & OP_DUMMY) == 0) {
1.86 espie 1775: JobStart(begin_node, JOB_SPECIAL);
1.66 espie 1776: while (nJobs) {
1777: Job_CatchOutput();
1.76 espie 1778: Job_CatchChildren();
1.66 espie 1779: }
1.1 deraadt 1780: }
1781: }
1782:
1783: /*-
1784: *-----------------------------------------------------------------------
1785: * Job_Full --
1786: * See if the job table is full. It is considered full if it is OR
1787: * if we are in the process of aborting OR if we have
1788: * reached/exceeded our local quota. This prevents any more jobs
1789: * from starting up.
1790: *
1791: * Results:
1.41 espie 1792: * true if the job table is full, false otherwise
1.1 deraadt 1793: *-----------------------------------------------------------------------
1794: */
1.41 espie 1795: bool
1.56 espie 1796: Job_Full(void)
1.1 deraadt 1797: {
1.66 espie 1798: return aborting || jobFull;
1.1 deraadt 1799: }
1800:
1801: /*-
1802: *-----------------------------------------------------------------------
1803: * Job_Empty --
1.40 espie 1804: * See if the job table is empty. Because the local concurrency may
1.1 deraadt 1805: * be set to 0, it is possible for the job table to become empty,
1806: * while the list of stoppedJobs remains non-empty. In such a case,
1807: * we want to restart as many jobs as we can.
1808: *
1809: * Results:
1.41 espie 1810: * true if it is. false if it ain't.
1.1 deraadt 1811: * -----------------------------------------------------------------------
1812: */
1.41 espie 1813: bool
1.56 espie 1814: Job_Empty(void)
1.1 deraadt 1815: {
1.66 espie 1816: if (nJobs == 0) {
1817: if (!Lst_IsEmpty(&stoppedJobs) && !aborting) {
1818: /*
1819: * The job table is obviously not full if it has no
1820: * jobs in it...Try and restart the stopped jobs.
1821: */
1822: jobFull = false;
1823: JobRestartJobs();
1824: return false;
1825: } else {
1826: return true;
1827: }
1.1 deraadt 1828: } else {
1.66 espie 1829: return false;
1.1 deraadt 1830: }
1831: }
1832:
1833: /*-
1834: *-----------------------------------------------------------------------
1835: * JobInterrupt --
1836: * Handle the receipt of an interrupt.
1837: *
1838: * Side Effects:
1839: * All children are killed. Another job will be started if the
1840: * .INTERRUPT target was given.
1841: *-----------------------------------------------------------------------
1842: */
1843: static void
1.56 espie 1844: JobInterrupt(int runINTERRUPT, /* Non-zero if commands for the .INTERRUPT
1.1 deraadt 1845: * target should be executed */
1.66 espie 1846: int signo) /* signal received */
1.1 deraadt 1847: {
1.66 espie 1848: LstNode ln; /* element in job table */
1.78 espie 1849: Job *job; /* job descriptor in that element */
1.66 espie 1850:
1851: aborting = ABORT_INTERRUPT;
1852:
1.95 espie 1853: for (ln = Lst_First(&runningJobs); ln != NULL; ln = Lst_Adv(ln)) {
1.66 espie 1854: job = (Job *)Lst_Datum(ln);
1855:
1856: if (!Targ_Precious(job->node)) {
1857: const char *file = job->node->path == NULL ?
1858: job->node->name : job->node->path;
1859: if (!noExecute && eunlink(file) != -1) {
1860: Error("*** %s removed", file);
1861: }
1862: }
1863: if (job->pid) {
1.84 espie 1864: debug_printf("JobInterrupt passing signal to "
1865: "child %ld.\n", (long)job->pid);
1.66 espie 1866: KILL(job->pid, signo);
1867: }
1.2 deraadt 1868: }
1.1 deraadt 1869:
1.66 espie 1870: if (runINTERRUPT && !touchFlag) {
1.78 espie 1871: if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66 espie 1872: ignoreErrors = false;
1873:
1.86 espie 1874: JobStart(interrupt_node, JOB_IGNDOTS);
1.66 espie 1875: while (nJobs) {
1876: Job_CatchOutput();
1.76 espie 1877: Job_CatchChildren();
1.66 espie 1878: }
1879: }
1.1 deraadt 1880: }
1.66 espie 1881: exit(signo);
1.1 deraadt 1882: }
1883:
1884: /*
1885: *-----------------------------------------------------------------------
1.12 espie 1886: * Job_Finish --
1.1 deraadt 1887: * Do final processing such as the running of the commands
1.6 millert 1888: * attached to the .END target.
1.1 deraadt 1889: *
1890: * Results:
1891: * Number of errors reported.
1.40 espie 1892: *
1.1 deraadt 1893: *-----------------------------------------------------------------------
1894: */
1895: int
1.56 espie 1896: Job_Finish(void)
1.1 deraadt 1897: {
1.78 espie 1898: if (end_node != NULL && !Lst_IsEmpty(&end_node->commands)) {
1.66 espie 1899: if (errors) {
1900: Error("Errors reported so .END ignored");
1901: } else {
1.86 espie 1902: JobStart(end_node, JOB_SPECIAL | JOB_IGNDOTS);
1.1 deraadt 1903:
1.66 espie 1904: while (nJobs) {
1905: Job_CatchOutput();
1.76 espie 1906: Job_CatchChildren();
1.66 espie 1907: }
1908: }
1.1 deraadt 1909: }
1.66 espie 1910: return errors;
1.1 deraadt 1911: }
1912:
1.41 espie 1913: #ifdef CLEANUP
1.12 espie 1914: void
1.56 espie 1915: Job_End(void)
1.12 espie 1916: {
1.41 espie 1917: }
1.13 espie 1918: #endif
1.40 espie 1919:
1.1 deraadt 1920: /*-
1921: *-----------------------------------------------------------------------
1922: * Job_Wait --
1923: * Waits for all running jobs to finish and returns. Sets 'aborting'
1924: * to ABORT_WAIT to prevent other jobs from starting.
1925: *
1926: * Side Effects:
1927: * Currently running jobs finish.
1928: *
1929: *-----------------------------------------------------------------------
1930: */
1931: void
1.56 espie 1932: Job_Wait(void)
1.1 deraadt 1933: {
1.66 espie 1934: aborting = ABORT_WAIT;
1935: while (nJobs != 0) {
1936: Job_CatchOutput();
1.76 espie 1937: Job_CatchChildren();
1.66 espie 1938: }
1939: aborting = 0;
1.1 deraadt 1940: }
1941:
1942: /*-
1943: *-----------------------------------------------------------------------
1944: * Job_AbortAll --
1945: * Abort all currently running jobs without handling output or anything.
1946: * This function is to be called only in the event of a major
1947: * error. Most definitely NOT to be called from JobInterrupt.
1948: *
1949: * Side Effects:
1950: * All children are killed, not just the firstborn
1951: *-----------------------------------------------------------------------
1952: */
1953: void
1.56 espie 1954: Job_AbortAll(void)
1.1 deraadt 1955: {
1.66 espie 1956: LstNode ln; /* element in job table */
1957: Job *job; /* the job descriptor in that element */
1958: int foo;
1.6 millert 1959:
1.66 espie 1960: aborting = ABORT_ERROR;
1.6 millert 1961:
1.66 espie 1962: if (nJobs) {
1.95 espie 1963: for (ln = Lst_First(&runningJobs); ln != NULL;
1964: ln = Lst_Adv(ln)) {
1.66 espie 1965: job = (Job *)Lst_Datum(ln);
1966:
1967: /*
1968: * kill the child process with increasingly drastic
1969: * signals to make darn sure it's dead.
1970: */
1971: KILL(job->pid, SIGINT);
1972: KILL(job->pid, SIGKILL);
1973: }
1.1 deraadt 1974: }
1.6 millert 1975:
1.66 espie 1976: /*
1977: * Catch as many children as want to report in at first, then give up
1978: */
1979: while (waitpid(-1, &foo, WNOHANG) > 0)
1980: continue;
1.2 deraadt 1981: }
1.40 espie 1982:
1.2 deraadt 1983: /*-
1984: *-----------------------------------------------------------------------
1985: * JobRestartJobs --
1986: * Tries to restart stopped jobs if there are slots available.
1987: * Note that this tries to restart them regardless of pending errors.
1988: * It's not good to leave stopped jobs lying around!
1989: *
1990: * Side Effects:
1991: * Resumes(and possibly migrates) jobs.
1992: *-----------------------------------------------------------------------
1993: */
1994: static void
1.56 espie 1995: JobRestartJobs(void)
1.2 deraadt 1996: {
1.66 espie 1997: Job *job;
1.19 espie 1998:
1.66 espie 1999: while (!jobFull && (job = (Job *)Lst_DeQueue(&stoppedJobs)) != NULL) {
1.84 espie 2000: debug_printf("Job queue is not full. "
2001: "Restarting a stopped job.\n");
1.66 espie 2002: JobRestart(job);
1.2 deraadt 2003: }
1.1 deraadt 2004: }