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