2021-06-25 18:16:24 +00:00
|
|
|
/* $NetBSD: job.c,v 1.435 2021/06/16 09:47:51 rillig Exp $ */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Adam de Boor.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1988, 1989 by Adam de Boor
|
|
|
|
* Copyright (c) 1989 by Berkeley Softworks
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Adam de Boor.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
/*
|
2012-06-08 21:57:36 +00:00
|
|
|
* job.c --
|
|
|
|
* handle the creation etc. of our child processes.
|
|
|
|
*
|
|
|
|
* Interface:
|
2020-11-07 19:39:21 +00:00
|
|
|
* Job_Init Called to initialize this module. In addition,
|
2021-02-11 01:51:11 +00:00
|
|
|
* the .BEGIN target is made including all of its
|
|
|
|
* dependencies before this function returns.
|
2020-11-07 19:39:21 +00:00
|
|
|
* Hence, the makefiles must have been parsed
|
|
|
|
* before this function is called.
|
|
|
|
*
|
|
|
|
* Job_End Clean up any memory used.
|
|
|
|
*
|
|
|
|
* Job_Make Start the creation of the given target.
|
|
|
|
*
|
|
|
|
* Job_CatchChildren
|
|
|
|
* Check for and handle the termination of any
|
|
|
|
* children. This must be called reasonably
|
|
|
|
* frequently to keep the whole make going at
|
|
|
|
* a decent clip, since job table entries aren't
|
|
|
|
* removed until their process is caught this way.
|
|
|
|
*
|
|
|
|
* Job_CatchOutput
|
|
|
|
* Print any output our children have produced.
|
|
|
|
* Should also be called fairly frequently to
|
|
|
|
* keep the user informed of what's going on.
|
|
|
|
* If no output is waiting, it will block for
|
|
|
|
* a time given by the SEL_* constants, below,
|
|
|
|
* or until output is ready.
|
|
|
|
*
|
2021-02-11 01:51:11 +00:00
|
|
|
* Job_ParseShell Given a special dependency line with target '.SHELL',
|
|
|
|
* define the shell that is used for the creation
|
|
|
|
* commands in jobs mode.
|
2020-11-07 19:39:21 +00:00
|
|
|
*
|
|
|
|
* Job_Finish Perform any final processing which needs doing.
|
|
|
|
* This includes the execution of any commands
|
|
|
|
* which have been/were attached to the .END
|
|
|
|
* target. It should only be called when the
|
|
|
|
* job table is empty.
|
|
|
|
*
|
2021-02-11 01:51:11 +00:00
|
|
|
* Job_AbortAll Abort all currently running jobs. Do not handle
|
|
|
|
* output or do anything for the jobs, just kill them.
|
|
|
|
* Should only be called in an emergency.
|
2020-11-07 19:39:21 +00:00
|
|
|
*
|
|
|
|
* Job_CheckCommands
|
|
|
|
* Verify that the commands for a target are
|
|
|
|
* ok. Provide them if necessary and possible.
|
|
|
|
*
|
|
|
|
* Job_Touch Update a target without really updating it.
|
|
|
|
*
|
|
|
|
* Job_Wait Wait for all currently-running jobs to finish.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include "wait.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#if !defined(USE_SELECT) && defined(HAVE_POLL_H)
|
|
|
|
#include <poll.h>
|
|
|
|
#else
|
|
|
|
#ifndef USE_SELECT /* no poll.h */
|
|
|
|
# define USE_SELECT
|
|
|
|
#endif
|
|
|
|
#if defined(HAVE_SYS_SELECT_H)
|
|
|
|
# include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#include <signal.h>
|
|
|
|
#include <utime.h>
|
|
|
|
#if defined(HAVE_SYS_SOCKET_H)
|
|
|
|
# include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "make.h"
|
|
|
|
#include "dir.h"
|
|
|
|
#include "job.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
#include "trace.h"
|
2020-11-07 19:39:21 +00:00
|
|
|
|
|
|
|
/* "@(#)job.c 8.2 (Berkeley) 3/19/94" */
|
2021-06-25 18:16:24 +00:00
|
|
|
MAKE_RCSID("$NetBSD: job.c,v 1.435 2021/06/16 09:47:51 rillig Exp $");
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* A shell defines how the commands are run. All commands for a target are
|
2020-11-07 19:39:21 +00:00
|
|
|
* written into a single file, which is then given to the shell to execute
|
|
|
|
* the commands from it. The commands are written to the file using a few
|
|
|
|
* templates for echo control and error control.
|
|
|
|
*
|
|
|
|
* The name of the shell is the basename for the predefined shells, such as
|
|
|
|
* "sh", "csh", "bash". For custom shells, it is the full pathname, and its
|
|
|
|
* basename is used to select the type of shell; the longest match wins.
|
|
|
|
* So /usr/pkg/bin/bash has type sh, /usr/local/bin/tcsh has type csh.
|
|
|
|
*
|
|
|
|
* The echoing of command lines is controlled using hasEchoCtl, echoOff,
|
|
|
|
* echoOn, noPrint and noPrintLen. When echoOff is executed by the shell, it
|
|
|
|
* still outputs something, but this something is not interesting, therefore
|
|
|
|
* it is filtered out using noPrint and noPrintLen.
|
|
|
|
*
|
|
|
|
* The error checking for individual commands is controlled using hasErrCtl,
|
2021-01-14 01:24:34 +00:00
|
|
|
* errOn, errOff and runChkTmpl.
|
2020-11-07 19:39:21 +00:00
|
|
|
*
|
2021-02-11 01:51:11 +00:00
|
|
|
* In case a shell doesn't have error control, echoTmpl is a printf template
|
|
|
|
* for echoing the command, should echoing be on; runIgnTmpl is another
|
|
|
|
* printf template for executing the command while ignoring the return
|
2021-01-14 01:24:34 +00:00
|
|
|
* status. Finally runChkTmpl is a printf template for running the command and
|
2020-11-07 19:39:21 +00:00
|
|
|
* causing the shell to exit on error. If any of these strings are empty when
|
2021-06-25 18:16:24 +00:00
|
|
|
* hasErrCtl is false, the command will be executed anyway as is, and if it
|
2020-11-20 03:54:37 +00:00
|
|
|
* causes an error, so be it. Any templates set up to echo the command will
|
2021-02-11 01:51:11 +00:00
|
|
|
* escape any '$ ` \ "' characters in the command string to avoid unwanted
|
|
|
|
* shell code injection, the escaped command is safe to use in double quotes.
|
2020-11-07 19:39:21 +00:00
|
|
|
*
|
|
|
|
* The command-line flags "echo" and "exit" also control the behavior. The
|
|
|
|
* "echo" flag causes the shell to start echoing commands right away. The
|
|
|
|
* "exit" flag causes the shell to exit when an error is detected in one of
|
|
|
|
* the commands.
|
|
|
|
*/
|
|
|
|
typedef struct Shell {
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* The name of the shell. For Bourne and C shells, this is used only
|
|
|
|
* to find the shell description when used as the single source of a
|
|
|
|
* .SHELL target. For user-defined shells, this is the full path of
|
|
|
|
* the shell.
|
|
|
|
*/
|
|
|
|
const char *name;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
bool hasEchoCtl; /* whether both echoOff and echoOn are there */
|
2021-01-14 01:24:34 +00:00
|
|
|
const char *echoOff; /* command to turn echoing off */
|
|
|
|
const char *echoOn; /* command to turn echoing back on */
|
|
|
|
const char *noPrint; /* text to skip when printing output from the
|
2020-11-07 19:39:21 +00:00
|
|
|
* shell. This is usually the same as echoOff */
|
2021-01-14 01:24:34 +00:00
|
|
|
size_t noPrintLen; /* length of noPrint command */
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
bool hasErrCtl; /* whether error checking can be controlled
|
2021-01-14 01:24:34 +00:00
|
|
|
* for individual commands */
|
|
|
|
const char *errOn; /* command to turn on error checking */
|
|
|
|
const char *errOff; /* command to turn off error checking */
|
|
|
|
|
|
|
|
const char *echoTmpl; /* template to echo a command */
|
|
|
|
const char *runIgnTmpl; /* template to run a command
|
|
|
|
* without error checking */
|
|
|
|
const char *runChkTmpl; /* template to run a command
|
|
|
|
* with error checking */
|
|
|
|
|
|
|
|
/* string literal that results in a newline character when it appears
|
|
|
|
* outside of any 'quote' or "quote" characters */
|
|
|
|
const char *newline;
|
|
|
|
char commentChar; /* character used by shell for comment lines */
|
|
|
|
|
|
|
|
const char *echoFlag; /* shell flag to echo commands */
|
|
|
|
const char *errFlag; /* shell flag to exit on error */
|
|
|
|
} Shell;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
typedef struct CommandFlags {
|
2021-02-11 01:51:11 +00:00
|
|
|
/* Whether to echo the command before or instead of running it. */
|
2021-06-25 18:16:24 +00:00
|
|
|
bool echo;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Run the command even in -n or -N mode. */
|
2021-06-25 18:16:24 +00:00
|
|
|
bool always;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
2021-06-25 18:16:24 +00:00
|
|
|
* true if we turned error checking off before writing the command to
|
|
|
|
* the commands file and need to turn it back on
|
2021-01-14 01:24:34 +00:00
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
bool ignerr;
|
2021-01-14 01:24:34 +00:00
|
|
|
} CommandFlags;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write shell commands to a file.
|
|
|
|
*
|
|
|
|
* TODO: keep track of whether commands are echoed.
|
|
|
|
* TODO: keep track of whether error checking is active.
|
|
|
|
*/
|
|
|
|
typedef struct ShellWriter {
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
/* we've sent 'set -x' */
|
2021-06-25 18:16:24 +00:00
|
|
|
bool xtraced;
|
2021-01-14 01:24:34 +00:00
|
|
|
|
|
|
|
} ShellWriter;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2013-08-16 18:47:18 +00:00
|
|
|
/*
|
|
|
|
* FreeBSD: traditionally .MAKE is not required to
|
|
|
|
* pass jobs queue to sub-makes.
|
|
|
|
* Use .MAKE.ALWAYS_PASS_JOB_QUEUE=no to disable.
|
|
|
|
*/
|
|
|
|
#define MAKE_ALWAYS_PASS_JOB_QUEUE ".MAKE.ALWAYS_PASS_JOB_QUEUE"
|
2021-06-25 21:26:16 +00:00
|
|
|
static bool Always_pass_job_queue = true;
|
2013-09-06 02:55:51 +00:00
|
|
|
/*
|
|
|
|
* FreeBSD: aborting entire parallel make isn't always
|
|
|
|
* desired. When doing tinderbox for example, failure of
|
|
|
|
* one architecture should not stop all.
|
|
|
|
* We still want to bail on interrupt though.
|
|
|
|
*/
|
|
|
|
#define MAKE_JOB_ERROR_TOKEN "MAKE_JOB_ERROR_TOKEN"
|
2021-06-25 21:26:16 +00:00
|
|
|
static bool Job_error_token = true;
|
2013-08-16 18:47:18 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* error handling variables
|
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
static int job_errors = 0; /* number of errors reported */
|
2021-06-25 18:16:24 +00:00
|
|
|
static enum { /* Why is the make aborting? */
|
2021-01-14 01:24:34 +00:00
|
|
|
ABORT_NONE,
|
2021-06-25 18:16:24 +00:00
|
|
|
ABORT_ERROR, /* Aborted because of an error */
|
|
|
|
ABORT_INTERRUPT, /* Aborted because it was interrupted */
|
2021-01-14 01:24:34 +00:00
|
|
|
ABORT_WAIT /* Waiting for jobs to finish */
|
2021-06-25 18:16:24 +00:00
|
|
|
} aborting = ABORT_NONE;
|
2021-01-14 01:24:34 +00:00
|
|
|
#define JOB_TOKENS "+EI+" /* Token to requeue for each abort state */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* this tracks the number of tokens currently "out" to build jobs.
|
|
|
|
*/
|
|
|
|
int jobTokensRunning = 0;
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
typedef enum JobStartResult {
|
2021-01-14 01:24:34 +00:00
|
|
|
JOB_RUNNING, /* Job is running */
|
|
|
|
JOB_ERROR, /* Error in starting the job */
|
|
|
|
JOB_FINISHED /* The job is already finished */
|
2020-11-07 19:39:21 +00:00
|
|
|
} JobStartResult;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Descriptions for various shells.
|
|
|
|
*
|
|
|
|
* The build environment may set DEFSHELL_INDEX to one of
|
|
|
|
* DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to
|
2020-11-07 19:39:21 +00:00
|
|
|
* select one of the predefined shells as the default shell.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the build environment may set DEFSHELL_CUSTOM to the
|
|
|
|
* name or the full path of a sh-compatible shell, which will be used as
|
|
|
|
* the default shell.
|
|
|
|
*
|
|
|
|
* ".SHELL" lines in Makefiles can choose the default shell from the
|
2020-11-07 19:39:21 +00:00
|
|
|
* set defined here, or add additional shells.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef DEFSHELL_CUSTOM
|
|
|
|
#define DEFSHELL_INDEX_CUSTOM 0
|
|
|
|
#define DEFSHELL_INDEX_SH 1
|
|
|
|
#define DEFSHELL_INDEX_KSH 2
|
|
|
|
#define DEFSHELL_INDEX_CSH 3
|
|
|
|
#else /* !DEFSHELL_CUSTOM */
|
|
|
|
#define DEFSHELL_INDEX_SH 0
|
|
|
|
#define DEFSHELL_INDEX_KSH 1
|
|
|
|
#define DEFSHELL_INDEX_CSH 2
|
|
|
|
#endif /* !DEFSHELL_CUSTOM */
|
|
|
|
|
|
|
|
#ifndef DEFSHELL_INDEX
|
|
|
|
#define DEFSHELL_INDEX 0 /* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */
|
|
|
|
#endif /* !DEFSHELL_INDEX */
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static Shell shells[] = {
|
2012-06-08 21:57:36 +00:00
|
|
|
#ifdef DEFSHELL_CUSTOM
|
|
|
|
/*
|
|
|
|
* An sh-compatible shell with a non-standard name.
|
|
|
|
*
|
|
|
|
* Keep this in sync with the "sh" description below, but avoid
|
|
|
|
* non-portable features that might not be supplied by all
|
|
|
|
* sh-compatible shells.
|
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
{
|
|
|
|
DEFSHELL_CUSTOM, /* .name */
|
2021-06-25 18:16:24 +00:00
|
|
|
false, /* .hasEchoCtl */
|
2021-01-14 01:24:34 +00:00
|
|
|
"", /* .echoOff */
|
|
|
|
"", /* .echoOn */
|
|
|
|
"", /* .noPrint */
|
|
|
|
0, /* .noPrintLen */
|
2021-06-25 18:16:24 +00:00
|
|
|
false, /* .hasErrCtl */
|
2021-01-14 01:24:34 +00:00
|
|
|
"", /* .errOn */
|
|
|
|
"", /* .errOff */
|
|
|
|
"echo \"%s\"\n", /* .echoTmpl */
|
|
|
|
"%s\n", /* .runIgnTmpl */
|
|
|
|
"{ %s \n} || exit $?\n", /* .runChkTmpl */
|
|
|
|
"'\n'", /* .newline */
|
|
|
|
'#', /* .commentChar */
|
|
|
|
"", /* .echoFlag */
|
|
|
|
"", /* .errFlag */
|
|
|
|
},
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif /* DEFSHELL_CUSTOM */
|
|
|
|
/*
|
|
|
|
* SH description. Echo control is also possible and, under
|
|
|
|
* sun UNIX anyway, one can even control error checking.
|
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
{
|
|
|
|
"sh", /* .name */
|
2021-06-25 18:16:24 +00:00
|
|
|
false, /* .hasEchoCtl */
|
2021-01-14 01:24:34 +00:00
|
|
|
"", /* .echoOff */
|
|
|
|
"", /* .echoOn */
|
|
|
|
"", /* .noPrint */
|
|
|
|
0, /* .noPrintLen */
|
2021-06-25 18:16:24 +00:00
|
|
|
false, /* .hasErrCtl */
|
2021-01-14 01:24:34 +00:00
|
|
|
"", /* .errOn */
|
|
|
|
"", /* .errOff */
|
|
|
|
"echo \"%s\"\n", /* .echoTmpl */
|
|
|
|
"%s\n", /* .runIgnTmpl */
|
|
|
|
"{ %s \n} || exit $?\n", /* .runChkTmpl */
|
|
|
|
"'\n'", /* .newline */
|
|
|
|
'#', /* .commentChar*/
|
2012-06-08 21:57:36 +00:00
|
|
|
#if defined(MAKE_NATIVE) && defined(__NetBSD__)
|
2021-01-14 01:24:34 +00:00
|
|
|
/* XXX: -q is not really echoFlag, it's more like noEchoInSysFlag. */
|
|
|
|
"q", /* .echoFlag */
|
2012-06-08 21:57:36 +00:00
|
|
|
#else
|
2021-01-14 01:24:34 +00:00
|
|
|
"", /* .echoFlag */
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
2021-01-14 01:24:34 +00:00
|
|
|
"", /* .errFlag */
|
|
|
|
},
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
2020-07-08 18:32:15 +00:00
|
|
|
* KSH description.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
{
|
|
|
|
"ksh", /* .name */
|
2021-06-25 18:16:24 +00:00
|
|
|
true, /* .hasEchoCtl */
|
2021-01-14 01:24:34 +00:00
|
|
|
"set +v", /* .echoOff */
|
|
|
|
"set -v", /* .echoOn */
|
|
|
|
"set +v", /* .noPrint */
|
|
|
|
6, /* .noPrintLen */
|
2021-06-25 18:16:24 +00:00
|
|
|
false, /* .hasErrCtl */
|
2021-01-14 01:24:34 +00:00
|
|
|
"", /* .errOn */
|
|
|
|
"", /* .errOff */
|
|
|
|
"echo \"%s\"\n", /* .echoTmpl */
|
|
|
|
"%s\n", /* .runIgnTmpl */
|
|
|
|
"{ %s \n} || exit $?\n", /* .runChkTmpl */
|
|
|
|
"'\n'", /* .newline */
|
|
|
|
'#', /* .commentChar */
|
|
|
|
"v", /* .echoFlag */
|
|
|
|
"", /* .errFlag */
|
|
|
|
},
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* CSH description. The csh can do echo control by playing
|
|
|
|
* with the setting of the 'echo' shell variable. Sadly,
|
|
|
|
* however, it is unable to do error control nicely.
|
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
{
|
|
|
|
"csh", /* .name */
|
2021-06-25 18:16:24 +00:00
|
|
|
true, /* .hasEchoCtl */
|
2021-01-14 01:24:34 +00:00
|
|
|
"unset verbose", /* .echoOff */
|
|
|
|
"set verbose", /* .echoOn */
|
|
|
|
"unset verbose", /* .noPrint */
|
|
|
|
13, /* .noPrintLen */
|
2021-06-25 18:16:24 +00:00
|
|
|
false, /* .hasErrCtl */
|
2021-01-14 01:24:34 +00:00
|
|
|
"", /* .errOn */
|
|
|
|
"", /* .errOff */
|
|
|
|
"echo \"%s\"\n", /* .echoTmpl */
|
|
|
|
"csh -c \"%s || exit 0\"\n", /* .runIgnTmpl */
|
|
|
|
"", /* .runChkTmpl */
|
|
|
|
"'\\\n'", /* .newline */
|
|
|
|
'#', /* .commentChar */
|
|
|
|
"v", /* .echoFlag */
|
|
|
|
"e", /* .errFlag */
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
};
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* This is the shell to which we pass all commands in the Makefile.
|
|
|
|
* It is set by the Job_ParseShell function.
|
|
|
|
*/
|
|
|
|
static Shell *shell = &shells[DEFSHELL_INDEX];
|
2020-11-07 19:39:21 +00:00
|
|
|
const char *shellPath = NULL; /* full pathname of executable image */
|
|
|
|
const char *shellName = NULL; /* last component of shellPath */
|
2013-08-02 05:23:57 +00:00
|
|
|
char *shellErrFlag = NULL;
|
2021-02-11 01:51:11 +00:00
|
|
|
static char *shell_freeIt = NULL; /* Allocated memory for custom .SHELL */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
static Job *job_table; /* The structures that describe them */
|
|
|
|
static Job *job_table_end; /* job_table + maxJobs */
|
|
|
|
static unsigned int wantToken; /* we want a token */
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool lurking_children = false;
|
|
|
|
static bool make_suspended = false; /* Whether we've seen a SIGTSTP (etc) */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set of descriptors of pipes connected to
|
|
|
|
* the output channels of children
|
|
|
|
*/
|
|
|
|
static struct pollfd *fds = NULL;
|
2021-02-11 01:51:11 +00:00
|
|
|
static Job **jobByFdIndex = NULL;
|
|
|
|
static nfds_t fdsLen = 0;
|
2012-06-08 21:57:36 +00:00
|
|
|
static void watchfd(Job *);
|
|
|
|
static void clearfd(Job *);
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool readyfd(Job *);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static char *targPrefix = NULL; /* To identify a job change in the output. */
|
2012-06-08 21:57:36 +00:00
|
|
|
static Job tokenWaitJob; /* token wait pseudo-job */
|
|
|
|
|
|
|
|
static Job childExitJob; /* child exit pseudo-job */
|
2021-01-14 01:24:34 +00:00
|
|
|
#define CHILD_EXIT "."
|
|
|
|
#define DO_JOB_RESUME "R"
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
enum {
|
|
|
|
npseudojobs = 2 /* number of pseudo-jobs */
|
|
|
|
};
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
static sigset_t caught_signals; /* Set of signals we handle */
|
2021-02-11 01:51:11 +00:00
|
|
|
static volatile sig_atomic_t caught_sigchld;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
static void CollectOutput(Job *, bool);
|
|
|
|
static void JobInterrupt(bool, int) MAKE_ATTR_DEAD;
|
2012-06-08 21:57:36 +00:00
|
|
|
static void JobRestartJobs(void);
|
|
|
|
static void JobSigReset(void);
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
|
|
|
SwitchOutputTo(GNode *gn)
|
|
|
|
{
|
|
|
|
/* The node for which output was most recently produced. */
|
|
|
|
static GNode *lastNode = NULL;
|
|
|
|
|
|
|
|
if (gn == lastNode)
|
|
|
|
return;
|
|
|
|
lastNode = gn;
|
|
|
|
|
|
|
|
if (opts.maxJobs != 1 && targPrefix != NULL && targPrefix[0] != '\0')
|
|
|
|
(void)fprintf(stdout, "%s %s ---\n", targPrefix, gn->name);
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:34:48 +00:00
|
|
|
static unsigned
|
|
|
|
nfds_per_job(void)
|
|
|
|
{
|
|
|
|
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
|
2021-01-14 01:24:34 +00:00
|
|
|
if (useMeta)
|
|
|
|
return 2;
|
2020-05-20 19:34:48 +00:00
|
|
|
#endif
|
2021-01-14 01:24:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Job_FlagsToString(const Job *job, char *buf, size_t bufsize)
|
|
|
|
{
|
|
|
|
snprintf(buf, bufsize, "%c%c%c",
|
|
|
|
job->ignerr ? 'i' : '-',
|
|
|
|
!job->echo ? 's' : '-',
|
|
|
|
job->special ? 'S' : '-');
|
2020-05-20 19:34:48 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
static void
|
2021-06-25 18:16:24 +00:00
|
|
|
DumpJobs(const char *where)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
Job *job;
|
|
|
|
char flags[4];
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
debug_printf("job table @ %s\n", where);
|
|
|
|
for (job = job_table; job < job_table_end; job++) {
|
|
|
|
Job_FlagsToString(job, flags, sizeof flags);
|
|
|
|
debug_printf("job %d, status %d, flags %s, pid %d\n",
|
|
|
|
(int)(job - job_table), job->status, flags, job->pid);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 21:50:59 +00:00
|
|
|
/*
|
|
|
|
* Delete the target of a failed, interrupted, or otherwise
|
|
|
|
* unsuccessful job unless inhibited by .PRECIOUS.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
JobDeleteTarget(GNode *gn)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
const char *file;
|
|
|
|
|
|
|
|
if (gn->type & OP_JOIN)
|
|
|
|
return;
|
|
|
|
if (gn->type & OP_PHONY)
|
|
|
|
return;
|
|
|
|
if (Targ_Precious(gn))
|
|
|
|
return;
|
|
|
|
if (opts.noExecute)
|
|
|
|
return;
|
|
|
|
|
|
|
|
file = GNode_Path(gn);
|
|
|
|
if (eunlink(file) != -1)
|
|
|
|
Error("*** %s removed", file);
|
2016-12-19 21:50:59 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* JobSigLock/JobSigUnlock
|
|
|
|
*
|
|
|
|
* Signal lock routines to get exclusive access. Currently used to
|
|
|
|
* protect `jobs' and `stoppedJobs' list manipulations.
|
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
|
|
|
JobSigLock(sigset_t *omaskp)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
|
|
|
if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) {
|
|
|
|
Punt("JobSigLock: sigprocmask: %s", strerror(errno));
|
|
|
|
sigemptyset(omaskp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
|
|
|
JobSigUnlock(sigset_t *omaskp)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
|
|
|
(void)sigprocmask(SIG_SETMASK, omaskp, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
JobCreatePipe(Job *job, int minfd)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
int i, fd, flags;
|
|
|
|
int pipe_fds[2];
|
|
|
|
|
|
|
|
if (pipe(pipe_fds) == -1)
|
|
|
|
Punt("Cannot create pipe: %s", strerror(errno));
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
/* Avoid using low numbered fds */
|
|
|
|
fd = fcntl(pipe_fds[i], F_DUPFD, minfd);
|
|
|
|
if (fd != -1) {
|
|
|
|
close(pipe_fds[i]);
|
|
|
|
pipe_fds[i] = fd;
|
|
|
|
}
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
job->inPipe = pipe_fds[0];
|
|
|
|
job->outPipe = pipe_fds[1];
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Set close-on-exec flag for both */
|
|
|
|
if (fcntl(job->inPipe, F_SETFD, FD_CLOEXEC) == -1)
|
|
|
|
Punt("Cannot set close-on-exec: %s", strerror(errno));
|
|
|
|
if (fcntl(job->outPipe, F_SETFD, FD_CLOEXEC) == -1)
|
|
|
|
Punt("Cannot set close-on-exec: %s", strerror(errno));
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* We mark the input side of the pipe non-blocking; we poll(2) the
|
|
|
|
* pipe when we're waiting for a job token, but we might lose the
|
|
|
|
* race for the token when a new one becomes available, so the read
|
|
|
|
* from the pipe should not block.
|
|
|
|
*/
|
|
|
|
flags = fcntl(job->inPipe, F_GETFL, 0);
|
|
|
|
if (flags == -1)
|
|
|
|
Punt("Cannot get flags: %s", strerror(errno));
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
if (fcntl(job->inPipe, F_SETFL, flags) == -1)
|
|
|
|
Punt("Cannot set flags: %s", strerror(errno));
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Pass the signal to each running job. */
|
2012-06-08 21:57:36 +00:00
|
|
|
static void
|
|
|
|
JobCondPassSig(int signo)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
Job *job;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
DEBUG1(JOB, "JobCondPassSig(%d) called.\n", signo);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (job = job_table; job < job_table_end; job++) {
|
|
|
|
if (job->status != JOB_ST_RUNNING)
|
|
|
|
continue;
|
|
|
|
DEBUG2(JOB, "JobCondPassSig passing signal %d to child %d.\n",
|
|
|
|
signo, job->pid);
|
|
|
|
KILLPG(job->pid, signo);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* SIGCHLD handler.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2021-01-14 01:24:34 +00:00
|
|
|
* Sends a token on the child exit pipe to wake us up from select()/poll().
|
|
|
|
*/
|
|
|
|
/*ARGSUSED*/
|
2012-06-08 21:57:36 +00:00
|
|
|
static void
|
2012-06-25 22:20:51 +00:00
|
|
|
JobChildSig(int signo MAKE_ATTR_UNUSED)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-02-11 01:51:11 +00:00
|
|
|
caught_sigchld = 1;
|
2021-01-14 01:24:34 +00:00
|
|
|
while (write(childExitJob.outPipe, CHILD_EXIT, 1) == -1 &&
|
|
|
|
errno == EAGAIN)
|
|
|
|
continue;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Resume all stopped jobs. */
|
2021-01-14 01:24:34 +00:00
|
|
|
/*ARGSUSED*/
|
2012-06-08 21:57:36 +00:00
|
|
|
static void
|
2012-06-25 22:20:51 +00:00
|
|
|
JobContinueSig(int signo MAKE_ATTR_UNUSED)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Defer sending SIGCONT to our stopped children until we return
|
|
|
|
* from the signal handler.
|
|
|
|
*/
|
|
|
|
while (write(childExitJob.outPipe, DO_JOB_RESUME, 1) == -1 &&
|
|
|
|
errno == EAGAIN)
|
|
|
|
continue;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Pass a signal on to all jobs, then resend to ourselves.
|
|
|
|
* We die by the same signal.
|
|
|
|
*/
|
2012-06-25 22:20:51 +00:00
|
|
|
MAKE_ATTR_DEAD static void
|
2012-06-08 21:57:36 +00:00
|
|
|
JobPassSig_int(int signo)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Run .INTERRUPT target then exit */
|
2021-06-25 18:16:24 +00:00
|
|
|
JobInterrupt(true, signo);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Pass a signal on to all jobs, then resend to ourselves.
|
|
|
|
* We die by the same signal.
|
|
|
|
*/
|
2012-06-25 22:20:51 +00:00
|
|
|
MAKE_ATTR_DEAD static void
|
2012-06-08 21:57:36 +00:00
|
|
|
JobPassSig_term(int signo)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Dont run .INTERRUPT target then exit */
|
2021-06-25 18:16:24 +00:00
|
|
|
JobInterrupt(false, signo);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
JobPassSig_suspend(int signo)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
sigset_t nmask, omask;
|
|
|
|
struct sigaction act;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Suppress job started/continued messages */
|
2021-06-25 18:16:24 +00:00
|
|
|
make_suspended = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Pass the signal onto every job */
|
|
|
|
JobCondPassSig(signo);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Send ourselves the signal now we've given the message to everyone
|
|
|
|
* else. Note we block everything else possible while we're getting
|
|
|
|
* the signal. This ensures that all our jobs get continued when we
|
|
|
|
* wake up before we take any other signal.
|
|
|
|
*/
|
|
|
|
sigfillset(&nmask);
|
|
|
|
sigdelset(&nmask, signo);
|
|
|
|
(void)sigprocmask(SIG_SETMASK, &nmask, &omask);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
act.sa_handler = SIG_DFL;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
(void)sigaction(signo, &act, NULL);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
DEBUG1(JOB, "JobPassSig passing signal %d to self.\n", signo);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)kill(getpid(), signo);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* We've been continued.
|
|
|
|
*
|
|
|
|
* A whole host of signals continue to happen!
|
|
|
|
* SIGCHLD for any processes that actually suspended themselves.
|
|
|
|
* SIGCHLD for any processes that exited while we were alseep.
|
|
|
|
* The SIGCONT that actually caused us to wakeup.
|
|
|
|
*
|
|
|
|
* Since we defer passing the SIGCONT on to our children until
|
|
|
|
* the main processing loop, we can be sure that all the SIGCHLD
|
|
|
|
* events will have happened by then - and that the waitpid() will
|
|
|
|
* collect the child 'suspended' events.
|
|
|
|
* For correct sequencing we just need to ensure we process the
|
|
|
|
* waitpid() before passing on the SIGCONT.
|
|
|
|
*
|
|
|
|
* In any case nothing else is needed here.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Restore handler and signal mask */
|
|
|
|
act.sa_handler = JobPassSig_suspend;
|
|
|
|
(void)sigaction(signo, &act, NULL);
|
|
|
|
(void)sigprocmask(SIG_SETMASK, &omask, NULL);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Job *
|
2021-06-25 18:16:24 +00:00
|
|
|
JobFindPid(int pid, JobStatus status, bool isJobs)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
Job *job;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (job = job_table; job < job_table_end; job++) {
|
|
|
|
if (job->status == status && job->pid == pid)
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
if (DEBUG(JOB) && isJobs)
|
2021-06-25 18:16:24 +00:00
|
|
|
DumpJobs("no pid");
|
2021-01-14 01:24:34 +00:00
|
|
|
return NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Parse leading '@', '-' and '+', which control the exact execution mode. */
|
|
|
|
static void
|
2021-01-14 01:24:34 +00:00
|
|
|
ParseCommandFlags(char **pp, CommandFlags *out_cmdFlags)
|
|
|
|
{
|
|
|
|
char *p = *pp;
|
2021-06-25 18:16:24 +00:00
|
|
|
out_cmdFlags->echo = true;
|
|
|
|
out_cmdFlags->ignerr = false;
|
|
|
|
out_cmdFlags->always = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (*p == '@')
|
|
|
|
out_cmdFlags->echo = DEBUG(LOUD);
|
|
|
|
else if (*p == '-')
|
2021-06-25 18:16:24 +00:00
|
|
|
out_cmdFlags->ignerr = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
else if (*p == '+')
|
2021-06-25 18:16:24 +00:00
|
|
|
out_cmdFlags->always = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
p++;
|
|
|
|
}
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
pp_skip_whitespace(&p);
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
*pp = p;
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Escape a string for a double-quoted string literal in sh, csh and ksh. */
|
|
|
|
static char *
|
|
|
|
EscapeShellDblQuot(const char *cmd)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
/* Worst that could happen is every char needs escaping. */
|
|
|
|
char *esc = bmake_malloc(strlen(cmd) * 2 + 1);
|
|
|
|
for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) {
|
|
|
|
if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' ||
|
|
|
|
cmd[i] == '"')
|
|
|
|
esc[j++] = '\\';
|
|
|
|
esc[j] = cmd[i];
|
|
|
|
}
|
|
|
|
esc[j] = '\0';
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
return esc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-06-25 18:16:24 +00:00
|
|
|
ShellWriter_WriteFmt(ShellWriter *wr, const char *fmt, const char *arg)
|
2021-01-14 01:24:34 +00:00
|
|
|
{
|
|
|
|
DEBUG1(JOB, fmt, arg);
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)fprintf(wr->f, fmt, arg);
|
|
|
|
/* XXX: Is flushing needed in any case, or only if f == stdout? */
|
|
|
|
(void)fflush(wr->f);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
static void
|
2021-06-25 18:16:24 +00:00
|
|
|
ShellWriter_WriteLine(ShellWriter *wr, const char *line)
|
2020-11-20 03:54:37 +00:00
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
ShellWriter_WriteFmt(wr, "%s\n", line);
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
|
|
|
ShellWriter_EchoOff(ShellWriter *wr)
|
|
|
|
{
|
|
|
|
if (shell->hasEchoCtl)
|
2021-06-25 18:16:24 +00:00
|
|
|
ShellWriter_WriteLine(wr, shell->echoOff);
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-01-14 01:24:34 +00:00
|
|
|
ShellWriter_EchoCmd(ShellWriter *wr, const char *escCmd)
|
2020-11-20 03:54:37 +00:00
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
ShellWriter_WriteFmt(wr, shell->echoTmpl, escCmd);
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
static void
|
2021-01-14 01:24:34 +00:00
|
|
|
ShellWriter_EchoOn(ShellWriter *wr)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
if (shell->hasEchoCtl)
|
2021-06-25 18:16:24 +00:00
|
|
|
ShellWriter_WriteLine(wr, shell->echoOn);
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ShellWriter_TraceOn(ShellWriter *wr)
|
|
|
|
{
|
|
|
|
if (!wr->xtraced) {
|
2021-06-25 18:16:24 +00:00
|
|
|
ShellWriter_WriteLine(wr, "set -x");
|
|
|
|
wr->xtraced = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
2021-06-25 18:16:24 +00:00
|
|
|
ShellWriter_ErrOff(ShellWriter *wr, bool echo)
|
2021-01-14 01:24:34 +00:00
|
|
|
{
|
|
|
|
if (echo)
|
|
|
|
ShellWriter_EchoOff(wr);
|
2021-06-25 18:16:24 +00:00
|
|
|
ShellWriter_WriteLine(wr, shell->errOff);
|
2021-01-14 01:24:34 +00:00
|
|
|
if (echo)
|
|
|
|
ShellWriter_EchoOn(wr);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
2021-06-25 18:16:24 +00:00
|
|
|
ShellWriter_ErrOn(ShellWriter *wr, bool echo)
|
2021-01-14 01:24:34 +00:00
|
|
|
{
|
|
|
|
if (echo)
|
|
|
|
ShellWriter_EchoOff(wr);
|
2021-06-25 18:16:24 +00:00
|
|
|
ShellWriter_WriteLine(wr, shell->errOn);
|
2021-01-14 01:24:34 +00:00
|
|
|
if (echo)
|
|
|
|
ShellWriter_EchoOn(wr);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* The shell has no built-in error control, so emulate error control by
|
|
|
|
* enclosing each shell command in a template like "{ %s \n } || exit $?"
|
|
|
|
* (configurable per shell).
|
|
|
|
*/
|
|
|
|
static void
|
2021-06-25 18:16:24 +00:00
|
|
|
JobWriteSpecialsEchoCtl(Job *job, ShellWriter *wr, CommandFlags *inout_cmdFlags,
|
2021-01-14 01:24:34 +00:00
|
|
|
const char *escCmd, const char **inout_cmdTemplate)
|
|
|
|
{
|
|
|
|
/* XXX: Why is the job modified at this point? */
|
2021-06-25 18:16:24 +00:00
|
|
|
job->ignerr = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (job->echo && inout_cmdFlags->echo) {
|
|
|
|
ShellWriter_EchoOff(wr);
|
|
|
|
ShellWriter_EchoCmd(wr, escCmd);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Leave echoing off so the user doesn't see the commands
|
|
|
|
* for toggling the error checking.
|
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
inout_cmdFlags->echo = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
} else {
|
|
|
|
if (inout_cmdFlags->echo)
|
|
|
|
ShellWriter_EchoCmd(wr, escCmd);
|
|
|
|
}
|
|
|
|
*inout_cmdTemplate = shell->runIgnTmpl;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/*
|
2021-01-14 01:24:34 +00:00
|
|
|
* The template runIgnTmpl already takes care of ignoring errors,
|
|
|
|
* so pretend error checking is still on.
|
|
|
|
* XXX: What effects does this have, and why is it necessary?
|
2020-11-07 19:39:21 +00:00
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
inout_cmdFlags->ignerr = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
2021-06-25 18:16:24 +00:00
|
|
|
JobWriteSpecials(Job *job, ShellWriter *wr, const char *escCmd, bool run,
|
2021-01-14 01:24:34 +00:00
|
|
|
CommandFlags *inout_cmdFlags, const char **inout_cmdTemplate)
|
|
|
|
{
|
|
|
|
if (!run) {
|
|
|
|
/*
|
|
|
|
* If there is no command to run, there is no need to switch
|
|
|
|
* error checking off and on again for nothing.
|
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
inout_cmdFlags->ignerr = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
} else if (shell->hasErrCtl)
|
|
|
|
ShellWriter_ErrOff(wr, job->echo && inout_cmdFlags->echo);
|
|
|
|
else if (shell->runIgnTmpl != NULL && shell->runIgnTmpl[0] != '\0') {
|
2021-06-25 18:16:24 +00:00
|
|
|
JobWriteSpecialsEchoCtl(job, wr, inout_cmdFlags, escCmd,
|
2021-01-14 01:24:34 +00:00
|
|
|
inout_cmdTemplate);
|
|
|
|
} else
|
2021-06-25 18:16:24 +00:00
|
|
|
inout_cmdFlags->ignerr = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
2021-06-25 18:16:24 +00:00
|
|
|
* Write a shell command to the job's commands file, to be run later.
|
2021-01-14 01:24:34 +00:00
|
|
|
*
|
|
|
|
* If the command starts with '@' and neither the -s nor the -n flag was
|
2021-06-25 18:16:24 +00:00
|
|
|
* given to make, stick a shell-specific echoOff command in the script.
|
2021-01-14 01:24:34 +00:00
|
|
|
*
|
|
|
|
* If the command starts with '-' and the shell has no error control (none
|
2021-06-25 18:16:24 +00:00
|
|
|
* of the predefined shells has that), ignore errors for the entire job.
|
2021-01-14 01:24:34 +00:00
|
|
|
*
|
2021-06-25 18:16:24 +00:00
|
|
|
* XXX: Why ignore errors for the entire job? This is even documented in the
|
|
|
|
* manual page, but without any rationale since there is no known rationale.
|
|
|
|
*
|
|
|
|
* XXX: The manual page says the '-' "affects the entire job", but that's not
|
|
|
|
* accurate. The '-' does not affect the commands before the '-'.
|
|
|
|
*
|
|
|
|
* If the command is just "...", skip all further commands of this job. These
|
|
|
|
* commands are attached to the .END node instead and will be run by
|
|
|
|
* Job_Finish after all other targets have been made.
|
2021-01-14 01:24:34 +00:00
|
|
|
*/
|
|
|
|
static void
|
2021-06-25 18:16:24 +00:00
|
|
|
JobWriteCommand(Job *job, ShellWriter *wr, StringListNode *ln, const char *ucmd)
|
2021-01-14 01:24:34 +00:00
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
bool run;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
CommandFlags cmdFlags;
|
2021-06-25 18:16:24 +00:00
|
|
|
/* Template for writing a command to the shell file */
|
2021-01-14 01:24:34 +00:00
|
|
|
const char *cmdTemplate;
|
|
|
|
char *xcmd; /* The expanded command */
|
|
|
|
char *xcmdStart;
|
|
|
|
char *escCmd; /* xcmd escaped to be used in double quotes */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
run = GNode_ShouldExecute(job->node);
|
|
|
|
|
|
|
|
Var_Subst(ucmd, job->node, VARE_WANTRES, &xcmd);
|
|
|
|
/* TODO: handle errors */
|
|
|
|
xcmdStart = xcmd;
|
|
|
|
|
|
|
|
cmdTemplate = "%s\n";
|
|
|
|
|
|
|
|
ParseCommandFlags(&xcmd, &cmdFlags);
|
|
|
|
|
|
|
|
/* The '+' command flag overrides the -n or -N options. */
|
|
|
|
if (cmdFlags.always && !run) {
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
2021-01-14 01:24:34 +00:00
|
|
|
* We're not actually executing anything...
|
|
|
|
* but this one needs to be - use compat mode just for it.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
Compat_RunCommand(ucmd, job->node, ln);
|
|
|
|
free(xcmdStart);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the shell doesn't have error control, the alternate echoing
|
|
|
|
* will be done (to avoid showing additional error checking code)
|
|
|
|
* and this needs some characters escaped.
|
|
|
|
*/
|
|
|
|
escCmd = shell->hasErrCtl ? NULL : EscapeShellDblQuot(xcmd);
|
|
|
|
|
|
|
|
if (!cmdFlags.echo) {
|
|
|
|
if (job->echo && run && shell->hasEchoCtl) {
|
|
|
|
ShellWriter_EchoOff(wr);
|
2012-06-08 21:57:36 +00:00
|
|
|
} else {
|
2021-01-14 01:24:34 +00:00
|
|
|
if (shell->hasErrCtl)
|
2021-06-25 18:16:24 +00:00
|
|
|
cmdFlags.echo = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cmdFlags.ignerr) {
|
2021-06-25 18:16:24 +00:00
|
|
|
JobWriteSpecials(job, wr, escCmd, run, &cmdFlags, &cmdTemplate);
|
2021-01-14 01:24:34 +00:00
|
|
|
} else {
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
2021-01-14 01:24:34 +00:00
|
|
|
* If errors are being checked and the shell doesn't have
|
|
|
|
* error control but does supply an runChkTmpl template, then
|
|
|
|
* set up commands to run through it.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
|
|
|
|
if (!shell->hasErrCtl && shell->runChkTmpl != NULL &&
|
|
|
|
shell->runChkTmpl[0] != '\0') {
|
|
|
|
if (job->echo && cmdFlags.echo) {
|
|
|
|
ShellWriter_EchoOff(wr);
|
|
|
|
ShellWriter_EchoCmd(wr, escCmd);
|
2021-06-25 18:16:24 +00:00
|
|
|
cmdFlags.echo = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If it's a comment line or blank, avoid the possible
|
|
|
|
* syntax error generated by "{\n} || exit $?".
|
|
|
|
*/
|
|
|
|
cmdTemplate = escCmd[0] == shell->commentChar ||
|
|
|
|
escCmd[0] == '\0'
|
|
|
|
? shell->runIgnTmpl
|
|
|
|
: shell->runChkTmpl;
|
2021-06-25 18:16:24 +00:00
|
|
|
cmdFlags.ignerr = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0)
|
|
|
|
ShellWriter_TraceOn(wr);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
ShellWriter_WriteFmt(wr, cmdTemplate, xcmd);
|
2021-01-14 01:24:34 +00:00
|
|
|
free(xcmdStart);
|
|
|
|
free(escCmd);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (cmdFlags.ignerr)
|
|
|
|
ShellWriter_ErrOn(wr, cmdFlags.echo && job->echo);
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (!cmdFlags.echo)
|
|
|
|
ShellWriter_EchoOn(wr);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
2021-06-25 18:16:24 +00:00
|
|
|
* Write all commands to the shell file that is later executed.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2021-06-25 18:16:24 +00:00
|
|
|
* The special command "..." stops writing and saves the remaining commands
|
2021-02-11 01:51:11 +00:00
|
|
|
* to be executed later, when the target '.END' is made.
|
2021-01-14 01:24:34 +00:00
|
|
|
*
|
|
|
|
* Return whether at least one command was written to the shell file.
|
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
|
|
|
JobWriteCommands(Job *job)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
StringListNode *ln;
|
2021-06-25 18:16:24 +00:00
|
|
|
bool seen = false;
|
|
|
|
ShellWriter wr;
|
|
|
|
|
|
|
|
wr.f = job->cmdFILE;
|
|
|
|
wr.xtraced = false;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (ln = job->node->commands.first; ln != NULL; ln = ln->next) {
|
|
|
|
const char *cmd = ln->datum;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (strcmp(cmd, "...") == 0) {
|
|
|
|
job->node->type |= OP_SAVE_CMDS;
|
|
|
|
job->tailCmds = ln->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
JobWriteCommand(job, &wr, ln, ln->datum);
|
|
|
|
seen = true;
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
return seen;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
/*
|
|
|
|
* Save the delayed commands (those after '...'), to be executed later in
|
|
|
|
* the '.END' node, when everything else is done.
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
static void
|
|
|
|
JobSaveCommands(Job *job)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
StringListNode *ln;
|
|
|
|
|
|
|
|
for (ln = job->tailCmds; ln != NULL; ln = ln->next) {
|
|
|
|
const char *cmd = ln->datum;
|
|
|
|
char *expanded_cmd;
|
2021-02-11 01:51:11 +00:00
|
|
|
/*
|
|
|
|
* XXX: This Var_Subst is only intended to expand the dynamic
|
2021-01-14 01:24:34 +00:00
|
|
|
* variables such as .TARGET, .IMPSRC. It is not intended to
|
2021-02-11 01:51:11 +00:00
|
|
|
* expand the other variables as well; see deptgt-end.mk.
|
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)Var_Subst(cmd, job->node, VARE_WANTRES, &expanded_cmd);
|
|
|
|
/* TODO: handle errors */
|
|
|
|
Lst_Append(&Targ_GetEndNode()->commands, expanded_cmd);
|
|
|
|
}
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
|
|
|
|
/* Called to close both input and output pipes when a job is finished. */
|
2012-06-08 21:57:36 +00:00
|
|
|
static void
|
2020-11-20 03:54:37 +00:00
|
|
|
JobClosePipes(Job *job)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
clearfd(job);
|
|
|
|
(void)close(job->outPipe);
|
|
|
|
job->outPipe = -1;
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
CollectOutput(job, true);
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)close(job->inPipe);
|
|
|
|
job->inPipe = -1;
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
static void
|
|
|
|
DebugFailedJob(const Job *job)
|
|
|
|
{
|
|
|
|
const StringListNode *ln;
|
|
|
|
|
|
|
|
if (!DEBUG(ERROR))
|
|
|
|
return;
|
|
|
|
|
|
|
|
debug_printf("\n*** Failed target: %s\n*** Failed commands:\n",
|
|
|
|
job->node->name);
|
|
|
|
for (ln = job->node->commands.first; ln != NULL; ln = ln->next)
|
|
|
|
debug_printf("\t%s\n", (const char *)ln->datum);
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
|
|
|
JobFinishDoneExitedError(Job *job, WAIT_T *inout_status)
|
|
|
|
{
|
|
|
|
SwitchOutputTo(job->node);
|
|
|
|
#ifdef USE_META
|
|
|
|
if (useMeta) {
|
|
|
|
meta_job_error(job, job->node,
|
|
|
|
job->ignerr, WEXITSTATUS(*inout_status));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!shouldDieQuietly(job->node, -1)) {
|
2021-06-25 18:16:24 +00:00
|
|
|
DebugFailedJob(job);
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)printf("*** [%s] Error code %d%s\n",
|
|
|
|
job->node->name, WEXITSTATUS(*inout_status),
|
|
|
|
job->ignerr ? " (ignored)" : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (job->ignerr)
|
|
|
|
WAIT_STATUS(*inout_status) = 0;
|
|
|
|
else {
|
|
|
|
if (deleteOnError)
|
|
|
|
JobDeleteTarget(job->node);
|
|
|
|
PrintOnError(job->node, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
JobFinishDoneExited(Job *job, WAIT_T *inout_status)
|
|
|
|
{
|
|
|
|
DEBUG2(JOB, "Process %d [%s] exited.\n", job->pid, job->node->name);
|
|
|
|
|
|
|
|
if (WEXITSTATUS(*inout_status) != 0)
|
|
|
|
JobFinishDoneExitedError(job, inout_status);
|
|
|
|
else if (DEBUG(JOB)) {
|
|
|
|
SwitchOutputTo(job->node);
|
|
|
|
(void)printf("*** [%s] Completed successfully\n",
|
|
|
|
job->node->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
JobFinishDoneSignaled(Job *job, WAIT_T status)
|
|
|
|
{
|
|
|
|
SwitchOutputTo(job->node);
|
2021-06-25 18:16:24 +00:00
|
|
|
DebugFailedJob(job);
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)printf("*** [%s] Signal %d\n", job->node->name, WTERMSIG(status));
|
|
|
|
if (deleteOnError)
|
|
|
|
JobDeleteTarget(job->node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
JobFinishDone(Job *job, WAIT_T *inout_status)
|
|
|
|
{
|
|
|
|
if (WIFEXITED(*inout_status))
|
|
|
|
JobFinishDoneExited(job, inout_status);
|
|
|
|
else
|
|
|
|
JobFinishDoneSignaled(job, *inout_status);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)fflush(stdout);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Do final processing for the given job including updating parent nodes and
|
2020-11-20 03:54:37 +00:00
|
|
|
* starting new jobs as available/necessary.
|
|
|
|
*
|
|
|
|
* Deferred commands for the job are placed on the .END node.
|
|
|
|
*
|
2021-01-14 01:24:34 +00:00
|
|
|
* If there was a serious error (job_errors != 0; not an ignored one), no more
|
2020-11-20 03:54:37 +00:00
|
|
|
* jobs will be started.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* job job to finish
|
|
|
|
* status sub-why job went away
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
JobFinish (Job *job, WAIT_T status)
|
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
bool done, return_job_token;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
DEBUG3(JOB, "JobFinish: %d [%s], status %d\n",
|
|
|
|
job->pid, job->node->name, status);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if ((WIFEXITED(status) &&
|
|
|
|
((WEXITSTATUS(status) != 0 && !job->ignerr))) ||
|
|
|
|
WIFSIGNALED(status)) {
|
|
|
|
/* Finished because of an error. */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
JobClosePipes(job);
|
|
|
|
if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
|
|
|
|
(void)fclose(job->cmdFILE);
|
|
|
|
job->cmdFILE = NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-06-25 18:16:24 +00:00
|
|
|
done = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
|
|
|
|
} else if (WIFEXITED(status)) {
|
|
|
|
/*
|
|
|
|
* Deal with ignored errors in -B mode. We need to print a
|
|
|
|
* message telling of the ignored error as well as to run
|
|
|
|
* the next command.
|
|
|
|
*/
|
|
|
|
done = WEXITSTATUS(status) != 0;
|
|
|
|
|
|
|
|
JobClosePipes(job);
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
} else {
|
2021-01-14 01:24:34 +00:00
|
|
|
/* No need to close things down or anything. */
|
2021-06-25 18:16:24 +00:00
|
|
|
done = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
|
|
|
|
if (done)
|
|
|
|
JobFinishDone(job, &status);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
#ifdef USE_META
|
2021-01-14 01:24:34 +00:00
|
|
|
if (useMeta) {
|
|
|
|
int meta_status = meta_job_finish(job);
|
|
|
|
if (meta_status != 0 && status == 0)
|
|
|
|
status = meta_status;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
return_job_token = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
Trace_Log(JOBEND, job);
|
|
|
|
if (!job->special) {
|
|
|
|
if (WAIT_STATUS(status) != 0 ||
|
|
|
|
(aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT)
|
2021-06-25 18:16:24 +00:00
|
|
|
return_job_token = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (aborting != ABORT_ERROR && aborting != ABORT_INTERRUPT &&
|
|
|
|
(WAIT_STATUS(status) == 0)) {
|
|
|
|
/*
|
|
|
|
* As long as we aren't aborting and the job didn't return a
|
|
|
|
* non-zero status that we shouldn't ignore, we call
|
|
|
|
* Make_Update to update the parents.
|
|
|
|
*/
|
|
|
|
JobSaveCommands(job);
|
|
|
|
job->node->made = MADE;
|
|
|
|
if (!job->special)
|
2021-06-25 18:16:24 +00:00
|
|
|
return_job_token = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
Make_Update(job->node);
|
|
|
|
job->status = JOB_ST_FREE;
|
|
|
|
} else if (status != 0) {
|
|
|
|
job_errors++;
|
|
|
|
job->status = JOB_ST_FREE;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (job_errors > 0 && !opts.keepgoing && aborting != ABORT_INTERRUPT) {
|
|
|
|
/* Prevent more jobs from getting started. */
|
|
|
|
aborting = ABORT_ERROR;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (return_job_token)
|
|
|
|
Job_TokenReturn();
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (aborting == ABORT_ERROR && jobTokensRunning == 0)
|
|
|
|
Finish(job_errors);
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
TouchRegular(GNode *gn)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
const char *file = GNode_Path(gn);
|
2021-06-25 18:16:24 +00:00
|
|
|
struct utimbuf times;
|
2021-01-14 01:24:34 +00:00
|
|
|
int fd;
|
|
|
|
char c;
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
times.actime = now;
|
|
|
|
times.modtime = now;
|
2021-01-14 01:24:34 +00:00
|
|
|
if (utime(file, ×) >= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fd = open(file, O_RDWR | O_CREAT, 0666);
|
|
|
|
if (fd < 0) {
|
|
|
|
(void)fprintf(stderr, "*** couldn't touch %s: %s\n",
|
|
|
|
file, strerror(errno));
|
|
|
|
(void)fflush(stderr);
|
|
|
|
return; /* XXX: What about propagating the error? */
|
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Last resort: update the file's time stamps in the traditional way.
|
|
|
|
* XXX: This doesn't work for empty files, which are sometimes used
|
|
|
|
* as marker files. */
|
|
|
|
if (read(fd, &c, 1) == 1) {
|
|
|
|
(void)lseek(fd, 0, SEEK_SET);
|
|
|
|
while (write(fd, &c, 1) == -1 && errno == EAGAIN)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(void)close(fd); /* XXX: What about propagating the error? */
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Touch the given target. Called by JobStart when the -t flag was given.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2020-11-07 19:39:21 +00:00
|
|
|
* The modification date of the file is changed.
|
2021-01-14 01:24:34 +00:00
|
|
|
* If the file did not exist, it is created.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
2021-06-25 18:16:24 +00:00
|
|
|
Job_Touch(GNode *gn, bool echo)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
if (gn->type &
|
|
|
|
(OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC | OP_OPTIONAL |
|
|
|
|
OP_SPECIAL | OP_PHONY)) {
|
|
|
|
/*
|
|
|
|
* These are "virtual" targets and should not really be
|
|
|
|
* created.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (echo || !GNode_ShouldExecute(gn)) {
|
|
|
|
(void)fprintf(stdout, "touch %s\n", gn->name);
|
|
|
|
(void)fflush(stdout);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (!GNode_ShouldExecute(gn))
|
|
|
|
return;
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (gn->type & OP_ARCHV)
|
|
|
|
Arch_Touch(gn);
|
|
|
|
else if (gn->type & OP_LIB)
|
|
|
|
Arch_TouchLib(gn);
|
|
|
|
else
|
|
|
|
TouchRegular(gn);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Make sure the given node has all the commands it needs.
|
2020-11-07 19:39:21 +00:00
|
|
|
*
|
|
|
|
* The node will have commands from the .DEFAULT rule added to it if it
|
|
|
|
* needs them.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* gn The target whose commands need verifying
|
|
|
|
* abortProc Function to abort with message
|
|
|
|
*
|
|
|
|
* Results:
|
2021-06-25 18:16:24 +00:00
|
|
|
* true if the commands list is/was ok.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
bool
|
2012-06-08 21:57:36 +00:00
|
|
|
Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
if (GNode_IsTarget(gn))
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2021-01-14 01:24:34 +00:00
|
|
|
if (!Lst_IsEmpty(&gn->commands))
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2021-01-14 01:24:34 +00:00
|
|
|
if ((gn->type & OP_LIB) && !Lst_IsEmpty(&gn->children))
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
2021-01-14 01:24:34 +00:00
|
|
|
* No commands. Look for .DEFAULT rule from which we might infer
|
|
|
|
* commands.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
if (defaultNode != NULL && !Lst_IsEmpty(&defaultNode->commands) &&
|
|
|
|
!(gn->type & OP_SPECIAL)) {
|
|
|
|
/*
|
|
|
|
* The traditional Make only looks for a .DEFAULT if the node
|
|
|
|
* was never the target of an operator, so that's what we do
|
|
|
|
* too.
|
|
|
|
*
|
|
|
|
* The .DEFAULT node acts like a transformation rule, in that
|
|
|
|
* gn also inherits any attributes or sources attached to
|
|
|
|
* .DEFAULT itself.
|
|
|
|
*/
|
|
|
|
Make_HandleUse(defaultNode, gn);
|
2021-02-11 01:51:11 +00:00
|
|
|
Var_Set(gn, IMPSRC, GNode_VarTarget(gn));
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
Dir_UpdateMTime(gn, false);
|
2021-01-14 01:24:34 +00:00
|
|
|
if (gn->mtime != 0 || (gn->type & OP_SPECIAL))
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* The node wasn't the target of an operator. We have no .DEFAULT
|
|
|
|
* rule to go on and the target doesn't already exist. There's
|
|
|
|
* nothing more we can do for this branch. If the -k flag wasn't
|
|
|
|
* given, we stop in our tracks, otherwise we just don't update
|
|
|
|
* this node's parents so they never get examined.
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (gn->flags & FROM_DEPEND) {
|
|
|
|
if (!Job_RunTarget(".STALE", gn->fname))
|
|
|
|
fprintf(stdout,
|
|
|
|
"%s: %s, %d: ignoring stale %s for %s\n",
|
|
|
|
progname, gn->fname, gn->lineno, makeDependfile,
|
|
|
|
gn->name);
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (gn->type & OP_OPTIONAL) {
|
|
|
|
(void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
|
|
|
|
progname, gn->name, "ignored");
|
|
|
|
(void)fflush(stdout);
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (opts.keepgoing) {
|
|
|
|
(void)fprintf(stdout, "%s: don't know how to make %s (%s)\n",
|
|
|
|
progname, gn->name, "continuing");
|
|
|
|
(void)fflush(stdout);
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
abortProc("%s: don't know how to make %s. Stop", progname, gn->name);
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Execute the shell for the given job.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2021-01-14 01:24:34 +00:00
|
|
|
* See Job_CatchOutput for handling the output of the shell.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
static void
|
|
|
|
JobExec(Job *job, char **argv)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
int cpid; /* ID of new child */
|
|
|
|
sigset_t mask;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (DEBUG(JOB)) {
|
|
|
|
int i;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
debug_printf("Running %s\n", job->node->name);
|
|
|
|
debug_printf("\tCommand: ");
|
|
|
|
for (i = 0; argv[i] != NULL; i++) {
|
|
|
|
debug_printf("%s ", argv[i]);
|
|
|
|
}
|
|
|
|
debug_printf("\n");
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Some jobs produce no output and it's disconcerting to have
|
|
|
|
* no feedback of their running (since they produce no output, the
|
|
|
|
* banner with their name in it never appears). This is an attempt to
|
|
|
|
* provide that feedback, even if nothing follows it.
|
|
|
|
*/
|
|
|
|
if (job->echo)
|
|
|
|
SwitchOutputTo(job->node);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* No interruptions until this job is on the `jobs' list */
|
|
|
|
JobSigLock(&mask);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Pre-emptively mark job running, pid still zero though */
|
|
|
|
job->status = JOB_ST_RUNNING;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
Var_ReexportVars();
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
cpid = vfork();
|
2021-01-14 01:24:34 +00:00
|
|
|
if (cpid == -1)
|
|
|
|
Punt("Cannot vfork: %s", strerror(errno));
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (cpid == 0) {
|
|
|
|
/* Child */
|
|
|
|
sigset_t tmask;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
#ifdef USE_META
|
2021-01-14 01:24:34 +00:00
|
|
|
if (useMeta) {
|
|
|
|
meta_job_child(job);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Reset all signal handlers; this is necessary because we
|
|
|
|
* also need to unblock signals before we exec(2).
|
|
|
|
*/
|
|
|
|
JobSigReset();
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Now unblock signals */
|
|
|
|
sigemptyset(&tmask);
|
|
|
|
JobSigUnlock(&tmask);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Must duplicate the input stream down to the child's input
|
|
|
|
* and reset it to the beginning (again). Since the stream
|
|
|
|
* was marked close-on-exec, we must clear that bit in the
|
|
|
|
* new input.
|
|
|
|
*/
|
|
|
|
if (dup2(fileno(job->cmdFILE), 0) == -1)
|
|
|
|
execDie("dup2", "job->cmdFILE");
|
|
|
|
if (fcntl(0, F_SETFD, 0) == -1)
|
|
|
|
execDie("fcntl clear close-on-exec", "stdin");
|
|
|
|
if (lseek(0, 0, SEEK_SET) == -1)
|
|
|
|
execDie("lseek to 0", "stdin");
|
|
|
|
|
2021-01-14 06:21:37 +00:00
|
|
|
if (Always_pass_job_queue ||
|
|
|
|
(job->node->type & (OP_MAKE | OP_SUBMAKE))) {
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Pass job token pipe to submakes.
|
|
|
|
*/
|
|
|
|
if (fcntl(tokenWaitJob.inPipe, F_SETFD, 0) == -1)
|
|
|
|
execDie("clear close-on-exec",
|
|
|
|
"tokenWaitJob.inPipe");
|
|
|
|
if (fcntl(tokenWaitJob.outPipe, F_SETFD, 0) == -1)
|
|
|
|
execDie("clear close-on-exec",
|
|
|
|
"tokenWaitJob.outPipe");
|
|
|
|
}
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Set up the child's output to be routed through the pipe
|
|
|
|
* we've created for it.
|
|
|
|
*/
|
|
|
|
if (dup2(job->outPipe, 1) == -1)
|
|
|
|
execDie("dup2", "job->outPipe");
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* The output channels are marked close on exec. This bit
|
|
|
|
* was duplicated by the dup2(on some systems), so we have
|
|
|
|
* to clear it before routing the shell's error output to
|
|
|
|
* the same place as its standard output.
|
|
|
|
*/
|
|
|
|
if (fcntl(1, F_SETFD, 0) == -1)
|
|
|
|
execDie("clear close-on-exec", "stdout");
|
|
|
|
if (dup2(1, 2) == -1)
|
|
|
|
execDie("dup2", "1, 2");
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* We want to switch the child into a different process
|
|
|
|
* family so we can kill it and all its descendants in
|
|
|
|
* one fell swoop, by killing its process family, but not
|
|
|
|
* commit suicide.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
#if defined(HAVE_SETPGID)
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)setpgid(0, getpid());
|
2012-06-08 21:57:36 +00:00
|
|
|
#else
|
2021-02-11 01:51:11 +00:00
|
|
|
# if defined(HAVE_SETSID)
|
2021-01-14 01:24:34 +00:00
|
|
|
/* XXX: dsl - I'm sure this should be setpgrp()... */
|
|
|
|
(void)setsid();
|
2021-02-11 01:51:11 +00:00
|
|
|
# else
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)setpgrp(0, getpid());
|
2021-02-11 01:51:11 +00:00
|
|
|
# endif
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)execv(shellPath, argv);
|
|
|
|
execDie("exec", shellPath);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Parent, continuing after the child exec */
|
|
|
|
job->pid = cpid;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
Trace_Log(JOBSTART, job);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-05-20 19:34:48 +00:00
|
|
|
#ifdef USE_META
|
2021-01-14 01:24:34 +00:00
|
|
|
if (useMeta) {
|
|
|
|
meta_job_parent(job, cpid);
|
|
|
|
}
|
2020-05-20 19:34:48 +00:00
|
|
|
#endif
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Set the current position in the buffer to the beginning
|
|
|
|
* and mark another stream to watch in the outputs mask
|
|
|
|
*/
|
|
|
|
job->curPos = 0;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
watchfd(job);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
|
|
|
|
(void)fclose(job->cmdFILE);
|
|
|
|
job->cmdFILE = NULL;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
/* Now that the job is actually running, add it to the table. */
|
2021-01-14 01:24:34 +00:00
|
|
|
if (DEBUG(JOB)) {
|
|
|
|
debug_printf("JobExec(%s): pid %d added to jobs table\n",
|
|
|
|
job->node->name, job->pid);
|
2021-06-25 18:16:24 +00:00
|
|
|
DumpJobs("job started");
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
JobSigUnlock(&mask);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Create the argv needed to execute the shell for a given job. */
|
2012-06-08 21:57:36 +00:00
|
|
|
static void
|
|
|
|
JobMakeArgv(Job *job, char **argv)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
int argc;
|
|
|
|
static char args[10]; /* For merged arguments */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
argv[0] = UNCONST(shellName);
|
|
|
|
argc = 1;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if ((shell->errFlag != NULL && shell->errFlag[0] != '-') ||
|
|
|
|
(shell->echoFlag != NULL && shell->echoFlag[0] != '-')) {
|
|
|
|
/*
|
|
|
|
* At least one of the flags doesn't have a minus before it,
|
|
|
|
* so merge them together. Have to do this because the Bourne
|
|
|
|
* shell thinks its second argument is a file to source.
|
|
|
|
* Grrrr. Note the ten-character limitation on the combined
|
|
|
|
* arguments.
|
|
|
|
*
|
|
|
|
* TODO: Research until when the above comments were
|
|
|
|
* practically relevant.
|
|
|
|
*/
|
|
|
|
(void)snprintf(args, sizeof args, "-%s%s",
|
|
|
|
(job->ignerr ? "" :
|
|
|
|
(shell->errFlag != NULL ? shell->errFlag : "")),
|
|
|
|
(!job->echo ? "" :
|
|
|
|
(shell->echoFlag != NULL ? shell->echoFlag : "")));
|
|
|
|
|
|
|
|
if (args[1] != '\0') {
|
|
|
|
argv[argc] = args;
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!job->ignerr && shell->errFlag != NULL) {
|
|
|
|
argv[argc] = UNCONST(shell->errFlag);
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
if (job->echo && shell->echoFlag != NULL) {
|
|
|
|
argv[argc] = UNCONST(shell->echoFlag);
|
|
|
|
argc++;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
argv[argc] = NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
2021-06-25 18:16:24 +00:00
|
|
|
JobWriteShellCommands(Job *job, GNode *gn, bool *out_run)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
|
|
|
/*
|
2021-01-14 01:24:34 +00:00
|
|
|
* tfile is the name of a file into which all shell commands
|
|
|
|
* are put. It is removed before the child shell is executed,
|
|
|
|
* unless DEBUG(SCRIPT) is set.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-02-11 01:51:11 +00:00
|
|
|
char tfile[MAXPATHLEN];
|
2021-01-14 01:24:34 +00:00
|
|
|
int tfd; /* File descriptor to the temp file */
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
tfd = Job_TempFile(TMPPAT, tfile, sizeof tfile);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
job->cmdFILE = fdopen(tfd, "w+");
|
2020-11-20 03:54:37 +00:00
|
|
|
if (job->cmdFILE == NULL)
|
2021-01-14 01:24:34 +00:00
|
|
|
Punt("Could not fdopen %s", tfile);
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
(void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
#ifdef USE_META
|
|
|
|
if (useMeta) {
|
2021-01-14 01:24:34 +00:00
|
|
|
meta_job_start(job, gn);
|
|
|
|
if (gn->type & OP_SILENT) /* might have changed */
|
2021-06-25 18:16:24 +00:00
|
|
|
job->echo = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
*out_run = JobWriteCommands(job);
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-06-25 18:16:24 +00:00
|
|
|
* Start a target-creation process going for the target described by gn.
|
2021-01-14 01:24:34 +00:00
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* JOB_ERROR if there was an error in the commands, JOB_FINISHED
|
|
|
|
* if there isn't actually anything left to do for the job and
|
|
|
|
* JOB_RUNNING if the job has been started.
|
|
|
|
*
|
2021-06-25 18:16:24 +00:00
|
|
|
* Details:
|
2021-01-14 01:24:34 +00:00
|
|
|
* A new Job node is created and added to the list of running
|
|
|
|
* jobs. PMake is forked and a child shell created.
|
|
|
|
*
|
|
|
|
* NB: The return value is ignored by everyone.
|
|
|
|
*/
|
|
|
|
static JobStartResult
|
2021-06-25 18:16:24 +00:00
|
|
|
JobStart(GNode *gn, bool special)
|
2021-01-14 01:24:34 +00:00
|
|
|
{
|
|
|
|
Job *job; /* new job descriptor */
|
|
|
|
char *argv[10]; /* Argument vector to shell */
|
2021-06-25 18:16:24 +00:00
|
|
|
bool cmdsOK; /* true if the nodes commands were all right */
|
|
|
|
bool run;
|
2021-01-14 01:24:34 +00:00
|
|
|
|
|
|
|
for (job = job_table; job < job_table_end; job++) {
|
|
|
|
if (job->status == JOB_ST_FREE)
|
|
|
|
break;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
if (job >= job_table_end)
|
|
|
|
Punt("JobStart no job slots vacant");
|
|
|
|
|
|
|
|
memset(job, 0, sizeof *job);
|
|
|
|
job->node = gn;
|
|
|
|
job->tailCmds = NULL;
|
|
|
|
job->status = JOB_ST_SET_UP;
|
|
|
|
|
|
|
|
job->special = special || gn->type & OP_SPECIAL;
|
|
|
|
job->ignerr = opts.ignoreErrors || gn->type & OP_IGNORE;
|
|
|
|
job->echo = !(opts.beSilent || gn->type & OP_SILENT);
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
2021-01-14 01:24:34 +00:00
|
|
|
* Check the commands now so any attributes from .DEFAULT have a
|
|
|
|
* chance to migrate to the node.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
cmdsOK = Job_CheckCommands(gn, Error);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
job->inPollfd = NULL;
|
2021-02-11 01:51:11 +00:00
|
|
|
|
|
|
|
if (Lst_IsEmpty(&gn->commands)) {
|
|
|
|
job->cmdFILE = stdout;
|
2021-06-25 18:16:24 +00:00
|
|
|
run = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're serious here, but if the commands were bogus, we're
|
|
|
|
* also dead...
|
|
|
|
*/
|
|
|
|
if (!cmdsOK) {
|
|
|
|
PrintOnError(gn, NULL); /* provide some clue */
|
|
|
|
DieHorribly();
|
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
} else if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) ||
|
2021-01-14 01:24:34 +00:00
|
|
|
(!opts.noExecute && !opts.touchFlag)) {
|
2021-02-11 01:51:11 +00:00
|
|
|
/*
|
|
|
|
* The above condition looks very similar to
|
|
|
|
* GNode_ShouldExecute but is subtly different. It prevents
|
|
|
|
* that .MAKE targets are touched since these are usually
|
|
|
|
* virtual targets.
|
|
|
|
*/
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
/*
|
|
|
|
* We're serious here, but if the commands were bogus, we're
|
|
|
|
* also dead...
|
|
|
|
*/
|
|
|
|
if (!cmdsOK) {
|
|
|
|
PrintOnError(gn, NULL); /* provide some clue */
|
|
|
|
DieHorribly();
|
|
|
|
}
|
|
|
|
|
|
|
|
JobWriteShellCommands(job, gn, &run);
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)fflush(job->cmdFILE);
|
2021-01-14 01:24:34 +00:00
|
|
|
} else if (!GNode_ShouldExecute(gn)) {
|
|
|
|
/*
|
2021-06-25 18:16:24 +00:00
|
|
|
* Just write all the commands to stdout in one fell swoop.
|
2021-02-11 01:51:11 +00:00
|
|
|
* This still sets up job->tailCmds correctly.
|
2021-01-14 01:24:34 +00:00
|
|
|
*/
|
|
|
|
SwitchOutputTo(gn);
|
|
|
|
job->cmdFILE = stdout;
|
|
|
|
if (cmdsOK)
|
2021-06-25 18:16:24 +00:00
|
|
|
JobWriteCommands(job);
|
|
|
|
run = false;
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)fflush(job->cmdFILE);
|
2021-01-14 01:24:34 +00:00
|
|
|
} else {
|
|
|
|
Job_Touch(gn, job->echo);
|
2021-06-25 18:16:24 +00:00
|
|
|
run = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
|
|
|
|
/* If we're not supposed to execute a shell, don't. */
|
|
|
|
if (!run) {
|
|
|
|
if (!job->special)
|
|
|
|
Job_TokenReturn();
|
|
|
|
/* Unlink and close the command file if we opened one */
|
|
|
|
if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
|
|
|
|
(void)fclose(job->cmdFILE);
|
|
|
|
job->cmdFILE = NULL;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* We only want to work our way up the graph if we aren't
|
|
|
|
* here because the commands for the job were no good.
|
|
|
|
*/
|
|
|
|
if (cmdsOK && aborting == ABORT_NONE) {
|
|
|
|
JobSaveCommands(job);
|
|
|
|
job->node->made = MADE;
|
|
|
|
Make_Update(job->node);
|
|
|
|
}
|
|
|
|
job->status = JOB_ST_FREE;
|
|
|
|
return cmdsOK ? JOB_FINISHED : JOB_ERROR;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Set up the control arguments to the shell. This is based on the
|
|
|
|
* flags set earlier for this job.
|
|
|
|
*/
|
|
|
|
JobMakeArgv(job, argv);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Create the pipe by which we'll get the shell's output. */
|
|
|
|
JobCreatePipe(job, 3);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
JobExec(job, argv);
|
|
|
|
return JOB_RUNNING;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
2021-06-25 18:16:24 +00:00
|
|
|
* If the shell has an output filter (which only csh and ksh have by default),
|
|
|
|
* print the output of the child process, skipping the noPrint text of the
|
|
|
|
* shell.
|
|
|
|
*
|
|
|
|
* Return the part of the output that the calling function needs to output by
|
|
|
|
* itself.
|
2021-01-14 01:24:34 +00:00
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
static char *
|
2021-06-25 18:16:24 +00:00
|
|
|
PrintFilteredOutput(char *cp, char *endp) /* XXX: should all be const */
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
char *ecp; /* XXX: should be const */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (shell->noPrint == NULL || shell->noPrint[0] == '\0')
|
|
|
|
return cp;
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* XXX: What happens if shell->noPrint occurs on the boundary of
|
|
|
|
* the buffer? To work correctly in all cases, this should rather
|
|
|
|
* be a proper stream filter instead of doing string matching on
|
|
|
|
* selected chunks of the output.
|
|
|
|
*/
|
|
|
|
while ((ecp = strstr(cp, shell->noPrint)) != NULL) {
|
|
|
|
if (ecp != cp) {
|
|
|
|
*ecp = '\0'; /* XXX: avoid writing to the buffer */
|
|
|
|
/*
|
|
|
|
* The only way there wouldn't be a newline after
|
|
|
|
* this line is if it were the last in the buffer.
|
|
|
|
* however, since the noPrint output comes after it,
|
|
|
|
* there must be a newline, so we don't print one.
|
|
|
|
*/
|
|
|
|
/* XXX: What about null bytes in the output? */
|
|
|
|
(void)fprintf(stdout, "%s", cp);
|
|
|
|
(void)fflush(stdout);
|
|
|
|
}
|
|
|
|
cp = ecp + shell->noPrintLen;
|
|
|
|
if (cp == endp)
|
|
|
|
break;
|
|
|
|
cp++; /* skip over the (XXX: assumed) newline */
|
|
|
|
pp_skip_whitespace(&cp);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
return cp;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
/*
|
|
|
|
* This function is called whenever there is something to read on the pipe.
|
|
|
|
* We collect more output from the given job and store it in the job's
|
|
|
|
* outBuf. If this makes up a line, we print it tagged by the job's
|
|
|
|
* identifier, as necessary.
|
|
|
|
*
|
|
|
|
* In the output of the shell, the 'noPrint' lines are removed. If the
|
|
|
|
* command is not alone on the line (the character after it is not \0 or
|
|
|
|
* \n), we do print whatever follows it.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* job the job whose output needs printing
|
2021-06-25 18:16:24 +00:00
|
|
|
* finish true if this is the last time we'll be called
|
2012-06-08 21:57:36 +00:00
|
|
|
* for this job
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
static void
|
2021-06-25 18:16:24 +00:00
|
|
|
CollectOutput(Job *job, bool finish)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
bool gotNL; /* true if got a newline */
|
|
|
|
bool fbuf; /* true if our buffer filled up */
|
2021-01-14 01:24:34 +00:00
|
|
|
size_t nr; /* number of bytes read */
|
|
|
|
size_t i; /* auxiliary index into outBuf */
|
|
|
|
size_t max; /* limit for i (end of current data) */
|
|
|
|
ssize_t nRead; /* (Temporary) number of bytes read */
|
|
|
|
|
|
|
|
/* Read as many bytes as will fit in the buffer. */
|
2020-11-20 03:54:37 +00:00
|
|
|
again:
|
2021-06-25 18:16:24 +00:00
|
|
|
gotNL = false;
|
|
|
|
fbuf = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
|
|
|
|
nRead = read(job->inPipe, &job->outBuf[job->curPos],
|
|
|
|
JOB_BUFSIZE - job->curPos);
|
|
|
|
if (nRead < 0) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return;
|
|
|
|
if (DEBUG(JOB)) {
|
2021-06-25 18:16:24 +00:00
|
|
|
perror("CollectOutput(piperead)");
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
nr = 0;
|
|
|
|
} else {
|
|
|
|
nr = (size_t)nRead;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* If we hit the end-of-file (the job is dead), we must flush its
|
|
|
|
* remaining output, so pretend we read a newline if there's any
|
|
|
|
* output remaining in the buffer.
|
|
|
|
* Also clear the 'finish' flag so we stop looping.
|
|
|
|
*/
|
|
|
|
if (nr == 0 && job->curPos != 0) {
|
|
|
|
job->outBuf[job->curPos] = '\n';
|
|
|
|
nr = 1;
|
2021-06-25 18:16:24 +00:00
|
|
|
finish = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
} else if (nr == 0) {
|
2021-06-25 18:16:24 +00:00
|
|
|
finish = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-01-14 01:24:34 +00:00
|
|
|
* Look for the last newline in the bytes we just got. If there is
|
|
|
|
* one, break out of the loop with 'i' as its index and gotNL set
|
2021-06-25 18:16:24 +00:00
|
|
|
* true.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
max = job->curPos + nr;
|
|
|
|
for (i = job->curPos + nr - 1;
|
|
|
|
i >= job->curPos && i != (size_t)-1; i--) {
|
|
|
|
if (job->outBuf[i] == '\n') {
|
2021-06-25 18:16:24 +00:00
|
|
|
gotNL = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
break;
|
|
|
|
} else if (job->outBuf[i] == '\0') {
|
|
|
|
/*
|
2021-06-25 18:16:24 +00:00
|
|
|
* FIXME: The null characters are only replaced with
|
|
|
|
* space _after_ the last '\n'. Everywhere else they
|
|
|
|
* hide the rest of the command output.
|
2021-01-14 01:24:34 +00:00
|
|
|
*/
|
|
|
|
job->outBuf[i] = ' ';
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gotNL) {
|
|
|
|
job->curPos += nr;
|
|
|
|
if (job->curPos == JOB_BUFSIZE) {
|
|
|
|
/*
|
|
|
|
* If we've run out of buffer space, we have no choice
|
|
|
|
* but to print the stuff. sigh.
|
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
fbuf = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
i = job->curPos;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
if (gotNL || fbuf) {
|
|
|
|
/*
|
|
|
|
* Need to send the output to the screen. Null terminate it
|
|
|
|
* first, overwriting the newline character if there was one.
|
|
|
|
* So long as the line isn't one we should filter (according
|
|
|
|
* to the shell description), we print the line, preceded
|
|
|
|
* by a target banner if this target isn't the same as the
|
|
|
|
* one for which we last printed something.
|
|
|
|
* The rest of the data in the buffer are then shifted down
|
|
|
|
* to the start of the buffer and curPos is set accordingly.
|
|
|
|
*/
|
|
|
|
job->outBuf[i] = '\0';
|
|
|
|
if (i >= job->curPos) {
|
|
|
|
char *cp;
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
/*
|
|
|
|
* FIXME: SwitchOutputTo should be here, according to
|
|
|
|
* the comment above. But since PrintOutput does not
|
|
|
|
* do anything in the default shell, this bug has gone
|
|
|
|
* unnoticed until now.
|
|
|
|
*/
|
|
|
|
cp = PrintFilteredOutput(job->outBuf, &job->outBuf[i]);
|
2021-01-14 01:24:34 +00:00
|
|
|
|
|
|
|
/*
|
2021-06-25 18:16:24 +00:00
|
|
|
* There's still more in the output buffer. This time,
|
2021-01-14 01:24:34 +00:00
|
|
|
* though, we know there's no newline at the end, so
|
|
|
|
* we add one of our own free will.
|
|
|
|
*/
|
|
|
|
if (*cp != '\0') {
|
|
|
|
if (!opts.beSilent)
|
|
|
|
SwitchOutputTo(job->node);
|
|
|
|
#ifdef USE_META
|
|
|
|
if (useMeta) {
|
|
|
|
meta_job_output(job, cp,
|
|
|
|
gotNL ? "\n" : "");
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)fprintf(stdout, "%s%s", cp,
|
|
|
|
gotNL ? "\n" : "");
|
|
|
|
(void)fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* max is the last offset still in the buffer. Move any
|
|
|
|
* remaining characters to the start of the buffer and
|
|
|
|
* update the end marker curPos.
|
|
|
|
*/
|
|
|
|
if (i < max) {
|
|
|
|
(void)memmove(job->outBuf, &job->outBuf[i + 1],
|
|
|
|
max - (i + 1));
|
|
|
|
job->curPos = max - (i + 1);
|
|
|
|
} else {
|
|
|
|
assert(i == max);
|
|
|
|
job->curPos = 0;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
if (finish) {
|
|
|
|
/*
|
|
|
|
* If the finish flag is true, we must loop until we hit
|
|
|
|
* end-of-file on the pipe. This is guaranteed to happen
|
|
|
|
* eventually since the other end of the pipe is now closed
|
|
|
|
* (we closed it explicitly and the child has exited). When
|
2021-06-25 18:16:24 +00:00
|
|
|
* we do get an EOF, finish will be set false and we'll fall
|
2021-01-14 01:24:34 +00:00
|
|
|
* through and out.
|
|
|
|
*/
|
|
|
|
goto again;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
JobRun(GNode *targ)
|
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
#if 0
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Unfortunately it is too complicated to run .BEGIN, .END, and
|
|
|
|
* .INTERRUPT job in the parallel job module. As of 2020-09-25,
|
|
|
|
* unit-tests/deptgt-end-jobs.mk hangs in an endless loop.
|
|
|
|
*
|
|
|
|
* Running these jobs in compat mode also guarantees that these
|
|
|
|
* jobs do not overlap with other unrelated jobs.
|
|
|
|
*/
|
2021-02-11 01:51:11 +00:00
|
|
|
GNodeList lst = LST_INIT;
|
|
|
|
Lst_Append(&lst, targ);
|
|
|
|
(void)Make_Run(&lst);
|
|
|
|
Lst_Done(&lst);
|
2021-06-25 18:16:24 +00:00
|
|
|
JobStart(targ, true);
|
2021-01-14 01:24:34 +00:00
|
|
|
while (jobTokensRunning != 0) {
|
|
|
|
Job_CatchOutput();
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
#else
|
2021-01-14 01:24:34 +00:00
|
|
|
Compat_Make(targ, targ);
|
|
|
|
/* XXX: Replace with GNode_IsError(gn) */
|
|
|
|
if (targ->made == ERROR) {
|
|
|
|
PrintOnError(targ, "\n\nStop.");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Handle the exit of a child. Called from Make_Make.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2020-11-07 19:39:21 +00:00
|
|
|
* The job descriptor is removed from the list of children.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
* We do waits, blocking or not, according to the wisdom of our
|
|
|
|
* caller, until there are no more children to report. For each
|
|
|
|
* job, call JobFinish to finish things off.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Job_CatchChildren(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
int pid; /* pid of dead child */
|
|
|
|
WAIT_T status; /* Exit/termination status */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Don't even bother if we know there's no one around. */
|
|
|
|
if (jobTokensRunning == 0)
|
|
|
|
return;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
/* Have we received SIGCHLD since last call? */
|
|
|
|
if (caught_sigchld == 0)
|
|
|
|
return;
|
|
|
|
caught_sigchld = 0;
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
while ((pid = waitpid((pid_t)-1, &status, WNOHANG | WUNTRACED)) > 0) {
|
|
|
|
DEBUG2(JOB, "Process %d exited/stopped status %x.\n",
|
|
|
|
pid, WAIT_STATUS(status));
|
2021-06-25 18:16:24 +00:00
|
|
|
JobReapChild(pid, status, true);
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is possible that wait[pid]() was called from elsewhere,
|
|
|
|
* this lets us reap jobs regardless.
|
|
|
|
*/
|
|
|
|
void
|
2021-06-25 18:16:24 +00:00
|
|
|
JobReapChild(pid_t pid, WAIT_T status, bool isJobs)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
Job *job; /* job descriptor for dead child */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Don't even bother if we know there's no one around. */
|
|
|
|
if (jobTokensRunning == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
job = JobFindPid(pid, JOB_ST_RUNNING, isJobs);
|
|
|
|
if (job == NULL) {
|
|
|
|
if (isJobs) {
|
|
|
|
if (!lurking_children)
|
|
|
|
Error("Child (%d) status %x not in table?",
|
|
|
|
pid, status);
|
|
|
|
}
|
|
|
|
return; /* not ours */
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
if (WIFSTOPPED(status)) {
|
|
|
|
DEBUG2(JOB, "Process %d (%s) stopped.\n",
|
|
|
|
job->pid, job->node->name);
|
|
|
|
if (!make_suspended) {
|
|
|
|
switch (WSTOPSIG(status)) {
|
|
|
|
case SIGTSTP:
|
|
|
|
(void)printf("*** [%s] Suspended\n",
|
|
|
|
job->node->name);
|
|
|
|
break;
|
|
|
|
case SIGSTOP:
|
|
|
|
(void)printf("*** [%s] Stopped\n",
|
|
|
|
job->node->name);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
(void)printf("*** [%s] Stopped -- signal %d\n",
|
|
|
|
job->node->name, WSTOPSIG(status));
|
|
|
|
}
|
2021-06-25 18:16:24 +00:00
|
|
|
job->suspended = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
(void)fflush(stdout);
|
|
|
|
return;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
job->status = JOB_ST_FINISHED;
|
|
|
|
job->exit_status = WAIT_STATUS(status);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
JobFinish(job, status);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Catch the output from our children, if we're using pipes do so. Otherwise
|
2020-11-07 19:39:21 +00:00
|
|
|
* just block time until we get a signal(most likely a SIGCHLD) since there's
|
|
|
|
* no point in just spinning when there's nothing to do and the reaping of a
|
2021-01-14 01:24:34 +00:00
|
|
|
* child can wait for a while.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
Job_CatchOutput(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
int nready;
|
|
|
|
Job *job;
|
|
|
|
unsigned int i;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)fflush(stdout);
|
2013-04-01 21:12:55 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* The first fd in the list is the job token pipe */
|
|
|
|
do {
|
2021-02-11 01:51:11 +00:00
|
|
|
nready = poll(fds + 1 - wantToken, fdsLen - 1 + wantToken,
|
2021-01-14 01:24:34 +00:00
|
|
|
POLL_MSEC);
|
|
|
|
} while (nready < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
if (nready < 0)
|
|
|
|
Punt("poll: %s", strerror(errno));
|
|
|
|
|
|
|
|
if (nready > 0 && readyfd(&childExitJob)) {
|
|
|
|
char token = 0;
|
|
|
|
ssize_t count;
|
|
|
|
count = read(childExitJob.inPipe, &token, 1);
|
2021-02-11 01:51:11 +00:00
|
|
|
if (count == 1) {
|
2021-01-14 01:24:34 +00:00
|
|
|
if (token == DO_JOB_RESUME[0])
|
|
|
|
/*
|
|
|
|
* Complete relay requested from our SIGCONT
|
|
|
|
* handler
|
|
|
|
*/
|
|
|
|
JobRestartJobs();
|
2021-02-11 01:51:11 +00:00
|
|
|
} else if (count == 0)
|
|
|
|
Punt("unexpected eof on token pipe");
|
|
|
|
else
|
|
|
|
Punt("token pipe read: %s", strerror(errno));
|
2021-01-14 01:24:34 +00:00
|
|
|
nready--;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
Job_CatchChildren();
|
|
|
|
if (nready == 0)
|
|
|
|
return;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
for (i = npseudojobs * nfds_per_job(); i < fdsLen; i++) {
|
2021-01-14 01:24:34 +00:00
|
|
|
if (fds[i].revents == 0)
|
|
|
|
continue;
|
2021-02-11 01:51:11 +00:00
|
|
|
job = jobByFdIndex[i];
|
2021-01-14 01:24:34 +00:00
|
|
|
if (job->status == JOB_ST_RUNNING)
|
2021-06-25 18:16:24 +00:00
|
|
|
CollectOutput(job, false);
|
2020-05-20 19:34:48 +00:00
|
|
|
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* With meta mode, we may have activity on the job's filemon
|
|
|
|
* descriptor too, which at the moment is any pollfd other
|
|
|
|
* than job->inPollfd.
|
|
|
|
*/
|
|
|
|
if (useMeta && job->inPollfd != &fds[i]) {
|
|
|
|
if (meta_job_event(job) <= 0) {
|
|
|
|
fds[i].events = 0; /* never mind */
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 19:34:48 +00:00
|
|
|
#endif
|
2021-01-14 01:24:34 +00:00
|
|
|
if (--nready == 0)
|
|
|
|
return;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Start the creation of a target. Basically a front-end for JobStart used by
|
|
|
|
* the Make module.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
Job_Make(GNode *gn)
|
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
(void)JobStart(gn, false);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
|
|
|
InitShellNameAndPath(void)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
shellName = shell->name;
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
#ifdef DEFSHELL_CUSTOM
|
2021-01-14 01:24:34 +00:00
|
|
|
if (shellName[0] == '/') {
|
|
|
|
shellPath = shellName;
|
|
|
|
shellName = str_basename(shellPath);
|
|
|
|
return;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
2021-01-14 01:24:34 +00:00
|
|
|
|
2020-09-05 16:11:04 +00:00
|
|
|
shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
void
|
|
|
|
Shell_Init(void)
|
|
|
|
{
|
|
|
|
if (shellPath == NULL)
|
|
|
|
InitShellNameAndPath();
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
Var_SetWithFlags(SCOPE_CMDLINE, ".SHELL", shellPath, VAR_SET_READONLY);
|
2021-01-14 01:24:34 +00:00
|
|
|
if (shell->errFlag == NULL)
|
|
|
|
shell->errFlag = "";
|
|
|
|
if (shell->echoFlag == NULL)
|
|
|
|
shell->echoFlag = "";
|
|
|
|
if (shell->hasErrCtl && shell->errFlag[0] != '\0') {
|
|
|
|
if (shellErrFlag != NULL &&
|
|
|
|
strcmp(shell->errFlag, &shellErrFlag[1]) != 0) {
|
|
|
|
free(shellErrFlag);
|
|
|
|
shellErrFlag = NULL;
|
|
|
|
}
|
|
|
|
if (shellErrFlag == NULL) {
|
|
|
|
size_t n = strlen(shell->errFlag) + 2;
|
|
|
|
|
|
|
|
shellErrFlag = bmake_malloc(n);
|
|
|
|
if (shellErrFlag != NULL)
|
|
|
|
snprintf(shellErrFlag, n, "-%s",
|
|
|
|
shell->errFlag);
|
|
|
|
}
|
|
|
|
} else if (shellErrFlag != NULL) {
|
|
|
|
free(shellErrFlag);
|
|
|
|
shellErrFlag = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the string literal that is used in the current command shell
|
|
|
|
* to produce a newline character.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
const char *
|
|
|
|
Shell_GetNewline(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
return shell->newline;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Job_SetPrefix(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
if (targPrefix != NULL) {
|
|
|
|
free(targPrefix);
|
2021-02-11 01:51:11 +00:00
|
|
|
} else if (!Var_Exists(SCOPE_GLOBAL, MAKE_JOB_PREFIX)) {
|
|
|
|
Global_Set(MAKE_JOB_PREFIX, "---");
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(void)Var_Subst("${" MAKE_JOB_PREFIX "}",
|
2021-02-11 01:51:11 +00:00
|
|
|
SCOPE_GLOBAL, VARE_WANTRES, &targPrefix);
|
2021-01-14 01:24:34 +00:00
|
|
|
/* TODO: handle errors */
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
|
|
|
AddSig(int sig, SignalProc handler)
|
|
|
|
{
|
|
|
|
if (bmake_signal(sig, SIG_IGN) != SIG_IGN) {
|
|
|
|
sigaddset(&caught_signals, sig);
|
|
|
|
(void)bmake_signal(sig, handler);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 16:11:04 +00:00
|
|
|
/* Initialize the process module. */
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
Job_Init(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
Job_SetPrefix();
|
|
|
|
/* Allocate space for all the job info */
|
|
|
|
job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table);
|
|
|
|
memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table);
|
|
|
|
job_table_end = job_table + opts.maxJobs;
|
|
|
|
wantToken = 0;
|
2021-02-11 01:51:11 +00:00
|
|
|
caught_sigchld = 0;
|
2013-08-16 18:47:18 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
aborting = ABORT_NONE;
|
|
|
|
job_errors = 0;
|
2013-09-06 02:55:51 +00:00
|
|
|
|
2021-06-25 21:26:16 +00:00
|
|
|
Always_pass_job_queue = GetBooleanExpr(MAKE_ALWAYS_PASS_JOB_QUEUE,
|
2021-01-14 06:21:37 +00:00
|
|
|
Always_pass_job_queue);
|
2013-09-06 02:55:51 +00:00
|
|
|
|
2021-06-25 21:26:16 +00:00
|
|
|
Job_error_token = GetBooleanExpr(MAKE_JOB_ERROR_TOKEN, Job_error_token);
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* There is a non-zero chance that we already have children.
|
|
|
|
* eg after 'make -f- <<EOF'
|
|
|
|
* Since their termination causes a 'Child (pid) not in table'
|
|
|
|
* message, Collect the status of any that are already dead, and
|
|
|
|
* suppress the error message if there are any undead ones.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
int rval;
|
|
|
|
WAIT_T status;
|
|
|
|
|
|
|
|
rval = waitpid((pid_t)-1, &status, WNOHANG);
|
|
|
|
if (rval > 0)
|
|
|
|
continue;
|
|
|
|
if (rval == 0)
|
2021-06-25 18:16:24 +00:00
|
|
|
lurking_children = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
Shell_Init();
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
JobCreatePipe(&childExitJob, 3);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
{
|
|
|
|
/* Preallocate enough for the maximum number of jobs. */
|
|
|
|
size_t nfds = (npseudojobs + (size_t)opts.maxJobs) *
|
|
|
|
nfds_per_job();
|
|
|
|
fds = bmake_malloc(sizeof *fds * nfds);
|
|
|
|
jobByFdIndex = bmake_malloc(sizeof *jobByFdIndex * nfds);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* These are permanent entries and take slots 0 and 1 */
|
|
|
|
watchfd(&tokenWaitJob);
|
|
|
|
watchfd(&childExitJob);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
sigemptyset(&caught_signals);
|
|
|
|
/*
|
|
|
|
* Install a SIGCHLD handler.
|
|
|
|
*/
|
|
|
|
(void)bmake_signal(SIGCHLD, JobChildSig);
|
|
|
|
sigaddset(&caught_signals, SIGCHLD);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Catch the four signals that POSIX specifies if they aren't ignored.
|
|
|
|
* JobPassSig will take care of calling JobInterrupt if appropriate.
|
|
|
|
*/
|
|
|
|
AddSig(SIGINT, JobPassSig_int);
|
|
|
|
AddSig(SIGHUP, JobPassSig_term);
|
|
|
|
AddSig(SIGTERM, JobPassSig_term);
|
|
|
|
AddSig(SIGQUIT, JobPassSig_term);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* There are additional signals that need to be caught and passed if
|
|
|
|
* either the export system wants to be told directly of signals or if
|
|
|
|
* we're giving each job its own process group (since then it won't get
|
|
|
|
* signals from the terminal driver as we own the terminal)
|
|
|
|
*/
|
|
|
|
AddSig(SIGTSTP, JobPassSig_suspend);
|
|
|
|
AddSig(SIGTTOU, JobPassSig_suspend);
|
|
|
|
AddSig(SIGTTIN, JobPassSig_suspend);
|
|
|
|
AddSig(SIGWINCH, JobCondPassSig);
|
|
|
|
AddSig(SIGCONT, JobContinueSig);
|
|
|
|
|
|
|
|
(void)Job_RunTarget(".BEGIN", NULL);
|
|
|
|
/* Create the .END node now, even though no code in the unit tests
|
|
|
|
* depends on it. See also Targ_GetEndNode in Compat_Run. */
|
|
|
|
(void)Targ_GetEndNode();
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
|
|
|
DelSig(int sig)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
if (sigismember(&caught_signals, sig) != 0)
|
|
|
|
(void)bmake_signal(sig, SIG_DFL);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
static void
|
|
|
|
JobSigReset(void)
|
|
|
|
{
|
|
|
|
DelSig(SIGINT);
|
|
|
|
DelSig(SIGHUP);
|
|
|
|
DelSig(SIGQUIT);
|
|
|
|
DelSig(SIGTERM);
|
|
|
|
DelSig(SIGTSTP);
|
|
|
|
DelSig(SIGTTOU);
|
|
|
|
DelSig(SIGTTIN);
|
|
|
|
DelSig(SIGWINCH);
|
|
|
|
DelSig(SIGCONT);
|
|
|
|
(void)bmake_signal(SIGCHLD, SIG_DFL);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 16:11:04 +00:00
|
|
|
/* Find a shell in 'shells' given its name, or return NULL. */
|
2012-06-08 21:57:36 +00:00
|
|
|
static Shell *
|
2020-11-07 19:39:21 +00:00
|
|
|
FindShellByName(const char *name)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
Shell *sh = shells;
|
|
|
|
const Shell *shellsEnd = sh + sizeof shells / sizeof shells[0];
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (sh = shells; sh < shellsEnd; sh++) {
|
|
|
|
if (strcmp(name, sh->name) == 0)
|
|
|
|
return sh;
|
|
|
|
}
|
|
|
|
return NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Parse a shell specification and set up 'shell', shellPath and
|
|
|
|
* shellName appropriately.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* line The shell spec
|
|
|
|
*
|
|
|
|
* Results:
|
2021-06-25 18:16:24 +00:00
|
|
|
* false if the specification was incorrect.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Side Effects:
|
2021-01-14 01:24:34 +00:00
|
|
|
* 'shell' points to a Shell structure (either predefined or
|
2012-06-08 21:57:36 +00:00
|
|
|
* created from the shell spec), shellPath is the full path of the
|
2021-01-14 01:24:34 +00:00
|
|
|
* shell described by 'shell', while shellName is just the
|
2012-06-08 21:57:36 +00:00
|
|
|
* final component of shellPath.
|
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
* A shell specification consists of a .SHELL target, with dependency
|
|
|
|
* operator, followed by a series of blank-separated words. Double
|
|
|
|
* quotes can be used to use blanks in words. A backslash escapes
|
|
|
|
* anything (most notably a double-quote and a space) and
|
|
|
|
* provides the functionality it does in C. Each word consists of
|
|
|
|
* keyword and value separated by an equal sign. There should be no
|
|
|
|
* unnecessary spaces in the word. The keywords are as follows:
|
2020-11-07 19:39:21 +00:00
|
|
|
* name Name of shell.
|
|
|
|
* path Location of shell.
|
|
|
|
* quiet Command to turn off echoing.
|
|
|
|
* echo Command to turn echoing on
|
|
|
|
* filter Result of turning off echoing that shouldn't be
|
|
|
|
* printed.
|
|
|
|
* echoFlag Flag to turn echoing on at the start
|
|
|
|
* errFlag Flag to turn error checking on at the start
|
|
|
|
* hasErrCtl True if shell has error checking control
|
|
|
|
* newline String literal to represent a newline char
|
|
|
|
* check Command to turn on error checking if hasErrCtl
|
2021-06-25 18:16:24 +00:00
|
|
|
* is true or template of command to echo a command
|
2020-11-07 19:39:21 +00:00
|
|
|
* for which error checking is off if hasErrCtl is
|
2021-06-25 18:16:24 +00:00
|
|
|
* false.
|
2020-11-07 19:39:21 +00:00
|
|
|
* ignore Command to turn off error checking if hasErrCtl
|
2021-06-25 18:16:24 +00:00
|
|
|
* is true or template of command to execute a
|
2020-11-07 19:39:21 +00:00
|
|
|
* command so as to ignore any errors it returns if
|
2021-06-25 18:16:24 +00:00
|
|
|
* hasErrCtl is false.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
bool
|
2012-06-08 21:57:36 +00:00
|
|
|
Job_ParseShell(char *line)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
Words wordsList;
|
|
|
|
char **words;
|
|
|
|
char **argv;
|
|
|
|
size_t argc;
|
|
|
|
char *path;
|
|
|
|
Shell newShell;
|
2021-06-25 18:16:24 +00:00
|
|
|
bool fullSpec = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
Shell *sh;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* XXX: don't use line as an iterator variable */
|
|
|
|
pp_skip_whitespace(&line);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
free(shell_freeIt);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
memset(&newShell, 0, sizeof newShell);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
2021-01-14 01:24:34 +00:00
|
|
|
* Parse the specification by keyword
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
wordsList = Str_Words(line, true);
|
2021-01-14 01:24:34 +00:00
|
|
|
words = wordsList.words;
|
|
|
|
argc = wordsList.len;
|
|
|
|
path = wordsList.freeIt;
|
|
|
|
if (words == NULL) {
|
|
|
|
Error("Unterminated quoted string [%s]", line);
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
shell_freeIt = path;
|
2021-01-14 01:24:34 +00:00
|
|
|
|
|
|
|
for (path = NULL, argv = words; argc != 0; argc--, argv++) {
|
|
|
|
char *arg = *argv;
|
|
|
|
if (strncmp(arg, "path=", 5) == 0) {
|
|
|
|
path = arg + 5;
|
|
|
|
} else if (strncmp(arg, "name=", 5) == 0) {
|
|
|
|
newShell.name = arg + 5;
|
|
|
|
} else {
|
|
|
|
if (strncmp(arg, "quiet=", 6) == 0) {
|
|
|
|
newShell.echoOff = arg + 6;
|
|
|
|
} else if (strncmp(arg, "echo=", 5) == 0) {
|
|
|
|
newShell.echoOn = arg + 5;
|
|
|
|
} else if (strncmp(arg, "filter=", 7) == 0) {
|
|
|
|
newShell.noPrint = arg + 7;
|
|
|
|
newShell.noPrintLen = strlen(newShell.noPrint);
|
|
|
|
} else if (strncmp(arg, "echoFlag=", 9) == 0) {
|
|
|
|
newShell.echoFlag = arg + 9;
|
|
|
|
} else if (strncmp(arg, "errFlag=", 8) == 0) {
|
|
|
|
newShell.errFlag = arg + 8;
|
|
|
|
} else if (strncmp(arg, "hasErrCtl=", 10) == 0) {
|
|
|
|
char c = arg[10];
|
|
|
|
newShell.hasErrCtl = c == 'Y' || c == 'y' ||
|
|
|
|
c == 'T' || c == 't';
|
|
|
|
} else if (strncmp(arg, "newline=", 8) == 0) {
|
|
|
|
newShell.newline = arg + 8;
|
|
|
|
} else if (strncmp(arg, "check=", 6) == 0) {
|
|
|
|
/* Before 2020-12-10, these two variables
|
|
|
|
* had been a single variable. */
|
|
|
|
newShell.errOn = arg + 6;
|
|
|
|
newShell.echoTmpl = arg + 6;
|
|
|
|
} else if (strncmp(arg, "ignore=", 7) == 0) {
|
|
|
|
/* Before 2020-12-10, these two variables
|
|
|
|
* had been a single variable. */
|
|
|
|
newShell.errOff = arg + 7;
|
|
|
|
newShell.runIgnTmpl = arg + 7;
|
|
|
|
} else if (strncmp(arg, "errout=", 7) == 0) {
|
|
|
|
newShell.runChkTmpl = arg + 7;
|
|
|
|
} else if (strncmp(arg, "comment=", 8) == 0) {
|
|
|
|
newShell.commentChar = arg[8];
|
|
|
|
} else {
|
|
|
|
Parse_Error(PARSE_FATAL,
|
|
|
|
"Unknown keyword \"%s\"", arg);
|
|
|
|
free(words);
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2021-06-25 18:16:24 +00:00
|
|
|
fullSpec = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
|
|
|
|
if (path == NULL) {
|
|
|
|
/*
|
|
|
|
* If no path was given, the user wants one of the
|
|
|
|
* pre-defined shells, yes? So we find the one s/he wants
|
|
|
|
* with the help of FindShellByName and set things up the
|
|
|
|
* right way. shellPath will be set up by Shell_Init.
|
|
|
|
*/
|
|
|
|
if (newShell.name == NULL) {
|
|
|
|
Parse_Error(PARSE_FATAL,
|
|
|
|
"Neither path nor name specified");
|
|
|
|
free(words);
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2021-01-14 01:24:34 +00:00
|
|
|
} else {
|
|
|
|
if ((sh = FindShellByName(newShell.name)) == NULL) {
|
|
|
|
Parse_Error(PARSE_WARNING,
|
|
|
|
"%s: No matching shell", newShell.name);
|
|
|
|
free(words);
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
shell = sh;
|
|
|
|
shellName = newShell.name;
|
|
|
|
if (shellPath != NULL) {
|
|
|
|
/*
|
|
|
|
* Shell_Init has already been called!
|
|
|
|
* Do it again.
|
|
|
|
*/
|
|
|
|
free(UNCONST(shellPath));
|
|
|
|
shellPath = NULL;
|
|
|
|
Shell_Init();
|
|
|
|
}
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
} else {
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* The user provided a path. If s/he gave nothing else
|
2021-06-25 18:16:24 +00:00
|
|
|
* (fullSpec is false), try and find a matching shell in the
|
2021-01-14 01:24:34 +00:00
|
|
|
* ones we know of. Else we just take the specification at
|
|
|
|
* its word and copy it to a new location. In either case,
|
|
|
|
* we need to record the path the user gave for the shell.
|
|
|
|
*/
|
|
|
|
shellPath = path;
|
|
|
|
path = strrchr(path, '/');
|
|
|
|
if (path == NULL) {
|
|
|
|
path = UNCONST(shellPath);
|
|
|
|
} else {
|
|
|
|
path++;
|
|
|
|
}
|
|
|
|
if (newShell.name != NULL) {
|
|
|
|
shellName = newShell.name;
|
|
|
|
} else {
|
|
|
|
shellName = path;
|
|
|
|
}
|
|
|
|
if (!fullSpec) {
|
|
|
|
if ((sh = FindShellByName(shellName)) == NULL) {
|
|
|
|
Parse_Error(PARSE_WARNING,
|
|
|
|
"%s: No matching shell", shellName);
|
|
|
|
free(words);
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
shell = sh;
|
|
|
|
} else {
|
|
|
|
shell = bmake_malloc(sizeof *shell);
|
|
|
|
*shell = newShell;
|
|
|
|
}
|
|
|
|
/* this will take care of shellErrFlag */
|
|
|
|
Shell_Init();
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (shell->echoOn != NULL && shell->echoOff != NULL)
|
2021-06-25 18:16:24 +00:00
|
|
|
shell->hasEchoCtl = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (!shell->hasErrCtl) {
|
|
|
|
if (shell->echoTmpl == NULL)
|
|
|
|
shell->echoTmpl = "";
|
|
|
|
if (shell->runIgnTmpl == NULL)
|
|
|
|
shell->runIgnTmpl = "%s\n";
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Do not free up the words themselves, since they might be in use
|
|
|
|
* by the shell specification.
|
|
|
|
*/
|
|
|
|
free(words);
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Handle the receipt of an interrupt.
|
2020-11-07 19:39:21 +00:00
|
|
|
*
|
|
|
|
* All children are killed. Another job will be started if the .INTERRUPT
|
|
|
|
* target is defined.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* runINTERRUPT Non-zero if commands for the .INTERRUPT target
|
|
|
|
* should be executed
|
|
|
|
* signo signal received
|
|
|
|
*/
|
|
|
|
static void
|
2021-06-25 18:16:24 +00:00
|
|
|
JobInterrupt(bool runINTERRUPT, int signo)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
Job *job; /* job descriptor in that element */
|
|
|
|
GNode *interrupt; /* the node describing the .INTERRUPT target */
|
|
|
|
sigset_t mask;
|
|
|
|
GNode *gn;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
aborting = ABORT_INTERRUPT;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
JobSigLock(&mask);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (job = job_table; job < job_table_end; job++) {
|
|
|
|
if (job->status != JOB_ST_RUNNING)
|
|
|
|
continue;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
gn = job->node;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
JobDeleteTarget(gn);
|
|
|
|
if (job->pid != 0) {
|
|
|
|
DEBUG2(JOB,
|
|
|
|
"JobInterrupt passing signal %d to child %d.\n",
|
|
|
|
signo, job->pid);
|
|
|
|
KILLPG(job->pid, signo);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
JobSigUnlock(&mask);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (runINTERRUPT && !opts.touchFlag) {
|
|
|
|
interrupt = Targ_FindNode(".INTERRUPT");
|
|
|
|
if (interrupt != NULL) {
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.ignoreErrors = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
JobRun(interrupt);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
Trace_Log(MAKEINTR, NULL);
|
|
|
|
exit(signo); /* XXX: why signo? */
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Do the final processing, i.e. run the commands attached to the .END target.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2021-01-14 01:24:34 +00:00
|
|
|
* Return the number of errors reported.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
int
|
|
|
|
Job_Finish(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
GNode *endNode = Targ_GetEndNode();
|
|
|
|
if (!Lst_IsEmpty(&endNode->commands) ||
|
|
|
|
!Lst_IsEmpty(&endNode->children)) {
|
|
|
|
if (job_errors != 0) {
|
|
|
|
Error("Errors reported so .END ignored");
|
|
|
|
} else {
|
|
|
|
JobRun(endNode);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
return job_errors;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Clean up any memory used by the jobs module. */
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
Job_End(void)
|
|
|
|
{
|
|
|
|
#ifdef CLEANUP
|
2021-02-11 01:51:11 +00:00
|
|
|
free(shell_freeIt);
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Waits for all running jobs to finish and returns.
|
|
|
|
* Sets 'aborting' to ABORT_WAIT to prevent other jobs from starting.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
Job_Wait(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
aborting = ABORT_WAIT;
|
|
|
|
while (jobTokensRunning != 0) {
|
|
|
|
Job_CatchOutput();
|
|
|
|
}
|
|
|
|
aborting = ABORT_NONE;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Abort all currently running jobs without handling output or anything.
|
2020-11-07 19:39:21 +00:00
|
|
|
* This function is to be called only in the event of a major error.
|
|
|
|
* Most definitely NOT to be called from JobInterrupt.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2021-01-14 01:24:34 +00:00
|
|
|
* All children are killed, not just the firstborn.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
Job_AbortAll(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
Job *job; /* the job descriptor in that element */
|
|
|
|
WAIT_T foo;
|
|
|
|
|
|
|
|
aborting = ABORT_ERROR;
|
|
|
|
|
|
|
|
if (jobTokensRunning != 0) {
|
|
|
|
for (job = job_table; job < job_table_end; job++) {
|
|
|
|
if (job->status != JOB_ST_RUNNING)
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* kill the child process with increasingly drastic
|
|
|
|
* signals to make darn sure it's dead.
|
|
|
|
*/
|
|
|
|
KILLPG(job->pid, SIGINT);
|
|
|
|
KILLPG(job->pid, SIGKILL);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Catch as many children as want to report in at first, then give up
|
|
|
|
*/
|
|
|
|
while (waitpid((pid_t)-1, &foo, WNOHANG) > 0)
|
|
|
|
continue;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Tries to restart stopped jobs if there are slots available.
|
|
|
|
* Called in process context in response to a SIGCONT.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
static void
|
|
|
|
JobRestartJobs(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
Job *job;
|
|
|
|
|
|
|
|
for (job = job_table; job < job_table_end; job++) {
|
|
|
|
if (job->status == JOB_ST_RUNNING &&
|
|
|
|
(make_suspended || job->suspended)) {
|
|
|
|
DEBUG1(JOB, "Restarting stopped job pid %d.\n",
|
|
|
|
job->pid);
|
|
|
|
if (job->suspended) {
|
|
|
|
(void)printf("*** [%s] Continued\n",
|
|
|
|
job->node->name);
|
|
|
|
(void)fflush(stdout);
|
|
|
|
}
|
2021-06-25 18:16:24 +00:00
|
|
|
job->suspended = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) {
|
|
|
|
debug_printf("Failed to send SIGCONT to %d\n",
|
|
|
|
job->pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (job->status == JOB_ST_FINISHED) {
|
|
|
|
/*
|
|
|
|
* Job exit deferred after calling waitpid() in a
|
|
|
|
* signal handler
|
|
|
|
*/
|
|
|
|
JobFinish(job, job->exit_status);
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 18:16:24 +00:00
|
|
|
make_suspended = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
watchfd(Job *job)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
if (job->inPollfd != NULL)
|
|
|
|
Punt("Watching watched job");
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
fds[fdsLen].fd = job->inPipe;
|
|
|
|
fds[fdsLen].events = POLLIN;
|
|
|
|
jobByFdIndex[fdsLen] = job;
|
|
|
|
job->inPollfd = &fds[fdsLen];
|
|
|
|
fdsLen++;
|
2020-05-20 19:34:48 +00:00
|
|
|
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
|
2021-01-14 01:24:34 +00:00
|
|
|
if (useMeta) {
|
2021-02-11 01:51:11 +00:00
|
|
|
fds[fdsLen].fd = meta_job_fd(job);
|
|
|
|
fds[fdsLen].events = fds[fdsLen].fd == -1 ? 0 : POLLIN;
|
|
|
|
jobByFdIndex[fdsLen] = job;
|
|
|
|
fdsLen++;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2020-05-20 19:34:48 +00:00
|
|
|
#endif
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clearfd(Job *job)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
size_t i;
|
|
|
|
if (job->inPollfd == NULL)
|
|
|
|
Punt("Unwatching unwatched job");
|
|
|
|
i = (size_t)(job->inPollfd - fds);
|
2021-02-11 01:51:11 +00:00
|
|
|
fdsLen--;
|
2020-05-20 19:34:48 +00:00
|
|
|
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
|
2021-01-14 01:24:34 +00:00
|
|
|
if (useMeta) {
|
|
|
|
/*
|
|
|
|
* Sanity check: there should be two fds per job, so the job's
|
|
|
|
* pollfd number should be even.
|
|
|
|
*/
|
|
|
|
assert(nfds_per_job() == 2);
|
|
|
|
if (i % 2 != 0)
|
|
|
|
Punt("odd-numbered fd with meta");
|
2021-02-11 01:51:11 +00:00
|
|
|
fdsLen--;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-05-20 19:34:48 +00:00
|
|
|
/*
|
2021-01-14 01:24:34 +00:00
|
|
|
* Move last job in table into hole made by dead job.
|
2020-05-20 19:34:48 +00:00
|
|
|
*/
|
2021-02-11 01:51:11 +00:00
|
|
|
if (fdsLen != i) {
|
|
|
|
fds[i] = fds[fdsLen];
|
|
|
|
jobByFdIndex[i] = jobByFdIndex[fdsLen];
|
|
|
|
jobByFdIndex[i]->inPollfd = &fds[i];
|
2020-05-20 19:34:48 +00:00
|
|
|
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
|
2021-01-14 01:24:34 +00:00
|
|
|
if (useMeta) {
|
2021-02-11 01:51:11 +00:00
|
|
|
fds[i + 1] = fds[fdsLen + 1];
|
|
|
|
jobByFdIndex[i + 1] = jobByFdIndex[fdsLen + 1];
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
2020-05-20 19:34:48 +00:00
|
|
|
#endif
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
job->inPollfd = NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2012-06-08 21:57:36 +00:00
|
|
|
readyfd(Job *job)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
if (job->inPollfd == NULL)
|
|
|
|
Punt("Polling unwatched job");
|
|
|
|
return (job->inPollfd->revents & POLLIN) != 0;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Put a token (back) into the job pipe.
|
|
|
|
* This allows a make process to start a build job.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
static void
|
|
|
|
JobTokenAdd(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
char tok = JOB_TOKENS[aborting], tok1;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 06:21:37 +00:00
|
|
|
if (!Job_error_token && aborting == ABORT_ERROR) {
|
|
|
|
if (jobTokensRunning == 0)
|
|
|
|
return;
|
|
|
|
tok = '+'; /* no error token */
|
|
|
|
}
|
2013-09-06 02:55:51 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* If we are depositing an error token flush everything else */
|
|
|
|
while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1)
|
|
|
|
continue;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
DEBUG3(JOB, "(%d) aborting %d, deposit token %c\n",
|
|
|
|
getpid(), aborting, tok);
|
|
|
|
while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN)
|
|
|
|
continue;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
/* Get a temp file */
|
|
|
|
int
|
|
|
|
Job_TempFile(const char *pattern, char *tfile, size_t tfile_sz)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
sigset_t mask;
|
|
|
|
|
|
|
|
JobSigLock(&mask);
|
|
|
|
fd = mkTempFile(pattern, tfile, tfile_sz);
|
|
|
|
if (tfile != NULL && !DEBUG(SCRIPT))
|
|
|
|
unlink(tfile);
|
|
|
|
JobSigUnlock(&mask);
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Prep the job token pipe in the root make process. */
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
Job_ServerStart(int max_tokens, int jp_0, int jp_1)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
int i;
|
|
|
|
char jobarg[64];
|
|
|
|
|
|
|
|
if (jp_0 >= 0 && jp_1 >= 0) {
|
|
|
|
/* Pipe passed in from parent */
|
|
|
|
tokenWaitJob.inPipe = jp_0;
|
|
|
|
tokenWaitJob.outPipe = jp_1;
|
|
|
|
(void)fcntl(jp_0, F_SETFD, FD_CLOEXEC);
|
|
|
|
(void)fcntl(jp_1, F_SETFD, FD_CLOEXEC);
|
|
|
|
return;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
JobCreatePipe(&tokenWaitJob, 15);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
snprintf(jobarg, sizeof jobarg, "%d,%d",
|
2012-06-08 21:57:36 +00:00
|
|
|
tokenWaitJob.inPipe, tokenWaitJob.outPipe);
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-J");
|
|
|
|
Global_Append(MAKEFLAGS, jobarg);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Preload the job pipe with one token per job, save the one
|
|
|
|
* "extra" token for the primary job.
|
|
|
|
*
|
|
|
|
* XXX should clip maxJobs against PIPE_BUF -- if max_tokens is
|
|
|
|
* larger than the write buffer size of the pipe, we will
|
|
|
|
* deadlock here.
|
|
|
|
*/
|
|
|
|
for (i = 1; i < max_tokens; i++)
|
|
|
|
JobTokenAdd();
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Return a withdrawn token to the pool. */
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
Job_TokenReturn(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
jobTokensRunning--;
|
|
|
|
if (jobTokensRunning < 0)
|
|
|
|
Punt("token botch");
|
|
|
|
if (jobTokensRunning != 0 || JOB_TOKENS[aborting] != '+')
|
|
|
|
JobTokenAdd();
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Attempt to withdraw a token from the pool.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2020-11-07 19:39:21 +00:00
|
|
|
* If pool is empty, set wantToken so that we wake up when a token is
|
|
|
|
* released.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2021-06-25 18:16:24 +00:00
|
|
|
* Returns true if a token was withdrawn, and false if the pool is currently
|
2021-01-14 01:24:34 +00:00
|
|
|
* empty.
|
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
bool
|
2012-06-08 21:57:36 +00:00
|
|
|
Job_TokenWithdraw(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
char tok, tok1;
|
|
|
|
ssize_t count;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
wantToken = 0;
|
|
|
|
DEBUG3(JOB, "Job_TokenWithdraw(%d): aborting %d, running %d\n",
|
|
|
|
getpid(), aborting, jobTokensRunning);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (aborting != ABORT_NONE || (jobTokensRunning >= opts.maxJobs))
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
count = read(tokenWaitJob.inPipe, &tok, 1);
|
|
|
|
if (count == 0)
|
|
|
|
Fatal("eof on job pipe!");
|
|
|
|
if (count < 0 && jobTokensRunning != 0) {
|
|
|
|
if (errno != EAGAIN) {
|
|
|
|
Fatal("job pipe read: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
DEBUG1(JOB, "(%d) blocked for token\n", getpid());
|
|
|
|
wantToken = 1;
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (count == 1 && tok != '+') {
|
|
|
|
/* make being aborted - remove any other job tokens */
|
|
|
|
DEBUG2(JOB, "(%d) aborted by token %c\n", getpid(), tok);
|
|
|
|
while (read(tokenWaitJob.inPipe, &tok1, 1) == 1)
|
|
|
|
continue;
|
|
|
|
/* And put the stopper back */
|
|
|
|
while (write(tokenWaitJob.outPipe, &tok, 1) == -1 &&
|
|
|
|
errno == EAGAIN)
|
|
|
|
continue;
|
|
|
|
if (shouldDieQuietly(NULL, 1))
|
|
|
|
exit(6); /* we aborted */
|
|
|
|
Fatal("A failure has been detected "
|
|
|
|
"in another branch of the parallel make");
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (count == 1 && jobTokensRunning == 0)
|
|
|
|
/* We didn't want the token really */
|
|
|
|
while (write(tokenWaitJob.outPipe, &tok, 1) == -1 &&
|
|
|
|
errno == EAGAIN)
|
|
|
|
continue;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
jobTokensRunning++;
|
|
|
|
DEBUG1(JOB, "(%d) withdrew token\n", getpid());
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Run the named target if found. If a filename is specified, then set that
|
2020-11-07 19:39:21 +00:00
|
|
|
* to the sources.
|
2013-04-01 21:12:55 +00:00
|
|
|
*
|
2021-01-14 01:24:34 +00:00
|
|
|
* Exits if the target fails.
|
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
bool
|
2021-01-14 01:24:34 +00:00
|
|
|
Job_RunTarget(const char *target, const char *fname)
|
|
|
|
{
|
|
|
|
GNode *gn = Targ_FindNode(target);
|
|
|
|
if (gn == NULL)
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2013-04-01 21:12:55 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (fname != NULL)
|
2021-02-11 01:51:11 +00:00
|
|
|
Var_Set(gn, ALLSRC, fname);
|
2013-04-01 21:12:55 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
JobRun(gn);
|
|
|
|
/* XXX: Replace with GNode_IsError(gn) */
|
|
|
|
if (gn->made == ERROR) {
|
|
|
|
PrintOnError(gn, "\n\nStop.");
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2013-04-01 21:12:55 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
#ifdef USE_SELECT
|
|
|
|
int
|
|
|
|
emul_poll(struct pollfd *fd, int nfd, int timeout)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
fd_set rfds, wfds;
|
|
|
|
int i, maxfd, nselect, npoll;
|
|
|
|
struct timeval tv, *tvp;
|
|
|
|
long usecs;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
maxfd = -1;
|
|
|
|
for (i = 0; i < nfd; i++) {
|
|
|
|
fd[i].revents = 0;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (fd[i].events & POLLIN)
|
|
|
|
FD_SET(fd[i].fd, &rfds);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (fd[i].events & POLLOUT)
|
|
|
|
FD_SET(fd[i].fd, &wfds);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (fd[i].fd > maxfd)
|
|
|
|
maxfd = fd[i].fd;
|
|
|
|
}
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (maxfd >= FD_SETSIZE) {
|
|
|
|
Punt("Ran out of fd_set slots; "
|
|
|
|
"recompile with a larger FD_SETSIZE.");
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (timeout < 0) {
|
|
|
|
tvp = NULL;
|
|
|
|
} else {
|
|
|
|
usecs = timeout * 1000;
|
|
|
|
tv.tv_sec = usecs / 1000000;
|
|
|
|
tv.tv_usec = usecs % 1000000;
|
|
|
|
tvp = &tv;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
nselect = select(maxfd + 1, &rfds, &wfds, NULL, tvp);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (nselect <= 0)
|
|
|
|
return nselect;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
npoll = 0;
|
|
|
|
for (i = 0; i < nfd; i++) {
|
|
|
|
if (FD_ISSET(fd[i].fd, &rfds))
|
|
|
|
fd[i].revents |= POLLIN;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (FD_ISSET(fd[i].fd, &wfds))
|
|
|
|
fd[i].revents |= POLLOUT;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (fd[i].revents)
|
|
|
|
npoll++;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
return npoll;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
#endif /* USE_SELECT */
|