Annotation of src/usr.bin/make/job.c, Revision 1.89
1.40 espie 1: /* $OpenPackages$ */
1.81 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
126: * 3) A LstNode for the first command to be saved after the job
127: * completes. This is NULL if there was no "..." in the job's
128: * commands.
129: * 4) An FILE* for writing out the commands. This is only
130: * used before the job is actually started.
1.78 espie 131: * 5) Things used for handling the shell's output.
1.76 espie 132: * the output is being caught via a pipe and
1.50 espie 133: * the descriptors of our pipe, an array in which output is line
134: * buffered and the current position in that buffer are all
1.78 espie 135: * maintained for each job.
1.50 espie 136: * 6) An identifier provided by and for the exclusive use of the
137: * Rmt module.
138: * 7) A word of flags which determine how the module handles errors,
139: * echoing, etc. for the job
140: *
141: * The job "table" is kept as a linked Lst in 'jobs', with the number of
142: * active jobs maintained in the 'nJobs' variable. At no time will this
143: * exceed the value of 'maxJobs', initialized by the Job_Init function.
144: *
145: * When a job is finished, the Make_Update function is called on each of the
146: * parents of the node which was just remade. This takes care of the upward
147: * traversal of the dependency graph.
148: */
149: #define JOB_BUFSIZE 1024
150: typedef struct Job_ {
1.51 mpech 151: pid_t pid; /* The child's process ID */
1.50 espie 152: GNode *node; /* The target the child is making */
153: LstNode tailCmds; /* The node of the first command to be
154: * saved when the job has been run */
155: FILE *cmdFILE; /* When creating the shell script, this is
156: * where the commands go */
157: int rmtID; /* ID returned from Rmt module */
158: short flags; /* Flags to control treatment of job */
159: #define JOB_IGNERR 0x001 /* Ignore non-zero exits */
160: #define JOB_SILENT 0x002 /* no output */
1.83 espie 161: #define JOB_SPECIAL 0x004 /* Target is a special one. */
1.79 espie 162: #define JOB_IGNDOTS 0x008 /* Ignore "..." lines when processing
163: * commands */
1.50 espie 164: #define JOB_RESTART 0x080 /* Job needs to be completely restarted */
1.79 espie 165: #define JOB_RESUME 0x100 /* Job needs to be resumed b/c it stopped,
166: * for some reason */
167: #define JOB_CONTINUING 0x200 /* We are in the process of resuming this job.
168: * Used to avoid infinite recursion between
169: * JobFinish and JobRestart */
1.76 espie 170: int inPipe; /* Input side of pipe associated
171: * with job's output channel */
172: int outPipe; /* Output side of pipe associated with
173: * job's output channel */
174: char outBuf[JOB_BUFSIZE + 1];
175: /* Buffer for storing the output of the
176: * job, line by line */
177: int curPos; /* Current position in op_outBuf */
1.50 espie 178: } Job;
179:
1.78 espie 180:
1.1 deraadt 181: /*
1.6 millert 182: * error handling variables
1.1 deraadt 183: */
1.40 espie 184: static int errors = 0; /* number of errors reported */
185: static int aborting = 0; /* why is the make aborting? */
186: #define ABORT_ERROR 1 /* Because of an error */
187: #define ABORT_INTERRUPT 2 /* Because it was interrupted */
188: #define ABORT_WAIT 3 /* Waiting for jobs to finish */
1.1 deraadt 189:
1.40 espie 190: static int numCommands; /* The number of commands actually printed
1.1 deraadt 191: * for a target. Should this number be
192: * 0, no shell will be executed. */
193:
194: /*
195: * Return values from JobStart.
196: */
1.40 espie 197: #define JOB_RUNNING 0 /* Job is running */
198: #define JOB_ERROR 1 /* Error in starting the job */
199: #define JOB_FINISHED 2 /* The job is already finished */
200: #define JOB_STOPPED 3 /* The job is stopped */
1.1 deraadt 201:
1.70 espie 202: #define SHELL_ECHO_OFF "set -"
203: #define SHELL_ECHO_ON "set -v"
204: #define SHELL_ERROR_ON "set -e"
205: #define SHELL_ERROR_OFF "set +e"
206: #define SHELL_ECHO_FLAG "v"
207: #define SHELL_ERROR_FLAG "e"
1.1 deraadt 208:
1.70 espie 209: static const char *shellPath = _PATH_BSHELL;
210: static const char *shellName = "sh";
1.1 deraadt 211:
1.78 espie 212:
1.40 espie 213: static int maxJobs; /* The most children we can run at once */
1.83 espie 214: static int nJobs; /* The number of children currently running */
1.48 espie 215: static LIST jobs; /* The structures that describe them */
1.79 espie 216: static bool jobFull; /* Flag to tell when the job table is full. It
1.83 espie 217: * is set true when nJobs equals maxJobs */
1.40 espie 218: static fd_set *outputsp; /* Set of descriptors of pipes connected to
1.1 deraadt 219: * the output channels of children */
1.8 deraadt 220: static int outputsn;
1.48 espie 221: static GNode *lastNode; /* The node for which output was most recently
1.1 deraadt 222: * produced. */
1.48 espie 223: static char *targFmt; /* Format string to use to head output from a
1.1 deraadt 224: * job when it's not the most-recent job heard
225: * from */
1.2 deraadt 226:
227: # define TARG_FMT "--- %s ---\n" /* Default format */
228: # define MESSAGE(fp, gn) \
1.40 espie 229: (void)fprintf(fp, targFmt, gn->name);
1.1 deraadt 230:
231: /*
1.50 espie 232: * When JobStart attempts to run a job but isn't allowed to,
233: * the job is placed on the stoppedJobs queue to be run
1.6 millert 234: * when the next job finishes.
1.1 deraadt 235: */
1.48 espie 236: static LIST stoppedJobs; /* Lst of Job structures describing
1.1 deraadt 237: * jobs that were stopped due to concurrency
238: * limits or migration home */
239:
240:
241: #if defined(USE_PGRP) && defined(SYSV)
1.40 espie 242: # define KILL(pid, sig) killpg(-(pid), (sig))
1.1 deraadt 243: #else
244: # if defined(USE_PGRP)
1.2 deraadt 245: # define KILL(pid, sig) killpg((pid), (sig))
1.1 deraadt 246: # else
1.2 deraadt 247: # define KILL(pid, sig) kill((pid), (sig))
1.1 deraadt 248: # endif
249: #endif
250:
1.6 millert 251: /*
1.2 deraadt 252: * Grmpf... There is no way to set bits of the wait structure
253: * anymore with the stupid W*() macros. I liked the union wait
254: * stuff much more. So, we devise our own macros... This is
255: * really ugly, use dramamine sparingly. You have been warned.
256: */
257: #define W_SETMASKED(st, val, fun) \
258: { \
259: int sh = (int) ~0; \
260: int mask = fun(sh); \
261: \
262: for (sh = 0; ((mask >> sh) & 1) == 0; sh++) \
263: continue; \
264: *(st) = (*(st) & ~mask) | ((val) << sh); \
265: }
266:
267: #define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG)
268: #define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS)
269:
270:
1.40 espie 271: static void JobCondPassSig(void *, void *);
1.57 espie 272: static void SigHandler(int);
273: static void HandleSigs(void);
1.40 espie 274: static void JobPassSig(int);
275: static int JobCmpPid(void *, void *);
1.60 espie 276: static int JobPrintCommand(LstNode, void *);
1.40 espie 277: static void JobSaveCommand(void *, void *);
278: static void JobClose(Job *);
279: static void JobFinish(Job *, int *);
280: static void JobExec(Job *, char **);
281: static void JobMakeArgv(Job *, char **);
282: static void JobRestart(Job *);
1.86 espie 283: static int JobStart(GNode *, int);
1.40 espie 284: static char *JobOutput(Job *, char *, char *, int);
1.41 espie 285: static void JobDoOutput(Job *, bool);
1.40 espie 286: static void JobInterrupt(int, int);
287: static void JobRestartJobs(void);
1.69 espie 288: static void DBPRINTF(Job *, const char *, ...);
1.84 espie 289: static void debug_printf(const char *, ...);
1.88 espie 290: static FILE *new_command_file(void);
1.1 deraadt 291:
1.57 espie 292: static volatile sig_atomic_t got_SIGINT, got_SIGHUP, got_SIGQUIT,
293: got_SIGTERM;
294: #if defined(USE_PGRP)
295: static volatile sig_atomic_t got_SIGTSTP, got_SIGTTOU, got_SIGTTIN,
296: got_SIGWINCH;
297: #endif
298:
1.88 espie 299: #define TMPPAT "/tmp/makeXXXXXXXXXX"
300:
301: static FILE *
302: new_command_file()
303: {
304: int fd;
305: FILE *f;
306: char tmp[] = TMPPAT;
307:
308: fd = mkstemp(tmp);
309: if (fd == -1)
310: return NULL;
311: f = fdopen(fd, "w");
312: if (f == NULL)
313: close(fd);
314: eunlink(tmp);
315: return f;
316: }
317:
1.57 espie 318: static void
319: SigHandler(int sig)
320: {
321: switch(sig) {
322: case SIGINT:
323: got_SIGINT++;
324: break;
325: case SIGHUP:
326: got_SIGHUP++;
327: break;
328: case SIGQUIT:
329: got_SIGQUIT++;
330: break;
331: case SIGTERM:
332: got_SIGTERM++;
333: break;
334: #if defined(USE_PGRGP)
335: case SIGTSTP:
336: got_SIGTSTP++;
337: break;
338: case SIGTTOU:
339: got_SIGTTOU++;
340: break;
341: case SIGTTIN:
342: got_SIGTTIN++;
343: break;
344: case SIGWINCH:
345: got_SIGWINCH++;
346: break;
347: #endif
348: }
349: }
350:
351: static void
352: HandleSigs()
353: {
354: if (got_SIGINT) {
355: got_SIGINT=0;
356: JobPassSig(SIGINT);
357: }
358: if (got_SIGHUP) {
359: got_SIGHUP=0;
360: JobPassSig(SIGHUP);
361: }
362: if (got_SIGQUIT) {
363: got_SIGQUIT=0;
364: JobPassSig(SIGQUIT);
365: }
366: if (got_SIGTERM) {
367: got_SIGTERM=0;
368: JobPassSig(SIGTERM);
369: }
370: #if defined(USE_PGRP)
371: if (got_SIGTSTP) {
372: got_SIGTSTP=0;
373: JobPassSig(SIGTSTP);
374: }
375: if (got_SIGTTOU) {
376: got_SIGTTOU=0;
377: JobPassSig(SIGTTOU);
378: }
379: if (got_SIGTTIN) {
380: got_SIGTTIN=0;
381: JobPassSig(SIGTTIN);
382: }
383: if (got_SIGWINCH) {
384: got_SIGWINCH=0;
385: JobPassSig(SIGWINCH);
386: }
387: #endif
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.56 espie 401: JobCondPassSig(void *jobp, /* Job to biff */
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,
408: "JobCondPassSig passing signal %d to child %ld.\n",
409: signo, (long)job->pid);
410: (void)fflush(stdout);
411: }
412: KILL(job->pid, signo);
1.1 deraadt 413: }
414:
415: /*-
416: *-----------------------------------------------------------------------
417: * JobPassSig --
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.56 espie 426: JobPassSig(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)) {
432: (void)fprintf(stdout, "JobPassSig(%d) called.\n", signo);
433: (void)fflush(stdout);
434: }
435: Lst_ForEach(&jobs, JobCondPassSig, &signo);
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,
473: "JobPassSig passing signal to self, mask = %x.\n",
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;
482: Lst_ForEach(&jobs, JobCondPassSig, &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;
844: Lst_AtEnd(&jobs, 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.6 millert 922:
1.66 espie 923: if (DEBUG(JOB)) {
1.78 espie 924: int i;
1.6 millert 925:
1.66 espie 926: (void)fprintf(stdout, "Running %s\n", job->node->name);
927: (void)fprintf(stdout, "\tCommand: ");
928: for (i = 0; argv[i] != NULL; i++) {
929: (void)fprintf(stdout, "%s ", argv[i]);
930: }
931: (void)fprintf(stdout, "\n");
932: (void)fflush(stdout);
1.1 deraadt 933: }
1.6 millert 934:
1.66 espie 935: /*
936: * Some jobs produce no output and it's disconcerting to have
937: * no feedback of their running (since they produce no output, the
938: * banner with their name in it never appears). This is an attempt to
939: * provide that feedback, even if nothing follows it.
940: */
1.87 espie 941: if (lastNode != job->node && !(job->flags & JOB_SILENT)) {
1.66 espie 942: MESSAGE(stdout, job->node);
943: lastNode = job->node;
944: }
1.1 deraadt 945:
1.66 espie 946: if ((cpid = fork()) == -1) {
947: Punt("Cannot fork");
948: } else if (cpid == 0) {
1.6 millert 949:
1.66 espie 950: /*
951: * Must duplicate the input stream down to the child's input
952: * and reset it to the beginning (again). Since the stream was
953: * marked close-on-exec, we must clear that bit in the new
954: * input.
955: */
1.81 espie 956: if (dup2(fileno(job->cmdFILE), 0) == -1)
1.82 espie 957: Punt("Cannot dup2(job->cmdFile): %s", strerror(errno));
1.66 espie 958: (void)fcntl(0, F_SETFD, 0);
959: (void)lseek(0, 0, SEEK_SET);
960:
1.76 espie 961: /*
1.78 espie 962: * Set up the child's output to be routed through the pipe
963: * we've created for it.
1.76 espie 964: */
965: if (dup2(job->outPipe, 1) == -1)
1.82 espie 966: Punt("Cannot dup2(job->outPipe): %s", strerror(errno));
1.66 espie 967: /*
968: * The output channels are marked close on exec. This bit was
969: * duplicated by the dup2 (on some systems), so we have to
970: * clear it before routing the shell's error output to the same
971: * place as its standard output.
972: */
973: (void)fcntl(1, F_SETFD, 0);
974: if (dup2(1, 2) == -1)
1.82 espie 975: Punt("Cannot dup2(stdout): %s", strerror(errno));
1.1 deraadt 976:
977: #ifdef USE_PGRP
1.66 espie 978: /*
979: * We want to switch the child into a different process family
980: * so we can kill it and all its descendants in one fell swoop,
981: * by killing its process family, but not commit suicide.
982: */
1.2 deraadt 983: # if defined(SYSV)
1.66 espie 984: (void)setsid();
1.2 deraadt 985: # else
1.66 espie 986: (void)setpgid(0, getpid());
1.2 deraadt 987: # endif
988: #endif /* USE_PGRP */
1.1 deraadt 989:
1.66 espie 990: (void)execv(shellPath, argv);
991:
992: (void)write(STDERR_FILENO, "Could not execute shell\n",
993: sizeof("Could not execute shell"));
994: _exit(1);
995: } else {
996: job->pid = cpid;
997:
1.87 espie 998: /* we set the current position in the buffer to the beginning
999: * and mark another stream to watch in the outputs mask
1000: */
1001: job->curPos = 0;
1.66 espie 1002:
1.87 espie 1003: if (outputsp == NULL || job->inPipe > outputsn) {
1004: int bytes, obytes;
1005: char *tmp;
1006:
1007: bytes = howmany(job->inPipe+1, NFDBITS) *
1008: sizeof(fd_mask);
1009: obytes = outputsn ?
1010: howmany(outputsn+1, NFDBITS) *
1011: sizeof(fd_mask) : 0;
1012:
1013: if (bytes != obytes) {
1014: tmp = realloc(outputsp, bytes);
1015: if (tmp == NULL)
1016: return;
1017: memset(tmp + obytes, 0, bytes - obytes);
1018: outputsp = (fd_set *)tmp;
1.66 espie 1019: }
1.87 espie 1020: outputsn = job->inPipe;
1.66 espie 1021: }
1.87 espie 1022: FD_SET(job->inPipe, outputsp);
1.2 deraadt 1023:
1.66 espie 1024: /*
1025: * XXX: Used to not happen if REMOTE. Why?
1026: */
1027: if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1028: (void)fclose(job->cmdFILE);
1029: job->cmdFILE = NULL;
1030: }
1.1 deraadt 1031: }
1032:
1.48 espie 1033: /*
1.66 espie 1034: * Now the job is actually running, add it to the table.
1.48 espie 1035: */
1.67 espie 1036: nJobs++;
1.66 espie 1037: Lst_AtEnd(&jobs, job);
1038: if (nJobs == maxJobs) {
1039: jobFull = true;
1.1 deraadt 1040: }
1041: }
1042:
1043: /*-
1044: *-----------------------------------------------------------------------
1045: * JobMakeArgv --
1046: * Create the argv needed to execute the shell for a given job.
1047: *-----------------------------------------------------------------------
1048: */
1049: static void
1.56 espie 1050: JobMakeArgv(Job *job, char **argv)
1.1 deraadt 1051: {
1.66 espie 1052: int argc;
1053: static char args[10]; /* For merged arguments */
1.6 millert 1054:
1.70 espie 1055: argv[0] = (char *)shellName;
1.66 espie 1056: argc = 1;
1.1 deraadt 1057:
1.73 espie 1058: (void)snprintf(args, sizeof(args), "-%s%s",
1059: (job->flags & JOB_IGNERR) ? "" : SHELL_ERROR_FLAG,
1060: (job->flags & JOB_SILENT) ? "" : SHELL_ECHO_FLAG);
1061:
1062: if (args[1]) {
1063: argv[argc] = args;
1064: argc++;
1.78 espie 1065: }
1.66 espie 1066: argv[argc] = NULL;
1.1 deraadt 1067: }
1068:
1069: /*-
1070: *-----------------------------------------------------------------------
1071: * JobRestart --
1.6 millert 1072: * Restart a job that stopped for some reason.
1.1 deraadt 1073: *
1074: * Side Effects:
1075: * jobFull will be set if the job couldn't be run.
1076: *-----------------------------------------------------------------------
1077: */
1078: static void
1.56 espie 1079: JobRestart(Job *job)
1.1 deraadt 1080: {
1.66 espie 1081: if (job->flags & JOB_RESTART) {
1082: /*
1083: * Set up the control arguments to the shell. This is based on
1084: * the flags set earlier for this job. If the JOB_IGNERR flag
1085: * is clear, the 'exit' flag of the commandShell is used to
1086: * cause it to exit upon receiving an error. If the JOB_SILENT
1087: * flag is clear, the 'echo' flag of the commandShell is used
1088: * to get it to start echoing as soon as it starts processing
1089: * commands.
1090: */
1091: char *argv[4];
1.6 millert 1092:
1.66 espie 1093: JobMakeArgv(job, argv);
1.2 deraadt 1094:
1.1 deraadt 1095: if (DEBUG(JOB)) {
1.68 espie 1096: (void)fprintf(stdout, "Restarting %s...",
1.66 espie 1097: job->node->name);
1098: (void)fflush(stdout);
1.1 deraadt 1099: }
1.83 espie 1100: if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) {
1.80 espie 1101: /*
1102: * Can't be exported and not allowed to run locally --
1103: * put it back on the hold queue and mark the table
1104: * full
1105: */
1.84 espie 1106: debug_printf("holding\n");
1.80 espie 1107: Lst_AtFront(&stoppedJobs, job);
1108: jobFull = true;
1.84 espie 1109: debug_printf("Job queue is full.\n");
1.80 espie 1110: return;
1.66 espie 1111: } else {
1.80 espie 1112: /*
1113: * Job may be run locally.
1114: */
1.84 espie 1115: debug_printf("running locally\n");
1.1 deraadt 1116: }
1.66 espie 1117: JobExec(job, argv);
1118: } else {
1.1 deraadt 1119: /*
1.66 espie 1120: * The job has stopped and needs to be restarted. Why it
1121: * stopped, we don't know...
1.1 deraadt 1122: */
1.84 espie 1123: debug_printf("Resuming %s...", job->node->name);
1.83 espie 1124: if ((nJobs < maxJobs || ((job->flags & JOB_SPECIAL) &&
1125: maxJobs == 0)) && nJobs != maxJobs) {
1.66 espie 1126: /*
1.79 espie 1127: * If we haven't reached the concurrency limit already
1.83 espie 1128: * (or maxJobs is 0), it's ok to resume the job.
1.66 espie 1129: */
1130: bool error;
1131: int status;
1132:
1133: error = KILL(job->pid, SIGCONT) != 0;
1134:
1135: if (!error) {
1136: /*
1137: * Make sure the user knows we've continued the
1138: * beast and actually put the thing in the job
1139: * table.
1140: */
1141: job->flags |= JOB_CONTINUING;
1142: W_SETTERMSIG(&status, SIGCONT);
1143: JobFinish(job, &status);
1144:
1145: job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1.84 espie 1146: debug_printf("done\n");
1.66 espie 1147: } else {
1148: Error("couldn't resume %s: %s",
1149: job->node->name, strerror(errno));
1150: status = 0;
1151: W_SETEXITSTATUS(&status, 1);
1152: JobFinish(job, &status);
1153: }
1154: } else {
1155: /*
1156: * Job cannot be restarted. Mark the table as full and
1157: * place the job back on the list of stopped jobs.
1158: */
1.84 espie 1159: debug_printf("table full\n");
1.66 espie 1160: Lst_AtFront(&stoppedJobs, job);
1161: jobFull = true;
1.84 espie 1162: debug_printf("Job queue is full.\n");
1.1 deraadt 1163: }
1164: }
1165: }
1166:
1167: /*-
1168: *-----------------------------------------------------------------------
1169: * JobStart --
1170: * Start a target-creation process going for the target described
1.6 millert 1171: * by the graph node gn.
1.1 deraadt 1172: *
1173: * Results:
1174: * JOB_ERROR if there was an error in the commands, JOB_FINISHED
1175: * if there isn't actually anything left to do for the job and
1176: * JOB_RUNNING if the job has been started.
1177: *
1178: * Side Effects:
1179: * A new Job node is created and added to the list of running
1180: * jobs. PMake is forked and a child shell created.
1181: *-----------------------------------------------------------------------
1182: */
1183: static int
1.78 espie 1184: JobStart(GNode *gn, /* target to create */
1.86 espie 1185: int flags) /* flags for the job to override normal ones.
1186: * e.g. JOB_SPECIAL */
1.78 espie 1187: {
1188: Job *job; /* new job descriptor */
1189: char *argv[4]; /* Argument vector to shell */
1190: bool cmdsOK; /* true if the nodes commands were all right */
1191: bool noExec; /* Set true if we decide not to run the job */
1.87 espie 1192: int fd[2];
1.66 espie 1193:
1.86 espie 1194: job = emalloc(sizeof(Job));
1195: if (job == NULL) {
1196: Punt("JobStart out of memory");
1.1 deraadt 1197: }
1198:
1.66 espie 1199: job->node = gn;
1200: job->tailCmds = NULL;
1.1 deraadt 1201:
1202: /*
1.66 espie 1203: * Set the initial value of the flags for this job based on the global
1204: * ones and the node's attributes... Any flags supplied by the caller
1205: * are also added to the field.
1.1 deraadt 1206: */
1.87 espie 1207: job->flags = flags;
1.66 espie 1208: if (Targ_Ignore(gn)) {
1209: job->flags |= JOB_IGNERR;
1210: }
1211: if (Targ_Silent(gn)) {
1212: job->flags |= JOB_SILENT;
1.1 deraadt 1213: }
1.6 millert 1214:
1.1 deraadt 1215: /*
1.66 espie 1216: * Check the commands now so any attributes from .DEFAULT have a chance
1217: * to migrate to the node
1.1 deraadt 1218: */
1.87 espie 1219: cmdsOK = Job_CheckCommands(gn, Error);
1.1 deraadt 1220:
1221: /*
1.66 espie 1222: * If the -n flag wasn't given, we open up OUR (not the child's)
1223: * temporary file to stuff commands in it. The thing is rd/wr so we
1224: * don't need to reopen it to feed it to the shell. If the -n flag
1225: * *was* given, we just set the file to be stdout. Cute, huh?
1.1 deraadt 1226: */
1.66 espie 1227: if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
1228: /*
1229: * We're serious here, but if the commands were bogus, we're
1230: * also dead...
1231: */
1232: if (!cmdsOK) {
1233: DieHorribly();
1234: }
1.6 millert 1235:
1.88 espie 1236: job->cmdFILE = new_command_file();
1.66 espie 1237: if (job->cmdFILE == NULL) {
1.88 espie 1238: Punt("Error creating command file");
1.66 espie 1239: }
1.81 espie 1240: (void)fcntl(fileno(job->cmdFILE), F_SETFD, 1);
1.66 espie 1241: /*
1242: * Send the commands to the command file, flush all its buffers
1243: * then rewind and remove the thing.
1244: */
1245: noExec = false;
1246:
1247: /*
1.85 espie 1248: * We can do all the commands at once. hooray for
1249: * sanity
1.66 espie 1250: */
1.85 espie 1251: numCommands = 0;
1252: Lst_ForEachNodeWhile(&gn->commands, JobPrintCommand,
1253: job);
1.66 espie 1254:
1.85 espie 1255: /*
1256: * If we didn't print out any commands to the shell
1257: * script, there's not much point in executing the
1258: * shell, is there?
1259: */
1260: if (numCommands == 0) {
1261: noExec = true;
1.66 espie 1262: }
1263: } else if (noExecute) {
1264: /*
1265: * Not executing anything -- just print all the commands to
1266: * stdout in one fell swoop. This will still set up
1267: * job->tailCmds correctly.
1268: */
1269: if (lastNode != gn) {
1270: MESSAGE(stdout, gn);
1271: lastNode = gn;
1272: }
1273: job->cmdFILE = stdout;
1274: /*
1275: * Only print the commands if they're ok, but don't die if
1276: * they're not -- just let the user know they're bad and keep
1277: * going. It doesn't do any harm in this case and may do some
1278: * good.
1279: */
1280: if (cmdsOK) {
1.68 espie 1281: Lst_ForEachNodeWhile(&gn->commands, JobPrintCommand,
1.66 espie 1282: job);
1283: }
1284: /*
1285: * Don't execute the shell, thank you.
1286: */
1.41 espie 1287: noExec = true;
1.66 espie 1288: } else {
1289: /*
1290: * Just touch the target and note that no shell should be
1291: * executed. Set cmdFILE to stdout to make life easier. Check
1292: * the commands, too, but don't die if they're no good -- it
1293: * does no harm to keep working up the graph.
1.30 espie 1294: */
1.66 espie 1295: job->cmdFILE = stdout;
1.87 espie 1296: Job_Touch(gn, job->flags & JOB_SILENT);
1.41 espie 1297: noExec = true;
1.1 deraadt 1298: }
1.66 espie 1299:
1.1 deraadt 1300: /*
1.66 espie 1301: * If we're not supposed to execute a shell, don't.
1.1 deraadt 1302: */
1.66 espie 1303: if (noExec) {
1304: /*
1305: * Unlink and close the command file if we opened one
1306: */
1307: if (job->cmdFILE != stdout) {
1308: if (job->cmdFILE != NULL)
1309: (void)fclose(job->cmdFILE);
1310: } else {
1311: (void)fflush(stdout);
1312: }
1.1 deraadt 1313:
1.66 espie 1314: /*
1315: * We only want to work our way up the graph if we aren't here
1316: * because the commands for the job were no good.
1317: */
1318: if (cmdsOK) {
1319: if (aborting == 0) {
1.68 espie 1320: Lst_ForEachFrom(job->tailCmds, JobSaveCommand,
1.66 espie 1321: job->node);
1322: Make_Update(job->node);
1323: }
1324: free(job);
1325: return JOB_FINISHED;
1326: } else {
1327: free(job);
1328: return JOB_ERROR;
1329: }
1.1 deraadt 1330: } else {
1.66 espie 1331: (void)fflush(job->cmdFILE);
1.1 deraadt 1332: }
1333:
1334: /*
1.66 espie 1335: * Set up the control arguments to the shell. This is based on the flags
1336: * set earlier for this job.
1.1 deraadt 1337: */
1.66 espie 1338: JobMakeArgv(job, argv);
1.1 deraadt 1339:
1.66 espie 1340: /*
1341: * If we're using pipes to catch output, create the pipe by which we'll
1342: * get the shell's output. If we're using files, print out that we're
1343: * starting a job and then set up its temporary-file name.
1344: */
1.87 espie 1345: if (pipe(fd) == -1)
1346: Punt("Cannot create pipe: %s", strerror(errno));
1347: job->inPipe = fd[0];
1348: job->outPipe = fd[1];
1349: (void)fcntl(job->inPipe, F_SETFD, 1);
1350: (void)fcntl(job->outPipe, F_SETFD, 1);
1.1 deraadt 1351:
1.83 espie 1352: if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) &&
1353: maxJobs != 0) {
1.79 espie 1354: /*
1355: * The job can only be run locally, but we've hit the limit of
1356: * local concurrency, so put the job on hold until some other
1357: * job finishes. Note that the special jobs (.BEGIN, .INTERRUPT
1358: * and .END) may be run locally even when the local limit has
1.83 espie 1359: * been reached (e.g. when maxJobs == 0), though they will be
1.79 espie 1360: * exported if at all possible. In addition, any target marked
1.83 espie 1361: * with .NOEXPORT will be run locally if maxJobs is 0.
1.66 espie 1362: */
1363: jobFull = true;
1.6 millert 1364:
1.84 espie 1365: debug_printf("Can only run job locally.\n");
1.66 espie 1366: job->flags |= JOB_RESTART;
1367: Lst_AtEnd(&stoppedJobs, job);
1.79 espie 1368: } else {
1.83 espie 1369: if (nJobs >= maxJobs) {
1.79 espie 1370: /*
1371: * If we're running this job locally as a special case
1372: * (see above), at least say the table is full.
1373: */
1374: jobFull = true;
1.84 espie 1375: debug_printf("Local job queue is full.\n");
1.79 espie 1376: }
1377: JobExec(job, argv);
1.1 deraadt 1378: }
1.66 espie 1379: return JOB_RUNNING;
1.1 deraadt 1380: }
1381:
1.6 millert 1382: static char *
1.56 espie 1383: JobOutput(Job *job, char *cp, char *endp, int msg)
1.2 deraadt 1384: {
1.66 espie 1385: char *ecp;
1.2 deraadt 1386:
1.74 espie 1387: ecp = strstr(cp, SHELL_ECHO_OFF);
1388: while (ecp != NULL) {
1389: if (cp != ecp) {
1390: *ecp = '\0';
1391: if (msg && job->node != lastNode) {
1392: MESSAGE(stdout, job->node);
1393: lastNode = job->node;
1.66 espie 1394: }
1.74 espie 1395: /*
1.78 espie 1396: * The only way there wouldn't be a newline after
1397: * this line is if it were the last in the buffer.
1398: * however, since the non-printable comes after it,
1399: * there must be a newline, so we don't print one.
1.74 espie 1400: */
1401: (void)fprintf(stdout, "%s", cp);
1402: (void)fflush(stdout);
1403: }
1404: cp = ecp + strlen(SHELL_ECHO_OFF);
1405: if (cp != endp) {
1406: /*
1.78 espie 1407: * Still more to print, look again after skipping
1408: * the whitespace following the non-printable
1409: * command....
1.74 espie 1410: */
1411: cp++;
1.78 espie 1412: while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
1.66 espie 1413: cp++;
1414: }
1.74 espie 1415: ecp = strstr(cp, SHELL_ECHO_OFF);
1416: } else {
1417: return cp;
1.2 deraadt 1418: }
1419: }
1.66 espie 1420: return cp;
1.2 deraadt 1421: }
1422:
1.1 deraadt 1423: /*-
1424: *-----------------------------------------------------------------------
1.40 espie 1425: * JobDoOutput --
1.89 ! espie 1426: * This functions is called whenever there is something to read on the
! 1427: * pipe. We collect more output from the given job and store it in the
! 1428: * job's outBuf. If this makes up a line, we print it tagged by the job's
! 1429: * identifier, as necessary.
! 1430: * We also keep our figurative eye out for the
1.1 deraadt 1431: * 'noPrint' line for the shell from which the output came. If
1432: * we recognize a line, we don't print it. If the command is not
1.6 millert 1433: * alone on the line (the character after it is not \0 or \n), we
1.1 deraadt 1434: * do print whatever follows it.
1435: *
1436: * Side Effects:
1437: * curPos may be shifted as may the contents of outBuf.
1438: *-----------------------------------------------------------------------
1439: */
1.48 espie 1440: static void
1.66 espie 1441: JobDoOutput(Job *job, /* the job whose output needs printing */
1442: bool finish) /* true if this is the last time we'll be
1443: * called for this job */
1444: {
1.78 espie 1445: bool gotNL = false; /* true if got a newline */
1.66 espie 1446: bool fbuf; /* true if our buffer filled up */
1447: int nr; /* number of bytes read */
1448: int i; /* auxiliary index into outBuf */
1.78 espie 1449: int max; /* limit for i (end of current data) */
1450: int nRead; /* (Temporary) number of bytes read */
1.1 deraadt 1451:
1.76 espie 1452: /*
1453: * Read as many bytes as will fit in the buffer.
1454: */
1.1 deraadt 1455: end_loop:
1.76 espie 1456: gotNL = false;
1457: fbuf = false;
1.6 millert 1458:
1.76 espie 1459: nRead = read(job->inPipe, &job->outBuf[job->curPos],
1460: JOB_BUFSIZE - job->curPos);
1461: if (nRead == -1) {
1462: if (DEBUG(JOB)) {
1463: perror("JobDoOutput(piperead)");
1.66 espie 1464: }
1.76 espie 1465: nr = 0;
1466: } else {
1467: nr = nRead;
1468: }
1.1 deraadt 1469:
1.76 espie 1470: /*
1.78 espie 1471: * If we hit the end-of-file (the job is dead), we must flush its
1472: * remaining output, so pretend we read a newline if there's any
1473: * output remaining in the buffer.
1474: * Also clear the 'finish' flag so we stop looping.
1.76 espie 1475: */
1476: if (nr == 0 && job->curPos != 0) {
1477: job->outBuf[job->curPos] = '\n';
1478: nr = 1;
1479: finish = false;
1480: } else if (nr == 0) {
1481: finish = false;
1482: }
1.6 millert 1483:
1.76 espie 1484: /*
1.78 espie 1485: * Look for the last newline in the bytes we just got. If there is
1486: * one, break out of the loop with 'i' as its index and gotNL set
1487: * true.
1.76 espie 1488: */
1489: max = job->curPos + nr;
1490: for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
1491: if (job->outBuf[i] == '\n') {
1492: gotNL = true;
1493: break;
1494: } else if (job->outBuf[i] == '\0') {
1495: /*
1.89 ! espie 1496: * To be fixed: don't use printf, it stops at NUL bytes.
1.76 espie 1497: */
1498: job->outBuf[i] = ' ';
1.66 espie 1499: }
1.76 espie 1500: }
1.6 millert 1501:
1.76 espie 1502: if (!gotNL) {
1503: job->curPos += nr;
1504: if (job->curPos == JOB_BUFSIZE) {
1.66 espie 1505: /*
1.78 espie 1506: * If we've run out of buffer space, we have no choice
1507: * but to print the stuff. sigh.
1.66 espie 1508: */
1.76 espie 1509: fbuf = true;
1510: i = job->curPos;
1511: }
1512: }
1513: if (gotNL || fbuf) {
1514: /*
1.78 espie 1515: * Need to send the output to the screen. Null terminate it
1516: * first, overwriting the newline character if there was one.
1517: * So long as the line isn't one we should filter (according
1518: * to the shell description), we print the line, preceded
1519: * by a target banner if this target isn't the same as the
1520: * one for which we last printed something.
1521: * The rest of the data in the buffer are then shifted down
1522: * to the start of the buffer and curPos is set accordingly.
1.76 espie 1523: */
1524: job->outBuf[i] = '\0';
1525: if (i >= job->curPos) {
1526: char *cp;
1.66 espie 1527:
1.78 espie 1528: cp = JobOutput(job, job->outBuf, &job->outBuf[i],
1529: false);
1.66 espie 1530:
1.76 espie 1531: /*
1.78 espie 1532: * There's still more in that thar buffer. This time,
1533: * though, we know there's no newline at the end, so we
1534: * add one of our own free will.
1.76 espie 1535: */
1536: if (*cp != '\0') {
1537: if (job->node != lastNode) {
1538: MESSAGE(stdout, job->node);
1539: lastNode = job->node;
1.66 espie 1540: }
1.76 espie 1541: (void)fprintf(stdout, "%s%s", cp,
1542: gotNL ? "\n" : "");
1543: (void)fflush(stdout);
1.66 espie 1544: }
1.76 espie 1545: }
1546: if (i < max - 1) {
1547: /* shift the remaining characters down */
1548: (void)memcpy(job->outBuf, &job->outBuf[i + 1],
1549: max - (i + 1));
1550: job->curPos = max - (i + 1);
1.66 espie 1551:
1.76 espie 1552: } else {
1.66 espie 1553: /*
1.78 espie 1554: * We have written everything out, so we just start over
1555: * from the start of the buffer. No copying. No nothing.
1.66 espie 1556: */
1.76 espie 1557: job->curPos = 0;
1.66 espie 1558: }
1.76 espie 1559: }
1560: if (finish) {
1.66 espie 1561: /*
1.78 espie 1562: * If the finish flag is true, we must loop until we hit
1563: * end-of-file on the pipe. This is guaranteed to happen
1564: * eventually since the other end of the pipe is now closed
1565: * (we closed it explicitly and the child has exited). When
1566: * we do get an EOF, finish will be set false and we'll fall
1567: * through and out.
1.76 espie 1568: */
1569: goto end_loop;
1.1 deraadt 1570: }
1571: }
1572:
1573: /*-
1574: *-----------------------------------------------------------------------
1575: * Job_CatchChildren --
1576: * Handle the exit of a child. Called from Make_Make.
1577: *
1578: * Side Effects:
1579: * The job descriptor is removed from the list of children.
1580: *
1581: * Notes:
1582: * We do waits, blocking or not, according to the wisdom of our
1583: * caller, until there are no more children to report. For each
1584: * job, call JobFinish to finish things off. This will take care of
1585: * putting jobs on the stoppedJobs queue.
1586: *-----------------------------------------------------------------------
1587: */
1588: void
1.76 espie 1589: Job_CatchChildren()
1.1 deraadt 1590: {
1.78 espie 1591: pid_t pid; /* pid of dead child */
1592: Job *job; /* job descriptor for dead child */
1593: LstNode jnode; /* list element for finding job */
1594: int status; /* Exit/termination status */
1.1 deraadt 1595:
1.66 espie 1596: /*
1597: * Don't even bother if we know there's no one around.
1598: */
1.83 espie 1599: if (nJobs == 0) {
1.66 espie 1600: return;
1.2 deraadt 1601: }
1.6 millert 1602:
1.76 espie 1603: while ((pid = waitpid((pid_t) -1, &status, WNOHANG|WUNTRACED)) > 0) {
1.66 espie 1604: HandleSigs();
1.84 espie 1605: debug_printf("Process %ld exited or stopped.\n", (long)pid);
1.1 deraadt 1606:
1.66 espie 1607: jnode = Lst_Find(&jobs, JobCmpPid, &pid);
1.1 deraadt 1608:
1.18 espie 1609: if (jnode == NULL) {
1.68 espie 1610: if (WIFSIGNALED(status) &&
1.66 espie 1611: (WTERMSIG(status) == SIGCONT)) {
1612: jnode = Lst_Find(&stoppedJobs, JobCmpPid, &pid);
1613: if (jnode == NULL) {
1614: Error("Resumed child (%ld) not in table", (long)pid);
1615: continue;
1616: }
1617: job = (Job *)Lst_Datum(jnode);
1618: Lst_Remove(&stoppedJobs, jnode);
1619: } else {
1620: Error("Child (%ld) not in table?", (long)pid);
1621: continue;
1622: }
1623: } else {
1624: job = (Job *)Lst_Datum(jnode);
1625: Lst_Remove(&jobs, jnode);
1.67 espie 1626: nJobs--;
1.84 espie 1627: if (jobFull)
1628: debug_printf("Job queue is no longer full.\n");
1.66 espie 1629: jobFull = false;
1.1 deraadt 1630: }
1.66 espie 1631:
1632: JobFinish(job, &status);
1.1 deraadt 1633: }
1634: }
1635:
1636: /*-
1637: *-----------------------------------------------------------------------
1638: * Job_CatchOutput --
1639: * Catch the output from our children, if we're using
1640: * pipes do so. Otherwise just block time until we get a
1.6 millert 1641: * signal (most likely a SIGCHLD) since there's no point in
1.1 deraadt 1642: * just spinning when there's nothing to do and the reaping
1.6 millert 1643: * of a child can wait for a while.
1.1 deraadt 1644: *
1645: * Side Effects:
1646: * Output is read from pipes if we're piping.
1647: * -----------------------------------------------------------------------
1648: */
1649: void
1.56 espie 1650: Job_CatchOutput(void)
1.1 deraadt 1651: {
1.66 espie 1652: int nfds;
1653: struct timeval timeout;
1654: LstNode ln;
1655: Job *job;
1656:
1.76 espie 1657: int count = howmany(outputsn+1, NFDBITS) * sizeof(fd_mask);
1658: fd_set *readfdsp = malloc(count);
1.77 espie 1659: (void)fflush(stdout);
1.76 espie 1660: if (readfdsp == NULL)
1661: return;
1662:
1663: memcpy(readfdsp, outputsp, count);
1664: timeout.tv_sec = SEL_SEC;
1665: timeout.tv_usec = SEL_USEC;
1.66 espie 1666:
1.76 espie 1667: if ((nfds = select(outputsn+1, readfdsp, (fd_set *) 0,
1668: (fd_set *) 0, &timeout)) <= 0) {
1669: HandleSigs();
1670: free(readfdsp);
1671: return;
1672: } else {
1673: HandleSigs();
1674: for (ln = Lst_First(&jobs); nfds && ln != NULL;
1675: ln = Lst_Adv(ln)) {
1676: job = (Job *)Lst_Datum(ln);
1677: if (FD_ISSET(job->inPipe, readfdsp)) {
1678: JobDoOutput(job, false);
1679: nfds--;
1.66 espie 1680: }
1.1 deraadt 1681: }
1682: }
1.76 espie 1683: free(readfdsp);
1.1 deraadt 1684: }
1685:
1686: /*-
1687: *-----------------------------------------------------------------------
1688: * Job_Make --
1689: * Start the creation of a target. Basically a front-end for
1690: * JobStart used by the Make module.
1691: *
1692: * Side Effects:
1693: * Another job is started.
1694: *-----------------------------------------------------------------------
1695: */
1696: void
1.56 espie 1697: Job_Make(GNode *gn)
1.1 deraadt 1698: {
1.86 espie 1699: (void)JobStart(gn, 0);
1.1 deraadt 1700: }
1701:
1702: /*-
1703: *-----------------------------------------------------------------------
1704: * Job_Init --
1705: * Initialize the process module
1706: *
1707: * Side Effects:
1708: * lists and counters are initialized
1709: *-----------------------------------------------------------------------
1710: */
1711: void
1.83 espie 1712: Job_Init(int maxproc)
1.1 deraadt 1713: {
1.66 espie 1714: Static_Lst_Init(&jobs);
1715: Static_Lst_Init(&stoppedJobs);
1716: maxJobs = maxproc;
1.79 espie 1717: nJobs = 0;
1.66 espie 1718: jobFull = false;
1719:
1720: aborting = 0;
1721: errors = 0;
1.40 espie 1722:
1.66 espie 1723: lastNode = NULL;
1.1 deraadt 1724:
1.66 espie 1725: if (maxJobs == 1) {
1726: /*
1727: * If only one job can run at a time, there's no need for a
1728: * banner, no is there?
1729: */
1730: targFmt = "";
1731: } else {
1732: targFmt = TARG_FMT;
1733: }
1.1 deraadt 1734:
1735: /*
1.66 espie 1736: * Catch the four signals that POSIX specifies if they aren't ignored.
1737: * JobPassSig will take care of calling JobInterrupt if appropriate.
1.1 deraadt 1738: */
1.66 espie 1739: if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
1740: (void)signal(SIGINT, SigHandler);
1741: }
1742: if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
1743: (void)signal(SIGHUP, SigHandler);
1744: }
1745: if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
1746: (void)signal(SIGQUIT, SigHandler);
1747: }
1748: if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
1749: (void)signal(SIGTERM, SigHandler);
1750: }
1.1 deraadt 1751: /*
1.66 espie 1752: * There are additional signals that need to be caught and passed if
1753: * either the export system wants to be told directly of signals or if
1754: * we're giving each job its own process group (since then it won't get
1755: * signals from the terminal driver as we own the terminal)
1.1 deraadt 1756: */
1.48 espie 1757: #if defined(USE_PGRP)
1.66 espie 1758: if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) {
1759: (void)signal(SIGTSTP, SigHandler);
1760: }
1761: if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) {
1762: (void)signal(SIGTTOU, SigHandler);
1763: }
1764: if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) {
1765: (void)signal(SIGTTIN, SigHandler);
1766: }
1767: if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) {
1768: (void)signal(SIGWINCH, SigHandler);
1769: }
1.1 deraadt 1770: #endif
1.6 millert 1771:
1.78 espie 1772: if ((begin_node->type & OP_DUMMY) == 0) {
1.86 espie 1773: JobStart(begin_node, JOB_SPECIAL);
1.66 espie 1774: while (nJobs) {
1775: Job_CatchOutput();
1.76 espie 1776: Job_CatchChildren();
1.66 espie 1777: }
1.1 deraadt 1778: }
1779: }
1780:
1781: /*-
1782: *-----------------------------------------------------------------------
1783: * Job_Full --
1784: * See if the job table is full. It is considered full if it is OR
1785: * if we are in the process of aborting OR if we have
1786: * reached/exceeded our local quota. This prevents any more jobs
1787: * from starting up.
1788: *
1789: * Results:
1.41 espie 1790: * true if the job table is full, false otherwise
1.1 deraadt 1791: *-----------------------------------------------------------------------
1792: */
1.41 espie 1793: bool
1.56 espie 1794: Job_Full(void)
1.1 deraadt 1795: {
1.66 espie 1796: return aborting || jobFull;
1.1 deraadt 1797: }
1798:
1799: /*-
1800: *-----------------------------------------------------------------------
1801: * Job_Empty --
1.40 espie 1802: * See if the job table is empty. Because the local concurrency may
1.1 deraadt 1803: * be set to 0, it is possible for the job table to become empty,
1804: * while the list of stoppedJobs remains non-empty. In such a case,
1805: * we want to restart as many jobs as we can.
1806: *
1807: * Results:
1.41 espie 1808: * true if it is. false if it ain't.
1.1 deraadt 1809: * -----------------------------------------------------------------------
1810: */
1.41 espie 1811: bool
1.56 espie 1812: Job_Empty(void)
1.1 deraadt 1813: {
1.66 espie 1814: if (nJobs == 0) {
1815: if (!Lst_IsEmpty(&stoppedJobs) && !aborting) {
1816: /*
1817: * The job table is obviously not full if it has no
1818: * jobs in it...Try and restart the stopped jobs.
1819: */
1820: jobFull = false;
1821: JobRestartJobs();
1822: return false;
1823: } else {
1824: return true;
1825: }
1.1 deraadt 1826: } else {
1.66 espie 1827: return false;
1.1 deraadt 1828: }
1829: }
1830:
1831: /*-
1832: *-----------------------------------------------------------------------
1833: * JobInterrupt --
1834: * Handle the receipt of an interrupt.
1835: *
1836: * Side Effects:
1837: * All children are killed. Another job will be started if the
1838: * .INTERRUPT target was given.
1839: *-----------------------------------------------------------------------
1840: */
1841: static void
1.56 espie 1842: JobInterrupt(int runINTERRUPT, /* Non-zero if commands for the .INTERRUPT
1.1 deraadt 1843: * target should be executed */
1.66 espie 1844: int signo) /* signal received */
1.1 deraadt 1845: {
1.66 espie 1846: LstNode ln; /* element in job table */
1.78 espie 1847: Job *job; /* job descriptor in that element */
1.66 espie 1848:
1849: aborting = ABORT_INTERRUPT;
1850:
1851: for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Adv(ln)) {
1852: job = (Job *)Lst_Datum(ln);
1853:
1854: if (!Targ_Precious(job->node)) {
1855: const char *file = job->node->path == NULL ?
1856: job->node->name : job->node->path;
1857: if (!noExecute && eunlink(file) != -1) {
1858: Error("*** %s removed", file);
1859: }
1860: }
1861: if (job->pid) {
1.84 espie 1862: debug_printf("JobInterrupt passing signal to "
1863: "child %ld.\n", (long)job->pid);
1.66 espie 1864: KILL(job->pid, signo);
1865: }
1.2 deraadt 1866: }
1.1 deraadt 1867:
1.66 espie 1868: if (runINTERRUPT && !touchFlag) {
1.78 espie 1869: if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66 espie 1870: ignoreErrors = false;
1871:
1.86 espie 1872: JobStart(interrupt_node, JOB_IGNDOTS);
1.66 espie 1873: while (nJobs) {
1874: Job_CatchOutput();
1.76 espie 1875: Job_CatchChildren();
1.66 espie 1876: }
1877: }
1.1 deraadt 1878: }
1.66 espie 1879: exit(signo);
1.1 deraadt 1880: }
1881:
1882: /*
1883: *-----------------------------------------------------------------------
1.12 espie 1884: * Job_Finish --
1.1 deraadt 1885: * Do final processing such as the running of the commands
1.6 millert 1886: * attached to the .END target.
1.1 deraadt 1887: *
1888: * Results:
1889: * Number of errors reported.
1.40 espie 1890: *
1.1 deraadt 1891: *-----------------------------------------------------------------------
1892: */
1893: int
1.56 espie 1894: Job_Finish(void)
1.1 deraadt 1895: {
1.78 espie 1896: if (end_node != NULL && !Lst_IsEmpty(&end_node->commands)) {
1.66 espie 1897: if (errors) {
1898: Error("Errors reported so .END ignored");
1899: } else {
1.86 espie 1900: JobStart(end_node, JOB_SPECIAL | JOB_IGNDOTS);
1.1 deraadt 1901:
1.66 espie 1902: while (nJobs) {
1903: Job_CatchOutput();
1.76 espie 1904: Job_CatchChildren();
1.66 espie 1905: }
1906: }
1.1 deraadt 1907: }
1.66 espie 1908: return errors;
1.1 deraadt 1909: }
1910:
1.41 espie 1911: #ifdef CLEANUP
1.12 espie 1912: void
1.56 espie 1913: Job_End(void)
1.12 espie 1914: {
1.41 espie 1915: }
1.13 espie 1916: #endif
1.40 espie 1917:
1.1 deraadt 1918: /*-
1919: *-----------------------------------------------------------------------
1920: * Job_Wait --
1921: * Waits for all running jobs to finish and returns. Sets 'aborting'
1922: * to ABORT_WAIT to prevent other jobs from starting.
1923: *
1924: * Side Effects:
1925: * Currently running jobs finish.
1926: *
1927: *-----------------------------------------------------------------------
1928: */
1929: void
1.56 espie 1930: Job_Wait(void)
1.1 deraadt 1931: {
1.66 espie 1932: aborting = ABORT_WAIT;
1933: while (nJobs != 0) {
1934: Job_CatchOutput();
1.76 espie 1935: Job_CatchChildren();
1.66 espie 1936: }
1937: aborting = 0;
1.1 deraadt 1938: }
1939:
1940: /*-
1941: *-----------------------------------------------------------------------
1942: * Job_AbortAll --
1943: * Abort all currently running jobs without handling output or anything.
1944: * This function is to be called only in the event of a major
1945: * error. Most definitely NOT to be called from JobInterrupt.
1946: *
1947: * Side Effects:
1948: * All children are killed, not just the firstborn
1949: *-----------------------------------------------------------------------
1950: */
1951: void
1.56 espie 1952: Job_AbortAll(void)
1.1 deraadt 1953: {
1.66 espie 1954: LstNode ln; /* element in job table */
1955: Job *job; /* the job descriptor in that element */
1956: int foo;
1.6 millert 1957:
1.66 espie 1958: aborting = ABORT_ERROR;
1.6 millert 1959:
1.66 espie 1960: if (nJobs) {
1961: for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Adv(ln)) {
1962: job = (Job *)Lst_Datum(ln);
1963:
1964: /*
1965: * kill the child process with increasingly drastic
1966: * signals to make darn sure it's dead.
1967: */
1968: KILL(job->pid, SIGINT);
1969: KILL(job->pid, SIGKILL);
1970: }
1.1 deraadt 1971: }
1.6 millert 1972:
1.66 espie 1973: /*
1974: * Catch as many children as want to report in at first, then give up
1975: */
1976: while (waitpid(-1, &foo, WNOHANG) > 0)
1977: continue;
1.2 deraadt 1978: }
1.40 espie 1979:
1.2 deraadt 1980: /*-
1981: *-----------------------------------------------------------------------
1982: * JobRestartJobs --
1983: * Tries to restart stopped jobs if there are slots available.
1984: * Note that this tries to restart them regardless of pending errors.
1985: * It's not good to leave stopped jobs lying around!
1986: *
1987: * Side Effects:
1988: * Resumes(and possibly migrates) jobs.
1989: *-----------------------------------------------------------------------
1990: */
1991: static void
1.56 espie 1992: JobRestartJobs(void)
1.2 deraadt 1993: {
1.66 espie 1994: Job *job;
1.19 espie 1995:
1.66 espie 1996: while (!jobFull && (job = (Job *)Lst_DeQueue(&stoppedJobs)) != NULL) {
1.84 espie 1997: debug_printf("Job queue is not full. "
1998: "Restarting a stopped job.\n");
1.66 espie 1999: JobRestart(job);
1.2 deraadt 2000: }
1.1 deraadt 2001: }