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