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