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