1 /* $NetBSD: job.h,v 1.17 2002/11/16 22:22:23 gson Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 5 * Copyright (c) 1988, 1989 by Adam de Boor 6 * Copyright (c) 1989 by Berkeley Softworks 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * Adam de Boor. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)job.h 8.1 (Berkeley) 6/6/93 41 */ 42 43 /*- 44 * job.h -- 45 * Definitions pertaining to the running of jobs in parallel mode. 46 * Exported from job.c for the use of remote-execution modules. 47 */ 48 #ifndef _JOB_H_ 49 #define _JOB_H_ 50 51 #define TMPPAT "/tmp/makeXXXXXX" 52 53 #ifdef USE_SELECT 54 /* 55 * Emulate poll() in terms of select(). This is not a complete 56 * emulation but it is sufficient for make's purposes. 57 */ 58 59 #define poll emul_poll 60 #define pollfd emul_pollfd 61 62 struct emul_pollfd { 63 int fd; 64 short events; 65 short revents; 66 }; 67 68 #define POLLIN 0x0001 69 #define POLLOUT 0x0004 70 71 int 72 emul_poll(struct pollfd *fd, int nfd, int timeout); 73 #endif 74 75 /* 76 * The POLL_MSEC constant determines the maximum number of milliseconds spent 77 * in poll before coming out to see if a child has finished. 78 */ 79 #define POLL_MSEC 5000 80 81 82 /*- 83 * Job Table definitions. 84 * 85 * Each job has several things associated with it: 86 * 1) The process id of the child shell 87 * 2) The graph node describing the target being made by this job 88 * 3) A LstNode for the first command to be saved after the job 89 * completes. This is NILLNODE if there was no "..." in the job's 90 * commands. 91 * 4) An FILE* for writing out the commands. This is only 92 * used before the job is actually started. 93 * 5) A union of things used for handling the shell's output. Different 94 * parts of the union are used based on the value of the usePipes 95 * flag. If it is true, the output is being caught via a pipe and 96 * the descriptors of our pipe, an array in which output is line 97 * buffered and the current position in that buffer are all 98 * maintained for each job. If, on the other hand, usePipes is false, 99 * the output is routed to a temporary file and all that is kept 100 * is the name of the file and the descriptor open to the file. 101 * 6) An identifier provided by and for the exclusive use of the 102 * Rmt module. 103 * 7) A word of flags which determine how the module handles errors, 104 * echoing, etc. for the job 105 * 106 * The job "table" is kept as a linked Lst in 'jobs', with the number of 107 * active jobs maintained in the 'nJobs' variable. At no time will this 108 * exceed the value of 'maxJobs', initialized by the Job_Init function. 109 * 110 * When a job is finished, the Make_Update function is called on each of the 111 * parents of the node which was just remade. This takes care of the upward 112 * traversal of the dependency graph. 113 */ 114 #ifndef RMT_WILL_WATCH 115 struct pollfd; 116 #endif 117 118 #define JOB_BUFSIZE 1024 119 typedef struct Job { 120 int pid; /* The child's process ID */ 121 GNode *node; /* The target the child is making */ 122 LstNode tailCmds; /* The node of the first command to be 123 * saved when the job has been run */ 124 FILE *cmdFILE; /* When creating the shell script, this is 125 * where the commands go */ 126 int rmtID; /* ID returned from Rmt module */ 127 short flags; /* Flags to control treatment of job */ 128 #define JOB_IGNERR 0x001 /* Ignore non-zero exits */ 129 #define JOB_SILENT 0x002 /* no output */ 130 #define JOB_SPECIAL 0x004 /* Target is a special one. i.e. run it locally 131 * if we can't export it and maxLocal is 0 */ 132 #define JOB_IGNDOTS 0x008 /* Ignore "..." lines when processing 133 * commands */ 134 #define JOB_REMOTE 0x010 /* Job is running remotely */ 135 #define JOB_FIRST 0x020 /* Job is first job for the node */ 136 #define JOB_REMIGRATE 0x040 /* Job needs to be remigrated */ 137 #define JOB_RESTART 0x080 /* Job needs to be completely restarted */ 138 #define JOB_RESUME 0x100 /* Job needs to be resumed b/c it stopped, 139 * for some reason */ 140 #define JOB_CONTINUING 0x200 /* We are in the process of resuming this job. 141 * Used to avoid infinite recursion between 142 * JobFinish and JobRestart */ 143 #define JOB_TRACED 0x400 /* we've sent 'set -x' */ 144 145 union { 146 struct { 147 int op_inPipe; /* Input side of pipe associated 148 * with job's output channel */ 149 #ifndef RMT_WILL_WATCH 150 struct pollfd *op_inPollfd; /* pollfd associated with inPipe */ 151 #endif 152 int op_outPipe; /* Output side of pipe associated with 153 * job's output channel */ 154 char op_outBuf[JOB_BUFSIZE + 1]; 155 /* Buffer for storing the output of the 156 * job, line by line */ 157 int op_curPos; /* Current position in op_outBuf */ 158 } o_pipe; /* data used when catching the output via 159 * a pipe */ 160 struct { 161 char of_outFile[sizeof(TMPPAT)+2]; 162 /* Name of file to which shell output 163 * was rerouted */ 164 int of_outFd; /* Stream open to the output 165 * file. Used to funnel all 166 * from a single job to one file 167 * while still allowing 168 * multiple shell invocations */ 169 } o_file; /* Data used when catching the output in 170 * a temporary file */ 171 } output; /* Data for tracking a shell's output */ 172 } Job; 173 174 #define outPipe output.o_pipe.op_outPipe 175 #define inPipe output.o_pipe.op_inPipe 176 #define inPollfd output.o_pipe.op_inPollfd 177 #define outBuf output.o_pipe.op_outBuf 178 #define curPos output.o_pipe.op_curPos 179 #define outFile output.o_file.of_outFile 180 #define outFd output.o_file.of_outFd 181 182 183 /*- 184 * Shell Specifications: 185 * Each shell type has associated with it the following information: 186 * 1) The string which must match the last character of the shell name 187 * for the shell to be considered of this type. The longest match 188 * wins. 189 * 2) A command to issue to turn off echoing of command lines 190 * 3) A command to issue to turn echoing back on again 191 * 4) What the shell prints, and its length, when given the echo-off 192 * command. This line will not be printed when received from the shell 193 * 5) A boolean to tell if the shell has the ability to control 194 * error checking for individual commands. 195 * 6) The string to turn this checking on. 196 * 7) The string to turn it off. 197 * 8) The command-flag to give to cause the shell to start echoing 198 * commands right away. 199 * 9) The command-flag to cause the shell to Lib_Exit when an error is 200 * detected in one of the commands. 201 * 202 * Some special stuff goes on if a shell doesn't have error control. In such 203 * a case, errCheck becomes a printf template for echoing the command, 204 * should echoing be on and ignErr becomes another printf template for 205 * executing the command while ignoring the return status. If either of these 206 * strings is empty when hasErrCtl is FALSE, the command will be executed 207 * anyway as is and if it causes an error, so be it. 208 */ 209 typedef struct Shell { 210 char *name; /* the name of the shell. For Bourne and C 211 * shells, this is used only to find the 212 * shell description when used as the single 213 * source of a .SHELL target. For user-defined 214 * shells, this is the full path of the shell. 215 */ 216 Boolean hasEchoCtl; /* True if both echoOff and echoOn defined */ 217 char *echoOff; /* command to turn off echo */ 218 char *echoOn; /* command to turn it back on again */ 219 char *noPrint; /* command to skip when printing output from 220 * shell. This is usually the command which 221 * was executed to turn off echoing */ 222 int noPLen; /* length of noPrint command */ 223 Boolean hasErrCtl; /* set if can control error checking for 224 * individual commands */ 225 char *errCheck; /* string to turn error checking on */ 226 char *ignErr; /* string to turn off error checking */ 227 /* 228 * command-line flags 229 */ 230 char *echo; /* echo commands */ 231 char *exit; /* exit on error */ 232 } Shell; 233 234 extern int job_pipe[2]; /* token pipe for jobs. */ 235 extern int jobTokensRunning; /* tokens currently "out" */ 236 extern int jobTokensFree; /* tokens free but not yet released to pipe */ 237 238 #ifdef REMOTE 239 extern char *targFmt; /* Format string for banner that separates 240 * output from multiple jobs. Contains a 241 * single %s where the name of the node being 242 * made should be put. */ 243 extern GNode *lastNode; /* Last node for which a banner was printed. 244 * If Rmt module finds it necessary to print 245 * a banner, it should set this to the node 246 * for which the banner was printed */ 247 extern int nJobs; /* Number of jobs running (local and remote) */ 248 extern int nLocal; /* Number of jobs running locally */ 249 extern Lst jobs; /* List of active job descriptors */ 250 extern Lst stoppedJobs; /* List of jobs that are stopped or didn't 251 * quite get started */ 252 #endif 253 254 void Job_Touch(GNode *, Boolean); 255 Boolean Job_CheckCommands(GNode *, void (*abortProc )(char *, ...)); 256 void Job_CatchChildren(Boolean); 257 void Job_CatchOutput(void); 258 void Job_Make(GNode *); 259 void Job_Init(int, int); 260 Boolean Job_Full(void); 261 Boolean Job_Empty(void); 262 ReturnStatus Job_ParseShell(char *); 263 int Job_Finish(void); 264 void Job_End(void); 265 void Job_Wait(void); 266 void Job_AbortAll(void); 267 void JobFlagForMigration(int); 268 void Job_TokenReturn(void); 269 void Job_TokenFlush(void); 270 Boolean Job_TokenWithdraw(void); 271 void Job_ServerStart(int); 272 273 #endif /* _JOB_H_ */ 274