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