job.h revision 138232
1/*
2 * Copyright (c) 1988, 1989, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * Copyright (c) 1988, 1989 by Adam de Boor
5 * Copyright (c) 1989 by Berkeley Softworks
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Adam de Boor.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the University of
22 *	California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *	@(#)job.h	8.1 (Berkeley) 6/6/93
40 * $FreeBSD: head/usr.bin/make/job.h 138232 2004-11-30 17:46:29Z harti $
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/makeXXXXXXXXXX"
52
53#ifndef USE_KQUEUE
54/*
55 * The SEL_ constants determine the maximum amount of time spent in select
56 * before coming out to see if a child has finished. SEL_SEC is the number of
57 * seconds and SEL_USEC is the number of micro-seconds
58 */
59#define	SEL_SEC		2
60#define	SEL_USEC	0
61#endif /* !USE_KQUEUE */
62
63/*-
64 * Job Table definitions.
65 *
66 * Each job has several things associated with it:
67 *	1) The process id of the child shell
68 *	2) The graph node describing the target being made by this job
69 *	3) A LstNode for the first command to be saved after the job
70 *	   completes. This is NULL if there was no "..." in the job's
71 *	   commands.
72 *	4) An FILE* for writing out the commands. This is only
73 *	   used before the job is actually started.
74 *	5) A union of things used for handling the shell's output. Different
75 *	   parts of the union are used based on the value of the usePipes
76 *	   flag. If it is true, the output is being caught via a pipe and
77 *	   the descriptors of our pipe, an array in which output is line
78 *	   buffered and the current position in that buffer are all
79 *	   maintained for each job. If, on the other hand, usePipes is false,
80 *	   the output is routed to a temporary file and all that is kept
81 *	   is the name of the file and the descriptor open to the file.
82 *	6) A word of flags which determine how the module handles errors,
83 *	   echoing, etc. for the job
84 *
85 * The job "table" is kept as a linked Lst in 'jobs', with the number of
86 * active jobs maintained in the 'nJobs' variable. At no time will this
87 * exceed the value of 'maxJobs', initialized by the Job_Init function.
88 *
89 * When a job is finished, the Make_Update function is called on each of the
90 * parents of the node which was just remade. This takes care of the upward
91 * traversal of the dependency graph.
92 */
93#define	JOB_BUFSIZE	1024
94typedef struct Job {
95    int       	pid;	    /* The child's process ID */
96    char	tfile[sizeof(TMPPAT)];
97			    /* Temporary file to use for job */
98    GNode    	*node;      /* The target the child is making */
99    LstNode 	tailCmds;   /* The node of the first command to be
100			     * saved when the job has been run */
101    FILE 	*cmdFILE;   /* When creating the shell script, this is
102			     * where the commands go */
103    short      	flags;	    /* Flags to control treatment of job */
104#define	JOB_IGNERR	0x001	/* Ignore non-zero exits */
105#define	JOB_SILENT	0x002	/* no output */
106#define	JOB_SPECIAL	0x004	/* Target is a special one. i.e. run it locally
107				 * if we can't export it and maxLocal is 0 */
108#define	JOB_IGNDOTS	0x008  	/* Ignore "..." lines when processing
109				 * commands */
110#define	JOB_FIRST	0x020	/* Job is first job for the node */
111#define	JOB_RESTART	0x080	/* Job needs to be completely restarted */
112#define	JOB_RESUME	0x100	/* Job needs to be resumed b/c it stopped,
113				 * for some reason */
114#define	JOB_CONTINUING	0x200	/* We are in the process of resuming this job.
115				 * Used to avoid infinite recursion between
116				 * JobFinish and JobRestart */
117    union {
118	struct {
119	    int	  	op_inPipe;	/* Input side of pipe associated
120					 * with job's output channel */
121	    int   	op_outPipe;	/* Output side of pipe associated with
122					 * job's output channel */
123	    char  	op_outBuf[JOB_BUFSIZE + 1];
124	    	  	    	    	/* Buffer for storing the output of the
125					 * job, line by line */
126	    int   	op_curPos;	/* Current position in op_outBuf */
127	}   	    o_pipe;	    /* data used when catching the output via
128				     * a pipe */
129	struct {
130	    char  	of_outFile[sizeof(TMPPAT)];
131	    	  	    	    	/* Name of file to which shell output
132					 * was rerouted */
133	    int	    	of_outFd;	/* Stream open to the output
134					 * file. Used to funnel all
135					 * from a single job to one file
136					 * while still allowing
137					 * multiple shell invocations */
138	}   	    o_file;	    /* Data used when catching the output in
139				     * a temporary file */
140    }       	output;	    /* Data for tracking a shell's output */
141} Job;
142
143#define	outPipe	  	output.o_pipe.op_outPipe
144#define	inPipe	  	output.o_pipe.op_inPipe
145#define	outBuf		output.o_pipe.op_outBuf
146#define	curPos		output.o_pipe.op_curPos
147#define	outFile		output.o_file.of_outFile
148#define	outFd	  	output.o_file.of_outFd
149
150/*-
151 * Shell Specifications:
152 * Each shell type has associated with it the following information:
153 *	1) The string which must match the last character of the shell name
154 *	   for the shell to be considered of this type. The longest match
155 *	   wins.
156 *	2) A command to issue to turn off echoing of command lines
157 *	3) A command to issue to turn echoing back on again
158 *	4) What the shell prints, and its length, when given the echo-off
159 *	   command. This line will not be printed when received from the shell
160 *	5) A boolean to tell if the shell has the ability to control
161 *	   error checking for individual commands.
162 *	6) The string to turn this checking on.
163 *	7) The string to turn it off.
164 *	8) The command-flag to give to cause the shell to start echoing
165 *	   commands right away.
166 *	9) The command-flag to cause the shell to Lib_Exit when an error is
167 *	   detected in one of the commands.
168 *
169 * Some special stuff goes on if a shell doesn't have error control. In such
170 * a case, errCheck becomes a printf template for echoing the command,
171 * should echoing be on and ignErr becomes another printf template for
172 * executing the command while ignoring the return status. If either of these
173 * strings is empty when hasErrCtl is FALSE, the command will be executed
174 * anyway as is and if it causes an error, so be it.
175 */
176#define	DEF_SHELL_STRUCT(TAG, CONST) \
177struct TAG { \
178    CONST char	  *name;	/* the name of the shell. For Bourne and C \
179				 * shells, this is used only to find the \
180				 * shell description when used as the single \
181				 * source of a .SHELL target. For user-defined \
182				 * shells, this is the full path of the shell. \
183				 */ \
184    Boolean 	  hasEchoCtl;	/* True if both echoOff and echoOn defined */ \
185    CONST char    *echoOff;	/* command to turn off echo */ \
186    CONST char    *echoOn;	/* command to turn it back on again */ \
187    CONST char    *noPrint;	/* command to skip when printing output from \
188				 * shell. This is usually the command which \
189				 * was executed to turn off echoing */ \
190    int           noPLen;	/* length of noPrint command */ \
191    Boolean	  hasErrCtl;	/* set if can control error checking for \
192				 * individual commands */ \
193    CONST char	  *errCheck;	/* string to turn error checking on */ \
194    CONST char	  *ignErr;	/* string to turn off error checking */ \
195    /* \
196     * command-line flags \
197     */ \
198    CONST char          *echo;	/* echo commands */ \
199    CONST char          *exit;	/* exit on error */ \
200}
201
202typedef DEF_SHELL_STRUCT(Shell,) Shell;
203
204extern char *shellPath;
205extern char *shellName;
206extern int	maxJobs;	/* Number of jobs that may run */
207
208
209void Shell_Init(void);
210void Job_Touch(GNode *, Boolean);
211Boolean Job_CheckCommands(GNode *, void (*abortProc)(const char *, ...));
212void Job_CatchChildren(Boolean);
213void Job_CatchOutput(int flag);
214void Job_Make(GNode *);
215void Job_Init(int);
216Boolean Job_Full(void);
217Boolean Job_Empty(void);
218ReturnStatus Job_ParseShell(char *);
219int Job_Finish(void);
220void Job_Wait(void);
221void Job_AbortAll(void);
222
223#endif /* _JOB_H_ */
224