Annotation of src/usr.bin/make/job.c, Revision 1.92
1.40 espie 1: /* $OpenPackages$ */
1.92 ! cnst 2: /* $OpenBSD: job.c,v 1.91 2007/09/29 09:59:04 espie Exp $ */
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) {
1.92 ! cnst 1004: int fdn, ofdn;
! 1005: fd_set *tmp;
1.87 espie 1006:
1.92 ! cnst 1007: fdn = howmany(job->inPipe+1, NFDBITS);
! 1008: ofdn = outputsn ? howmany(outputsn+1, NFDBITS) : 0;
1.87 espie 1009:
1.92 ! cnst 1010: if (fdn != ofdn) {
! 1011: tmp = recalloc(outputsp, fdn, sizeof(fd_mask));
1.87 espie 1012: if (tmp == NULL)
1013: return;
1.92 ! cnst 1014: outputsp = tmp;
1.66 espie 1015: }
1.87 espie 1016: outputsn = job->inPipe;
1.66 espie 1017: }
1.87 espie 1018: FD_SET(job->inPipe, outputsp);
1.2 deraadt 1019:
1.66 espie 1020: /*
1021: * XXX: Used to not happen if REMOTE. Why?
1022: */
1023: if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1024: (void)fclose(job->cmdFILE);
1025: job->cmdFILE = NULL;
1026: }
1.1 deraadt 1027: }
1028:
1.48 espie 1029: /*
1.66 espie 1030: * Now the job is actually running, add it to the table.
1.48 espie 1031: */
1.67 espie 1032: nJobs++;
1.66 espie 1033: Lst_AtEnd(&jobs, job);
1034: if (nJobs == maxJobs) {
1035: jobFull = true;
1.1 deraadt 1036: }
1037: }
1038:
1039: /*-
1040: *-----------------------------------------------------------------------
1041: * JobMakeArgv --
1042: * Create the argv needed to execute the shell for a given job.
1043: *-----------------------------------------------------------------------
1044: */
1045: static void
1.56 espie 1046: JobMakeArgv(Job *job, char **argv)
1.1 deraadt 1047: {
1.66 espie 1048: int argc;
1049: static char args[10]; /* For merged arguments */
1.6 millert 1050:
1.70 espie 1051: argv[0] = (char *)shellName;
1.66 espie 1052: argc = 1;
1.1 deraadt 1053:
1.73 espie 1054: (void)snprintf(args, sizeof(args), "-%s%s",
1055: (job->flags & JOB_IGNERR) ? "" : SHELL_ERROR_FLAG,
1056: (job->flags & JOB_SILENT) ? "" : SHELL_ECHO_FLAG);
1057:
1058: if (args[1]) {
1059: argv[argc] = args;
1060: argc++;
1.78 espie 1061: }
1.66 espie 1062: argv[argc] = NULL;
1.1 deraadt 1063: }
1064:
1065: /*-
1066: *-----------------------------------------------------------------------
1067: * JobRestart --
1.6 millert 1068: * Restart a job that stopped for some reason.
1.1 deraadt 1069: *
1070: * Side Effects:
1071: * jobFull will be set if the job couldn't be run.
1072: *-----------------------------------------------------------------------
1073: */
1074: static void
1.56 espie 1075: JobRestart(Job *job)
1.1 deraadt 1076: {
1.66 espie 1077: if (job->flags & JOB_RESTART) {
1078: /*
1079: * Set up the control arguments to the shell. This is based on
1080: * the flags set earlier for this job. If the JOB_IGNERR flag
1081: * is clear, the 'exit' flag of the commandShell is used to
1082: * cause it to exit upon receiving an error. If the JOB_SILENT
1083: * flag is clear, the 'echo' flag of the commandShell is used
1084: * to get it to start echoing as soon as it starts processing
1085: * commands.
1086: */
1087: char *argv[4];
1.6 millert 1088:
1.66 espie 1089: JobMakeArgv(job, argv);
1.2 deraadt 1090:
1.1 deraadt 1091: if (DEBUG(JOB)) {
1.68 espie 1092: (void)fprintf(stdout, "Restarting %s...",
1.66 espie 1093: job->node->name);
1094: (void)fflush(stdout);
1.1 deraadt 1095: }
1.83 espie 1096: if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) {
1.80 espie 1097: /*
1098: * Can't be exported and not allowed to run locally --
1099: * put it back on the hold queue and mark the table
1100: * full
1101: */
1.84 espie 1102: debug_printf("holding\n");
1.80 espie 1103: Lst_AtFront(&stoppedJobs, job);
1104: jobFull = true;
1.84 espie 1105: debug_printf("Job queue is full.\n");
1.80 espie 1106: return;
1.66 espie 1107: } else {
1.80 espie 1108: /*
1109: * Job may be run locally.
1110: */
1.84 espie 1111: debug_printf("running locally\n");
1.1 deraadt 1112: }
1.66 espie 1113: JobExec(job, argv);
1114: } else {
1.1 deraadt 1115: /*
1.66 espie 1116: * The job has stopped and needs to be restarted. Why it
1117: * stopped, we don't know...
1.1 deraadt 1118: */
1.84 espie 1119: debug_printf("Resuming %s...", job->node->name);
1.83 espie 1120: if ((nJobs < maxJobs || ((job->flags & JOB_SPECIAL) &&
1121: maxJobs == 0)) && nJobs != maxJobs) {
1.66 espie 1122: /*
1.79 espie 1123: * If we haven't reached the concurrency limit already
1.83 espie 1124: * (or maxJobs is 0), it's ok to resume the job.
1.66 espie 1125: */
1126: bool error;
1127: int status;
1128:
1129: error = KILL(job->pid, SIGCONT) != 0;
1130:
1131: if (!error) {
1132: /*
1133: * Make sure the user knows we've continued the
1134: * beast and actually put the thing in the job
1135: * table.
1136: */
1137: job->flags |= JOB_CONTINUING;
1138: W_SETTERMSIG(&status, SIGCONT);
1139: JobFinish(job, &status);
1140:
1141: job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1.84 espie 1142: debug_printf("done\n");
1.66 espie 1143: } else {
1144: Error("couldn't resume %s: %s",
1145: job->node->name, strerror(errno));
1146: status = 0;
1147: W_SETEXITSTATUS(&status, 1);
1148: JobFinish(job, &status);
1149: }
1150: } else {
1151: /*
1152: * Job cannot be restarted. Mark the table as full and
1153: * place the job back on the list of stopped jobs.
1154: */
1.84 espie 1155: debug_printf("table full\n");
1.66 espie 1156: Lst_AtFront(&stoppedJobs, job);
1157: jobFull = true;
1.84 espie 1158: debug_printf("Job queue is full.\n");
1.1 deraadt 1159: }
1160: }
1161: }
1162:
1163: /*-
1164: *-----------------------------------------------------------------------
1165: * JobStart --
1166: * Start a target-creation process going for the target described
1.6 millert 1167: * by the graph node gn.
1.1 deraadt 1168: *
1169: * Results:
1170: * JOB_ERROR if there was an error in the commands, JOB_FINISHED
1171: * if there isn't actually anything left to do for the job and
1172: * JOB_RUNNING if the job has been started.
1173: *
1174: * Side Effects:
1175: * A new Job node is created and added to the list of running
1176: * jobs. PMake is forked and a child shell created.
1177: *-----------------------------------------------------------------------
1178: */
1179: static int
1.78 espie 1180: JobStart(GNode *gn, /* target to create */
1.86 espie 1181: int flags) /* flags for the job to override normal ones.
1182: * e.g. JOB_SPECIAL */
1.78 espie 1183: {
1184: Job *job; /* new job descriptor */
1185: char *argv[4]; /* Argument vector to shell */
1186: bool cmdsOK; /* true if the nodes commands were all right */
1187: bool noExec; /* Set true if we decide not to run the job */
1.87 espie 1188: int fd[2];
1.66 espie 1189:
1.86 espie 1190: job = emalloc(sizeof(Job));
1191: if (job == NULL) {
1192: Punt("JobStart out of memory");
1.1 deraadt 1193: }
1194:
1.66 espie 1195: job->node = gn;
1196: job->tailCmds = NULL;
1.1 deraadt 1197:
1198: /*
1.66 espie 1199: * Set the initial value of the flags for this job based on the global
1200: * ones and the node's attributes... Any flags supplied by the caller
1201: * are also added to the field.
1.1 deraadt 1202: */
1.87 espie 1203: job->flags = flags;
1.66 espie 1204: if (Targ_Ignore(gn)) {
1205: job->flags |= JOB_IGNERR;
1206: }
1207: if (Targ_Silent(gn)) {
1208: job->flags |= JOB_SILENT;
1.1 deraadt 1209: }
1.6 millert 1210:
1.1 deraadt 1211: /*
1.66 espie 1212: * Check the commands now so any attributes from .DEFAULT have a chance
1213: * to migrate to the node
1.1 deraadt 1214: */
1.87 espie 1215: cmdsOK = Job_CheckCommands(gn, Error);
1.1 deraadt 1216:
1217: /*
1.66 espie 1218: * If the -n flag wasn't given, we open up OUR (not the child's)
1219: * temporary file to stuff commands in it. The thing is rd/wr so we
1220: * don't need to reopen it to feed it to the shell. If the -n flag
1221: * *was* given, we just set the file to be stdout. Cute, huh?
1.1 deraadt 1222: */
1.66 espie 1223: if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
1224: /*
1225: * We're serious here, but if the commands were bogus, we're
1226: * also dead...
1227: */
1228: if (!cmdsOK) {
1229: DieHorribly();
1230: }
1.6 millert 1231:
1.88 espie 1232: job->cmdFILE = new_command_file();
1.66 espie 1233: if (job->cmdFILE == NULL) {
1.88 espie 1234: Punt("Error creating command file");
1.66 espie 1235: }
1.90 espie 1236: (void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC);
1.66 espie 1237: /*
1238: * Send the commands to the command file, flush all its buffers
1239: * then rewind and remove the thing.
1240: */
1241: noExec = false;
1242:
1243: /*
1.85 espie 1244: * We can do all the commands at once. hooray for
1245: * sanity
1.66 espie 1246: */
1.85 espie 1247: numCommands = 0;
1248: Lst_ForEachNodeWhile(&gn->commands, JobPrintCommand,
1249: job);
1.66 espie 1250:
1.85 espie 1251: /*
1252: * If we didn't print out any commands to the shell
1253: * script, there's not much point in executing the
1254: * shell, is there?
1255: */
1256: if (numCommands == 0) {
1257: noExec = true;
1.66 espie 1258: }
1259: } else if (noExecute) {
1260: /*
1261: * Not executing anything -- just print all the commands to
1262: * stdout in one fell swoop. This will still set up
1263: * job->tailCmds correctly.
1264: */
1265: if (lastNode != gn) {
1266: MESSAGE(stdout, gn);
1267: lastNode = gn;
1268: }
1269: job->cmdFILE = stdout;
1270: /*
1271: * Only print the commands if they're ok, but don't die if
1272: * they're not -- just let the user know they're bad and keep
1273: * going. It doesn't do any harm in this case and may do some
1274: * good.
1275: */
1276: if (cmdsOK) {
1.68 espie 1277: Lst_ForEachNodeWhile(&gn->commands, JobPrintCommand,
1.66 espie 1278: job);
1279: }
1280: /*
1281: * Don't execute the shell, thank you.
1282: */
1.41 espie 1283: noExec = true;
1.66 espie 1284: } else {
1285: /*
1286: * Just touch the target and note that no shell should be
1287: * executed. Set cmdFILE to stdout to make life easier. Check
1288: * the commands, too, but don't die if they're no good -- it
1289: * does no harm to keep working up the graph.
1.30 espie 1290: */
1.66 espie 1291: job->cmdFILE = stdout;
1.87 espie 1292: Job_Touch(gn, job->flags & JOB_SILENT);
1.41 espie 1293: noExec = true;
1.1 deraadt 1294: }
1.66 espie 1295:
1.1 deraadt 1296: /*
1.66 espie 1297: * If we're not supposed to execute a shell, don't.
1.1 deraadt 1298: */
1.66 espie 1299: if (noExec) {
1300: /*
1301: * Unlink and close the command file if we opened one
1302: */
1303: if (job->cmdFILE != stdout) {
1304: if (job->cmdFILE != NULL)
1305: (void)fclose(job->cmdFILE);
1306: } else {
1307: (void)fflush(stdout);
1308: }
1.1 deraadt 1309:
1.66 espie 1310: /*
1311: * We only want to work our way up the graph if we aren't here
1312: * because the commands for the job were no good.
1313: */
1314: if (cmdsOK) {
1315: if (aborting == 0) {
1.68 espie 1316: Lst_ForEachFrom(job->tailCmds, JobSaveCommand,
1.66 espie 1317: job->node);
1318: Make_Update(job->node);
1319: }
1320: free(job);
1321: return JOB_FINISHED;
1322: } else {
1323: free(job);
1324: return JOB_ERROR;
1325: }
1.1 deraadt 1326: } else {
1.66 espie 1327: (void)fflush(job->cmdFILE);
1.1 deraadt 1328: }
1329:
1330: /*
1.66 espie 1331: * Set up the control arguments to the shell. This is based on the flags
1332: * set earlier for this job.
1.1 deraadt 1333: */
1.66 espie 1334: JobMakeArgv(job, argv);
1.1 deraadt 1335:
1.90 espie 1336: /* Create the pipe by which we'll get the shell's output.
1.66 espie 1337: */
1.87 espie 1338: if (pipe(fd) == -1)
1339: Punt("Cannot create pipe: %s", strerror(errno));
1340: job->inPipe = fd[0];
1341: job->outPipe = fd[1];
1.90 espie 1342: (void)fcntl(job->inPipe, F_SETFD, FD_CLOEXEC);
1343: (void)fcntl(job->outPipe, F_SETFD, FD_CLOEXEC);
1.1 deraadt 1344:
1.83 espie 1345: if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) &&
1346: maxJobs != 0) {
1.79 espie 1347: /*
1348: * The job can only be run locally, but we've hit the limit of
1349: * local concurrency, so put the job on hold until some other
1350: * job finishes. Note that the special jobs (.BEGIN, .INTERRUPT
1351: * and .END) may be run locally even when the local limit has
1.83 espie 1352: * been reached (e.g. when maxJobs == 0), though they will be
1.79 espie 1353: * exported if at all possible. In addition, any target marked
1.83 espie 1354: * with .NOEXPORT will be run locally if maxJobs is 0.
1.66 espie 1355: */
1356: jobFull = true;
1.6 millert 1357:
1.84 espie 1358: debug_printf("Can only run job locally.\n");
1.66 espie 1359: job->flags |= JOB_RESTART;
1360: Lst_AtEnd(&stoppedJobs, job);
1.79 espie 1361: } else {
1.83 espie 1362: if (nJobs >= maxJobs) {
1.79 espie 1363: /*
1364: * If we're running this job locally as a special case
1365: * (see above), at least say the table is full.
1366: */
1367: jobFull = true;
1.84 espie 1368: debug_printf("Local job queue is full.\n");
1.79 espie 1369: }
1370: JobExec(job, argv);
1.1 deraadt 1371: }
1.66 espie 1372: return JOB_RUNNING;
1.1 deraadt 1373: }
1374:
1.6 millert 1375: static char *
1.56 espie 1376: JobOutput(Job *job, char *cp, char *endp, int msg)
1.2 deraadt 1377: {
1.66 espie 1378: char *ecp;
1.2 deraadt 1379:
1.74 espie 1380: ecp = strstr(cp, SHELL_ECHO_OFF);
1381: while (ecp != NULL) {
1382: if (cp != ecp) {
1383: *ecp = '\0';
1384: if (msg && job->node != lastNode) {
1385: MESSAGE(stdout, job->node);
1386: lastNode = job->node;
1.66 espie 1387: }
1.74 espie 1388: /*
1.78 espie 1389: * The only way there wouldn't be a newline after
1390: * this line is if it were the last in the buffer.
1391: * however, since the non-printable comes after it,
1392: * there must be a newline, so we don't print one.
1.74 espie 1393: */
1394: (void)fprintf(stdout, "%s", cp);
1395: (void)fflush(stdout);
1396: }
1397: cp = ecp + strlen(SHELL_ECHO_OFF);
1398: if (cp != endp) {
1399: /*
1.78 espie 1400: * Still more to print, look again after skipping
1401: * the whitespace following the non-printable
1402: * command....
1.74 espie 1403: */
1404: cp++;
1.78 espie 1405: while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
1.66 espie 1406: cp++;
1407: }
1.74 espie 1408: ecp = strstr(cp, SHELL_ECHO_OFF);
1409: } else {
1410: return cp;
1.2 deraadt 1411: }
1412: }
1.66 espie 1413: return cp;
1.2 deraadt 1414: }
1415:
1.1 deraadt 1416: /*-
1417: *-----------------------------------------------------------------------
1.40 espie 1418: * JobDoOutput --
1.89 espie 1419: * This functions is called whenever there is something to read on the
1420: * pipe. We collect more output from the given job and store it in the
1421: * job's outBuf. If this makes up a line, we print it tagged by the job's
1422: * identifier, as necessary.
1423: * We also keep our figurative eye out for the
1.1 deraadt 1424: * 'noPrint' line for the shell from which the output came. If
1425: * we recognize a line, we don't print it. If the command is not
1.6 millert 1426: * alone on the line (the character after it is not \0 or \n), we
1.1 deraadt 1427: * do print whatever follows it.
1428: *
1429: * Side Effects:
1430: * curPos may be shifted as may the contents of outBuf.
1431: *-----------------------------------------------------------------------
1432: */
1.48 espie 1433: static void
1.66 espie 1434: JobDoOutput(Job *job, /* the job whose output needs printing */
1435: bool finish) /* true if this is the last time we'll be
1436: * called for this job */
1437: {
1.78 espie 1438: bool gotNL = false; /* true if got a newline */
1.66 espie 1439: bool fbuf; /* true if our buffer filled up */
1440: int nr; /* number of bytes read */
1441: int i; /* auxiliary index into outBuf */
1.78 espie 1442: int max; /* limit for i (end of current data) */
1443: int nRead; /* (Temporary) number of bytes read */
1.1 deraadt 1444:
1.76 espie 1445: /*
1446: * Read as many bytes as will fit in the buffer.
1447: */
1.1 deraadt 1448: end_loop:
1.76 espie 1449: gotNL = false;
1450: fbuf = false;
1.6 millert 1451:
1.76 espie 1452: nRead = read(job->inPipe, &job->outBuf[job->curPos],
1453: JOB_BUFSIZE - job->curPos);
1454: if (nRead == -1) {
1455: if (DEBUG(JOB)) {
1456: perror("JobDoOutput(piperead)");
1.66 espie 1457: }
1.76 espie 1458: nr = 0;
1459: } else {
1460: nr = nRead;
1461: }
1.1 deraadt 1462:
1.76 espie 1463: /*
1.78 espie 1464: * If we hit the end-of-file (the job is dead), we must flush its
1465: * remaining output, so pretend we read a newline if there's any
1466: * output remaining in the buffer.
1467: * Also clear the 'finish' flag so we stop looping.
1.76 espie 1468: */
1469: if (nr == 0 && job->curPos != 0) {
1470: job->outBuf[job->curPos] = '\n';
1471: nr = 1;
1472: finish = false;
1473: } else if (nr == 0) {
1474: finish = false;
1475: }
1.6 millert 1476:
1.76 espie 1477: /*
1.78 espie 1478: * Look for the last newline in the bytes we just got. If there is
1479: * one, break out of the loop with 'i' as its index and gotNL set
1480: * true.
1.76 espie 1481: */
1482: max = job->curPos + nr;
1483: for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
1484: if (job->outBuf[i] == '\n') {
1485: gotNL = true;
1486: break;
1487: } else if (job->outBuf[i] == '\0') {
1488: /*
1.89 espie 1489: * To be fixed: don't use printf, it stops at NUL bytes.
1.76 espie 1490: */
1491: job->outBuf[i] = ' ';
1.66 espie 1492: }
1.76 espie 1493: }
1.6 millert 1494:
1.76 espie 1495: if (!gotNL) {
1496: job->curPos += nr;
1497: if (job->curPos == JOB_BUFSIZE) {
1.66 espie 1498: /*
1.78 espie 1499: * If we've run out of buffer space, we have no choice
1500: * but to print the stuff. sigh.
1.66 espie 1501: */
1.76 espie 1502: fbuf = true;
1503: i = job->curPos;
1504: }
1505: }
1506: if (gotNL || fbuf) {
1507: /*
1.78 espie 1508: * Need to send the output to the screen. Null terminate it
1509: * first, overwriting the newline character if there was one.
1510: * So long as the line isn't one we should filter (according
1511: * to the shell description), we print the line, preceded
1512: * by a target banner if this target isn't the same as the
1513: * one for which we last printed something.
1514: * The rest of the data in the buffer are then shifted down
1515: * to the start of the buffer and curPos is set accordingly.
1.76 espie 1516: */
1517: job->outBuf[i] = '\0';
1518: if (i >= job->curPos) {
1519: char *cp;
1.66 espie 1520:
1.78 espie 1521: cp = JobOutput(job, job->outBuf, &job->outBuf[i],
1522: false);
1.66 espie 1523:
1.76 espie 1524: /*
1.78 espie 1525: * There's still more in that thar buffer. This time,
1526: * though, we know there's no newline at the end, so we
1527: * add one of our own free will.
1.76 espie 1528: */
1529: if (*cp != '\0') {
1530: if (job->node != lastNode) {
1531: MESSAGE(stdout, job->node);
1532: lastNode = job->node;
1.66 espie 1533: }
1.76 espie 1534: (void)fprintf(stdout, "%s%s", cp,
1535: gotNL ? "\n" : "");
1536: (void)fflush(stdout);
1.66 espie 1537: }
1.76 espie 1538: }
1539: if (i < max - 1) {
1540: /* shift the remaining characters down */
1541: (void)memcpy(job->outBuf, &job->outBuf[i + 1],
1542: max - (i + 1));
1543: job->curPos = max - (i + 1);
1.66 espie 1544:
1.76 espie 1545: } else {
1.66 espie 1546: /*
1.78 espie 1547: * We have written everything out, so we just start over
1548: * from the start of the buffer. No copying. No nothing.
1.66 espie 1549: */
1.76 espie 1550: job->curPos = 0;
1.66 espie 1551: }
1.76 espie 1552: }
1553: if (finish) {
1.66 espie 1554: /*
1.78 espie 1555: * If the finish flag is true, we must loop until we hit
1556: * end-of-file on the pipe. This is guaranteed to happen
1557: * eventually since the other end of the pipe is now closed
1558: * (we closed it explicitly and the child has exited). When
1559: * we do get an EOF, finish will be set false and we'll fall
1560: * through and out.
1.76 espie 1561: */
1562: goto end_loop;
1.1 deraadt 1563: }
1564: }
1565:
1566: /*-
1567: *-----------------------------------------------------------------------
1568: * Job_CatchChildren --
1569: * Handle the exit of a child. Called from Make_Make.
1570: *
1571: * Side Effects:
1572: * The job descriptor is removed from the list of children.
1573: *
1574: * Notes:
1575: * We do waits, blocking or not, according to the wisdom of our
1576: * caller, until there are no more children to report. For each
1577: * job, call JobFinish to finish things off. This will take care of
1578: * putting jobs on the stoppedJobs queue.
1579: *-----------------------------------------------------------------------
1580: */
1581: void
1.76 espie 1582: Job_CatchChildren()
1.1 deraadt 1583: {
1.78 espie 1584: pid_t pid; /* pid of dead child */
1585: Job *job; /* job descriptor for dead child */
1586: LstNode jnode; /* list element for finding job */
1587: int status; /* Exit/termination status */
1.1 deraadt 1588:
1.66 espie 1589: /*
1590: * Don't even bother if we know there's no one around.
1591: */
1.83 espie 1592: if (nJobs == 0) {
1.66 espie 1593: return;
1.2 deraadt 1594: }
1.6 millert 1595:
1.76 espie 1596: while ((pid = waitpid((pid_t) -1, &status, WNOHANG|WUNTRACED)) > 0) {
1.66 espie 1597: HandleSigs();
1.84 espie 1598: debug_printf("Process %ld exited or stopped.\n", (long)pid);
1.1 deraadt 1599:
1.66 espie 1600: jnode = Lst_Find(&jobs, JobCmpPid, &pid);
1.1 deraadt 1601:
1.18 espie 1602: if (jnode == NULL) {
1.68 espie 1603: if (WIFSIGNALED(status) &&
1.66 espie 1604: (WTERMSIG(status) == SIGCONT)) {
1605: jnode = Lst_Find(&stoppedJobs, JobCmpPid, &pid);
1606: if (jnode == NULL) {
1607: Error("Resumed child (%ld) not in table", (long)pid);
1608: continue;
1609: }
1610: job = (Job *)Lst_Datum(jnode);
1611: Lst_Remove(&stoppedJobs, jnode);
1612: } else {
1613: Error("Child (%ld) not in table?", (long)pid);
1614: continue;
1615: }
1616: } else {
1617: job = (Job *)Lst_Datum(jnode);
1618: Lst_Remove(&jobs, jnode);
1.67 espie 1619: nJobs--;
1.84 espie 1620: if (jobFull)
1621: debug_printf("Job queue is no longer full.\n");
1.66 espie 1622: jobFull = false;
1.1 deraadt 1623: }
1.66 espie 1624:
1625: JobFinish(job, &status);
1.1 deraadt 1626: }
1627: }
1628:
1629: /*-
1630: *-----------------------------------------------------------------------
1631: * Job_CatchOutput --
1632: * Catch the output from our children, if we're using
1633: * pipes do so. Otherwise just block time until we get a
1.6 millert 1634: * signal (most likely a SIGCHLD) since there's no point in
1.1 deraadt 1635: * just spinning when there's nothing to do and the reaping
1.6 millert 1636: * of a child can wait for a while.
1.1 deraadt 1637: *
1638: * Side Effects:
1639: * Output is read from pipes if we're piping.
1640: * -----------------------------------------------------------------------
1641: */
1642: void
1.56 espie 1643: Job_CatchOutput(void)
1.1 deraadt 1644: {
1.66 espie 1645: int nfds;
1646: struct timeval timeout;
1647: LstNode ln;
1648: Job *job;
1649:
1.76 espie 1650: int count = howmany(outputsn+1, NFDBITS) * sizeof(fd_mask);
1651: fd_set *readfdsp = malloc(count);
1.91 espie 1652:
1.77 espie 1653: (void)fflush(stdout);
1.76 espie 1654: if (readfdsp == NULL)
1655: return;
1656:
1657: memcpy(readfdsp, outputsp, count);
1658: timeout.tv_sec = SEL_SEC;
1659: timeout.tv_usec = SEL_USEC;
1.66 espie 1660:
1.91 espie 1661: nfds = select(outputsn+1, readfdsp, NULL, NULL, &timeout);
1662: HandleSigs();
1663: if (nfds > 0) {
1.76 espie 1664: for (ln = Lst_First(&jobs); nfds && ln != NULL;
1665: ln = Lst_Adv(ln)) {
1666: job = (Job *)Lst_Datum(ln);
1667: if (FD_ISSET(job->inPipe, readfdsp)) {
1668: JobDoOutput(job, false);
1669: nfds--;
1.66 espie 1670: }
1.1 deraadt 1671: }
1672: }
1.76 espie 1673: free(readfdsp);
1.1 deraadt 1674: }
1675:
1676: /*-
1677: *-----------------------------------------------------------------------
1678: * Job_Make --
1679: * Start the creation of a target. Basically a front-end for
1680: * JobStart used by the Make module.
1681: *
1682: * Side Effects:
1683: * Another job is started.
1684: *-----------------------------------------------------------------------
1685: */
1686: void
1.56 espie 1687: Job_Make(GNode *gn)
1.1 deraadt 1688: {
1.86 espie 1689: (void)JobStart(gn, 0);
1.1 deraadt 1690: }
1691:
1692: /*-
1693: *-----------------------------------------------------------------------
1694: * Job_Init --
1695: * Initialize the process module
1696: *
1697: * Side Effects:
1698: * lists and counters are initialized
1699: *-----------------------------------------------------------------------
1700: */
1701: void
1.83 espie 1702: Job_Init(int maxproc)
1.1 deraadt 1703: {
1.66 espie 1704: Static_Lst_Init(&jobs);
1705: Static_Lst_Init(&stoppedJobs);
1706: maxJobs = maxproc;
1.79 espie 1707: nJobs = 0;
1.66 espie 1708: jobFull = false;
1709:
1710: aborting = 0;
1711: errors = 0;
1.40 espie 1712:
1.66 espie 1713: lastNode = NULL;
1.1 deraadt 1714:
1.66 espie 1715: if (maxJobs == 1) {
1716: /*
1717: * If only one job can run at a time, there's no need for a
1718: * banner, no is there?
1719: */
1720: targFmt = "";
1721: } else {
1722: targFmt = TARG_FMT;
1723: }
1.1 deraadt 1724:
1725: /*
1.66 espie 1726: * Catch the four signals that POSIX specifies if they aren't ignored.
1727: * JobPassSig will take care of calling JobInterrupt if appropriate.
1.1 deraadt 1728: */
1.66 espie 1729: if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
1730: (void)signal(SIGINT, SigHandler);
1731: }
1732: if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
1733: (void)signal(SIGHUP, SigHandler);
1734: }
1735: if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
1736: (void)signal(SIGQUIT, SigHandler);
1737: }
1738: if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
1739: (void)signal(SIGTERM, SigHandler);
1740: }
1.1 deraadt 1741: /*
1.66 espie 1742: * There are additional signals that need to be caught and passed if
1743: * either the export system wants to be told directly of signals or if
1744: * we're giving each job its own process group (since then it won't get
1745: * signals from the terminal driver as we own the terminal)
1.1 deraadt 1746: */
1.48 espie 1747: #if defined(USE_PGRP)
1.66 espie 1748: if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) {
1749: (void)signal(SIGTSTP, SigHandler);
1750: }
1751: if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) {
1752: (void)signal(SIGTTOU, SigHandler);
1753: }
1754: if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) {
1755: (void)signal(SIGTTIN, SigHandler);
1756: }
1757: if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) {
1758: (void)signal(SIGWINCH, SigHandler);
1759: }
1.1 deraadt 1760: #endif
1.6 millert 1761:
1.78 espie 1762: if ((begin_node->type & OP_DUMMY) == 0) {
1.86 espie 1763: JobStart(begin_node, JOB_SPECIAL);
1.66 espie 1764: while (nJobs) {
1765: Job_CatchOutput();
1.76 espie 1766: Job_CatchChildren();
1.66 espie 1767: }
1.1 deraadt 1768: }
1769: }
1770:
1771: /*-
1772: *-----------------------------------------------------------------------
1773: * Job_Full --
1774: * See if the job table is full. It is considered full if it is OR
1775: * if we are in the process of aborting OR if we have
1776: * reached/exceeded our local quota. This prevents any more jobs
1777: * from starting up.
1778: *
1779: * Results:
1.41 espie 1780: * true if the job table is full, false otherwise
1.1 deraadt 1781: *-----------------------------------------------------------------------
1782: */
1.41 espie 1783: bool
1.56 espie 1784: Job_Full(void)
1.1 deraadt 1785: {
1.66 espie 1786: return aborting || jobFull;
1.1 deraadt 1787: }
1788:
1789: /*-
1790: *-----------------------------------------------------------------------
1791: * Job_Empty --
1.40 espie 1792: * See if the job table is empty. Because the local concurrency may
1.1 deraadt 1793: * be set to 0, it is possible for the job table to become empty,
1794: * while the list of stoppedJobs remains non-empty. In such a case,
1795: * we want to restart as many jobs as we can.
1796: *
1797: * Results:
1.41 espie 1798: * true if it is. false if it ain't.
1.1 deraadt 1799: * -----------------------------------------------------------------------
1800: */
1.41 espie 1801: bool
1.56 espie 1802: Job_Empty(void)
1.1 deraadt 1803: {
1.66 espie 1804: if (nJobs == 0) {
1805: if (!Lst_IsEmpty(&stoppedJobs) && !aborting) {
1806: /*
1807: * The job table is obviously not full if it has no
1808: * jobs in it...Try and restart the stopped jobs.
1809: */
1810: jobFull = false;
1811: JobRestartJobs();
1812: return false;
1813: } else {
1814: return true;
1815: }
1.1 deraadt 1816: } else {
1.66 espie 1817: return false;
1.1 deraadt 1818: }
1819: }
1820:
1821: /*-
1822: *-----------------------------------------------------------------------
1823: * JobInterrupt --
1824: * Handle the receipt of an interrupt.
1825: *
1826: * Side Effects:
1827: * All children are killed. Another job will be started if the
1828: * .INTERRUPT target was given.
1829: *-----------------------------------------------------------------------
1830: */
1831: static void
1.56 espie 1832: JobInterrupt(int runINTERRUPT, /* Non-zero if commands for the .INTERRUPT
1.1 deraadt 1833: * target should be executed */
1.66 espie 1834: int signo) /* signal received */
1.1 deraadt 1835: {
1.66 espie 1836: LstNode ln; /* element in job table */
1.78 espie 1837: Job *job; /* job descriptor in that element */
1.66 espie 1838:
1839: aborting = ABORT_INTERRUPT;
1840:
1841: for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Adv(ln)) {
1842: job = (Job *)Lst_Datum(ln);
1843:
1844: if (!Targ_Precious(job->node)) {
1845: const char *file = job->node->path == NULL ?
1846: job->node->name : job->node->path;
1847: if (!noExecute && eunlink(file) != -1) {
1848: Error("*** %s removed", file);
1849: }
1850: }
1851: if (job->pid) {
1.84 espie 1852: debug_printf("JobInterrupt passing signal to "
1853: "child %ld.\n", (long)job->pid);
1.66 espie 1854: KILL(job->pid, signo);
1855: }
1.2 deraadt 1856: }
1.1 deraadt 1857:
1.66 espie 1858: if (runINTERRUPT && !touchFlag) {
1.78 espie 1859: if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66 espie 1860: ignoreErrors = false;
1861:
1.86 espie 1862: JobStart(interrupt_node, JOB_IGNDOTS);
1.66 espie 1863: while (nJobs) {
1864: Job_CatchOutput();
1.76 espie 1865: Job_CatchChildren();
1.66 espie 1866: }
1867: }
1.1 deraadt 1868: }
1.66 espie 1869: exit(signo);
1.1 deraadt 1870: }
1871:
1872: /*
1873: *-----------------------------------------------------------------------
1.12 espie 1874: * Job_Finish --
1.1 deraadt 1875: * Do final processing such as the running of the commands
1.6 millert 1876: * attached to the .END target.
1.1 deraadt 1877: *
1878: * Results:
1879: * Number of errors reported.
1.40 espie 1880: *
1.1 deraadt 1881: *-----------------------------------------------------------------------
1882: */
1883: int
1.56 espie 1884: Job_Finish(void)
1.1 deraadt 1885: {
1.78 espie 1886: if (end_node != NULL && !Lst_IsEmpty(&end_node->commands)) {
1.66 espie 1887: if (errors) {
1888: Error("Errors reported so .END ignored");
1889: } else {
1.86 espie 1890: JobStart(end_node, JOB_SPECIAL | JOB_IGNDOTS);
1.1 deraadt 1891:
1.66 espie 1892: while (nJobs) {
1893: Job_CatchOutput();
1.76 espie 1894: Job_CatchChildren();
1.66 espie 1895: }
1896: }
1.1 deraadt 1897: }
1.66 espie 1898: return errors;
1.1 deraadt 1899: }
1900:
1.41 espie 1901: #ifdef CLEANUP
1.12 espie 1902: void
1.56 espie 1903: Job_End(void)
1.12 espie 1904: {
1.41 espie 1905: }
1.13 espie 1906: #endif
1.40 espie 1907:
1.1 deraadt 1908: /*-
1909: *-----------------------------------------------------------------------
1910: * Job_Wait --
1911: * Waits for all running jobs to finish and returns. Sets 'aborting'
1912: * to ABORT_WAIT to prevent other jobs from starting.
1913: *
1914: * Side Effects:
1915: * Currently running jobs finish.
1916: *
1917: *-----------------------------------------------------------------------
1918: */
1919: void
1.56 espie 1920: Job_Wait(void)
1.1 deraadt 1921: {
1.66 espie 1922: aborting = ABORT_WAIT;
1923: while (nJobs != 0) {
1924: Job_CatchOutput();
1.76 espie 1925: Job_CatchChildren();
1.66 espie 1926: }
1927: aborting = 0;
1.1 deraadt 1928: }
1929:
1930: /*-
1931: *-----------------------------------------------------------------------
1932: * Job_AbortAll --
1933: * Abort all currently running jobs without handling output or anything.
1934: * This function is to be called only in the event of a major
1935: * error. Most definitely NOT to be called from JobInterrupt.
1936: *
1937: * Side Effects:
1938: * All children are killed, not just the firstborn
1939: *-----------------------------------------------------------------------
1940: */
1941: void
1.56 espie 1942: Job_AbortAll(void)
1.1 deraadt 1943: {
1.66 espie 1944: LstNode ln; /* element in job table */
1945: Job *job; /* the job descriptor in that element */
1946: int foo;
1.6 millert 1947:
1.66 espie 1948: aborting = ABORT_ERROR;
1.6 millert 1949:
1.66 espie 1950: if (nJobs) {
1951: for (ln = Lst_First(&jobs); ln != NULL; ln = Lst_Adv(ln)) {
1952: job = (Job *)Lst_Datum(ln);
1953:
1954: /*
1955: * kill the child process with increasingly drastic
1956: * signals to make darn sure it's dead.
1957: */
1958: KILL(job->pid, SIGINT);
1959: KILL(job->pid, SIGKILL);
1960: }
1.1 deraadt 1961: }
1.6 millert 1962:
1.66 espie 1963: /*
1964: * Catch as many children as want to report in at first, then give up
1965: */
1966: while (waitpid(-1, &foo, WNOHANG) > 0)
1967: continue;
1.2 deraadt 1968: }
1.40 espie 1969:
1.2 deraadt 1970: /*-
1971: *-----------------------------------------------------------------------
1972: * JobRestartJobs --
1973: * Tries to restart stopped jobs if there are slots available.
1974: * Note that this tries to restart them regardless of pending errors.
1975: * It's not good to leave stopped jobs lying around!
1976: *
1977: * Side Effects:
1978: * Resumes(and possibly migrates) jobs.
1979: *-----------------------------------------------------------------------
1980: */
1981: static void
1.56 espie 1982: JobRestartJobs(void)
1.2 deraadt 1983: {
1.66 espie 1984: Job *job;
1.19 espie 1985:
1.66 espie 1986: while (!jobFull && (job = (Job *)Lst_DeQueue(&stoppedJobs)) != NULL) {
1.84 espie 1987: debug_printf("Job queue is not full. "
1988: "Restarting a stopped job.\n");
1.66 espie 1989: JobRestart(job);
1.2 deraadt 1990: }
1.1 deraadt 1991: }