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