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