job.h revision 136840
118334Speter/* 290075Sobrien * Copyright (c) 1988, 1989, 1990, 1993 3169689Skan * The Regents of the University of California. All rights reserved. 418334Speter * Copyright (c) 1988, 1989 by Adam de Boor 518334Speter * Copyright (c) 1989 by Berkeley Softworks 6132718Skan * All rights reserved. 718334Speter * 8132718Skan * This code is derived from software contributed to Berkeley by 918334Speter * Adam de Boor. 1018334Speter * 1118334Speter * Redistribution and use in source and binary forms, with or without 1218334Speter * modification, are permitted provided that the following conditions 13132718Skan * are met: 1418334Speter * 1. Redistributions of source code must retain the above copyright 1518334Speter * notice, this list of conditions and the following disclaimer. 1618334Speter * 2. Redistributions in binary form must reproduce the above copyright 1718334Speter * notice, this list of conditions and the following disclaimer in the 1818334Speter * documentation and/or other materials provided with the distribution. 19132718Skan * 3. All advertising materials mentioning features or use of this software 20169689Skan * must display the following acknowledgement: 21169689Skan * This product includes software developed by the University of 2218334Speter * California, Berkeley and its contributors. 2350397Sobrien * 4. Neither the name of the University nor the names of its contributors 2418334Speter * may be used to endorse or promote products derived from this software 2518334Speter * without specific prior written permission. 2650397Sobrien * 27132718Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28132718Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2918334Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3018334Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3190075Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3218334Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3318334Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3418334Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3550397Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3650397Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37169689Skan * SUCH DAMAGE. 3818334Speter * 39132718Skan * @(#)job.h 8.1 (Berkeley) 6/6/93 40132718Skan * $FreeBSD: head/usr.bin/make/job.h 136840 2004-10-23 21:34:41Z ru $ 41117395Skan */ 42132718Skan 43132718Skan/*- 44132718Skan * job.h -- 45117395Skan * Definitions pertaining to the running of jobs in parallel mode. 46132718Skan * Exported from job.c for the use of remote-execution modules. 47132718Skan */ 48132718Skan#ifndef _JOB_H_ 49117395Skan#define _JOB_H_ 50132718Skan 51132718Skan#define TMPPAT "/tmp/makeXXXXXXXXXX" 52132718Skan 53132718Skan#ifndef USE_KQUEUE 54132718Skan/* 55132718Skan * The SEL_ constants determine the maximum amount of time spent in select 56132718Skan * before coming out to see if a child has finished. SEL_SEC is the number of 57132718Skan * seconds and SEL_USEC is the number of micro-seconds 5818334Speter */ 5990075Sobrien#define SEL_SEC 2 6090075Sobrien#define SEL_USEC 0 6190075Sobrien#endif /* !USE_KQUEUE */ 6290075Sobrien 6390075Sobrien 6490075Sobrien/*- 6590075Sobrien * Job Table definitions. 6690075Sobrien * 6718334Speter * Each job has several things associated with it: 68132718Skan * 1) The process id of the child shell 69132718Skan * 2) The graph node describing the target being made by this job 7090075Sobrien * 3) A LstNode for the first command to be saved after the job 71132718Skan * completes. This is NULL if there was no "..." in the job's 72169689Skan * commands. 73132718Skan * 4) An FILE* for writing out the commands. This is only 74169689Skan * used before the job is actually started. 75132718Skan * 5) A union of things used for handling the shell's output. Different 76132718Skan * parts of the union are used based on the value of the usePipes 7790075Sobrien * flag. If it is true, the output is being caught via a pipe and 7818334Speter * the descriptors of our pipe, an array in which output is line 7990075Sobrien * buffered and the current position in that buffer are all 8090075Sobrien * maintained for each job. If, on the other hand, usePipes is false, 8118334Speter * the output is routed to a temporary file and all that is kept 82132718Skan * is the name of the file and the descriptor open to the file. 83132718Skan * 6) An identifier provided by and for the exclusive use of the 84169689Skan * Rmt module. 85132718Skan * 7) A word of flags which determine how the module handles errors, 86169689Skan * echoing, etc. for the job 87132718Skan * 88132718Skan * The job "table" is kept as a linked Lst in 'jobs', with the number of 89169689Skan * active jobs maintained in the 'nJobs' variable. At no time will this 90169689Skan * exceed the value of 'maxJobs', initialized by the Job_Init function. 9190075Sobrien * 9290075Sobrien * When a job is finished, the Make_Update function is called on each of the 9390075Sobrien * parents of the node which was just remade. This takes care of the upward 9490075Sobrien * traversal of the dependency graph. 9590075Sobrien */ 9690075Sobrien#define JOB_BUFSIZE 1024 9790075Sobrientypedef struct Job { 9890075Sobrien int pid; /* The child's process ID */ 9990075Sobrien char tfile[sizeof(TMPPAT)]; 10090075Sobrien /* Temporary file to use for job */ 101132718Skan GNode *node; /* The target the child is making */ 10290075Sobrien LstNode tailCmds; /* The node of the first command to be 103169689Skan * saved when the job has been run */ 104169689Skan FILE *cmdFILE; /* When creating the shell script, this is 105169689Skan * where the commands go */ 106169689Skan int rmtID; /* ID returned from Rmt module */ 10790075Sobrien short flags; /* Flags to control treatment of job */ 10890075Sobrien#define JOB_IGNERR 0x001 /* Ignore non-zero exits */ 10990075Sobrien#define JOB_SILENT 0x002 /* no output */ 11090075Sobrien#define JOB_SPECIAL 0x004 /* Target is a special one. i.e. run it locally 11190075Sobrien * if we can't export it and maxLocal is 0 */ 11290075Sobrien#define JOB_IGNDOTS 0x008 /* Ignore "..." lines when processing 11390075Sobrien * commands */ 11490075Sobrien#define JOB_REMOTE 0x010 /* Job is running remotely */ 11590075Sobrien#define JOB_FIRST 0x020 /* Job is first job for the node */ 11690075Sobrien#define JOB_REMIGRATE 0x040 /* Job needs to be remigrated */ 11790075Sobrien#define JOB_RESTART 0x080 /* Job needs to be completely restarted */ 11890075Sobrien#define JOB_RESUME 0x100 /* Job needs to be resumed b/c it stopped, 11990075Sobrien * for some reason */ 12090075Sobrien#define JOB_CONTINUING 0x200 /* We are in the process of resuming this job. 12118334Speter * Used to avoid infinite recursion between 122132718Skan * JobFinish and JobRestart */ 12318334Speter union { 12490075Sobrien struct { 12590075Sobrien int op_inPipe; /* Input side of pipe associated 12618334Speter * with job's output channel */ 12790075Sobrien int op_outPipe; /* Output side of pipe associated with 12890075Sobrien * job's output channel */ 12990075Sobrien char op_outBuf[JOB_BUFSIZE + 1]; 13090075Sobrien /* Buffer for storing the output of the 131117395Skan * job, line by line */ 13290075Sobrien int op_curPos; /* Current position in op_outBuf */ 13390075Sobrien } o_pipe; /* data used when catching the output via 134169689Skan * a pipe */ 13590075Sobrien struct { 13690075Sobrien char of_outFile[sizeof(TMPPAT)]; 137117395Skan /* Name of file to which shell output 138117395Skan * was rerouted */ 139117395Skan int of_outFd; /* Stream open to the output 140117395Skan * file. Used to funnel all 141117395Skan * from a single job to one file 142117395Skan * while still allowing 143117395Skan * multiple shell invocations */ 144117395Skan } o_file; /* Data used when catching the output in 145117395Skan * a temporary file */ 146117395Skan } output; /* Data for tracking a shell's output */ 147102780Skan} Job; 148102780Skan 149117395Skan#define outPipe output.o_pipe.op_outPipe 150102780Skan#define inPipe output.o_pipe.op_inPipe 151117395Skan#define outBuf output.o_pipe.op_outBuf 152102780Skan#define curPos output.o_pipe.op_curPos 153117395Skan#define outFile output.o_file.of_outFile 154117395Skan#define outFd output.o_file.of_outFd 155117395Skan 156117395Skan 157117395Skan/*- 158169689Skan * Shell Specifications: 159117395Skan * Each shell type has associated with it the following information: 160117395Skan * 1) The string which must match the last character of the shell name 161169689Skan * for the shell to be considered of this type. The longest match 162117395Skan * wins. 163117395Skan * 2) A command to issue to turn off echoing of command lines 164169689Skan * 3) A command to issue to turn echoing back on again 165117395Skan * 4) What the shell prints, and its length, when given the echo-off 166117395Skan * command. This line will not be printed when received from the shell 167169689Skan * 5) A boolean to tell if the shell has the ability to control 168117395Skan * error checking for individual commands. 169117395Skan * 6) The string to turn this checking on. 170117395Skan * 7) The string to turn it off. 171169689Skan * 8) The command-flag to give to cause the shell to start echoing 172117395Skan * commands right away. 173117395Skan * 9) The command-flag to cause the shell to Lib_Exit when an error is 174117395Skan * detected in one of the commands. 175117395Skan * 176117395Skan * Some special stuff goes on if a shell doesn't have error control. In such 177117395Skan * a case, errCheck becomes a printf template for echoing the command, 178117395Skan * should echoing be on and ignErr becomes another printf template for 179117395Skan * executing the command while ignoring the return status. If either of these 180117395Skan * strings is empty when hasErrCtl is FALSE, the command will be executed 181171825Skan * anyway as is and if it causes an error, so be it. 182117395Skan */ 183117395Skantypedef struct Shell { 184102780Skan char *name; /* the name of the shell. For Bourne and C 185117395Skan * shells, this is used only to find the 186169689Skan * shell description when used as the single 187117395Skan * source of a .SHELL target. For user-defined 188117395Skan * shells, this is the full path of the shell. 189117395Skan */ 190117395Skan Boolean hasEchoCtl; /* True if both echoOff and echoOn defined */ 191117395Skan char *echoOff; /* command to turn off echo */ 192117395Skan char *echoOn; /* command to turn it back on again */ 193117395Skan char *noPrint; /* command to skip when printing output from 194117395Skan * shell. This is usually the command which 195117395Skan * was executed to turn off echoing */ 196117395Skan int noPLen; /* length of noPrint command */ 197117395Skan Boolean hasErrCtl; /* set if can control error checking for 198171825Skan * individual commands */ 199169689Skan char *errCheck; /* string to turn error checking on */ 200169689Skan char *ignErr; /* string to turn off error checking */ 201169689Skan /* 202169689Skan * command-line flags 203169689Skan */ 204169689Skan char *echo; /* echo commands */ 205117395Skan char *exit; /* exit on error */ 206117395Skan} Shell; 207117395Skan 208117395Skanextern char *shellPath; 209117395Skanextern char *shellName; 210169689Skan 211169689Skan/* 212169689Skan * If REMOTE is defined then these things need exposed, otherwise they are 213102780Skan * static to job.c! 214117395Skan */ 215117395Skan#ifdef REMOTE 216117395Skanextern char *targFmt; /* Format string for banner that separates 217169689Skan * output from multiple jobs. Contains a 218117395Skan * single %s where the name of the node being 219117395Skan * made should be put. */ 220169689Skanextern GNode *lastNode; /* Last node for which a banner was printed. 221169689Skan * If Rmt module finds it necessary to print 222169689Skan * a banner, it should set this to the node 223169689Skan * for which the banner was printed */ 224169689Skanextern int nJobs; /* Number of jobs running (local and remote) */ 225117395Skanextern int nLocal; /* Number of jobs running locally */ 226169689Skanextern Lst jobs; /* List of active job descriptors */ 227169689Skanextern Lst stoppedJobs; /* List of jobs that are stopped or didn't 228169689Skan * quite get started */ 229169689Skanextern Boolean jobFull; /* Non-zero if no more jobs should/will start*/ 230169689Skan#endif 231169689Skan 232132718Skanextern int maxJobs; /* Number of jobs that may run */ 233132718Skan 234132718Skan 235146895Skanvoid Shell_Init(void); 236169689Skanvoid Job_Touch(GNode *, Boolean); 237169689SkanBoolean Job_CheckCommands(GNode *, void (*abortProc)(const char *, ...)); 238169689Skanvoid Job_CatchChildren(Boolean); 239169689Skanvoid Job_CatchOutput(void); 240169689Skanvoid Job_Make(GNode *); 241169689Skanvoid Job_Init(int, int); 242169689SkanBoolean Job_Full(void); 243169689SkanBoolean Job_Empty(void); 244169689SkanReturnStatus Job_ParseShell(char *); 245169689Skanint Job_Finish(void); 246169689Skanvoid Job_Wait(void); 247169689Skanvoid Job_AbortAll(void); 248169689Skanvoid JobFlagForMigration(int); 249169689Skan 250169689Skan#endif /* _JOB_H_ */ 251146895Skan