job.h revision 1.31
1/*	$NetBSD: job.h,v 1.31 2006/10/09 13:40:11 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 */
80#ifndef _JOB_H_
81#define _JOB_H_
82
83#define TMPPAT	"/tmp/makeXXXXXX"
84
85#ifdef USE_SELECT
86/*
87 * Emulate poll() in terms of select().  This is not a complete
88 * emulation but it is sufficient for make's purposes.
89 */
90
91#define poll emul_poll
92#define pollfd emul_pollfd
93
94struct emul_pollfd {
95    int fd;
96    short events;
97    short revents;
98};
99
100#define	POLLIN		0x0001
101#define	POLLOUT		0x0004
102
103int
104emul_poll(struct pollfd *fd, int nfd, int timeout);
105#endif
106
107/*
108 * The POLL_MSEC constant determines the maximum number of milliseconds spent
109 * in poll before coming out to see if a child has finished.
110 */
111#define POLL_MSEC	5000
112
113
114/*-
115 * Job Table definitions.
116 *
117 * Each job has several things associated with it:
118 *	1) The process id of the child shell
119 *	2) The graph node describing the target being made by this job
120 *	3) A LstNode for the first command to be saved after the job
121 *	   completes. This is NILLNODE if there was no "..." in the job's
122 *	   commands.
123 *	4) An FILE* for writing out the commands. This is only
124 *	   used before the job is actually started.
125 *	5) A union of things used for handling the shell's output. Different
126 *	   parts of the union are used based on the value of the usePipes
127 *	   flag. If it is true, the output is being caught via a pipe and
128 *	   the descriptors of our pipe, an array in which output is line
129 *	   buffered and the current position in that buffer are all
130 *	   maintained for each job. If, on the other hand, usePipes is false,
131 *	   the output is routed to a temporary file and all that is kept
132 *	   is the name of the file and the descriptor open to the file.
133 *	6) A word of flags which determine how the module handles errors,
134 *	   echoing, etc. for the job
135 *
136 * The job "table" is kept as a linked Lst in 'jobs', with the number of
137 * active jobs maintained in the 'nJobs' variable. At no time will this
138 * exceed the value of 'maxJobs', initialized by the Job_Init function.
139 *
140 * When a job is finished, the Make_Update function is called on each of the
141 * parents of the node which was just remade. This takes care of the upward
142 * traversal of the dependency graph.
143 */
144struct pollfd;
145
146#define JOB_BUFSIZE	1024
147typedef struct Job {
148    int       	pid;	    /* The child's process ID */
149    GNode    	*node;      /* The target the child is making */
150    LstNode 	tailCmds;   /* The node of the first command to be
151			     * saved when the job has been run */
152    FILE 	*cmdFILE;   /* When creating the shell script, this is
153			     * where the commands go */
154    int		exit_status; /* from wait4() in signal handler */
155    char        job_state;  /* status of the job entry */
156#define JOB_ST_FREE	0	/* Job is available */
157#define JOB_ST_SETUP	1	/* Job is allocated but otherwise invalid */
158#define JOB_ST_RUNNING	3	/* Job is running, pid valid */
159#define JOB_ST_FINISHED	4	/* Job is done (ie after SIGCHILD) */
160    char        job_suspended;
161    short      	flags;	    /* Flags to control treatment of job */
162#define	JOB_IGNERR	0x001	/* Ignore non-zero exits */
163#define	JOB_SILENT	0x002	/* no output */
164#define JOB_SPECIAL	0x004	/* Target is a special one. i.e. run it locally
165				 * if we can't export it and maxLocal is 0 */
166#define JOB_IGNDOTS	0x008  	/* Ignore "..." lines when processing
167				 * commands */
168#define JOB_TRACED	0x400	/* we've sent 'set -x' */
169
170    union {
171	struct {
172	    int	  	op_inPipe;	/* Input side of pipe associated
173					 * with job's output channel */
174	    struct pollfd *op_inPollfd;	/* pollfd associated with inPipe */
175	    int   	op_outPipe;	/* Output side of pipe associated with
176					 * job's output channel */
177	    char  	op_outBuf[JOB_BUFSIZE + 1];
178	    	  	    	    	/* Buffer for storing the output of the
179					 * job, line by line */
180	    int   	op_curPos;	/* Current position in op_outBuf */
181	}   	    o_pipe;	    /* data used when catching the output via
182				     * a pipe */
183	struct {
184	    char  	of_outFile[sizeof(TMPPAT)+2];
185	    	  	    	    	/* Name of file to which shell output
186					 * was rerouted */
187	    int	    	of_outFd;	/* Stream open to the output
188					 * file. Used to funnel all
189					 * from a single job to one file
190					 * while still allowing
191					 * multiple shell invocations */
192	}   	    o_file;	    /* Data used when catching the output in
193				     * a temporary file */
194    }       	output;	    /* Data for tracking a shell's output */
195} Job;
196
197#define outPipe	  	output.o_pipe.op_outPipe
198#define inPipe	  	output.o_pipe.op_inPipe
199#define inPollfd	output.o_pipe.op_inPollfd
200#define outBuf		output.o_pipe.op_outBuf
201#define curPos		output.o_pipe.op_curPos
202#define outFile		output.o_file.of_outFile
203#define outFd	  	output.o_file.of_outFd
204
205
206/*-
207 * Shell Specifications:
208 * Each shell type has associated with it the following information:
209 *	1) The string which must match the last character of the shell name
210 *	   for the shell to be considered of this type. The longest match
211 *	   wins.
212 *	2) A command to issue to turn off echoing of command lines
213 *	3) A command to issue to turn echoing back on again
214 *	4) What the shell prints, and its length, when given the echo-off
215 *	   command. This line will not be printed when received from the shell
216 *	5) A boolean to tell if the shell has the ability to control
217 *	   error checking for individual commands.
218 *	6) The string to turn this checking on.
219 *	7) The string to turn it off.
220 *	8) The command-flag to give to cause the shell to start echoing
221 *	   commands right away.
222 *	9) The command-flag to cause the shell to Lib_Exit when an error is
223 *	   detected in one of the commands.
224 *
225 * Some special stuff goes on if a shell doesn't have error control. In such
226 * a case, errCheck becomes a printf template for echoing the command,
227 * should echoing be on and ignErr becomes another printf template for
228 * executing the command while ignoring the return status. Finally errOut
229 * is a printf template for running the command and causing the shell to
230 * exit on error. If any of these strings are empty when hasErrCtl is FALSE,
231 * the command will be executed anyway as is and if it causes an error, so be
232 * it. Any templates setup to echo the command will escape any '$ ` \ "'i
233 * characters in the command string to avoid common problems with
234 * echo "%s\n" as a template.
235 */
236typedef struct Shell {
237    const char	 *name;		/* the name of the shell. For Bourne and C
238				 * shells, this is used only to find the
239				 * shell description when used as the single
240				 * source of a .SHELL target. For user-defined
241				 * shells, this is the full path of the shell.
242				 */
243    Boolean 	  hasEchoCtl;	/* True if both echoOff and echoOn defined */
244    const char   *echoOff;	/* command to turn off echo */
245    const char   *echoOn;	/* command to turn it back on again */
246    const char   *noPrint;	/* command to skip when printing output from
247				 * shell. This is usually the command which
248				 * was executed to turn off echoing */
249    int           noPLen;	/* length of noPrint command */
250    Boolean	  hasErrCtl;	/* set if can control error checking for
251				 * individual commands */
252    const char	 *errCheck;	/* string to turn error checking on */
253    const char	 *ignErr;	/* string to turn off error checking */
254    const char	 *errOut;	/* string to use for testing exit code */
255    const char	 *newline;	/* string literal that results in a newline
256				 * character when it appears outside of any
257				 * 'quote' or "quote" characters */
258    char   commentChar;		/* character used by shell for comment lines */
259
260    /*
261     * command-line flags
262     */
263    const char   *echo;		/* echo commands */
264    const char   *exit;		/* exit on error */
265}               Shell;
266
267extern const char *shellPath;
268extern const char *shellName;
269
270extern int	job_pipe[2];	/* token pipe for jobs. */
271extern int	jobTokensRunning; /* tokens currently "out" */
272extern int	maxJobs;	/* Max jobs we can run */
273extern int	maxJobTokens;	/* Number of token for the job pipe */
274
275void Shell_Init(void);
276const char *Shell_GetNewline(void);
277void Job_Touch(GNode *, Boolean);
278Boolean Job_CheckCommands(GNode *, void (*abortProc )(const char *, ...));
279#define CATCH_BLOCK	1
280void Job_CatchChildren(unsigned int);
281void Job_CatchOutput(void);
282void Job_Make(GNode *);
283void Job_Init(void);
284Boolean Job_Full(void);
285Boolean Job_Empty(void);
286ReturnStatus Job_ParseShell(char *);
287int Job_Finish(void);
288void Job_End(void);
289void Job_Wait(void);
290void Job_AbortAll(void);
291void JobFlagForMigration(int);
292void Job_TokenReturn(void);
293Boolean Job_TokenWithdraw(void);
294void Job_ServerStart(void);
295
296#endif /* _JOB_H_ */
297