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