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