1*84d9c625SLionel Sambuc /* $NetBSD: job.h,v 1.42 2013/07/05 22:14:56 sjg Exp $ */ 22e2caf59SThomas Veerman 32e2caf59SThomas Veerman /* 42e2caf59SThomas Veerman * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 52e2caf59SThomas Veerman * All rights reserved. 62e2caf59SThomas Veerman * 72e2caf59SThomas Veerman * This code is derived from software contributed to Berkeley by 82e2caf59SThomas Veerman * Adam de Boor. 92e2caf59SThomas Veerman * 102e2caf59SThomas Veerman * Redistribution and use in source and binary forms, with or without 112e2caf59SThomas Veerman * modification, are permitted provided that the following conditions 122e2caf59SThomas Veerman * are met: 132e2caf59SThomas Veerman * 1. Redistributions of source code must retain the above copyright 142e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer. 152e2caf59SThomas Veerman * 2. Redistributions in binary form must reproduce the above copyright 162e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer in the 172e2caf59SThomas Veerman * documentation and/or other materials provided with the distribution. 182e2caf59SThomas Veerman * 3. Neither the name of the University nor the names of its contributors 192e2caf59SThomas Veerman * may be used to endorse or promote products derived from this software 202e2caf59SThomas Veerman * without specific prior written permission. 212e2caf59SThomas Veerman * 222e2caf59SThomas Veerman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 232e2caf59SThomas Veerman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 242e2caf59SThomas Veerman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 252e2caf59SThomas Veerman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 262e2caf59SThomas Veerman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 272e2caf59SThomas Veerman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 282e2caf59SThomas Veerman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 292e2caf59SThomas Veerman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 302e2caf59SThomas Veerman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 312e2caf59SThomas Veerman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 322e2caf59SThomas Veerman * SUCH DAMAGE. 332e2caf59SThomas Veerman * 342e2caf59SThomas Veerman * from: @(#)job.h 8.1 (Berkeley) 6/6/93 352e2caf59SThomas Veerman */ 362e2caf59SThomas Veerman 372e2caf59SThomas Veerman /* 382e2caf59SThomas Veerman * Copyright (c) 1988, 1989 by Adam de Boor 392e2caf59SThomas Veerman * Copyright (c) 1989 by Berkeley Softworks 402e2caf59SThomas Veerman * All rights reserved. 412e2caf59SThomas Veerman * 422e2caf59SThomas Veerman * This code is derived from software contributed to Berkeley by 432e2caf59SThomas Veerman * Adam de Boor. 442e2caf59SThomas Veerman * 452e2caf59SThomas Veerman * Redistribution and use in source and binary forms, with or without 462e2caf59SThomas Veerman * modification, are permitted provided that the following conditions 472e2caf59SThomas Veerman * are met: 482e2caf59SThomas Veerman * 1. Redistributions of source code must retain the above copyright 492e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer. 502e2caf59SThomas Veerman * 2. Redistributions in binary form must reproduce the above copyright 512e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer in the 522e2caf59SThomas Veerman * documentation and/or other materials provided with the distribution. 532e2caf59SThomas Veerman * 3. All advertising materials mentioning features or use of this software 542e2caf59SThomas Veerman * must display the following acknowledgement: 552e2caf59SThomas Veerman * This product includes software developed by the University of 562e2caf59SThomas Veerman * California, Berkeley and its contributors. 572e2caf59SThomas Veerman * 4. Neither the name of the University nor the names of its contributors 582e2caf59SThomas Veerman * may be used to endorse or promote products derived from this software 592e2caf59SThomas Veerman * without specific prior written permission. 602e2caf59SThomas Veerman * 612e2caf59SThomas Veerman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 622e2caf59SThomas Veerman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 632e2caf59SThomas Veerman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 642e2caf59SThomas Veerman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 652e2caf59SThomas Veerman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 662e2caf59SThomas Veerman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 672e2caf59SThomas Veerman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 682e2caf59SThomas Veerman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 692e2caf59SThomas Veerman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 702e2caf59SThomas Veerman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 712e2caf59SThomas Veerman * SUCH DAMAGE. 722e2caf59SThomas Veerman * 732e2caf59SThomas Veerman * from: @(#)job.h 8.1 (Berkeley) 6/6/93 742e2caf59SThomas Veerman */ 752e2caf59SThomas Veerman 762e2caf59SThomas Veerman /*- 772e2caf59SThomas Veerman * job.h -- 782e2caf59SThomas Veerman * Definitions pertaining to the running of jobs in parallel mode. 792e2caf59SThomas Veerman */ 802e2caf59SThomas Veerman #ifndef _JOB_H_ 812e2caf59SThomas Veerman #define _JOB_H_ 822e2caf59SThomas Veerman 832e2caf59SThomas Veerman #define TMPPAT "makeXXXXXX" /* relative to tmpdir */ 842e2caf59SThomas Veerman 852e2caf59SThomas Veerman #ifdef USE_SELECT 862e2caf59SThomas Veerman /* 872e2caf59SThomas Veerman * Emulate poll() in terms of select(). This is not a complete 882e2caf59SThomas Veerman * emulation but it is sufficient for make's purposes. 892e2caf59SThomas Veerman */ 902e2caf59SThomas Veerman 912e2caf59SThomas Veerman #define poll emul_poll 922e2caf59SThomas Veerman #define pollfd emul_pollfd 932e2caf59SThomas Veerman 942e2caf59SThomas Veerman struct emul_pollfd { 952e2caf59SThomas Veerman int fd; 962e2caf59SThomas Veerman short events; 972e2caf59SThomas Veerman short revents; 982e2caf59SThomas Veerman }; 992e2caf59SThomas Veerman 1002e2caf59SThomas Veerman #define POLLIN 0x0001 1012e2caf59SThomas Veerman #define POLLOUT 0x0004 1022e2caf59SThomas Veerman 1032e2caf59SThomas Veerman int 1042e2caf59SThomas Veerman emul_poll(struct pollfd *fd, int nfd, int timeout); 1052e2caf59SThomas Veerman #endif 1062e2caf59SThomas Veerman 1072e2caf59SThomas Veerman /* 1082e2caf59SThomas Veerman * The POLL_MSEC constant determines the maximum number of milliseconds spent 1092e2caf59SThomas Veerman * in poll before coming out to see if a child has finished. 1102e2caf59SThomas Veerman */ 1112e2caf59SThomas Veerman #define POLL_MSEC 5000 1122e2caf59SThomas Veerman 1132e2caf59SThomas Veerman 1142e2caf59SThomas Veerman /*- 1152e2caf59SThomas Veerman * Job Table definitions. 1162e2caf59SThomas Veerman * 1172e2caf59SThomas Veerman * Each job has several things associated with it: 1182e2caf59SThomas Veerman * 1) The process id of the child shell 1192e2caf59SThomas Veerman * 2) The graph node describing the target being made by this job 1202e2caf59SThomas Veerman * 3) A LstNode for the first command to be saved after the job 1212e2caf59SThomas Veerman * completes. This is NULL if there was no "..." in the job's 1222e2caf59SThomas Veerman * commands. 1232e2caf59SThomas Veerman * 4) An FILE* for writing out the commands. This is only 1242e2caf59SThomas Veerman * used before the job is actually started. 1252e2caf59SThomas Veerman * 5) The output is being caught via a pipe and 1262e2caf59SThomas Veerman * the descriptors of our pipe, an array in which output is line 1272e2caf59SThomas Veerman * buffered and the current position in that buffer are all 1282e2caf59SThomas Veerman * maintained for each job. 1292e2caf59SThomas Veerman * 6) A word of flags which determine how the module handles errors, 1302e2caf59SThomas Veerman * echoing, etc. for the job 1312e2caf59SThomas Veerman * 1322e2caf59SThomas Veerman * When a job is finished, the Make_Update function is called on each of the 1332e2caf59SThomas Veerman * parents of the node which was just remade. This takes care of the upward 1342e2caf59SThomas Veerman * traversal of the dependency graph. 1352e2caf59SThomas Veerman */ 1362e2caf59SThomas Veerman struct pollfd; 1372e2caf59SThomas Veerman 1382e2caf59SThomas Veerman 1392e2caf59SThomas Veerman #ifdef USE_META 1402e2caf59SThomas Veerman # include "meta.h" 1412e2caf59SThomas Veerman #endif 1422e2caf59SThomas Veerman 1432e2caf59SThomas Veerman #define JOB_BUFSIZE 1024 1442e2caf59SThomas Veerman typedef struct Job { 1452e2caf59SThomas Veerman int pid; /* The child's process ID */ 1462e2caf59SThomas Veerman GNode *node; /* The target the child is making */ 1472e2caf59SThomas Veerman LstNode tailCmds; /* The node of the first command to be 1482e2caf59SThomas Veerman * saved when the job has been run */ 1492e2caf59SThomas Veerman FILE *cmdFILE; /* When creating the shell script, this is 1502e2caf59SThomas Veerman * where the commands go */ 1512e2caf59SThomas Veerman int exit_status; /* from wait4() in signal handler */ 1522e2caf59SThomas Veerman char job_state; /* status of the job entry */ 1532e2caf59SThomas Veerman #define JOB_ST_FREE 0 /* Job is available */ 1542e2caf59SThomas Veerman #define JOB_ST_SETUP 1 /* Job is allocated but otherwise invalid */ 1552e2caf59SThomas Veerman #define JOB_ST_RUNNING 3 /* Job is running, pid valid */ 1562e2caf59SThomas Veerman #define JOB_ST_FINISHED 4 /* Job is done (ie after SIGCHILD) */ 1572e2caf59SThomas Veerman char job_suspended; 1582e2caf59SThomas Veerman short flags; /* Flags to control treatment of job */ 1592e2caf59SThomas Veerman #define JOB_IGNERR 0x001 /* Ignore non-zero exits */ 1602e2caf59SThomas Veerman #define JOB_SILENT 0x002 /* no output */ 1612e2caf59SThomas Veerman #define JOB_SPECIAL 0x004 /* Target is a special one. i.e. run it locally 1622e2caf59SThomas Veerman * if we can't export it and maxLocal is 0 */ 1632e2caf59SThomas Veerman #define JOB_IGNDOTS 0x008 /* Ignore "..." lines when processing 1642e2caf59SThomas Veerman * commands */ 1652e2caf59SThomas Veerman #define JOB_TRACED 0x400 /* we've sent 'set -x' */ 1662e2caf59SThomas Veerman 1672e2caf59SThomas Veerman int jobPipe[2]; /* Pipe for readind output from job */ 1682e2caf59SThomas Veerman struct pollfd *inPollfd; /* pollfd associated with inPipe */ 1692e2caf59SThomas Veerman char outBuf[JOB_BUFSIZE + 1]; 1702e2caf59SThomas Veerman /* Buffer for storing the output of the 1712e2caf59SThomas Veerman * job, line by line */ 1722e2caf59SThomas Veerman int curPos; /* Current position in op_outBuf */ 1732e2caf59SThomas Veerman 1742e2caf59SThomas Veerman #ifdef USE_META 1752e2caf59SThomas Veerman struct BuildMon bm; 1762e2caf59SThomas Veerman #endif 1772e2caf59SThomas Veerman } Job; 1782e2caf59SThomas Veerman 1792e2caf59SThomas Veerman #define inPipe jobPipe[0] 1802e2caf59SThomas Veerman #define outPipe jobPipe[1] 1812e2caf59SThomas Veerman 1822e2caf59SThomas Veerman 1832e2caf59SThomas Veerman /*- 1842e2caf59SThomas Veerman * Shell Specifications: 1852e2caf59SThomas Veerman * Each shell type has associated with it the following information: 1862e2caf59SThomas Veerman * 1) The string which must match the last character of the shell name 1872e2caf59SThomas Veerman * for the shell to be considered of this type. The longest match 1882e2caf59SThomas Veerman * wins. 1892e2caf59SThomas Veerman * 2) A command to issue to turn off echoing of command lines 1902e2caf59SThomas Veerman * 3) A command to issue to turn echoing back on again 1912e2caf59SThomas Veerman * 4) What the shell prints, and its length, when given the echo-off 1922e2caf59SThomas Veerman * command. This line will not be printed when received from the shell 1932e2caf59SThomas Veerman * 5) A boolean to tell if the shell has the ability to control 1942e2caf59SThomas Veerman * error checking for individual commands. 1952e2caf59SThomas Veerman * 6) The string to turn this checking on. 1962e2caf59SThomas Veerman * 7) The string to turn it off. 1972e2caf59SThomas Veerman * 8) The command-flag to give to cause the shell to start echoing 1982e2caf59SThomas Veerman * commands right away. 1992e2caf59SThomas Veerman * 9) The command-flag to cause the shell to Lib_Exit when an error is 2002e2caf59SThomas Veerman * detected in one of the commands. 2012e2caf59SThomas Veerman * 2022e2caf59SThomas Veerman * Some special stuff goes on if a shell doesn't have error control. In such 2032e2caf59SThomas Veerman * a case, errCheck becomes a printf template for echoing the command, 2042e2caf59SThomas Veerman * should echoing be on and ignErr becomes another printf template for 2052e2caf59SThomas Veerman * executing the command while ignoring the return status. Finally errOut 2062e2caf59SThomas Veerman * is a printf template for running the command and causing the shell to 2072e2caf59SThomas Veerman * exit on error. If any of these strings are empty when hasErrCtl is FALSE, 2082e2caf59SThomas Veerman * the command will be executed anyway as is and if it causes an error, so be 2092e2caf59SThomas Veerman * it. Any templates setup to echo the command will escape any '$ ` \ "'i 2102e2caf59SThomas Veerman * characters in the command string to avoid common problems with 2112e2caf59SThomas Veerman * echo "%s\n" as a template. 2122e2caf59SThomas Veerman */ 2132e2caf59SThomas Veerman typedef struct Shell { 2142e2caf59SThomas Veerman const char *name; /* the name of the shell. For Bourne and C 2152e2caf59SThomas Veerman * shells, this is used only to find the 2162e2caf59SThomas Veerman * shell description when used as the single 2172e2caf59SThomas Veerman * source of a .SHELL target. For user-defined 2182e2caf59SThomas Veerman * shells, this is the full path of the shell. 2192e2caf59SThomas Veerman */ 2202e2caf59SThomas Veerman Boolean hasEchoCtl; /* True if both echoOff and echoOn defined */ 2212e2caf59SThomas Veerman const char *echoOff; /* command to turn off echo */ 2222e2caf59SThomas Veerman const char *echoOn; /* command to turn it back on again */ 2232e2caf59SThomas Veerman const char *noPrint; /* command to skip when printing output from 2242e2caf59SThomas Veerman * shell. This is usually the command which 2252e2caf59SThomas Veerman * was executed to turn off echoing */ 2262e2caf59SThomas Veerman int noPLen; /* length of noPrint command */ 2272e2caf59SThomas Veerman Boolean hasErrCtl; /* set if can control error checking for 2282e2caf59SThomas Veerman * individual commands */ 2292e2caf59SThomas Veerman const char *errCheck; /* string to turn error checking on */ 2302e2caf59SThomas Veerman const char *ignErr; /* string to turn off error checking */ 2312e2caf59SThomas Veerman const char *errOut; /* string to use for testing exit code */ 2322e2caf59SThomas Veerman const char *newline; /* string literal that results in a newline 2332e2caf59SThomas Veerman * character when it appears outside of any 2342e2caf59SThomas Veerman * 'quote' or "quote" characters */ 2352e2caf59SThomas Veerman char commentChar; /* character used by shell for comment lines */ 2362e2caf59SThomas Veerman 2372e2caf59SThomas Veerman /* 2382e2caf59SThomas Veerman * command-line flags 2392e2caf59SThomas Veerman */ 2402e2caf59SThomas Veerman const char *echo; /* echo commands */ 2412e2caf59SThomas Veerman const char *exit; /* exit on error */ 2422e2caf59SThomas Veerman } Shell; 2432e2caf59SThomas Veerman 2442e2caf59SThomas Veerman extern const char *shellPath; 2452e2caf59SThomas Veerman extern const char *shellName; 246*84d9c625SLionel Sambuc extern char *shellErrFlag; 2472e2caf59SThomas Veerman 2482e2caf59SThomas Veerman extern int jobTokensRunning; /* tokens currently "out" */ 2492e2caf59SThomas Veerman extern int maxJobs; /* Max jobs we can run */ 2502e2caf59SThomas Veerman 2512e2caf59SThomas Veerman void Shell_Init(void); 2522e2caf59SThomas Veerman const char *Shell_GetNewline(void); 2532e2caf59SThomas Veerman void Job_Touch(GNode *, Boolean); 2542e2caf59SThomas Veerman Boolean Job_CheckCommands(GNode *, void (*abortProc )(const char *, ...)); 2552e2caf59SThomas Veerman #define CATCH_BLOCK 1 2562e2caf59SThomas Veerman void Job_CatchChildren(void); 2572e2caf59SThomas Veerman void Job_CatchOutput(void); 2582e2caf59SThomas Veerman void Job_Make(GNode *); 2592e2caf59SThomas Veerman void Job_Init(void); 2602e2caf59SThomas Veerman Boolean Job_Full(void); 2612e2caf59SThomas Veerman Boolean Job_Empty(void); 2622e2caf59SThomas Veerman ReturnStatus Job_ParseShell(char *); 2632e2caf59SThomas Veerman int Job_Finish(void); 2642e2caf59SThomas Veerman void Job_End(void); 2652e2caf59SThomas Veerman void Job_Wait(void); 2662e2caf59SThomas Veerman void Job_AbortAll(void); 2672e2caf59SThomas Veerman void JobFlagForMigration(int); 2682e2caf59SThomas Veerman void Job_TokenReturn(void); 2692e2caf59SThomas Veerman Boolean Job_TokenWithdraw(void); 2702e2caf59SThomas Veerman void Job_ServerStart(int, int, int); 2712e2caf59SThomas Veerman void Job_SetPrefix(void); 272*84d9c625SLionel Sambuc Boolean Job_RunTarget(const char *, const char *); 2732e2caf59SThomas Veerman 2742e2caf59SThomas Veerman #endif /* _JOB_H_ */ 275