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