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