job.h revision 1.55
1/*	$NetBSD: job.h,v 1.55 2020/10/05 21:37:07 rillig 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 * Running of jobs in parallel mode.
78 */
79
80#ifndef MAKE_JOB_H
81#define MAKE_JOB_H
82
83#define TMPPAT	"makeXXXXXX"		/* relative to tmpdir */
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
113struct pollfd;
114
115
116#ifdef USE_META
117# include "meta.h"
118#endif
119
120/* A Job manages the shell commands that are run to create a single target.
121 * Each job is run in a separate subprocess by a shell.  Several jobs can run
122 * in parallel.
123 *
124 * The shell commands for the target are written to a temporary file,
125 * then the shell is run with the temporary file as stdin, and the output
126 * of that shell is captured via a pipe.
127 *
128 * When a job is finished, Make_Update updates all parents of the node
129 * that was just remade, marking them as ready to be made next if all
130 * other dependencies are finished as well. */
131typedef struct Job {
132    /* The process ID of the shell running the commands */
133    int pid;
134
135    /* The target the child is making */
136    GNode *node;
137
138    /* If one of the shell commands is "...", all following commands are
139     * delayed until the .END node is made.  This list node points to the
140     * first of these commands, if any. */
141    StringListNode *tailCmds;
142
143    /* When creating the shell script, this is where the commands go.
144     * This is only used before the job is actually started. */
145    FILE *cmdFILE;
146
147    int exit_status;		/* from wait4() in signal handler */
148
149    char job_state;		/* status of the job entry */
150#define JOB_ST_FREE	0	/* Job is available */
151#define JOB_ST_SETUP	1	/* Job is allocated but otherwise invalid */
152#define JOB_ST_RUNNING	3	/* Job is running, pid valid */
153#define JOB_ST_FINISHED	4	/* Job is done (ie after SIGCHILD) */
154
155    char job_suspended;
156
157    int flags;			/* Flags to control treatment of job */
158#define	JOB_IGNERR	0x001	/* Ignore non-zero exits */
159#define	JOB_SILENT	0x002	/* no output */
160#define JOB_SPECIAL	0x004	/* Target is a special one. i.e. run it locally
161				 * if we can't export it and maxLocal is 0 */
162#define JOB_IGNDOTS	0x008	/* Ignore "..." lines when processing
163				 * commands */
164#define JOB_TRACED	0x400	/* we've sent 'set -x' */
165
166    int inPipe;			/* Pipe for reading output from job */
167    int outPipe;		/* Pipe for writing control commands */
168    struct pollfd *inPollfd;	/* pollfd associated with inPipe */
169
170#define JOB_BUFSIZE	1024
171    /* Buffer for storing the output of the job, line by line. */
172    char outBuf[JOB_BUFSIZE + 1];
173    size_t curPos;		/* Current position in outBuf. */
174
175#ifdef USE_META
176    struct BuildMon bm;
177#endif
178} Job;
179
180/*-
181 * Shell Specifications:
182 * Each shell type has associated with it the following information:
183 *	1) The string which must match the last character of the shell name
184 *	   for the shell to be considered of this type. The longest match
185 *	   wins.
186 *	2) A command to issue to turn off echoing of command lines
187 *	3) A command to issue to turn echoing back on again
188 *	4) What the shell prints, and its length, when given the echo-off
189 *	   command. This line will not be printed when received from the shell
190 *	5) A boolean to tell if the shell has the ability to control
191 *	   error checking for individual commands.
192 *	6) The string to turn this checking on.
193 *	7) The string to turn it off.
194 *	8) The command-flag to give to cause the shell to start echoing
195 *	   commands right away.
196 *	9) The command-flag to cause the shell to Lib_Exit when an error is
197 *	   detected in one of the commands.
198 *
199 * Some special stuff goes on if a shell doesn't have error control. In such
200 * a case, errCheck becomes a printf template for echoing the command,
201 * should echoing be on and ignErr becomes another printf template for
202 * executing the command while ignoring the return status. Finally errOut
203 * is a printf template for running the command and causing the shell to
204 * exit on error. If any of these strings are empty when hasErrCtl is FALSE,
205 * the command will be executed anyway as is and if it causes an error, so be
206 * it. Any templates setup to echo the command will escape any '$ ` \ "'i
207 * characters in the command string to avoid common problems with
208 * echo "%s\n" as a template.
209 */
210typedef struct Shell {
211    const char *name;		/* the name of the shell. For Bourne and C
212				 * shells, this is used only to find the
213				 * shell description when used as the single
214				 * source of a .SHELL target. For user-defined
215				 * shells, this is the full path of the shell.
216				 */
217    Boolean hasEchoCtl;		/* True if both echoOff and echoOn defined */
218    const char *echoOff;	/* command to turn off echo */
219    const char *echoOn;		/* command to turn it back on again */
220    const char *noPrint;	/* command to skip when printing output from
221				 * shell. This is usually the command which
222				 * was executed to turn off echoing */
223    size_t noPLen;		/* length of noPrint command */
224    Boolean hasErrCtl;		/* set if can control error checking for
225				 * individual commands */
226    const char *errCheck;	/* string to turn error checking on */
227    const char *ignErr;		/* string to turn off error checking */
228    const char *errOut;		/* string to use for testing exit code */
229    const char *newline;	/* string literal that results in a newline
230				 * character when it appears outside of any
231				 * 'quote' or "quote" characters */
232    char commentChar;		/* character used by shell for comment lines */
233
234    /*
235     * command-line flags
236     */
237    const char *echo;		/* echo commands */
238    const char *exit;		/* exit on error */
239} Shell;
240
241extern const char *shellPath;
242extern const char *shellName;
243extern char *shellErrFlag;
244
245extern int jobTokensRunning;	/* tokens currently "out" */
246extern int maxJobs;		/* Max jobs we can run */
247
248void Shell_Init(void);
249const char *Shell_GetNewline(void);
250void Job_Touch(GNode *, Boolean);
251Boolean Job_CheckCommands(GNode *, void (*abortProc)(const char *, ...));
252void Job_CatchChildren(void);
253void Job_CatchOutput(void);
254void Job_Make(GNode *);
255void Job_Init(void);
256Boolean Job_ParseShell(char *);
257int Job_Finish(void);
258void Job_End(void);
259void Job_Wait(void);
260void Job_AbortAll(void);
261void Job_TokenReturn(void);
262Boolean Job_TokenWithdraw(void);
263void Job_ServerStart(int, int, int);
264void Job_SetPrefix(void);
265Boolean Job_RunTarget(const char *, const char *);
266
267#endif /* MAKE_JOB_H */
268