1 | /* Job execution and handling for GNU Make.
|
---|
2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
---|
3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
|
---|
4 | Foundation, Inc.
|
---|
5 | This file is part of GNU Make.
|
---|
6 |
|
---|
7 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
8 | terms of the GNU General Public License as published by the Free Software
|
---|
9 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
10 | version.
|
---|
11 |
|
---|
12 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
14 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License along with
|
---|
17 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
18 |
|
---|
19 | #include "make.h"
|
---|
20 |
|
---|
21 | #include <assert.h>
|
---|
22 |
|
---|
23 | #include "job.h"
|
---|
24 | #include "debug.h"
|
---|
25 | #include "filedef.h"
|
---|
26 | #include "commands.h"
|
---|
27 | #include "variable.h"
|
---|
28 | #include "debug.h"
|
---|
29 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
---|
30 | # include "kmkbuiltin.h"
|
---|
31 | #endif
|
---|
32 | #ifdef KMK
|
---|
33 | # include "kbuild.h"
|
---|
34 | #endif
|
---|
35 |
|
---|
36 |
|
---|
37 | #include <string.h>
|
---|
38 |
|
---|
39 | /* Default shell to use. */
|
---|
40 | #ifdef WINDOWS32
|
---|
41 | #include <windows.h>
|
---|
42 |
|
---|
43 | char *default_shell = "sh.exe";
|
---|
44 | int no_default_sh_exe = 1;
|
---|
45 | int batch_mode_shell = 1;
|
---|
46 | HANDLE main_thread;
|
---|
47 |
|
---|
48 | #elif defined (_AMIGA)
|
---|
49 |
|
---|
50 | char default_shell[] = "";
|
---|
51 | extern int MyExecute (char **);
|
---|
52 | int batch_mode_shell = 0;
|
---|
53 |
|
---|
54 | #elif defined (__MSDOS__)
|
---|
55 |
|
---|
56 | /* The default shell is a pointer so we can change it if Makefile
|
---|
57 | says so. It is without an explicit path so we get a chance
|
---|
58 | to search the $PATH for it (since MSDOS doesn't have standard
|
---|
59 | directories we could trust). */
|
---|
60 | char *default_shell = "command.com";
|
---|
61 | int batch_mode_shell = 0;
|
---|
62 |
|
---|
63 | #elif defined (__EMX__)
|
---|
64 |
|
---|
65 | char *default_shell = "sh.exe"; /* bird changed this from "/bin/sh" as that doesn't make sense on OS/2. */
|
---|
66 | int batch_mode_shell = 0;
|
---|
67 |
|
---|
68 | #elif defined (VMS)
|
---|
69 |
|
---|
70 | # include <descrip.h>
|
---|
71 | char default_shell[] = "";
|
---|
72 | int batch_mode_shell = 0;
|
---|
73 |
|
---|
74 | #elif defined (__riscos__)
|
---|
75 |
|
---|
76 | char default_shell[] = "";
|
---|
77 | int batch_mode_shell = 0;
|
---|
78 |
|
---|
79 | #else
|
---|
80 |
|
---|
81 | char default_shell[] = "/bin/sh";
|
---|
82 | int batch_mode_shell = 0;
|
---|
83 |
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | #ifdef __MSDOS__
|
---|
87 | # include <process.h>
|
---|
88 | static int execute_by_shell;
|
---|
89 | static int dos_pid = 123;
|
---|
90 | int dos_status;
|
---|
91 | int dos_command_running;
|
---|
92 | #endif /* __MSDOS__ */
|
---|
93 |
|
---|
94 | #ifdef _AMIGA
|
---|
95 | # include <proto/dos.h>
|
---|
96 | static int amiga_pid = 123;
|
---|
97 | static int amiga_status;
|
---|
98 | static char amiga_bname[32];
|
---|
99 | static int amiga_batch_file;
|
---|
100 | #endif /* Amiga. */
|
---|
101 |
|
---|
102 | #ifdef VMS
|
---|
103 | # ifndef __GNUC__
|
---|
104 | # include <processes.h>
|
---|
105 | # endif
|
---|
106 | # include <starlet.h>
|
---|
107 | # include <lib$routines.h>
|
---|
108 | static void vmsWaitForChildren (int *);
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | #ifdef WINDOWS32
|
---|
112 | # include <windows.h>
|
---|
113 | # include <io.h>
|
---|
114 | # include <process.h>
|
---|
115 | # include "sub_proc.h"
|
---|
116 | # include "w32err.h"
|
---|
117 | # include "pathstuff.h"
|
---|
118 | #endif /* WINDOWS32 */
|
---|
119 |
|
---|
120 | #ifdef __EMX__
|
---|
121 | # include <process.h>
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | #if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT)
|
---|
125 | # include <sys/wait.h>
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | #ifdef HAVE_WAITPID
|
---|
129 | # define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG)
|
---|
130 | #else /* Don't have waitpid. */
|
---|
131 | # ifdef HAVE_WAIT3
|
---|
132 | # ifndef wait3
|
---|
133 | extern int wait3 ();
|
---|
134 | # endif
|
---|
135 | # define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0)
|
---|
136 | # endif /* Have wait3. */
|
---|
137 | #endif /* Have waitpid. */
|
---|
138 |
|
---|
139 | #if !defined (wait) && !defined (POSIX)
|
---|
140 | int wait ();
|
---|
141 | #endif
|
---|
142 |
|
---|
143 | #ifndef HAVE_UNION_WAIT
|
---|
144 |
|
---|
145 | # define WAIT_T int
|
---|
146 |
|
---|
147 | # ifndef WTERMSIG
|
---|
148 | # define WTERMSIG(x) ((x) & 0x7f)
|
---|
149 | # endif
|
---|
150 | # ifndef WCOREDUMP
|
---|
151 | # define WCOREDUMP(x) ((x) & 0x80)
|
---|
152 | # endif
|
---|
153 | # ifndef WEXITSTATUS
|
---|
154 | # define WEXITSTATUS(x) (((x) >> 8) & 0xff)
|
---|
155 | # endif
|
---|
156 | # ifndef WIFSIGNALED
|
---|
157 | # define WIFSIGNALED(x) (WTERMSIG (x) != 0)
|
---|
158 | # endif
|
---|
159 | # ifndef WIFEXITED
|
---|
160 | # define WIFEXITED(x) (WTERMSIG (x) == 0)
|
---|
161 | # endif
|
---|
162 |
|
---|
163 | #else /* Have `union wait'. */
|
---|
164 |
|
---|
165 | # define WAIT_T union wait
|
---|
166 | # ifndef WTERMSIG
|
---|
167 | # define WTERMSIG(x) ((x).w_termsig)
|
---|
168 | # endif
|
---|
169 | # ifndef WCOREDUMP
|
---|
170 | # define WCOREDUMP(x) ((x).w_coredump)
|
---|
171 | # endif
|
---|
172 | # ifndef WEXITSTATUS
|
---|
173 | # define WEXITSTATUS(x) ((x).w_retcode)
|
---|
174 | # endif
|
---|
175 | # ifndef WIFSIGNALED
|
---|
176 | # define WIFSIGNALED(x) (WTERMSIG(x) != 0)
|
---|
177 | # endif
|
---|
178 | # ifndef WIFEXITED
|
---|
179 | # define WIFEXITED(x) (WTERMSIG(x) == 0)
|
---|
180 | # endif
|
---|
181 |
|
---|
182 | #endif /* Don't have `union wait'. */
|
---|
183 |
|
---|
184 | #ifndef HAVE_UNISTD_H
|
---|
185 | # ifndef _MSC_VER /* bird */
|
---|
186 | int dup2 ();
|
---|
187 | int execve ();
|
---|
188 | void _exit ();
|
---|
189 | # endif /* bird */
|
---|
190 | # ifndef VMS
|
---|
191 | int geteuid ();
|
---|
192 | int getegid ();
|
---|
193 | int setgid ();
|
---|
194 | int getgid ();
|
---|
195 | # endif
|
---|
196 | #endif
|
---|
197 |
|
---|
198 | int getloadavg (double loadavg[], int nelem);
|
---|
199 | int start_remote_job (char **argv, char **envp, int stdin_fd, int *is_remote,
|
---|
200 | int *id_ptr, int *used_stdin);
|
---|
201 | int start_remote_job_p (int);
|
---|
202 | int remote_status (int *exit_code_ptr, int *signal_ptr, int *coredump_ptr,
|
---|
203 | int block);
|
---|
204 |
|
---|
205 | RETSIGTYPE child_handler (int);
|
---|
206 | static void free_child (struct child *);
|
---|
207 | static void start_job_command (struct child *child);
|
---|
208 | static int load_too_high (void);
|
---|
209 | static int job_next_command (struct child *);
|
---|
210 | static int start_waiting_job (struct child *);
|
---|
211 | |
---|
212 |
|
---|
213 | /* Chain of all live (or recently deceased) children. */
|
---|
214 |
|
---|
215 | struct child *children = 0;
|
---|
216 |
|
---|
217 | /* Number of children currently running. */
|
---|
218 |
|
---|
219 | unsigned int job_slots_used = 0;
|
---|
220 |
|
---|
221 | /* Nonzero if the `good' standard input is in use. */
|
---|
222 |
|
---|
223 | static int good_stdin_used = 0;
|
---|
224 |
|
---|
225 | /* Chain of children waiting to run until the load average goes down. */
|
---|
226 |
|
---|
227 | static struct child *waiting_jobs = 0;
|
---|
228 |
|
---|
229 | /* Non-zero if we use a *real* shell (always so on Unix). */
|
---|
230 |
|
---|
231 | int unixy_shell = 1;
|
---|
232 |
|
---|
233 | /* Number of jobs started in the current second. */
|
---|
234 |
|
---|
235 | unsigned long job_counter = 0;
|
---|
236 |
|
---|
237 | /* Number of jobserver tokens this instance is currently using. */
|
---|
238 |
|
---|
239 | unsigned int jobserver_tokens = 0;
|
---|
240 | |
---|
241 |
|
---|
242 | #ifdef WINDOWS32
|
---|
243 | /*
|
---|
244 | * The macro which references this function is defined in make.h.
|
---|
245 | */
|
---|
246 | int
|
---|
247 | w32_kill(int pid, int sig)
|
---|
248 | {
|
---|
249 | return ((process_kill((HANDLE)pid, sig) == TRUE) ? 0 : -1);
|
---|
250 | }
|
---|
251 |
|
---|
252 | /* This function creates a temporary file name with an extension specified
|
---|
253 | * by the unixy arg.
|
---|
254 | * Return an xmalloc'ed string of a newly created temp file and its
|
---|
255 | * file descriptor, or die. */
|
---|
256 | static char *
|
---|
257 | create_batch_file (char const *base, int unixy, int *fd)
|
---|
258 | {
|
---|
259 | const char *const ext = unixy ? "sh" : "bat";
|
---|
260 | const char *error = NULL;
|
---|
261 | char temp_path[MAXPATHLEN]; /* need to know its length */
|
---|
262 | unsigned path_size = GetTempPath(sizeof temp_path, temp_path);
|
---|
263 | int path_is_dot = 0;
|
---|
264 | unsigned uniq = 1;
|
---|
265 | const unsigned sizemax = strlen (base) + strlen (ext) + 10;
|
---|
266 |
|
---|
267 | if (path_size == 0)
|
---|
268 | {
|
---|
269 | path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
|
---|
270 | path_is_dot = 1;
|
---|
271 | }
|
---|
272 |
|
---|
273 | while (path_size > 0 &&
|
---|
274 | path_size + sizemax < sizeof temp_path &&
|
---|
275 | uniq < 0x10000)
|
---|
276 | {
|
---|
277 | unsigned size = sprintf (temp_path + path_size,
|
---|
278 | "%s%s-%x.%s",
|
---|
279 | temp_path[path_size - 1] == '\\' ? "" : "\\",
|
---|
280 | base, uniq, ext);
|
---|
281 | HANDLE h = CreateFile (temp_path, /* file name */
|
---|
282 | GENERIC_READ | GENERIC_WRITE, /* desired access */
|
---|
283 | 0, /* no share mode */
|
---|
284 | NULL, /* default security attributes */
|
---|
285 | CREATE_NEW, /* creation disposition */
|
---|
286 | FILE_ATTRIBUTE_NORMAL | /* flags and attributes */
|
---|
287 | FILE_ATTRIBUTE_TEMPORARY, /* we'll delete it */
|
---|
288 | NULL); /* no template file */
|
---|
289 |
|
---|
290 | if (h == INVALID_HANDLE_VALUE)
|
---|
291 | {
|
---|
292 | const DWORD er = GetLastError();
|
---|
293 |
|
---|
294 | if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS)
|
---|
295 | ++uniq;
|
---|
296 |
|
---|
297 | /* the temporary path is not guaranteed to exist */
|
---|
298 | else if (path_is_dot == 0)
|
---|
299 | {
|
---|
300 | path_size = GetCurrentDirectory (sizeof temp_path, temp_path);
|
---|
301 | path_is_dot = 1;
|
---|
302 | }
|
---|
303 |
|
---|
304 | else
|
---|
305 | {
|
---|
306 | error = map_windows32_error_to_string (er);
|
---|
307 | break;
|
---|
308 | }
|
---|
309 | }
|
---|
310 | else
|
---|
311 | {
|
---|
312 | const unsigned final_size = path_size + size + 1;
|
---|
313 | char *const path = xmalloc (final_size);
|
---|
314 | memcpy (path, temp_path, final_size);
|
---|
315 | *fd = _open_osfhandle ((long)h, 0);
|
---|
316 | if (unixy)
|
---|
317 | {
|
---|
318 | char *p;
|
---|
319 | int ch;
|
---|
320 | for (p = path; (ch = *p) != 0; ++p)
|
---|
321 | if (ch == '\\')
|
---|
322 | *p = '/';
|
---|
323 | }
|
---|
324 | return path; /* good return */
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | *fd = -1;
|
---|
329 | if (error == NULL)
|
---|
330 | error = _("Cannot create a temporary file\n");
|
---|
331 | fatal (NILF, error);
|
---|
332 |
|
---|
333 | /* not reached */
|
---|
334 | return NULL;
|
---|
335 | }
|
---|
336 | #endif /* WINDOWS32 */
|
---|
337 |
|
---|
338 | #ifdef __EMX__
|
---|
339 | /* returns whether path is assumed to be a unix like shell. */
|
---|
340 | int
|
---|
341 | _is_unixy_shell (const char *path)
|
---|
342 | {
|
---|
343 | /* list of non unix shells */
|
---|
344 | const char *known_os2shells[] = {
|
---|
345 | "cmd.exe",
|
---|
346 | "cmd",
|
---|
347 | "4os2.exe",
|
---|
348 | "4os2",
|
---|
349 | "4dos.exe",
|
---|
350 | "4dos",
|
---|
351 | "command.com",
|
---|
352 | "command",
|
---|
353 | NULL
|
---|
354 | };
|
---|
355 |
|
---|
356 | /* find the rightmost '/' or '\\' */
|
---|
357 | const char *name = strrchr (path, '/');
|
---|
358 | const char *p = strrchr (path, '\\');
|
---|
359 | unsigned i;
|
---|
360 |
|
---|
361 | if (name && p) /* take the max */
|
---|
362 | name = (name > p) ? name : p;
|
---|
363 | else if (p) /* name must be 0 */
|
---|
364 | name = p;
|
---|
365 | else if (!name) /* name and p must be 0 */
|
---|
366 | name = path;
|
---|
367 |
|
---|
368 | if (*name == '/' || *name == '\\') name++;
|
---|
369 |
|
---|
370 | i = 0;
|
---|
371 | while (known_os2shells[i] != NULL) {
|
---|
372 | if (strcasecmp (name, known_os2shells[i]) == 0)
|
---|
373 | return 0; /* not a unix shell */
|
---|
374 | i++;
|
---|
375 | }
|
---|
376 |
|
---|
377 | /* in doubt assume a unix like shell */
|
---|
378 | return 1;
|
---|
379 | }
|
---|
380 | #endif /* __EMX__ */
|
---|
381 |
|
---|
382 | |
---|
383 |
|
---|
384 | /* Write an error message describing the exit status given in
|
---|
385 | EXIT_CODE, EXIT_SIG, and COREDUMP, for the target TARGET_NAME.
|
---|
386 | Append "(ignored)" if IGNORED is nonzero. */
|
---|
387 |
|
---|
388 | static void
|
---|
389 | child_error (const char *target_name,
|
---|
390 | int exit_code, int exit_sig, int coredump, int ignored)
|
---|
391 | {
|
---|
392 | if (ignored && silent_flag)
|
---|
393 | return;
|
---|
394 |
|
---|
395 | #ifdef VMS
|
---|
396 | if (!(exit_code & 1))
|
---|
397 | error (NILF,
|
---|
398 | (ignored ? _("*** [%s] Error 0x%x (ignored)")
|
---|
399 | : _("*** [%s] Error 0x%x")),
|
---|
400 | target_name, exit_code);
|
---|
401 | #else
|
---|
402 | if (exit_sig == 0)
|
---|
403 | error (NILF, ignored ? _("[%s] Error %d (ignored)") :
|
---|
404 | _("*** [%s] Error %d"),
|
---|
405 | target_name, exit_code);
|
---|
406 | else
|
---|
407 | error (NILF, "*** [%s] %s%s",
|
---|
408 | target_name, strsignal (exit_sig),
|
---|
409 | coredump ? _(" (core dumped)") : "");
|
---|
410 | #endif /* VMS */
|
---|
411 | }
|
---|
412 | |
---|
413 |
|
---|
414 |
|
---|
415 | /* Handle a dead child. This handler may or may not ever be installed.
|
---|
416 |
|
---|
417 | If we're using the jobserver feature, we need it. First, installing it
|
---|
418 | ensures the read will interrupt on SIGCHLD. Second, we close the dup'd
|
---|
419 | read FD to ensure we don't enter another blocking read without reaping all
|
---|
420 | the dead children. In this case we don't need the dead_children count.
|
---|
421 |
|
---|
422 | If we don't have either waitpid or wait3, then make is unreliable, but we
|
---|
423 | use the dead_children count to reap children as best we can. */
|
---|
424 |
|
---|
425 | static unsigned int dead_children = 0;
|
---|
426 |
|
---|
427 | RETSIGTYPE
|
---|
428 | child_handler (int sig UNUSED)
|
---|
429 | {
|
---|
430 | ++dead_children;
|
---|
431 |
|
---|
432 | if (job_rfd >= 0)
|
---|
433 | {
|
---|
434 | close (job_rfd);
|
---|
435 | job_rfd = -1;
|
---|
436 | }
|
---|
437 |
|
---|
438 | #if defined __EMX__ && !defined(__INNOTEK_LIBC__) /* bird */
|
---|
439 | /* The signal handler must called only once! */
|
---|
440 | signal (SIGCHLD, SIG_DFL);
|
---|
441 | #endif
|
---|
442 |
|
---|
443 | /* This causes problems if the SIGCHLD interrupts a printf().
|
---|
444 | DB (DB_JOBS, (_("Got a SIGCHLD; %u unreaped children.\n"), dead_children));
|
---|
445 | */
|
---|
446 | }
|
---|
447 |
|
---|
448 | extern int shell_function_pid, shell_function_completed;
|
---|
449 |
|
---|
450 | /* Reap all dead children, storing the returned status and the new command
|
---|
451 | state (`cs_finished') in the `file' member of the `struct child' for the
|
---|
452 | dead child, and removing the child from the chain. In addition, if BLOCK
|
---|
453 | nonzero, we block in this function until we've reaped at least one
|
---|
454 | complete child, waiting for it to die if necessary. If ERR is nonzero,
|
---|
455 | print an error message first. */
|
---|
456 |
|
---|
457 | void
|
---|
458 | reap_children (int block, int err)
|
---|
459 | {
|
---|
460 | #ifndef WINDOWS32
|
---|
461 | WAIT_T status;
|
---|
462 | /* Initially, assume we have some. */
|
---|
463 | int reap_more = 1;
|
---|
464 | #endif
|
---|
465 |
|
---|
466 | #ifdef WAIT_NOHANG
|
---|
467 | # define REAP_MORE reap_more
|
---|
468 | #else
|
---|
469 | # define REAP_MORE dead_children
|
---|
470 | #endif
|
---|
471 |
|
---|
472 | /* As long as:
|
---|
473 |
|
---|
474 | We have at least one child outstanding OR a shell function in progress,
|
---|
475 | AND
|
---|
476 | We're blocking for a complete child OR there are more children to reap
|
---|
477 |
|
---|
478 | we'll keep reaping children. */
|
---|
479 |
|
---|
480 | while ((children != 0 || shell_function_pid != 0)
|
---|
481 | && (block || REAP_MORE))
|
---|
482 | {
|
---|
483 | int remote = 0;
|
---|
484 | pid_t pid;
|
---|
485 | int exit_code, exit_sig, coredump;
|
---|
486 | register struct child *lastc, *c;
|
---|
487 | int child_failed;
|
---|
488 | int any_remote, any_local;
|
---|
489 | int dontcare;
|
---|
490 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
---|
491 | struct child *completed_child = NULL;
|
---|
492 | #endif
|
---|
493 |
|
---|
494 | if (err && block)
|
---|
495 | {
|
---|
496 | static int printed = 0;
|
---|
497 |
|
---|
498 | /* We might block for a while, so let the user know why.
|
---|
499 | Only print this message once no matter how many jobs are left. */
|
---|
500 | fflush (stdout);
|
---|
501 | if (!printed)
|
---|
502 | error (NILF, _("*** Waiting for unfinished jobs...."));
|
---|
503 | printed = 1;
|
---|
504 | }
|
---|
505 |
|
---|
506 | /* We have one less dead child to reap. As noted in
|
---|
507 | child_handler() above, this count is completely unimportant for
|
---|
508 | all modern, POSIX-y systems that support wait3() or waitpid().
|
---|
509 | The rest of this comment below applies only to early, broken
|
---|
510 | pre-POSIX systems. We keep the count only because... it's there...
|
---|
511 |
|
---|
512 | The test and decrement are not atomic; if it is compiled into:
|
---|
513 | register = dead_children - 1;
|
---|
514 | dead_children = register;
|
---|
515 | a SIGCHLD could come between the two instructions.
|
---|
516 | child_handler increments dead_children.
|
---|
517 | The second instruction here would lose that increment. But the
|
---|
518 | only effect of dead_children being wrong is that we might wait
|
---|
519 | longer than necessary to reap a child, and lose some parallelism;
|
---|
520 | and we might print the "Waiting for unfinished jobs" message above
|
---|
521 | when not necessary. */
|
---|
522 |
|
---|
523 | if (dead_children > 0)
|
---|
524 | --dead_children;
|
---|
525 |
|
---|
526 | any_remote = 0;
|
---|
527 | any_local = shell_function_pid != 0;
|
---|
528 | for (c = children; c != 0; c = c->next)
|
---|
529 | {
|
---|
530 | any_remote |= c->remote;
|
---|
531 | any_local |= ! c->remote;
|
---|
532 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
---|
533 | if (c->has_status)
|
---|
534 | {
|
---|
535 | completed_child = c;
|
---|
536 | DB (DB_JOBS, (_("builtin child 0x%08lx (%s) PID %ld %s Status %ld\n"),
|
---|
537 | (unsigned long int) c, c->file->name,
|
---|
538 | (long) c->pid, c->remote ? _(" (remote)") : "",
|
---|
539 | (long) c->status));
|
---|
540 | }
|
---|
541 | else
|
---|
542 | #endif
|
---|
543 | DB (DB_JOBS, (_("Live child 0x%08lx (%s) PID %ld %s\n"),
|
---|
544 | (unsigned long int) c, c->file->name,
|
---|
545 | (long) c->pid, c->remote ? _(" (remote)") : ""));
|
---|
546 | #ifdef VMS
|
---|
547 | break;
|
---|
548 | #endif
|
---|
549 | }
|
---|
550 |
|
---|
551 | /* First, check for remote children. */
|
---|
552 | if (any_remote)
|
---|
553 | pid = remote_status (&exit_code, &exit_sig, &coredump, 0);
|
---|
554 | else
|
---|
555 | pid = 0;
|
---|
556 |
|
---|
557 | if (pid > 0)
|
---|
558 | /* We got a remote child. */
|
---|
559 | remote = 1;
|
---|
560 | else if (pid < 0)
|
---|
561 | {
|
---|
562 | /* A remote status command failed miserably. Punt. */
|
---|
563 | remote_status_lose:
|
---|
564 | pfatal_with_name ("remote_status");
|
---|
565 | }
|
---|
566 | else
|
---|
567 | {
|
---|
568 | /* No remote children. Check for local children. */
|
---|
569 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
---|
570 | if (completed_child)
|
---|
571 | {
|
---|
572 | pid = completed_child->pid;
|
---|
573 | # if defined(WINDOWS32)
|
---|
574 | exit_code = completed_child->status;
|
---|
575 | exit_sig = 0;
|
---|
576 | coredump = 0;
|
---|
577 | # else
|
---|
578 | status = (WAIT_T)completed_child->status;
|
---|
579 | # endif
|
---|
580 | }
|
---|
581 | else
|
---|
582 | #endif /* CONFIG_WITH_KMK_BUILTIN */
|
---|
583 | #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
|
---|
584 | if (any_local)
|
---|
585 | {
|
---|
586 | #ifdef VMS
|
---|
587 | vmsWaitForChildren (&status);
|
---|
588 | pid = c->pid;
|
---|
589 | #else
|
---|
590 | #ifdef WAIT_NOHANG
|
---|
591 | if (!block)
|
---|
592 | pid = WAIT_NOHANG (&status);
|
---|
593 | else
|
---|
594 | #endif
|
---|
595 | pid = wait (&status);
|
---|
596 | #endif /* !VMS */
|
---|
597 | }
|
---|
598 | else
|
---|
599 | pid = 0;
|
---|
600 |
|
---|
601 | if (pid < 0)
|
---|
602 | {
|
---|
603 | /* The wait*() failed miserably. Punt. */
|
---|
604 | pfatal_with_name ("wait");
|
---|
605 | }
|
---|
606 | else if (pid > 0)
|
---|
607 | {
|
---|
608 | /* We got a child exit; chop the status word up. */
|
---|
609 | exit_code = WEXITSTATUS (status);
|
---|
610 | exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
|
---|
611 | coredump = WCOREDUMP (status);
|
---|
612 |
|
---|
613 | /* If we have started jobs in this second, remove one. */
|
---|
614 | if (job_counter)
|
---|
615 | --job_counter;
|
---|
616 | }
|
---|
617 | else
|
---|
618 | {
|
---|
619 | /* No local children are dead. */
|
---|
620 | reap_more = 0;
|
---|
621 |
|
---|
622 | if (!block || !any_remote)
|
---|
623 | break;
|
---|
624 |
|
---|
625 | /* Now try a blocking wait for a remote child. */
|
---|
626 | pid = remote_status (&exit_code, &exit_sig, &coredump, 1);
|
---|
627 | if (pid < 0)
|
---|
628 | goto remote_status_lose;
|
---|
629 | else if (pid == 0)
|
---|
630 | /* No remote children either. Finally give up. */
|
---|
631 | break;
|
---|
632 |
|
---|
633 | /* We got a remote child. */
|
---|
634 | remote = 1;
|
---|
635 | }
|
---|
636 | #endif /* !__MSDOS__, !Amiga, !WINDOWS32. */
|
---|
637 |
|
---|
638 | #ifdef __MSDOS__
|
---|
639 | /* Life is very different on MSDOS. */
|
---|
640 | pid = dos_pid - 1;
|
---|
641 | status = dos_status;
|
---|
642 | exit_code = WEXITSTATUS (status);
|
---|
643 | if (exit_code == 0xff)
|
---|
644 | exit_code = -1;
|
---|
645 | exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0;
|
---|
646 | coredump = 0;
|
---|
647 | #endif /* __MSDOS__ */
|
---|
648 | #ifdef _AMIGA
|
---|
649 | /* Same on Amiga */
|
---|
650 | pid = amiga_pid - 1;
|
---|
651 | status = amiga_status;
|
---|
652 | exit_code = amiga_status;
|
---|
653 | exit_sig = 0;
|
---|
654 | coredump = 0;
|
---|
655 | #endif /* _AMIGA */
|
---|
656 | #ifdef WINDOWS32
|
---|
657 | {
|
---|
658 | HANDLE hPID;
|
---|
659 | int werr;
|
---|
660 | HANDLE hcTID, hcPID;
|
---|
661 | exit_code = 0;
|
---|
662 | exit_sig = 0;
|
---|
663 | coredump = 0;
|
---|
664 |
|
---|
665 | /* Record the thread ID of the main process, so that we
|
---|
666 | could suspend it in the signal handler. */
|
---|
667 | if (!main_thread)
|
---|
668 | {
|
---|
669 | hcTID = GetCurrentThread ();
|
---|
670 | hcPID = GetCurrentProcess ();
|
---|
671 | if (!DuplicateHandle (hcPID, hcTID, hcPID, &main_thread, 0,
|
---|
672 | FALSE, DUPLICATE_SAME_ACCESS))
|
---|
673 | {
|
---|
674 | DWORD e = GetLastError ();
|
---|
675 | fprintf (stderr,
|
---|
676 | "Determine main thread ID (Error %ld: %s)\n",
|
---|
677 | e, map_windows32_error_to_string(e));
|
---|
678 | }
|
---|
679 | else
|
---|
680 | DB (DB_VERBOSE, ("Main thread handle = 0x%08lx\n",
|
---|
681 | (unsigned long)main_thread));
|
---|
682 | }
|
---|
683 |
|
---|
684 | /* wait for anything to finish */
|
---|
685 | hPID = process_wait_for_any();
|
---|
686 | if (hPID)
|
---|
687 | {
|
---|
688 |
|
---|
689 | /* was an error found on this process? */
|
---|
690 | werr = process_last_err(hPID);
|
---|
691 |
|
---|
692 | /* get exit data */
|
---|
693 | exit_code = process_exit_code(hPID);
|
---|
694 |
|
---|
695 | if (werr)
|
---|
696 | fprintf(stderr, "make (e=%d): %s",
|
---|
697 | exit_code, map_windows32_error_to_string(exit_code));
|
---|
698 |
|
---|
699 | /* signal */
|
---|
700 | exit_sig = process_signal(hPID);
|
---|
701 |
|
---|
702 | /* cleanup process */
|
---|
703 | process_cleanup(hPID);
|
---|
704 |
|
---|
705 | coredump = 0;
|
---|
706 | }
|
---|
707 | else if (!process_used_slots())
|
---|
708 | {
|
---|
709 | /* The wait*() failed miserably. Punt. */
|
---|
710 | errno = ECHILD;
|
---|
711 | pfatal_with_name ("wait");
|
---|
712 | }
|
---|
713 |
|
---|
714 | pid = (pid_t) hPID;
|
---|
715 | }
|
---|
716 | #endif /* WINDOWS32 */
|
---|
717 | }
|
---|
718 |
|
---|
719 | /* Check if this is the child of the `shell' function. */
|
---|
720 | if (!remote && pid == shell_function_pid)
|
---|
721 | {
|
---|
722 | /* It is. Leave an indicator for the `shell' function. */
|
---|
723 | if (exit_sig == 0 && exit_code == 127)
|
---|
724 | shell_function_completed = -1;
|
---|
725 | else
|
---|
726 | shell_function_completed = 1;
|
---|
727 | break;
|
---|
728 | }
|
---|
729 |
|
---|
730 | child_failed = exit_sig != 0 || exit_code != 0;
|
---|
731 |
|
---|
732 | /* Search for a child matching the deceased one. */
|
---|
733 | lastc = 0;
|
---|
734 | for (c = children; c != 0; lastc = c, c = c->next)
|
---|
735 | if (c->remote == remote && c->pid == pid)
|
---|
736 | break;
|
---|
737 |
|
---|
738 | if (c == 0)
|
---|
739 | /* An unknown child died.
|
---|
740 | Ignore it; it was inherited from our invoker. */
|
---|
741 | continue;
|
---|
742 |
|
---|
743 | DB (DB_JOBS, (child_failed
|
---|
744 | ? _("Reaping losing child 0x%08lx PID %ld %s\n")
|
---|
745 | : _("Reaping winning child 0x%08lx PID %ld %s\n"),
|
---|
746 | (unsigned long int) c, (long) c->pid,
|
---|
747 | c->remote ? _(" (remote)") : ""));
|
---|
748 |
|
---|
749 | if (c->sh_batch_file) {
|
---|
750 | DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"),
|
---|
751 | c->sh_batch_file));
|
---|
752 |
|
---|
753 | /* just try and remove, don't care if this fails */
|
---|
754 | remove (c->sh_batch_file);
|
---|
755 |
|
---|
756 | /* all done with memory */
|
---|
757 | free (c->sh_batch_file);
|
---|
758 | c->sh_batch_file = NULL;
|
---|
759 | }
|
---|
760 |
|
---|
761 | /* If this child had the good stdin, say it is now free. */
|
---|
762 | if (c->good_stdin)
|
---|
763 | good_stdin_used = 0;
|
---|
764 |
|
---|
765 | dontcare = c->dontcare;
|
---|
766 |
|
---|
767 | if (child_failed && !c->noerror && !ignore_errors_flag)
|
---|
768 | {
|
---|
769 | /* The commands failed. Write an error message,
|
---|
770 | delete non-precious targets, and abort. */
|
---|
771 | static int delete_on_error = -1;
|
---|
772 |
|
---|
773 | if (!dontcare)
|
---|
774 | #ifdef KMK
|
---|
775 | {
|
---|
776 | child_error (c->file->name, exit_code, exit_sig, coredump, 0);
|
---|
777 | if (( c->file->cmds->lines_flags[c->command_line - 1]
|
---|
778 | & (COMMANDS_SILENT | COMMANDS_RECURSE))
|
---|
779 | == COMMANDS_SILENT)
|
---|
780 | message (0, "The failing command:\n%s", c->file->cmds->command_lines[c->command_line - 1]);
|
---|
781 | }
|
---|
782 | #else /* !KMK */
|
---|
783 | child_error (c->file->name, exit_code, exit_sig, coredump, 0);
|
---|
784 | #endif /* !KMK */
|
---|
785 |
|
---|
786 | c->file->update_status = 2;
|
---|
787 | if (delete_on_error == -1)
|
---|
788 | {
|
---|
789 | struct file *f = lookup_file (".DELETE_ON_ERROR");
|
---|
790 | delete_on_error = f != 0 && f->is_target;
|
---|
791 | }
|
---|
792 | if (exit_sig != 0 || delete_on_error)
|
---|
793 | delete_child_targets (c);
|
---|
794 | }
|
---|
795 | else
|
---|
796 | {
|
---|
797 | if (child_failed)
|
---|
798 | {
|
---|
799 | /* The commands failed, but we don't care. */
|
---|
800 | child_error (c->file->name,
|
---|
801 | exit_code, exit_sig, coredump, 1);
|
---|
802 | child_failed = 0;
|
---|
803 | }
|
---|
804 |
|
---|
805 | /* If there are more commands to run, try to start them. */
|
---|
806 | if (job_next_command (c))
|
---|
807 | {
|
---|
808 | if (handling_fatal_signal)
|
---|
809 | {
|
---|
810 | /* Never start new commands while we are dying.
|
---|
811 | Since there are more commands that wanted to be run,
|
---|
812 | the target was not completely remade. So we treat
|
---|
813 | this as if a command had failed. */
|
---|
814 | c->file->update_status = 2;
|
---|
815 | }
|
---|
816 | else
|
---|
817 | {
|
---|
818 | /* Check again whether to start remotely.
|
---|
819 | Whether or not we want to changes over time.
|
---|
820 | Also, start_remote_job may need state set up
|
---|
821 | by start_remote_job_p. */
|
---|
822 | c->remote = start_remote_job_p (0);
|
---|
823 | start_job_command (c);
|
---|
824 | /* Fatal signals are left blocked in case we were
|
---|
825 | about to put that child on the chain. But it is
|
---|
826 | already there, so it is safe for a fatal signal to
|
---|
827 | arrive now; it will clean up this child's targets. */
|
---|
828 | unblock_sigs ();
|
---|
829 | if (c->file->command_state == cs_running)
|
---|
830 | /* We successfully started the new command.
|
---|
831 | Loop to reap more children. */
|
---|
832 | continue;
|
---|
833 | }
|
---|
834 |
|
---|
835 | if (c->file->update_status != 0)
|
---|
836 | /* We failed to start the commands. */
|
---|
837 | delete_child_targets (c);
|
---|
838 | }
|
---|
839 | else
|
---|
840 | /* There are no more commands. We got through them all
|
---|
841 | without an unignored error. Now the target has been
|
---|
842 | successfully updated. */
|
---|
843 | c->file->update_status = 0;
|
---|
844 | }
|
---|
845 |
|
---|
846 | /* When we get here, all the commands for C->file are finished
|
---|
847 | (or aborted) and C->file->update_status contains 0 or 2. But
|
---|
848 | C->file->command_state is still cs_running if all the commands
|
---|
849 | ran; notice_finish_file looks for cs_running to tell it that
|
---|
850 | it's interesting to check the file's modtime again now. */
|
---|
851 |
|
---|
852 | if (! handling_fatal_signal)
|
---|
853 | /* Notice if the target of the commands has been changed.
|
---|
854 | This also propagates its values for command_state and
|
---|
855 | update_status to its also_make files. */
|
---|
856 | notice_finished_file (c->file);
|
---|
857 |
|
---|
858 | DB (DB_JOBS, (_("Removing child 0x%08lx PID %ld%s from chain.\n"),
|
---|
859 | (unsigned long int) c, (long) c->pid,
|
---|
860 | c->remote ? _(" (remote)") : ""));
|
---|
861 |
|
---|
862 | /* Block fatal signals while frobnicating the list, so that
|
---|
863 | children and job_slots_used are always consistent. Otherwise
|
---|
864 | a fatal signal arriving after the child is off the chain and
|
---|
865 | before job_slots_used is decremented would believe a child was
|
---|
866 | live and call reap_children again. */
|
---|
867 | block_sigs ();
|
---|
868 |
|
---|
869 | /* There is now another slot open. */
|
---|
870 | if (job_slots_used > 0)
|
---|
871 | --job_slots_used;
|
---|
872 |
|
---|
873 | /* Remove the child from the chain and free it. */
|
---|
874 | if (lastc == 0)
|
---|
875 | children = c->next;
|
---|
876 | else
|
---|
877 | lastc->next = c->next;
|
---|
878 |
|
---|
879 | free_child (c);
|
---|
880 |
|
---|
881 | unblock_sigs ();
|
---|
882 |
|
---|
883 | /* If the job failed, and the -k flag was not given, die,
|
---|
884 | unless we are already in the process of dying. */
|
---|
885 | if (!err && child_failed && !dontcare && !keep_going_flag &&
|
---|
886 | /* fatal_error_signal will die with the right signal. */
|
---|
887 | !handling_fatal_signal)
|
---|
888 | die (2);
|
---|
889 |
|
---|
890 | /* Only block for one child. */
|
---|
891 | block = 0;
|
---|
892 | }
|
---|
893 |
|
---|
894 | return;
|
---|
895 | }
|
---|
896 | |
---|
897 |
|
---|
898 | /* Free the storage allocated for CHILD. */
|
---|
899 |
|
---|
900 | static void
|
---|
901 | free_child (struct child *child)
|
---|
902 | {
|
---|
903 | if (!jobserver_tokens)
|
---|
904 | fatal (NILF, "INTERNAL: Freeing child 0x%08lx (%s) but no tokens left!\n",
|
---|
905 | (unsigned long int) child, child->file->name);
|
---|
906 |
|
---|
907 | /* If we're using the jobserver and this child is not the only outstanding
|
---|
908 | job, put a token back into the pipe for it. */
|
---|
909 |
|
---|
910 | if (job_fds[1] >= 0 && jobserver_tokens > 1)
|
---|
911 | {
|
---|
912 | char token = '+';
|
---|
913 | int r;
|
---|
914 |
|
---|
915 | /* Write a job token back to the pipe. */
|
---|
916 |
|
---|
917 | EINTRLOOP (r, write (job_fds[1], &token, 1));
|
---|
918 | if (r != 1)
|
---|
919 | pfatal_with_name (_("write jobserver"));
|
---|
920 |
|
---|
921 | DB (DB_JOBS, (_("Released token for child 0x%08lx (%s).\n"),
|
---|
922 | (unsigned long int) child, child->file->name));
|
---|
923 | }
|
---|
924 |
|
---|
925 | --jobserver_tokens;
|
---|
926 |
|
---|
927 | if (handling_fatal_signal) /* Don't bother free'ing if about to die. */
|
---|
928 | return;
|
---|
929 |
|
---|
930 | if (child->command_lines != 0)
|
---|
931 | {
|
---|
932 | register unsigned int i;
|
---|
933 | for (i = 0; i < child->file->cmds->ncommand_lines; ++i)
|
---|
934 | free (child->command_lines[i]);
|
---|
935 | free (child->command_lines);
|
---|
936 | }
|
---|
937 |
|
---|
938 | if (child->environment != 0)
|
---|
939 | {
|
---|
940 | register char **ep = child->environment;
|
---|
941 | while (*ep != 0)
|
---|
942 | free (*ep++);
|
---|
943 | free (child->environment);
|
---|
944 | }
|
---|
945 |
|
---|
946 | #ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
|
---|
947 | /* Free the chopped command lines for simple targets when
|
---|
948 | there are no more active references to them. */
|
---|
949 |
|
---|
950 | child->file->cmds->refs--;
|
---|
951 | if ( !child->file->intermediate
|
---|
952 | && !child->file->pat_variables
|
---|
953 | && child->file->cmds->refs == 0)
|
---|
954 | {
|
---|
955 | struct commands *cmds = child->file->cmds;
|
---|
956 | unsigned int i;
|
---|
957 |
|
---|
958 | for (i = 0; i < cmds->ncommand_lines; ++i)
|
---|
959 | {
|
---|
960 | free (cmds->command_lines[i]);
|
---|
961 | cmds->command_lines[i] = 0;
|
---|
962 | }
|
---|
963 | free (cmds->command_lines);
|
---|
964 | cmds->command_lines = 0;
|
---|
965 | free (cmds->lines_flags);
|
---|
966 | cmds->lines_flags = 0;
|
---|
967 | cmds->ncommand_lines = 0;
|
---|
968 | }
|
---|
969 | #endif /* CONFIG_WITH_MEMORY_OPTIMIZATIONS */
|
---|
970 |
|
---|
971 | free (child);
|
---|
972 | }
|
---|
973 | |
---|
974 |
|
---|
975 | #ifdef POSIX
|
---|
976 | extern sigset_t fatal_signal_set;
|
---|
977 | #endif
|
---|
978 |
|
---|
979 | void
|
---|
980 | block_sigs (void)
|
---|
981 | {
|
---|
982 | #ifdef POSIX
|
---|
983 | (void) sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0);
|
---|
984 | #else
|
---|
985 | # ifdef HAVE_SIGSETMASK
|
---|
986 | (void) sigblock (fatal_signal_mask);
|
---|
987 | # endif
|
---|
988 | #endif
|
---|
989 | }
|
---|
990 |
|
---|
991 | #ifdef POSIX
|
---|
992 | void
|
---|
993 | unblock_sigs (void)
|
---|
994 | {
|
---|
995 | sigset_t empty;
|
---|
996 | sigemptyset (&empty);
|
---|
997 | sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0);
|
---|
998 | }
|
---|
999 | #endif
|
---|
1000 |
|
---|
1001 | #ifdef MAKE_JOBSERVER
|
---|
1002 | RETSIGTYPE
|
---|
1003 | job_noop (int sig UNUSED)
|
---|
1004 | {
|
---|
1005 | }
|
---|
1006 | /* Set the child handler action flags to FLAGS. */
|
---|
1007 | static void
|
---|
1008 | set_child_handler_action_flags (int set_handler, int set_alarm)
|
---|
1009 | {
|
---|
1010 | struct sigaction sa;
|
---|
1011 |
|
---|
1012 | #if defined(__EMX__) && !defined(__KLIBC__) /* bird */
|
---|
1013 | /* The child handler must be turned off here. */
|
---|
1014 | signal (SIGCHLD, SIG_DFL);
|
---|
1015 | #endif
|
---|
1016 |
|
---|
1017 | memset (&sa, '\0', sizeof sa);
|
---|
1018 | sa.sa_handler = child_handler;
|
---|
1019 | sa.sa_flags = set_handler ? 0 : SA_RESTART;
|
---|
1020 | #if defined SIGCHLD
|
---|
1021 | sigaction (SIGCHLD, &sa, NULL);
|
---|
1022 | #endif
|
---|
1023 | #if defined SIGCLD && SIGCLD != SIGCHLD
|
---|
1024 | sigaction (SIGCLD, &sa, NULL);
|
---|
1025 | #endif
|
---|
1026 | #if defined SIGALRM
|
---|
1027 | if (set_alarm)
|
---|
1028 | {
|
---|
1029 | /* If we're about to enter the read(), set an alarm to wake up in a
|
---|
1030 | second so we can check if the load has dropped and we can start more
|
---|
1031 | work. On the way out, turn off the alarm and set SIG_DFL. */
|
---|
1032 | alarm (set_handler ? 1 : 0);
|
---|
1033 | sa.sa_handler = set_handler ? job_noop : SIG_DFL;
|
---|
1034 | sa.sa_flags = 0;
|
---|
1035 | sigaction (SIGALRM, &sa, NULL);
|
---|
1036 | }
|
---|
1037 | #endif
|
---|
1038 | }
|
---|
1039 | #endif
|
---|
1040 |
|
---|
1041 |
|
---|
1042 | /* Start a job to run the commands specified in CHILD.
|
---|
1043 | CHILD is updated to reflect the commands and ID of the child process.
|
---|
1044 |
|
---|
1045 | NOTE: On return fatal signals are blocked! The caller is responsible
|
---|
1046 | for calling `unblock_sigs', once the new child is safely on the chain so
|
---|
1047 | it can be cleaned up in the event of a fatal signal. */
|
---|
1048 |
|
---|
1049 | static void
|
---|
1050 | start_job_command (struct child *child)
|
---|
1051 | {
|
---|
1052 | #if !defined(_AMIGA) && !defined(WINDOWS32)
|
---|
1053 | static int bad_stdin = -1;
|
---|
1054 | #endif
|
---|
1055 | register char *p;
|
---|
1056 | int flags;
|
---|
1057 | #ifdef VMS
|
---|
1058 | char *argv;
|
---|
1059 | #else
|
---|
1060 | char **argv;
|
---|
1061 | #endif
|
---|
1062 |
|
---|
1063 | /* If we have a completely empty commandset, stop now. */
|
---|
1064 | if (!child->command_ptr)
|
---|
1065 | goto next_command;
|
---|
1066 |
|
---|
1067 | /* Combine the flags parsed for the line itself with
|
---|
1068 | the flags specified globally for this target. */
|
---|
1069 | flags = (child->file->command_flags
|
---|
1070 | | child->file->cmds->lines_flags[child->command_line - 1]);
|
---|
1071 |
|
---|
1072 | p = child->command_ptr;
|
---|
1073 | child->noerror = ((flags & COMMANDS_NOERROR) != 0);
|
---|
1074 |
|
---|
1075 | while (*p != '\0')
|
---|
1076 | {
|
---|
1077 | if (*p == '@')
|
---|
1078 | flags |= COMMANDS_SILENT;
|
---|
1079 | else if (*p == '+')
|
---|
1080 | flags |= COMMANDS_RECURSE;
|
---|
1081 | else if (*p == '-')
|
---|
1082 | child->noerror = 1;
|
---|
1083 | #ifdef CONFIG_WITH_COMMANDS_FUNC
|
---|
1084 | else if (*p == '%')
|
---|
1085 | flags |= COMMAND_GETTER_SKIP_IT;
|
---|
1086 | #endif
|
---|
1087 | else if (!isblank ((unsigned char)*p))
|
---|
1088 | #ifndef CONFIG_WITH_KMK_BUILTIN
|
---|
1089 | break;
|
---|
1090 | #else /* CONFIG_WITH_KMK_BUILTIN */
|
---|
1091 |
|
---|
1092 | {
|
---|
1093 | if ( !(flags & COMMANDS_KMK_BUILTIN)
|
---|
1094 | && !strncmp(p, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
|
---|
1095 | flags |= COMMANDS_KMK_BUILTIN;
|
---|
1096 | break;
|
---|
1097 | }
|
---|
1098 | #endif /* CONFIG_WITH_KMK_BUILTIN */
|
---|
1099 | ++p;
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | /* Update the file's command flags with any new ones we found. We only
|
---|
1103 | keep the COMMANDS_RECURSE setting. Even this isn't 100% correct; we are
|
---|
1104 | now marking more commands recursive than should be in the case of
|
---|
1105 | multiline define/endef scripts where only one line is marked "+". In
|
---|
1106 | order to really fix this, we'll have to keep a lines_flags for every
|
---|
1107 | actual line, after expansion. */
|
---|
1108 | child->file->cmds->lines_flags[child->command_line - 1]
|
---|
1109 | |= flags & COMMANDS_RECURSE;
|
---|
1110 |
|
---|
1111 | /* Figure out an argument list from this command line. */
|
---|
1112 |
|
---|
1113 | {
|
---|
1114 | char *end = 0;
|
---|
1115 | #ifdef VMS
|
---|
1116 | argv = p;
|
---|
1117 | #else
|
---|
1118 | argv = construct_command_argv (p, &end, child->file,
|
---|
1119 | child->file->cmds->lines_flags[child->command_line - 1],
|
---|
1120 | &child->sh_batch_file);
|
---|
1121 | #endif
|
---|
1122 | if (end == NULL)
|
---|
1123 | child->command_ptr = NULL;
|
---|
1124 | else
|
---|
1125 | {
|
---|
1126 | *end++ = '\0';
|
---|
1127 | child->command_ptr = end;
|
---|
1128 | }
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | /* If -q was given, say that updating `failed' if there was any text on the
|
---|
1132 | command line, or `succeeded' otherwise. The exit status of 1 tells the
|
---|
1133 | user that -q is saying `something to do'; the exit status for a random
|
---|
1134 | error is 2. */
|
---|
1135 | if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE))
|
---|
1136 | {
|
---|
1137 | #ifndef VMS
|
---|
1138 | free (argv[0]);
|
---|
1139 | free (argv);
|
---|
1140 | #endif
|
---|
1141 | child->file->update_status = 1;
|
---|
1142 | notice_finished_file (child->file);
|
---|
1143 | return;
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | if (touch_flag && !(flags & COMMANDS_RECURSE))
|
---|
1147 | {
|
---|
1148 | /* Go on to the next command. It might be the recursive one.
|
---|
1149 | We construct ARGV only to find the end of the command line. */
|
---|
1150 | #ifndef VMS
|
---|
1151 | if (argv)
|
---|
1152 | {
|
---|
1153 | free (argv[0]);
|
---|
1154 | free (argv);
|
---|
1155 | }
|
---|
1156 | #endif
|
---|
1157 | argv = 0;
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | if (argv == 0)
|
---|
1161 | {
|
---|
1162 | next_command:
|
---|
1163 | #ifdef __MSDOS__
|
---|
1164 | execute_by_shell = 0; /* in case construct_command_argv sets it */
|
---|
1165 | #endif
|
---|
1166 | /* This line has no commands. Go to the next. */
|
---|
1167 | if (job_next_command (child))
|
---|
1168 | start_job_command (child);
|
---|
1169 | else
|
---|
1170 | {
|
---|
1171 | /* No more commands. Make sure we're "running"; we might not be if
|
---|
1172 | (e.g.) all commands were skipped due to -n. */
|
---|
1173 | set_command_state (child->file, cs_running);
|
---|
1174 | child->file->update_status = 0;
|
---|
1175 | notice_finished_file (child->file);
|
---|
1176 | }
|
---|
1177 | return;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | /* Print out the command. If silent, we call `message' with null so it
|
---|
1181 | can log the working directory before the command's own error messages
|
---|
1182 | appear. */
|
---|
1183 | #ifdef CONFIG_PRETTY_COMMAND_PRINTING
|
---|
1184 | if ( pretty_command_printing
|
---|
1185 | && (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag))
|
---|
1186 | && argv[0][0] != '\0')
|
---|
1187 | {
|
---|
1188 | unsigned i;
|
---|
1189 | for (i = 0; argv[i]; i++)
|
---|
1190 | message (0, "%s'%s'%s", i ? "\t" : "> ", argv[i], argv[i + 1] ? " \\" : "");
|
---|
1191 | }
|
---|
1192 | else
|
---|
1193 | #endif /* CONFIG_PRETTY_COMMAND_PRINTING */
|
---|
1194 | message (0, (just_print_flag || (!(flags & COMMANDS_SILENT) && !silent_flag))
|
---|
1195 | ? "%s" : (char *) 0, p);
|
---|
1196 |
|
---|
1197 | /* Tell update_goal_chain that a command has been started on behalf of
|
---|
1198 | this target. It is important that this happens here and not in
|
---|
1199 | reap_children (where we used to do it), because reap_children might be
|
---|
1200 | reaping children from a different target. We want this increment to
|
---|
1201 | guaranteedly indicate that a command was started for the dependency
|
---|
1202 | chain (i.e., update_file recursion chain) we are processing. */
|
---|
1203 |
|
---|
1204 | ++commands_started;
|
---|
1205 |
|
---|
1206 | /* Optimize an empty command. People use this for timestamp rules,
|
---|
1207 | so avoid forking a useless shell. Do this after we increment
|
---|
1208 | commands_started so make still treats this special case as if it
|
---|
1209 | performed some action (makes a difference as to what messages are
|
---|
1210 | printed, etc. */
|
---|
1211 |
|
---|
1212 | #if !defined(VMS) && !defined(_AMIGA)
|
---|
1213 | if (
|
---|
1214 | #if defined __MSDOS__ || defined (__EMX__)
|
---|
1215 | unixy_shell /* the test is complicated and we already did it */
|
---|
1216 | #else
|
---|
1217 | (argv[0] && !strcmp (argv[0], "/bin/sh"))
|
---|
1218 | #endif
|
---|
1219 | && (argv[1]
|
---|
1220 | && argv[1][0] == '-' && argv[1][1] == 'c' && argv[1][2] == '\0')
|
---|
1221 | && (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0')
|
---|
1222 | && argv[3] == NULL)
|
---|
1223 | {
|
---|
1224 | free (argv[0]);
|
---|
1225 | free (argv);
|
---|
1226 | goto next_command;
|
---|
1227 | }
|
---|
1228 | #endif /* !VMS && !_AMIGA */
|
---|
1229 |
|
---|
1230 | /* If -n was given, recurse to get the next line in the sequence. */
|
---|
1231 |
|
---|
1232 | if (just_print_flag && !(flags & COMMANDS_RECURSE))
|
---|
1233 | {
|
---|
1234 | #ifndef VMS
|
---|
1235 | free (argv[0]);
|
---|
1236 | free (argv);
|
---|
1237 | #endif
|
---|
1238 | goto next_command;
|
---|
1239 | }
|
---|
1240 |
|
---|
1241 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
---|
1242 | /* If builtin command then pass it on to the builtin shell interpreter. */
|
---|
1243 |
|
---|
1244 | if ((flags & COMMANDS_KMK_BUILTIN) && !just_print_flag)
|
---|
1245 | {
|
---|
1246 | int rc;
|
---|
1247 | char **argv_spawn = NULL;
|
---|
1248 | char **p2 = argv;
|
---|
1249 | while (*p2 && strncmp (*p2, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
|
---|
1250 | p2++;
|
---|
1251 | assert (*p2);
|
---|
1252 | set_command_state (child->file, cs_running);
|
---|
1253 | child->pid = 0;
|
---|
1254 | if (p2 != argv)
|
---|
1255 | rc = kmk_builtin_command (*p2, &argv_spawn, &child->pid);
|
---|
1256 | else
|
---|
1257 | {
|
---|
1258 | int argc = 1;
|
---|
1259 | while (argv[argc])
|
---|
1260 | argc++;
|
---|
1261 | rc = kmk_builtin_command_parsed (argc, argv, &argv_spawn, &child->pid);
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 | # ifndef VMS
|
---|
1265 | free (argv[0]);
|
---|
1266 | free ((char *) argv);
|
---|
1267 | # endif
|
---|
1268 |
|
---|
1269 | /* synchronous command execution? */
|
---|
1270 | if (!rc && !argv_spawn)
|
---|
1271 | goto next_command;
|
---|
1272 |
|
---|
1273 | /* spawned a child? */
|
---|
1274 | if (!rc && child->pid)
|
---|
1275 | {
|
---|
1276 | ++job_counter;
|
---|
1277 | return;
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 | /* failure? */
|
---|
1281 | if (rc)
|
---|
1282 | {
|
---|
1283 | child->pid = (pid_t)42424242;
|
---|
1284 | child->status = rc << 8;
|
---|
1285 | child->has_status = 1;
|
---|
1286 | unblock_sigs();
|
---|
1287 | return;
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | /* conditional check == true; kicking off a child (not kmk_builtin_*). */
|
---|
1291 | argv = argv_spawn;
|
---|
1292 | }
|
---|
1293 | #endif /* CONFIG_WITH_KMK_BUILTIN */
|
---|
1294 |
|
---|
1295 | /* Flush the output streams so they won't have things written twice. */
|
---|
1296 |
|
---|
1297 | fflush (stdout);
|
---|
1298 | fflush (stderr);
|
---|
1299 |
|
---|
1300 | #ifndef VMS
|
---|
1301 | #if !defined(WINDOWS32) && !defined(_AMIGA) && !defined(__MSDOS__)
|
---|
1302 |
|
---|
1303 | /* Set up a bad standard input that reads from a broken pipe. */
|
---|
1304 |
|
---|
1305 | if (bad_stdin == -1)
|
---|
1306 | {
|
---|
1307 | /* Make a file descriptor that is the read end of a broken pipe.
|
---|
1308 | This will be used for some children's standard inputs. */
|
---|
1309 | int pd[2];
|
---|
1310 | if (pipe (pd) == 0)
|
---|
1311 | {
|
---|
1312 | /* Close the write side. */
|
---|
1313 | (void) close (pd[1]);
|
---|
1314 | /* Save the read side. */
|
---|
1315 | bad_stdin = pd[0];
|
---|
1316 |
|
---|
1317 | /* Set the descriptor to close on exec, so it does not litter any
|
---|
1318 | child's descriptor table. When it is dup2'd onto descriptor 0,
|
---|
1319 | that descriptor will not close on exec. */
|
---|
1320 | CLOSE_ON_EXEC (bad_stdin);
|
---|
1321 | }
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | #endif /* !WINDOWS32 && !_AMIGA && !__MSDOS__ */
|
---|
1325 |
|
---|
1326 | /* Decide whether to give this child the `good' standard input
|
---|
1327 | (one that points to the terminal or whatever), or the `bad' one
|
---|
1328 | that points to the read side of a broken pipe. */
|
---|
1329 |
|
---|
1330 | child->good_stdin = !good_stdin_used;
|
---|
1331 | if (child->good_stdin)
|
---|
1332 | good_stdin_used = 1;
|
---|
1333 |
|
---|
1334 | #endif /* !VMS */
|
---|
1335 |
|
---|
1336 | child->deleted = 0;
|
---|
1337 |
|
---|
1338 | #ifndef _AMIGA
|
---|
1339 | /* Set up the environment for the child. */
|
---|
1340 | if (child->environment == 0)
|
---|
1341 | child->environment = target_environment (child->file);
|
---|
1342 | #endif
|
---|
1343 |
|
---|
1344 | #if !defined(__MSDOS__) && !defined(_AMIGA) && !defined(WINDOWS32)
|
---|
1345 |
|
---|
1346 | #ifndef VMS
|
---|
1347 | /* start_waiting_job has set CHILD->remote if we can start a remote job. */
|
---|
1348 | if (child->remote)
|
---|
1349 | {
|
---|
1350 | int is_remote, id, used_stdin;
|
---|
1351 | if (start_remote_job (argv, child->environment,
|
---|
1352 | child->good_stdin ? 0 : bad_stdin,
|
---|
1353 | &is_remote, &id, &used_stdin))
|
---|
1354 | /* Don't give up; remote execution may fail for various reasons. If
|
---|
1355 | so, simply run the job locally. */
|
---|
1356 | goto run_local;
|
---|
1357 | else
|
---|
1358 | {
|
---|
1359 | if (child->good_stdin && !used_stdin)
|
---|
1360 | {
|
---|
1361 | child->good_stdin = 0;
|
---|
1362 | good_stdin_used = 0;
|
---|
1363 | }
|
---|
1364 | child->remote = is_remote;
|
---|
1365 | child->pid = id;
|
---|
1366 | }
|
---|
1367 | }
|
---|
1368 | else
|
---|
1369 | #endif /* !VMS */
|
---|
1370 | {
|
---|
1371 | /* Fork the child process. */
|
---|
1372 |
|
---|
1373 | char **parent_environ;
|
---|
1374 |
|
---|
1375 | run_local:
|
---|
1376 | block_sigs ();
|
---|
1377 |
|
---|
1378 | child->remote = 0;
|
---|
1379 |
|
---|
1380 | #ifdef VMS
|
---|
1381 | if (!child_execute_job (argv, child)) {
|
---|
1382 | /* Fork failed! */
|
---|
1383 | perror_with_name ("vfork", "");
|
---|
1384 | goto error;
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | #else
|
---|
1388 |
|
---|
1389 | parent_environ = environ;
|
---|
1390 |
|
---|
1391 | # ifdef __EMX__
|
---|
1392 | /* If we aren't running a recursive command and we have a jobserver
|
---|
1393 | pipe, close it before exec'ing. */
|
---|
1394 | if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
|
---|
1395 | {
|
---|
1396 | CLOSE_ON_EXEC (job_fds[0]);
|
---|
1397 | CLOSE_ON_EXEC (job_fds[1]);
|
---|
1398 | }
|
---|
1399 | if (job_rfd >= 0)
|
---|
1400 | CLOSE_ON_EXEC (job_rfd);
|
---|
1401 |
|
---|
1402 | /* Never use fork()/exec() here! Use spawn() instead in exec_command() */
|
---|
1403 | child->pid = child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
|
---|
1404 | argv, child->environment);
|
---|
1405 | if (child->pid < 0)
|
---|
1406 | {
|
---|
1407 | /* spawn failed! */
|
---|
1408 | unblock_sigs ();
|
---|
1409 | perror_with_name ("spawn", "");
|
---|
1410 | goto error;
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | /* undo CLOSE_ON_EXEC() after the child process has been started */
|
---|
1414 | if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
|
---|
1415 | {
|
---|
1416 | fcntl (job_fds[0], F_SETFD, 0);
|
---|
1417 | fcntl (job_fds[1], F_SETFD, 0);
|
---|
1418 | }
|
---|
1419 | if (job_rfd >= 0)
|
---|
1420 | fcntl (job_rfd, F_SETFD, 0);
|
---|
1421 |
|
---|
1422 | #else /* !__EMX__ */
|
---|
1423 |
|
---|
1424 | child->pid = vfork ();
|
---|
1425 | environ = parent_environ; /* Restore value child may have clobbered. */
|
---|
1426 | if (child->pid == 0)
|
---|
1427 | {
|
---|
1428 | /* We are the child side. */
|
---|
1429 | unblock_sigs ();
|
---|
1430 |
|
---|
1431 | /* If we aren't running a recursive command and we have a jobserver
|
---|
1432 | pipe, close it before exec'ing. */
|
---|
1433 | if (!(flags & COMMANDS_RECURSE) && job_fds[0] >= 0)
|
---|
1434 | {
|
---|
1435 | close (job_fds[0]);
|
---|
1436 | close (job_fds[1]);
|
---|
1437 | }
|
---|
1438 | if (job_rfd >= 0)
|
---|
1439 | close (job_rfd);
|
---|
1440 |
|
---|
1441 | child_execute_job (child->good_stdin ? 0 : bad_stdin, 1,
|
---|
1442 | argv, child->environment);
|
---|
1443 | }
|
---|
1444 | else if (child->pid < 0)
|
---|
1445 | {
|
---|
1446 | /* Fork failed! */
|
---|
1447 | unblock_sigs ();
|
---|
1448 | perror_with_name ("vfork", "");
|
---|
1449 | goto error;
|
---|
1450 | }
|
---|
1451 | # endif /* !__EMX__ */
|
---|
1452 | #endif /* !VMS */
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | #else /* __MSDOS__ or Amiga or WINDOWS32 */
|
---|
1456 | #ifdef __MSDOS__
|
---|
1457 | {
|
---|
1458 | int proc_return;
|
---|
1459 |
|
---|
1460 | block_sigs ();
|
---|
1461 | dos_status = 0;
|
---|
1462 |
|
---|
1463 | /* We call `system' to do the job of the SHELL, since stock DOS
|
---|
1464 | shell is too dumb. Our `system' knows how to handle long
|
---|
1465 | command lines even if pipes/redirection is needed; it will only
|
---|
1466 | call COMMAND.COM when its internal commands are used. */
|
---|
1467 | if (execute_by_shell)
|
---|
1468 | {
|
---|
1469 | char *cmdline = argv[0];
|
---|
1470 | /* We don't have a way to pass environment to `system',
|
---|
1471 | so we need to save and restore ours, sigh... */
|
---|
1472 | char **parent_environ = environ;
|
---|
1473 |
|
---|
1474 | environ = child->environment;
|
---|
1475 |
|
---|
1476 | /* If we have a *real* shell, tell `system' to call
|
---|
1477 | it to do everything for us. */
|
---|
1478 | if (unixy_shell)
|
---|
1479 | {
|
---|
1480 | /* A *real* shell on MSDOS may not support long
|
---|
1481 | command lines the DJGPP way, so we must use `system'. */
|
---|
1482 | cmdline = argv[2]; /* get past "shell -c" */
|
---|
1483 | }
|
---|
1484 |
|
---|
1485 | dos_command_running = 1;
|
---|
1486 | proc_return = system (cmdline);
|
---|
1487 | environ = parent_environ;
|
---|
1488 | execute_by_shell = 0; /* for the next time */
|
---|
1489 | }
|
---|
1490 | else
|
---|
1491 | {
|
---|
1492 | dos_command_running = 1;
|
---|
1493 | proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment);
|
---|
1494 | }
|
---|
1495 |
|
---|
1496 | /* Need to unblock signals before turning off
|
---|
1497 | dos_command_running, so that child's signals
|
---|
1498 | will be treated as such (see fatal_error_signal). */
|
---|
1499 | unblock_sigs ();
|
---|
1500 | dos_command_running = 0;
|
---|
1501 |
|
---|
1502 | /* If the child got a signal, dos_status has its
|
---|
1503 | high 8 bits set, so be careful not to alter them. */
|
---|
1504 | if (proc_return == -1)
|
---|
1505 | dos_status |= 0xff;
|
---|
1506 | else
|
---|
1507 | dos_status |= (proc_return & 0xff);
|
---|
1508 | ++dead_children;
|
---|
1509 | child->pid = dos_pid++;
|
---|
1510 | }
|
---|
1511 | #endif /* __MSDOS__ */
|
---|
1512 | #ifdef _AMIGA
|
---|
1513 | amiga_status = MyExecute (argv);
|
---|
1514 |
|
---|
1515 | ++dead_children;
|
---|
1516 | child->pid = amiga_pid++;
|
---|
1517 | if (amiga_batch_file)
|
---|
1518 | {
|
---|
1519 | amiga_batch_file = 0;
|
---|
1520 | DeleteFile (amiga_bname); /* Ignore errors. */
|
---|
1521 | }
|
---|
1522 | #endif /* Amiga */
|
---|
1523 | #ifdef WINDOWS32
|
---|
1524 | {
|
---|
1525 | HANDLE hPID;
|
---|
1526 | char* arg0;
|
---|
1527 |
|
---|
1528 | /* make UNC paths safe for CreateProcess -- backslash format */
|
---|
1529 | arg0 = argv[0];
|
---|
1530 | if (arg0 && arg0[0] == '/' && arg0[1] == '/')
|
---|
1531 | for ( ; arg0 && *arg0; arg0++)
|
---|
1532 | if (*arg0 == '/')
|
---|
1533 | *arg0 = '\\';
|
---|
1534 |
|
---|
1535 | /* make sure CreateProcess() has Path it needs */
|
---|
1536 | sync_Path_environment();
|
---|
1537 |
|
---|
1538 | hPID = process_easy(argv, child->environment);
|
---|
1539 |
|
---|
1540 | if (hPID != INVALID_HANDLE_VALUE)
|
---|
1541 | child->pid = (int) hPID;
|
---|
1542 | else {
|
---|
1543 | int i;
|
---|
1544 | unblock_sigs();
|
---|
1545 | fprintf(stderr,
|
---|
1546 | _("process_easy() failed to launch process (e=%ld)\n"),
|
---|
1547 | process_last_err(hPID));
|
---|
1548 | for (i = 0; argv[i]; i++)
|
---|
1549 | fprintf(stderr, "%s ", argv[i]);
|
---|
1550 | fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
|
---|
1551 | goto error;
|
---|
1552 | }
|
---|
1553 | }
|
---|
1554 | #endif /* WINDOWS32 */
|
---|
1555 | #endif /* __MSDOS__ or Amiga or WINDOWS32 */
|
---|
1556 |
|
---|
1557 | /* Bump the number of jobs started in this second. */
|
---|
1558 | ++job_counter;
|
---|
1559 |
|
---|
1560 | /* We are the parent side. Set the state to
|
---|
1561 | say the commands are running and return. */
|
---|
1562 |
|
---|
1563 | set_command_state (child->file, cs_running);
|
---|
1564 |
|
---|
1565 | /* Free the storage used by the child's argument list. */
|
---|
1566 | #ifdef KMK /* leak */
|
---|
1567 | cleanup_argv:
|
---|
1568 | #endif
|
---|
1569 | #ifndef VMS
|
---|
1570 | free (argv[0]);
|
---|
1571 | free (argv);
|
---|
1572 | #endif
|
---|
1573 |
|
---|
1574 | return;
|
---|
1575 |
|
---|
1576 | error:
|
---|
1577 | child->file->update_status = 2;
|
---|
1578 | notice_finished_file (child->file);
|
---|
1579 | #ifdef KMK /* fix leak */
|
---|
1580 | goto cleanup_argv;
|
---|
1581 | #else
|
---|
1582 | return;
|
---|
1583 | #endif
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 | /* Try to start a child running.
|
---|
1587 | Returns nonzero if the child was started (and maybe finished), or zero if
|
---|
1588 | the load was too high and the child was put on the `waiting_jobs' chain. */
|
---|
1589 |
|
---|
1590 | static int
|
---|
1591 | start_waiting_job (struct child *c)
|
---|
1592 | {
|
---|
1593 | struct file *f = c->file;
|
---|
1594 | #ifdef DB_KMK
|
---|
1595 | DB (DB_KMK, (_("start_waiting_job %p (`%s') command_flags=%#x slots=%d/%d\n"),
|
---|
1596 | (void *)c, c->file->name, c->file->command_flags, job_slots_used, job_slots));
|
---|
1597 | #endif
|
---|
1598 |
|
---|
1599 | /* If we can start a job remotely, we always want to, and don't care about
|
---|
1600 | the local load average. We record that the job should be started
|
---|
1601 | remotely in C->remote for start_job_command to test. */
|
---|
1602 |
|
---|
1603 | c->remote = start_remote_job_p (1);
|
---|
1604 |
|
---|
1605 | #ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
|
---|
1606 | if (c->file->command_flags & COMMANDS_NOTPARALLEL)
|
---|
1607 | {
|
---|
1608 | DB (DB_KMK, (_("not_parallel %d -> %d (file=%p `%s') [start_waiting_job]\n"),
|
---|
1609 | not_parallel, not_parallel + 1, (void *)c->file, c->file->name));
|
---|
1610 | assert(not_parallel >= 0);
|
---|
1611 | ++not_parallel;
|
---|
1612 | }
|
---|
1613 | #endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
|
---|
1614 |
|
---|
1615 | /* If we are running at least one job already and the load average
|
---|
1616 | is too high, make this one wait. */
|
---|
1617 | if (!c->remote
|
---|
1618 | #ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
|
---|
1619 | && ((job_slots_used > 0 && (not_parallel > 0 || load_too_high ()))
|
---|
1620 | #else
|
---|
1621 | && ((job_slots_used > 0 && load_too_high ())
|
---|
1622 | #endif
|
---|
1623 | #ifdef WINDOWS32
|
---|
1624 | || (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
|
---|
1625 | #endif
|
---|
1626 | ))
|
---|
1627 | {
|
---|
1628 | #ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL
|
---|
1629 | /* Put this child on the chain of children waiting for the load average
|
---|
1630 | to go down. */
|
---|
1631 | set_command_state (f, cs_running);
|
---|
1632 | c->next = waiting_jobs;
|
---|
1633 | waiting_jobs = c;
|
---|
1634 |
|
---|
1635 | #else /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
|
---|
1636 |
|
---|
1637 | /* Put this child on the chain of children waiting for the load average
|
---|
1638 | to go down. If not parallel, put it last. */
|
---|
1639 | set_command_state (f, cs_running);
|
---|
1640 | c->next = waiting_jobs;
|
---|
1641 | if (c->next && (c->file->command_flags & COMMANDS_NOTPARALLEL))
|
---|
1642 | {
|
---|
1643 | struct child *prev = waiting_jobs;
|
---|
1644 | while (prev->next)
|
---|
1645 | prev = prev->next;
|
---|
1646 | c->next = 0;
|
---|
1647 | prev->next = c;
|
---|
1648 | }
|
---|
1649 | else /* FIXME: insert after the last node with COMMANDS_NOTPARALLEL set */
|
---|
1650 | waiting_jobs = c;
|
---|
1651 | DB (DB_KMK, (_("queued child %p (`%s')\n"), (void *)c, c->file->name));
|
---|
1652 | #endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
|
---|
1653 | return 0;
|
---|
1654 | }
|
---|
1655 |
|
---|
1656 | /* Start the first command; reap_children will run later command lines. */
|
---|
1657 | start_job_command (c);
|
---|
1658 |
|
---|
1659 | switch (f->command_state)
|
---|
1660 | {
|
---|
1661 | case cs_running:
|
---|
1662 | c->next = children;
|
---|
1663 | DB (DB_JOBS, (_("Putting child 0x%08lx (%s) PID %ld%s on the chain.\n"),
|
---|
1664 | (unsigned long int) c, c->file->name,
|
---|
1665 | (long) c->pid, c->remote ? _(" (remote)") : ""));
|
---|
1666 | children = c;
|
---|
1667 | /* One more job slot is in use. */
|
---|
1668 | ++job_slots_used;
|
---|
1669 | unblock_sigs ();
|
---|
1670 | break;
|
---|
1671 |
|
---|
1672 | case cs_not_started:
|
---|
1673 | /* All the command lines turned out to be empty. */
|
---|
1674 | f->update_status = 0;
|
---|
1675 | /* FALLTHROUGH */
|
---|
1676 |
|
---|
1677 | case cs_finished:
|
---|
1678 | notice_finished_file (f);
|
---|
1679 | free_child (c);
|
---|
1680 | break;
|
---|
1681 |
|
---|
1682 | default:
|
---|
1683 | assert (f->command_state == cs_finished);
|
---|
1684 | break;
|
---|
1685 | }
|
---|
1686 |
|
---|
1687 | return 1;
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | /* Create a `struct child' for FILE and start its commands running. */
|
---|
1691 |
|
---|
1692 | void
|
---|
1693 | new_job (struct file *file)
|
---|
1694 | {
|
---|
1695 | struct commands *cmds = file->cmds;
|
---|
1696 | struct child *c;
|
---|
1697 | char **lines;
|
---|
1698 | unsigned int i;
|
---|
1699 |
|
---|
1700 | /* Let any previously decided-upon jobs that are waiting
|
---|
1701 | for the load to go down start before this new one. */
|
---|
1702 | start_waiting_jobs ();
|
---|
1703 |
|
---|
1704 | /* Reap any children that might have finished recently. */
|
---|
1705 | reap_children (0, 0);
|
---|
1706 |
|
---|
1707 | /* Chop the commands up into lines if they aren't already. */
|
---|
1708 | chop_commands (cmds);
|
---|
1709 | #ifdef CONFIG_WITH_MEMORY_OPTIMIZATIONS
|
---|
1710 | cmds->refs++; /* retain the chopped lines. */
|
---|
1711 | #endif
|
---|
1712 |
|
---|
1713 | /* Expand the command lines and store the results in LINES. */
|
---|
1714 | lines = xmalloc (cmds->ncommand_lines * sizeof (char *));
|
---|
1715 | for (i = 0; i < cmds->ncommand_lines; ++i)
|
---|
1716 | {
|
---|
1717 | /* Collapse backslash-newline combinations that are inside variable
|
---|
1718 | or function references. These are left alone by the parser so
|
---|
1719 | that they will appear in the echoing of commands (where they look
|
---|
1720 | nice); and collapsed by construct_command_argv when it tokenizes.
|
---|
1721 | But letting them survive inside function invocations loses because
|
---|
1722 | we don't want the functions to see them as part of the text. */
|
---|
1723 |
|
---|
1724 | char *in, *out, *ref;
|
---|
1725 |
|
---|
1726 | /* IN points to where in the line we are scanning.
|
---|
1727 | OUT points to where in the line we are writing.
|
---|
1728 | When we collapse a backslash-newline combination,
|
---|
1729 | IN gets ahead of OUT. */
|
---|
1730 |
|
---|
1731 | in = out = cmds->command_lines[i];
|
---|
1732 | while ((ref = strchr (in, '$')) != 0)
|
---|
1733 | {
|
---|
1734 | ++ref; /* Move past the $. */
|
---|
1735 |
|
---|
1736 | if (out != in)
|
---|
1737 | /* Copy the text between the end of the last chunk
|
---|
1738 | we processed (where IN points) and the new chunk
|
---|
1739 | we are about to process (where REF points). */
|
---|
1740 | memmove (out, in, ref - in);
|
---|
1741 |
|
---|
1742 | /* Move both pointers past the boring stuff. */
|
---|
1743 | out += ref - in;
|
---|
1744 | in = ref;
|
---|
1745 |
|
---|
1746 | if (*ref == '(' || *ref == '{')
|
---|
1747 | {
|
---|
1748 | char openparen = *ref;
|
---|
1749 | char closeparen = openparen == '(' ? ')' : '}';
|
---|
1750 | int count;
|
---|
1751 | char *p;
|
---|
1752 |
|
---|
1753 | *out++ = *in++; /* Copy OPENPAREN. */
|
---|
1754 | /* IN now points past the opening paren or brace.
|
---|
1755 | Count parens or braces until it is matched. */
|
---|
1756 | count = 0;
|
---|
1757 | while (*in != '\0')
|
---|
1758 | {
|
---|
1759 | if (*in == closeparen && --count < 0)
|
---|
1760 | break;
|
---|
1761 | else if (*in == '\\' && in[1] == '\n')
|
---|
1762 | {
|
---|
1763 | /* We have found a backslash-newline inside a
|
---|
1764 | variable or function reference. Eat it and
|
---|
1765 | any following whitespace. */
|
---|
1766 |
|
---|
1767 | int quoted = 0;
|
---|
1768 | for (p = in - 1; p > ref && *p == '\\'; --p)
|
---|
1769 | quoted = !quoted;
|
---|
1770 |
|
---|
1771 | if (quoted)
|
---|
1772 | /* There were two or more backslashes, so this is
|
---|
1773 | not really a continuation line. We don't collapse
|
---|
1774 | the quoting backslashes here as is done in
|
---|
1775 | collapse_continuations, because the line will
|
---|
1776 | be collapsed again after expansion. */
|
---|
1777 | *out++ = *in++;
|
---|
1778 | else
|
---|
1779 | {
|
---|
1780 | /* Skip the backslash, newline and
|
---|
1781 | any following whitespace. */
|
---|
1782 | in = next_token (in + 2);
|
---|
1783 |
|
---|
1784 | /* Discard any preceding whitespace that has
|
---|
1785 | already been written to the output. */
|
---|
1786 | while (out > ref
|
---|
1787 | && isblank ((unsigned char)out[-1]))
|
---|
1788 | --out;
|
---|
1789 |
|
---|
1790 | /* Replace it all with a single space. */
|
---|
1791 | *out++ = ' ';
|
---|
1792 | }
|
---|
1793 | }
|
---|
1794 | else
|
---|
1795 | {
|
---|
1796 | if (*in == openparen)
|
---|
1797 | ++count;
|
---|
1798 |
|
---|
1799 | *out++ = *in++;
|
---|
1800 | }
|
---|
1801 | }
|
---|
1802 | }
|
---|
1803 | }
|
---|
1804 |
|
---|
1805 | /* There are no more references in this line to worry about.
|
---|
1806 | Copy the remaining uninteresting text to the output. */
|
---|
1807 | if (out != in)
|
---|
1808 | strcpy (out, in);
|
---|
1809 |
|
---|
1810 | /* Finally, expand the line. */
|
---|
1811 | lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
|
---|
1812 | file);
|
---|
1813 | }
|
---|
1814 |
|
---|
1815 | /* Start the command sequence, record it in a new
|
---|
1816 | `struct child', and add that to the chain. */
|
---|
1817 |
|
---|
1818 | c = xmalloc (sizeof (struct child));
|
---|
1819 | memset (c, '\0', sizeof (struct child));
|
---|
1820 | c->file = file;
|
---|
1821 | c->command_lines = lines;
|
---|
1822 | c->sh_batch_file = NULL;
|
---|
1823 |
|
---|
1824 | /* Cache dontcare flag because file->dontcare can be changed once we
|
---|
1825 | return. Check dontcare inheritance mechanism for details. */
|
---|
1826 | c->dontcare = file->dontcare;
|
---|
1827 |
|
---|
1828 | /* Fetch the first command line to be run. */
|
---|
1829 | job_next_command (c);
|
---|
1830 |
|
---|
1831 | /* Wait for a job slot to be freed up. If we allow an infinite number
|
---|
1832 | don't bother; also job_slots will == 0 if we're using the jobserver. */
|
---|
1833 |
|
---|
1834 | if (job_slots != 0)
|
---|
1835 | while (job_slots_used == job_slots)
|
---|
1836 | reap_children (1, 0);
|
---|
1837 |
|
---|
1838 | #ifdef MAKE_JOBSERVER
|
---|
1839 | /* If we are controlling multiple jobs make sure we have a token before
|
---|
1840 | starting the child. */
|
---|
1841 |
|
---|
1842 | /* This can be inefficient. There's a decent chance that this job won't
|
---|
1843 | actually have to run any subprocesses: the command script may be empty
|
---|
1844 | or otherwise optimized away. It would be nice if we could defer
|
---|
1845 | obtaining a token until just before we need it, in start_job_command.
|
---|
1846 | To do that we'd need to keep track of whether we'd already obtained a
|
---|
1847 | token (since start_job_command is called for each line of the job, not
|
---|
1848 | just once). Also more thought needs to go into the entire algorithm;
|
---|
1849 | this is where the old parallel job code waits, so... */
|
---|
1850 |
|
---|
1851 | else if (job_fds[0] >= 0)
|
---|
1852 | while (1)
|
---|
1853 | {
|
---|
1854 | char token;
|
---|
1855 | int got_token;
|
---|
1856 | int saved_errno;
|
---|
1857 |
|
---|
1858 | DB (DB_JOBS, ("Need a job token; we %shave children\n",
|
---|
1859 | children ? "" : "don't "));
|
---|
1860 |
|
---|
1861 | /* If we don't already have a job started, use our "free" token. */
|
---|
1862 | if (!jobserver_tokens)
|
---|
1863 | break;
|
---|
1864 |
|
---|
1865 | /* Read a token. As long as there's no token available we'll block.
|
---|
1866 | We enable interruptible system calls before the read(2) so that if
|
---|
1867 | we get a SIGCHLD while we're waiting, we'll return with EINTR and
|
---|
1868 | we can process the death(s) and return tokens to the free pool.
|
---|
1869 |
|
---|
1870 | Once we return from the read, we immediately reinstate restartable
|
---|
1871 | system calls. This allows us to not worry about checking for
|
---|
1872 | EINTR on all the other system calls in the program.
|
---|
1873 |
|
---|
1874 | There is one other twist: there is a span between the time
|
---|
1875 | reap_children() does its last check for dead children and the time
|
---|
1876 | the read(2) call is entered, below, where if a child dies we won't
|
---|
1877 | notice. This is extremely serious as it could cause us to
|
---|
1878 | deadlock, given the right set of events.
|
---|
1879 |
|
---|
1880 | To avoid this, we do the following: before we reap_children(), we
|
---|
1881 | dup(2) the read FD on the jobserver pipe. The read(2) call below
|
---|
1882 | uses that new FD. In the signal handler, we close that FD. That
|
---|
1883 | way, if a child dies during the section mentioned above, the
|
---|
1884 | read(2) will be invoked with an invalid FD and will return
|
---|
1885 | immediately with EBADF. */
|
---|
1886 |
|
---|
1887 | /* Make sure we have a dup'd FD. */
|
---|
1888 | if (job_rfd < 0)
|
---|
1889 | {
|
---|
1890 | DB (DB_JOBS, ("Duplicate the job FD\n"));
|
---|
1891 | job_rfd = dup (job_fds[0]);
|
---|
1892 | }
|
---|
1893 |
|
---|
1894 | /* Reap anything that's currently waiting. */
|
---|
1895 | reap_children (0, 0);
|
---|
1896 |
|
---|
1897 | /* Kick off any jobs we have waiting for an opportunity that
|
---|
1898 | can run now (ie waiting for load). */
|
---|
1899 | start_waiting_jobs ();
|
---|
1900 |
|
---|
1901 | /* If our "free" slot has become available, use it; we don't need an
|
---|
1902 | actual token. */
|
---|
1903 | if (!jobserver_tokens)
|
---|
1904 | break;
|
---|
1905 |
|
---|
1906 | /* There must be at least one child already, or we have no business
|
---|
1907 | waiting for a token. */
|
---|
1908 | if (!children)
|
---|
1909 | fatal (NILF, "INTERNAL: no children as we go to sleep on read\n");
|
---|
1910 |
|
---|
1911 | /* Set interruptible system calls, and read() for a job token. */
|
---|
1912 | set_child_handler_action_flags (1, waiting_jobs != NULL);
|
---|
1913 | got_token = read (job_rfd, &token, 1);
|
---|
1914 | saved_errno = errno;
|
---|
1915 | set_child_handler_action_flags (0, waiting_jobs != NULL);
|
---|
1916 |
|
---|
1917 | /* If we got one, we're done here. */
|
---|
1918 | if (got_token == 1)
|
---|
1919 | {
|
---|
1920 | DB (DB_JOBS, (_("Obtained token for child 0x%08lx (%s).\n"),
|
---|
1921 | (unsigned long int) c, c->file->name));
|
---|
1922 | break;
|
---|
1923 | }
|
---|
1924 |
|
---|
1925 | /* If the error _wasn't_ expected (EINTR or EBADF), punt. Otherwise,
|
---|
1926 | go back and reap_children(), and try again. */
|
---|
1927 | errno = saved_errno;
|
---|
1928 | if (errno != EINTR && errno != EBADF)
|
---|
1929 | pfatal_with_name (_("read jobs pipe"));
|
---|
1930 | if (errno == EBADF)
|
---|
1931 | DB (DB_JOBS, ("Read returned EBADF.\n"));
|
---|
1932 | }
|
---|
1933 | #endif
|
---|
1934 |
|
---|
1935 | ++jobserver_tokens;
|
---|
1936 |
|
---|
1937 | /* The job is now primed. Start it running.
|
---|
1938 | (This will notice if there is in fact no recipe.) */
|
---|
1939 | if (cmds->fileinfo.filenm)
|
---|
1940 | DB (DB_BASIC, (_("Invoking recipe from %s:%lu to update target `%s'.\n"),
|
---|
1941 | cmds->fileinfo.filenm, cmds->fileinfo.lineno,
|
---|
1942 | c->file->name));
|
---|
1943 | else
|
---|
1944 | DB (DB_BASIC, (_("Invoking builtin recipe to update target `%s'.\n"),
|
---|
1945 | c->file->name));
|
---|
1946 |
|
---|
1947 |
|
---|
1948 | start_waiting_job (c);
|
---|
1949 |
|
---|
1950 | #ifndef CONFIG_WITH_EXTENDED_NOTPARALLEL
|
---|
1951 | if (job_slots == 1 || not_parallel)
|
---|
1952 | /* Since there is only one job slot, make things run linearly.
|
---|
1953 | Wait for the child to die, setting the state to `cs_finished'. */
|
---|
1954 | while (file->command_state == cs_running)
|
---|
1955 | reap_children (1, 0);
|
---|
1956 |
|
---|
1957 | #else /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
|
---|
1958 |
|
---|
1959 | if (job_slots == 1 || not_parallel < 0)
|
---|
1960 | {
|
---|
1961 | /* Since there is only one job slot, make things run linearly.
|
---|
1962 | Wait for the child to die, setting the state to `cs_finished'. */
|
---|
1963 | while (file->command_state == cs_running)
|
---|
1964 | reap_children (1, 0);
|
---|
1965 | }
|
---|
1966 | else if (not_parallel > 0)
|
---|
1967 | {
|
---|
1968 | /* wait for all live children to finish and then continue
|
---|
1969 | with the not-parallel child(s). FIXME: this loop could be better? */
|
---|
1970 | while (file->command_state == cs_running
|
---|
1971 | && (children != 0 || shell_function_pid != 0) /* reap_child condition */
|
---|
1972 | && not_parallel > 0)
|
---|
1973 | reap_children (1, 0);
|
---|
1974 | }
|
---|
1975 | #endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
|
---|
1976 |
|
---|
1977 | return;
|
---|
1978 | }
|
---|
1979 | |
---|
1980 |
|
---|
1981 | /* Move CHILD's pointers to the next command for it to execute.
|
---|
1982 | Returns nonzero if there is another command. */
|
---|
1983 |
|
---|
1984 | static int
|
---|
1985 | job_next_command (struct child *child)
|
---|
1986 | {
|
---|
1987 | while (child->command_ptr == 0 || *child->command_ptr == '\0')
|
---|
1988 | {
|
---|
1989 | /* There are no more lines in the expansion of this line. */
|
---|
1990 | if (child->command_line == child->file->cmds->ncommand_lines)
|
---|
1991 | {
|
---|
1992 | /* There are no more lines to be expanded. */
|
---|
1993 | child->command_ptr = 0;
|
---|
1994 | return 0;
|
---|
1995 | }
|
---|
1996 | else
|
---|
1997 | /* Get the next line to run. */
|
---|
1998 | child->command_ptr = child->command_lines[child->command_line++];
|
---|
1999 | }
|
---|
2000 | return 1;
|
---|
2001 | }
|
---|
2002 |
|
---|
2003 | /* Determine if the load average on the system is too high to start a new job.
|
---|
2004 | The real system load average is only recomputed once a second. However, a
|
---|
2005 | very parallel make can easily start tens or even hundreds of jobs in a
|
---|
2006 | second, which brings the system to its knees for a while until that first
|
---|
2007 | batch of jobs clears out.
|
---|
2008 |
|
---|
2009 | To avoid this we use a weighted algorithm to try to account for jobs which
|
---|
2010 | have been started since the last second, and guess what the load average
|
---|
2011 | would be now if it were computed.
|
---|
2012 |
|
---|
2013 | This algorithm was provided by Thomas Riedl <[email protected]>,
|
---|
2014 | who writes:
|
---|
2015 |
|
---|
2016 | ! calculate something load-oid and add to the observed sys.load,
|
---|
2017 | ! so that latter can catch up:
|
---|
2018 | ! - every job started increases jobctr;
|
---|
2019 | ! - every dying job decreases a positive jobctr;
|
---|
2020 | ! - the jobctr value gets zeroed every change of seconds,
|
---|
2021 | ! after its value*weight_b is stored into the 'backlog' value last_sec
|
---|
2022 | ! - weight_a times the sum of jobctr and last_sec gets
|
---|
2023 | ! added to the observed sys.load.
|
---|
2024 | !
|
---|
2025 | ! The two weights have been tried out on 24 and 48 proc. Sun Solaris-9
|
---|
2026 | ! machines, using a several-thousand-jobs-mix of cpp, cc, cxx and smallish
|
---|
2027 | ! sub-shelled commands (rm, echo, sed...) for tests.
|
---|
2028 | ! lowering the 'direct influence' factor weight_a (e.g. to 0.1)
|
---|
2029 | ! resulted in significant excession of the load limit, raising it
|
---|
2030 | ! (e.g. to 0.5) took bad to small, fast-executing jobs and didn't
|
---|
2031 | ! reach the limit in most test cases.
|
---|
2032 | !
|
---|
2033 | ! lowering the 'history influence' weight_b (e.g. to 0.1) resulted in
|
---|
2034 | ! exceeding the limit for longer-running stuff (compile jobs in
|
---|
2035 | ! the .5 to 1.5 sec. range),raising it (e.g. to 0.5) overrepresented
|
---|
2036 | ! small jobs' effects.
|
---|
2037 |
|
---|
2038 | */
|
---|
2039 |
|
---|
2040 | #define LOAD_WEIGHT_A 0.25
|
---|
2041 | #define LOAD_WEIGHT_B 0.25
|
---|
2042 |
|
---|
2043 | static int
|
---|
2044 | load_too_high (void)
|
---|
2045 | {
|
---|
2046 | #if defined(__MSDOS__) || defined(VMS) || defined(_AMIGA) || defined(__riscos__)
|
---|
2047 | return 1;
|
---|
2048 | #else
|
---|
2049 | static double last_sec;
|
---|
2050 | static time_t last_now;
|
---|
2051 | double load, guess;
|
---|
2052 | time_t now;
|
---|
2053 |
|
---|
2054 | #ifdef WINDOWS32
|
---|
2055 | /* sub_proc.c cannot wait for more than MAXIMUM_WAIT_OBJECTS children */
|
---|
2056 | if (process_used_slots () >= MAXIMUM_WAIT_OBJECTS)
|
---|
2057 | return 1;
|
---|
2058 | #endif
|
---|
2059 |
|
---|
2060 | if (max_load_average < 0)
|
---|
2061 | return 0;
|
---|
2062 |
|
---|
2063 | /* Find the real system load average. */
|
---|
2064 | make_access ();
|
---|
2065 | if (getloadavg (&load, 1) != 1)
|
---|
2066 | {
|
---|
2067 | static int lossage = -1;
|
---|
2068 | /* Complain only once for the same error. */
|
---|
2069 | if (lossage == -1 || errno != lossage)
|
---|
2070 | {
|
---|
2071 | if (errno == 0)
|
---|
2072 | /* An errno value of zero means getloadavg is just unsupported. */
|
---|
2073 | error (NILF,
|
---|
2074 | _("cannot enforce load limits on this operating system"));
|
---|
2075 | else
|
---|
2076 | perror_with_name (_("cannot enforce load limit: "), "getloadavg");
|
---|
2077 | }
|
---|
2078 | lossage = errno;
|
---|
2079 | load = 0;
|
---|
2080 | }
|
---|
2081 | user_access ();
|
---|
2082 |
|
---|
2083 | /* If we're in a new second zero the counter and correct the backlog
|
---|
2084 | value. Only keep the backlog for one extra second; after that it's 0. */
|
---|
2085 | now = time (NULL);
|
---|
2086 | if (last_now < now)
|
---|
2087 | {
|
---|
2088 | if (last_now == now - 1)
|
---|
2089 | last_sec = LOAD_WEIGHT_B * job_counter;
|
---|
2090 | else
|
---|
2091 | last_sec = 0.0;
|
---|
2092 |
|
---|
2093 | job_counter = 0;
|
---|
2094 | last_now = now;
|
---|
2095 | }
|
---|
2096 |
|
---|
2097 | /* Try to guess what the load would be right now. */
|
---|
2098 | guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec));
|
---|
2099 |
|
---|
2100 | DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n",
|
---|
2101 | guess, load, max_load_average));
|
---|
2102 |
|
---|
2103 | return guess >= max_load_average;
|
---|
2104 | #endif
|
---|
2105 | }
|
---|
2106 |
|
---|
2107 | /* Start jobs that are waiting for the load to be lower. */
|
---|
2108 |
|
---|
2109 | void
|
---|
2110 | start_waiting_jobs (void)
|
---|
2111 | {
|
---|
2112 | struct child *job;
|
---|
2113 |
|
---|
2114 | if (waiting_jobs == 0)
|
---|
2115 | return;
|
---|
2116 |
|
---|
2117 | do
|
---|
2118 | {
|
---|
2119 | /* Check for recently deceased descendants. */
|
---|
2120 | reap_children (0, 0);
|
---|
2121 |
|
---|
2122 | /* Take a job off the waiting list. */
|
---|
2123 | job = waiting_jobs;
|
---|
2124 | waiting_jobs = job->next;
|
---|
2125 |
|
---|
2126 | #ifdef CONFIG_WITH_EXTENDED_NOTPARALLEL
|
---|
2127 | /* If it's a not-parallel job, we've already counted it once
|
---|
2128 | when it was queued in start_waiting_job, so decrement
|
---|
2129 | before sending it to start_waiting_job again. */
|
---|
2130 | if (job->file->command_flags & COMMANDS_NOTPARALLEL)
|
---|
2131 | {
|
---|
2132 | DB (DB_KMK, (_("not_parallel %d -> %d (file=%p `%s') [start_waiting_jobs]\n"),
|
---|
2133 | not_parallel, not_parallel - 1, (void *) job->file, job->file->name));
|
---|
2134 | assert(not_parallel > 0);
|
---|
2135 | --not_parallel;
|
---|
2136 | }
|
---|
2137 | #endif /* CONFIG_WITH_EXTENDED_NOTPARALLEL */
|
---|
2138 |
|
---|
2139 | /* Try to start that job. We break out of the loop as soon
|
---|
2140 | as start_waiting_job puts one back on the waiting list. */
|
---|
2141 | }
|
---|
2142 | while (start_waiting_job (job) && waiting_jobs != 0);
|
---|
2143 |
|
---|
2144 | return;
|
---|
2145 | }
|
---|
2146 | |
---|
2147 |
|
---|
2148 | #ifndef WINDOWS32
|
---|
2149 |
|
---|
2150 | /* EMX: Start a child process. This function returns the new pid. */
|
---|
2151 | # if defined __EMX__
|
---|
2152 | int
|
---|
2153 | child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
|
---|
2154 | {
|
---|
2155 | int pid;
|
---|
2156 | /* stdin_fd == 0 means: nothing to do for stdin;
|
---|
2157 | stdout_fd == 1 means: nothing to do for stdout */
|
---|
2158 | int save_stdin = (stdin_fd != 0) ? dup (0) : 0;
|
---|
2159 | int save_stdout = (stdout_fd != 1) ? dup (1): 1;
|
---|
2160 |
|
---|
2161 | /* < 0 only if dup() failed */
|
---|
2162 | if (save_stdin < 0)
|
---|
2163 | fatal (NILF, _("no more file handles: could not duplicate stdin\n"));
|
---|
2164 | if (save_stdout < 0)
|
---|
2165 | fatal (NILF, _("no more file handles: could not duplicate stdout\n"));
|
---|
2166 |
|
---|
2167 | /* Close unnecessary file handles for the child. */
|
---|
2168 | if (save_stdin != 0)
|
---|
2169 | CLOSE_ON_EXEC (save_stdin);
|
---|
2170 | if (save_stdout != 1)
|
---|
2171 | CLOSE_ON_EXEC (save_stdout);
|
---|
2172 |
|
---|
2173 | /* Connect the pipes to the child process. */
|
---|
2174 | if (stdin_fd != 0)
|
---|
2175 | (void) dup2 (stdin_fd, 0);
|
---|
2176 | if (stdout_fd != 1)
|
---|
2177 | (void) dup2 (stdout_fd, 1);
|
---|
2178 |
|
---|
2179 | /* stdin_fd and stdout_fd must be closed on exit because we are
|
---|
2180 | still in the parent process */
|
---|
2181 | if (stdin_fd != 0)
|
---|
2182 | CLOSE_ON_EXEC (stdin_fd);
|
---|
2183 | if (stdout_fd != 1)
|
---|
2184 | CLOSE_ON_EXEC (stdout_fd);
|
---|
2185 |
|
---|
2186 | /* Run the command. */
|
---|
2187 | pid = exec_command (argv, envp);
|
---|
2188 |
|
---|
2189 | /* Restore stdout/stdin of the parent and close temporary FDs. */
|
---|
2190 | if (stdin_fd != 0)
|
---|
2191 | {
|
---|
2192 | if (dup2 (save_stdin, 0) != 0)
|
---|
2193 | fatal (NILF, _("Could not restore stdin\n"));
|
---|
2194 | else
|
---|
2195 | close (save_stdin);
|
---|
2196 | }
|
---|
2197 |
|
---|
2198 | if (stdout_fd != 1)
|
---|
2199 | {
|
---|
2200 | if (dup2 (save_stdout, 1) != 1)
|
---|
2201 | fatal (NILF, _("Could not restore stdout\n"));
|
---|
2202 | else
|
---|
2203 | close (save_stdout);
|
---|
2204 | }
|
---|
2205 |
|
---|
2206 | return pid;
|
---|
2207 | }
|
---|
2208 |
|
---|
2209 | #elif !defined (_AMIGA) && !defined (__MSDOS__) && !defined (VMS)
|
---|
2210 |
|
---|
2211 | /* UNIX:
|
---|
2212 | Replace the current process with one executing the command in ARGV.
|
---|
2213 | STDIN_FD and STDOUT_FD are used as the process's stdin and stdout; ENVP is
|
---|
2214 | the environment of the new program. This function does not return. */
|
---|
2215 | void
|
---|
2216 | child_execute_job (int stdin_fd, int stdout_fd, char **argv, char **envp)
|
---|
2217 | {
|
---|
2218 | if (stdin_fd != 0)
|
---|
2219 | (void) dup2 (stdin_fd, 0);
|
---|
2220 | if (stdout_fd != 1)
|
---|
2221 | (void) dup2 (stdout_fd, 1);
|
---|
2222 | if (stdin_fd != 0)
|
---|
2223 | (void) close (stdin_fd);
|
---|
2224 | if (stdout_fd != 1)
|
---|
2225 | (void) close (stdout_fd);
|
---|
2226 |
|
---|
2227 | /* Run the command. */
|
---|
2228 | exec_command (argv, envp);
|
---|
2229 | }
|
---|
2230 | #endif /* !AMIGA && !__MSDOS__ && !VMS */
|
---|
2231 | #endif /* !WINDOWS32 */
|
---|
2232 | |
---|
2233 |
|
---|
2234 | #ifndef _AMIGA
|
---|
2235 | /* Replace the current process with one running the command in ARGV,
|
---|
2236 | with environment ENVP. This function does not return. */
|
---|
2237 |
|
---|
2238 | /* EMX: This function returns the pid of the child process. */
|
---|
2239 | # ifdef __EMX__
|
---|
2240 | int
|
---|
2241 | # else
|
---|
2242 | void
|
---|
2243 | # endif
|
---|
2244 | exec_command (char **argv, char **envp)
|
---|
2245 | {
|
---|
2246 | #ifdef VMS
|
---|
2247 | /* to work around a problem with signals and execve: ignore them */
|
---|
2248 | #ifdef SIGCHLD
|
---|
2249 | signal (SIGCHLD,SIG_IGN);
|
---|
2250 | #endif
|
---|
2251 | /* Run the program. */
|
---|
2252 | execve (argv[0], argv, envp);
|
---|
2253 | perror_with_name ("execve: ", argv[0]);
|
---|
2254 | _exit (EXIT_FAILURE);
|
---|
2255 | #else
|
---|
2256 | #ifdef WINDOWS32
|
---|
2257 | HANDLE hPID;
|
---|
2258 | HANDLE hWaitPID;
|
---|
2259 | int err = 0;
|
---|
2260 | int exit_code = EXIT_FAILURE;
|
---|
2261 |
|
---|
2262 | /* make sure CreateProcess() has Path it needs */
|
---|
2263 | sync_Path_environment();
|
---|
2264 |
|
---|
2265 | /* launch command */
|
---|
2266 | hPID = process_easy(argv, envp);
|
---|
2267 |
|
---|
2268 | /* make sure launch ok */
|
---|
2269 | if (hPID == INVALID_HANDLE_VALUE)
|
---|
2270 | {
|
---|
2271 | int i;
|
---|
2272 | fprintf(stderr,
|
---|
2273 | _("process_easy() failed failed to launch process (e=%ld)\n"),
|
---|
2274 | process_last_err(hPID));
|
---|
2275 | for (i = 0; argv[i]; i++)
|
---|
2276 | fprintf(stderr, "%s ", argv[i]);
|
---|
2277 | fprintf(stderr, _("\nCounted %d args in failed launch\n"), i);
|
---|
2278 | exit(EXIT_FAILURE);
|
---|
2279 | }
|
---|
2280 |
|
---|
2281 | /* wait and reap last child */
|
---|
2282 | hWaitPID = process_wait_for_any();
|
---|
2283 | while (hWaitPID)
|
---|
2284 | {
|
---|
2285 | /* was an error found on this process? */
|
---|
2286 | err = process_last_err(hWaitPID);
|
---|
2287 |
|
---|
2288 | /* get exit data */
|
---|
2289 | exit_code = process_exit_code(hWaitPID);
|
---|
2290 |
|
---|
2291 | if (err)
|
---|
2292 | fprintf(stderr, "make (e=%d, rc=%d): %s",
|
---|
2293 | err, exit_code, map_windows32_error_to_string(err));
|
---|
2294 |
|
---|
2295 | /* cleanup process */
|
---|
2296 | process_cleanup(hWaitPID);
|
---|
2297 |
|
---|
2298 | /* expect to find only last pid, warn about other pids reaped */
|
---|
2299 | if (hWaitPID == hPID)
|
---|
2300 | break;
|
---|
2301 | else
|
---|
2302 | fprintf(stderr,
|
---|
2303 | _("make reaped child pid %ld, still waiting for pid %ld\n"),
|
---|
2304 | (DWORD)hWaitPID, (DWORD)hPID);
|
---|
2305 | }
|
---|
2306 |
|
---|
2307 | /* return child's exit code as our exit code */
|
---|
2308 | exit(exit_code);
|
---|
2309 |
|
---|
2310 | #else /* !WINDOWS32 */
|
---|
2311 |
|
---|
2312 | # ifdef __EMX__
|
---|
2313 | int pid;
|
---|
2314 | # endif
|
---|
2315 |
|
---|
2316 | /* Be the user, permanently. */
|
---|
2317 | child_access ();
|
---|
2318 |
|
---|
2319 | # ifdef __EMX__
|
---|
2320 |
|
---|
2321 | /* Run the program. */
|
---|
2322 | pid = spawnvpe (P_NOWAIT, argv[0], argv, envp);
|
---|
2323 |
|
---|
2324 | if (pid >= 0)
|
---|
2325 | return pid;
|
---|
2326 |
|
---|
2327 | /* the file might have a strange shell extension */
|
---|
2328 | if (errno == ENOENT)
|
---|
2329 | errno = ENOEXEC;
|
---|
2330 |
|
---|
2331 | # else
|
---|
2332 |
|
---|
2333 | /* Run the program. */
|
---|
2334 | environ = envp;
|
---|
2335 | execvp (argv[0], argv);
|
---|
2336 |
|
---|
2337 | # endif /* !__EMX__ */
|
---|
2338 |
|
---|
2339 | switch (errno)
|
---|
2340 | {
|
---|
2341 | case ENOENT:
|
---|
2342 | error (NILF, _("%s: Command not found"), argv[0]);
|
---|
2343 | break;
|
---|
2344 | case ENOEXEC:
|
---|
2345 | {
|
---|
2346 | /* The file is not executable. Try it as a shell script. */
|
---|
2347 | extern char *getenv ();
|
---|
2348 | char *shell;
|
---|
2349 | char **new_argv;
|
---|
2350 | int argc;
|
---|
2351 | int i=1;
|
---|
2352 |
|
---|
2353 | # ifdef __EMX__
|
---|
2354 | /* Do not use $SHELL from the environment */
|
---|
2355 | struct variable *p = lookup_variable ("SHELL", 5);
|
---|
2356 | if (p)
|
---|
2357 | shell = p->value;
|
---|
2358 | else
|
---|
2359 | shell = 0;
|
---|
2360 | # else
|
---|
2361 | shell = getenv ("SHELL");
|
---|
2362 | # endif
|
---|
2363 | if (shell == 0)
|
---|
2364 | shell = default_shell;
|
---|
2365 |
|
---|
2366 | argc = 1;
|
---|
2367 | while (argv[argc] != 0)
|
---|
2368 | ++argc;
|
---|
2369 |
|
---|
2370 | # ifdef __EMX__
|
---|
2371 | if (!unixy_shell)
|
---|
2372 | ++argc;
|
---|
2373 | # endif
|
---|
2374 |
|
---|
2375 | new_argv = alloca ((1 + argc + 1) * sizeof (char *));
|
---|
2376 | new_argv[0] = shell;
|
---|
2377 |
|
---|
2378 | # ifdef __EMX__
|
---|
2379 | if (!unixy_shell)
|
---|
2380 | {
|
---|
2381 | new_argv[1] = "/c";
|
---|
2382 | ++i;
|
---|
2383 | --argc;
|
---|
2384 | }
|
---|
2385 | # endif
|
---|
2386 |
|
---|
2387 | new_argv[i] = argv[0];
|
---|
2388 | while (argc > 0)
|
---|
2389 | {
|
---|
2390 | new_argv[i + argc] = argv[argc];
|
---|
2391 | --argc;
|
---|
2392 | }
|
---|
2393 |
|
---|
2394 | # ifdef __EMX__
|
---|
2395 | pid = spawnvpe (P_NOWAIT, shell, new_argv, envp);
|
---|
2396 | if (pid >= 0)
|
---|
2397 | break;
|
---|
2398 | # else
|
---|
2399 | execvp (shell, new_argv);
|
---|
2400 | # endif
|
---|
2401 | if (errno == ENOENT)
|
---|
2402 | error (NILF, _("%s: Shell program not found"), shell);
|
---|
2403 | else
|
---|
2404 | perror_with_name ("execvp: ", shell);
|
---|
2405 | break;
|
---|
2406 | }
|
---|
2407 |
|
---|
2408 | # ifdef __EMX__
|
---|
2409 | case EINVAL:
|
---|
2410 | /* this nasty error was driving me nuts :-( */
|
---|
2411 | error (NILF, _("spawnvpe: environment space might be exhausted"));
|
---|
2412 | /* FALLTHROUGH */
|
---|
2413 | # endif
|
---|
2414 |
|
---|
2415 | default:
|
---|
2416 | perror_with_name ("execvp: ", argv[0]);
|
---|
2417 | break;
|
---|
2418 | }
|
---|
2419 |
|
---|
2420 | # ifdef __EMX__
|
---|
2421 | return pid;
|
---|
2422 | # else
|
---|
2423 | _exit (127);
|
---|
2424 | # endif
|
---|
2425 | #endif /* !WINDOWS32 */
|
---|
2426 | #endif /* !VMS */
|
---|
2427 | }
|
---|
2428 | #else /* On Amiga */
|
---|
2429 | void exec_command (char **argv)
|
---|
2430 | {
|
---|
2431 | MyExecute (argv);
|
---|
2432 | }
|
---|
2433 |
|
---|
2434 | void clean_tmp (void)
|
---|
2435 | {
|
---|
2436 | DeleteFile (amiga_bname);
|
---|
2437 | }
|
---|
2438 |
|
---|
2439 | #endif /* On Amiga */
|
---|
2440 | |
---|
2441 |
|
---|
2442 | #ifndef VMS
|
---|
2443 | /* Figure out the argument list necessary to run LINE as a command. Try to
|
---|
2444 | avoid using a shell. This routine handles only ' quoting, and " quoting
|
---|
2445 | when no backslash, $ or ` characters are seen in the quotes. Starting
|
---|
2446 | quotes may be escaped with a backslash. If any of the characters in
|
---|
2447 | sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
|
---|
2448 | is the first word of a line, the shell is used.
|
---|
2449 |
|
---|
2450 | If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
|
---|
2451 | If *RESTP is NULL, newlines will be ignored.
|
---|
2452 |
|
---|
2453 | SHELL is the shell to use, or nil to use the default shell.
|
---|
2454 | IFS is the value of $IFS, or nil (meaning the default).
|
---|
2455 |
|
---|
2456 | FLAGS is the value of lines_flags for this command line. It is
|
---|
2457 | used in the WINDOWS32 port to check whether + or $(MAKE) were found
|
---|
2458 | in this command line, in which case the effect of just_print_flag
|
---|
2459 | is overridden. */
|
---|
2460 |
|
---|
2461 | static char **
|
---|
2462 | construct_command_argv_internal (char *line, char **restp, char *shell,
|
---|
2463 | char *ifs, int flags,
|
---|
2464 | char **batch_filename_ptr)
|
---|
2465 | {
|
---|
2466 | #ifdef __MSDOS__
|
---|
2467 | /* MSDOS supports both the stock DOS shell and ports of Unixy shells.
|
---|
2468 | We call `system' for anything that requires ``slow'' processing,
|
---|
2469 | because DOS shells are too dumb. When $SHELL points to a real
|
---|
2470 | (unix-style) shell, `system' just calls it to do everything. When
|
---|
2471 | $SHELL points to a DOS shell, `system' does most of the work
|
---|
2472 | internally, calling the shell only for its internal commands.
|
---|
2473 | However, it looks on the $PATH first, so you can e.g. have an
|
---|
2474 | external command named `mkdir'.
|
---|
2475 |
|
---|
2476 | Since we call `system', certain characters and commands below are
|
---|
2477 | actually not specific to COMMAND.COM, but to the DJGPP implementation
|
---|
2478 | of `system'. In particular:
|
---|
2479 |
|
---|
2480 | The shell wildcard characters are in DOS_CHARS because they will
|
---|
2481 | not be expanded if we call the child via `spawnXX'.
|
---|
2482 |
|
---|
2483 | The `;' is in DOS_CHARS, because our `system' knows how to run
|
---|
2484 | multiple commands on a single line.
|
---|
2485 |
|
---|
2486 | DOS_CHARS also include characters special to 4DOS/NDOS, so we
|
---|
2487 | won't have to tell one from another and have one more set of
|
---|
2488 | commands and special characters. */
|
---|
2489 | static char sh_chars_dos[] = "*?[];|<>%^&()";
|
---|
2490 | static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
|
---|
2491 | "copy", "ctty", "date", "del", "dir", "echo",
|
---|
2492 | "erase", "exit", "for", "goto", "if", "md",
|
---|
2493 | "mkdir", "path", "pause", "prompt", "rd",
|
---|
2494 | "rmdir", "rem", "ren", "rename", "set",
|
---|
2495 | "shift", "time", "type", "ver", "verify",
|
---|
2496 | "vol", ":", 0 };
|
---|
2497 |
|
---|
2498 | static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
|
---|
2499 | static char *sh_cmds_sh[] = { "cd", "echo", "eval", "exec", "exit", "login",
|
---|
2500 | "logout", "set", "umask", "wait", "while",
|
---|
2501 | "for", "case", "if", ":", ".", "break",
|
---|
2502 | "continue", "export", "read", "readonly",
|
---|
2503 | "shift", "times", "trap", "switch", "unset",
|
---|
2504 | 0 };
|
---|
2505 |
|
---|
2506 | char *sh_chars;
|
---|
2507 | char **sh_cmds;
|
---|
2508 | #elif defined (__EMX__)
|
---|
2509 | static char sh_chars_dos[] = "*?[];|<>%^&()";
|
---|
2510 | static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
|
---|
2511 | "copy", "ctty", "date", "del", "dir", "echo",
|
---|
2512 | "erase", "exit", "for", "goto", "if", "md",
|
---|
2513 | "mkdir", "path", "pause", "prompt", "rd",
|
---|
2514 | "rmdir", "rem", "ren", "rename", "set",
|
---|
2515 | "shift", "time", "type", "ver", "verify",
|
---|
2516 | "vol", ":", 0 };
|
---|
2517 |
|
---|
2518 | static char sh_chars_os2[] = "*?[];|<>%^()\"'&";
|
---|
2519 | static char *sh_cmds_os2[] = { "call", "cd", "chcp", "chdir", "cls", "copy",
|
---|
2520 | "date", "del", "detach", "dir", "echo",
|
---|
2521 | "endlocal", "erase", "exit", "for", "goto", "if",
|
---|
2522 | "keys", "md", "mkdir", "move", "path", "pause",
|
---|
2523 | "prompt", "rd", "rem", "ren", "rename", "rmdir",
|
---|
2524 | "set", "setlocal", "shift", "start", "time",
|
---|
2525 | "type", "ver", "verify", "vol", ":", 0 };
|
---|
2526 |
|
---|
2527 | static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~'";
|
---|
2528 | static char *sh_cmds_sh[] = { "echo", "cd", "eval", "exec", "exit", "login",
|
---|
2529 | "logout", "set", "umask", "wait", "while",
|
---|
2530 | "for", "case", "if", ":", ".", "break",
|
---|
2531 | "continue", "export", "read", "readonly",
|
---|
2532 | "shift", "times", "trap", "switch", "unset",
|
---|
2533 | 0 };
|
---|
2534 | char *sh_chars;
|
---|
2535 | char **sh_cmds;
|
---|
2536 |
|
---|
2537 | #elif defined (_AMIGA)
|
---|
2538 | static char sh_chars[] = "#;\"|<>()?*$`";
|
---|
2539 | static char *sh_cmds[] = { "cd", "eval", "if", "delete", "echo", "copy",
|
---|
2540 | "rename", "set", "setenv", "date", "makedir",
|
---|
2541 | "skip", "else", "endif", "path", "prompt",
|
---|
2542 | "unset", "unsetenv", "version",
|
---|
2543 | 0 };
|
---|
2544 | #elif defined (WINDOWS32)
|
---|
2545 | static char sh_chars_dos[] = "\"|&<>";
|
---|
2546 | static char *sh_cmds_dos[] = { "break", "call", "cd", "chcp", "chdir", "cls",
|
---|
2547 | "copy", "ctty", "date", "del", "dir", "echo",
|
---|
2548 | "erase", "exit", "for", "goto", "if", "if", "md",
|
---|
2549 | "mkdir", "path", "pause", "prompt", "rd", "rem",
|
---|
2550 | "ren", "rename", "rmdir", "set", "shift", "time",
|
---|
2551 | "type", "ver", "verify", "vol", ":", 0 };
|
---|
2552 | static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^";
|
---|
2553 | static char *sh_cmds_sh[] = { "cd", "eval", "exec", "exit", "login",
|
---|
2554 | "logout", "set", "umask", "wait", "while", "for",
|
---|
2555 | "case", "if", ":", ".", "break", "continue",
|
---|
2556 | "export", "read", "readonly", "shift", "times",
|
---|
2557 | "trap", "switch", "test",
|
---|
2558 | #ifdef BATCH_MODE_ONLY_SHELL
|
---|
2559 | "echo",
|
---|
2560 | #endif
|
---|
2561 | 0 };
|
---|
2562 | char* sh_chars;
|
---|
2563 | char** sh_cmds;
|
---|
2564 | #elif defined(__riscos__)
|
---|
2565 | static char sh_chars[] = "";
|
---|
2566 | static char *sh_cmds[] = { 0 };
|
---|
2567 | #else /* must be UNIX-ish */
|
---|
2568 | static char sh_chars_sh[] = "#;\"*?[]&|<>(){}$`^~!"; /* kmk: +_sh */
|
---|
2569 | static char *sh_cmds_sh[] = { ".", ":", "break", "case", "cd", "continue", /* kmk: +_sh */
|
---|
2570 | "eval", "exec", "exit", "export", "for", "if",
|
---|
2571 | "login", "logout", "read", "readonly", "set",
|
---|
2572 | "shift", "switch", "test", "times", "trap",
|
---|
2573 | "umask", "wait", "while", 0 };
|
---|
2574 | # ifdef HAVE_DOS_PATHS
|
---|
2575 | /* This is required if the MSYS/Cygwin ports (which do not define
|
---|
2576 | WINDOWS32) are compiled with HAVE_DOS_PATHS defined, which uses
|
---|
2577 | sh_chars_sh[] directly (see below). */
|
---|
2578 | static char *sh_chars_sh = sh_chars;
|
---|
2579 | # endif /* HAVE_DOS_PATHS */
|
---|
2580 | char* sh_chars = sh_chars_sh; /* kmk: +_sh */
|
---|
2581 | char** sh_cmds = sh_cmds_sh; /* kmk: +_sh */
|
---|
2582 | #endif
|
---|
2583 | #ifdef KMK
|
---|
2584 | static char sh_chars_kash[] = "#;*?[]&|<>(){}$`^~!"; /* note: no \" - good idea? */
|
---|
2585 | static char *sh_cmds_kash[] = {
|
---|
2586 | ".", ":", "break", "case", "cd", "continue",
|
---|
2587 | "echo", "eval", "exec", "exit", "export", "for", "if",
|
---|
2588 | "login", "logout", "read", "readonly", "set",
|
---|
2589 | "shift", "switch", "test", "times", "trap",
|
---|
2590 | "umask", "wait", "while", 0
|
---|
2591 | };
|
---|
2592 | int is_kmk_shell = 0;
|
---|
2593 | #endif
|
---|
2594 | int i;
|
---|
2595 | char *p;
|
---|
2596 | char *ap;
|
---|
2597 | char *end;
|
---|
2598 | int instring, word_has_equals, seen_nonequals, last_argument_was_empty;
|
---|
2599 | char **new_argv = 0;
|
---|
2600 | char *argstr = 0;
|
---|
2601 | #ifdef WINDOWS32
|
---|
2602 | int slow_flag = 0;
|
---|
2603 |
|
---|
2604 | if (!unixy_shell) {
|
---|
2605 | sh_cmds = sh_cmds_dos;
|
---|
2606 | sh_chars = sh_chars_dos;
|
---|
2607 | } else {
|
---|
2608 | sh_cmds = sh_cmds_sh;
|
---|
2609 | sh_chars = sh_chars_sh;
|
---|
2610 | }
|
---|
2611 | #endif /* WINDOWS32 */
|
---|
2612 |
|
---|
2613 | if (restp != NULL)
|
---|
2614 | *restp = NULL;
|
---|
2615 |
|
---|
2616 | /* Make sure not to bother processing an empty line. */
|
---|
2617 | while (isblank ((unsigned char)*line))
|
---|
2618 | ++line;
|
---|
2619 | if (*line == '\0')
|
---|
2620 | return 0;
|
---|
2621 |
|
---|
2622 | /* See if it is safe to parse commands internally. */
|
---|
2623 | #ifdef KMK /* kmk_ash and kmk_kash are both fine, kmk_ash is the default btw. */
|
---|
2624 | if (shell == 0)
|
---|
2625 | {
|
---|
2626 | is_kmk_shell = 1;
|
---|
2627 | shell = (char *)get_default_kbuild_shell ();
|
---|
2628 | }
|
---|
2629 | else if (!strcmp (shell, get_default_kbuild_shell()))
|
---|
2630 | is_kmk_shell = 1;
|
---|
2631 | else
|
---|
2632 | {
|
---|
2633 | const char *psz = strstr (shell, "/kmk_ash");
|
---|
2634 | if (psz)
|
---|
2635 | psz += sizeof ("/kmk_ash") - 1;
|
---|
2636 | else
|
---|
2637 | {
|
---|
2638 | psz = strstr (shell, "/kmk_kash");
|
---|
2639 | if (psz)
|
---|
2640 | psz += sizeof ("/kmk_kash") - 1;
|
---|
2641 | }
|
---|
2642 | # if defined (__OS2__) || defined (_WIN32) || defined (WINDOWS32)
|
---|
2643 | is_kmk_shell = psz && (*psz == '\0' || !stricmp (psz, ".exe"));
|
---|
2644 | # else
|
---|
2645 | is_kmk_shell = psz && *psz == '\0';
|
---|
2646 | # endif
|
---|
2647 | }
|
---|
2648 | if (is_kmk_shell)
|
---|
2649 | {
|
---|
2650 | sh_chars = sh_chars_kash;
|
---|
2651 | sh_cmds = sh_cmds_kash;
|
---|
2652 | }
|
---|
2653 | #else /* !KMK */
|
---|
2654 | if (shell == 0)
|
---|
2655 | shell = default_shell;
|
---|
2656 | #endif /* !KMK */
|
---|
2657 | #ifdef WINDOWS32
|
---|
2658 | else if (strcmp (shell, default_shell))
|
---|
2659 | {
|
---|
2660 | char *s1 = _fullpath (NULL, shell, 0);
|
---|
2661 | char *s2 = _fullpath (NULL, default_shell, 0);
|
---|
2662 |
|
---|
2663 | slow_flag = strcmp ((s1 ? s1 : ""), (s2 ? s2 : ""));
|
---|
2664 |
|
---|
2665 | if (s1)
|
---|
2666 | free (s1);
|
---|
2667 | if (s2)
|
---|
2668 | free (s2);
|
---|
2669 | }
|
---|
2670 | if (slow_flag)
|
---|
2671 | goto slow;
|
---|
2672 | #else /* not WINDOWS32 */
|
---|
2673 | #if defined (__MSDOS__) || defined (__EMX__)
|
---|
2674 | else if (strcasecmp (shell, default_shell))
|
---|
2675 | {
|
---|
2676 | extern int _is_unixy_shell (const char *_path);
|
---|
2677 |
|
---|
2678 | DB (DB_BASIC, (_("$SHELL changed (was `%s', now `%s')\n"),
|
---|
2679 | default_shell, shell));
|
---|
2680 | unixy_shell = _is_unixy_shell (shell);
|
---|
2681 | /* we must allocate a copy of shell: construct_command_argv() will free
|
---|
2682 | * shell after this function returns. */
|
---|
2683 | default_shell = xstrdup (shell);
|
---|
2684 | }
|
---|
2685 | if (unixy_shell)
|
---|
2686 | {
|
---|
2687 | sh_chars = sh_chars_sh;
|
---|
2688 | sh_cmds = sh_cmds_sh;
|
---|
2689 | }
|
---|
2690 | else
|
---|
2691 | {
|
---|
2692 | sh_chars = sh_chars_dos;
|
---|
2693 | sh_cmds = sh_cmds_dos;
|
---|
2694 | # ifdef __EMX__
|
---|
2695 | if (_osmode == OS2_MODE)
|
---|
2696 | {
|
---|
2697 | sh_chars = sh_chars_os2;
|
---|
2698 | sh_cmds = sh_cmds_os2;
|
---|
2699 | }
|
---|
2700 | # endif
|
---|
2701 | }
|
---|
2702 | #else /* !__MSDOS__ */
|
---|
2703 | else if (strcmp (shell, default_shell))
|
---|
2704 | goto slow;
|
---|
2705 | #endif /* !__MSDOS__ && !__EMX__ */
|
---|
2706 | #endif /* not WINDOWS32 */
|
---|
2707 |
|
---|
2708 | if (ifs != 0)
|
---|
2709 | for (ap = ifs; *ap != '\0'; ++ap)
|
---|
2710 | if (*ap != ' ' && *ap != '\t' && *ap != '\n')
|
---|
2711 | goto slow;
|
---|
2712 |
|
---|
2713 | i = strlen (line) + 1;
|
---|
2714 |
|
---|
2715 | /* More than 1 arg per character is impossible. */
|
---|
2716 | new_argv = xmalloc (i * sizeof (char *));
|
---|
2717 |
|
---|
2718 | /* All the args can fit in a buffer as big as LINE is. */
|
---|
2719 | ap = new_argv[0] = argstr = xmalloc (i);
|
---|
2720 | end = ap + i;
|
---|
2721 |
|
---|
2722 | /* I is how many complete arguments have been found. */
|
---|
2723 | i = 0;
|
---|
2724 | instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0;
|
---|
2725 | for (p = line; *p != '\0'; ++p)
|
---|
2726 | {
|
---|
2727 | assert (ap <= end);
|
---|
2728 |
|
---|
2729 | if (instring)
|
---|
2730 | {
|
---|
2731 | /* Inside a string, just copy any char except a closing quote
|
---|
2732 | or a backslash-newline combination. */
|
---|
2733 | if (*p == instring)
|
---|
2734 | {
|
---|
2735 | instring = 0;
|
---|
2736 | if (ap == new_argv[0] || *(ap-1) == '\0')
|
---|
2737 | last_argument_was_empty = 1;
|
---|
2738 | }
|
---|
2739 | else if (*p == '\\' && p[1] == '\n')
|
---|
2740 | {
|
---|
2741 | /* Backslash-newline is handled differently depending on what
|
---|
2742 | kind of string we're in: inside single-quoted strings you
|
---|
2743 | keep them; in double-quoted strings they disappear.
|
---|
2744 | For DOS/Windows/OS2, if we don't have a POSIX shell,
|
---|
2745 | we keep the pre-POSIX behavior of removing the
|
---|
2746 | backslash-newline. */
|
---|
2747 | if (instring == '"'
|
---|
2748 | #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
|
---|
2749 | || !unixy_shell
|
---|
2750 | #endif
|
---|
2751 | )
|
---|
2752 | ++p;
|
---|
2753 | else
|
---|
2754 | {
|
---|
2755 | *(ap++) = *(p++);
|
---|
2756 | *(ap++) = *p;
|
---|
2757 | }
|
---|
2758 | }
|
---|
2759 | else if (*p == '\n' && restp != NULL)
|
---|
2760 | {
|
---|
2761 | /* End of the command line. */
|
---|
2762 | *restp = p;
|
---|
2763 | goto end_of_line;
|
---|
2764 | }
|
---|
2765 | /* Backslash, $, and ` are special inside double quotes.
|
---|
2766 | If we see any of those, punt.
|
---|
2767 | But on MSDOS, if we use COMMAND.COM, double and single
|
---|
2768 | quotes have the same effect. */
|
---|
2769 | else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell)
|
---|
2770 | goto slow;
|
---|
2771 | else
|
---|
2772 | *ap++ = *p;
|
---|
2773 | }
|
---|
2774 | else if (strchr (sh_chars, *p) != 0)
|
---|
2775 | #ifdef KMK
|
---|
2776 | {
|
---|
2777 | /* Tilde is only special if at the start of a path spec,
|
---|
2778 | i.e. don't get excited when we by 8.3 files on windows. */
|
---|
2779 | if ( *p == '~'
|
---|
2780 | && p > line
|
---|
2781 | && !isspace (p[-1])
|
---|
2782 | && p[-1] != '='
|
---|
2783 | && p[-1] != ':'
|
---|
2784 | && p[-1] != '"'
|
---|
2785 | && p[-1] != '\'')
|
---|
2786 | *ap++ = *p;
|
---|
2787 | else
|
---|
2788 | /* Not inside a string, but it's a special char. */
|
---|
2789 | goto slow;
|
---|
2790 | }
|
---|
2791 | #else /* !KMK */
|
---|
2792 | /* Not inside a string, but it's a special char. */
|
---|
2793 | goto slow;
|
---|
2794 | #endif /* !KMK */
|
---|
2795 | #ifdef __MSDOS__
|
---|
2796 | else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.')
|
---|
2797 | /* `...' is a wildcard in DJGPP. */
|
---|
2798 | goto slow;
|
---|
2799 | #endif
|
---|
2800 | else
|
---|
2801 | /* Not a special char. */
|
---|
2802 | switch (*p)
|
---|
2803 | {
|
---|
2804 | case '=':
|
---|
2805 | /* Equals is a special character in leading words before the
|
---|
2806 | first word with no equals sign in it. This is not the case
|
---|
2807 | with sh -k, but we never get here when using nonstandard
|
---|
2808 | shell flags. */
|
---|
2809 | if (! seen_nonequals && unixy_shell)
|
---|
2810 | goto slow;
|
---|
2811 | word_has_equals = 1;
|
---|
2812 | *ap++ = '=';
|
---|
2813 | break;
|
---|
2814 |
|
---|
2815 | case '\\':
|
---|
2816 | /* Backslash-newline has special case handling, ref POSIX.
|
---|
2817 | We're in the fastpath, so emulate what the shell would do. */
|
---|
2818 | if (p[1] == '\n')
|
---|
2819 | {
|
---|
2820 | /* Throw out the backslash and newline. */
|
---|
2821 | ++p;
|
---|
2822 |
|
---|
2823 | /* If there's nothing in this argument yet, skip any
|
---|
2824 | whitespace before the start of the next word. */
|
---|
2825 | if (ap == new_argv[i])
|
---|
2826 | p = next_token (p + 1) - 1;
|
---|
2827 | }
|
---|
2828 | else if (p[1] != '\0')
|
---|
2829 | {
|
---|
2830 | #ifdef HAVE_DOS_PATHS
|
---|
2831 | /* Only remove backslashes before characters special to Unixy
|
---|
2832 | shells. All other backslashes are copied verbatim, since
|
---|
2833 | they are probably DOS-style directory separators. This
|
---|
2834 | still leaves a small window for problems, but at least it
|
---|
2835 | should work for the vast majority of naive users. */
|
---|
2836 |
|
---|
2837 | #ifdef __MSDOS__
|
---|
2838 | /* A dot is only special as part of the "..."
|
---|
2839 | wildcard. */
|
---|
2840 | if (strneq (p + 1, ".\\.\\.", 5))
|
---|
2841 | {
|
---|
2842 | *ap++ = '.';
|
---|
2843 | *ap++ = '.';
|
---|
2844 | p += 4;
|
---|
2845 | }
|
---|
2846 | else
|
---|
2847 | #endif
|
---|
2848 | if (p[1] != '\\' && p[1] != '\''
|
---|
2849 | && !isspace ((unsigned char)p[1])
|
---|
2850 | # ifdef KMK
|
---|
2851 | && strchr (sh_chars, p[1]) == 0
|
---|
2852 | && (p[1] != '"' || !unixy_shell))
|
---|
2853 | # else
|
---|
2854 | && strchr (sh_chars_sh, p[1]) == 0)
|
---|
2855 | # endif
|
---|
2856 | /* back up one notch, to copy the backslash */
|
---|
2857 | --p;
|
---|
2858 | #endif /* HAVE_DOS_PATHS */
|
---|
2859 |
|
---|
2860 | /* Copy and skip the following char. */
|
---|
2861 | *ap++ = *++p;
|
---|
2862 | }
|
---|
2863 | break;
|
---|
2864 |
|
---|
2865 | case '\'':
|
---|
2866 | case '"':
|
---|
2867 | instring = *p;
|
---|
2868 | break;
|
---|
2869 |
|
---|
2870 | case '\n':
|
---|
2871 | if (restp != NULL)
|
---|
2872 | {
|
---|
2873 | /* End of the command line. */
|
---|
2874 | *restp = p;
|
---|
2875 | goto end_of_line;
|
---|
2876 | }
|
---|
2877 | else
|
---|
2878 | /* Newlines are not special. */
|
---|
2879 | *ap++ = '\n';
|
---|
2880 | break;
|
---|
2881 |
|
---|
2882 | case ' ':
|
---|
2883 | case '\t':
|
---|
2884 | /* We have the end of an argument.
|
---|
2885 | Terminate the text of the argument. */
|
---|
2886 | *ap++ = '\0';
|
---|
2887 | new_argv[++i] = ap;
|
---|
2888 | last_argument_was_empty = 0;
|
---|
2889 |
|
---|
2890 | /* Update SEEN_NONEQUALS, which tells us if every word
|
---|
2891 | heretofore has contained an `='. */
|
---|
2892 | seen_nonequals |= ! word_has_equals;
|
---|
2893 | if (word_has_equals && ! seen_nonequals)
|
---|
2894 | /* An `=' in a word before the first
|
---|
2895 | word without one is magical. */
|
---|
2896 | goto slow;
|
---|
2897 | word_has_equals = 0; /* Prepare for the next word. */
|
---|
2898 |
|
---|
2899 | /* If this argument is the command name,
|
---|
2900 | see if it is a built-in shell command.
|
---|
2901 | If so, have the shell handle it. */
|
---|
2902 | if (i == 1)
|
---|
2903 | {
|
---|
2904 | register int j;
|
---|
2905 | for (j = 0; sh_cmds[j] != 0; ++j)
|
---|
2906 | {
|
---|
2907 | if (streq (sh_cmds[j], new_argv[0]))
|
---|
2908 | goto slow;
|
---|
2909 | # ifdef __EMX__
|
---|
2910 | /* Non-Unix shells are case insensitive. */
|
---|
2911 | if (!unixy_shell
|
---|
2912 | && strcasecmp (sh_cmds[j], new_argv[0]) == 0)
|
---|
2913 | goto slow;
|
---|
2914 | # endif
|
---|
2915 | }
|
---|
2916 | }
|
---|
2917 |
|
---|
2918 | /* Ignore multiple whitespace chars. */
|
---|
2919 | p = next_token (p) - 1;
|
---|
2920 | break;
|
---|
2921 |
|
---|
2922 | default:
|
---|
2923 | *ap++ = *p;
|
---|
2924 | break;
|
---|
2925 | }
|
---|
2926 | }
|
---|
2927 | end_of_line:
|
---|
2928 |
|
---|
2929 | if (instring)
|
---|
2930 | /* Let the shell deal with an unterminated quote. */
|
---|
2931 | goto slow;
|
---|
2932 |
|
---|
2933 | /* Terminate the last argument and the argument list. */
|
---|
2934 |
|
---|
2935 | *ap = '\0';
|
---|
2936 | if (new_argv[i][0] != '\0' || last_argument_was_empty)
|
---|
2937 | ++i;
|
---|
2938 | new_argv[i] = 0;
|
---|
2939 |
|
---|
2940 | if (i == 1)
|
---|
2941 | {
|
---|
2942 | register int j;
|
---|
2943 | for (j = 0; sh_cmds[j] != 0; ++j)
|
---|
2944 | if (streq (sh_cmds[j], new_argv[0]))
|
---|
2945 | goto slow;
|
---|
2946 | }
|
---|
2947 |
|
---|
2948 | if (new_argv[0] == 0)
|
---|
2949 | {
|
---|
2950 | /* Line was empty. */
|
---|
2951 | free (argstr);
|
---|
2952 | free (new_argv);
|
---|
2953 | return 0;
|
---|
2954 | }
|
---|
2955 |
|
---|
2956 | return new_argv;
|
---|
2957 |
|
---|
2958 | slow:;
|
---|
2959 | /* We must use the shell. */
|
---|
2960 |
|
---|
2961 | if (new_argv != 0)
|
---|
2962 | {
|
---|
2963 | /* Free the old argument list we were working on. */
|
---|
2964 | free (argstr);
|
---|
2965 | free (new_argv);
|
---|
2966 | }
|
---|
2967 |
|
---|
2968 | #ifdef __MSDOS__
|
---|
2969 | execute_by_shell = 1; /* actually, call `system' if shell isn't unixy */
|
---|
2970 | #endif
|
---|
2971 |
|
---|
2972 | #ifdef _AMIGA
|
---|
2973 | {
|
---|
2974 | char *ptr;
|
---|
2975 | char *buffer;
|
---|
2976 | char *dptr;
|
---|
2977 |
|
---|
2978 | buffer = xmalloc (strlen (line)+1);
|
---|
2979 |
|
---|
2980 | ptr = line;
|
---|
2981 | for (dptr=buffer; *ptr; )
|
---|
2982 | {
|
---|
2983 | if (*ptr == '\\' && ptr[1] == '\n')
|
---|
2984 | ptr += 2;
|
---|
2985 | else if (*ptr == '@') /* Kludge: multiline commands */
|
---|
2986 | {
|
---|
2987 | ptr += 2;
|
---|
2988 | *dptr++ = '\n';
|
---|
2989 | }
|
---|
2990 | else
|
---|
2991 | *dptr++ = *ptr++;
|
---|
2992 | }
|
---|
2993 | *dptr = 0;
|
---|
2994 |
|
---|
2995 | new_argv = xmalloc (2 * sizeof (char *));
|
---|
2996 | new_argv[0] = buffer;
|
---|
2997 | new_argv[1] = 0;
|
---|
2998 | }
|
---|
2999 | #else /* Not Amiga */
|
---|
3000 | #ifdef WINDOWS32
|
---|
3001 | /*
|
---|
3002 | * Not eating this whitespace caused things like
|
---|
3003 | *
|
---|
3004 | * sh -c "\n"
|
---|
3005 | *
|
---|
3006 | * which gave the shell fits. I think we have to eat
|
---|
3007 | * whitespace here, but this code should be considered
|
---|
3008 | * suspicious if things start failing....
|
---|
3009 | */
|
---|
3010 |
|
---|
3011 | /* Make sure not to bother processing an empty line. */
|
---|
3012 | while (isspace ((unsigned char)*line))
|
---|
3013 | ++line;
|
---|
3014 | if (*line == '\0')
|
---|
3015 | return 0;
|
---|
3016 | #endif /* WINDOWS32 */
|
---|
3017 | {
|
---|
3018 | /* SHELL may be a multi-word command. Construct a command line
|
---|
3019 | "SHELL -c LINE", with all special chars in LINE escaped.
|
---|
3020 | Then recurse, expanding this command line to get the final
|
---|
3021 | argument list. */
|
---|
3022 |
|
---|
3023 | unsigned int shell_len = strlen (shell);
|
---|
3024 | #ifndef VMS
|
---|
3025 | static char minus_c[] = " -c ";
|
---|
3026 | #else
|
---|
3027 | static char minus_c[] = "";
|
---|
3028 | #endif
|
---|
3029 | unsigned int line_len = strlen (line);
|
---|
3030 |
|
---|
3031 | char *new_line = alloca (shell_len + (sizeof (minus_c)-1)
|
---|
3032 | + (line_len*2) + 1);
|
---|
3033 | char *command_ptr = NULL; /* used for batch_mode_shell mode */
|
---|
3034 |
|
---|
3035 | # ifdef __EMX__ /* is this necessary? */
|
---|
3036 | if (!unixy_shell)
|
---|
3037 | minus_c[1] = '/'; /* " /c " */
|
---|
3038 | # endif
|
---|
3039 |
|
---|
3040 | ap = new_line;
|
---|
3041 | memcpy (ap, shell, shell_len);
|
---|
3042 | ap += shell_len;
|
---|
3043 | memcpy (ap, minus_c, sizeof (minus_c) - 1);
|
---|
3044 | ap += sizeof (minus_c) - 1;
|
---|
3045 | command_ptr = ap;
|
---|
3046 | for (p = line; *p != '\0'; ++p)
|
---|
3047 | {
|
---|
3048 | if (restp != NULL && *p == '\n')
|
---|
3049 | {
|
---|
3050 | *restp = p;
|
---|
3051 | break;
|
---|
3052 | }
|
---|
3053 | else if (*p == '\\' && p[1] == '\n')
|
---|
3054 | {
|
---|
3055 | /* POSIX says we keep the backslash-newline. If we don't have a
|
---|
3056 | POSIX shell on DOS/Windows/OS2, mimic the pre-POSIX behavior
|
---|
3057 | and remove the backslash/newline. */
|
---|
3058 | #if defined (__MSDOS__) || defined (__EMX__) || defined (WINDOWS32)
|
---|
3059 | # define PRESERVE_BSNL unixy_shell
|
---|
3060 | #else
|
---|
3061 | # define PRESERVE_BSNL 1
|
---|
3062 | #endif
|
---|
3063 | if (PRESERVE_BSNL)
|
---|
3064 | {
|
---|
3065 | *(ap++) = '\\';
|
---|
3066 | /* Only non-batch execution needs another backslash,
|
---|
3067 | because it will be passed through a recursive
|
---|
3068 | invocation of this function. */
|
---|
3069 | if (!batch_mode_shell)
|
---|
3070 | *(ap++) = '\\';
|
---|
3071 | *(ap++) = '\n';
|
---|
3072 | }
|
---|
3073 | ++p;
|
---|
3074 | continue;
|
---|
3075 | }
|
---|
3076 |
|
---|
3077 | /* DOS shells don't know about backslash-escaping. */
|
---|
3078 | if (unixy_shell && !batch_mode_shell &&
|
---|
3079 | (*p == '\\' || *p == '\'' || *p == '"'
|
---|
3080 | || isspace ((unsigned char)*p)
|
---|
3081 | || strchr (sh_chars, *p) != 0))
|
---|
3082 | *ap++ = '\\';
|
---|
3083 | #ifdef __MSDOS__
|
---|
3084 | else if (unixy_shell && strneq (p, "...", 3))
|
---|
3085 | {
|
---|
3086 | /* The case of `...' wildcard again. */
|
---|
3087 | strcpy (ap, "\\.\\.\\");
|
---|
3088 | ap += 5;
|
---|
3089 | p += 2;
|
---|
3090 | }
|
---|
3091 | #endif
|
---|
3092 | *ap++ = *p;
|
---|
3093 | }
|
---|
3094 | if (ap == new_line + shell_len + sizeof (minus_c) - 1)
|
---|
3095 | /* Line was empty. */
|
---|
3096 | return 0;
|
---|
3097 | *ap = '\0';
|
---|
3098 |
|
---|
3099 | #ifdef WINDOWS32
|
---|
3100 | /* Some shells do not work well when invoked as 'sh -c xxx' to run a
|
---|
3101 | command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems). In these
|
---|
3102 | cases, run commands via a script file. */
|
---|
3103 | if (just_print_flag && !(flags & COMMANDS_RECURSE)) {
|
---|
3104 | /* Need to allocate new_argv, although it's unused, because
|
---|
3105 | start_job_command will want to free it and its 0'th element. */
|
---|
3106 | new_argv = xmalloc(2 * sizeof (char *));
|
---|
3107 | new_argv[0] = xstrdup ("");
|
---|
3108 | new_argv[1] = NULL;
|
---|
3109 | } else if ((no_default_sh_exe || batch_mode_shell) && batch_filename_ptr) {
|
---|
3110 | int temp_fd;
|
---|
3111 | FILE* batch = NULL;
|
---|
3112 | int id = GetCurrentProcessId();
|
---|
3113 | PATH_VAR(fbuf);
|
---|
3114 |
|
---|
3115 | /* create a file name */
|
---|
3116 | sprintf(fbuf, "make%d", id);
|
---|
3117 | *batch_filename_ptr = create_batch_file (fbuf, unixy_shell, &temp_fd);
|
---|
3118 |
|
---|
3119 | DB (DB_JOBS, (_("Creating temporary batch file %s\n"),
|
---|
3120 | *batch_filename_ptr));
|
---|
3121 |
|
---|
3122 | /* Create a FILE object for the batch file, and write to it the
|
---|
3123 | commands to be executed. Put the batch file in TEXT mode. */
|
---|
3124 | _setmode (temp_fd, _O_TEXT);
|
---|
3125 | batch = _fdopen (temp_fd, "wt");
|
---|
3126 | if (!unixy_shell)
|
---|
3127 | fputs ("@echo off\n", batch);
|
---|
3128 | fputs (command_ptr, batch);
|
---|
3129 | fputc ('\n', batch);
|
---|
3130 | fclose (batch);
|
---|
3131 | DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"),
|
---|
3132 | !unixy_shell ? "\n\t@echo off" : "", command_ptr));
|
---|
3133 |
|
---|
3134 | /* create argv */
|
---|
3135 | new_argv = xmalloc(3 * sizeof (char *));
|
---|
3136 | if (unixy_shell) {
|
---|
3137 | new_argv[0] = xstrdup (shell);
|
---|
3138 | new_argv[1] = *batch_filename_ptr; /* only argv[0] gets freed later */
|
---|
3139 | } else {
|
---|
3140 | new_argv[0] = xstrdup (*batch_filename_ptr);
|
---|
3141 | new_argv[1] = NULL;
|
---|
3142 | }
|
---|
3143 | new_argv[2] = NULL;
|
---|
3144 | } else
|
---|
3145 | #endif /* WINDOWS32 */
|
---|
3146 | if (unixy_shell)
|
---|
3147 | new_argv = construct_command_argv_internal (new_line, 0, 0, 0, flags, 0);
|
---|
3148 | #ifdef __EMX__
|
---|
3149 | else if (!unixy_shell)
|
---|
3150 | {
|
---|
3151 | /* new_line is local, must not be freed therefore
|
---|
3152 | We use line here instead of new_line because we run the shell
|
---|
3153 | manually. */
|
---|
3154 | size_t line_len = strlen (line);
|
---|
3155 | char *p = new_line;
|
---|
3156 | char *q = new_line;
|
---|
3157 | memcpy (new_line, line, line_len + 1);
|
---|
3158 | /* replace all backslash-newline combination and also following tabs */
|
---|
3159 | while (*q != '\0')
|
---|
3160 | {
|
---|
3161 | if (q[0] == '\\' && q[1] == '\n')
|
---|
3162 | q += 2; /* remove '\\' and '\n' */
|
---|
3163 | else
|
---|
3164 | *p++ = *q++;
|
---|
3165 | }
|
---|
3166 | *p = '\0';
|
---|
3167 |
|
---|
3168 | # ifndef NO_CMD_DEFAULT
|
---|
3169 | if (strnicmp (new_line, "echo", 4) == 0
|
---|
3170 | && (new_line[4] == ' ' || new_line[4] == '\t'))
|
---|
3171 | {
|
---|
3172 | /* the builtin echo command: handle it separately */
|
---|
3173 | size_t echo_len = line_len - 5;
|
---|
3174 | char *echo_line = new_line + 5;
|
---|
3175 |
|
---|
3176 | /* special case: echo 'x="y"'
|
---|
3177 | cmd works this way: a string is printed as is, i.e., no quotes
|
---|
3178 | are removed. But autoconf uses a command like echo 'x="y"' to
|
---|
3179 | determine whether make works. autoconf expects the output x="y"
|
---|
3180 | so we will do exactly that.
|
---|
3181 | Note: if we do not allow cmd to be the default shell
|
---|
3182 | we do not need this kind of voodoo */
|
---|
3183 | if (echo_line[0] == '\''
|
---|
3184 | && echo_line[echo_len - 1] == '\''
|
---|
3185 | && strncmp (echo_line + 1, "ac_maketemp=",
|
---|
3186 | strlen ("ac_maketemp=")) == 0)
|
---|
3187 | {
|
---|
3188 | /* remove the enclosing quotes */
|
---|
3189 | memmove (echo_line, echo_line + 1, echo_len - 2);
|
---|
3190 | echo_line[echo_len - 2] = '\0';
|
---|
3191 | }
|
---|
3192 | }
|
---|
3193 | # endif
|
---|
3194 |
|
---|
3195 | {
|
---|
3196 | /* Let the shell decide what to do. Put the command line into the
|
---|
3197 | 2nd command line argument and hope for the best ;-) */
|
---|
3198 | size_t sh_len = strlen (shell);
|
---|
3199 |
|
---|
3200 | /* exactly 3 arguments + NULL */
|
---|
3201 | new_argv = xmalloc (4 * sizeof (char *));
|
---|
3202 | /* Exactly strlen(shell) + strlen("/c") + strlen(line) + 3 times
|
---|
3203 | the trailing '\0' */
|
---|
3204 | new_argv[0] = xmalloc (sh_len + line_len + 5);
|
---|
3205 | memcpy (new_argv[0], shell, sh_len + 1);
|
---|
3206 | new_argv[1] = new_argv[0] + sh_len + 1;
|
---|
3207 | memcpy (new_argv[1], "/c", 3);
|
---|
3208 | new_argv[2] = new_argv[1] + 3;
|
---|
3209 | memcpy (new_argv[2], new_line, line_len + 1);
|
---|
3210 | new_argv[3] = NULL;
|
---|
3211 | }
|
---|
3212 | }
|
---|
3213 | #elif defined(__MSDOS__)
|
---|
3214 | else
|
---|
3215 | {
|
---|
3216 | /* With MSDOS shells, we must construct the command line here
|
---|
3217 | instead of recursively calling ourselves, because we
|
---|
3218 | cannot backslash-escape the special characters (see above). */
|
---|
3219 | new_argv = xmalloc (sizeof (char *));
|
---|
3220 | line_len = strlen (new_line) - shell_len - sizeof (minus_c) + 1;
|
---|
3221 | new_argv[0] = xmalloc (line_len + 1);
|
---|
3222 | strncpy (new_argv[0],
|
---|
3223 | new_line + shell_len + sizeof (minus_c) - 1, line_len);
|
---|
3224 | new_argv[0][line_len] = '\0';
|
---|
3225 | }
|
---|
3226 | #else
|
---|
3227 | else
|
---|
3228 | fatal (NILF, _("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"),
|
---|
3229 | __FILE__, __LINE__);
|
---|
3230 | #endif
|
---|
3231 | }
|
---|
3232 | #endif /* ! AMIGA */
|
---|
3233 |
|
---|
3234 | return new_argv;
|
---|
3235 | }
|
---|
3236 | #endif /* !VMS */
|
---|
3237 |
|
---|
3238 | /* Figure out the argument list necessary to run LINE as a command. Try to
|
---|
3239 | avoid using a shell. This routine handles only ' quoting, and " quoting
|
---|
3240 | when no backslash, $ or ` characters are seen in the quotes. Starting
|
---|
3241 | quotes may be escaped with a backslash. If any of the characters in
|
---|
3242 | sh_chars[] is seen, or any of the builtin commands listed in sh_cmds[]
|
---|
3243 | is the first word of a line, the shell is used.
|
---|
3244 |
|
---|
3245 | If RESTP is not NULL, *RESTP is set to point to the first newline in LINE.
|
---|
3246 | If *RESTP is NULL, newlines will be ignored.
|
---|
3247 |
|
---|
3248 | FILE is the target whose commands these are. It is used for
|
---|
3249 | variable expansion for $(SHELL) and $(IFS). */
|
---|
3250 |
|
---|
3251 | char **
|
---|
3252 | construct_command_argv (char *line, char **restp, struct file *file,
|
---|
3253 | int cmd_flags, char **batch_filename_ptr)
|
---|
3254 | {
|
---|
3255 | char *shell, *ifs;
|
---|
3256 | char **argv;
|
---|
3257 |
|
---|
3258 | #ifdef VMS
|
---|
3259 | char *cptr;
|
---|
3260 | int argc;
|
---|
3261 |
|
---|
3262 | argc = 0;
|
---|
3263 | cptr = line;
|
---|
3264 | for (;;)
|
---|
3265 | {
|
---|
3266 | while ((*cptr != 0)
|
---|
3267 | && (isspace ((unsigned char)*cptr)))
|
---|
3268 | cptr++;
|
---|
3269 | if (*cptr == 0)
|
---|
3270 | break;
|
---|
3271 | while ((*cptr != 0)
|
---|
3272 | && (!isspace((unsigned char)*cptr)))
|
---|
3273 | cptr++;
|
---|
3274 | argc++;
|
---|
3275 | }
|
---|
3276 |
|
---|
3277 | argv = xmalloc (argc * sizeof (char *));
|
---|
3278 | if (argv == 0)
|
---|
3279 | abort ();
|
---|
3280 |
|
---|
3281 | cptr = line;
|
---|
3282 | argc = 0;
|
---|
3283 | for (;;)
|
---|
3284 | {
|
---|
3285 | while ((*cptr != 0)
|
---|
3286 | && (isspace ((unsigned char)*cptr)))
|
---|
3287 | cptr++;
|
---|
3288 | if (*cptr == 0)
|
---|
3289 | break;
|
---|
3290 | DB (DB_JOBS, ("argv[%d] = [%s]\n", argc, cptr));
|
---|
3291 | argv[argc++] = cptr;
|
---|
3292 | while ((*cptr != 0)
|
---|
3293 | && (!isspace((unsigned char)*cptr)))
|
---|
3294 | cptr++;
|
---|
3295 | if (*cptr != 0)
|
---|
3296 | *cptr++ = 0;
|
---|
3297 | }
|
---|
3298 | #else
|
---|
3299 | {
|
---|
3300 | /* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
|
---|
3301 | int save = warn_undefined_variables_flag;
|
---|
3302 | warn_undefined_variables_flag = 0;
|
---|
3303 |
|
---|
3304 | shell = allocated_variable_expand_for_file ("$(SHELL)", file);
|
---|
3305 | #ifdef WINDOWS32
|
---|
3306 | /*
|
---|
3307 | * Convert to forward slashes so that construct_command_argv_internal()
|
---|
3308 | * is not confused.
|
---|
3309 | */
|
---|
3310 | if (shell) {
|
---|
3311 | char *p = w32ify (shell, 0);
|
---|
3312 | strcpy (shell, p);
|
---|
3313 | }
|
---|
3314 | #endif
|
---|
3315 | #ifdef __EMX__
|
---|
3316 | {
|
---|
3317 | static const char *unixroot = NULL;
|
---|
3318 | static const char *last_shell = "";
|
---|
3319 | static int init = 0;
|
---|
3320 | if (init == 0)
|
---|
3321 | {
|
---|
3322 | unixroot = getenv ("UNIXROOT");
|
---|
3323 | /* unixroot must be NULL or not empty */
|
---|
3324 | if (unixroot && unixroot[0] == '\0') unixroot = NULL;
|
---|
3325 | init = 1;
|
---|
3326 | }
|
---|
3327 |
|
---|
3328 | /* if we have an unixroot drive and if shell is not default_shell
|
---|
3329 | (which means it's either cmd.exe or the test has already been
|
---|
3330 | performed) and if shell is an absolute path without drive letter,
|
---|
3331 | try whether it exists e.g.: if "/bin/sh" does not exist use
|
---|
3332 | "$UNIXROOT/bin/sh" instead. */
|
---|
3333 | if (unixroot && shell && strcmp (shell, last_shell) != 0
|
---|
3334 | && (shell[0] == '/' || shell[0] == '\\'))
|
---|
3335 | {
|
---|
3336 | /* trying a new shell, check whether it exists */
|
---|
3337 | size_t size = strlen (shell);
|
---|
3338 | char *buf = xmalloc (size + 7);
|
---|
3339 | memcpy (buf, shell, size);
|
---|
3340 | memcpy (buf + size, ".exe", 5); /* including the trailing '\0' */
|
---|
3341 | if (access (shell, F_OK) != 0 && access (buf, F_OK) != 0)
|
---|
3342 | {
|
---|
3343 | /* try the same for the unixroot drive */
|
---|
3344 | memmove (buf + 2, buf, size + 5);
|
---|
3345 | buf[0] = unixroot[0];
|
---|
3346 | buf[1] = unixroot[1];
|
---|
3347 | if (access (buf, F_OK) == 0)
|
---|
3348 | /* we have found a shell! */
|
---|
3349 | /* free(shell); */
|
---|
3350 | shell = buf;
|
---|
3351 | else
|
---|
3352 | free (buf);
|
---|
3353 | }
|
---|
3354 | else
|
---|
3355 | free (buf);
|
---|
3356 | }
|
---|
3357 | }
|
---|
3358 | #endif /* __EMX__ */
|
---|
3359 |
|
---|
3360 | ifs = allocated_variable_expand_for_file ("$(IFS)", file);
|
---|
3361 |
|
---|
3362 | warn_undefined_variables_flag = save;
|
---|
3363 | }
|
---|
3364 |
|
---|
3365 | #ifdef CONFIG_WITH_KMK_BUILTIN
|
---|
3366 | /* If it's a kmk_builtin command, make sure we're treated like a
|
---|
3367 | unix shell and and don't get batch files. */
|
---|
3368 | if ( ( !unixy_shell
|
---|
3369 | || batch_mode_shell
|
---|
3370 | # ifdef WINDOWS32
|
---|
3371 | || no_default_sh_exe
|
---|
3372 | # endif
|
---|
3373 | )
|
---|
3374 | && line
|
---|
3375 | && !strncmp(line, "kmk_builtin_", sizeof("kmk_builtin_") - 1))
|
---|
3376 | {
|
---|
3377 | int saved_batch_mode_shell = batch_mode_shell;
|
---|
3378 | int saved_unixy_shell = unixy_shell;
|
---|
3379 | # ifdef WINDOWS32
|
---|
3380 | int saved_no_default_sh_exe = no_default_sh_exe;
|
---|
3381 | no_default_sh_exe = 0;
|
---|
3382 | # endif
|
---|
3383 | unixy_shell = 1;
|
---|
3384 | batch_mode_shell = 0;
|
---|
3385 | argv = construct_command_argv_internal (line, restp, shell, ifs,
|
---|
3386 | cmd_flags, batch_filename_ptr);
|
---|
3387 | batch_mode_shell = saved_batch_mode_shell;
|
---|
3388 | unixy_shell = saved_unixy_shell;
|
---|
3389 | # ifdef WINDOWS32
|
---|
3390 | no_default_sh_exe = saved_no_default_sh_exe;
|
---|
3391 | # endif
|
---|
3392 | }
|
---|
3393 | else
|
---|
3394 | #endif /* CONFIG_WITH_KMK_BUILTIN */
|
---|
3395 | argv = construct_command_argv_internal (line, restp, shell, ifs,
|
---|
3396 | cmd_flags, batch_filename_ptr);
|
---|
3397 |
|
---|
3398 | free (shell);
|
---|
3399 | free (ifs);
|
---|
3400 | #endif /* !VMS */
|
---|
3401 | return argv;
|
---|
3402 | }
|
---|
3403 | |
---|
3404 |
|
---|
3405 | #if !defined(HAVE_DUP2) && !defined(_AMIGA)
|
---|
3406 | int
|
---|
3407 | dup2 (int old, int new)
|
---|
3408 | {
|
---|
3409 | int fd;
|
---|
3410 |
|
---|
3411 | (void) close (new);
|
---|
3412 | fd = dup (old);
|
---|
3413 | if (fd != new)
|
---|
3414 | {
|
---|
3415 | (void) close (fd);
|
---|
3416 | errno = EMFILE;
|
---|
3417 | return -1;
|
---|
3418 | }
|
---|
3419 |
|
---|
3420 | return fd;
|
---|
3421 | }
|
---|
3422 | #endif /* !HAPE_DUP2 && !_AMIGA */
|
---|
3423 |
|
---|
3424 | /* On VMS systems, include special VMS functions. */
|
---|
3425 |
|
---|
3426 | #ifdef VMS
|
---|
3427 | #include "vmsjobs.c"
|
---|
3428 | #endif
|
---|