xref: /openbsd/usr.bin/make/job.h (revision f2dfb0a4)
1 /*	$OpenBSD: job.h,v 1.4 1997/06/15 21:29:23 millert Exp $	*/
2 /*	$NetBSD: job.h,v 1.5 1996/11/06 17:59:10 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
6  * Copyright (c) 1988, 1989 by Adam de Boor
7  * Copyright (c) 1989 by Berkeley Softworks
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Adam de Boor.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	from: @(#)job.h	8.1 (Berkeley) 6/6/93
42  */
43 
44 /*-
45  * job.h --
46  *	Definitions pertaining to the running of jobs in parallel mode.
47  *	Exported from job.c for the use of remote-execution modules.
48  */
49 #ifndef _JOB_H_
50 #define _JOB_H_
51 
52 #define TMPPAT	"/tmp/makeXXXXXXXXXX"
53 
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		0
60 #define SEL_USEC	500000
61 
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 NILLNODE 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) An identifier provided by and for the exclusive use of the
83  *	   Rmt module.
84  *	7) A word of flags which determine how the module handles errors,
85  *	   echoing, etc. for the job
86  *
87  * The job "table" is kept as a linked Lst in 'jobs', with the number of
88  * active jobs maintained in the 'nJobs' variable. At no time will this
89  * exceed the value of 'maxJobs', initialized by the Job_Init function.
90  *
91  * When a job is finished, the Make_Update function is called on each of the
92  * parents of the node which was just remade. This takes care of the upward
93  * traversal of the dependency graph.
94  */
95 #define JOB_BUFSIZE	1024
96 typedef struct Job {
97     int       	pid;	    /* The child's process ID */
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     int    	rmtID;     /* ID returned from Rmt module */
104     short      	flags;	    /* Flags to control treatment of job */
105 #define	JOB_IGNERR	0x001	/* Ignore non-zero exits */
106 #define	JOB_SILENT	0x002	/* no output */
107 #define JOB_SPECIAL	0x004	/* Target is a special one. i.e. run it locally
108 				 * if we can't export it and maxLocal is 0 */
109 #define JOB_IGNDOTS	0x008  	/* Ignore "..." lines when processing
110 				 * commands */
111 #define JOB_REMOTE	0x010	/* Job is running remotely */
112 #define JOB_FIRST	0x020	/* Job is first job for the node */
113 #define JOB_REMIGRATE	0x040	/* Job needs to be remigrated */
114 #define JOB_RESTART	0x080	/* Job needs to be completely restarted */
115 #define JOB_RESUME	0x100	/* Job needs to be resumed b/c it stopped,
116 				 * for some reason */
117 #define JOB_CONTINUING	0x200	/* We are in the process of resuming this job.
118 				 * Used to avoid infinite recursion between
119 				 * JobFinish and JobRestart */
120     union {
121 	struct {
122 	    int	  	op_inPipe;	/* Input side of pipe associated
123 					 * with job's output channel */
124 	    int   	op_outPipe;	/* Output side of pipe associated with
125 					 * job's output channel */
126 	    char  	op_outBuf[JOB_BUFSIZE + 1];
127 	    	  	    	    	/* Buffer for storing the output of the
128 					 * job, line by line */
129 	    int   	op_curPos;	/* Current position in op_outBuf */
130 	}   	    o_pipe;	    /* data used when catching the output via
131 				     * a pipe */
132 	struct {
133 	    char  	of_outFile[sizeof(TMPPAT)];
134 	    	  	    	    	/* Name of file to which shell output
135 					 * was rerouted */
136 	    int	    	of_outFd;	/* Stream open to the output
137 					 * file. Used to funnel all
138 					 * from a single job to one file
139 					 * while still allowing
140 					 * multiple shell invocations */
141 	}   	    o_file;	    /* Data used when catching the output in
142 				     * a temporary file */
143     }       	output;	    /* Data for tracking a shell's output */
144 } Job;
145 
146 #define outPipe	  	output.o_pipe.op_outPipe
147 #define inPipe	  	output.o_pipe.op_inPipe
148 #define outBuf		output.o_pipe.op_outBuf
149 #define curPos		output.o_pipe.op_curPos
150 #define outFile		output.o_file.of_outFile
151 #define outFd	  	output.o_file.of_outFd
152 
153 
154 /*-
155  * Shell Specifications:
156  * Each shell type has associated with it the following information:
157  *	1) The string which must match the last character of the shell name
158  *	   for the shell to be considered of this type. The longest match
159  *	   wins.
160  *	2) A command to issue to turn off echoing of command lines
161  *	3) A command to issue to turn echoing back on again
162  *	4) What the shell prints, and its length, when given the echo-off
163  *	   command. This line will not be printed when received from the shell
164  *	5) A boolean to tell if the shell has the ability to control
165  *	   error checking for individual commands.
166  *	6) The string to turn this checking on.
167  *	7) The string to turn it off.
168  *	8) The command-flag to give to cause the shell to start echoing
169  *	   commands right away.
170  *	9) The command-flag to cause the shell to Lib_Exit when an error is
171  *	   detected in one of the commands.
172  *
173  * Some special stuff goes on if a shell doesn't have error control. In such
174  * a case, errCheck becomes a printf template for echoing the command,
175  * should echoing be on and ignErr becomes another printf template for
176  * executing the command while ignoring the return status. If either of these
177  * strings is empty when hasErrCtl is FALSE, the command will be executed
178  * anyway as is and if it causes an error, so be it.
179  */
180 typedef struct Shell {
181     char	  *name;	/* the name of the shell. For Bourne and C
182 				 * shells, this is used only to find the
183 				 * shell description when used as the single
184 				 * source of a .SHELL target. For user-defined
185 				 * shells, this is the full path of the shell.
186 				 */
187     Boolean 	  hasEchoCtl;	/* True if both echoOff and echoOn defined */
188     char          *echoOff;	/* command to turn off echo */
189     char          *echoOn;	/* command to turn it back on again */
190     char          *noPrint;	/* command to skip when printing output from
191 				 * shell. This is usually the command which
192 				 * was executed to turn off echoing */
193     int           noPLen;	/* length of noPrint command */
194     Boolean	  hasErrCtl;	/* set if can control error checking for
195 				 * individual commands */
196     char	  *errCheck;	/* string to turn error checking on */
197     char	  *ignErr;	/* string to turn off error checking */
198     /*
199      * command-line flags
200      */
201     char          *echo;	/* echo commands */
202     char          *exit;	/* exit on error */
203 }               Shell;
204 
205 
206 extern char 	*targFmt;   	/* Format string for banner that separates
207 				 * output from multiple jobs. Contains a
208 				 * single %s where the name of the node being
209 				 * made should be put. */
210 extern GNode	*lastNode;  	/* Last node for which a banner was printed.
211 				 * If Rmt module finds it necessary to print
212 				 * a banner, it should set this to the node
213 				 * for which the banner was printed */
214 extern int  	nJobs;	    	/* Number of jobs running (local and remote) */
215 extern int  	nLocal;	    	/* Number of jobs running locally */
216 extern Lst  	jobs;	    	/* List of active job descriptors */
217 extern Lst  	stoppedJobs;	/* List of jobs that are stopped or didn't
218 				 * quite get started */
219 extern Boolean	jobFull;    	/* Non-zero if no more jobs should/will start*/
220 
221 
222 void Job_Touch __P((GNode *, Boolean));
223 Boolean Job_CheckCommands __P((GNode *, void (*abortProc )(char *, ...)));
224 void Job_CatchChildren __P((Boolean));
225 void Job_CatchOutput __P((void));
226 void Job_Make __P((GNode *));
227 void Job_Init __P((int, int));
228 Boolean Job_Full __P((void));
229 Boolean Job_Empty __P((void));
230 ReturnStatus Job_ParseShell __P((char *));
231 int Job_End __P((void));
232 void Job_Wait __P((void));
233 void Job_AbortAll __P((void));
234 void JobFlagForMigration __P((int));
235 
236 #endif /* _JOB_H_ */
237