1*d5e0a182SSimon J. Gerraty /* $NetBSD: job.c,v 1.465 2024/01/07 11:39:04 rillig 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 /* 73*d5e0a182SSimon 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, 77*d5e0a182SSimon 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*d5e0a182SSimon J. Gerraty MAKE_RCSID("$NetBSD: job.c,v 1.465 2024/01/07 11:39:04 rillig 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 276*d5e0a182SSimon 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 286*d5e0a182SSimon 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 315*d5e0a182SSimon J. Gerraty #else 3163955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH 0 3173955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH 1 3183955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH 2 319*d5e0a182SSimon J. Gerraty #endif 3203955d011SMarcel Moolenaar 3213955d011SMarcel Moolenaar #ifndef DEFSHELL_INDEX 3223955d011SMarcel Moolenaar #define DEFSHELL_INDEX 0 /* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */ 323*d5e0a182SSimon 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 */ 439*d5e0a182SSimon 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++) { 574*d5e0a182SSimon 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 * 707*d5e0a182SSimon J. Gerraty * A whole host of signals is going to happen! 7083955d011SMarcel Moolenaar * SIGCHLD for any processes that actually suspended themselves. 709*d5e0a182SSimon 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)) 759*d5e0a182SSimon 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 { 889*d5e0a182SSimon J. Gerraty if (!run) 890b0c40a00SSimon J. Gerraty inout_cmdFlags->ignerr = false; 891*d5e0a182SSimon 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 9338c973ee2SSimon J. Gerraty xcmd = Var_Subst(ucmd, job->node, VARE_WANTRES); 934956e45f6SSimon J. Gerraty /* TODO: handle errors */ 93506b9b3e0SSimon J. Gerraty xcmdStart = xcmd; 9363955d011SMarcel Moolenaar 9373955d011SMarcel Moolenaar cmdTemplate = "%s\n"; 9383955d011SMarcel Moolenaar 93906b9b3e0SSimon J. Gerraty ParseCommandFlags(&xcmd, &cmdFlags); 940956e45f6SSimon J. Gerraty 94106b9b3e0SSimon J. Gerraty /* The '+' command flag overrides the -n or -N options. */ 94206b9b3e0SSimon J. Gerraty if (cmdFlags.always && !run) { 9433955d011SMarcel Moolenaar /* 9443955d011SMarcel Moolenaar * We're not actually executing anything... 9453955d011SMarcel Moolenaar * but this one needs to be - use compat mode just for it. 9463955d011SMarcel Moolenaar */ 9479f45a3c8SSimon J. Gerraty (void)Compat_RunCommand(ucmd, job->node, ln); 94806b9b3e0SSimon J. Gerraty free(xcmdStart); 949956e45f6SSimon J. Gerraty return; 9503955d011SMarcel Moolenaar } 9513955d011SMarcel Moolenaar 9523955d011SMarcel Moolenaar /* 95306b9b3e0SSimon J. Gerraty * If the shell doesn't have error control, the alternate echoing 95406b9b3e0SSimon J. Gerraty * will be done (to avoid showing additional error checking code) 95506b9b3e0SSimon J. Gerraty * and this needs some characters escaped. 9563955d011SMarcel Moolenaar */ 95706b9b3e0SSimon J. Gerraty escCmd = shell->hasErrCtl ? NULL : EscapeShellDblQuot(xcmd); 9583955d011SMarcel Moolenaar 95906b9b3e0SSimon J. Gerraty if (!cmdFlags.echo) { 960*d5e0a182SSimon J. Gerraty if (job->echo && run && shell->hasEchoCtl) 96106b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(wr); 962*d5e0a182SSimon J. Gerraty else if (shell->hasErrCtl) 963b0c40a00SSimon J. Gerraty cmdFlags.echo = true; 9643955d011SMarcel Moolenaar } 9653955d011SMarcel Moolenaar 96606b9b3e0SSimon J. Gerraty if (cmdFlags.ignerr) { 967b0c40a00SSimon J. Gerraty JobWriteSpecials(job, wr, escCmd, run, &cmdFlags, &cmdTemplate); 9683955d011SMarcel Moolenaar } else { 9693955d011SMarcel Moolenaar 9703955d011SMarcel Moolenaar /* 97106b9b3e0SSimon J. Gerraty * If errors are being checked and the shell doesn't have 97206b9b3e0SSimon J. Gerraty * error control but does supply an runChkTmpl template, then 97306b9b3e0SSimon J. Gerraty * set up commands to run through it. 9743955d011SMarcel Moolenaar */ 9753955d011SMarcel Moolenaar 97606b9b3e0SSimon J. Gerraty if (!shell->hasErrCtl && shell->runChkTmpl != NULL && 97706b9b3e0SSimon J. Gerraty shell->runChkTmpl[0] != '\0') { 97806b9b3e0SSimon J. Gerraty if (job->echo && cmdFlags.echo) { 97906b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(wr); 98006b9b3e0SSimon J. Gerraty ShellWriter_EchoCmd(wr, escCmd); 981b0c40a00SSimon J. Gerraty cmdFlags.echo = false; 9823955d011SMarcel Moolenaar } 98306b9b3e0SSimon J. Gerraty /* 98406b9b3e0SSimon J. Gerraty * If it's a comment line or blank, avoid the possible 98506b9b3e0SSimon J. Gerraty * syntax error generated by "{\n} || exit $?". 98606b9b3e0SSimon J. Gerraty */ 98706b9b3e0SSimon J. Gerraty cmdTemplate = escCmd[0] == shell->commentChar || 98806b9b3e0SSimon J. Gerraty escCmd[0] == '\0' 98906b9b3e0SSimon J. Gerraty ? shell->runIgnTmpl 99006b9b3e0SSimon J. Gerraty : shell->runChkTmpl; 991b0c40a00SSimon J. Gerraty cmdFlags.ignerr = false; 9923955d011SMarcel Moolenaar } 9933955d011SMarcel Moolenaar } 9943955d011SMarcel Moolenaar 99506b9b3e0SSimon J. Gerraty if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0) 99606b9b3e0SSimon J. Gerraty ShellWriter_TraceOn(wr); 9973955d011SMarcel Moolenaar 998b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(wr, cmdTemplate, xcmd); 99906b9b3e0SSimon J. Gerraty free(xcmdStart); 10003955d011SMarcel Moolenaar free(escCmd); 100106b9b3e0SSimon J. Gerraty 100206b9b3e0SSimon J. Gerraty if (cmdFlags.ignerr) 100306b9b3e0SSimon J. Gerraty ShellWriter_ErrOn(wr, cmdFlags.echo && job->echo); 100406b9b3e0SSimon J. Gerraty 100506b9b3e0SSimon J. Gerraty if (!cmdFlags.echo) 100606b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(wr); 10073955d011SMarcel Moolenaar } 10083955d011SMarcel Moolenaar 100906b9b3e0SSimon J. Gerraty /* 1010b0c40a00SSimon J. Gerraty * Write all commands to the shell file that is later executed. 10113955d011SMarcel Moolenaar * 1012b0c40a00SSimon J. Gerraty * The special command "..." stops writing and saves the remaining commands 1013dba7b0efSSimon J. Gerraty * to be executed later, when the target '.END' is made. 101406b9b3e0SSimon J. Gerraty * 101506b9b3e0SSimon J. Gerraty * Return whether at least one command was written to the shell file. 101606b9b3e0SSimon J. Gerraty */ 1017b0c40a00SSimon J. Gerraty static bool 1018b0c40a00SSimon J. Gerraty JobWriteCommands(Job *job) 10193955d011SMarcel Moolenaar { 1020956e45f6SSimon J. Gerraty StringListNode *ln; 1021b0c40a00SSimon J. Gerraty bool seen = false; 1022b0c40a00SSimon J. Gerraty ShellWriter wr; 1023b0c40a00SSimon J. Gerraty 1024b0c40a00SSimon J. Gerraty wr.f = job->cmdFILE; 1025b0c40a00SSimon J. Gerraty wr.xtraced = false; 1026956e45f6SSimon J. Gerraty 102706b9b3e0SSimon J. Gerraty for (ln = job->node->commands.first; ln != NULL; ln = ln->next) { 1028956e45f6SSimon J. Gerraty const char *cmd = ln->datum; 1029956e45f6SSimon J. Gerraty 1030956e45f6SSimon J. Gerraty if (strcmp(cmd, "...") == 0) { 1031956e45f6SSimon J. Gerraty job->node->type |= OP_SAVE_CMDS; 1032956e45f6SSimon J. Gerraty job->tailCmds = ln->next; 1033956e45f6SSimon J. Gerraty break; 1034956e45f6SSimon J. Gerraty } 1035e2eeea75SSimon J. Gerraty 1036b0c40a00SSimon J. Gerraty JobWriteCommand(job, &wr, ln, ln->datum); 1037b0c40a00SSimon J. Gerraty seen = true; 1038956e45f6SSimon J. Gerraty } 103906b9b3e0SSimon J. Gerraty 104006b9b3e0SSimon J. Gerraty return seen; 1041956e45f6SSimon J. Gerraty } 1042956e45f6SSimon J. Gerraty 1043dba7b0efSSimon J. Gerraty /* 1044dba7b0efSSimon J. Gerraty * Save the delayed commands (those after '...'), to be executed later in 1045dba7b0efSSimon J. Gerraty * the '.END' node, when everything else is done. 1046dba7b0efSSimon J. Gerraty */ 1047956e45f6SSimon J. Gerraty static void 1048956e45f6SSimon J. Gerraty JobSaveCommands(Job *job) 1049956e45f6SSimon J. Gerraty { 105006b9b3e0SSimon J. Gerraty StringListNode *ln; 1051956e45f6SSimon J. Gerraty 105206b9b3e0SSimon J. Gerraty for (ln = job->tailCmds; ln != NULL; ln = ln->next) { 105306b9b3e0SSimon J. Gerraty const char *cmd = ln->datum; 1054956e45f6SSimon J. Gerraty char *expanded_cmd; 1055dba7b0efSSimon J. Gerraty /* 1056dba7b0efSSimon J. Gerraty * XXX: This Var_Subst is only intended to expand the dynamic 1057956e45f6SSimon J. Gerraty * variables such as .TARGET, .IMPSRC. It is not intended to 1058dba7b0efSSimon J. Gerraty * expand the other variables as well; see deptgt-end.mk. 1059dba7b0efSSimon J. Gerraty */ 10608c973ee2SSimon J. Gerraty expanded_cmd = Var_Subst(cmd, job->node, VARE_WANTRES); 1061956e45f6SSimon J. Gerraty /* TODO: handle errors */ 106206b9b3e0SSimon J. Gerraty Lst_Append(&Targ_GetEndNode()->commands, expanded_cmd); 1063956e45f6SSimon J. Gerraty } 10643955d011SMarcel Moolenaar } 10653955d011SMarcel Moolenaar 10663955d011SMarcel Moolenaar 1067956e45f6SSimon J. Gerraty /* Called to close both input and output pipes when a job is finished. */ 10683955d011SMarcel Moolenaar static void 1069e2eeea75SSimon J. Gerraty JobClosePipes(Job *job) 10703955d011SMarcel Moolenaar { 10713955d011SMarcel Moolenaar clearfd(job); 10723955d011SMarcel Moolenaar (void)close(job->outPipe); 10733955d011SMarcel Moolenaar job->outPipe = -1; 10743955d011SMarcel Moolenaar 1075b0c40a00SSimon J. Gerraty CollectOutput(job, true); 10763955d011SMarcel Moolenaar (void)close(job->inPipe); 10773955d011SMarcel Moolenaar job->inPipe = -1; 10783955d011SMarcel Moolenaar } 10793955d011SMarcel Moolenaar 108006b9b3e0SSimon J. Gerraty static void 1081b0c40a00SSimon J. Gerraty DebugFailedJob(const Job *job) 1082b0c40a00SSimon J. Gerraty { 1083b0c40a00SSimon J. Gerraty const StringListNode *ln; 1084b0c40a00SSimon J. Gerraty 1085b0c40a00SSimon J. Gerraty if (!DEBUG(ERROR)) 1086b0c40a00SSimon J. Gerraty return; 1087b0c40a00SSimon J. Gerraty 108812904384SSimon J. Gerraty debug_printf("\n"); 108912904384SSimon J. Gerraty debug_printf("*** Failed target: %s\n", job->node->name); 109012904384SSimon J. Gerraty debug_printf("*** Failed commands:\n"); 109112904384SSimon J. Gerraty for (ln = job->node->commands.first; ln != NULL; ln = ln->next) { 109212904384SSimon J. Gerraty const char *cmd = ln->datum; 109312904384SSimon J. Gerraty debug_printf("\t%s\n", cmd); 109412904384SSimon J. Gerraty 109512904384SSimon J. Gerraty if (strchr(cmd, '$') != NULL) { 10968c973ee2SSimon J. Gerraty char *xcmd = Var_Subst(cmd, job->node, VARE_WANTRES); 109712904384SSimon J. Gerraty debug_printf("\t=> %s\n", xcmd); 109812904384SSimon J. Gerraty free(xcmd); 109912904384SSimon J. Gerraty } 110012904384SSimon J. Gerraty } 1101b0c40a00SSimon J. Gerraty } 1102b0c40a00SSimon J. Gerraty 1103b0c40a00SSimon J. Gerraty static void 110406b9b3e0SSimon J. Gerraty JobFinishDoneExitedError(Job *job, WAIT_T *inout_status) 110506b9b3e0SSimon J. Gerraty { 110606b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 110706b9b3e0SSimon J. Gerraty #ifdef USE_META 110806b9b3e0SSimon J. Gerraty if (useMeta) { 110906b9b3e0SSimon J. Gerraty meta_job_error(job, job->node, 111006b9b3e0SSimon J. Gerraty job->ignerr, WEXITSTATUS(*inout_status)); 111106b9b3e0SSimon J. Gerraty } 111206b9b3e0SSimon J. Gerraty #endif 111306b9b3e0SSimon J. Gerraty if (!shouldDieQuietly(job->node, -1)) { 1114b0c40a00SSimon J. Gerraty DebugFailedJob(job); 111506b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Error code %d%s\n", 111606b9b3e0SSimon J. Gerraty job->node->name, WEXITSTATUS(*inout_status), 111706b9b3e0SSimon J. Gerraty job->ignerr ? " (ignored)" : ""); 111806b9b3e0SSimon J. Gerraty } 111906b9b3e0SSimon J. Gerraty 112006b9b3e0SSimon J. Gerraty if (job->ignerr) 112106b9b3e0SSimon J. Gerraty WAIT_STATUS(*inout_status) = 0; 112206b9b3e0SSimon J. Gerraty else { 112306b9b3e0SSimon J. Gerraty if (deleteOnError) 112406b9b3e0SSimon J. Gerraty JobDeleteTarget(job->node); 11259f45a3c8SSimon J. Gerraty PrintOnError(job->node, "\n"); 112606b9b3e0SSimon J. Gerraty } 112706b9b3e0SSimon J. Gerraty } 112806b9b3e0SSimon J. Gerraty 112906b9b3e0SSimon J. Gerraty static void 113006b9b3e0SSimon J. Gerraty JobFinishDoneExited(Job *job, WAIT_T *inout_status) 113106b9b3e0SSimon J. Gerraty { 113206b9b3e0SSimon J. Gerraty DEBUG2(JOB, "Process %d [%s] exited.\n", job->pid, job->node->name); 113306b9b3e0SSimon J. Gerraty 113406b9b3e0SSimon J. Gerraty if (WEXITSTATUS(*inout_status) != 0) 113506b9b3e0SSimon J. Gerraty JobFinishDoneExitedError(job, inout_status); 113606b9b3e0SSimon J. Gerraty else if (DEBUG(JOB)) { 113706b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 113806b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Completed successfully\n", 113906b9b3e0SSimon J. Gerraty job->node->name); 114006b9b3e0SSimon J. Gerraty } 114106b9b3e0SSimon J. Gerraty } 114206b9b3e0SSimon J. Gerraty 114306b9b3e0SSimon J. Gerraty static void 114406b9b3e0SSimon J. Gerraty JobFinishDoneSignaled(Job *job, WAIT_T status) 114506b9b3e0SSimon J. Gerraty { 114606b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 1147b0c40a00SSimon J. Gerraty DebugFailedJob(job); 114806b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Signal %d\n", job->node->name, WTERMSIG(status)); 114906b9b3e0SSimon J. Gerraty if (deleteOnError) 115006b9b3e0SSimon J. Gerraty JobDeleteTarget(job->node); 115106b9b3e0SSimon J. Gerraty } 115206b9b3e0SSimon J. Gerraty 115306b9b3e0SSimon J. Gerraty static void 115406b9b3e0SSimon J. Gerraty JobFinishDone(Job *job, WAIT_T *inout_status) 115506b9b3e0SSimon J. Gerraty { 115606b9b3e0SSimon J. Gerraty if (WIFEXITED(*inout_status)) 115706b9b3e0SSimon J. Gerraty JobFinishDoneExited(job, inout_status); 115806b9b3e0SSimon J. Gerraty else 115906b9b3e0SSimon J. Gerraty JobFinishDoneSignaled(job, *inout_status); 116006b9b3e0SSimon J. Gerraty 116106b9b3e0SSimon J. Gerraty (void)fflush(stdout); 116206b9b3e0SSimon J. Gerraty } 116306b9b3e0SSimon J. Gerraty 116406b9b3e0SSimon J. Gerraty /* 116506b9b3e0SSimon J. Gerraty * Do final processing for the given job including updating parent nodes and 1166e2eeea75SSimon J. Gerraty * starting new jobs as available/necessary. 1167e2eeea75SSimon J. Gerraty * 1168e2eeea75SSimon J. Gerraty * Deferred commands for the job are placed on the .END node. 1169e2eeea75SSimon J. Gerraty * 117006b9b3e0SSimon J. Gerraty * If there was a serious error (job_errors != 0; not an ignored one), no more 1171e2eeea75SSimon J. Gerraty * jobs will be started. 11723955d011SMarcel Moolenaar * 11733955d011SMarcel Moolenaar * Input: 11743955d011SMarcel Moolenaar * job job to finish 11753955d011SMarcel Moolenaar * status sub-why job went away 11763955d011SMarcel Moolenaar */ 11773955d011SMarcel Moolenaar static void 11783955d011SMarcel Moolenaar JobFinish (Job *job, WAIT_T status) 11793955d011SMarcel Moolenaar { 1180b0c40a00SSimon J. Gerraty bool done, return_job_token; 11813955d011SMarcel Moolenaar 1182956e45f6SSimon J. Gerraty DEBUG3(JOB, "JobFinish: %d [%s], status %d\n", 11833955d011SMarcel Moolenaar job->pid, job->node->name, status); 11843955d011SMarcel Moolenaar 11853955d011SMarcel Moolenaar if ((WIFEXITED(status) && 118606b9b3e0SSimon J. Gerraty ((WEXITSTATUS(status) != 0 && !job->ignerr))) || 118706b9b3e0SSimon J. Gerraty WIFSIGNALED(status)) { 118806b9b3e0SSimon J. Gerraty /* Finished because of an error. */ 118906b9b3e0SSimon J. Gerraty 1190e2eeea75SSimon J. Gerraty JobClosePipes(job); 11913955d011SMarcel Moolenaar if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 119212904384SSimon J. Gerraty if (fclose(job->cmdFILE) != 0) 119312904384SSimon J. Gerraty Punt("Cannot write shell script for '%s': %s", 119412904384SSimon J. Gerraty job->node->name, strerror(errno)); 11953955d011SMarcel Moolenaar job->cmdFILE = NULL; 11963955d011SMarcel Moolenaar } 1197b0c40a00SSimon J. Gerraty done = true; 119806b9b3e0SSimon J. Gerraty 11993955d011SMarcel Moolenaar } else if (WIFEXITED(status)) { 12003955d011SMarcel Moolenaar /* 120106b9b3e0SSimon J. Gerraty * Deal with ignored errors in -B mode. We need to print a 120206b9b3e0SSimon J. Gerraty * message telling of the ignored error as well as to run 120306b9b3e0SSimon J. Gerraty * the next command. 12043955d011SMarcel Moolenaar */ 12053955d011SMarcel Moolenaar done = WEXITSTATUS(status) != 0; 120606b9b3e0SSimon J. Gerraty 1207e2eeea75SSimon J. Gerraty JobClosePipes(job); 120806b9b3e0SSimon J. Gerraty 12093955d011SMarcel Moolenaar } else { 121006b9b3e0SSimon J. Gerraty /* No need to close things down or anything. */ 1211b0c40a00SSimon J. Gerraty done = false; 12123955d011SMarcel Moolenaar } 12133955d011SMarcel Moolenaar 121406b9b3e0SSimon J. Gerraty if (done) 121506b9b3e0SSimon J. Gerraty JobFinishDone(job, &status); 12163955d011SMarcel Moolenaar 12173955d011SMarcel Moolenaar #ifdef USE_META 12183955d011SMarcel Moolenaar if (useMeta) { 1219e2eeea75SSimon J. Gerraty int meta_status = meta_job_finish(job); 1220e2eeea75SSimon J. Gerraty if (meta_status != 0 && status == 0) 1221e2eeea75SSimon J. Gerraty status = meta_status; 12223955d011SMarcel Moolenaar } 12233955d011SMarcel Moolenaar #endif 12243955d011SMarcel Moolenaar 1225b0c40a00SSimon J. Gerraty return_job_token = false; 12263955d011SMarcel Moolenaar 12273955d011SMarcel Moolenaar Trace_Log(JOBEND, job); 122806b9b3e0SSimon J. Gerraty if (!job->special) { 1229e2eeea75SSimon J. Gerraty if (WAIT_STATUS(status) != 0 || 1230e2eeea75SSimon J. Gerraty (aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT) 1231b0c40a00SSimon J. Gerraty return_job_token = true; 12323955d011SMarcel Moolenaar } 12333955d011SMarcel Moolenaar 1234e2eeea75SSimon J. Gerraty if (aborting != ABORT_ERROR && aborting != ABORT_INTERRUPT && 12353955d011SMarcel Moolenaar (WAIT_STATUS(status) == 0)) { 12363955d011SMarcel Moolenaar /* 123706b9b3e0SSimon J. Gerraty * As long as we aren't aborting and the job didn't return a 123806b9b3e0SSimon J. Gerraty * non-zero status that we shouldn't ignore, we call 123906b9b3e0SSimon J. Gerraty * Make_Update to update the parents. 12403955d011SMarcel Moolenaar */ 1241956e45f6SSimon J. Gerraty JobSaveCommands(job); 12423955d011SMarcel Moolenaar job->node->made = MADE; 124306b9b3e0SSimon J. Gerraty if (!job->special) 1244b0c40a00SSimon J. Gerraty return_job_token = true; 12453955d011SMarcel Moolenaar Make_Update(job->node); 1246e2eeea75SSimon J. Gerraty job->status = JOB_ST_FREE; 124706b9b3e0SSimon J. Gerraty } else if (status != 0) { 124806b9b3e0SSimon J. Gerraty job_errors++; 1249e2eeea75SSimon J. Gerraty job->status = JOB_ST_FREE; 12503955d011SMarcel Moolenaar } 12513955d011SMarcel Moolenaar 125206b9b3e0SSimon J. Gerraty if (job_errors > 0 && !opts.keepgoing && aborting != ABORT_INTERRUPT) { 125306b9b3e0SSimon J. Gerraty /* Prevent more jobs from getting started. */ 125406b9b3e0SSimon J. Gerraty aborting = ABORT_ERROR; 125506b9b3e0SSimon J. Gerraty } 12563955d011SMarcel Moolenaar 12573955d011SMarcel Moolenaar if (return_job_token) 12583955d011SMarcel Moolenaar Job_TokenReturn(); 12593955d011SMarcel Moolenaar 1260e2eeea75SSimon J. Gerraty if (aborting == ABORT_ERROR && jobTokensRunning == 0) 126106b9b3e0SSimon J. Gerraty Finish(job_errors); 12623955d011SMarcel Moolenaar } 1263e2eeea75SSimon J. Gerraty 1264e2eeea75SSimon J. Gerraty static void 1265e2eeea75SSimon J. Gerraty TouchRegular(GNode *gn) 1266e2eeea75SSimon J. Gerraty { 1267e2eeea75SSimon J. Gerraty const char *file = GNode_Path(gn); 1268b0c40a00SSimon J. Gerraty struct utimbuf times; 1269e2eeea75SSimon J. Gerraty int fd; 1270e2eeea75SSimon J. Gerraty char c; 1271e2eeea75SSimon J. Gerraty 1272b0c40a00SSimon J. Gerraty times.actime = now; 1273b0c40a00SSimon J. Gerraty times.modtime = now; 1274e2eeea75SSimon J. Gerraty if (utime(file, ×) >= 0) 1275e2eeea75SSimon J. Gerraty return; 1276e2eeea75SSimon J. Gerraty 1277e2eeea75SSimon J. Gerraty fd = open(file, O_RDWR | O_CREAT, 0666); 1278e2eeea75SSimon J. Gerraty if (fd < 0) { 1279e2eeea75SSimon J. Gerraty (void)fprintf(stderr, "*** couldn't touch %s: %s\n", 1280e2eeea75SSimon J. Gerraty file, strerror(errno)); 1281e2eeea75SSimon J. Gerraty (void)fflush(stderr); 1282e2eeea75SSimon J. Gerraty return; /* XXX: What about propagating the error? */ 1283e2eeea75SSimon J. Gerraty } 1284e2eeea75SSimon J. Gerraty 12859f45a3c8SSimon J. Gerraty /* 12869f45a3c8SSimon J. Gerraty * Last resort: update the file's time stamps in the traditional way. 1287e2eeea75SSimon J. Gerraty * XXX: This doesn't work for empty files, which are sometimes used 12889f45a3c8SSimon J. Gerraty * as marker files. 12899f45a3c8SSimon J. Gerraty */ 1290e2eeea75SSimon J. Gerraty if (read(fd, &c, 1) == 1) { 1291e2eeea75SSimon J. Gerraty (void)lseek(fd, 0, SEEK_SET); 1292e2eeea75SSimon J. Gerraty while (write(fd, &c, 1) == -1 && errno == EAGAIN) 1293e2eeea75SSimon J. Gerraty continue; 1294e2eeea75SSimon J. Gerraty } 1295e2eeea75SSimon J. Gerraty (void)close(fd); /* XXX: What about propagating the error? */ 12963955d011SMarcel Moolenaar } 12973955d011SMarcel Moolenaar 129806b9b3e0SSimon J. Gerraty /* 129906b9b3e0SSimon J. Gerraty * Touch the given target. Called by JobStart when the -t flag was given. 13003955d011SMarcel Moolenaar * 1301956e45f6SSimon J. Gerraty * The modification date of the file is changed. 130206b9b3e0SSimon J. Gerraty * If the file did not exist, it is created. 130306b9b3e0SSimon J. Gerraty */ 13043955d011SMarcel Moolenaar void 1305b0c40a00SSimon J. Gerraty Job_Touch(GNode *gn, bool echo) 13063955d011SMarcel Moolenaar { 130706b9b3e0SSimon J. Gerraty if (gn->type & 130806b9b3e0SSimon J. Gerraty (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC | OP_OPTIONAL | 13093955d011SMarcel Moolenaar OP_SPECIAL | OP_PHONY)) { 131006b9b3e0SSimon J. Gerraty /* 131106b9b3e0SSimon J. Gerraty * These are "virtual" targets and should not really be 131206b9b3e0SSimon J. Gerraty * created. 131306b9b3e0SSimon J. Gerraty */ 13143955d011SMarcel Moolenaar return; 13153955d011SMarcel Moolenaar } 13163955d011SMarcel Moolenaar 131706b9b3e0SSimon J. Gerraty if (echo || !GNode_ShouldExecute(gn)) { 13183955d011SMarcel Moolenaar (void)fprintf(stdout, "touch %s\n", gn->name); 13193955d011SMarcel Moolenaar (void)fflush(stdout); 13203955d011SMarcel Moolenaar } 13213955d011SMarcel Moolenaar 1322e2eeea75SSimon J. Gerraty if (!GNode_ShouldExecute(gn)) 13233955d011SMarcel Moolenaar return; 13243955d011SMarcel Moolenaar 132506b9b3e0SSimon J. Gerraty if (gn->type & OP_ARCHV) 13263955d011SMarcel Moolenaar Arch_Touch(gn); 132706b9b3e0SSimon J. Gerraty else if (gn->type & OP_LIB) 13283955d011SMarcel Moolenaar Arch_TouchLib(gn); 132906b9b3e0SSimon J. Gerraty else 1330e2eeea75SSimon J. Gerraty TouchRegular(gn); 13313955d011SMarcel Moolenaar } 13323955d011SMarcel Moolenaar 133306b9b3e0SSimon J. Gerraty /* 133406b9b3e0SSimon J. Gerraty * Make sure the given node has all the commands it needs. 1335956e45f6SSimon J. Gerraty * 1336956e45f6SSimon J. Gerraty * The node will have commands from the .DEFAULT rule added to it if it 1337956e45f6SSimon J. Gerraty * needs them. 13383955d011SMarcel Moolenaar * 13393955d011SMarcel Moolenaar * Input: 13403955d011SMarcel Moolenaar * gn The target whose commands need verifying 13413955d011SMarcel Moolenaar * abortProc Function to abort with message 13423955d011SMarcel Moolenaar * 13433955d011SMarcel Moolenaar * Results: 1344b0c40a00SSimon J. Gerraty * true if the commands list is/was ok. 13453955d011SMarcel Moolenaar */ 1346b0c40a00SSimon J. Gerraty bool 13473955d011SMarcel Moolenaar Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...)) 13483955d011SMarcel Moolenaar { 1349956e45f6SSimon J. Gerraty if (GNode_IsTarget(gn)) 1350b0c40a00SSimon J. Gerraty return true; 135106b9b3e0SSimon J. Gerraty if (!Lst_IsEmpty(&gn->commands)) 1352b0c40a00SSimon J. Gerraty return true; 135306b9b3e0SSimon J. Gerraty if ((gn->type & OP_LIB) && !Lst_IsEmpty(&gn->children)) 1354b0c40a00SSimon J. Gerraty return true; 1355956e45f6SSimon J. Gerraty 13563955d011SMarcel Moolenaar /* 13573955d011SMarcel Moolenaar * No commands. Look for .DEFAULT rule from which we might infer 1358e2eeea75SSimon J. Gerraty * commands. 13593955d011SMarcel Moolenaar */ 136006b9b3e0SSimon J. Gerraty if (defaultNode != NULL && !Lst_IsEmpty(&defaultNode->commands) && 1361e2eeea75SSimon J. Gerraty !(gn->type & OP_SPECIAL)) { 13623955d011SMarcel Moolenaar /* 136306b9b3e0SSimon J. Gerraty * The traditional Make only looks for a .DEFAULT if the node 136406b9b3e0SSimon J. Gerraty * was never the target of an operator, so that's what we do 136506b9b3e0SSimon J. Gerraty * too. 1366e2eeea75SSimon J. Gerraty * 1367e2eeea75SSimon J. Gerraty * The .DEFAULT node acts like a transformation rule, in that 13683955d011SMarcel Moolenaar * gn also inherits any attributes or sources attached to 13693955d011SMarcel Moolenaar * .DEFAULT itself. 13703955d011SMarcel Moolenaar */ 1371e2eeea75SSimon J. Gerraty Make_HandleUse(defaultNode, gn); 1372dba7b0efSSimon J. Gerraty Var_Set(gn, IMPSRC, GNode_VarTarget(gn)); 1373b0c40a00SSimon J. Gerraty return true; 1374956e45f6SSimon J. Gerraty } 1375956e45f6SSimon J. Gerraty 1376b0c40a00SSimon J. Gerraty Dir_UpdateMTime(gn, false); 1377e2eeea75SSimon J. Gerraty if (gn->mtime != 0 || (gn->type & OP_SPECIAL)) 1378b0c40a00SSimon J. Gerraty return true; 1379956e45f6SSimon J. Gerraty 13803955d011SMarcel Moolenaar /* 1381956e45f6SSimon J. Gerraty * The node wasn't the target of an operator. We have no .DEFAULT 13823955d011SMarcel Moolenaar * rule to go on and the target doesn't already exist. There's 13833955d011SMarcel Moolenaar * nothing more we can do for this branch. If the -k flag wasn't 13843955d011SMarcel Moolenaar * given, we stop in our tracks, otherwise we just don't update 13853955d011SMarcel Moolenaar * this node's parents so they never get examined. 13863955d011SMarcel Moolenaar */ 13873955d011SMarcel Moolenaar 138812904384SSimon J. Gerraty if (gn->flags.fromDepend) { 13891748de26SSimon J. Gerraty if (!Job_RunTarget(".STALE", gn->fname)) 139006b9b3e0SSimon J. Gerraty fprintf(stdout, 13919f45a3c8SSimon J. Gerraty "%s: %s, %u: ignoring stale %s for %s\n", 13921748de26SSimon J. Gerraty progname, gn->fname, gn->lineno, makeDependfile, 13931748de26SSimon J. Gerraty gn->name); 1394b0c40a00SSimon J. Gerraty return true; 13953955d011SMarcel Moolenaar } 13963955d011SMarcel Moolenaar 13973955d011SMarcel Moolenaar if (gn->type & OP_OPTIONAL) { 1398956e45f6SSimon J. Gerraty (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n", 1399956e45f6SSimon J. Gerraty progname, gn->name, "ignored"); 14003955d011SMarcel Moolenaar (void)fflush(stdout); 1401b0c40a00SSimon J. Gerraty return true; 14023955d011SMarcel Moolenaar } 14033955d011SMarcel Moolenaar 1404956e45f6SSimon J. Gerraty if (opts.keepgoing) { 1405956e45f6SSimon J. Gerraty (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n", 1406956e45f6SSimon J. Gerraty progname, gn->name, "continuing"); 1407956e45f6SSimon J. Gerraty (void)fflush(stdout); 1408b0c40a00SSimon J. Gerraty return false; 1409956e45f6SSimon J. Gerraty } 1410956e45f6SSimon J. Gerraty 1411*d5e0a182SSimon J. Gerraty abortProc("don't know how to make %s. Stop", gn->name); 1412b0c40a00SSimon J. Gerraty return false; 1413956e45f6SSimon J. Gerraty } 1414956e45f6SSimon J. Gerraty 141506b9b3e0SSimon J. Gerraty /* 141606b9b3e0SSimon J. Gerraty * Execute the shell for the given job. 14173955d011SMarcel Moolenaar * 141806b9b3e0SSimon J. Gerraty * See Job_CatchOutput for handling the output of the shell. 141906b9b3e0SSimon J. Gerraty */ 14203955d011SMarcel Moolenaar static void 14213955d011SMarcel Moolenaar JobExec(Job *job, char **argv) 14223955d011SMarcel Moolenaar { 14233955d011SMarcel Moolenaar int cpid; /* ID of new child */ 14243955d011SMarcel Moolenaar sigset_t mask; 14253955d011SMarcel Moolenaar 14263955d011SMarcel Moolenaar if (DEBUG(JOB)) { 14273955d011SMarcel Moolenaar int i; 14283955d011SMarcel Moolenaar 1429e2eeea75SSimon J. Gerraty debug_printf("Running %s\n", job->node->name); 1430956e45f6SSimon J. Gerraty debug_printf("\tCommand: "); 14313955d011SMarcel Moolenaar for (i = 0; argv[i] != NULL; i++) { 1432956e45f6SSimon J. Gerraty debug_printf("%s ", argv[i]); 14333955d011SMarcel Moolenaar } 1434956e45f6SSimon J. Gerraty debug_printf("\n"); 14353955d011SMarcel Moolenaar } 14363955d011SMarcel Moolenaar 14373955d011SMarcel Moolenaar /* 1438*d5e0a182SSimon J. Gerraty * Some jobs produce no output, and it's disconcerting to have 14393955d011SMarcel Moolenaar * no feedback of their running (since they produce no output, the 14403955d011SMarcel Moolenaar * banner with their name in it never appears). This is an attempt to 14413955d011SMarcel Moolenaar * provide that feedback, even if nothing follows it. 14423955d011SMarcel Moolenaar */ 144306b9b3e0SSimon J. Gerraty if (job->echo) 144406b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 14453955d011SMarcel Moolenaar 14463955d011SMarcel Moolenaar /* No interruptions until this job is on the `jobs' list */ 14473955d011SMarcel Moolenaar JobSigLock(&mask); 14483955d011SMarcel Moolenaar 14493955d011SMarcel Moolenaar /* Pre-emptively mark job running, pid still zero though */ 1450e2eeea75SSimon J. Gerraty job->status = JOB_ST_RUNNING; 14513955d011SMarcel Moolenaar 145206b9b3e0SSimon J. Gerraty Var_ReexportVars(); 145306b9b3e0SSimon J. Gerraty 1454dba7b0efSSimon J. Gerraty cpid = vfork(); 14553955d011SMarcel Moolenaar if (cpid == -1) 14563955d011SMarcel Moolenaar Punt("Cannot vfork: %s", strerror(errno)); 14573955d011SMarcel Moolenaar 14583955d011SMarcel Moolenaar if (cpid == 0) { 14593955d011SMarcel Moolenaar /* Child */ 14603955d011SMarcel Moolenaar sigset_t tmask; 14613955d011SMarcel Moolenaar 14623955d011SMarcel Moolenaar #ifdef USE_META 14639f45a3c8SSimon J. Gerraty if (useMeta) 14643955d011SMarcel Moolenaar meta_job_child(job); 14653955d011SMarcel Moolenaar #endif 14663955d011SMarcel Moolenaar /* 146706b9b3e0SSimon J. Gerraty * Reset all signal handlers; this is necessary because we 146806b9b3e0SSimon J. Gerraty * also need to unblock signals before we exec(2). 14693955d011SMarcel Moolenaar */ 14703955d011SMarcel Moolenaar JobSigReset(); 14713955d011SMarcel Moolenaar 14723955d011SMarcel Moolenaar /* Now unblock signals */ 14733955d011SMarcel Moolenaar sigemptyset(&tmask); 14743955d011SMarcel Moolenaar JobSigUnlock(&tmask); 14753955d011SMarcel Moolenaar 14763955d011SMarcel Moolenaar /* 147706b9b3e0SSimon J. Gerraty * Must duplicate the input stream down to the child's input 147806b9b3e0SSimon J. Gerraty * and reset it to the beginning (again). Since the stream 147906b9b3e0SSimon J. Gerraty * was marked close-on-exec, we must clear that bit in the 148006b9b3e0SSimon J. Gerraty * new input. 14813955d011SMarcel Moolenaar */ 1482956e45f6SSimon J. Gerraty if (dup2(fileno(job->cmdFILE), 0) == -1) 1483956e45f6SSimon J. Gerraty execDie("dup2", "job->cmdFILE"); 1484956e45f6SSimon J. Gerraty if (fcntl(0, F_SETFD, 0) == -1) 1485956e45f6SSimon J. Gerraty execDie("fcntl clear close-on-exec", "stdin"); 1486e2eeea75SSimon J. Gerraty if (lseek(0, 0, SEEK_SET) == -1) 1487956e45f6SSimon J. Gerraty execDie("lseek to 0", "stdin"); 14883955d011SMarcel Moolenaar 1489db29cad8SSimon J. Gerraty if (Always_pass_job_queue || 1490db29cad8SSimon J. Gerraty (job->node->type & (OP_MAKE | OP_SUBMAKE))) { 1491*d5e0a182SSimon J. Gerraty /* Pass job token pipe to submakes. */ 1492956e45f6SSimon J. Gerraty if (fcntl(tokenWaitJob.inPipe, F_SETFD, 0) == -1) 149306b9b3e0SSimon J. Gerraty execDie("clear close-on-exec", 149406b9b3e0SSimon J. Gerraty "tokenWaitJob.inPipe"); 1495956e45f6SSimon J. Gerraty if (fcntl(tokenWaitJob.outPipe, F_SETFD, 0) == -1) 149606b9b3e0SSimon J. Gerraty execDie("clear close-on-exec", 149706b9b3e0SSimon J. Gerraty "tokenWaitJob.outPipe"); 14983955d011SMarcel Moolenaar } 14993955d011SMarcel Moolenaar 15003955d011SMarcel Moolenaar /* 15013955d011SMarcel Moolenaar * Set up the child's output to be routed through the pipe 15023955d011SMarcel Moolenaar * we've created for it. 15033955d011SMarcel Moolenaar */ 1504956e45f6SSimon J. Gerraty if (dup2(job->outPipe, 1) == -1) 1505956e45f6SSimon J. Gerraty execDie("dup2", "job->outPipe"); 1506956e45f6SSimon J. Gerraty 15073955d011SMarcel Moolenaar /* 150806b9b3e0SSimon J. Gerraty * The output channels are marked close on exec. This bit 150906b9b3e0SSimon J. Gerraty * was duplicated by the dup2(on some systems), so we have 151006b9b3e0SSimon J. Gerraty * to clear it before routing the shell's error output to 151106b9b3e0SSimon J. Gerraty * the same place as its standard output. 15123955d011SMarcel Moolenaar */ 1513956e45f6SSimon J. Gerraty if (fcntl(1, F_SETFD, 0) == -1) 1514956e45f6SSimon J. Gerraty execDie("clear close-on-exec", "stdout"); 1515956e45f6SSimon J. Gerraty if (dup2(1, 2) == -1) 1516956e45f6SSimon J. Gerraty execDie("dup2", "1, 2"); 15173955d011SMarcel Moolenaar 15183955d011SMarcel Moolenaar /* 151906b9b3e0SSimon J. Gerraty * We want to switch the child into a different process 152006b9b3e0SSimon J. Gerraty * family so we can kill it and all its descendants in 152106b9b3e0SSimon J. Gerraty * one fell swoop, by killing its process family, but not 152206b9b3e0SSimon J. Gerraty * commit suicide. 15233955d011SMarcel Moolenaar */ 15243955d011SMarcel Moolenaar #if defined(HAVE_SETPGID) 15253955d011SMarcel Moolenaar (void)setpgid(0, getpid()); 15263955d011SMarcel Moolenaar #else 15273955d011SMarcel Moolenaar # if defined(HAVE_SETSID) 15283955d011SMarcel Moolenaar /* XXX: dsl - I'm sure this should be setpgrp()... */ 15293955d011SMarcel Moolenaar (void)setsid(); 15303955d011SMarcel Moolenaar # else 15313955d011SMarcel Moolenaar (void)setpgrp(0, getpid()); 15323955d011SMarcel Moolenaar # endif 15333955d011SMarcel Moolenaar #endif 15343955d011SMarcel Moolenaar 15353955d011SMarcel Moolenaar (void)execv(shellPath, argv); 1536956e45f6SSimon J. Gerraty execDie("exec", shellPath); 15373955d011SMarcel Moolenaar } 15383955d011SMarcel Moolenaar 15393955d011SMarcel Moolenaar /* Parent, continuing after the child exec */ 15403955d011SMarcel Moolenaar job->pid = cpid; 15413955d011SMarcel Moolenaar 15423955d011SMarcel Moolenaar Trace_Log(JOBSTART, job); 15433955d011SMarcel Moolenaar 154449caa483SSimon J. Gerraty #ifdef USE_META 15459f45a3c8SSimon J. Gerraty if (useMeta) 154649caa483SSimon J. Gerraty meta_job_parent(job, cpid); 154749caa483SSimon J. Gerraty #endif 154849caa483SSimon J. Gerraty 15493955d011SMarcel Moolenaar /* 15503955d011SMarcel Moolenaar * Set the current position in the buffer to the beginning 15513955d011SMarcel Moolenaar * and mark another stream to watch in the outputs mask 15523955d011SMarcel Moolenaar */ 15533955d011SMarcel Moolenaar job->curPos = 0; 15543955d011SMarcel Moolenaar 15553955d011SMarcel Moolenaar watchfd(job); 15563955d011SMarcel Moolenaar 15573955d011SMarcel Moolenaar if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 155812904384SSimon J. Gerraty if (fclose(job->cmdFILE) != 0) 155912904384SSimon J. Gerraty Punt("Cannot write shell script for '%s': %s", 156012904384SSimon J. Gerraty job->node->name, strerror(errno)); 15613955d011SMarcel Moolenaar job->cmdFILE = NULL; 15623955d011SMarcel Moolenaar } 15633955d011SMarcel Moolenaar 1564dba7b0efSSimon J. Gerraty /* Now that the job is actually running, add it to the table. */ 15653955d011SMarcel Moolenaar if (DEBUG(JOB)) { 1566956e45f6SSimon J. Gerraty debug_printf("JobExec(%s): pid %d added to jobs table\n", 15673955d011SMarcel Moolenaar job->node->name, job->pid); 1568b0c40a00SSimon J. Gerraty DumpJobs("job started"); 15693955d011SMarcel Moolenaar } 15703955d011SMarcel Moolenaar JobSigUnlock(&mask); 15713955d011SMarcel Moolenaar } 15723955d011SMarcel Moolenaar 1573956e45f6SSimon J. Gerraty /* Create the argv needed to execute the shell for a given job. */ 15743955d011SMarcel Moolenaar static void 15753955d011SMarcel Moolenaar JobMakeArgv(Job *job, char **argv) 15763955d011SMarcel Moolenaar { 15773955d011SMarcel Moolenaar int argc; 15783955d011SMarcel Moolenaar static char args[10]; /* For merged arguments */ 15793955d011SMarcel Moolenaar 15803955d011SMarcel Moolenaar argv[0] = UNCONST(shellName); 15813955d011SMarcel Moolenaar argc = 1; 15823955d011SMarcel Moolenaar 158306b9b3e0SSimon J. Gerraty if ((shell->errFlag != NULL && shell->errFlag[0] != '-') || 158406b9b3e0SSimon J. Gerraty (shell->echoFlag != NULL && shell->echoFlag[0] != '-')) { 15853955d011SMarcel Moolenaar /* 158606b9b3e0SSimon J. Gerraty * At least one of the flags doesn't have a minus before it, 158706b9b3e0SSimon J. Gerraty * so merge them together. Have to do this because the Bourne 158806b9b3e0SSimon J. Gerraty * shell thinks its second argument is a file to source. 158906b9b3e0SSimon J. Gerraty * Grrrr. Note the ten-character limitation on the combined 159006b9b3e0SSimon J. Gerraty * arguments. 159106b9b3e0SSimon J. Gerraty * 159206b9b3e0SSimon J. Gerraty * TODO: Research until when the above comments were 159306b9b3e0SSimon J. Gerraty * practically relevant. 15943955d011SMarcel Moolenaar */ 1595e2eeea75SSimon J. Gerraty (void)snprintf(args, sizeof args, "-%s%s", 159606b9b3e0SSimon J. Gerraty (job->ignerr ? "" : 159706b9b3e0SSimon J. Gerraty (shell->errFlag != NULL ? shell->errFlag : "")), 159806b9b3e0SSimon J. Gerraty (!job->echo ? "" : 159906b9b3e0SSimon J. Gerraty (shell->echoFlag != NULL ? shell->echoFlag : ""))); 16003955d011SMarcel Moolenaar 160106b9b3e0SSimon J. Gerraty if (args[1] != '\0') { 16023955d011SMarcel Moolenaar argv[argc] = args; 16033955d011SMarcel Moolenaar argc++; 16043955d011SMarcel Moolenaar } 16053955d011SMarcel Moolenaar } else { 160606b9b3e0SSimon J. Gerraty if (!job->ignerr && shell->errFlag != NULL) { 160706b9b3e0SSimon J. Gerraty argv[argc] = UNCONST(shell->errFlag); 16083955d011SMarcel Moolenaar argc++; 16093955d011SMarcel Moolenaar } 161006b9b3e0SSimon J. Gerraty if (job->echo && shell->echoFlag != NULL) { 161106b9b3e0SSimon J. Gerraty argv[argc] = UNCONST(shell->echoFlag); 16123955d011SMarcel Moolenaar argc++; 16133955d011SMarcel Moolenaar } 16143955d011SMarcel Moolenaar } 16153955d011SMarcel Moolenaar argv[argc] = NULL; 16163955d011SMarcel Moolenaar } 16173955d011SMarcel Moolenaar 161806b9b3e0SSimon J. Gerraty static void 1619b0c40a00SSimon J. Gerraty JobWriteShellCommands(Job *job, GNode *gn, bool *out_run) 16203955d011SMarcel Moolenaar { 16213955d011SMarcel Moolenaar /* 162206b9b3e0SSimon J. Gerraty * tfile is the name of a file into which all shell commands 162306b9b3e0SSimon J. Gerraty * are put. It is removed before the child shell is executed, 162406b9b3e0SSimon J. Gerraty * unless DEBUG(SCRIPT) is set. 16253955d011SMarcel Moolenaar */ 1626dba7b0efSSimon J. Gerraty char tfile[MAXPATHLEN]; 162706b9b3e0SSimon J. Gerraty int tfd; /* File descriptor to the temp file */ 162806b9b3e0SSimon J. Gerraty 1629dba7b0efSSimon J. Gerraty tfd = Job_TempFile(TMPPAT, tfile, sizeof tfile); 16303955d011SMarcel Moolenaar 16313955d011SMarcel Moolenaar job->cmdFILE = fdopen(tfd, "w+"); 1632e2eeea75SSimon J. Gerraty if (job->cmdFILE == NULL) 16333955d011SMarcel Moolenaar Punt("Could not fdopen %s", tfile); 1634e2eeea75SSimon J. Gerraty 1635956e45f6SSimon J. Gerraty (void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC); 16363955d011SMarcel Moolenaar 16373955d011SMarcel Moolenaar #ifdef USE_META 16383955d011SMarcel Moolenaar if (useMeta) { 16393955d011SMarcel Moolenaar meta_job_start(job, gn); 164006b9b3e0SSimon J. Gerraty if (gn->type & OP_SILENT) /* might have changed */ 1641b0c40a00SSimon J. Gerraty job->echo = false; 16423955d011SMarcel Moolenaar } 16433955d011SMarcel Moolenaar #endif 16443955d011SMarcel Moolenaar 1645b0c40a00SSimon J. Gerraty *out_run = JobWriteCommands(job); 164606b9b3e0SSimon J. Gerraty } 164706b9b3e0SSimon J. Gerraty 164806b9b3e0SSimon J. Gerraty /* 1649b0c40a00SSimon J. Gerraty * Start a target-creation process going for the target described by gn. 165006b9b3e0SSimon J. Gerraty * 165106b9b3e0SSimon J. Gerraty * Results: 165206b9b3e0SSimon J. Gerraty * JOB_ERROR if there was an error in the commands, JOB_FINISHED 165306b9b3e0SSimon J. Gerraty * if there isn't actually anything left to do for the job and 165406b9b3e0SSimon J. Gerraty * JOB_RUNNING if the job has been started. 165506b9b3e0SSimon J. Gerraty * 1656b0c40a00SSimon J. Gerraty * Details: 165706b9b3e0SSimon J. Gerraty * A new Job node is created and added to the list of running 165806b9b3e0SSimon J. Gerraty * jobs. PMake is forked and a child shell created. 165906b9b3e0SSimon J. Gerraty * 166006b9b3e0SSimon J. Gerraty * NB: The return value is ignored by everyone. 166106b9b3e0SSimon J. Gerraty */ 166206b9b3e0SSimon J. Gerraty static JobStartResult 1663b0c40a00SSimon J. Gerraty JobStart(GNode *gn, bool special) 166406b9b3e0SSimon J. Gerraty { 166506b9b3e0SSimon J. Gerraty Job *job; /* new job descriptor */ 166606b9b3e0SSimon J. Gerraty char *argv[10]; /* Argument vector to shell */ 1667b0c40a00SSimon J. Gerraty bool cmdsOK; /* true if the nodes commands were all right */ 1668b0c40a00SSimon J. Gerraty bool run; 166906b9b3e0SSimon J. Gerraty 167006b9b3e0SSimon J. Gerraty for (job = job_table; job < job_table_end; job++) { 167106b9b3e0SSimon J. Gerraty if (job->status == JOB_ST_FREE) 167206b9b3e0SSimon J. Gerraty break; 167306b9b3e0SSimon J. Gerraty } 167406b9b3e0SSimon J. Gerraty if (job >= job_table_end) 167506b9b3e0SSimon J. Gerraty Punt("JobStart no job slots vacant"); 167606b9b3e0SSimon J. Gerraty 167706b9b3e0SSimon J. Gerraty memset(job, 0, sizeof *job); 167806b9b3e0SSimon J. Gerraty job->node = gn; 167906b9b3e0SSimon J. Gerraty job->tailCmds = NULL; 168006b9b3e0SSimon J. Gerraty job->status = JOB_ST_SET_UP; 168106b9b3e0SSimon J. Gerraty 168206b9b3e0SSimon J. Gerraty job->special = special || gn->type & OP_SPECIAL; 168306b9b3e0SSimon J. Gerraty job->ignerr = opts.ignoreErrors || gn->type & OP_IGNORE; 16849f45a3c8SSimon J. Gerraty job->echo = !(opts.silent || gn->type & OP_SILENT); 168506b9b3e0SSimon J. Gerraty 168606b9b3e0SSimon J. Gerraty /* 168706b9b3e0SSimon J. Gerraty * Check the commands now so any attributes from .DEFAULT have a 168806b9b3e0SSimon J. Gerraty * chance to migrate to the node. 168906b9b3e0SSimon J. Gerraty */ 169006b9b3e0SSimon J. Gerraty cmdsOK = Job_CheckCommands(gn, Error); 169106b9b3e0SSimon J. Gerraty 169206b9b3e0SSimon J. Gerraty job->inPollfd = NULL; 1693dba7b0efSSimon J. Gerraty 1694dba7b0efSSimon J. Gerraty if (Lst_IsEmpty(&gn->commands)) { 1695dba7b0efSSimon J. Gerraty job->cmdFILE = stdout; 1696b0c40a00SSimon J. Gerraty run = false; 1697b0c40a00SSimon J. Gerraty 1698b0c40a00SSimon J. Gerraty /* 1699b0c40a00SSimon J. Gerraty * We're serious here, but if the commands were bogus, we're 1700b0c40a00SSimon J. Gerraty * also dead... 1701b0c40a00SSimon J. Gerraty */ 1702b0c40a00SSimon J. Gerraty if (!cmdsOK) { 17039f45a3c8SSimon J. Gerraty PrintOnError(gn, "\n"); /* provide some clue */ 1704b0c40a00SSimon J. Gerraty DieHorribly(); 1705b0c40a00SSimon J. Gerraty } 1706dba7b0efSSimon J. Gerraty } else if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) || 17079f45a3c8SSimon J. Gerraty (!opts.noExecute && !opts.touch)) { 1708dba7b0efSSimon J. Gerraty /* 1709dba7b0efSSimon J. Gerraty * The above condition looks very similar to 1710dba7b0efSSimon J. Gerraty * GNode_ShouldExecute but is subtly different. It prevents 1711dba7b0efSSimon J. Gerraty * that .MAKE targets are touched since these are usually 1712dba7b0efSSimon J. Gerraty * virtual targets. 1713dba7b0efSSimon J. Gerraty */ 1714dba7b0efSSimon J. Gerraty 1715b0c40a00SSimon J. Gerraty /* 1716b0c40a00SSimon J. Gerraty * We're serious here, but if the commands were bogus, we're 1717b0c40a00SSimon J. Gerraty * also dead... 1718b0c40a00SSimon J. Gerraty */ 1719b0c40a00SSimon J. Gerraty if (!cmdsOK) { 17209f45a3c8SSimon J. Gerraty PrintOnError(gn, "\n"); /* provide some clue */ 1721b0c40a00SSimon J. Gerraty DieHorribly(); 1722b0c40a00SSimon J. Gerraty } 1723b0c40a00SSimon J. Gerraty 1724b0c40a00SSimon J. Gerraty JobWriteShellCommands(job, gn, &run); 1725dba7b0efSSimon J. Gerraty (void)fflush(job->cmdFILE); 1726956e45f6SSimon J. Gerraty } else if (!GNode_ShouldExecute(gn)) { 17273955d011SMarcel Moolenaar /* 1728b0c40a00SSimon J. Gerraty * Just write all the commands to stdout in one fell swoop. 1729dba7b0efSSimon J. Gerraty * This still sets up job->tailCmds correctly. 17303955d011SMarcel Moolenaar */ 173106b9b3e0SSimon J. Gerraty SwitchOutputTo(gn); 17323955d011SMarcel Moolenaar job->cmdFILE = stdout; 1733956e45f6SSimon J. Gerraty if (cmdsOK) 1734b0c40a00SSimon J. Gerraty JobWriteCommands(job); 1735b0c40a00SSimon J. Gerraty run = false; 1736dba7b0efSSimon J. Gerraty (void)fflush(job->cmdFILE); 17373955d011SMarcel Moolenaar } else { 173806b9b3e0SSimon J. Gerraty Job_Touch(gn, job->echo); 1739b0c40a00SSimon J. Gerraty run = false; 17403955d011SMarcel Moolenaar } 17413955d011SMarcel Moolenaar 174206b9b3e0SSimon J. Gerraty /* If we're not supposed to execute a shell, don't. */ 174306b9b3e0SSimon J. Gerraty if (!run) { 174406b9b3e0SSimon J. Gerraty if (!job->special) 17453955d011SMarcel Moolenaar Job_TokenReturn(); 174606b9b3e0SSimon J. Gerraty /* Unlink and close the command file if we opened one */ 1747e2eeea75SSimon J. Gerraty if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 17483955d011SMarcel Moolenaar (void)fclose(job->cmdFILE); 17493955d011SMarcel Moolenaar job->cmdFILE = NULL; 17503955d011SMarcel Moolenaar } 17513955d011SMarcel Moolenaar 17523955d011SMarcel Moolenaar /* 175306b9b3e0SSimon J. Gerraty * We only want to work our way up the graph if we aren't 175406b9b3e0SSimon J. Gerraty * here because the commands for the job were no good. 17553955d011SMarcel Moolenaar */ 1756956e45f6SSimon J. Gerraty if (cmdsOK && aborting == ABORT_NONE) { 1757956e45f6SSimon J. Gerraty JobSaveCommands(job); 17583955d011SMarcel Moolenaar job->node->made = MADE; 17593955d011SMarcel Moolenaar Make_Update(job->node); 17603955d011SMarcel Moolenaar } 1761e2eeea75SSimon J. Gerraty job->status = JOB_ST_FREE; 17623955d011SMarcel Moolenaar return cmdsOK ? JOB_FINISHED : JOB_ERROR; 17633955d011SMarcel Moolenaar } 17643955d011SMarcel Moolenaar 17653955d011SMarcel Moolenaar /* 176606b9b3e0SSimon J. Gerraty * Set up the control arguments to the shell. This is based on the 176706b9b3e0SSimon J. Gerraty * flags set earlier for this job. 17683955d011SMarcel Moolenaar */ 17693955d011SMarcel Moolenaar JobMakeArgv(job, argv); 17703955d011SMarcel Moolenaar 17713955d011SMarcel Moolenaar /* Create the pipe by which we'll get the shell's output. */ 17723955d011SMarcel Moolenaar JobCreatePipe(job, 3); 17733955d011SMarcel Moolenaar 17743955d011SMarcel Moolenaar JobExec(job, argv); 17753841c287SSimon J. Gerraty return JOB_RUNNING; 17763955d011SMarcel Moolenaar } 17773955d011SMarcel Moolenaar 177806b9b3e0SSimon J. Gerraty /* 1779b0c40a00SSimon J. Gerraty * If the shell has an output filter (which only csh and ksh have by default), 1780b0c40a00SSimon J. Gerraty * print the output of the child process, skipping the noPrint text of the 1781b0c40a00SSimon J. Gerraty * shell. 1782b0c40a00SSimon J. Gerraty * 1783b0c40a00SSimon J. Gerraty * Return the part of the output that the calling function needs to output by 1784b0c40a00SSimon J. Gerraty * itself. 178506b9b3e0SSimon J. Gerraty */ 17863955d011SMarcel Moolenaar static char * 1787*d5e0a182SSimon J. Gerraty PrintFilteredOutput(char *p, char *endp) /* XXX: should all be const */ 17883955d011SMarcel Moolenaar { 1789*d5e0a182SSimon J. Gerraty char *ep; /* XXX: should be const */ 17903955d011SMarcel Moolenaar 179106b9b3e0SSimon J. Gerraty if (shell->noPrint == NULL || shell->noPrint[0] == '\0') 1792*d5e0a182SSimon J. Gerraty return p; 1793e2eeea75SSimon J. Gerraty 179406b9b3e0SSimon J. Gerraty /* 179506b9b3e0SSimon J. Gerraty * XXX: What happens if shell->noPrint occurs on the boundary of 179606b9b3e0SSimon J. Gerraty * the buffer? To work correctly in all cases, this should rather 179706b9b3e0SSimon J. Gerraty * be a proper stream filter instead of doing string matching on 179806b9b3e0SSimon J. Gerraty * selected chunks of the output. 179906b9b3e0SSimon J. Gerraty */ 1800*d5e0a182SSimon J. Gerraty while ((ep = strstr(p, shell->noPrint)) != NULL) { 1801*d5e0a182SSimon J. Gerraty if (ep != p) { 1802*d5e0a182SSimon J. Gerraty *ep = '\0'; /* XXX: avoid writing to the buffer */ 18033955d011SMarcel Moolenaar /* 18043955d011SMarcel Moolenaar * The only way there wouldn't be a newline after 18053955d011SMarcel Moolenaar * this line is if it were the last in the buffer. 180606b9b3e0SSimon J. Gerraty * however, since the noPrint output comes after it, 18073955d011SMarcel Moolenaar * there must be a newline, so we don't print one. 18083955d011SMarcel Moolenaar */ 180906b9b3e0SSimon J. Gerraty /* XXX: What about null bytes in the output? */ 1810*d5e0a182SSimon J. Gerraty (void)fprintf(stdout, "%s", p); 18113955d011SMarcel Moolenaar (void)fflush(stdout); 18123955d011SMarcel Moolenaar } 1813*d5e0a182SSimon J. Gerraty p = ep + shell->noPrintLen; 1814*d5e0a182SSimon J. Gerraty if (p == endp) 181506b9b3e0SSimon J. Gerraty break; 1816*d5e0a182SSimon J. Gerraty p++; /* skip over the (XXX: assumed) newline */ 1817*d5e0a182SSimon J. Gerraty pp_skip_whitespace(&p); 18183955d011SMarcel Moolenaar } 1819*d5e0a182SSimon J. Gerraty return p; 18203955d011SMarcel Moolenaar } 18213955d011SMarcel Moolenaar 1822e2eeea75SSimon J. Gerraty /* 1823e2eeea75SSimon J. Gerraty * This function is called whenever there is something to read on the pipe. 1824e2eeea75SSimon J. Gerraty * We collect more output from the given job and store it in the job's 1825e2eeea75SSimon J. Gerraty * outBuf. If this makes up a line, we print it tagged by the job's 1826e2eeea75SSimon J. Gerraty * identifier, as necessary. 1827e2eeea75SSimon J. Gerraty * 1828e2eeea75SSimon J. Gerraty * In the output of the shell, the 'noPrint' lines are removed. If the 1829e2eeea75SSimon J. Gerraty * command is not alone on the line (the character after it is not \0 or 1830e2eeea75SSimon J. Gerraty * \n), we do print whatever follows it. 18313955d011SMarcel Moolenaar * 18323955d011SMarcel Moolenaar * Input: 18333955d011SMarcel Moolenaar * job the job whose output needs printing 1834b0c40a00SSimon J. Gerraty * finish true if this is the last time we'll be called 18353955d011SMarcel Moolenaar * for this job 18363955d011SMarcel Moolenaar */ 1837956e45f6SSimon J. Gerraty static void 1838b0c40a00SSimon J. Gerraty CollectOutput(Job *job, bool finish) 18393955d011SMarcel Moolenaar { 1840b0c40a00SSimon J. Gerraty bool gotNL; /* true if got a newline */ 1841b0c40a00SSimon J. Gerraty bool fbuf; /* true if our buffer filled up */ 1842956e45f6SSimon J. Gerraty size_t nr; /* number of bytes read */ 1843956e45f6SSimon J. Gerraty size_t i; /* auxiliary index into outBuf */ 1844956e45f6SSimon J. Gerraty size_t max; /* limit for i (end of current data) */ 1845956e45f6SSimon J. Gerraty ssize_t nRead; /* (Temporary) number of bytes read */ 18463955d011SMarcel Moolenaar 184706b9b3e0SSimon J. Gerraty /* Read as many bytes as will fit in the buffer. */ 1848e2eeea75SSimon J. Gerraty again: 1849b0c40a00SSimon J. Gerraty gotNL = false; 1850b0c40a00SSimon J. Gerraty fbuf = false; 18513955d011SMarcel Moolenaar 18523955d011SMarcel Moolenaar nRead = read(job->inPipe, &job->outBuf[job->curPos], 18533955d011SMarcel Moolenaar JOB_BUFSIZE - job->curPos); 18543955d011SMarcel Moolenaar if (nRead < 0) { 18553955d011SMarcel Moolenaar if (errno == EAGAIN) 18563955d011SMarcel Moolenaar return; 18574fde40d9SSimon J. Gerraty if (DEBUG(JOB)) 1858b0c40a00SSimon J. Gerraty perror("CollectOutput(piperead)"); 18593955d011SMarcel Moolenaar nr = 0; 18604fde40d9SSimon J. Gerraty } else 1861956e45f6SSimon J. Gerraty nr = (size_t)nRead; 18624fde40d9SSimon J. Gerraty 18634fde40d9SSimon J. Gerraty if (nr == 0) 18644fde40d9SSimon J. Gerraty finish = false; /* stop looping */ 18653955d011SMarcel Moolenaar 18663955d011SMarcel Moolenaar /* 18673955d011SMarcel Moolenaar * If we hit the end-of-file (the job is dead), we must flush its 18683955d011SMarcel Moolenaar * remaining output, so pretend we read a newline if there's any 18693955d011SMarcel Moolenaar * output remaining in the buffer. 18703955d011SMarcel Moolenaar */ 1871e2eeea75SSimon J. Gerraty if (nr == 0 && job->curPos != 0) { 18723955d011SMarcel Moolenaar job->outBuf[job->curPos] = '\n'; 18733955d011SMarcel Moolenaar nr = 1; 18743955d011SMarcel Moolenaar } 18753955d011SMarcel Moolenaar 18763955d011SMarcel Moolenaar max = job->curPos + nr; 18774fde40d9SSimon J. Gerraty for (i = job->curPos; i < max; i++) 18784fde40d9SSimon J. Gerraty if (job->outBuf[i] == '\0') 18794fde40d9SSimon J. Gerraty job->outBuf[i] = ' '; 18804fde40d9SSimon J. Gerraty 18814fde40d9SSimon J. Gerraty /* Look for the last newline in the bytes we just got. */ 188206b9b3e0SSimon J. Gerraty for (i = job->curPos + nr - 1; 188306b9b3e0SSimon J. Gerraty i >= job->curPos && i != (size_t)-1; i--) { 18843955d011SMarcel Moolenaar if (job->outBuf[i] == '\n') { 1885b0c40a00SSimon J. Gerraty gotNL = true; 18863955d011SMarcel Moolenaar break; 18873955d011SMarcel Moolenaar } 18883955d011SMarcel Moolenaar } 18893955d011SMarcel Moolenaar 18903955d011SMarcel Moolenaar if (!gotNL) { 18913955d011SMarcel Moolenaar job->curPos += nr; 18923955d011SMarcel Moolenaar if (job->curPos == JOB_BUFSIZE) { 18933955d011SMarcel Moolenaar /* 18943955d011SMarcel Moolenaar * If we've run out of buffer space, we have no choice 18953955d011SMarcel Moolenaar * but to print the stuff. sigh. 18963955d011SMarcel Moolenaar */ 1897b0c40a00SSimon J. Gerraty fbuf = true; 18983955d011SMarcel Moolenaar i = job->curPos; 18993955d011SMarcel Moolenaar } 19003955d011SMarcel Moolenaar } 19013955d011SMarcel Moolenaar if (gotNL || fbuf) { 19023955d011SMarcel Moolenaar /* 19033955d011SMarcel Moolenaar * Need to send the output to the screen. Null terminate it 19043955d011SMarcel Moolenaar * first, overwriting the newline character if there was one. 19053955d011SMarcel Moolenaar * So long as the line isn't one we should filter (according 19063955d011SMarcel Moolenaar * to the shell description), we print the line, preceded 19073955d011SMarcel Moolenaar * by a target banner if this target isn't the same as the 19083955d011SMarcel Moolenaar * one for which we last printed something. 19093955d011SMarcel Moolenaar * The rest of the data in the buffer are then shifted down 19103955d011SMarcel Moolenaar * to the start of the buffer and curPos is set accordingly. 19113955d011SMarcel Moolenaar */ 19123955d011SMarcel Moolenaar job->outBuf[i] = '\0'; 19133955d011SMarcel Moolenaar if (i >= job->curPos) { 1914*d5e0a182SSimon J. Gerraty char *p; 19153955d011SMarcel Moolenaar 1916b0c40a00SSimon J. Gerraty /* 1917b0c40a00SSimon J. Gerraty * FIXME: SwitchOutputTo should be here, according to 1918b0c40a00SSimon J. Gerraty * the comment above. But since PrintOutput does not 1919b0c40a00SSimon J. Gerraty * do anything in the default shell, this bug has gone 1920b0c40a00SSimon J. Gerraty * unnoticed until now. 1921b0c40a00SSimon J. Gerraty */ 1922*d5e0a182SSimon J. Gerraty p = PrintFilteredOutput(job->outBuf, &job->outBuf[i]); 19233955d011SMarcel Moolenaar 19243955d011SMarcel Moolenaar /* 1925b0c40a00SSimon J. Gerraty * There's still more in the output buffer. This time, 192606b9b3e0SSimon J. Gerraty * though, we know there's no newline at the end, so 192706b9b3e0SSimon J. Gerraty * we add one of our own free will. 19283955d011SMarcel Moolenaar */ 1929*d5e0a182SSimon J. Gerraty if (*p != '\0') { 19309f45a3c8SSimon J. Gerraty if (!opts.silent) 193106b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 19323955d011SMarcel Moolenaar #ifdef USE_META 19333955d011SMarcel Moolenaar if (useMeta) { 1934*d5e0a182SSimon J. Gerraty meta_job_output(job, p, 193506b9b3e0SSimon J. Gerraty gotNL ? "\n" : ""); 19363955d011SMarcel Moolenaar } 19373955d011SMarcel Moolenaar #endif 1938*d5e0a182SSimon J. Gerraty (void)fprintf(stdout, "%s%s", p, 193906b9b3e0SSimon J. Gerraty gotNL ? "\n" : ""); 19403955d011SMarcel Moolenaar (void)fflush(stdout); 19413955d011SMarcel Moolenaar } 19423955d011SMarcel Moolenaar } 19433955d011SMarcel Moolenaar /* 194406b9b3e0SSimon J. Gerraty * max is the last offset still in the buffer. Move any 194506b9b3e0SSimon J. Gerraty * remaining characters to the start of the buffer and 194606b9b3e0SSimon J. Gerraty * update the end marker curPos. 19473955d011SMarcel Moolenaar */ 1948db29cad8SSimon J. Gerraty if (i < max) { 194906b9b3e0SSimon J. Gerraty (void)memmove(job->outBuf, &job->outBuf[i + 1], 195006b9b3e0SSimon J. Gerraty max - (i + 1)); 1951db29cad8SSimon J. Gerraty job->curPos = max - (i + 1); 1952db29cad8SSimon J. Gerraty } else { 1953db29cad8SSimon J. Gerraty assert(i == max); 19543955d011SMarcel Moolenaar job->curPos = 0; 19553955d011SMarcel Moolenaar } 19563955d011SMarcel Moolenaar } 19573955d011SMarcel Moolenaar if (finish) { 19583955d011SMarcel Moolenaar /* 19593955d011SMarcel Moolenaar * If the finish flag is true, we must loop until we hit 19603955d011SMarcel Moolenaar * end-of-file on the pipe. This is guaranteed to happen 19613955d011SMarcel Moolenaar * eventually since the other end of the pipe is now closed 19623955d011SMarcel Moolenaar * (we closed it explicitly and the child has exited). When 1963b0c40a00SSimon J. Gerraty * we do get an EOF, finish will be set false and we'll fall 19643955d011SMarcel Moolenaar * through and out. 19653955d011SMarcel Moolenaar */ 1966e2eeea75SSimon J. Gerraty goto again; 19673955d011SMarcel Moolenaar } 19683955d011SMarcel Moolenaar } 19693955d011SMarcel Moolenaar 19703955d011SMarcel Moolenaar static void 19713955d011SMarcel Moolenaar JobRun(GNode *targ) 19723955d011SMarcel Moolenaar { 1973956e45f6SSimon J. Gerraty #if 0 19743955d011SMarcel Moolenaar /* 1975956e45f6SSimon J. Gerraty * Unfortunately it is too complicated to run .BEGIN, .END, and 1976956e45f6SSimon J. Gerraty * .INTERRUPT job in the parallel job module. As of 2020-09-25, 1977956e45f6SSimon J. Gerraty * unit-tests/deptgt-end-jobs.mk hangs in an endless loop. 1978956e45f6SSimon J. Gerraty * 1979956e45f6SSimon J. Gerraty * Running these jobs in compat mode also guarantees that these 1980956e45f6SSimon J. Gerraty * jobs do not overlap with other unrelated jobs. 19813955d011SMarcel Moolenaar */ 1982dba7b0efSSimon J. Gerraty GNodeList lst = LST_INIT; 1983dba7b0efSSimon J. Gerraty Lst_Append(&lst, targ); 1984dba7b0efSSimon J. Gerraty (void)Make_Run(&lst); 1985dba7b0efSSimon J. Gerraty Lst_Done(&lst); 1986b0c40a00SSimon J. Gerraty JobStart(targ, true); 198706b9b3e0SSimon J. Gerraty while (jobTokensRunning != 0) { 19883955d011SMarcel Moolenaar Job_CatchOutput(); 19893955d011SMarcel Moolenaar } 19903955d011SMarcel Moolenaar #else 19913955d011SMarcel Moolenaar Compat_Make(targ, targ); 199206b9b3e0SSimon J. Gerraty /* XXX: Replace with GNode_IsError(gn) */ 19933955d011SMarcel Moolenaar if (targ->made == ERROR) { 19949f45a3c8SSimon J. Gerraty PrintOnError(targ, "\n\nStop.\n"); 19953955d011SMarcel Moolenaar exit(1); 19963955d011SMarcel Moolenaar } 19973955d011SMarcel Moolenaar #endif 19983955d011SMarcel Moolenaar } 19993955d011SMarcel Moolenaar 200006b9b3e0SSimon J. Gerraty /* 200106b9b3e0SSimon J. Gerraty * Handle the exit of a child. Called from Make_Make. 20023955d011SMarcel Moolenaar * 20033955d011SMarcel Moolenaar * The job descriptor is removed from the list of children. 20043955d011SMarcel Moolenaar * 20053955d011SMarcel Moolenaar * Notes: 20063955d011SMarcel Moolenaar * We do waits, blocking or not, according to the wisdom of our 20073955d011SMarcel Moolenaar * caller, until there are no more children to report. For each 20083955d011SMarcel Moolenaar * job, call JobFinish to finish things off. 20093955d011SMarcel Moolenaar */ 20103955d011SMarcel Moolenaar void 20113955d011SMarcel Moolenaar Job_CatchChildren(void) 20123955d011SMarcel Moolenaar { 20133955d011SMarcel Moolenaar int pid; /* pid of dead child */ 20143955d011SMarcel Moolenaar WAIT_T status; /* Exit/termination status */ 20153955d011SMarcel Moolenaar 201606b9b3e0SSimon J. Gerraty /* Don't even bother if we know there's no one around. */ 20173955d011SMarcel Moolenaar if (jobTokensRunning == 0) 20183955d011SMarcel Moolenaar return; 20193955d011SMarcel Moolenaar 2020dba7b0efSSimon J. Gerraty /* Have we received SIGCHLD since last call? */ 2021dba7b0efSSimon J. Gerraty if (caught_sigchld == 0) 2022dba7b0efSSimon J. Gerraty return; 2023dba7b0efSSimon J. Gerraty caught_sigchld = 0; 2024dba7b0efSSimon J. Gerraty 20253955d011SMarcel Moolenaar while ((pid = waitpid((pid_t)-1, &status, WNOHANG | WUNTRACED)) > 0) { 202606b9b3e0SSimon J. Gerraty DEBUG2(JOB, "Process %d exited/stopped status %x.\n", 202706b9b3e0SSimon J. Gerraty pid, WAIT_STATUS(status)); 2028b0c40a00SSimon J. Gerraty JobReapChild(pid, status, true); 20293955d011SMarcel Moolenaar } 20303955d011SMarcel Moolenaar } 20313955d011SMarcel Moolenaar 20323955d011SMarcel Moolenaar /* 20333955d011SMarcel Moolenaar * It is possible that wait[pid]() was called from elsewhere, 20343955d011SMarcel Moolenaar * this lets us reap jobs regardless. 20353955d011SMarcel Moolenaar */ 20363955d011SMarcel Moolenaar void 2037b0c40a00SSimon J. Gerraty JobReapChild(pid_t pid, WAIT_T status, bool isJobs) 20383955d011SMarcel Moolenaar { 20393955d011SMarcel Moolenaar Job *job; /* job descriptor for dead child */ 20403955d011SMarcel Moolenaar 204106b9b3e0SSimon J. Gerraty /* Don't even bother if we know there's no one around. */ 20423955d011SMarcel Moolenaar if (jobTokensRunning == 0) 20433955d011SMarcel Moolenaar return; 20443955d011SMarcel Moolenaar 20453955d011SMarcel Moolenaar job = JobFindPid(pid, JOB_ST_RUNNING, isJobs); 20463955d011SMarcel Moolenaar if (job == NULL) { 20473955d011SMarcel Moolenaar if (isJobs) { 20483955d011SMarcel Moolenaar if (!lurking_children) 204906b9b3e0SSimon J. Gerraty Error("Child (%d) status %x not in table?", 205006b9b3e0SSimon J. Gerraty pid, status); 20513955d011SMarcel Moolenaar } 20523955d011SMarcel Moolenaar return; /* not ours */ 20533955d011SMarcel Moolenaar } 20543955d011SMarcel Moolenaar if (WIFSTOPPED(status)) { 205506b9b3e0SSimon J. Gerraty DEBUG2(JOB, "Process %d (%s) stopped.\n", 205606b9b3e0SSimon J. Gerraty job->pid, job->node->name); 20573955d011SMarcel Moolenaar if (!make_suspended) { 20583955d011SMarcel Moolenaar switch (WSTOPSIG(status)) { 20593955d011SMarcel Moolenaar case SIGTSTP: 206006b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Suspended\n", 206106b9b3e0SSimon J. Gerraty job->node->name); 20623955d011SMarcel Moolenaar break; 20633955d011SMarcel Moolenaar case SIGSTOP: 206406b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Stopped\n", 206506b9b3e0SSimon J. Gerraty job->node->name); 20663955d011SMarcel Moolenaar break; 20673955d011SMarcel Moolenaar default: 20683955d011SMarcel Moolenaar (void)printf("*** [%s] Stopped -- signal %d\n", 20693955d011SMarcel Moolenaar job->node->name, WSTOPSIG(status)); 20703955d011SMarcel Moolenaar } 2071b0c40a00SSimon J. Gerraty job->suspended = true; 20723955d011SMarcel Moolenaar } 20733955d011SMarcel Moolenaar (void)fflush(stdout); 20743955d011SMarcel Moolenaar return; 20753955d011SMarcel Moolenaar } 20763955d011SMarcel Moolenaar 2077e2eeea75SSimon J. Gerraty job->status = JOB_ST_FINISHED; 20783955d011SMarcel Moolenaar job->exit_status = WAIT_STATUS(status); 20793955d011SMarcel Moolenaar 20803955d011SMarcel Moolenaar JobFinish(job, status); 20813955d011SMarcel Moolenaar } 20823955d011SMarcel Moolenaar 208306b9b3e0SSimon J. Gerraty /* 208406b9b3e0SSimon J. Gerraty * Catch the output from our children, if we're using pipes do so. Otherwise 2085956e45f6SSimon J. Gerraty * just block time until we get a signal(most likely a SIGCHLD) since there's 2086956e45f6SSimon J. Gerraty * no point in just spinning when there's nothing to do and the reaping of a 208706b9b3e0SSimon J. Gerraty * child can wait for a while. 208806b9b3e0SSimon J. Gerraty */ 20893955d011SMarcel Moolenaar void 20903955d011SMarcel Moolenaar Job_CatchOutput(void) 20913955d011SMarcel Moolenaar { 20923955d011SMarcel Moolenaar int nready; 20933955d011SMarcel Moolenaar Job *job; 2094956e45f6SSimon J. Gerraty unsigned int i; 20953955d011SMarcel Moolenaar 20963955d011SMarcel Moolenaar (void)fflush(stdout); 20973955d011SMarcel Moolenaar 20983955d011SMarcel Moolenaar /* The first fd in the list is the job token pipe */ 20991748de26SSimon J. Gerraty do { 2100dba7b0efSSimon J. Gerraty nready = poll(fds + 1 - wantToken, fdsLen - 1 + wantToken, 210106b9b3e0SSimon J. Gerraty POLL_MSEC); 21021748de26SSimon J. Gerraty } while (nready < 0 && errno == EINTR); 21033955d011SMarcel Moolenaar 21041748de26SSimon J. Gerraty if (nready < 0) 21051748de26SSimon J. Gerraty Punt("poll: %s", strerror(errno)); 21061748de26SSimon J. Gerraty 21071748de26SSimon J. Gerraty if (nready > 0 && readyfd(&childExitJob)) { 21083955d011SMarcel Moolenaar char token = 0; 21091748de26SSimon J. Gerraty ssize_t count; 21101748de26SSimon J. Gerraty count = read(childExitJob.inPipe, &token, 1); 2111dba7b0efSSimon J. Gerraty if (count == 1) { 21123955d011SMarcel Moolenaar if (token == DO_JOB_RESUME[0]) 211306b9b3e0SSimon J. Gerraty /* 211406b9b3e0SSimon J. Gerraty * Complete relay requested from our SIGCONT 211506b9b3e0SSimon J. Gerraty * handler 211606b9b3e0SSimon J. Gerraty */ 21173955d011SMarcel Moolenaar JobRestartJobs(); 2118dba7b0efSSimon J. Gerraty } else if (count == 0) 2119dba7b0efSSimon J. Gerraty Punt("unexpected eof on token pipe"); 212012904384SSimon J. Gerraty else if (errno != EAGAIN) 2121dba7b0efSSimon J. Gerraty Punt("token pipe read: %s", strerror(errno)); 2122e2eeea75SSimon J. Gerraty nready--; 21233955d011SMarcel Moolenaar } 21243955d011SMarcel Moolenaar 21251748de26SSimon J. Gerraty Job_CatchChildren(); 21261748de26SSimon J. Gerraty if (nready == 0) 21273955d011SMarcel Moolenaar return; 21283955d011SMarcel Moolenaar 2129dba7b0efSSimon J. Gerraty for (i = npseudojobs * nfds_per_job(); i < fdsLen; i++) { 213006b9b3e0SSimon J. Gerraty if (fds[i].revents == 0) 21313955d011SMarcel Moolenaar continue; 2132dba7b0efSSimon J. Gerraty job = jobByFdIndex[i]; 2133e2eeea75SSimon J. Gerraty if (job->status == JOB_ST_RUNNING) 2134b0c40a00SSimon J. Gerraty CollectOutput(job, false); 213549caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 213649caa483SSimon J. Gerraty /* 213749caa483SSimon J. Gerraty * With meta mode, we may have activity on the job's filemon 213806b9b3e0SSimon J. Gerraty * descriptor too, which at the moment is any pollfd other 213906b9b3e0SSimon J. Gerraty * than job->inPollfd. 214049caa483SSimon J. Gerraty */ 214149caa483SSimon J. Gerraty if (useMeta && job->inPollfd != &fds[i]) { 21429f45a3c8SSimon J. Gerraty if (meta_job_event(job) <= 0) 214349caa483SSimon J. Gerraty fds[i].events = 0; /* never mind */ 214449caa483SSimon J. Gerraty } 214549caa483SSimon J. Gerraty #endif 21461748de26SSimon J. Gerraty if (--nready == 0) 21471748de26SSimon J. Gerraty return; 21483955d011SMarcel Moolenaar } 21493955d011SMarcel Moolenaar } 21503955d011SMarcel Moolenaar 215106b9b3e0SSimon J. Gerraty /* 215206b9b3e0SSimon J. Gerraty * Start the creation of a target. Basically a front-end for JobStart used by 215306b9b3e0SSimon J. Gerraty * the Make module. 215406b9b3e0SSimon J. Gerraty */ 21553955d011SMarcel Moolenaar void 21563955d011SMarcel Moolenaar Job_Make(GNode *gn) 21573955d011SMarcel Moolenaar { 2158b0c40a00SSimon J. Gerraty (void)JobStart(gn, false); 215906b9b3e0SSimon J. Gerraty } 216006b9b3e0SSimon J. Gerraty 216106b9b3e0SSimon J. Gerraty static void 216206b9b3e0SSimon J. Gerraty InitShellNameAndPath(void) 216306b9b3e0SSimon J. Gerraty { 216406b9b3e0SSimon J. Gerraty shellName = shell->name; 216506b9b3e0SSimon J. Gerraty 216606b9b3e0SSimon J. Gerraty #ifdef DEFSHELL_CUSTOM 216706b9b3e0SSimon J. Gerraty if (shellName[0] == '/') { 216806b9b3e0SSimon J. Gerraty shellPath = shellName; 216906b9b3e0SSimon J. Gerraty shellName = str_basename(shellPath); 217006b9b3e0SSimon J. Gerraty return; 217106b9b3e0SSimon J. Gerraty } 217206b9b3e0SSimon J. Gerraty #endif 217312904384SSimon J. Gerraty #ifdef DEFSHELL_PATH 217412904384SSimon J. Gerraty shellPath = DEFSHELL_PATH; 217512904384SSimon J. Gerraty #else 217606b9b3e0SSimon J. Gerraty shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName); 217712904384SSimon J. Gerraty #endif 21783955d011SMarcel Moolenaar } 21793955d011SMarcel Moolenaar 21803955d011SMarcel Moolenaar void 21813955d011SMarcel Moolenaar Shell_Init(void) 21823955d011SMarcel Moolenaar { 218306b9b3e0SSimon J. Gerraty if (shellPath == NULL) 218406b9b3e0SSimon J. Gerraty InitShellNameAndPath(); 218506b9b3e0SSimon J. Gerraty 2186dba7b0efSSimon J. Gerraty Var_SetWithFlags(SCOPE_CMDLINE, ".SHELL", shellPath, VAR_SET_READONLY); 218706b9b3e0SSimon J. Gerraty if (shell->errFlag == NULL) 218806b9b3e0SSimon J. Gerraty shell->errFlag = ""; 218906b9b3e0SSimon J. Gerraty if (shell->echoFlag == NULL) 219006b9b3e0SSimon J. Gerraty shell->echoFlag = ""; 219106b9b3e0SSimon J. Gerraty if (shell->hasErrCtl && shell->errFlag[0] != '\0') { 219206b9b3e0SSimon J. Gerraty if (shellErrFlag != NULL && 219306b9b3e0SSimon J. Gerraty strcmp(shell->errFlag, &shellErrFlag[1]) != 0) { 219451ee2c1cSSimon J. Gerraty free(shellErrFlag); 219551ee2c1cSSimon J. Gerraty shellErrFlag = NULL; 219651ee2c1cSSimon J. Gerraty } 21979f45a3c8SSimon J. Gerraty if (shellErrFlag == NULL) 21989f45a3c8SSimon J. Gerraty shellErrFlag = str_concat2("-", shell->errFlag); 219906b9b3e0SSimon J. Gerraty } else if (shellErrFlag != NULL) { 220051ee2c1cSSimon J. Gerraty free(shellErrFlag); 220151ee2c1cSSimon J. Gerraty shellErrFlag = NULL; 220251ee2c1cSSimon J. Gerraty } 22033955d011SMarcel Moolenaar } 22043955d011SMarcel Moolenaar 220506b9b3e0SSimon J. Gerraty /* 220606b9b3e0SSimon J. Gerraty * Return the string literal that is used in the current command shell 220706b9b3e0SSimon J. Gerraty * to produce a newline character. 220806b9b3e0SSimon J. Gerraty */ 22093955d011SMarcel Moolenaar const char * 22103955d011SMarcel Moolenaar Shell_GetNewline(void) 22113955d011SMarcel Moolenaar { 221206b9b3e0SSimon J. Gerraty return shell->newline; 22133955d011SMarcel Moolenaar } 22143955d011SMarcel Moolenaar 22153955d011SMarcel Moolenaar void 22163955d011SMarcel Moolenaar Job_SetPrefix(void) 22173955d011SMarcel Moolenaar { 2218*d5e0a182SSimon J. Gerraty if (targPrefix != NULL) 22193955d011SMarcel Moolenaar free(targPrefix); 2220*d5e0a182SSimon J. Gerraty else if (!Var_Exists(SCOPE_GLOBAL, ".MAKE.JOB.PREFIX")) 22218c973ee2SSimon J. Gerraty Global_Set(".MAKE.JOB.PREFIX", "---"); 22223955d011SMarcel Moolenaar 22238c973ee2SSimon J. Gerraty targPrefix = Var_Subst("${.MAKE.JOB.PREFIX}", 22248c973ee2SSimon J. Gerraty SCOPE_GLOBAL, VARE_WANTRES); 2225956e45f6SSimon J. Gerraty /* TODO: handle errors */ 22263955d011SMarcel Moolenaar } 22273955d011SMarcel Moolenaar 222806b9b3e0SSimon J. Gerraty static void 222906b9b3e0SSimon J. Gerraty AddSig(int sig, SignalProc handler) 223006b9b3e0SSimon J. Gerraty { 223106b9b3e0SSimon J. Gerraty if (bmake_signal(sig, SIG_IGN) != SIG_IGN) { 223206b9b3e0SSimon J. Gerraty sigaddset(&caught_signals, sig); 223306b9b3e0SSimon J. Gerraty (void)bmake_signal(sig, handler); 223406b9b3e0SSimon J. Gerraty } 223506b9b3e0SSimon J. Gerraty } 223606b9b3e0SSimon J. Gerraty 22372c3632d1SSimon J. Gerraty /* Initialize the process module. */ 22383955d011SMarcel Moolenaar void 22393955d011SMarcel Moolenaar Job_Init(void) 22403955d011SMarcel Moolenaar { 2241d191243dSSimon J. Gerraty Job_SetPrefix(); 22423955d011SMarcel Moolenaar /* Allocate space for all the job info */ 2243956e45f6SSimon J. Gerraty job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table); 2244956e45f6SSimon J. Gerraty memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table); 2245956e45f6SSimon J. Gerraty job_table_end = job_table + opts.maxJobs; 22463955d011SMarcel Moolenaar wantToken = 0; 2247dba7b0efSSimon J. Gerraty caught_sigchld = 0; 22483955d011SMarcel Moolenaar 2249956e45f6SSimon J. Gerraty aborting = ABORT_NONE; 225006b9b3e0SSimon J. Gerraty job_errors = 0; 22513955d011SMarcel Moolenaar 225268c4481aSSimon J. Gerraty Always_pass_job_queue = GetBooleanExpr(MAKE_ALWAYS_PASS_JOB_QUEUE, 22539a4bc556SSimon J. Gerraty Always_pass_job_queue); 22549a4bc556SSimon J. Gerraty 225568c4481aSSimon J. Gerraty Job_error_token = GetBooleanExpr(MAKE_JOB_ERROR_TOKEN, Job_error_token); 225668c4481aSSimon J. Gerraty 22572d395cb5SSimon J. Gerraty 22583955d011SMarcel Moolenaar /* 22593955d011SMarcel Moolenaar * There is a non-zero chance that we already have children. 22603955d011SMarcel Moolenaar * eg after 'make -f- <<EOF' 226106b9b3e0SSimon J. Gerraty * Since their termination causes a 'Child (pid) not in table' 226206b9b3e0SSimon J. Gerraty * message, Collect the status of any that are already dead, and 226306b9b3e0SSimon J. Gerraty * suppress the error message if there are any undead ones. 22643955d011SMarcel Moolenaar */ 22653955d011SMarcel Moolenaar for (;;) { 226606b9b3e0SSimon J. Gerraty int rval; 226706b9b3e0SSimon J. Gerraty WAIT_T status; 226806b9b3e0SSimon J. Gerraty 22693955d011SMarcel Moolenaar rval = waitpid((pid_t)-1, &status, WNOHANG); 22703955d011SMarcel Moolenaar if (rval > 0) 22713955d011SMarcel Moolenaar continue; 22723955d011SMarcel Moolenaar if (rval == 0) 2273b0c40a00SSimon J. Gerraty lurking_children = true; 22743955d011SMarcel Moolenaar break; 22753955d011SMarcel Moolenaar } 22763955d011SMarcel Moolenaar 22773955d011SMarcel Moolenaar Shell_Init(); 22783955d011SMarcel Moolenaar 22793955d011SMarcel Moolenaar JobCreatePipe(&childExitJob, 3); 22803955d011SMarcel Moolenaar 2281dba7b0efSSimon J. Gerraty { 228249caa483SSimon J. Gerraty /* Preallocate enough for the maximum number of jobs. */ 2283dba7b0efSSimon J. Gerraty size_t nfds = (npseudojobs + (size_t)opts.maxJobs) * 2284dba7b0efSSimon J. Gerraty nfds_per_job(); 2285dba7b0efSSimon J. Gerraty fds = bmake_malloc(sizeof *fds * nfds); 2286dba7b0efSSimon J. Gerraty jobByFdIndex = bmake_malloc(sizeof *jobByFdIndex * nfds); 2287dba7b0efSSimon J. Gerraty } 22883955d011SMarcel Moolenaar 22893955d011SMarcel Moolenaar /* These are permanent entries and take slots 0 and 1 */ 22903955d011SMarcel Moolenaar watchfd(&tokenWaitJob); 22913955d011SMarcel Moolenaar watchfd(&childExitJob); 22923955d011SMarcel Moolenaar 22933955d011SMarcel Moolenaar sigemptyset(&caught_signals); 2294*d5e0a182SSimon J. Gerraty /* Install a SIGCHLD handler. */ 22953955d011SMarcel Moolenaar (void)bmake_signal(SIGCHLD, JobChildSig); 22963955d011SMarcel Moolenaar sigaddset(&caught_signals, SIGCHLD); 22973955d011SMarcel Moolenaar 22983955d011SMarcel Moolenaar /* 22993955d011SMarcel Moolenaar * Catch the four signals that POSIX specifies if they aren't ignored. 23003955d011SMarcel Moolenaar * JobPassSig will take care of calling JobInterrupt if appropriate. 23013955d011SMarcel Moolenaar */ 230206b9b3e0SSimon J. Gerraty AddSig(SIGINT, JobPassSig_int); 230306b9b3e0SSimon J. Gerraty AddSig(SIGHUP, JobPassSig_term); 230406b9b3e0SSimon J. Gerraty AddSig(SIGTERM, JobPassSig_term); 230506b9b3e0SSimon J. Gerraty AddSig(SIGQUIT, JobPassSig_term); 23063955d011SMarcel Moolenaar 23073955d011SMarcel Moolenaar /* 23083955d011SMarcel Moolenaar * There are additional signals that need to be caught and passed if 23093955d011SMarcel Moolenaar * either the export system wants to be told directly of signals or if 23103955d011SMarcel Moolenaar * we're giving each job its own process group (since then it won't get 23113955d011SMarcel Moolenaar * signals from the terminal driver as we own the terminal) 23123955d011SMarcel Moolenaar */ 231306b9b3e0SSimon J. Gerraty AddSig(SIGTSTP, JobPassSig_suspend); 231406b9b3e0SSimon J. Gerraty AddSig(SIGTTOU, JobPassSig_suspend); 231506b9b3e0SSimon J. Gerraty AddSig(SIGTTIN, JobPassSig_suspend); 231606b9b3e0SSimon J. Gerraty AddSig(SIGWINCH, JobCondPassSig); 231706b9b3e0SSimon J. Gerraty AddSig(SIGCONT, JobContinueSig); 23183955d011SMarcel Moolenaar 23191748de26SSimon J. Gerraty (void)Job_RunTarget(".BEGIN", NULL); 23209f45a3c8SSimon J. Gerraty /* 23219f45a3c8SSimon J. Gerraty * Create the .END node now, even though no code in the unit tests 2322954401e6SSimon J. Gerraty * depends on it. See also Targ_GetEndNode in Compat_MakeAll. 23239f45a3c8SSimon J. Gerraty */ 2324956e45f6SSimon J. Gerraty (void)Targ_GetEndNode(); 23253955d011SMarcel Moolenaar } 23263955d011SMarcel Moolenaar 232706b9b3e0SSimon J. Gerraty static void 232806b9b3e0SSimon J. Gerraty DelSig(int sig) 23293955d011SMarcel Moolenaar { 233006b9b3e0SSimon J. Gerraty if (sigismember(&caught_signals, sig) != 0) 233106b9b3e0SSimon J. Gerraty (void)bmake_signal(sig, SIG_DFL); 23323955d011SMarcel Moolenaar } 23333955d011SMarcel Moolenaar 233406b9b3e0SSimon J. Gerraty static void 233506b9b3e0SSimon J. Gerraty JobSigReset(void) 233606b9b3e0SSimon J. Gerraty { 233706b9b3e0SSimon J. Gerraty DelSig(SIGINT); 233806b9b3e0SSimon J. Gerraty DelSig(SIGHUP); 233906b9b3e0SSimon J. Gerraty DelSig(SIGQUIT); 234006b9b3e0SSimon J. Gerraty DelSig(SIGTERM); 234106b9b3e0SSimon J. Gerraty DelSig(SIGTSTP); 234206b9b3e0SSimon J. Gerraty DelSig(SIGTTOU); 234306b9b3e0SSimon J. Gerraty DelSig(SIGTTIN); 234406b9b3e0SSimon J. Gerraty DelSig(SIGWINCH); 234506b9b3e0SSimon J. Gerraty DelSig(SIGCONT); 23463955d011SMarcel Moolenaar (void)bmake_signal(SIGCHLD, SIG_DFL); 23473955d011SMarcel Moolenaar } 23483955d011SMarcel Moolenaar 23492c3632d1SSimon J. Gerraty /* Find a shell in 'shells' given its name, or return NULL. */ 23503955d011SMarcel Moolenaar static Shell * 2351956e45f6SSimon J. Gerraty FindShellByName(const char *name) 23523955d011SMarcel Moolenaar { 2353956e45f6SSimon J. Gerraty Shell *sh = shells; 2354956e45f6SSimon J. Gerraty const Shell *shellsEnd = sh + sizeof shells / sizeof shells[0]; 23553955d011SMarcel Moolenaar 2356956e45f6SSimon J. Gerraty for (sh = shells; sh < shellsEnd; sh++) { 23573955d011SMarcel Moolenaar if (strcmp(name, sh->name) == 0) 23583841c287SSimon J. Gerraty return sh; 23593955d011SMarcel Moolenaar } 23603955d011SMarcel Moolenaar return NULL; 23613955d011SMarcel Moolenaar } 23623955d011SMarcel Moolenaar 236306b9b3e0SSimon J. Gerraty /* 236406b9b3e0SSimon J. Gerraty * Parse a shell specification and set up 'shell', shellPath and 236506b9b3e0SSimon J. Gerraty * shellName appropriately. 23663955d011SMarcel Moolenaar * 23673955d011SMarcel Moolenaar * Input: 23683955d011SMarcel Moolenaar * line The shell spec 23693955d011SMarcel Moolenaar * 23703955d011SMarcel Moolenaar * Results: 2371b0c40a00SSimon J. Gerraty * false if the specification was incorrect. 23723955d011SMarcel Moolenaar * 23733955d011SMarcel Moolenaar * Side Effects: 237406b9b3e0SSimon J. Gerraty * 'shell' points to a Shell structure (either predefined or 23753955d011SMarcel Moolenaar * created from the shell spec), shellPath is the full path of the 237606b9b3e0SSimon J. Gerraty * shell described by 'shell', while shellName is just the 23773955d011SMarcel Moolenaar * final component of shellPath. 23783955d011SMarcel Moolenaar * 23793955d011SMarcel Moolenaar * Notes: 23803955d011SMarcel Moolenaar * A shell specification consists of a .SHELL target, with dependency 23813955d011SMarcel Moolenaar * operator, followed by a series of blank-separated words. Double 23823955d011SMarcel Moolenaar * quotes can be used to use blanks in words. A backslash escapes 23833955d011SMarcel Moolenaar * anything (most notably a double-quote and a space) and 23843955d011SMarcel Moolenaar * provides the functionality it does in C. Each word consists of 23853955d011SMarcel Moolenaar * keyword and value separated by an equal sign. There should be no 23863955d011SMarcel Moolenaar * unnecessary spaces in the word. The keywords are as follows: 23873955d011SMarcel Moolenaar * name Name of shell. 23883955d011SMarcel Moolenaar * path Location of shell. 23893955d011SMarcel Moolenaar * quiet Command to turn off echoing. 23903955d011SMarcel Moolenaar * echo Command to turn echoing on 23913955d011SMarcel Moolenaar * filter Result of turning off echoing that shouldn't be 23923955d011SMarcel Moolenaar * printed. 23933955d011SMarcel Moolenaar * echoFlag Flag to turn echoing on at the start 23943955d011SMarcel Moolenaar * errFlag Flag to turn error checking on at the start 23953955d011SMarcel Moolenaar * hasErrCtl True if shell has error checking control 23963955d011SMarcel Moolenaar * newline String literal to represent a newline char 23973955d011SMarcel Moolenaar * check Command to turn on error checking if hasErrCtl 2398b0c40a00SSimon J. Gerraty * is true or template of command to echo a command 23993955d011SMarcel Moolenaar * for which error checking is off if hasErrCtl is 2400b0c40a00SSimon J. Gerraty * false. 24013955d011SMarcel Moolenaar * ignore Command to turn off error checking if hasErrCtl 2402b0c40a00SSimon J. Gerraty * is true or template of command to execute a 24033955d011SMarcel Moolenaar * command so as to ignore any errors it returns if 2404b0c40a00SSimon J. Gerraty * hasErrCtl is false. 24053955d011SMarcel Moolenaar */ 2406b0c40a00SSimon J. Gerraty bool 24073955d011SMarcel Moolenaar Job_ParseShell(char *line) 24083955d011SMarcel Moolenaar { 24092c3632d1SSimon J. Gerraty Words wordsList; 24103955d011SMarcel Moolenaar char **words; 24113955d011SMarcel Moolenaar char **argv; 24122c3632d1SSimon J. Gerraty size_t argc; 24133955d011SMarcel Moolenaar char *path; 24143955d011SMarcel Moolenaar Shell newShell; 2415b0c40a00SSimon J. Gerraty bool fullSpec = false; 24163955d011SMarcel Moolenaar Shell *sh; 24173955d011SMarcel Moolenaar 241806b9b3e0SSimon J. Gerraty /* XXX: don't use line as an iterator variable */ 2419956e45f6SSimon J. Gerraty pp_skip_whitespace(&line); 24203955d011SMarcel Moolenaar 2421dba7b0efSSimon J. Gerraty free(shell_freeIt); 24223955d011SMarcel Moolenaar 2423e2eeea75SSimon J. Gerraty memset(&newShell, 0, sizeof newShell); 24243955d011SMarcel Moolenaar 2425*d5e0a182SSimon J. Gerraty /* Parse the specification by keyword. */ 2426b0c40a00SSimon J. Gerraty wordsList = Str_Words(line, true); 24272c3632d1SSimon J. Gerraty words = wordsList.words; 24282c3632d1SSimon J. Gerraty argc = wordsList.len; 24292c3632d1SSimon J. Gerraty path = wordsList.freeIt; 24303955d011SMarcel Moolenaar if (words == NULL) { 24313955d011SMarcel Moolenaar Error("Unterminated quoted string [%s]", line); 2432b0c40a00SSimon J. Gerraty return false; 24333955d011SMarcel Moolenaar } 2434dba7b0efSSimon J. Gerraty shell_freeIt = path; 24353955d011SMarcel Moolenaar 24363955d011SMarcel Moolenaar for (path = NULL, argv = words; argc != 0; argc--, argv++) { 2437956e45f6SSimon J. Gerraty char *arg = *argv; 2438956e45f6SSimon J. Gerraty if (strncmp(arg, "path=", 5) == 0) { 2439956e45f6SSimon J. Gerraty path = arg + 5; 2440956e45f6SSimon J. Gerraty } else if (strncmp(arg, "name=", 5) == 0) { 2441956e45f6SSimon J. Gerraty newShell.name = arg + 5; 24423955d011SMarcel Moolenaar } else { 2443956e45f6SSimon J. Gerraty if (strncmp(arg, "quiet=", 6) == 0) { 2444956e45f6SSimon J. Gerraty newShell.echoOff = arg + 6; 2445956e45f6SSimon J. Gerraty } else if (strncmp(arg, "echo=", 5) == 0) { 2446956e45f6SSimon J. Gerraty newShell.echoOn = arg + 5; 2447956e45f6SSimon J. Gerraty } else if (strncmp(arg, "filter=", 7) == 0) { 2448956e45f6SSimon J. Gerraty newShell.noPrint = arg + 7; 2449956e45f6SSimon J. Gerraty newShell.noPrintLen = strlen(newShell.noPrint); 2450956e45f6SSimon J. Gerraty } else if (strncmp(arg, "echoFlag=", 9) == 0) { 245106b9b3e0SSimon J. Gerraty newShell.echoFlag = arg + 9; 2452956e45f6SSimon J. Gerraty } else if (strncmp(arg, "errFlag=", 8) == 0) { 245306b9b3e0SSimon J. Gerraty newShell.errFlag = arg + 8; 2454956e45f6SSimon J. Gerraty } else if (strncmp(arg, "hasErrCtl=", 10) == 0) { 2455956e45f6SSimon J. Gerraty char c = arg[10]; 2456956e45f6SSimon J. Gerraty newShell.hasErrCtl = c == 'Y' || c == 'y' || 2457956e45f6SSimon J. Gerraty c == 'T' || c == 't'; 2458956e45f6SSimon J. Gerraty } else if (strncmp(arg, "newline=", 8) == 0) { 2459956e45f6SSimon J. Gerraty newShell.newline = arg + 8; 2460956e45f6SSimon J. Gerraty } else if (strncmp(arg, "check=", 6) == 0) { 24619f45a3c8SSimon J. Gerraty /* 24629f45a3c8SSimon J. Gerraty * Before 2020-12-10, these two variables had 24639f45a3c8SSimon J. Gerraty * been a single variable. 24649f45a3c8SSimon J. Gerraty */ 246506b9b3e0SSimon J. Gerraty newShell.errOn = arg + 6; 246606b9b3e0SSimon J. Gerraty newShell.echoTmpl = arg + 6; 2467956e45f6SSimon J. Gerraty } else if (strncmp(arg, "ignore=", 7) == 0) { 24689f45a3c8SSimon J. Gerraty /* 24699f45a3c8SSimon J. Gerraty * Before 2020-12-10, these two variables had 24709f45a3c8SSimon J. Gerraty * been a single variable. 24719f45a3c8SSimon J. Gerraty */ 247206b9b3e0SSimon J. Gerraty newShell.errOff = arg + 7; 247306b9b3e0SSimon J. Gerraty newShell.runIgnTmpl = arg + 7; 2474956e45f6SSimon J. Gerraty } else if (strncmp(arg, "errout=", 7) == 0) { 247506b9b3e0SSimon J. Gerraty newShell.runChkTmpl = arg + 7; 2476956e45f6SSimon J. Gerraty } else if (strncmp(arg, "comment=", 8) == 0) { 2477956e45f6SSimon J. Gerraty newShell.commentChar = arg[8]; 24783955d011SMarcel Moolenaar } else { 247906b9b3e0SSimon J. Gerraty Parse_Error(PARSE_FATAL, 248006b9b3e0SSimon J. Gerraty "Unknown keyword \"%s\"", arg); 24813955d011SMarcel Moolenaar free(words); 2482b0c40a00SSimon J. Gerraty return false; 24833955d011SMarcel Moolenaar } 2484b0c40a00SSimon J. Gerraty fullSpec = true; 24853955d011SMarcel Moolenaar } 24863955d011SMarcel Moolenaar } 24873955d011SMarcel Moolenaar 24883955d011SMarcel Moolenaar if (path == NULL) { 24893955d011SMarcel Moolenaar /* 249006b9b3e0SSimon J. Gerraty * If no path was given, the user wants one of the 249106b9b3e0SSimon J. Gerraty * pre-defined shells, yes? So we find the one s/he wants 249206b9b3e0SSimon J. Gerraty * with the help of FindShellByName and set things up the 249306b9b3e0SSimon J. Gerraty * right way. shellPath will be set up by Shell_Init. 24943955d011SMarcel Moolenaar */ 24953955d011SMarcel Moolenaar if (newShell.name == NULL) { 249606b9b3e0SSimon J. Gerraty Parse_Error(PARSE_FATAL, 249706b9b3e0SSimon J. Gerraty "Neither path nor name specified"); 24983955d011SMarcel Moolenaar free(words); 2499b0c40a00SSimon J. Gerraty return false; 25003955d011SMarcel Moolenaar } else { 2501956e45f6SSimon J. Gerraty if ((sh = FindShellByName(newShell.name)) == NULL) { 250206b9b3e0SSimon J. Gerraty Parse_Error(PARSE_WARNING, 250306b9b3e0SSimon J. Gerraty "%s: No matching shell", newShell.name); 25043955d011SMarcel Moolenaar free(words); 2505b0c40a00SSimon J. Gerraty return false; 25063955d011SMarcel Moolenaar } 250706b9b3e0SSimon J. Gerraty shell = sh; 25083955d011SMarcel Moolenaar shellName = newShell.name; 250906b9b3e0SSimon J. Gerraty if (shellPath != NULL) { 251006b9b3e0SSimon J. Gerraty /* 251106b9b3e0SSimon J. Gerraty * Shell_Init has already been called! 251206b9b3e0SSimon J. Gerraty * Do it again. 251306b9b3e0SSimon J. Gerraty */ 25143955d011SMarcel Moolenaar free(UNCONST(shellPath)); 25153955d011SMarcel Moolenaar shellPath = NULL; 25163955d011SMarcel Moolenaar Shell_Init(); 25173955d011SMarcel Moolenaar } 25183955d011SMarcel Moolenaar } 25193955d011SMarcel Moolenaar } else { 25203955d011SMarcel Moolenaar shellPath = path; 2521*d5e0a182SSimon J. Gerraty shellName = newShell.name != NULL ? newShell.name 2522*d5e0a182SSimon J. Gerraty : str_basename(path); 25233955d011SMarcel Moolenaar if (!fullSpec) { 2524956e45f6SSimon J. Gerraty if ((sh = FindShellByName(shellName)) == NULL) { 252506b9b3e0SSimon J. Gerraty Parse_Error(PARSE_WARNING, 252606b9b3e0SSimon J. Gerraty "%s: No matching shell", shellName); 25273955d011SMarcel Moolenaar free(words); 2528b0c40a00SSimon J. Gerraty return false; 25293955d011SMarcel Moolenaar } 253006b9b3e0SSimon J. Gerraty shell = sh; 25313955d011SMarcel Moolenaar } else { 253206b9b3e0SSimon J. Gerraty shell = bmake_malloc(sizeof *shell); 253306b9b3e0SSimon J. Gerraty *shell = newShell; 25343955d011SMarcel Moolenaar } 253551ee2c1cSSimon J. Gerraty /* this will take care of shellErrFlag */ 253651ee2c1cSSimon J. Gerraty Shell_Init(); 25373955d011SMarcel Moolenaar } 25383955d011SMarcel Moolenaar 253906b9b3e0SSimon J. Gerraty if (shell->echoOn != NULL && shell->echoOff != NULL) 2540b0c40a00SSimon J. Gerraty shell->hasEchoCtl = true; 25413955d011SMarcel Moolenaar 254206b9b3e0SSimon J. Gerraty if (!shell->hasErrCtl) { 254306b9b3e0SSimon J. Gerraty if (shell->echoTmpl == NULL) 254406b9b3e0SSimon J. Gerraty shell->echoTmpl = ""; 254506b9b3e0SSimon J. Gerraty if (shell->runIgnTmpl == NULL) 254606b9b3e0SSimon J. Gerraty shell->runIgnTmpl = "%s\n"; 25473955d011SMarcel Moolenaar } 25483955d011SMarcel Moolenaar 25493955d011SMarcel Moolenaar /* 255006b9b3e0SSimon J. Gerraty * Do not free up the words themselves, since they might be in use 255106b9b3e0SSimon J. Gerraty * by the shell specification. 25523955d011SMarcel Moolenaar */ 25533955d011SMarcel Moolenaar free(words); 2554b0c40a00SSimon J. Gerraty return true; 25553955d011SMarcel Moolenaar } 25563955d011SMarcel Moolenaar 255706b9b3e0SSimon J. Gerraty /* 255806b9b3e0SSimon J. Gerraty * Handle the receipt of an interrupt. 2559956e45f6SSimon J. Gerraty * 2560956e45f6SSimon J. Gerraty * All children are killed. Another job will be started if the .INTERRUPT 2561956e45f6SSimon J. Gerraty * target is defined. 25623955d011SMarcel Moolenaar * 25633955d011SMarcel Moolenaar * Input: 25643955d011SMarcel Moolenaar * runINTERRUPT Non-zero if commands for the .INTERRUPT target 25653955d011SMarcel Moolenaar * should be executed 25663955d011SMarcel Moolenaar * signo signal received 25673955d011SMarcel Moolenaar */ 25683955d011SMarcel Moolenaar static void 2569b0c40a00SSimon J. Gerraty JobInterrupt(bool runINTERRUPT, int signo) 25703955d011SMarcel Moolenaar { 25713955d011SMarcel Moolenaar Job *job; /* job descriptor in that element */ 25723955d011SMarcel Moolenaar GNode *interrupt; /* the node describing the .INTERRUPT target */ 25733955d011SMarcel Moolenaar sigset_t mask; 25743955d011SMarcel Moolenaar GNode *gn; 25753955d011SMarcel Moolenaar 25763955d011SMarcel Moolenaar aborting = ABORT_INTERRUPT; 25773955d011SMarcel Moolenaar 25783955d011SMarcel Moolenaar JobSigLock(&mask); 25793955d011SMarcel Moolenaar 25803955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 2581e2eeea75SSimon J. Gerraty if (job->status != JOB_ST_RUNNING) 25823955d011SMarcel Moolenaar continue; 25833955d011SMarcel Moolenaar 25843955d011SMarcel Moolenaar gn = job->node; 25853955d011SMarcel Moolenaar 258645447996SSimon J. Gerraty JobDeleteTarget(gn); 258706b9b3e0SSimon J. Gerraty if (job->pid != 0) { 258806b9b3e0SSimon J. Gerraty DEBUG2(JOB, 258906b9b3e0SSimon J. Gerraty "JobInterrupt passing signal %d to child %d.\n", 25903955d011SMarcel Moolenaar signo, job->pid); 25913955d011SMarcel Moolenaar KILLPG(job->pid, signo); 25923955d011SMarcel Moolenaar } 25933955d011SMarcel Moolenaar } 25943955d011SMarcel Moolenaar 25953955d011SMarcel Moolenaar JobSigUnlock(&mask); 25963955d011SMarcel Moolenaar 25979f45a3c8SSimon J. Gerraty if (runINTERRUPT && !opts.touch) { 2598956e45f6SSimon J. Gerraty interrupt = Targ_FindNode(".INTERRUPT"); 25993955d011SMarcel Moolenaar if (interrupt != NULL) { 2600b0c40a00SSimon J. Gerraty opts.ignoreErrors = false; 26013955d011SMarcel Moolenaar JobRun(interrupt); 26023955d011SMarcel Moolenaar } 26033955d011SMarcel Moolenaar } 2604e2eeea75SSimon J. Gerraty Trace_Log(MAKEINTR, NULL); 260506b9b3e0SSimon J. Gerraty exit(signo); /* XXX: why signo? */ 26063955d011SMarcel Moolenaar } 26073955d011SMarcel Moolenaar 260806b9b3e0SSimon J. Gerraty /* 260906b9b3e0SSimon J. Gerraty * Do the final processing, i.e. run the commands attached to the .END target. 26103955d011SMarcel Moolenaar * 261106b9b3e0SSimon J. Gerraty * Return the number of errors reported. 261206b9b3e0SSimon J. Gerraty */ 26133955d011SMarcel Moolenaar int 26143955d011SMarcel Moolenaar Job_Finish(void) 26153955d011SMarcel Moolenaar { 2616956e45f6SSimon J. Gerraty GNode *endNode = Targ_GetEndNode(); 261706b9b3e0SSimon J. Gerraty if (!Lst_IsEmpty(&endNode->commands) || 261806b9b3e0SSimon J. Gerraty !Lst_IsEmpty(&endNode->children)) { 2619*d5e0a182SSimon J. Gerraty if (job_errors != 0) 26203955d011SMarcel Moolenaar Error("Errors reported so .END ignored"); 2621*d5e0a182SSimon J. Gerraty else 2622956e45f6SSimon J. Gerraty JobRun(endNode); 26233955d011SMarcel Moolenaar } 262406b9b3e0SSimon J. Gerraty return job_errors; 26253955d011SMarcel Moolenaar } 26263955d011SMarcel Moolenaar 2627956e45f6SSimon J. Gerraty /* Clean up any memory used by the jobs module. */ 26283955d011SMarcel Moolenaar void 26293955d011SMarcel Moolenaar Job_End(void) 26303955d011SMarcel Moolenaar { 26313955d011SMarcel Moolenaar #ifdef CLEANUP 2632dba7b0efSSimon J. Gerraty free(shell_freeIt); 26333955d011SMarcel Moolenaar #endif 26343955d011SMarcel Moolenaar } 26353955d011SMarcel Moolenaar 263606b9b3e0SSimon J. Gerraty /* 263706b9b3e0SSimon J. Gerraty * Waits for all running jobs to finish and returns. 263806b9b3e0SSimon J. Gerraty * Sets 'aborting' to ABORT_WAIT to prevent other jobs from starting. 263906b9b3e0SSimon J. Gerraty */ 26403955d011SMarcel Moolenaar void 26413955d011SMarcel Moolenaar Job_Wait(void) 26423955d011SMarcel Moolenaar { 26433955d011SMarcel Moolenaar aborting = ABORT_WAIT; 26443955d011SMarcel Moolenaar while (jobTokensRunning != 0) { 26453955d011SMarcel Moolenaar Job_CatchOutput(); 26463955d011SMarcel Moolenaar } 2647956e45f6SSimon J. Gerraty aborting = ABORT_NONE; 26483955d011SMarcel Moolenaar } 26493955d011SMarcel Moolenaar 265006b9b3e0SSimon J. Gerraty /* 265106b9b3e0SSimon J. Gerraty * Abort all currently running jobs without handling output or anything. 2652956e45f6SSimon J. Gerraty * This function is to be called only in the event of a major error. 2653956e45f6SSimon J. Gerraty * Most definitely NOT to be called from JobInterrupt. 26543955d011SMarcel Moolenaar * 265506b9b3e0SSimon J. Gerraty * All children are killed, not just the firstborn. 265606b9b3e0SSimon J. Gerraty */ 26573955d011SMarcel Moolenaar void 26583955d011SMarcel Moolenaar Job_AbortAll(void) 26593955d011SMarcel Moolenaar { 26603955d011SMarcel Moolenaar Job *job; /* the job descriptor in that element */ 26613955d011SMarcel Moolenaar WAIT_T foo; 26623955d011SMarcel Moolenaar 26633955d011SMarcel Moolenaar aborting = ABORT_ERROR; 26643955d011SMarcel Moolenaar 266506b9b3e0SSimon J. Gerraty if (jobTokensRunning != 0) { 26663955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 2667e2eeea75SSimon J. Gerraty if (job->status != JOB_ST_RUNNING) 26683955d011SMarcel Moolenaar continue; 26693955d011SMarcel Moolenaar /* 267006b9b3e0SSimon J. Gerraty * kill the child process with increasingly drastic 267106b9b3e0SSimon J. Gerraty * signals to make darn sure it's dead. 26723955d011SMarcel Moolenaar */ 26733955d011SMarcel Moolenaar KILLPG(job->pid, SIGINT); 26743955d011SMarcel Moolenaar KILLPG(job->pid, SIGKILL); 26753955d011SMarcel Moolenaar } 26763955d011SMarcel Moolenaar } 26773955d011SMarcel Moolenaar 26783955d011SMarcel Moolenaar /* 26793955d011SMarcel Moolenaar * Catch as many children as want to report in at first, then give up 26803955d011SMarcel Moolenaar */ 26813955d011SMarcel Moolenaar while (waitpid((pid_t)-1, &foo, WNOHANG) > 0) 26823955d011SMarcel Moolenaar continue; 26833955d011SMarcel Moolenaar } 26843955d011SMarcel Moolenaar 268506b9b3e0SSimon J. Gerraty /* 268606b9b3e0SSimon J. Gerraty * Tries to restart stopped jobs if there are slots available. 268706b9b3e0SSimon J. Gerraty * Called in process context in response to a SIGCONT. 268806b9b3e0SSimon J. Gerraty */ 26893955d011SMarcel Moolenaar static void 26903955d011SMarcel Moolenaar JobRestartJobs(void) 26913955d011SMarcel Moolenaar { 26923955d011SMarcel Moolenaar Job *job; 26933955d011SMarcel Moolenaar 26943955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 2695e2eeea75SSimon J. Gerraty if (job->status == JOB_ST_RUNNING && 2696e2eeea75SSimon J. Gerraty (make_suspended || job->suspended)) { 269706b9b3e0SSimon J. Gerraty DEBUG1(JOB, "Restarting stopped job pid %d.\n", 269806b9b3e0SSimon J. Gerraty job->pid); 2699e2eeea75SSimon J. Gerraty if (job->suspended) { 270006b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Continued\n", 270106b9b3e0SSimon J. Gerraty job->node->name); 27023955d011SMarcel Moolenaar (void)fflush(stdout); 27033955d011SMarcel Moolenaar } 2704b0c40a00SSimon J. Gerraty job->suspended = false; 27053955d011SMarcel Moolenaar if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) { 270606b9b3e0SSimon J. Gerraty debug_printf("Failed to send SIGCONT to %d\n", 270706b9b3e0SSimon J. Gerraty job->pid); 27083955d011SMarcel Moolenaar } 27093955d011SMarcel Moolenaar } 271006b9b3e0SSimon J. Gerraty if (job->status == JOB_ST_FINISHED) { 271106b9b3e0SSimon J. Gerraty /* 271206b9b3e0SSimon J. Gerraty * Job exit deferred after calling waitpid() in a 271306b9b3e0SSimon J. Gerraty * signal handler 271406b9b3e0SSimon J. Gerraty */ 27153955d011SMarcel Moolenaar JobFinish(job, job->exit_status); 27163955d011SMarcel Moolenaar } 271706b9b3e0SSimon J. Gerraty } 2718b0c40a00SSimon J. Gerraty make_suspended = false; 27193955d011SMarcel Moolenaar } 27203955d011SMarcel Moolenaar 27213955d011SMarcel Moolenaar static void 27223955d011SMarcel Moolenaar watchfd(Job *job) 27233955d011SMarcel Moolenaar { 27243955d011SMarcel Moolenaar if (job->inPollfd != NULL) 27253955d011SMarcel Moolenaar Punt("Watching watched job"); 27263955d011SMarcel Moolenaar 2727dba7b0efSSimon J. Gerraty fds[fdsLen].fd = job->inPipe; 2728dba7b0efSSimon J. Gerraty fds[fdsLen].events = POLLIN; 2729dba7b0efSSimon J. Gerraty jobByFdIndex[fdsLen] = job; 2730dba7b0efSSimon J. Gerraty job->inPollfd = &fds[fdsLen]; 2731dba7b0efSSimon J. Gerraty fdsLen++; 273249caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 273349caa483SSimon J. Gerraty if (useMeta) { 2734dba7b0efSSimon J. Gerraty fds[fdsLen].fd = meta_job_fd(job); 2735dba7b0efSSimon J. Gerraty fds[fdsLen].events = fds[fdsLen].fd == -1 ? 0 : POLLIN; 2736dba7b0efSSimon J. Gerraty jobByFdIndex[fdsLen] = job; 2737dba7b0efSSimon J. Gerraty fdsLen++; 273849caa483SSimon J. Gerraty } 273949caa483SSimon J. Gerraty #endif 27403955d011SMarcel Moolenaar } 27413955d011SMarcel Moolenaar 27423955d011SMarcel Moolenaar static void 27433955d011SMarcel Moolenaar clearfd(Job *job) 27443955d011SMarcel Moolenaar { 2745956e45f6SSimon J. Gerraty size_t i; 27463955d011SMarcel Moolenaar if (job->inPollfd == NULL) 27473955d011SMarcel Moolenaar Punt("Unwatching unwatched job"); 2748956e45f6SSimon J. Gerraty i = (size_t)(job->inPollfd - fds); 2749dba7b0efSSimon J. Gerraty fdsLen--; 275049caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 275149caa483SSimon J. Gerraty if (useMeta) { 275249caa483SSimon J. Gerraty /* 275349caa483SSimon J. Gerraty * Sanity check: there should be two fds per job, so the job's 275449caa483SSimon J. Gerraty * pollfd number should be even. 275549caa483SSimon J. Gerraty */ 275649caa483SSimon J. Gerraty assert(nfds_per_job() == 2); 275706b9b3e0SSimon J. Gerraty if (i % 2 != 0) 275849caa483SSimon J. Gerraty Punt("odd-numbered fd with meta"); 2759dba7b0efSSimon J. Gerraty fdsLen--; 276049caa483SSimon J. Gerraty } 276149caa483SSimon J. Gerraty #endif 2762*d5e0a182SSimon J. Gerraty /* Move last job in table into hole made by dead job. */ 2763dba7b0efSSimon J. Gerraty if (fdsLen != i) { 2764dba7b0efSSimon J. Gerraty fds[i] = fds[fdsLen]; 2765dba7b0efSSimon J. Gerraty jobByFdIndex[i] = jobByFdIndex[fdsLen]; 2766dba7b0efSSimon J. Gerraty jobByFdIndex[i]->inPollfd = &fds[i]; 276749caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 276849caa483SSimon J. Gerraty if (useMeta) { 2769dba7b0efSSimon J. Gerraty fds[i + 1] = fds[fdsLen + 1]; 2770dba7b0efSSimon J. Gerraty jobByFdIndex[i + 1] = jobByFdIndex[fdsLen + 1]; 277149caa483SSimon J. Gerraty } 277249caa483SSimon J. Gerraty #endif 27733955d011SMarcel Moolenaar } 27743955d011SMarcel Moolenaar job->inPollfd = NULL; 27753955d011SMarcel Moolenaar } 27763955d011SMarcel Moolenaar 2777b0c40a00SSimon J. Gerraty static bool 27783955d011SMarcel Moolenaar readyfd(Job *job) 27793955d011SMarcel Moolenaar { 27803955d011SMarcel Moolenaar if (job->inPollfd == NULL) 27813955d011SMarcel Moolenaar Punt("Polling unwatched job"); 27823955d011SMarcel Moolenaar return (job->inPollfd->revents & POLLIN) != 0; 27833955d011SMarcel Moolenaar } 27843955d011SMarcel Moolenaar 278506b9b3e0SSimon J. Gerraty /* 278606b9b3e0SSimon J. Gerraty * Put a token (back) into the job pipe. 278706b9b3e0SSimon J. Gerraty * This allows a make process to start a build job. 278806b9b3e0SSimon J. Gerraty */ 27893955d011SMarcel Moolenaar static void 27903955d011SMarcel Moolenaar JobTokenAdd(void) 27913955d011SMarcel Moolenaar { 27923955d011SMarcel Moolenaar char tok = JOB_TOKENS[aborting], tok1; 27933955d011SMarcel Moolenaar 27942d395cb5SSimon J. Gerraty if (!Job_error_token && aborting == ABORT_ERROR) { 27952d395cb5SSimon J. Gerraty if (jobTokensRunning == 0) 27962d395cb5SSimon J. Gerraty return; 27972d395cb5SSimon J. Gerraty tok = '+'; /* no error token */ 27982d395cb5SSimon J. Gerraty } 27992d395cb5SSimon J. Gerraty 28003955d011SMarcel Moolenaar /* If we are depositing an error token flush everything else */ 28013955d011SMarcel Moolenaar while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1) 28023955d011SMarcel Moolenaar continue; 28033955d011SMarcel Moolenaar 2804956e45f6SSimon J. Gerraty DEBUG3(JOB, "(%d) aborting %d, deposit token %c\n", 28052d395cb5SSimon J. Gerraty getpid(), aborting, tok); 28063cbdda60SSimon J. Gerraty while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN) 28073cbdda60SSimon J. Gerraty continue; 28083955d011SMarcel Moolenaar } 28093955d011SMarcel Moolenaar 2810dba7b0efSSimon J. Gerraty /* Get a temp file */ 2811dba7b0efSSimon J. Gerraty int 2812dba7b0efSSimon J. Gerraty Job_TempFile(const char *pattern, char *tfile, size_t tfile_sz) 2813dba7b0efSSimon J. Gerraty { 2814dba7b0efSSimon J. Gerraty int fd; 2815dba7b0efSSimon J. Gerraty sigset_t mask; 2816dba7b0efSSimon J. Gerraty 2817dba7b0efSSimon J. Gerraty JobSigLock(&mask); 2818dba7b0efSSimon J. Gerraty fd = mkTempFile(pattern, tfile, tfile_sz); 2819dba7b0efSSimon J. Gerraty if (tfile != NULL && !DEBUG(SCRIPT)) 2820dba7b0efSSimon J. Gerraty unlink(tfile); 2821dba7b0efSSimon J. Gerraty JobSigUnlock(&mask); 2822dba7b0efSSimon J. Gerraty 2823dba7b0efSSimon J. Gerraty return fd; 2824dba7b0efSSimon J. Gerraty } 2825dba7b0efSSimon J. Gerraty 2826956e45f6SSimon J. Gerraty /* Prep the job token pipe in the root make process. */ 28273955d011SMarcel Moolenaar void 28283955d011SMarcel Moolenaar Job_ServerStart(int max_tokens, int jp_0, int jp_1) 28293955d011SMarcel Moolenaar { 28303955d011SMarcel Moolenaar int i; 28313955d011SMarcel Moolenaar char jobarg[64]; 28323955d011SMarcel Moolenaar 28333955d011SMarcel Moolenaar if (jp_0 >= 0 && jp_1 >= 0) { 28343955d011SMarcel Moolenaar /* Pipe passed in from parent */ 28353955d011SMarcel Moolenaar tokenWaitJob.inPipe = jp_0; 28363955d011SMarcel Moolenaar tokenWaitJob.outPipe = jp_1; 2837be19d90bSSimon J. Gerraty (void)fcntl(jp_0, F_SETFD, FD_CLOEXEC); 2838be19d90bSSimon J. Gerraty (void)fcntl(jp_1, F_SETFD, FD_CLOEXEC); 28393955d011SMarcel Moolenaar return; 28403955d011SMarcel Moolenaar } 28413955d011SMarcel Moolenaar 28423955d011SMarcel Moolenaar JobCreatePipe(&tokenWaitJob, 15); 28433955d011SMarcel Moolenaar 2844e2eeea75SSimon J. Gerraty snprintf(jobarg, sizeof jobarg, "%d,%d", 28453955d011SMarcel Moolenaar tokenWaitJob.inPipe, tokenWaitJob.outPipe); 28463955d011SMarcel Moolenaar 2847dba7b0efSSimon J. Gerraty Global_Append(MAKEFLAGS, "-J"); 2848dba7b0efSSimon J. Gerraty Global_Append(MAKEFLAGS, jobarg); 28493955d011SMarcel Moolenaar 28503955d011SMarcel Moolenaar /* 28513955d011SMarcel Moolenaar * Preload the job pipe with one token per job, save the one 28523955d011SMarcel Moolenaar * "extra" token for the primary job. 28533955d011SMarcel Moolenaar * 28543955d011SMarcel Moolenaar * XXX should clip maxJobs against PIPE_BUF -- if max_tokens is 28553955d011SMarcel Moolenaar * larger than the write buffer size of the pipe, we will 28563955d011SMarcel Moolenaar * deadlock here. 28573955d011SMarcel Moolenaar */ 28583955d011SMarcel Moolenaar for (i = 1; i < max_tokens; i++) 28593955d011SMarcel Moolenaar JobTokenAdd(); 28603955d011SMarcel Moolenaar } 28613955d011SMarcel Moolenaar 2862956e45f6SSimon J. Gerraty /* Return a withdrawn token to the pool. */ 28633955d011SMarcel Moolenaar void 28643955d011SMarcel Moolenaar Job_TokenReturn(void) 28653955d011SMarcel Moolenaar { 28663955d011SMarcel Moolenaar jobTokensRunning--; 28673955d011SMarcel Moolenaar if (jobTokensRunning < 0) 28683955d011SMarcel Moolenaar Punt("token botch"); 286906b9b3e0SSimon J. Gerraty if (jobTokensRunning != 0 || JOB_TOKENS[aborting] != '+') 28703955d011SMarcel Moolenaar JobTokenAdd(); 28713955d011SMarcel Moolenaar } 28723955d011SMarcel Moolenaar 287306b9b3e0SSimon J. Gerraty /* 287406b9b3e0SSimon J. Gerraty * Attempt to withdraw a token from the pool. 28753955d011SMarcel Moolenaar * 2876956e45f6SSimon J. Gerraty * If pool is empty, set wantToken so that we wake up when a token is 2877956e45f6SSimon J. Gerraty * released. 28783955d011SMarcel Moolenaar * 2879b0c40a00SSimon J. Gerraty * Returns true if a token was withdrawn, and false if the pool is currently 288006b9b3e0SSimon J. Gerraty * empty. 288106b9b3e0SSimon J. Gerraty */ 2882b0c40a00SSimon J. Gerraty bool 28833955d011SMarcel Moolenaar Job_TokenWithdraw(void) 28843955d011SMarcel Moolenaar { 28853955d011SMarcel Moolenaar char tok, tok1; 2886956e45f6SSimon J. Gerraty ssize_t count; 28873955d011SMarcel Moolenaar 28883955d011SMarcel Moolenaar wantToken = 0; 2889956e45f6SSimon J. Gerraty DEBUG3(JOB, "Job_TokenWithdraw(%d): aborting %d, running %d\n", 28903955d011SMarcel Moolenaar getpid(), aborting, jobTokensRunning); 28913955d011SMarcel Moolenaar 2892956e45f6SSimon J. Gerraty if (aborting != ABORT_NONE || (jobTokensRunning >= opts.maxJobs)) 2893b0c40a00SSimon J. Gerraty return false; 28943955d011SMarcel Moolenaar 28953955d011SMarcel Moolenaar count = read(tokenWaitJob.inPipe, &tok, 1); 28963955d011SMarcel Moolenaar if (count == 0) 28973955d011SMarcel Moolenaar Fatal("eof on job pipe!"); 28983955d011SMarcel Moolenaar if (count < 0 && jobTokensRunning != 0) { 2899*d5e0a182SSimon J. Gerraty if (errno != EAGAIN) 29003955d011SMarcel Moolenaar Fatal("job pipe read: %s", strerror(errno)); 2901956e45f6SSimon J. Gerraty DEBUG1(JOB, "(%d) blocked for token\n", getpid()); 290206b9b3e0SSimon J. Gerraty wantToken = 1; 2903b0c40a00SSimon J. Gerraty return false; 29043955d011SMarcel Moolenaar } 29053955d011SMarcel Moolenaar 29063955d011SMarcel Moolenaar if (count == 1 && tok != '+') { 290706b9b3e0SSimon J. Gerraty /* make being aborted - remove any other job tokens */ 2908956e45f6SSimon J. Gerraty DEBUG2(JOB, "(%d) aborted by token %c\n", getpid(), tok); 29093955d011SMarcel Moolenaar while (read(tokenWaitJob.inPipe, &tok1, 1) == 1) 29103955d011SMarcel Moolenaar continue; 29113955d011SMarcel Moolenaar /* And put the stopper back */ 291206b9b3e0SSimon J. Gerraty while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && 291306b9b3e0SSimon J. Gerraty errno == EAGAIN) 29143cbdda60SSimon J. Gerraty continue; 2915e2eeea75SSimon J. Gerraty if (shouldDieQuietly(NULL, 1)) 291606b9b3e0SSimon J. Gerraty exit(6); /* we aborted */ 291706b9b3e0SSimon J. Gerraty Fatal("A failure has been detected " 291806b9b3e0SSimon J. Gerraty "in another branch of the parallel make"); 29193955d011SMarcel Moolenaar } 29203955d011SMarcel Moolenaar 29213955d011SMarcel Moolenaar if (count == 1 && jobTokensRunning == 0) 29223955d011SMarcel Moolenaar /* We didn't want the token really */ 292306b9b3e0SSimon J. Gerraty while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && 292406b9b3e0SSimon J. Gerraty errno == EAGAIN) 29253cbdda60SSimon J. Gerraty continue; 29263955d011SMarcel Moolenaar 29273955d011SMarcel Moolenaar jobTokensRunning++; 2928956e45f6SSimon J. Gerraty DEBUG1(JOB, "(%d) withdrew token\n", getpid()); 2929b0c40a00SSimon J. Gerraty return true; 29303955d011SMarcel Moolenaar } 29313955d011SMarcel Moolenaar 293206b9b3e0SSimon J. Gerraty /* 293306b9b3e0SSimon J. Gerraty * Run the named target if found. If a filename is specified, then set that 2934956e45f6SSimon J. Gerraty * to the sources. 29351748de26SSimon J. Gerraty * 293606b9b3e0SSimon J. Gerraty * Exits if the target fails. 293706b9b3e0SSimon J. Gerraty */ 2938b0c40a00SSimon J. Gerraty bool 293906b9b3e0SSimon J. Gerraty Job_RunTarget(const char *target, const char *fname) 294006b9b3e0SSimon J. Gerraty { 2941956e45f6SSimon J. Gerraty GNode *gn = Targ_FindNode(target); 29421748de26SSimon J. Gerraty if (gn == NULL) 2943b0c40a00SSimon J. Gerraty return false; 29441748de26SSimon J. Gerraty 294506b9b3e0SSimon J. Gerraty if (fname != NULL) 2946dba7b0efSSimon J. Gerraty Var_Set(gn, ALLSRC, fname); 29471748de26SSimon J. Gerraty 29481748de26SSimon J. Gerraty JobRun(gn); 294906b9b3e0SSimon J. Gerraty /* XXX: Replace with GNode_IsError(gn) */ 29501748de26SSimon J. Gerraty if (gn->made == ERROR) { 29519f45a3c8SSimon J. Gerraty PrintOnError(gn, "\n\nStop.\n"); 29521748de26SSimon J. Gerraty exit(1); 29531748de26SSimon J. Gerraty } 2954b0c40a00SSimon J. Gerraty return true; 29551748de26SSimon J. Gerraty } 29561748de26SSimon J. Gerraty 29573955d011SMarcel Moolenaar #ifdef USE_SELECT 29583955d011SMarcel Moolenaar int 29593955d011SMarcel Moolenaar emul_poll(struct pollfd *fd, int nfd, int timeout) 29603955d011SMarcel Moolenaar { 29613955d011SMarcel Moolenaar fd_set rfds, wfds; 29623955d011SMarcel Moolenaar int i, maxfd, nselect, npoll; 29633955d011SMarcel Moolenaar struct timeval tv, *tvp; 29643955d011SMarcel Moolenaar long usecs; 29653955d011SMarcel Moolenaar 29663955d011SMarcel Moolenaar FD_ZERO(&rfds); 29673955d011SMarcel Moolenaar FD_ZERO(&wfds); 29683955d011SMarcel Moolenaar 29693955d011SMarcel Moolenaar maxfd = -1; 29703955d011SMarcel Moolenaar for (i = 0; i < nfd; i++) { 29713955d011SMarcel Moolenaar fd[i].revents = 0; 29723955d011SMarcel Moolenaar 29733955d011SMarcel Moolenaar if (fd[i].events & POLLIN) 29743955d011SMarcel Moolenaar FD_SET(fd[i].fd, &rfds); 29753955d011SMarcel Moolenaar 29763955d011SMarcel Moolenaar if (fd[i].events & POLLOUT) 29773955d011SMarcel Moolenaar FD_SET(fd[i].fd, &wfds); 29783955d011SMarcel Moolenaar 29793955d011SMarcel Moolenaar if (fd[i].fd > maxfd) 29803955d011SMarcel Moolenaar maxfd = fd[i].fd; 29813955d011SMarcel Moolenaar } 29823955d011SMarcel Moolenaar 29833955d011SMarcel Moolenaar if (maxfd >= FD_SETSIZE) { 29843955d011SMarcel Moolenaar Punt("Ran out of fd_set slots; " 29853955d011SMarcel Moolenaar "recompile with a larger FD_SETSIZE."); 29863955d011SMarcel Moolenaar } 29873955d011SMarcel Moolenaar 29883955d011SMarcel Moolenaar if (timeout < 0) { 29893955d011SMarcel Moolenaar tvp = NULL; 29903955d011SMarcel Moolenaar } else { 29913955d011SMarcel Moolenaar usecs = timeout * 1000; 29923955d011SMarcel Moolenaar tv.tv_sec = usecs / 1000000; 29933955d011SMarcel Moolenaar tv.tv_usec = usecs % 1000000; 29943955d011SMarcel Moolenaar tvp = &tv; 29953955d011SMarcel Moolenaar } 29963955d011SMarcel Moolenaar 2997e2eeea75SSimon J. Gerraty nselect = select(maxfd + 1, &rfds, &wfds, NULL, tvp); 29983955d011SMarcel Moolenaar 29993955d011SMarcel Moolenaar if (nselect <= 0) 30003955d011SMarcel Moolenaar return nselect; 30013955d011SMarcel Moolenaar 30023955d011SMarcel Moolenaar npoll = 0; 30033955d011SMarcel Moolenaar for (i = 0; i < nfd; i++) { 30043955d011SMarcel Moolenaar if (FD_ISSET(fd[i].fd, &rfds)) 30053955d011SMarcel Moolenaar fd[i].revents |= POLLIN; 30063955d011SMarcel Moolenaar 30073955d011SMarcel Moolenaar if (FD_ISSET(fd[i].fd, &wfds)) 30083955d011SMarcel Moolenaar fd[i].revents |= POLLOUT; 30093955d011SMarcel Moolenaar 30103955d011SMarcel Moolenaar if (fd[i].revents) 30113955d011SMarcel Moolenaar npoll++; 30123955d011SMarcel Moolenaar } 30133955d011SMarcel Moolenaar 30143955d011SMarcel Moolenaar return npoll; 30153955d011SMarcel Moolenaar } 30163955d011SMarcel Moolenaar #endif /* USE_SELECT */ 3017