xref: /freebsd/contrib/bmake/job.h (revision 6137b5f7b8c183ee8806d79b3f1d8e5e3ddb3df3)
1 /*	$NetBSD: job.h,v 1.78 2023/12/19 19:33:39 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 /* Run jobs in parallel mode. */
77 
78 #ifndef MAKE_JOB_H
79 #define MAKE_JOB_H
80 
81 #define TMPPAT	"makeXXXXXX"	/* relative to tmpdir */
82 
83 #ifdef USE_SELECT
84 /*
85  * Emulate poll() in terms of select().  This is not a complete
86  * emulation but it is sufficient for make's purposes.
87  */
88 
89 #define poll emul_poll
90 #define pollfd emul_pollfd
91 
92 struct emul_pollfd {
93 	int fd;
94 	short events;
95 	short revents;
96 };
97 
98 #define POLLIN		0x0001
99 #define POLLOUT		0x0004
100 
101 int emul_poll(struct pollfd *, int, int);
102 #endif
103 
104 /*
105  * The POLL_MSEC constant determines the maximum number of milliseconds spent
106  * in poll before coming out to see if a child has finished.
107  */
108 #define POLL_MSEC	5000
109 
110 struct pollfd;
111 
112 
113 #ifdef USE_META
114 # include "meta.h"
115 #endif
116 
117 typedef enum JobStatus {
118 	JOB_ST_FREE	= 0,	/* Job is available */
119 	JOB_ST_SET_UP	= 1,	/* Job is allocated but otherwise invalid */
120 	/* XXX: What about the 2? */
121 	JOB_ST_RUNNING	= 3,	/* Job is running, pid valid */
122 	JOB_ST_FINISHED	= 4	/* Job is done (ie after SIGCHILD) */
123 } JobStatus;
124 
125 /*
126  * A Job manages the shell commands that are run to create a single target.
127  * Each job is run in a separate subprocess by a shell.  Several jobs can run
128  * in parallel.
129  *
130  * The shell commands for the target are written to a temporary file,
131  * then the shell is run with the temporary file as stdin, and the output
132  * of that shell is captured via a pipe.
133  *
134  * When a job is finished, Make_Update updates all parents of the node
135  * that was just remade, marking them as ready to be made next if all
136  * other dependencies are finished as well.
137  */
138 typedef struct Job {
139 	/* The process ID of the shell running the commands */
140 	int pid;
141 
142 	/* The target the child is making */
143 	GNode *node;
144 
145 	/*
146 	 * If one of the shell commands is "...", all following commands are
147 	 * delayed until the .END node is made.  This list node points to the
148 	 * first of these commands, if any.
149 	 */
150 	StringListNode *tailCmds;
151 
152 	/* This is where the shell commands go. */
153 	FILE *cmdFILE;
154 
155 	int exit_status;	/* from wait4() in signal handler */
156 
157 	JobStatus status;
158 
159 	bool suspended;
160 
161 	/* Ignore non-zero exits */
162 	bool ignerr;
163 	/* Output the command before or instead of running it. */
164 	bool echo;
165 	/* Target is a special one. */
166 	bool special;
167 
168 	int inPipe;		/* Pipe for reading output from job */
169 	int outPipe;		/* Pipe for writing control commands */
170 	struct pollfd *inPollfd; /* pollfd associated with inPipe */
171 
172 #define JOB_BUFSIZE	1024
173 	/* Buffer for storing the output of the job, line by line. */
174 	char outBuf[JOB_BUFSIZE + 1];
175 	size_t curPos;		/* Current position in outBuf. */
176 
177 #ifdef USE_META
178 	struct BuildMon bm;
179 #endif
180 } Job;
181 
182 extern const char *shellPath;
183 extern const char *shellName;
184 extern char *shellErrFlag;
185 
186 extern int jobTokensRunning;	/* tokens currently "out" */
187 
188 void Shell_Init(void);
189 const char *Shell_GetNewline(void) MAKE_ATTR_USE;
190 void Job_Touch(GNode *, bool);
191 bool Job_CheckCommands(GNode *, void (*abortProc)(const char *, ...))
192     MAKE_ATTR_USE;
193 void Job_CatchChildren(void);
194 void Job_CatchOutput(void);
195 void Job_Make(GNode *);
196 void Job_Init(void);
197 bool Job_ParseShell(char *) MAKE_ATTR_USE;
198 int Job_Finish(void);
199 void Job_End(void);
200 void Job_Wait(void);
201 void Job_AbortAll(void);
202 void Job_TokenReturn(void);
203 bool Job_TokenWithdraw(void) MAKE_ATTR_USE;
204 void Job_ServerStart(int, int, int);
205 void Job_SetPrefix(void);
206 bool Job_RunTarget(const char *, const char *);
207 void Job_FlagsToString(const Job *, char *, size_t);
208 int Job_TempFile(const char *, char *, size_t) MAKE_ATTR_USE;
209 
210 #endif
211