1/*- 2 * Copyright (c) 1988, 1989, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 1988, 1989 by Adam de Boor 5 * Copyright (c) 1989 by Berkeley Softworks 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Adam de Boor. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)job.c 8.2 (Berkeley) 3/19/94 40 */ 41 42#include <sys/cdefs.h>
| 1/*- 2 * Copyright (c) 1988, 1989, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 1988, 1989 by Adam de Boor 5 * Copyright (c) 1989 by Berkeley Softworks 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Adam de Boor. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)job.c 8.2 (Berkeley) 3/19/94 40 */ 41 42#include <sys/cdefs.h>
|
43__FBSDID("$FreeBSD: head/usr.bin/make/job.c 146572 2005-05-24 15:30:03Z harti $");
| 43__FBSDID("$FreeBSD: head/usr.bin/make/job.c 146574 2005-05-24 15:41:34Z harti $");
|
44 45#ifndef OLD_JOKE 46#define OLD_JOKE 0 47#endif /* OLD_JOKE */ 48 49/*- 50 * job.c -- 51 * handle the creation etc. of our child processes. 52 * 53 * Interface: 54 * Job_Make Start the creation of the given target. 55 * 56 * Job_CatchChildren 57 * Check for and handle the termination of any children. 58 * This must be called reasonably frequently to keep the 59 * whole make going at a decent clip, since job table 60 * entries aren't removed until their process is caught 61 * this way. Its single argument is TRUE if the function 62 * should block waiting for a child to terminate. 63 * 64 * Job_CatchOutput Print any output our children have produced. Should 65 * also be called fairly frequently to keep the user 66 * informed of what's going on. If no output is waiting, 67 * it will block for a time given by the SEL_* constants, 68 * below, or until output is ready. 69 * 70 * Job_Init Called to intialize this module. in addition, any 71 * commands attached to the .BEGIN target are executed 72 * before this function returns. Hence, the makefile must 73 * have been parsed before this function is called. 74 * 75 * Job_Full Return TRUE if the job table is filled. 76 * 77 * Job_Empty Return TRUE if the job table is completely empty. 78 * 79 * Job_Finish Perform any final processing which needs doing. This 80 * includes the execution of any commands which have 81 * been/were attached to the .END target. It should only 82 * be called when the job table is empty. 83 * 84 * Job_AbortAll Abort all currently running jobs. It doesn't handle 85 * output or do anything for the jobs, just kills them. 86 * It should only be called in an emergency, as it were. 87 * 88 * Job_CheckCommands 89 * Verify that the commands for a target are ok. Provide 90 * them if necessary and possible. 91 * 92 * Job_Touch Update a target without really updating it. 93 * 94 * Job_Wait Wait for all currently-running jobs to finish. 95 * 96 * compat.c -- 97 * The routines in this file implement the full-compatibility 98 * mode of PMake. Most of the special functionality of PMake 99 * is available in this mode. Things not supported: 100 * - different shells. 101 * - friendly variable substitution. 102 * 103 * Interface: 104 * Compat_Run Initialize things for this module and recreate 105 * thems as need creatin' 106 */ 107 108#include <sys/queue.h> 109#include <sys/types.h> 110#include <sys/select.h> 111#include <sys/stat.h> 112#ifdef USE_KQUEUE 113#include <sys/event.h> 114#endif 115#include <sys/wait.h> 116#include <ctype.h> 117#include <err.h> 118#include <errno.h> 119#include <fcntl.h> 120#include <inttypes.h> 121#include <string.h> 122#include <signal.h> 123#include <stdlib.h> 124#include <unistd.h> 125#include <utime.h> 126 127#include "arch.h" 128#include "buf.h" 129#include "config.h" 130#include "dir.h" 131#include "globals.h" 132#include "GNode.h" 133#include "job.h" 134#include "make.h" 135#include "parse.h"
| 44 45#ifndef OLD_JOKE 46#define OLD_JOKE 0 47#endif /* OLD_JOKE */ 48 49/*- 50 * job.c -- 51 * handle the creation etc. of our child processes. 52 * 53 * Interface: 54 * Job_Make Start the creation of the given target. 55 * 56 * Job_CatchChildren 57 * Check for and handle the termination of any children. 58 * This must be called reasonably frequently to keep the 59 * whole make going at a decent clip, since job table 60 * entries aren't removed until their process is caught 61 * this way. Its single argument is TRUE if the function 62 * should block waiting for a child to terminate. 63 * 64 * Job_CatchOutput Print any output our children have produced. Should 65 * also be called fairly frequently to keep the user 66 * informed of what's going on. If no output is waiting, 67 * it will block for a time given by the SEL_* constants, 68 * below, or until output is ready. 69 * 70 * Job_Init Called to intialize this module. in addition, any 71 * commands attached to the .BEGIN target are executed 72 * before this function returns. Hence, the makefile must 73 * have been parsed before this function is called. 74 * 75 * Job_Full Return TRUE if the job table is filled. 76 * 77 * Job_Empty Return TRUE if the job table is completely empty. 78 * 79 * Job_Finish Perform any final processing which needs doing. This 80 * includes the execution of any commands which have 81 * been/were attached to the .END target. It should only 82 * be called when the job table is empty. 83 * 84 * Job_AbortAll Abort all currently running jobs. It doesn't handle 85 * output or do anything for the jobs, just kills them. 86 * It should only be called in an emergency, as it were. 87 * 88 * Job_CheckCommands 89 * Verify that the commands for a target are ok. Provide 90 * them if necessary and possible. 91 * 92 * Job_Touch Update a target without really updating it. 93 * 94 * Job_Wait Wait for all currently-running jobs to finish. 95 * 96 * compat.c -- 97 * The routines in this file implement the full-compatibility 98 * mode of PMake. Most of the special functionality of PMake 99 * is available in this mode. Things not supported: 100 * - different shells. 101 * - friendly variable substitution. 102 * 103 * Interface: 104 * Compat_Run Initialize things for this module and recreate 105 * thems as need creatin' 106 */ 107 108#include <sys/queue.h> 109#include <sys/types.h> 110#include <sys/select.h> 111#include <sys/stat.h> 112#ifdef USE_KQUEUE 113#include <sys/event.h> 114#endif 115#include <sys/wait.h> 116#include <ctype.h> 117#include <err.h> 118#include <errno.h> 119#include <fcntl.h> 120#include <inttypes.h> 121#include <string.h> 122#include <signal.h> 123#include <stdlib.h> 124#include <unistd.h> 125#include <utime.h> 126 127#include "arch.h" 128#include "buf.h" 129#include "config.h" 130#include "dir.h" 131#include "globals.h" 132#include "GNode.h" 133#include "job.h" 134#include "make.h" 135#include "parse.h"
|
136#include "pathnames.h"
| 136#include "proc.h"
|
137#include "shell.h" 138#include "str.h" 139#include "suff.h" 140#include "targ.h" 141#include "util.h" 142#include "var.h" 143 144#define TMPPAT "/tmp/makeXXXXXXXXXX" 145 146#ifndef USE_KQUEUE 147/* 148 * The SEL_ constants determine the maximum amount of time spent in select 149 * before coming out to see if a child has finished. SEL_SEC is the number of 150 * seconds and SEL_USEC is the number of micro-seconds 151 */ 152#define SEL_SEC 2 153#define SEL_USEC 0 154#endif /* !USE_KQUEUE */ 155 156/* 157 * Job Table definitions. 158 * 159 * The job "table" is kept as a linked Lst in 'jobs', with the number of 160 * active jobs maintained in the 'nJobs' variable. At no time will this 161 * exceed the value of 'maxJobs', initialized by the Job_Init function. 162 * 163 * When a job is finished, the Make_Update function is called on each of the 164 * parents of the node which was just remade. This takes care of the upward 165 * traversal of the dependency graph. 166 */ 167#define JOB_BUFSIZE 1024 168typedef struct Job { 169 pid_t pid; /* The child's process ID */ 170 171 struct GNode *node; /* The target the child is making */ 172 173 /* 174 * A LstNode for the first command to be saved after the job completes. 175 * This is NULL if there was no "..." in the job's commands. 176 */ 177 LstNode *tailCmds; 178 179 /* 180 * An FILE* for writing out the commands. This is only 181 * used before the job is actually started. 182 */ 183 FILE *cmdFILE; 184 185 /* 186 * A word of flags which determine how the module handles errors, 187 * echoing, etc. for the job 188 */ 189 short flags; /* Flags to control treatment of job */ 190#define JOB_IGNERR 0x001 /* Ignore non-zero exits */ 191#define JOB_SILENT 0x002 /* no output */ 192#define JOB_SPECIAL 0x004 /* Target is a special one. i.e. run it locally 193 * if we can't export it and maxLocal is 0 */ 194#define JOB_IGNDOTS 0x008 /* Ignore "..." lines when processing 195 * commands */ 196#define JOB_FIRST 0x020 /* Job is first job for the node */ 197#define JOB_RESTART 0x080 /* Job needs to be completely restarted */ 198#define JOB_RESUME 0x100 /* Job needs to be resumed b/c it stopped, 199 * for some reason */ 200#define JOB_CONTINUING 0x200 /* We are in the process of resuming this job. 201 * Used to avoid infinite recursion between 202 * JobFinish and JobRestart */ 203 204 /* union for handling shell's output */ 205 union { 206 /* 207 * This part is used when usePipes is true. 208 * The output is being caught via a pipe and the descriptors 209 * of our pipe, an array in which output is line buffered and 210 * the current position in that buffer are all maintained for 211 * each job. 212 */ 213 struct { 214 /* 215 * Input side of pipe associated with 216 * job's output channel 217 */ 218 int op_inPipe; 219 220 /* 221 * Output side of pipe associated with job's 222 * output channel 223 */ 224 int op_outPipe; 225 226 /* 227 * Buffer for storing the output of the 228 * job, line by line 229 */ 230 char op_outBuf[JOB_BUFSIZE + 1]; 231 232 /* Current position in op_outBuf */ 233 int op_curPos; 234 } o_pipe; 235 236 /* 237 * If usePipes is false the output is routed to a temporary 238 * file and all that is kept is the name of the file and the 239 * descriptor open to the file. 240 */ 241 struct { 242 /* Name of file to which shell output was rerouted */ 243 char of_outFile[sizeof(TMPPAT)]; 244 245 /* 246 * Stream open to the output file. Used to funnel all 247 * from a single job to one file while still allowing 248 * multiple shell invocations 249 */ 250 int of_outFd; 251 } o_file; 252 253 } output; /* Data for tracking a shell's output */ 254 255 TAILQ_ENTRY(Job) link; /* list link */ 256} Job; 257 258#define outPipe output.o_pipe.op_outPipe 259#define inPipe output.o_pipe.op_inPipe 260#define outBuf output.o_pipe.op_outBuf 261#define curPos output.o_pipe.op_curPos 262#define outFile output.o_file.of_outFile 263#define outFd output.o_file.of_outFd 264 265TAILQ_HEAD(JobList, Job); 266 267/* 268 * error handling variables 269 */ 270static int errors = 0; /* number of errors reported */ 271static int aborting = 0; /* why is the make aborting? */ 272#define ABORT_ERROR 1 /* Because of an error */ 273#define ABORT_INTERRUPT 2 /* Because it was interrupted */ 274#define ABORT_WAIT 3 /* Waiting for jobs to finish */ 275 276/* 277 * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file 278 * is a char! So when we go above 127 we turn negative! 279 */ 280#define FILENO(a) ((unsigned)fileno(a)) 281 282/* 283 * post-make command processing. The node postCommands is really just the 284 * .END target but we keep it around to avoid having to search for it 285 * all the time. 286 */ 287static GNode *postCommands; 288 289/* 290 * The number of commands actually printed for a target. Should this 291 * number be 0, no shell will be executed. 292 */ 293static int numCommands; 294 295/* 296 * Return values from JobStart. 297 */ 298#define JOB_RUNNING 0 /* Job is running */ 299#define JOB_ERROR 1 /* Error in starting the job */ 300#define JOB_FINISHED 2 /* The job is already finished */ 301#define JOB_STOPPED 3 /* The job is stopped */ 302 303/* 304 * The maximum number of jobs that may run. This is initialize from the 305 * -j argument for the leading make and from the FIFO for sub-makes. 306 */ 307static int maxJobs; 308 309static int nJobs; /* The number of children currently running */ 310 311/* The structures that describe them */ 312static struct JobList jobs = TAILQ_HEAD_INITIALIZER(jobs); 313 314static Boolean jobFull; /* Flag to tell when the job table is full. It 315 * is set TRUE when (1) the total number of 316 * running jobs equals the maximum allowed */ 317#ifdef USE_KQUEUE 318static int kqfd; /* File descriptor obtained by kqueue() */ 319#else 320static fd_set outputs; /* Set of descriptors of pipes connected to 321 * the output channels of children */ 322#endif 323 324static GNode *lastNode; /* The node for which output was most recently 325 * produced. */ 326static const char *targFmt; /* Format string to use to head output from a 327 * job when it's not the most-recent job heard 328 * from */ 329 330#define TARG_FMT "--- %s ---\n" /* Default format */ 331#define MESSAGE(fp, gn) \ 332 fprintf(fp, targFmt, gn->name); 333 334/* 335 * When JobStart attempts to run a job but isn't allowed to 336 * or when Job_CatchChildren detects a job that has 337 * been stopped somehow, the job is placed on the stoppedJobs queue to be run 338 * when the next job finishes. 339 * 340 * Lst of Job structures describing jobs that were stopped due to 341 * concurrency limits or externally 342 */ 343static struct JobList stoppedJobs = TAILQ_HEAD_INITIALIZER(stoppedJobs); 344 345static int fifoFd; /* Fd of our job fifo */ 346static char fifoName[] = "/tmp/make_fifo_XXXXXXXXX"; 347static int fifoMaster; 348 349static sig_atomic_t interrupted; 350 351 352#if defined(USE_PGRP) && defined(SYSV) 353# define KILL(pid, sig) killpg(-(pid), (sig)) 354#else 355# if defined(USE_PGRP) 356# define KILL(pid, sig) killpg((pid), (sig)) 357# else 358# define KILL(pid, sig) kill((pid), (sig)) 359# endif 360#endif 361 362/* 363 * Grmpf... There is no way to set bits of the wait structure 364 * anymore with the stupid W*() macros. I liked the union wait 365 * stuff much more. So, we devise our own macros... This is 366 * really ugly, use dramamine sparingly. You have been warned. 367 */ 368#define W_SETMASKED(st, val, fun) \ 369 { \ 370 int sh = (int)~0; \ 371 int mask = fun(sh); \ 372 \ 373 for (sh = 0; ((mask >> sh) & 1) == 0; sh++) \ 374 continue; \ 375 *(st) = (*(st) & ~mask) | ((val) << sh); \ 376 } 377 378#define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG) 379#define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS) 380
| 137#include "shell.h" 138#include "str.h" 139#include "suff.h" 140#include "targ.h" 141#include "util.h" 142#include "var.h" 143 144#define TMPPAT "/tmp/makeXXXXXXXXXX" 145 146#ifndef USE_KQUEUE 147/* 148 * The SEL_ constants determine the maximum amount of time spent in select 149 * before coming out to see if a child has finished. SEL_SEC is the number of 150 * seconds and SEL_USEC is the number of micro-seconds 151 */ 152#define SEL_SEC 2 153#define SEL_USEC 0 154#endif /* !USE_KQUEUE */ 155 156/* 157 * Job Table definitions. 158 * 159 * The job "table" is kept as a linked Lst in 'jobs', with the number of 160 * active jobs maintained in the 'nJobs' variable. At no time will this 161 * exceed the value of 'maxJobs', initialized by the Job_Init function. 162 * 163 * When a job is finished, the Make_Update function is called on each of the 164 * parents of the node which was just remade. This takes care of the upward 165 * traversal of the dependency graph. 166 */ 167#define JOB_BUFSIZE 1024 168typedef struct Job { 169 pid_t pid; /* The child's process ID */ 170 171 struct GNode *node; /* The target the child is making */ 172 173 /* 174 * A LstNode for the first command to be saved after the job completes. 175 * This is NULL if there was no "..." in the job's commands. 176 */ 177 LstNode *tailCmds; 178 179 /* 180 * An FILE* for writing out the commands. This is only 181 * used before the job is actually started. 182 */ 183 FILE *cmdFILE; 184 185 /* 186 * A word of flags which determine how the module handles errors, 187 * echoing, etc. for the job 188 */ 189 short flags; /* Flags to control treatment of job */ 190#define JOB_IGNERR 0x001 /* Ignore non-zero exits */ 191#define JOB_SILENT 0x002 /* no output */ 192#define JOB_SPECIAL 0x004 /* Target is a special one. i.e. run it locally 193 * if we can't export it and maxLocal is 0 */ 194#define JOB_IGNDOTS 0x008 /* Ignore "..." lines when processing 195 * commands */ 196#define JOB_FIRST 0x020 /* Job is first job for the node */ 197#define JOB_RESTART 0x080 /* Job needs to be completely restarted */ 198#define JOB_RESUME 0x100 /* Job needs to be resumed b/c it stopped, 199 * for some reason */ 200#define JOB_CONTINUING 0x200 /* We are in the process of resuming this job. 201 * Used to avoid infinite recursion between 202 * JobFinish and JobRestart */ 203 204 /* union for handling shell's output */ 205 union { 206 /* 207 * This part is used when usePipes is true. 208 * The output is being caught via a pipe and the descriptors 209 * of our pipe, an array in which output is line buffered and 210 * the current position in that buffer are all maintained for 211 * each job. 212 */ 213 struct { 214 /* 215 * Input side of pipe associated with 216 * job's output channel 217 */ 218 int op_inPipe; 219 220 /* 221 * Output side of pipe associated with job's 222 * output channel 223 */ 224 int op_outPipe; 225 226 /* 227 * Buffer for storing the output of the 228 * job, line by line 229 */ 230 char op_outBuf[JOB_BUFSIZE + 1]; 231 232 /* Current position in op_outBuf */ 233 int op_curPos; 234 } o_pipe; 235 236 /* 237 * If usePipes is false the output is routed to a temporary 238 * file and all that is kept is the name of the file and the 239 * descriptor open to the file. 240 */ 241 struct { 242 /* Name of file to which shell output was rerouted */ 243 char of_outFile[sizeof(TMPPAT)]; 244 245 /* 246 * Stream open to the output file. Used to funnel all 247 * from a single job to one file while still allowing 248 * multiple shell invocations 249 */ 250 int of_outFd; 251 } o_file; 252 253 } output; /* Data for tracking a shell's output */ 254 255 TAILQ_ENTRY(Job) link; /* list link */ 256} Job; 257 258#define outPipe output.o_pipe.op_outPipe 259#define inPipe output.o_pipe.op_inPipe 260#define outBuf output.o_pipe.op_outBuf 261#define curPos output.o_pipe.op_curPos 262#define outFile output.o_file.of_outFile 263#define outFd output.o_file.of_outFd 264 265TAILQ_HEAD(JobList, Job); 266 267/* 268 * error handling variables 269 */ 270static int errors = 0; /* number of errors reported */ 271static int aborting = 0; /* why is the make aborting? */ 272#define ABORT_ERROR 1 /* Because of an error */ 273#define ABORT_INTERRUPT 2 /* Because it was interrupted */ 274#define ABORT_WAIT 3 /* Waiting for jobs to finish */ 275 276/* 277 * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file 278 * is a char! So when we go above 127 we turn negative! 279 */ 280#define FILENO(a) ((unsigned)fileno(a)) 281 282/* 283 * post-make command processing. The node postCommands is really just the 284 * .END target but we keep it around to avoid having to search for it 285 * all the time. 286 */ 287static GNode *postCommands; 288 289/* 290 * The number of commands actually printed for a target. Should this 291 * number be 0, no shell will be executed. 292 */ 293static int numCommands; 294 295/* 296 * Return values from JobStart. 297 */ 298#define JOB_RUNNING 0 /* Job is running */ 299#define JOB_ERROR 1 /* Error in starting the job */ 300#define JOB_FINISHED 2 /* The job is already finished */ 301#define JOB_STOPPED 3 /* The job is stopped */ 302 303/* 304 * The maximum number of jobs that may run. This is initialize from the 305 * -j argument for the leading make and from the FIFO for sub-makes. 306 */ 307static int maxJobs; 308 309static int nJobs; /* The number of children currently running */ 310 311/* The structures that describe them */ 312static struct JobList jobs = TAILQ_HEAD_INITIALIZER(jobs); 313 314static Boolean jobFull; /* Flag to tell when the job table is full. It 315 * is set TRUE when (1) the total number of 316 * running jobs equals the maximum allowed */ 317#ifdef USE_KQUEUE 318static int kqfd; /* File descriptor obtained by kqueue() */ 319#else 320static fd_set outputs; /* Set of descriptors of pipes connected to 321 * the output channels of children */ 322#endif 323 324static GNode *lastNode; /* The node for which output was most recently 325 * produced. */ 326static const char *targFmt; /* Format string to use to head output from a 327 * job when it's not the most-recent job heard 328 * from */ 329 330#define TARG_FMT "--- %s ---\n" /* Default format */ 331#define MESSAGE(fp, gn) \ 332 fprintf(fp, targFmt, gn->name); 333 334/* 335 * When JobStart attempts to run a job but isn't allowed to 336 * or when Job_CatchChildren detects a job that has 337 * been stopped somehow, the job is placed on the stoppedJobs queue to be run 338 * when the next job finishes. 339 * 340 * Lst of Job structures describing jobs that were stopped due to 341 * concurrency limits or externally 342 */ 343static struct JobList stoppedJobs = TAILQ_HEAD_INITIALIZER(stoppedJobs); 344 345static int fifoFd; /* Fd of our job fifo */ 346static char fifoName[] = "/tmp/make_fifo_XXXXXXXXX"; 347static int fifoMaster; 348 349static sig_atomic_t interrupted; 350 351 352#if defined(USE_PGRP) && defined(SYSV) 353# define KILL(pid, sig) killpg(-(pid), (sig)) 354#else 355# if defined(USE_PGRP) 356# define KILL(pid, sig) killpg((pid), (sig)) 357# else 358# define KILL(pid, sig) kill((pid), (sig)) 359# endif 360#endif 361 362/* 363 * Grmpf... There is no way to set bits of the wait structure 364 * anymore with the stupid W*() macros. I liked the union wait 365 * stuff much more. So, we devise our own macros... This is 366 * really ugly, use dramamine sparingly. You have been warned. 367 */ 368#define W_SETMASKED(st, val, fun) \ 369 { \ 370 int sh = (int)~0; \ 371 int mask = fun(sh); \ 372 \ 373 for (sh = 0; ((mask >> sh) & 1) == 0; sh++) \ 374 continue; \ 375 *(st) = (*(st) & ~mask) | ((val) << sh); \ 376 } 377 378#define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG) 379#define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS) 380
|
381/** 382 * Information used to create a new process. 383 */ 384typedef struct ProcStuff { 385 int in; /* stdin for new process */ 386 int out; /* stdout for new process */ 387 int err; /* stderr for new process */ 388 389 int merge_errors; /* true if stderr is redirected to stdin */ 390 int pgroup; /* true if new process a process leader */ 391 int searchpath; /* true if binary should be found via $PATH */ 392 393 char **argv; 394 int argv_free; /* release argv after use */ 395 int errCheck; 396 397 pid_t child_pid; 398} ProcStuff; 399
| |
400static void JobRestart(Job *); 401static int JobStart(GNode *, int, Job *); 402static void JobDoOutput(Job *, Boolean); 403static void JobInterrupt(int, int); 404static void JobRestartJobs(void);
| 381static void JobRestart(Job *); 382static int JobStart(GNode *, int, Job *); 383static void JobDoOutput(Job *, Boolean); 384static void JobInterrupt(int, int); 385static void JobRestartJobs(void);
|
405static void ProcExec(const ProcStuff *) __dead2;
| |
406static int Compat_RunCommand(char *, struct GNode *); 407 408static GNode *curTarg = NULL; 409static GNode *ENDNode; 410 411/** 412 * Create a fifo file with a uniq filename, and returns a file 413 * descriptor to that fifo. 414 */ 415static int 416mkfifotemp(char *template) 417{ 418 char *start; 419 char *pathend; 420 char *ptr; 421 const unsigned char padchar[] = 422 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 423 424 if (template[0] == '\0') { 425 errno = EINVAL; /* bad input string */ 426 return (-1); 427 } 428 429 /* Find end of template string. */ 430 pathend = strchr(template, '\0'); 431 ptr = pathend - 1; 432 433 /* 434 * Starting from the end of the template replace spaces with 'X' in 435 * them with random characters until there are no more 'X'. 436 */ 437 while (ptr >= template && *ptr == 'X') { 438 uint32_t rand_num = arc4random() % (sizeof(padchar) - 1); 439 *ptr-- = padchar[rand_num]; 440 } 441 start = ptr + 1; 442 443 /* Check the target directory. */ 444 for (; ptr > template; --ptr) { 445 if (*ptr == '/') { 446 struct stat sbuf; 447 448 *ptr = '\0'; 449 if (stat(template, &sbuf) != 0) 450 return (-1); 451 452 if (!S_ISDIR(sbuf.st_mode)) { 453 errno = ENOTDIR; 454 return (-1); 455 } 456 *ptr = '/'; 457 break; 458 } 459 } 460 461 for (;;) { 462 if (mkfifo(template, 0600) == 0) { 463 int fd; 464 465 if ((fd = open(template, O_RDWR, 0600)) < 0) { 466 unlink(template); 467 return (-1); 468 } else { 469 return (fd); 470 } 471 } else { 472 if (errno != EEXIST) { 473 return (-1); 474 } 475 } 476 477 /* 478 * If we have a collision, cycle through the space of 479 * filenames. 480 */ 481 for (ptr = start;;) { 482 char *pad; 483 484 if (*ptr == '\0' || ptr == pathend) 485 return (-1); 486 487 pad = strchr(padchar, *ptr); 488 if (pad == NULL || *++pad == '\0') { 489 *ptr++ = padchar[0]; 490 } else { 491 *ptr++ = *pad; 492 break; 493 } 494 } 495 } 496 /*NOTREACHED*/ 497} 498 499static void 500catch_child(int sig __unused) 501{ 502} 503 504/** 505 */ 506void 507Proc_Init() 508{ 509 /* 510 * Catch SIGCHLD so that we get kicked out of select() when we 511 * need to look at a child. This is only known to matter for the 512 * -j case (perhaps without -P). 513 * 514 * XXX this is intentionally misplaced. 515 */ 516 struct sigaction sa; 517 518 sigemptyset(&sa.sa_mask); 519 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP; 520 sa.sa_handler = catch_child; 521 sigaction(SIGCHLD, &sa, NULL); 522} 523 524/**
| 386static int Compat_RunCommand(char *, struct GNode *); 387 388static GNode *curTarg = NULL; 389static GNode *ENDNode; 390 391/** 392 * Create a fifo file with a uniq filename, and returns a file 393 * descriptor to that fifo. 394 */ 395static int 396mkfifotemp(char *template) 397{ 398 char *start; 399 char *pathend; 400 char *ptr; 401 const unsigned char padchar[] = 402 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 403 404 if (template[0] == '\0') { 405 errno = EINVAL; /* bad input string */ 406 return (-1); 407 } 408 409 /* Find end of template string. */ 410 pathend = strchr(template, '\0'); 411 ptr = pathend - 1; 412 413 /* 414 * Starting from the end of the template replace spaces with 'X' in 415 * them with random characters until there are no more 'X'. 416 */ 417 while (ptr >= template && *ptr == 'X') { 418 uint32_t rand_num = arc4random() % (sizeof(padchar) - 1); 419 *ptr-- = padchar[rand_num]; 420 } 421 start = ptr + 1; 422 423 /* Check the target directory. */ 424 for (; ptr > template; --ptr) { 425 if (*ptr == '/') { 426 struct stat sbuf; 427 428 *ptr = '\0'; 429 if (stat(template, &sbuf) != 0) 430 return (-1); 431 432 if (!S_ISDIR(sbuf.st_mode)) { 433 errno = ENOTDIR; 434 return (-1); 435 } 436 *ptr = '/'; 437 break; 438 } 439 } 440 441 for (;;) { 442 if (mkfifo(template, 0600) == 0) { 443 int fd; 444 445 if ((fd = open(template, O_RDWR, 0600)) < 0) { 446 unlink(template); 447 return (-1); 448 } else { 449 return (fd); 450 } 451 } else { 452 if (errno != EEXIST) { 453 return (-1); 454 } 455 } 456 457 /* 458 * If we have a collision, cycle through the space of 459 * filenames. 460 */ 461 for (ptr = start;;) { 462 char *pad; 463 464 if (*ptr == '\0' || ptr == pathend) 465 return (-1); 466 467 pad = strchr(padchar, *ptr); 468 if (pad == NULL || *++pad == '\0') { 469 *ptr++ = padchar[0]; 470 } else { 471 *ptr++ = *pad; 472 break; 473 } 474 } 475 } 476 /*NOTREACHED*/ 477} 478 479static void 480catch_child(int sig __unused) 481{ 482} 483 484/** 485 */ 486void 487Proc_Init() 488{ 489 /* 490 * Catch SIGCHLD so that we get kicked out of select() when we 491 * need to look at a child. This is only known to matter for the 492 * -j case (perhaps without -P). 493 * 494 * XXX this is intentionally misplaced. 495 */ 496 struct sigaction sa; 497 498 sigemptyset(&sa.sa_mask); 499 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP; 500 sa.sa_handler = catch_child; 501 sigaction(SIGCHLD, &sa, NULL); 502} 503 504/**
|
525 * Replace the current process. 526 */ 527static void 528ProcExec(const ProcStuff *ps) 529{ 530 531 if (ps->in != STDIN_FILENO) { 532 /* 533 * Redirect the child's stdin to the input fd 534 * and reset it to the beginning (again). 535 */ 536 if (dup2(ps->in, STDIN_FILENO) == -1) 537 Punt("Cannot dup2: %s", strerror(errno)); 538 lseek(STDIN_FILENO, (off_t)0, SEEK_SET); 539 } 540 541 if (ps->out != STDOUT_FILENO) { 542 /* 543 * Redirect the child's stdout to the output fd. 544 */ 545 if (dup2(ps->out, STDOUT_FILENO) == -1) 546 Punt("Cannot dup2: %s", strerror(errno)); 547 close(ps->out); 548 } 549 550 if (ps->err != STDERR_FILENO) { 551 /* 552 * Redirect the child's stderr to the err fd. 553 */ 554 if (dup2(ps->err, STDERR_FILENO) == -1) 555 Punt("Cannot dup2: %s", strerror(errno)); 556 close(ps->err); 557 } 558 559 if (ps->merge_errors) { 560 /* 561 * Send stderr to parent process too. 562 */ 563 if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1) 564 Punt("Cannot dup2: %s", strerror(errno)); 565 } 566 567 if (commandShell->unsetenv) { 568 /* for the benfit of ksh */ 569 unsetenv("ENV"); 570 } 571 572 /* 573 * The file descriptors for stdin, stdout, or stderr might 574 * have been marked close-on-exec. Clear the flag on all 575 * of them. 576 */ 577 fcntl(STDIN_FILENO, F_SETFD, 578 fcntl(STDIN_FILENO, F_GETFD) & (~FD_CLOEXEC)); 579 fcntl(STDOUT_FILENO, F_SETFD, 580 fcntl(STDOUT_FILENO, F_GETFD) & (~FD_CLOEXEC)); 581 fcntl(STDERR_FILENO, F_SETFD, 582 fcntl(STDERR_FILENO, F_GETFD) & (~FD_CLOEXEC)); 583 584 if (ps->pgroup) { 585#ifdef USE_PGRP 586 /* 587 * Become a process group leader, so we can kill it and all 588 * its descendants in one fell swoop, by killing its process 589 * family, but not commit suicide. 590 */ 591#if defined(SYSV) 592 setsid(); 593#else 594 setpgid(0, getpid()); 595#endif 596#endif /* USE_PGRP */ 597 } 598 599 if (ps->searchpath) { 600 execvp(ps->argv[0], ps->argv); 601 602 write(STDERR_FILENO, ps->argv[0], strlen(ps->argv[0])); 603 write(STDERR_FILENO, ":", 1); 604 write(STDERR_FILENO, strerror(errno), strlen(strerror(errno))); 605 write(STDERR_FILENO, "\n", 1); 606 } else { 607 execv(commandShell->path, ps->argv); 608 609 write(STDERR_FILENO, 610 "Could not execute shell\n", 611 sizeof("Could not execute shell")); 612 } 613 614 /* 615 * Since we are the child process, exit without flushing buffers. 616 */ 617 _exit(1); 618 /* NOTREACHED */ 619} 620 621/**
| |
622 * Wait for child process to terminate. 623 */ 624static int 625ProcWait(ProcStuff *ps) 626{ 627 pid_t pid; 628 int status; 629 630 /* 631 * Wait for the process to exit. 632 */ 633 for (;;) { 634 pid = wait(&status); 635 if (pid == -1 && errno != EINTR) { 636 Fatal("error in wait: %d", pid); 637 /* NOTREACHED */ 638 } 639 if (pid == ps->child_pid) { 640 break; 641 } 642 if (interrupted) { 643 break; 644 } 645 } 646 647 return (status); 648} 649 650/** 651 * JobCatchSignal 652 * Got a signal. Set global variables and hope that someone will 653 * handle it. 654 */ 655static void 656JobCatchSig(int signo) 657{ 658 659 interrupted = signo; 660} 661 662/** 663 * JobPassSig -- 664 * Pass a signal on to all local jobs if 665 * USE_PGRP is defined, then die ourselves. 666 * 667 * Side Effects: 668 * We die by the same signal. 669 */ 670static void 671JobPassSig(int signo) 672{ 673 Job *job; 674 sigset_t nmask, omask; 675 struct sigaction act; 676 677 sigemptyset(&nmask); 678 sigaddset(&nmask, signo); 679 sigprocmask(SIG_SETMASK, &nmask, &omask); 680 681 DEBUGF(JOB, ("JobPassSig(%d) called.\n", signo)); 682 TAILQ_FOREACH(job, &jobs, link) { 683 DEBUGF(JOB, ("JobPassSig passing signal %d to child %jd.\n", 684 signo, (intmax_t)job->pid)); 685 KILL(job->pid, signo); 686 } 687 688 /* 689 * Deal with proper cleanup based on the signal received. We only run 690 * the .INTERRUPT target if the signal was in fact an interrupt. 691 * The other three termination signals are more of a "get out *now*" 692 * command. 693 */ 694 if (signo == SIGINT) { 695 JobInterrupt(TRUE, signo); 696 } else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT) { 697 JobInterrupt(FALSE, signo); 698 } 699 700 /* 701 * Leave gracefully if SIGQUIT, rather than core dumping. 702 */ 703 if (signo == SIGQUIT) { 704 signo = SIGINT; 705 } 706 707 /* 708 * Send ourselves the signal now we've given the message to everyone 709 * else. Note we block everything else possible while we're getting 710 * the signal. This ensures that all our jobs get continued when we 711 * wake up before we take any other signal. 712 * XXX this comment seems wrong. 713 */ 714 act.sa_handler = SIG_DFL; 715 sigemptyset(&act.sa_mask); 716 act.sa_flags = 0; 717 sigaction(signo, &act, NULL); 718 719 DEBUGF(JOB, ("JobPassSig passing signal to self, mask = %x.\n", 720 ~0 & ~(1 << (signo - 1)))); 721 signal(signo, SIG_DFL); 722 723 KILL(getpid(), signo); 724 725 signo = SIGCONT; 726 TAILQ_FOREACH(job, &jobs, link) { 727 DEBUGF(JOB, ("JobPassSig passing signal %d to child %jd.\n", 728 signo, (intmax_t)job->pid)); 729 KILL(job->pid, signo); 730 } 731 732 sigprocmask(SIG_SETMASK, &omask, NULL); 733 sigprocmask(SIG_SETMASK, &omask, NULL); 734 act.sa_handler = JobPassSig; 735 sigaction(signo, &act, NULL); 736} 737 738/** 739 * JobPrintCommand -- 740 * Put out another command for the given job. If the command starts 741 * with an @ or a - we process it specially. In the former case, 742 * so long as the -s and -n flags weren't given to make, we stick 743 * a shell-specific echoOff command in the script. In the latter, 744 * we ignore errors for the entire job, unless the shell has error 745 * control. 746 * If the command is just "..." we take all future commands for this 747 * job to be commands to be executed once the entire graph has been 748 * made and return non-zero to signal that the end of the commands 749 * was reached. These commands are later attached to the postCommands 750 * node and executed by Job_Finish when all things are done. 751 * This function is called from JobStart via LST_FOREACH. 752 * 753 * Results: 754 * Always 0, unless the command was "..." 755 * 756 * Side Effects: 757 * If the command begins with a '-' and the shell has no error control, 758 * the JOB_IGNERR flag is set in the job descriptor. 759 * If the command is "..." and we're not ignoring such things, 760 * tailCmds is set to the successor node of the cmd. 761 * numCommands is incremented if the command is actually printed. 762 */ 763static int 764JobPrintCommand(char *cmd, Job *job) 765{ 766 Boolean noSpecials; /* true if we shouldn't worry about 767 * inserting special commands into 768 * the input stream. */ 769 Boolean shutUp = FALSE; /* true if we put a no echo command 770 * into the command file */ 771 Boolean errOff = FALSE; /* true if we turned error checking 772 * off before printing the command 773 * and need to turn it back on */ 774 const char *cmdTemplate;/* Template to use when printing the command */ 775 char *cmdStart; /* Start of expanded command */ 776 LstNode *cmdNode; /* Node for replacing the command */ 777 778 noSpecials = (noExecute && !(job->node->type & OP_MAKE)); 779 780 if (strcmp(cmd, "...") == 0) { 781 job->node->type |= OP_SAVE_CMDS; 782 if ((job->flags & JOB_IGNDOTS) == 0) { 783 job->tailCmds = 784 Lst_Succ(Lst_Member(&job->node->commands, cmd)); 785 return (1); 786 } 787 return (0); 788 } 789 790#define DBPRINTF(fmt, arg) \ 791 DEBUGF(JOB, (fmt, arg)); \ 792 fprintf(job->cmdFILE, fmt, arg); \ 793 fflush(job->cmdFILE); 794 795 numCommands += 1; 796 797 /* 798 * For debugging, we replace each command with the result of expanding 799 * the variables in the command. 800 */ 801 cmdNode = Lst_Member(&job->node->commands, cmd); 802 803 cmd = Buf_Peel(Var_Subst(cmd, job->node, FALSE)); 804 cmdStart = cmd; 805 806 Lst_Replace(cmdNode, cmdStart); 807 808 cmdTemplate = "%s\n"; 809 810 /* 811 * Check for leading @', -' or +'s to control echoing, error checking, 812 * and execution on -n. 813 */ 814 while (*cmd == '@' || *cmd == '-' || *cmd == '+') { 815 switch (*cmd) { 816 817 case '@': 818 shutUp = DEBUG(LOUD) ? FALSE : TRUE; 819 break; 820 821 case '-': 822 errOff = TRUE; 823 break; 824 825 case '+': 826 if (noSpecials) { 827 /* 828 * We're not actually exececuting anything... 829 * but this one needs to be - use compat mode 830 * just for it. 831 */ 832 Compat_RunCommand(cmd, job->node); 833 return (0); 834 } 835 break; 836 } 837 cmd++; 838 } 839 840 while (isspace((unsigned char)*cmd)) 841 cmd++; 842 843 if (shutUp) { 844 if (!(job->flags & JOB_SILENT) && !noSpecials && 845 commandShell->hasEchoCtl) { 846 DBPRINTF("%s\n", commandShell->echoOff); 847 } else { 848 shutUp = FALSE; 849 } 850 } 851 852 if (errOff) { 853 if (!(job->flags & JOB_IGNERR) && !noSpecials) { 854 if (commandShell->hasErrCtl) { 855 /* 856 * We don't want the error-control commands 857 * showing up either, so we turn off echoing 858 * while executing them. We could put another 859 * field in the shell structure to tell 860 * JobDoOutput to look for this string too, 861 * but why make it any more complex than 862 * it already is? 863 */ 864 if (!(job->flags & JOB_SILENT) && !shutUp && 865 commandShell->hasEchoCtl) { 866 DBPRINTF("%s\n", commandShell->echoOff); 867 DBPRINTF("%s\n", commandShell->ignErr); 868 DBPRINTF("%s\n", commandShell->echoOn); 869 } else { 870 DBPRINTF("%s\n", commandShell->ignErr); 871 } 872 } else if (commandShell->ignErr && 873 *commandShell->ignErr != '\0') { 874 /* 875 * The shell has no error control, so we need to 876 * be weird to get it to ignore any errors from 877 * the command. If echoing is turned on, we turn 878 * it off and use the errCheck template to echo 879 * the command. Leave echoing off so the user 880 * doesn't see the weirdness we go through to 881 * ignore errors. Set cmdTemplate to use the 882 * weirdness instead of the simple "%s\n" 883 * template. 884 */ 885 if (!(job->flags & JOB_SILENT) && !shutUp && 886 commandShell->hasEchoCtl) { 887 DBPRINTF("%s\n", commandShell->echoOff); 888 DBPRINTF(commandShell->errCheck, cmd); 889 shutUp = TRUE; 890 } 891 cmdTemplate = commandShell->ignErr; 892 /* 893 * The error ignoration (hee hee) is already 894 * taken care of by the ignErr template, so 895 * pretend error checking is still on. 896 */ 897 errOff = FALSE; 898 } else { 899 errOff = FALSE; 900 } 901 } else { 902 errOff = FALSE; 903 } 904 } 905 906 DBPRINTF(cmdTemplate, cmd); 907 908 if (errOff) { 909 /* 910 * If echoing is already off, there's no point in issuing the 911 * echoOff command. Otherwise we issue it and pretend it was on 912 * for the whole command... 913 */ 914 if (!shutUp && !(job->flags & JOB_SILENT) && 915 commandShell->hasEchoCtl) { 916 DBPRINTF("%s\n", commandShell->echoOff); 917 shutUp = TRUE; 918 } 919 DBPRINTF("%s\n", commandShell->errCheck); 920 } 921 if (shutUp) { 922 DBPRINTF("%s\n", commandShell->echoOn); 923 } 924 return (0); 925} 926 927/** 928 * JobClose -- 929 * Called to close both input and output pipes when a job is finished. 930 * 931 * Side Effects: 932 * The file descriptors associated with the job are closed. 933 */ 934static void 935JobClose(Job *job) 936{ 937 938 if (usePipes) { 939#if !defined(USE_KQUEUE) 940 FD_CLR(job->inPipe, &outputs); 941#endif 942 if (job->outPipe != job->inPipe) { 943 close(job->outPipe); 944 } 945 JobDoOutput(job, TRUE); 946 close(job->inPipe); 947 } else { 948 close(job->outFd); 949 JobDoOutput(job, TRUE); 950 } 951} 952 953/** 954 * JobFinish -- 955 * Do final processing for the given job including updating 956 * parents and starting new jobs as available/necessary. Note 957 * that we pay no attention to the JOB_IGNERR flag here. 958 * This is because when we're called because of a noexecute flag 959 * or something, jstat.w_status is 0 and when called from 960 * Job_CatchChildren, the status is zeroed if it s/b ignored. 961 * 962 * Side Effects: 963 * Some nodes may be put on the toBeMade queue. 964 * Final commands for the job are placed on postCommands. 965 * 966 * If we got an error and are aborting (aborting == ABORT_ERROR) and 967 * the job list is now empty, we are done for the day. 968 * If we recognized an error (errors !=0), we set the aborting flag 969 * to ABORT_ERROR so no more jobs will be started. 970 */ 971static void 972JobFinish(Job *job, int *status) 973{ 974 Boolean done; 975 LstNode *ln; 976 977 if (WIFEXITED(*status)) { 978 int job_status = WEXITSTATUS(*status); 979 980 JobClose(job); 981 /* 982 * Deal with ignored errors in -B mode. We need to 983 * print a message telling of the ignored error as 984 * well as setting status.w_status to 0 so the next 985 * command gets run. To do this, we set done to be 986 * TRUE if in -B mode and the job exited non-zero. 987 */ 988 if (job_status == 0) { 989 done = FALSE; 990 } else { 991 if (job->flags & JOB_IGNERR) { 992 done = TRUE; 993 } else { 994 /* 995 * If it exited non-zero and either we're 996 * doing things our way or we're not ignoring 997 * errors, the job is finished. Similarly, if 998 * the shell died because of a signal the job 999 * is also finished. In these cases, finish 1000 * out the job's output before printing the 1001 * exit status... 1002 */ 1003 done = TRUE; 1004 if (job->cmdFILE != NULL && 1005 job->cmdFILE != stdout) { 1006 fclose(job->cmdFILE); 1007 } 1008 1009 } 1010 } 1011 } else if (WIFSIGNALED(*status)) { 1012 if (WTERMSIG(*status) == SIGCONT) { 1013 /* 1014 * No need to close things down or anything. 1015 */ 1016 done = FALSE; 1017 } else { 1018 /* 1019 * If it exited non-zero and either we're 1020 * doing things our way or we're not ignoring 1021 * errors, the job is finished. Similarly, if 1022 * the shell died because of a signal the job 1023 * is also finished. In these cases, finish 1024 * out the job's output before printing the 1025 * exit status... 1026 */ 1027 JobClose(job); 1028 if (job->cmdFILE != NULL && 1029 job->cmdFILE != stdout) { 1030 fclose(job->cmdFILE); 1031 } 1032 done = TRUE; 1033 } 1034 } else { 1035 /* 1036 * No need to close things down or anything. 1037 */ 1038 done = FALSE; 1039 } 1040 1041 if (WIFEXITED(*status)) { 1042 if (done || DEBUG(JOB)) { 1043 FILE *out; 1044 1045 if (compatMake && 1046 !usePipes && 1047 (job->flags & JOB_IGNERR)) { 1048 /* 1049 * If output is going to a file and this job 1050 * is ignoring errors, arrange to have the 1051 * exit status sent to the output file as 1052 * well. 1053 */ 1054 out = fdopen(job->outFd, "w"); 1055 if (out == NULL) 1056 Punt("Cannot fdopen"); 1057 } else { 1058 out = stdout; 1059 } 1060 1061 DEBUGF(JOB, ("Process %jd exited.\n", 1062 (intmax_t)job->pid)); 1063 1064 if (WEXITSTATUS(*status) == 0) { 1065 if (DEBUG(JOB)) { 1066 if (usePipes && job->node != lastNode) { 1067 MESSAGE(out, job->node); 1068 lastNode = job->node; 1069 } 1070 fprintf(out, 1071 "*** Completed successfully\n"); 1072 } 1073 } else { 1074 if (usePipes && job->node != lastNode) { 1075 MESSAGE(out, job->node); 1076 lastNode = job->node; 1077 } 1078 fprintf(out, "*** Error code %d%s\n", 1079 WEXITSTATUS(*status), 1080 (job->flags & JOB_IGNERR) ? 1081 "(ignored)" : ""); 1082 1083 if (job->flags & JOB_IGNERR) { 1084 *status = 0; 1085 } 1086 } 1087 1088 fflush(out); 1089 } 1090 } else if (WIFSIGNALED(*status)) { 1091 if (done || DEBUG(JOB) || (WTERMSIG(*status) == SIGCONT)) { 1092 FILE *out; 1093 1094 if (compatMake && 1095 !usePipes && 1096 (job->flags & JOB_IGNERR)) { 1097 /* 1098 * If output is going to a file and this job 1099 * is ignoring errors, arrange to have the 1100 * exit status sent to the output file as 1101 * well. 1102 */ 1103 out = fdopen(job->outFd, "w"); 1104 if (out == NULL) 1105 Punt("Cannot fdopen"); 1106 } else { 1107 out = stdout; 1108 } 1109 1110 if (WTERMSIG(*status) == SIGCONT) { 1111 /* 1112 * If the beastie has continued, shift the 1113 * Job from the stopped list to the running 1114 * one (or re-stop it if concurrency is 1115 * exceeded) and go and get another child. 1116 */ 1117 if (job->flags & (JOB_RESUME | JOB_RESTART)) { 1118 if (usePipes && job->node != lastNode) { 1119 MESSAGE(out, job->node); 1120 lastNode = job->node; 1121 } 1122 fprintf(out, "*** Continued\n"); 1123 } 1124 if (!(job->flags & JOB_CONTINUING)) { 1125 DEBUGF(JOB, ("Warning: process %jd was not " 1126 "continuing.\n", (intmax_t) job->pid)); 1127#ifdef notdef 1128 /* 1129 * We don't really want to restart a 1130 * job from scratch just because it 1131 * continued, especially not without 1132 * killing the continuing process! 1133 * That's why this is ifdef'ed out. 1134 * FD - 9/17/90 1135 */ 1136 JobRestart(job); 1137#endif 1138 } 1139 job->flags &= ~JOB_CONTINUING; 1140 TAILQ_INSERT_TAIL(&jobs, job, link); 1141 nJobs += 1; 1142 DEBUGF(JOB, ("Process %jd is continuing locally.\n", 1143 (intmax_t) job->pid)); 1144 if (nJobs == maxJobs) { 1145 jobFull = TRUE; 1146 DEBUGF(JOB, ("Job queue is full.\n")); 1147 } 1148 fflush(out); 1149 return; 1150 1151 } else { 1152 if (usePipes && job->node != lastNode) { 1153 MESSAGE(out, job->node); 1154 lastNode = job->node; 1155 } 1156 fprintf(out, 1157 "*** Signal %d\n", WTERMSIG(*status)); 1158 fflush(out); 1159 } 1160 } 1161 } else { 1162 /* STOPPED */ 1163 FILE *out; 1164 1165 if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) { 1166 /* 1167 * If output is going to a file and this job 1168 * is ignoring errors, arrange to have the 1169 * exit status sent to the output file as 1170 * well. 1171 */ 1172 out = fdopen(job->outFd, "w"); 1173 if (out == NULL) 1174 Punt("Cannot fdopen"); 1175 } else { 1176 out = stdout; 1177 } 1178 1179 DEBUGF(JOB, ("Process %jd stopped.\n", (intmax_t) job->pid)); 1180 if (usePipes && job->node != lastNode) { 1181 MESSAGE(out, job->node); 1182 lastNode = job->node; 1183 } 1184 fprintf(out, "*** Stopped -- signal %d\n", WSTOPSIG(*status)); 1185 job->flags |= JOB_RESUME; 1186 TAILQ_INSERT_TAIL(&stoppedJobs, job, link); 1187 fflush(out); 1188 return; 1189 } 1190 1191 /* 1192 * Now handle the -B-mode stuff. If the beast still isn't finished, 1193 * try and restart the job on the next command. If JobStart says it's 1194 * ok, it's ok. If there's an error, this puppy is done. 1195 */ 1196 if (compatMake && WIFEXITED(*status) && 1197 Lst_Succ(job->node->compat_command) != NULL) { 1198 switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) { 1199 case JOB_RUNNING: 1200 done = FALSE; 1201 break; 1202 case JOB_ERROR: 1203 done = TRUE; 1204 W_SETEXITSTATUS(status, 1); 1205 break; 1206 case JOB_FINISHED: 1207 /* 1208 * If we got back a JOB_FINISHED code, JobStart has 1209 * already called Make_Update and freed the job 1210 * descriptor. We set done to false here to avoid fake 1211 * cycles and double frees. JobStart needs to do the 1212 * update so we can proceed up the graph when given 1213 * the -n flag.. 1214 */ 1215 done = FALSE; 1216 break; 1217 default: 1218 break; 1219 } 1220 } else { 1221 done = TRUE; 1222 } 1223 1224 if (done && aborting != ABORT_ERROR && 1225 aborting != ABORT_INTERRUPT && *status == 0) { 1226 /* 1227 * As long as we aren't aborting and the job didn't return a 1228 * non-zero status that we shouldn't ignore, we call 1229 * Make_Update to update the parents. In addition, any saved 1230 * commands for the node are placed on the .END target. 1231 */ 1232 for (ln = job->tailCmds; ln != NULL; ln = LST_NEXT(ln)) { 1233 Lst_AtEnd(&postCommands->commands, 1234 Buf_Peel( 1235 Var_Subst(Lst_Datum(ln), job->node, FALSE))); 1236 } 1237 1238 job->node->made = MADE; 1239 Make_Update(job->node); 1240 free(job); 1241 1242 } else if (*status != 0) { 1243 errors += 1; 1244 free(job); 1245 } 1246 1247 JobRestartJobs(); 1248 1249 /* 1250 * Set aborting if any error. 1251 */ 1252 if (errors && !keepgoing && aborting != ABORT_INTERRUPT) { 1253 /* 1254 * If we found any errors in this batch of children and the -k 1255 * flag wasn't given, we set the aborting flag so no more jobs 1256 * get started. 1257 */ 1258 aborting = ABORT_ERROR; 1259 } 1260 1261 if (aborting == ABORT_ERROR && Job_Empty()) { 1262 /* 1263 * If we are aborting and the job table is now empty, we finish. 1264 */ 1265 Finish(errors); 1266 } 1267} 1268 1269/** 1270 * Job_Touch 1271 * Touch the given target. Called by JobStart when the -t flag was 1272 * given. Prints messages unless told to be silent. 1273 * 1274 * Side Effects: 1275 * The data modification of the file is changed. In addition, if the 1276 * file did not exist, it is created. 1277 */ 1278void 1279Job_Touch(GNode *gn, Boolean silent) 1280{ 1281 int streamID; /* ID of stream opened to do the touch */ 1282 struct utimbuf times; /* Times for utime() call */ 1283 1284 if (gn->type & (OP_JOIN | OP_USE | OP_EXEC | OP_OPTIONAL)) { 1285 /* 1286 * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" 1287 * targets and, as such, shouldn't really be created. 1288 */ 1289 return; 1290 } 1291 1292 if (!silent) { 1293 fprintf(stdout, "touch %s\n", gn->name); 1294 fflush(stdout); 1295 } 1296 1297 if (noExecute) { 1298 return; 1299 } 1300 1301 if (gn->type & OP_ARCHV) { 1302 Arch_Touch(gn); 1303 } else if (gn->type & OP_LIB) { 1304 Arch_TouchLib(gn); 1305 } else { 1306 char *file = gn->path ? gn->path : gn->name; 1307 1308 times.actime = times.modtime = now; 1309 if (utime(file, ×) < 0) { 1310 streamID = open(file, O_RDWR | O_CREAT, 0666); 1311 1312 if (streamID >= 0) { 1313 char c; 1314 1315 /* 1316 * Read and write a byte to the file to change 1317 * the modification time, then close the file. 1318 */ 1319 if (read(streamID, &c, 1) == 1) { 1320 lseek(streamID, (off_t)0, SEEK_SET); 1321 write(streamID, &c, 1); 1322 } 1323 1324 close(streamID); 1325 } else { 1326 fprintf(stdout, "*** couldn't touch %s: %s", 1327 file, strerror(errno)); 1328 fflush(stdout); 1329 } 1330 } 1331 } 1332} 1333 1334/** 1335 * Job_CheckCommands 1336 * Make sure the given node has all the commands it needs. 1337 * 1338 * Results: 1339 * TRUE if the commands list is/was ok. 1340 * 1341 * Side Effects: 1342 * The node will have commands from the .DEFAULT rule added to it 1343 * if it needs them. 1344 */ 1345Boolean 1346Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...)) 1347{ 1348 1349 if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands) && 1350 (gn->type & OP_LIB) == 0) { 1351 /* 1352 * No commands. Look for .DEFAULT rule from which we might infer 1353 * commands. 1354 */ 1355 if (DEFAULT != NULL && !Lst_IsEmpty(&DEFAULT->commands)) { 1356 char *p1; 1357 /* 1358 * Make only looks for a .DEFAULT if the node was 1359 * never the target of an operator, so that's what we 1360 * do too. If a .DEFAULT was given, we substitute its 1361 * commands for gn's commands and set the IMPSRC 1362 * variable to be the target's name The DEFAULT node 1363 * acts like a transformation rule, in that gn also 1364 * inherits any attributes or sources attached to 1365 * .DEFAULT itself. 1366 */ 1367 Make_HandleUse(DEFAULT, gn); 1368 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), gn); 1369 free(p1); 1370 1371 } else if (Dir_MTime(gn) == 0) { 1372 /* 1373 * The node wasn't the target of an operator we have 1374 * no .DEFAULT rule to go on and the target doesn't 1375 * already exist. There's nothing more we can do for 1376 * this branch. If the -k flag wasn't given, we stop 1377 * in our tracks, otherwise we just don't update 1378 * this node's parents so they never get examined. 1379 */ 1380 static const char msg[] = 1381 "make: don't know how to make"; 1382 1383 if (gn->type & OP_OPTIONAL) { 1384 fprintf(stdout, "%s %s(ignored)\n", 1385 msg, gn->name); 1386 fflush(stdout); 1387 } else if (keepgoing) { 1388 fprintf(stdout, "%s %s(continuing)\n", 1389 msg, gn->name); 1390 fflush(stdout); 1391 return (FALSE); 1392 } else { 1393#if OLD_JOKE 1394 if (strcmp(gn->name,"love") == 0) 1395 (*abortProc)("Not war."); 1396 else 1397#endif 1398 (*abortProc)("%s %s. Stop", 1399 msg, gn->name); 1400 return (FALSE); 1401 } 1402 } 1403 } 1404 return (TRUE); 1405} 1406 1407/** 1408 * JobExec 1409 * Execute the shell for the given job. Called from JobStart and 1410 * JobRestart. 1411 * 1412 * Side Effects: 1413 * A shell is executed, outputs is altered and the Job structure added 1414 * to the job table. 1415 */ 1416static void 1417JobExec(Job *job, char **argv) 1418{ 1419 ProcStuff ps; 1420 1421 if (DEBUG(JOB)) { 1422 int i; 1423 1424 DEBUGF(JOB, ("Running %s\n", job->node->name)); 1425 DEBUGF(JOB, ("\tCommand: ")); 1426 for (i = 0; argv[i] != NULL; i++) { 1427 DEBUGF(JOB, ("%s ", argv[i])); 1428 } 1429 DEBUGF(JOB, ("\n")); 1430 } 1431 1432 /* 1433 * Some jobs produce no output and it's disconcerting to have 1434 * no feedback of their running (since they produce no output, the 1435 * banner with their name in it never appears). This is an attempt to 1436 * provide that feedback, even if nothing follows it. 1437 */ 1438 if (lastNode != job->node && (job->flags & JOB_FIRST) && 1439 !(job->flags & JOB_SILENT)) { 1440 MESSAGE(stdout, job->node); 1441 lastNode = job->node; 1442 } 1443 1444 ps.in = FILENO(job->cmdFILE); 1445 if (usePipes) { 1446 /* 1447 * Set up the child's output to be routed through the 1448 * pipe we've created for it. 1449 */ 1450 ps.out = job->outPipe; 1451 } else { 1452 /* 1453 * We're capturing output in a file, so we duplicate 1454 * the descriptor to the temporary file into the 1455 * standard output. 1456 */ 1457 ps.out = job->outFd; 1458 } 1459 ps.err = STDERR_FILENO; 1460 1461 ps.merge_errors = 1; 1462 ps.pgroup = 1; 1463 ps.searchpath = 0; 1464 1465 ps.argv = argv; 1466 ps.argv_free = 0; 1467 1468 /* 1469 * Fork. Warning since we are doing vfork() instead of fork(), 1470 * do not allocate memory in the child process! 1471 */ 1472 if ((ps.child_pid = vfork()) == -1) { 1473 Punt("Cannot fork"); 1474 1475 1476 } else if (ps.child_pid == 0) { 1477 /* 1478 * Child 1479 */ 1480 if (fifoFd >= 0) 1481 close(fifoFd); 1482
| 505 * Wait for child process to terminate. 506 */ 507static int 508ProcWait(ProcStuff *ps) 509{ 510 pid_t pid; 511 int status; 512 513 /* 514 * Wait for the process to exit. 515 */ 516 for (;;) { 517 pid = wait(&status); 518 if (pid == -1 && errno != EINTR) { 519 Fatal("error in wait: %d", pid); 520 /* NOTREACHED */ 521 } 522 if (pid == ps->child_pid) { 523 break; 524 } 525 if (interrupted) { 526 break; 527 } 528 } 529 530 return (status); 531} 532 533/** 534 * JobCatchSignal 535 * Got a signal. Set global variables and hope that someone will 536 * handle it. 537 */ 538static void 539JobCatchSig(int signo) 540{ 541 542 interrupted = signo; 543} 544 545/** 546 * JobPassSig -- 547 * Pass a signal on to all local jobs if 548 * USE_PGRP is defined, then die ourselves. 549 * 550 * Side Effects: 551 * We die by the same signal. 552 */ 553static void 554JobPassSig(int signo) 555{ 556 Job *job; 557 sigset_t nmask, omask; 558 struct sigaction act; 559 560 sigemptyset(&nmask); 561 sigaddset(&nmask, signo); 562 sigprocmask(SIG_SETMASK, &nmask, &omask); 563 564 DEBUGF(JOB, ("JobPassSig(%d) called.\n", signo)); 565 TAILQ_FOREACH(job, &jobs, link) { 566 DEBUGF(JOB, ("JobPassSig passing signal %d to child %jd.\n", 567 signo, (intmax_t)job->pid)); 568 KILL(job->pid, signo); 569 } 570 571 /* 572 * Deal with proper cleanup based on the signal received. We only run 573 * the .INTERRUPT target if the signal was in fact an interrupt. 574 * The other three termination signals are more of a "get out *now*" 575 * command. 576 */ 577 if (signo == SIGINT) { 578 JobInterrupt(TRUE, signo); 579 } else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT) { 580 JobInterrupt(FALSE, signo); 581 } 582 583 /* 584 * Leave gracefully if SIGQUIT, rather than core dumping. 585 */ 586 if (signo == SIGQUIT) { 587 signo = SIGINT; 588 } 589 590 /* 591 * Send ourselves the signal now we've given the message to everyone 592 * else. Note we block everything else possible while we're getting 593 * the signal. This ensures that all our jobs get continued when we 594 * wake up before we take any other signal. 595 * XXX this comment seems wrong. 596 */ 597 act.sa_handler = SIG_DFL; 598 sigemptyset(&act.sa_mask); 599 act.sa_flags = 0; 600 sigaction(signo, &act, NULL); 601 602 DEBUGF(JOB, ("JobPassSig passing signal to self, mask = %x.\n", 603 ~0 & ~(1 << (signo - 1)))); 604 signal(signo, SIG_DFL); 605 606 KILL(getpid(), signo); 607 608 signo = SIGCONT; 609 TAILQ_FOREACH(job, &jobs, link) { 610 DEBUGF(JOB, ("JobPassSig passing signal %d to child %jd.\n", 611 signo, (intmax_t)job->pid)); 612 KILL(job->pid, signo); 613 } 614 615 sigprocmask(SIG_SETMASK, &omask, NULL); 616 sigprocmask(SIG_SETMASK, &omask, NULL); 617 act.sa_handler = JobPassSig; 618 sigaction(signo, &act, NULL); 619} 620 621/** 622 * JobPrintCommand -- 623 * Put out another command for the given job. If the command starts 624 * with an @ or a - we process it specially. In the former case, 625 * so long as the -s and -n flags weren't given to make, we stick 626 * a shell-specific echoOff command in the script. In the latter, 627 * we ignore errors for the entire job, unless the shell has error 628 * control. 629 * If the command is just "..." we take all future commands for this 630 * job to be commands to be executed once the entire graph has been 631 * made and return non-zero to signal that the end of the commands 632 * was reached. These commands are later attached to the postCommands 633 * node and executed by Job_Finish when all things are done. 634 * This function is called from JobStart via LST_FOREACH. 635 * 636 * Results: 637 * Always 0, unless the command was "..." 638 * 639 * Side Effects: 640 * If the command begins with a '-' and the shell has no error control, 641 * the JOB_IGNERR flag is set in the job descriptor. 642 * If the command is "..." and we're not ignoring such things, 643 * tailCmds is set to the successor node of the cmd. 644 * numCommands is incremented if the command is actually printed. 645 */ 646static int 647JobPrintCommand(char *cmd, Job *job) 648{ 649 Boolean noSpecials; /* true if we shouldn't worry about 650 * inserting special commands into 651 * the input stream. */ 652 Boolean shutUp = FALSE; /* true if we put a no echo command 653 * into the command file */ 654 Boolean errOff = FALSE; /* true if we turned error checking 655 * off before printing the command 656 * and need to turn it back on */ 657 const char *cmdTemplate;/* Template to use when printing the command */ 658 char *cmdStart; /* Start of expanded command */ 659 LstNode *cmdNode; /* Node for replacing the command */ 660 661 noSpecials = (noExecute && !(job->node->type & OP_MAKE)); 662 663 if (strcmp(cmd, "...") == 0) { 664 job->node->type |= OP_SAVE_CMDS; 665 if ((job->flags & JOB_IGNDOTS) == 0) { 666 job->tailCmds = 667 Lst_Succ(Lst_Member(&job->node->commands, cmd)); 668 return (1); 669 } 670 return (0); 671 } 672 673#define DBPRINTF(fmt, arg) \ 674 DEBUGF(JOB, (fmt, arg)); \ 675 fprintf(job->cmdFILE, fmt, arg); \ 676 fflush(job->cmdFILE); 677 678 numCommands += 1; 679 680 /* 681 * For debugging, we replace each command with the result of expanding 682 * the variables in the command. 683 */ 684 cmdNode = Lst_Member(&job->node->commands, cmd); 685 686 cmd = Buf_Peel(Var_Subst(cmd, job->node, FALSE)); 687 cmdStart = cmd; 688 689 Lst_Replace(cmdNode, cmdStart); 690 691 cmdTemplate = "%s\n"; 692 693 /* 694 * Check for leading @', -' or +'s to control echoing, error checking, 695 * and execution on -n. 696 */ 697 while (*cmd == '@' || *cmd == '-' || *cmd == '+') { 698 switch (*cmd) { 699 700 case '@': 701 shutUp = DEBUG(LOUD) ? FALSE : TRUE; 702 break; 703 704 case '-': 705 errOff = TRUE; 706 break; 707 708 case '+': 709 if (noSpecials) { 710 /* 711 * We're not actually exececuting anything... 712 * but this one needs to be - use compat mode 713 * just for it. 714 */ 715 Compat_RunCommand(cmd, job->node); 716 return (0); 717 } 718 break; 719 } 720 cmd++; 721 } 722 723 while (isspace((unsigned char)*cmd)) 724 cmd++; 725 726 if (shutUp) { 727 if (!(job->flags & JOB_SILENT) && !noSpecials && 728 commandShell->hasEchoCtl) { 729 DBPRINTF("%s\n", commandShell->echoOff); 730 } else { 731 shutUp = FALSE; 732 } 733 } 734 735 if (errOff) { 736 if (!(job->flags & JOB_IGNERR) && !noSpecials) { 737 if (commandShell->hasErrCtl) { 738 /* 739 * We don't want the error-control commands 740 * showing up either, so we turn off echoing 741 * while executing them. We could put another 742 * field in the shell structure to tell 743 * JobDoOutput to look for this string too, 744 * but why make it any more complex than 745 * it already is? 746 */ 747 if (!(job->flags & JOB_SILENT) && !shutUp && 748 commandShell->hasEchoCtl) { 749 DBPRINTF("%s\n", commandShell->echoOff); 750 DBPRINTF("%s\n", commandShell->ignErr); 751 DBPRINTF("%s\n", commandShell->echoOn); 752 } else { 753 DBPRINTF("%s\n", commandShell->ignErr); 754 } 755 } else if (commandShell->ignErr && 756 *commandShell->ignErr != '\0') { 757 /* 758 * The shell has no error control, so we need to 759 * be weird to get it to ignore any errors from 760 * the command. If echoing is turned on, we turn 761 * it off and use the errCheck template to echo 762 * the command. Leave echoing off so the user 763 * doesn't see the weirdness we go through to 764 * ignore errors. Set cmdTemplate to use the 765 * weirdness instead of the simple "%s\n" 766 * template. 767 */ 768 if (!(job->flags & JOB_SILENT) && !shutUp && 769 commandShell->hasEchoCtl) { 770 DBPRINTF("%s\n", commandShell->echoOff); 771 DBPRINTF(commandShell->errCheck, cmd); 772 shutUp = TRUE; 773 } 774 cmdTemplate = commandShell->ignErr; 775 /* 776 * The error ignoration (hee hee) is already 777 * taken care of by the ignErr template, so 778 * pretend error checking is still on. 779 */ 780 errOff = FALSE; 781 } else { 782 errOff = FALSE; 783 } 784 } else { 785 errOff = FALSE; 786 } 787 } 788 789 DBPRINTF(cmdTemplate, cmd); 790 791 if (errOff) { 792 /* 793 * If echoing is already off, there's no point in issuing the 794 * echoOff command. Otherwise we issue it and pretend it was on 795 * for the whole command... 796 */ 797 if (!shutUp && !(job->flags & JOB_SILENT) && 798 commandShell->hasEchoCtl) { 799 DBPRINTF("%s\n", commandShell->echoOff); 800 shutUp = TRUE; 801 } 802 DBPRINTF("%s\n", commandShell->errCheck); 803 } 804 if (shutUp) { 805 DBPRINTF("%s\n", commandShell->echoOn); 806 } 807 return (0); 808} 809 810/** 811 * JobClose -- 812 * Called to close both input and output pipes when a job is finished. 813 * 814 * Side Effects: 815 * The file descriptors associated with the job are closed. 816 */ 817static void 818JobClose(Job *job) 819{ 820 821 if (usePipes) { 822#if !defined(USE_KQUEUE) 823 FD_CLR(job->inPipe, &outputs); 824#endif 825 if (job->outPipe != job->inPipe) { 826 close(job->outPipe); 827 } 828 JobDoOutput(job, TRUE); 829 close(job->inPipe); 830 } else { 831 close(job->outFd); 832 JobDoOutput(job, TRUE); 833 } 834} 835 836/** 837 * JobFinish -- 838 * Do final processing for the given job including updating 839 * parents and starting new jobs as available/necessary. Note 840 * that we pay no attention to the JOB_IGNERR flag here. 841 * This is because when we're called because of a noexecute flag 842 * or something, jstat.w_status is 0 and when called from 843 * Job_CatchChildren, the status is zeroed if it s/b ignored. 844 * 845 * Side Effects: 846 * Some nodes may be put on the toBeMade queue. 847 * Final commands for the job are placed on postCommands. 848 * 849 * If we got an error and are aborting (aborting == ABORT_ERROR) and 850 * the job list is now empty, we are done for the day. 851 * If we recognized an error (errors !=0), we set the aborting flag 852 * to ABORT_ERROR so no more jobs will be started. 853 */ 854static void 855JobFinish(Job *job, int *status) 856{ 857 Boolean done; 858 LstNode *ln; 859 860 if (WIFEXITED(*status)) { 861 int job_status = WEXITSTATUS(*status); 862 863 JobClose(job); 864 /* 865 * Deal with ignored errors in -B mode. We need to 866 * print a message telling of the ignored error as 867 * well as setting status.w_status to 0 so the next 868 * command gets run. To do this, we set done to be 869 * TRUE if in -B mode and the job exited non-zero. 870 */ 871 if (job_status == 0) { 872 done = FALSE; 873 } else { 874 if (job->flags & JOB_IGNERR) { 875 done = TRUE; 876 } else { 877 /* 878 * If it exited non-zero and either we're 879 * doing things our way or we're not ignoring 880 * errors, the job is finished. Similarly, if 881 * the shell died because of a signal the job 882 * is also finished. In these cases, finish 883 * out the job's output before printing the 884 * exit status... 885 */ 886 done = TRUE; 887 if (job->cmdFILE != NULL && 888 job->cmdFILE != stdout) { 889 fclose(job->cmdFILE); 890 } 891 892 } 893 } 894 } else if (WIFSIGNALED(*status)) { 895 if (WTERMSIG(*status) == SIGCONT) { 896 /* 897 * No need to close things down or anything. 898 */ 899 done = FALSE; 900 } else { 901 /* 902 * If it exited non-zero and either we're 903 * doing things our way or we're not ignoring 904 * errors, the job is finished. Similarly, if 905 * the shell died because of a signal the job 906 * is also finished. In these cases, finish 907 * out the job's output before printing the 908 * exit status... 909 */ 910 JobClose(job); 911 if (job->cmdFILE != NULL && 912 job->cmdFILE != stdout) { 913 fclose(job->cmdFILE); 914 } 915 done = TRUE; 916 } 917 } else { 918 /* 919 * No need to close things down or anything. 920 */ 921 done = FALSE; 922 } 923 924 if (WIFEXITED(*status)) { 925 if (done || DEBUG(JOB)) { 926 FILE *out; 927 928 if (compatMake && 929 !usePipes && 930 (job->flags & JOB_IGNERR)) { 931 /* 932 * If output is going to a file and this job 933 * is ignoring errors, arrange to have the 934 * exit status sent to the output file as 935 * well. 936 */ 937 out = fdopen(job->outFd, "w"); 938 if (out == NULL) 939 Punt("Cannot fdopen"); 940 } else { 941 out = stdout; 942 } 943 944 DEBUGF(JOB, ("Process %jd exited.\n", 945 (intmax_t)job->pid)); 946 947 if (WEXITSTATUS(*status) == 0) { 948 if (DEBUG(JOB)) { 949 if (usePipes && job->node != lastNode) { 950 MESSAGE(out, job->node); 951 lastNode = job->node; 952 } 953 fprintf(out, 954 "*** Completed successfully\n"); 955 } 956 } else { 957 if (usePipes && job->node != lastNode) { 958 MESSAGE(out, job->node); 959 lastNode = job->node; 960 } 961 fprintf(out, "*** Error code %d%s\n", 962 WEXITSTATUS(*status), 963 (job->flags & JOB_IGNERR) ? 964 "(ignored)" : ""); 965 966 if (job->flags & JOB_IGNERR) { 967 *status = 0; 968 } 969 } 970 971 fflush(out); 972 } 973 } else if (WIFSIGNALED(*status)) { 974 if (done || DEBUG(JOB) || (WTERMSIG(*status) == SIGCONT)) { 975 FILE *out; 976 977 if (compatMake && 978 !usePipes && 979 (job->flags & JOB_IGNERR)) { 980 /* 981 * If output is going to a file and this job 982 * is ignoring errors, arrange to have the 983 * exit status sent to the output file as 984 * well. 985 */ 986 out = fdopen(job->outFd, "w"); 987 if (out == NULL) 988 Punt("Cannot fdopen"); 989 } else { 990 out = stdout; 991 } 992 993 if (WTERMSIG(*status) == SIGCONT) { 994 /* 995 * If the beastie has continued, shift the 996 * Job from the stopped list to the running 997 * one (or re-stop it if concurrency is 998 * exceeded) and go and get another child. 999 */ 1000 if (job->flags & (JOB_RESUME | JOB_RESTART)) { 1001 if (usePipes && job->node != lastNode) { 1002 MESSAGE(out, job->node); 1003 lastNode = job->node; 1004 } 1005 fprintf(out, "*** Continued\n"); 1006 } 1007 if (!(job->flags & JOB_CONTINUING)) { 1008 DEBUGF(JOB, ("Warning: process %jd was not " 1009 "continuing.\n", (intmax_t) job->pid)); 1010#ifdef notdef 1011 /* 1012 * We don't really want to restart a 1013 * job from scratch just because it 1014 * continued, especially not without 1015 * killing the continuing process! 1016 * That's why this is ifdef'ed out. 1017 * FD - 9/17/90 1018 */ 1019 JobRestart(job); 1020#endif 1021 } 1022 job->flags &= ~JOB_CONTINUING; 1023 TAILQ_INSERT_TAIL(&jobs, job, link); 1024 nJobs += 1; 1025 DEBUGF(JOB, ("Process %jd is continuing locally.\n", 1026 (intmax_t) job->pid)); 1027 if (nJobs == maxJobs) { 1028 jobFull = TRUE; 1029 DEBUGF(JOB, ("Job queue is full.\n")); 1030 } 1031 fflush(out); 1032 return; 1033 1034 } else { 1035 if (usePipes && job->node != lastNode) { 1036 MESSAGE(out, job->node); 1037 lastNode = job->node; 1038 } 1039 fprintf(out, 1040 "*** Signal %d\n", WTERMSIG(*status)); 1041 fflush(out); 1042 } 1043 } 1044 } else { 1045 /* STOPPED */ 1046 FILE *out; 1047 1048 if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) { 1049 /* 1050 * If output is going to a file and this job 1051 * is ignoring errors, arrange to have the 1052 * exit status sent to the output file as 1053 * well. 1054 */ 1055 out = fdopen(job->outFd, "w"); 1056 if (out == NULL) 1057 Punt("Cannot fdopen"); 1058 } else { 1059 out = stdout; 1060 } 1061 1062 DEBUGF(JOB, ("Process %jd stopped.\n", (intmax_t) job->pid)); 1063 if (usePipes && job->node != lastNode) { 1064 MESSAGE(out, job->node); 1065 lastNode = job->node; 1066 } 1067 fprintf(out, "*** Stopped -- signal %d\n", WSTOPSIG(*status)); 1068 job->flags |= JOB_RESUME; 1069 TAILQ_INSERT_TAIL(&stoppedJobs, job, link); 1070 fflush(out); 1071 return; 1072 } 1073 1074 /* 1075 * Now handle the -B-mode stuff. If the beast still isn't finished, 1076 * try and restart the job on the next command. If JobStart says it's 1077 * ok, it's ok. If there's an error, this puppy is done. 1078 */ 1079 if (compatMake && WIFEXITED(*status) && 1080 Lst_Succ(job->node->compat_command) != NULL) { 1081 switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) { 1082 case JOB_RUNNING: 1083 done = FALSE; 1084 break; 1085 case JOB_ERROR: 1086 done = TRUE; 1087 W_SETEXITSTATUS(status, 1); 1088 break; 1089 case JOB_FINISHED: 1090 /* 1091 * If we got back a JOB_FINISHED code, JobStart has 1092 * already called Make_Update and freed the job 1093 * descriptor. We set done to false here to avoid fake 1094 * cycles and double frees. JobStart needs to do the 1095 * update so we can proceed up the graph when given 1096 * the -n flag.. 1097 */ 1098 done = FALSE; 1099 break; 1100 default: 1101 break; 1102 } 1103 } else { 1104 done = TRUE; 1105 } 1106 1107 if (done && aborting != ABORT_ERROR && 1108 aborting != ABORT_INTERRUPT && *status == 0) { 1109 /* 1110 * As long as we aren't aborting and the job didn't return a 1111 * non-zero status that we shouldn't ignore, we call 1112 * Make_Update to update the parents. In addition, any saved 1113 * commands for the node are placed on the .END target. 1114 */ 1115 for (ln = job->tailCmds; ln != NULL; ln = LST_NEXT(ln)) { 1116 Lst_AtEnd(&postCommands->commands, 1117 Buf_Peel( 1118 Var_Subst(Lst_Datum(ln), job->node, FALSE))); 1119 } 1120 1121 job->node->made = MADE; 1122 Make_Update(job->node); 1123 free(job); 1124 1125 } else if (*status != 0) { 1126 errors += 1; 1127 free(job); 1128 } 1129 1130 JobRestartJobs(); 1131 1132 /* 1133 * Set aborting if any error. 1134 */ 1135 if (errors && !keepgoing && aborting != ABORT_INTERRUPT) { 1136 /* 1137 * If we found any errors in this batch of children and the -k 1138 * flag wasn't given, we set the aborting flag so no more jobs 1139 * get started. 1140 */ 1141 aborting = ABORT_ERROR; 1142 } 1143 1144 if (aborting == ABORT_ERROR && Job_Empty()) { 1145 /* 1146 * If we are aborting and the job table is now empty, we finish. 1147 */ 1148 Finish(errors); 1149 } 1150} 1151 1152/** 1153 * Job_Touch 1154 * Touch the given target. Called by JobStart when the -t flag was 1155 * given. Prints messages unless told to be silent. 1156 * 1157 * Side Effects: 1158 * The data modification of the file is changed. In addition, if the 1159 * file did not exist, it is created. 1160 */ 1161void 1162Job_Touch(GNode *gn, Boolean silent) 1163{ 1164 int streamID; /* ID of stream opened to do the touch */ 1165 struct utimbuf times; /* Times for utime() call */ 1166 1167 if (gn->type & (OP_JOIN | OP_USE | OP_EXEC | OP_OPTIONAL)) { 1168 /* 1169 * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual" 1170 * targets and, as such, shouldn't really be created. 1171 */ 1172 return; 1173 } 1174 1175 if (!silent) { 1176 fprintf(stdout, "touch %s\n", gn->name); 1177 fflush(stdout); 1178 } 1179 1180 if (noExecute) { 1181 return; 1182 } 1183 1184 if (gn->type & OP_ARCHV) { 1185 Arch_Touch(gn); 1186 } else if (gn->type & OP_LIB) { 1187 Arch_TouchLib(gn); 1188 } else { 1189 char *file = gn->path ? gn->path : gn->name; 1190 1191 times.actime = times.modtime = now; 1192 if (utime(file, ×) < 0) { 1193 streamID = open(file, O_RDWR | O_CREAT, 0666); 1194 1195 if (streamID >= 0) { 1196 char c; 1197 1198 /* 1199 * Read and write a byte to the file to change 1200 * the modification time, then close the file. 1201 */ 1202 if (read(streamID, &c, 1) == 1) { 1203 lseek(streamID, (off_t)0, SEEK_SET); 1204 write(streamID, &c, 1); 1205 } 1206 1207 close(streamID); 1208 } else { 1209 fprintf(stdout, "*** couldn't touch %s: %s", 1210 file, strerror(errno)); 1211 fflush(stdout); 1212 } 1213 } 1214 } 1215} 1216 1217/** 1218 * Job_CheckCommands 1219 * Make sure the given node has all the commands it needs. 1220 * 1221 * Results: 1222 * TRUE if the commands list is/was ok. 1223 * 1224 * Side Effects: 1225 * The node will have commands from the .DEFAULT rule added to it 1226 * if it needs them. 1227 */ 1228Boolean 1229Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...)) 1230{ 1231 1232 if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands) && 1233 (gn->type & OP_LIB) == 0) { 1234 /* 1235 * No commands. Look for .DEFAULT rule from which we might infer 1236 * commands. 1237 */ 1238 if (DEFAULT != NULL && !Lst_IsEmpty(&DEFAULT->commands)) { 1239 char *p1; 1240 /* 1241 * Make only looks for a .DEFAULT if the node was 1242 * never the target of an operator, so that's what we 1243 * do too. If a .DEFAULT was given, we substitute its 1244 * commands for gn's commands and set the IMPSRC 1245 * variable to be the target's name The DEFAULT node 1246 * acts like a transformation rule, in that gn also 1247 * inherits any attributes or sources attached to 1248 * .DEFAULT itself. 1249 */ 1250 Make_HandleUse(DEFAULT, gn); 1251 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), gn); 1252 free(p1); 1253 1254 } else if (Dir_MTime(gn) == 0) { 1255 /* 1256 * The node wasn't the target of an operator we have 1257 * no .DEFAULT rule to go on and the target doesn't 1258 * already exist. There's nothing more we can do for 1259 * this branch. If the -k flag wasn't given, we stop 1260 * in our tracks, otherwise we just don't update 1261 * this node's parents so they never get examined. 1262 */ 1263 static const char msg[] = 1264 "make: don't know how to make"; 1265 1266 if (gn->type & OP_OPTIONAL) { 1267 fprintf(stdout, "%s %s(ignored)\n", 1268 msg, gn->name); 1269 fflush(stdout); 1270 } else if (keepgoing) { 1271 fprintf(stdout, "%s %s(continuing)\n", 1272 msg, gn->name); 1273 fflush(stdout); 1274 return (FALSE); 1275 } else { 1276#if OLD_JOKE 1277 if (strcmp(gn->name,"love") == 0) 1278 (*abortProc)("Not war."); 1279 else 1280#endif 1281 (*abortProc)("%s %s. Stop", 1282 msg, gn->name); 1283 return (FALSE); 1284 } 1285 } 1286 } 1287 return (TRUE); 1288} 1289 1290/** 1291 * JobExec 1292 * Execute the shell for the given job. Called from JobStart and 1293 * JobRestart. 1294 * 1295 * Side Effects: 1296 * A shell is executed, outputs is altered and the Job structure added 1297 * to the job table. 1298 */ 1299static void 1300JobExec(Job *job, char **argv) 1301{ 1302 ProcStuff ps; 1303 1304 if (DEBUG(JOB)) { 1305 int i; 1306 1307 DEBUGF(JOB, ("Running %s\n", job->node->name)); 1308 DEBUGF(JOB, ("\tCommand: ")); 1309 for (i = 0; argv[i] != NULL; i++) { 1310 DEBUGF(JOB, ("%s ", argv[i])); 1311 } 1312 DEBUGF(JOB, ("\n")); 1313 } 1314 1315 /* 1316 * Some jobs produce no output and it's disconcerting to have 1317 * no feedback of their running (since they produce no output, the 1318 * banner with their name in it never appears). This is an attempt to 1319 * provide that feedback, even if nothing follows it. 1320 */ 1321 if (lastNode != job->node && (job->flags & JOB_FIRST) && 1322 !(job->flags & JOB_SILENT)) { 1323 MESSAGE(stdout, job->node); 1324 lastNode = job->node; 1325 } 1326 1327 ps.in = FILENO(job->cmdFILE); 1328 if (usePipes) { 1329 /* 1330 * Set up the child's output to be routed through the 1331 * pipe we've created for it. 1332 */ 1333 ps.out = job->outPipe; 1334 } else { 1335 /* 1336 * We're capturing output in a file, so we duplicate 1337 * the descriptor to the temporary file into the 1338 * standard output. 1339 */ 1340 ps.out = job->outFd; 1341 } 1342 ps.err = STDERR_FILENO; 1343 1344 ps.merge_errors = 1; 1345 ps.pgroup = 1; 1346 ps.searchpath = 0; 1347 1348 ps.argv = argv; 1349 ps.argv_free = 0; 1350 1351 /* 1352 * Fork. Warning since we are doing vfork() instead of fork(), 1353 * do not allocate memory in the child process! 1354 */ 1355 if ((ps.child_pid = vfork()) == -1) { 1356 Punt("Cannot fork"); 1357 1358 1359 } else if (ps.child_pid == 0) { 1360 /* 1361 * Child 1362 */ 1363 if (fifoFd >= 0) 1364 close(fifoFd); 1365
|
1483 ProcExec(&ps);
| 1366 Proc_Exec(&ps);
|
1484 /* NOTREACHED */ 1485 } 1486 1487 /* 1488 * Parent 1489 */ 1490 job->pid = ps.child_pid; 1491 1492 if (usePipes && (job->flags & JOB_FIRST)) { 1493 /* 1494 * The first time a job is run for a node, we set the 1495 * current position in the buffer to the beginning and 1496 * mark another stream to watch in the outputs mask. 1497 */ 1498#ifdef USE_KQUEUE 1499 struct kevent kev[2]; 1500#endif 1501 job->curPos = 0; 1502 1503#if defined(USE_KQUEUE) 1504 EV_SET(&kev[0], job->inPipe, EVFILT_READ, EV_ADD, 0, 0, job); 1505 EV_SET(&kev[1], job->pid, EVFILT_PROC, 1506 EV_ADD | EV_ONESHOT, NOTE_EXIT, 0, NULL); 1507 if (kevent(kqfd, kev, 2, NULL, 0, NULL) != 0) { 1508 /* 1509 * kevent() will fail if the job is already 1510 * finished 1511 */ 1512 if (errno != EINTR && errno != EBADF && errno != ESRCH) 1513 Punt("kevent: %s", strerror(errno)); 1514 } 1515#else 1516 FD_SET(job->inPipe, &outputs); 1517#endif /* USE_KQUEUE */ 1518 } 1519 1520 if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1521 fclose(job->cmdFILE); 1522 job->cmdFILE = NULL; 1523 } 1524 1525 /* 1526 * Now the job is actually running, add it to the table. 1527 */ 1528 nJobs += 1; 1529 TAILQ_INSERT_TAIL(&jobs, job, link); 1530 if (nJobs == maxJobs) { 1531 jobFull = TRUE; 1532 } 1533} 1534 1535/** 1536 * JobMakeArgv 1537 * Create the argv needed to execute the shell for a given job. 1538 */ 1539static void 1540JobMakeArgv(Job *job, char **argv) 1541{ 1542 int argc; 1543 static char args[10]; /* For merged arguments */ 1544 1545 argv[0] = commandShell->name; 1546 argc = 1; 1547 1548 if ((commandShell->exit && *commandShell->exit != '-') || 1549 (commandShell->echo && *commandShell->echo != '-')) { 1550 /* 1551 * At least one of the flags doesn't have a minus before it, so 1552 * merge them together. Have to do this because the *(&(@*#*&#$# 1553 * Bourne shell thinks its second argument is a file to source. 1554 * Grrrr. Note the ten-character limitation on the combined 1555 * arguments. 1556 */ 1557 sprintf(args, "-%s%s", (job->flags & JOB_IGNERR) ? "" : 1558 commandShell->exit ? commandShell->exit : "", 1559 (job->flags & JOB_SILENT) ? "" : 1560 commandShell->echo ? commandShell->echo : ""); 1561 1562 if (args[1]) { 1563 argv[argc] = args; 1564 argc++; 1565 } 1566 } else { 1567 if (!(job->flags & JOB_IGNERR) && commandShell->exit) { 1568 argv[argc] = commandShell->exit; 1569 argc++; 1570 } 1571 if (!(job->flags & JOB_SILENT) && commandShell->echo) { 1572 argv[argc] = commandShell->echo; 1573 argc++; 1574 } 1575 } 1576 argv[argc] = NULL; 1577} 1578 1579/** 1580 * JobRestart 1581 * Restart a job that stopped for some reason. The job must be neither 1582 * on the jobs nor on the stoppedJobs list. 1583 * 1584 * Side Effects: 1585 * jobFull will be set if the job couldn't be run. 1586 */ 1587static void 1588JobRestart(Job *job) 1589{ 1590 1591 if (job->flags & JOB_RESTART) { 1592 /* 1593 * Set up the control arguments to the shell. This is based on 1594 * the flags set earlier for this job. If the JOB_IGNERR flag 1595 * is clear, the 'exit' flag of the commandShell is used to 1596 * cause it to exit upon receiving an error. If the JOB_SILENT 1597 * flag is clear, the 'echo' flag of the commandShell is used 1598 * to get it to start echoing as soon as it starts 1599 * processing commands. 1600 */ 1601 char *argv[4]; 1602 1603 JobMakeArgv(job, argv); 1604 1605 DEBUGF(JOB, ("Restarting %s...", job->node->name)); 1606 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) { 1607 /* 1608 * Not allowed to run -- put it back on the hold 1609 * queue and mark the table full 1610 */ 1611 DEBUGF(JOB, ("holding\n")); 1612 TAILQ_INSERT_HEAD(&stoppedJobs, job, link); 1613 jobFull = TRUE; 1614 DEBUGF(JOB, ("Job queue is full.\n")); 1615 return; 1616 } else { 1617 /* 1618 * Job may be run locally. 1619 */ 1620 DEBUGF(JOB, ("running locally\n")); 1621 } 1622 JobExec(job, argv); 1623 1624 } else { 1625 /* 1626 * The job has stopped and needs to be restarted. 1627 * Why it stopped, we don't know... 1628 */ 1629 DEBUGF(JOB, ("Resuming %s...", job->node->name)); 1630 if ((nJobs < maxJobs || ((job->flags & JOB_SPECIAL) && 1631 maxJobs == 0)) && nJobs != maxJobs) { 1632 /* 1633 * If we haven't reached the concurrency limit already 1634 * (or the job must be run and maxJobs is 0), it's ok 1635 * to resume it. 1636 */ 1637 Boolean error; 1638 int status; 1639 1640 error = (KILL(job->pid, SIGCONT) != 0); 1641 1642 if (!error) { 1643 /* 1644 * Make sure the user knows we've continued 1645 * the beast and actually put the thing in the 1646 * job table. 1647 */ 1648 job->flags |= JOB_CONTINUING; 1649 status = 0; 1650 W_SETTERMSIG(&status, SIGCONT); 1651 JobFinish(job, &status); 1652 1653 job->flags &= ~(JOB_RESUME|JOB_CONTINUING); 1654 DEBUGF(JOB, ("done\n")); 1655 } else { 1656 Error("couldn't resume %s: %s", 1657 job->node->name, strerror(errno)); 1658 status = 0; 1659 W_SETEXITSTATUS(&status, 1); 1660 JobFinish(job, &status); 1661 } 1662 } else { 1663 /* 1664 * Job cannot be restarted. Mark the table as full and 1665 * place the job back on the list of stopped jobs. 1666 */ 1667 DEBUGF(JOB, ("table full\n")); 1668 TAILQ_INSERT_HEAD(&stoppedJobs, job, link); 1669 jobFull = TRUE; 1670 DEBUGF(JOB, ("Job queue is full.\n")); 1671 } 1672 } 1673} 1674 1675/** 1676 * JobStart 1677 * Start a target-creation process going for the target described 1678 * by the graph node gn. 1679 * 1680 * Results: 1681 * JOB_ERROR if there was an error in the commands, JOB_FINISHED 1682 * if there isn't actually anything left to do for the job and 1683 * JOB_RUNNING if the job has been started. 1684 * 1685 * Side Effects: 1686 * A new Job node is created and added to the list of running 1687 * jobs. PMake is forked and a child shell created. 1688 */ 1689static int 1690JobStart(GNode *gn, int flags, Job *previous) 1691{ 1692 Job *job; /* new job descriptor */ 1693 char *argv[4]; /* Argument vector to shell */ 1694 Boolean cmdsOK; /* true if the nodes commands were all right */ 1695 Boolean noExec; /* Set true if we decide not to run the job */ 1696 int tfd; /* File descriptor for temp file */ 1697 LstNode *ln; 1698 char tfile[sizeof(TMPPAT)]; 1699 1700 if (interrupted) { 1701 JobPassSig(interrupted); 1702 return (JOB_ERROR); 1703 } 1704 if (previous != NULL) { 1705 previous->flags &= ~(JOB_FIRST | JOB_IGNERR | JOB_SILENT); 1706 job = previous; 1707 } else { 1708 job = emalloc(sizeof(Job)); 1709 flags |= JOB_FIRST; 1710 } 1711 1712 job->node = gn; 1713 job->tailCmds = NULL; 1714 1715 /* 1716 * Set the initial value of the flags for this job based on the global 1717 * ones and the node's attributes... Any flags supplied by the caller 1718 * are also added to the field. 1719 */ 1720 job->flags = 0; 1721 if (Targ_Ignore(gn)) { 1722 job->flags |= JOB_IGNERR; 1723 } 1724 if (Targ_Silent(gn)) { 1725 job->flags |= JOB_SILENT; 1726 } 1727 job->flags |= flags; 1728 1729 /* 1730 * Check the commands now so any attributes from .DEFAULT have a chance 1731 * to migrate to the node. 1732 */ 1733 if (!compatMake && (job->flags & JOB_FIRST)) { 1734 cmdsOK = Job_CheckCommands(gn, Error); 1735 } else { 1736 cmdsOK = TRUE; 1737 } 1738 1739 /* 1740 * If the -n flag wasn't given, we open up OUR (not the child's) 1741 * temporary file to stuff commands in it. The thing is rd/wr so we 1742 * don't need to reopen it to feed it to the shell. If the -n flag 1743 * *was* given, we just set the file to be stdout. Cute, huh? 1744 */ 1745 if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) { 1746 /* 1747 * We're serious here, but if the commands were bogus, we're 1748 * also dead... 1749 */ 1750 if (!cmdsOK) { 1751 DieHorribly(); 1752 } 1753 1754 strcpy(tfile, TMPPAT); 1755 if ((tfd = mkstemp(tfile)) == -1) 1756 Punt("Cannot create temp file: %s", strerror(errno)); 1757 job->cmdFILE = fdopen(tfd, "w+"); 1758 eunlink(tfile); 1759 if (job->cmdFILE == NULL) { 1760 close(tfd); 1761 Punt("Could not open %s", tfile); 1762 } 1763 fcntl(FILENO(job->cmdFILE), F_SETFD, 1); 1764 /* 1765 * Send the commands to the command file, flush all its 1766 * buffers then rewind and remove the thing. 1767 */ 1768 noExec = FALSE; 1769 1770 /* 1771 * Used to be backwards; replace when start doing multiple 1772 * commands per shell. 1773 */ 1774 if (compatMake) { 1775 /* 1776 * Be compatible: If this is the first time for this 1777 * node, verify its commands are ok and open the 1778 * commands list for sequential access by later 1779 * invocations of JobStart. Once that is done, we take 1780 * the next command off the list and print it to the 1781 * command file. If the command was an ellipsis, note 1782 * that there's nothing more to execute. 1783 */ 1784 if (job->flags & JOB_FIRST) 1785 gn->compat_command = Lst_First(&gn->commands); 1786 else 1787 gn->compat_command = 1788 Lst_Succ(gn->compat_command); 1789 1790 if (gn->compat_command == NULL || 1791 JobPrintCommand(Lst_Datum(gn->compat_command), job)) 1792 noExec = TRUE; 1793 1794 if (noExec && !(job->flags & JOB_FIRST)) { 1795 /* 1796 * If we're not going to execute anything, the 1797 * job is done and we need to close down the 1798 * various file descriptors we've opened for 1799 * output, then call JobDoOutput to catch the 1800 * final characters or send the file to the 1801 * screen... Note that the i/o streams are only 1802 * open if this isn't the first job. Note also 1803 * that this could not be done in 1804 * Job_CatchChildren b/c it wasn't clear if 1805 * there were more commands to execute or not... 1806 */ 1807 JobClose(job); 1808 } 1809 } else { 1810 /* 1811 * We can do all the commands at once. hooray for sanity 1812 */ 1813 numCommands = 0; 1814 LST_FOREACH(ln, &gn->commands) { 1815 if (JobPrintCommand(Lst_Datum(ln), job)) 1816 break; 1817 } 1818 1819 /* 1820 * If we didn't print out any commands to the shell 1821 * script, there's not much point in executing the 1822 * shell, is there? 1823 */ 1824 if (numCommands == 0) { 1825 noExec = TRUE; 1826 } 1827 } 1828 1829 } else if (noExecute) { 1830 /* 1831 * Not executing anything -- just print all the commands to 1832 * stdout in one fell swoop. This will still set up 1833 * job->tailCmds correctly. 1834 */ 1835 if (lastNode != gn) { 1836 MESSAGE(stdout, gn); 1837 lastNode = gn; 1838 } 1839 job->cmdFILE = stdout; 1840 1841 /* 1842 * Only print the commands if they're ok, but don't die if 1843 * they're not -- just let the user know they're bad and keep 1844 * going. It doesn't do any harm in this case and may do 1845 * some good. 1846 */ 1847 if (cmdsOK) { 1848 LST_FOREACH(ln, &gn->commands) { 1849 if (JobPrintCommand(Lst_Datum(ln), job)) 1850 break; 1851 } 1852 } 1853 /* 1854 * Don't execute the shell, thank you. 1855 */ 1856 noExec = TRUE; 1857 1858 } else { 1859 /* 1860 * Just touch the target and note that no shell should be 1861 * executed. Set cmdFILE to stdout to make life easier. Check 1862 * the commands, too, but don't die if they're no good -- it 1863 * does no harm to keep working up the graph. 1864 */ 1865 job->cmdFILE = stdout; 1866 Job_Touch(gn, job->flags & JOB_SILENT); 1867 noExec = TRUE; 1868 } 1869 1870 /* 1871 * If we're not supposed to execute a shell, don't. 1872 */ 1873 if (noExec) { 1874 /* 1875 * Unlink and close the command file if we opened one 1876 */ 1877 if (job->cmdFILE != stdout) { 1878 if (job->cmdFILE != NULL) 1879 fclose(job->cmdFILE); 1880 } else { 1881 fflush(stdout); 1882 } 1883 1884 /* 1885 * We only want to work our way up the graph if we aren't here 1886 * because the commands for the job were no good. 1887 */ 1888 if (cmdsOK) { 1889 if (aborting == 0) { 1890 for (ln = job->tailCmds; ln != NULL; 1891 ln = LST_NEXT(ln)) { 1892 Lst_AtEnd(&postCommands->commands, 1893 Buf_Peel(Var_Subst(Lst_Datum(ln), 1894 job->node, FALSE))); 1895 } 1896 job->node->made = MADE; 1897 Make_Update(job->node); 1898 } 1899 free(job); 1900 return(JOB_FINISHED); 1901 } else { 1902 free(job); 1903 return(JOB_ERROR); 1904 } 1905 } else { 1906 fflush(job->cmdFILE); 1907 } 1908 1909 /* 1910 * Set up the control arguments to the shell. This is based on the flags 1911 * set earlier for this job. 1912 */ 1913 JobMakeArgv(job, argv); 1914 1915 /* 1916 * If we're using pipes to catch output, create the pipe by which we'll 1917 * get the shell's output. If we're using files, print out that we're 1918 * starting a job and then set up its temporary-file name. 1919 */ 1920 if (!compatMake || (job->flags & JOB_FIRST)) { 1921 if (usePipes) { 1922 int fd[2]; 1923 1924 if (pipe(fd) == -1) 1925 Punt("Cannot create pipe: %s", strerror(errno)); 1926 job->inPipe = fd[0]; 1927 job->outPipe = fd[1]; 1928 fcntl(job->inPipe, F_SETFD, 1); 1929 fcntl(job->outPipe, F_SETFD, 1); 1930 } else { 1931 fprintf(stdout, "Remaking `%s'\n", gn->name); 1932 fflush(stdout); 1933 strcpy(job->outFile, TMPPAT); 1934 if ((job->outFd = mkstemp(job->outFile)) == -1) 1935 Punt("cannot create temp file: %s", 1936 strerror(errno)); 1937 fcntl(job->outFd, F_SETFD, 1); 1938 } 1939 } 1940 1941 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) && maxJobs != 0) { 1942 /* 1943 * We've hit the limit of concurrency, so put the job on hold 1944 * until some other job finishes. Note that the special jobs 1945 * (.BEGIN, .INTERRUPT and .END) may be run even when the 1946 * limit has been reached (e.g. when maxJobs == 0). 1947 */ 1948 jobFull = TRUE; 1949 1950 DEBUGF(JOB, ("Can only run job locally.\n")); 1951 job->flags |= JOB_RESTART; 1952 TAILQ_INSERT_TAIL(&stoppedJobs, job, link); 1953 } else { 1954 if (nJobs >= maxJobs) { 1955 /* 1956 * If we're running this job as a special case 1957 * (see above), at least say the table is full. 1958 */ 1959 jobFull = TRUE; 1960 DEBUGF(JOB, ("Local job queue is full.\n")); 1961 } 1962 JobExec(job, argv); 1963 } 1964 return (JOB_RUNNING); 1965} 1966 1967static char * 1968JobOutput(Job *job, char *cp, char *endp, int msg) 1969{ 1970 char *ecp; 1971 1972 if (commandShell->noPrint) { 1973 ecp = strstr(cp, commandShell->noPrint); 1974 while (ecp != NULL) { 1975 if (cp != ecp) { 1976 *ecp = '\0'; 1977 if (msg && job->node != lastNode) { 1978 MESSAGE(stdout, job->node); 1979 lastNode = job->node; 1980 } 1981 /* 1982 * The only way there wouldn't be a newline 1983 * after this line is if it were the last in 1984 * the buffer. However, since the non-printable 1985 * comes after it, there must be a newline, so 1986 * we don't print one. 1987 */ 1988 fprintf(stdout, "%s", cp); 1989 fflush(stdout); 1990 } 1991 cp = ecp + strlen(commandShell->noPrint); 1992 if (cp != endp) { 1993 /* 1994 * Still more to print, look again after 1995 * skipping the whitespace following the 1996 * non-printable command.... 1997 */ 1998 cp++; 1999 while (*cp == ' ' || *cp == '\t' || 2000 *cp == '\n') { 2001 cp++; 2002 } 2003 ecp = strstr(cp, commandShell->noPrint); 2004 } else { 2005 return (cp); 2006 } 2007 } 2008 } 2009 return (cp); 2010} 2011 2012/** 2013 * JobDoOutput 2014 * This function is called at different times depending on 2015 * whether the user has specified that output is to be collected 2016 * via pipes or temporary files. In the former case, we are called 2017 * whenever there is something to read on the pipe. We collect more 2018 * output from the given job and store it in the job's outBuf. If 2019 * this makes up a line, we print it tagged by the job's identifier, 2020 * as necessary. 2021 * If output has been collected in a temporary file, we open the 2022 * file and read it line by line, transfering it to our own 2023 * output channel until the file is empty. At which point we 2024 * remove the temporary file. 2025 * In both cases, however, we keep our figurative eye out for the 2026 * 'noPrint' line for the shell from which the output came. If 2027 * we recognize a line, we don't print it. If the command is not 2028 * alone on the line (the character after it is not \0 or \n), we 2029 * do print whatever follows it. 2030 * 2031 * Side Effects: 2032 * curPos may be shifted as may the contents of outBuf. 2033 */ 2034static void 2035JobDoOutput(Job *job, Boolean finish) 2036{ 2037 Boolean gotNL = FALSE; /* true if got a newline */ 2038 Boolean fbuf; /* true if our buffer filled up */ 2039 int nr; /* number of bytes read */ 2040 int i; /* auxiliary index into outBuf */ 2041 int max; /* limit for i (end of current data) */ 2042 int nRead; /* (Temporary) number of bytes read */ 2043 FILE *oFILE; /* Stream pointer to shell's output file */ 2044 char inLine[132]; 2045 2046 if (usePipes) { 2047 /* 2048 * Read as many bytes as will fit in the buffer. 2049 */ 2050 end_loop: 2051 gotNL = FALSE; 2052 fbuf = FALSE; 2053 2054 nRead = read(job->inPipe, &job->outBuf[job->curPos], 2055 JOB_BUFSIZE - job->curPos); 2056 /* 2057 * Check for interrupt here too, because the above read may 2058 * block when the child process is stopped. In this case the 2059 * interrupt will unblock it (we don't use SA_RESTART). 2060 */ 2061 if (interrupted) 2062 JobPassSig(interrupted); 2063 2064 if (nRead < 0) { 2065 DEBUGF(JOB, ("JobDoOutput(piperead)")); 2066 nr = 0; 2067 } else { 2068 nr = nRead; 2069 } 2070 2071 /* 2072 * If we hit the end-of-file (the job is dead), we must flush 2073 * its remaining output, so pretend we read a newline if 2074 * there's any output remaining in the buffer. 2075 * Also clear the 'finish' flag so we stop looping. 2076 */ 2077 if (nr == 0 && job->curPos != 0) { 2078 job->outBuf[job->curPos] = '\n'; 2079 nr = 1; 2080 finish = FALSE; 2081 } else if (nr == 0) { 2082 finish = FALSE; 2083 } 2084 2085 /* 2086 * Look for the last newline in the bytes we just got. If there 2087 * is one, break out of the loop with 'i' as its index and 2088 * gotNL set TRUE. 2089 */ 2090 max = job->curPos + nr; 2091 for (i = job->curPos + nr - 1; i >= job->curPos; i--) { 2092 if (job->outBuf[i] == '\n') { 2093 gotNL = TRUE; 2094 break; 2095 } else if (job->outBuf[i] == '\0') { 2096 /* 2097 * Why? 2098 */ 2099 job->outBuf[i] = ' '; 2100 } 2101 } 2102 2103 if (!gotNL) { 2104 job->curPos += nr; 2105 if (job->curPos == JOB_BUFSIZE) { 2106 /* 2107 * If we've run out of buffer space, we have 2108 * no choice but to print the stuff. sigh. 2109 */ 2110 fbuf = TRUE; 2111 i = job->curPos; 2112 } 2113 } 2114 if (gotNL || fbuf) { 2115 /* 2116 * Need to send the output to the screen. Null terminate 2117 * it first, overwriting the newline character if there 2118 * was one. So long as the line isn't one we should 2119 * filter (according to the shell description), we print 2120 * the line, preceded by a target banner if this target 2121 * isn't the same as the one for which we last printed 2122 * something. The rest of the data in the buffer are 2123 * then shifted down to the start of the buffer and 2124 * curPos is set accordingly. 2125 */ 2126 job->outBuf[i] = '\0'; 2127 if (i >= job->curPos) { 2128 char *cp; 2129 2130 cp = JobOutput(job, job->outBuf, 2131 &job->outBuf[i], FALSE); 2132 2133 /* 2134 * There's still more in that buffer. This time, 2135 * though, we know there's no newline at the 2136 * end, so we add one of our own free will. 2137 */ 2138 if (*cp != '\0') { 2139 if (job->node != lastNode) { 2140 MESSAGE(stdout, job->node); 2141 lastNode = job->node; 2142 } 2143 fprintf(stdout, "%s%s", cp, 2144 gotNL ? "\n" : ""); 2145 fflush(stdout); 2146 } 2147 } 2148 if (i < max - 1) { 2149 /* shift the remaining characters down */ 2150 memcpy(job->outBuf, &job->outBuf[i + 1], 2151 max - (i + 1)); 2152 job->curPos = max - (i + 1); 2153 2154 } else { 2155 /* 2156 * We have written everything out, so we just 2157 * start over from the start of the buffer. 2158 * No copying. No nothing. 2159 */ 2160 job->curPos = 0; 2161 } 2162 } 2163 if (finish) { 2164 /* 2165 * If the finish flag is true, we must loop until we hit 2166 * end-of-file on the pipe. This is guaranteed to happen 2167 * eventually since the other end of the pipe is now 2168 * closed (we closed it explicitly and the child has 2169 * exited). When we do get an EOF, finish will be set 2170 * FALSE and we'll fall through and out. 2171 */ 2172 goto end_loop; 2173 } 2174 2175 } else { 2176 /* 2177 * We've been called to retrieve the output of the job from the 2178 * temporary file where it's been squirreled away. This consists 2179 * of opening the file, reading the output line by line, being 2180 * sure not to print the noPrint line for the shell we used, 2181 * then close and remove the temporary file. Very simple. 2182 * 2183 * Change to read in blocks and do FindSubString type things 2184 * as for pipes? That would allow for "@echo -n..." 2185 */ 2186 oFILE = fopen(job->outFile, "r"); 2187 if (oFILE != NULL) { 2188 fprintf(stdout, "Results of making %s:\n", 2189 job->node->name); 2190 fflush(stdout); 2191 2192 while (fgets(inLine, sizeof(inLine), oFILE) != NULL) { 2193 char *cp, *endp, *oendp; 2194 2195 cp = inLine; 2196 oendp = endp = inLine + strlen(inLine); 2197 if (endp[-1] == '\n') { 2198 *--endp = '\0'; 2199 } 2200 cp = JobOutput(job, inLine, endp, FALSE); 2201 2202 /* 2203 * There's still more in that buffer. This time, 2204 * though, we know there's no newline at the 2205 * end, so we add one of our own free will. 2206 */ 2207 fprintf(stdout, "%s", cp); 2208 fflush(stdout); 2209 if (endp != oendp) { 2210 fprintf(stdout, "\n"); 2211 fflush(stdout); 2212 } 2213 } 2214 fclose(oFILE); 2215 eunlink(job->outFile); 2216 } 2217 } 2218} 2219 2220/** 2221 * Job_CatchChildren 2222 * Handle the exit of a child. Called from Make_Make. 2223 * 2224 * Side Effects: 2225 * The job descriptor is removed from the list of children. 2226 * 2227 * Notes: 2228 * We do waits, blocking or not, according to the wisdom of our 2229 * caller, until there are no more children to report. For each 2230 * job, call JobFinish to finish things off. This will take care of 2231 * putting jobs on the stoppedJobs queue. 2232 */ 2233void 2234Job_CatchChildren(Boolean block) 2235{ 2236 pid_t pid; /* pid of dead child */ 2237 Job *job; /* job descriptor for dead child */ 2238 int status; /* Exit/termination status */ 2239 2240 /* 2241 * Don't even bother if we know there's no one around. 2242 */ 2243 if (nJobs == 0) { 2244 return; 2245 } 2246 2247 for (;;) { 2248 pid = waitpid((pid_t)-1, &status, 2249 (block ? 0 : WNOHANG) | WUNTRACED); 2250 if (pid <= 0) 2251 break; 2252 2253 DEBUGF(JOB, ("Process %jd exited or stopped.\n", 2254 (intmax_t)pid)); 2255 2256 TAILQ_FOREACH(job, &jobs, link) { 2257 if (job->pid == pid) 2258 break; 2259 } 2260 2261 if (job == NULL) { 2262 if (WIFSIGNALED(status) && 2263 (WTERMSIG(status) == SIGCONT)) { 2264 TAILQ_FOREACH(job, &jobs, link) { 2265 if (job->pid == pid) 2266 break; 2267 } 2268 if (job == NULL) { 2269 Error("Resumed child (%jd) " 2270 "not in table", (intmax_t)pid); 2271 continue; 2272 } 2273 TAILQ_REMOVE(&stoppedJobs, job, link); 2274 } else { 2275 Error("Child (%jd) not in table?", 2276 (intmax_t)pid); 2277 continue; 2278 } 2279 } else { 2280 TAILQ_REMOVE(&jobs, job, link); 2281 nJobs -= 1; 2282 if (fifoFd >= 0 && maxJobs > 1) { 2283 write(fifoFd, "+", 1); 2284 maxJobs--; 2285 if (nJobs >= maxJobs) 2286 jobFull = TRUE; 2287 else 2288 jobFull = FALSE; 2289 } else { 2290 DEBUGF(JOB, ("Job queue is no longer full.\n")); 2291 jobFull = FALSE; 2292 } 2293 } 2294 2295 JobFinish(job, &status); 2296 } 2297 if (interrupted) 2298 JobPassSig(interrupted); 2299} 2300 2301/** 2302 * Job_CatchOutput 2303 * Catch the output from our children, if we're using 2304 * pipes do so. Otherwise just block time until we get a 2305 * signal(most likely a SIGCHLD) since there's no point in 2306 * just spinning when there's nothing to do and the reaping 2307 * of a child can wait for a while. 2308 * 2309 * Side Effects: 2310 * Output is read from pipes if we're piping. 2311 * ----------------------------------------------------------------------- 2312 */ 2313void 2314#ifdef USE_KQUEUE 2315Job_CatchOutput(int flag __unused) 2316#else 2317Job_CatchOutput(int flag) 2318#endif 2319{ 2320 int nfds; 2321#ifdef USE_KQUEUE 2322#define KEV_SIZE 4 2323 struct kevent kev[KEV_SIZE]; 2324 int i; 2325#else 2326 struct timeval timeout; 2327 fd_set readfds; 2328 Job *job; 2329#endif 2330 2331 fflush(stdout); 2332 2333 if (usePipes) { 2334#ifdef USE_KQUEUE 2335 if ((nfds = kevent(kqfd, NULL, 0, kev, KEV_SIZE, NULL)) == -1) { 2336 if (errno != EINTR) 2337 Punt("kevent: %s", strerror(errno)); 2338 if (interrupted) 2339 JobPassSig(interrupted); 2340 } else { 2341 for (i = 0; i < nfds; i++) { 2342 if (kev[i].flags & EV_ERROR) { 2343 warnc(kev[i].data, "kevent"); 2344 continue; 2345 } 2346 switch (kev[i].filter) { 2347 case EVFILT_READ: 2348 JobDoOutput(kev[i].udata, FALSE); 2349 break; 2350 case EVFILT_PROC: 2351 /* 2352 * Just wake up and let 2353 * Job_CatchChildren() collect the 2354 * terminated job. 2355 */ 2356 break; 2357 } 2358 } 2359 } 2360#else 2361 readfds = outputs; 2362 timeout.tv_sec = SEL_SEC; 2363 timeout.tv_usec = SEL_USEC; 2364 if (flag && jobFull && fifoFd >= 0) 2365 FD_SET(fifoFd, &readfds); 2366 2367 nfds = select(FD_SETSIZE, &readfds, (fd_set *)NULL, 2368 (fd_set *)NULL, &timeout); 2369 if (nfds <= 0) { 2370 if (interrupted) 2371 JobPassSig(interrupted); 2372 return; 2373 } 2374 if (fifoFd >= 0 && FD_ISSET(fifoFd, &readfds)) { 2375 if (--nfds <= 0) 2376 return; 2377 } 2378 job = TAILQ_FIRST(&jobs); 2379 while (nfds != 0 && job != NULL) { 2380 if (FD_ISSET(job->inPipe, &readfds)) { 2381 JobDoOutput(job, FALSE); 2382 nfds--; 2383 } 2384 job = TAILQ_NEXT(job, link); 2385 } 2386#endif /* !USE_KQUEUE */ 2387 } 2388} 2389 2390/** 2391 * Job_Make 2392 * Start the creation of a target. Basically a front-end for 2393 * JobStart used by the Make module. 2394 * 2395 * Side Effects: 2396 * Another job is started. 2397 */ 2398void 2399Job_Make(GNode *gn) 2400{ 2401 2402 JobStart(gn, 0, NULL); 2403} 2404 2405/** 2406 * Job_Init 2407 * Initialize the process module, given a maximum number of jobs. 2408 * 2409 * Side Effects: 2410 * lists and counters are initialized 2411 */ 2412void 2413Job_Init(int maxproc) 2414{ 2415 GNode *begin; /* node for commands to do at the very start */ 2416 const char *env; 2417 struct sigaction sa; 2418 2419 fifoFd = -1; 2420 env = getenv("MAKE_JOBS_FIFO"); 2421 2422 if (env == NULL && maxproc > 1) { 2423 /* 2424 * We did not find the environment variable so we are the 2425 * leader. Create the fifo, open it, write one char per 2426 * allowed job into the pipe. 2427 */ 2428 fifoFd = mkfifotemp(fifoName); 2429 if (fifoFd < 0) { 2430 env = NULL; 2431 } else { 2432 fifoMaster = 1; 2433 fcntl(fifoFd, F_SETFL, O_NONBLOCK); 2434 env = fifoName; 2435 setenv("MAKE_JOBS_FIFO", env, 1); 2436 while (maxproc-- > 0) { 2437 write(fifoFd, "+", 1); 2438 } 2439 /* The master make does not get a magic token */ 2440 jobFull = TRUE; 2441 maxJobs = 0; 2442 } 2443 2444 } else if (env != NULL) { 2445 /* 2446 * We had the environment variable so we are a slave. 2447 * Open fifo and give ourselves a magic token which represents 2448 * the token our parent make has grabbed to start his make 2449 * process. Otherwise the sub-makes would gobble up tokens and 2450 * the proper number of tokens to specify to -j would depend 2451 * on the depth of the tree and the order of execution. 2452 */ 2453 fifoFd = open(env, O_RDWR, 0); 2454 if (fifoFd >= 0) { 2455 fcntl(fifoFd, F_SETFL, O_NONBLOCK); 2456 maxJobs = 1; 2457 jobFull = FALSE; 2458 } 2459 } 2460 if (fifoFd <= 0) { 2461 maxJobs = maxproc; 2462 jobFull = FALSE; 2463 } else { 2464 } 2465 nJobs = 0; 2466 2467 aborting = 0; 2468 errors = 0; 2469 2470 lastNode = NULL; 2471 2472 if ((maxJobs == 1 && fifoFd < 0) || beVerbose == 0) { 2473 /* 2474 * If only one job can run at a time, there's no need for a 2475 * banner, no is there? 2476 */ 2477 targFmt = ""; 2478 } else { 2479 targFmt = TARG_FMT; 2480 } 2481 2482 /* 2483 * Catch the four signals that POSIX specifies if they aren't ignored. 2484 * JobCatchSignal will just set global variables and hope someone 2485 * else is going to handle the interrupt. 2486 */ 2487 sa.sa_handler = JobCatchSig; 2488 sigemptyset(&sa.sa_mask); 2489 sa.sa_flags = 0; 2490 2491 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 2492 sigaction(SIGINT, &sa, NULL); 2493 } 2494 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 2495 sigaction(SIGHUP, &sa, NULL); 2496 } 2497 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 2498 sigaction(SIGQUIT, &sa, NULL); 2499 } 2500 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 2501 sigaction(SIGTERM, &sa, NULL); 2502 } 2503 /* 2504 * There are additional signals that need to be caught and passed if 2505 * either the export system wants to be told directly of signals or if 2506 * we're giving each job its own process group (since then it won't get 2507 * signals from the terminal driver as we own the terminal) 2508 */ 2509#if defined(USE_PGRP) 2510 if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) { 2511 sigaction(SIGTSTP, &sa, NULL); 2512 } 2513 if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) { 2514 sigaction(SIGTTOU, &sa, NULL); 2515 } 2516 if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) { 2517 sigaction(SIGTTIN, &sa, NULL); 2518 } 2519 if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) { 2520 sigaction(SIGWINCH, &sa, NULL); 2521 } 2522#endif 2523 2524#ifdef USE_KQUEUE 2525 if ((kqfd = kqueue()) == -1) { 2526 Punt("kqueue: %s", strerror(errno)); 2527 } 2528#endif 2529 2530 begin = Targ_FindNode(".BEGIN", TARG_NOCREATE); 2531 2532 if (begin != NULL) { 2533 JobStart(begin, JOB_SPECIAL, (Job *)NULL); 2534 while (nJobs) { 2535 Job_CatchOutput(0); 2536 Job_CatchChildren(!usePipes); 2537 } 2538 } 2539 postCommands = Targ_FindNode(".END", TARG_CREATE); 2540} 2541 2542/** 2543 * Job_Full 2544 * See if the job table is full. It is considered full if it is OR 2545 * if we are in the process of aborting OR if we have 2546 * reached/exceeded our local quota. This prevents any more jobs 2547 * from starting up. 2548 * 2549 * Results: 2550 * TRUE if the job table is full, FALSE otherwise 2551 */ 2552Boolean 2553Job_Full(void) 2554{ 2555 char c; 2556 int i; 2557 2558 if (aborting) 2559 return (aborting); 2560 if (fifoFd >= 0 && jobFull) { 2561 i = read(fifoFd, &c, 1); 2562 if (i > 0) { 2563 maxJobs++; 2564 jobFull = FALSE; 2565 } 2566 } 2567 return (jobFull); 2568} 2569 2570/** 2571 * Job_Empty 2572 * See if the job table is empty. Because the local concurrency may 2573 * be set to 0, it is possible for the job table to become empty, 2574 * while the list of stoppedJobs remains non-empty. In such a case, 2575 * we want to restart as many jobs as we can. 2576 * 2577 * Results: 2578 * TRUE if it is. FALSE if it ain't. 2579 */ 2580Boolean 2581Job_Empty(void) 2582{ 2583 if (nJobs == 0) { 2584 if (!TAILQ_EMPTY(&stoppedJobs) && !aborting) { 2585 /* 2586 * The job table is obviously not full if it has no 2587 * jobs in it...Try and restart the stopped jobs. 2588 */ 2589 jobFull = FALSE; 2590 JobRestartJobs(); 2591 return (FALSE); 2592 } else { 2593 return (TRUE); 2594 } 2595 } else { 2596 return (FALSE); 2597 } 2598} 2599 2600/** 2601 * JobInterrupt 2602 * Handle the receipt of an interrupt. 2603 * 2604 * Side Effects: 2605 * All children are killed. Another job will be started if the 2606 * .INTERRUPT target was given. 2607 */ 2608static void 2609JobInterrupt(int runINTERRUPT, int signo) 2610{ 2611 Job *job; /* job descriptor in that element */ 2612 GNode *interrupt; /* the node describing the .INTERRUPT target */ 2613 2614 aborting = ABORT_INTERRUPT; 2615 2616 TAILQ_FOREACH(job, &jobs, link) { 2617 if (!Targ_Precious(job->node)) { 2618 char *file = (job->node->path == NULL ? 2619 job->node->name : job->node->path); 2620 2621 if (!noExecute && eunlink(file) != -1) { 2622 Error("*** %s removed", file); 2623 } 2624 } 2625 if (job->pid) { 2626 DEBUGF(JOB, ("JobInterrupt passing signal to child " 2627 "%jd.\n", (intmax_t)job->pid)); 2628 KILL(job->pid, signo); 2629 } 2630 } 2631 2632 if (runINTERRUPT && !touchFlag) { 2633 /* 2634 * clear the interrupted flag because we would get an 2635 * infinite loop otherwise. 2636 */ 2637 interrupted = 0; 2638 2639 interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 2640 if (interrupt != NULL) { 2641 ignoreErrors = FALSE; 2642 2643 JobStart(interrupt, JOB_IGNDOTS, (Job *)NULL); 2644 while (nJobs) { 2645 Job_CatchOutput(0); 2646 Job_CatchChildren(!usePipes); 2647 } 2648 } 2649 } 2650} 2651 2652/** 2653 * Job_Finish 2654 * Do final processing such as the running of the commands 2655 * attached to the .END target. 2656 * 2657 * Results: 2658 * Number of errors reported. 2659 */ 2660int 2661Job_Finish(void) 2662{ 2663 2664 if (postCommands != NULL && !Lst_IsEmpty(&postCommands->commands)) { 2665 if (errors) { 2666 Error("Errors reported so .END ignored"); 2667 } else { 2668 JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL); 2669 2670 while (nJobs) { 2671 Job_CatchOutput(0); 2672 Job_CatchChildren(!usePipes); 2673 } 2674 } 2675 } 2676 if (fifoFd >= 0) { 2677 close(fifoFd); 2678 fifoFd = -1; 2679 if (fifoMaster) 2680 unlink(fifoName); 2681 } 2682 return (errors); 2683} 2684 2685/** 2686 * Job_Wait 2687 * Waits for all running jobs to finish and returns. Sets 'aborting' 2688 * to ABORT_WAIT to prevent other jobs from starting. 2689 * 2690 * Side Effects: 2691 * Currently running jobs finish. 2692 */ 2693void 2694Job_Wait(void) 2695{ 2696 2697 aborting = ABORT_WAIT; 2698 while (nJobs != 0) { 2699 Job_CatchOutput(0); 2700 Job_CatchChildren(!usePipes); 2701 } 2702 aborting = 0; 2703} 2704 2705/** 2706 * Job_AbortAll 2707 * Abort all currently running jobs without handling output or anything. 2708 * This function is to be called only in the event of a major 2709 * error. Most definitely NOT to be called from JobInterrupt. 2710 * 2711 * Side Effects: 2712 * All children are killed, not just the firstborn 2713 */ 2714void 2715Job_AbortAll(void) 2716{ 2717 Job *job; /* the job descriptor in that element */ 2718 int foo; 2719 2720 aborting = ABORT_ERROR; 2721 2722 if (nJobs) { 2723 TAILQ_FOREACH(job, &jobs, link) { 2724 /* 2725 * kill the child process with increasingly drastic 2726 * signals to make darn sure it's dead. 2727 */ 2728 KILL(job->pid, SIGINT); 2729 KILL(job->pid, SIGKILL); 2730 } 2731 } 2732 2733 /* 2734 * Catch as many children as want to report in at first, then give up 2735 */ 2736 while (waitpid((pid_t)-1, &foo, WNOHANG) > 0) 2737 ; 2738} 2739 2740/** 2741 * JobRestartJobs 2742 * Tries to restart stopped jobs if there are slots available. 2743 * Note that this tries to restart them regardless of pending errors. 2744 * It's not good to leave stopped jobs lying around! 2745 * 2746 * Side Effects: 2747 * Resumes(and possibly migrates) jobs. 2748 */ 2749static void 2750JobRestartJobs(void) 2751{ 2752 Job *job; 2753 2754 while (!jobFull && (job = TAILQ_FIRST(&stoppedJobs)) != NULL) { 2755 DEBUGF(JOB, ("Job queue is not full. " 2756 "Restarting a stopped job.\n")); 2757 TAILQ_REMOVE(&stoppedJobs, job, link); 2758 JobRestart(job); 2759 } 2760} 2761 2762/** 2763 * Cmd_Exec 2764 * Execute the command in cmd, and return the output of that command 2765 * in a string. 2766 * 2767 * Results: 2768 * A string containing the output of the command, or the empty string 2769 * If error is not NULL, it contains the reason for the command failure 2770 * Any output sent to stderr in the child process is passed to stderr, 2771 * and not captured in the string. 2772 * 2773 * Side Effects: 2774 * The string must be freed by the caller. 2775 */ 2776Buffer * 2777Cmd_Exec(const char *cmd, const char **error) 2778{ 2779 int fds[2]; /* Pipe streams */ 2780 int status; /* command exit status */ 2781 Buffer *buf; /* buffer to store the result */ 2782 ssize_t rcnt; 2783 ProcStuff ps; 2784 2785 *error = NULL; 2786 buf = Buf_Init(0); 2787 2788 /* 2789 * Open a pipe for fetching its output 2790 */ 2791 if (pipe(fds) == -1) { 2792 *error = "Couldn't create pipe for \"%s\""; 2793 return (buf); 2794 } 2795 2796 /* Set close-on-exec on read side of pipe. */ 2797 fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD) | FD_CLOEXEC); 2798 2799 ps.in = STDIN_FILENO; 2800 ps.out = fds[1]; 2801 ps.err = STDERR_FILENO; 2802 2803 ps.merge_errors = 0; 2804 ps.pgroup = 0; 2805 ps.searchpath = 0; 2806 2807 /* Set up arguments for shell */ 2808 ps.argv = emalloc(4 * sizeof(char *)); 2809 ps.argv[0] = strdup(commandShell->name); 2810 ps.argv[1] = strdup("-c"); 2811 ps.argv[2] = strdup(cmd); 2812 ps.argv[3] = NULL; 2813 ps.argv_free = 1; 2814 2815 /* 2816 * Fork. Warning since we are doing vfork() instead of fork(), 2817 * do not allocate memory in the child process! 2818 */ 2819 if ((ps.child_pid = vfork()) == -1) { 2820 *error = "Couldn't exec \"%s\""; 2821 return (buf); 2822 2823 } else if (ps.child_pid == 0) { 2824 /* 2825 * Child 2826 */
| 1367 /* NOTREACHED */ 1368 } 1369 1370 /* 1371 * Parent 1372 */ 1373 job->pid = ps.child_pid; 1374 1375 if (usePipes && (job->flags & JOB_FIRST)) { 1376 /* 1377 * The first time a job is run for a node, we set the 1378 * current position in the buffer to the beginning and 1379 * mark another stream to watch in the outputs mask. 1380 */ 1381#ifdef USE_KQUEUE 1382 struct kevent kev[2]; 1383#endif 1384 job->curPos = 0; 1385 1386#if defined(USE_KQUEUE) 1387 EV_SET(&kev[0], job->inPipe, EVFILT_READ, EV_ADD, 0, 0, job); 1388 EV_SET(&kev[1], job->pid, EVFILT_PROC, 1389 EV_ADD | EV_ONESHOT, NOTE_EXIT, 0, NULL); 1390 if (kevent(kqfd, kev, 2, NULL, 0, NULL) != 0) { 1391 /* 1392 * kevent() will fail if the job is already 1393 * finished 1394 */ 1395 if (errno != EINTR && errno != EBADF && errno != ESRCH) 1396 Punt("kevent: %s", strerror(errno)); 1397 } 1398#else 1399 FD_SET(job->inPipe, &outputs); 1400#endif /* USE_KQUEUE */ 1401 } 1402 1403 if (job->cmdFILE != NULL && job->cmdFILE != stdout) { 1404 fclose(job->cmdFILE); 1405 job->cmdFILE = NULL; 1406 } 1407 1408 /* 1409 * Now the job is actually running, add it to the table. 1410 */ 1411 nJobs += 1; 1412 TAILQ_INSERT_TAIL(&jobs, job, link); 1413 if (nJobs == maxJobs) { 1414 jobFull = TRUE; 1415 } 1416} 1417 1418/** 1419 * JobMakeArgv 1420 * Create the argv needed to execute the shell for a given job. 1421 */ 1422static void 1423JobMakeArgv(Job *job, char **argv) 1424{ 1425 int argc; 1426 static char args[10]; /* For merged arguments */ 1427 1428 argv[0] = commandShell->name; 1429 argc = 1; 1430 1431 if ((commandShell->exit && *commandShell->exit != '-') || 1432 (commandShell->echo && *commandShell->echo != '-')) { 1433 /* 1434 * At least one of the flags doesn't have a minus before it, so 1435 * merge them together. Have to do this because the *(&(@*#*&#$# 1436 * Bourne shell thinks its second argument is a file to source. 1437 * Grrrr. Note the ten-character limitation on the combined 1438 * arguments. 1439 */ 1440 sprintf(args, "-%s%s", (job->flags & JOB_IGNERR) ? "" : 1441 commandShell->exit ? commandShell->exit : "", 1442 (job->flags & JOB_SILENT) ? "" : 1443 commandShell->echo ? commandShell->echo : ""); 1444 1445 if (args[1]) { 1446 argv[argc] = args; 1447 argc++; 1448 } 1449 } else { 1450 if (!(job->flags & JOB_IGNERR) && commandShell->exit) { 1451 argv[argc] = commandShell->exit; 1452 argc++; 1453 } 1454 if (!(job->flags & JOB_SILENT) && commandShell->echo) { 1455 argv[argc] = commandShell->echo; 1456 argc++; 1457 } 1458 } 1459 argv[argc] = NULL; 1460} 1461 1462/** 1463 * JobRestart 1464 * Restart a job that stopped for some reason. The job must be neither 1465 * on the jobs nor on the stoppedJobs list. 1466 * 1467 * Side Effects: 1468 * jobFull will be set if the job couldn't be run. 1469 */ 1470static void 1471JobRestart(Job *job) 1472{ 1473 1474 if (job->flags & JOB_RESTART) { 1475 /* 1476 * Set up the control arguments to the shell. This is based on 1477 * the flags set earlier for this job. If the JOB_IGNERR flag 1478 * is clear, the 'exit' flag of the commandShell is used to 1479 * cause it to exit upon receiving an error. If the JOB_SILENT 1480 * flag is clear, the 'echo' flag of the commandShell is used 1481 * to get it to start echoing as soon as it starts 1482 * processing commands. 1483 */ 1484 char *argv[4]; 1485 1486 JobMakeArgv(job, argv); 1487 1488 DEBUGF(JOB, ("Restarting %s...", job->node->name)); 1489 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) { 1490 /* 1491 * Not allowed to run -- put it back on the hold 1492 * queue and mark the table full 1493 */ 1494 DEBUGF(JOB, ("holding\n")); 1495 TAILQ_INSERT_HEAD(&stoppedJobs, job, link); 1496 jobFull = TRUE; 1497 DEBUGF(JOB, ("Job queue is full.\n")); 1498 return; 1499 } else { 1500 /* 1501 * Job may be run locally. 1502 */ 1503 DEBUGF(JOB, ("running locally\n")); 1504 } 1505 JobExec(job, argv); 1506 1507 } else { 1508 /* 1509 * The job has stopped and needs to be restarted. 1510 * Why it stopped, we don't know... 1511 */ 1512 DEBUGF(JOB, ("Resuming %s...", job->node->name)); 1513 if ((nJobs < maxJobs || ((job->flags & JOB_SPECIAL) && 1514 maxJobs == 0)) && nJobs != maxJobs) { 1515 /* 1516 * If we haven't reached the concurrency limit already 1517 * (or the job must be run and maxJobs is 0), it's ok 1518 * to resume it. 1519 */ 1520 Boolean error; 1521 int status; 1522 1523 error = (KILL(job->pid, SIGCONT) != 0); 1524 1525 if (!error) { 1526 /* 1527 * Make sure the user knows we've continued 1528 * the beast and actually put the thing in the 1529 * job table. 1530 */ 1531 job->flags |= JOB_CONTINUING; 1532 status = 0; 1533 W_SETTERMSIG(&status, SIGCONT); 1534 JobFinish(job, &status); 1535 1536 job->flags &= ~(JOB_RESUME|JOB_CONTINUING); 1537 DEBUGF(JOB, ("done\n")); 1538 } else { 1539 Error("couldn't resume %s: %s", 1540 job->node->name, strerror(errno)); 1541 status = 0; 1542 W_SETEXITSTATUS(&status, 1); 1543 JobFinish(job, &status); 1544 } 1545 } else { 1546 /* 1547 * Job cannot be restarted. Mark the table as full and 1548 * place the job back on the list of stopped jobs. 1549 */ 1550 DEBUGF(JOB, ("table full\n")); 1551 TAILQ_INSERT_HEAD(&stoppedJobs, job, link); 1552 jobFull = TRUE; 1553 DEBUGF(JOB, ("Job queue is full.\n")); 1554 } 1555 } 1556} 1557 1558/** 1559 * JobStart 1560 * Start a target-creation process going for the target described 1561 * by the graph node gn. 1562 * 1563 * Results: 1564 * JOB_ERROR if there was an error in the commands, JOB_FINISHED 1565 * if there isn't actually anything left to do for the job and 1566 * JOB_RUNNING if the job has been started. 1567 * 1568 * Side Effects: 1569 * A new Job node is created and added to the list of running 1570 * jobs. PMake is forked and a child shell created. 1571 */ 1572static int 1573JobStart(GNode *gn, int flags, Job *previous) 1574{ 1575 Job *job; /* new job descriptor */ 1576 char *argv[4]; /* Argument vector to shell */ 1577 Boolean cmdsOK; /* true if the nodes commands were all right */ 1578 Boolean noExec; /* Set true if we decide not to run the job */ 1579 int tfd; /* File descriptor for temp file */ 1580 LstNode *ln; 1581 char tfile[sizeof(TMPPAT)]; 1582 1583 if (interrupted) { 1584 JobPassSig(interrupted); 1585 return (JOB_ERROR); 1586 } 1587 if (previous != NULL) { 1588 previous->flags &= ~(JOB_FIRST | JOB_IGNERR | JOB_SILENT); 1589 job = previous; 1590 } else { 1591 job = emalloc(sizeof(Job)); 1592 flags |= JOB_FIRST; 1593 } 1594 1595 job->node = gn; 1596 job->tailCmds = NULL; 1597 1598 /* 1599 * Set the initial value of the flags for this job based on the global 1600 * ones and the node's attributes... Any flags supplied by the caller 1601 * are also added to the field. 1602 */ 1603 job->flags = 0; 1604 if (Targ_Ignore(gn)) { 1605 job->flags |= JOB_IGNERR; 1606 } 1607 if (Targ_Silent(gn)) { 1608 job->flags |= JOB_SILENT; 1609 } 1610 job->flags |= flags; 1611 1612 /* 1613 * Check the commands now so any attributes from .DEFAULT have a chance 1614 * to migrate to the node. 1615 */ 1616 if (!compatMake && (job->flags & JOB_FIRST)) { 1617 cmdsOK = Job_CheckCommands(gn, Error); 1618 } else { 1619 cmdsOK = TRUE; 1620 } 1621 1622 /* 1623 * If the -n flag wasn't given, we open up OUR (not the child's) 1624 * temporary file to stuff commands in it. The thing is rd/wr so we 1625 * don't need to reopen it to feed it to the shell. If the -n flag 1626 * *was* given, we just set the file to be stdout. Cute, huh? 1627 */ 1628 if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) { 1629 /* 1630 * We're serious here, but if the commands were bogus, we're 1631 * also dead... 1632 */ 1633 if (!cmdsOK) { 1634 DieHorribly(); 1635 } 1636 1637 strcpy(tfile, TMPPAT); 1638 if ((tfd = mkstemp(tfile)) == -1) 1639 Punt("Cannot create temp file: %s", strerror(errno)); 1640 job->cmdFILE = fdopen(tfd, "w+"); 1641 eunlink(tfile); 1642 if (job->cmdFILE == NULL) { 1643 close(tfd); 1644 Punt("Could not open %s", tfile); 1645 } 1646 fcntl(FILENO(job->cmdFILE), F_SETFD, 1); 1647 /* 1648 * Send the commands to the command file, flush all its 1649 * buffers then rewind and remove the thing. 1650 */ 1651 noExec = FALSE; 1652 1653 /* 1654 * Used to be backwards; replace when start doing multiple 1655 * commands per shell. 1656 */ 1657 if (compatMake) { 1658 /* 1659 * Be compatible: If this is the first time for this 1660 * node, verify its commands are ok and open the 1661 * commands list for sequential access by later 1662 * invocations of JobStart. Once that is done, we take 1663 * the next command off the list and print it to the 1664 * command file. If the command was an ellipsis, note 1665 * that there's nothing more to execute. 1666 */ 1667 if (job->flags & JOB_FIRST) 1668 gn->compat_command = Lst_First(&gn->commands); 1669 else 1670 gn->compat_command = 1671 Lst_Succ(gn->compat_command); 1672 1673 if (gn->compat_command == NULL || 1674 JobPrintCommand(Lst_Datum(gn->compat_command), job)) 1675 noExec = TRUE; 1676 1677 if (noExec && !(job->flags & JOB_FIRST)) { 1678 /* 1679 * If we're not going to execute anything, the 1680 * job is done and we need to close down the 1681 * various file descriptors we've opened for 1682 * output, then call JobDoOutput to catch the 1683 * final characters or send the file to the 1684 * screen... Note that the i/o streams are only 1685 * open if this isn't the first job. Note also 1686 * that this could not be done in 1687 * Job_CatchChildren b/c it wasn't clear if 1688 * there were more commands to execute or not... 1689 */ 1690 JobClose(job); 1691 } 1692 } else { 1693 /* 1694 * We can do all the commands at once. hooray for sanity 1695 */ 1696 numCommands = 0; 1697 LST_FOREACH(ln, &gn->commands) { 1698 if (JobPrintCommand(Lst_Datum(ln), job)) 1699 break; 1700 } 1701 1702 /* 1703 * If we didn't print out any commands to the shell 1704 * script, there's not much point in executing the 1705 * shell, is there? 1706 */ 1707 if (numCommands == 0) { 1708 noExec = TRUE; 1709 } 1710 } 1711 1712 } else if (noExecute) { 1713 /* 1714 * Not executing anything -- just print all the commands to 1715 * stdout in one fell swoop. This will still set up 1716 * job->tailCmds correctly. 1717 */ 1718 if (lastNode != gn) { 1719 MESSAGE(stdout, gn); 1720 lastNode = gn; 1721 } 1722 job->cmdFILE = stdout; 1723 1724 /* 1725 * Only print the commands if they're ok, but don't die if 1726 * they're not -- just let the user know they're bad and keep 1727 * going. It doesn't do any harm in this case and may do 1728 * some good. 1729 */ 1730 if (cmdsOK) { 1731 LST_FOREACH(ln, &gn->commands) { 1732 if (JobPrintCommand(Lst_Datum(ln), job)) 1733 break; 1734 } 1735 } 1736 /* 1737 * Don't execute the shell, thank you. 1738 */ 1739 noExec = TRUE; 1740 1741 } else { 1742 /* 1743 * Just touch the target and note that no shell should be 1744 * executed. Set cmdFILE to stdout to make life easier. Check 1745 * the commands, too, but don't die if they're no good -- it 1746 * does no harm to keep working up the graph. 1747 */ 1748 job->cmdFILE = stdout; 1749 Job_Touch(gn, job->flags & JOB_SILENT); 1750 noExec = TRUE; 1751 } 1752 1753 /* 1754 * If we're not supposed to execute a shell, don't. 1755 */ 1756 if (noExec) { 1757 /* 1758 * Unlink and close the command file if we opened one 1759 */ 1760 if (job->cmdFILE != stdout) { 1761 if (job->cmdFILE != NULL) 1762 fclose(job->cmdFILE); 1763 } else { 1764 fflush(stdout); 1765 } 1766 1767 /* 1768 * We only want to work our way up the graph if we aren't here 1769 * because the commands for the job were no good. 1770 */ 1771 if (cmdsOK) { 1772 if (aborting == 0) { 1773 for (ln = job->tailCmds; ln != NULL; 1774 ln = LST_NEXT(ln)) { 1775 Lst_AtEnd(&postCommands->commands, 1776 Buf_Peel(Var_Subst(Lst_Datum(ln), 1777 job->node, FALSE))); 1778 } 1779 job->node->made = MADE; 1780 Make_Update(job->node); 1781 } 1782 free(job); 1783 return(JOB_FINISHED); 1784 } else { 1785 free(job); 1786 return(JOB_ERROR); 1787 } 1788 } else { 1789 fflush(job->cmdFILE); 1790 } 1791 1792 /* 1793 * Set up the control arguments to the shell. This is based on the flags 1794 * set earlier for this job. 1795 */ 1796 JobMakeArgv(job, argv); 1797 1798 /* 1799 * If we're using pipes to catch output, create the pipe by which we'll 1800 * get the shell's output. If we're using files, print out that we're 1801 * starting a job and then set up its temporary-file name. 1802 */ 1803 if (!compatMake || (job->flags & JOB_FIRST)) { 1804 if (usePipes) { 1805 int fd[2]; 1806 1807 if (pipe(fd) == -1) 1808 Punt("Cannot create pipe: %s", strerror(errno)); 1809 job->inPipe = fd[0]; 1810 job->outPipe = fd[1]; 1811 fcntl(job->inPipe, F_SETFD, 1); 1812 fcntl(job->outPipe, F_SETFD, 1); 1813 } else { 1814 fprintf(stdout, "Remaking `%s'\n", gn->name); 1815 fflush(stdout); 1816 strcpy(job->outFile, TMPPAT); 1817 if ((job->outFd = mkstemp(job->outFile)) == -1) 1818 Punt("cannot create temp file: %s", 1819 strerror(errno)); 1820 fcntl(job->outFd, F_SETFD, 1); 1821 } 1822 } 1823 1824 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) && maxJobs != 0) { 1825 /* 1826 * We've hit the limit of concurrency, so put the job on hold 1827 * until some other job finishes. Note that the special jobs 1828 * (.BEGIN, .INTERRUPT and .END) may be run even when the 1829 * limit has been reached (e.g. when maxJobs == 0). 1830 */ 1831 jobFull = TRUE; 1832 1833 DEBUGF(JOB, ("Can only run job locally.\n")); 1834 job->flags |= JOB_RESTART; 1835 TAILQ_INSERT_TAIL(&stoppedJobs, job, link); 1836 } else { 1837 if (nJobs >= maxJobs) { 1838 /* 1839 * If we're running this job as a special case 1840 * (see above), at least say the table is full. 1841 */ 1842 jobFull = TRUE; 1843 DEBUGF(JOB, ("Local job queue is full.\n")); 1844 } 1845 JobExec(job, argv); 1846 } 1847 return (JOB_RUNNING); 1848} 1849 1850static char * 1851JobOutput(Job *job, char *cp, char *endp, int msg) 1852{ 1853 char *ecp; 1854 1855 if (commandShell->noPrint) { 1856 ecp = strstr(cp, commandShell->noPrint); 1857 while (ecp != NULL) { 1858 if (cp != ecp) { 1859 *ecp = '\0'; 1860 if (msg && job->node != lastNode) { 1861 MESSAGE(stdout, job->node); 1862 lastNode = job->node; 1863 } 1864 /* 1865 * The only way there wouldn't be a newline 1866 * after this line is if it were the last in 1867 * the buffer. However, since the non-printable 1868 * comes after it, there must be a newline, so 1869 * we don't print one. 1870 */ 1871 fprintf(stdout, "%s", cp); 1872 fflush(stdout); 1873 } 1874 cp = ecp + strlen(commandShell->noPrint); 1875 if (cp != endp) { 1876 /* 1877 * Still more to print, look again after 1878 * skipping the whitespace following the 1879 * non-printable command.... 1880 */ 1881 cp++; 1882 while (*cp == ' ' || *cp == '\t' || 1883 *cp == '\n') { 1884 cp++; 1885 } 1886 ecp = strstr(cp, commandShell->noPrint); 1887 } else { 1888 return (cp); 1889 } 1890 } 1891 } 1892 return (cp); 1893} 1894 1895/** 1896 * JobDoOutput 1897 * This function is called at different times depending on 1898 * whether the user has specified that output is to be collected 1899 * via pipes or temporary files. In the former case, we are called 1900 * whenever there is something to read on the pipe. We collect more 1901 * output from the given job and store it in the job's outBuf. If 1902 * this makes up a line, we print it tagged by the job's identifier, 1903 * as necessary. 1904 * If output has been collected in a temporary file, we open the 1905 * file and read it line by line, transfering it to our own 1906 * output channel until the file is empty. At which point we 1907 * remove the temporary file. 1908 * In both cases, however, we keep our figurative eye out for the 1909 * 'noPrint' line for the shell from which the output came. If 1910 * we recognize a line, we don't print it. If the command is not 1911 * alone on the line (the character after it is not \0 or \n), we 1912 * do print whatever follows it. 1913 * 1914 * Side Effects: 1915 * curPos may be shifted as may the contents of outBuf. 1916 */ 1917static void 1918JobDoOutput(Job *job, Boolean finish) 1919{ 1920 Boolean gotNL = FALSE; /* true if got a newline */ 1921 Boolean fbuf; /* true if our buffer filled up */ 1922 int nr; /* number of bytes read */ 1923 int i; /* auxiliary index into outBuf */ 1924 int max; /* limit for i (end of current data) */ 1925 int nRead; /* (Temporary) number of bytes read */ 1926 FILE *oFILE; /* Stream pointer to shell's output file */ 1927 char inLine[132]; 1928 1929 if (usePipes) { 1930 /* 1931 * Read as many bytes as will fit in the buffer. 1932 */ 1933 end_loop: 1934 gotNL = FALSE; 1935 fbuf = FALSE; 1936 1937 nRead = read(job->inPipe, &job->outBuf[job->curPos], 1938 JOB_BUFSIZE - job->curPos); 1939 /* 1940 * Check for interrupt here too, because the above read may 1941 * block when the child process is stopped. In this case the 1942 * interrupt will unblock it (we don't use SA_RESTART). 1943 */ 1944 if (interrupted) 1945 JobPassSig(interrupted); 1946 1947 if (nRead < 0) { 1948 DEBUGF(JOB, ("JobDoOutput(piperead)")); 1949 nr = 0; 1950 } else { 1951 nr = nRead; 1952 } 1953 1954 /* 1955 * If we hit the end-of-file (the job is dead), we must flush 1956 * its remaining output, so pretend we read a newline if 1957 * there's any output remaining in the buffer. 1958 * Also clear the 'finish' flag so we stop looping. 1959 */ 1960 if (nr == 0 && job->curPos != 0) { 1961 job->outBuf[job->curPos] = '\n'; 1962 nr = 1; 1963 finish = FALSE; 1964 } else if (nr == 0) { 1965 finish = FALSE; 1966 } 1967 1968 /* 1969 * Look for the last newline in the bytes we just got. If there 1970 * is one, break out of the loop with 'i' as its index and 1971 * gotNL set TRUE. 1972 */ 1973 max = job->curPos + nr; 1974 for (i = job->curPos + nr - 1; i >= job->curPos; i--) { 1975 if (job->outBuf[i] == '\n') { 1976 gotNL = TRUE; 1977 break; 1978 } else if (job->outBuf[i] == '\0') { 1979 /* 1980 * Why? 1981 */ 1982 job->outBuf[i] = ' '; 1983 } 1984 } 1985 1986 if (!gotNL) { 1987 job->curPos += nr; 1988 if (job->curPos == JOB_BUFSIZE) { 1989 /* 1990 * If we've run out of buffer space, we have 1991 * no choice but to print the stuff. sigh. 1992 */ 1993 fbuf = TRUE; 1994 i = job->curPos; 1995 } 1996 } 1997 if (gotNL || fbuf) { 1998 /* 1999 * Need to send the output to the screen. Null terminate 2000 * it first, overwriting the newline character if there 2001 * was one. So long as the line isn't one we should 2002 * filter (according to the shell description), we print 2003 * the line, preceded by a target banner if this target 2004 * isn't the same as the one for which we last printed 2005 * something. The rest of the data in the buffer are 2006 * then shifted down to the start of the buffer and 2007 * curPos is set accordingly. 2008 */ 2009 job->outBuf[i] = '\0'; 2010 if (i >= job->curPos) { 2011 char *cp; 2012 2013 cp = JobOutput(job, job->outBuf, 2014 &job->outBuf[i], FALSE); 2015 2016 /* 2017 * There's still more in that buffer. This time, 2018 * though, we know there's no newline at the 2019 * end, so we add one of our own free will. 2020 */ 2021 if (*cp != '\0') { 2022 if (job->node != lastNode) { 2023 MESSAGE(stdout, job->node); 2024 lastNode = job->node; 2025 } 2026 fprintf(stdout, "%s%s", cp, 2027 gotNL ? "\n" : ""); 2028 fflush(stdout); 2029 } 2030 } 2031 if (i < max - 1) { 2032 /* shift the remaining characters down */ 2033 memcpy(job->outBuf, &job->outBuf[i + 1], 2034 max - (i + 1)); 2035 job->curPos = max - (i + 1); 2036 2037 } else { 2038 /* 2039 * We have written everything out, so we just 2040 * start over from the start of the buffer. 2041 * No copying. No nothing. 2042 */ 2043 job->curPos = 0; 2044 } 2045 } 2046 if (finish) { 2047 /* 2048 * If the finish flag is true, we must loop until we hit 2049 * end-of-file on the pipe. This is guaranteed to happen 2050 * eventually since the other end of the pipe is now 2051 * closed (we closed it explicitly and the child has 2052 * exited). When we do get an EOF, finish will be set 2053 * FALSE and we'll fall through and out. 2054 */ 2055 goto end_loop; 2056 } 2057 2058 } else { 2059 /* 2060 * We've been called to retrieve the output of the job from the 2061 * temporary file where it's been squirreled away. This consists 2062 * of opening the file, reading the output line by line, being 2063 * sure not to print the noPrint line for the shell we used, 2064 * then close and remove the temporary file. Very simple. 2065 * 2066 * Change to read in blocks and do FindSubString type things 2067 * as for pipes? That would allow for "@echo -n..." 2068 */ 2069 oFILE = fopen(job->outFile, "r"); 2070 if (oFILE != NULL) { 2071 fprintf(stdout, "Results of making %s:\n", 2072 job->node->name); 2073 fflush(stdout); 2074 2075 while (fgets(inLine, sizeof(inLine), oFILE) != NULL) { 2076 char *cp, *endp, *oendp; 2077 2078 cp = inLine; 2079 oendp = endp = inLine + strlen(inLine); 2080 if (endp[-1] == '\n') { 2081 *--endp = '\0'; 2082 } 2083 cp = JobOutput(job, inLine, endp, FALSE); 2084 2085 /* 2086 * There's still more in that buffer. This time, 2087 * though, we know there's no newline at the 2088 * end, so we add one of our own free will. 2089 */ 2090 fprintf(stdout, "%s", cp); 2091 fflush(stdout); 2092 if (endp != oendp) { 2093 fprintf(stdout, "\n"); 2094 fflush(stdout); 2095 } 2096 } 2097 fclose(oFILE); 2098 eunlink(job->outFile); 2099 } 2100 } 2101} 2102 2103/** 2104 * Job_CatchChildren 2105 * Handle the exit of a child. Called from Make_Make. 2106 * 2107 * Side Effects: 2108 * The job descriptor is removed from the list of children. 2109 * 2110 * Notes: 2111 * We do waits, blocking or not, according to the wisdom of our 2112 * caller, until there are no more children to report. For each 2113 * job, call JobFinish to finish things off. This will take care of 2114 * putting jobs on the stoppedJobs queue. 2115 */ 2116void 2117Job_CatchChildren(Boolean block) 2118{ 2119 pid_t pid; /* pid of dead child */ 2120 Job *job; /* job descriptor for dead child */ 2121 int status; /* Exit/termination status */ 2122 2123 /* 2124 * Don't even bother if we know there's no one around. 2125 */ 2126 if (nJobs == 0) { 2127 return; 2128 } 2129 2130 for (;;) { 2131 pid = waitpid((pid_t)-1, &status, 2132 (block ? 0 : WNOHANG) | WUNTRACED); 2133 if (pid <= 0) 2134 break; 2135 2136 DEBUGF(JOB, ("Process %jd exited or stopped.\n", 2137 (intmax_t)pid)); 2138 2139 TAILQ_FOREACH(job, &jobs, link) { 2140 if (job->pid == pid) 2141 break; 2142 } 2143 2144 if (job == NULL) { 2145 if (WIFSIGNALED(status) && 2146 (WTERMSIG(status) == SIGCONT)) { 2147 TAILQ_FOREACH(job, &jobs, link) { 2148 if (job->pid == pid) 2149 break; 2150 } 2151 if (job == NULL) { 2152 Error("Resumed child (%jd) " 2153 "not in table", (intmax_t)pid); 2154 continue; 2155 } 2156 TAILQ_REMOVE(&stoppedJobs, job, link); 2157 } else { 2158 Error("Child (%jd) not in table?", 2159 (intmax_t)pid); 2160 continue; 2161 } 2162 } else { 2163 TAILQ_REMOVE(&jobs, job, link); 2164 nJobs -= 1; 2165 if (fifoFd >= 0 && maxJobs > 1) { 2166 write(fifoFd, "+", 1); 2167 maxJobs--; 2168 if (nJobs >= maxJobs) 2169 jobFull = TRUE; 2170 else 2171 jobFull = FALSE; 2172 } else { 2173 DEBUGF(JOB, ("Job queue is no longer full.\n")); 2174 jobFull = FALSE; 2175 } 2176 } 2177 2178 JobFinish(job, &status); 2179 } 2180 if (interrupted) 2181 JobPassSig(interrupted); 2182} 2183 2184/** 2185 * Job_CatchOutput 2186 * Catch the output from our children, if we're using 2187 * pipes do so. Otherwise just block time until we get a 2188 * signal(most likely a SIGCHLD) since there's no point in 2189 * just spinning when there's nothing to do and the reaping 2190 * of a child can wait for a while. 2191 * 2192 * Side Effects: 2193 * Output is read from pipes if we're piping. 2194 * ----------------------------------------------------------------------- 2195 */ 2196void 2197#ifdef USE_KQUEUE 2198Job_CatchOutput(int flag __unused) 2199#else 2200Job_CatchOutput(int flag) 2201#endif 2202{ 2203 int nfds; 2204#ifdef USE_KQUEUE 2205#define KEV_SIZE 4 2206 struct kevent kev[KEV_SIZE]; 2207 int i; 2208#else 2209 struct timeval timeout; 2210 fd_set readfds; 2211 Job *job; 2212#endif 2213 2214 fflush(stdout); 2215 2216 if (usePipes) { 2217#ifdef USE_KQUEUE 2218 if ((nfds = kevent(kqfd, NULL, 0, kev, KEV_SIZE, NULL)) == -1) { 2219 if (errno != EINTR) 2220 Punt("kevent: %s", strerror(errno)); 2221 if (interrupted) 2222 JobPassSig(interrupted); 2223 } else { 2224 for (i = 0; i < nfds; i++) { 2225 if (kev[i].flags & EV_ERROR) { 2226 warnc(kev[i].data, "kevent"); 2227 continue; 2228 } 2229 switch (kev[i].filter) { 2230 case EVFILT_READ: 2231 JobDoOutput(kev[i].udata, FALSE); 2232 break; 2233 case EVFILT_PROC: 2234 /* 2235 * Just wake up and let 2236 * Job_CatchChildren() collect the 2237 * terminated job. 2238 */ 2239 break; 2240 } 2241 } 2242 } 2243#else 2244 readfds = outputs; 2245 timeout.tv_sec = SEL_SEC; 2246 timeout.tv_usec = SEL_USEC; 2247 if (flag && jobFull && fifoFd >= 0) 2248 FD_SET(fifoFd, &readfds); 2249 2250 nfds = select(FD_SETSIZE, &readfds, (fd_set *)NULL, 2251 (fd_set *)NULL, &timeout); 2252 if (nfds <= 0) { 2253 if (interrupted) 2254 JobPassSig(interrupted); 2255 return; 2256 } 2257 if (fifoFd >= 0 && FD_ISSET(fifoFd, &readfds)) { 2258 if (--nfds <= 0) 2259 return; 2260 } 2261 job = TAILQ_FIRST(&jobs); 2262 while (nfds != 0 && job != NULL) { 2263 if (FD_ISSET(job->inPipe, &readfds)) { 2264 JobDoOutput(job, FALSE); 2265 nfds--; 2266 } 2267 job = TAILQ_NEXT(job, link); 2268 } 2269#endif /* !USE_KQUEUE */ 2270 } 2271} 2272 2273/** 2274 * Job_Make 2275 * Start the creation of a target. Basically a front-end for 2276 * JobStart used by the Make module. 2277 * 2278 * Side Effects: 2279 * Another job is started. 2280 */ 2281void 2282Job_Make(GNode *gn) 2283{ 2284 2285 JobStart(gn, 0, NULL); 2286} 2287 2288/** 2289 * Job_Init 2290 * Initialize the process module, given a maximum number of jobs. 2291 * 2292 * Side Effects: 2293 * lists and counters are initialized 2294 */ 2295void 2296Job_Init(int maxproc) 2297{ 2298 GNode *begin; /* node for commands to do at the very start */ 2299 const char *env; 2300 struct sigaction sa; 2301 2302 fifoFd = -1; 2303 env = getenv("MAKE_JOBS_FIFO"); 2304 2305 if (env == NULL && maxproc > 1) { 2306 /* 2307 * We did not find the environment variable so we are the 2308 * leader. Create the fifo, open it, write one char per 2309 * allowed job into the pipe. 2310 */ 2311 fifoFd = mkfifotemp(fifoName); 2312 if (fifoFd < 0) { 2313 env = NULL; 2314 } else { 2315 fifoMaster = 1; 2316 fcntl(fifoFd, F_SETFL, O_NONBLOCK); 2317 env = fifoName; 2318 setenv("MAKE_JOBS_FIFO", env, 1); 2319 while (maxproc-- > 0) { 2320 write(fifoFd, "+", 1); 2321 } 2322 /* The master make does not get a magic token */ 2323 jobFull = TRUE; 2324 maxJobs = 0; 2325 } 2326 2327 } else if (env != NULL) { 2328 /* 2329 * We had the environment variable so we are a slave. 2330 * Open fifo and give ourselves a magic token which represents 2331 * the token our parent make has grabbed to start his make 2332 * process. Otherwise the sub-makes would gobble up tokens and 2333 * the proper number of tokens to specify to -j would depend 2334 * on the depth of the tree and the order of execution. 2335 */ 2336 fifoFd = open(env, O_RDWR, 0); 2337 if (fifoFd >= 0) { 2338 fcntl(fifoFd, F_SETFL, O_NONBLOCK); 2339 maxJobs = 1; 2340 jobFull = FALSE; 2341 } 2342 } 2343 if (fifoFd <= 0) { 2344 maxJobs = maxproc; 2345 jobFull = FALSE; 2346 } else { 2347 } 2348 nJobs = 0; 2349 2350 aborting = 0; 2351 errors = 0; 2352 2353 lastNode = NULL; 2354 2355 if ((maxJobs == 1 && fifoFd < 0) || beVerbose == 0) { 2356 /* 2357 * If only one job can run at a time, there's no need for a 2358 * banner, no is there? 2359 */ 2360 targFmt = ""; 2361 } else { 2362 targFmt = TARG_FMT; 2363 } 2364 2365 /* 2366 * Catch the four signals that POSIX specifies if they aren't ignored. 2367 * JobCatchSignal will just set global variables and hope someone 2368 * else is going to handle the interrupt. 2369 */ 2370 sa.sa_handler = JobCatchSig; 2371 sigemptyset(&sa.sa_mask); 2372 sa.sa_flags = 0; 2373 2374 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 2375 sigaction(SIGINT, &sa, NULL); 2376 } 2377 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 2378 sigaction(SIGHUP, &sa, NULL); 2379 } 2380 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 2381 sigaction(SIGQUIT, &sa, NULL); 2382 } 2383 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 2384 sigaction(SIGTERM, &sa, NULL); 2385 } 2386 /* 2387 * There are additional signals that need to be caught and passed if 2388 * either the export system wants to be told directly of signals or if 2389 * we're giving each job its own process group (since then it won't get 2390 * signals from the terminal driver as we own the terminal) 2391 */ 2392#if defined(USE_PGRP) 2393 if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) { 2394 sigaction(SIGTSTP, &sa, NULL); 2395 } 2396 if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) { 2397 sigaction(SIGTTOU, &sa, NULL); 2398 } 2399 if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) { 2400 sigaction(SIGTTIN, &sa, NULL); 2401 } 2402 if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) { 2403 sigaction(SIGWINCH, &sa, NULL); 2404 } 2405#endif 2406 2407#ifdef USE_KQUEUE 2408 if ((kqfd = kqueue()) == -1) { 2409 Punt("kqueue: %s", strerror(errno)); 2410 } 2411#endif 2412 2413 begin = Targ_FindNode(".BEGIN", TARG_NOCREATE); 2414 2415 if (begin != NULL) { 2416 JobStart(begin, JOB_SPECIAL, (Job *)NULL); 2417 while (nJobs) { 2418 Job_CatchOutput(0); 2419 Job_CatchChildren(!usePipes); 2420 } 2421 } 2422 postCommands = Targ_FindNode(".END", TARG_CREATE); 2423} 2424 2425/** 2426 * Job_Full 2427 * See if the job table is full. It is considered full if it is OR 2428 * if we are in the process of aborting OR if we have 2429 * reached/exceeded our local quota. This prevents any more jobs 2430 * from starting up. 2431 * 2432 * Results: 2433 * TRUE if the job table is full, FALSE otherwise 2434 */ 2435Boolean 2436Job_Full(void) 2437{ 2438 char c; 2439 int i; 2440 2441 if (aborting) 2442 return (aborting); 2443 if (fifoFd >= 0 && jobFull) { 2444 i = read(fifoFd, &c, 1); 2445 if (i > 0) { 2446 maxJobs++; 2447 jobFull = FALSE; 2448 } 2449 } 2450 return (jobFull); 2451} 2452 2453/** 2454 * Job_Empty 2455 * See if the job table is empty. Because the local concurrency may 2456 * be set to 0, it is possible for the job table to become empty, 2457 * while the list of stoppedJobs remains non-empty. In such a case, 2458 * we want to restart as many jobs as we can. 2459 * 2460 * Results: 2461 * TRUE if it is. FALSE if it ain't. 2462 */ 2463Boolean 2464Job_Empty(void) 2465{ 2466 if (nJobs == 0) { 2467 if (!TAILQ_EMPTY(&stoppedJobs) && !aborting) { 2468 /* 2469 * The job table is obviously not full if it has no 2470 * jobs in it...Try and restart the stopped jobs. 2471 */ 2472 jobFull = FALSE; 2473 JobRestartJobs(); 2474 return (FALSE); 2475 } else { 2476 return (TRUE); 2477 } 2478 } else { 2479 return (FALSE); 2480 } 2481} 2482 2483/** 2484 * JobInterrupt 2485 * Handle the receipt of an interrupt. 2486 * 2487 * Side Effects: 2488 * All children are killed. Another job will be started if the 2489 * .INTERRUPT target was given. 2490 */ 2491static void 2492JobInterrupt(int runINTERRUPT, int signo) 2493{ 2494 Job *job; /* job descriptor in that element */ 2495 GNode *interrupt; /* the node describing the .INTERRUPT target */ 2496 2497 aborting = ABORT_INTERRUPT; 2498 2499 TAILQ_FOREACH(job, &jobs, link) { 2500 if (!Targ_Precious(job->node)) { 2501 char *file = (job->node->path == NULL ? 2502 job->node->name : job->node->path); 2503 2504 if (!noExecute && eunlink(file) != -1) { 2505 Error("*** %s removed", file); 2506 } 2507 } 2508 if (job->pid) { 2509 DEBUGF(JOB, ("JobInterrupt passing signal to child " 2510 "%jd.\n", (intmax_t)job->pid)); 2511 KILL(job->pid, signo); 2512 } 2513 } 2514 2515 if (runINTERRUPT && !touchFlag) { 2516 /* 2517 * clear the interrupted flag because we would get an 2518 * infinite loop otherwise. 2519 */ 2520 interrupted = 0; 2521 2522 interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 2523 if (interrupt != NULL) { 2524 ignoreErrors = FALSE; 2525 2526 JobStart(interrupt, JOB_IGNDOTS, (Job *)NULL); 2527 while (nJobs) { 2528 Job_CatchOutput(0); 2529 Job_CatchChildren(!usePipes); 2530 } 2531 } 2532 } 2533} 2534 2535/** 2536 * Job_Finish 2537 * Do final processing such as the running of the commands 2538 * attached to the .END target. 2539 * 2540 * Results: 2541 * Number of errors reported. 2542 */ 2543int 2544Job_Finish(void) 2545{ 2546 2547 if (postCommands != NULL && !Lst_IsEmpty(&postCommands->commands)) { 2548 if (errors) { 2549 Error("Errors reported so .END ignored"); 2550 } else { 2551 JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL); 2552 2553 while (nJobs) { 2554 Job_CatchOutput(0); 2555 Job_CatchChildren(!usePipes); 2556 } 2557 } 2558 } 2559 if (fifoFd >= 0) { 2560 close(fifoFd); 2561 fifoFd = -1; 2562 if (fifoMaster) 2563 unlink(fifoName); 2564 } 2565 return (errors); 2566} 2567 2568/** 2569 * Job_Wait 2570 * Waits for all running jobs to finish and returns. Sets 'aborting' 2571 * to ABORT_WAIT to prevent other jobs from starting. 2572 * 2573 * Side Effects: 2574 * Currently running jobs finish. 2575 */ 2576void 2577Job_Wait(void) 2578{ 2579 2580 aborting = ABORT_WAIT; 2581 while (nJobs != 0) { 2582 Job_CatchOutput(0); 2583 Job_CatchChildren(!usePipes); 2584 } 2585 aborting = 0; 2586} 2587 2588/** 2589 * Job_AbortAll 2590 * Abort all currently running jobs without handling output or anything. 2591 * This function is to be called only in the event of a major 2592 * error. Most definitely NOT to be called from JobInterrupt. 2593 * 2594 * Side Effects: 2595 * All children are killed, not just the firstborn 2596 */ 2597void 2598Job_AbortAll(void) 2599{ 2600 Job *job; /* the job descriptor in that element */ 2601 int foo; 2602 2603 aborting = ABORT_ERROR; 2604 2605 if (nJobs) { 2606 TAILQ_FOREACH(job, &jobs, link) { 2607 /* 2608 * kill the child process with increasingly drastic 2609 * signals to make darn sure it's dead. 2610 */ 2611 KILL(job->pid, SIGINT); 2612 KILL(job->pid, SIGKILL); 2613 } 2614 } 2615 2616 /* 2617 * Catch as many children as want to report in at first, then give up 2618 */ 2619 while (waitpid((pid_t)-1, &foo, WNOHANG) > 0) 2620 ; 2621} 2622 2623/** 2624 * JobRestartJobs 2625 * Tries to restart stopped jobs if there are slots available. 2626 * Note that this tries to restart them regardless of pending errors. 2627 * It's not good to leave stopped jobs lying around! 2628 * 2629 * Side Effects: 2630 * Resumes(and possibly migrates) jobs. 2631 */ 2632static void 2633JobRestartJobs(void) 2634{ 2635 Job *job; 2636 2637 while (!jobFull && (job = TAILQ_FIRST(&stoppedJobs)) != NULL) { 2638 DEBUGF(JOB, ("Job queue is not full. " 2639 "Restarting a stopped job.\n")); 2640 TAILQ_REMOVE(&stoppedJobs, job, link); 2641 JobRestart(job); 2642 } 2643} 2644 2645/** 2646 * Cmd_Exec 2647 * Execute the command in cmd, and return the output of that command 2648 * in a string. 2649 * 2650 * Results: 2651 * A string containing the output of the command, or the empty string 2652 * If error is not NULL, it contains the reason for the command failure 2653 * Any output sent to stderr in the child process is passed to stderr, 2654 * and not captured in the string. 2655 * 2656 * Side Effects: 2657 * The string must be freed by the caller. 2658 */ 2659Buffer * 2660Cmd_Exec(const char *cmd, const char **error) 2661{ 2662 int fds[2]; /* Pipe streams */ 2663 int status; /* command exit status */ 2664 Buffer *buf; /* buffer to store the result */ 2665 ssize_t rcnt; 2666 ProcStuff ps; 2667 2668 *error = NULL; 2669 buf = Buf_Init(0); 2670 2671 /* 2672 * Open a pipe for fetching its output 2673 */ 2674 if (pipe(fds) == -1) { 2675 *error = "Couldn't create pipe for \"%s\""; 2676 return (buf); 2677 } 2678 2679 /* Set close-on-exec on read side of pipe. */ 2680 fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD) | FD_CLOEXEC); 2681 2682 ps.in = STDIN_FILENO; 2683 ps.out = fds[1]; 2684 ps.err = STDERR_FILENO; 2685 2686 ps.merge_errors = 0; 2687 ps.pgroup = 0; 2688 ps.searchpath = 0; 2689 2690 /* Set up arguments for shell */ 2691 ps.argv = emalloc(4 * sizeof(char *)); 2692 ps.argv[0] = strdup(commandShell->name); 2693 ps.argv[1] = strdup("-c"); 2694 ps.argv[2] = strdup(cmd); 2695 ps.argv[3] = NULL; 2696 ps.argv_free = 1; 2697 2698 /* 2699 * Fork. Warning since we are doing vfork() instead of fork(), 2700 * do not allocate memory in the child process! 2701 */ 2702 if ((ps.child_pid = vfork()) == -1) { 2703 *error = "Couldn't exec \"%s\""; 2704 return (buf); 2705 2706 } else if (ps.child_pid == 0) { 2707 /* 2708 * Child 2709 */
|
2827 ProcExec(&ps);
| 2710 Proc_Exec(&ps);
|
2828 /* NOTREACHED */ 2829 } 2830 2831 free(ps.argv[2]); 2832 free(ps.argv[1]); 2833 free(ps.argv[0]); 2834 free(ps.argv); 2835 2836 close(fds[1]); /* No need for the writing half of the pipe. */ 2837 2838 do { 2839 char result[BUFSIZ]; 2840 2841 rcnt = read(fds[0], result, sizeof(result)); 2842 if (rcnt != -1) 2843 Buf_AddBytes(buf, (size_t)rcnt, (Byte *)result); 2844 } while (rcnt > 0 || (rcnt == -1 && errno == EINTR)); 2845 2846 if (rcnt == -1) 2847 *error = "Error reading shell's output for \"%s\""; 2848 2849 /* 2850 * Close the input side of the pipe. 2851 */ 2852 close(fds[0]); 2853 2854 status = ProcWait(&ps); 2855 2856 if (status) 2857 *error = "\"%s\" returned non-zero status"; 2858 2859 Buf_StripNewlines(buf); 2860 2861 return (buf); 2862} 2863 2864 2865/* 2866 * Interrupt handler - set flag and defer handling to the main code 2867 */ 2868static void 2869CompatCatchSig(int signo) 2870{ 2871 2872 interrupted = signo; 2873} 2874 2875/*- 2876 *----------------------------------------------------------------------- 2877 * CompatInterrupt -- 2878 * Interrupt the creation of the current target and remove it if 2879 * it ain't precious. 2880 * 2881 * Results: 2882 * None. 2883 * 2884 * Side Effects: 2885 * The target is removed and the process exits. If .INTERRUPT exists, 2886 * its commands are run first WITH INTERRUPTS IGNORED.. 2887 * 2888 *----------------------------------------------------------------------- 2889 */ 2890static void 2891CompatInterrupt(int signo) 2892{ 2893 GNode *gn; 2894 sigset_t nmask, omask; 2895 LstNode *ln; 2896 2897 sigemptyset(&nmask); 2898 sigaddset(&nmask, SIGINT); 2899 sigaddset(&nmask, SIGTERM); 2900 sigaddset(&nmask, SIGHUP); 2901 sigaddset(&nmask, SIGQUIT); 2902 sigprocmask(SIG_SETMASK, &nmask, &omask); 2903 2904 /* prevent recursion in evaluation of .INTERRUPT */ 2905 interrupted = 0; 2906 2907 if (curTarg != NULL && !Targ_Precious(curTarg)) { 2908 char *p1; 2909 char *file = Var_Value(TARGET, curTarg, &p1); 2910 2911 if (!noExecute && eunlink(file) != -1) { 2912 printf("*** %s removed\n", file); 2913 } 2914 free(p1); 2915 } 2916 2917 /* 2918 * Run .INTERRUPT only if hit with interrupt signal 2919 */ 2920 if (signo == SIGINT) { 2921 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 2922 if (gn != NULL) { 2923 LST_FOREACH(ln, &gn->commands) { 2924 if (Compat_RunCommand(Lst_Datum(ln), gn)) 2925 break; 2926 } 2927 } 2928 } 2929 2930 sigprocmask(SIG_SETMASK, &omask, NULL); 2931 2932 if (signo == SIGQUIT) 2933 exit(signo); 2934 signal(signo, SIG_DFL); 2935 kill(getpid(), signo); 2936} 2937 2938/** 2939 * shellneed 2940 * 2941 * Results: 2942 * Returns NULL if a specified line must be executed by the shell, 2943 * and an argument vector if it can be run via execvp(). 2944 * 2945 * Side Effects: 2946 * Uses brk_string so destroys the contents of argv. 2947 */ 2948static char ** 2949shellneed(ArgArray *aa, char *cmd) 2950{ 2951 char **p; 2952 int ret; 2953 2954 if (commandShell->meta == NULL || commandShell->builtins.argc <= 1) 2955 /* use shell */ 2956 return (NULL); 2957 2958 if (strpbrk(cmd, commandShell->meta) != NULL) 2959 return (NULL); 2960 2961 /* 2962 * Break the command into words to form an argument 2963 * vector we can execute. 2964 */ 2965 brk_string(aa, cmd, TRUE); 2966 for (p = commandShell->builtins.argv + 1; *p != 0; p++) { 2967 if ((ret = strcmp(aa->argv[1], *p)) == 0) { 2968 /* found - use shell */ 2969 ArgArray_Done(aa); 2970 return (NULL); 2971 } 2972 if (ret < 0) { 2973 /* not found */ 2974 break; 2975 } 2976 } 2977 return (aa->argv + 1); 2978} 2979 2980/** 2981 * Execute the next command for a target. If the command returns an 2982 * error, the node's made field is set to ERROR and creation stops. 2983 * The node from which the command came is also given. This is used 2984 * to execute the commands in compat mode and when executing commands 2985 * with the '+' flag in non-compat mode. In these modes each command 2986 * line should be executed by its own shell. We do some optimisation here: 2987 * if the shell description defines both a string of meta characters and 2988 * a list of builtins and the command line neither contains a meta character 2989 * nor starts with one of the builtins then we execute the command directly 2990 * without invoking a shell. 2991 * 2992 * Results: 2993 * 0 if the command succeeded, 1 if an error occurred. 2994 * 2995 * Side Effects: 2996 * The node's 'made' field may be set to ERROR. 2997 */ 2998static int 2999Compat_RunCommand(char *cmd, GNode *gn) 3000{ 3001 ArgArray aa; 3002 char *cmdStart; /* Start of expanded command */ 3003 Boolean silent; /* Don't print command */ 3004 Boolean doit; /* Execute even in -n */ 3005 Boolean errCheck; /* Check errors */ 3006 int reason; /* Reason for child's death */ 3007 int status; /* Description of child's death */ 3008 LstNode *cmdNode; /* Node where current cmd is located */ 3009 char **av; /* Argument vector for thing to exec */ 3010 ProcStuff ps; 3011 3012 silent = gn->type & OP_SILENT; 3013 errCheck = !(gn->type & OP_IGNORE); 3014 doit = FALSE; 3015 3016 cmdNode = Lst_Member(&gn->commands, cmd); 3017 cmdStart = Buf_Peel(Var_Subst(cmd, gn, FALSE)); 3018 3019 /* 3020 * brk_string will return an argv with a NULL in av[0], thus causing 3021 * execvp() to choke and die horribly. Besides, how can we execute a 3022 * null command? In any case, we warn the user that the command 3023 * expanded to nothing (is this the right thing to do?). 3024 */ 3025 if (*cmdStart == '\0') { 3026 free(cmdStart); 3027 Error("%s expands to empty string", cmd); 3028 return (0); 3029 } else { 3030 cmd = cmdStart; 3031 } 3032 Lst_Replace(cmdNode, cmdStart); 3033 3034 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) { 3035 Lst_AtEnd(&ENDNode->commands, cmdStart); 3036 return (0); 3037 } else if (strcmp(cmdStart, "...") == 0) { 3038 gn->type |= OP_SAVE_CMDS; 3039 return (0); 3040 } 3041 3042 while (*cmd == '@' || *cmd == '-' || *cmd == '+') { 3043 switch (*cmd) { 3044 3045 case '@': 3046 silent = DEBUG(LOUD) ? FALSE : TRUE; 3047 break; 3048 3049 case '-': 3050 errCheck = FALSE; 3051 break; 3052 3053 case '+': 3054 doit = TRUE; 3055 break; 3056 } 3057 cmd++; 3058 } 3059 3060 while (isspace((unsigned char)*cmd)) 3061 cmd++; 3062 3063 /* 3064 * Print the command before echoing if we're not supposed to be quiet 3065 * for this one. We also print the command if -n given, but not if '+'. 3066 */ 3067 if (!silent || (noExecute && !doit)) { 3068 printf("%s\n", cmd); 3069 fflush(stdout); 3070 } 3071 3072 /* 3073 * If we're not supposed to execute any commands, this is as far as 3074 * we go... 3075 */ 3076 if (!doit && noExecute) { 3077 return (0); 3078 } 3079 3080 ps.in = STDIN_FILENO; 3081 ps.out = STDOUT_FILENO; 3082 ps.err = STDERR_FILENO; 3083 3084 ps.merge_errors = 0; 3085 ps.pgroup = 0; 3086 ps.searchpath = 1; 3087 3088 if ((av = shellneed(&aa, cmd)) == NULL) { 3089 /* 3090 * Shell meta character or shell builtin found - pass 3091 * command to shell. We give the shell the -e flag as 3092 * well as -c if it is supposed to exit when it hits an error. 3093 */ 3094 ps.argv = emalloc(4 * sizeof(char *)); 3095 ps.argv[0] = strdup(commandShell->path); 3096 ps.argv[1] = strdup(errCheck ? "-ec" : "-c"); 3097 ps.argv[2] = strdup(cmd); 3098 ps.argv[3] = NULL; 3099 ps.argv_free = 1; 3100 } else { 3101 ps.argv = av; 3102 ps.argv_free = 0; 3103 } 3104 ps.errCheck = errCheck; 3105 3106 /* 3107 * Warning since we are doing vfork() instead of fork(), 3108 * do not allocate memory in the child process! 3109 */ 3110 if ((ps.child_pid = vfork()) == -1) { 3111 Fatal("Could not fork"); 3112 3113 } else if (ps.child_pid == 0) { 3114 /* 3115 * Child 3116 */
| 2711 /* NOTREACHED */ 2712 } 2713 2714 free(ps.argv[2]); 2715 free(ps.argv[1]); 2716 free(ps.argv[0]); 2717 free(ps.argv); 2718 2719 close(fds[1]); /* No need for the writing half of the pipe. */ 2720 2721 do { 2722 char result[BUFSIZ]; 2723 2724 rcnt = read(fds[0], result, sizeof(result)); 2725 if (rcnt != -1) 2726 Buf_AddBytes(buf, (size_t)rcnt, (Byte *)result); 2727 } while (rcnt > 0 || (rcnt == -1 && errno == EINTR)); 2728 2729 if (rcnt == -1) 2730 *error = "Error reading shell's output for \"%s\""; 2731 2732 /* 2733 * Close the input side of the pipe. 2734 */ 2735 close(fds[0]); 2736 2737 status = ProcWait(&ps); 2738 2739 if (status) 2740 *error = "\"%s\" returned non-zero status"; 2741 2742 Buf_StripNewlines(buf); 2743 2744 return (buf); 2745} 2746 2747 2748/* 2749 * Interrupt handler - set flag and defer handling to the main code 2750 */ 2751static void 2752CompatCatchSig(int signo) 2753{ 2754 2755 interrupted = signo; 2756} 2757 2758/*- 2759 *----------------------------------------------------------------------- 2760 * CompatInterrupt -- 2761 * Interrupt the creation of the current target and remove it if 2762 * it ain't precious. 2763 * 2764 * Results: 2765 * None. 2766 * 2767 * Side Effects: 2768 * The target is removed and the process exits. If .INTERRUPT exists, 2769 * its commands are run first WITH INTERRUPTS IGNORED.. 2770 * 2771 *----------------------------------------------------------------------- 2772 */ 2773static void 2774CompatInterrupt(int signo) 2775{ 2776 GNode *gn; 2777 sigset_t nmask, omask; 2778 LstNode *ln; 2779 2780 sigemptyset(&nmask); 2781 sigaddset(&nmask, SIGINT); 2782 sigaddset(&nmask, SIGTERM); 2783 sigaddset(&nmask, SIGHUP); 2784 sigaddset(&nmask, SIGQUIT); 2785 sigprocmask(SIG_SETMASK, &nmask, &omask); 2786 2787 /* prevent recursion in evaluation of .INTERRUPT */ 2788 interrupted = 0; 2789 2790 if (curTarg != NULL && !Targ_Precious(curTarg)) { 2791 char *p1; 2792 char *file = Var_Value(TARGET, curTarg, &p1); 2793 2794 if (!noExecute && eunlink(file) != -1) { 2795 printf("*** %s removed\n", file); 2796 } 2797 free(p1); 2798 } 2799 2800 /* 2801 * Run .INTERRUPT only if hit with interrupt signal 2802 */ 2803 if (signo == SIGINT) { 2804 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE); 2805 if (gn != NULL) { 2806 LST_FOREACH(ln, &gn->commands) { 2807 if (Compat_RunCommand(Lst_Datum(ln), gn)) 2808 break; 2809 } 2810 } 2811 } 2812 2813 sigprocmask(SIG_SETMASK, &omask, NULL); 2814 2815 if (signo == SIGQUIT) 2816 exit(signo); 2817 signal(signo, SIG_DFL); 2818 kill(getpid(), signo); 2819} 2820 2821/** 2822 * shellneed 2823 * 2824 * Results: 2825 * Returns NULL if a specified line must be executed by the shell, 2826 * and an argument vector if it can be run via execvp(). 2827 * 2828 * Side Effects: 2829 * Uses brk_string so destroys the contents of argv. 2830 */ 2831static char ** 2832shellneed(ArgArray *aa, char *cmd) 2833{ 2834 char **p; 2835 int ret; 2836 2837 if (commandShell->meta == NULL || commandShell->builtins.argc <= 1) 2838 /* use shell */ 2839 return (NULL); 2840 2841 if (strpbrk(cmd, commandShell->meta) != NULL) 2842 return (NULL); 2843 2844 /* 2845 * Break the command into words to form an argument 2846 * vector we can execute. 2847 */ 2848 brk_string(aa, cmd, TRUE); 2849 for (p = commandShell->builtins.argv + 1; *p != 0; p++) { 2850 if ((ret = strcmp(aa->argv[1], *p)) == 0) { 2851 /* found - use shell */ 2852 ArgArray_Done(aa); 2853 return (NULL); 2854 } 2855 if (ret < 0) { 2856 /* not found */ 2857 break; 2858 } 2859 } 2860 return (aa->argv + 1); 2861} 2862 2863/** 2864 * Execute the next command for a target. If the command returns an 2865 * error, the node's made field is set to ERROR and creation stops. 2866 * The node from which the command came is also given. This is used 2867 * to execute the commands in compat mode and when executing commands 2868 * with the '+' flag in non-compat mode. In these modes each command 2869 * line should be executed by its own shell. We do some optimisation here: 2870 * if the shell description defines both a string of meta characters and 2871 * a list of builtins and the command line neither contains a meta character 2872 * nor starts with one of the builtins then we execute the command directly 2873 * without invoking a shell. 2874 * 2875 * Results: 2876 * 0 if the command succeeded, 1 if an error occurred. 2877 * 2878 * Side Effects: 2879 * The node's 'made' field may be set to ERROR. 2880 */ 2881static int 2882Compat_RunCommand(char *cmd, GNode *gn) 2883{ 2884 ArgArray aa; 2885 char *cmdStart; /* Start of expanded command */ 2886 Boolean silent; /* Don't print command */ 2887 Boolean doit; /* Execute even in -n */ 2888 Boolean errCheck; /* Check errors */ 2889 int reason; /* Reason for child's death */ 2890 int status; /* Description of child's death */ 2891 LstNode *cmdNode; /* Node where current cmd is located */ 2892 char **av; /* Argument vector for thing to exec */ 2893 ProcStuff ps; 2894 2895 silent = gn->type & OP_SILENT; 2896 errCheck = !(gn->type & OP_IGNORE); 2897 doit = FALSE; 2898 2899 cmdNode = Lst_Member(&gn->commands, cmd); 2900 cmdStart = Buf_Peel(Var_Subst(cmd, gn, FALSE)); 2901 2902 /* 2903 * brk_string will return an argv with a NULL in av[0], thus causing 2904 * execvp() to choke and die horribly. Besides, how can we execute a 2905 * null command? In any case, we warn the user that the command 2906 * expanded to nothing (is this the right thing to do?). 2907 */ 2908 if (*cmdStart == '\0') { 2909 free(cmdStart); 2910 Error("%s expands to empty string", cmd); 2911 return (0); 2912 } else { 2913 cmd = cmdStart; 2914 } 2915 Lst_Replace(cmdNode, cmdStart); 2916 2917 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) { 2918 Lst_AtEnd(&ENDNode->commands, cmdStart); 2919 return (0); 2920 } else if (strcmp(cmdStart, "...") == 0) { 2921 gn->type |= OP_SAVE_CMDS; 2922 return (0); 2923 } 2924 2925 while (*cmd == '@' || *cmd == '-' || *cmd == '+') { 2926 switch (*cmd) { 2927 2928 case '@': 2929 silent = DEBUG(LOUD) ? FALSE : TRUE; 2930 break; 2931 2932 case '-': 2933 errCheck = FALSE; 2934 break; 2935 2936 case '+': 2937 doit = TRUE; 2938 break; 2939 } 2940 cmd++; 2941 } 2942 2943 while (isspace((unsigned char)*cmd)) 2944 cmd++; 2945 2946 /* 2947 * Print the command before echoing if we're not supposed to be quiet 2948 * for this one. We also print the command if -n given, but not if '+'. 2949 */ 2950 if (!silent || (noExecute && !doit)) { 2951 printf("%s\n", cmd); 2952 fflush(stdout); 2953 } 2954 2955 /* 2956 * If we're not supposed to execute any commands, this is as far as 2957 * we go... 2958 */ 2959 if (!doit && noExecute) { 2960 return (0); 2961 } 2962 2963 ps.in = STDIN_FILENO; 2964 ps.out = STDOUT_FILENO; 2965 ps.err = STDERR_FILENO; 2966 2967 ps.merge_errors = 0; 2968 ps.pgroup = 0; 2969 ps.searchpath = 1; 2970 2971 if ((av = shellneed(&aa, cmd)) == NULL) { 2972 /* 2973 * Shell meta character or shell builtin found - pass 2974 * command to shell. We give the shell the -e flag as 2975 * well as -c if it is supposed to exit when it hits an error. 2976 */ 2977 ps.argv = emalloc(4 * sizeof(char *)); 2978 ps.argv[0] = strdup(commandShell->path); 2979 ps.argv[1] = strdup(errCheck ? "-ec" : "-c"); 2980 ps.argv[2] = strdup(cmd); 2981 ps.argv[3] = NULL; 2982 ps.argv_free = 1; 2983 } else { 2984 ps.argv = av; 2985 ps.argv_free = 0; 2986 } 2987 ps.errCheck = errCheck; 2988 2989 /* 2990 * Warning since we are doing vfork() instead of fork(), 2991 * do not allocate memory in the child process! 2992 */ 2993 if ((ps.child_pid = vfork()) == -1) { 2994 Fatal("Could not fork"); 2995 2996 } else if (ps.child_pid == 0) { 2997 /* 2998 * Child 2999 */
|
3117 ProcExec(&ps);
| 3000 Proc_Exec(&ps);
|
3118 /* NOTREACHED */ 3119 3120 } else { 3121 if (ps.argv_free) { 3122 free(ps.argv[2]); 3123 free(ps.argv[1]); 3124 free(ps.argv[0]); 3125 free(ps.argv); 3126 } else { 3127 ArgArray_Done(&aa); 3128 } 3129 3130 /* 3131 * we need to print out the command associated with this 3132 * Gnode in Targ_PrintCmd from Targ_PrintGraph when debugging 3133 * at level g2, in main(), Fatal() and DieHorribly(), 3134 * therefore do not free it when debugging. 3135 */ 3136 if (!DEBUG(GRAPH2)) { 3137 free(cmdStart); 3138 } 3139 3140 /* 3141 * The child is off and running. Now all we can do is wait... 3142 */ 3143 reason = ProcWait(&ps); 3144 3145 if (interrupted) 3146 CompatInterrupt(interrupted); 3147 3148 /* 3149 * Decode and report the reason child exited, then 3150 * indicate how we handled it. 3151 */ 3152 if (WIFEXITED(reason)) { 3153 status = WEXITSTATUS(reason); 3154 if (status == 0) { 3155 return (0); 3156 } else { 3157 printf("*** Error code %d", status); 3158 } 3159 } else if (WIFSTOPPED(reason)) { 3160 status = WSTOPSIG(reason); 3161 } else { 3162 status = WTERMSIG(reason); 3163 printf("*** Signal %d", status); 3164 } 3165 3166 if (ps.errCheck) { 3167 gn->made = ERROR; 3168 if (keepgoing) { 3169 /* 3170 * Abort the current 3171 * target, but let 3172 * others continue. 3173 */ 3174 printf(" (continuing)\n"); 3175 } 3176 return (status); 3177 } else { 3178 /* 3179 * Continue executing 3180 * commands for this target. 3181 * If we return 0, this will 3182 * happen... 3183 */ 3184 printf(" (ignored)\n"); 3185 return (0); 3186 } 3187 } 3188} 3189 3190/*- 3191 *----------------------------------------------------------------------- 3192 * CompatMake -- 3193 * Make a target, given the parent, to abort if necessary. 3194 * 3195 * Side Effects: 3196 * If an error is detected and not being ignored, the process exits. 3197 * 3198 *----------------------------------------------------------------------- 3199 */ 3200static int 3201CompatMake(GNode *gn, GNode *pgn) 3202{ 3203 LstNode *ln; 3204 3205 if (gn->type & OP_USE) { 3206 Make_HandleUse(gn, pgn); 3207 3208 } else if (gn->made == UNMADE) { 3209 /* 3210 * First mark ourselves to be made, then apply whatever 3211 * transformations the suffix module thinks are necessary. 3212 * Once that's done, we can descend and make all our children. 3213 * If any of them has an error but the -k flag was given, our 3214 * 'make' field will be set FALSE again. This is our signal to 3215 * not attempt to do anything but abort our parent as well. 3216 */ 3217 gn->make = TRUE; 3218 gn->made = BEINGMADE; 3219 Suff_FindDeps(gn); 3220 LST_FOREACH(ln, &gn->children) 3221 CompatMake(Lst_Datum(ln), gn); 3222 if (!gn->make) { 3223 gn->made = ABORTED; 3224 pgn->make = FALSE; 3225 return (0); 3226 } 3227 3228 if (Lst_Member(&gn->iParents, pgn) != NULL) { 3229 char *p1; 3230 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn); 3231 free(p1); 3232 } 3233 3234 /* 3235 * All the children were made ok. Now cmtime contains the 3236 * modification time of the newest child, we need to find out 3237 * if we exist and when we were modified last. The criteria for 3238 * datedness are defined by the Make_OODate function. 3239 */ 3240 DEBUGF(MAKE, ("Examining %s...", gn->name)); 3241 if (!Make_OODate(gn)) { 3242 gn->made = UPTODATE; 3243 DEBUGF(MAKE, ("up-to-date.\n")); 3244 return (0); 3245 } else { 3246 DEBUGF(MAKE, ("out-of-date.\n")); 3247 } 3248 3249 /* 3250 * If the user is just seeing if something is out-of-date, 3251 * exit now to tell him/her "yes". 3252 */ 3253 if (queryFlag) { 3254 exit(1); 3255 } 3256 3257 /* 3258 * We need to be re-made. We also have to make sure we've got 3259 * a $? variable. To be nice, we also define the $> variable 3260 * using Make_DoAllVar(). 3261 */ 3262 Make_DoAllVar(gn); 3263 3264 /* 3265 * Alter our type to tell if errors should be ignored or things 3266 * should not be printed so Compat_RunCommand knows what to do. 3267 */ 3268 if (Targ_Ignore(gn)) { 3269 gn->type |= OP_IGNORE; 3270 } 3271 if (Targ_Silent(gn)) { 3272 gn->type |= OP_SILENT; 3273 } 3274 3275 if (Job_CheckCommands(gn, Fatal)) { 3276 /* 3277 * Our commands are ok, but we still have to worry 3278 * about the -t flag... 3279 */ 3280 if (!touchFlag) { 3281 curTarg = gn; 3282 LST_FOREACH(ln, &gn->commands) { 3283 if (Compat_RunCommand(Lst_Datum(ln), 3284 gn)) 3285 break; 3286 } 3287 curTarg = NULL; 3288 } else { 3289 Job_Touch(gn, gn->type & OP_SILENT); 3290 } 3291 } else { 3292 gn->made = ERROR; 3293 } 3294 3295 if (gn->made != ERROR) { 3296 /* 3297 * If the node was made successfully, mark it so, update 3298 * its modification time and timestamp all its parents. 3299 * Note that for .ZEROTIME targets, the timestamping 3300 * isn't done. This is to keep its state from affecting 3301 * that of its parent. 3302 */ 3303 gn->made = MADE; 3304#ifndef RECHECK 3305 /* 3306 * We can't re-stat the thing, but we can at least take 3307 * care of rules where a target depends on a source that 3308 * actually creates the target, but only if it has 3309 * changed, e.g. 3310 * 3311 * parse.h : parse.o 3312 * 3313 * parse.o : parse.y 3314 * yacc -d parse.y 3315 * cc -c y.tab.c 3316 * mv y.tab.o parse.o 3317 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h 3318 * 3319 * In this case, if the definitions produced by yacc 3320 * haven't changed from before, parse.h won't have been 3321 * updated and gn->mtime will reflect the current 3322 * modification time for parse.h. This is something of a 3323 * kludge, I admit, but it's a useful one.. 3324 * 3325 * XXX: People like to use a rule like 3326 * 3327 * FRC: 3328 * 3329 * To force things that depend on FRC to be made, so we 3330 * have to check for gn->children being empty as well... 3331 */ 3332 if (!Lst_IsEmpty(&gn->commands) || 3333 Lst_IsEmpty(&gn->children)) { 3334 gn->mtime = now; 3335 } 3336#else 3337 /* 3338 * This is what Make does and it's actually a good 3339 * thing, as it allows rules like 3340 * 3341 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h 3342 * 3343 * to function as intended. Unfortunately, thanks to 3344 * the stateless nature of NFS (and the speed of this 3345 * program), there are times when the modification time 3346 * of a file created on a remote machine will not be 3347 * modified before the stat() implied by the Dir_MTime 3348 * occurs, thus leading us to believe that the file 3349 * is unchanged, wreaking havoc with files that depend 3350 * on this one. 3351 * 3352 * I have decided it is better to make too much than to 3353 * make too little, so this stuff is commented out 3354 * unless you're sure it's ok. 3355 * -- ardeb 1/12/88 3356 */ 3357 if (noExecute || Dir_MTime(gn) == 0) { 3358 gn->mtime = now; 3359 } 3360 if (gn->cmtime > gn->mtime) 3361 gn->mtime = gn->cmtime; 3362 DEBUGF(MAKE, ("update time: %s\n", 3363 Targ_FmtTime(gn->mtime))); 3364#endif 3365 if (!(gn->type & OP_EXEC)) { 3366 pgn->childMade = TRUE; 3367 Make_TimeStamp(pgn, gn); 3368 } 3369 3370 } else if (keepgoing) { 3371 pgn->make = FALSE; 3372 3373 } else { 3374 char *p1; 3375 3376 printf("\n\nStop in %s.\n", 3377 Var_Value(".CURDIR", gn, &p1)); 3378 free(p1); 3379 exit(1); 3380 } 3381 } else if (gn->made == ERROR) { 3382 /* 3383 * Already had an error when making this beastie. Tell the 3384 * parent to abort. 3385 */ 3386 pgn->make = FALSE; 3387 } else { 3388 if (Lst_Member(&gn->iParents, pgn) != NULL) { 3389 char *p1; 3390 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn); 3391 free(p1); 3392 } 3393 switch(gn->made) { 3394 case BEINGMADE: 3395 Error("Graph cycles through %s\n", gn->name); 3396 gn->made = ERROR; 3397 pgn->make = FALSE; 3398 break; 3399 case MADE: 3400 if ((gn->type & OP_EXEC) == 0) { 3401 pgn->childMade = TRUE; 3402 Make_TimeStamp(pgn, gn); 3403 } 3404 break; 3405 case UPTODATE: 3406 if ((gn->type & OP_EXEC) == 0) { 3407 Make_TimeStamp(pgn, gn); 3408 } 3409 break; 3410 default: 3411 break; 3412 } 3413 } 3414 3415 return (0); 3416} 3417 3418/*- 3419 *----------------------------------------------------------------------- 3420 * Compat_Run -- 3421 * Start making again, given a list of target nodes. 3422 * 3423 * Results: 3424 * None. 3425 * 3426 * Side Effects: 3427 * Guess what? 3428 * 3429 *----------------------------------------------------------------------- 3430 */ 3431void 3432Compat_Run(Lst *targs) 3433{ 3434 GNode *gn = NULL; /* Current root target */ 3435 int error_cnt; /* Number of targets not remade due to errors */ 3436 LstNode *ln; 3437 3438 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 3439 signal(SIGINT, CompatCatchSig); 3440 } 3441 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 3442 signal(SIGTERM, CompatCatchSig); 3443 } 3444 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 3445 signal(SIGHUP, CompatCatchSig); 3446 } 3447 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 3448 signal(SIGQUIT, CompatCatchSig); 3449 } 3450 3451 ENDNode = Targ_FindNode(".END", TARG_CREATE); 3452 /* 3453 * If the user has defined a .BEGIN target, execute the commands 3454 * attached to it. 3455 */ 3456 if (!queryFlag) { 3457 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE); 3458 if (gn != NULL) { 3459 LST_FOREACH(ln, &gn->commands) { 3460 if (Compat_RunCommand(Lst_Datum(ln), gn)) 3461 break; 3462 } 3463 if (gn->made == ERROR) { 3464 printf("\n\nStop.\n"); 3465 exit(1); 3466 } 3467 } 3468 } 3469 3470 /* 3471 * For each entry in the list of targets to create, call CompatMake on 3472 * it to create the thing. CompatMake will leave the 'made' field of gn 3473 * in one of several states: 3474 * UPTODATE gn was already up-to-date 3475 * MADE gn was recreated successfully 3476 * ERROR An error occurred while gn was being created 3477 * ABORTED gn was not remade because one of its inferiors 3478 * could not be made due to errors. 3479 */ 3480 error_cnt = 0; 3481 while (!Lst_IsEmpty(targs)) { 3482 gn = Lst_DeQueue(targs); 3483 CompatMake(gn, gn); 3484 3485 if (gn->made == UPTODATE) { 3486 printf("`%s' is up to date.\n", gn->name); 3487 } else if (gn->made == ABORTED) { 3488 printf("`%s' not remade because of errors.\n", 3489 gn->name); 3490 error_cnt += 1; 3491 } 3492 } 3493 3494 /* 3495 * If the user has defined a .END target, run its commands. 3496 */ 3497 if (error_cnt == 0) { 3498 LST_FOREACH(ln, &ENDNode->commands) { 3499 if (Compat_RunCommand(Lst_Datum(ln), gn)) 3500 break; 3501 } 3502 } 3503} 3504
| 3001 /* NOTREACHED */ 3002 3003 } else { 3004 if (ps.argv_free) { 3005 free(ps.argv[2]); 3006 free(ps.argv[1]); 3007 free(ps.argv[0]); 3008 free(ps.argv); 3009 } else { 3010 ArgArray_Done(&aa); 3011 } 3012 3013 /* 3014 * we need to print out the command associated with this 3015 * Gnode in Targ_PrintCmd from Targ_PrintGraph when debugging 3016 * at level g2, in main(), Fatal() and DieHorribly(), 3017 * therefore do not free it when debugging. 3018 */ 3019 if (!DEBUG(GRAPH2)) { 3020 free(cmdStart); 3021 } 3022 3023 /* 3024 * The child is off and running. Now all we can do is wait... 3025 */ 3026 reason = ProcWait(&ps); 3027 3028 if (interrupted) 3029 CompatInterrupt(interrupted); 3030 3031 /* 3032 * Decode and report the reason child exited, then 3033 * indicate how we handled it. 3034 */ 3035 if (WIFEXITED(reason)) { 3036 status = WEXITSTATUS(reason); 3037 if (status == 0) { 3038 return (0); 3039 } else { 3040 printf("*** Error code %d", status); 3041 } 3042 } else if (WIFSTOPPED(reason)) { 3043 status = WSTOPSIG(reason); 3044 } else { 3045 status = WTERMSIG(reason); 3046 printf("*** Signal %d", status); 3047 } 3048 3049 if (ps.errCheck) { 3050 gn->made = ERROR; 3051 if (keepgoing) { 3052 /* 3053 * Abort the current 3054 * target, but let 3055 * others continue. 3056 */ 3057 printf(" (continuing)\n"); 3058 } 3059 return (status); 3060 } else { 3061 /* 3062 * Continue executing 3063 * commands for this target. 3064 * If we return 0, this will 3065 * happen... 3066 */ 3067 printf(" (ignored)\n"); 3068 return (0); 3069 } 3070 } 3071} 3072 3073/*- 3074 *----------------------------------------------------------------------- 3075 * CompatMake -- 3076 * Make a target, given the parent, to abort if necessary. 3077 * 3078 * Side Effects: 3079 * If an error is detected and not being ignored, the process exits. 3080 * 3081 *----------------------------------------------------------------------- 3082 */ 3083static int 3084CompatMake(GNode *gn, GNode *pgn) 3085{ 3086 LstNode *ln; 3087 3088 if (gn->type & OP_USE) { 3089 Make_HandleUse(gn, pgn); 3090 3091 } else if (gn->made == UNMADE) { 3092 /* 3093 * First mark ourselves to be made, then apply whatever 3094 * transformations the suffix module thinks are necessary. 3095 * Once that's done, we can descend and make all our children. 3096 * If any of them has an error but the -k flag was given, our 3097 * 'make' field will be set FALSE again. This is our signal to 3098 * not attempt to do anything but abort our parent as well. 3099 */ 3100 gn->make = TRUE; 3101 gn->made = BEINGMADE; 3102 Suff_FindDeps(gn); 3103 LST_FOREACH(ln, &gn->children) 3104 CompatMake(Lst_Datum(ln), gn); 3105 if (!gn->make) { 3106 gn->made = ABORTED; 3107 pgn->make = FALSE; 3108 return (0); 3109 } 3110 3111 if (Lst_Member(&gn->iParents, pgn) != NULL) { 3112 char *p1; 3113 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn); 3114 free(p1); 3115 } 3116 3117 /* 3118 * All the children were made ok. Now cmtime contains the 3119 * modification time of the newest child, we need to find out 3120 * if we exist and when we were modified last. The criteria for 3121 * datedness are defined by the Make_OODate function. 3122 */ 3123 DEBUGF(MAKE, ("Examining %s...", gn->name)); 3124 if (!Make_OODate(gn)) { 3125 gn->made = UPTODATE; 3126 DEBUGF(MAKE, ("up-to-date.\n")); 3127 return (0); 3128 } else { 3129 DEBUGF(MAKE, ("out-of-date.\n")); 3130 } 3131 3132 /* 3133 * If the user is just seeing if something is out-of-date, 3134 * exit now to tell him/her "yes". 3135 */ 3136 if (queryFlag) { 3137 exit(1); 3138 } 3139 3140 /* 3141 * We need to be re-made. We also have to make sure we've got 3142 * a $? variable. To be nice, we also define the $> variable 3143 * using Make_DoAllVar(). 3144 */ 3145 Make_DoAllVar(gn); 3146 3147 /* 3148 * Alter our type to tell if errors should be ignored or things 3149 * should not be printed so Compat_RunCommand knows what to do. 3150 */ 3151 if (Targ_Ignore(gn)) { 3152 gn->type |= OP_IGNORE; 3153 } 3154 if (Targ_Silent(gn)) { 3155 gn->type |= OP_SILENT; 3156 } 3157 3158 if (Job_CheckCommands(gn, Fatal)) { 3159 /* 3160 * Our commands are ok, but we still have to worry 3161 * about the -t flag... 3162 */ 3163 if (!touchFlag) { 3164 curTarg = gn; 3165 LST_FOREACH(ln, &gn->commands) { 3166 if (Compat_RunCommand(Lst_Datum(ln), 3167 gn)) 3168 break; 3169 } 3170 curTarg = NULL; 3171 } else { 3172 Job_Touch(gn, gn->type & OP_SILENT); 3173 } 3174 } else { 3175 gn->made = ERROR; 3176 } 3177 3178 if (gn->made != ERROR) { 3179 /* 3180 * If the node was made successfully, mark it so, update 3181 * its modification time and timestamp all its parents. 3182 * Note that for .ZEROTIME targets, the timestamping 3183 * isn't done. This is to keep its state from affecting 3184 * that of its parent. 3185 */ 3186 gn->made = MADE; 3187#ifndef RECHECK 3188 /* 3189 * We can't re-stat the thing, but we can at least take 3190 * care of rules where a target depends on a source that 3191 * actually creates the target, but only if it has 3192 * changed, e.g. 3193 * 3194 * parse.h : parse.o 3195 * 3196 * parse.o : parse.y 3197 * yacc -d parse.y 3198 * cc -c y.tab.c 3199 * mv y.tab.o parse.o 3200 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h 3201 * 3202 * In this case, if the definitions produced by yacc 3203 * haven't changed from before, parse.h won't have been 3204 * updated and gn->mtime will reflect the current 3205 * modification time for parse.h. This is something of a 3206 * kludge, I admit, but it's a useful one.. 3207 * 3208 * XXX: People like to use a rule like 3209 * 3210 * FRC: 3211 * 3212 * To force things that depend on FRC to be made, so we 3213 * have to check for gn->children being empty as well... 3214 */ 3215 if (!Lst_IsEmpty(&gn->commands) || 3216 Lst_IsEmpty(&gn->children)) { 3217 gn->mtime = now; 3218 } 3219#else 3220 /* 3221 * This is what Make does and it's actually a good 3222 * thing, as it allows rules like 3223 * 3224 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h 3225 * 3226 * to function as intended. Unfortunately, thanks to 3227 * the stateless nature of NFS (and the speed of this 3228 * program), there are times when the modification time 3229 * of a file created on a remote machine will not be 3230 * modified before the stat() implied by the Dir_MTime 3231 * occurs, thus leading us to believe that the file 3232 * is unchanged, wreaking havoc with files that depend 3233 * on this one. 3234 * 3235 * I have decided it is better to make too much than to 3236 * make too little, so this stuff is commented out 3237 * unless you're sure it's ok. 3238 * -- ardeb 1/12/88 3239 */ 3240 if (noExecute || Dir_MTime(gn) == 0) { 3241 gn->mtime = now; 3242 } 3243 if (gn->cmtime > gn->mtime) 3244 gn->mtime = gn->cmtime; 3245 DEBUGF(MAKE, ("update time: %s\n", 3246 Targ_FmtTime(gn->mtime))); 3247#endif 3248 if (!(gn->type & OP_EXEC)) { 3249 pgn->childMade = TRUE; 3250 Make_TimeStamp(pgn, gn); 3251 } 3252 3253 } else if (keepgoing) { 3254 pgn->make = FALSE; 3255 3256 } else { 3257 char *p1; 3258 3259 printf("\n\nStop in %s.\n", 3260 Var_Value(".CURDIR", gn, &p1)); 3261 free(p1); 3262 exit(1); 3263 } 3264 } else if (gn->made == ERROR) { 3265 /* 3266 * Already had an error when making this beastie. Tell the 3267 * parent to abort. 3268 */ 3269 pgn->make = FALSE; 3270 } else { 3271 if (Lst_Member(&gn->iParents, pgn) != NULL) { 3272 char *p1; 3273 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn); 3274 free(p1); 3275 } 3276 switch(gn->made) { 3277 case BEINGMADE: 3278 Error("Graph cycles through %s\n", gn->name); 3279 gn->made = ERROR; 3280 pgn->make = FALSE; 3281 break; 3282 case MADE: 3283 if ((gn->type & OP_EXEC) == 0) { 3284 pgn->childMade = TRUE; 3285 Make_TimeStamp(pgn, gn); 3286 } 3287 break; 3288 case UPTODATE: 3289 if ((gn->type & OP_EXEC) == 0) { 3290 Make_TimeStamp(pgn, gn); 3291 } 3292 break; 3293 default: 3294 break; 3295 } 3296 } 3297 3298 return (0); 3299} 3300 3301/*- 3302 *----------------------------------------------------------------------- 3303 * Compat_Run -- 3304 * Start making again, given a list of target nodes. 3305 * 3306 * Results: 3307 * None. 3308 * 3309 * Side Effects: 3310 * Guess what? 3311 * 3312 *----------------------------------------------------------------------- 3313 */ 3314void 3315Compat_Run(Lst *targs) 3316{ 3317 GNode *gn = NULL; /* Current root target */ 3318 int error_cnt; /* Number of targets not remade due to errors */ 3319 LstNode *ln; 3320 3321 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 3322 signal(SIGINT, CompatCatchSig); 3323 } 3324 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { 3325 signal(SIGTERM, CompatCatchSig); 3326 } 3327 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 3328 signal(SIGHUP, CompatCatchSig); 3329 } 3330 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) { 3331 signal(SIGQUIT, CompatCatchSig); 3332 } 3333 3334 ENDNode = Targ_FindNode(".END", TARG_CREATE); 3335 /* 3336 * If the user has defined a .BEGIN target, execute the commands 3337 * attached to it. 3338 */ 3339 if (!queryFlag) { 3340 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE); 3341 if (gn != NULL) { 3342 LST_FOREACH(ln, &gn->commands) { 3343 if (Compat_RunCommand(Lst_Datum(ln), gn)) 3344 break; 3345 } 3346 if (gn->made == ERROR) { 3347 printf("\n\nStop.\n"); 3348 exit(1); 3349 } 3350 } 3351 } 3352 3353 /* 3354 * For each entry in the list of targets to create, call CompatMake on 3355 * it to create the thing. CompatMake will leave the 'made' field of gn 3356 * in one of several states: 3357 * UPTODATE gn was already up-to-date 3358 * MADE gn was recreated successfully 3359 * ERROR An error occurred while gn was being created 3360 * ABORTED gn was not remade because one of its inferiors 3361 * could not be made due to errors. 3362 */ 3363 error_cnt = 0; 3364 while (!Lst_IsEmpty(targs)) { 3365 gn = Lst_DeQueue(targs); 3366 CompatMake(gn, gn); 3367 3368 if (gn->made == UPTODATE) { 3369 printf("`%s' is up to date.\n", gn->name); 3370 } else if (gn->made == ABORTED) { 3371 printf("`%s' not remade because of errors.\n", 3372 gn->name); 3373 error_cnt += 1; 3374 } 3375 } 3376 3377 /* 3378 * If the user has defined a .END target, run its commands. 3379 */ 3380 if (error_cnt == 0) { 3381 LST_FOREACH(ln, &ENDNode->commands) { 3382 if (Compat_RunCommand(Lst_Datum(ln), gn)) 3383 break; 3384 } 3385 } 3386} 3387
|