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