Annotation of src/usr.bin/make/job.c, Revision 1.119
1.40 espie 1: /* $OpenPackages$ */
1.93 espie 2: /* $OpenBSD$ */
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.53 jmc 46: * Job_Init Called to initialize this module. in addition,
1.40 espie 47: * any commands attached to the .BEGIN target
48: * are executed before this function returns.
49: * Hence, the makefile must have been parsed
50: * before this function is called.
51: *
52: * Job_End Cleanup any memory used.
53: *
1.117 espie 54: * can_start_job Return true if we can start job
1.40 espie 55: *
1.41 espie 56: * Job_Empty Return true if the job table is completely
1.40 espie 57: * empty.
58: *
59: * Job_Finish Perform any final processing which needs doing.
60: * This includes the execution of any commands
61: * which have been/were attached to the .END
62: * target. It should only be called when the
63: * job table is empty.
64: *
1.117 espie 65: * Job_AbortAll Abort all current jobs. It doesn't
1.40 espie 66: * handle output or do anything for the jobs,
67: * just kills them. It should only be called in
68: * an emergency, as it were.
1.1 deraadt 69: *
1.117 espie 70: * Job_Wait Wait for all running jobs to finish.
1.1 deraadt 71: */
72:
73: #include <sys/types.h>
74: #include <sys/wait.h>
1.41 espie 75: #include <ctype.h>
76: #include <errno.h>
1.1 deraadt 77: #include <fcntl.h>
1.41 espie 78: #include <signal.h>
1.69 espie 79: #include <stdarg.h>
1.1 deraadt 80: #include <stdio.h>
1.42 espie 81: #include <stdlib.h>
1.1 deraadt 82: #include <string.h>
1.41 espie 83: #include <unistd.h>
84: #include "config.h"
85: #include "defines.h"
1.1 deraadt 86: #include "job.h"
1.63 espie 87: #include "engine.h"
1.1 deraadt 88: #include "pathnames.h"
1.41 espie 89: #include "var.h"
90: #include "targ.h"
91: #include "error.h"
92: #include "lst.h"
93: #include "extern.h"
94: #include "gnode.h"
95: #include "memory.h"
96: #include "make.h"
97:
1.50 espie 98: /*
99: * The SEL_ constants determine the maximum amount of time spent in select
100: * before coming out to see if a child has finished. SEL_SEC is the number of
101: * seconds and SEL_USEC is the number of micro-seconds
102: */
103: #define SEL_SEC 0
104: #define SEL_USEC 500000
105:
106:
107: /*-
108: * Job Table definitions.
109: *
110: * Each job has several things associated with it:
111: * 1) The process id of the child shell
112: * 2) The graph node describing the target being made by this job
1.93 espie 113: * 3) An FILE* for writing out the commands. This is only
1.50 espie 114: * used before the job is actually started.
1.93 espie 115: * 4) Things used for handling the shell's output.
1.76 espie 116: * the output is being caught via a pipe and
1.50 espie 117: * the descriptors of our pipe, an array in which output is line
118: * buffered and the current position in that buffer are all
1.78 espie 119: * maintained for each job.
1.93 espie 120: * 5) A word of flags which determine how the module handles errors,
1.50 espie 121: * echoing, etc. for the job
122: *
123: * The job "table" is kept as a linked Lst in 'jobs', with the number of
124: * active jobs maintained in the 'nJobs' variable. At no time will this
125: * exceed the value of 'maxJobs', initialized by the Job_Init function.
126: *
127: * When a job is finished, the Make_Update function is called on each of the
128: * parents of the node which was just remade. This takes care of the upward
129: * traversal of the dependency graph.
130: */
131: #define JOB_BUFSIZE 1024
1.101 espie 132: struct job_pipe {
133: int fd;
134: char buffer[JOB_BUFSIZE];
135: size_t pos;
136: };
1.119 ! espie 137:
1.50 espie 138: typedef struct Job_ {
1.51 mpech 139: pid_t pid; /* The child's process ID */
1.50 espie 140: GNode *node; /* The target the child is making */
141: short flags; /* Flags to control treatment of job */
1.117 espie 142: LstNode p;
1.111 espie 143: #define JOB_DIDOUTPUT 0x001
1.117 espie 144: #define JOB_IS_SPECIAL 0x004 /* Target is a special one. */
145: #define JOB_IS_EXPENSIVE 0x002
1.101 espie 146: struct job_pipe in[2];
1.50 espie 147: } Job;
148:
1.117 espie 149: struct job_pid {
150: pid_t pid;
151: };
1.78 espie 152:
1.40 espie 153: static int aborting = 0; /* why is the make aborting? */
154: #define ABORT_ERROR 1 /* Because of an error */
155: #define ABORT_INTERRUPT 2 /* Because it was interrupted */
156: #define ABORT_WAIT 3 /* Waiting for jobs to finish */
1.1 deraadt 157:
1.40 espie 158: static int maxJobs; /* The most children we can run at once */
1.117 espie 159: static int nJobs; /* The number of current children */
160: static bool expensive_job;
1.95 espie 161: static LIST runningJobs; /* The structures that describe them */
1.48 espie 162: static GNode *lastNode; /* The node for which output was most recently
1.1 deraadt 163: * produced. */
1.117 espie 164: static LIST job_pids; /* a simple list that doesn't move that much */
1.112 espie 165:
166: /* data structure linked to job handling through select */
167: static fd_set *output_mask = NULL; /* File descriptors to look for */
1.119 ! espie 168:
1.112 espie 169: static fd_set *actual_mask = NULL; /* actual select argument */
170: static int largest_fd = -1;
171: static size_t mask_size = 0;
172:
173: /* wait possibilities */
174: #define JOB_EXITED 0
175: #define JOB_SIGNALED 1
176: #define JOB_UNKNOWN 4
177:
1.101 espie 178: static LIST errorsList;
179: static int errors;
180: struct error_info {
1.112 espie 181: int reason;
182: int code;
1.111 espie 183: GNode *n;
1.101 espie 184: };
185:
1.117 espie 186: /* for blocking/unblocking */
187: static sigset_t oset, set;
188: static void block_signals(void);
189: static void unblock_signals(void);
1.1 deraadt 190:
1.93 espie 191: static void handle_all_signals(void);
192: static void handle_signal(int);
1.40 espie 193: static int JobCmpPid(void *, void *);
1.117 espie 194: static void process_job_status(Job *, int);
1.101 espie 195: static void JobExec(Job *);
1.97 espie 196: static void JobStart(GNode *, int);
1.117 espie 197: static void JobInterrupt(bool, int);
1.84 espie 198: static void debug_printf(const char *, ...);
1.98 espie 199: static Job *prepare_job(GNode *, int);
1.105 espie 200: static void banner(Job *, FILE *);
1.117 espie 201: static bool Job_Full(void);
1.112 espie 202:
203: /***
204: *** Input/output from jobs
205: ***/
206:
207: /* prepare_pipe(jp, &fd):
208: * set up pipe data structure (buffer and pos) corresponding to
209: * pointed fd, and prepare to watch for it.
210: */
211: static void prepare_pipe(struct job_pipe *, int *);
212:
213: /* close_job_pipes(j):
214: * handle final output from job, and close pipes properly
215: */
216: static void close_job_pipes(Job *);
217:
218:
219: static void handle_all_jobs_output(void);
220:
1.119 ! espie 221: /* handle_job_output(job, n, finish):
1.112 espie 222: * n = 0 or 1 (stdout/stderr), set finish to retrieve everything.
223: */
224: static void handle_job_output(Job *, int, bool);
225:
1.101 espie 226: static void print_partial_buffer(struct job_pipe *, Job *, FILE *, size_t);
1.119 ! espie 227: static void print_partial_buffer_and_shift(struct job_pipe *, Job *, FILE *,
1.101 espie 228: size_t);
229: static bool print_complete_lines(struct job_pipe *, Job *, FILE *, size_t);
1.112 espie 230:
231:
232: static void register_error(int, int, Job *);
1.111 espie 233: static void loop_handle_running_jobs(void);
234: static void Job_CatchChildren(void);
1.93 espie 235:
1.101 espie 236: static void
1.112 espie 237: register_error(int reason, int code, Job *job)
1.101 espie 238: {
239: struct error_info *p;
1.1 deraadt 240:
1.101 espie 241: errors++;
242: p = emalloc(sizeof(struct error_info));
1.112 espie 243: p->reason = reason;
244: p->code = code;
1.111 espie 245: p->n = job->node;
246: Lst_AtEnd(&errorsList, p);
1.101 espie 247: }
1.93 espie 248:
1.101 espie 249: void
250: print_errors()
1.88 espie 251: {
1.101 espie 252: LstNode ln;
253: struct error_info *p;
1.111 espie 254: const char *type;
1.88 espie 255:
1.101 espie 256: for (ln = Lst_First(&errorsList); ln != NULL; ln = Lst_Adv(ln)) {
257: p = (struct error_info *)Lst_Datum(ln);
1.112 espie 258: switch(p->reason) {
259: case JOB_EXITED:
1.111 espie 260: type = "Exit status";
1.112 espie 261: break;
262: case JOB_SIGNALED:
1.111 espie 263: type = "Received signal";
1.112 espie 264: break;
265: default:
266: type = "Should not happen";
267: break;
1.101 espie 268: }
1.111 espie 269: if (p->n->lineno)
270: Error(" %s %d (%s, line %lu of %s)",
1.112 espie 271: type, p->code, p->n->name, p->n->lineno, p->n->fname);
1.119 ! espie 272: else
1.112 espie 273: Error(" %s %d (%s)", type, p->code, p->n->name);
1.101 espie 274: }
1.88 espie 275: }
276:
1.57 espie 277: static void
1.105 espie 278: banner(Job *job, FILE *out)
1.57 espie 279: {
1.101 espie 280: if (job->node != lastNode) {
1.119 ! espie 281: if (DEBUG(JOBBANNER))
1.101 espie 282: (void)fprintf(out, "--- %s ---\n", job->node->name);
283: lastNode = job->node;
1.57 espie 284: }
285: }
286:
1.117 espie 287: volatile sig_atomic_t got_SIGTSTP, got_SIGTTOU, got_SIGTTIN, got_SIGWINCH,
288: got_SIGCONT;
1.57 espie 289: static void
1.93 espie 290: handle_all_signals()
1.57 espie 291: {
1.117 espie 292: while (got_signal) {
1.93 espie 293: got_signal = 0;
294:
1.117 espie 295: if (got_SIGINT) {
296: got_SIGINT=0;
297: handle_signal(SIGINT);
298: }
299: if (got_SIGHUP) {
300: got_SIGHUP=0;
301: handle_signal(SIGHUP);
302: }
303: if (got_SIGQUIT) {
304: got_SIGQUIT=0;
305: handle_signal(SIGQUIT);
306: }
307: if (got_SIGTERM) {
308: got_SIGTERM=0;
309: handle_signal(SIGTERM);
310: }
311: if (got_SIGTSTP) {
312: got_SIGTSTP=0;
313: signal(SIGTSTP, parallel_handler);
314: }
315: if (got_SIGTTOU) {
316: got_SIGTTOU=0;
317: signal(SIGTTOU, parallel_handler);
318: }
319: if (got_SIGTTIN) {
320: got_SIGTTIN=0;
321: signal(SIGTTIN, parallel_handler);
322: }
323: if (got_SIGWINCH) {
324: got_SIGWINCH=0;
325: signal(SIGWINCH, parallel_handler);
326: }
327: if (got_SIGCONT) {
328: got_SIGCONT = 0;
329: signal(SIGCONT, parallel_handler);
330: }
1.57 espie 331: }
332: }
333:
1.117 espie 334: /* this is safe from interrupts, actually */
335: void
336: parallel_handler(int signo)
1.1 deraadt 337: {
1.117 espie 338: int save_errno = errno;
1.114 espie 339: LstNode ln;
1.117 espie 340: for (ln = Lst_First(&job_pids); ln != NULL; ln = Lst_Adv(ln)) {
341: struct job_pid *p = Lst_Datum(ln);
342: killpg(p->pid, signo);
343: }
344: errno = save_errno;
345:
346: switch(signo) {
347: case SIGINT:
348: got_SIGINT++;
349: got_signal = 1;
350: return;
351: case SIGHUP:
352: got_SIGHUP++;
353: got_signal = 1;
354: return;
355: case SIGQUIT:
356: got_SIGQUIT++;
357: got_signal = 1;
358: return;
359: case SIGTERM:
360: got_SIGTERM++;
361: got_signal = 1;
362: return;
363: case SIGTSTP:
364: got_SIGTSTP++;
365: got_signal = 1;
366: break;
367: case SIGTTOU:
368: got_SIGTTOU++;
369: got_signal = 1;
370: break;
371: case SIGTTIN:
372: got_SIGTTIN++;
373: got_signal = 1;
374: break;
375: case SIGWINCH:
376: got_SIGWINCH++;
377: got_signal = 1;
378: break;
379: case SIGCONT:
380: got_SIGCONT++;
381: got_signal = 1;
382: break;
1.66 espie 383: }
1.117 espie 384: (void)killpg(getpid(), signo);
385:
386: (void)signal(signo, SIG_DFL);
387: errno = save_errno;
1.1 deraadt 388: }
389:
390: /*-
391: *-----------------------------------------------------------------------
1.94 espie 392: * handle_signal --
1.117 espie 393: * handle a signal for ourselves
1.1 deraadt 394: *
395: *-----------------------------------------------------------------------
396: */
397: static void
1.117 espie 398: handle_signal(int signo)
1.1 deraadt 399: {
1.66 espie 400: if (DEBUG(JOB)) {
1.93 espie 401: (void)fprintf(stdout, "handle_signal(%d) called.\n", signo);
1.66 espie 402: (void)fflush(stdout);
403: }
404:
405: /*
406: * Deal with proper cleanup based on the signal received. We only run
407: * the .INTERRUPT target if the signal was in fact an interrupt. The
408: * other three termination signals are more of a "get out *now*"
409: * command.
410: */
1.117 espie 411: if (signo == SIGINT)
1.66 espie 412: JobInterrupt(true, signo);
1.117 espie 413: else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT)
1.66 espie 414: JobInterrupt(false, signo);
1.1 deraadt 415:
1.117 espie 416: if (signo == SIGQUIT)
1.66 espie 417: Finish(0);
1.1 deraadt 418: }
419:
420: /*-
421: *-----------------------------------------------------------------------
422: * JobCmpPid --
423: * Compare the pid of the job with the given pid and return 0 if they
424: * are equal. This function is called from Job_CatchChildren via
425: * Lst_Find to find the job descriptor of the finished job.
426: *
427: * Results:
428: * 0 if the pid's match
429: *-----------------------------------------------------------------------
430: */
431: static int
1.56 espie 432: JobCmpPid(void *job, /* job to examine */
433: void *pid) /* process id desired */
1.1 deraadt 434: {
1.66 espie 435: return *(pid_t *)pid - ((Job *)job)->pid;
1.1 deraadt 436: }
437:
1.69 espie 438: static void
1.84 espie 439: debug_printf(const char *fmt, ...)
440: {
441: if (DEBUG(JOB)) {
442: va_list va;
443:
444: va_start(va, fmt);
445: (void)vfprintf(stdout, fmt, va);
446: fflush(stdout);
447: va_end(va);
448: }
449: }
1.119 ! espie 450:
1.2 deraadt 451: static void
1.112 espie 452: close_job_pipes(Job *job)
1.2 deraadt 453: {
1.101 espie 454: int i;
455:
1.112 espie 456: for (i = 1; i >= 0; i--) {
457: FD_CLR(job->in[i].fd, output_mask);
1.101 espie 458: handle_job_output(job, i, true);
459: (void)close(job->in[i].fd);
1.66 espie 460: }
1.1 deraadt 461: }
462:
463: /*-
464: *-----------------------------------------------------------------------
1.117 espie 465: * process_job_status --
466: * Do processing for the given job including updating
1.119 ! espie 467: * parents and starting new jobs as available/necessary.
1.1 deraadt 468: *
469: * Side Effects:
470: * Some nodes may be put on the toBeMade queue.
1.78 espie 471: * Final commands for the job are placed on end_node.
1.1 deraadt 472: *
1.6 millert 473: * If we got an error and are aborting (aborting == ABORT_ERROR) and
1.1 deraadt 474: * the job list is now empty, we are done for the day.
1.101 espie 475: * If we recognized an error we set the aborting flag
1.1 deraadt 476: * to ABORT_ERROR so no more jobs will be started.
477: *-----------------------------------------------------------------------
478: */
479: /*ARGSUSED*/
1.112 espie 480:
1.1 deraadt 481: static void
1.117 espie 482: process_job_status(Job *job, int status)
1.2 deraadt 483: {
1.112 espie 484: int reason, code;
1.117 espie 485: bool done;
486:
1.119 ! espie 487: debug_printf("Process %ld (%s) exited with status %d.\n",
1.117 espie 488: (long)job->pid, job->node->name, status);
1.112 espie 489: /* parse status */
490: if (WIFEXITED(status)) {
491: reason = JOB_EXITED;
492: code = WEXITSTATUS(status);
493: } else if (WIFSIGNALED(status)) {
494: reason = JOB_SIGNALED;
495: code = WTERMSIG(status);
496: } else {
497: /* can't happen, set things to be bad. */
498: reason = UNKNOWN;
499: code = status;
500: }
1.2 deraadt 501:
1.112 espie 502: if ((reason == JOB_EXITED &&
503: code != 0 && !(job->node->type & OP_IGNORE)) ||
1.117 espie 504: reason == JOB_SIGNALED) {
1.66 espie 505: /*
506: * If it exited non-zero and either we're doing things our
507: * way or we're not ignoring errors, the job is finished.
508: * Similarly, if the shell died because of a signal
509: * the job is also finished. In these
510: * cases, finish out the job's output before printing the exit
511: * status...
512: */
1.112 espie 513: close_job_pipes(job);
1.66 espie 514: done = true;
1.112 espie 515: } else if (reason == JOB_EXITED) {
1.66 espie 516: /*
1.117 espie 517: * Deal with ignored errors. We need to print a message telling
518: * of the ignored error as well as setting status.w_status to 0
519: * so the next command gets run. To do this, we set done to be
520: * true and the job exited non-zero.
1.66 espie 521: */
1.112 espie 522: done = code != 0;
523: close_job_pipes(job);
1.66 espie 524: } else {
525: /*
526: * No need to close things down or anything.
527: */
528: done = false;
1.1 deraadt 529: }
1.6 millert 530:
1.112 espie 531: if (done || DEBUG(JOB)) {
532: if (reason == JOB_EXITED) {
1.119 ! espie 533: debug_printf("Process %ld (%s) exited.\n",
1.117 espie 534: (long)job->pid, job->node->name);
1.112 espie 535: if (code != 0) {
1.105 espie 536: banner(job, stdout);
1.111 espie 537: (void)fprintf(stdout, "*** Error code %d %s\n",
1.112 espie 538: code,
1.119 ! espie 539: (job->node->type & OP_IGNORE) ?
1.106 espie 540: "(ignored)" : "");
1.66 espie 541:
1.106 espie 542: if (job->node->type & OP_IGNORE) {
1.112 espie 543: reason = JOB_EXITED;
544: code = 0;
1.66 espie 545: }
546: } else if (DEBUG(JOB)) {
1.101 espie 547: (void)fprintf(stdout,
1.119 ! espie 548: "*** %ld (%s) Completed successfully\n",
1.117 espie 549: (long)job->pid, job->node->name);
1.66 espie 550: }
551: } else {
1.105 espie 552: banner(job, stdout);
1.112 espie 553: (void)fprintf(stdout, "*** Signal %d\n", code);
1.40 espie 554: }
1.66 espie 555:
1.101 espie 556: (void)fflush(stdout);
1.1 deraadt 557: }
558:
1.85 espie 559: done = true;
1.1 deraadt 560:
1.66 espie 561: if (done &&
562: aborting != ABORT_ERROR &&
563: aborting != ABORT_INTERRUPT &&
1.112 espie 564: reason == JOB_EXITED && code == 0) {
1.66 espie 565: /* As long as we aren't aborting and the job didn't return a
566: * non-zero status that we shouldn't ignore, we call
1.101 espie 567: * Make_Update to update the parents. */
1.108 espie 568: job->node->built_status = MADE;
1.66 espie 569: Make_Update(job->node);
1.112 espie 570: } else if (!(reason == JOB_EXITED && code == 0)) {
571: register_error(reason, code, job);
1.66 espie 572: }
1.117 espie 573: free(job);
1.1 deraadt 574:
1.119 ! espie 575: if (errors && !keepgoing &&
1.117 espie 576: aborting != ABORT_INTERRUPT)
1.66 espie 577: aborting = ABORT_ERROR;
1.6 millert 578:
1.117 espie 579: if (aborting == ABORT_ERROR && Job_Empty())
1.66 espie 580: Finish(errors);
1.1 deraadt 581: }
582:
1.119 ! espie 583: static void
1.101 espie 584: prepare_pipe(struct job_pipe *p, int *fd)
585: {
586: p->pos = 0;
587: (void)fcntl(fd[0], F_SETFD, FD_CLOEXEC);
1.119 ! espie 588: p->fd = fd[0];
1.101 espie 589: close(fd[1]);
590:
1.112 espie 591: if (output_mask == NULL || p->fd > largest_fd) {
1.101 espie 592: int fdn, ofdn;
593:
594: fdn = howmany(p->fd+1, NFDBITS);
1.112 espie 595: ofdn = howmany(largest_fd+1, NFDBITS);
1.101 espie 596:
597: if (fdn != ofdn) {
1.119 ! espie 598: output_mask = emult_realloc(output_mask, fdn,
1.112 espie 599: sizeof(fd_mask));
1.119 ! espie 600: memset(((char *)output_mask) + ofdn * sizeof(fd_mask),
1.113 espie 601: 0, (fdn-ofdn) * sizeof(fd_mask));
1.119 ! espie 602: actual_mask = emult_realloc(actual_mask, fdn,
1.112 espie 603: sizeof(fd_mask));
604: mask_size = fdn * sizeof(fd_mask);
1.101 espie 605: }
1.112 espie 606: largest_fd = p->fd;
1.101 espie 607: }
608: fcntl(p->fd, F_SETFL, O_NONBLOCK);
1.112 espie 609: FD_SET(p->fd, output_mask);
1.101 espie 610: }
611:
1.1 deraadt 612: /*-
613: *-----------------------------------------------------------------------
614: * JobExec --
1.119 ! espie 615: * Execute the shell for the given job. Called from JobStart
1.1 deraadt 616: *
617: * Side Effects:
618: * A shell is executed, outputs is altered and the Job structure added
619: * to the job table.
620: *-----------------------------------------------------------------------
621: */
622: static void
1.101 espie 623: JobExec(Job *job)
1.1 deraadt 624: {
1.66 espie 625: pid_t cpid; /* ID of new child */
1.117 espie 626: struct job_pid *p;
1.101 espie 627: int fds[4];
628: int *fdout = fds;
629: int *fderr = fds+2;
630: int i;
1.6 millert 631:
1.105 espie 632: banner(job, stdout);
1.101 espie 633:
1.117 espie 634: setup_engine(1);
1.101 espie 635:
1.119 ! espie 636: /* Create the pipe by which we'll get the shell's output.
1.101 espie 637: */
638: if (pipe(fdout) == -1)
639: Punt("Cannot create pipe: %s", strerror(errno));
1.1 deraadt 640:
1.101 espie 641: if (pipe(fderr) == -1)
642: Punt("Cannot create pipe: %s", strerror(errno));
1.94 espie 643:
1.117 espie 644: block_signals();
1.66 espie 645: if ((cpid = fork()) == -1) {
646: Punt("Cannot fork");
1.117 espie 647: unblock_signals();
1.66 espie 648: } else if (cpid == 0) {
1.104 espie 649: supervise_jobs = false;
1.101 espie 650: /* standard pipe code to route stdout and stderr */
651: close(fdout[0]);
652: if (dup2(fdout[1], 1) == -1)
653: Punt("Cannot dup2(outPipe): %s", strerror(errno));
654: if (fdout[1] != 1)
655: close(fdout[1]);
656: close(fderr[0]);
657: if (dup2(fderr[1], 2) == -1)
658: Punt("Cannot dup2(errPipe): %s", strerror(errno));
659: if (fderr[1] != 2)
660: close(fderr[1]);
1.1 deraadt 661:
1.66 espie 662: /*
663: * We want to switch the child into a different process family
664: * so we can kill it and all its descendants in one fell swoop,
665: * by killing its process family, but not commit suicide.
666: */
667: (void)setpgid(0, getpid());
1.1 deraadt 668:
1.109 espie 669: if (random_delay)
1.110 espie 670: if (!(nJobs == 1 && no_jobs_left()))
671: usleep(random() % random_delay);
1.109 espie 672:
1.117 espie 673: setup_all_signals(SigHandler, SIG_DFL);
674: unblock_signals();
1.116 espie 675: /* this exits directly */
676: run_gnode_parallel(job->node);
677: /*NOTREACHED*/
1.66 espie 678: } else {
1.104 espie 679: supervise_jobs = true;
1.66 espie 680: job->pid = cpid;
681:
1.101 espie 682: /* we set the current position in the buffers to the beginning
1.87 espie 683: * and mark another stream to watch in the outputs mask
684: */
1.101 espie 685: for (i = 0; i < 2; i++)
686: prepare_pipe(&job->in[i], fds+2*i);
1.1 deraadt 687: }
1.48 espie 688: /*
1.66 espie 689: * Now the job is actually running, add it to the table.
1.48 espie 690: */
1.67 espie 691: nJobs++;
1.95 espie 692: Lst_AtEnd(&runningJobs, job);
1.117 espie 693: if (job->flags & JOB_IS_EXPENSIVE)
694: expensive_job = true;
695: p = emalloc(sizeof(struct job_pid));
696: p->pid = cpid;
697: Lst_AtEnd(&job_pids, p);
698: job->p = Lst_Last(&job_pids);
1.1 deraadt 699:
1.117 espie 700: unblock_signals();
1.96 espie 701: if (DEBUG(JOB)) {
1.117 espie 702: LstNode ln;
703:
704: (void)fprintf(stdout, "Running %ld (%s)\n", (long)cpid,
1.96 espie 705: job->node->name);
1.119 ! espie 706: for (ln = Lst_First(&job->node->commands); ln != NULL ;
1.117 espie 707: ln = Lst_Adv(ln))
708: fprintf(stdout, "\t%s\n", (char *)Lst_Datum(ln));
1.96 espie 709: (void)fflush(stdout);
710: }
1.117 espie 711:
1.96 espie 712: }
713:
1.117 espie 714: static bool
715: expensive_command(const char *s)
1.1 deraadt 716: {
1.117 espie 717: const char *p;
718: bool include = false;
719: bool expensive = false;
1.66 espie 720:
1.117 espie 721: /* okay, comments are cheap, always */
722: if (*s == '#')
723: return false;
1.66 espie 724:
1.117 espie 725: for (p = s; *p != '\0'; p++) {
726: if (*p == ' ' || *p == '\t') {
727: include = false;
728: if (p[1] == '-' && p[2] == 'I')
729: include = true;
730: }
731: if (include)
732: continue;
733: /* KMP variant, avoid looking twice at the same
734: * letter.
735: */
736: if (*p != 'm')
737: continue;
738: if (p[1] != 'a')
739: continue;
740: p++;
741: if (p[1] != 'k')
742: continue;
743: p++;
744: if (p[1] != 'e')
745: continue;
746: p++;
747: expensive = true;
748: while (p[1] != '\0' && p[1] != ' ' && p[1] != '\t') {
749: if (p[1] == '.') {
750: expensive = false;
751: break;
1.66 espie 752: }
1.117 espie 753: p++;
1.1 deraadt 754: }
1.117 espie 755: if (expensive)
756: return true;
1.1 deraadt 757: }
1.117 espie 758: return false;
759: }
760:
761: static bool
762: expensive_commands(Lst l)
763: {
764: LstNode ln;
765: for (ln = Lst_First(l); ln != NULL; ln = Lst_Adv(ln))
766: if (expensive_command(Lst_Datum(ln)))
767: return true;
768: return false;
1.1 deraadt 769: }
770:
1.98 espie 771: static Job *
772: prepare_job(GNode *gn, int flags)
1.78 espie 773: {
774: bool cmdsOK; /* true if the nodes commands were all right */
775: bool noExec; /* Set true if we decide not to run the job */
1.66 espie 776:
1.1 deraadt 777: /*
1.66 espie 778: * Check the commands now so any attributes from .DEFAULT have a chance
779: * to migrate to the node
1.1 deraadt 780: */
1.114 espie 781: cmdsOK = Job_CheckCommands(gn);
1.112 espie 782: expand_commands(gn);
1.1 deraadt 783:
1.66 espie 784: if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
785: /*
786: * We're serious here, but if the commands were bogus, we're
787: * also dead...
788: */
1.114 espie 789: if (!cmdsOK)
790: job_failure(gn, Punt);
1.6 millert 791:
1.101 espie 792: if (Lst_IsEmpty(&gn->commands))
793: noExec = true;
794: else
795: noExec = false;
1.66 espie 796:
1.101 espie 797: } else if (noExecute) {
798: if (!cmdsOK || Lst_IsEmpty(&gn->commands))
1.85 espie 799: noExec = true;
1.101 espie 800: else
801: noExec = false;
1.66 espie 802: } else {
803: /*
804: * Just touch the target and note that no shell should be
1.101 espie 805: * executed. Check
1.66 espie 806: * the commands, too, but don't die if they're no good -- it
807: * does no harm to keep working up the graph.
1.30 espie 808: */
1.106 espie 809: Job_Touch(gn);
1.41 espie 810: noExec = true;
1.1 deraadt 811: }
1.66 espie 812:
1.1 deraadt 813: /*
1.66 espie 814: * If we're not supposed to execute a shell, don't.
1.1 deraadt 815: */
1.66 espie 816: if (noExec) {
817: /*
818: * We only want to work our way up the graph if we aren't here
819: * because the commands for the job were no good.
820: */
1.115 espie 821: if (cmdsOK && !aborting) {
822: gn->built_status = MADE;
823: Make_Update(gn);
1.66 espie 824: }
1.99 espie 825: return NULL;
1.1 deraadt 826: } else {
1.115 espie 827: Job *job; /* new job descriptor */
828: job = emalloc(sizeof(Job));
829: if (job == NULL)
830: Punt("JobStart out of memory");
831:
832: job->node = gn;
833:
834: /*
835: * Set the initial value of the flags for this job based on the
836: * global ones and the node's attributes... Any flags supplied
837: * by the caller are also added to the field.
838: */
839: job->flags = flags;
1.117 espie 840: if (expensive_commands(&gn->expanded)) {
841: job->flags |= JOB_IS_EXPENSIVE;
842: }
1.115 espie 843:
1.98 espie 844: return job;
1.1 deraadt 845: }
1.98 espie 846: }
1.1 deraadt 847:
1.98 espie 848: /*-
849: *-----------------------------------------------------------------------
850: * JobStart --
851: * Start a target-creation process going for the target described
852: * by the graph node gn.
853: *
854: * Side Effects:
855: * A new Job node is created and added to the list of running
1.114 espie 856: * jobs. Make is forked and a child shell created.
1.98 espie 857: *-----------------------------------------------------------------------
858: */
859: static void
860: JobStart(GNode *gn, /* target to create */
861: int flags) /* flags for the job to override normal ones.
1.117 espie 862: * e.g. JOB_IS_SPECIAL */
1.98 espie 863: {
864: Job *job;
865: job = prepare_job(gn, flags);
866: if (!job)
867: return;
1.117 espie 868: JobExec(job);
1.1 deraadt 869: }
870:
1.101 espie 871: /* Helper functions for JobDoOutput */
872:
873:
1.105 espie 874: /* output debugging banner and print characters from 0 to endpos */
1.101 espie 875: static void
876: print_partial_buffer(struct job_pipe *p, Job *job, FILE *out, size_t endPos)
1.2 deraadt 877: {
1.101 espie 878: size_t i;
1.2 deraadt 879:
1.105 espie 880: banner(job, out);
1.111 espie 881: job->flags |= JOB_DIDOUTPUT;
1.101 espie 882: for (i = 0; i < endPos; i++)
883: putc(p->buffer[i], out);
884: }
885:
886: /* print partial buffer and shift remaining contents */
887: static void
888: print_partial_buffer_and_shift(struct job_pipe *p, Job *job, FILE *out,
889: size_t endPos)
890: {
891: size_t i;
892:
893: print_partial_buffer(p, job, out, endPos);
894:
895: for (i = endPos; i < p->pos; i++)
896: p->buffer[i-endPos] = p->buffer[i];
897: p->pos -= endPos;
898: }
899:
900: /* print complete lines, looking back to the limit position
901: * (stuff before limit was already scanned).
902: * returns true if something was printed.
903: */
904: static bool
905: print_complete_lines(struct job_pipe *p, Job *job, FILE *out, size_t limit)
906: {
907: size_t i;
908:
909: for (i = p->pos; i > limit; i--) {
910: if (p->buffer[i-1] == '\n') {
911: print_partial_buffer_and_shift(p, job, out, i);
912: return true;
1.2 deraadt 913: }
914: }
1.101 espie 915: return false;
1.2 deraadt 916: }
1.111 espie 917:
1.1 deraadt 918: /*-
919: *-----------------------------------------------------------------------
1.101 espie 920: * handle_pipe --
1.89 espie 921: * This functions is called whenever there is something to read on the
922: * pipe. We collect more output from the given job and store it in the
1.101 espie 923: * job's outBuf. If this makes up lines, we print it tagged by the job's
1.89 espie 924: * identifier, as necessary.
1.1 deraadt 925: *
926: * Side Effects:
927: * curPos may be shifted as may the contents of outBuf.
928: *-----------------------------------------------------------------------
929: */
1.48 espie 930: static void
1.119 ! espie 931: handle_pipe(struct job_pipe *p,
1.101 espie 932: Job *job, FILE *out, bool finish)
1.66 espie 933: {
934: int nr; /* number of bytes read */
1.101 espie 935: int oldpos; /* optimization */
1.1 deraadt 936:
1.101 espie 937: /* want to get everything ? -> we block */
938: if (finish)
939: fcntl(p->fd, F_SETFL, 0);
940:
941: do {
942: nr = read(p->fd, &p->buffer[p->pos],
943: JOB_BUFSIZE - p->pos);
944: if (nr == -1) {
945: if (errno == EAGAIN)
946: break;
947: if (DEBUG(JOB)) {
948: perror("JobDoOutput(piperead)");
949: }
1.66 espie 950: }
1.101 espie 951: oldpos = p->pos;
952: p->pos += nr;
953: if (!print_complete_lines(p, job, out, oldpos))
954: if (p->pos == JOB_BUFSIZE) {
955: print_partial_buffer(p, job, out, p->pos);
956: p->pos = 0;
957: }
958: } while (nr != 0);
1.1 deraadt 959:
1.101 espie 960: /* at end of file, we print whatever is left */
961: if (nr == 0) {
962: print_partial_buffer(p, job, out, p->pos);
963: if (p->pos > 0 && p->buffer[p->pos - 1] != '\n')
964: putchar('\n');
965: p->pos = 0;
1.76 espie 966: }
1.101 espie 967: }
1.6 millert 968:
1.101 espie 969: static void
970: handle_job_output(Job *job, int i, bool finish)
971: {
972: handle_pipe(&job->in[i], job, i == 0 ? stdout : stderr, finish);
1.1 deraadt 973: }
974:
1.117 espie 975: static void
976: remove_job(LstNode ln, int status)
977: {
978: Job *job;
979:
980: job = (Job *)Lst_Datum(ln);
981: Lst_Remove(&runningJobs, ln);
982: block_signals();
983: free(Lst_Datum(job->p));
984: Lst_Remove(&job_pids, job->p);
985: unblock_signals();
986: nJobs--;
987: if (job->flags & JOB_IS_EXPENSIVE)
988: expensive_job = false;
989: process_job_status(job, status);
990: }
991:
1.1 deraadt 992: /*-
993: *-----------------------------------------------------------------------
994: * Job_CatchChildren --
1.111 espie 995: * Handle the exit of a child. Called by handle_running_jobs
1.1 deraadt 996: *
997: * Side Effects:
998: * The job descriptor is removed from the list of children.
999: *
1000: * Notes:
1001: * We do waits, blocking or not, according to the wisdom of our
1002: * caller, until there are no more children to report. For each
1.119 ! espie 1003: * job, call process_job_status to finish things off.
1.1 deraadt 1004: *-----------------------------------------------------------------------
1005: */
1006: void
1.76 espie 1007: Job_CatchChildren()
1.1 deraadt 1008: {
1.78 espie 1009: pid_t pid; /* pid of dead child */
1010: LstNode jnode; /* list element for finding job */
1011: int status; /* Exit/termination status */
1.1 deraadt 1012:
1.66 espie 1013: /*
1014: * Don't even bother if we know there's no one around.
1015: */
1.112 espie 1016: if (nJobs == 0)
1.66 espie 1017: return;
1.6 millert 1018:
1.117 espie 1019: while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
1.93 espie 1020: handle_all_signals();
1.1 deraadt 1021:
1.95 espie 1022: jnode = Lst_Find(&runningJobs, JobCmpPid, &pid);
1.1 deraadt 1023:
1.18 espie 1024: if (jnode == NULL) {
1.117 espie 1025: Error("Child (%ld) not in table?", (long)pid);
1.66 espie 1026: } else {
1.117 espie 1027: remove_job(jnode, status);
1.1 deraadt 1028: }
1029: }
1030: }
1031:
1032: void
1.112 espie 1033: handle_all_jobs_output(void)
1.1 deraadt 1034: {
1.66 espie 1035: int nfds;
1036: struct timeval timeout;
1.111 espie 1037: LstNode ln, ln2;
1.66 espie 1038: Job *job;
1.101 espie 1039: int i;
1.111 espie 1040: int status;
1.66 espie 1041:
1.112 espie 1042: /* no jobs */
1043: if (Lst_IsEmpty(&runningJobs))
1044: return;
1.91 espie 1045:
1.77 espie 1046: (void)fflush(stdout);
1.76 espie 1047:
1.112 espie 1048: memcpy(actual_mask, output_mask, mask_size);
1.76 espie 1049: timeout.tv_sec = SEL_SEC;
1050: timeout.tv_usec = SEL_USEC;
1.66 espie 1051:
1.112 espie 1052: nfds = select(largest_fd+1, actual_mask, NULL, NULL, &timeout);
1.93 espie 1053: handle_all_signals();
1.118 espie 1054: for (ln = Lst_First(&runningJobs); nfds && ln != NULL; ln = ln2) {
1.111 espie 1055: ln2 = Lst_Adv(ln);
1056: job = (Job *)Lst_Datum(ln);
1057: job->flags &= ~JOB_DIDOUTPUT;
1058: for (i = 1; i >= 0; i--) {
1.112 espie 1059: if (FD_ISSET(job->in[i].fd, actual_mask)) {
1.111 espie 1060: nfds--;
1061: handle_job_output(job, i, false);
1062: }
1063: }
1064: if (job->flags & JOB_DIDOUTPUT) {
1.117 espie 1065: if (waitpid(job->pid, &status, WNOHANG) == job->pid) {
1066: remove_job(ln, status);
1.111 espie 1067: } else {
1068: Lst_Requeue(&runningJobs, ln);
1.66 espie 1069: }
1.1 deraadt 1070: }
1071: }
1072: }
1073:
1.111 espie 1074: void
1075: handle_running_jobs()
1076: {
1.112 espie 1077: handle_all_jobs_output();
1.111 espie 1078: Job_CatchChildren();
1079: }
1080:
1081: static void
1082: loop_handle_running_jobs()
1083: {
1084: while (nJobs)
1085: handle_running_jobs();
1086: }
1.1 deraadt 1087: /*-
1088: *-----------------------------------------------------------------------
1089: * Job_Make --
1090: * Start the creation of a target. Basically a front-end for
1091: * JobStart used by the Make module.
1092: *
1093: * Side Effects:
1094: * Another job is started.
1095: *-----------------------------------------------------------------------
1096: */
1097: void
1.56 espie 1098: Job_Make(GNode *gn)
1.1 deraadt 1099: {
1.86 espie 1100: (void)JobStart(gn, 0);
1.1 deraadt 1101: }
1102:
1.117 espie 1103:
1104: static void
1105: block_signals()
1106: {
1107: sigprocmask(SIG_BLOCK, &set, &oset);
1108: }
1109:
1110: static void
1111: unblock_signals()
1112: {
1113: sigprocmask(SIG_SETMASK, &oset, NULL);
1114: }
1115:
1.1 deraadt 1116: /*-
1117: *-----------------------------------------------------------------------
1118: * Job_Init --
1119: * Initialize the process module
1120: *
1121: * Side Effects:
1122: * lists and counters are initialized
1123: *-----------------------------------------------------------------------
1124: */
1125: void
1.83 espie 1126: Job_Init(int maxproc)
1.1 deraadt 1127: {
1.95 espie 1128: Static_Lst_Init(&runningJobs);
1.101 espie 1129: Static_Lst_Init(&errorsList);
1.117 espie 1130: maxJobs = maxproc;
1131: nJobs = 0;
1.101 espie 1132: errors = 0;
1.117 espie 1133: sigemptyset(&set);
1134: sigaddset(&set, SIGINT);
1135: sigaddset(&set, SIGHUP);
1136: sigaddset(&set, SIGQUIT);
1137: sigaddset(&set, SIGTERM);
1138: sigaddset(&set, SIGTSTP);
1139: sigaddset(&set, SIGTTOU);
1140: sigaddset(&set, SIGTTIN);
1.66 espie 1141:
1.117 espie 1142: aborting = 0;
1.40 espie 1143:
1.117 espie 1144: lastNode = NULL;
1.1 deraadt 1145:
1.78 espie 1146: if ((begin_node->type & OP_DUMMY) == 0) {
1.117 espie 1147: JobStart(begin_node, JOB_IS_SPECIAL);
1.111 espie 1148: loop_handle_running_jobs();
1.1 deraadt 1149: }
1150: }
1151:
1.117 espie 1152: static bool
1153: Job_Full()
1154: {
1155: return aborting || (nJobs >= maxJobs);
1156: }
1.1 deraadt 1157: /*-
1158: *-----------------------------------------------------------------------
1159: * Job_Full --
1.117 espie 1160: * See if the job table is full. It is considered full
1.1 deraadt 1161: * if we are in the process of aborting OR if we have
1.119 ! espie 1162: * reached/exceeded our quota.
1.1 deraadt 1163: *
1164: * Results:
1.41 espie 1165: * true if the job table is full, false otherwise
1.1 deraadt 1166: *-----------------------------------------------------------------------
1167: */
1.41 espie 1168: bool
1.117 espie 1169: can_start_job(void)
1.1 deraadt 1170: {
1.117 espie 1171: if (Job_Full() || expensive_job)
1172: return false;
1.119 ! espie 1173: else
1.117 espie 1174: return true;
1.1 deraadt 1175: }
1176:
1177: /*-
1178: *-----------------------------------------------------------------------
1179: * Job_Empty --
1.119 ! espie 1180: * See if the job table is empty.
1.1 deraadt 1181: *
1182: * Results:
1.41 espie 1183: * true if it is. false if it ain't.
1.1 deraadt 1184: * -----------------------------------------------------------------------
1185: */
1.41 espie 1186: bool
1.56 espie 1187: Job_Empty(void)
1.1 deraadt 1188: {
1.117 espie 1189: if (nJobs == 0)
1190: return true;
1191: else
1.66 espie 1192: return false;
1.1 deraadt 1193: }
1194:
1195: /*-
1196: *-----------------------------------------------------------------------
1197: * JobInterrupt --
1198: * Handle the receipt of an interrupt.
1199: *
1200: * Side Effects:
1201: * All children are killed. Another job will be started if the
1202: * .INTERRUPT target was given.
1203: *-----------------------------------------------------------------------
1204: */
1205: static void
1.117 espie 1206: JobInterrupt(bool runINTERRUPT, /* true if commands for the .INTERRUPT
1.1 deraadt 1207: * target should be executed */
1.66 espie 1208: int signo) /* signal received */
1.1 deraadt 1209: {
1.66 espie 1210: LstNode ln; /* element in job table */
1.78 espie 1211: Job *job; /* job descriptor in that element */
1.66 espie 1212:
1213: aborting = ABORT_INTERRUPT;
1214:
1.95 espie 1215: for (ln = Lst_First(&runningJobs); ln != NULL; ln = Lst_Adv(ln)) {
1.66 espie 1216: job = (Job *)Lst_Datum(ln);
1217:
1218: if (!Targ_Precious(job->node)) {
1219: const char *file = job->node->path == NULL ?
1220: job->node->name : job->node->path;
1221: if (!noExecute && eunlink(file) != -1) {
1222: Error("*** %s removed", file);
1223: }
1224: }
1225: if (job->pid) {
1.84 espie 1226: debug_printf("JobInterrupt passing signal to "
1227: "child %ld.\n", (long)job->pid);
1.117 espie 1228: killpg(job->pid, signo);
1.66 espie 1229: }
1.2 deraadt 1230: }
1.1 deraadt 1231:
1.66 espie 1232: if (runINTERRUPT && !touchFlag) {
1.78 espie 1233: if ((interrupt_node->type & OP_DUMMY) == 0) {
1.66 espie 1234: ignoreErrors = false;
1235:
1.101 espie 1236: JobStart(interrupt_node, 0);
1.111 espie 1237: loop_handle_running_jobs();
1.66 espie 1238: }
1.1 deraadt 1239: }
1.66 espie 1240: exit(signo);
1.1 deraadt 1241: }
1242:
1243: /*
1244: *-----------------------------------------------------------------------
1.12 espie 1245: * Job_Finish --
1.1 deraadt 1246: * Do final processing such as the running of the commands
1.6 millert 1247: * attached to the .END target.
1.1 deraadt 1248: *
1249: * Results:
1250: * Number of errors reported.
1.40 espie 1251: *
1.1 deraadt 1252: *-----------------------------------------------------------------------
1253: */
1254: int
1.56 espie 1255: Job_Finish(void)
1.1 deraadt 1256: {
1.116 espie 1257: if ((end_node->type & OP_DUMMY) == 0) {
1.66 espie 1258: if (errors) {
1259: Error("Errors reported so .END ignored");
1260: } else {
1.117 espie 1261: JobStart(end_node, JOB_IS_SPECIAL);
1.111 espie 1262: loop_handle_running_jobs();
1.66 espie 1263: }
1.1 deraadt 1264: }
1.66 espie 1265: return errors;
1.1 deraadt 1266: }
1267:
1.41 espie 1268: #ifdef CLEANUP
1.12 espie 1269: void
1.56 espie 1270: Job_End(void)
1.12 espie 1271: {
1.41 espie 1272: }
1.13 espie 1273: #endif
1.40 espie 1274:
1.1 deraadt 1275: /*-
1276: *-----------------------------------------------------------------------
1277: * Job_Wait --
1278: * Waits for all running jobs to finish and returns. Sets 'aborting'
1279: * to ABORT_WAIT to prevent other jobs from starting.
1280: *
1281: * Side Effects:
1282: * Currently running jobs finish.
1283: *
1284: *-----------------------------------------------------------------------
1285: */
1286: void
1.56 espie 1287: Job_Wait(void)
1.1 deraadt 1288: {
1.66 espie 1289: aborting = ABORT_WAIT;
1.111 espie 1290: loop_handle_running_jobs();
1.66 espie 1291: aborting = 0;
1.1 deraadt 1292: }
1293:
1294: /*-
1295: *-----------------------------------------------------------------------
1296: * Job_AbortAll --
1297: * Abort all currently running jobs without handling output or anything.
1298: * This function is to be called only in the event of a major
1299: * error. Most definitely NOT to be called from JobInterrupt.
1300: *
1301: * Side Effects:
1302: * All children are killed, not just the firstborn
1303: *-----------------------------------------------------------------------
1304: */
1305: void
1.56 espie 1306: Job_AbortAll(void)
1.1 deraadt 1307: {
1.66 espie 1308: LstNode ln; /* element in job table */
1309: Job *job; /* the job descriptor in that element */
1310: int foo;
1.6 millert 1311:
1.66 espie 1312: aborting = ABORT_ERROR;
1.6 millert 1313:
1.66 espie 1314: if (nJobs) {
1.119 ! espie 1315: for (ln = Lst_First(&runningJobs); ln != NULL;
1.95 espie 1316: ln = Lst_Adv(ln)) {
1.66 espie 1317: job = (Job *)Lst_Datum(ln);
1318:
1319: /*
1320: * kill the child process with increasingly drastic
1321: * signals to make darn sure it's dead.
1322: */
1.117 espie 1323: killpg(job->pid, SIGINT);
1324: killpg(job->pid, SIGKILL);
1.66 espie 1325: }
1.1 deraadt 1326: }
1.6 millert 1327:
1.66 espie 1328: /*
1329: * Catch as many children as want to report in at first, then give up
1330: */
1.117 espie 1331: while (waitpid(WAIT_ANY, &foo, WNOHANG) > 0)
1.66 espie 1332: continue;
1.2 deraadt 1333: }
1.40 espie 1334: