1b0c40a00SSimon J. Gerraty /* $NetBSD: job.c,v 1.435 2021/06/16 09:47:51 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 /* 733955d011SMarcel Moolenaar * job.c -- 743955d011SMarcel Moolenaar * handle the creation etc. of our child processes. 753955d011SMarcel Moolenaar * 763955d011SMarcel Moolenaar * Interface: 77956e45f6SSimon J. Gerraty * Job_Init Called to initialize this module. In addition, 78dba7b0efSSimon J. Gerraty * the .BEGIN target is made including all of its 79dba7b0efSSimon J. Gerraty * dependencies before this function returns. 80956e45f6SSimon J. Gerraty * Hence, the makefiles must have been parsed 81956e45f6SSimon J. Gerraty * before this function is called. 82956e45f6SSimon J. Gerraty * 83956e45f6SSimon J. Gerraty * Job_End Clean up any memory used. 84956e45f6SSimon J. Gerraty * 853955d011SMarcel Moolenaar * Job_Make Start the creation of the given target. 863955d011SMarcel Moolenaar * 87956e45f6SSimon J. Gerraty * Job_CatchChildren 88956e45f6SSimon J. Gerraty * Check for and handle the termination of any 893955d011SMarcel Moolenaar * children. This must be called reasonably 903955d011SMarcel Moolenaar * frequently to keep the whole make going at 913955d011SMarcel Moolenaar * a decent clip, since job table entries aren't 923955d011SMarcel Moolenaar * removed until their process is caught this way. 933955d011SMarcel Moolenaar * 94956e45f6SSimon J. Gerraty * Job_CatchOutput 95956e45f6SSimon J. Gerraty * Print any output our children have produced. 963955d011SMarcel Moolenaar * Should also be called fairly frequently to 973955d011SMarcel Moolenaar * keep the user informed of what's going on. 983955d011SMarcel Moolenaar * If no output is waiting, it will block for 993955d011SMarcel Moolenaar * a time given by the SEL_* constants, below, 1003955d011SMarcel Moolenaar * or until output is ready. 1013955d011SMarcel Moolenaar * 102dba7b0efSSimon J. Gerraty * Job_ParseShell Given a special dependency line with target '.SHELL', 103dba7b0efSSimon J. Gerraty * define the shell that is used for the creation 104dba7b0efSSimon J. Gerraty * commands in jobs mode. 1053955d011SMarcel Moolenaar * 1063955d011SMarcel Moolenaar * Job_Finish Perform any final processing which needs doing. 1073955d011SMarcel Moolenaar * This includes the execution of any commands 1083955d011SMarcel Moolenaar * which have been/were attached to the .END 1093955d011SMarcel Moolenaar * target. It should only be called when the 1103955d011SMarcel Moolenaar * job table is empty. 1113955d011SMarcel Moolenaar * 112dba7b0efSSimon J. Gerraty * Job_AbortAll Abort all currently running jobs. Do not handle 113dba7b0efSSimon J. Gerraty * output or do anything for the jobs, just kill them. 114dba7b0efSSimon J. Gerraty * Should only be called in an emergency. 1153955d011SMarcel Moolenaar * 116956e45f6SSimon J. Gerraty * Job_CheckCommands 117956e45f6SSimon J. Gerraty * Verify that the commands for a target are 1183955d011SMarcel Moolenaar * ok. Provide them if necessary and possible. 1193955d011SMarcel Moolenaar * 1203955d011SMarcel Moolenaar * Job_Touch Update a target without really updating it. 1213955d011SMarcel Moolenaar * 1223955d011SMarcel Moolenaar * Job_Wait Wait for all currently-running jobs to finish. 1233955d011SMarcel Moolenaar */ 1243955d011SMarcel Moolenaar 1253955d011SMarcel Moolenaar #ifdef HAVE_CONFIG_H 1263955d011SMarcel Moolenaar # include "config.h" 1273955d011SMarcel Moolenaar #endif 1283955d011SMarcel Moolenaar #include <sys/types.h> 1293955d011SMarcel Moolenaar #include <sys/stat.h> 1303955d011SMarcel Moolenaar #include <sys/file.h> 1313955d011SMarcel Moolenaar #include <sys/time.h> 1323955d011SMarcel Moolenaar #include "wait.h" 1333955d011SMarcel Moolenaar 1343955d011SMarcel Moolenaar #include <errno.h> 1353955d011SMarcel Moolenaar #if !defined(USE_SELECT) && defined(HAVE_POLL_H) 1363955d011SMarcel Moolenaar #include <poll.h> 1373955d011SMarcel Moolenaar #else 1383955d011SMarcel Moolenaar #ifndef USE_SELECT /* no poll.h */ 1393955d011SMarcel Moolenaar # define USE_SELECT 1403955d011SMarcel Moolenaar #endif 1413955d011SMarcel Moolenaar #if defined(HAVE_SYS_SELECT_H) 1423955d011SMarcel Moolenaar # include <sys/select.h> 1433955d011SMarcel Moolenaar #endif 1443955d011SMarcel Moolenaar #endif 1453955d011SMarcel Moolenaar #include <signal.h> 1463955d011SMarcel Moolenaar #include <utime.h> 1473955d011SMarcel Moolenaar #if defined(HAVE_SYS_SOCKET_H) 1483955d011SMarcel Moolenaar # include <sys/socket.h> 1493955d011SMarcel Moolenaar #endif 1503955d011SMarcel Moolenaar 1513955d011SMarcel Moolenaar #include "make.h" 1523955d011SMarcel Moolenaar #include "dir.h" 1533955d011SMarcel Moolenaar #include "job.h" 1543955d011SMarcel Moolenaar #include "pathnames.h" 1553955d011SMarcel Moolenaar #include "trace.h" 156956e45f6SSimon J. Gerraty 157956e45f6SSimon J. Gerraty /* "@(#)job.c 8.2 (Berkeley) 3/19/94" */ 158b0c40a00SSimon J. Gerraty MAKE_RCSID("$NetBSD: job.c,v 1.435 2021/06/16 09:47:51 rillig Exp $"); 159956e45f6SSimon J. Gerraty 16006b9b3e0SSimon J. Gerraty /* 16106b9b3e0SSimon J. Gerraty * A shell defines how the commands are run. All commands for a target are 162956e45f6SSimon J. Gerraty * written into a single file, which is then given to the shell to execute 163956e45f6SSimon J. Gerraty * the commands from it. The commands are written to the file using a few 164956e45f6SSimon J. Gerraty * templates for echo control and error control. 165956e45f6SSimon J. Gerraty * 166956e45f6SSimon J. Gerraty * The name of the shell is the basename for the predefined shells, such as 167956e45f6SSimon J. Gerraty * "sh", "csh", "bash". For custom shells, it is the full pathname, and its 168956e45f6SSimon J. Gerraty * basename is used to select the type of shell; the longest match wins. 169956e45f6SSimon J. Gerraty * So /usr/pkg/bin/bash has type sh, /usr/local/bin/tcsh has type csh. 170956e45f6SSimon J. Gerraty * 171956e45f6SSimon J. Gerraty * The echoing of command lines is controlled using hasEchoCtl, echoOff, 172956e45f6SSimon J. Gerraty * echoOn, noPrint and noPrintLen. When echoOff is executed by the shell, it 173956e45f6SSimon J. Gerraty * still outputs something, but this something is not interesting, therefore 174956e45f6SSimon J. Gerraty * it is filtered out using noPrint and noPrintLen. 175956e45f6SSimon J. Gerraty * 176956e45f6SSimon J. Gerraty * The error checking for individual commands is controlled using hasErrCtl, 17706b9b3e0SSimon J. Gerraty * errOn, errOff and runChkTmpl. 178956e45f6SSimon J. Gerraty * 179dba7b0efSSimon J. Gerraty * In case a shell doesn't have error control, echoTmpl is a printf template 180dba7b0efSSimon J. Gerraty * for echoing the command, should echoing be on; runIgnTmpl is another 181dba7b0efSSimon J. Gerraty * printf template for executing the command while ignoring the return 18206b9b3e0SSimon J. Gerraty * status. Finally runChkTmpl is a printf template for running the command and 183956e45f6SSimon J. Gerraty * causing the shell to exit on error. If any of these strings are empty when 184b0c40a00SSimon J. Gerraty * hasErrCtl is false, the command will be executed anyway as is, and if it 185956e45f6SSimon J. Gerraty * causes an error, so be it. Any templates set up to echo the command will 186dba7b0efSSimon J. Gerraty * escape any '$ ` \ "' characters in the command string to avoid unwanted 187dba7b0efSSimon J. Gerraty * shell code injection, the escaped command is safe to use in double quotes. 188956e45f6SSimon J. Gerraty * 189956e45f6SSimon J. Gerraty * The command-line flags "echo" and "exit" also control the behavior. The 190956e45f6SSimon J. Gerraty * "echo" flag causes the shell to start echoing commands right away. The 191956e45f6SSimon J. Gerraty * "exit" flag causes the shell to exit when an error is detected in one of 192956e45f6SSimon J. Gerraty * the commands. 193956e45f6SSimon J. Gerraty */ 194956e45f6SSimon J. Gerraty typedef struct Shell { 195956e45f6SSimon J. Gerraty 19606b9b3e0SSimon J. Gerraty /* 19706b9b3e0SSimon J. Gerraty * The name of the shell. For Bourne and C shells, this is used only 19806b9b3e0SSimon J. Gerraty * to find the shell description when used as the single source of a 19906b9b3e0SSimon J. Gerraty * .SHELL target. For user-defined shells, this is the full path of 20006b9b3e0SSimon J. Gerraty * the shell. 20106b9b3e0SSimon J. Gerraty */ 202956e45f6SSimon J. Gerraty const char *name; 203956e45f6SSimon J. Gerraty 204b0c40a00SSimon J. Gerraty bool hasEchoCtl; /* whether both echoOff and echoOn are there */ 20506b9b3e0SSimon J. Gerraty const char *echoOff; /* command to turn echoing off */ 20606b9b3e0SSimon J. Gerraty const char *echoOn; /* command to turn echoing back on */ 20706b9b3e0SSimon J. Gerraty const char *noPrint; /* text to skip when printing output from the 208956e45f6SSimon J. Gerraty * shell. This is usually the same as echoOff */ 209956e45f6SSimon J. Gerraty size_t noPrintLen; /* length of noPrint command */ 210956e45f6SSimon J. Gerraty 211b0c40a00SSimon J. Gerraty bool hasErrCtl; /* whether error checking can be controlled 21206b9b3e0SSimon J. Gerraty * for individual commands */ 21306b9b3e0SSimon J. Gerraty const char *errOn; /* command to turn on error checking */ 21406b9b3e0SSimon J. Gerraty const char *errOff; /* command to turn off error checking */ 21506b9b3e0SSimon J. Gerraty 21606b9b3e0SSimon J. Gerraty const char *echoTmpl; /* template to echo a command */ 21706b9b3e0SSimon J. Gerraty const char *runIgnTmpl; /* template to run a command 21806b9b3e0SSimon J. Gerraty * without error checking */ 21906b9b3e0SSimon J. Gerraty const char *runChkTmpl; /* template to run a command 22006b9b3e0SSimon J. Gerraty * with error checking */ 221956e45f6SSimon J. Gerraty 222956e45f6SSimon J. Gerraty /* string literal that results in a newline character when it appears 223956e45f6SSimon J. Gerraty * outside of any 'quote' or "quote" characters */ 224956e45f6SSimon J. Gerraty const char *newline; 225956e45f6SSimon J. Gerraty char commentChar; /* character used by shell for comment lines */ 226956e45f6SSimon J. Gerraty 22706b9b3e0SSimon J. Gerraty const char *echoFlag; /* shell flag to echo commands */ 22806b9b3e0SSimon J. Gerraty const char *errFlag; /* shell flag to exit on error */ 229956e45f6SSimon J. Gerraty } Shell; 2303955d011SMarcel Moolenaar 23106b9b3e0SSimon J. Gerraty typedef struct CommandFlags { 232dba7b0efSSimon J. Gerraty /* Whether to echo the command before or instead of running it. */ 233b0c40a00SSimon J. Gerraty bool echo; 23406b9b3e0SSimon J. Gerraty 23506b9b3e0SSimon J. Gerraty /* Run the command even in -n or -N mode. */ 236b0c40a00SSimon J. Gerraty bool always; 23706b9b3e0SSimon J. Gerraty 23806b9b3e0SSimon J. Gerraty /* 239b0c40a00SSimon J. Gerraty * true if we turned error checking off before writing the command to 240b0c40a00SSimon J. Gerraty * the commands file and need to turn it back on 24106b9b3e0SSimon J. Gerraty */ 242b0c40a00SSimon J. Gerraty bool ignerr; 24306b9b3e0SSimon J. Gerraty } CommandFlags; 24406b9b3e0SSimon J. Gerraty 24506b9b3e0SSimon J. Gerraty /* 24606b9b3e0SSimon J. Gerraty * Write shell commands to a file. 24706b9b3e0SSimon J. Gerraty * 24806b9b3e0SSimon J. Gerraty * TODO: keep track of whether commands are echoed. 24906b9b3e0SSimon J. Gerraty * TODO: keep track of whether error checking is active. 25006b9b3e0SSimon J. Gerraty */ 25106b9b3e0SSimon J. Gerraty typedef struct ShellWriter { 25206b9b3e0SSimon J. Gerraty FILE *f; 25306b9b3e0SSimon J. Gerraty 25406b9b3e0SSimon J. Gerraty /* we've sent 'set -x' */ 255b0c40a00SSimon J. Gerraty bool xtraced; 25606b9b3e0SSimon J. Gerraty 25706b9b3e0SSimon J. Gerraty } ShellWriter; 25806b9b3e0SSimon J. Gerraty 2593955d011SMarcel Moolenaar /* 2609a4bc556SSimon J. Gerraty * FreeBSD: traditionally .MAKE is not required to 2619a4bc556SSimon J. Gerraty * pass jobs queue to sub-makes. 2629a4bc556SSimon J. Gerraty * Use .MAKE.ALWAYS_PASS_JOB_QUEUE=no to disable. 2639a4bc556SSimon J. Gerraty */ 2649a4bc556SSimon J. Gerraty #define MAKE_ALWAYS_PASS_JOB_QUEUE ".MAKE.ALWAYS_PASS_JOB_QUEUE" 265*68c4481aSSimon J. Gerraty static bool Always_pass_job_queue = true; 2662d395cb5SSimon J. Gerraty /* 2672d395cb5SSimon J. Gerraty * FreeBSD: aborting entire parallel make isn't always 2682d395cb5SSimon J. Gerraty * desired. When doing tinderbox for example, failure of 2692d395cb5SSimon J. Gerraty * one architecture should not stop all. 2702d395cb5SSimon J. Gerraty * We still want to bail on interrupt though. 2712d395cb5SSimon J. Gerraty */ 2722d395cb5SSimon J. Gerraty #define MAKE_JOB_ERROR_TOKEN "MAKE_JOB_ERROR_TOKEN" 273*68c4481aSSimon J. Gerraty static bool Job_error_token = true; 2749a4bc556SSimon J. Gerraty 2759a4bc556SSimon J. Gerraty /* 2763955d011SMarcel Moolenaar * error handling variables 2773955d011SMarcel Moolenaar */ 27806b9b3e0SSimon J. Gerraty static int job_errors = 0; /* number of errors reported */ 279b0c40a00SSimon J. Gerraty static enum { /* Why is the make aborting? */ 280956e45f6SSimon J. Gerraty ABORT_NONE, 281b0c40a00SSimon J. Gerraty ABORT_ERROR, /* Aborted because of an error */ 282b0c40a00SSimon J. Gerraty ABORT_INTERRUPT, /* Aborted because it was interrupted */ 283956e45f6SSimon J. Gerraty ABORT_WAIT /* Waiting for jobs to finish */ 284b0c40a00SSimon J. Gerraty } aborting = ABORT_NONE; 2853955d011SMarcel Moolenaar #define JOB_TOKENS "+EI+" /* Token to requeue for each abort state */ 2863955d011SMarcel Moolenaar 2873955d011SMarcel Moolenaar /* 2883955d011SMarcel Moolenaar * this tracks the number of tokens currently "out" to build jobs. 2893955d011SMarcel Moolenaar */ 2903955d011SMarcel Moolenaar int jobTokensRunning = 0; 2913955d011SMarcel Moolenaar 292956e45f6SSimon J. Gerraty typedef enum JobStartResult { 293956e45f6SSimon J. Gerraty JOB_RUNNING, /* Job is running */ 294956e45f6SSimon J. Gerraty JOB_ERROR, /* Error in starting the job */ 295956e45f6SSimon J. Gerraty JOB_FINISHED /* The job is already finished */ 296956e45f6SSimon J. Gerraty } JobStartResult; 2973955d011SMarcel Moolenaar 2983955d011SMarcel Moolenaar /* 2993955d011SMarcel Moolenaar * Descriptions for various shells. 3003955d011SMarcel Moolenaar * 3013955d011SMarcel Moolenaar * The build environment may set DEFSHELL_INDEX to one of 3023955d011SMarcel Moolenaar * DEFSHELL_INDEX_SH, DEFSHELL_INDEX_KSH, or DEFSHELL_INDEX_CSH, to 303956e45f6SSimon J. Gerraty * select one of the predefined shells as the default shell. 3043955d011SMarcel Moolenaar * 3053955d011SMarcel Moolenaar * Alternatively, the build environment may set DEFSHELL_CUSTOM to the 3063955d011SMarcel Moolenaar * name or the full path of a sh-compatible shell, which will be used as 3073955d011SMarcel Moolenaar * the default shell. 3083955d011SMarcel Moolenaar * 3093955d011SMarcel Moolenaar * ".SHELL" lines in Makefiles can choose the default shell from the 310956e45f6SSimon J. Gerraty * set defined here, or add additional shells. 3113955d011SMarcel Moolenaar */ 3123955d011SMarcel Moolenaar 3133955d011SMarcel Moolenaar #ifdef DEFSHELL_CUSTOM 3143955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CUSTOM 0 3153955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH 1 3163955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH 2 3173955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH 3 3183955d011SMarcel Moolenaar #else /* !DEFSHELL_CUSTOM */ 3193955d011SMarcel Moolenaar #define DEFSHELL_INDEX_SH 0 3203955d011SMarcel Moolenaar #define DEFSHELL_INDEX_KSH 1 3213955d011SMarcel Moolenaar #define DEFSHELL_INDEX_CSH 2 3223955d011SMarcel Moolenaar #endif /* !DEFSHELL_CUSTOM */ 3233955d011SMarcel Moolenaar 3243955d011SMarcel Moolenaar #ifndef DEFSHELL_INDEX 3253955d011SMarcel Moolenaar #define DEFSHELL_INDEX 0 /* DEFSHELL_INDEX_CUSTOM or DEFSHELL_INDEX_SH */ 3263955d011SMarcel Moolenaar #endif /* !DEFSHELL_INDEX */ 3273955d011SMarcel Moolenaar 3283955d011SMarcel Moolenaar static Shell shells[] = { 3293955d011SMarcel Moolenaar #ifdef DEFSHELL_CUSTOM 3303955d011SMarcel Moolenaar /* 3313955d011SMarcel Moolenaar * An sh-compatible shell with a non-standard name. 3323955d011SMarcel Moolenaar * 3333955d011SMarcel Moolenaar * Keep this in sync with the "sh" description below, but avoid 3343955d011SMarcel Moolenaar * non-portable features that might not be supplied by all 3353955d011SMarcel Moolenaar * sh-compatible shells. 3363955d011SMarcel Moolenaar */ 3373955d011SMarcel Moolenaar { 338956e45f6SSimon J. Gerraty DEFSHELL_CUSTOM, /* .name */ 339b0c40a00SSimon J. Gerraty false, /* .hasEchoCtl */ 340956e45f6SSimon J. Gerraty "", /* .echoOff */ 341956e45f6SSimon J. Gerraty "", /* .echoOn */ 342956e45f6SSimon J. Gerraty "", /* .noPrint */ 343956e45f6SSimon J. Gerraty 0, /* .noPrintLen */ 344b0c40a00SSimon J. Gerraty false, /* .hasErrCtl */ 34506b9b3e0SSimon J. Gerraty "", /* .errOn */ 34606b9b3e0SSimon J. Gerraty "", /* .errOff */ 34706b9b3e0SSimon J. Gerraty "echo \"%s\"\n", /* .echoTmpl */ 34806b9b3e0SSimon J. Gerraty "%s\n", /* .runIgnTmpl */ 34906b9b3e0SSimon J. Gerraty "{ %s \n} || exit $?\n", /* .runChkTmpl */ 350956e45f6SSimon J. Gerraty "'\n'", /* .newline */ 351956e45f6SSimon J. Gerraty '#', /* .commentChar */ 35206b9b3e0SSimon J. Gerraty "", /* .echoFlag */ 35306b9b3e0SSimon J. Gerraty "", /* .errFlag */ 3543955d011SMarcel Moolenaar }, 3553955d011SMarcel Moolenaar #endif /* DEFSHELL_CUSTOM */ 3563955d011SMarcel Moolenaar /* 3573955d011SMarcel Moolenaar * SH description. Echo control is also possible and, under 3583955d011SMarcel Moolenaar * sun UNIX anyway, one can even control error checking. 3593955d011SMarcel Moolenaar */ 3603955d011SMarcel Moolenaar { 361956e45f6SSimon J. Gerraty "sh", /* .name */ 362b0c40a00SSimon J. Gerraty false, /* .hasEchoCtl */ 363956e45f6SSimon J. Gerraty "", /* .echoOff */ 364956e45f6SSimon J. Gerraty "", /* .echoOn */ 365956e45f6SSimon J. Gerraty "", /* .noPrint */ 366956e45f6SSimon J. Gerraty 0, /* .noPrintLen */ 367b0c40a00SSimon J. Gerraty false, /* .hasErrCtl */ 36806b9b3e0SSimon J. Gerraty "", /* .errOn */ 36906b9b3e0SSimon J. Gerraty "", /* .errOff */ 37006b9b3e0SSimon J. Gerraty "echo \"%s\"\n", /* .echoTmpl */ 37106b9b3e0SSimon J. Gerraty "%s\n", /* .runIgnTmpl */ 37206b9b3e0SSimon J. Gerraty "{ %s \n} || exit $?\n", /* .runChkTmpl */ 373956e45f6SSimon J. Gerraty "'\n'", /* .newline */ 374956e45f6SSimon J. Gerraty '#', /* .commentChar*/ 3753955d011SMarcel Moolenaar #if defined(MAKE_NATIVE) && defined(__NetBSD__) 37606b9b3e0SSimon J. Gerraty /* XXX: -q is not really echoFlag, it's more like noEchoInSysFlag. */ 37706b9b3e0SSimon J. Gerraty "q", /* .echoFlag */ 3783955d011SMarcel Moolenaar #else 37906b9b3e0SSimon J. Gerraty "", /* .echoFlag */ 3803955d011SMarcel Moolenaar #endif 38106b9b3e0SSimon J. Gerraty "", /* .errFlag */ 3823955d011SMarcel Moolenaar }, 3833955d011SMarcel Moolenaar /* 3843955d011SMarcel Moolenaar * KSH description. 3853955d011SMarcel Moolenaar */ 3863955d011SMarcel Moolenaar { 387956e45f6SSimon J. Gerraty "ksh", /* .name */ 388b0c40a00SSimon J. Gerraty true, /* .hasEchoCtl */ 389956e45f6SSimon J. Gerraty "set +v", /* .echoOff */ 390956e45f6SSimon J. Gerraty "set -v", /* .echoOn */ 391956e45f6SSimon J. Gerraty "set +v", /* .noPrint */ 392956e45f6SSimon J. Gerraty 6, /* .noPrintLen */ 393b0c40a00SSimon J. Gerraty false, /* .hasErrCtl */ 39406b9b3e0SSimon J. Gerraty "", /* .errOn */ 39506b9b3e0SSimon J. Gerraty "", /* .errOff */ 39606b9b3e0SSimon J. Gerraty "echo \"%s\"\n", /* .echoTmpl */ 39706b9b3e0SSimon J. Gerraty "%s\n", /* .runIgnTmpl */ 39806b9b3e0SSimon J. Gerraty "{ %s \n} || exit $?\n", /* .runChkTmpl */ 399956e45f6SSimon J. Gerraty "'\n'", /* .newline */ 400956e45f6SSimon J. Gerraty '#', /* .commentChar */ 40106b9b3e0SSimon J. Gerraty "v", /* .echoFlag */ 40206b9b3e0SSimon J. Gerraty "", /* .errFlag */ 4033955d011SMarcel Moolenaar }, 4043955d011SMarcel Moolenaar /* 4053955d011SMarcel Moolenaar * CSH description. The csh can do echo control by playing 4063955d011SMarcel Moolenaar * with the setting of the 'echo' shell variable. Sadly, 4073955d011SMarcel Moolenaar * however, it is unable to do error control nicely. 4083955d011SMarcel Moolenaar */ 4093955d011SMarcel Moolenaar { 410956e45f6SSimon J. Gerraty "csh", /* .name */ 411b0c40a00SSimon J. Gerraty true, /* .hasEchoCtl */ 412956e45f6SSimon J. Gerraty "unset verbose", /* .echoOff */ 413956e45f6SSimon J. Gerraty "set verbose", /* .echoOn */ 414956e45f6SSimon J. Gerraty "unset verbose", /* .noPrint */ 415956e45f6SSimon J. Gerraty 13, /* .noPrintLen */ 416b0c40a00SSimon J. Gerraty false, /* .hasErrCtl */ 41706b9b3e0SSimon J. Gerraty "", /* .errOn */ 41806b9b3e0SSimon J. Gerraty "", /* .errOff */ 41906b9b3e0SSimon J. Gerraty "echo \"%s\"\n", /* .echoTmpl */ 42006b9b3e0SSimon J. Gerraty "csh -c \"%s || exit 0\"\n", /* .runIgnTmpl */ 42106b9b3e0SSimon J. Gerraty "", /* .runChkTmpl */ 422956e45f6SSimon J. Gerraty "'\\\n'", /* .newline */ 423956e45f6SSimon J. Gerraty '#', /* .commentChar */ 42406b9b3e0SSimon J. Gerraty "v", /* .echoFlag */ 42506b9b3e0SSimon J. Gerraty "e", /* .errFlag */ 4263955d011SMarcel Moolenaar } 4273955d011SMarcel Moolenaar }; 428956e45f6SSimon J. Gerraty 42906b9b3e0SSimon J. Gerraty /* 43006b9b3e0SSimon J. Gerraty * This is the shell to which we pass all commands in the Makefile. 43106b9b3e0SSimon J. Gerraty * It is set by the Job_ParseShell function. 43206b9b3e0SSimon J. Gerraty */ 43306b9b3e0SSimon J. Gerraty static Shell *shell = &shells[DEFSHELL_INDEX]; 434956e45f6SSimon J. Gerraty const char *shellPath = NULL; /* full pathname of executable image */ 435956e45f6SSimon J. Gerraty const char *shellName = NULL; /* last component of shellPath */ 43651ee2c1cSSimon J. Gerraty char *shellErrFlag = NULL; 437dba7b0efSSimon J. Gerraty static char *shell_freeIt = NULL; /* Allocated memory for custom .SHELL */ 4383955d011SMarcel Moolenaar 4393955d011SMarcel Moolenaar 440956e45f6SSimon J. Gerraty static Job *job_table; /* The structures that describe them */ 441956e45f6SSimon J. Gerraty static Job *job_table_end; /* job_table + maxJobs */ 442956e45f6SSimon J. Gerraty static unsigned int wantToken; /* we want a token */ 443b0c40a00SSimon J. Gerraty static bool lurking_children = false; 444b0c40a00SSimon J. Gerraty static bool make_suspended = false; /* Whether we've seen a SIGTSTP (etc) */ 4453955d011SMarcel Moolenaar 4463955d011SMarcel Moolenaar /* 4473955d011SMarcel Moolenaar * Set of descriptors of pipes connected to 4483955d011SMarcel Moolenaar * the output channels of children 4493955d011SMarcel Moolenaar */ 4503955d011SMarcel Moolenaar static struct pollfd *fds = NULL; 451dba7b0efSSimon J. Gerraty static Job **jobByFdIndex = NULL; 452dba7b0efSSimon J. Gerraty static nfds_t fdsLen = 0; 4533955d011SMarcel Moolenaar static void watchfd(Job *); 4543955d011SMarcel Moolenaar static void clearfd(Job *); 455b0c40a00SSimon J. Gerraty static bool readyfd(Job *); 4563955d011SMarcel Moolenaar 45706b9b3e0SSimon J. Gerraty static char *targPrefix = NULL; /* To identify a job change in the output. */ 4583955d011SMarcel Moolenaar static Job tokenWaitJob; /* token wait pseudo-job */ 4593955d011SMarcel Moolenaar 4603955d011SMarcel Moolenaar static Job childExitJob; /* child exit pseudo-job */ 4613955d011SMarcel Moolenaar #define CHILD_EXIT "." 4623955d011SMarcel Moolenaar #define DO_JOB_RESUME "R" 4633955d011SMarcel Moolenaar 46406b9b3e0SSimon J. Gerraty enum { 46506b9b3e0SSimon J. Gerraty npseudojobs = 2 /* number of pseudo-jobs */ 46606b9b3e0SSimon J. Gerraty }; 4673955d011SMarcel Moolenaar 4683955d011SMarcel Moolenaar static sigset_t caught_signals; /* Set of signals we handle */ 469dba7b0efSSimon J. Gerraty static volatile sig_atomic_t caught_sigchld; 4703955d011SMarcel Moolenaar 471b0c40a00SSimon J. Gerraty static void CollectOutput(Job *, bool); 472b0c40a00SSimon J. Gerraty static void JobInterrupt(bool, int) MAKE_ATTR_DEAD; 4733955d011SMarcel Moolenaar static void JobRestartJobs(void); 4743955d011SMarcel Moolenaar static void JobSigReset(void); 4753955d011SMarcel Moolenaar 47606b9b3e0SSimon J. Gerraty static void 47706b9b3e0SSimon J. Gerraty SwitchOutputTo(GNode *gn) 47806b9b3e0SSimon J. Gerraty { 47906b9b3e0SSimon J. Gerraty /* The node for which output was most recently produced. */ 48006b9b3e0SSimon J. Gerraty static GNode *lastNode = NULL; 48106b9b3e0SSimon J. Gerraty 48206b9b3e0SSimon J. Gerraty if (gn == lastNode) 48306b9b3e0SSimon J. Gerraty return; 48406b9b3e0SSimon J. Gerraty lastNode = gn; 48506b9b3e0SSimon J. Gerraty 48606b9b3e0SSimon J. Gerraty if (opts.maxJobs != 1 && targPrefix != NULL && targPrefix[0] != '\0') 48706b9b3e0SSimon J. Gerraty (void)fprintf(stdout, "%s %s ---\n", targPrefix, gn->name); 48806b9b3e0SSimon J. Gerraty } 48906b9b3e0SSimon J. Gerraty 49049caa483SSimon J. Gerraty static unsigned 49149caa483SSimon J. Gerraty nfds_per_job(void) 49249caa483SSimon J. Gerraty { 49349caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 49449caa483SSimon J. Gerraty if (useMeta) 49549caa483SSimon J. Gerraty return 2; 49649caa483SSimon J. Gerraty #endif 49749caa483SSimon J. Gerraty return 1; 49849caa483SSimon J. Gerraty } 49949caa483SSimon J. Gerraty 50006b9b3e0SSimon J. Gerraty void 50106b9b3e0SSimon J. Gerraty Job_FlagsToString(const Job *job, char *buf, size_t bufsize) 50206b9b3e0SSimon J. Gerraty { 50306b9b3e0SSimon J. Gerraty snprintf(buf, bufsize, "%c%c%c", 50406b9b3e0SSimon J. Gerraty job->ignerr ? 'i' : '-', 50506b9b3e0SSimon J. Gerraty !job->echo ? 's' : '-', 50606b9b3e0SSimon J. Gerraty job->special ? 'S' : '-'); 50706b9b3e0SSimon J. Gerraty } 50806b9b3e0SSimon J. Gerraty 5093955d011SMarcel Moolenaar static void 510b0c40a00SSimon J. Gerraty DumpJobs(const char *where) 5113955d011SMarcel Moolenaar { 5123955d011SMarcel Moolenaar Job *job; 51306b9b3e0SSimon J. Gerraty char flags[4]; 5143955d011SMarcel Moolenaar 515956e45f6SSimon J. Gerraty debug_printf("job table @ %s\n", where); 5163955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 51706b9b3e0SSimon J. Gerraty Job_FlagsToString(job, flags, sizeof flags); 51806b9b3e0SSimon J. Gerraty debug_printf("job %d, status %d, flags %s, pid %d\n", 51906b9b3e0SSimon J. Gerraty (int)(job - job_table), job->status, flags, job->pid); 5203955d011SMarcel Moolenaar } 5213955d011SMarcel Moolenaar } 5223955d011SMarcel Moolenaar 5233955d011SMarcel Moolenaar /* 52445447996SSimon J. Gerraty * Delete the target of a failed, interrupted, or otherwise 52545447996SSimon J. Gerraty * unsuccessful job unless inhibited by .PRECIOUS. 52645447996SSimon J. Gerraty */ 52745447996SSimon J. Gerraty static void 52845447996SSimon J. Gerraty JobDeleteTarget(GNode *gn) 52945447996SSimon J. Gerraty { 530956e45f6SSimon J. Gerraty const char *file; 531956e45f6SSimon J. Gerraty 532956e45f6SSimon J. Gerraty if (gn->type & OP_JOIN) 533956e45f6SSimon J. Gerraty return; 534956e45f6SSimon J. Gerraty if (gn->type & OP_PHONY) 535956e45f6SSimon J. Gerraty return; 536956e45f6SSimon J. Gerraty if (Targ_Precious(gn)) 537956e45f6SSimon J. Gerraty return; 538956e45f6SSimon J. Gerraty if (opts.noExecute) 539956e45f6SSimon J. Gerraty return; 540956e45f6SSimon J. Gerraty 541956e45f6SSimon J. Gerraty file = GNode_Path(gn); 542956e45f6SSimon J. Gerraty if (eunlink(file) != -1) 54345447996SSimon J. Gerraty Error("*** %s removed", file); 54445447996SSimon J. Gerraty } 54545447996SSimon J. Gerraty 54645447996SSimon J. Gerraty /* 5473955d011SMarcel Moolenaar * JobSigLock/JobSigUnlock 5483955d011SMarcel Moolenaar * 5493955d011SMarcel Moolenaar * Signal lock routines to get exclusive access. Currently used to 5503955d011SMarcel Moolenaar * protect `jobs' and `stoppedJobs' list manipulations. 5513955d011SMarcel Moolenaar */ 55206b9b3e0SSimon J. Gerraty static void 55306b9b3e0SSimon J. Gerraty JobSigLock(sigset_t *omaskp) 5543955d011SMarcel Moolenaar { 5553955d011SMarcel Moolenaar if (sigprocmask(SIG_BLOCK, &caught_signals, omaskp) != 0) { 5563955d011SMarcel Moolenaar Punt("JobSigLock: sigprocmask: %s", strerror(errno)); 5573955d011SMarcel Moolenaar sigemptyset(omaskp); 5583955d011SMarcel Moolenaar } 5593955d011SMarcel Moolenaar } 5603955d011SMarcel Moolenaar 56106b9b3e0SSimon J. Gerraty static void 56206b9b3e0SSimon J. Gerraty JobSigUnlock(sigset_t *omaskp) 5633955d011SMarcel Moolenaar { 5643955d011SMarcel Moolenaar (void)sigprocmask(SIG_SETMASK, omaskp, NULL); 5653955d011SMarcel Moolenaar } 5663955d011SMarcel Moolenaar 5673955d011SMarcel Moolenaar static void 5683955d011SMarcel Moolenaar JobCreatePipe(Job *job, int minfd) 5693955d011SMarcel Moolenaar { 570e1cee40dSSimon J. Gerraty int i, fd, flags; 571956e45f6SSimon J. Gerraty int pipe_fds[2]; 5723955d011SMarcel Moolenaar 573956e45f6SSimon J. Gerraty if (pipe(pipe_fds) == -1) 5743955d011SMarcel Moolenaar Punt("Cannot create pipe: %s", strerror(errno)); 5753955d011SMarcel Moolenaar 57674d2e02bSSimon J. Gerraty for (i = 0; i < 2; i++) { 57774d2e02bSSimon J. Gerraty /* Avoid using low numbered fds */ 578956e45f6SSimon J. Gerraty fd = fcntl(pipe_fds[i], F_DUPFD, minfd); 57974d2e02bSSimon J. Gerraty if (fd != -1) { 580956e45f6SSimon J. Gerraty close(pipe_fds[i]); 581956e45f6SSimon J. Gerraty pipe_fds[i] = fd; 58274d2e02bSSimon J. Gerraty } 58374d2e02bSSimon J. Gerraty } 58474d2e02bSSimon J. Gerraty 585956e45f6SSimon J. Gerraty job->inPipe = pipe_fds[0]; 586956e45f6SSimon J. Gerraty job->outPipe = pipe_fds[1]; 587956e45f6SSimon J. Gerraty 5883955d011SMarcel Moolenaar /* Set close-on-exec flag for both */ 589956e45f6SSimon J. Gerraty if (fcntl(job->inPipe, F_SETFD, FD_CLOEXEC) == -1) 590e1cee40dSSimon J. Gerraty Punt("Cannot set close-on-exec: %s", strerror(errno)); 591956e45f6SSimon J. Gerraty if (fcntl(job->outPipe, F_SETFD, FD_CLOEXEC) == -1) 592e1cee40dSSimon J. Gerraty Punt("Cannot set close-on-exec: %s", strerror(errno)); 5933955d011SMarcel Moolenaar 5943955d011SMarcel Moolenaar /* 5953955d011SMarcel Moolenaar * We mark the input side of the pipe non-blocking; we poll(2) the 5963955d011SMarcel Moolenaar * pipe when we're waiting for a job token, but we might lose the 5973955d011SMarcel Moolenaar * race for the token when a new one becomes available, so the read 5983955d011SMarcel Moolenaar * from the pipe should not block. 5993955d011SMarcel Moolenaar */ 600956e45f6SSimon J. Gerraty flags = fcntl(job->inPipe, F_GETFL, 0); 601e1cee40dSSimon J. Gerraty if (flags == -1) 602e1cee40dSSimon J. Gerraty Punt("Cannot get flags: %s", strerror(errno)); 603e1cee40dSSimon J. Gerraty flags |= O_NONBLOCK; 604956e45f6SSimon J. Gerraty if (fcntl(job->inPipe, F_SETFL, flags) == -1) 605e1cee40dSSimon J. Gerraty Punt("Cannot set flags: %s", strerror(errno)); 6063955d011SMarcel Moolenaar } 6073955d011SMarcel Moolenaar 608956e45f6SSimon J. Gerraty /* Pass the signal to each running job. */ 6093955d011SMarcel Moolenaar static void 6103955d011SMarcel Moolenaar JobCondPassSig(int signo) 6113955d011SMarcel Moolenaar { 6123955d011SMarcel Moolenaar Job *job; 6133955d011SMarcel Moolenaar 614956e45f6SSimon J. Gerraty DEBUG1(JOB, "JobCondPassSig(%d) called.\n", signo); 6153955d011SMarcel Moolenaar 6163955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 617e2eeea75SSimon J. Gerraty if (job->status != JOB_ST_RUNNING) 6183955d011SMarcel Moolenaar continue; 619956e45f6SSimon J. Gerraty DEBUG2(JOB, "JobCondPassSig passing signal %d to child %d.\n", 6203955d011SMarcel Moolenaar signo, job->pid); 6213955d011SMarcel Moolenaar KILLPG(job->pid, signo); 6223955d011SMarcel Moolenaar } 6233955d011SMarcel Moolenaar } 6243955d011SMarcel Moolenaar 62506b9b3e0SSimon J. Gerraty /* 62606b9b3e0SSimon J. Gerraty * SIGCHLD handler. 6273955d011SMarcel Moolenaar * 62806b9b3e0SSimon J. Gerraty * Sends a token on the child exit pipe to wake us up from select()/poll(). 62906b9b3e0SSimon J. Gerraty */ 63006b9b3e0SSimon J. Gerraty /*ARGSUSED*/ 6313955d011SMarcel Moolenaar static void 6323955d011SMarcel Moolenaar JobChildSig(int signo MAKE_ATTR_UNUSED) 6333955d011SMarcel Moolenaar { 634dba7b0efSSimon J. Gerraty caught_sigchld = 1; 63506b9b3e0SSimon J. Gerraty while (write(childExitJob.outPipe, CHILD_EXIT, 1) == -1 && 63606b9b3e0SSimon J. Gerraty errno == EAGAIN) 6373cbdda60SSimon J. Gerraty continue; 6383955d011SMarcel Moolenaar } 6393955d011SMarcel Moolenaar 6403955d011SMarcel Moolenaar 641956e45f6SSimon J. Gerraty /* Resume all stopped jobs. */ 64206b9b3e0SSimon J. Gerraty /*ARGSUSED*/ 6433955d011SMarcel Moolenaar static void 6443955d011SMarcel Moolenaar JobContinueSig(int signo MAKE_ATTR_UNUSED) 6453955d011SMarcel Moolenaar { 6463955d011SMarcel Moolenaar /* 647956e45f6SSimon J. Gerraty * Defer sending SIGCONT to our stopped children until we return 6483955d011SMarcel Moolenaar * from the signal handler. 6493955d011SMarcel Moolenaar */ 6503cbdda60SSimon J. Gerraty while (write(childExitJob.outPipe, DO_JOB_RESUME, 1) == -1 && 6513cbdda60SSimon J. Gerraty errno == EAGAIN) 6523cbdda60SSimon J. Gerraty continue; 6533955d011SMarcel Moolenaar } 6543955d011SMarcel Moolenaar 65506b9b3e0SSimon J. Gerraty /* 65606b9b3e0SSimon J. Gerraty * Pass a signal on to all jobs, then resend to ourselves. 65706b9b3e0SSimon J. Gerraty * We die by the same signal. 65806b9b3e0SSimon J. Gerraty */ 6593955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void 6603955d011SMarcel Moolenaar JobPassSig_int(int signo) 6613955d011SMarcel Moolenaar { 6623955d011SMarcel Moolenaar /* Run .INTERRUPT target then exit */ 663b0c40a00SSimon J. Gerraty JobInterrupt(true, signo); 6643955d011SMarcel Moolenaar } 6653955d011SMarcel Moolenaar 66606b9b3e0SSimon J. Gerraty /* 66706b9b3e0SSimon J. Gerraty * Pass a signal on to all jobs, then resend to ourselves. 66806b9b3e0SSimon J. Gerraty * We die by the same signal. 66906b9b3e0SSimon J. Gerraty */ 6703955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void 6713955d011SMarcel Moolenaar JobPassSig_term(int signo) 6723955d011SMarcel Moolenaar { 6733955d011SMarcel Moolenaar /* Dont run .INTERRUPT target then exit */ 674b0c40a00SSimon J. Gerraty JobInterrupt(false, signo); 6753955d011SMarcel Moolenaar } 6763955d011SMarcel Moolenaar 6773955d011SMarcel Moolenaar static void 6783955d011SMarcel Moolenaar JobPassSig_suspend(int signo) 6793955d011SMarcel Moolenaar { 6803955d011SMarcel Moolenaar sigset_t nmask, omask; 6813955d011SMarcel Moolenaar struct sigaction act; 6823955d011SMarcel Moolenaar 6833955d011SMarcel Moolenaar /* Suppress job started/continued messages */ 684b0c40a00SSimon J. Gerraty make_suspended = true; 6853955d011SMarcel Moolenaar 6863955d011SMarcel Moolenaar /* Pass the signal onto every job */ 6873955d011SMarcel Moolenaar JobCondPassSig(signo); 6883955d011SMarcel Moolenaar 6893955d011SMarcel Moolenaar /* 69006b9b3e0SSimon J. Gerraty * Send ourselves the signal now we've given the message to everyone 69106b9b3e0SSimon J. Gerraty * else. Note we block everything else possible while we're getting 69206b9b3e0SSimon J. Gerraty * the signal. This ensures that all our jobs get continued when we 69306b9b3e0SSimon J. Gerraty * wake up before we take any other signal. 6943955d011SMarcel Moolenaar */ 6953955d011SMarcel Moolenaar sigfillset(&nmask); 6963955d011SMarcel Moolenaar sigdelset(&nmask, signo); 6973955d011SMarcel Moolenaar (void)sigprocmask(SIG_SETMASK, &nmask, &omask); 6983955d011SMarcel Moolenaar 6993955d011SMarcel Moolenaar act.sa_handler = SIG_DFL; 7003955d011SMarcel Moolenaar sigemptyset(&act.sa_mask); 7013955d011SMarcel Moolenaar act.sa_flags = 0; 7023955d011SMarcel Moolenaar (void)sigaction(signo, &act, NULL); 7033955d011SMarcel Moolenaar 70406b9b3e0SSimon J. Gerraty DEBUG1(JOB, "JobPassSig passing signal %d to self.\n", signo); 7053955d011SMarcel Moolenaar 7063955d011SMarcel Moolenaar (void)kill(getpid(), signo); 7073955d011SMarcel Moolenaar 7083955d011SMarcel Moolenaar /* 7093955d011SMarcel Moolenaar * We've been continued. 7103955d011SMarcel Moolenaar * 7113955d011SMarcel Moolenaar * A whole host of signals continue to happen! 7123955d011SMarcel Moolenaar * SIGCHLD for any processes that actually suspended themselves. 7133955d011SMarcel Moolenaar * SIGCHLD for any processes that exited while we were alseep. 7143955d011SMarcel Moolenaar * The SIGCONT that actually caused us to wakeup. 7153955d011SMarcel Moolenaar * 7163955d011SMarcel Moolenaar * Since we defer passing the SIGCONT on to our children until 7173955d011SMarcel Moolenaar * the main processing loop, we can be sure that all the SIGCHLD 7183955d011SMarcel Moolenaar * events will have happened by then - and that the waitpid() will 7193955d011SMarcel Moolenaar * collect the child 'suspended' events. 7203955d011SMarcel Moolenaar * For correct sequencing we just need to ensure we process the 721956e45f6SSimon J. Gerraty * waitpid() before passing on the SIGCONT. 7223955d011SMarcel Moolenaar * 7233955d011SMarcel Moolenaar * In any case nothing else is needed here. 7243955d011SMarcel Moolenaar */ 7253955d011SMarcel Moolenaar 7263955d011SMarcel Moolenaar /* Restore handler and signal mask */ 7273955d011SMarcel Moolenaar act.sa_handler = JobPassSig_suspend; 7283955d011SMarcel Moolenaar (void)sigaction(signo, &act, NULL); 7293955d011SMarcel Moolenaar (void)sigprocmask(SIG_SETMASK, &omask, NULL); 7303955d011SMarcel Moolenaar } 7313955d011SMarcel Moolenaar 7323955d011SMarcel Moolenaar static Job * 733b0c40a00SSimon J. Gerraty JobFindPid(int pid, JobStatus status, bool isJobs) 7343955d011SMarcel Moolenaar { 7353955d011SMarcel Moolenaar Job *job; 7363955d011SMarcel Moolenaar 7373955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 738e2eeea75SSimon J. Gerraty if (job->status == status && job->pid == pid) 7393955d011SMarcel Moolenaar return job; 7403955d011SMarcel Moolenaar } 7413955d011SMarcel Moolenaar if (DEBUG(JOB) && isJobs) 742b0c40a00SSimon J. Gerraty DumpJobs("no pid"); 7433955d011SMarcel Moolenaar return NULL; 7443955d011SMarcel Moolenaar } 7453955d011SMarcel Moolenaar 746956e45f6SSimon J. Gerraty /* Parse leading '@', '-' and '+', which control the exact execution mode. */ 747956e45f6SSimon J. Gerraty static void 74806b9b3e0SSimon J. Gerraty ParseCommandFlags(char **pp, CommandFlags *out_cmdFlags) 749956e45f6SSimon J. Gerraty { 750956e45f6SSimon J. Gerraty char *p = *pp; 751b0c40a00SSimon J. Gerraty out_cmdFlags->echo = true; 752b0c40a00SSimon J. Gerraty out_cmdFlags->ignerr = false; 753b0c40a00SSimon J. Gerraty out_cmdFlags->always = false; 754956e45f6SSimon J. Gerraty 755956e45f6SSimon J. Gerraty for (;;) { 756956e45f6SSimon J. Gerraty if (*p == '@') 75706b9b3e0SSimon J. Gerraty out_cmdFlags->echo = DEBUG(LOUD); 758956e45f6SSimon J. Gerraty else if (*p == '-') 759b0c40a00SSimon J. Gerraty out_cmdFlags->ignerr = true; 760956e45f6SSimon J. Gerraty else if (*p == '+') 761b0c40a00SSimon J. Gerraty out_cmdFlags->always = true; 762956e45f6SSimon J. Gerraty else 763956e45f6SSimon J. Gerraty break; 764956e45f6SSimon J. Gerraty p++; 765956e45f6SSimon J. Gerraty } 766956e45f6SSimon J. Gerraty 767956e45f6SSimon J. Gerraty pp_skip_whitespace(&p); 768956e45f6SSimon J. Gerraty 769956e45f6SSimon J. Gerraty *pp = p; 770956e45f6SSimon J. Gerraty } 771956e45f6SSimon J. Gerraty 772956e45f6SSimon J. Gerraty /* Escape a string for a double-quoted string literal in sh, csh and ksh. */ 773956e45f6SSimon J. Gerraty static char * 774956e45f6SSimon J. Gerraty EscapeShellDblQuot(const char *cmd) 775956e45f6SSimon J. Gerraty { 776956e45f6SSimon J. Gerraty size_t i, j; 777956e45f6SSimon J. Gerraty 778956e45f6SSimon J. Gerraty /* Worst that could happen is every char needs escaping. */ 779956e45f6SSimon J. Gerraty char *esc = bmake_malloc(strlen(cmd) * 2 + 1); 780956e45f6SSimon J. Gerraty for (i = 0, j = 0; cmd[i] != '\0'; i++, j++) { 78106b9b3e0SSimon J. Gerraty if (cmd[i] == '$' || cmd[i] == '`' || cmd[i] == '\\' || 78206b9b3e0SSimon J. Gerraty cmd[i] == '"') 783956e45f6SSimon J. Gerraty esc[j++] = '\\'; 784956e45f6SSimon J. Gerraty esc[j] = cmd[i]; 785956e45f6SSimon J. Gerraty } 786956e45f6SSimon J. Gerraty esc[j] = '\0'; 787956e45f6SSimon J. Gerraty 788956e45f6SSimon J. Gerraty return esc; 789956e45f6SSimon J. Gerraty } 790956e45f6SSimon J. Gerraty 791e2eeea75SSimon J. Gerraty static void 792b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(ShellWriter *wr, const char *fmt, const char *arg) 793e2eeea75SSimon J. Gerraty { 79406b9b3e0SSimon J. Gerraty DEBUG1(JOB, fmt, arg); 795e2eeea75SSimon J. Gerraty 79606b9b3e0SSimon J. Gerraty (void)fprintf(wr->f, fmt, arg); 79706b9b3e0SSimon J. Gerraty /* XXX: Is flushing needed in any case, or only if f == stdout? */ 79806b9b3e0SSimon J. Gerraty (void)fflush(wr->f); 799e2eeea75SSimon J. Gerraty } 800e2eeea75SSimon J. Gerraty 801e2eeea75SSimon J. Gerraty static void 802b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(ShellWriter *wr, const char *line) 803e2eeea75SSimon J. Gerraty { 804b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(wr, "%s\n", line); 805e2eeea75SSimon J. Gerraty } 806e2eeea75SSimon J. Gerraty 80706b9b3e0SSimon J. Gerraty static void 80806b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(ShellWriter *wr) 80906b9b3e0SSimon J. Gerraty { 81006b9b3e0SSimon J. Gerraty if (shell->hasEchoCtl) 811b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(wr, shell->echoOff); 81206b9b3e0SSimon J. Gerraty } 81306b9b3e0SSimon J. Gerraty 81406b9b3e0SSimon J. Gerraty static void 81506b9b3e0SSimon J. Gerraty ShellWriter_EchoCmd(ShellWriter *wr, const char *escCmd) 81606b9b3e0SSimon J. Gerraty { 817b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(wr, shell->echoTmpl, escCmd); 81806b9b3e0SSimon J. Gerraty } 81906b9b3e0SSimon J. Gerraty 82006b9b3e0SSimon J. Gerraty static void 82106b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(ShellWriter *wr) 82206b9b3e0SSimon J. Gerraty { 82306b9b3e0SSimon J. Gerraty if (shell->hasEchoCtl) 824b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(wr, shell->echoOn); 82506b9b3e0SSimon J. Gerraty } 82606b9b3e0SSimon J. Gerraty 82706b9b3e0SSimon J. Gerraty static void 82806b9b3e0SSimon J. Gerraty ShellWriter_TraceOn(ShellWriter *wr) 82906b9b3e0SSimon J. Gerraty { 83006b9b3e0SSimon J. Gerraty if (!wr->xtraced) { 831b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(wr, "set -x"); 832b0c40a00SSimon J. Gerraty wr->xtraced = true; 83306b9b3e0SSimon J. Gerraty } 83406b9b3e0SSimon J. Gerraty } 83506b9b3e0SSimon J. Gerraty 83606b9b3e0SSimon J. Gerraty static void 837b0c40a00SSimon J. Gerraty ShellWriter_ErrOff(ShellWriter *wr, bool echo) 83806b9b3e0SSimon J. Gerraty { 83906b9b3e0SSimon J. Gerraty if (echo) 84006b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(wr); 841b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(wr, shell->errOff); 84206b9b3e0SSimon J. Gerraty if (echo) 84306b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(wr); 84406b9b3e0SSimon J. Gerraty } 84506b9b3e0SSimon J. Gerraty 84606b9b3e0SSimon J. Gerraty static void 847b0c40a00SSimon J. Gerraty ShellWriter_ErrOn(ShellWriter *wr, bool echo) 84806b9b3e0SSimon J. Gerraty { 84906b9b3e0SSimon J. Gerraty if (echo) 85006b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(wr); 851b0c40a00SSimon J. Gerraty ShellWriter_WriteLine(wr, shell->errOn); 85206b9b3e0SSimon J. Gerraty if (echo) 85306b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(wr); 85406b9b3e0SSimon J. Gerraty } 85506b9b3e0SSimon J. Gerraty 85606b9b3e0SSimon J. Gerraty /* 85706b9b3e0SSimon J. Gerraty * The shell has no built-in error control, so emulate error control by 85806b9b3e0SSimon J. Gerraty * enclosing each shell command in a template like "{ %s \n } || exit $?" 85906b9b3e0SSimon J. Gerraty * (configurable per shell). 8603955d011SMarcel Moolenaar */ 861956e45f6SSimon J. Gerraty static void 862b0c40a00SSimon J. Gerraty JobWriteSpecialsEchoCtl(Job *job, ShellWriter *wr, CommandFlags *inout_cmdFlags, 86306b9b3e0SSimon J. Gerraty const char *escCmd, const char **inout_cmdTemplate) 8643955d011SMarcel Moolenaar { 86506b9b3e0SSimon J. Gerraty /* XXX: Why is the job modified at this point? */ 866b0c40a00SSimon J. Gerraty job->ignerr = true; 8673955d011SMarcel Moolenaar 86806b9b3e0SSimon J. Gerraty if (job->echo && inout_cmdFlags->echo) { 86906b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(wr); 87006b9b3e0SSimon J. Gerraty ShellWriter_EchoCmd(wr, escCmd); 8713955d011SMarcel Moolenaar 87206b9b3e0SSimon J. Gerraty /* 87306b9b3e0SSimon J. Gerraty * Leave echoing off so the user doesn't see the commands 87406b9b3e0SSimon J. Gerraty * for toggling the error checking. 87506b9b3e0SSimon J. Gerraty */ 876b0c40a00SSimon J. Gerraty inout_cmdFlags->echo = false; 87706b9b3e0SSimon J. Gerraty } else { 87806b9b3e0SSimon J. Gerraty if (inout_cmdFlags->echo) 87906b9b3e0SSimon J. Gerraty ShellWriter_EchoCmd(wr, escCmd); 88006b9b3e0SSimon J. Gerraty } 88106b9b3e0SSimon J. Gerraty *inout_cmdTemplate = shell->runIgnTmpl; 8823955d011SMarcel Moolenaar 88306b9b3e0SSimon J. Gerraty /* 88406b9b3e0SSimon J. Gerraty * The template runIgnTmpl already takes care of ignoring errors, 88506b9b3e0SSimon J. Gerraty * so pretend error checking is still on. 88606b9b3e0SSimon J. Gerraty * XXX: What effects does this have, and why is it necessary? 88706b9b3e0SSimon J. Gerraty */ 888b0c40a00SSimon J. Gerraty inout_cmdFlags->ignerr = false; 88906b9b3e0SSimon J. Gerraty } 89006b9b3e0SSimon J. Gerraty 89106b9b3e0SSimon J. Gerraty static void 892b0c40a00SSimon J. Gerraty JobWriteSpecials(Job *job, ShellWriter *wr, const char *escCmd, bool run, 89306b9b3e0SSimon J. Gerraty CommandFlags *inout_cmdFlags, const char **inout_cmdTemplate) 89406b9b3e0SSimon J. Gerraty { 89506b9b3e0SSimon J. Gerraty if (!run) { 89606b9b3e0SSimon J. Gerraty /* 89706b9b3e0SSimon J. Gerraty * If there is no command to run, there is no need to switch 89806b9b3e0SSimon J. Gerraty * error checking off and on again for nothing. 89906b9b3e0SSimon J. Gerraty */ 900b0c40a00SSimon J. Gerraty inout_cmdFlags->ignerr = false; 90106b9b3e0SSimon J. Gerraty } else if (shell->hasErrCtl) 90206b9b3e0SSimon J. Gerraty ShellWriter_ErrOff(wr, job->echo && inout_cmdFlags->echo); 90306b9b3e0SSimon J. Gerraty else if (shell->runIgnTmpl != NULL && shell->runIgnTmpl[0] != '\0') { 904b0c40a00SSimon J. Gerraty JobWriteSpecialsEchoCtl(job, wr, inout_cmdFlags, escCmd, 90506b9b3e0SSimon J. Gerraty inout_cmdTemplate); 90606b9b3e0SSimon J. Gerraty } else 907b0c40a00SSimon J. Gerraty inout_cmdFlags->ignerr = false; 90806b9b3e0SSimon J. Gerraty } 90906b9b3e0SSimon J. Gerraty 91006b9b3e0SSimon J. Gerraty /* 911b0c40a00SSimon J. Gerraty * Write a shell command to the job's commands file, to be run later. 91206b9b3e0SSimon J. Gerraty * 91306b9b3e0SSimon J. Gerraty * If the command starts with '@' and neither the -s nor the -n flag was 914b0c40a00SSimon J. Gerraty * given to make, stick a shell-specific echoOff command in the script. 91506b9b3e0SSimon J. Gerraty * 91606b9b3e0SSimon J. Gerraty * If the command starts with '-' and the shell has no error control (none 917b0c40a00SSimon J. Gerraty * of the predefined shells has that), ignore errors for the entire job. 91806b9b3e0SSimon J. Gerraty * 919b0c40a00SSimon J. Gerraty * XXX: Why ignore errors for the entire job? This is even documented in the 920b0c40a00SSimon J. Gerraty * manual page, but without any rationale since there is no known rationale. 921b0c40a00SSimon J. Gerraty * 922b0c40a00SSimon J. Gerraty * XXX: The manual page says the '-' "affects the entire job", but that's not 923b0c40a00SSimon J. Gerraty * accurate. The '-' does not affect the commands before the '-'. 924b0c40a00SSimon J. Gerraty * 925b0c40a00SSimon J. Gerraty * If the command is just "...", skip all further commands of this job. These 926b0c40a00SSimon J. Gerraty * commands are attached to the .END node instead and will be run by 927b0c40a00SSimon J. Gerraty * Job_Finish after all other targets have been made. 92806b9b3e0SSimon J. Gerraty */ 92906b9b3e0SSimon J. Gerraty static void 930b0c40a00SSimon J. Gerraty JobWriteCommand(Job *job, ShellWriter *wr, StringListNode *ln, const char *ucmd) 93106b9b3e0SSimon J. Gerraty { 932b0c40a00SSimon J. Gerraty bool run; 93306b9b3e0SSimon J. Gerraty 93406b9b3e0SSimon J. Gerraty CommandFlags cmdFlags; 935b0c40a00SSimon J. Gerraty /* Template for writing a command to the shell file */ 93606b9b3e0SSimon J. Gerraty const char *cmdTemplate; 93706b9b3e0SSimon J. Gerraty char *xcmd; /* The expanded command */ 93806b9b3e0SSimon J. Gerraty char *xcmdStart; 93906b9b3e0SSimon J. Gerraty char *escCmd; /* xcmd escaped to be used in double quotes */ 94006b9b3e0SSimon J. Gerraty 94106b9b3e0SSimon J. Gerraty run = GNode_ShouldExecute(job->node); 94206b9b3e0SSimon J. Gerraty 94306b9b3e0SSimon J. Gerraty Var_Subst(ucmd, job->node, VARE_WANTRES, &xcmd); 944956e45f6SSimon J. Gerraty /* TODO: handle errors */ 94506b9b3e0SSimon J. Gerraty xcmdStart = xcmd; 9463955d011SMarcel Moolenaar 9473955d011SMarcel Moolenaar cmdTemplate = "%s\n"; 9483955d011SMarcel Moolenaar 94906b9b3e0SSimon J. Gerraty ParseCommandFlags(&xcmd, &cmdFlags); 950956e45f6SSimon J. Gerraty 95106b9b3e0SSimon J. Gerraty /* The '+' command flag overrides the -n or -N options. */ 95206b9b3e0SSimon J. Gerraty if (cmdFlags.always && !run) { 9533955d011SMarcel Moolenaar /* 9543955d011SMarcel Moolenaar * We're not actually executing anything... 9553955d011SMarcel Moolenaar * but this one needs to be - use compat mode just for it. 9563955d011SMarcel Moolenaar */ 95706b9b3e0SSimon J. Gerraty Compat_RunCommand(ucmd, job->node, ln); 95806b9b3e0SSimon J. Gerraty free(xcmdStart); 959956e45f6SSimon J. Gerraty return; 9603955d011SMarcel Moolenaar } 9613955d011SMarcel Moolenaar 9623955d011SMarcel Moolenaar /* 96306b9b3e0SSimon J. Gerraty * If the shell doesn't have error control, the alternate echoing 96406b9b3e0SSimon J. Gerraty * will be done (to avoid showing additional error checking code) 96506b9b3e0SSimon J. Gerraty * and this needs some characters escaped. 9663955d011SMarcel Moolenaar */ 96706b9b3e0SSimon J. Gerraty escCmd = shell->hasErrCtl ? NULL : EscapeShellDblQuot(xcmd); 9683955d011SMarcel Moolenaar 96906b9b3e0SSimon J. Gerraty if (!cmdFlags.echo) { 97006b9b3e0SSimon J. Gerraty if (job->echo && run && shell->hasEchoCtl) { 97106b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(wr); 9723955d011SMarcel Moolenaar } else { 97306b9b3e0SSimon J. Gerraty if (shell->hasErrCtl) 974b0c40a00SSimon J. Gerraty cmdFlags.echo = true; 9753955d011SMarcel Moolenaar } 9763955d011SMarcel Moolenaar } 9773955d011SMarcel Moolenaar 97806b9b3e0SSimon J. Gerraty if (cmdFlags.ignerr) { 979b0c40a00SSimon J. Gerraty JobWriteSpecials(job, wr, escCmd, run, &cmdFlags, &cmdTemplate); 9803955d011SMarcel Moolenaar } else { 9813955d011SMarcel Moolenaar 9823955d011SMarcel Moolenaar /* 98306b9b3e0SSimon J. Gerraty * If errors are being checked and the shell doesn't have 98406b9b3e0SSimon J. Gerraty * error control but does supply an runChkTmpl template, then 98506b9b3e0SSimon J. Gerraty * set up commands to run through it. 9863955d011SMarcel Moolenaar */ 9873955d011SMarcel Moolenaar 98806b9b3e0SSimon J. Gerraty if (!shell->hasErrCtl && shell->runChkTmpl != NULL && 98906b9b3e0SSimon J. Gerraty shell->runChkTmpl[0] != '\0') { 99006b9b3e0SSimon J. Gerraty if (job->echo && cmdFlags.echo) { 99106b9b3e0SSimon J. Gerraty ShellWriter_EchoOff(wr); 99206b9b3e0SSimon J. Gerraty ShellWriter_EchoCmd(wr, escCmd); 993b0c40a00SSimon J. Gerraty cmdFlags.echo = false; 9943955d011SMarcel Moolenaar } 99506b9b3e0SSimon J. Gerraty /* 99606b9b3e0SSimon J. Gerraty * If it's a comment line or blank, avoid the possible 99706b9b3e0SSimon J. Gerraty * syntax error generated by "{\n} || exit $?". 99806b9b3e0SSimon J. Gerraty */ 99906b9b3e0SSimon J. Gerraty cmdTemplate = escCmd[0] == shell->commentChar || 100006b9b3e0SSimon J. Gerraty escCmd[0] == '\0' 100106b9b3e0SSimon J. Gerraty ? shell->runIgnTmpl 100206b9b3e0SSimon J. Gerraty : shell->runChkTmpl; 1003b0c40a00SSimon J. Gerraty cmdFlags.ignerr = false; 10043955d011SMarcel Moolenaar } 10053955d011SMarcel Moolenaar } 10063955d011SMarcel Moolenaar 100706b9b3e0SSimon J. Gerraty if (DEBUG(SHELL) && strcmp(shellName, "sh") == 0) 100806b9b3e0SSimon J. Gerraty ShellWriter_TraceOn(wr); 10093955d011SMarcel Moolenaar 1010b0c40a00SSimon J. Gerraty ShellWriter_WriteFmt(wr, cmdTemplate, xcmd); 101106b9b3e0SSimon J. Gerraty free(xcmdStart); 10123955d011SMarcel Moolenaar free(escCmd); 101306b9b3e0SSimon J. Gerraty 101406b9b3e0SSimon J. Gerraty if (cmdFlags.ignerr) 101506b9b3e0SSimon J. Gerraty ShellWriter_ErrOn(wr, cmdFlags.echo && job->echo); 101606b9b3e0SSimon J. Gerraty 101706b9b3e0SSimon J. Gerraty if (!cmdFlags.echo) 101806b9b3e0SSimon J. Gerraty ShellWriter_EchoOn(wr); 10193955d011SMarcel Moolenaar } 10203955d011SMarcel Moolenaar 102106b9b3e0SSimon J. Gerraty /* 1022b0c40a00SSimon J. Gerraty * Write all commands to the shell file that is later executed. 10233955d011SMarcel Moolenaar * 1024b0c40a00SSimon J. Gerraty * The special command "..." stops writing and saves the remaining commands 1025dba7b0efSSimon J. Gerraty * to be executed later, when the target '.END' is made. 102606b9b3e0SSimon J. Gerraty * 102706b9b3e0SSimon J. Gerraty * Return whether at least one command was written to the shell file. 102806b9b3e0SSimon J. Gerraty */ 1029b0c40a00SSimon J. Gerraty static bool 1030b0c40a00SSimon J. Gerraty JobWriteCommands(Job *job) 10313955d011SMarcel Moolenaar { 1032956e45f6SSimon J. Gerraty StringListNode *ln; 1033b0c40a00SSimon J. Gerraty bool seen = false; 1034b0c40a00SSimon J. Gerraty ShellWriter wr; 1035b0c40a00SSimon J. Gerraty 1036b0c40a00SSimon J. Gerraty wr.f = job->cmdFILE; 1037b0c40a00SSimon J. Gerraty wr.xtraced = false; 1038956e45f6SSimon J. Gerraty 103906b9b3e0SSimon J. Gerraty for (ln = job->node->commands.first; ln != NULL; ln = ln->next) { 1040956e45f6SSimon J. Gerraty const char *cmd = ln->datum; 1041956e45f6SSimon J. Gerraty 1042956e45f6SSimon J. Gerraty if (strcmp(cmd, "...") == 0) { 1043956e45f6SSimon J. Gerraty job->node->type |= OP_SAVE_CMDS; 1044956e45f6SSimon J. Gerraty job->tailCmds = ln->next; 1045956e45f6SSimon J. Gerraty break; 1046956e45f6SSimon J. Gerraty } 1047e2eeea75SSimon J. Gerraty 1048b0c40a00SSimon J. Gerraty JobWriteCommand(job, &wr, ln, ln->datum); 1049b0c40a00SSimon J. Gerraty seen = true; 1050956e45f6SSimon J. Gerraty } 105106b9b3e0SSimon J. Gerraty 105206b9b3e0SSimon J. Gerraty return seen; 1053956e45f6SSimon J. Gerraty } 1054956e45f6SSimon J. Gerraty 1055dba7b0efSSimon J. Gerraty /* 1056dba7b0efSSimon J. Gerraty * Save the delayed commands (those after '...'), to be executed later in 1057dba7b0efSSimon J. Gerraty * the '.END' node, when everything else is done. 1058dba7b0efSSimon J. Gerraty */ 1059956e45f6SSimon J. Gerraty static void 1060956e45f6SSimon J. Gerraty JobSaveCommands(Job *job) 1061956e45f6SSimon J. Gerraty { 106206b9b3e0SSimon J. Gerraty StringListNode *ln; 1063956e45f6SSimon J. Gerraty 106406b9b3e0SSimon J. Gerraty for (ln = job->tailCmds; ln != NULL; ln = ln->next) { 106506b9b3e0SSimon J. Gerraty const char *cmd = ln->datum; 1066956e45f6SSimon J. Gerraty char *expanded_cmd; 1067dba7b0efSSimon J. Gerraty /* 1068dba7b0efSSimon J. Gerraty * XXX: This Var_Subst is only intended to expand the dynamic 1069956e45f6SSimon J. Gerraty * variables such as .TARGET, .IMPSRC. It is not intended to 1070dba7b0efSSimon J. Gerraty * expand the other variables as well; see deptgt-end.mk. 1071dba7b0efSSimon J. Gerraty */ 1072956e45f6SSimon J. Gerraty (void)Var_Subst(cmd, job->node, VARE_WANTRES, &expanded_cmd); 1073956e45f6SSimon J. Gerraty /* TODO: handle errors */ 107406b9b3e0SSimon J. Gerraty Lst_Append(&Targ_GetEndNode()->commands, expanded_cmd); 1075956e45f6SSimon J. Gerraty } 10763955d011SMarcel Moolenaar } 10773955d011SMarcel Moolenaar 10783955d011SMarcel Moolenaar 1079956e45f6SSimon J. Gerraty /* Called to close both input and output pipes when a job is finished. */ 10803955d011SMarcel Moolenaar static void 1081e2eeea75SSimon J. Gerraty JobClosePipes(Job *job) 10823955d011SMarcel Moolenaar { 10833955d011SMarcel Moolenaar clearfd(job); 10843955d011SMarcel Moolenaar (void)close(job->outPipe); 10853955d011SMarcel Moolenaar job->outPipe = -1; 10863955d011SMarcel Moolenaar 1087b0c40a00SSimon J. Gerraty CollectOutput(job, true); 10883955d011SMarcel Moolenaar (void)close(job->inPipe); 10893955d011SMarcel Moolenaar job->inPipe = -1; 10903955d011SMarcel Moolenaar } 10913955d011SMarcel Moolenaar 109206b9b3e0SSimon J. Gerraty static void 1093b0c40a00SSimon J. Gerraty DebugFailedJob(const Job *job) 1094b0c40a00SSimon J. Gerraty { 1095b0c40a00SSimon J. Gerraty const StringListNode *ln; 1096b0c40a00SSimon J. Gerraty 1097b0c40a00SSimon J. Gerraty if (!DEBUG(ERROR)) 1098b0c40a00SSimon J. Gerraty return; 1099b0c40a00SSimon J. Gerraty 1100b0c40a00SSimon J. Gerraty debug_printf("\n*** Failed target: %s\n*** Failed commands:\n", 1101b0c40a00SSimon J. Gerraty job->node->name); 1102b0c40a00SSimon J. Gerraty for (ln = job->node->commands.first; ln != NULL; ln = ln->next) 1103b0c40a00SSimon J. Gerraty debug_printf("\t%s\n", (const char *)ln->datum); 1104b0c40a00SSimon J. Gerraty } 1105b0c40a00SSimon J. Gerraty 1106b0c40a00SSimon J. Gerraty static void 110706b9b3e0SSimon J. Gerraty JobFinishDoneExitedError(Job *job, WAIT_T *inout_status) 110806b9b3e0SSimon J. Gerraty { 110906b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 111006b9b3e0SSimon J. Gerraty #ifdef USE_META 111106b9b3e0SSimon J. Gerraty if (useMeta) { 111206b9b3e0SSimon J. Gerraty meta_job_error(job, job->node, 111306b9b3e0SSimon J. Gerraty job->ignerr, WEXITSTATUS(*inout_status)); 111406b9b3e0SSimon J. Gerraty } 111506b9b3e0SSimon J. Gerraty #endif 111606b9b3e0SSimon J. Gerraty if (!shouldDieQuietly(job->node, -1)) { 1117b0c40a00SSimon J. Gerraty DebugFailedJob(job); 111806b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Error code %d%s\n", 111906b9b3e0SSimon J. Gerraty job->node->name, WEXITSTATUS(*inout_status), 112006b9b3e0SSimon J. Gerraty job->ignerr ? " (ignored)" : ""); 112106b9b3e0SSimon J. Gerraty } 112206b9b3e0SSimon J. Gerraty 112306b9b3e0SSimon J. Gerraty if (job->ignerr) 112406b9b3e0SSimon J. Gerraty WAIT_STATUS(*inout_status) = 0; 112506b9b3e0SSimon J. Gerraty else { 112606b9b3e0SSimon J. Gerraty if (deleteOnError) 112706b9b3e0SSimon J. Gerraty JobDeleteTarget(job->node); 112806b9b3e0SSimon J. Gerraty PrintOnError(job->node, NULL); 112906b9b3e0SSimon J. Gerraty } 113006b9b3e0SSimon J. Gerraty } 113106b9b3e0SSimon J. Gerraty 113206b9b3e0SSimon J. Gerraty static void 113306b9b3e0SSimon J. Gerraty JobFinishDoneExited(Job *job, WAIT_T *inout_status) 113406b9b3e0SSimon J. Gerraty { 113506b9b3e0SSimon J. Gerraty DEBUG2(JOB, "Process %d [%s] exited.\n", job->pid, job->node->name); 113606b9b3e0SSimon J. Gerraty 113706b9b3e0SSimon J. Gerraty if (WEXITSTATUS(*inout_status) != 0) 113806b9b3e0SSimon J. Gerraty JobFinishDoneExitedError(job, inout_status); 113906b9b3e0SSimon J. Gerraty else if (DEBUG(JOB)) { 114006b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 114106b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Completed successfully\n", 114206b9b3e0SSimon J. Gerraty job->node->name); 114306b9b3e0SSimon J. Gerraty } 114406b9b3e0SSimon J. Gerraty } 114506b9b3e0SSimon J. Gerraty 114606b9b3e0SSimon J. Gerraty static void 114706b9b3e0SSimon J. Gerraty JobFinishDoneSignaled(Job *job, WAIT_T status) 114806b9b3e0SSimon J. Gerraty { 114906b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 1150b0c40a00SSimon J. Gerraty DebugFailedJob(job); 115106b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Signal %d\n", job->node->name, WTERMSIG(status)); 115206b9b3e0SSimon J. Gerraty if (deleteOnError) 115306b9b3e0SSimon J. Gerraty JobDeleteTarget(job->node); 115406b9b3e0SSimon J. Gerraty } 115506b9b3e0SSimon J. Gerraty 115606b9b3e0SSimon J. Gerraty static void 115706b9b3e0SSimon J. Gerraty JobFinishDone(Job *job, WAIT_T *inout_status) 115806b9b3e0SSimon J. Gerraty { 115906b9b3e0SSimon J. Gerraty if (WIFEXITED(*inout_status)) 116006b9b3e0SSimon J. Gerraty JobFinishDoneExited(job, inout_status); 116106b9b3e0SSimon J. Gerraty else 116206b9b3e0SSimon J. Gerraty JobFinishDoneSignaled(job, *inout_status); 116306b9b3e0SSimon J. Gerraty 116406b9b3e0SSimon J. Gerraty (void)fflush(stdout); 116506b9b3e0SSimon J. Gerraty } 116606b9b3e0SSimon J. Gerraty 116706b9b3e0SSimon J. Gerraty /* 116806b9b3e0SSimon J. Gerraty * Do final processing for the given job including updating parent nodes and 1169e2eeea75SSimon J. Gerraty * starting new jobs as available/necessary. 1170e2eeea75SSimon J. Gerraty * 1171e2eeea75SSimon J. Gerraty * Deferred commands for the job are placed on the .END node. 1172e2eeea75SSimon J. Gerraty * 117306b9b3e0SSimon J. Gerraty * If there was a serious error (job_errors != 0; not an ignored one), no more 1174e2eeea75SSimon J. Gerraty * jobs will be started. 11753955d011SMarcel Moolenaar * 11763955d011SMarcel Moolenaar * Input: 11773955d011SMarcel Moolenaar * job job to finish 11783955d011SMarcel Moolenaar * status sub-why job went away 11793955d011SMarcel Moolenaar */ 11803955d011SMarcel Moolenaar static void 11813955d011SMarcel Moolenaar JobFinish (Job *job, WAIT_T status) 11823955d011SMarcel Moolenaar { 1183b0c40a00SSimon J. Gerraty bool done, return_job_token; 11843955d011SMarcel Moolenaar 1185956e45f6SSimon J. Gerraty DEBUG3(JOB, "JobFinish: %d [%s], status %d\n", 11863955d011SMarcel Moolenaar job->pid, job->node->name, status); 11873955d011SMarcel Moolenaar 11883955d011SMarcel Moolenaar if ((WIFEXITED(status) && 118906b9b3e0SSimon J. Gerraty ((WEXITSTATUS(status) != 0 && !job->ignerr))) || 119006b9b3e0SSimon J. Gerraty WIFSIGNALED(status)) { 119106b9b3e0SSimon J. Gerraty /* Finished because of an error. */ 119206b9b3e0SSimon J. Gerraty 1193e2eeea75SSimon J. Gerraty JobClosePipes(job); 11943955d011SMarcel Moolenaar if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 11953955d011SMarcel Moolenaar (void)fclose(job->cmdFILE); 11963955d011SMarcel Moolenaar job->cmdFILE = NULL; 11973955d011SMarcel Moolenaar } 1198b0c40a00SSimon J. Gerraty done = true; 119906b9b3e0SSimon J. Gerraty 12003955d011SMarcel Moolenaar } else if (WIFEXITED(status)) { 12013955d011SMarcel Moolenaar /* 120206b9b3e0SSimon J. Gerraty * Deal with ignored errors in -B mode. We need to print a 120306b9b3e0SSimon J. Gerraty * message telling of the ignored error as well as to run 120406b9b3e0SSimon J. Gerraty * the next command. 12053955d011SMarcel Moolenaar */ 12063955d011SMarcel Moolenaar done = WEXITSTATUS(status) != 0; 120706b9b3e0SSimon J. Gerraty 1208e2eeea75SSimon J. Gerraty JobClosePipes(job); 120906b9b3e0SSimon J. Gerraty 12103955d011SMarcel Moolenaar } else { 121106b9b3e0SSimon J. Gerraty /* No need to close things down or anything. */ 1212b0c40a00SSimon J. Gerraty done = false; 12133955d011SMarcel Moolenaar } 12143955d011SMarcel Moolenaar 121506b9b3e0SSimon J. Gerraty if (done) 121606b9b3e0SSimon J. Gerraty JobFinishDone(job, &status); 12173955d011SMarcel Moolenaar 12183955d011SMarcel Moolenaar #ifdef USE_META 12193955d011SMarcel Moolenaar if (useMeta) { 1220e2eeea75SSimon J. Gerraty int meta_status = meta_job_finish(job); 1221e2eeea75SSimon J. Gerraty if (meta_status != 0 && status == 0) 1222e2eeea75SSimon J. Gerraty status = meta_status; 12233955d011SMarcel Moolenaar } 12243955d011SMarcel Moolenaar #endif 12253955d011SMarcel Moolenaar 1226b0c40a00SSimon J. Gerraty return_job_token = false; 12273955d011SMarcel Moolenaar 12283955d011SMarcel Moolenaar Trace_Log(JOBEND, job); 122906b9b3e0SSimon J. Gerraty if (!job->special) { 1230e2eeea75SSimon J. Gerraty if (WAIT_STATUS(status) != 0 || 1231e2eeea75SSimon J. Gerraty (aborting == ABORT_ERROR) || aborting == ABORT_INTERRUPT) 1232b0c40a00SSimon J. Gerraty return_job_token = true; 12333955d011SMarcel Moolenaar } 12343955d011SMarcel Moolenaar 1235e2eeea75SSimon J. Gerraty if (aborting != ABORT_ERROR && aborting != ABORT_INTERRUPT && 12363955d011SMarcel Moolenaar (WAIT_STATUS(status) == 0)) { 12373955d011SMarcel Moolenaar /* 123806b9b3e0SSimon J. Gerraty * As long as we aren't aborting and the job didn't return a 123906b9b3e0SSimon J. Gerraty * non-zero status that we shouldn't ignore, we call 124006b9b3e0SSimon J. Gerraty * Make_Update to update the parents. 12413955d011SMarcel Moolenaar */ 1242956e45f6SSimon J. Gerraty JobSaveCommands(job); 12433955d011SMarcel Moolenaar job->node->made = MADE; 124406b9b3e0SSimon J. Gerraty if (!job->special) 1245b0c40a00SSimon J. Gerraty return_job_token = true; 12463955d011SMarcel Moolenaar Make_Update(job->node); 1247e2eeea75SSimon J. Gerraty job->status = JOB_ST_FREE; 124806b9b3e0SSimon J. Gerraty } else if (status != 0) { 124906b9b3e0SSimon J. Gerraty job_errors++; 1250e2eeea75SSimon J. Gerraty job->status = JOB_ST_FREE; 12513955d011SMarcel Moolenaar } 12523955d011SMarcel Moolenaar 125306b9b3e0SSimon J. Gerraty if (job_errors > 0 && !opts.keepgoing && aborting != ABORT_INTERRUPT) { 125406b9b3e0SSimon J. Gerraty /* Prevent more jobs from getting started. */ 125506b9b3e0SSimon J. Gerraty aborting = ABORT_ERROR; 125606b9b3e0SSimon J. Gerraty } 12573955d011SMarcel Moolenaar 12583955d011SMarcel Moolenaar if (return_job_token) 12593955d011SMarcel Moolenaar Job_TokenReturn(); 12603955d011SMarcel Moolenaar 1261e2eeea75SSimon J. Gerraty if (aborting == ABORT_ERROR && jobTokensRunning == 0) 126206b9b3e0SSimon J. Gerraty Finish(job_errors); 12633955d011SMarcel Moolenaar } 1264e2eeea75SSimon J. Gerraty 1265e2eeea75SSimon J. Gerraty static void 1266e2eeea75SSimon J. Gerraty TouchRegular(GNode *gn) 1267e2eeea75SSimon J. Gerraty { 1268e2eeea75SSimon J. Gerraty const char *file = GNode_Path(gn); 1269b0c40a00SSimon J. Gerraty struct utimbuf times; 1270e2eeea75SSimon J. Gerraty int fd; 1271e2eeea75SSimon J. Gerraty char c; 1272e2eeea75SSimon J. Gerraty 1273b0c40a00SSimon J. Gerraty times.actime = now; 1274b0c40a00SSimon J. Gerraty times.modtime = now; 1275e2eeea75SSimon J. Gerraty if (utime(file, ×) >= 0) 1276e2eeea75SSimon J. Gerraty return; 1277e2eeea75SSimon J. Gerraty 1278e2eeea75SSimon J. Gerraty fd = open(file, O_RDWR | O_CREAT, 0666); 1279e2eeea75SSimon J. Gerraty if (fd < 0) { 1280e2eeea75SSimon J. Gerraty (void)fprintf(stderr, "*** couldn't touch %s: %s\n", 1281e2eeea75SSimon J. Gerraty file, strerror(errno)); 1282e2eeea75SSimon J. Gerraty (void)fflush(stderr); 1283e2eeea75SSimon J. Gerraty return; /* XXX: What about propagating the error? */ 1284e2eeea75SSimon J. Gerraty } 1285e2eeea75SSimon J. Gerraty 1286e2eeea75SSimon 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 1288e2eeea75SSimon J. Gerraty * as marker files. */ 1289e2eeea75SSimon J. Gerraty if (read(fd, &c, 1) == 1) { 1290e2eeea75SSimon J. Gerraty (void)lseek(fd, 0, SEEK_SET); 1291e2eeea75SSimon J. Gerraty while (write(fd, &c, 1) == -1 && errno == EAGAIN) 1292e2eeea75SSimon J. Gerraty continue; 1293e2eeea75SSimon J. Gerraty } 1294e2eeea75SSimon J. Gerraty (void)close(fd); /* XXX: What about propagating the error? */ 12953955d011SMarcel Moolenaar } 12963955d011SMarcel Moolenaar 129706b9b3e0SSimon J. Gerraty /* 129806b9b3e0SSimon J. Gerraty * Touch the given target. Called by JobStart when the -t flag was given. 12993955d011SMarcel Moolenaar * 1300956e45f6SSimon J. Gerraty * The modification date of the file is changed. 130106b9b3e0SSimon J. Gerraty * If the file did not exist, it is created. 130206b9b3e0SSimon J. Gerraty */ 13033955d011SMarcel Moolenaar void 1304b0c40a00SSimon J. Gerraty Job_Touch(GNode *gn, bool echo) 13053955d011SMarcel Moolenaar { 130606b9b3e0SSimon J. Gerraty if (gn->type & 130706b9b3e0SSimon J. Gerraty (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC | OP_OPTIONAL | 13083955d011SMarcel Moolenaar OP_SPECIAL | OP_PHONY)) { 130906b9b3e0SSimon J. Gerraty /* 131006b9b3e0SSimon J. Gerraty * These are "virtual" targets and should not really be 131106b9b3e0SSimon J. Gerraty * created. 131206b9b3e0SSimon J. Gerraty */ 13133955d011SMarcel Moolenaar return; 13143955d011SMarcel Moolenaar } 13153955d011SMarcel Moolenaar 131606b9b3e0SSimon J. Gerraty if (echo || !GNode_ShouldExecute(gn)) { 13173955d011SMarcel Moolenaar (void)fprintf(stdout, "touch %s\n", gn->name); 13183955d011SMarcel Moolenaar (void)fflush(stdout); 13193955d011SMarcel Moolenaar } 13203955d011SMarcel Moolenaar 1321e2eeea75SSimon J. Gerraty if (!GNode_ShouldExecute(gn)) 13223955d011SMarcel Moolenaar return; 13233955d011SMarcel Moolenaar 132406b9b3e0SSimon J. Gerraty if (gn->type & OP_ARCHV) 13253955d011SMarcel Moolenaar Arch_Touch(gn); 132606b9b3e0SSimon J. Gerraty else if (gn->type & OP_LIB) 13273955d011SMarcel Moolenaar Arch_TouchLib(gn); 132806b9b3e0SSimon J. Gerraty else 1329e2eeea75SSimon J. Gerraty TouchRegular(gn); 13303955d011SMarcel Moolenaar } 13313955d011SMarcel Moolenaar 133206b9b3e0SSimon J. Gerraty /* 133306b9b3e0SSimon J. Gerraty * Make sure the given node has all the commands it needs. 1334956e45f6SSimon J. Gerraty * 1335956e45f6SSimon J. Gerraty * The node will have commands from the .DEFAULT rule added to it if it 1336956e45f6SSimon J. Gerraty * needs them. 13373955d011SMarcel Moolenaar * 13383955d011SMarcel Moolenaar * Input: 13393955d011SMarcel Moolenaar * gn The target whose commands need verifying 13403955d011SMarcel Moolenaar * abortProc Function to abort with message 13413955d011SMarcel Moolenaar * 13423955d011SMarcel Moolenaar * Results: 1343b0c40a00SSimon J. Gerraty * true if the commands list is/was ok. 13443955d011SMarcel Moolenaar */ 1345b0c40a00SSimon J. Gerraty bool 13463955d011SMarcel Moolenaar Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...)) 13473955d011SMarcel Moolenaar { 1348956e45f6SSimon J. Gerraty if (GNode_IsTarget(gn)) 1349b0c40a00SSimon J. Gerraty return true; 135006b9b3e0SSimon J. Gerraty if (!Lst_IsEmpty(&gn->commands)) 1351b0c40a00SSimon J. Gerraty return true; 135206b9b3e0SSimon J. Gerraty if ((gn->type & OP_LIB) && !Lst_IsEmpty(&gn->children)) 1353b0c40a00SSimon J. Gerraty return true; 1354956e45f6SSimon J. Gerraty 13553955d011SMarcel Moolenaar /* 13563955d011SMarcel Moolenaar * No commands. Look for .DEFAULT rule from which we might infer 1357e2eeea75SSimon J. Gerraty * commands. 13583955d011SMarcel Moolenaar */ 135906b9b3e0SSimon J. Gerraty if (defaultNode != NULL && !Lst_IsEmpty(&defaultNode->commands) && 1360e2eeea75SSimon J. Gerraty !(gn->type & OP_SPECIAL)) { 13613955d011SMarcel Moolenaar /* 136206b9b3e0SSimon J. Gerraty * The traditional Make only looks for a .DEFAULT if the node 136306b9b3e0SSimon J. Gerraty * was never the target of an operator, so that's what we do 136406b9b3e0SSimon J. Gerraty * too. 1365e2eeea75SSimon J. Gerraty * 1366e2eeea75SSimon J. Gerraty * The .DEFAULT node acts like a transformation rule, in that 13673955d011SMarcel Moolenaar * gn also inherits any attributes or sources attached to 13683955d011SMarcel Moolenaar * .DEFAULT itself. 13693955d011SMarcel Moolenaar */ 1370e2eeea75SSimon J. Gerraty Make_HandleUse(defaultNode, gn); 1371dba7b0efSSimon J. Gerraty Var_Set(gn, IMPSRC, GNode_VarTarget(gn)); 1372b0c40a00SSimon J. Gerraty return true; 1373956e45f6SSimon J. Gerraty } 1374956e45f6SSimon J. Gerraty 1375b0c40a00SSimon J. Gerraty Dir_UpdateMTime(gn, false); 1376e2eeea75SSimon J. Gerraty if (gn->mtime != 0 || (gn->type & OP_SPECIAL)) 1377b0c40a00SSimon J. Gerraty return true; 1378956e45f6SSimon J. Gerraty 13793955d011SMarcel Moolenaar /* 1380956e45f6SSimon J. Gerraty * The node wasn't the target of an operator. We have no .DEFAULT 13813955d011SMarcel Moolenaar * rule to go on and the target doesn't already exist. There's 13823955d011SMarcel Moolenaar * nothing more we can do for this branch. If the -k flag wasn't 13833955d011SMarcel Moolenaar * given, we stop in our tracks, otherwise we just don't update 13843955d011SMarcel Moolenaar * this node's parents so they never get examined. 13853955d011SMarcel Moolenaar */ 13863955d011SMarcel Moolenaar 13873955d011SMarcel Moolenaar if (gn->flags & FROM_DEPEND) { 13881748de26SSimon J. Gerraty if (!Job_RunTarget(".STALE", gn->fname)) 138906b9b3e0SSimon J. Gerraty fprintf(stdout, 139006b9b3e0SSimon J. Gerraty "%s: %s, %d: ignoring stale %s for %s\n", 13911748de26SSimon J. Gerraty progname, gn->fname, gn->lineno, makeDependfile, 13921748de26SSimon J. Gerraty gn->name); 1393b0c40a00SSimon J. Gerraty return true; 13943955d011SMarcel Moolenaar } 13953955d011SMarcel Moolenaar 13963955d011SMarcel Moolenaar if (gn->type & OP_OPTIONAL) { 1397956e45f6SSimon J. Gerraty (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n", 1398956e45f6SSimon J. Gerraty progname, gn->name, "ignored"); 13993955d011SMarcel Moolenaar (void)fflush(stdout); 1400b0c40a00SSimon J. Gerraty return true; 14013955d011SMarcel Moolenaar } 14023955d011SMarcel Moolenaar 1403956e45f6SSimon J. Gerraty if (opts.keepgoing) { 1404956e45f6SSimon J. Gerraty (void)fprintf(stdout, "%s: don't know how to make %s (%s)\n", 1405956e45f6SSimon J. Gerraty progname, gn->name, "continuing"); 1406956e45f6SSimon J. Gerraty (void)fflush(stdout); 1407b0c40a00SSimon J. Gerraty return false; 1408956e45f6SSimon J. Gerraty } 1409956e45f6SSimon J. Gerraty 1410956e45f6SSimon J. Gerraty abortProc("%s: don't know how to make %s. Stop", progname, gn->name); 1411b0c40a00SSimon J. Gerraty return false; 1412956e45f6SSimon J. Gerraty } 1413956e45f6SSimon J. Gerraty 141406b9b3e0SSimon J. Gerraty /* 141506b9b3e0SSimon J. Gerraty * Execute the shell for the given job. 14163955d011SMarcel Moolenaar * 141706b9b3e0SSimon J. Gerraty * See Job_CatchOutput for handling the output of the shell. 141806b9b3e0SSimon J. Gerraty */ 14193955d011SMarcel Moolenaar static void 14203955d011SMarcel Moolenaar JobExec(Job *job, char **argv) 14213955d011SMarcel Moolenaar { 14223955d011SMarcel Moolenaar int cpid; /* ID of new child */ 14233955d011SMarcel Moolenaar sigset_t mask; 14243955d011SMarcel Moolenaar 14253955d011SMarcel Moolenaar if (DEBUG(JOB)) { 14263955d011SMarcel Moolenaar int i; 14273955d011SMarcel Moolenaar 1428e2eeea75SSimon J. Gerraty debug_printf("Running %s\n", job->node->name); 1429956e45f6SSimon J. Gerraty debug_printf("\tCommand: "); 14303955d011SMarcel Moolenaar for (i = 0; argv[i] != NULL; i++) { 1431956e45f6SSimon J. Gerraty debug_printf("%s ", argv[i]); 14323955d011SMarcel Moolenaar } 1433956e45f6SSimon J. Gerraty debug_printf("\n"); 14343955d011SMarcel Moolenaar } 14353955d011SMarcel Moolenaar 14363955d011SMarcel Moolenaar /* 14373955d011SMarcel Moolenaar * Some jobs produce no output and it's disconcerting to have 14383955d011SMarcel Moolenaar * no feedback of their running (since they produce no output, the 14393955d011SMarcel Moolenaar * banner with their name in it never appears). This is an attempt to 14403955d011SMarcel Moolenaar * provide that feedback, even if nothing follows it. 14413955d011SMarcel Moolenaar */ 144206b9b3e0SSimon J. Gerraty if (job->echo) 144306b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 14443955d011SMarcel Moolenaar 14453955d011SMarcel Moolenaar /* No interruptions until this job is on the `jobs' list */ 14463955d011SMarcel Moolenaar JobSigLock(&mask); 14473955d011SMarcel Moolenaar 14483955d011SMarcel Moolenaar /* Pre-emptively mark job running, pid still zero though */ 1449e2eeea75SSimon J. Gerraty job->status = JOB_ST_RUNNING; 14503955d011SMarcel Moolenaar 145106b9b3e0SSimon J. Gerraty Var_ReexportVars(); 145206b9b3e0SSimon J. Gerraty 1453dba7b0efSSimon J. Gerraty cpid = vfork(); 14543955d011SMarcel Moolenaar if (cpid == -1) 14553955d011SMarcel Moolenaar Punt("Cannot vfork: %s", strerror(errno)); 14563955d011SMarcel Moolenaar 14573955d011SMarcel Moolenaar if (cpid == 0) { 14583955d011SMarcel Moolenaar /* Child */ 14593955d011SMarcel Moolenaar sigset_t tmask; 14603955d011SMarcel Moolenaar 14613955d011SMarcel Moolenaar #ifdef USE_META 14623955d011SMarcel Moolenaar if (useMeta) { 14633955d011SMarcel Moolenaar meta_job_child(job); 14643955d011SMarcel Moolenaar } 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))) { 14913955d011SMarcel Moolenaar /* 14923955d011SMarcel Moolenaar * Pass job token pipe to submakes. 14933955d011SMarcel Moolenaar */ 1494956e45f6SSimon J. Gerraty if (fcntl(tokenWaitJob.inPipe, F_SETFD, 0) == -1) 149506b9b3e0SSimon J. Gerraty execDie("clear close-on-exec", 149606b9b3e0SSimon J. Gerraty "tokenWaitJob.inPipe"); 1497956e45f6SSimon J. Gerraty if (fcntl(tokenWaitJob.outPipe, F_SETFD, 0) == -1) 149806b9b3e0SSimon J. Gerraty execDie("clear close-on-exec", 149906b9b3e0SSimon J. Gerraty "tokenWaitJob.outPipe"); 15003955d011SMarcel Moolenaar } 15013955d011SMarcel Moolenaar 15023955d011SMarcel Moolenaar /* 15033955d011SMarcel Moolenaar * Set up the child's output to be routed through the pipe 15043955d011SMarcel Moolenaar * we've created for it. 15053955d011SMarcel Moolenaar */ 1506956e45f6SSimon J. Gerraty if (dup2(job->outPipe, 1) == -1) 1507956e45f6SSimon J. Gerraty execDie("dup2", "job->outPipe"); 1508956e45f6SSimon J. Gerraty 15093955d011SMarcel Moolenaar /* 151006b9b3e0SSimon J. Gerraty * The output channels are marked close on exec. This bit 151106b9b3e0SSimon J. Gerraty * was duplicated by the dup2(on some systems), so we have 151206b9b3e0SSimon J. Gerraty * to clear it before routing the shell's error output to 151306b9b3e0SSimon J. Gerraty * the same place as its standard output. 15143955d011SMarcel Moolenaar */ 1515956e45f6SSimon J. Gerraty if (fcntl(1, F_SETFD, 0) == -1) 1516956e45f6SSimon J. Gerraty execDie("clear close-on-exec", "stdout"); 1517956e45f6SSimon J. Gerraty if (dup2(1, 2) == -1) 1518956e45f6SSimon J. Gerraty execDie("dup2", "1, 2"); 15193955d011SMarcel Moolenaar 15203955d011SMarcel Moolenaar /* 152106b9b3e0SSimon J. Gerraty * We want to switch the child into a different process 152206b9b3e0SSimon J. Gerraty * family so we can kill it and all its descendants in 152306b9b3e0SSimon J. Gerraty * one fell swoop, by killing its process family, but not 152406b9b3e0SSimon J. Gerraty * commit suicide. 15253955d011SMarcel Moolenaar */ 15263955d011SMarcel Moolenaar #if defined(HAVE_SETPGID) 15273955d011SMarcel Moolenaar (void)setpgid(0, getpid()); 15283955d011SMarcel Moolenaar #else 15293955d011SMarcel Moolenaar # if defined(HAVE_SETSID) 15303955d011SMarcel Moolenaar /* XXX: dsl - I'm sure this should be setpgrp()... */ 15313955d011SMarcel Moolenaar (void)setsid(); 15323955d011SMarcel Moolenaar # else 15333955d011SMarcel Moolenaar (void)setpgrp(0, getpid()); 15343955d011SMarcel Moolenaar # endif 15353955d011SMarcel Moolenaar #endif 15363955d011SMarcel Moolenaar 15373955d011SMarcel Moolenaar (void)execv(shellPath, argv); 1538956e45f6SSimon J. Gerraty execDie("exec", shellPath); 15393955d011SMarcel Moolenaar } 15403955d011SMarcel Moolenaar 15413955d011SMarcel Moolenaar /* Parent, continuing after the child exec */ 15423955d011SMarcel Moolenaar job->pid = cpid; 15433955d011SMarcel Moolenaar 15443955d011SMarcel Moolenaar Trace_Log(JOBSTART, job); 15453955d011SMarcel Moolenaar 154649caa483SSimon J. Gerraty #ifdef USE_META 154749caa483SSimon J. Gerraty if (useMeta) { 154849caa483SSimon J. Gerraty meta_job_parent(job, cpid); 154949caa483SSimon J. Gerraty } 155049caa483SSimon J. Gerraty #endif 155149caa483SSimon J. Gerraty 15523955d011SMarcel Moolenaar /* 15533955d011SMarcel Moolenaar * Set the current position in the buffer to the beginning 15543955d011SMarcel Moolenaar * and mark another stream to watch in the outputs mask 15553955d011SMarcel Moolenaar */ 15563955d011SMarcel Moolenaar job->curPos = 0; 15573955d011SMarcel Moolenaar 15583955d011SMarcel Moolenaar watchfd(job); 15593955d011SMarcel Moolenaar 15603955d011SMarcel Moolenaar if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 15613955d011SMarcel Moolenaar (void)fclose(job->cmdFILE); 15623955d011SMarcel Moolenaar job->cmdFILE = NULL; 15633955d011SMarcel Moolenaar } 15643955d011SMarcel Moolenaar 1565dba7b0efSSimon J. Gerraty /* Now that the job is actually running, add it to the table. */ 15663955d011SMarcel Moolenaar if (DEBUG(JOB)) { 1567956e45f6SSimon J. Gerraty debug_printf("JobExec(%s): pid %d added to jobs table\n", 15683955d011SMarcel Moolenaar job->node->name, job->pid); 1569b0c40a00SSimon J. Gerraty DumpJobs("job started"); 15703955d011SMarcel Moolenaar } 15713955d011SMarcel Moolenaar JobSigUnlock(&mask); 15723955d011SMarcel Moolenaar } 15733955d011SMarcel Moolenaar 1574956e45f6SSimon J. Gerraty /* Create the argv needed to execute the shell for a given job. */ 15753955d011SMarcel Moolenaar static void 15763955d011SMarcel Moolenaar JobMakeArgv(Job *job, char **argv) 15773955d011SMarcel Moolenaar { 15783955d011SMarcel Moolenaar int argc; 15793955d011SMarcel Moolenaar static char args[10]; /* For merged arguments */ 15803955d011SMarcel Moolenaar 15813955d011SMarcel Moolenaar argv[0] = UNCONST(shellName); 15823955d011SMarcel Moolenaar argc = 1; 15833955d011SMarcel Moolenaar 158406b9b3e0SSimon J. Gerraty if ((shell->errFlag != NULL && shell->errFlag[0] != '-') || 158506b9b3e0SSimon J. Gerraty (shell->echoFlag != NULL && shell->echoFlag[0] != '-')) { 15863955d011SMarcel Moolenaar /* 158706b9b3e0SSimon J. Gerraty * At least one of the flags doesn't have a minus before it, 158806b9b3e0SSimon J. Gerraty * so merge them together. Have to do this because the Bourne 158906b9b3e0SSimon J. Gerraty * shell thinks its second argument is a file to source. 159006b9b3e0SSimon J. Gerraty * Grrrr. Note the ten-character limitation on the combined 159106b9b3e0SSimon J. Gerraty * arguments. 159206b9b3e0SSimon J. Gerraty * 159306b9b3e0SSimon J. Gerraty * TODO: Research until when the above comments were 159406b9b3e0SSimon J. Gerraty * practically relevant. 15953955d011SMarcel Moolenaar */ 1596e2eeea75SSimon J. Gerraty (void)snprintf(args, sizeof args, "-%s%s", 159706b9b3e0SSimon J. Gerraty (job->ignerr ? "" : 159806b9b3e0SSimon J. Gerraty (shell->errFlag != NULL ? shell->errFlag : "")), 159906b9b3e0SSimon J. Gerraty (!job->echo ? "" : 160006b9b3e0SSimon J. Gerraty (shell->echoFlag != NULL ? shell->echoFlag : ""))); 16013955d011SMarcel Moolenaar 160206b9b3e0SSimon J. Gerraty if (args[1] != '\0') { 16033955d011SMarcel Moolenaar argv[argc] = args; 16043955d011SMarcel Moolenaar argc++; 16053955d011SMarcel Moolenaar } 16063955d011SMarcel Moolenaar } else { 160706b9b3e0SSimon J. Gerraty if (!job->ignerr && shell->errFlag != NULL) { 160806b9b3e0SSimon J. Gerraty argv[argc] = UNCONST(shell->errFlag); 16093955d011SMarcel Moolenaar argc++; 16103955d011SMarcel Moolenaar } 161106b9b3e0SSimon J. Gerraty if (job->echo && shell->echoFlag != NULL) { 161206b9b3e0SSimon J. Gerraty argv[argc] = UNCONST(shell->echoFlag); 16133955d011SMarcel Moolenaar argc++; 16143955d011SMarcel Moolenaar } 16153955d011SMarcel Moolenaar } 16163955d011SMarcel Moolenaar argv[argc] = NULL; 16173955d011SMarcel Moolenaar } 16183955d011SMarcel Moolenaar 161906b9b3e0SSimon J. Gerraty static void 1620b0c40a00SSimon J. Gerraty JobWriteShellCommands(Job *job, GNode *gn, bool *out_run) 16213955d011SMarcel Moolenaar { 16223955d011SMarcel Moolenaar /* 162306b9b3e0SSimon J. Gerraty * tfile is the name of a file into which all shell commands 162406b9b3e0SSimon J. Gerraty * are put. It is removed before the child shell is executed, 162506b9b3e0SSimon J. Gerraty * unless DEBUG(SCRIPT) is set. 16263955d011SMarcel Moolenaar */ 1627dba7b0efSSimon J. Gerraty char tfile[MAXPATHLEN]; 162806b9b3e0SSimon J. Gerraty int tfd; /* File descriptor to the temp file */ 162906b9b3e0SSimon J. Gerraty 1630dba7b0efSSimon J. Gerraty tfd = Job_TempFile(TMPPAT, tfile, sizeof tfile); 16313955d011SMarcel Moolenaar 16323955d011SMarcel Moolenaar job->cmdFILE = fdopen(tfd, "w+"); 1633e2eeea75SSimon J. Gerraty if (job->cmdFILE == NULL) 16343955d011SMarcel Moolenaar Punt("Could not fdopen %s", tfile); 1635e2eeea75SSimon J. Gerraty 1636956e45f6SSimon J. Gerraty (void)fcntl(fileno(job->cmdFILE), F_SETFD, FD_CLOEXEC); 16373955d011SMarcel Moolenaar 16383955d011SMarcel Moolenaar #ifdef USE_META 16393955d011SMarcel Moolenaar if (useMeta) { 16403955d011SMarcel Moolenaar meta_job_start(job, gn); 164106b9b3e0SSimon J. Gerraty if (gn->type & OP_SILENT) /* might have changed */ 1642b0c40a00SSimon J. Gerraty job->echo = false; 16433955d011SMarcel Moolenaar } 16443955d011SMarcel Moolenaar #endif 16453955d011SMarcel Moolenaar 1646b0c40a00SSimon J. Gerraty *out_run = JobWriteCommands(job); 164706b9b3e0SSimon J. Gerraty } 164806b9b3e0SSimon J. Gerraty 164906b9b3e0SSimon J. Gerraty /* 1650b0c40a00SSimon J. Gerraty * Start a target-creation process going for the target described by gn. 165106b9b3e0SSimon J. Gerraty * 165206b9b3e0SSimon J. Gerraty * Results: 165306b9b3e0SSimon J. Gerraty * JOB_ERROR if there was an error in the commands, JOB_FINISHED 165406b9b3e0SSimon J. Gerraty * if there isn't actually anything left to do for the job and 165506b9b3e0SSimon J. Gerraty * JOB_RUNNING if the job has been started. 165606b9b3e0SSimon J. Gerraty * 1657b0c40a00SSimon J. Gerraty * Details: 165806b9b3e0SSimon J. Gerraty * A new Job node is created and added to the list of running 165906b9b3e0SSimon J. Gerraty * jobs. PMake is forked and a child shell created. 166006b9b3e0SSimon J. Gerraty * 166106b9b3e0SSimon J. Gerraty * NB: The return value is ignored by everyone. 166206b9b3e0SSimon J. Gerraty */ 166306b9b3e0SSimon J. Gerraty static JobStartResult 1664b0c40a00SSimon J. Gerraty JobStart(GNode *gn, bool special) 166506b9b3e0SSimon J. Gerraty { 166606b9b3e0SSimon J. Gerraty Job *job; /* new job descriptor */ 166706b9b3e0SSimon J. Gerraty char *argv[10]; /* Argument vector to shell */ 1668b0c40a00SSimon J. Gerraty bool cmdsOK; /* true if the nodes commands were all right */ 1669b0c40a00SSimon J. Gerraty bool run; 167006b9b3e0SSimon J. Gerraty 167106b9b3e0SSimon J. Gerraty for (job = job_table; job < job_table_end; job++) { 167206b9b3e0SSimon J. Gerraty if (job->status == JOB_ST_FREE) 167306b9b3e0SSimon J. Gerraty break; 167406b9b3e0SSimon J. Gerraty } 167506b9b3e0SSimon J. Gerraty if (job >= job_table_end) 167606b9b3e0SSimon J. Gerraty Punt("JobStart no job slots vacant"); 167706b9b3e0SSimon J. Gerraty 167806b9b3e0SSimon J. Gerraty memset(job, 0, sizeof *job); 167906b9b3e0SSimon J. Gerraty job->node = gn; 168006b9b3e0SSimon J. Gerraty job->tailCmds = NULL; 168106b9b3e0SSimon J. Gerraty job->status = JOB_ST_SET_UP; 168206b9b3e0SSimon J. Gerraty 168306b9b3e0SSimon J. Gerraty job->special = special || gn->type & OP_SPECIAL; 168406b9b3e0SSimon J. Gerraty job->ignerr = opts.ignoreErrors || gn->type & OP_IGNORE; 168506b9b3e0SSimon J. Gerraty job->echo = !(opts.beSilent || gn->type & OP_SILENT); 168606b9b3e0SSimon J. Gerraty 168706b9b3e0SSimon J. Gerraty /* 168806b9b3e0SSimon J. Gerraty * Check the commands now so any attributes from .DEFAULT have a 168906b9b3e0SSimon J. Gerraty * chance to migrate to the node. 169006b9b3e0SSimon J. Gerraty */ 169106b9b3e0SSimon J. Gerraty cmdsOK = Job_CheckCommands(gn, Error); 169206b9b3e0SSimon J. Gerraty 169306b9b3e0SSimon J. Gerraty job->inPollfd = NULL; 1694dba7b0efSSimon J. Gerraty 1695dba7b0efSSimon J. Gerraty if (Lst_IsEmpty(&gn->commands)) { 1696dba7b0efSSimon J. Gerraty job->cmdFILE = stdout; 1697b0c40a00SSimon J. Gerraty run = false; 1698b0c40a00SSimon J. Gerraty 1699b0c40a00SSimon J. Gerraty /* 1700b0c40a00SSimon J. Gerraty * We're serious here, but if the commands were bogus, we're 1701b0c40a00SSimon J. Gerraty * also dead... 1702b0c40a00SSimon J. Gerraty */ 1703b0c40a00SSimon J. Gerraty if (!cmdsOK) { 1704b0c40a00SSimon J. Gerraty PrintOnError(gn, NULL); /* provide some clue */ 1705b0c40a00SSimon J. Gerraty DieHorribly(); 1706b0c40a00SSimon J. Gerraty } 1707dba7b0efSSimon J. Gerraty } else if (((gn->type & OP_MAKE) && !opts.noRecursiveExecute) || 170806b9b3e0SSimon J. Gerraty (!opts.noExecute && !opts.touchFlag)) { 1709dba7b0efSSimon J. Gerraty /* 1710dba7b0efSSimon J. Gerraty * The above condition looks very similar to 1711dba7b0efSSimon J. Gerraty * GNode_ShouldExecute but is subtly different. It prevents 1712dba7b0efSSimon J. Gerraty * that .MAKE targets are touched since these are usually 1713dba7b0efSSimon J. Gerraty * virtual targets. 1714dba7b0efSSimon J. Gerraty */ 1715dba7b0efSSimon J. Gerraty 1716b0c40a00SSimon J. Gerraty /* 1717b0c40a00SSimon J. Gerraty * We're serious here, but if the commands were bogus, we're 1718b0c40a00SSimon J. Gerraty * also dead... 1719b0c40a00SSimon J. Gerraty */ 1720b0c40a00SSimon J. Gerraty if (!cmdsOK) { 1721b0c40a00SSimon J. Gerraty PrintOnError(gn, NULL); /* provide some clue */ 1722b0c40a00SSimon J. Gerraty DieHorribly(); 1723b0c40a00SSimon J. Gerraty } 1724b0c40a00SSimon J. Gerraty 1725b0c40a00SSimon J. Gerraty JobWriteShellCommands(job, gn, &run); 1726dba7b0efSSimon J. Gerraty (void)fflush(job->cmdFILE); 1727956e45f6SSimon J. Gerraty } else if (!GNode_ShouldExecute(gn)) { 17283955d011SMarcel Moolenaar /* 1729b0c40a00SSimon J. Gerraty * Just write all the commands to stdout in one fell swoop. 1730dba7b0efSSimon J. Gerraty * This still sets up job->tailCmds correctly. 17313955d011SMarcel Moolenaar */ 173206b9b3e0SSimon J. Gerraty SwitchOutputTo(gn); 17333955d011SMarcel Moolenaar job->cmdFILE = stdout; 1734956e45f6SSimon J. Gerraty if (cmdsOK) 1735b0c40a00SSimon J. Gerraty JobWriteCommands(job); 1736b0c40a00SSimon J. Gerraty run = false; 1737dba7b0efSSimon J. Gerraty (void)fflush(job->cmdFILE); 17383955d011SMarcel Moolenaar } else { 173906b9b3e0SSimon J. Gerraty Job_Touch(gn, job->echo); 1740b0c40a00SSimon J. Gerraty run = false; 17413955d011SMarcel Moolenaar } 17423955d011SMarcel Moolenaar 174306b9b3e0SSimon J. Gerraty /* If we're not supposed to execute a shell, don't. */ 174406b9b3e0SSimon J. Gerraty if (!run) { 174506b9b3e0SSimon J. Gerraty if (!job->special) 17463955d011SMarcel Moolenaar Job_TokenReturn(); 174706b9b3e0SSimon J. Gerraty /* Unlink and close the command file if we opened one */ 1748e2eeea75SSimon J. Gerraty if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 17493955d011SMarcel Moolenaar (void)fclose(job->cmdFILE); 17503955d011SMarcel Moolenaar job->cmdFILE = NULL; 17513955d011SMarcel Moolenaar } 17523955d011SMarcel Moolenaar 17533955d011SMarcel Moolenaar /* 175406b9b3e0SSimon J. Gerraty * We only want to work our way up the graph if we aren't 175506b9b3e0SSimon J. Gerraty * here because the commands for the job were no good. 17563955d011SMarcel Moolenaar */ 1757956e45f6SSimon J. Gerraty if (cmdsOK && aborting == ABORT_NONE) { 1758956e45f6SSimon J. Gerraty JobSaveCommands(job); 17593955d011SMarcel Moolenaar job->node->made = MADE; 17603955d011SMarcel Moolenaar Make_Update(job->node); 17613955d011SMarcel Moolenaar } 1762e2eeea75SSimon J. Gerraty job->status = JOB_ST_FREE; 17633955d011SMarcel Moolenaar return cmdsOK ? JOB_FINISHED : JOB_ERROR; 17643955d011SMarcel Moolenaar } 17653955d011SMarcel Moolenaar 17663955d011SMarcel Moolenaar /* 176706b9b3e0SSimon J. Gerraty * Set up the control arguments to the shell. This is based on the 176806b9b3e0SSimon J. Gerraty * flags set earlier for this job. 17693955d011SMarcel Moolenaar */ 17703955d011SMarcel Moolenaar JobMakeArgv(job, argv); 17713955d011SMarcel Moolenaar 17723955d011SMarcel Moolenaar /* Create the pipe by which we'll get the shell's output. */ 17733955d011SMarcel Moolenaar JobCreatePipe(job, 3); 17743955d011SMarcel Moolenaar 17753955d011SMarcel Moolenaar JobExec(job, argv); 17763841c287SSimon J. Gerraty return JOB_RUNNING; 17773955d011SMarcel Moolenaar } 17783955d011SMarcel Moolenaar 177906b9b3e0SSimon J. Gerraty /* 1780b0c40a00SSimon J. Gerraty * If the shell has an output filter (which only csh and ksh have by default), 1781b0c40a00SSimon J. Gerraty * print the output of the child process, skipping the noPrint text of the 1782b0c40a00SSimon J. Gerraty * shell. 1783b0c40a00SSimon J. Gerraty * 1784b0c40a00SSimon J. Gerraty * Return the part of the output that the calling function needs to output by 1785b0c40a00SSimon J. Gerraty * itself. 178606b9b3e0SSimon J. Gerraty */ 17873955d011SMarcel Moolenaar static char * 1788b0c40a00SSimon J. Gerraty PrintFilteredOutput(char *cp, char *endp) /* XXX: should all be const */ 17893955d011SMarcel Moolenaar { 179006b9b3e0SSimon J. Gerraty char *ecp; /* XXX: should be const */ 17913955d011SMarcel Moolenaar 179206b9b3e0SSimon J. Gerraty if (shell->noPrint == NULL || shell->noPrint[0] == '\0') 1793e2eeea75SSimon J. Gerraty return cp; 1794e2eeea75SSimon J. Gerraty 179506b9b3e0SSimon J. Gerraty /* 179606b9b3e0SSimon J. Gerraty * XXX: What happens if shell->noPrint occurs on the boundary of 179706b9b3e0SSimon J. Gerraty * the buffer? To work correctly in all cases, this should rather 179806b9b3e0SSimon J. Gerraty * be a proper stream filter instead of doing string matching on 179906b9b3e0SSimon J. Gerraty * selected chunks of the output. 180006b9b3e0SSimon J. Gerraty */ 180106b9b3e0SSimon J. Gerraty while ((ecp = strstr(cp, shell->noPrint)) != NULL) { 1802e2eeea75SSimon J. Gerraty if (ecp != cp) { 180306b9b3e0SSimon J. Gerraty *ecp = '\0'; /* XXX: avoid writing to the buffer */ 18043955d011SMarcel Moolenaar /* 18053955d011SMarcel Moolenaar * The only way there wouldn't be a newline after 18063955d011SMarcel Moolenaar * this line is if it were the last in the buffer. 180706b9b3e0SSimon J. Gerraty * however, since the noPrint output comes after it, 18083955d011SMarcel Moolenaar * there must be a newline, so we don't print one. 18093955d011SMarcel Moolenaar */ 181006b9b3e0SSimon J. Gerraty /* XXX: What about null bytes in the output? */ 18113955d011SMarcel Moolenaar (void)fprintf(stdout, "%s", cp); 18123955d011SMarcel Moolenaar (void)fflush(stdout); 18133955d011SMarcel Moolenaar } 181406b9b3e0SSimon J. Gerraty cp = ecp + shell->noPrintLen; 181506b9b3e0SSimon J. Gerraty if (cp == endp) 181606b9b3e0SSimon J. Gerraty break; 181706b9b3e0SSimon J. Gerraty cp++; /* skip over the (XXX: assumed) newline */ 1818e2eeea75SSimon J. Gerraty pp_skip_whitespace(&cp); 18193955d011SMarcel Moolenaar } 18203955d011SMarcel Moolenaar return cp; 18213955d011SMarcel Moolenaar } 18223955d011SMarcel Moolenaar 1823e2eeea75SSimon J. Gerraty /* 1824e2eeea75SSimon J. Gerraty * This function is called whenever there is something to read on the pipe. 1825e2eeea75SSimon J. Gerraty * We collect more output from the given job and store it in the job's 1826e2eeea75SSimon J. Gerraty * outBuf. If this makes up a line, we print it tagged by the job's 1827e2eeea75SSimon J. Gerraty * identifier, as necessary. 1828e2eeea75SSimon J. Gerraty * 1829e2eeea75SSimon J. Gerraty * In the output of the shell, the 'noPrint' lines are removed. If the 1830e2eeea75SSimon J. Gerraty * command is not alone on the line (the character after it is not \0 or 1831e2eeea75SSimon J. Gerraty * \n), we do print whatever follows it. 18323955d011SMarcel Moolenaar * 18333955d011SMarcel Moolenaar * Input: 18343955d011SMarcel Moolenaar * job the job whose output needs printing 1835b0c40a00SSimon J. Gerraty * finish true if this is the last time we'll be called 18363955d011SMarcel Moolenaar * for this job 18373955d011SMarcel Moolenaar */ 1838956e45f6SSimon J. Gerraty static void 1839b0c40a00SSimon J. Gerraty CollectOutput(Job *job, bool finish) 18403955d011SMarcel Moolenaar { 1841b0c40a00SSimon J. Gerraty bool gotNL; /* true if got a newline */ 1842b0c40a00SSimon J. Gerraty bool fbuf; /* true if our buffer filled up */ 1843956e45f6SSimon J. Gerraty size_t nr; /* number of bytes read */ 1844956e45f6SSimon J. Gerraty size_t i; /* auxiliary index into outBuf */ 1845956e45f6SSimon J. Gerraty size_t max; /* limit for i (end of current data) */ 1846956e45f6SSimon J. Gerraty ssize_t nRead; /* (Temporary) number of bytes read */ 18473955d011SMarcel Moolenaar 184806b9b3e0SSimon J. Gerraty /* Read as many bytes as will fit in the buffer. */ 1849e2eeea75SSimon J. Gerraty again: 1850b0c40a00SSimon J. Gerraty gotNL = false; 1851b0c40a00SSimon J. Gerraty fbuf = false; 18523955d011SMarcel Moolenaar 18533955d011SMarcel Moolenaar nRead = read(job->inPipe, &job->outBuf[job->curPos], 18543955d011SMarcel Moolenaar JOB_BUFSIZE - job->curPos); 18553955d011SMarcel Moolenaar if (nRead < 0) { 18563955d011SMarcel Moolenaar if (errno == EAGAIN) 18573955d011SMarcel Moolenaar return; 18583955d011SMarcel Moolenaar if (DEBUG(JOB)) { 1859b0c40a00SSimon J. Gerraty perror("CollectOutput(piperead)"); 18603955d011SMarcel Moolenaar } 18613955d011SMarcel Moolenaar nr = 0; 18623955d011SMarcel Moolenaar } else { 1863956e45f6SSimon J. Gerraty nr = (size_t)nRead; 18643955d011SMarcel Moolenaar } 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 * Also clear the 'finish' flag so we stop looping. 18713955d011SMarcel Moolenaar */ 1872e2eeea75SSimon J. Gerraty if (nr == 0 && job->curPos != 0) { 18733955d011SMarcel Moolenaar job->outBuf[job->curPos] = '\n'; 18743955d011SMarcel Moolenaar nr = 1; 1875b0c40a00SSimon J. Gerraty finish = false; 18763955d011SMarcel Moolenaar } else if (nr == 0) { 1877b0c40a00SSimon J. Gerraty finish = false; 18783955d011SMarcel Moolenaar } 18793955d011SMarcel Moolenaar 18803955d011SMarcel Moolenaar /* 18813955d011SMarcel Moolenaar * Look for the last newline in the bytes we just got. If there is 18823955d011SMarcel Moolenaar * one, break out of the loop with 'i' as its index and gotNL set 1883b0c40a00SSimon J. Gerraty * true. 18843955d011SMarcel Moolenaar */ 18853955d011SMarcel Moolenaar max = job->curPos + nr; 188606b9b3e0SSimon J. Gerraty for (i = job->curPos + nr - 1; 188706b9b3e0SSimon J. Gerraty i >= job->curPos && i != (size_t)-1; i--) { 18883955d011SMarcel Moolenaar if (job->outBuf[i] == '\n') { 1889b0c40a00SSimon J. Gerraty gotNL = true; 18903955d011SMarcel Moolenaar break; 18913955d011SMarcel Moolenaar } else if (job->outBuf[i] == '\0') { 18923955d011SMarcel Moolenaar /* 1893b0c40a00SSimon J. Gerraty * FIXME: The null characters are only replaced with 1894b0c40a00SSimon J. Gerraty * space _after_ the last '\n'. Everywhere else they 1895b0c40a00SSimon J. Gerraty * hide the rest of the command output. 18963955d011SMarcel Moolenaar */ 18973955d011SMarcel Moolenaar job->outBuf[i] = ' '; 18983955d011SMarcel Moolenaar } 18993955d011SMarcel Moolenaar } 19003955d011SMarcel Moolenaar 19013955d011SMarcel Moolenaar if (!gotNL) { 19023955d011SMarcel Moolenaar job->curPos += nr; 19033955d011SMarcel Moolenaar if (job->curPos == JOB_BUFSIZE) { 19043955d011SMarcel Moolenaar /* 19053955d011SMarcel Moolenaar * If we've run out of buffer space, we have no choice 19063955d011SMarcel Moolenaar * but to print the stuff. sigh. 19073955d011SMarcel Moolenaar */ 1908b0c40a00SSimon J. Gerraty fbuf = true; 19093955d011SMarcel Moolenaar i = job->curPos; 19103955d011SMarcel Moolenaar } 19113955d011SMarcel Moolenaar } 19123955d011SMarcel Moolenaar if (gotNL || fbuf) { 19133955d011SMarcel Moolenaar /* 19143955d011SMarcel Moolenaar * Need to send the output to the screen. Null terminate it 19153955d011SMarcel Moolenaar * first, overwriting the newline character if there was one. 19163955d011SMarcel Moolenaar * So long as the line isn't one we should filter (according 19173955d011SMarcel Moolenaar * to the shell description), we print the line, preceded 19183955d011SMarcel Moolenaar * by a target banner if this target isn't the same as the 19193955d011SMarcel Moolenaar * one for which we last printed something. 19203955d011SMarcel Moolenaar * The rest of the data in the buffer are then shifted down 19213955d011SMarcel Moolenaar * to the start of the buffer and curPos is set accordingly. 19223955d011SMarcel Moolenaar */ 19233955d011SMarcel Moolenaar job->outBuf[i] = '\0'; 19243955d011SMarcel Moolenaar if (i >= job->curPos) { 19253955d011SMarcel Moolenaar char *cp; 19263955d011SMarcel Moolenaar 1927b0c40a00SSimon J. Gerraty /* 1928b0c40a00SSimon J. Gerraty * FIXME: SwitchOutputTo should be here, according to 1929b0c40a00SSimon J. Gerraty * the comment above. But since PrintOutput does not 1930b0c40a00SSimon J. Gerraty * do anything in the default shell, this bug has gone 1931b0c40a00SSimon J. Gerraty * unnoticed until now. 1932b0c40a00SSimon J. Gerraty */ 1933b0c40a00SSimon J. Gerraty cp = PrintFilteredOutput(job->outBuf, &job->outBuf[i]); 19343955d011SMarcel Moolenaar 19353955d011SMarcel Moolenaar /* 1936b0c40a00SSimon J. Gerraty * There's still more in the output buffer. This time, 193706b9b3e0SSimon J. Gerraty * though, we know there's no newline at the end, so 193806b9b3e0SSimon J. Gerraty * we add one of our own free will. 19393955d011SMarcel Moolenaar */ 19403955d011SMarcel Moolenaar if (*cp != '\0') { 194106b9b3e0SSimon J. Gerraty if (!opts.beSilent) 194206b9b3e0SSimon J. Gerraty SwitchOutputTo(job->node); 19433955d011SMarcel Moolenaar #ifdef USE_META 19443955d011SMarcel Moolenaar if (useMeta) { 194506b9b3e0SSimon J. Gerraty meta_job_output(job, cp, 194606b9b3e0SSimon J. Gerraty gotNL ? "\n" : ""); 19473955d011SMarcel Moolenaar } 19483955d011SMarcel Moolenaar #endif 194906b9b3e0SSimon J. Gerraty (void)fprintf(stdout, "%s%s", cp, 195006b9b3e0SSimon J. Gerraty gotNL ? "\n" : ""); 19513955d011SMarcel Moolenaar (void)fflush(stdout); 19523955d011SMarcel Moolenaar } 19533955d011SMarcel Moolenaar } 19543955d011SMarcel Moolenaar /* 195506b9b3e0SSimon J. Gerraty * max is the last offset still in the buffer. Move any 195606b9b3e0SSimon J. Gerraty * remaining characters to the start of the buffer and 195706b9b3e0SSimon J. Gerraty * update the end marker curPos. 19583955d011SMarcel Moolenaar */ 1959db29cad8SSimon J. Gerraty if (i < max) { 196006b9b3e0SSimon J. Gerraty (void)memmove(job->outBuf, &job->outBuf[i + 1], 196106b9b3e0SSimon J. Gerraty max - (i + 1)); 1962db29cad8SSimon J. Gerraty job->curPos = max - (i + 1); 1963db29cad8SSimon J. Gerraty } else { 1964db29cad8SSimon J. Gerraty assert(i == max); 19653955d011SMarcel Moolenaar job->curPos = 0; 19663955d011SMarcel Moolenaar } 19673955d011SMarcel Moolenaar } 19683955d011SMarcel Moolenaar if (finish) { 19693955d011SMarcel Moolenaar /* 19703955d011SMarcel Moolenaar * If the finish flag is true, we must loop until we hit 19713955d011SMarcel Moolenaar * end-of-file on the pipe. This is guaranteed to happen 19723955d011SMarcel Moolenaar * eventually since the other end of the pipe is now closed 19733955d011SMarcel Moolenaar * (we closed it explicitly and the child has exited). When 1974b0c40a00SSimon J. Gerraty * we do get an EOF, finish will be set false and we'll fall 19753955d011SMarcel Moolenaar * through and out. 19763955d011SMarcel Moolenaar */ 1977e2eeea75SSimon J. Gerraty goto again; 19783955d011SMarcel Moolenaar } 19793955d011SMarcel Moolenaar } 19803955d011SMarcel Moolenaar 19813955d011SMarcel Moolenaar static void 19823955d011SMarcel Moolenaar JobRun(GNode *targ) 19833955d011SMarcel Moolenaar { 1984956e45f6SSimon J. Gerraty #if 0 19853955d011SMarcel Moolenaar /* 1986956e45f6SSimon J. Gerraty * Unfortunately it is too complicated to run .BEGIN, .END, and 1987956e45f6SSimon J. Gerraty * .INTERRUPT job in the parallel job module. As of 2020-09-25, 1988956e45f6SSimon J. Gerraty * unit-tests/deptgt-end-jobs.mk hangs in an endless loop. 1989956e45f6SSimon J. Gerraty * 1990956e45f6SSimon J. Gerraty * Running these jobs in compat mode also guarantees that these 1991956e45f6SSimon J. Gerraty * jobs do not overlap with other unrelated jobs. 19923955d011SMarcel Moolenaar */ 1993dba7b0efSSimon J. Gerraty GNodeList lst = LST_INIT; 1994dba7b0efSSimon J. Gerraty Lst_Append(&lst, targ); 1995dba7b0efSSimon J. Gerraty (void)Make_Run(&lst); 1996dba7b0efSSimon J. Gerraty Lst_Done(&lst); 1997b0c40a00SSimon J. Gerraty JobStart(targ, true); 199806b9b3e0SSimon J. Gerraty while (jobTokensRunning != 0) { 19993955d011SMarcel Moolenaar Job_CatchOutput(); 20003955d011SMarcel Moolenaar } 20013955d011SMarcel Moolenaar #else 20023955d011SMarcel Moolenaar Compat_Make(targ, targ); 200306b9b3e0SSimon J. Gerraty /* XXX: Replace with GNode_IsError(gn) */ 20043955d011SMarcel Moolenaar if (targ->made == ERROR) { 20053955d011SMarcel Moolenaar PrintOnError(targ, "\n\nStop."); 20063955d011SMarcel Moolenaar exit(1); 20073955d011SMarcel Moolenaar } 20083955d011SMarcel Moolenaar #endif 20093955d011SMarcel Moolenaar } 20103955d011SMarcel Moolenaar 201106b9b3e0SSimon J. Gerraty /* 201206b9b3e0SSimon J. Gerraty * Handle the exit of a child. Called from Make_Make. 20133955d011SMarcel Moolenaar * 20143955d011SMarcel Moolenaar * The job descriptor is removed from the list of children. 20153955d011SMarcel Moolenaar * 20163955d011SMarcel Moolenaar * Notes: 20173955d011SMarcel Moolenaar * We do waits, blocking or not, according to the wisdom of our 20183955d011SMarcel Moolenaar * caller, until there are no more children to report. For each 20193955d011SMarcel Moolenaar * job, call JobFinish to finish things off. 20203955d011SMarcel Moolenaar */ 20213955d011SMarcel Moolenaar void 20223955d011SMarcel Moolenaar Job_CatchChildren(void) 20233955d011SMarcel Moolenaar { 20243955d011SMarcel Moolenaar int pid; /* pid of dead child */ 20253955d011SMarcel Moolenaar WAIT_T status; /* Exit/termination status */ 20263955d011SMarcel Moolenaar 202706b9b3e0SSimon J. Gerraty /* Don't even bother if we know there's no one around. */ 20283955d011SMarcel Moolenaar if (jobTokensRunning == 0) 20293955d011SMarcel Moolenaar return; 20303955d011SMarcel Moolenaar 2031dba7b0efSSimon J. Gerraty /* Have we received SIGCHLD since last call? */ 2032dba7b0efSSimon J. Gerraty if (caught_sigchld == 0) 2033dba7b0efSSimon J. Gerraty return; 2034dba7b0efSSimon J. Gerraty caught_sigchld = 0; 2035dba7b0efSSimon J. Gerraty 20363955d011SMarcel Moolenaar while ((pid = waitpid((pid_t)-1, &status, WNOHANG | WUNTRACED)) > 0) { 203706b9b3e0SSimon J. Gerraty DEBUG2(JOB, "Process %d exited/stopped status %x.\n", 203806b9b3e0SSimon J. Gerraty pid, WAIT_STATUS(status)); 2039b0c40a00SSimon J. Gerraty JobReapChild(pid, status, true); 20403955d011SMarcel Moolenaar } 20413955d011SMarcel Moolenaar } 20423955d011SMarcel Moolenaar 20433955d011SMarcel Moolenaar /* 20443955d011SMarcel Moolenaar * It is possible that wait[pid]() was called from elsewhere, 20453955d011SMarcel Moolenaar * this lets us reap jobs regardless. 20463955d011SMarcel Moolenaar */ 20473955d011SMarcel Moolenaar void 2048b0c40a00SSimon J. Gerraty JobReapChild(pid_t pid, WAIT_T status, bool isJobs) 20493955d011SMarcel Moolenaar { 20503955d011SMarcel Moolenaar Job *job; /* job descriptor for dead child */ 20513955d011SMarcel Moolenaar 205206b9b3e0SSimon J. Gerraty /* Don't even bother if we know there's no one around. */ 20533955d011SMarcel Moolenaar if (jobTokensRunning == 0) 20543955d011SMarcel Moolenaar return; 20553955d011SMarcel Moolenaar 20563955d011SMarcel Moolenaar job = JobFindPid(pid, JOB_ST_RUNNING, isJobs); 20573955d011SMarcel Moolenaar if (job == NULL) { 20583955d011SMarcel Moolenaar if (isJobs) { 20593955d011SMarcel Moolenaar if (!lurking_children) 206006b9b3e0SSimon J. Gerraty Error("Child (%d) status %x not in table?", 206106b9b3e0SSimon J. Gerraty pid, status); 20623955d011SMarcel Moolenaar } 20633955d011SMarcel Moolenaar return; /* not ours */ 20643955d011SMarcel Moolenaar } 20653955d011SMarcel Moolenaar if (WIFSTOPPED(status)) { 206606b9b3e0SSimon J. Gerraty DEBUG2(JOB, "Process %d (%s) stopped.\n", 206706b9b3e0SSimon J. Gerraty job->pid, job->node->name); 20683955d011SMarcel Moolenaar if (!make_suspended) { 20693955d011SMarcel Moolenaar switch (WSTOPSIG(status)) { 20703955d011SMarcel Moolenaar case SIGTSTP: 207106b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Suspended\n", 207206b9b3e0SSimon J. Gerraty job->node->name); 20733955d011SMarcel Moolenaar break; 20743955d011SMarcel Moolenaar case SIGSTOP: 207506b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Stopped\n", 207606b9b3e0SSimon J. Gerraty job->node->name); 20773955d011SMarcel Moolenaar break; 20783955d011SMarcel Moolenaar default: 20793955d011SMarcel Moolenaar (void)printf("*** [%s] Stopped -- signal %d\n", 20803955d011SMarcel Moolenaar job->node->name, WSTOPSIG(status)); 20813955d011SMarcel Moolenaar } 2082b0c40a00SSimon J. Gerraty job->suspended = true; 20833955d011SMarcel Moolenaar } 20843955d011SMarcel Moolenaar (void)fflush(stdout); 20853955d011SMarcel Moolenaar return; 20863955d011SMarcel Moolenaar } 20873955d011SMarcel Moolenaar 2088e2eeea75SSimon J. Gerraty job->status = JOB_ST_FINISHED; 20893955d011SMarcel Moolenaar job->exit_status = WAIT_STATUS(status); 20903955d011SMarcel Moolenaar 20913955d011SMarcel Moolenaar JobFinish(job, status); 20923955d011SMarcel Moolenaar } 20933955d011SMarcel Moolenaar 209406b9b3e0SSimon J. Gerraty /* 209506b9b3e0SSimon J. Gerraty * Catch the output from our children, if we're using pipes do so. Otherwise 2096956e45f6SSimon J. Gerraty * just block time until we get a signal(most likely a SIGCHLD) since there's 2097956e45f6SSimon J. Gerraty * no point in just spinning when there's nothing to do and the reaping of a 209806b9b3e0SSimon J. Gerraty * child can wait for a while. 209906b9b3e0SSimon J. Gerraty */ 21003955d011SMarcel Moolenaar void 21013955d011SMarcel Moolenaar Job_CatchOutput(void) 21023955d011SMarcel Moolenaar { 21033955d011SMarcel Moolenaar int nready; 21043955d011SMarcel Moolenaar Job *job; 2105956e45f6SSimon J. Gerraty unsigned int i; 21063955d011SMarcel Moolenaar 21073955d011SMarcel Moolenaar (void)fflush(stdout); 21083955d011SMarcel Moolenaar 21093955d011SMarcel Moolenaar /* The first fd in the list is the job token pipe */ 21101748de26SSimon J. Gerraty do { 2111dba7b0efSSimon J. Gerraty nready = poll(fds + 1 - wantToken, fdsLen - 1 + wantToken, 211206b9b3e0SSimon J. Gerraty POLL_MSEC); 21131748de26SSimon J. Gerraty } while (nready < 0 && errno == EINTR); 21143955d011SMarcel Moolenaar 21151748de26SSimon J. Gerraty if (nready < 0) 21161748de26SSimon J. Gerraty Punt("poll: %s", strerror(errno)); 21171748de26SSimon J. Gerraty 21181748de26SSimon J. Gerraty if (nready > 0 && readyfd(&childExitJob)) { 21193955d011SMarcel Moolenaar char token = 0; 21201748de26SSimon J. Gerraty ssize_t count; 21211748de26SSimon J. Gerraty count = read(childExitJob.inPipe, &token, 1); 2122dba7b0efSSimon J. Gerraty if (count == 1) { 21233955d011SMarcel Moolenaar if (token == DO_JOB_RESUME[0]) 212406b9b3e0SSimon J. Gerraty /* 212506b9b3e0SSimon J. Gerraty * Complete relay requested from our SIGCONT 212606b9b3e0SSimon J. Gerraty * handler 212706b9b3e0SSimon J. Gerraty */ 21283955d011SMarcel Moolenaar JobRestartJobs(); 2129dba7b0efSSimon J. Gerraty } else if (count == 0) 2130dba7b0efSSimon J. Gerraty Punt("unexpected eof on token pipe"); 2131dba7b0efSSimon J. Gerraty else 2132dba7b0efSSimon J. Gerraty Punt("token pipe read: %s", strerror(errno)); 2133e2eeea75SSimon J. Gerraty nready--; 21343955d011SMarcel Moolenaar } 21353955d011SMarcel Moolenaar 21361748de26SSimon J. Gerraty Job_CatchChildren(); 21371748de26SSimon J. Gerraty if (nready == 0) 21383955d011SMarcel Moolenaar return; 21393955d011SMarcel Moolenaar 2140dba7b0efSSimon J. Gerraty for (i = npseudojobs * nfds_per_job(); i < fdsLen; i++) { 214106b9b3e0SSimon J. Gerraty if (fds[i].revents == 0) 21423955d011SMarcel Moolenaar continue; 2143dba7b0efSSimon J. Gerraty job = jobByFdIndex[i]; 2144e2eeea75SSimon J. Gerraty if (job->status == JOB_ST_RUNNING) 2145b0c40a00SSimon J. Gerraty CollectOutput(job, false); 214649caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 214749caa483SSimon J. Gerraty /* 214849caa483SSimon J. Gerraty * With meta mode, we may have activity on the job's filemon 214906b9b3e0SSimon J. Gerraty * descriptor too, which at the moment is any pollfd other 215006b9b3e0SSimon J. Gerraty * than job->inPollfd. 215149caa483SSimon J. Gerraty */ 215249caa483SSimon J. Gerraty if (useMeta && job->inPollfd != &fds[i]) { 215349caa483SSimon J. Gerraty if (meta_job_event(job) <= 0) { 215449caa483SSimon J. Gerraty fds[i].events = 0; /* never mind */ 215549caa483SSimon J. Gerraty } 215649caa483SSimon J. Gerraty } 215749caa483SSimon J. Gerraty #endif 21581748de26SSimon J. Gerraty if (--nready == 0) 21591748de26SSimon J. Gerraty return; 21603955d011SMarcel Moolenaar } 21613955d011SMarcel Moolenaar } 21623955d011SMarcel Moolenaar 216306b9b3e0SSimon J. Gerraty /* 216406b9b3e0SSimon J. Gerraty * Start the creation of a target. Basically a front-end for JobStart used by 216506b9b3e0SSimon J. Gerraty * the Make module. 216606b9b3e0SSimon J. Gerraty */ 21673955d011SMarcel Moolenaar void 21683955d011SMarcel Moolenaar Job_Make(GNode *gn) 21693955d011SMarcel Moolenaar { 2170b0c40a00SSimon J. Gerraty (void)JobStart(gn, false); 217106b9b3e0SSimon J. Gerraty } 217206b9b3e0SSimon J. Gerraty 217306b9b3e0SSimon J. Gerraty static void 217406b9b3e0SSimon J. Gerraty InitShellNameAndPath(void) 217506b9b3e0SSimon J. Gerraty { 217606b9b3e0SSimon J. Gerraty shellName = shell->name; 217706b9b3e0SSimon J. Gerraty 217806b9b3e0SSimon J. Gerraty #ifdef DEFSHELL_CUSTOM 217906b9b3e0SSimon J. Gerraty if (shellName[0] == '/') { 218006b9b3e0SSimon J. Gerraty shellPath = shellName; 218106b9b3e0SSimon J. Gerraty shellName = str_basename(shellPath); 218206b9b3e0SSimon J. Gerraty return; 218306b9b3e0SSimon J. Gerraty } 218406b9b3e0SSimon J. Gerraty #endif 218506b9b3e0SSimon J. Gerraty 218606b9b3e0SSimon J. Gerraty shellPath = str_concat3(_PATH_DEFSHELLDIR, "/", shellName); 21873955d011SMarcel Moolenaar } 21883955d011SMarcel Moolenaar 21893955d011SMarcel Moolenaar void 21903955d011SMarcel Moolenaar Shell_Init(void) 21913955d011SMarcel Moolenaar { 219206b9b3e0SSimon J. Gerraty if (shellPath == NULL) 219306b9b3e0SSimon J. Gerraty InitShellNameAndPath(); 219406b9b3e0SSimon J. Gerraty 2195dba7b0efSSimon J. Gerraty Var_SetWithFlags(SCOPE_CMDLINE, ".SHELL", shellPath, VAR_SET_READONLY); 219606b9b3e0SSimon J. Gerraty if (shell->errFlag == NULL) 219706b9b3e0SSimon J. Gerraty shell->errFlag = ""; 219806b9b3e0SSimon J. Gerraty if (shell->echoFlag == NULL) 219906b9b3e0SSimon J. Gerraty shell->echoFlag = ""; 220006b9b3e0SSimon J. Gerraty if (shell->hasErrCtl && shell->errFlag[0] != '\0') { 220106b9b3e0SSimon J. Gerraty if (shellErrFlag != NULL && 220206b9b3e0SSimon J. Gerraty strcmp(shell->errFlag, &shellErrFlag[1]) != 0) { 220351ee2c1cSSimon J. Gerraty free(shellErrFlag); 220451ee2c1cSSimon J. Gerraty shellErrFlag = NULL; 220551ee2c1cSSimon J. Gerraty } 220606b9b3e0SSimon J. Gerraty if (shellErrFlag == NULL) { 220706b9b3e0SSimon J. Gerraty size_t n = strlen(shell->errFlag) + 2; 220851ee2c1cSSimon J. Gerraty 220951ee2c1cSSimon J. Gerraty shellErrFlag = bmake_malloc(n); 221006b9b3e0SSimon J. Gerraty if (shellErrFlag != NULL) 221106b9b3e0SSimon J. Gerraty snprintf(shellErrFlag, n, "-%s", 221206b9b3e0SSimon J. Gerraty shell->errFlag); 221351ee2c1cSSimon J. Gerraty } 221406b9b3e0SSimon J. Gerraty } else if (shellErrFlag != NULL) { 221551ee2c1cSSimon J. Gerraty free(shellErrFlag); 221651ee2c1cSSimon J. Gerraty shellErrFlag = NULL; 221751ee2c1cSSimon J. Gerraty } 22183955d011SMarcel Moolenaar } 22193955d011SMarcel Moolenaar 222006b9b3e0SSimon J. Gerraty /* 222106b9b3e0SSimon J. Gerraty * Return the string literal that is used in the current command shell 222206b9b3e0SSimon J. Gerraty * to produce a newline character. 222306b9b3e0SSimon J. Gerraty */ 22243955d011SMarcel Moolenaar const char * 22253955d011SMarcel Moolenaar Shell_GetNewline(void) 22263955d011SMarcel Moolenaar { 222706b9b3e0SSimon J. Gerraty return shell->newline; 22283955d011SMarcel Moolenaar } 22293955d011SMarcel Moolenaar 22303955d011SMarcel Moolenaar void 22313955d011SMarcel Moolenaar Job_SetPrefix(void) 22323955d011SMarcel Moolenaar { 223306b9b3e0SSimon J. Gerraty if (targPrefix != NULL) { 22343955d011SMarcel Moolenaar free(targPrefix); 2235dba7b0efSSimon J. Gerraty } else if (!Var_Exists(SCOPE_GLOBAL, MAKE_JOB_PREFIX)) { 2236dba7b0efSSimon J. Gerraty Global_Set(MAKE_JOB_PREFIX, "---"); 22373955d011SMarcel Moolenaar } 22383955d011SMarcel Moolenaar 2239956e45f6SSimon J. Gerraty (void)Var_Subst("${" MAKE_JOB_PREFIX "}", 2240dba7b0efSSimon J. Gerraty SCOPE_GLOBAL, VARE_WANTRES, &targPrefix); 2241956e45f6SSimon J. Gerraty /* TODO: handle errors */ 22423955d011SMarcel Moolenaar } 22433955d011SMarcel Moolenaar 224406b9b3e0SSimon J. Gerraty static void 224506b9b3e0SSimon J. Gerraty AddSig(int sig, SignalProc handler) 224606b9b3e0SSimon J. Gerraty { 224706b9b3e0SSimon J. Gerraty if (bmake_signal(sig, SIG_IGN) != SIG_IGN) { 224806b9b3e0SSimon J. Gerraty sigaddset(&caught_signals, sig); 224906b9b3e0SSimon J. Gerraty (void)bmake_signal(sig, handler); 225006b9b3e0SSimon J. Gerraty } 225106b9b3e0SSimon J. Gerraty } 225206b9b3e0SSimon J. Gerraty 22532c3632d1SSimon J. Gerraty /* Initialize the process module. */ 22543955d011SMarcel Moolenaar void 22553955d011SMarcel Moolenaar Job_Init(void) 22563955d011SMarcel Moolenaar { 2257d191243dSSimon J. Gerraty Job_SetPrefix(); 22583955d011SMarcel Moolenaar /* Allocate space for all the job info */ 2259956e45f6SSimon J. Gerraty job_table = bmake_malloc((size_t)opts.maxJobs * sizeof *job_table); 2260956e45f6SSimon J. Gerraty memset(job_table, 0, (size_t)opts.maxJobs * sizeof *job_table); 2261956e45f6SSimon J. Gerraty job_table_end = job_table + opts.maxJobs; 22623955d011SMarcel Moolenaar wantToken = 0; 2263dba7b0efSSimon J. Gerraty caught_sigchld = 0; 22643955d011SMarcel Moolenaar 2265956e45f6SSimon J. Gerraty aborting = ABORT_NONE; 226606b9b3e0SSimon J. Gerraty job_errors = 0; 22673955d011SMarcel Moolenaar 2268*68c4481aSSimon J. Gerraty Always_pass_job_queue = GetBooleanExpr(MAKE_ALWAYS_PASS_JOB_QUEUE, 22699a4bc556SSimon J. Gerraty Always_pass_job_queue); 22709a4bc556SSimon J. Gerraty 2271*68c4481aSSimon J. Gerraty Job_error_token = GetBooleanExpr(MAKE_JOB_ERROR_TOKEN, Job_error_token); 2272*68c4481aSSimon J. Gerraty 22732d395cb5SSimon J. Gerraty 22743955d011SMarcel Moolenaar /* 22753955d011SMarcel Moolenaar * There is a non-zero chance that we already have children. 22763955d011SMarcel Moolenaar * eg after 'make -f- <<EOF' 227706b9b3e0SSimon J. Gerraty * Since their termination causes a 'Child (pid) not in table' 227806b9b3e0SSimon J. Gerraty * message, Collect the status of any that are already dead, and 227906b9b3e0SSimon J. Gerraty * suppress the error message if there are any undead ones. 22803955d011SMarcel Moolenaar */ 22813955d011SMarcel Moolenaar for (;;) { 228206b9b3e0SSimon J. Gerraty int rval; 228306b9b3e0SSimon J. Gerraty WAIT_T status; 228406b9b3e0SSimon J. Gerraty 22853955d011SMarcel Moolenaar rval = waitpid((pid_t)-1, &status, WNOHANG); 22863955d011SMarcel Moolenaar if (rval > 0) 22873955d011SMarcel Moolenaar continue; 22883955d011SMarcel Moolenaar if (rval == 0) 2289b0c40a00SSimon J. Gerraty lurking_children = true; 22903955d011SMarcel Moolenaar break; 22913955d011SMarcel Moolenaar } 22923955d011SMarcel Moolenaar 22933955d011SMarcel Moolenaar Shell_Init(); 22943955d011SMarcel Moolenaar 22953955d011SMarcel Moolenaar JobCreatePipe(&childExitJob, 3); 22963955d011SMarcel Moolenaar 2297dba7b0efSSimon J. Gerraty { 229849caa483SSimon J. Gerraty /* Preallocate enough for the maximum number of jobs. */ 2299dba7b0efSSimon J. Gerraty size_t nfds = (npseudojobs + (size_t)opts.maxJobs) * 2300dba7b0efSSimon J. Gerraty nfds_per_job(); 2301dba7b0efSSimon J. Gerraty fds = bmake_malloc(sizeof *fds * nfds); 2302dba7b0efSSimon J. Gerraty jobByFdIndex = bmake_malloc(sizeof *jobByFdIndex * nfds); 2303dba7b0efSSimon J. Gerraty } 23043955d011SMarcel Moolenaar 23053955d011SMarcel Moolenaar /* These are permanent entries and take slots 0 and 1 */ 23063955d011SMarcel Moolenaar watchfd(&tokenWaitJob); 23073955d011SMarcel Moolenaar watchfd(&childExitJob); 23083955d011SMarcel Moolenaar 23093955d011SMarcel Moolenaar sigemptyset(&caught_signals); 23103955d011SMarcel Moolenaar /* 23113955d011SMarcel Moolenaar * Install a SIGCHLD handler. 23123955d011SMarcel Moolenaar */ 23133955d011SMarcel Moolenaar (void)bmake_signal(SIGCHLD, JobChildSig); 23143955d011SMarcel Moolenaar sigaddset(&caught_signals, SIGCHLD); 23153955d011SMarcel Moolenaar 23163955d011SMarcel Moolenaar /* 23173955d011SMarcel Moolenaar * Catch the four signals that POSIX specifies if they aren't ignored. 23183955d011SMarcel Moolenaar * JobPassSig will take care of calling JobInterrupt if appropriate. 23193955d011SMarcel Moolenaar */ 232006b9b3e0SSimon J. Gerraty AddSig(SIGINT, JobPassSig_int); 232106b9b3e0SSimon J. Gerraty AddSig(SIGHUP, JobPassSig_term); 232206b9b3e0SSimon J. Gerraty AddSig(SIGTERM, JobPassSig_term); 232306b9b3e0SSimon J. Gerraty AddSig(SIGQUIT, JobPassSig_term); 23243955d011SMarcel Moolenaar 23253955d011SMarcel Moolenaar /* 23263955d011SMarcel Moolenaar * There are additional signals that need to be caught and passed if 23273955d011SMarcel Moolenaar * either the export system wants to be told directly of signals or if 23283955d011SMarcel Moolenaar * we're giving each job its own process group (since then it won't get 23293955d011SMarcel Moolenaar * signals from the terminal driver as we own the terminal) 23303955d011SMarcel Moolenaar */ 233106b9b3e0SSimon J. Gerraty AddSig(SIGTSTP, JobPassSig_suspend); 233206b9b3e0SSimon J. Gerraty AddSig(SIGTTOU, JobPassSig_suspend); 233306b9b3e0SSimon J. Gerraty AddSig(SIGTTIN, JobPassSig_suspend); 233406b9b3e0SSimon J. Gerraty AddSig(SIGWINCH, JobCondPassSig); 233506b9b3e0SSimon J. Gerraty AddSig(SIGCONT, JobContinueSig); 23363955d011SMarcel Moolenaar 23371748de26SSimon J. Gerraty (void)Job_RunTarget(".BEGIN", NULL); 2338956e45f6SSimon J. Gerraty /* Create the .END node now, even though no code in the unit tests 2339956e45f6SSimon J. Gerraty * depends on it. See also Targ_GetEndNode in Compat_Run. */ 2340956e45f6SSimon J. Gerraty (void)Targ_GetEndNode(); 23413955d011SMarcel Moolenaar } 23423955d011SMarcel Moolenaar 234306b9b3e0SSimon J. Gerraty static void 234406b9b3e0SSimon J. Gerraty DelSig(int sig) 23453955d011SMarcel Moolenaar { 234606b9b3e0SSimon J. Gerraty if (sigismember(&caught_signals, sig) != 0) 234706b9b3e0SSimon J. Gerraty (void)bmake_signal(sig, SIG_DFL); 23483955d011SMarcel Moolenaar } 23493955d011SMarcel Moolenaar 235006b9b3e0SSimon J. Gerraty static void 235106b9b3e0SSimon J. Gerraty JobSigReset(void) 235206b9b3e0SSimon J. Gerraty { 235306b9b3e0SSimon J. Gerraty DelSig(SIGINT); 235406b9b3e0SSimon J. Gerraty DelSig(SIGHUP); 235506b9b3e0SSimon J. Gerraty DelSig(SIGQUIT); 235606b9b3e0SSimon J. Gerraty DelSig(SIGTERM); 235706b9b3e0SSimon J. Gerraty DelSig(SIGTSTP); 235806b9b3e0SSimon J. Gerraty DelSig(SIGTTOU); 235906b9b3e0SSimon J. Gerraty DelSig(SIGTTIN); 236006b9b3e0SSimon J. Gerraty DelSig(SIGWINCH); 236106b9b3e0SSimon J. Gerraty DelSig(SIGCONT); 23623955d011SMarcel Moolenaar (void)bmake_signal(SIGCHLD, SIG_DFL); 23633955d011SMarcel Moolenaar } 23643955d011SMarcel Moolenaar 23652c3632d1SSimon J. Gerraty /* Find a shell in 'shells' given its name, or return NULL. */ 23663955d011SMarcel Moolenaar static Shell * 2367956e45f6SSimon J. Gerraty FindShellByName(const char *name) 23683955d011SMarcel Moolenaar { 2369956e45f6SSimon J. Gerraty Shell *sh = shells; 2370956e45f6SSimon J. Gerraty const Shell *shellsEnd = sh + sizeof shells / sizeof shells[0]; 23713955d011SMarcel Moolenaar 2372956e45f6SSimon J. Gerraty for (sh = shells; sh < shellsEnd; sh++) { 23733955d011SMarcel Moolenaar if (strcmp(name, sh->name) == 0) 23743841c287SSimon J. Gerraty return sh; 23753955d011SMarcel Moolenaar } 23763955d011SMarcel Moolenaar return NULL; 23773955d011SMarcel Moolenaar } 23783955d011SMarcel Moolenaar 237906b9b3e0SSimon J. Gerraty /* 238006b9b3e0SSimon J. Gerraty * Parse a shell specification and set up 'shell', shellPath and 238106b9b3e0SSimon J. Gerraty * shellName appropriately. 23823955d011SMarcel Moolenaar * 23833955d011SMarcel Moolenaar * Input: 23843955d011SMarcel Moolenaar * line The shell spec 23853955d011SMarcel Moolenaar * 23863955d011SMarcel Moolenaar * Results: 2387b0c40a00SSimon J. Gerraty * false if the specification was incorrect. 23883955d011SMarcel Moolenaar * 23893955d011SMarcel Moolenaar * Side Effects: 239006b9b3e0SSimon J. Gerraty * 'shell' points to a Shell structure (either predefined or 23913955d011SMarcel Moolenaar * created from the shell spec), shellPath is the full path of the 239206b9b3e0SSimon J. Gerraty * shell described by 'shell', while shellName is just the 23933955d011SMarcel Moolenaar * final component of shellPath. 23943955d011SMarcel Moolenaar * 23953955d011SMarcel Moolenaar * Notes: 23963955d011SMarcel Moolenaar * A shell specification consists of a .SHELL target, with dependency 23973955d011SMarcel Moolenaar * operator, followed by a series of blank-separated words. Double 23983955d011SMarcel Moolenaar * quotes can be used to use blanks in words. A backslash escapes 23993955d011SMarcel Moolenaar * anything (most notably a double-quote and a space) and 24003955d011SMarcel Moolenaar * provides the functionality it does in C. Each word consists of 24013955d011SMarcel Moolenaar * keyword and value separated by an equal sign. There should be no 24023955d011SMarcel Moolenaar * unnecessary spaces in the word. The keywords are as follows: 24033955d011SMarcel Moolenaar * name Name of shell. 24043955d011SMarcel Moolenaar * path Location of shell. 24053955d011SMarcel Moolenaar * quiet Command to turn off echoing. 24063955d011SMarcel Moolenaar * echo Command to turn echoing on 24073955d011SMarcel Moolenaar * filter Result of turning off echoing that shouldn't be 24083955d011SMarcel Moolenaar * printed. 24093955d011SMarcel Moolenaar * echoFlag Flag to turn echoing on at the start 24103955d011SMarcel Moolenaar * errFlag Flag to turn error checking on at the start 24113955d011SMarcel Moolenaar * hasErrCtl True if shell has error checking control 24123955d011SMarcel Moolenaar * newline String literal to represent a newline char 24133955d011SMarcel Moolenaar * check Command to turn on error checking if hasErrCtl 2414b0c40a00SSimon J. Gerraty * is true or template of command to echo a command 24153955d011SMarcel Moolenaar * for which error checking is off if hasErrCtl is 2416b0c40a00SSimon J. Gerraty * false. 24173955d011SMarcel Moolenaar * ignore Command to turn off error checking if hasErrCtl 2418b0c40a00SSimon J. Gerraty * is true or template of command to execute a 24193955d011SMarcel Moolenaar * command so as to ignore any errors it returns if 2420b0c40a00SSimon J. Gerraty * hasErrCtl is false. 24213955d011SMarcel Moolenaar */ 2422b0c40a00SSimon J. Gerraty bool 24233955d011SMarcel Moolenaar Job_ParseShell(char *line) 24243955d011SMarcel Moolenaar { 24252c3632d1SSimon J. Gerraty Words wordsList; 24263955d011SMarcel Moolenaar char **words; 24273955d011SMarcel Moolenaar char **argv; 24282c3632d1SSimon J. Gerraty size_t argc; 24293955d011SMarcel Moolenaar char *path; 24303955d011SMarcel Moolenaar Shell newShell; 2431b0c40a00SSimon J. Gerraty bool fullSpec = false; 24323955d011SMarcel Moolenaar Shell *sh; 24333955d011SMarcel Moolenaar 243406b9b3e0SSimon J. Gerraty /* XXX: don't use line as an iterator variable */ 2435956e45f6SSimon J. Gerraty pp_skip_whitespace(&line); 24363955d011SMarcel Moolenaar 2437dba7b0efSSimon J. Gerraty free(shell_freeIt); 24383955d011SMarcel Moolenaar 2439e2eeea75SSimon J. Gerraty memset(&newShell, 0, sizeof newShell); 24403955d011SMarcel Moolenaar 24413955d011SMarcel Moolenaar /* 24423955d011SMarcel Moolenaar * Parse the specification by keyword 24433955d011SMarcel Moolenaar */ 2444b0c40a00SSimon J. Gerraty wordsList = Str_Words(line, true); 24452c3632d1SSimon J. Gerraty words = wordsList.words; 24462c3632d1SSimon J. Gerraty argc = wordsList.len; 24472c3632d1SSimon J. Gerraty path = wordsList.freeIt; 24483955d011SMarcel Moolenaar if (words == NULL) { 24493955d011SMarcel Moolenaar Error("Unterminated quoted string [%s]", line); 2450b0c40a00SSimon J. Gerraty return false; 24513955d011SMarcel Moolenaar } 2452dba7b0efSSimon J. Gerraty shell_freeIt = path; 24533955d011SMarcel Moolenaar 24543955d011SMarcel Moolenaar for (path = NULL, argv = words; argc != 0; argc--, argv++) { 2455956e45f6SSimon J. Gerraty char *arg = *argv; 2456956e45f6SSimon J. Gerraty if (strncmp(arg, "path=", 5) == 0) { 2457956e45f6SSimon J. Gerraty path = arg + 5; 2458956e45f6SSimon J. Gerraty } else if (strncmp(arg, "name=", 5) == 0) { 2459956e45f6SSimon J. Gerraty newShell.name = arg + 5; 24603955d011SMarcel Moolenaar } else { 2461956e45f6SSimon J. Gerraty if (strncmp(arg, "quiet=", 6) == 0) { 2462956e45f6SSimon J. Gerraty newShell.echoOff = arg + 6; 2463956e45f6SSimon J. Gerraty } else if (strncmp(arg, "echo=", 5) == 0) { 2464956e45f6SSimon J. Gerraty newShell.echoOn = arg + 5; 2465956e45f6SSimon J. Gerraty } else if (strncmp(arg, "filter=", 7) == 0) { 2466956e45f6SSimon J. Gerraty newShell.noPrint = arg + 7; 2467956e45f6SSimon J. Gerraty newShell.noPrintLen = strlen(newShell.noPrint); 2468956e45f6SSimon J. Gerraty } else if (strncmp(arg, "echoFlag=", 9) == 0) { 246906b9b3e0SSimon J. Gerraty newShell.echoFlag = arg + 9; 2470956e45f6SSimon J. Gerraty } else if (strncmp(arg, "errFlag=", 8) == 0) { 247106b9b3e0SSimon J. Gerraty newShell.errFlag = arg + 8; 2472956e45f6SSimon J. Gerraty } else if (strncmp(arg, "hasErrCtl=", 10) == 0) { 2473956e45f6SSimon J. Gerraty char c = arg[10]; 2474956e45f6SSimon J. Gerraty newShell.hasErrCtl = c == 'Y' || c == 'y' || 2475956e45f6SSimon J. Gerraty c == 'T' || c == 't'; 2476956e45f6SSimon J. Gerraty } else if (strncmp(arg, "newline=", 8) == 0) { 2477956e45f6SSimon J. Gerraty newShell.newline = arg + 8; 2478956e45f6SSimon J. Gerraty } else if (strncmp(arg, "check=", 6) == 0) { 247906b9b3e0SSimon J. Gerraty /* Before 2020-12-10, these two variables 248006b9b3e0SSimon J. Gerraty * had been a single variable. */ 248106b9b3e0SSimon J. Gerraty newShell.errOn = arg + 6; 248206b9b3e0SSimon J. Gerraty newShell.echoTmpl = arg + 6; 2483956e45f6SSimon J. Gerraty } else if (strncmp(arg, "ignore=", 7) == 0) { 248406b9b3e0SSimon J. Gerraty /* Before 2020-12-10, these two variables 248506b9b3e0SSimon J. Gerraty * had been a single variable. */ 248606b9b3e0SSimon J. Gerraty newShell.errOff = arg + 7; 248706b9b3e0SSimon J. Gerraty newShell.runIgnTmpl = arg + 7; 2488956e45f6SSimon J. Gerraty } else if (strncmp(arg, "errout=", 7) == 0) { 248906b9b3e0SSimon J. Gerraty newShell.runChkTmpl = arg + 7; 2490956e45f6SSimon J. Gerraty } else if (strncmp(arg, "comment=", 8) == 0) { 2491956e45f6SSimon J. Gerraty newShell.commentChar = arg[8]; 24923955d011SMarcel Moolenaar } else { 249306b9b3e0SSimon J. Gerraty Parse_Error(PARSE_FATAL, 249406b9b3e0SSimon J. Gerraty "Unknown keyword \"%s\"", arg); 24953955d011SMarcel Moolenaar free(words); 2496b0c40a00SSimon J. Gerraty return false; 24973955d011SMarcel Moolenaar } 2498b0c40a00SSimon J. Gerraty fullSpec = true; 24993955d011SMarcel Moolenaar } 25003955d011SMarcel Moolenaar } 25013955d011SMarcel Moolenaar 25023955d011SMarcel Moolenaar if (path == NULL) { 25033955d011SMarcel Moolenaar /* 250406b9b3e0SSimon J. Gerraty * If no path was given, the user wants one of the 250506b9b3e0SSimon J. Gerraty * pre-defined shells, yes? So we find the one s/he wants 250606b9b3e0SSimon J. Gerraty * with the help of FindShellByName and set things up the 250706b9b3e0SSimon J. Gerraty * right way. shellPath will be set up by Shell_Init. 25083955d011SMarcel Moolenaar */ 25093955d011SMarcel Moolenaar if (newShell.name == NULL) { 251006b9b3e0SSimon J. Gerraty Parse_Error(PARSE_FATAL, 251106b9b3e0SSimon J. Gerraty "Neither path nor name specified"); 25123955d011SMarcel Moolenaar free(words); 2513b0c40a00SSimon J. Gerraty return false; 25143955d011SMarcel Moolenaar } else { 2515956e45f6SSimon J. Gerraty if ((sh = FindShellByName(newShell.name)) == NULL) { 251606b9b3e0SSimon J. Gerraty Parse_Error(PARSE_WARNING, 251706b9b3e0SSimon J. Gerraty "%s: No matching shell", newShell.name); 25183955d011SMarcel Moolenaar free(words); 2519b0c40a00SSimon J. Gerraty return false; 25203955d011SMarcel Moolenaar } 252106b9b3e0SSimon J. Gerraty shell = sh; 25223955d011SMarcel Moolenaar shellName = newShell.name; 252306b9b3e0SSimon J. Gerraty if (shellPath != NULL) { 252406b9b3e0SSimon J. Gerraty /* 252506b9b3e0SSimon J. Gerraty * Shell_Init has already been called! 252606b9b3e0SSimon J. Gerraty * Do it again. 252706b9b3e0SSimon J. Gerraty */ 25283955d011SMarcel Moolenaar free(UNCONST(shellPath)); 25293955d011SMarcel Moolenaar shellPath = NULL; 25303955d011SMarcel Moolenaar Shell_Init(); 25313955d011SMarcel Moolenaar } 25323955d011SMarcel Moolenaar } 25333955d011SMarcel Moolenaar } else { 25343955d011SMarcel Moolenaar /* 253506b9b3e0SSimon J. Gerraty * The user provided a path. If s/he gave nothing else 2536b0c40a00SSimon J. Gerraty * (fullSpec is false), try and find a matching shell in the 253706b9b3e0SSimon J. Gerraty * ones we know of. Else we just take the specification at 253806b9b3e0SSimon J. Gerraty * its word and copy it to a new location. In either case, 253906b9b3e0SSimon J. Gerraty * we need to record the path the user gave for the shell. 25403955d011SMarcel Moolenaar */ 25413955d011SMarcel Moolenaar shellPath = path; 25423955d011SMarcel Moolenaar path = strrchr(path, '/'); 25433955d011SMarcel Moolenaar if (path == NULL) { 25443955d011SMarcel Moolenaar path = UNCONST(shellPath); 25453955d011SMarcel Moolenaar } else { 2546956e45f6SSimon J. Gerraty path++; 25473955d011SMarcel Moolenaar } 25483955d011SMarcel Moolenaar if (newShell.name != NULL) { 25493955d011SMarcel Moolenaar shellName = newShell.name; 25503955d011SMarcel Moolenaar } else { 25513955d011SMarcel Moolenaar shellName = path; 25523955d011SMarcel Moolenaar } 25533955d011SMarcel Moolenaar if (!fullSpec) { 2554956e45f6SSimon J. Gerraty if ((sh = FindShellByName(shellName)) == NULL) { 255506b9b3e0SSimon J. Gerraty Parse_Error(PARSE_WARNING, 255606b9b3e0SSimon J. Gerraty "%s: No matching shell", shellName); 25573955d011SMarcel Moolenaar free(words); 2558b0c40a00SSimon J. Gerraty return false; 25593955d011SMarcel Moolenaar } 256006b9b3e0SSimon J. Gerraty shell = sh; 25613955d011SMarcel Moolenaar } else { 256206b9b3e0SSimon J. Gerraty shell = bmake_malloc(sizeof *shell); 256306b9b3e0SSimon J. Gerraty *shell = newShell; 25643955d011SMarcel Moolenaar } 256551ee2c1cSSimon J. Gerraty /* this will take care of shellErrFlag */ 256651ee2c1cSSimon J. Gerraty Shell_Init(); 25673955d011SMarcel Moolenaar } 25683955d011SMarcel Moolenaar 256906b9b3e0SSimon J. Gerraty if (shell->echoOn != NULL && shell->echoOff != NULL) 2570b0c40a00SSimon J. Gerraty shell->hasEchoCtl = true; 25713955d011SMarcel Moolenaar 257206b9b3e0SSimon J. Gerraty if (!shell->hasErrCtl) { 257306b9b3e0SSimon J. Gerraty if (shell->echoTmpl == NULL) 257406b9b3e0SSimon J. Gerraty shell->echoTmpl = ""; 257506b9b3e0SSimon J. Gerraty if (shell->runIgnTmpl == NULL) 257606b9b3e0SSimon J. Gerraty shell->runIgnTmpl = "%s\n"; 25773955d011SMarcel Moolenaar } 25783955d011SMarcel Moolenaar 25793955d011SMarcel Moolenaar /* 258006b9b3e0SSimon J. Gerraty * Do not free up the words themselves, since they might be in use 258106b9b3e0SSimon J. Gerraty * by the shell specification. 25823955d011SMarcel Moolenaar */ 25833955d011SMarcel Moolenaar free(words); 2584b0c40a00SSimon J. Gerraty return true; 25853955d011SMarcel Moolenaar } 25863955d011SMarcel Moolenaar 258706b9b3e0SSimon J. Gerraty /* 258806b9b3e0SSimon J. Gerraty * Handle the receipt of an interrupt. 2589956e45f6SSimon J. Gerraty * 2590956e45f6SSimon J. Gerraty * All children are killed. Another job will be started if the .INTERRUPT 2591956e45f6SSimon J. Gerraty * target is defined. 25923955d011SMarcel Moolenaar * 25933955d011SMarcel Moolenaar * Input: 25943955d011SMarcel Moolenaar * runINTERRUPT Non-zero if commands for the .INTERRUPT target 25953955d011SMarcel Moolenaar * should be executed 25963955d011SMarcel Moolenaar * signo signal received 25973955d011SMarcel Moolenaar */ 25983955d011SMarcel Moolenaar static void 2599b0c40a00SSimon J. Gerraty JobInterrupt(bool runINTERRUPT, int signo) 26003955d011SMarcel Moolenaar { 26013955d011SMarcel Moolenaar Job *job; /* job descriptor in that element */ 26023955d011SMarcel Moolenaar GNode *interrupt; /* the node describing the .INTERRUPT target */ 26033955d011SMarcel Moolenaar sigset_t mask; 26043955d011SMarcel Moolenaar GNode *gn; 26053955d011SMarcel Moolenaar 26063955d011SMarcel Moolenaar aborting = ABORT_INTERRUPT; 26073955d011SMarcel Moolenaar 26083955d011SMarcel Moolenaar JobSigLock(&mask); 26093955d011SMarcel Moolenaar 26103955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 2611e2eeea75SSimon J. Gerraty if (job->status != JOB_ST_RUNNING) 26123955d011SMarcel Moolenaar continue; 26133955d011SMarcel Moolenaar 26143955d011SMarcel Moolenaar gn = job->node; 26153955d011SMarcel Moolenaar 261645447996SSimon J. Gerraty JobDeleteTarget(gn); 261706b9b3e0SSimon J. Gerraty if (job->pid != 0) { 261806b9b3e0SSimon J. Gerraty DEBUG2(JOB, 261906b9b3e0SSimon J. Gerraty "JobInterrupt passing signal %d to child %d.\n", 26203955d011SMarcel Moolenaar signo, job->pid); 26213955d011SMarcel Moolenaar KILLPG(job->pid, signo); 26223955d011SMarcel Moolenaar } 26233955d011SMarcel Moolenaar } 26243955d011SMarcel Moolenaar 26253955d011SMarcel Moolenaar JobSigUnlock(&mask); 26263955d011SMarcel Moolenaar 2627956e45f6SSimon J. Gerraty if (runINTERRUPT && !opts.touchFlag) { 2628956e45f6SSimon J. Gerraty interrupt = Targ_FindNode(".INTERRUPT"); 26293955d011SMarcel Moolenaar if (interrupt != NULL) { 2630b0c40a00SSimon J. Gerraty opts.ignoreErrors = false; 26313955d011SMarcel Moolenaar JobRun(interrupt); 26323955d011SMarcel Moolenaar } 26333955d011SMarcel Moolenaar } 2634e2eeea75SSimon J. Gerraty Trace_Log(MAKEINTR, NULL); 263506b9b3e0SSimon J. Gerraty exit(signo); /* XXX: why signo? */ 26363955d011SMarcel Moolenaar } 26373955d011SMarcel Moolenaar 263806b9b3e0SSimon J. Gerraty /* 263906b9b3e0SSimon J. Gerraty * Do the final processing, i.e. run the commands attached to the .END target. 26403955d011SMarcel Moolenaar * 264106b9b3e0SSimon J. Gerraty * Return the number of errors reported. 264206b9b3e0SSimon J. Gerraty */ 26433955d011SMarcel Moolenaar int 26443955d011SMarcel Moolenaar Job_Finish(void) 26453955d011SMarcel Moolenaar { 2646956e45f6SSimon J. Gerraty GNode *endNode = Targ_GetEndNode(); 264706b9b3e0SSimon J. Gerraty if (!Lst_IsEmpty(&endNode->commands) || 264806b9b3e0SSimon J. Gerraty !Lst_IsEmpty(&endNode->children)) { 264906b9b3e0SSimon J. Gerraty if (job_errors != 0) { 26503955d011SMarcel Moolenaar Error("Errors reported so .END ignored"); 26513955d011SMarcel Moolenaar } else { 2652956e45f6SSimon J. Gerraty JobRun(endNode); 26533955d011SMarcel Moolenaar } 26543955d011SMarcel Moolenaar } 265506b9b3e0SSimon J. Gerraty return job_errors; 26563955d011SMarcel Moolenaar } 26573955d011SMarcel Moolenaar 2658956e45f6SSimon J. Gerraty /* Clean up any memory used by the jobs module. */ 26593955d011SMarcel Moolenaar void 26603955d011SMarcel Moolenaar Job_End(void) 26613955d011SMarcel Moolenaar { 26623955d011SMarcel Moolenaar #ifdef CLEANUP 2663dba7b0efSSimon J. Gerraty free(shell_freeIt); 26643955d011SMarcel Moolenaar #endif 26653955d011SMarcel Moolenaar } 26663955d011SMarcel Moolenaar 266706b9b3e0SSimon J. Gerraty /* 266806b9b3e0SSimon J. Gerraty * Waits for all running jobs to finish and returns. 266906b9b3e0SSimon J. Gerraty * Sets 'aborting' to ABORT_WAIT to prevent other jobs from starting. 267006b9b3e0SSimon J. Gerraty */ 26713955d011SMarcel Moolenaar void 26723955d011SMarcel Moolenaar Job_Wait(void) 26733955d011SMarcel Moolenaar { 26743955d011SMarcel Moolenaar aborting = ABORT_WAIT; 26753955d011SMarcel Moolenaar while (jobTokensRunning != 0) { 26763955d011SMarcel Moolenaar Job_CatchOutput(); 26773955d011SMarcel Moolenaar } 2678956e45f6SSimon J. Gerraty aborting = ABORT_NONE; 26793955d011SMarcel Moolenaar } 26803955d011SMarcel Moolenaar 268106b9b3e0SSimon J. Gerraty /* 268206b9b3e0SSimon J. Gerraty * Abort all currently running jobs without handling output or anything. 2683956e45f6SSimon J. Gerraty * This function is to be called only in the event of a major error. 2684956e45f6SSimon J. Gerraty * Most definitely NOT to be called from JobInterrupt. 26853955d011SMarcel Moolenaar * 268606b9b3e0SSimon J. Gerraty * All children are killed, not just the firstborn. 268706b9b3e0SSimon J. Gerraty */ 26883955d011SMarcel Moolenaar void 26893955d011SMarcel Moolenaar Job_AbortAll(void) 26903955d011SMarcel Moolenaar { 26913955d011SMarcel Moolenaar Job *job; /* the job descriptor in that element */ 26923955d011SMarcel Moolenaar WAIT_T foo; 26933955d011SMarcel Moolenaar 26943955d011SMarcel Moolenaar aborting = ABORT_ERROR; 26953955d011SMarcel Moolenaar 269606b9b3e0SSimon J. Gerraty if (jobTokensRunning != 0) { 26973955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 2698e2eeea75SSimon J. Gerraty if (job->status != JOB_ST_RUNNING) 26993955d011SMarcel Moolenaar continue; 27003955d011SMarcel Moolenaar /* 270106b9b3e0SSimon J. Gerraty * kill the child process with increasingly drastic 270206b9b3e0SSimon J. Gerraty * signals to make darn sure it's dead. 27033955d011SMarcel Moolenaar */ 27043955d011SMarcel Moolenaar KILLPG(job->pid, SIGINT); 27053955d011SMarcel Moolenaar KILLPG(job->pid, SIGKILL); 27063955d011SMarcel Moolenaar } 27073955d011SMarcel Moolenaar } 27083955d011SMarcel Moolenaar 27093955d011SMarcel Moolenaar /* 27103955d011SMarcel Moolenaar * Catch as many children as want to report in at first, then give up 27113955d011SMarcel Moolenaar */ 27123955d011SMarcel Moolenaar while (waitpid((pid_t)-1, &foo, WNOHANG) > 0) 27133955d011SMarcel Moolenaar continue; 27143955d011SMarcel Moolenaar } 27153955d011SMarcel Moolenaar 271606b9b3e0SSimon J. Gerraty /* 271706b9b3e0SSimon J. Gerraty * Tries to restart stopped jobs if there are slots available. 271806b9b3e0SSimon J. Gerraty * Called in process context in response to a SIGCONT. 271906b9b3e0SSimon J. Gerraty */ 27203955d011SMarcel Moolenaar static void 27213955d011SMarcel Moolenaar JobRestartJobs(void) 27223955d011SMarcel Moolenaar { 27233955d011SMarcel Moolenaar Job *job; 27243955d011SMarcel Moolenaar 27253955d011SMarcel Moolenaar for (job = job_table; job < job_table_end; job++) { 2726e2eeea75SSimon J. Gerraty if (job->status == JOB_ST_RUNNING && 2727e2eeea75SSimon J. Gerraty (make_suspended || job->suspended)) { 272806b9b3e0SSimon J. Gerraty DEBUG1(JOB, "Restarting stopped job pid %d.\n", 272906b9b3e0SSimon J. Gerraty job->pid); 2730e2eeea75SSimon J. Gerraty if (job->suspended) { 273106b9b3e0SSimon J. Gerraty (void)printf("*** [%s] Continued\n", 273206b9b3e0SSimon J. Gerraty job->node->name); 27333955d011SMarcel Moolenaar (void)fflush(stdout); 27343955d011SMarcel Moolenaar } 2735b0c40a00SSimon J. Gerraty job->suspended = false; 27363955d011SMarcel Moolenaar if (KILLPG(job->pid, SIGCONT) != 0 && DEBUG(JOB)) { 273706b9b3e0SSimon J. Gerraty debug_printf("Failed to send SIGCONT to %d\n", 273806b9b3e0SSimon J. Gerraty job->pid); 27393955d011SMarcel Moolenaar } 27403955d011SMarcel Moolenaar } 274106b9b3e0SSimon J. Gerraty if (job->status == JOB_ST_FINISHED) { 274206b9b3e0SSimon J. Gerraty /* 274306b9b3e0SSimon J. Gerraty * Job exit deferred after calling waitpid() in a 274406b9b3e0SSimon J. Gerraty * signal handler 274506b9b3e0SSimon J. Gerraty */ 27463955d011SMarcel Moolenaar JobFinish(job, job->exit_status); 27473955d011SMarcel Moolenaar } 274806b9b3e0SSimon J. Gerraty } 2749b0c40a00SSimon J. Gerraty make_suspended = false; 27503955d011SMarcel Moolenaar } 27513955d011SMarcel Moolenaar 27523955d011SMarcel Moolenaar static void 27533955d011SMarcel Moolenaar watchfd(Job *job) 27543955d011SMarcel Moolenaar { 27553955d011SMarcel Moolenaar if (job->inPollfd != NULL) 27563955d011SMarcel Moolenaar Punt("Watching watched job"); 27573955d011SMarcel Moolenaar 2758dba7b0efSSimon J. Gerraty fds[fdsLen].fd = job->inPipe; 2759dba7b0efSSimon J. Gerraty fds[fdsLen].events = POLLIN; 2760dba7b0efSSimon J. Gerraty jobByFdIndex[fdsLen] = job; 2761dba7b0efSSimon J. Gerraty job->inPollfd = &fds[fdsLen]; 2762dba7b0efSSimon J. Gerraty fdsLen++; 276349caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 276449caa483SSimon J. Gerraty if (useMeta) { 2765dba7b0efSSimon J. Gerraty fds[fdsLen].fd = meta_job_fd(job); 2766dba7b0efSSimon J. Gerraty fds[fdsLen].events = fds[fdsLen].fd == -1 ? 0 : POLLIN; 2767dba7b0efSSimon J. Gerraty jobByFdIndex[fdsLen] = job; 2768dba7b0efSSimon J. Gerraty fdsLen++; 276949caa483SSimon J. Gerraty } 277049caa483SSimon J. Gerraty #endif 27713955d011SMarcel Moolenaar } 27723955d011SMarcel Moolenaar 27733955d011SMarcel Moolenaar static void 27743955d011SMarcel Moolenaar clearfd(Job *job) 27753955d011SMarcel Moolenaar { 2776956e45f6SSimon J. Gerraty size_t i; 27773955d011SMarcel Moolenaar if (job->inPollfd == NULL) 27783955d011SMarcel Moolenaar Punt("Unwatching unwatched job"); 2779956e45f6SSimon J. Gerraty i = (size_t)(job->inPollfd - fds); 2780dba7b0efSSimon J. Gerraty fdsLen--; 278149caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 278249caa483SSimon J. Gerraty if (useMeta) { 278349caa483SSimon J. Gerraty /* 278449caa483SSimon J. Gerraty * Sanity check: there should be two fds per job, so the job's 278549caa483SSimon J. Gerraty * pollfd number should be even. 278649caa483SSimon J. Gerraty */ 278749caa483SSimon J. Gerraty assert(nfds_per_job() == 2); 278806b9b3e0SSimon J. Gerraty if (i % 2 != 0) 278949caa483SSimon J. Gerraty Punt("odd-numbered fd with meta"); 2790dba7b0efSSimon J. Gerraty fdsLen--; 279149caa483SSimon J. Gerraty } 279249caa483SSimon J. Gerraty #endif 27933955d011SMarcel Moolenaar /* 27943955d011SMarcel Moolenaar * Move last job in table into hole made by dead job. 27953955d011SMarcel Moolenaar */ 2796dba7b0efSSimon J. Gerraty if (fdsLen != i) { 2797dba7b0efSSimon J. Gerraty fds[i] = fds[fdsLen]; 2798dba7b0efSSimon J. Gerraty jobByFdIndex[i] = jobByFdIndex[fdsLen]; 2799dba7b0efSSimon J. Gerraty jobByFdIndex[i]->inPollfd = &fds[i]; 280049caa483SSimon J. Gerraty #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV) 280149caa483SSimon J. Gerraty if (useMeta) { 2802dba7b0efSSimon J. Gerraty fds[i + 1] = fds[fdsLen + 1]; 2803dba7b0efSSimon J. Gerraty jobByFdIndex[i + 1] = jobByFdIndex[fdsLen + 1]; 280449caa483SSimon J. Gerraty } 280549caa483SSimon J. Gerraty #endif 28063955d011SMarcel Moolenaar } 28073955d011SMarcel Moolenaar job->inPollfd = NULL; 28083955d011SMarcel Moolenaar } 28093955d011SMarcel Moolenaar 2810b0c40a00SSimon J. Gerraty static bool 28113955d011SMarcel Moolenaar readyfd(Job *job) 28123955d011SMarcel Moolenaar { 28133955d011SMarcel Moolenaar if (job->inPollfd == NULL) 28143955d011SMarcel Moolenaar Punt("Polling unwatched job"); 28153955d011SMarcel Moolenaar return (job->inPollfd->revents & POLLIN) != 0; 28163955d011SMarcel Moolenaar } 28173955d011SMarcel Moolenaar 281806b9b3e0SSimon J. Gerraty /* 281906b9b3e0SSimon J. Gerraty * Put a token (back) into the job pipe. 282006b9b3e0SSimon J. Gerraty * This allows a make process to start a build job. 282106b9b3e0SSimon J. Gerraty */ 28223955d011SMarcel Moolenaar static void 28233955d011SMarcel Moolenaar JobTokenAdd(void) 28243955d011SMarcel Moolenaar { 28253955d011SMarcel Moolenaar char tok = JOB_TOKENS[aborting], tok1; 28263955d011SMarcel Moolenaar 28272d395cb5SSimon J. Gerraty if (!Job_error_token && aborting == ABORT_ERROR) { 28282d395cb5SSimon J. Gerraty if (jobTokensRunning == 0) 28292d395cb5SSimon J. Gerraty return; 28302d395cb5SSimon J. Gerraty tok = '+'; /* no error token */ 28312d395cb5SSimon J. Gerraty } 28322d395cb5SSimon J. Gerraty 28333955d011SMarcel Moolenaar /* If we are depositing an error token flush everything else */ 28343955d011SMarcel Moolenaar while (tok != '+' && read(tokenWaitJob.inPipe, &tok1, 1) == 1) 28353955d011SMarcel Moolenaar continue; 28363955d011SMarcel Moolenaar 2837956e45f6SSimon J. Gerraty DEBUG3(JOB, "(%d) aborting %d, deposit token %c\n", 28382d395cb5SSimon J. Gerraty getpid(), aborting, tok); 28393cbdda60SSimon J. Gerraty while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && errno == EAGAIN) 28403cbdda60SSimon J. Gerraty continue; 28413955d011SMarcel Moolenaar } 28423955d011SMarcel Moolenaar 2843dba7b0efSSimon J. Gerraty /* Get a temp file */ 2844dba7b0efSSimon J. Gerraty int 2845dba7b0efSSimon J. Gerraty Job_TempFile(const char *pattern, char *tfile, size_t tfile_sz) 2846dba7b0efSSimon J. Gerraty { 2847dba7b0efSSimon J. Gerraty int fd; 2848dba7b0efSSimon J. Gerraty sigset_t mask; 2849dba7b0efSSimon J. Gerraty 2850dba7b0efSSimon J. Gerraty JobSigLock(&mask); 2851dba7b0efSSimon J. Gerraty fd = mkTempFile(pattern, tfile, tfile_sz); 2852dba7b0efSSimon J. Gerraty if (tfile != NULL && !DEBUG(SCRIPT)) 2853dba7b0efSSimon J. Gerraty unlink(tfile); 2854dba7b0efSSimon J. Gerraty JobSigUnlock(&mask); 2855dba7b0efSSimon J. Gerraty 2856dba7b0efSSimon J. Gerraty return fd; 2857dba7b0efSSimon J. Gerraty } 2858dba7b0efSSimon J. Gerraty 2859956e45f6SSimon J. Gerraty /* Prep the job token pipe in the root make process. */ 28603955d011SMarcel Moolenaar void 28613955d011SMarcel Moolenaar Job_ServerStart(int max_tokens, int jp_0, int jp_1) 28623955d011SMarcel Moolenaar { 28633955d011SMarcel Moolenaar int i; 28643955d011SMarcel Moolenaar char jobarg[64]; 28653955d011SMarcel Moolenaar 28663955d011SMarcel Moolenaar if (jp_0 >= 0 && jp_1 >= 0) { 28673955d011SMarcel Moolenaar /* Pipe passed in from parent */ 28683955d011SMarcel Moolenaar tokenWaitJob.inPipe = jp_0; 28693955d011SMarcel Moolenaar tokenWaitJob.outPipe = jp_1; 2870be19d90bSSimon J. Gerraty (void)fcntl(jp_0, F_SETFD, FD_CLOEXEC); 2871be19d90bSSimon J. Gerraty (void)fcntl(jp_1, F_SETFD, FD_CLOEXEC); 28723955d011SMarcel Moolenaar return; 28733955d011SMarcel Moolenaar } 28743955d011SMarcel Moolenaar 28753955d011SMarcel Moolenaar JobCreatePipe(&tokenWaitJob, 15); 28763955d011SMarcel Moolenaar 2877e2eeea75SSimon J. Gerraty snprintf(jobarg, sizeof jobarg, "%d,%d", 28783955d011SMarcel Moolenaar tokenWaitJob.inPipe, tokenWaitJob.outPipe); 28793955d011SMarcel Moolenaar 2880dba7b0efSSimon J. Gerraty Global_Append(MAKEFLAGS, "-J"); 2881dba7b0efSSimon J. Gerraty Global_Append(MAKEFLAGS, jobarg); 28823955d011SMarcel Moolenaar 28833955d011SMarcel Moolenaar /* 28843955d011SMarcel Moolenaar * Preload the job pipe with one token per job, save the one 28853955d011SMarcel Moolenaar * "extra" token for the primary job. 28863955d011SMarcel Moolenaar * 28873955d011SMarcel Moolenaar * XXX should clip maxJobs against PIPE_BUF -- if max_tokens is 28883955d011SMarcel Moolenaar * larger than the write buffer size of the pipe, we will 28893955d011SMarcel Moolenaar * deadlock here. 28903955d011SMarcel Moolenaar */ 28913955d011SMarcel Moolenaar for (i = 1; i < max_tokens; i++) 28923955d011SMarcel Moolenaar JobTokenAdd(); 28933955d011SMarcel Moolenaar } 28943955d011SMarcel Moolenaar 2895956e45f6SSimon J. Gerraty /* Return a withdrawn token to the pool. */ 28963955d011SMarcel Moolenaar void 28973955d011SMarcel Moolenaar Job_TokenReturn(void) 28983955d011SMarcel Moolenaar { 28993955d011SMarcel Moolenaar jobTokensRunning--; 29003955d011SMarcel Moolenaar if (jobTokensRunning < 0) 29013955d011SMarcel Moolenaar Punt("token botch"); 290206b9b3e0SSimon J. Gerraty if (jobTokensRunning != 0 || JOB_TOKENS[aborting] != '+') 29033955d011SMarcel Moolenaar JobTokenAdd(); 29043955d011SMarcel Moolenaar } 29053955d011SMarcel Moolenaar 290606b9b3e0SSimon J. Gerraty /* 290706b9b3e0SSimon J. Gerraty * Attempt to withdraw a token from the pool. 29083955d011SMarcel Moolenaar * 2909956e45f6SSimon J. Gerraty * If pool is empty, set wantToken so that we wake up when a token is 2910956e45f6SSimon J. Gerraty * released. 29113955d011SMarcel Moolenaar * 2912b0c40a00SSimon J. Gerraty * Returns true if a token was withdrawn, and false if the pool is currently 291306b9b3e0SSimon J. Gerraty * empty. 291406b9b3e0SSimon J. Gerraty */ 2915b0c40a00SSimon J. Gerraty bool 29163955d011SMarcel Moolenaar Job_TokenWithdraw(void) 29173955d011SMarcel Moolenaar { 29183955d011SMarcel Moolenaar char tok, tok1; 2919956e45f6SSimon J. Gerraty ssize_t count; 29203955d011SMarcel Moolenaar 29213955d011SMarcel Moolenaar wantToken = 0; 2922956e45f6SSimon J. Gerraty DEBUG3(JOB, "Job_TokenWithdraw(%d): aborting %d, running %d\n", 29233955d011SMarcel Moolenaar getpid(), aborting, jobTokensRunning); 29243955d011SMarcel Moolenaar 2925956e45f6SSimon J. Gerraty if (aborting != ABORT_NONE || (jobTokensRunning >= opts.maxJobs)) 2926b0c40a00SSimon J. Gerraty return false; 29273955d011SMarcel Moolenaar 29283955d011SMarcel Moolenaar count = read(tokenWaitJob.inPipe, &tok, 1); 29293955d011SMarcel Moolenaar if (count == 0) 29303955d011SMarcel Moolenaar Fatal("eof on job pipe!"); 29313955d011SMarcel Moolenaar if (count < 0 && jobTokensRunning != 0) { 29323955d011SMarcel Moolenaar if (errno != EAGAIN) { 29333955d011SMarcel Moolenaar Fatal("job pipe read: %s", strerror(errno)); 29343955d011SMarcel Moolenaar } 2935956e45f6SSimon J. Gerraty DEBUG1(JOB, "(%d) blocked for token\n", getpid()); 293606b9b3e0SSimon J. Gerraty wantToken = 1; 2937b0c40a00SSimon J. Gerraty return false; 29383955d011SMarcel Moolenaar } 29393955d011SMarcel Moolenaar 29403955d011SMarcel Moolenaar if (count == 1 && tok != '+') { 294106b9b3e0SSimon J. Gerraty /* make being aborted - remove any other job tokens */ 2942956e45f6SSimon J. Gerraty DEBUG2(JOB, "(%d) aborted by token %c\n", getpid(), tok); 29433955d011SMarcel Moolenaar while (read(tokenWaitJob.inPipe, &tok1, 1) == 1) 29443955d011SMarcel Moolenaar continue; 29453955d011SMarcel Moolenaar /* And put the stopper back */ 294606b9b3e0SSimon J. Gerraty while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && 294706b9b3e0SSimon J. Gerraty errno == EAGAIN) 29483cbdda60SSimon J. Gerraty continue; 2949e2eeea75SSimon J. Gerraty if (shouldDieQuietly(NULL, 1)) 295006b9b3e0SSimon J. Gerraty exit(6); /* we aborted */ 295106b9b3e0SSimon J. Gerraty Fatal("A failure has been detected " 295206b9b3e0SSimon J. Gerraty "in another branch of the parallel make"); 29533955d011SMarcel Moolenaar } 29543955d011SMarcel Moolenaar 29553955d011SMarcel Moolenaar if (count == 1 && jobTokensRunning == 0) 29563955d011SMarcel Moolenaar /* We didn't want the token really */ 295706b9b3e0SSimon J. Gerraty while (write(tokenWaitJob.outPipe, &tok, 1) == -1 && 295806b9b3e0SSimon J. Gerraty errno == EAGAIN) 29593cbdda60SSimon J. Gerraty continue; 29603955d011SMarcel Moolenaar 29613955d011SMarcel Moolenaar jobTokensRunning++; 2962956e45f6SSimon J. Gerraty DEBUG1(JOB, "(%d) withdrew token\n", getpid()); 2963b0c40a00SSimon J. Gerraty return true; 29643955d011SMarcel Moolenaar } 29653955d011SMarcel Moolenaar 296606b9b3e0SSimon J. Gerraty /* 296706b9b3e0SSimon J. Gerraty * Run the named target if found. If a filename is specified, then set that 2968956e45f6SSimon J. Gerraty * to the sources. 29691748de26SSimon J. Gerraty * 297006b9b3e0SSimon J. Gerraty * Exits if the target fails. 297106b9b3e0SSimon J. Gerraty */ 2972b0c40a00SSimon J. Gerraty bool 297306b9b3e0SSimon J. Gerraty Job_RunTarget(const char *target, const char *fname) 297406b9b3e0SSimon J. Gerraty { 2975956e45f6SSimon J. Gerraty GNode *gn = Targ_FindNode(target); 29761748de26SSimon J. Gerraty if (gn == NULL) 2977b0c40a00SSimon J. Gerraty return false; 29781748de26SSimon J. Gerraty 297906b9b3e0SSimon J. Gerraty if (fname != NULL) 2980dba7b0efSSimon J. Gerraty Var_Set(gn, ALLSRC, fname); 29811748de26SSimon J. Gerraty 29821748de26SSimon J. Gerraty JobRun(gn); 298306b9b3e0SSimon J. Gerraty /* XXX: Replace with GNode_IsError(gn) */ 29841748de26SSimon J. Gerraty if (gn->made == ERROR) { 29851748de26SSimon J. Gerraty PrintOnError(gn, "\n\nStop."); 29861748de26SSimon J. Gerraty exit(1); 29871748de26SSimon J. Gerraty } 2988b0c40a00SSimon J. Gerraty return true; 29891748de26SSimon J. Gerraty } 29901748de26SSimon J. Gerraty 29913955d011SMarcel Moolenaar #ifdef USE_SELECT 29923955d011SMarcel Moolenaar int 29933955d011SMarcel Moolenaar emul_poll(struct pollfd *fd, int nfd, int timeout) 29943955d011SMarcel Moolenaar { 29953955d011SMarcel Moolenaar fd_set rfds, wfds; 29963955d011SMarcel Moolenaar int i, maxfd, nselect, npoll; 29973955d011SMarcel Moolenaar struct timeval tv, *tvp; 29983955d011SMarcel Moolenaar long usecs; 29993955d011SMarcel Moolenaar 30003955d011SMarcel Moolenaar FD_ZERO(&rfds); 30013955d011SMarcel Moolenaar FD_ZERO(&wfds); 30023955d011SMarcel Moolenaar 30033955d011SMarcel Moolenaar maxfd = -1; 30043955d011SMarcel Moolenaar for (i = 0; i < nfd; i++) { 30053955d011SMarcel Moolenaar fd[i].revents = 0; 30063955d011SMarcel Moolenaar 30073955d011SMarcel Moolenaar if (fd[i].events & POLLIN) 30083955d011SMarcel Moolenaar FD_SET(fd[i].fd, &rfds); 30093955d011SMarcel Moolenaar 30103955d011SMarcel Moolenaar if (fd[i].events & POLLOUT) 30113955d011SMarcel Moolenaar FD_SET(fd[i].fd, &wfds); 30123955d011SMarcel Moolenaar 30133955d011SMarcel Moolenaar if (fd[i].fd > maxfd) 30143955d011SMarcel Moolenaar maxfd = fd[i].fd; 30153955d011SMarcel Moolenaar } 30163955d011SMarcel Moolenaar 30173955d011SMarcel Moolenaar if (maxfd >= FD_SETSIZE) { 30183955d011SMarcel Moolenaar Punt("Ran out of fd_set slots; " 30193955d011SMarcel Moolenaar "recompile with a larger FD_SETSIZE."); 30203955d011SMarcel Moolenaar } 30213955d011SMarcel Moolenaar 30223955d011SMarcel Moolenaar if (timeout < 0) { 30233955d011SMarcel Moolenaar tvp = NULL; 30243955d011SMarcel Moolenaar } else { 30253955d011SMarcel Moolenaar usecs = timeout * 1000; 30263955d011SMarcel Moolenaar tv.tv_sec = usecs / 1000000; 30273955d011SMarcel Moolenaar tv.tv_usec = usecs % 1000000; 30283955d011SMarcel Moolenaar tvp = &tv; 30293955d011SMarcel Moolenaar } 30303955d011SMarcel Moolenaar 3031e2eeea75SSimon J. Gerraty nselect = select(maxfd + 1, &rfds, &wfds, NULL, tvp); 30323955d011SMarcel Moolenaar 30333955d011SMarcel Moolenaar if (nselect <= 0) 30343955d011SMarcel Moolenaar return nselect; 30353955d011SMarcel Moolenaar 30363955d011SMarcel Moolenaar npoll = 0; 30373955d011SMarcel Moolenaar for (i = 0; i < nfd; i++) { 30383955d011SMarcel Moolenaar if (FD_ISSET(fd[i].fd, &rfds)) 30393955d011SMarcel Moolenaar fd[i].revents |= POLLIN; 30403955d011SMarcel Moolenaar 30413955d011SMarcel Moolenaar if (FD_ISSET(fd[i].fd, &wfds)) 30423955d011SMarcel Moolenaar fd[i].revents |= POLLOUT; 30433955d011SMarcel Moolenaar 30443955d011SMarcel Moolenaar if (fd[i].revents) 30453955d011SMarcel Moolenaar npoll++; 30463955d011SMarcel Moolenaar } 30473955d011SMarcel Moolenaar 30483955d011SMarcel Moolenaar return npoll; 30493955d011SMarcel Moolenaar } 30503955d011SMarcel Moolenaar #endif /* USE_SELECT */ 3051