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