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