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