1*c9f4001fSSimon J. Gerraty /* $NetBSD: job.c,v 1.471 2024/05/07 18:26:22 sjg Exp $ */ 23955d011SMarcel Moolenaar 33955d011SMarcel Moolenaar /* 43955d011SMarcel Moolenaar * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 53955d011SMarcel Moolenaar * All rights reserved. 63955d011SMarcel Moolenaar * 73955d011SMarcel Moolenaar * This code is derived from software contributed to Berkeley by 83955d011SMarcel Moolenaar * Adam de Boor. 93955d011SMarcel Moolenaar * 103955d011SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 113955d011SMarcel Moolenaar * modification, are permitted provided that the following conditions 123955d011SMarcel Moolenaar * are met: 133955d011SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 143955d011SMarcel Moolenaar * notice, this list of conditions and the following disclaimer. 153955d011SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 163955d011SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 173955d011SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 183955d011SMarcel Moolenaar * 3. Neither the name of the University nor the names of its contributors 193955d011SMarcel Moolenaar * may be used to endorse or promote products derived from this software 203955d011SMarcel Moolenaar * without specific prior written permission. 213955d011SMarcel Moolenaar * 223955d011SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 233955d011SMarcel Moolenaar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 243955d011SMarcel Moolenaar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 253955d011SMarcel Moolenaar * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 263955d011SMarcel Moolenaar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 273955d011SMarcel Moolenaar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 283955d011SMarcel Moolenaar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 293955d011SMarcel Moolenaar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 303955d011SMarcel Moolenaar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 313955d011SMarcel Moolenaar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 323955d011SMarcel Moolenaar * SUCH DAMAGE. 333955d011SMarcel Moolenaar */ 343955d011SMarcel Moolenaar 353955d011SMarcel Moolenaar /* 363955d011SMarcel Moolenaar * Copyright (c) 1988, 1989 by Adam de Boor 373955d011SMarcel Moolenaar * Copyright (c) 1989 by Berkeley Softworks 383955d011SMarcel Moolenaar * All rights reserved. 393955d011SMarcel Moolenaar * 403955d011SMarcel Moolenaar * This code is derived from software contributed to Berkeley by 413955d011SMarcel Moolenaar * Adam de Boor. 423955d011SMarcel Moolenaar * 433955d011SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 443955d011SMarcel Moolenaar * modification, are permitted provided that the following conditions 453955d011SMarcel Moolenaar * are met: 463955d011SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 473955d011SMarcel Moolenaar * notice, this list of conditions and the following disclaimer. 483955d011SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 493955d011SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 503955d011SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 513955d011SMarcel Moolenaar * 3. All advertising materials mentioning features or use of this software 523955d011SMarcel Moolenaar * must display the following acknowledgement: 533955d011SMarcel Moolenaar * This product includes software developed by the University of 543955d011SMarcel Moolenaar * California, Berkeley and its contributors. 553955d011SMarcel Moolenaar * 4. Neither the name of the University nor the names of its contributors 563955d011SMarcel Moolenaar * may be used to endorse or promote products derived from this software 573955d011SMarcel Moolenaar * without specific prior written permission. 583955d011SMarcel Moolenaar * 593955d011SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 603955d011SMarcel Moolenaar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 613955d011SMarcel Moolenaar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 623955d011SMarcel Moolenaar * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 633955d011SMarcel Moolenaar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 643955d011SMarcel Moolenaar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 653955d011SMarcel Moolenaar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 663955d011SMarcel Moolenaar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 673955d011SMarcel Moolenaar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 683955d011SMarcel Moolenaar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 693955d011SMarcel Moolenaar * SUCH DAMAGE. 703955d011SMarcel Moolenaar */ 713955d011SMarcel Moolenaar 72dba7b0efSSimon J. Gerraty /* 73d5e0a182SSimon J. Gerraty * Create child processes and collect their output. 743955d011SMarcel Moolenaar * 753955d011SMarcel Moolenaar * Interface: 76956e45f6SSimon J. Gerraty * Job_Init Called to initialize this module. In addition, 77d5e0a182SSimon J. Gerraty * the .BEGIN target is made, including all of its 78dba7b0efSSimon J. Gerraty * dependencies before this function returns. 79956e45f6SSimon J. Gerraty * Hence, the makefiles must have been parsed 80956e45f6SSimon J. Gerraty * before this function is called. 81956e45f6SSimon J. Gerraty * 82956e45f6SSimon J. Gerraty * Job_End Clean up any memory used. 83956e45f6SSimon J. Gerraty * 843955d011SMarcel Moolenaar * Job_Make Start the creation of the given target. 853955d011SMarcel Moolenaar * 86956e45f6SSimon J. Gerraty * Job_CatchChildren 87956e45f6SSimon J. Gerraty * Check for and handle the termination of any 883955d011SMarcel Moolenaar * children. This must be called reasonably 893955d011SMarcel Moolenaar * frequently to keep the whole make going at 903955d011SMarcel Moolenaar * a decent clip, since job table entries aren't 913955d011SMarcel Moolenaar * removed until their process is caught this way. 923955d011SMarcel Moolenaar * 93956e45f6SSimon J. Gerraty * Job_CatchOutput 94956e45f6SSimon J. Gerraty * Print any output our children have produced. 953955d011SMarcel Moolenaar * Should also be called fairly frequently to 963955d011SMarcel Moolenaar * keep the user informed of what's going on. 973955d011SMarcel Moolenaar * If no output is waiting, it will block for 983955d011SMarcel Moolenaar * a time given by the SEL_* constants, below, 993955d011SMarcel Moolenaar * or until output is ready. 1003955d011SMarcel Moolenaar * 101dba7b0efSSimon J. Gerraty * Job_ParseShell Given a special dependency line with target '.SHELL', 102dba7b0efSSimon J. Gerraty * define the shell that is used for the creation 103dba7b0efSSimon J. Gerraty * commands in jobs mode. 1043955d011SMarcel Moolenaar * 1053955d011SMarcel Moolenaar * Job_Finish Perform any final processing which needs doing. 1063955d011SMarcel Moolenaar * This includes the execution of any commands 1073955d011SMarcel Moolenaar * which have been/were attached to the .END 1083955d011SMarcel Moolenaar * target. It should only be called when the 1093955d011SMarcel Moolenaar * job table is empty. 1103955d011SMarcel Moolenaar * 111dba7b0efSSimon J. Gerraty * Job_AbortAll Abort all currently running jobs. Do not handle 112dba7b0efSSimon J. Gerraty * output or do anything for the jobs, just kill them. 113dba7b0efSSimon J. Gerraty * Should only be called in an emergency. 1143955d011SMarcel Moolenaar * 115956e45f6SSimon J. Gerraty * Job_CheckCommands 116956e45f6SSimon J. Gerraty * Verify that the commands for a target are 1173955d011SMarcel Moolenaar * ok. Provide them if necessary and possible. 1183955d011SMarcel Moolenaar * 1193955d011SMarcel Moolenaar * Job_Touch Update a target without really updating it. 1203955d011SMarcel Moolenaar * 1213955d011SMarcel Moolenaar * Job_Wait Wait for all currently-running jobs to finish. 1223955d011SMarcel Moolenaar */ 1233955d011SMarcel Moolenaar 1243955d011SMarcel Moolenaar #ifdef HAVE_CONFIG_H 1253955d011SMarcel Moolenaar # include "config.h" 1263955d011SMarcel Moolenaar #endif 1273955d011SMarcel Moolenaar #include <sys/types.h> 1283955d011SMarcel Moolenaar #include <sys/stat.h> 1293955d011SMarcel Moolenaar #include <sys/file.h> 1303955d011SMarcel Moolenaar #include <sys/time.h> 1313955d011SMarcel Moolenaar #include "wait.h" 1323955d011SMarcel Moolenaar 1333955d011SMarcel Moolenaar #include <errno.h> 1343955d011SMarcel Moolenaar #if !defined(USE_SELECT) && defined(HAVE_POLL_H) 1353955d011SMarcel Moolenaar #include <poll.h> 1363955d011SMarcel Moolenaar #else 1373955d011SMarcel Moolenaar #ifndef USE_SELECT /* no poll.h */ 1383955d011SMarcel Moolenaar # define USE_SELECT 1393955d011SMarcel Moolenaar #endif 1403955d011SMarcel Moolenaar #if defined(HAVE_SYS_SELECT_H) 1413955d011SMarcel Moolenaar # include <sys/select.h> 1423955d011SMarcel Moolenaar #endif 1433955d011SMarcel Moolenaar #endif 1443955d011SMarcel Moolenaar #include <signal.h> 1453955d011SMarcel Moolenaar #include <utime.h> 1463955d011SMarcel Moolenaar #if defined(HAVE_SYS_SOCKET_H) 1473955d011SMarcel Moolenaar # include <sys/socket.h> 1483955d011SMarcel Moolenaar #endif 1493955d011SMarcel Moolenaar 1503955d011SMarcel Moolenaar #include "make.h" 1513955d011SMarcel Moolenaar #include "dir.h" 1523955d011SMarcel Moolenaar #include "job.h" 1533955d011SMarcel Moolenaar #include "pathnames.h" 1543955d011SMarcel Moolenaar #include "trace.h" 155956e45f6SSimon J. Gerraty 156956e45f6SSimon J. Gerraty /* "@(#)job.c 8.2 (Berkeley) 3/19/94" */ 157*c9f4001fSSimon J. Gerraty MAKE_RCSID("$NetBSD: job.c,v 1.471 2024/05/07 18:26:22 sjg Exp $"); 158956e45f6SSimon J. Gerraty 15906b9b3e0SSimon J. Gerraty /* 16006b9b3e0SSimon J. Gerraty * A shell defines how the commands are run. All commands for a target are 161956e45f6SSimon J. Gerraty * written into a single file, which is then given to the shell to execute 162956e45f6SSimon J. Gerraty * the commands from it. The commands are written to the file using a few 163956e45f6SSimon J. Gerraty * templates for echo control and error control. 164956e45f6SSimon J. Gerraty * 165956e45f6SSimon J. Gerraty * The name of the shell is the basename for the predefined shells, such as 166956e45f6SSimon J. Gerraty * "sh", "csh", "bash". For custom shells, it is the full pathname, and its 167956e45f6SSimon J. Gerraty * basename is used to select the type of shell; the longest match wins. 168956e45f6SSimon J. Gerraty * So /usr/pkg/bin/bash has type sh, /usr/local/bin/tcsh has type csh. 169956e45f6SSimon J. Gerraty * 170956e45f6SSimon J. Gerraty * The echoing of command lines is controlled using hasEchoCtl, echoOff, 171956e45f6SSimon J. Gerraty * echoOn, noPrint and noPrintLen. When echoOff is executed by the shell, it 172956e45f6SSimon J. Gerraty * still outputs something, but this something is not interesting, therefore 173956e45f6SSimon J. Gerraty * it is filtered out using noPrint and noPrintLen. 174956e45f6SSimon J. Gerraty * 175956e45f6SSimon J. Gerraty * The error checking for individual commands is controlled using hasErrCtl, 17606b9b3e0SSimon J. Gerraty * errOn, errOff and runChkTmpl. 177956e45f6SSimon J. Gerraty * 178dba7b0efSSimon J. Gerraty * In case a shell doesn't have error control, echoTmpl is a printf template 179dba7b0efSSimon J. Gerraty * for echoing the command, should echoing be on; runIgnTmpl is another 180dba7b0efSSimon J. Gerraty * printf template for executing the command while ignoring the return 18106b9b3e0SSimon J. Gerraty * status. Finally runChkTmpl is a printf template for running the command and 182956e45f6SSimon J. Gerraty * causing the shell to exit on error. If any of these strings are empty when 183b0c40a00SSimon J. Gerraty * hasErrCtl is false, the command will be executed anyway as is, and if it 184956e45f6SSimon J. Gerraty * causes an error, so be it. Any templates set up to echo the command will 185dba7b0efSSimon J. Gerraty * escape any '$ ` \ "' characters in the command string to avoid unwanted 186dba7b0efSSimon J. Gerraty * shell code injection, the escaped command is safe to use in double quotes. 187956e45f6SSimon J. Gerraty * 188956e45f6SSimon J. Gerraty * The command-line flags "echo" and "exit" also control the behavior. The 189956e45f6SSimon J. Gerraty * "echo" flag causes the shell to start echoing commands right away. The 190956e45f6SSimon J. Gerraty * "exit" flag causes the shell to exit when an error is detected in one of 191956e45f6SSimon J. Gerraty * the commands. 192956e45f6SSimon J. Gerraty */ 193956e45f6SSimon J. Gerraty typedef struct Shell { 194956e45f6SSimon J. Gerraty 19506b9b3e0SSimon J. Gerraty /* 19606b9b3e0SSimon J. Gerraty * The name of the shell. For Bourne and C shells, this is used only 19706b9b3e0SSimon J. Gerraty * to find the shell description when used as the single source of a 19806b9b3e0SSimon J. Gerraty * .SHELL target. For user-defined shells, this is the full path of 19906b9b3e0SSimon J. Gerraty * the shell. 20006b9b3e0SSimon J. Gerraty */ 201956e45f6SSimon J. Gerraty const char *name; 202956e45f6SSimon J. Gerraty 203b0c40a00SSimon J. Gerraty bool hasEchoCtl; /* whether both echoOff and echoOn are there */ 20406b9b3e0SSimon J. Gerraty const char *echoOff; /* command to turn echoing off */ 20506b9b3e0SSimon J. Gerraty const char *echoOn; /* command to turn echoing back on */ 20606b9b3e0SSimon J. Gerraty const char *noPrint; /* text to skip when printing output from the 207956e45f6SSimon J. Gerraty * shell. This is usually the same as echoOff */ 208956e45f6SSimon J. Gerraty size_t noPrintLen; /* length of noPrint command */ 209956e45f6SSimon J. Gerraty 210b0c40a00SSimon J. Gerraty bool hasErrCtl; /* whether error checking can be controlled 21106b9b3e0SSimon J. Gerraty * for individual commands */ 21206b9b3e0SSimon J. Gerraty const char *errOn; /* command to turn on error checking */ 21306b9b3e0SSimon J. Gerraty const char *errOff; /* command to turn off error checking */ 21406b9b3e0SSimon J. Gerraty 21506b9b3e0SSimon J. Gerraty const char *echoTmpl; /* template to echo a command */ 2169f45a3c8SSimon J. Gerraty const char *runIgnTmpl; /* template to run a command without error 2179f45a3c8SSimon J. Gerraty * checking */ 2189f45a3c8SSimon J. Gerraty const char *runChkTmpl; /* template to run a command with error 2199f45a3c8SSimon J. Gerraty * checking */ 220956e45f6SSimon J. Gerraty 2219f45a3c8SSimon J. Gerraty /* 2229f45a3c8SSimon J. Gerraty * A string literal that results in a newline character when it 2239f45a3c8SSimon J. Gerraty * occurs outside of any 'quote' or "quote" characters. 2249f45a3c8SSimon J. Gerraty */ 225956e45f6SSimon J. Gerraty const char *newline; 226956e45f6SSimon J. Gerraty char commentChar; /* character used by shell for comment lines */ 227956e45f6SSimon J. Gerraty 22806b9b3e0SSimon J. Gerraty const char *echoFlag; /* shell flag to echo commands */ 22906b9b3e0SSimon J. Gerraty const char *errFlag; /* shell flag to exit on error */ 230956e45f6SSimon J. Gerraty } Shell; 2313955d011SMarcel Moolenaar 23206b9b3e0SSimon J. Gerraty typedef struct CommandFlags { 233dba7b0efSSimon J. Gerraty /* Whether to echo the command before or instead of running it. */ 234b0c40a00SSimon J. Gerraty bool echo; 23506b9b3e0SSimon J. Gerraty 23606b9b3e0SSimon J. Gerraty /* Run the command even in -n or -N mode. */ 237b0c40a00SSimon J. Gerraty bool always; 23806b9b3e0SSimon J. Gerraty 23906b9b3e0SSimon J. Gerraty /* 240b0c40a00SSimon J. Gerraty * true if we turned error checking off before writing the command to 241b0c40a00SSimon J. Gerraty * the commands file and need to turn it back on 24206b9b3e0SSimon J. Gerraty */ 243b0c40a00SSimon J. Gerraty bool ignerr; 24406b9b3e0SSimon J. Gerraty } CommandFlags; 24506b9b3e0SSimon J. Gerraty 24606b9b3e0SSimon J. Gerraty /* 24706b9b3e0SSimon J. Gerraty * Write shell commands to a file. 24806b9b3e0SSimon J. Gerraty * 24906b9b3e0SSimon J. Gerraty * TODO: keep track of whether commands are echoed. 25006b9b3e0SSimon J. Gerraty * TODO: keep track of whether error checking is active. 25106b9b3e0SSimon J. Gerraty */ 25206b9b3e0SSimon J. Gerraty typedef struct ShellWriter { 25306b9b3e0SSimon J. Gerraty FILE *f; 25406b9b3e0SSimon J. Gerraty 25506b9b3e0SSimon J. Gerraty /* we've sent 'set -x' */ 256b0c40a00SSimon J. Gerraty bool xtraced; 25706b9b3e0SSimon J. Gerraty 25806b9b3e0SSimon J. Gerraty } ShellWriter; 25906b9b3e0SSimon J. Gerraty 2603955d011SMarcel Moolenaar /* 2619a4bc556SSimon J. Gerraty * FreeBSD: traditionally .MAKE is not required to 2629a4bc556SSimon J. Gerraty * pass jobs queue to sub-makes. 2639a4bc556SSimon J. Gerraty * Use .MAKE.ALWAYS_PASS_JOB_QUEUE=no to disable. 2649a4bc556SSimon J. Gerraty */ 2653b96abbaSSimon J. Gerraty #define MAKE_ALWAYS_PASS_JOB_QUEUE "${.MAKE.ALWAYS_PASS_JOB_QUEUE:U}" 26668c4481aSSimon J. Gerraty static bool Always_pass_job_queue = true; 2672d395cb5SSimon J. Gerraty /* 2682d395cb5SSimon J. Gerraty * FreeBSD: aborting entire parallel make isn't always 2692d395cb5SSimon J. Gerraty * desired. When doing tinderbox for example, failure of 2702d395cb5SSimon J. Gerraty * one architecture should not stop all. 2712d395cb5SSimon J. Gerraty * We still want to bail on interrupt though. 2722d395cb5SSimon J. Gerraty */ 2733b96abbaSSimon J. Gerraty #define MAKE_JOB_ERROR_TOKEN "${MAKE_JOB_ERROR_TOKEN:U}" 27468c4481aSSimon J. Gerraty static bool Job_error_token = true; 2759a4bc556SSimon J. Gerraty 276d5e0a182SSimon J. Gerraty /* error handling variables */ 27706b9b3e0SSimon J. Gerraty static int job_errors = 0; /* number of errors reported */ 278b0c40a00SSimon J. Gerraty static enum { /* Why is the make aborting? */ 279956e45f6SSimon J. Gerraty ABORT_NONE, 280b0c40a00SSimon J. Gerraty ABORT_ERROR, /* Aborted because of an error */ 281b0c40a00SSimon J. Gerraty ABORT_INTERRUPT, /* Aborted because it was interrupted */ 282956e45f6SSimon J. Gerraty ABORT_WAIT /* Waiting for jobs to finish */ 283b0c40a00SSimon J. Gerraty } aborting = ABORT_NONE; 2843955d011SMarcel Moolenaar #define JOB_TOKENS "+EI+" /* Token to requeue for each abort state */ 2853955d011SMarcel Moolenaar 286d5e0a182SSimon J. Gerraty /* Tracks the number of tokens currently "out" to build jobs. */ 2873955d011SMarcel Moolenaar int jobTokensRunning = 0; 2883955d011SMarcel Moolenaar 289956e45f6SSimon J. Gerraty typedef enum JobStartResult { 290956e45f6SSimon J. Gerraty JOB_RUNNING, /* Job is running */ 291956e45f6SSimon J. Gerraty JOB_ERROR, /* Error in starting the job */ 292956e45f6SSimon J. Gerraty JOB_FINISHED /* The job is already finished */ 293956e45f6SSimon J. Gerraty } JobStartResult; 2943955d011SMarcel Moolenaar 2953955d011SMarcel Moolenaar /* 2963955d011SMarcel Moolenaar * Descriptions for various shells. 2973955d011SMarcel Moolenaar * 2983955d011SMarcel Moolenaar * The build environment may set DEFSHELL_INDEX to one of 2993955d011SMarcel Moolenaar * DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to 300956e45f6SSimon J. Gerraty * select one of the predefined shells as the default shell. 3013955d011SMarcel Moolenaar * 3023955d011SMarcel Moolenaar * Alternatively, the build environment may set DEFSHELL_CUSTOM to the 3033955d011SMarcel Moolenaar * name or the full path of a sh-compatible shell, which will be used as 3043955d011SMarcel Moolenaar * the default shell. 3053955d011SMarcel Moolenaar * 3063955d011SMarcel Moolenaar * ".SHELL" lines in Makefiles can choose the default shell from the 307956e45f6SSimon J. Gerraty * set defined here, or add additional shells. 3083955d011SMarcel Moolenaar */ 3093955d011SMarcel Moolenaar 3103955d011SMarcel Moolenaar #ifdef DEFSHELL_CUSTOM 3113955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CUSTOM 0 3123955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH 1 3133955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH 2 3143955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH 3 315d5e0a182SSimon J. Gerraty #else 3163955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH 0 3173955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH 1 3183955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH 2 319d5e0a182SSimon J. Gerraty #endif 3203955d011SMarcel Moolenaar 3213955d011SMarcel Moolenaar #ifndef DEFSHELL_INDEX 3223955d011SMarcel Moolenaar #define DEFSHELL_INDEX 0 /* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */ 323d5e0a182SSimon J. Gerraty #endif 3243955d011SMarcel Moolenaar 3253955d011SMarcel Moolenaar static Shell shells[] = { 3263955d011SMarcel Moolenaar #ifdef DEFSHELL_CUSTOM 3273955d011SMarcel Moolenaar /* 3283955d011SMarcel Moolenaar * An sh-compatible shell with a non-standard name. 3293955d011SMarcel Moolenaar * 3303955d011SMarcel Moolenaar * Keep this in sync with the "sh" description below, but avoid 3313955d011SMarcel Moolenaar * non-portable features that might not be supplied by all 3323955d011SMarcel Moolenaar * sh-compatible shells. 3333955d011SMarcel Moolenaar */ 3343955d011SMarcel Moolenaar { 335956e45f6SSimon J. Gerraty DEFSHELL_CUSTOM, /* .name */ 336b0c40a00SSimon J. Gerraty false, /* .hasEchoCtl */ 337956e45f6SSimon J. Gerraty "", /* .echoOff */ 338956e45f6SSimon J. Gerraty "", /* .echoOn */ 339956e45f6SSimon J. Gerraty "", /* .noPrint */ 340956e45f6SSimon J. Gerraty 0, /* .noPrintLen */ 341b0c40a00SSimon J. Gerraty false, /* .hasErrCtl */ 34206b9b3e0SSimon J. Gerraty "", /* .errOn */ 34306b9b3e0SSimon J. Gerraty "", /* .errOff */ 34406b9b3e0SSimon J. Gerraty "echo \"%s\"\n", /* .echoTmpl */ 34506b9b3e0SSimon J. Gerraty "%s\n", /* .runIgnTmpl */ 34606b9b3e0SSimon J. Gerraty "{ %s \n} || exit $?\n", /* .runChkTmpl */ 347956e45f6SSimon J. Gerraty "'\n'", /* .newline */ 348956e45f6SSimon J. Gerraty '#', /* .commentChar */ 34906b9b3e0SSimon J. Gerraty "", /* .echoFlag */ 35006b9b3e0SSimon J. Gerraty "", /* .errFlag */ 3513955d011SMarcel Moolenaar }, 3523955d011SMarcel Moolenaar #endif /* DEFSHELL_CUSTOM */ 3533955d011SMarcel Moolenaar /* 3543955d011SMarcel Moolenaar * SH description. Echo control is also possible and, under 3553955d011SMarcel Moolenaar * sun UNIX anyway, one can even control error checking. 3563955d011SMarcel Moolenaar */ 3573955d011SMarcel Moolenaar { 358956e45f6SSimon J. Gerraty "sh", /* .name */ 359b0c40a00SSimon J. Gerraty false, /* .hasEchoCtl */ 360956e45f6SSimon J. Gerraty "", /* .echoOff */ 361956e45f6SSimon J. Gerraty "", /* .echoOn */ 362956e45f6SSimon J. Gerraty "", /* .noPrint */ 363956e45f6SSimon J. Gerraty 0, /* .noPrintLen */ 364b0c40a00SSimon J. Gerraty false, /* .hasErrCtl */ 36506b9b3e0SSimon J. Gerraty "", /* .errOn */ 36606b9b3e0SSimon J. Gerraty "", /* .errOff */ 36706b9b3e0SSimon J. Gerraty "echo \"%s\"\n", /* .echoTmpl */ 36806b9b3e0SSimon J. Gerraty "%s\n", /* .runIgnTmpl */ 36906b9b3e0SSimon J. Gerraty "{ %s \n} || exit $?\n", /* .runChkTmpl */ 370956e45f6SSimon J. Gerraty "'\n'", /* .newline */ 371956e45f6SSimon J. Gerraty '#', /* .commentChar*/ 3723955d011SMarcel Moolenaar #if defined(MAKE_NATIVE) && defined(__NetBSD__) 37306b9b3e0SSimon J. Gerraty /* XXX: -q is not really echoFlag, it's more like noEchoInSysFlag. */ 37406b9b3e0SSimon J. Gerraty "q", /* .echoFlag */ 3753955d011SMarcel Moolenaar #else 37606b9b3e0SSimon J. Gerraty "", /* .echoFlag */ 3773955d011SMarcel Moolenaar #endif 37806b9b3e0SSimon J. Gerraty "", /* .errFlag */ 3793955d011SMarcel Moolenaar }, 3803955d011SMarcel Moolenaar /* 3813955d011SMarcel Moolenaar * KSH description. 3823955d011SMarcel Moolenaar */ 3833955d011SMarcel Moolenaar { 384956e45f6SSimon J. Gerraty "ksh", /* .name */ 385b0c40a00SSimon J. Gerraty true, /* .hasEchoCtl */ 386956e45f6SSimon J. Gerraty "set +v", /* .echoOff */ 387956e45f6SSimon J. Gerraty "set -v", /* .echoOn */ 388956e45f6SSimon J. Gerraty "set +v", /* .noPrint */ 389956e45f6SSimon J. Gerraty 6, /* .noPrintLen */ 390b0c40a00SSimon J. Gerraty false, /* .hasErrCtl */ 39106b9b3e0SSimon J. Gerraty "", /* .errOn */ 39206b9b3e0SSimon J. Gerraty "", /* .errOff */ 39306b9b3e0SSimon J. Gerraty "echo \"%s\"\n", /* .echoTmpl */ 39406b9b3e0SSimon J. Gerraty "%s\n", /* .runIgnTmpl */ 39506b9b3e0SSimon J. Gerraty "{ %s \n} || exit $?\n", /* .runChkTmpl */ 396956e45f6SSimon J. Gerraty "'\n'", /* .newline */ 397956e45f6SSimon J. Gerraty '#', /* .commentChar */ 39806b9b3e0SSimon J. Gerraty "v", /* .echoFlag */ 39906b9b3e0SSimon J. Gerraty "", /* .errFlag */ 4003955d011SMarcel Moolenaar }, 4013955d011SMarcel Moolenaar /* 4023955d011SMarcel Moolenaar * CSH description. The csh can do echo control by playing 4033955d011SMarcel Moolenaar * with the setting of the 'echo' shell variable. Sadly, 4043955d011SMarcel Moolenaar * however, it is unable to do error control nicely. 4053955d011SMarcel Moolenaar */ 4063955d011SMarcel Moolenaar { 407956e45f6SSimon J. Gerraty "csh", /* .name */ 408b0c40a00SSimon J. Gerraty true, /* .hasEchoCtl */ 409956e45f6SSimon J. Gerraty "unset verbose", /* .echoOff */ 410956e45f6SSimon J. Gerraty "set verbose", /* .echoOn */ 411956e45f6SSimon J. Gerraty "unset verbose", /* .noPrint */ 412956e45f6SSimon J. Gerraty 13, /* .noPrintLen */ 413b0c40a00SSimon J. Gerraty false, /* .hasErrCtl */ 41406b9b3e0SSimon J. Gerraty "", /* .errOn */ 41506b9b3e0SSimon J. Gerraty "", /* .errOff */ 41606b9b3e0SSimon J. Gerraty "echo \"%s\"\n", /* .echoTmpl */ 41706b9b3e0SSimon J. Gerraty "csh -c \"%s || exit 0\"\n", /* .runIgnTmpl */ 41806b9b3e0SSimon J. Gerraty "", /* .runChkTmpl */ 419956e45f6SSimon J. Gerraty "'\\\n'", /* .newline */ 420956e45f6SSimon J. Gerraty '#', /* .commentChar */ 42106b9b3e0SSimon J. Gerraty "v", /* .echoFlag */ 42206b9b3e0SSimon J. Gerraty "e", /* .errFlag */ 4233955d011SMarcel Moolenaar } 4243955d011SMarcel Moolenaar }; 425956e45f6SSimon J. Gerraty 42606b9b3e0SSimon J. Gerraty /* 42706b9b3e0SSimon J. Gerraty * This is the shell to which we pass all commands in the Makefile. 42806b9b3e0SSimon J. Gerraty * It is set by the Job_ParseShell function. 42906b9b3e0SSimon J. Gerraty */ 43006b9b3e0SSimon J. Gerraty static Shell *shell = &shells[DEFSHELL_INDEX]; 431956e45f6SSimon J. Gerraty const char *shellPath = NULL; /* full pathname of executable image */ 432956e45f6SSimon J. Gerraty const char *shellName = NULL; /* last component of shellPath */ 43351ee2c1cSSimon J. Gerraty char *shellErrFlag = NULL; 434dba7b0efSSimon J. Gerraty static char *shell_freeIt = NULL; /* Allocated memory for custom .SHELL */ 4353955d011SMarcel Moolenaar 4363955d011SMarcel Moolenaar 437956e45f6SSimon J. Gerraty static Job *job_table; /* The structures that describe them */ 438956e45f6SSimon J. Gerraty static Job *job_table_end; /* job_table + maxJobs */ 439d5e0a182SSimon J. Gerraty static unsigned int wantToken; 440b0c40a00SSimon J. Gerraty static bool lurking_children = false; 441b0c40a00SSimon J. Gerraty static bool make_suspended = false; /* Whether we've seen a SIGTSTP (etc) */ 4423955d011SMarcel Moolenaar 4433955d011SMarcel Moolenaar /* 4443955d011SMarcel Moolenaar * Set of descriptors of pipes connected to 4453955d011SMarcel Moolenaar * the output channels of children 4463955d011SMarcel Moolenaar */ 4473955d011SMarcel Moolenaar static struct pollfd *fds = NULL; 448dba7b0efSSimon J. Gerraty static Job **jobByFdIndex = NULL; 449dba7b0efSSimon J. Gerraty static nfds_t fdsLen = 0; 4503955d011SMarcel Moolenaar static void watchfd(Job *); 4513955d011SMarcel Moolenaar static void clearfd(Job *); 452b0c40a00SSimon J. Gerraty static bool readyfd(Job *); 4533955d011SMarcel Moolenaar 45406b9b3e0SSimon J. Gerraty static char *targPrefix = NULL; /* To identify a job change in the output. */ 4553955d011SMarcel Moolenaar static Job tokenWaitJob; /* token wait pseudo-job */ 4563955d011SMarcel Moolenaar 4573955d011SMarcel Moolenaar static Job childExitJob; /* child exit pseudo-job */ 4583955d011SMarcel Moolenaar #define CHILD_EXIT "." 4593955d011SMarcel Moolenaar #define DO_JOB_RESUME "R" 4603955d011SMarcel Moolenaar 46106b9b3e0SSimon J. Gerraty enum { 46206b9b3e0SSimon J. Gerraty npseudojobs = 2 /* number of pseudo-jobs */ 46306b9b3e0SSimon J. Gerraty }; 4643955d011SMarcel Moolenaar 4653955d011SMarcel Moolenaar static sigset_t caught_signals; /* Set of signals we handle */ 466dba7b0efSSimon J. Gerraty static volatile sig_atomic_t caught_sigchld; 4673955d011SMarcel Moolenaar 468b0c40a00SSimon J. Gerraty static void CollectOutput(Job *, bool); 469b0c40a00SSimon J. Gerraty static void JobInterrupt(bool, int) MAKE_ATTR_DEAD; 4703955d011SMarcel Moolenaar static void JobRestartJobs(void); 4713955d011SMarcel Moolenaar static void JobSigReset(void); 4723955d011SMarcel Moolenaar 47306b9b3e0SSimon J. Gerraty static void 47406b9b3e0SSimon J. Gerraty SwitchOutputTo(GNode *gn) 47506b9b3e0SSimon J. Gerraty { 47606b9b3e0SSimon J. Gerraty /* The node for which output was most recently produced. */ 47706b9b3e0SSimon J. Gerraty static GNode *lastNode = NULL; 47806b9b3e0SSimon J. Gerraty 47906b9b3e0SSimon J. Gerraty if (gn == lastNode) 48006b9b3e0SSimon J. Gerraty return; 48106b9b3e0SSimon J. Gerraty lastNode = gn; 48206b9b3e0SSimon J. Gerraty 48306b9b3e0SSimon J. Gerraty if (opts.maxJobs != 1 && targPrefix != NULL && targPrefix[0] != '\0') 48406b9b3e0SSimon J. Gerraty (void)fprintf(stdout, "%s %s ---\n", targPrefix, gn->name); 48506b9b3e0SSimon J. Gerraty } 48606b9b3e0SSimon J. Gerraty 48749caa483SSimon J. Gerraty static unsigned 48849caa483SSimon J. Gerraty nfds_per_job(void) 48949caa483SSimon J. Gerraty { 49049caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 49149caa483SSimon J. Gerraty if (useMeta) 49249caa483SSimon J. Gerraty return 2; 49349caa483SSimon J. Gerraty #endif 49449caa483SSimon J. Gerraty return 1; 49549caa483SSimon J. Gerraty } 49649caa483SSimon J. Gerraty 49706b9b3e0SSimon J. Gerraty void 49806b9b3e0SSimon J. Gerraty Job_FlagsToString(const Job *job, char *buf, size_t bufsize) 49906b9b3e0SSimon J. Gerraty { 50006b9b3e0SSimon J. Gerraty snprintf(buf, bufsize, "%c%c%c", 50106b9b3e0SSimon J. Gerraty job->ignerr ? 'i' : '-', 50206b9b3e0SSimon J. Gerraty !job->echo ? 's' : '-', 50306b9b3e0SSimon J. Gerraty job->special ? 'S' : '-'); 50406b9b3e0SSimon J. Gerraty } 50506b9b3e0SSimon J. Gerraty 5063955d011SMarcel Moolenaar static void 507b0c40a00SSimon J. Gerraty DumpJobs(const char *where) 5083955d011SMarcel Moolenaar { 5093955d011SMarcel Moolenaar Job *job; 51006b9b3e0SSimon J. Gerraty char flags[4]; 5113955d011SMarcel Moolenaar 512956e45f6SSimon J. Gerraty debug_printf("job table @ %s\n", where); 5133955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 51406b9b3e0SSimon J. Gerraty Job_FlagsToString(job, flags, sizeof flags); 51506b9b3e0SSimon J. Gerraty debug_printf("job %d, status %d, flags %s, pid %d\n", 51606b9b3e0SSimon J. Gerraty (int)(job - job_table), job->status, flags, job->pid); 5173955d011SMarcel Moolenaar } 5183955d011SMarcel Moolenaar } 5193955d011SMarcel Moolenaar 5203955d011SMarcel Moolenaar /* 52145447996SSimon J. Gerraty * Delete the target of a failed, interrupted, or otherwise 52245447996SSimon J. Gerraty * unsuccessful job unless inhibited by .PRECIOUS. 52345447996SSimon J. Gerraty */ 52445447996SSimon J. Gerraty static void 52545447996SSimon J. Gerraty JobDeleteTarget(GNode *gn) 52645447996SSimon J. Gerraty { 527956e45f6SSimon J. Gerraty const char *file; 528956e45f6SSimon J. Gerraty 529956e45f6SSimon J. Gerraty if (gn->type & OP_JOIN) 530956e45f6SSimon J. Gerraty return; 531956e45f6SSimon J. Gerraty if (gn->type & OP_PHONY) 532956e45f6SSimon J. Gerraty return; 5339f45a3c8SSimon J. Gerraty if (GNode_IsPrecious(gn)) 534956e45f6SSimon J. Gerraty return; 535956e45f6SSimon J. Gerraty if (opts.noExecute) 536956e45f6SSimon J. Gerraty return; 537956e45f6SSimon J. Gerraty 538956e45f6SSimon J. Gerraty file = GNode_Path(gn); 5394fde40d9SSimon J. Gerraty if (unlink_file(file) == 0) 54045447996SSimon J. Gerraty Error("*** %s removed", file); 54145447996SSimon J. Gerraty } 54245447996SSimon J. Gerraty 54345447996SSimon J. Gerraty /* 5443955d011SMarcel Moolenaar * JobSigLock/JobSigUnlock 5453955d011SMarcel Moolenaar * 5463955d011SMarcel Moolenaar * Signal lock routines to get exclusive access. Currently used to 5473955d011SMarcel Moolenaar * protect `jobs' and `stoppedJobs' list manipulations. 5483955d011SMarcel Moolenaar */ 54906b9b3e0SSimon J. Gerraty static void 55006b9b3e0SSimon J. Gerraty JobSigLock(sigset_t *omaskp) 5513955d011SMarcel Moolenaar { 5523955d011SMarcel Moolenaar if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) { 5533955d011SMarcel Moolenaar Punt("JobSigLock: sigprocmask: %s", strerror(errno)); 5543955d011SMarcel Moolenaar sigemptyset(omaskp); 5553955d011SMarcel Moolenaar } 5563955d011SMarcel Moolenaar } 5573955d011SMarcel Moolenaar 55806b9b3e0SSimon J. Gerraty static void 55906b9b3e0SSimon J. Gerraty JobSigUnlock(sigset_t *omaskp) 5603955d011SMarcel Moolenaar { 5613955d011SMarcel Moolenaar (void)sigprocmask(SIG_SETMASK, omaskp, NULL); 5623955d011SMarcel Moolenaar } 5633955d011SMarcel Moolenaar 5643955d011SMarcel Moolenaar static void 5653955d011SMarcel Moolenaar JobCreatePipe(Job *job, int minfd) 5663955d011SMarcel Moolenaar { 567e1cee40dSSimon J. Gerraty int i, fd, flags; 568956e45f6SSimon J. Gerraty int pipe_fds[2]; 5693955d011SMarcel Moolenaar 570956e45f6SSimon J. Gerraty if (pipe(pipe_fds) == -1) 5713955d011SMarcel Moolenaar Punt("Cannot create pipe: %s", strerror(errno)); 5723955d011SMarcel Moolenaar 57374d2e02bSSimon J. Gerraty for (i = 0; i < 2; i++) { 574d5e0a182SSimon J. Gerraty /* Avoid using low-numbered fds */ 575956e45f6SSimon J. Gerraty fd = fcntl(pipe_fds[i], F_DUPFD, minfd); 57674d2e02bSSimon J. Gerraty if (fd != -1) { 577956e45f6SSimon J. Gerraty close(pipe_fds[i]); 578956e45f6SSimon J. Gerraty pipe_fds[i] = fd; 57974d2e02bSSimon J. Gerraty } 58074d2e02bSSimon J. Gerraty } 58174d2e02bSSimon J. Gerraty 582956e45f6SSimon J. Gerraty job->inPipe = pipe_fds[0]; 583956e45f6SSimon J. Gerraty job->outPipe = pipe_fds[1]; 584956e45f6SSimon J. Gerraty 585956e45f6SSimon J. Gerraty if (fcntl(job->inPipe, F_SETFD, FD_CLOEXEC) == -1) 586e1cee40dSSimon J. Gerraty Punt("Cannot set close-on-exec: %s", strerror(errno)); 587956e45f6SSimon J. Gerraty if (fcntl(job->outPipe, F_SETFD, FD_CLOEXEC) == -1) 588e1cee40dSSimon J. Gerraty Punt("Cannot set close-on-exec: %s", strerror(errno)); 5893955d011SMarcel Moolenaar 5903955d011SMarcel Moolenaar /* 5913955d011SMarcel Moolenaar * We mark the input side of the pipe non-blocking; we poll(2) the 5923955d011SMarcel Moolenaar * pipe when we're waiting for a job token, but we might lose the 5933955d011SMarcel Moolenaar * race for the token when a new one becomes available, so the read 5943955d011SMarcel Moolenaar * from the pipe should not block. 5953955d011SMarcel Moolenaar */ 596956e45f6SSimon J. Gerraty flags = fcntl(job->inPipe, F_GETFL, 0); 597e1cee40dSSimon J. Gerraty if (flags == -1) 598e1cee40dSSimon J. Gerraty Punt("Cannot get flags: %s", strerror(errno)); 599e1cee40dSSimon J. Gerraty flags |= O_NONBLOCK; 600956e45f6SSimon J. Gerraty if (fcntl(job->inPipe, F_SETFL, flags) == -1) 601e1cee40dSSimon J. Gerraty Punt("Cannot set flags: %s", strerror(errno)); 6023955d011SMarcel Moolenaar } 6033955d011SMarcel Moolenaar 604956e45f6SSimon J. Gerraty /* Pass the signal to each running job. */ 6053955d011SMarcel Moolenaar static void 6063955d011SMarcel Moolenaar JobCondPassSig(int signo) 6073955d011SMarcel Moolenaar { 6083955d011SMarcel Moolenaar Job *job; 6093955d011SMarcel Moolenaar 610956e45f6SSimon J. Gerraty DEBUG1(JOB, "JobCondPassSig(%d) called.\n", signo); 6113955d011SMarcel Moolenaar 6123955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 613e2eeea75SSimon J. Gerraty if (job->status != JOB_ST_RUNNING) 6143955d011SMarcel Moolenaar continue; 615956e45f6SSimon J. Gerraty DEBUG2(JOB, "JobCondPassSig passing signal %d to child %d.\n", 6163955d011SMarcel Moolenaar signo, job->pid); 6173955d011SMarcel Moolenaar KILLPG(job->pid, signo); 6183955d011SMarcel Moolenaar } 6193955d011SMarcel Moolenaar } 6203955d011SMarcel Moolenaar 62106b9b3e0SSimon J. Gerraty /* 62206b9b3e0SSimon J. Gerraty * SIGCHLD handler. 6233955d011SMarcel Moolenaar * 62406b9b3e0SSimon J. Gerraty * Sends a token on the child exit pipe to wake us up from select()/poll(). 62506b9b3e0SSimon J. Gerraty */ 62606b9b3e0SSimon J. Gerraty /*ARGSUSED*/ 6273955d011SMarcel Moolenaar static void 6283955d011SMarcel Moolenaar JobChildSig(int signo MAKE_ATTR_UNUSED) 6293955d011SMarcel Moolenaar { 630dba7b0efSSimon J. Gerraty caught_sigchld = 1; 63106b9b3e0SSimon J. Gerraty while (write(childExitJob.outPipe, CHILD_EXIT, 1) == -1 && 63206b9b3e0SSimon J. Gerraty errno == EAGAIN) 6333cbdda60SSimon J. Gerraty continue; 6343955d011SMarcel Moolenaar } 6353955d011SMarcel Moolenaar 6363955d011SMarcel Moolenaar 637956e45f6SSimon J. Gerraty /* Resume all stopped jobs. */ 63806b9b3e0SSimon J. Gerraty /*ARGSUSED*/ 6393955d011SMarcel Moolenaar static void 6403955d011SMarcel Moolenaar JobContinueSig(int signo MAKE_ATTR_UNUSED) 6413955d011SMarcel Moolenaar { 6423955d011SMarcel Moolenaar /* 643956e45f6SSimon J. Gerraty * Defer sending SIGCONT to our stopped children until we return 6443955d011SMarcel Moolenaar * from the signal handler. 6453955d011SMarcel Moolenaar */ 6463cbdda60SSimon J. Gerraty while (write(childExitJob.outPipe, DO_JOB_RESUME, 1) == -1 && 6473cbdda60SSimon J. Gerraty errno == EAGAIN) 6483cbdda60SSimon J. Gerraty continue; 6493955d011SMarcel Moolenaar } 6503955d011SMarcel Moolenaar 65106b9b3e0SSimon J. Gerraty /* 65206b9b3e0SSimon J. Gerraty * Pass a signal on to all jobs, then resend to ourselves. 65306b9b3e0SSimon J. Gerraty * We die by the same signal. 65406b9b3e0SSimon J. Gerraty */ 6553955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void 6563955d011SMarcel Moolenaar JobPassSig_int(int signo) 6573955d011SMarcel Moolenaar { 6583955d011SMarcel Moolenaar /* Run .INTERRUPT target then exit */ 659b0c40a00SSimon J. Gerraty JobInterrupt(true, signo); 6603955d011SMarcel Moolenaar } 6613955d011SMarcel Moolenaar 66206b9b3e0SSimon J. Gerraty /* 66306b9b3e0SSimon J. Gerraty * Pass a signal on to all jobs, then resend to ourselves. 66406b9b3e0SSimon J. Gerraty * We die by the same signal. 66506b9b3e0SSimon J. Gerraty */ 6663955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void 6673955d011SMarcel Moolenaar JobPassSig_term(int signo) 6683955d011SMarcel Moolenaar { 6693955d011SMarcel Moolenaar /* Dont run .INTERRUPT target then exit */ 670b0c40a00SSimon J. Gerraty JobInterrupt(false, signo); 6713955d011SMarcel Moolenaar } 6723955d011SMarcel Moolenaar 6733955d011SMarcel Moolenaar static void 6743955d011SMarcel Moolenaar JobPassSig_suspend(int signo) 6753955d011SMarcel Moolenaar { 6763955d011SMarcel Moolenaar sigset_t nmask, omask; 6773955d011SMarcel Moolenaar struct sigaction act; 6783955d011SMarcel Moolenaar 6793955d011SMarcel Moolenaar /* Suppress job started/continued messages */ 680b0c40a00SSimon J. Gerraty make_suspended = true; 6813955d011SMarcel Moolenaar 6823955d011SMarcel Moolenaar /* Pass the signal onto every job */ 6833955d011SMarcel Moolenaar JobCondPassSig(signo); 6843955d011SMarcel Moolenaar 6853955d011SMarcel Moolenaar /* 68606b9b3e0SSimon J. Gerraty * Send ourselves the signal now we've given the message to everyone 68706b9b3e0SSimon J. Gerraty * else. Note we block everything else possible while we're getting 68806b9b3e0SSimon J. Gerraty * the signal. This ensures that all our jobs get continued when we 68906b9b3e0SSimon J. Gerraty * wake up before we take any other signal. 6903955d011SMarcel Moolenaar */ 6913955d011SMarcel Moolenaar sigfillset(&nmask); 6923955d011SMarcel Moolenaar sigdelset(&nmask, signo); 6933955d011SMarcel Moolenaar (void)sigprocmask(SIG_SETMASK, &nmask, &omask); 6943955d011SMarcel Moolenaar 6953955d011SMarcel Moolenaar act.sa_handler = SIG_DFL; 6963955d011SMarcel Moolenaar sigemptyset(&act.sa_mask); 6973955d011SMarcel Moolenaar act.sa_flags = 0; 6983955d011SMarcel Moolenaar (void)sigaction(signo, &act, NULL); 6993955d011SMarcel Moolenaar 70006b9b3e0SSimon J. Gerraty DEBUG1(JOB, "JobPassSig passing signal %d to self.\n", signo); 7013955d011SMarcel Moolenaar 7023955d011SMarcel Moolenaar (void)kill(getpid(), signo); 7033955d011SMarcel Moolenaar 7043955d011SMarcel Moolenaar /* 7053955d011SMarcel Moolenaar * We've been continued. 7063955d011SMarcel Moolenaar * 707d5e0a182SSimon J. Gerraty * A whole host of signals is going to happen! 7083955d011SMarcel Moolenaar * SIGCHLD for any processes that actually suspended themselves. 709d5e0a182SSimon J. Gerraty * SIGCHLD for any processes that exited while we were asleep. 7103955d011SMarcel Moolenaar * The SIGCONT that actually caused us to wake up. 7113955d011SMarcel Moolenaar * 7123955d011SMarcel Moolenaar * Since we defer passing the SIGCONT on to our children until 7133955d011SMarcel Moolenaar * the main processing loop, we can be sure that all the SIGCHLD 7143955d011SMarcel Moolenaar * events will have happened by then - and that the waitpid() will 7153955d011SMarcel Moolenaar * collect the child 'suspended' events. 7163955d011SMarcel Moolenaar * For correct sequencing we just need to ensure we process the 717956e45f6SSimon J. Gerraty * waitpid() before passing on the SIGCONT. 7183955d011SMarcel Moolenaar * 7193955d011SMarcel Moolenaar * In any case nothing else is needed here. 7203955d011SMarcel Moolenaar */ 7213955d011SMarcel Moolenaar 7223955d011SMarcel Moolenaar /* Restore handler and signal mask */ 7233955d011SMarcel Moolenaar act.sa_handler = JobPassSig_suspend; 7243955d011SMarcel Moolenaar (void)sigaction(signo, &act, NULL); 7253955d011SMarcel Moolenaar (void)sigprocmask(SIG_SETMASK, &omask, NULL); 7263955d011SMarcel Moolenaar } 7273955d011SMarcel Moolenaar 7283955d011SMarcel Moolenaar static Job * 729b0c40a00SSimon J. Gerraty JobFindPid(int pid, JobStatus status, bool isJobs) 7303955d011SMarcel Moolenaar { 7313955d011SMarcel Moolenaar Job *job; 7323955d011SMarcel Moolenaar 7333955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 734e2eeea75SSimon J. Gerraty if (job->status == status && job->pid == pid) 7353955d011SMarcel Moolenaar return job; 7363955d011SMarcel Moolenaar } 7373955d011SMarcel Moolenaar if (DEBUG(JOB) && isJobs) 738b0c40a00SSimon J. Gerraty DumpJobs("no pid"); 7393955d011SMarcel Moolenaar return NULL; 7403955d011SMarcel Moolenaar } 7413955d011SMarcel Moolenaar 742956e45f6SSimon J. Gerraty /* Parse leading '@', '-' and '+', which control the exact execution mode. */ 743956e45f6SSimon J. Gerraty static void 74406b9b3e0SSimon J. Gerraty ParseCommandFlags(char **pp, CommandFlags *out_cmdFlags) 745956e45f6SSimon J. Gerraty { 746956e45f6SSimon J. Gerraty char *p = *pp; 747b0c40a00SSimon J. Gerraty out_cmdFlags->echo = true; 748b0c40a00SSimon J. Gerraty out_cmdFlags->ignerr = false; 749b0c40a00SSimon J. Gerraty out_cmdFlags->always = false; 750956e45f6SSimon J. Gerraty 751956e45f6SSimon J. Gerraty for (;;) { 752956e45f6SSimon J. Gerraty if (*p == '@') 75306b9b3e0SSimon J. Gerraty out_cmdFlags->echo = DEBUG(LOUD); 754956e45f6SSimon J. Gerraty else if (*p == '-') 755b0c40a00SSimon J. Gerraty out_cmdFlags->ignerr = true; 756956e45f6SSimon J. Gerraty else if (*p == '+') 757b0c40a00SSimon J. Gerraty out_cmdFlags->always = true; 7584fde40d9SSimon J. Gerraty else if (!ch_isspace(*p)) 759d5e0a182SSimon J. Gerraty /* Ignore whitespace for compatibility with GNU make */ 760956e45f6SSimon J. Gerraty break; 761956e45f6SSimon J. Gerraty p++; 762956e45f6SSimon J. Gerraty } 763956e45f6SSimon J. Gerraty 764956e45f6SSimon J. Gerraty pp_skip_whitespace(&p); 765956e45f6SSimon J. Gerraty 766956e45f6SSimon J. Gerraty *pp = p; 767956e45f6SSimon J. Gerraty } 768956e45f6SSimon J. Gerraty 769956e45f6SSimon J. Gerraty /* Escape a string for a double-quoted string literal in sh, csh and ksh. */ 770956e45f6SSimon J. Gerraty static char * 771956e45f6SSimon J. Gerraty EscapeShellDblQuot(const char *cmd) 772956e45f6SSimon J. Gerraty { 773956e45f6SSimon J. Gerraty size_t i, j; 774956e45f6SSimon J. Gerraty 775956e45f6SSimon J. Gerraty /* Worst that could happen is every char needs escaping. */ 776956e45f6SSimon J. Gerraty char *esc = bmake_malloc(strlen(cmd) * 2 + 1); 777956e45f6SSimon J. Gerraty for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) { 77806b9b3e0SSimon J. Gerraty if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' || 77906b9b3e0SSimon J. Gerraty cmd[i] == '"') 780956e45f6SSimon J. Gerraty esc[j++] = '\\'; 781956e45f6SSimon J. Gerraty esc[j] = cmd[i]; 782956e45f6SSimon J. Gerraty } 783956e45f6SSimon J. Gerraty esc[j] = '\0'; 784956e45f6SSimon J. Gerraty 785956e45f6SSimon J. Gerraty return esc; 786956e45f6SSimon J. Gerraty } 787956e45f6SSimon J. Gerraty 788e2eeea75SSimon J. Gerraty static void 789b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(ShellWriter *wr, const char *fmt, const char *arg) 790e2eeea75SSimon J. Gerraty { 79106b9b3e0SSimon J. Gerraty DEBUG1(JOB, fmt, arg); 792e2eeea75SSimon J. Gerraty 79306b9b3e0SSimon J. Gerraty (void)fprintf(wr->f, fmt, arg); 79412904384SSimon J. Gerraty if (wr->f == stdout) 79506b9b3e0SSimon J. Gerraty (void)fflush(wr->f); 796e2eeea75SSimon J. Gerraty } 797e2eeea75SSimon J. Gerraty 798e2eeea75SSimon J. Gerraty static void 799b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(ShellWriter *wr, const char *line) 800e2eeea75SSimon J. Gerraty { 801b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(wr, "%s\n", line); 802e2eeea75SSimon J. Gerraty } 803e2eeea75SSimon J. Gerraty 80406b9b3e0SSimon J. Gerraty static void 80506b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(ShellWriter *wr) 80606b9b3e0SSimon J. Gerraty { 80706b9b3e0SSimon J. Gerraty if (shell->hasEchoCtl) 808b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(wr, shell->echoOff); 80906b9b3e0SSimon J. Gerraty } 81006b9b3e0SSimon J. Gerraty 81106b9b3e0SSimon J. Gerraty static void 81206b9b3e0SSimon J. Gerraty ShellWriter_EchoCmd(ShellWriter *wr, const char *escCmd) 81306b9b3e0SSimon J. Gerraty { 814b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(wr, shell->echoTmpl, escCmd); 81506b9b3e0SSimon J. Gerraty } 81606b9b3e0SSimon J. Gerraty 81706b9b3e0SSimon J. Gerraty static void 81806b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(ShellWriter *wr) 81906b9b3e0SSimon J. Gerraty { 82006b9b3e0SSimon J. Gerraty if (shell->hasEchoCtl) 821b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(wr, shell->echoOn); 82206b9b3e0SSimon J. Gerraty } 82306b9b3e0SSimon J. Gerraty 82406b9b3e0SSimon J. Gerraty static void 82506b9b3e0SSimon J. Gerraty ShellWriter_TraceOn(ShellWriter *wr) 82606b9b3e0SSimon J. Gerraty { 82706b9b3e0SSimon J. Gerraty if (!wr->xtraced) { 828b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(wr, "set -x"); 829b0c40a00SSimon J. Gerraty wr->xtraced = true; 83006b9b3e0SSimon J. Gerraty } 83106b9b3e0SSimon J. Gerraty } 83206b9b3e0SSimon J. Gerraty 83306b9b3e0SSimon J. Gerraty static void 834b0c40a00SSimon J. Gerraty ShellWriter_ErrOff(ShellWriter *wr, bool echo) 83506b9b3e0SSimon J. Gerraty { 83606b9b3e0SSimon J. Gerraty if (echo) 83706b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(wr); 838b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(wr, shell->errOff); 83906b9b3e0SSimon J. Gerraty if (echo) 84006b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(wr); 84106b9b3e0SSimon J. Gerraty } 84206b9b3e0SSimon J. Gerraty 84306b9b3e0SSimon J. Gerraty static void 844b0c40a00SSimon J. Gerraty ShellWriter_ErrOn(ShellWriter *wr, bool echo) 84506b9b3e0SSimon J. Gerraty { 84606b9b3e0SSimon J. Gerraty if (echo) 84706b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(wr); 848b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(wr, shell->errOn); 84906b9b3e0SSimon J. Gerraty if (echo) 85006b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(wr); 85106b9b3e0SSimon J. Gerraty } 85206b9b3e0SSimon J. Gerraty 85306b9b3e0SSimon J. Gerraty /* 85406b9b3e0SSimon J. Gerraty * The shell has no built-in error control, so emulate error control by 85506b9b3e0SSimon J. Gerraty * enclosing each shell command in a template like "{ %s \n } || exit $?" 85606b9b3e0SSimon J. Gerraty * (configurable per shell). 8573955d011SMarcel Moolenaar */ 858956e45f6SSimon J. Gerraty static void 859b0c40a00SSimon J. Gerraty JobWriteSpecialsEchoCtl(Job *job, ShellWriter *wr, CommandFlags *inout_cmdFlags, 86006b9b3e0SSimon J. Gerraty const char *escCmd, const char **inout_cmdTemplate) 8613955d011SMarcel Moolenaar { 8621d3f2ddcSSimon J. Gerraty /* XXX: Why is the whole job modified at this point? */ 863b0c40a00SSimon J. Gerraty job->ignerr = true; 8643955d011SMarcel Moolenaar 86506b9b3e0SSimon J. Gerraty if (job->echo && inout_cmdFlags->echo) { 86606b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(wr); 86706b9b3e0SSimon J. Gerraty ShellWriter_EchoCmd(wr, escCmd); 8683955d011SMarcel Moolenaar 86906b9b3e0SSimon J. Gerraty /* 87006b9b3e0SSimon J. Gerraty * Leave echoing off so the user doesn't see the commands 87106b9b3e0SSimon J. Gerraty * for toggling the error checking. 87206b9b3e0SSimon J. Gerraty */ 873b0c40a00SSimon J. Gerraty inout_cmdFlags->echo = false; 87406b9b3e0SSimon J. Gerraty } 87506b9b3e0SSimon J. Gerraty *inout_cmdTemplate = shell->runIgnTmpl; 8763955d011SMarcel Moolenaar 87706b9b3e0SSimon J. Gerraty /* 87806b9b3e0SSimon J. Gerraty * The template runIgnTmpl already takes care of ignoring errors, 87906b9b3e0SSimon J. Gerraty * so pretend error checking is still on. 88006b9b3e0SSimon J. Gerraty * XXX: What effects does this have, and why is it necessary? 88106b9b3e0SSimon J. Gerraty */ 882b0c40a00SSimon J. Gerraty inout_cmdFlags->ignerr = false; 88306b9b3e0SSimon J. Gerraty } 88406b9b3e0SSimon J. Gerraty 88506b9b3e0SSimon J. Gerraty static void 886b0c40a00SSimon J. Gerraty JobWriteSpecials(Job *job, ShellWriter *wr, const char *escCmd, bool run, 88706b9b3e0SSimon J. Gerraty CommandFlags *inout_cmdFlags, const char **inout_cmdTemplate) 88806b9b3e0SSimon J. Gerraty { 889d5e0a182SSimon J. Gerraty if (!run) 890b0c40a00SSimon J. Gerraty inout_cmdFlags->ignerr = false; 891d5e0a182SSimon J. Gerraty else if (shell->hasErrCtl) 89206b9b3e0SSimon J. Gerraty ShellWriter_ErrOff(wr, job->echo && inout_cmdFlags->echo); 89306b9b3e0SSimon J. Gerraty else if (shell->runIgnTmpl != NULL && shell->runIgnTmpl[0] != '\0') { 894b0c40a00SSimon J. Gerraty JobWriteSpecialsEchoCtl(job, wr, inout_cmdFlags, escCmd, 89506b9b3e0SSimon J. Gerraty inout_cmdTemplate); 89606b9b3e0SSimon J. Gerraty } else 897b0c40a00SSimon J. Gerraty inout_cmdFlags->ignerr = false; 89806b9b3e0SSimon J. Gerraty } 89906b9b3e0SSimon J. Gerraty 90006b9b3e0SSimon J. Gerraty /* 901b0c40a00SSimon J. Gerraty * Write a shell command to the job's commands file, to be run later. 90206b9b3e0SSimon J. Gerraty * 90306b9b3e0SSimon J. Gerraty * If the command starts with '@' and neither the -s nor the -n flag was 904b0c40a00SSimon J. Gerraty * given to make, stick a shell-specific echoOff command in the script. 90506b9b3e0SSimon J. Gerraty * 90606b9b3e0SSimon J. Gerraty * If the command starts with '-' and the shell has no error control (none 907b0c40a00SSimon J. Gerraty * of the predefined shells has that), ignore errors for the entire job. 90806b9b3e0SSimon J. Gerraty * 909b0c40a00SSimon J. Gerraty * XXX: Why ignore errors for the entire job? This is even documented in the 910b0c40a00SSimon J. Gerraty * manual page, but without any rationale since there is no known rationale. 911b0c40a00SSimon J. Gerraty * 912b0c40a00SSimon J. Gerraty * XXX: The manual page says the '-' "affects the entire job", but that's not 913b0c40a00SSimon J. Gerraty * accurate. The '-' does not affect the commands before the '-'. 914b0c40a00SSimon J. Gerraty * 915b0c40a00SSimon J. Gerraty * If the command is just "...", skip all further commands of this job. These 916b0c40a00SSimon J. Gerraty * commands are attached to the .END node instead and will be run by 917b0c40a00SSimon J. Gerraty * Job_Finish after all other targets have been made. 91806b9b3e0SSimon J. Gerraty */ 91906b9b3e0SSimon J. Gerraty static void 920b0c40a00SSimon J. Gerraty JobWriteCommand(Job *job, ShellWriter *wr, StringListNode *ln, const char *ucmd) 92106b9b3e0SSimon J. Gerraty { 922b0c40a00SSimon J. Gerraty bool run; 92306b9b3e0SSimon J. Gerraty 92406b9b3e0SSimon J. Gerraty CommandFlags cmdFlags; 925b0c40a00SSimon J. Gerraty /* Template for writing a command to the shell file */ 92606b9b3e0SSimon J. Gerraty const char *cmdTemplate; 92706b9b3e0SSimon J. Gerraty char *xcmd; /* The expanded command */ 92806b9b3e0SSimon J. Gerraty char *xcmdStart; 92906b9b3e0SSimon J. Gerraty char *escCmd; /* xcmd escaped to be used in double quotes */ 93006b9b3e0SSimon J. Gerraty 93106b9b3e0SSimon J. Gerraty run = GNode_ShouldExecute(job->node); 93206b9b3e0SSimon J. Gerraty 933548bfc56SSimon J. Gerraty EvalStack_Push(job->node->name, NULL, NULL); 9348c973ee2SSimon J. Gerraty xcmd = Var_Subst(ucmd, job->node, VARE_WANTRES); 935548bfc56SSimon J. Gerraty EvalStack_Pop(); 936956e45f6SSimon J. Gerraty /* TODO: handle errors */ 93706b9b3e0SSimon J. Gerraty xcmdStart = xcmd; 9383955d011SMarcel Moolenaar 9393955d011SMarcel Moolenaar cmdTemplate = "%s\n"; 9403955d011SMarcel Moolenaar 94106b9b3e0SSimon J. Gerraty ParseCommandFlags(&xcmd, &cmdFlags); 942956e45f6SSimon J. Gerraty 94306b9b3e0SSimon J. Gerraty /* The '+' command flag overrides the -n or -N options. */ 94406b9b3e0SSimon J. Gerraty if (cmdFlags.always && !run) { 9453955d011SMarcel Moolenaar /* 9463955d011SMarcel Moolenaar * We're not actually executing anything... 9473955d011SMarcel Moolenaar * but this one needs to be - use compat mode just for it. 9483955d011SMarcel Moolenaar */ 9499f45a3c8SSimon J. Gerraty (void)Compat_RunCommand(ucmd, job->node, ln); 95006b9b3e0SSimon J. Gerraty free(xcmdStart); 951956e45f6SSimon J. Gerraty return; 9523955d011SMarcel Moolenaar } 9533955d011SMarcel Moolenaar 9543955d011SMarcel Moolenaar /* 95506b9b3e0SSimon J. Gerraty * If the shell doesn't have error control, the alternate echoing 95606b9b3e0SSimon J. Gerraty * will be done (to avoid showing additional error checking code) 95706b9b3e0SSimon J. Gerraty * and this needs some characters escaped. 9583955d011SMarcel Moolenaar */ 95906b9b3e0SSimon J. Gerraty escCmd = shell->hasErrCtl ? NULL : EscapeShellDblQuot(xcmd); 9603955d011SMarcel Moolenaar 96106b9b3e0SSimon J. Gerraty if (!cmdFlags.echo) { 962d5e0a182SSimon J. Gerraty if (job->echo && run && shell->hasEchoCtl) 96306b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(wr); 964d5e0a182SSimon J. Gerraty else if (shell->hasErrCtl) 965b0c40a00SSimon J. Gerraty cmdFlags.echo = true; 9663955d011SMarcel Moolenaar } 9673955d011SMarcel Moolenaar 96806b9b3e0SSimon J. Gerraty if (cmdFlags.ignerr) { 969b0c40a00SSimon J. Gerraty JobWriteSpecials(job, wr, escCmd, run, &cmdFlags, &cmdTemplate); 9703955d011SMarcel Moolenaar } else { 9713955d011SMarcel Moolenaar 9723955d011SMarcel Moolenaar /* 97306b9b3e0SSimon J. Gerraty * If errors are being checked and the shell doesn't have 97406b9b3e0SSimon J. Gerraty * error control but does supply an runChkTmpl template, then 97506b9b3e0SSimon J. Gerraty * set up commands to run through it. 9763955d011SMarcel Moolenaar */ 9773955d011SMarcel Moolenaar 97806b9b3e0SSimon J. Gerraty if (!shell->hasErrCtl && shell->runChkTmpl != NULL && 97906b9b3e0SSimon J. Gerraty shell->runChkTmpl[0] != '\0') { 98006b9b3e0SSimon J. Gerraty if (job->echo && cmdFlags.echo) { 98106b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(wr); 98206b9b3e0SSimon J. Gerraty ShellWriter_EchoCmd(wr, escCmd); 983b0c40a00SSimon J. Gerraty cmdFlags.echo = false; 9843955d011SMarcel Moolenaar } 98506b9b3e0SSimon J. Gerraty /* 98606b9b3e0SSimon J. Gerraty * If it's a comment line or blank, avoid the possible 98706b9b3e0SSimon J. Gerraty * syntax error generated by "{\n} || exit $?". 98806b9b3e0SSimon J. Gerraty */ 98906b9b3e0SSimon J. Gerraty cmdTemplate = escCmd[0] == shell->commentChar || 99006b9b3e0SSimon J. Gerraty escCmd[0] == '\0' 99106b9b3e0SSimon J. Gerraty ? shell->runIgnTmpl 99206b9b3e0SSimon J. Gerraty : shell->runChkTmpl; 993b0c40a00SSimon J. Gerraty cmdFlags.ignerr = false; 9943955d011SMarcel Moolenaar } 9953955d011SMarcel Moolenaar } 9963955d011SMarcel Moolenaar 99706b9b3e0SSimon J. Gerraty if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0) 99806b9b3e0SSimon J. Gerraty ShellWriter_TraceOn(wr); 9993955d011SMarcel Moolenaar 1000b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(wr, cmdTemplate, xcmd); 100106b9b3e0SSimon J. Gerraty free(xcmdStart); 10023955d011SMarcel Moolenaar free(escCmd); 100306b9b3e0SSimon J. Gerraty 100406b9b3e0SSimon J. Gerraty if (cmdFlags.ignerr) 100506b9b3e0SSimon J. Gerraty ShellWriter_ErrOn(wr, cmdFlags.echo && job->echo); 100606b9b3e0SSimon J. Gerraty 100706b9b3e0SSimon J. Gerraty if (!cmdFlags.echo) 100806b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(wr); 10093955d011SMarcel Moolenaar } 10103955d011SMarcel Moolenaar 101106b9b3e0SSimon J. Gerraty /* 1012b0c40a00SSimon J. Gerraty * Write all commands to the shell file that is later executed. 10133955d011SMarcel Moolenaar * 1014b0c40a00SSimon J. Gerraty * The special command "..." stops writing and saves the remaining commands 1015dba7b0efSSimon J. Gerraty * to be executed later, when the target '.END' is made. 101606b9b3e0SSimon J. Gerraty * 101706b9b3e0SSimon J. Gerraty * Return whether at least one command was written to the shell file. 101806b9b3e0SSimon J. Gerraty */ 1019b0c40a00SSimon J. Gerraty static bool 1020b0c40a00SSimon J. Gerraty JobWriteCommands(Job *job) 10213955d011SMarcel Moolenaar { 1022956e45f6SSimon J. Gerraty StringListNode *ln; 1023b0c40a00SSimon J. Gerraty bool seen = false; 1024b0c40a00SSimon J. Gerraty ShellWriter wr; 1025b0c40a00SSimon J. Gerraty 1026b0c40a00SSimon J. Gerraty wr.f = job->cmdFILE; 1027b0c40a00SSimon J. Gerraty wr.xtraced = false; 1028956e45f6SSimon J. Gerraty 102906b9b3e0SSimon J. Gerraty for (ln = job->node->commands.first; ln != NULL; ln = ln->next) { 1030956e45f6SSimon J. Gerraty const char *cmd = ln->datum; 1031956e45f6SSimon J. Gerraty 1032956e45f6SSimon J. Gerraty if (strcmp(cmd, "...") == 0) { 1033956e45f6SSimon J. Gerraty job->node->type |= OP_SAVE_CMDS; 1034956e45f6SSimon J. Gerraty job->tailCmds = ln->next; 1035956e45f6SSimon J. Gerraty break; 1036956e45f6SSimon J. Gerraty } 1037e2eeea75SSimon J. Gerraty 1038b0c40a00SSimon J. Gerraty JobWriteCommand(job, &wr, ln, ln->datum); 1039b0c40a00SSimon J. Gerraty seen = true; 1040956e45f6SSimon J. Gerraty } 104106b9b3e0SSimon J. Gerraty 104206b9b3e0SSimon J. Gerraty return seen; 1043956e45f6SSimon J. Gerraty } 1044956e45f6SSimon J. Gerraty 1045dba7b0efSSimon J. Gerraty /* 1046dba7b0efSSimon J. Gerraty * Save the delayed commands (those after '...'), to be executed later in 1047dba7b0efSSimon J. Gerraty * the '.END' node, when everything else is done. 1048dba7b0efSSimon J. Gerraty */ 1049956e45f6SSimon J. Gerraty static void 1050956e45f6SSimon J. Gerraty JobSaveCommands(Job *job) 1051956e45f6SSimon J. Gerraty { 105206b9b3e0SSimon J. Gerraty StringListNode *ln; 1053956e45f6SSimon J. Gerraty 105406b9b3e0SSimon J. Gerraty for (ln = job->tailCmds; ln != NULL; ln = ln->next) { 105506b9b3e0SSimon J. Gerraty const char *cmd = ln->datum; 1056956e45f6SSimon J. Gerraty char *expanded_cmd; 1057dba7b0efSSimon J. Gerraty /* 1058dba7b0efSSimon J. Gerraty * XXX: This Var_Subst is only intended to expand the dynamic 1059956e45f6SSimon J. Gerraty * variables such as .TARGET, .IMPSRC. It is not intended to 1060dba7b0efSSimon J. Gerraty * expand the other variables as well; see deptgt-end.mk. 1061dba7b0efSSimon J. Gerraty */ 1062548bfc56SSimon J. Gerraty EvalStack_Push(job->node->name, NULL, NULL); 10638c973ee2SSimon J. Gerraty expanded_cmd = Var_Subst(cmd, job->node, VARE_WANTRES); 1064548bfc56SSimon J. Gerraty EvalStack_Pop(); 1065956e45f6SSimon J. Gerraty /* TODO: handle errors */ 106606b9b3e0SSimon J. Gerraty Lst_Append(&Targ_GetEndNode()->commands, expanded_cmd); 1067956e45f6SSimon J. Gerraty } 10683955d011SMarcel Moolenaar } 10693955d011SMarcel Moolenaar 10703955d011SMarcel Moolenaar 1071956e45f6SSimon J. Gerraty /* Called to close both input and output pipes when a job is finished. */ 10723955d011SMarcel Moolenaar static void 1073e2eeea75SSimon J. Gerraty JobClosePipes(Job *job) 10743955d011SMarcel Moolenaar { 10753955d011SMarcel Moolenaar clearfd(job); 10763955d011SMarcel Moolenaar (void)close(job->outPipe); 10773955d011SMarcel Moolenaar job->outPipe = -1; 10783955d011SMarcel Moolenaar 1079b0c40a00SSimon J. Gerraty CollectOutput(job, true); 10803955d011SMarcel Moolenaar (void)close(job->inPipe); 10813955d011SMarcel Moolenaar job->inPipe = -1; 10823955d011SMarcel Moolenaar } 10833955d011SMarcel Moolenaar 108406b9b3e0SSimon J. Gerraty static void 1085b0c40a00SSimon J. Gerraty DebugFailedJob(const Job *job) 1086b0c40a00SSimon J. Gerraty { 1087b0c40a00SSimon J. Gerraty const StringListNode *ln; 1088b0c40a00SSimon J. Gerraty 1089b0c40a00SSimon J. Gerraty if (!DEBUG(ERROR)) 1090b0c40a00SSimon J. Gerraty return; 1091b0c40a00SSimon J. Gerraty 109212904384SSimon J. Gerraty debug_printf("\n"); 109312904384SSimon J. Gerraty debug_printf("*** Failed target: %s\n", job->node->name); 1094548bfc56SSimon J. Gerraty debug_printf("*** In directory: %s\n", curdir); 109512904384SSimon J. Gerraty debug_printf("*** Failed commands:\n"); 109612904384SSimon J. Gerraty for (ln = job->node->commands.first; ln != NULL; ln = ln->next) { 109712904384SSimon J. Gerraty const char *cmd = ln->datum; 109812904384SSimon J. Gerraty debug_printf("\t%s\n", cmd); 109912904384SSimon J. Gerraty 110012904384SSimon J. Gerraty if (strchr(cmd, '$') != NULL) { 11018c973ee2SSimon J. Gerraty char *xcmd = Var_Subst(cmd, job->node, VARE_WANTRES); 110212904384SSimon J. Gerraty debug_printf("\t=> %s\n", xcmd); 110312904384SSimon J. Gerraty free(xcmd); 110412904384SSimon J. Gerraty } 110512904384SSimon J. Gerraty } 1106b0c40a00SSimon J. Gerraty } 1107b0c40a00SSimon J. Gerraty 1108b0c40a00SSimon J. Gerraty static void 110906b9b3e0SSimon J. Gerraty JobFinishDoneExitedError(Job *job, WAIT_T *inout_status) 111006b9b3e0SSimon J. Gerraty { 111106b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 111206b9b3e0SSimon J. Gerraty #ifdef USE_META 111306b9b3e0SSimon J. Gerraty if (useMeta) { 111406b9b3e0SSimon J. Gerraty meta_job_error(job, job->node, 111506b9b3e0SSimon J. Gerraty job->ignerr, WEXITSTATUS(*inout_status)); 111606b9b3e0SSimon J. Gerraty } 111706b9b3e0SSimon J. Gerraty #endif 111806b9b3e0SSimon J. Gerraty if (!shouldDieQuietly(job->node, -1)) { 1119b0c40a00SSimon J. Gerraty DebugFailedJob(job); 112006b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Error code %d%s\n", 112106b9b3e0SSimon J. Gerraty job->node->name, WEXITSTATUS(*inout_status), 112206b9b3e0SSimon J. Gerraty job->ignerr ? " (ignored)" : ""); 112306b9b3e0SSimon J. Gerraty } 112406b9b3e0SSimon J. Gerraty 112506b9b3e0SSimon J. Gerraty if (job->ignerr) 112606b9b3e0SSimon J. Gerraty WAIT_STATUS(*inout_status) = 0; 112706b9b3e0SSimon J. Gerraty else { 112806b9b3e0SSimon J. Gerraty if (deleteOnError) 112906b9b3e0SSimon J. Gerraty JobDeleteTarget(job->node); 11309f45a3c8SSimon J. Gerraty PrintOnError(job->node, "\n"); 113106b9b3e0SSimon J. Gerraty } 113206b9b3e0SSimon J. Gerraty } 113306b9b3e0SSimon J. Gerraty 113406b9b3e0SSimon J. Gerraty static void 113506b9b3e0SSimon J. Gerraty JobFinishDoneExited(Job *job, WAIT_T *inout_status) 113606b9b3e0SSimon J. Gerraty { 113706b9b3e0SSimon J. Gerraty DEBUG2(JOB, "Process %d [%s] exited.\n", job->pid, job->node->name); 113806b9b3e0SSimon J. Gerraty 113906b9b3e0SSimon J. Gerraty if (WEXITSTATUS(*inout_status) != 0) 114006b9b3e0SSimon J. Gerraty JobFinishDoneExitedError(job, inout_status); 114106b9b3e0SSimon J. Gerraty else if (DEBUG(JOB)) { 114206b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 114306b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Completed successfully\n", 114406b9b3e0SSimon J. Gerraty job->node->name); 114506b9b3e0SSimon J. Gerraty } 114606b9b3e0SSimon J. Gerraty } 114706b9b3e0SSimon J. Gerraty 114806b9b3e0SSimon J. Gerraty static void 114906b9b3e0SSimon J. Gerraty JobFinishDoneSignaled(Job *job, WAIT_T status) 115006b9b3e0SSimon J. Gerraty { 115106b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 1152b0c40a00SSimon J. Gerraty DebugFailedJob(job); 115306b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Signal %d\n", job->node->name, WTERMSIG(status)); 115406b9b3e0SSimon J. Gerraty if (deleteOnError) 115506b9b3e0SSimon J. Gerraty JobDeleteTarget(job->node); 115606b9b3e0SSimon J. Gerraty } 115706b9b3e0SSimon J. Gerraty 115806b9b3e0SSimon J. Gerraty static void 115906b9b3e0SSimon J. Gerraty JobFinishDone(Job *job, WAIT_T *inout_status) 116006b9b3e0SSimon J. Gerraty { 116106b9b3e0SSimon J. Gerraty if (WIFEXITED(*inout_status)) 116206b9b3e0SSimon J. Gerraty JobFinishDoneExited(job, inout_status); 116306b9b3e0SSimon J. Gerraty else 116406b9b3e0SSimon J. Gerraty JobFinishDoneSignaled(job, *inout_status); 116506b9b3e0SSimon J. Gerraty 116606b9b3e0SSimon J. Gerraty (void)fflush(stdout); 116706b9b3e0SSimon J. Gerraty } 116806b9b3e0SSimon J. Gerraty 116906b9b3e0SSimon J. Gerraty /* 117006b9b3e0SSimon J. Gerraty * Do final processing for the given job including updating parent nodes and 1171e2eeea75SSimon J. Gerraty * starting new jobs as available/necessary. 1172e2eeea75SSimon J. Gerraty * 1173e2eeea75SSimon J. Gerraty * Deferred commands for the job are placed on the .END node. 1174e2eeea75SSimon J. Gerraty * 117506b9b3e0SSimon J. Gerraty * If there was a serious error (job_errors != 0; not an ignored one), no more 1176e2eeea75SSimon J. Gerraty * jobs will be started. 11773955d011SMarcel Moolenaar * 11783955d011SMarcel Moolenaar * Input: 11793955d011SMarcel Moolenaar * job job to finish 11803955d011SMarcel Moolenaar * status sub-why job went away 11813955d011SMarcel Moolenaar */ 11823955d011SMarcel Moolenaar static void 11833955d011SMarcel Moolenaar JobFinish (Job *job, WAIT_T status) 11843955d011SMarcel Moolenaar { 1185b0c40a00SSimon J. Gerraty bool done, return_job_token; 11863955d011SMarcel Moolenaar 1187956e45f6SSimon J. Gerraty DEBUG3(JOB, "JobFinish: %d [%s], status %d\n", 11883955d011SMarcel Moolenaar job->pid, job->node->name, status); 11893955d011SMarcel Moolenaar 11903955d011SMarcel Moolenaar if ((WIFEXITED(status) && 119106b9b3e0SSimon J. Gerraty ((WEXITSTATUS(status) != 0 && !job->ignerr))) || 119206b9b3e0SSimon J. Gerraty WIFSIGNALED(status)) { 119306b9b3e0SSimon J. Gerraty /* Finished because of an error. */ 119406b9b3e0SSimon J. Gerraty 1195e2eeea75SSimon J. Gerraty JobClosePipes(job); 11963955d011SMarcel Moolenaar if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 119712904384SSimon J. Gerraty if (fclose(job->cmdFILE) != 0) 119812904384SSimon J. Gerraty Punt("Cannot write shell script for '%s': %s", 119912904384SSimon J. Gerraty job->node->name, strerror(errno)); 12003955d011SMarcel Moolenaar job->cmdFILE = NULL; 12013955d011SMarcel Moolenaar } 1202b0c40a00SSimon J. Gerraty done = true; 120306b9b3e0SSimon J. Gerraty 12043955d011SMarcel Moolenaar } else if (WIFEXITED(status)) { 12053955d011SMarcel Moolenaar /* 120606b9b3e0SSimon J. Gerraty * Deal with ignored errors in -B mode. We need to print a 120706b9b3e0SSimon J. Gerraty * message telling of the ignored error as well as to run 120806b9b3e0SSimon J. Gerraty * the next command. 12093955d011SMarcel Moolenaar */ 12103955d011SMarcel Moolenaar done = WEXITSTATUS(status) != 0; 121106b9b3e0SSimon J. Gerraty 1212e2eeea75SSimon J. Gerraty JobClosePipes(job); 121306b9b3e0SSimon J. Gerraty 12143955d011SMarcel Moolenaar } else { 121506b9b3e0SSimon J. Gerraty /* No need to close things down or anything. */ 1216b0c40a00SSimon J. Gerraty done = false; 12173955d011SMarcel Moolenaar } 12183955d011SMarcel Moolenaar 121906b9b3e0SSimon J. Gerraty if (done) 122006b9b3e0SSimon J. Gerraty JobFinishDone(job, &status); 12213955d011SMarcel Moolenaar 12223955d011SMarcel Moolenaar #ifdef USE_META 12233955d011SMarcel Moolenaar if (useMeta) { 1224e2eeea75SSimon J. Gerraty int meta_status = meta_job_finish(job); 1225e2eeea75SSimon J. Gerraty if (meta_status != 0 && status == 0) 1226e2eeea75SSimon J. Gerraty status = meta_status; 12273955d011SMarcel Moolenaar } 12283955d011SMarcel Moolenaar #endif 12293955d011SMarcel Moolenaar 1230b0c40a00SSimon J. Gerraty return_job_token = false; 12313955d011SMarcel Moolenaar 12323955d011SMarcel Moolenaar Trace_Log(JOBEND, job); 123306b9b3e0SSimon J. Gerraty if (!job->special) { 1234e2eeea75SSimon J. Gerraty if (WAIT_STATUS(status) != 0 || 1235e2eeea75SSimon J. Gerraty (aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT) 1236b0c40a00SSimon J. Gerraty return_job_token = true; 12373955d011SMarcel Moolenaar } 12383955d011SMarcel Moolenaar 1239e2eeea75SSimon J. Gerraty if (aborting != ABORT_ERROR && aborting != ABORT_INTERRUPT && 12403955d011SMarcel Moolenaar (WAIT_STATUS(status) == 0)) { 12413955d011SMarcel Moolenaar /* 124206b9b3e0SSimon J. Gerraty * As long as we aren't aborting and the job didn't return a 124306b9b3e0SSimon J. Gerraty * non-zero status that we shouldn't ignore, we call 124406b9b3e0SSimon J. Gerraty * Make_Update to update the parents. 12453955d011SMarcel Moolenaar */ 1246956e45f6SSimon J. Gerraty JobSaveCommands(job); 12473955d011SMarcel Moolenaar job->node->made = MADE; 124806b9b3e0SSimon J. Gerraty if (!job->special) 1249b0c40a00SSimon J. Gerraty return_job_token = true; 12503955d011SMarcel Moolenaar Make_Update(job->node); 1251e2eeea75SSimon J. Gerraty job->status = JOB_ST_FREE; 125206b9b3e0SSimon J. Gerraty } else if (status != 0) { 125306b9b3e0SSimon J. Gerraty job_errors++; 1254e2eeea75SSimon J. Gerraty job->status = JOB_ST_FREE; 12553955d011SMarcel Moolenaar } 12563955d011SMarcel Moolenaar 125706b9b3e0SSimon J. Gerraty if (job_errors > 0 && !opts.keepgoing && aborting != ABORT_INTERRUPT) { 125806b9b3e0SSimon J. Gerraty /* Prevent more jobs from getting started. */ 125906b9b3e0SSimon J. Gerraty aborting = ABORT_ERROR; 126006b9b3e0SSimon J. Gerraty } 12613955d011SMarcel Moolenaar 12623955d011SMarcel Moolenaar if (return_job_token) 12633955d011SMarcel Moolenaar Job_TokenReturn(); 12643955d011SMarcel Moolenaar 1265e2eeea75SSimon J. Gerraty if (aborting == ABORT_ERROR && jobTokensRunning == 0) 126606b9b3e0SSimon J. Gerraty Finish(job_errors); 12673955d011SMarcel Moolenaar } 1268e2eeea75SSimon J. Gerraty 1269e2eeea75SSimon J. Gerraty static void 1270e2eeea75SSimon J. Gerraty TouchRegular(GNode *gn) 1271e2eeea75SSimon J. Gerraty { 1272e2eeea75SSimon J. Gerraty const char *file = GNode_Path(gn); 1273b0c40a00SSimon J. Gerraty struct utimbuf times; 1274e2eeea75SSimon J. Gerraty int fd; 1275e2eeea75SSimon J. Gerraty char c; 1276e2eeea75SSimon J. Gerraty 1277b0c40a00SSimon J. Gerraty times.actime = now; 1278b0c40a00SSimon J. Gerraty times.modtime = now; 1279e2eeea75SSimon J. Gerraty if (utime(file, ×) >= 0) 1280e2eeea75SSimon J. Gerraty return; 1281e2eeea75SSimon J. Gerraty 1282e2eeea75SSimon J. Gerraty fd = open(file, O_RDWR | O_CREAT, 0666); 1283e2eeea75SSimon J. Gerraty if (fd < 0) { 1284e2eeea75SSimon J. Gerraty (void)fprintf(stderr, "*** couldn't touch %s: %s\n", 1285e2eeea75SSimon J. Gerraty file, strerror(errno)); 1286e2eeea75SSimon J. Gerraty (void)fflush(stderr); 1287e2eeea75SSimon J. Gerraty return; /* XXX: What about propagating the error? */ 1288e2eeea75SSimon J. Gerraty } 1289e2eeea75SSimon J. Gerraty 12909f45a3c8SSimon J. Gerraty /* 12919f45a3c8SSimon J. Gerraty * Last resort: update the file's time stamps in the traditional way. 1292e2eeea75SSimon J. Gerraty * XXX: This doesn't work for empty files, which are sometimes used 12939f45a3c8SSimon J. Gerraty * as marker files. 12949f45a3c8SSimon J. Gerraty */ 1295e2eeea75SSimon J. Gerraty if (read(fd, &c, 1) == 1) { 1296e2eeea75SSimon J. Gerraty (void)lseek(fd, 0, SEEK_SET); 1297e2eeea75SSimon J. Gerraty while (write(fd, &c, 1) == -1 && errno == EAGAIN) 1298e2eeea75SSimon J. Gerraty continue; 1299e2eeea75SSimon J. Gerraty } 1300e2eeea75SSimon J. Gerraty (void)close(fd); /* XXX: What about propagating the error? */ 13013955d011SMarcel Moolenaar } 13023955d011SMarcel Moolenaar 130306b9b3e0SSimon J. Gerraty /* 130406b9b3e0SSimon J. Gerraty * Touch the given target. Called by JobStart when the -t flag was given. 13053955d011SMarcel Moolenaar * 1306956e45f6SSimon J. Gerraty * The modification date of the file is changed. 130706b9b3e0SSimon J. Gerraty * If the file did not exist, it is created. 130806b9b3e0SSimon J. Gerraty */ 13093955d011SMarcel Moolenaar void 1310b0c40a00SSimon J. Gerraty Job_Touch(GNode *gn, bool echo) 13113955d011SMarcel Moolenaar { 131206b9b3e0SSimon J. Gerraty if (gn->type & 131306b9b3e0SSimon J. Gerraty (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC | OP_OPTIONAL | 13143955d011SMarcel Moolenaar OP_SPECIAL | OP_PHONY)) { 131506b9b3e0SSimon J. Gerraty /* 131606b9b3e0SSimon J. Gerraty * These are "virtual" targets and should not really be 131706b9b3e0SSimon J. Gerraty * created. 131806b9b3e0SSimon J. Gerraty */ 13193955d011SMarcel Moolenaar return; 13203955d011SMarcel Moolenaar } 13213955d011SMarcel Moolenaar 132206b9b3e0SSimon J. Gerraty if (echo || !GNode_ShouldExecute(gn)) { 13233955d011SMarcel Moolenaar (void)fprintf(stdout, "touch %s\n", gn->name); 13243955d011SMarcel Moolenaar (void)fflush(stdout); 13253955d011SMarcel Moolenaar } 13263955d011SMarcel Moolenaar 1327e2eeea75SSimon J. Gerraty if (!GNode_ShouldExecute(gn)) 13283955d011SMarcel Moolenaar return; 13293955d011SMarcel Moolenaar 133006b9b3e0SSimon J. Gerraty if (gn->type & OP_ARCHV) 13313955d011SMarcel Moolenaar Arch_Touch(gn); 133206b9b3e0SSimon J. Gerraty else if (gn->type & OP_LIB) 13333955d011SMarcel Moolenaar Arch_TouchLib(gn); 133406b9b3e0SSimon J. Gerraty else 1335e2eeea75SSimon J. Gerraty TouchRegular(gn); 13363955d011SMarcel Moolenaar } 13373955d011SMarcel Moolenaar 133806b9b3e0SSimon J. Gerraty /* 133906b9b3e0SSimon J. Gerraty * Make sure the given node has all the commands it needs. 1340956e45f6SSimon J. Gerraty * 1341956e45f6SSimon J. Gerraty * The node will have commands from the .DEFAULT rule added to it if it 1342956e45f6SSimon J. Gerraty * needs them. 13433955d011SMarcel Moolenaar * 13443955d011SMarcel Moolenaar * Input: 13453955d011SMarcel Moolenaar * gn The target whose commands need verifying 13463955d011SMarcel Moolenaar * abortProc Function to abort with message 13473955d011SMarcel Moolenaar * 13483955d011SMarcel Moolenaar * Results: 1349b0c40a00SSimon J. Gerraty * true if the commands list is/was ok. 13503955d011SMarcel Moolenaar */ 1351b0c40a00SSimon J. Gerraty bool 13523955d011SMarcel Moolenaar Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...)) 13533955d011SMarcel Moolenaar { 1354956e45f6SSimon J. Gerraty if (GNode_IsTarget(gn)) 1355b0c40a00SSimon J. Gerraty return true; 135606b9b3e0SSimon J. Gerraty if (!Lst_IsEmpty(&gn->commands)) 1357b0c40a00SSimon J. Gerraty return true; 135806b9b3e0SSimon J. Gerraty if ((gn->type & OP_LIB) && !Lst_IsEmpty(&gn->children)) 1359b0c40a00SSimon J. Gerraty return true; 1360956e45f6SSimon J. Gerraty 13613955d011SMarcel Moolenaar /* 13623955d011SMarcel Moolenaar * No commands. Look for .DEFAULT rule from which we might infer 1363e2eeea75SSimon J. Gerraty * commands. 13643955d011SMarcel Moolenaar */ 136506b9b3e0SSimon J. Gerraty if (defaultNode != NULL && !Lst_IsEmpty(&defaultNode->commands) && 1366e2eeea75SSimon J. Gerraty !(gn->type & OP_SPECIAL)) { 13673955d011SMarcel Moolenaar /* 136806b9b3e0SSimon J. Gerraty * The traditional Make only looks for a .DEFAULT if the node 136906b9b3e0SSimon J. Gerraty * was never the target of an operator, so that's what we do 137006b9b3e0SSimon J. Gerraty * too. 1371e2eeea75SSimon J. Gerraty * 1372e2eeea75SSimon J. Gerraty * The .DEFAULT node acts like a transformation rule, in that 13733955d011SMarcel Moolenaar * gn also inherits any attributes or sources attached to 13743955d011SMarcel Moolenaar * .DEFAULT itself. 13753955d011SMarcel Moolenaar */ 1376e2eeea75SSimon J. Gerraty Make_HandleUse(defaultNode, gn); 1377dba7b0efSSimon J. Gerraty Var_Set(gn, IMPSRC, GNode_VarTarget(gn)); 1378b0c40a00SSimon J. Gerraty return true; 1379956e45f6SSimon J. Gerraty } 1380956e45f6SSimon J. Gerraty 1381b0c40a00SSimon J. Gerraty Dir_UpdateMTime(gn, false); 1382e2eeea75SSimon J. Gerraty if (gn->mtime != 0 || (gn->type & OP_SPECIAL)) 1383b0c40a00SSimon J. Gerraty return true; 1384956e45f6SSimon J. Gerraty 13853955d011SMarcel Moolenaar /* 1386956e45f6SSimon J. Gerraty * The node wasn't the target of an operator. We have no .DEFAULT 13873955d011SMarcel Moolenaar * rule to go on and the target doesn't already exist. There's 13883955d011SMarcel Moolenaar * nothing more we can do for this branch. If the -k flag wasn't 13893955d011SMarcel Moolenaar * given, we stop in our tracks, otherwise we just don't update 13903955d011SMarcel Moolenaar * this node's parents so they never get examined. 13913955d011SMarcel Moolenaar */ 13923955d011SMarcel Moolenaar 139312904384SSimon J. Gerraty if (gn->flags.fromDepend) { 13941748de26SSimon J. Gerraty if (!Job_RunTarget(".STALE", gn->fname)) 139506b9b3e0SSimon J. Gerraty fprintf(stdout, 13969f45a3c8SSimon J. Gerraty "%s: %s, %u: ignoring stale %s for %s\n", 13971748de26SSimon J. Gerraty progname, gn->fname, gn->lineno, makeDependfile, 13981748de26SSimon J. Gerraty gn->name); 1399b0c40a00SSimon J. Gerraty return true; 14003955d011SMarcel Moolenaar } 14013955d011SMarcel Moolenaar 14023955d011SMarcel Moolenaar if (gn->type & OP_OPTIONAL) { 1403956e45f6SSimon J. Gerraty (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n", 1404956e45f6SSimon J. Gerraty progname, gn->name, "ignored"); 14053955d011SMarcel Moolenaar (void)fflush(stdout); 1406b0c40a00SSimon J. Gerraty return true; 14073955d011SMarcel Moolenaar } 14083955d011SMarcel Moolenaar 1409956e45f6SSimon J. Gerraty if (opts.keepgoing) { 1410956e45f6SSimon J. Gerraty (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n", 1411956e45f6SSimon J. Gerraty progname, gn->name, "continuing"); 1412956e45f6SSimon J. Gerraty (void)fflush(stdout); 1413b0c40a00SSimon J. Gerraty return false; 1414956e45f6SSimon J. Gerraty } 1415956e45f6SSimon J. Gerraty 1416d5e0a182SSimon J. Gerraty abortProc("don't know how to make %s. Stop", gn->name); 1417b0c40a00SSimon J. Gerraty return false; 1418956e45f6SSimon J. Gerraty } 1419956e45f6SSimon J. Gerraty 142006b9b3e0SSimon J. Gerraty /* 142106b9b3e0SSimon J. Gerraty * Execute the shell for the given job. 14223955d011SMarcel Moolenaar * 142306b9b3e0SSimon J. Gerraty * See Job_CatchOutput for handling the output of the shell. 142406b9b3e0SSimon J. Gerraty */ 14253955d011SMarcel Moolenaar static void 14263955d011SMarcel Moolenaar JobExec(Job *job, char **argv) 14273955d011SMarcel Moolenaar { 14283955d011SMarcel Moolenaar int cpid; /* ID of new child */ 14293955d011SMarcel Moolenaar sigset_t mask; 14303955d011SMarcel Moolenaar 14313955d011SMarcel Moolenaar if (DEBUG(JOB)) { 14323955d011SMarcel Moolenaar int i; 14333955d011SMarcel Moolenaar 1434e2eeea75SSimon J. Gerraty debug_printf("Running %s\n", job->node->name); 1435956e45f6SSimon J. Gerraty debug_printf("\tCommand: "); 14363955d011SMarcel Moolenaar for (i = 0; argv[i] != NULL; i++) { 1437956e45f6SSimon J. Gerraty debug_printf("%s ", argv[i]); 14383955d011SMarcel Moolenaar } 1439956e45f6SSimon J. Gerraty debug_printf("\n"); 14403955d011SMarcel Moolenaar } 14413955d011SMarcel Moolenaar 14423955d011SMarcel Moolenaar /* 1443d5e0a182SSimon J. Gerraty * Some jobs produce no output, and it's disconcerting to have 14443955d011SMarcel Moolenaar * no feedback of their running (since they produce no output, the 14453955d011SMarcel Moolenaar * banner with their name in it never appears). This is an attempt to 14463955d011SMarcel Moolenaar * provide that feedback, even if nothing follows it. 14473955d011SMarcel Moolenaar */ 144806b9b3e0SSimon J. Gerraty if (job->echo) 144906b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 14503955d011SMarcel Moolenaar 14513955d011SMarcel Moolenaar /* No interruptions until this job is on the `jobs' list */ 14523955d011SMarcel Moolenaar JobSigLock(&mask); 14533955d011SMarcel Moolenaar 14543955d011SMarcel Moolenaar /* Pre-emptively mark job running, pid still zero though */ 1455e2eeea75SSimon J. Gerraty job->status = JOB_ST_RUNNING; 14563955d011SMarcel Moolenaar 1457c59c3bf3SSimon J. Gerraty Var_ReexportVars(job->node); 145806b9b3e0SSimon J. Gerraty 1459dba7b0efSSimon J. Gerraty cpid = vfork(); 14603955d011SMarcel Moolenaar if (cpid == -1) 14613955d011SMarcel Moolenaar Punt("Cannot vfork: %s", strerror(errno)); 14623955d011SMarcel Moolenaar 14633955d011SMarcel Moolenaar if (cpid == 0) { 14643955d011SMarcel Moolenaar /* Child */ 14653955d011SMarcel Moolenaar sigset_t tmask; 14663955d011SMarcel Moolenaar 14673955d011SMarcel Moolenaar #ifdef USE_META 14689f45a3c8SSimon J. Gerraty if (useMeta) 14693955d011SMarcel Moolenaar meta_job_child(job); 14703955d011SMarcel Moolenaar #endif 14713955d011SMarcel Moolenaar /* 147206b9b3e0SSimon J. Gerraty * Reset all signal handlers; this is necessary because we 147306b9b3e0SSimon J. Gerraty * also need to unblock signals before we exec(2). 14743955d011SMarcel Moolenaar */ 14753955d011SMarcel Moolenaar JobSigReset(); 14763955d011SMarcel Moolenaar 14773955d011SMarcel Moolenaar /* Now unblock signals */ 14783955d011SMarcel Moolenaar sigemptyset(&tmask); 14793955d011SMarcel Moolenaar JobSigUnlock(&tmask); 14803955d011SMarcel Moolenaar 14813955d011SMarcel Moolenaar /* 148206b9b3e0SSimon J. Gerraty * Must duplicate the input stream down to the child's input 148306b9b3e0SSimon J. Gerraty * and reset it to the beginning (again). Since the stream 148406b9b3e0SSimon J. Gerraty * was marked close-on-exec, we must clear that bit in the 148506b9b3e0SSimon J. Gerraty * new input. 14863955d011SMarcel Moolenaar */ 1487956e45f6SSimon J. Gerraty if (dup2(fileno(job->cmdFILE), 0) == -1) 1488956e45f6SSimon J. Gerraty execDie("dup2", "job->cmdFILE"); 1489956e45f6SSimon J. Gerraty if (fcntl(0, F_SETFD, 0) == -1) 1490956e45f6SSimon J. Gerraty execDie("fcntl clear close-on-exec", "stdin"); 1491e2eeea75SSimon J. Gerraty if (lseek(0, 0, SEEK_SET) == -1) 1492956e45f6SSimon J. Gerraty execDie("lseek to 0", "stdin"); 14933955d011SMarcel Moolenaar 1494db29cad8SSimon J. Gerraty if (Always_pass_job_queue || 1495db29cad8SSimon J. Gerraty (job->node->type & (OP_MAKE | OP_SUBMAKE))) { 1496d5e0a182SSimon J. Gerraty /* Pass job token pipe to submakes. */ 1497956e45f6SSimon J. Gerraty if (fcntl(tokenWaitJob.inPipe, F_SETFD, 0) == -1) 149806b9b3e0SSimon J. Gerraty execDie("clear close-on-exec", 149906b9b3e0SSimon J. Gerraty "tokenWaitJob.inPipe"); 1500956e45f6SSimon J. Gerraty if (fcntl(tokenWaitJob.outPipe, F_SETFD, 0) == -1) 150106b9b3e0SSimon J. Gerraty execDie("clear close-on-exec", 150206b9b3e0SSimon J. Gerraty "tokenWaitJob.outPipe"); 15033955d011SMarcel Moolenaar } 15043955d011SMarcel Moolenaar 15053955d011SMarcel Moolenaar /* 15063955d011SMarcel Moolenaar * Set up the child's output to be routed through the pipe 15073955d011SMarcel Moolenaar * we've created for it. 15083955d011SMarcel Moolenaar */ 1509956e45f6SSimon J. Gerraty if (dup2(job->outPipe, 1) == -1) 1510956e45f6SSimon J. Gerraty execDie("dup2", "job->outPipe"); 1511956e45f6SSimon J. Gerraty 15123955d011SMarcel Moolenaar /* 151306b9b3e0SSimon J. Gerraty * The output channels are marked close on exec. This bit 151406b9b3e0SSimon J. Gerraty * was duplicated by the dup2(on some systems), so we have 151506b9b3e0SSimon J. Gerraty * to clear it before routing the shell's error output to 151606b9b3e0SSimon J. Gerraty * the same place as its standard output. 15173955d011SMarcel Moolenaar */ 1518956e45f6SSimon J. Gerraty if (fcntl(1, F_SETFD, 0) == -1) 1519956e45f6SSimon J. Gerraty execDie("clear close-on-exec", "stdout"); 1520956e45f6SSimon J. Gerraty if (dup2(1, 2) == -1) 1521956e45f6SSimon J. Gerraty execDie("dup2", "1, 2"); 15223955d011SMarcel Moolenaar 15233955d011SMarcel Moolenaar /* 152406b9b3e0SSimon J. Gerraty * We want to switch the child into a different process 152506b9b3e0SSimon J. Gerraty * family so we can kill it and all its descendants in 152606b9b3e0SSimon J. Gerraty * one fell swoop, by killing its process family, but not 152706b9b3e0SSimon J. Gerraty * commit suicide. 15283955d011SMarcel Moolenaar */ 15293955d011SMarcel Moolenaar #if defined(HAVE_SETPGID) 15303955d011SMarcel Moolenaar (void)setpgid(0, getpid()); 15313955d011SMarcel Moolenaar #else 15323955d011SMarcel Moolenaar # if defined(HAVE_SETSID) 15333955d011SMarcel Moolenaar /* XXX: dsl - I'm sure this should be setpgrp()... */ 15343955d011SMarcel Moolenaar (void)setsid(); 15353955d011SMarcel Moolenaar # else 15363955d011SMarcel Moolenaar (void)setpgrp(0, getpid()); 15373955d011SMarcel Moolenaar # endif 15383955d011SMarcel Moolenaar #endif 15393955d011SMarcel Moolenaar 15403955d011SMarcel Moolenaar (void)execv(shellPath, argv); 1541956e45f6SSimon J. Gerraty execDie("exec", shellPath); 15423955d011SMarcel Moolenaar } 15433955d011SMarcel Moolenaar 15443955d011SMarcel Moolenaar /* Parent, continuing after the child exec */ 15453955d011SMarcel Moolenaar job->pid = cpid; 15463955d011SMarcel Moolenaar 15473955d011SMarcel Moolenaar Trace_Log(JOBSTART, job); 15483955d011SMarcel Moolenaar 154949caa483SSimon J. Gerraty #ifdef USE_META 15509f45a3c8SSimon J. Gerraty if (useMeta) 155149caa483SSimon J. Gerraty meta_job_parent(job, cpid); 155249caa483SSimon J. Gerraty #endif 155349caa483SSimon J. Gerraty 15543955d011SMarcel Moolenaar /* 15553955d011SMarcel Moolenaar * Set the current position in the buffer to the beginning 15563955d011SMarcel Moolenaar * and mark another stream to watch in the outputs mask 15573955d011SMarcel Moolenaar */ 15583955d011SMarcel Moolenaar job->curPos = 0; 15593955d011SMarcel Moolenaar 15603955d011SMarcel Moolenaar watchfd(job); 15613955d011SMarcel Moolenaar 15623955d011SMarcel Moolenaar if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 156312904384SSimon J. Gerraty if (fclose(job->cmdFILE) != 0) 156412904384SSimon J. Gerraty Punt("Cannot write shell script for '%s': %s", 156512904384SSimon J. Gerraty job->node->name, strerror(errno)); 15663955d011SMarcel Moolenaar job->cmdFILE = NULL; 15673955d011SMarcel Moolenaar } 15683955d011SMarcel Moolenaar 1569dba7b0efSSimon J. Gerraty /* Now that the job is actually running, add it to the table. */ 15703955d011SMarcel Moolenaar if (DEBUG(JOB)) { 1571956e45f6SSimon J. Gerraty debug_printf("JobExec(%s): pid %d added to jobs table\n", 15723955d011SMarcel Moolenaar job->node->name, job->pid); 1573b0c40a00SSimon J. Gerraty DumpJobs("job started"); 15743955d011SMarcel Moolenaar } 15753955d011SMarcel Moolenaar JobSigUnlock(&mask); 15763955d011SMarcel Moolenaar } 15773955d011SMarcel Moolenaar 1578956e45f6SSimon J. Gerraty /* Create the argv needed to execute the shell for a given job. */ 15793955d011SMarcel Moolenaar static void 15803955d011SMarcel Moolenaar JobMakeArgv(Job *job, char **argv) 15813955d011SMarcel Moolenaar { 15823955d011SMarcel Moolenaar int argc; 15833955d011SMarcel Moolenaar static char args[10]; /* For merged arguments */ 15843955d011SMarcel Moolenaar 15853955d011SMarcel Moolenaar argv[0] = UNCONST(shellName); 15863955d011SMarcel Moolenaar argc = 1; 15873955d011SMarcel Moolenaar 158806b9b3e0SSimon J. Gerraty if ((shell->errFlag != NULL && shell->errFlag[0] != '-') || 158906b9b3e0SSimon J. Gerraty (shell->echoFlag != NULL && shell->echoFlag[0] != '-')) { 15903955d011SMarcel Moolenaar /* 159106b9b3e0SSimon J. Gerraty * At least one of the flags doesn't have a minus before it, 159206b9b3e0SSimon J. Gerraty * so merge them together. Have to do this because the Bourne 159306b9b3e0SSimon J. Gerraty * shell thinks its second argument is a file to source. 159406b9b3e0SSimon J. Gerraty * Grrrr. Note the ten-character limitation on the combined 159506b9b3e0SSimon J. Gerraty * arguments. 159606b9b3e0SSimon J. Gerraty * 159706b9b3e0SSimon J. Gerraty * TODO: Research until when the above comments were 159806b9b3e0SSimon J. Gerraty * practically relevant. 15993955d011SMarcel Moolenaar */ 1600e2eeea75SSimon J. Gerraty (void)snprintf(args, sizeof args, "-%s%s", 160106b9b3e0SSimon J. Gerraty (job->ignerr ? "" : 160206b9b3e0SSimon J. Gerraty (shell->errFlag != NULL ? shell->errFlag : "")), 160306b9b3e0SSimon J. Gerraty (!job->echo ? "" : 160406b9b3e0SSimon J. Gerraty (shell->echoFlag != NULL ? shell->echoFlag : ""))); 16053955d011SMarcel Moolenaar 160606b9b3e0SSimon J. Gerraty if (args[1] != '\0') { 16073955d011SMarcel Moolenaar argv[argc] = args; 16083955d011SMarcel Moolenaar argc++; 16093955d011SMarcel Moolenaar } 16103955d011SMarcel Moolenaar } else { 161106b9b3e0SSimon J. Gerraty if (!job->ignerr && shell->errFlag != NULL) { 161206b9b3e0SSimon J. Gerraty argv[argc] = UNCONST(shell->errFlag); 16133955d011SMarcel Moolenaar argc++; 16143955d011SMarcel Moolenaar } 161506b9b3e0SSimon J. Gerraty if (job->echo && shell->echoFlag != NULL) { 161606b9b3e0SSimon J. Gerraty argv[argc] = UNCONST(shell->echoFlag); 16173955d011SMarcel Moolenaar argc++; 16183955d011SMarcel Moolenaar } 16193955d011SMarcel Moolenaar } 16203955d011SMarcel Moolenaar argv[argc] = NULL; 16213955d011SMarcel Moolenaar } 16223955d011SMarcel Moolenaar 162306b9b3e0SSimon J. Gerraty static void 1624b0c40a00SSimon J. Gerraty JobWriteShellCommands(Job *job, GNode *gn, bool *out_run) 16253955d011SMarcel Moolenaar { 16263955d011SMarcel Moolenaar /* 162706b9b3e0SSimon J. Gerraty * tfile is the name of a file into which all shell commands 162806b9b3e0SSimon J. Gerraty * are put. It is removed before the child shell is executed, 162906b9b3e0SSimon J. Gerraty * unless DEBUG(SCRIPT) is set. 16303955d011SMarcel Moolenaar */ 1631dba7b0efSSimon J. Gerraty char tfile[MAXPATHLEN]; 163206b9b3e0SSimon J. Gerraty int tfd; /* File descriptor to the temp file */ 163306b9b3e0SSimon J. Gerraty 1634dba7b0efSSimon J. Gerraty tfd = Job_TempFile(TMPPAT, tfile, sizeof tfile); 16353955d011SMarcel Moolenaar 16363955d011SMarcel Moolenaar job->cmdFILE = fdopen(tfd, "w+"); 1637e2eeea75SSimon J. Gerraty if (job->cmdFILE == NULL) 16383955d011SMarcel Moolenaar Punt("Could not fdopen %s", tfile); 1639e2eeea75SSimon J. Gerraty 1640956e45f6SSimon J. Gerraty (void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC); 16413955d011SMarcel Moolenaar 16423955d011SMarcel Moolenaar #ifdef USE_META 16433955d011SMarcel Moolenaar if (useMeta) { 16443955d011SMarcel Moolenaar meta_job_start(job, gn); 164506b9b3e0SSimon J. Gerraty if (gn->type & OP_SILENT) /* might have changed */ 1646b0c40a00SSimon J. Gerraty job->echo = false; 16473955d011SMarcel Moolenaar } 16483955d011SMarcel Moolenaar #endif 16493955d011SMarcel Moolenaar 1650b0c40a00SSimon J. Gerraty *out_run = JobWriteCommands(job); 165106b9b3e0SSimon J. Gerraty } 165206b9b3e0SSimon J. Gerraty 165306b9b3e0SSimon J. Gerraty /* 1654b0c40a00SSimon J. Gerraty * Start a target-creation process going for the target described by gn. 165506b9b3e0SSimon J. Gerraty * 165606b9b3e0SSimon J. Gerraty * Results: 165706b9b3e0SSimon J. Gerraty * JOB_ERROR if there was an error in the commands, JOB_FINISHED 165806b9b3e0SSimon J. Gerraty * if there isn't actually anything left to do for the job and 165906b9b3e0SSimon J. Gerraty * JOB_RUNNING if the job has been started. 166006b9b3e0SSimon J. Gerraty * 1661b0c40a00SSimon J. Gerraty * Details: 166206b9b3e0SSimon J. Gerraty * A new Job node is created and added to the list of running 166306b9b3e0SSimon J. Gerraty * jobs. PMake is forked and a child shell created. 166406b9b3e0SSimon J. Gerraty * 166506b9b3e0SSimon J. Gerraty * NB: The return value is ignored by everyone. 166606b9b3e0SSimon J. Gerraty */ 166706b9b3e0SSimon J. Gerraty static JobStartResult 1668b0c40a00SSimon J. Gerraty JobStart(GNode *gn, bool special) 166906b9b3e0SSimon J. Gerraty { 167006b9b3e0SSimon J. Gerraty Job *job; /* new job descriptor */ 167106b9b3e0SSimon J. Gerraty char *argv[10]; /* Argument vector to shell */ 1672b0c40a00SSimon J. Gerraty bool cmdsOK; /* true if the nodes commands were all right */ 1673b0c40a00SSimon J. Gerraty bool run; 167406b9b3e0SSimon J. Gerraty 167506b9b3e0SSimon J. Gerraty for (job = job_table; job < job_table_end; job++) { 167606b9b3e0SSimon J. Gerraty if (job->status == JOB_ST_FREE) 167706b9b3e0SSimon J. Gerraty break; 167806b9b3e0SSimon J. Gerraty } 167906b9b3e0SSimon J. Gerraty if (job >= job_table_end) 168006b9b3e0SSimon J. Gerraty Punt("JobStart no job slots vacant"); 168106b9b3e0SSimon J. Gerraty 168206b9b3e0SSimon J. Gerraty memset(job, 0, sizeof *job); 168306b9b3e0SSimon J. Gerraty job->node = gn; 168406b9b3e0SSimon J. Gerraty job->tailCmds = NULL; 168506b9b3e0SSimon J. Gerraty job->status = JOB_ST_SET_UP; 168606b9b3e0SSimon J. Gerraty 168706b9b3e0SSimon J. Gerraty job->special = special || gn->type & OP_SPECIAL; 168806b9b3e0SSimon J. Gerraty job->ignerr = opts.ignoreErrors || gn->type & OP_IGNORE; 16899f45a3c8SSimon J. Gerraty job->echo = !(opts.silent || gn->type & OP_SILENT); 169006b9b3e0SSimon J. Gerraty 169106b9b3e0SSimon J. Gerraty /* 169206b9b3e0SSimon J. Gerraty * Check the commands now so any attributes from .DEFAULT have a 169306b9b3e0SSimon J. Gerraty * chance to migrate to the node. 169406b9b3e0SSimon J. Gerraty */ 169506b9b3e0SSimon J. Gerraty cmdsOK = Job_CheckCommands(gn, Error); 169606b9b3e0SSimon J. Gerraty 169706b9b3e0SSimon J. Gerraty job->inPollfd = NULL; 1698dba7b0efSSimon J. Gerraty 1699dba7b0efSSimon J. Gerraty if (Lst_IsEmpty(&gn->commands)) { 1700dba7b0efSSimon J. Gerraty job->cmdFILE = stdout; 1701b0c40a00SSimon J. Gerraty run = false; 1702b0c40a00SSimon J. Gerraty 1703b0c40a00SSimon J. Gerraty /* 1704b0c40a00SSimon J. Gerraty * We're serious here, but if the commands were bogus, we're 1705b0c40a00SSimon J. Gerraty * also dead... 1706b0c40a00SSimon J. Gerraty */ 1707b0c40a00SSimon J. Gerraty if (!cmdsOK) { 17089f45a3c8SSimon J. Gerraty PrintOnError(gn, "\n"); /* provide some clue */ 1709b0c40a00SSimon J. Gerraty DieHorribly(); 1710b0c40a00SSimon J. Gerraty } 1711dba7b0efSSimon J. Gerraty } else if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) || 17129f45a3c8SSimon J. Gerraty (!opts.noExecute && !opts.touch)) { 1713dba7b0efSSimon J. Gerraty /* 1714dba7b0efSSimon J. Gerraty * The above condition looks very similar to 1715dba7b0efSSimon J. Gerraty * GNode_ShouldExecute but is subtly different. It prevents 1716dba7b0efSSimon J. Gerraty * that .MAKE targets are touched since these are usually 1717dba7b0efSSimon J. Gerraty * virtual targets. 1718dba7b0efSSimon J. Gerraty */ 1719dba7b0efSSimon J. Gerraty 1720b0c40a00SSimon J. Gerraty /* 1721b0c40a00SSimon J. Gerraty * We're serious here, but if the commands were bogus, we're 1722b0c40a00SSimon J. Gerraty * also dead... 1723b0c40a00SSimon J. Gerraty */ 1724b0c40a00SSimon J. Gerraty if (!cmdsOK) { 17259f45a3c8SSimon J. Gerraty PrintOnError(gn, "\n"); /* provide some clue */ 1726b0c40a00SSimon J. Gerraty DieHorribly(); 1727b0c40a00SSimon J. Gerraty } 1728b0c40a00SSimon J. Gerraty 1729b0c40a00SSimon J. Gerraty JobWriteShellCommands(job, gn, &run); 1730dba7b0efSSimon J. Gerraty (void)fflush(job->cmdFILE); 1731956e45f6SSimon J. Gerraty } else if (!GNode_ShouldExecute(gn)) { 17323955d011SMarcel Moolenaar /* 1733b0c40a00SSimon J. Gerraty * Just write all the commands to stdout in one fell swoop. 1734dba7b0efSSimon J. Gerraty * This still sets up job->tailCmds correctly. 17353955d011SMarcel Moolenaar */ 173606b9b3e0SSimon J. Gerraty SwitchOutputTo(gn); 17373955d011SMarcel Moolenaar job->cmdFILE = stdout; 1738956e45f6SSimon J. Gerraty if (cmdsOK) 1739b0c40a00SSimon J. Gerraty JobWriteCommands(job); 1740b0c40a00SSimon J. Gerraty run = false; 1741dba7b0efSSimon J. Gerraty (void)fflush(job->cmdFILE); 17423955d011SMarcel Moolenaar } else { 174306b9b3e0SSimon J. Gerraty Job_Touch(gn, job->echo); 1744b0c40a00SSimon J. Gerraty run = false; 17453955d011SMarcel Moolenaar } 17463955d011SMarcel Moolenaar 174706b9b3e0SSimon J. Gerraty /* If we're not supposed to execute a shell, don't. */ 174806b9b3e0SSimon J. Gerraty if (!run) { 174906b9b3e0SSimon J. Gerraty if (!job->special) 17503955d011SMarcel Moolenaar Job_TokenReturn(); 175106b9b3e0SSimon J. Gerraty /* Unlink and close the command file if we opened one */ 1752e2eeea75SSimon J. Gerraty if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 17533955d011SMarcel Moolenaar (void)fclose(job->cmdFILE); 17543955d011SMarcel Moolenaar job->cmdFILE = NULL; 17553955d011SMarcel Moolenaar } 17563955d011SMarcel Moolenaar 17573955d011SMarcel Moolenaar /* 175806b9b3e0SSimon J. Gerraty * We only want to work our way up the graph if we aren't 175906b9b3e0SSimon J. Gerraty * here because the commands for the job were no good. 17603955d011SMarcel Moolenaar */ 1761956e45f6SSimon J. Gerraty if (cmdsOK && aborting == ABORT_NONE) { 1762956e45f6SSimon J. Gerraty JobSaveCommands(job); 17633955d011SMarcel Moolenaar job->node->made = MADE; 17643955d011SMarcel Moolenaar Make_Update(job->node); 17653955d011SMarcel Moolenaar } 1766e2eeea75SSimon J. Gerraty job->status = JOB_ST_FREE; 17673955d011SMarcel Moolenaar return cmdsOK ? JOB_FINISHED : JOB_ERROR; 17683955d011SMarcel Moolenaar } 17693955d011SMarcel Moolenaar 17703955d011SMarcel Moolenaar /* 177106b9b3e0SSimon J. Gerraty * Set up the control arguments to the shell. This is based on the 177206b9b3e0SSimon J. Gerraty * flags set earlier for this job. 17733955d011SMarcel Moolenaar */ 17743955d011SMarcel Moolenaar JobMakeArgv(job, argv); 17753955d011SMarcel Moolenaar 17763955d011SMarcel Moolenaar /* Create the pipe by which we'll get the shell's output. */ 17773955d011SMarcel Moolenaar JobCreatePipe(job, 3); 17783955d011SMarcel Moolenaar 17793955d011SMarcel Moolenaar JobExec(job, argv); 17803841c287SSimon J. Gerraty return JOB_RUNNING; 17813955d011SMarcel Moolenaar } 17823955d011SMarcel Moolenaar 178306b9b3e0SSimon J. Gerraty /* 1784b0c40a00SSimon J. Gerraty * If the shell has an output filter (which only csh and ksh have by default), 1785b0c40a00SSimon J. Gerraty * print the output of the child process, skipping the noPrint text of the 1786b0c40a00SSimon J. Gerraty * shell. 1787b0c40a00SSimon J. Gerraty * 1788b0c40a00SSimon J. Gerraty * Return the part of the output that the calling function needs to output by 1789b0c40a00SSimon J. Gerraty * itself. 179006b9b3e0SSimon J. Gerraty */ 17913955d011SMarcel Moolenaar static char * 1792548bfc56SSimon J. Gerraty PrintFilteredOutput(char *p, const char *endp) /* XXX: p should be const */ 17933955d011SMarcel Moolenaar { 1794d5e0a182SSimon J. Gerraty char *ep; /* XXX: should be const */ 17953955d011SMarcel Moolenaar 179606b9b3e0SSimon J. Gerraty if (shell->noPrint == NULL || shell->noPrint[0] == '\0') 1797d5e0a182SSimon J. Gerraty return p; 1798e2eeea75SSimon J. Gerraty 179906b9b3e0SSimon J. Gerraty /* 180006b9b3e0SSimon J. Gerraty * XXX: What happens if shell->noPrint occurs on the boundary of 180106b9b3e0SSimon J. Gerraty * the buffer? To work correctly in all cases, this should rather 180206b9b3e0SSimon J. Gerraty * be a proper stream filter instead of doing string matching on 180306b9b3e0SSimon J. Gerraty * selected chunks of the output. 180406b9b3e0SSimon J. Gerraty */ 1805d5e0a182SSimon J. Gerraty while ((ep = strstr(p, shell->noPrint)) != NULL) { 1806d5e0a182SSimon J. Gerraty if (ep != p) { 1807d5e0a182SSimon J. Gerraty *ep = '\0'; /* XXX: avoid writing to the buffer */ 18083955d011SMarcel Moolenaar /* 18093955d011SMarcel Moolenaar * The only way there wouldn't be a newline after 18103955d011SMarcel Moolenaar * this line is if it were the last in the buffer. 181106b9b3e0SSimon J. Gerraty * however, since the noPrint output comes after it, 18123955d011SMarcel Moolenaar * there must be a newline, so we don't print one. 18133955d011SMarcel Moolenaar */ 181406b9b3e0SSimon J. Gerraty /* XXX: What about null bytes in the output? */ 1815d5e0a182SSimon J. Gerraty (void)fprintf(stdout, "%s", p); 18163955d011SMarcel Moolenaar (void)fflush(stdout); 18173955d011SMarcel Moolenaar } 1818d5e0a182SSimon J. Gerraty p = ep + shell->noPrintLen; 1819d5e0a182SSimon J. Gerraty if (p == endp) 182006b9b3e0SSimon J. Gerraty break; 1821d5e0a182SSimon J. Gerraty p++; /* skip over the (XXX: assumed) newline */ 1822d5e0a182SSimon J. Gerraty pp_skip_whitespace(&p); 18233955d011SMarcel Moolenaar } 1824d5e0a182SSimon J. Gerraty return p; 18253955d011SMarcel Moolenaar } 18263955d011SMarcel Moolenaar 1827e2eeea75SSimon J. Gerraty /* 1828e2eeea75SSimon J. Gerraty * This function is called whenever there is something to read on the pipe. 1829e2eeea75SSimon J. Gerraty * We collect more output from the given job and store it in the job's 1830e2eeea75SSimon J. Gerraty * outBuf. If this makes up a line, we print it tagged by the job's 1831e2eeea75SSimon J. Gerraty * identifier, as necessary. 1832e2eeea75SSimon J. Gerraty * 1833e2eeea75SSimon J. Gerraty * In the output of the shell, the 'noPrint' lines are removed. If the 1834e2eeea75SSimon J. Gerraty * command is not alone on the line (the character after it is not \0 or 1835e2eeea75SSimon J. Gerraty * \n), we do print whatever follows it. 18363955d011SMarcel Moolenaar * 18373955d011SMarcel Moolenaar * Input: 18383955d011SMarcel Moolenaar * job the job whose output needs printing 1839b0c40a00SSimon J. Gerraty * finish true if this is the last time we'll be called 18403955d011SMarcel Moolenaar * for this job 18413955d011SMarcel Moolenaar */ 1842956e45f6SSimon J. Gerraty static void 1843b0c40a00SSimon J. Gerraty CollectOutput(Job *job, bool finish) 18443955d011SMarcel Moolenaar { 1845b0c40a00SSimon J. Gerraty bool gotNL; /* true if got a newline */ 1846b0c40a00SSimon J. Gerraty bool fbuf; /* true if our buffer filled up */ 1847956e45f6SSimon J. Gerraty size_t nr; /* number of bytes read */ 1848956e45f6SSimon J. Gerraty size_t i; /* auxiliary index into outBuf */ 1849956e45f6SSimon J. Gerraty size_t max; /* limit for i (end of current data) */ 1850956e45f6SSimon J. Gerraty ssize_t nRead; /* (Temporary) number of bytes read */ 18513955d011SMarcel Moolenaar 185206b9b3e0SSimon J. Gerraty /* Read as many bytes as will fit in the buffer. */ 1853e2eeea75SSimon J. Gerraty again: 1854b0c40a00SSimon J. Gerraty gotNL = false; 1855b0c40a00SSimon J. Gerraty fbuf = false; 18563955d011SMarcel Moolenaar 18573955d011SMarcel Moolenaar nRead = read(job->inPipe, &job->outBuf[job->curPos], 18583955d011SMarcel Moolenaar JOB_BUFSIZE - job->curPos); 18593955d011SMarcel Moolenaar if (nRead < 0) { 18603955d011SMarcel Moolenaar if (errno == EAGAIN) 18613955d011SMarcel Moolenaar return; 18624fde40d9SSimon J. Gerraty if (DEBUG(JOB)) 1863b0c40a00SSimon J. Gerraty perror("CollectOutput(piperead)"); 18643955d011SMarcel Moolenaar nr = 0; 18654fde40d9SSimon J. Gerraty } else 1866956e45f6SSimon J. Gerraty nr = (size_t)nRead; 18674fde40d9SSimon J. Gerraty 18684fde40d9SSimon J. Gerraty if (nr == 0) 18694fde40d9SSimon J. Gerraty finish = false; /* stop looping */ 18703955d011SMarcel Moolenaar 18713955d011SMarcel Moolenaar /* 18723955d011SMarcel Moolenaar * If we hit the end-of-file (the job is dead), we must flush its 18733955d011SMarcel Moolenaar * remaining output, so pretend we read a newline if there's any 18743955d011SMarcel Moolenaar * output remaining in the buffer. 18753955d011SMarcel Moolenaar */ 1876e2eeea75SSimon J. Gerraty if (nr == 0 && job->curPos != 0) { 18773955d011SMarcel Moolenaar job->outBuf[job->curPos] = '\n'; 18783955d011SMarcel Moolenaar nr = 1; 18793955d011SMarcel Moolenaar } 18803955d011SMarcel Moolenaar 18813955d011SMarcel Moolenaar max = job->curPos + nr; 18824fde40d9SSimon J. Gerraty for (i = job->curPos; i < max; i++) 18834fde40d9SSimon J. Gerraty if (job->outBuf[i] == '\0') 18844fde40d9SSimon J. Gerraty job->outBuf[i] = ' '; 18854fde40d9SSimon J. Gerraty 18864fde40d9SSimon J. Gerraty /* Look for the last newline in the bytes we just got. */ 188706b9b3e0SSimon J. Gerraty for (i = job->curPos + nr - 1; 188806b9b3e0SSimon J. Gerraty i >= job->curPos && i != (size_t)-1; i--) { 18893955d011SMarcel Moolenaar if (job->outBuf[i] == '\n') { 1890b0c40a00SSimon J. Gerraty gotNL = true; 18913955d011SMarcel Moolenaar break; 18923955d011SMarcel Moolenaar } 18933955d011SMarcel Moolenaar } 18943955d011SMarcel Moolenaar 18953955d011SMarcel Moolenaar if (!gotNL) { 18963955d011SMarcel Moolenaar job->curPos += nr; 18973955d011SMarcel Moolenaar if (job->curPos == JOB_BUFSIZE) { 18983955d011SMarcel Moolenaar /* 18993955d011SMarcel Moolenaar * If we've run out of buffer space, we have no choice 19003955d011SMarcel Moolenaar * but to print the stuff. sigh. 19013955d011SMarcel Moolenaar */ 1902b0c40a00SSimon J. Gerraty fbuf = true; 19033955d011SMarcel Moolenaar i = job->curPos; 19043955d011SMarcel Moolenaar } 19053955d011SMarcel Moolenaar } 19063955d011SMarcel Moolenaar if (gotNL || fbuf) { 19073955d011SMarcel Moolenaar /* 19083955d011SMarcel Moolenaar * Need to send the output to the screen. Null terminate it 19093955d011SMarcel Moolenaar * first, overwriting the newline character if there was one. 19103955d011SMarcel Moolenaar * So long as the line isn't one we should filter (according 19113955d011SMarcel Moolenaar * to the shell description), we print the line, preceded 19123955d011SMarcel Moolenaar * by a target banner if this target isn't the same as the 19133955d011SMarcel Moolenaar * one for which we last printed something. 19143955d011SMarcel Moolenaar * The rest of the data in the buffer are then shifted down 19153955d011SMarcel Moolenaar * to the start of the buffer and curPos is set accordingly. 19163955d011SMarcel Moolenaar */ 19173955d011SMarcel Moolenaar job->outBuf[i] = '\0'; 19183955d011SMarcel Moolenaar if (i >= job->curPos) { 1919d5e0a182SSimon J. Gerraty char *p; 19203955d011SMarcel Moolenaar 1921b0c40a00SSimon J. Gerraty /* 1922b0c40a00SSimon J. Gerraty * FIXME: SwitchOutputTo should be here, according to 1923b0c40a00SSimon J. Gerraty * the comment above. But since PrintOutput does not 1924b0c40a00SSimon J. Gerraty * do anything in the default shell, this bug has gone 1925b0c40a00SSimon J. Gerraty * unnoticed until now. 1926b0c40a00SSimon J. Gerraty */ 1927d5e0a182SSimon J. Gerraty p = PrintFilteredOutput(job->outBuf, &job->outBuf[i]); 19283955d011SMarcel Moolenaar 19293955d011SMarcel Moolenaar /* 1930b0c40a00SSimon J. Gerraty * There's still more in the output buffer. This time, 193106b9b3e0SSimon J. Gerraty * though, we know there's no newline at the end, so 193206b9b3e0SSimon J. Gerraty * we add one of our own free will. 19333955d011SMarcel Moolenaar */ 1934d5e0a182SSimon J. Gerraty if (*p != '\0') { 19359f45a3c8SSimon J. Gerraty if (!opts.silent) 193606b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 19373955d011SMarcel Moolenaar #ifdef USE_META 19383955d011SMarcel Moolenaar if (useMeta) { 1939d5e0a182SSimon J. Gerraty meta_job_output(job, p, 194006b9b3e0SSimon J. Gerraty gotNL ? "\n" : ""); 19413955d011SMarcel Moolenaar } 19423955d011SMarcel Moolenaar #endif 1943d5e0a182SSimon J. Gerraty (void)fprintf(stdout, "%s%s", p, 194406b9b3e0SSimon J. Gerraty gotNL ? "\n" : ""); 19453955d011SMarcel Moolenaar (void)fflush(stdout); 19463955d011SMarcel Moolenaar } 19473955d011SMarcel Moolenaar } 19483955d011SMarcel Moolenaar /* 194906b9b3e0SSimon J. Gerraty * max is the last offset still in the buffer. Move any 195006b9b3e0SSimon J. Gerraty * remaining characters to the start of the buffer and 195106b9b3e0SSimon J. Gerraty * update the end marker curPos. 19523955d011SMarcel Moolenaar */ 1953db29cad8SSimon J. Gerraty if (i < max) { 195406b9b3e0SSimon J. Gerraty (void)memmove(job->outBuf, &job->outBuf[i + 1], 195506b9b3e0SSimon J. Gerraty max - (i + 1)); 1956db29cad8SSimon J. Gerraty job->curPos = max - (i + 1); 1957db29cad8SSimon J. Gerraty } else { 1958db29cad8SSimon J. Gerraty assert(i == max); 19593955d011SMarcel Moolenaar job->curPos = 0; 19603955d011SMarcel Moolenaar } 19613955d011SMarcel Moolenaar } 19623955d011SMarcel Moolenaar if (finish) { 19633955d011SMarcel Moolenaar /* 19643955d011SMarcel Moolenaar * If the finish flag is true, we must loop until we hit 19653955d011SMarcel Moolenaar * end-of-file on the pipe. This is guaranteed to happen 19663955d011SMarcel Moolenaar * eventually since the other end of the pipe is now closed 19673955d011SMarcel Moolenaar * (we closed it explicitly and the child has exited). When 1968b0c40a00SSimon J. Gerraty * we do get an EOF, finish will be set false and we'll fall 19693955d011SMarcel Moolenaar * through and out. 19703955d011SMarcel Moolenaar */ 1971e2eeea75SSimon J. Gerraty goto again; 19723955d011SMarcel Moolenaar } 19733955d011SMarcel Moolenaar } 19743955d011SMarcel Moolenaar 19753955d011SMarcel Moolenaar static void 19763955d011SMarcel Moolenaar JobRun(GNode *targ) 19773955d011SMarcel Moolenaar { 1978956e45f6SSimon J. Gerraty #if 0 19793955d011SMarcel Moolenaar /* 1980956e45f6SSimon J. Gerraty * Unfortunately it is too complicated to run .BEGIN, .END, and 1981956e45f6SSimon J. Gerraty * .INTERRUPT job in the parallel job module. As of 2020-09-25, 1982956e45f6SSimon J. Gerraty * unit-tests/deptgt-end-jobs.mk hangs in an endless loop. 1983956e45f6SSimon J. Gerraty * 1984956e45f6SSimon J. Gerraty * Running these jobs in compat mode also guarantees that these 1985956e45f6SSimon J. Gerraty * jobs do not overlap with other unrelated jobs. 19863955d011SMarcel Moolenaar */ 1987dba7b0efSSimon J. Gerraty GNodeList lst = LST_INIT; 1988dba7b0efSSimon J. Gerraty Lst_Append(&lst, targ); 1989dba7b0efSSimon J. Gerraty (void)Make_Run(&lst); 1990dba7b0efSSimon J. Gerraty Lst_Done(&lst); 1991b0c40a00SSimon J. Gerraty JobStart(targ, true); 199206b9b3e0SSimon J. Gerraty while (jobTokensRunning != 0) { 19933955d011SMarcel Moolenaar Job_CatchOutput(); 19943955d011SMarcel Moolenaar } 19953955d011SMarcel Moolenaar #else 19963955d011SMarcel Moolenaar Compat_Make(targ, targ); 199706b9b3e0SSimon J. Gerraty /* XXX: Replace with GNode_IsError(gn) */ 19983955d011SMarcel Moolenaar if (targ->made == ERROR) { 19999f45a3c8SSimon J. Gerraty PrintOnError(targ, "\n\nStop.\n"); 20003955d011SMarcel Moolenaar exit(1); 20013955d011SMarcel Moolenaar } 20023955d011SMarcel Moolenaar #endif 20033955d011SMarcel Moolenaar } 20043955d011SMarcel Moolenaar 200506b9b3e0SSimon J. Gerraty /* 200606b9b3e0SSimon J. Gerraty * Handle the exit of a child. Called from Make_Make. 20073955d011SMarcel Moolenaar * 20083955d011SMarcel Moolenaar * The job descriptor is removed from the list of children. 20093955d011SMarcel Moolenaar * 20103955d011SMarcel Moolenaar * Notes: 20113955d011SMarcel Moolenaar * We do waits, blocking or not, according to the wisdom of our 20123955d011SMarcel Moolenaar * caller, until there are no more children to report. For each 20133955d011SMarcel Moolenaar * job, call JobFinish to finish things off. 20143955d011SMarcel Moolenaar */ 20153955d011SMarcel Moolenaar void 20163955d011SMarcel Moolenaar Job_CatchChildren(void) 20173955d011SMarcel Moolenaar { 20183955d011SMarcel Moolenaar int pid; /* pid of dead child */ 20193955d011SMarcel Moolenaar WAIT_T status; /* Exit/termination status */ 20203955d011SMarcel Moolenaar 202106b9b3e0SSimon J. Gerraty /* Don't even bother if we know there's no one around. */ 20223955d011SMarcel Moolenaar if (jobTokensRunning == 0) 20233955d011SMarcel Moolenaar return; 20243955d011SMarcel Moolenaar 2025dba7b0efSSimon J. Gerraty /* Have we received SIGCHLD since last call? */ 2026dba7b0efSSimon J. Gerraty if (caught_sigchld == 0) 2027dba7b0efSSimon J. Gerraty return; 2028dba7b0efSSimon J. Gerraty caught_sigchld = 0; 2029dba7b0efSSimon J. Gerraty 20303955d011SMarcel Moolenaar while ((pid = waitpid((pid_t)-1, &status, WNOHANG | WUNTRACED)) > 0) { 203106b9b3e0SSimon J. Gerraty DEBUG2(JOB, "Process %d exited/stopped status %x.\n", 203206b9b3e0SSimon J. Gerraty pid, WAIT_STATUS(status)); 2033b0c40a00SSimon J. Gerraty JobReapChild(pid, status, true); 20343955d011SMarcel Moolenaar } 20353955d011SMarcel Moolenaar } 20363955d011SMarcel Moolenaar 20373955d011SMarcel Moolenaar /* 20383955d011SMarcel Moolenaar * It is possible that wait[pid]() was called from elsewhere, 20393955d011SMarcel Moolenaar * this lets us reap jobs regardless. 20403955d011SMarcel Moolenaar */ 20413955d011SMarcel Moolenaar void 2042b0c40a00SSimon J. Gerraty JobReapChild(pid_t pid, WAIT_T status, bool isJobs) 20433955d011SMarcel Moolenaar { 20443955d011SMarcel Moolenaar Job *job; /* job descriptor for dead child */ 20453955d011SMarcel Moolenaar 204606b9b3e0SSimon J. Gerraty /* Don't even bother if we know there's no one around. */ 20473955d011SMarcel Moolenaar if (jobTokensRunning == 0) 20483955d011SMarcel Moolenaar return; 20493955d011SMarcel Moolenaar 20503955d011SMarcel Moolenaar job = JobFindPid(pid, JOB_ST_RUNNING, isJobs); 20513955d011SMarcel Moolenaar if (job == NULL) { 20523955d011SMarcel Moolenaar if (isJobs) { 20533955d011SMarcel Moolenaar if (!lurking_children) 205406b9b3e0SSimon J. Gerraty Error("Child (%d) status %x not in table?", 205506b9b3e0SSimon J. Gerraty pid, status); 20563955d011SMarcel Moolenaar } 20573955d011SMarcel Moolenaar return; /* not ours */ 20583955d011SMarcel Moolenaar } 20593955d011SMarcel Moolenaar if (WIFSTOPPED(status)) { 206006b9b3e0SSimon J. Gerraty DEBUG2(JOB, "Process %d (%s) stopped.\n", 206106b9b3e0SSimon J. Gerraty job->pid, job->node->name); 20623955d011SMarcel Moolenaar if (!make_suspended) { 20633955d011SMarcel Moolenaar switch (WSTOPSIG(status)) { 20643955d011SMarcel Moolenaar case SIGTSTP: 206506b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Suspended\n", 206606b9b3e0SSimon J. Gerraty job->node->name); 20673955d011SMarcel Moolenaar break; 20683955d011SMarcel Moolenaar case SIGSTOP: 206906b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Stopped\n", 207006b9b3e0SSimon J. Gerraty job->node->name); 20713955d011SMarcel Moolenaar break; 20723955d011SMarcel Moolenaar default: 20733955d011SMarcel Moolenaar (void)printf("*** [%s] Stopped -- signal %d\n", 20743955d011SMarcel Moolenaar job->node->name, WSTOPSIG(status)); 20753955d011SMarcel Moolenaar } 2076b0c40a00SSimon J. Gerraty job->suspended = true; 20773955d011SMarcel Moolenaar } 20783955d011SMarcel Moolenaar (void)fflush(stdout); 20793955d011SMarcel Moolenaar return; 20803955d011SMarcel Moolenaar } 20813955d011SMarcel Moolenaar 2082e2eeea75SSimon J. Gerraty job->status = JOB_ST_FINISHED; 20833955d011SMarcel Moolenaar job->exit_status = WAIT_STATUS(status); 2084c59c3bf3SSimon J. Gerraty if (WIFEXITED(status)) 2085c59c3bf3SSimon J. Gerraty job->node->exit_status = WEXITSTATUS(status); 20863955d011SMarcel Moolenaar 20873955d011SMarcel Moolenaar JobFinish(job, status); 20883955d011SMarcel Moolenaar } 20893955d011SMarcel Moolenaar 209006b9b3e0SSimon J. Gerraty /* 209106b9b3e0SSimon J. Gerraty * Catch the output from our children, if we're using pipes do so. Otherwise 2092956e45f6SSimon J. Gerraty * just block time until we get a signal(most likely a SIGCHLD) since there's 2093956e45f6SSimon J. Gerraty * no point in just spinning when there's nothing to do and the reaping of a 209406b9b3e0SSimon J. Gerraty * child can wait for a while. 209506b9b3e0SSimon J. Gerraty */ 20963955d011SMarcel Moolenaar void 20973955d011SMarcel Moolenaar Job_CatchOutput(void) 20983955d011SMarcel Moolenaar { 20993955d011SMarcel Moolenaar int nready; 21003955d011SMarcel Moolenaar Job *job; 2101956e45f6SSimon J. Gerraty unsigned int i; 21023955d011SMarcel Moolenaar 21033955d011SMarcel Moolenaar (void)fflush(stdout); 21043955d011SMarcel Moolenaar 21053955d011SMarcel Moolenaar /* The first fd in the list is the job token pipe */ 21061748de26SSimon J. Gerraty do { 2107dba7b0efSSimon J. Gerraty nready = poll(fds + 1 - wantToken, fdsLen - 1 + wantToken, 210806b9b3e0SSimon J. Gerraty POLL_MSEC); 21091748de26SSimon J. Gerraty } while (nready < 0 && errno == EINTR); 21103955d011SMarcel Moolenaar 21111748de26SSimon J. Gerraty if (nready < 0) 21121748de26SSimon J. Gerraty Punt("poll: %s", strerror(errno)); 21131748de26SSimon J. Gerraty 21141748de26SSimon J. Gerraty if (nready > 0 && readyfd(&childExitJob)) { 21153955d011SMarcel Moolenaar char token = 0; 21161748de26SSimon J. Gerraty ssize_t count; 21171748de26SSimon J. Gerraty count = read(childExitJob.inPipe, &token, 1); 2118dba7b0efSSimon J. Gerraty if (count == 1) { 21193955d011SMarcel Moolenaar if (token == DO_JOB_RESUME[0]) 212006b9b3e0SSimon J. Gerraty /* 212106b9b3e0SSimon J. Gerraty * Complete relay requested from our SIGCONT 212206b9b3e0SSimon J. Gerraty * handler 212306b9b3e0SSimon J. Gerraty */ 21243955d011SMarcel Moolenaar JobRestartJobs(); 2125dba7b0efSSimon J. Gerraty } else if (count == 0) 2126dba7b0efSSimon J. Gerraty Punt("unexpected eof on token pipe"); 212712904384SSimon J. Gerraty else if (errno != EAGAIN) 2128dba7b0efSSimon J. Gerraty Punt("token pipe read: %s", strerror(errno)); 2129e2eeea75SSimon J. Gerraty nready--; 21303955d011SMarcel Moolenaar } 21313955d011SMarcel Moolenaar 21321748de26SSimon J. Gerraty Job_CatchChildren(); 21331748de26SSimon J. Gerraty if (nready == 0) 21343955d011SMarcel Moolenaar return; 21353955d011SMarcel Moolenaar 2136dba7b0efSSimon J. Gerraty for (i = npseudojobs * nfds_per_job(); i < fdsLen; i++) { 213706b9b3e0SSimon J. Gerraty if (fds[i].revents == 0) 21383955d011SMarcel Moolenaar continue; 2139dba7b0efSSimon J. Gerraty job = jobByFdIndex[i]; 2140e2eeea75SSimon J. Gerraty if (job->status == JOB_ST_RUNNING) 2141b0c40a00SSimon J. Gerraty CollectOutput(job, false); 214249caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 214349caa483SSimon J. Gerraty /* 214449caa483SSimon J. Gerraty * With meta mode, we may have activity on the job's filemon 214506b9b3e0SSimon J. Gerraty * descriptor too, which at the moment is any pollfd other 214606b9b3e0SSimon J. Gerraty * than job->inPollfd. 214749caa483SSimon J. Gerraty */ 214849caa483SSimon J. Gerraty if (useMeta && job->inPollfd != &fds[i]) { 21499f45a3c8SSimon J. Gerraty if (meta_job_event(job) <= 0) 215049caa483SSimon J. Gerraty fds[i].events = 0; /* never mind */ 215149caa483SSimon J. Gerraty } 215249caa483SSimon J. Gerraty #endif 21531748de26SSimon J. Gerraty if (--nready == 0) 21541748de26SSimon J. Gerraty return; 21553955d011SMarcel Moolenaar } 21563955d011SMarcel Moolenaar } 21573955d011SMarcel Moolenaar 215806b9b3e0SSimon J. Gerraty /* 215906b9b3e0SSimon J. Gerraty * Start the creation of a target. Basically a front-end for JobStart used by 216006b9b3e0SSimon J. Gerraty * the Make module. 216106b9b3e0SSimon J. Gerraty */ 21623955d011SMarcel Moolenaar void 21633955d011SMarcel Moolenaar Job_Make(GNode *gn) 21643955d011SMarcel Moolenaar { 2165b0c40a00SSimon J. Gerraty (void)JobStart(gn, false); 216606b9b3e0SSimon J. Gerraty } 216706b9b3e0SSimon J. Gerraty 216806b9b3e0SSimon J. Gerraty static void 216906b9b3e0SSimon J. Gerraty InitShellNameAndPath(void) 217006b9b3e0SSimon J. Gerraty { 217106b9b3e0SSimon J. Gerraty shellName = shell->name; 217206b9b3e0SSimon J. Gerraty 217306b9b3e0SSimon J. Gerraty #ifdef DEFSHELL_CUSTOM 217406b9b3e0SSimon J. Gerraty if (shellName[0] == '/') { 217506b9b3e0SSimon J. Gerraty shellPath = shellName; 217606b9b3e0SSimon J. Gerraty shellName = str_basename(shellPath); 217706b9b3e0SSimon J. Gerraty return; 217806b9b3e0SSimon J. Gerraty } 217906b9b3e0SSimon J. Gerraty #endif 218012904384SSimon J. Gerraty #ifdef DEFSHELL_PATH 218112904384SSimon J. Gerraty shellPath = DEFSHELL_PATH; 218212904384SSimon J. Gerraty #else 218306b9b3e0SSimon J. Gerraty shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName); 218412904384SSimon J. Gerraty #endif 21853955d011SMarcel Moolenaar } 21863955d011SMarcel Moolenaar 21873955d011SMarcel Moolenaar void 21883955d011SMarcel Moolenaar Shell_Init(void) 21893955d011SMarcel Moolenaar { 219006b9b3e0SSimon J. Gerraty if (shellPath == NULL) 219106b9b3e0SSimon J. Gerraty InitShellNameAndPath(); 219206b9b3e0SSimon J. Gerraty 2193*c9f4001fSSimon J. Gerraty Var_SetWithFlags(SCOPE_CMDLINE, ".SHELL", shellPath, 2194*c9f4001fSSimon J. Gerraty VAR_SET_INTERNAL|VAR_SET_READONLY); 219506b9b3e0SSimon J. Gerraty if (shell->errFlag == NULL) 219606b9b3e0SSimon J. Gerraty shell->errFlag = ""; 219706b9b3e0SSimon J. Gerraty if (shell->echoFlag == NULL) 219806b9b3e0SSimon J. Gerraty shell->echoFlag = ""; 219906b9b3e0SSimon J. Gerraty if (shell->hasErrCtl && shell->errFlag[0] != '\0') { 220006b9b3e0SSimon J. Gerraty if (shellErrFlag != NULL && 220106b9b3e0SSimon J. Gerraty strcmp(shell->errFlag, &shellErrFlag[1]) != 0) { 220251ee2c1cSSimon J. Gerraty free(shellErrFlag); 220351ee2c1cSSimon J. Gerraty shellErrFlag = NULL; 220451ee2c1cSSimon J. Gerraty } 22059f45a3c8SSimon J. Gerraty if (shellErrFlag == NULL) 22069f45a3c8SSimon J. Gerraty shellErrFlag = str_concat2("-", shell->errFlag); 220706b9b3e0SSimon J. Gerraty } else if (shellErrFlag != NULL) { 220851ee2c1cSSimon J. Gerraty free(shellErrFlag); 220951ee2c1cSSimon J. Gerraty shellErrFlag = NULL; 221051ee2c1cSSimon J. Gerraty } 22113955d011SMarcel Moolenaar } 22123955d011SMarcel Moolenaar 221306b9b3e0SSimon J. Gerraty /* 221406b9b3e0SSimon J. Gerraty * Return the string literal that is used in the current command shell 221506b9b3e0SSimon J. Gerraty * to produce a newline character. 221606b9b3e0SSimon J. Gerraty */ 22173955d011SMarcel Moolenaar const char * 22183955d011SMarcel Moolenaar Shell_GetNewline(void) 22193955d011SMarcel Moolenaar { 222006b9b3e0SSimon J. Gerraty return shell->newline; 22213955d011SMarcel Moolenaar } 22223955d011SMarcel Moolenaar 22233955d011SMarcel Moolenaar void 22243955d011SMarcel Moolenaar Job_SetPrefix(void) 22253955d011SMarcel Moolenaar { 2226d5e0a182SSimon J. Gerraty if (targPrefix != NULL) 22273955d011SMarcel Moolenaar free(targPrefix); 2228d5e0a182SSimon J. Gerraty else if (!Var_Exists(SCOPE_GLOBAL, ".MAKE.JOB.PREFIX")) 22298c973ee2SSimon J. Gerraty Global_Set(".MAKE.JOB.PREFIX", "---"); 22303955d011SMarcel Moolenaar 22318c973ee2SSimon J. Gerraty targPrefix = Var_Subst("${.MAKE.JOB.PREFIX}", 22328c973ee2SSimon J. Gerraty SCOPE_GLOBAL, VARE_WANTRES); 2233956e45f6SSimon J. Gerraty /* TODO: handle errors */ 22343955d011SMarcel Moolenaar } 22353955d011SMarcel Moolenaar 223606b9b3e0SSimon J. Gerraty static void 223706b9b3e0SSimon J. Gerraty AddSig(int sig, SignalProc handler) 223806b9b3e0SSimon J. Gerraty { 223906b9b3e0SSimon J. Gerraty if (bmake_signal(sig, SIG_IGN) != SIG_IGN) { 224006b9b3e0SSimon J. Gerraty sigaddset(&caught_signals, sig); 224106b9b3e0SSimon J. Gerraty (void)bmake_signal(sig, handler); 224206b9b3e0SSimon J. Gerraty } 224306b9b3e0SSimon J. Gerraty } 224406b9b3e0SSimon J. Gerraty 22452c3632d1SSimon J. Gerraty /* Initialize the process module. */ 22463955d011SMarcel Moolenaar void 22473955d011SMarcel Moolenaar Job_Init(void) 22483955d011SMarcel Moolenaar { 2249d191243dSSimon J. Gerraty Job_SetPrefix(); 22503955d011SMarcel Moolenaar /* Allocate space for all the job info */ 2251956e45f6SSimon J. Gerraty job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table); 2252956e45f6SSimon J. Gerraty memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table); 2253956e45f6SSimon J. Gerraty job_table_end = job_table + opts.maxJobs; 22543955d011SMarcel Moolenaar wantToken = 0; 2255dba7b0efSSimon J. Gerraty caught_sigchld = 0; 22563955d011SMarcel Moolenaar 2257956e45f6SSimon J. Gerraty aborting = ABORT_NONE; 225806b9b3e0SSimon J. Gerraty job_errors = 0; 22593955d011SMarcel Moolenaar 226068c4481aSSimon J. Gerraty Always_pass_job_queue = GetBooleanExpr(MAKE_ALWAYS_PASS_JOB_QUEUE, 22619a4bc556SSimon J. Gerraty Always_pass_job_queue); 22629a4bc556SSimon J. Gerraty 226368c4481aSSimon J. Gerraty Job_error_token = GetBooleanExpr(MAKE_JOB_ERROR_TOKEN, Job_error_token); 226468c4481aSSimon J. Gerraty 22652d395cb5SSimon J. Gerraty 22663955d011SMarcel Moolenaar /* 22673955d011SMarcel Moolenaar * There is a non-zero chance that we already have children. 22683955d011SMarcel Moolenaar * eg after 'make -f- <<EOF' 226906b9b3e0SSimon J. Gerraty * Since their termination causes a 'Child (pid) not in table' 227006b9b3e0SSimon J. Gerraty * message, Collect the status of any that are already dead, and 227106b9b3e0SSimon J. Gerraty * suppress the error message if there are any undead ones. 22723955d011SMarcel Moolenaar */ 22733955d011SMarcel Moolenaar for (;;) { 227406b9b3e0SSimon J. Gerraty int rval; 227506b9b3e0SSimon J. Gerraty WAIT_T status; 227606b9b3e0SSimon J. Gerraty 22773955d011SMarcel Moolenaar rval = waitpid((pid_t)-1, &status, WNOHANG); 22783955d011SMarcel Moolenaar if (rval > 0) 22793955d011SMarcel Moolenaar continue; 22803955d011SMarcel Moolenaar if (rval == 0) 2281b0c40a00SSimon J. Gerraty lurking_children = true; 22823955d011SMarcel Moolenaar break; 22833955d011SMarcel Moolenaar } 22843955d011SMarcel Moolenaar 22853955d011SMarcel Moolenaar Shell_Init(); 22863955d011SMarcel Moolenaar 22873955d011SMarcel Moolenaar JobCreatePipe(&childExitJob, 3); 22883955d011SMarcel Moolenaar 2289dba7b0efSSimon J. Gerraty { 229049caa483SSimon J. Gerraty /* Preallocate enough for the maximum number of jobs. */ 2291dba7b0efSSimon J. Gerraty size_t nfds = (npseudojobs + (size_t)opts.maxJobs) * 2292dba7b0efSSimon J. Gerraty nfds_per_job(); 2293dba7b0efSSimon J. Gerraty fds = bmake_malloc(sizeof *fds * nfds); 2294dba7b0efSSimon J. Gerraty jobByFdIndex = bmake_malloc(sizeof *jobByFdIndex * nfds); 2295dba7b0efSSimon J. Gerraty } 22963955d011SMarcel Moolenaar 22973955d011SMarcel Moolenaar /* These are permanent entries and take slots 0 and 1 */ 22983955d011SMarcel Moolenaar watchfd(&tokenWaitJob); 22993955d011SMarcel Moolenaar watchfd(&childExitJob); 23003955d011SMarcel Moolenaar 23013955d011SMarcel Moolenaar sigemptyset(&caught_signals); 2302d5e0a182SSimon J. Gerraty /* Install a SIGCHLD handler. */ 23033955d011SMarcel Moolenaar (void)bmake_signal(SIGCHLD, JobChildSig); 23043955d011SMarcel Moolenaar sigaddset(&caught_signals, SIGCHLD); 23053955d011SMarcel Moolenaar 23063955d011SMarcel Moolenaar /* 23073955d011SMarcel Moolenaar * Catch the four signals that POSIX specifies if they aren't ignored. 23083955d011SMarcel Moolenaar * JobPassSig will take care of calling JobInterrupt if appropriate. 23093955d011SMarcel Moolenaar */ 231006b9b3e0SSimon J. Gerraty AddSig(SIGINT, JobPassSig_int); 231106b9b3e0SSimon J. Gerraty AddSig(SIGHUP, JobPassSig_term); 231206b9b3e0SSimon J. Gerraty AddSig(SIGTERM, JobPassSig_term); 231306b9b3e0SSimon J. Gerraty AddSig(SIGQUIT, JobPassSig_term); 23143955d011SMarcel Moolenaar 23153955d011SMarcel Moolenaar /* 23163955d011SMarcel Moolenaar * There are additional signals that need to be caught and passed if 23173955d011SMarcel Moolenaar * either the export system wants to be told directly of signals or if 23183955d011SMarcel Moolenaar * we're giving each job its own process group (since then it won't get 23193955d011SMarcel Moolenaar * signals from the terminal driver as we own the terminal) 23203955d011SMarcel Moolenaar */ 232106b9b3e0SSimon J. Gerraty AddSig(SIGTSTP, JobPassSig_suspend); 232206b9b3e0SSimon J. Gerraty AddSig(SIGTTOU, JobPassSig_suspend); 232306b9b3e0SSimon J. Gerraty AddSig(SIGTTIN, JobPassSig_suspend); 232406b9b3e0SSimon J. Gerraty AddSig(SIGWINCH, JobCondPassSig); 232506b9b3e0SSimon J. Gerraty AddSig(SIGCONT, JobContinueSig); 23263955d011SMarcel Moolenaar 23271748de26SSimon J. Gerraty (void)Job_RunTarget(".BEGIN", NULL); 23289f45a3c8SSimon J. Gerraty /* 23299f45a3c8SSimon J. Gerraty * Create the .END node now, even though no code in the unit tests 2330954401e6SSimon J. Gerraty * depends on it. See also Targ_GetEndNode in Compat_MakeAll. 23319f45a3c8SSimon J. Gerraty */ 2332956e45f6SSimon J. Gerraty (void)Targ_GetEndNode(); 23333955d011SMarcel Moolenaar } 23343955d011SMarcel Moolenaar 233506b9b3e0SSimon J. Gerraty static void 233606b9b3e0SSimon J. Gerraty DelSig(int sig) 23373955d011SMarcel Moolenaar { 233806b9b3e0SSimon J. Gerraty if (sigismember(&caught_signals, sig) != 0) 233906b9b3e0SSimon J. Gerraty (void)bmake_signal(sig, SIG_DFL); 23403955d011SMarcel Moolenaar } 23413955d011SMarcel Moolenaar 234206b9b3e0SSimon J. Gerraty static void 234306b9b3e0SSimon J. Gerraty JobSigReset(void) 234406b9b3e0SSimon J. Gerraty { 234506b9b3e0SSimon J. Gerraty DelSig(SIGINT); 234606b9b3e0SSimon J. Gerraty DelSig(SIGHUP); 234706b9b3e0SSimon J. Gerraty DelSig(SIGQUIT); 234806b9b3e0SSimon J. Gerraty DelSig(SIGTERM); 234906b9b3e0SSimon J. Gerraty DelSig(SIGTSTP); 235006b9b3e0SSimon J. Gerraty DelSig(SIGTTOU); 235106b9b3e0SSimon J. Gerraty DelSig(SIGTTIN); 235206b9b3e0SSimon J. Gerraty DelSig(SIGWINCH); 235306b9b3e0SSimon J. Gerraty DelSig(SIGCONT); 23543955d011SMarcel Moolenaar (void)bmake_signal(SIGCHLD, SIG_DFL); 23553955d011SMarcel Moolenaar } 23563955d011SMarcel Moolenaar 23572c3632d1SSimon J. Gerraty /* Find a shell in 'shells' given its name, or return NULL. */ 23583955d011SMarcel Moolenaar static Shell * 2359956e45f6SSimon J. Gerraty FindShellByName(const char *name) 23603955d011SMarcel Moolenaar { 2361956e45f6SSimon J. Gerraty Shell *sh = shells; 2362956e45f6SSimon J. Gerraty const Shell *shellsEnd = sh + sizeof shells / sizeof shells[0]; 23633955d011SMarcel Moolenaar 2364956e45f6SSimon J. Gerraty for (sh = shells; sh < shellsEnd; sh++) { 23653955d011SMarcel Moolenaar if (strcmp(name, sh->name) == 0) 23663841c287SSimon J. Gerraty return sh; 23673955d011SMarcel Moolenaar } 23683955d011SMarcel Moolenaar return NULL; 23693955d011SMarcel Moolenaar } 23703955d011SMarcel Moolenaar 237106b9b3e0SSimon J. Gerraty /* 237206b9b3e0SSimon J. Gerraty * Parse a shell specification and set up 'shell', shellPath and 237306b9b3e0SSimon J. Gerraty * shellName appropriately. 23743955d011SMarcel Moolenaar * 23753955d011SMarcel Moolenaar * Input: 23763955d011SMarcel Moolenaar * line The shell spec 23773955d011SMarcel Moolenaar * 23783955d011SMarcel Moolenaar * Results: 2379b0c40a00SSimon J. Gerraty * false if the specification was incorrect. 23803955d011SMarcel Moolenaar * 23813955d011SMarcel Moolenaar * Side Effects: 238206b9b3e0SSimon J. Gerraty * 'shell' points to a Shell structure (either predefined or 23833955d011SMarcel Moolenaar * created from the shell spec), shellPath is the full path of the 238406b9b3e0SSimon J. Gerraty * shell described by 'shell', while shellName is just the 23853955d011SMarcel Moolenaar * final component of shellPath. 23863955d011SMarcel Moolenaar * 23873955d011SMarcel Moolenaar * Notes: 23883955d011SMarcel Moolenaar * A shell specification consists of a .SHELL target, with dependency 23893955d011SMarcel Moolenaar * operator, followed by a series of blank-separated words. Double 23903955d011SMarcel Moolenaar * quotes can be used to use blanks in words. A backslash escapes 23913955d011SMarcel Moolenaar * anything (most notably a double-quote and a space) and 23923955d011SMarcel Moolenaar * provides the functionality it does in C. Each word consists of 23933955d011SMarcel Moolenaar * keyword and value separated by an equal sign. There should be no 23943955d011SMarcel Moolenaar * unnecessary spaces in the word. The keywords are as follows: 23953955d011SMarcel Moolenaar * name Name of shell. 23963955d011SMarcel Moolenaar * path Location of shell. 23973955d011SMarcel Moolenaar * quiet Command to turn off echoing. 23983955d011SMarcel Moolenaar * echo Command to turn echoing on 23993955d011SMarcel Moolenaar * filter Result of turning off echoing that shouldn't be 24003955d011SMarcel Moolenaar * printed. 24013955d011SMarcel Moolenaar * echoFlag Flag to turn echoing on at the start 24023955d011SMarcel Moolenaar * errFlag Flag to turn error checking on at the start 24033955d011SMarcel Moolenaar * hasErrCtl True if shell has error checking control 24043955d011SMarcel Moolenaar * newline String literal to represent a newline char 24053955d011SMarcel Moolenaar * check Command to turn on error checking if hasErrCtl 2406b0c40a00SSimon J. Gerraty * is true or template of command to echo a command 24073955d011SMarcel Moolenaar * for which error checking is off if hasErrCtl is 2408b0c40a00SSimon J. Gerraty * false. 24093955d011SMarcel Moolenaar * ignore Command to turn off error checking if hasErrCtl 2410b0c40a00SSimon J. Gerraty * is true or template of command to execute a 24113955d011SMarcel Moolenaar * command so as to ignore any errors it returns if 2412b0c40a00SSimon J. Gerraty * hasErrCtl is false. 24133955d011SMarcel Moolenaar */ 2414b0c40a00SSimon J. Gerraty bool 24153955d011SMarcel Moolenaar Job_ParseShell(char *line) 24163955d011SMarcel Moolenaar { 24172c3632d1SSimon J. Gerraty Words wordsList; 24183955d011SMarcel Moolenaar char **words; 24193955d011SMarcel Moolenaar char **argv; 24202c3632d1SSimon J. Gerraty size_t argc; 24213955d011SMarcel Moolenaar char *path; 24223955d011SMarcel Moolenaar Shell newShell; 2423b0c40a00SSimon J. Gerraty bool fullSpec = false; 24243955d011SMarcel Moolenaar Shell *sh; 24253955d011SMarcel Moolenaar 242606b9b3e0SSimon J. Gerraty /* XXX: don't use line as an iterator variable */ 2427956e45f6SSimon J. Gerraty pp_skip_whitespace(&line); 24283955d011SMarcel Moolenaar 2429dba7b0efSSimon J. Gerraty free(shell_freeIt); 24303955d011SMarcel Moolenaar 2431e2eeea75SSimon J. Gerraty memset(&newShell, 0, sizeof newShell); 24323955d011SMarcel Moolenaar 2433d5e0a182SSimon J. Gerraty /* Parse the specification by keyword. */ 2434b0c40a00SSimon J. Gerraty wordsList = Str_Words(line, true); 24352c3632d1SSimon J. Gerraty words = wordsList.words; 24362c3632d1SSimon J. Gerraty argc = wordsList.len; 24372c3632d1SSimon J. Gerraty path = wordsList.freeIt; 24383955d011SMarcel Moolenaar if (words == NULL) { 24393955d011SMarcel Moolenaar Error("Unterminated quoted string [%s]", line); 2440b0c40a00SSimon J. Gerraty return false; 24413955d011SMarcel Moolenaar } 2442dba7b0efSSimon J. Gerraty shell_freeIt = path; 24433955d011SMarcel Moolenaar 24443955d011SMarcel Moolenaar for (path = NULL, argv = words; argc != 0; argc--, argv++) { 2445956e45f6SSimon J. Gerraty char *arg = *argv; 2446956e45f6SSimon J. Gerraty if (strncmp(arg, "path=", 5) == 0) { 2447956e45f6SSimon J. Gerraty path = arg + 5; 2448956e45f6SSimon J. Gerraty } else if (strncmp(arg, "name=", 5) == 0) { 2449956e45f6SSimon J. Gerraty newShell.name = arg + 5; 24503955d011SMarcel Moolenaar } else { 2451956e45f6SSimon J. Gerraty if (strncmp(arg, "quiet=", 6) == 0) { 2452956e45f6SSimon J. Gerraty newShell.echoOff = arg + 6; 2453956e45f6SSimon J. Gerraty } else if (strncmp(arg, "echo=", 5) == 0) { 2454956e45f6SSimon J. Gerraty newShell.echoOn = arg + 5; 2455956e45f6SSimon J. Gerraty } else if (strncmp(arg, "filter=", 7) == 0) { 2456956e45f6SSimon J. Gerraty newShell.noPrint = arg + 7; 2457956e45f6SSimon J. Gerraty newShell.noPrintLen = strlen(newShell.noPrint); 2458956e45f6SSimon J. Gerraty } else if (strncmp(arg, "echoFlag=", 9) == 0) { 245906b9b3e0SSimon J. Gerraty newShell.echoFlag = arg + 9; 2460956e45f6SSimon J. Gerraty } else if (strncmp(arg, "errFlag=", 8) == 0) { 246106b9b3e0SSimon J. Gerraty newShell.errFlag = arg + 8; 2462956e45f6SSimon J. Gerraty } else if (strncmp(arg, "hasErrCtl=", 10) == 0) { 2463956e45f6SSimon J. Gerraty char c = arg[10]; 2464956e45f6SSimon J. Gerraty newShell.hasErrCtl = c == 'Y' || c == 'y' || 2465956e45f6SSimon J. Gerraty c == 'T' || c == 't'; 2466956e45f6SSimon J. Gerraty } else if (strncmp(arg, "newline=", 8) == 0) { 2467956e45f6SSimon J. Gerraty newShell.newline = arg + 8; 2468956e45f6SSimon J. Gerraty } else if (strncmp(arg, "check=", 6) == 0) { 24699f45a3c8SSimon J. Gerraty /* 24709f45a3c8SSimon J. Gerraty * Before 2020-12-10, these two variables had 24719f45a3c8SSimon J. Gerraty * been a single variable. 24729f45a3c8SSimon J. Gerraty */ 247306b9b3e0SSimon J. Gerraty newShell.errOn = arg + 6; 247406b9b3e0SSimon J. Gerraty newShell.echoTmpl = arg + 6; 2475956e45f6SSimon J. Gerraty } else if (strncmp(arg, "ignore=", 7) == 0) { 24769f45a3c8SSimon J. Gerraty /* 24779f45a3c8SSimon J. Gerraty * Before 2020-12-10, these two variables had 24789f45a3c8SSimon J. Gerraty * been a single variable. 24799f45a3c8SSimon J. Gerraty */ 248006b9b3e0SSimon J. Gerraty newShell.errOff = arg + 7; 248106b9b3e0SSimon J. Gerraty newShell.runIgnTmpl = arg + 7; 2482956e45f6SSimon J. Gerraty } else if (strncmp(arg, "errout=", 7) == 0) { 248306b9b3e0SSimon J. Gerraty newShell.runChkTmpl = arg + 7; 2484956e45f6SSimon J. Gerraty } else if (strncmp(arg, "comment=", 8) == 0) { 2485956e45f6SSimon J. Gerraty newShell.commentChar = arg[8]; 24863955d011SMarcel Moolenaar } else { 248706b9b3e0SSimon J. Gerraty Parse_Error(PARSE_FATAL, 248806b9b3e0SSimon J. Gerraty "Unknown keyword \"%s\"", arg); 24893955d011SMarcel Moolenaar free(words); 2490b0c40a00SSimon J. Gerraty return false; 24913955d011SMarcel Moolenaar } 2492b0c40a00SSimon J. Gerraty fullSpec = true; 24933955d011SMarcel Moolenaar } 24943955d011SMarcel Moolenaar } 24953955d011SMarcel Moolenaar 24963955d011SMarcel Moolenaar if (path == NULL) { 24973955d011SMarcel Moolenaar /* 249806b9b3e0SSimon J. Gerraty * If no path was given, the user wants one of the 249906b9b3e0SSimon J. Gerraty * pre-defined shells, yes? So we find the one s/he wants 250006b9b3e0SSimon J. Gerraty * with the help of FindShellByName and set things up the 250106b9b3e0SSimon J. Gerraty * right way. shellPath will be set up by Shell_Init. 25023955d011SMarcel Moolenaar */ 25033955d011SMarcel Moolenaar if (newShell.name == NULL) { 250406b9b3e0SSimon J. Gerraty Parse_Error(PARSE_FATAL, 250506b9b3e0SSimon J. Gerraty "Neither path nor name specified"); 25063955d011SMarcel Moolenaar free(words); 2507b0c40a00SSimon J. Gerraty return false; 25083955d011SMarcel Moolenaar } else { 2509956e45f6SSimon J. Gerraty if ((sh = FindShellByName(newShell.name)) == NULL) { 251006b9b3e0SSimon J. Gerraty Parse_Error(PARSE_WARNING, 251106b9b3e0SSimon J. Gerraty "%s: No matching shell", newShell.name); 25123955d011SMarcel Moolenaar free(words); 2513b0c40a00SSimon J. Gerraty return false; 25143955d011SMarcel Moolenaar } 251506b9b3e0SSimon J. Gerraty shell = sh; 25163955d011SMarcel Moolenaar shellName = newShell.name; 251706b9b3e0SSimon J. Gerraty if (shellPath != NULL) { 251806b9b3e0SSimon J. Gerraty /* 251906b9b3e0SSimon J. Gerraty * Shell_Init has already been called! 252006b9b3e0SSimon J. Gerraty * Do it again. 252106b9b3e0SSimon J. Gerraty */ 25223955d011SMarcel Moolenaar free(UNCONST(shellPath)); 25233955d011SMarcel Moolenaar shellPath = NULL; 25243955d011SMarcel Moolenaar Shell_Init(); 25253955d011SMarcel Moolenaar } 25263955d011SMarcel Moolenaar } 25273955d011SMarcel Moolenaar } else { 25283955d011SMarcel Moolenaar shellPath = path; 2529d5e0a182SSimon J. Gerraty shellName = newShell.name != NULL ? newShell.name 2530d5e0a182SSimon J. Gerraty : str_basename(path); 25313955d011SMarcel Moolenaar if (!fullSpec) { 2532956e45f6SSimon J. Gerraty if ((sh = FindShellByName(shellName)) == NULL) { 253306b9b3e0SSimon J. Gerraty Parse_Error(PARSE_WARNING, 253406b9b3e0SSimon J. Gerraty "%s: No matching shell", shellName); 25353955d011SMarcel Moolenaar free(words); 2536b0c40a00SSimon J. Gerraty return false; 25373955d011SMarcel Moolenaar } 253806b9b3e0SSimon J. Gerraty shell = sh; 25393955d011SMarcel Moolenaar } else { 254006b9b3e0SSimon J. Gerraty shell = bmake_malloc(sizeof *shell); 254106b9b3e0SSimon J. Gerraty *shell = newShell; 25423955d011SMarcel Moolenaar } 254351ee2c1cSSimon J. Gerraty /* this will take care of shellErrFlag */ 254451ee2c1cSSimon J. Gerraty Shell_Init(); 25453955d011SMarcel Moolenaar } 25463955d011SMarcel Moolenaar 254706b9b3e0SSimon J. Gerraty if (shell->echoOn != NULL && shell->echoOff != NULL) 2548b0c40a00SSimon J. Gerraty shell->hasEchoCtl = true; 25493955d011SMarcel Moolenaar 255006b9b3e0SSimon J. Gerraty if (!shell->hasErrCtl) { 255106b9b3e0SSimon J. Gerraty if (shell->echoTmpl == NULL) 255206b9b3e0SSimon J. Gerraty shell->echoTmpl = ""; 255306b9b3e0SSimon J. Gerraty if (shell->runIgnTmpl == NULL) 255406b9b3e0SSimon J. Gerraty shell->runIgnTmpl = "%s\n"; 25553955d011SMarcel Moolenaar } 25563955d011SMarcel Moolenaar 25573955d011SMarcel Moolenaar /* 255806b9b3e0SSimon J. Gerraty * Do not free up the words themselves, since they might be in use 255906b9b3e0SSimon J. Gerraty * by the shell specification. 25603955d011SMarcel Moolenaar */ 25613955d011SMarcel Moolenaar free(words); 2562b0c40a00SSimon J. Gerraty return true; 25633955d011SMarcel Moolenaar } 25643955d011SMarcel Moolenaar 256506b9b3e0SSimon J. Gerraty /* 256606b9b3e0SSimon J. Gerraty * Handle the receipt of an interrupt. 2567956e45f6SSimon J. Gerraty * 2568956e45f6SSimon J. Gerraty * All children are killed. Another job will be started if the .INTERRUPT 2569956e45f6SSimon J. Gerraty * target is defined. 25703955d011SMarcel Moolenaar * 25713955d011SMarcel Moolenaar * Input: 25723955d011SMarcel Moolenaar * runINTERRUPT Non-zero if commands for the .INTERRUPT target 25733955d011SMarcel Moolenaar * should be executed 25743955d011SMarcel Moolenaar * signo signal received 25753955d011SMarcel Moolenaar */ 25763955d011SMarcel Moolenaar static void 2577b0c40a00SSimon J. Gerraty JobInterrupt(bool runINTERRUPT, int signo) 25783955d011SMarcel Moolenaar { 25793955d011SMarcel Moolenaar Job *job; /* job descriptor in that element */ 25803955d011SMarcel Moolenaar GNode *interrupt; /* the node describing the .INTERRUPT target */ 25813955d011SMarcel Moolenaar sigset_t mask; 25823955d011SMarcel Moolenaar GNode *gn; 25833955d011SMarcel Moolenaar 25843955d011SMarcel Moolenaar aborting = ABORT_INTERRUPT; 25853955d011SMarcel Moolenaar 25863955d011SMarcel Moolenaar JobSigLock(&mask); 25873955d011SMarcel Moolenaar 25883955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 2589e2eeea75SSimon J. Gerraty if (job->status != JOB_ST_RUNNING) 25903955d011SMarcel Moolenaar continue; 25913955d011SMarcel Moolenaar 25923955d011SMarcel Moolenaar gn = job->node; 25933955d011SMarcel Moolenaar 259445447996SSimon J. Gerraty JobDeleteTarget(gn); 259506b9b3e0SSimon J. Gerraty if (job->pid != 0) { 259606b9b3e0SSimon J. Gerraty DEBUG2(JOB, 259706b9b3e0SSimon J. Gerraty "JobInterrupt passing signal %d to child %d.\n", 25983955d011SMarcel Moolenaar signo, job->pid); 25993955d011SMarcel Moolenaar KILLPG(job->pid, signo); 26003955d011SMarcel Moolenaar } 26013955d011SMarcel Moolenaar } 26023955d011SMarcel Moolenaar 26033955d011SMarcel Moolenaar JobSigUnlock(&mask); 26043955d011SMarcel Moolenaar 26059f45a3c8SSimon J. Gerraty if (runINTERRUPT && !opts.touch) { 2606956e45f6SSimon J. Gerraty interrupt = Targ_FindNode(".INTERRUPT"); 26073955d011SMarcel Moolenaar if (interrupt != NULL) { 2608b0c40a00SSimon J. Gerraty opts.ignoreErrors = false; 26093955d011SMarcel Moolenaar JobRun(interrupt); 26103955d011SMarcel Moolenaar } 26113955d011SMarcel Moolenaar } 2612e2eeea75SSimon J. Gerraty Trace_Log(MAKEINTR, NULL); 261306b9b3e0SSimon J. Gerraty exit(signo); /* XXX: why signo? */ 26143955d011SMarcel Moolenaar } 26153955d011SMarcel Moolenaar 261606b9b3e0SSimon J. Gerraty /* 261706b9b3e0SSimon J. Gerraty * Do the final processing, i.e. run the commands attached to the .END target. 26183955d011SMarcel Moolenaar * 261906b9b3e0SSimon J. Gerraty * Return the number of errors reported. 262006b9b3e0SSimon J. Gerraty */ 26213955d011SMarcel Moolenaar int 26223955d011SMarcel Moolenaar Job_Finish(void) 26233955d011SMarcel Moolenaar { 2624956e45f6SSimon J. Gerraty GNode *endNode = Targ_GetEndNode(); 262506b9b3e0SSimon J. Gerraty if (!Lst_IsEmpty(&endNode->commands) || 262606b9b3e0SSimon J. Gerraty !Lst_IsEmpty(&endNode->children)) { 2627d5e0a182SSimon J. Gerraty if (job_errors != 0) 26283955d011SMarcel Moolenaar Error("Errors reported so .END ignored"); 2629d5e0a182SSimon J. Gerraty else 2630956e45f6SSimon J. Gerraty JobRun(endNode); 26313955d011SMarcel Moolenaar } 263206b9b3e0SSimon J. Gerraty return job_errors; 26333955d011SMarcel Moolenaar } 26343955d011SMarcel Moolenaar 2635956e45f6SSimon J. Gerraty /* Clean up any memory used by the jobs module. */ 26363955d011SMarcel Moolenaar void 26373955d011SMarcel Moolenaar Job_End(void) 26383955d011SMarcel Moolenaar { 26393955d011SMarcel Moolenaar #ifdef CLEANUP 2640dba7b0efSSimon J. Gerraty free(shell_freeIt); 26413955d011SMarcel Moolenaar #endif 26423955d011SMarcel Moolenaar } 26433955d011SMarcel Moolenaar 264406b9b3e0SSimon J. Gerraty /* 264506b9b3e0SSimon J. Gerraty * Waits for all running jobs to finish and returns. 264606b9b3e0SSimon J. Gerraty * Sets 'aborting' to ABORT_WAIT to prevent other jobs from starting. 264706b9b3e0SSimon J. Gerraty */ 26483955d011SMarcel Moolenaar void 26493955d011SMarcel Moolenaar Job_Wait(void) 26503955d011SMarcel Moolenaar { 26513955d011SMarcel Moolenaar aborting = ABORT_WAIT; 26523955d011SMarcel Moolenaar while (jobTokensRunning != 0) { 26533955d011SMarcel Moolenaar Job_CatchOutput(); 26543955d011SMarcel Moolenaar } 2655956e45f6SSimon J. Gerraty aborting = ABORT_NONE; 26563955d011SMarcel Moolenaar } 26573955d011SMarcel Moolenaar 265806b9b3e0SSimon J. Gerraty /* 265906b9b3e0SSimon J. Gerraty * Abort all currently running jobs without handling output or anything. 2660956e45f6SSimon J. Gerraty * This function is to be called only in the event of a major error. 2661956e45f6SSimon J. Gerraty * Most definitely NOT to be called from JobInterrupt. 26623955d011SMarcel Moolenaar * 266306b9b3e0SSimon J. Gerraty * All children are killed, not just the firstborn. 266406b9b3e0SSimon J. Gerraty */ 26653955d011SMarcel Moolenaar void 26663955d011SMarcel Moolenaar Job_AbortAll(void) 26673955d011SMarcel Moolenaar { 26683955d011SMarcel Moolenaar Job *job; /* the job descriptor in that element */ 26693955d011SMarcel Moolenaar WAIT_T foo; 26703955d011SMarcel Moolenaar 26713955d011SMarcel Moolenaar aborting = ABORT_ERROR; 26723955d011SMarcel Moolenaar 267306b9b3e0SSimon J. Gerraty if (jobTokensRunning != 0) { 26743955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 2675e2eeea75SSimon J. Gerraty if (job->status != JOB_ST_RUNNING) 26763955d011SMarcel Moolenaar continue; 26773955d011SMarcel Moolenaar /* 267806b9b3e0SSimon J. Gerraty * kill the child process with increasingly drastic 267906b9b3e0SSimon J. Gerraty * signals to make darn sure it's dead. 26803955d011SMarcel Moolenaar */ 26813955d011SMarcel Moolenaar KILLPG(job->pid, SIGINT); 26823955d011SMarcel Moolenaar KILLPG(job->pid, SIGKILL); 26833955d011SMarcel Moolenaar } 26843955d011SMarcel Moolenaar } 26853955d011SMarcel Moolenaar 26863955d011SMarcel Moolenaar /* 26873955d011SMarcel Moolenaar * Catch as many children as want to report in at first, then give up 26883955d011SMarcel Moolenaar */ 26893955d011SMarcel Moolenaar while (waitpid((pid_t)-1, &foo, WNOHANG) > 0) 26903955d011SMarcel Moolenaar continue; 26913955d011SMarcel Moolenaar } 26923955d011SMarcel Moolenaar 269306b9b3e0SSimon J. Gerraty /* 269406b9b3e0SSimon J. Gerraty * Tries to restart stopped jobs if there are slots available. 269506b9b3e0SSimon J. Gerraty * Called in process context in response to a SIGCONT. 269606b9b3e0SSimon J. Gerraty */ 26973955d011SMarcel Moolenaar static void 26983955d011SMarcel Moolenaar JobRestartJobs(void) 26993955d011SMarcel Moolenaar { 27003955d011SMarcel Moolenaar Job *job; 27013955d011SMarcel Moolenaar 27023955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 2703e2eeea75SSimon J. Gerraty if (job->status == JOB_ST_RUNNING && 2704e2eeea75SSimon J. Gerraty (make_suspended || job->suspended)) { 270506b9b3e0SSimon J. Gerraty DEBUG1(JOB, "Restarting stopped job pid %d.\n", 270606b9b3e0SSimon J. Gerraty job->pid); 2707e2eeea75SSimon J. Gerraty if (job->suspended) { 270806b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Continued\n", 270906b9b3e0SSimon J. Gerraty job->node->name); 27103955d011SMarcel Moolenaar (void)fflush(stdout); 27113955d011SMarcel Moolenaar } 2712b0c40a00SSimon J. Gerraty job->suspended = false; 27133955d011SMarcel Moolenaar if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) { 271406b9b3e0SSimon J. Gerraty debug_printf("Failed to send SIGCONT to %d\n", 271506b9b3e0SSimon J. Gerraty job->pid); 27163955d011SMarcel Moolenaar } 27173955d011SMarcel Moolenaar } 271806b9b3e0SSimon J. Gerraty if (job->status == JOB_ST_FINISHED) { 271906b9b3e0SSimon J. Gerraty /* 272006b9b3e0SSimon J. Gerraty * Job exit deferred after calling waitpid() in a 272106b9b3e0SSimon J. Gerraty * signal handler 272206b9b3e0SSimon J. Gerraty */ 27233955d011SMarcel Moolenaar JobFinish(job, job->exit_status); 27243955d011SMarcel Moolenaar } 272506b9b3e0SSimon J. Gerraty } 2726b0c40a00SSimon J. Gerraty make_suspended = false; 27273955d011SMarcel Moolenaar } 27283955d011SMarcel Moolenaar 27293955d011SMarcel Moolenaar static void 27303955d011SMarcel Moolenaar watchfd(Job *job) 27313955d011SMarcel Moolenaar { 27323955d011SMarcel Moolenaar if (job->inPollfd != NULL) 27333955d011SMarcel Moolenaar Punt("Watching watched job"); 27343955d011SMarcel Moolenaar 2735dba7b0efSSimon J. Gerraty fds[fdsLen].fd = job->inPipe; 2736dba7b0efSSimon J. Gerraty fds[fdsLen].events = POLLIN; 2737dba7b0efSSimon J. Gerraty jobByFdIndex[fdsLen] = job; 2738dba7b0efSSimon J. Gerraty job->inPollfd = &fds[fdsLen]; 2739dba7b0efSSimon J. Gerraty fdsLen++; 274049caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 274149caa483SSimon J. Gerraty if (useMeta) { 2742dba7b0efSSimon J. Gerraty fds[fdsLen].fd = meta_job_fd(job); 2743dba7b0efSSimon J. Gerraty fds[fdsLen].events = fds[fdsLen].fd == -1 ? 0 : POLLIN; 2744dba7b0efSSimon J. Gerraty jobByFdIndex[fdsLen] = job; 2745dba7b0efSSimon J. Gerraty fdsLen++; 274649caa483SSimon J. Gerraty } 274749caa483SSimon J. Gerraty #endif 27483955d011SMarcel Moolenaar } 27493955d011SMarcel Moolenaar 27503955d011SMarcel Moolenaar static void 27513955d011SMarcel Moolenaar clearfd(Job *job) 27523955d011SMarcel Moolenaar { 2753956e45f6SSimon J. Gerraty size_t i; 27543955d011SMarcel Moolenaar if (job->inPollfd == NULL) 27553955d011SMarcel Moolenaar Punt("Unwatching unwatched job"); 2756956e45f6SSimon J. Gerraty i = (size_t)(job->inPollfd - fds); 2757dba7b0efSSimon J. Gerraty fdsLen--; 275849caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 275949caa483SSimon J. Gerraty if (useMeta) { 276049caa483SSimon J. Gerraty /* 276149caa483SSimon J. Gerraty * Sanity check: there should be two fds per job, so the job's 276249caa483SSimon J. Gerraty * pollfd number should be even. 276349caa483SSimon J. Gerraty */ 276449caa483SSimon J. Gerraty assert(nfds_per_job() == 2); 276506b9b3e0SSimon J. Gerraty if (i % 2 != 0) 276649caa483SSimon J. Gerraty Punt("odd-numbered fd with meta"); 2767dba7b0efSSimon J. Gerraty fdsLen--; 276849caa483SSimon J. Gerraty } 276949caa483SSimon J. Gerraty #endif 2770d5e0a182SSimon J. Gerraty /* Move last job in table into hole made by dead job. */ 2771dba7b0efSSimon J. Gerraty if (fdsLen != i) { 2772dba7b0efSSimon J. Gerraty fds[i] = fds[fdsLen]; 2773dba7b0efSSimon J. Gerraty jobByFdIndex[i] = jobByFdIndex[fdsLen]; 2774dba7b0efSSimon J. Gerraty jobByFdIndex[i]->inPollfd = &fds[i]; 277549caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 277649caa483SSimon J. Gerraty if (useMeta) { 2777dba7b0efSSimon J. Gerraty fds[i + 1] = fds[fdsLen + 1]; 2778dba7b0efSSimon J. Gerraty jobByFdIndex[i + 1] = jobByFdIndex[fdsLen + 1]; 277949caa483SSimon J. Gerraty } 278049caa483SSimon J. Gerraty #endif 27813955d011SMarcel Moolenaar } 27823955d011SMarcel Moolenaar job->inPollfd = NULL; 27833955d011SMarcel Moolenaar } 27843955d011SMarcel Moolenaar 2785b0c40a00SSimon J. Gerraty static bool 27863955d011SMarcel Moolenaar readyfd(Job *job) 27873955d011SMarcel Moolenaar { 27883955d011SMarcel Moolenaar if (job->inPollfd == NULL) 27893955d011SMarcel Moolenaar Punt("Polling unwatched job"); 27903955d011SMarcel Moolenaar return (job->inPollfd->revents & POLLIN) != 0; 27913955d011SMarcel Moolenaar } 27923955d011SMarcel Moolenaar 279306b9b3e0SSimon J. Gerraty /* 279406b9b3e0SSimon J. Gerraty * Put a token (back) into the job pipe. 279506b9b3e0SSimon J. Gerraty * This allows a make process to start a build job. 279606b9b3e0SSimon J. Gerraty */ 27973955d011SMarcel Moolenaar static void 27983955d011SMarcel Moolenaar JobTokenAdd(void) 27993955d011SMarcel Moolenaar { 28003955d011SMarcel Moolenaar char tok = JOB_TOKENS[aborting], tok1; 28013955d011SMarcel Moolenaar 28022d395cb5SSimon J. Gerraty if (!Job_error_token && aborting == ABORT_ERROR) { 28032d395cb5SSimon J. Gerraty if (jobTokensRunning == 0) 28042d395cb5SSimon J. Gerraty return; 28052d395cb5SSimon J. Gerraty tok = '+'; /* no error token */ 28062d395cb5SSimon J. Gerraty } 28072d395cb5SSimon J. Gerraty 28083955d011SMarcel Moolenaar /* If we are depositing an error token flush everything else */ 28093955d011SMarcel Moolenaar while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1) 28103955d011SMarcel Moolenaar continue; 28113955d011SMarcel Moolenaar 2812956e45f6SSimon J. Gerraty DEBUG3(JOB, "(%d) aborting %d, deposit token %c\n", 28132d395cb5SSimon J. Gerraty getpid(), aborting, tok); 28143cbdda60SSimon J. Gerraty while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN) 28153cbdda60SSimon J. Gerraty continue; 28163955d011SMarcel Moolenaar } 28173955d011SMarcel Moolenaar 2818dba7b0efSSimon J. Gerraty /* Get a temp file */ 2819dba7b0efSSimon J. Gerraty int 2820dba7b0efSSimon J. Gerraty Job_TempFile(const char *pattern, char *tfile, size_t tfile_sz) 2821dba7b0efSSimon J. Gerraty { 2822dba7b0efSSimon J. Gerraty int fd; 2823dba7b0efSSimon J. Gerraty sigset_t mask; 2824dba7b0efSSimon J. Gerraty 2825dba7b0efSSimon J. Gerraty JobSigLock(&mask); 2826dba7b0efSSimon J. Gerraty fd = mkTempFile(pattern, tfile, tfile_sz); 2827dba7b0efSSimon J. Gerraty if (tfile != NULL && !DEBUG(SCRIPT)) 2828dba7b0efSSimon J. Gerraty unlink(tfile); 2829dba7b0efSSimon J. Gerraty JobSigUnlock(&mask); 2830dba7b0efSSimon J. Gerraty 2831dba7b0efSSimon J. Gerraty return fd; 2832dba7b0efSSimon J. Gerraty } 2833dba7b0efSSimon J. Gerraty 2834956e45f6SSimon J. Gerraty /* Prep the job token pipe in the root make process. */ 28353955d011SMarcel Moolenaar void 28363955d011SMarcel Moolenaar Job_ServerStart(int max_tokens, int jp_0, int jp_1) 28373955d011SMarcel Moolenaar { 28383955d011SMarcel Moolenaar int i; 28393955d011SMarcel Moolenaar char jobarg[64]; 28403955d011SMarcel Moolenaar 28413955d011SMarcel Moolenaar if (jp_0 >= 0 && jp_1 >= 0) { 28423955d011SMarcel Moolenaar /* Pipe passed in from parent */ 28433955d011SMarcel Moolenaar tokenWaitJob.inPipe = jp_0; 28443955d011SMarcel Moolenaar tokenWaitJob.outPipe = jp_1; 2845be19d90bSSimon J. Gerraty (void)fcntl(jp_0, F_SETFD, FD_CLOEXEC); 2846be19d90bSSimon J. Gerraty (void)fcntl(jp_1, F_SETFD, FD_CLOEXEC); 28473955d011SMarcel Moolenaar return; 28483955d011SMarcel Moolenaar } 28493955d011SMarcel Moolenaar 28503955d011SMarcel Moolenaar JobCreatePipe(&tokenWaitJob, 15); 28513955d011SMarcel Moolenaar 2852e2eeea75SSimon J. Gerraty snprintf(jobarg, sizeof jobarg, "%d,%d", 28533955d011SMarcel Moolenaar tokenWaitJob.inPipe, tokenWaitJob.outPipe); 28543955d011SMarcel Moolenaar 2855dba7b0efSSimon J. Gerraty Global_Append(MAKEFLAGS, "-J"); 2856dba7b0efSSimon J. Gerraty Global_Append(MAKEFLAGS, jobarg); 28573955d011SMarcel Moolenaar 28583955d011SMarcel Moolenaar /* 28593955d011SMarcel Moolenaar * Preload the job pipe with one token per job, save the one 28603955d011SMarcel Moolenaar * "extra" token for the primary job. 28613955d011SMarcel Moolenaar * 28623955d011SMarcel Moolenaar * XXX should clip maxJobs against PIPE_BUF -- if max_tokens is 28633955d011SMarcel Moolenaar * larger than the write buffer size of the pipe, we will 28643955d011SMarcel Moolenaar * deadlock here. 28653955d011SMarcel Moolenaar */ 28663955d011SMarcel Moolenaar for (i = 1; i < max_tokens; i++) 28673955d011SMarcel Moolenaar JobTokenAdd(); 28683955d011SMarcel Moolenaar } 28693955d011SMarcel Moolenaar 2870956e45f6SSimon J. Gerraty /* Return a withdrawn token to the pool. */ 28713955d011SMarcel Moolenaar void 28723955d011SMarcel Moolenaar Job_TokenReturn(void) 28733955d011SMarcel Moolenaar { 28743955d011SMarcel Moolenaar jobTokensRunning--; 28753955d011SMarcel Moolenaar if (jobTokensRunning < 0) 28763955d011SMarcel Moolenaar Punt("token botch"); 287706b9b3e0SSimon J. Gerraty if (jobTokensRunning != 0 || JOB_TOKENS[aborting] != '+') 28783955d011SMarcel Moolenaar JobTokenAdd(); 28793955d011SMarcel Moolenaar } 28803955d011SMarcel Moolenaar 288106b9b3e0SSimon J. Gerraty /* 288206b9b3e0SSimon J. Gerraty * Attempt to withdraw a token from the pool. 28833955d011SMarcel Moolenaar * 2884956e45f6SSimon J. Gerraty * If pool is empty, set wantToken so that we wake up when a token is 2885956e45f6SSimon J. Gerraty * released. 28863955d011SMarcel Moolenaar * 2887b0c40a00SSimon J. Gerraty * Returns true if a token was withdrawn, and false if the pool is currently 288806b9b3e0SSimon J. Gerraty * empty. 288906b9b3e0SSimon J. Gerraty */ 2890b0c40a00SSimon J. Gerraty bool 28913955d011SMarcel Moolenaar Job_TokenWithdraw(void) 28923955d011SMarcel Moolenaar { 28933955d011SMarcel Moolenaar char tok, tok1; 2894956e45f6SSimon J. Gerraty ssize_t count; 28953955d011SMarcel Moolenaar 28963955d011SMarcel Moolenaar wantToken = 0; 2897956e45f6SSimon J. Gerraty DEBUG3(JOB, "Job_TokenWithdraw(%d): aborting %d, running %d\n", 28983955d011SMarcel Moolenaar getpid(), aborting, jobTokensRunning); 28993955d011SMarcel Moolenaar 2900956e45f6SSimon J. Gerraty if (aborting != ABORT_NONE || (jobTokensRunning >= opts.maxJobs)) 2901b0c40a00SSimon J. Gerraty return false; 29023955d011SMarcel Moolenaar 29033955d011SMarcel Moolenaar count = read(tokenWaitJob.inPipe, &tok, 1); 29043955d011SMarcel Moolenaar if (count == 0) 29053955d011SMarcel Moolenaar Fatal("eof on job pipe!"); 29063955d011SMarcel Moolenaar if (count < 0 && jobTokensRunning != 0) { 2907d5e0a182SSimon J. Gerraty if (errno != EAGAIN) 29083955d011SMarcel Moolenaar Fatal("job pipe read: %s", strerror(errno)); 2909956e45f6SSimon J. Gerraty DEBUG1(JOB, "(%d) blocked for token\n", getpid()); 291006b9b3e0SSimon J. Gerraty wantToken = 1; 2911b0c40a00SSimon J. Gerraty return false; 29123955d011SMarcel Moolenaar } 29133955d011SMarcel Moolenaar 29143955d011SMarcel Moolenaar if (count == 1 && tok != '+') { 291506b9b3e0SSimon J. Gerraty /* make being aborted - remove any other job tokens */ 2916956e45f6SSimon J. Gerraty DEBUG2(JOB, "(%d) aborted by token %c\n", getpid(), tok); 29173955d011SMarcel Moolenaar while (read(tokenWaitJob.inPipe, &tok1, 1) == 1) 29183955d011SMarcel Moolenaar continue; 29193955d011SMarcel Moolenaar /* And put the stopper back */ 292006b9b3e0SSimon J. Gerraty while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && 292106b9b3e0SSimon J. Gerraty errno == EAGAIN) 29223cbdda60SSimon J. Gerraty continue; 2923e2eeea75SSimon J. Gerraty if (shouldDieQuietly(NULL, 1)) 292406b9b3e0SSimon J. Gerraty exit(6); /* we aborted */ 292506b9b3e0SSimon J. Gerraty Fatal("A failure has been detected " 292606b9b3e0SSimon J. Gerraty "in another branch of the parallel make"); 29273955d011SMarcel Moolenaar } 29283955d011SMarcel Moolenaar 29293955d011SMarcel Moolenaar if (count == 1 && jobTokensRunning == 0) 29303955d011SMarcel Moolenaar /* We didn't want the token really */ 293106b9b3e0SSimon J. Gerraty while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && 293206b9b3e0SSimon J. Gerraty errno == EAGAIN) 29333cbdda60SSimon J. Gerraty continue; 29343955d011SMarcel Moolenaar 29353955d011SMarcel Moolenaar jobTokensRunning++; 2936956e45f6SSimon J. Gerraty DEBUG1(JOB, "(%d) withdrew token\n", getpid()); 2937b0c40a00SSimon J. Gerraty return true; 29383955d011SMarcel Moolenaar } 29393955d011SMarcel Moolenaar 294006b9b3e0SSimon J. Gerraty /* 294106b9b3e0SSimon J. Gerraty * Run the named target if found. If a filename is specified, then set that 2942956e45f6SSimon J. Gerraty * to the sources. 29431748de26SSimon J. Gerraty * 294406b9b3e0SSimon J. Gerraty * Exits if the target fails. 294506b9b3e0SSimon J. Gerraty */ 2946b0c40a00SSimon J. Gerraty bool 294706b9b3e0SSimon J. Gerraty Job_RunTarget(const char *target, const char *fname) 294806b9b3e0SSimon J. Gerraty { 2949956e45f6SSimon J. Gerraty GNode *gn = Targ_FindNode(target); 29501748de26SSimon J. Gerraty if (gn == NULL) 2951b0c40a00SSimon J. Gerraty return false; 29521748de26SSimon J. Gerraty 295306b9b3e0SSimon J. Gerraty if (fname != NULL) 2954dba7b0efSSimon J. Gerraty Var_Set(gn, ALLSRC, fname); 29551748de26SSimon J. Gerraty 29561748de26SSimon J. Gerraty JobRun(gn); 295706b9b3e0SSimon J. Gerraty /* XXX: Replace with GNode_IsError(gn) */ 29581748de26SSimon J. Gerraty if (gn->made == ERROR) { 29599f45a3c8SSimon J. Gerraty PrintOnError(gn, "\n\nStop.\n"); 29601748de26SSimon J. Gerraty exit(1); 29611748de26SSimon J. Gerraty } 2962b0c40a00SSimon J. Gerraty return true; 29631748de26SSimon J. Gerraty } 29641748de26SSimon J. Gerraty 29653955d011SMarcel Moolenaar #ifdef USE_SELECT 29663955d011SMarcel Moolenaar int 29673955d011SMarcel Moolenaar emul_poll(struct pollfd *fd, int nfd, int timeout) 29683955d011SMarcel Moolenaar { 29693955d011SMarcel Moolenaar fd_set rfds, wfds; 29703955d011SMarcel Moolenaar int i, maxfd, nselect, npoll; 29713955d011SMarcel Moolenaar struct timeval tv, *tvp; 29723955d011SMarcel Moolenaar long usecs; 29733955d011SMarcel Moolenaar 29743955d011SMarcel Moolenaar FD_ZERO(&rfds); 29753955d011SMarcel Moolenaar FD_ZERO(&wfds); 29763955d011SMarcel Moolenaar 29773955d011SMarcel Moolenaar maxfd = -1; 29783955d011SMarcel Moolenaar for (i = 0; i < nfd; i++) { 29793955d011SMarcel Moolenaar fd[i].revents = 0; 29803955d011SMarcel Moolenaar 29813955d011SMarcel Moolenaar if (fd[i].events & POLLIN) 29823955d011SMarcel Moolenaar FD_SET(fd[i].fd, &rfds); 29833955d011SMarcel Moolenaar 29843955d011SMarcel Moolenaar if (fd[i].events & POLLOUT) 29853955d011SMarcel Moolenaar FD_SET(fd[i].fd, &wfds); 29863955d011SMarcel Moolenaar 29873955d011SMarcel Moolenaar if (fd[i].fd > maxfd) 29883955d011SMarcel Moolenaar maxfd = fd[i].fd; 29893955d011SMarcel Moolenaar } 29903955d011SMarcel Moolenaar 29913955d011SMarcel Moolenaar if (maxfd >= FD_SETSIZE) { 29923955d011SMarcel Moolenaar Punt("Ran out of fd_set slots; " 29933955d011SMarcel Moolenaar "recompile with a larger FD_SETSIZE."); 29943955d011SMarcel Moolenaar } 29953955d011SMarcel Moolenaar 29963955d011SMarcel Moolenaar if (timeout < 0) { 29973955d011SMarcel Moolenaar tvp = NULL; 29983955d011SMarcel Moolenaar } else { 29993955d011SMarcel Moolenaar usecs = timeout * 1000; 30003955d011SMarcel Moolenaar tv.tv_sec = usecs / 1000000; 30013955d011SMarcel Moolenaar tv.tv_usec = usecs % 1000000; 30023955d011SMarcel Moolenaar tvp = &tv; 30033955d011SMarcel Moolenaar } 30043955d011SMarcel Moolenaar 3005e2eeea75SSimon J. Gerraty nselect = select(maxfd + 1, &rfds, &wfds, NULL, tvp); 30063955d011SMarcel Moolenaar 30073955d011SMarcel Moolenaar if (nselect <= 0) 30083955d011SMarcel Moolenaar return nselect; 30093955d011SMarcel Moolenaar 30103955d011SMarcel Moolenaar npoll = 0; 30113955d011SMarcel Moolenaar for (i = 0; i < nfd; i++) { 30123955d011SMarcel Moolenaar if (FD_ISSET(fd[i].fd, &rfds)) 30133955d011SMarcel Moolenaar fd[i].revents |= POLLIN; 30143955d011SMarcel Moolenaar 30153955d011SMarcel Moolenaar if (FD_ISSET(fd[i].fd, &wfds)) 30163955d011SMarcel Moolenaar fd[i].revents |= POLLOUT; 30173955d011SMarcel Moolenaar 30183955d011SMarcel Moolenaar if (fd[i].revents) 30193955d011SMarcel Moolenaar npoll++; 30203955d011SMarcel Moolenaar } 30213955d011SMarcel Moolenaar 30223955d011SMarcel Moolenaar return npoll; 30233955d011SMarcel Moolenaar } 30243955d011SMarcel Moolenaar #endif /* USE_SELECT */ 3025