| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "types.h" |
| 18 |
#include "makeint.h" |
| 19 |
#include "globals.h" |
| 20 |
|
| 21 |
#include "cmd.h" |
| 22 |
|
| 23 |
#include <assert.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "job.h" |
| 27 |
#include "print.h" |
| 28 |
#include "debug.h" |
| 29 |
#include "filedef.h" |
| 30 |
#include "commands.h" |
| 31 |
#include "variable.h" |
| 32 |
#include "os.h" |
| 33 |
|
| 34 |
|
| 35 |
const char *default_shell = "/bin/sh"; |
| 36 |
int batch_mode_shell = 0; |
| 37 |
|
| 38 |
#if defined (HAVE_SYS_WAIT_H) || defined (HAVE_UNION_WAIT) |
| 39 |
# include <sys/wait.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifdef HAVE_WAITPID |
| 43 |
# define WAIT_NOHANG(status) waitpid (-1, (status), WNOHANG) |
| 44 |
#else |
| 45 |
# ifdef HAVE_WAIT3 |
| 46 |
# ifndef wait3 |
| 47 |
extern int wait3 (); |
| 48 |
# endif |
| 49 |
# define WAIT_NOHANG(status) wait3 ((status), WNOHANG, (struct rusage *) 0) |
| 50 |
# endif |
| 51 |
#endif |
| 52 |
|
| 53 |
#ifdef USE_POSIX_SPAWN |
| 54 |
# include <spawn.h> |
| 55 |
# include "findprog.h" |
| 56 |
#endif |
| 57 |
|
| 58 |
#if !defined (wait) && !defined (POSIX) |
| 59 |
int wait (); |
| 60 |
#endif |
| 61 |
|
| 62 |
#ifndef HAVE_UNION_WAIT |
| 63 |
|
| 64 |
# define WAIT_T int |
| 65 |
|
| 66 |
# ifndef WTERMSIG |
| 67 |
# define WTERMSIG(x) ((x) & 0x7f) |
| 68 |
# endif |
| 69 |
# ifndef WCOREDUMP |
| 70 |
# define WCOREDUMP(x) ((x) & 0x80) |
| 71 |
# endif |
| 72 |
# ifndef WEXITSTATUS |
| 73 |
# define WEXITSTATUS(x) (((x) >> 8) & 0xff) |
| 74 |
# endif |
| 75 |
# ifndef WIFSIGNALED |
| 76 |
# define WIFSIGNALED(x) (WTERMSIG (x) != 0) |
| 77 |
# endif |
| 78 |
# ifndef WIFEXITED |
| 79 |
# define WIFEXITED(x) (WTERMSIG (x) == 0) |
| 80 |
# endif |
| 81 |
|
| 82 |
#else |
| 83 |
|
| 84 |
# define WAIT_T union wait |
| 85 |
# ifndef WTERMSIG |
| 86 |
# define WTERMSIG(x) ((x).w_termsig) |
| 87 |
# endif |
| 88 |
# ifndef WCOREDUMP |
| 89 |
# define WCOREDUMP(x) ((x).w_coredump) |
| 90 |
# endif |
| 91 |
# ifndef WEXITSTATUS |
| 92 |
# define WEXITSTATUS(x) ((x).w_retcode) |
| 93 |
# endif |
| 94 |
# ifndef WIFSIGNALED |
| 95 |
# define WIFSIGNALED(x) (WTERMSIG(x) != 0) |
| 96 |
# endif |
| 97 |
# ifndef WIFEXITED |
| 98 |
# define WIFEXITED(x) (WTERMSIG(x) == 0) |
| 99 |
# endif |
| 100 |
|
| 101 |
#endif |
| 102 |
|
| 103 |
#if !defined(HAVE_UNISTD_H) && !defined(WINDOWS32) |
| 104 |
int dup2 (); |
| 105 |
int execve (); |
| 106 |
void _exit (); |
| 107 |
int geteuid (); |
| 108 |
int getegid (); |
| 109 |
int setgid (); |
| 110 |
int getgid (); |
| 111 |
#endif |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
static const char * |
| 116 |
pid2str (pid_t pid) |
| 117 |
{ |
| 118 |
static char pidstring[100]; |
| 119 |
#if defined(WINDOWS32) && (__GNUC__ > 3 || _MSC_VER > 1300) |
| 120 |
|
| 121 |
|
| 122 |
sprintf (pidstring, "%Id", pid); |
| 123 |
#else |
| 124 |
sprintf (pidstring, "%lu", (unsigned long) pid); |
| 125 |
#endif |
| 126 |
return pidstring; |
| 127 |
} |
| 128 |
|
| 129 |
#ifndef HAVE_GETLOADAVG |
| 130 |
int getloadavg (double loadavg[], int nelem); |
| 131 |
#endif |
| 132 |
|
| 133 |
static void free_child (struct child *); |
| 134 |
static void start_job_command (struct child *child, |
| 135 |
target_stack_node_t *p_call_stack); |
| 136 |
static int load_too_high (void); |
| 137 |
static int job_next_command (struct child *); |
| 138 |
static int start_waiting_job (child_t *c, |
| 139 |
target_stack_node_t *p_call_stack); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
struct child *children = 0; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
unsigned int job_slots_used = 0; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static int good_stdin_used = 0; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
static struct child *waiting_jobs = 0; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
int unixy_shell = 1; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
unsigned long job_counter = 0; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
unsigned int jobserver_tokens = 0; |
| 168 |
|
| 169 |
|
| 170 |
#ifdef WINDOWS32 |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
int |
| 175 |
w32_kill (pid_t pid, int sig) |
| 176 |
{ |
| 177 |
return ((process_kill ((HANDLE)pid, sig) == TRUE) ? 0 : -1); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
static char * |
| 185 |
create_batch_file (char const *base, int unixy, int *fd) |
| 186 |
{ |
| 187 |
const char *const ext = unixy ? "sh" : "bat"; |
| 188 |
const char *error_string = NULL; |
| 189 |
char temp_path[MAXPATHLEN]; |
| 190 |
unsigned path_size = GetTempPath (sizeof temp_path, temp_path); |
| 191 |
int path_is_dot = 0; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
static unsigned uniq = 0; |
| 201 |
static int second_loop = 0; |
| 202 |
const size_t sizemax = strlen (base) + strlen (ext) + 10; |
| 203 |
|
| 204 |
if (path_size == 0) |
| 205 |
{ |
| 206 |
path_size = GetCurrentDirectory (sizeof temp_path, temp_path); |
| 207 |
path_is_dot = 1; |
| 208 |
} |
| 209 |
|
| 210 |
++uniq; |
| 211 |
if (uniq >= 0x10000 && !second_loop) |
| 212 |
{ |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
second_loop = 1; |
| 218 |
uniq = 1; |
| 219 |
} |
| 220 |
while (path_size > 0 && |
| 221 |
path_size + sizemax < sizeof temp_path && |
| 222 |
!(uniq >= 0x10000 && second_loop)) |
| 223 |
{ |
| 224 |
unsigned size = sprintf (temp_path + path_size, |
| 225 |
"%s%s-%x.%s", |
| 226 |
temp_path[path_size - 1] == '\\' ? "" : "\\", |
| 227 |
base, uniq, ext); |
| 228 |
HANDLE h = CreateFile (temp_path, |
| 229 |
GENERIC_READ | GENERIC_WRITE, |
| 230 |
0, |
| 231 |
NULL, |
| 232 |
CREATE_NEW, |
| 233 |
FILE_ATTRIBUTE_NORMAL | |
| 234 |
FILE_ATTRIBUTE_TEMPORARY, |
| 235 |
NULL); |
| 236 |
|
| 237 |
if (h == INVALID_HANDLE_VALUE) |
| 238 |
{ |
| 239 |
const DWORD er = GetLastError (); |
| 240 |
|
| 241 |
if (er == ERROR_FILE_EXISTS || er == ERROR_ALREADY_EXISTS) |
| 242 |
{ |
| 243 |
++uniq; |
| 244 |
if (uniq == 0x10000 && !second_loop) |
| 245 |
{ |
| 246 |
second_loop = 1; |
| 247 |
uniq = 1; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
else if (path_is_dot == 0) |
| 253 |
{ |
| 254 |
path_size = GetCurrentDirectory (sizeof temp_path, temp_path); |
| 255 |
path_is_dot = 1; |
| 256 |
} |
| 257 |
|
| 258 |
else |
| 259 |
{ |
| 260 |
error_string = map_windows32_error_to_string (er); |
| 261 |
break; |
| 262 |
} |
| 263 |
} |
| 264 |
else |
| 265 |
{ |
| 266 |
const unsigned final_size = path_size + size + 1; |
| 267 |
char *const path = xmalloc (final_size); |
| 268 |
memcpy (path, temp_path, final_size); |
| 269 |
*fd = _open_osfhandle ((intptr_t)h, 0); |
| 270 |
if (unixy) |
| 271 |
{ |
| 272 |
char *p; |
| 273 |
int ch; |
| 274 |
for (p = path; (ch = *p) != 0; ++p) |
| 275 |
if (ch == '\\') |
| 276 |
*p = '/'; |
| 277 |
} |
| 278 |
return path; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
*fd = -1; |
| 283 |
if (error_string == NULL) |
| 284 |
error_string = _("Cannot create a temporary file\n"); |
| 285 |
O (fatal, NILF, error_string); |
| 286 |
|
| 287 |
|
| 288 |
return NULL; |
| 289 |
} |
| 290 |
#endif |
| 291 |
|
| 292 |
|
| 293 |
int |
| 294 |
is_bourne_compatible_shell (const char *path) |
| 295 |
{ |
| 296 |
|
| 297 |
static const char *unix_shells[] = { |
| 298 |
"sh", |
| 299 |
"bash", |
| 300 |
"ksh", |
| 301 |
"rksh", |
| 302 |
"zsh", |
| 303 |
"ash", |
| 304 |
"dash", |
| 305 |
NULL |
| 306 |
}; |
| 307 |
const char **s; |
| 308 |
|
| 309 |
|
| 310 |
const char *name = strrchr (path, '/'); |
| 311 |
char *p = strrchr (path, '\\'); |
| 312 |
|
| 313 |
if (name && p) |
| 314 |
name = (name > p) ? name : p; |
| 315 |
else if (p) |
| 316 |
name = p; |
| 317 |
else if (!name) |
| 318 |
name = path; |
| 319 |
|
| 320 |
if (*name == '/' || *name == '\\') |
| 321 |
++name; |
| 322 |
|
| 323 |
|
| 324 |
for (s = unix_shells; *s != NULL; ++s) |
| 325 |
{ |
| 326 |
#if defined(WINDOWS32) || defined(__MSDOS__) |
| 327 |
size_t len = strlen (*s); |
| 328 |
if ((strlen (name) >= len && STOP_SET (name[len], MAP_DOT|MAP_NUL)) |
| 329 |
&& strncasecmp (name, *s, len) == 0) |
| 330 |
#else |
| 331 |
if (strcmp (name, *s) == 0) |
| 332 |
#endif |
| 333 |
return 1; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
return 0; |
| 338 |
} |
| 339 |
|
| 340 |
#ifdef POSIX |
| 341 |
extern sigset_t fatal_signal_set; |
| 342 |
|
| 343 |
static void |
| 344 |
block_sigs () |
| 345 |
{ |
| 346 |
sigprocmask (SIG_BLOCK, &fatal_signal_set, (sigset_t *) 0); |
| 347 |
} |
| 348 |
|
| 349 |
static void |
| 350 |
unblock_sigs () |
| 351 |
{ |
| 352 |
sigprocmask (SIG_UNBLOCK, &fatal_signal_set, (sigset_t *) 0); |
| 353 |
} |
| 354 |
|
| 355 |
void |
| 356 |
unblock_all_sigs () |
| 357 |
{ |
| 358 |
sigset_t empty; |
| 359 |
sigemptyset (&empty); |
| 360 |
sigprocmask (SIG_SETMASK, &empty, (sigset_t *) 0); |
| 361 |
} |
| 362 |
|
| 363 |
#elif defined(HAVE_SIGSETMASK) |
| 364 |
|
| 365 |
extern int fatal_signal_mask; |
| 366 |
|
| 367 |
static void |
| 368 |
block_sigs () |
| 369 |
{ |
| 370 |
sigblock (fatal_signal_mask); |
| 371 |
} |
| 372 |
|
| 373 |
static void |
| 374 |
unblock_sigs () |
| 375 |
{ |
| 376 |
sigsetmask (siggetmask (0) & ~fatal_signal_mask) |
| 377 |
} |
| 378 |
|
| 379 |
void |
| 380 |
unblock_all_sigs () |
| 381 |
{ |
| 382 |
sigsetmask (0); |
| 383 |
} |
| 384 |
|
| 385 |
#else |
| 386 |
|
| 387 |
#define block_sigs() |
| 388 |
#define unblock_sigs() |
| 389 |
|
| 390 |
void |
| 391 |
unblock_all_sigs () |
| 392 |
{ |
| 393 |
} |
| 394 |
|
| 395 |
#endif |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
static void |
| 402 |
child_error (child_t *p_child, target_stack_node_t *p_call_stack, |
| 403 |
int exit_code, int exit_sig, int coredump, int ignored) |
| 404 |
{ |
| 405 |
const char *pre = "*** "; |
| 406 |
const char *post = ""; |
| 407 |
const char *dump = ""; |
| 408 |
const struct file *f = p_child->file; |
| 409 |
|
| 410 |
if (ignored && run_silent) |
| 411 |
return; |
| 412 |
|
| 413 |
if (exit_sig && coredump) |
| 414 |
dump = _(" (core dumped)"); |
| 415 |
|
| 416 |
if (ignored) |
| 417 |
{ |
| 418 |
pre = ""; |
| 419 |
post = _(" (ignored)"); |
| 420 |
} |
| 421 |
|
| 422 |
OUTPUT_SET (&p_child->output); |
| 423 |
|
| 424 |
show_goal_error (); |
| 425 |
|
| 426 |
if (exit_sig == 0) |
| 427 |
err_with_stack(p_call_stack, |
| 428 |
_("%s[%s] error %d%s"), |
| 429 |
pre, f->name, exit_code, post); |
| 430 |
else |
| 431 |
err_with_stack(p_call_stack, "%s[%s] %s%s%s", |
| 432 |
pre, f->name, strsignal (exit_sig), dump, post); |
| 433 |
|
| 434 |
OUTPUT_UNSET (); |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
if (! (debugger_on_error & DEBUGGER_ON_ERROR) && debugger_enabled ) |
| 442 |
enter_debugger(p_call_stack, p_child->file, exit_code, DEBUG_ERROR_HIT); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
static unsigned int dead_children = 0; |
| 458 |
|
| 459 |
RETSIGTYPE |
| 460 |
child_handler (int sig UNUSED) |
| 461 |
{ |
| 462 |
++dead_children; |
| 463 |
|
| 464 |
jobserver_signal (); |
| 465 |
} |
| 466 |
|
| 467 |
extern pid_t shell_function_pid; |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
void |
| 477 |
reap_children (int block, int err, target_stack_node_t *p_call_stack) |
| 478 |
{ |
| 479 |
#ifndef WINDOWS32 |
| 480 |
WAIT_T status; |
| 481 |
#endif |
| 482 |
|
| 483 |
int reap_more = 1; |
| 484 |
|
| 485 |
#ifdef WAIT_NOHANG |
| 486 |
# define REAP_MORE reap_more |
| 487 |
#else |
| 488 |
# define REAP_MORE dead_children |
| 489 |
#endif |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
while ((children != 0 || shell_function_pid != 0) |
| 500 |
&& (block || REAP_MORE)) |
| 501 |
{ |
| 502 |
unsigned int remote = 0; |
| 503 |
pid_t pid; |
| 504 |
int exit_code, exit_sig, coredump; |
| 505 |
struct child *lastc, *c; |
| 506 |
int child_failed; |
| 507 |
int any_remote, any_local; |
| 508 |
int dontcare; |
| 509 |
|
| 510 |
if (err && block) |
| 511 |
{ |
| 512 |
static int printed = 0; |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
fflush (stdout); |
| 517 |
if (!printed) |
| 518 |
O (error, NILF, _("*** Waiting for unfinished jobs....")); |
| 519 |
printed = 1; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
if (dead_children > 0) |
| 540 |
--dead_children; |
| 541 |
|
| 542 |
any_remote = 0; |
| 543 |
any_local = shell_function_pid != 0; |
| 544 |
lastc = 0; |
| 545 |
for (c = children; c != 0; lastc = c, c = c->next) |
| 546 |
{ |
| 547 |
any_remote |= c->remote; |
| 548 |
any_local |= ! c->remote; |
| 549 |
|
| 550 |
|
| 551 |
if (c->pid < 0) |
| 552 |
{ |
| 553 |
exit_sig = 0; |
| 554 |
coredump = 0; |
| 555 |
|
| 556 |
exit_code = 127; |
| 557 |
goto process_child; |
| 558 |
} |
| 559 |
|
| 560 |
DB (DB_JOBS, (_("Live child %p (%s) PID %s %s\n"), |
| 561 |
c, c->file->name, pid2str (c->pid), |
| 562 |
c->remote ? _(" (remote)") : "")); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
if (any_remote) |
| 567 |
pid = remote_status (&exit_code, &exit_sig, &coredump, 0); |
| 568 |
else |
| 569 |
pid = 0; |
| 570 |
|
| 571 |
if (pid > 0) |
| 572 |
|
| 573 |
remote = 1; |
| 574 |
else if (pid < 0) |
| 575 |
{ |
| 576 |
|
| 577 |
#if !defined(__MSDOS__) && !defined(WINDOWS32) |
| 578 |
remote_status_lose: |
| 579 |
#endif |
| 580 |
pfatal_with_name ("remote_status"); |
| 581 |
} |
| 582 |
else |
| 583 |
{ |
| 584 |
|
| 585 |
#if !defined(__MSDOS__) && !defined(WINDOWS32) |
| 586 |
if (any_local) |
| 587 |
{ |
| 588 |
#ifdef WAIT_NOHANG |
| 589 |
if (!block) |
| 590 |
pid = WAIT_NOHANG (&status); |
| 591 |
else |
| 592 |
#endif |
| 593 |
EINTRLOOP (pid, wait (&status)); |
| 594 |
} |
| 595 |
else |
| 596 |
pid = 0; |
| 597 |
|
| 598 |
if (pid < 0) |
| 599 |
{ |
| 600 |
|
| 601 |
pfatal_with_name ("wait"); |
| 602 |
} |
| 603 |
else if (pid > 0) |
| 604 |
{ |
| 605 |
|
| 606 |
exit_code = WEXITSTATUS (status); |
| 607 |
exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0; |
| 608 |
coredump = WCOREDUMP (status); |
| 609 |
} |
| 610 |
else |
| 611 |
{ |
| 612 |
|
| 613 |
reap_more = 0; |
| 614 |
|
| 615 |
if (!block || !any_remote) |
| 616 |
break; |
| 617 |
|
| 618 |
|
| 619 |
pid = remote_status (&exit_code, &exit_sig, &coredump, 1); |
| 620 |
if (pid < 0) |
| 621 |
goto remote_status_lose; |
| 622 |
else if (pid == 0) |
| 623 |
|
| 624 |
break; |
| 625 |
|
| 626 |
|
| 627 |
remote = 1; |
| 628 |
} |
| 629 |
#endif |
| 630 |
|
| 631 |
#ifdef __MSDOS__ |
| 632 |
|
| 633 |
pid = dos_pid - 1; |
| 634 |
status = dos_status; |
| 635 |
exit_code = WEXITSTATUS (status); |
| 636 |
if (exit_code == 0xff) |
| 637 |
exit_code = -1; |
| 638 |
exit_sig = WIFSIGNALED (status) ? WTERMSIG (status) : 0; |
| 639 |
coredump = 0; |
| 640 |
#endif |
| 641 |
#ifdef WINDOWS32 |
| 642 |
{ |
| 643 |
HANDLE hPID; |
| 644 |
HANDLE hcTID, hcPID; |
| 645 |
DWORD dwWaitStatus = 0; |
| 646 |
exit_code = 0; |
| 647 |
exit_sig = 0; |
| 648 |
coredump = 0; |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
if (!main_thread) |
| 653 |
{ |
| 654 |
hcTID = GetCurrentThread (); |
| 655 |
hcPID = GetCurrentProcess (); |
| 656 |
if (!DuplicateHandle (hcPID, hcTID, hcPID, &main_thread, 0, |
| 657 |
FALSE, DUPLICATE_SAME_ACCESS)) |
| 658 |
{ |
| 659 |
DWORD e = GetLastError (); |
| 660 |
fprintf (stderr, |
| 661 |
"Determine main thread ID (Error %ld: %s)\n", |
| 662 |
e, map_windows32_error_to_string (e)); |
| 663 |
} |
| 664 |
else |
| 665 |
DB (DB_VERBOSE, ("Main thread handle = %p\n", main_thread)); |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
hPID = process_wait_for_any (block, &dwWaitStatus); |
| 670 |
if (hPID) |
| 671 |
{ |
| 672 |
|
| 673 |
int werr = process_last_err (hPID); |
| 674 |
|
| 675 |
|
| 676 |
exit_code = process_exit_code (hPID); |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
if (werr && exit_code > 0 && exit_code < WSABASEERR) |
| 682 |
fprintf (stderr, "make (e=%d): %s", exit_code, |
| 683 |
map_windows32_error_to_string (exit_code)); |
| 684 |
|
| 685 |
|
| 686 |
exit_sig = process_signal (hPID); |
| 687 |
|
| 688 |
|
| 689 |
process_cleanup (hPID); |
| 690 |
|
| 691 |
coredump = 0; |
| 692 |
} |
| 693 |
else if (dwWaitStatus == WAIT_FAILED) |
| 694 |
{ |
| 695 |
|
| 696 |
pfatal_with_name ("WaitForMultipleObjects"); |
| 697 |
} |
| 698 |
else if (dwWaitStatus == WAIT_TIMEOUT) |
| 699 |
{ |
| 700 |
|
| 701 |
reap_more = 0; |
| 702 |
break; |
| 703 |
} |
| 704 |
|
| 705 |
pid = (pid_t) hPID; |
| 706 |
} |
| 707 |
#endif |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
if (!remote && pid == shell_function_pid) |
| 712 |
{ |
| 713 |
shell_completed (exit_code, exit_sig); |
| 714 |
break; |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
lastc = 0; |
| 719 |
for (c = children; c != 0; lastc = c, c = c->next) |
| 720 |
if (c->pid == pid && c->remote == remote) |
| 721 |
break; |
| 722 |
|
| 723 |
if (c == 0) |
| 724 |
|
| 725 |
|
| 726 |
continue; |
| 727 |
|
| 728 |
DB (DB_JOBS, (exit_sig == 0 && exit_code == 0 |
| 729 |
? _("Reaping winning child %p PID %s %s\n") |
| 730 |
: _("Reaping losing child %p PID %s %s\n"), |
| 731 |
c, pid2str (c->pid), c->remote ? _(" (remote)") : "")); |
| 732 |
|
| 733 |
|
| 734 |
if (job_counter) |
| 735 |
--job_counter; |
| 736 |
|
| 737 |
process_child: |
| 738 |
|
| 739 |
#if defined(USE_POSIX_SPAWN) |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
if (exit_sig == 0 && exit_code == 127 && c->cmd_name) |
| 745 |
{ |
| 746 |
const char *e = NULL; |
| 747 |
struct stat st; |
| 748 |
int r; |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
EINTRLOOP(r, stat(c->cmd_name, &st)); |
| 756 |
if (r < 0) |
| 757 |
e = strerror (errno); |
| 758 |
else if (S_ISDIR(st.st_mode) || !(st.st_mode & S_IXUSR)) |
| 759 |
e = strerror (EACCES); |
| 760 |
else if (st.st_size == 0) |
| 761 |
e = strerror (ENOEXEC); |
| 762 |
|
| 763 |
if (e) |
| 764 |
OSS(error, NILF, "%s: %s", c->cmd_name, e); |
| 765 |
} |
| 766 |
#endif |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
if (exit_sig == 0 && exit_code == 0) |
| 771 |
child_failed = MAKE_SUCCESS; |
| 772 |
else if (exit_sig == 0 && exit_code == 1 && question_flag && c->recursive) |
| 773 |
child_failed = MAKE_TROUBLE; |
| 774 |
else |
| 775 |
child_failed = MAKE_FAILURE; |
| 776 |
|
| 777 |
if (c->sh_batch_file) |
| 778 |
{ |
| 779 |
int rm_status; |
| 780 |
|
| 781 |
DB (DB_JOBS, (_("Cleaning up temp batch file %s\n"), |
| 782 |
c->sh_batch_file)); |
| 783 |
|
| 784 |
errno = 0; |
| 785 |
rm_status = remove (c->sh_batch_file); |
| 786 |
if (rm_status) |
| 787 |
DB (DB_JOBS, (_("Cleaning up temp batch file %s failed (%d)\n"), |
| 788 |
c->sh_batch_file, errno)); |
| 789 |
|
| 790 |
|
| 791 |
free (c->sh_batch_file); |
| 792 |
c->sh_batch_file = NULL; |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
if (c->good_stdin) |
| 797 |
good_stdin_used = 0; |
| 798 |
|
| 799 |
dontcare = c->dontcare; |
| 800 |
|
| 801 |
if (exit_code == DEBUGGER_QUIT_RC && debugger_enabled) { |
| 802 |
if (job_slots_used > 0) --job_slots_used; |
| 803 |
c->file->update_status = 0; |
| 804 |
free_child (c); |
| 805 |
in_debugger = DEBUGGER_QUIT_RC; |
| 806 |
die(DEBUGGER_QUIT_RC); |
| 807 |
} |
| 808 |
|
| 809 |
if (child_failed && !c->noerror && !ignore_errors_flag) |
| 810 |
{ |
| 811 |
|
| 812 |
|
| 813 |
static int delete_on_error = -1; |
| 814 |
|
| 815 |
if (!dontcare && child_failed == MAKE_FAILURE) |
| 816 |
child_error (c, p_call_stack, exit_code, exit_sig, coredump, 0); |
| 817 |
|
| 818 |
c->file->update_status = child_failed == MAKE_FAILURE ? us_failed : us_question; |
| 819 |
if (delete_on_error == -1) |
| 820 |
{ |
| 821 |
struct file *f = lookup_file (".DELETE_ON_ERROR"); |
| 822 |
delete_on_error = f != 0 && f->is_target; |
| 823 |
} |
| 824 |
if (exit_sig != 0 || delete_on_error) |
| 825 |
delete_child_targets (c); |
| 826 |
} |
| 827 |
else |
| 828 |
{ |
| 829 |
if (child_failed) |
| 830 |
{ |
| 831 |
|
| 832 |
child_error (c, p_call_stack, exit_code, exit_sig, coredump, 1); |
| 833 |
child_failed = 0; |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
if (job_next_command (c)) |
| 838 |
{ |
| 839 |
if (handling_fatal_signal) |
| 840 |
{ |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
c->file->update_status = us_failed; |
| 846 |
} |
| 847 |
else |
| 848 |
{ |
| 849 |
#ifndef NO_OUTPUT_SYNC |
| 850 |
|
| 851 |
|
| 852 |
if (output_sync == OUTPUT_SYNC_LINE) |
| 853 |
output_dump (&c->output); |
| 854 |
#endif |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
c->remote = start_remote_job_p (0); |
| 860 |
start_job_command (c, p_call_stack); |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
unblock_sigs (); |
| 866 |
if (c->file->command_state == cs_running) |
| 867 |
|
| 868 |
|
| 869 |
continue; |
| 870 |
} |
| 871 |
|
| 872 |
if (c->file->update_status != us_success) |
| 873 |
|
| 874 |
delete_child_targets (c); |
| 875 |
} |
| 876 |
else |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
c->file->update_status = us_success; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
#ifndef NO_OUTPUT_SYNC |
| 886 |
|
| 887 |
output_dump (&c->output); |
| 888 |
#endif |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
if (! handling_fatal_signal) |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
notice_finished_file (c->file); |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
block_sigs (); |
| 907 |
|
| 908 |
if (c->pid > 0) |
| 909 |
{ |
| 910 |
DB (DB_JOBS, (_("Removing child %p PID %s%s from chain.\n"), |
| 911 |
c, pid2str (c->pid), c->remote ? _(" (remote)") : "")); |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
if (job_slots_used > 0) |
| 916 |
job_slots_used -= c->jobslot; |
| 917 |
|
| 918 |
|
| 919 |
if (lastc == 0) |
| 920 |
children = c->next; |
| 921 |
else |
| 922 |
lastc->next = c->next; |
| 923 |
|
| 924 |
{ |
| 925 |
|
| 926 |
|
| 927 |
file_t file; |
| 928 |
|
| 929 |
memcpy(&file, c->file, sizeof(file_t)); |
| 930 |
|
| 931 |
free_child (c); |
| 932 |
|
| 933 |
unblock_sigs (); |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
if (exit_code == DEBUGGER_QUIT_RC && debugger_enabled) { |
| 939 |
if (job_slots_used > 0) --job_slots_used; |
| 940 |
in_debugger = DEBUGGER_QUIT_RC; |
| 941 |
die(DEBUGGER_QUIT_RC); |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
if (!err && child_failed && !dontcare && !keep_going_flag && |
| 947 |
|
| 948 |
!handling_fatal_signal) { |
| 949 |
if ( (debugger_on_error & DEBUGGER_ON_FATAL) |
| 950 |
|| i_debugger_stepping || i_debugger_nexting ) |
| 951 |
enter_debugger(p_call_stack, &file, 2, DEBUG_ERROR_HIT); |
| 952 |
die (child_failed); |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
block = 0; |
| 958 |
} |
| 959 |
|
| 960 |
return; |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
static void |
| 966 |
free_child (struct child *child) |
| 967 |
{ |
| 968 |
output_close (&child->output); |
| 969 |
|
| 970 |
if (!jobserver_tokens) |
| 971 |
ONS (fatal, NILF, "INTERNAL: Freeing child %p (%s) but no tokens left!\n", |
| 972 |
child, child->file->name); |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
if (jobserver_enabled () && jobserver_tokens > 1) |
| 978 |
{ |
| 979 |
jobserver_release (1); |
| 980 |
DB (DB_JOBS, (_("Released token for child %p (%s).\n"), |
| 981 |
child, child->file->name)); |
| 982 |
} |
| 983 |
|
| 984 |
--jobserver_tokens; |
| 985 |
|
| 986 |
if (handling_fatal_signal) |
| 987 |
return; |
| 988 |
|
| 989 |
if (child->command_lines != 0) |
| 990 |
{ |
| 991 |
unsigned int i; |
| 992 |
for (i = 0; i < child->file->cmds->ncommand_lines; ++i) |
| 993 |
free (child->command_lines[i]); |
| 994 |
free (child->command_lines); |
| 995 |
} |
| 996 |
|
| 997 |
if (child->environment != 0) |
| 998 |
{ |
| 999 |
char **ep = child->environment; |
| 1000 |
while (*ep != 0) |
| 1001 |
free (*ep++); |
| 1002 |
free (child->environment); |
| 1003 |
} |
| 1004 |
|
| 1005 |
free (child->cmd_name); |
| 1006 |
free (child); |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
static void |
| 1018 |
start_job_command (child_t *child, |
| 1019 |
target_stack_node_t *p_call_stack) |
| 1020 |
{ |
| 1021 |
int flags; |
| 1022 |
char *p; |
| 1023 |
# define FREE_ARGV(_a) do{ if (_a) { free ((_a)[0]); free (_a); } }while(0) |
| 1024 |
char **argv; |
| 1025 |
|
| 1026 |
|
| 1027 |
if (!child->command_ptr) |
| 1028 |
goto next_command; |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
flags = (child->file->command_flags |
| 1033 |
| child->file->cmds->lines_flags[child->command_line - 1]); |
| 1034 |
|
| 1035 |
p = child->command_ptr; |
| 1036 |
child->noerror = ((flags & COMMANDS_NOERROR) != 0); |
| 1037 |
|
| 1038 |
while (*p != '\0') |
| 1039 |
{ |
| 1040 |
if (*p == '@') |
| 1041 |
flags |= COMMANDS_SILENT; |
| 1042 |
else if (*p == '+') |
| 1043 |
flags |= COMMANDS_RECURSE; |
| 1044 |
else if (*p == '-') |
| 1045 |
child->noerror = 1; |
| 1046 |
|
| 1047 |
else if (!ISBLANK (*p)) |
| 1048 |
break; |
| 1049 |
++p; |
| 1050 |
} |
| 1051 |
|
| 1052 |
child->recursive = ((flags & COMMANDS_RECURSE) != 0); |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
child->file->cmds->lines_flags[child->command_line - 1] |= flags & COMMANDS_RECURSE; |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
{ |
| 1065 |
char prefix = child->file->cmds->recipe_prefix; |
| 1066 |
char *p1, *p2; |
| 1067 |
p1 = p2 = p; |
| 1068 |
while (*p1 != '\0') |
| 1069 |
{ |
| 1070 |
*(p2++) = *p1; |
| 1071 |
if (p1[0] == '\n' && p1[1] == prefix) |
| 1072 |
++p1; |
| 1073 |
++p1; |
| 1074 |
} |
| 1075 |
*p2 = *p1; |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
{ |
| 1080 |
char *end = 0; |
| 1081 |
|
| 1082 |
argv = construct_command_argv (p, &end, child->file, |
| 1083 |
child->file->cmds->lines_flags[child->command_line - 1], |
| 1084 |
&child->sh_batch_file); |
| 1085 |
if (end == NULL) |
| 1086 |
child->command_ptr = NULL; |
| 1087 |
else |
| 1088 |
{ |
| 1089 |
*end++ = '\0'; |
| 1090 |
child->command_ptr = end; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
if (argv != 0 && question_flag && !(flags & COMMANDS_RECURSE)) |
| 1099 |
{ |
| 1100 |
FREE_ARGV (argv); |
| 1101 |
child->file->update_status = us_question; |
| 1102 |
notice_finished_file (child->file); |
| 1103 |
return; |
| 1104 |
} |
| 1105 |
|
| 1106 |
if (touch_flag && !(flags & COMMANDS_RECURSE)) |
| 1107 |
{ |
| 1108 |
|
| 1109 |
|
| 1110 |
FREE_ARGV (argv); |
| 1111 |
argv = 0; |
| 1112 |
} |
| 1113 |
|
| 1114 |
if (argv == 0) |
| 1115 |
{ |
| 1116 |
next_command: |
| 1117 |
#ifdef __MSDOS__ |
| 1118 |
execute_by_shell = 0; |
| 1119 |
#endif |
| 1120 |
|
| 1121 |
if (job_next_command (child)) |
| 1122 |
start_job_command (child, p_call_stack); |
| 1123 |
else |
| 1124 |
{ |
| 1125 |
|
| 1126 |
|
| 1127 |
set_command_state (child->file, cs_running); |
| 1128 |
child->file->update_status = us_success; |
| 1129 |
notice_finished_file (child->file); |
| 1130 |
} |
| 1131 |
|
| 1132 |
OUTPUT_UNSET(); |
| 1133 |
return; |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
child->output.syncout = output_sync && (output_sync == OUTPUT_SYNC_RECURSE |
| 1140 |
|| !(flags & COMMANDS_RECURSE)); |
| 1141 |
|
| 1142 |
OUTPUT_SET (&child->output); |
| 1143 |
|
| 1144 |
#ifndef NO_OUTPUT_SYNC |
| 1145 |
if (! child->output.syncout) |
| 1146 |
|
| 1147 |
|
| 1148 |
output_dump (&child->output); |
| 1149 |
#endif |
| 1150 |
|
| 1151 |
|
| 1152 |
{ |
| 1153 |
bool print_it = |
| 1154 |
(just_print_flag |
| 1155 |
|| (!(flags & COMMANDS_SILENT) && !run_silent) |
| 1156 |
|| (db_level & DB_SHELL)); |
| 1157 |
|
| 1158 |
if (print_it) { |
| 1159 |
if (db_level & DB_SHELL) { |
| 1160 |
char pid_str[20] = ">>"; |
| 1161 |
if (job_slots != 1) |
| 1162 |
snprintf(pid_str, sizeof(pid_str), "%d", child->pid); |
| 1163 |
OS (message, 0, "##>>>>>>>>>>>>>>>>>>>>>>>>>>%s>>>>>>>>>>>>>>>>>>>>>>>>>>>>", |
| 1164 |
pid_str); |
| 1165 |
OS (message, 0, "%s", p); |
| 1166 |
OS (message, 0, "##<<<<<<<<<<<<<<<<<<<<<<<<<<%s<<<<<<<<<<<<<<<<<<<<<<<<<<<<", |
| 1167 |
job_slots != 1 ? pid_str : "<<"); |
| 1168 |
} else |
| 1169 |
OS (message, 0, "%s", p); |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
++commands_started; |
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
if ( |
| 1189 |
#if defined __MSDOS__ |
| 1190 |
unixy_shell |
| 1191 |
#else |
| 1192 |
(argv[0] && is_bourne_compatible_shell (argv[0])) |
| 1193 |
#endif |
| 1194 |
&& (argv[1] && argv[1][0] == '-' |
| 1195 |
&& |
| 1196 |
((argv[1][1] == 'c' && argv[1][2] == '\0') |
| 1197 |
|| |
| 1198 |
(argv[1][1] == 'e' && argv[1][2] == 'c' && argv[1][3] == '\0'))) |
| 1199 |
&& (argv[2] && argv[2][0] == ':' && argv[2][1] == '\0') |
| 1200 |
&& argv[3] == NULL) |
| 1201 |
{ |
| 1202 |
FREE_ARGV (argv); |
| 1203 |
goto next_command; |
| 1204 |
} |
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
if (just_print_flag && !(flags & COMMANDS_RECURSE)) |
| 1209 |
{ |
| 1210 |
FREE_ARGV (argv); |
| 1211 |
goto next_command; |
| 1212 |
} |
| 1213 |
|
| 1214 |
|
| 1215 |
output_start (); |
| 1216 |
|
| 1217 |
p_stack_top = p_call_stack; |
| 1218 |
if (i_debugger_stepping) |
| 1219 |
enter_debugger(p_call_stack, child->file, 0, DEBUG_STEP_COMMAND); |
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
fflush (stdout); |
| 1224 |
fflush (stderr); |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
child->good_stdin = !good_stdin_used; |
| 1231 |
if (child->good_stdin) |
| 1232 |
good_stdin_used = 1; |
| 1233 |
|
| 1234 |
child->deleted = 0; |
| 1235 |
|
| 1236 |
|
| 1237 |
if (child->environment == 0) |
| 1238 |
child->environment = target_environment (child->file); |
| 1239 |
|
| 1240 |
#if !defined(__MSDOS__) && !defined(WINDOWS32) |
| 1241 |
|
| 1242 |
|
| 1243 |
if (child->remote) |
| 1244 |
{ |
| 1245 |
int is_remote, used_stdin; |
| 1246 |
pid_t id; |
| 1247 |
if (start_remote_job (argv, child->environment, |
| 1248 |
child->good_stdin ? 0 : get_bad_stdin (), |
| 1249 |
&is_remote, &id, &used_stdin)) |
| 1250 |
|
| 1251 |
|
| 1252 |
goto run_local; |
| 1253 |
else |
| 1254 |
{ |
| 1255 |
if (child->good_stdin && !used_stdin) |
| 1256 |
{ |
| 1257 |
child->good_stdin = 0; |
| 1258 |
good_stdin_used = 0; |
| 1259 |
} |
| 1260 |
child->remote = is_remote; |
| 1261 |
child->pid = id; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
else |
| 1265 |
{ |
| 1266 |
|
| 1267 |
|
| 1268 |
char **parent_environ; |
| 1269 |
|
| 1270 |
run_local: |
| 1271 |
block_sigs (); |
| 1272 |
|
| 1273 |
child->remote = 0; |
| 1274 |
|
| 1275 |
parent_environ = environ; |
| 1276 |
|
| 1277 |
jobserver_pre_child (flags & COMMANDS_RECURSE); |
| 1278 |
|
| 1279 |
child->pid = child_execute_job ((struct childbase *)child, |
| 1280 |
child->good_stdin, argv); |
| 1281 |
|
| 1282 |
environ = parent_environ; |
| 1283 |
jobserver_post_child (flags & COMMANDS_RECURSE); |
| 1284 |
} |
| 1285 |
|
| 1286 |
#else |
| 1287 |
#ifdef __MSDOS__ |
| 1288 |
{ |
| 1289 |
int proc_return; |
| 1290 |
|
| 1291 |
block_sigs (); |
| 1292 |
dos_status = 0; |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
if (execute_by_shell) |
| 1299 |
{ |
| 1300 |
char *cmdline = argv[0]; |
| 1301 |
|
| 1302 |
|
| 1303 |
char **parent_environ = environ; |
| 1304 |
|
| 1305 |
environ = child->environment; |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
if (unixy_shell) |
| 1310 |
{ |
| 1311 |
|
| 1312 |
|
| 1313 |
cmdline = argv[2]; |
| 1314 |
} |
| 1315 |
|
| 1316 |
dos_command_running = 1; |
| 1317 |
proc_return = system (cmdline); |
| 1318 |
environ = parent_environ; |
| 1319 |
execute_by_shell = 0; |
| 1320 |
} |
| 1321 |
else |
| 1322 |
{ |
| 1323 |
dos_command_running = 1; |
| 1324 |
proc_return = spawnvpe (P_WAIT, argv[0], argv, child->environment); |
| 1325 |
} |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
unblock_sigs (); |
| 1331 |
dos_command_running = 0; |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
if (proc_return == -1) |
| 1336 |
dos_status |= 0xff; |
| 1337 |
else |
| 1338 |
dos_status |= (proc_return & 0xff); |
| 1339 |
++dead_children; |
| 1340 |
child->pid = dos_pid++; |
| 1341 |
} |
| 1342 |
#endif |
| 1343 |
#ifdef WINDOWS32 |
| 1344 |
{ |
| 1345 |
HANDLE hPID; |
| 1346 |
char* arg0; |
| 1347 |
int outfd = FD_STDOUT; |
| 1348 |
int errfd = FD_STDERR; |
| 1349 |
|
| 1350 |
|
| 1351 |
arg0 = argv[0]; |
| 1352 |
if (arg0 && arg0[0] == '/' && arg0[1] == '/') |
| 1353 |
for ( ; arg0 && *arg0; arg0++) |
| 1354 |
if (*arg0 == '/') |
| 1355 |
*arg0 = '\\'; |
| 1356 |
|
| 1357 |
|
| 1358 |
sync_Path_environment (); |
| 1359 |
|
| 1360 |
#ifndef NO_OUTPUT_SYNC |
| 1361 |
|
| 1362 |
if (child->output.syncout) |
| 1363 |
{ |
| 1364 |
if (child->output.out >= 0) |
| 1365 |
outfd = child->output.out; |
| 1366 |
if (child->output.err >= 0) |
| 1367 |
errfd = child->output.err; |
| 1368 |
} |
| 1369 |
#else |
| 1370 |
outfd = errfd = -1; |
| 1371 |
#endif |
| 1372 |
hPID = process_easy (argv, child->environment, outfd, errfd); |
| 1373 |
|
| 1374 |
if (hPID != INVALID_HANDLE_VALUE) |
| 1375 |
child->pid = (pid_t) hPID; |
| 1376 |
else |
| 1377 |
{ |
| 1378 |
int i; |
| 1379 |
unblock_sigs (); |
| 1380 |
fprintf (stderr, |
| 1381 |
_("process_easy() failed to launch process (e=%ld)\n"), |
| 1382 |
process_last_err (hPID)); |
| 1383 |
for (i = 0; argv[i]; i++) |
| 1384 |
fprintf (stderr, "%s ", argv[i]); |
| 1385 |
fprintf (stderr, _("\nCounted %d args in failed launch\n"), i); |
| 1386 |
child->pid = -1; |
| 1387 |
} |
| 1388 |
} |
| 1389 |
#endif |
| 1390 |
#endif |
| 1391 |
|
| 1392 |
|
| 1393 |
if (child->pid >= 0) |
| 1394 |
++job_counter; |
| 1395 |
|
| 1396 |
|
| 1397 |
set_command_state (child->file, cs_running); |
| 1398 |
|
| 1399 |
|
| 1400 |
FREE_ARGV (argv); |
| 1401 |
|
| 1402 |
OUTPUT_UNSET(); |
| 1403 |
|
| 1404 |
#undef FREE_ARGV |
| 1405 |
} |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
static int |
| 1412 |
start_waiting_job (struct child *c, target_stack_node_t *p_call_stack) |
| 1413 |
{ |
| 1414 |
struct file *f = c->file; |
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
c->remote = start_remote_job_p (1); |
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
if (!c->remote |
| 1425 |
&& ((job_slots_used > 0 && load_too_high ()) |
| 1426 |
#ifdef WINDOWS32 |
| 1427 |
|| process_table_full () |
| 1428 |
#endif |
| 1429 |
)) |
| 1430 |
{ |
| 1431 |
|
| 1432 |
|
| 1433 |
set_command_state (f, cs_running); |
| 1434 |
c->next = waiting_jobs; |
| 1435 |
waiting_jobs = c; |
| 1436 |
return 0; |
| 1437 |
} |
| 1438 |
|
| 1439 |
|
| 1440 |
start_job_command (c, p_call_stack); |
| 1441 |
|
| 1442 |
switch (f->command_state) |
| 1443 |
{ |
| 1444 |
case cs_running: |
| 1445 |
c->next = children; |
| 1446 |
if (c->pid > 0) |
| 1447 |
{ |
| 1448 |
DB (DB_JOBS, (_("Putting child %p (%s) PID %s%s on the chain.\n"), |
| 1449 |
c, c->file->name, pid2str (c->pid), |
| 1450 |
c->remote ? _(" (remote)") : "")); |
| 1451 |
|
| 1452 |
++job_slots_used; |
| 1453 |
assert (c->jobslot == 0); |
| 1454 |
c->jobslot = 1; |
| 1455 |
} |
| 1456 |
children = c; |
| 1457 |
unblock_sigs (); |
| 1458 |
break; |
| 1459 |
|
| 1460 |
case cs_not_started: |
| 1461 |
|
| 1462 |
f->update_status = us_success; |
| 1463 |
|
| 1464 |
|
| 1465 |
case cs_finished: |
| 1466 |
notice_finished_file (f); |
| 1467 |
free_child (c); |
| 1468 |
break; |
| 1469 |
|
| 1470 |
default: |
| 1471 |
assert (f->command_state == cs_finished); |
| 1472 |
break; |
| 1473 |
} |
| 1474 |
|
| 1475 |
return 1; |
| 1476 |
} |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
void |
| 1481 |
new_job (struct file *file, target_stack_node_t *p_call_stack) |
| 1482 |
{ |
| 1483 |
struct commands *cmds = file->cmds; |
| 1484 |
struct child *c; |
| 1485 |
char **lines; |
| 1486 |
unsigned int i; |
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
start_waiting_jobs (p_call_stack); |
| 1491 |
|
| 1492 |
|
| 1493 |
reap_children (0, 0, p_call_stack); |
| 1494 |
|
| 1495 |
|
| 1496 |
chop_commands (cmds); |
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
c = xcalloc (sizeof (struct child)); |
| 1502 |
output_init (&c->output); |
| 1503 |
|
| 1504 |
c->file = file; |
| 1505 |
c->sh_batch_file = NULL; |
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
c->dontcare = file->dontcare; |
| 1510 |
|
| 1511 |
|
| 1512 |
OUTPUT_SET (&c->output); |
| 1513 |
|
| 1514 |
|
| 1515 |
lines = xmalloc (cmds->ncommand_lines * sizeof (char *)); |
| 1516 |
for (i = 0; i < cmds->ncommand_lines; ++i) |
| 1517 |
{ |
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
char *in, *out, *ref; |
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
in = out = cmds->command_lines[i]; |
| 1533 |
while ((ref = strchr (in, '$')) != 0) |
| 1534 |
{ |
| 1535 |
++ref; |
| 1536 |
|
| 1537 |
if (out != in) |
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
memmove (out, in, ref - in); |
| 1542 |
|
| 1543 |
|
| 1544 |
out += ref - in; |
| 1545 |
in = ref; |
| 1546 |
|
| 1547 |
if (*ref == '(' || *ref == '{') |
| 1548 |
{ |
| 1549 |
char openparen = *ref; |
| 1550 |
char closeparen = openparen == '(' ? ')' : '}'; |
| 1551 |
char *outref; |
| 1552 |
int count; |
| 1553 |
char *p; |
| 1554 |
|
| 1555 |
*out++ = *in++; |
| 1556 |
outref = out; |
| 1557 |
|
| 1558 |
|
| 1559 |
count = 0; |
| 1560 |
while (*in != '\0') |
| 1561 |
{ |
| 1562 |
if (*in == closeparen && --count < 0) |
| 1563 |
break; |
| 1564 |
else if (*in == '\\' && in[1] == '\n') |
| 1565 |
{ |
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
int quoted = 0; |
| 1571 |
for (p = in - 1; p > ref && *p == '\\'; --p) |
| 1572 |
quoted = !quoted; |
| 1573 |
|
| 1574 |
if (quoted) |
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
*out++ = *in++; |
| 1581 |
else |
| 1582 |
{ |
| 1583 |
|
| 1584 |
in += 2; |
| 1585 |
NEXT_TOKEN (in); |
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
while (out > outref && ISBLANK (out[-1])) |
| 1590 |
--out; |
| 1591 |
|
| 1592 |
|
| 1593 |
*out++ = ' '; |
| 1594 |
} |
| 1595 |
} |
| 1596 |
else |
| 1597 |
{ |
| 1598 |
if (*in == openparen) |
| 1599 |
++count; |
| 1600 |
|
| 1601 |
*out++ = *in++; |
| 1602 |
} |
| 1603 |
} |
| 1604 |
} |
| 1605 |
} |
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
if (out != in) |
| 1610 |
memmove (out, in, strlen (in) + 1); |
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
cmds->fileinfo.offset = i; |
| 1615 |
if (p_call_stack && p_call_stack->p_target) { |
| 1616 |
gmk_floc *p_floc = &(p_call_stack->p_target->floc); |
| 1617 |
p_floc->offset = i; |
| 1618 |
} |
| 1619 |
|
| 1620 |
lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i], |
| 1621 |
file); |
| 1622 |
} |
| 1623 |
|
| 1624 |
cmds->fileinfo.offset = 0; |
| 1625 |
c->command_lines = lines; |
| 1626 |
|
| 1627 |
|
| 1628 |
job_next_command (c); |
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
if (job_slots != 0) |
| 1634 |
while (job_slots_used == job_slots) |
| 1635 |
reap_children (1, 0, p_call_stack); |
| 1636 |
|
| 1637 |
#ifdef MAKE_JOBSERVER |
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
else if (jobserver_enabled ()) |
| 1651 |
while (1) |
| 1652 |
{ |
| 1653 |
int got_token; |
| 1654 |
|
| 1655 |
DB (DB_JOBS, ("Need a job token; we %shave children\n", |
| 1656 |
children ? "" : "don't ")); |
| 1657 |
|
| 1658 |
|
| 1659 |
if (!jobserver_tokens) |
| 1660 |
break; |
| 1661 |
|
| 1662 |
|
| 1663 |
jobserver_pre_acquire (); |
| 1664 |
|
| 1665 |
|
| 1666 |
reap_children (0, 0, p_call_stack); |
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
start_waiting_jobs (p_call_stack); |
| 1671 |
|
| 1672 |
|
| 1673 |
if (!jobserver_tokens) |
| 1674 |
break; |
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
if (!children) |
| 1679 |
fatal_err(p_call_stack, "INTERNAL: no children as we go to sleep on read\n"); |
| 1680 |
|
| 1681 |
|
| 1682 |
got_token = jobserver_acquire (waiting_jobs != NULL); |
| 1683 |
|
| 1684 |
|
| 1685 |
if (got_token == 1) |
| 1686 |
{ |
| 1687 |
DB (DB_JOBS, (_("Obtained token for child %p (%s).\n"), |
| 1688 |
c, c->file->name)); |
| 1689 |
break; |
| 1690 |
} |
| 1691 |
} |
| 1692 |
#endif |
| 1693 |
|
| 1694 |
++jobserver_tokens; |
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
if (db_level & DB_SHELL) |
| 1699 |
{ |
| 1700 |
char *newer = allocated_variable_expand_for_file ("$?", c->file); |
| 1701 |
const char *nm; |
| 1702 |
|
| 1703 |
if (! cmds->fileinfo.filenm) |
| 1704 |
nm = _("<builtin>"); |
| 1705 |
else |
| 1706 |
{ |
| 1707 |
char *n = alloca (strlen (cmds->fileinfo.filenm) + 1 + 11 + 1); |
| 1708 |
sprintf (n, "%s:%lu", cmds->fileinfo.filenm, cmds->fileinfo.lineno); |
| 1709 |
nm = n; |
| 1710 |
} |
| 1711 |
|
| 1712 |
if (newer[0] == '\0') |
| 1713 |
OSS (message, 0, |
| 1714 |
_("%s: target '%s' does not exist"), nm, c->file->name); |
| 1715 |
else |
| 1716 |
OSSS (message, 0, |
| 1717 |
_("%s: update target '%s' due to: %s"), nm, c->file->name, newer); |
| 1718 |
|
| 1719 |
free (newer); |
| 1720 |
} |
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
if (cmds->fileinfo.filenm) { |
| 1725 |
if (!file->floc.filenm) { |
| 1726 |
file->floc.filenm = cmds->fileinfo.filenm; |
| 1727 |
file->floc.lineno = cmds->fileinfo.lineno - 1; |
| 1728 |
if (!p_call_stack->p_target->floc.filenm) { |
| 1729 |
p_call_stack->p_target->floc.filenm = file->floc.filenm; |
| 1730 |
p_call_stack->p_target->floc.lineno = file->floc.lineno; |
| 1731 |
} |
| 1732 |
} |
| 1733 |
} |
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
(void) start_waiting_job (c, p_call_stack); |
| 1738 |
|
| 1739 |
if (job_slots == 1 || not_parallel) |
| 1740 |
|
| 1741 |
|
| 1742 |
while (file->command_state == cs_running) |
| 1743 |
reap_children (1, 0, p_call_stack); |
| 1744 |
|
| 1745 |
OUTPUT_UNSET (); |
| 1746 |
return; |
| 1747 |
} |
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
static int |
| 1753 |
job_next_command (struct child *child) |
| 1754 |
{ |
| 1755 |
while (child->command_ptr == 0 || *child->command_ptr == '\0') |
| 1756 |
{ |
| 1757 |
|
| 1758 |
if (child->command_line == child->file->cmds->ncommand_lines) |
| 1759 |
{ |
| 1760 |
|
| 1761 |
child->command_ptr = 0; |
| 1762 |
child->file->cmds->fileinfo.offset = 0; |
| 1763 |
return 0; |
| 1764 |
} |
| 1765 |
else |
| 1766 |
|
| 1767 |
child->command_ptr = child->command_lines[child->command_line++]; |
| 1768 |
} |
| 1769 |
|
| 1770 |
child->file->cmds->fileinfo.offset = child->command_line - 1; |
| 1771 |
return 1; |
| 1772 |
} |
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
#define LOAD_WEIGHT_A 0.25 |
| 1826 |
#define LOAD_WEIGHT_B 0.25 |
| 1827 |
|
| 1828 |
static int |
| 1829 |
load_too_high (void) |
| 1830 |
{ |
| 1831 |
#if defined(__MSDOS__) |
| 1832 |
return 1; |
| 1833 |
#else |
| 1834 |
static double last_sec; |
| 1835 |
static time_t last_now; |
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
#define PROC_FD_INIT -1 |
| 1846 |
static int proc_fd = PROC_FD_INIT; |
| 1847 |
|
| 1848 |
double load, guess; |
| 1849 |
time_t now; |
| 1850 |
|
| 1851 |
#ifdef WINDOWS32 |
| 1852 |
|
| 1853 |
if (process_table_full ()) |
| 1854 |
return 1; |
| 1855 |
#endif |
| 1856 |
|
| 1857 |
if (max_load_average < 0) |
| 1858 |
return 0; |
| 1859 |
|
| 1860 |
|
| 1861 |
#define LOADAVG "/proc/loadavg" |
| 1862 |
if (proc_fd == -2) |
| 1863 |
{ |
| 1864 |
EINTRLOOP (proc_fd, open (LOADAVG, O_RDONLY)); |
| 1865 |
if (proc_fd < 0) |
| 1866 |
DB (DB_JOBS, ("Using system load detection method.\n")); |
| 1867 |
else |
| 1868 |
{ |
| 1869 |
DB (DB_JOBS, ("Using " LOADAVG " load detection method.\n")); |
| 1870 |
fd_noinherit (proc_fd); |
| 1871 |
} |
| 1872 |
} |
| 1873 |
|
| 1874 |
|
| 1875 |
if (proc_fd >= 0) |
| 1876 |
{ |
| 1877 |
int r; |
| 1878 |
|
| 1879 |
EINTRLOOP (r, lseek (proc_fd, 0, SEEK_SET)); |
| 1880 |
if (r >= 0) |
| 1881 |
{ |
| 1882 |
#define PROC_LOADAVG_SIZE 64 |
| 1883 |
char avg[PROC_LOADAVG_SIZE+1]; |
| 1884 |
|
| 1885 |
EINTRLOOP (r, read (proc_fd, avg, PROC_LOADAVG_SIZE)); |
| 1886 |
if (r >= 0) |
| 1887 |
{ |
| 1888 |
const char *p; |
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
avg[r] = '\0'; |
| 1896 |
p = strchr (avg, ' '); |
| 1897 |
if (p) |
| 1898 |
p = strchr (p+1, ' '); |
| 1899 |
if (p) |
| 1900 |
p = strchr (p+1, ' '); |
| 1901 |
|
| 1902 |
if (p && ISDIGIT(p[1])) |
| 1903 |
{ |
| 1904 |
int cnt = atoi (p+1); |
| 1905 |
DB (DB_JOBS, ("Running: system = %d / make = %u (max requested = %f)\n", |
| 1906 |
cnt, job_slots_used, max_load_average)); |
| 1907 |
return (double)cnt > max_load_average; |
| 1908 |
} |
| 1909 |
|
| 1910 |
DB (DB_JOBS, ("Failed to parse " LOADAVG ": %s\n", avg)); |
| 1911 |
} |
| 1912 |
} |
| 1913 |
|
| 1914 |
|
| 1915 |
if (r < 0) |
| 1916 |
DB (DB_JOBS, ("Failed to read " LOADAVG ": %s\n", strerror (errno))); |
| 1917 |
|
| 1918 |
close (proc_fd); |
| 1919 |
proc_fd = -1; |
| 1920 |
} |
| 1921 |
|
| 1922 |
|
| 1923 |
make_access (); |
| 1924 |
if (getloadavg (&load, 1) != 1) |
| 1925 |
{ |
| 1926 |
static int lossage = -1; |
| 1927 |
|
| 1928 |
if (lossage == -1 || errno != lossage) |
| 1929 |
{ |
| 1930 |
if (errno == 0) |
| 1931 |
|
| 1932 |
O (error, NILF, |
| 1933 |
_("cannot enforce load limits on this operating system")); |
| 1934 |
else |
| 1935 |
perror_with_name (_("cannot enforce load limit: "), "getloadavg"); |
| 1936 |
} |
| 1937 |
lossage = errno; |
| 1938 |
load = 0; |
| 1939 |
} |
| 1940 |
user_access (); |
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
now = time (NULL); |
| 1945 |
if (last_now < now) |
| 1946 |
{ |
| 1947 |
if (last_now == now - 1) |
| 1948 |
last_sec = LOAD_WEIGHT_B * job_counter; |
| 1949 |
else |
| 1950 |
last_sec = 0.0; |
| 1951 |
|
| 1952 |
job_counter = 0; |
| 1953 |
last_now = now; |
| 1954 |
} |
| 1955 |
|
| 1956 |
|
| 1957 |
guess = load + (LOAD_WEIGHT_A * (job_counter + last_sec)); |
| 1958 |
|
| 1959 |
DB (DB_JOBS, ("Estimated system load = %f (actual = %f) (max requested = %f)\n", |
| 1960 |
guess, load, max_load_average)); |
| 1961 |
|
| 1962 |
return guess >= max_load_average; |
| 1963 |
#endif |
| 1964 |
} |
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
void |
| 1969 |
start_waiting_jobs (target_stack_node_t *p_call_stack) |
| 1970 |
{ |
| 1971 |
struct child *job; |
| 1972 |
|
| 1973 |
if (waiting_jobs == 0) |
| 1974 |
return; |
| 1975 |
|
| 1976 |
do |
| 1977 |
{ |
| 1978 |
|
| 1979 |
reap_children (0, 0, NULL); |
| 1980 |
|
| 1981 |
|
| 1982 |
job = waiting_jobs; |
| 1983 |
waiting_jobs = job->next; |
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
} |
| 1988 |
while (start_waiting_job (job, p_call_stack) && waiting_jobs != 0); |
| 1989 |
|
| 1990 |
return; |
| 1991 |
} |
| 1992 |
|
| 1993 |
#ifndef WINDOWS32 |
| 1994 |
|
| 1995 |
#if !defined (__MSDOS__) |
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
pid_t |
| 2001 |
child_execute_job (struct childbase *child, int good_stdin, char **argv) |
| 2002 |
{ |
| 2003 |
const int fdin = good_stdin ? FD_STDIN : get_bad_stdin (); |
| 2004 |
int fdout = FD_STDOUT; |
| 2005 |
int fderr = FD_STDERR; |
| 2006 |
pid_t pid; |
| 2007 |
int r; |
| 2008 |
#if defined(USE_POSIX_SPAWN) |
| 2009 |
char *cmd; |
| 2010 |
posix_spawnattr_t attr; |
| 2011 |
posix_spawn_file_actions_t fa; |
| 2012 |
short flags = 0; |
| 2013 |
#endif |
| 2014 |
|
| 2015 |
|
| 2016 |
if (child->output.syncout) |
| 2017 |
{ |
| 2018 |
if (child->output.out >= 0) |
| 2019 |
fdout = child->output.out; |
| 2020 |
if (child->output.err >= 0) |
| 2021 |
fderr = child->output.err; |
| 2022 |
} |
| 2023 |
|
| 2024 |
#if !defined(USE_POSIX_SPAWN) |
| 2025 |
|
| 2026 |
pid = vfork(); |
| 2027 |
if (pid != 0) |
| 2028 |
return pid; |
| 2029 |
|
| 2030 |
|
| 2031 |
unblock_all_sigs (); |
| 2032 |
|
| 2033 |
#ifdef SET_STACK_SIZE |
| 2034 |
|
| 2035 |
if (stack_limit.rlim_cur) |
| 2036 |
setrlimit (RLIMIT_STACK, &stack_limit); |
| 2037 |
#endif |
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
if (fdin >= 0 && fdin != FD_STDIN) |
| 2042 |
EINTRLOOP (r, dup2 (fdin, FD_STDIN)); |
| 2043 |
if (fdout != FD_STDOUT) |
| 2044 |
EINTRLOOP (r, dup2 (fdout, FD_STDOUT)); |
| 2045 |
if (fderr != FD_STDERR) |
| 2046 |
EINTRLOOP (r, dup2 (fderr, FD_STDERR)); |
| 2047 |
|
| 2048 |
|
| 2049 |
exec_command (argv, child->environment); |
| 2050 |
|
| 2051 |
#else |
| 2052 |
|
| 2053 |
if ((r = posix_spawnattr_init (&attr)) != 0) |
| 2054 |
goto done; |
| 2055 |
|
| 2056 |
if ((r = posix_spawn_file_actions_init (&fa)) != 0) |
| 2057 |
{ |
| 2058 |
posix_spawnattr_destroy (&attr); |
| 2059 |
goto done; |
| 2060 |
} |
| 2061 |
|
| 2062 |
|
| 2063 |
#ifdef HAVE_POSIX_SPAWNATTR_SETSIGMASK |
| 2064 |
{ |
| 2065 |
sigset_t mask; |
| 2066 |
sigemptyset (&mask); |
| 2067 |
r = posix_spawnattr_setsigmask (&attr, &mask); |
| 2068 |
if (r != 0) |
| 2069 |
goto cleanup; |
| 2070 |
flags |= POSIX_SPAWN_SETSIGMASK; |
| 2071 |
} |
| 2072 |
#endif |
| 2073 |
|
| 2074 |
|
| 2075 |
#ifdef POSIX_SPAWN_USEVFORK |
| 2076 |
flags |= POSIX_SPAWN_USEVFORK; |
| 2077 |
#endif |
| 2078 |
|
| 2079 |
|
| 2080 |
|
| 2081 |
if (fdin >= 0 && fdin != FD_STDIN) |
| 2082 |
if ((r = posix_spawn_file_actions_adddup2 (&fa, fdin, FD_STDIN)) != 0) |
| 2083 |
goto cleanup; |
| 2084 |
if (fdout != FD_STDOUT) |
| 2085 |
if ((r = posix_spawn_file_actions_adddup2 (&fa, fdout, FD_STDOUT)) != 0) |
| 2086 |
goto cleanup; |
| 2087 |
if (fderr != FD_STDERR) |
| 2088 |
if ((r = posix_spawn_file_actions_adddup2 (&fa, fderr, FD_STDERR)) != 0) |
| 2089 |
goto cleanup; |
| 2090 |
|
| 2091 |
|
| 2092 |
flags |= POSIX_SPAWN_RESETIDS; |
| 2093 |
|
| 2094 |
|
| 2095 |
if ((r = posix_spawnattr_setflags (&attr, flags)) != 0) |
| 2096 |
goto cleanup; |
| 2097 |
|
| 2098 |
|
| 2099 |
{ |
| 2100 |
const char *p = NULL; |
| 2101 |
char **pp; |
| 2102 |
|
| 2103 |
for (pp = child->environment; *pp != NULL; ++pp) |
| 2104 |
if ((*pp)[0] == 'P' && (*pp)[1] == 'A' && (*pp)[2] == 'T' |
| 2105 |
&& (*pp)[3] == 'H' &&(*pp)[4] == '=') |
| 2106 |
{ |
| 2107 |
p = (*pp) + 5; |
| 2108 |
break; |
| 2109 |
} |
| 2110 |
|
| 2111 |
cmd = (char *)find_in_given_path (argv[0], p, 0); |
| 2112 |
} |
| 2113 |
|
| 2114 |
if (!cmd) |
| 2115 |
{ |
| 2116 |
r = errno; |
| 2117 |
goto cleanup; |
| 2118 |
} |
| 2119 |
|
| 2120 |
|
| 2121 |
while ((r = posix_spawn (&pid, cmd, &fa, &attr, argv, |
| 2122 |
child->environment)) == EINTR) |
| 2123 |
; |
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
if (r == ENOEXEC) |
| 2129 |
{ |
| 2130 |
char **nargv; |
| 2131 |
char **pp; |
| 2132 |
size_t l = 0; |
| 2133 |
|
| 2134 |
for (pp = argv; *pp != NULL; ++pp) |
| 2135 |
++l; |
| 2136 |
|
| 2137 |
nargv = xmalloc (sizeof (char *) * (l + 3)); |
| 2138 |
nargv[0] = (char *)default_shell; |
| 2139 |
nargv[1] = cmd; |
| 2140 |
memcpy (&nargv[2], &argv[1], sizeof (char *) * l); |
| 2141 |
|
| 2142 |
while ((r = posix_spawn (&pid, nargv[0], &fa, &attr, nargv, |
| 2143 |
child->environment)) == EINTR) |
| 2144 |
; |
| 2145 |
|
| 2146 |
free (nargv); |
| 2147 |
} |
| 2148 |
|
| 2149 |
if (r == 0) |
| 2150 |
{ |
| 2151 |
|
| 2152 |
free (child->cmd_name); |
| 2153 |
if (cmd != argv[0]) |
| 2154 |
child->cmd_name = cmd; |
| 2155 |
else |
| 2156 |
child->cmd_name = xstrdup(cmd); |
| 2157 |
} |
| 2158 |
|
| 2159 |
cleanup: |
| 2160 |
posix_spawn_file_actions_destroy (&fa); |
| 2161 |
posix_spawnattr_destroy (&attr); |
| 2162 |
|
| 2163 |
done: |
| 2164 |
if (r != 0) |
| 2165 |
pid = -1; |
| 2166 |
|
| 2167 |
#endif |
| 2168 |
|
| 2169 |
if (pid < 0) |
| 2170 |
OSS (error, NILF, "%s: %s", argv[0], strerror (r)); |
| 2171 |
|
| 2172 |
return pid; |
| 2173 |
} |
| 2174 |
#endif |
| 2175 |
#endif |
| 2176 |
|
| 2177 |
|
| 2178 |
|
| 2179 |
void |
| 2180 |
exec_command (char **argv, char **envp) |
| 2181 |
{ |
| 2182 |
#ifdef WINDOWS32 |
| 2183 |
HANDLE hPID; |
| 2184 |
HANDLE hWaitPID; |
| 2185 |
int exit_code = EXIT_FAILURE; |
| 2186 |
|
| 2187 |
|
| 2188 |
sync_Path_environment (); |
| 2189 |
|
| 2190 |
|
| 2191 |
hPID = process_easy (argv, envp, -1, -1); |
| 2192 |
|
| 2193 |
|
| 2194 |
if (hPID == INVALID_HANDLE_VALUE) |
| 2195 |
{ |
| 2196 |
int i; |
| 2197 |
fprintf (stderr, _("process_easy() failed to launch process (e=%ld)\n"), |
| 2198 |
process_last_err (hPID)); |
| 2199 |
for (i = 0; argv[i]; i++) |
| 2200 |
fprintf (stderr, "%s ", argv[i]); |
| 2201 |
fprintf (stderr, _("\nCounted %d args in failed launch\n"), i); |
| 2202 |
exit (EXIT_FAILURE); |
| 2203 |
} |
| 2204 |
|
| 2205 |
|
| 2206 |
hWaitPID = process_wait_for_any (1, 0); |
| 2207 |
while (hWaitPID) |
| 2208 |
{ |
| 2209 |
|
| 2210 |
int err = process_last_err (hWaitPID); |
| 2211 |
|
| 2212 |
|
| 2213 |
exit_code = process_exit_code (hWaitPID); |
| 2214 |
|
| 2215 |
if (err) |
| 2216 |
fprintf (stderr, "make (e=%d, rc=%d): %s", |
| 2217 |
err, exit_code, map_windows32_error_to_string (err)); |
| 2218 |
|
| 2219 |
|
| 2220 |
process_cleanup (hWaitPID); |
| 2221 |
|
| 2222 |
|
| 2223 |
if (hWaitPID == hPID) |
| 2224 |
break; |
| 2225 |
else |
| 2226 |
{ |
| 2227 |
char *pidstr = xstrdup (pid2str ((pid_t)hWaitPID)); |
| 2228 |
|
| 2229 |
fprintf (stderr, |
| 2230 |
_("make reaped child pid %s, still waiting for pid %s\n"), |
| 2231 |
pidstr, pid2str ((pid_t)hPID)); |
| 2232 |
free (pidstr); |
| 2233 |
} |
| 2234 |
} |
| 2235 |
|
| 2236 |
|
| 2237 |
exit (exit_code); |
| 2238 |
|
| 2239 |
#else |
| 2240 |
|
| 2241 |
|
| 2242 |
child_access (); |
| 2243 |
|
| 2244 |
|
| 2245 |
environ = envp; |
| 2246 |
execvp (argv[0], argv); |
| 2247 |
|
| 2248 |
switch (errno) |
| 2249 |
{ |
| 2250 |
case ENOENT: |
| 2251 |
OSS (error, NILF, "%s: %s", argv[0], strerror (errno)); |
| 2252 |
break; |
| 2253 |
case ENOEXEC: |
| 2254 |
{ |
| 2255 |
|
| 2256 |
const char *shell; |
| 2257 |
char **new_argv; |
| 2258 |
int argc; |
| 2259 |
int i=1; |
| 2260 |
|
| 2261 |
shell = getenv ("SHELL"); |
| 2262 |
if (shell == 0) |
| 2263 |
shell = default_shell; |
| 2264 |
|
| 2265 |
argc = 1; |
| 2266 |
while (argv[argc] != 0) |
| 2267 |
++argc; |
| 2268 |
|
| 2269 |
new_argv = alloca ((1 + argc + 1) * sizeof (char *)); |
| 2270 |
new_argv[0] = (char *)shell; |
| 2271 |
|
| 2272 |
new_argv[i] = argv[0]; |
| 2273 |
while (argc > 0) |
| 2274 |
{ |
| 2275 |
new_argv[i + argc] = argv[argc]; |
| 2276 |
--argc; |
| 2277 |
} |
| 2278 |
|
| 2279 |
execvp (shell, new_argv); |
| 2280 |
OSS (error, NILF, "%s: %s", new_argv[0], strerror (errno)); |
| 2281 |
break; |
| 2282 |
} |
| 2283 |
|
| 2284 |
default: |
| 2285 |
OSS (error, NILF, "%s: %s", argv[0], strerror (errno)); |
| 2286 |
break; |
| 2287 |
} |
| 2288 |
|
| 2289 |
_exit (127); |
| 2290 |
#endif |
| 2291 |
} |
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
static char ** |
| 2312 |
construct_command_argv_internal (char *line, char **restp, const char *shell, |
| 2313 |
const char *shellflags, const char *ifs, |
| 2314 |
int flags, char **batch_filename UNUSED) |
| 2315 |
{ |
| 2316 |
#ifdef __MSDOS__ |
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
|
| 2325 |
|
| 2326 |
|
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
|
| 2331 |
|
| 2332 |
|
| 2333 |
|
| 2334 |
|
| 2335 |
|
| 2336 |
|
| 2337 |
|
| 2338 |
|
| 2339 |
static const char *sh_chars_dos = "*?[];|<>%^&()"; |
| 2340 |
static const char *sh_cmds_dos[] = |
| 2341 |
{ "break", "call", "cd", "chcp", "chdir", "cls", "copy", "ctty", "date", |
| 2342 |
"del", "dir", "echo", "erase", "exit", "for", "goto", "if", "md", |
| 2343 |
"mkdir", "path", "pause", "prompt", "rd", "rmdir", "rem", "ren", |
| 2344 |
"rename", "set", "shift", "time", "type", "ver", "verify", "vol", ":", |
| 2345 |
0 }; |
| 2346 |
|
| 2347 |
static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^"; |
| 2348 |
static const char *sh_cmds_sh[] = |
| 2349 |
{ "cd", "echo", "eval", "exec", "exit", "login", "logout", "set", "umask", |
| 2350 |
"wait", "while", "for", "case", "if", ":", ".", "break", "continue", |
| 2351 |
"export", "read", "readonly", "shift", "times", "trap", "switch", |
| 2352 |
"unset", "ulimit", "command", 0 }; |
| 2353 |
|
| 2354 |
const char *sh_chars; |
| 2355 |
const char **sh_cmds; |
| 2356 |
|
| 2357 |
#elif defined (WINDOWS32) |
| 2358 |
|
| 2359 |
|
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
|
| 2364 |
|
| 2365 |
static const char *sh_chars_dos = "|&<>"; |
| 2366 |
static const char *sh_cmds_dos[] = |
| 2367 |
{ "assoc", "break", "call", "cd", "chcp", "chdir", "cls", "color", "copy", |
| 2368 |
"ctty", "date", "del", "dir", "echo", "echo.", "endlocal", "erase", |
| 2369 |
"exit", "for", "ftype", "goto", "if", "if", "md", "mkdir", "move", |
| 2370 |
"path", "pause", "prompt", "rd", "rem", "ren", "rename", "rmdir", |
| 2371 |
"set", "setlocal", "shift", "time", "title", "type", "ver", "verify", |
| 2372 |
"vol", ":", 0 }; |
| 2373 |
|
| 2374 |
static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^"; |
| 2375 |
static const char *sh_cmds_sh[] = |
| 2376 |
{ "cd", "eval", "exec", "exit", "login", "logout", "set", "umask", "wait", |
| 2377 |
"while", "for", "case", "if", ":", ".", "break", "continue", "export", |
| 2378 |
"read", "readonly", "shift", "times", "trap", "switch", "test", "command", |
| 2379 |
#ifdef BATCH_MODE_ONLY_SHELL |
| 2380 |
"echo", |
| 2381 |
#endif |
| 2382 |
0 }; |
| 2383 |
|
| 2384 |
const char *sh_chars; |
| 2385 |
const char **sh_cmds; |
| 2386 |
#else |
| 2387 |
static const char *sh_chars = "#;\"*?[]&|<>(){}$`^~!"; |
| 2388 |
static const char *sh_cmds[] = |
| 2389 |
{ ".", ":", "alias", "bg", "break", "case", "cd", "command", "continue", |
| 2390 |
"eval", "exec", "exit", "export", "fc", "fg", "for", "getopts", "hash", |
| 2391 |
"if", "jobs", "login", "logout", "read", "readonly", "return", "set", |
| 2392 |
"shift", "test", "times", "trap", "type", "ulimit", "umask", "unalias", |
| 2393 |
"unset", "wait", "while", 0 }; |
| 2394 |
|
| 2395 |
# ifdef HAVE_DOS_PATHS |
| 2396 |
|
| 2397 |
|
| 2398 |
|
| 2399 |
|
| 2400 |
static const char *sh_chars_sh = "#;\"*?[]&|<>(){}$`^~!"; |
| 2401 |
# endif |
| 2402 |
#endif |
| 2403 |
size_t i; |
| 2404 |
char *p; |
| 2405 |
#ifndef NDEBUG |
| 2406 |
char *end; |
| 2407 |
#endif |
| 2408 |
char *ap; |
| 2409 |
const char *cap; |
| 2410 |
const char *cp; |
| 2411 |
int instring, word_has_equals, seen_nonequals, last_argument_was_empty; |
| 2412 |
char **new_argv = 0; |
| 2413 |
char *argstr = 0; |
| 2414 |
#ifdef WINDOWS32 |
| 2415 |
int slow_flag = 0; |
| 2416 |
|
| 2417 |
if (!unixy_shell) |
| 2418 |
{ |
| 2419 |
sh_cmds = sh_cmds_dos; |
| 2420 |
sh_chars = sh_chars_dos; |
| 2421 |
} |
| 2422 |
else |
| 2423 |
{ |
| 2424 |
sh_cmds = sh_cmds_sh; |
| 2425 |
sh_chars = sh_chars_sh; |
| 2426 |
} |
| 2427 |
#endif |
| 2428 |
|
| 2429 |
if (restp != NULL) |
| 2430 |
*restp = NULL; |
| 2431 |
|
| 2432 |
|
| 2433 |
while (ISBLANK (*line)) |
| 2434 |
++line; |
| 2435 |
if (*line == '\0') |
| 2436 |
return 0; |
| 2437 |
|
| 2438 |
if (shellflags == 0) |
| 2439 |
shellflags = posix_pedantic ? "-ec" : "-c"; |
| 2440 |
|
| 2441 |
|
| 2442 |
if (shell == 0) |
| 2443 |
shell = default_shell; |
| 2444 |
#ifdef WINDOWS32 |
| 2445 |
else if (strcmp (shell, default_shell)) |
| 2446 |
{ |
| 2447 |
char *s1 = _fullpath (NULL, shell, 0); |
| 2448 |
char *s2 = _fullpath (NULL, default_shell, 0); |
| 2449 |
|
| 2450 |
slow_flag = strcmp ((s1 ? s1 : ""), (s2 ? s2 : "")); |
| 2451 |
|
| 2452 |
free (s1); |
| 2453 |
free (s2); |
| 2454 |
} |
| 2455 |
if (slow_flag) |
| 2456 |
goto slow; |
| 2457 |
#else |
| 2458 |
#if defined (__MSDOS__) |
| 2459 |
else if (strcasecmp (shell, default_shell)) |
| 2460 |
{ |
| 2461 |
extern int _is_unixy_shell (const char *_path); |
| 2462 |
|
| 2463 |
DB (DB_BASIC, (_("$SHELL changed (was '%s', now '%s')\n"), |
| 2464 |
default_shell, shell)); |
| 2465 |
unixy_shell = _is_unixy_shell (shell); |
| 2466 |
|
| 2467 |
|
| 2468 |
default_shell = xstrdup (shell); |
| 2469 |
} |
| 2470 |
if (unixy_shell) |
| 2471 |
{ |
| 2472 |
sh_chars = sh_chars_sh; |
| 2473 |
sh_cmds = sh_cmds_sh; |
| 2474 |
} |
| 2475 |
else |
| 2476 |
{ |
| 2477 |
sh_chars = sh_chars_dos; |
| 2478 |
sh_cmds = sh_cmds_dos; |
| 2479 |
} |
| 2480 |
#else |
| 2481 |
else if (strcmp (shell, default_shell)) |
| 2482 |
goto slow; |
| 2483 |
#endif |
| 2484 |
#endif |
| 2485 |
|
| 2486 |
if (ifs) |
| 2487 |
for (cap = ifs; *cap != '\0'; ++cap) |
| 2488 |
if (*cap != ' ' && *cap != '\t' && *cap != '\n') |
| 2489 |
goto slow; |
| 2490 |
|
| 2491 |
if (shellflags) |
| 2492 |
if (shellflags[0] != '-' |
| 2493 |
|| ((shellflags[1] != 'c' || shellflags[2] != '\0') |
| 2494 |
&& (shellflags[1] != 'e' || shellflags[2] != 'c' || shellflags[3] != '\0'))) |
| 2495 |
goto slow; |
| 2496 |
|
| 2497 |
i = strlen (line) + 1; |
| 2498 |
|
| 2499 |
|
| 2500 |
new_argv = xmalloc (i * sizeof (char *)); |
| 2501 |
|
| 2502 |
|
| 2503 |
ap = new_argv[0] = argstr = xmalloc (i); |
| 2504 |
#ifndef NDEBUG |
| 2505 |
end = ap + i; |
| 2506 |
#endif |
| 2507 |
|
| 2508 |
|
| 2509 |
i = 0; |
| 2510 |
instring = word_has_equals = seen_nonequals = last_argument_was_empty = 0; |
| 2511 |
for (p = line; *p != '\0'; ++p) |
| 2512 |
{ |
| 2513 |
assert (ap <= end); |
| 2514 |
|
| 2515 |
if (instring) |
| 2516 |
{ |
| 2517 |
|
| 2518 |
|
| 2519 |
if (*p == instring) |
| 2520 |
{ |
| 2521 |
instring = 0; |
| 2522 |
if (ap == new_argv[0] || *(ap-1) == '\0') |
| 2523 |
last_argument_was_empty = 1; |
| 2524 |
} |
| 2525 |
else if (*p == '\\' && p[1] == '\n') |
| 2526 |
{ |
| 2527 |
|
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
|
| 2532 |
if (instring == '"' |
| 2533 |
#if defined (__MSDOS__) || defined (WINDOWS32) |
| 2534 |
|| !unixy_shell |
| 2535 |
#endif |
| 2536 |
) |
| 2537 |
++p; |
| 2538 |
else |
| 2539 |
{ |
| 2540 |
*(ap++) = *(p++); |
| 2541 |
*(ap++) = *p; |
| 2542 |
} |
| 2543 |
} |
| 2544 |
else if (*p == '\n' && restp != NULL) |
| 2545 |
{ |
| 2546 |
|
| 2547 |
*restp = p; |
| 2548 |
goto end_of_line; |
| 2549 |
} |
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
|
| 2554 |
else if (instring == '"' && strchr ("\\$`", *p) != 0 && unixy_shell) |
| 2555 |
goto slow; |
| 2556 |
#ifdef WINDOWS32 |
| 2557 |
|
| 2558 |
|
| 2559 |
else if (instring == '"' && strchr ("*?", *p) != 0 && !unixy_shell) |
| 2560 |
goto slow; |
| 2561 |
else if (instring == '"' && strncmp (p, "\\\"", 2) == 0) |
| 2562 |
*ap++ = *++p; |
| 2563 |
#endif |
| 2564 |
else |
| 2565 |
*ap++ = *p; |
| 2566 |
} |
| 2567 |
else if (strchr (sh_chars, *p) != 0) |
| 2568 |
|
| 2569 |
goto slow; |
| 2570 |
else if (one_shell && *p == '\n') |
| 2571 |
|
| 2572 |
goto slow; |
| 2573 |
#ifdef __MSDOS__ |
| 2574 |
else if (*p == '.' && p[1] == '.' && p[2] == '.' && p[3] != '.') |
| 2575 |
|
| 2576 |
goto slow; |
| 2577 |
#endif |
| 2578 |
else |
| 2579 |
|
| 2580 |
switch (*p) |
| 2581 |
{ |
| 2582 |
case '=': |
| 2583 |
|
| 2584 |
|
| 2585 |
|
| 2586 |
|
| 2587 |
if (! seen_nonequals && unixy_shell) |
| 2588 |
goto slow; |
| 2589 |
word_has_equals = 1; |
| 2590 |
*ap++ = '='; |
| 2591 |
break; |
| 2592 |
|
| 2593 |
case '\\': |
| 2594 |
|
| 2595 |
|
| 2596 |
if (p[1] == '\n') |
| 2597 |
{ |
| 2598 |
|
| 2599 |
++p; |
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
if (ap == new_argv[i]) |
| 2604 |
while (ISBLANK (p[1])) |
| 2605 |
++p; |
| 2606 |
} |
| 2607 |
#ifdef WINDOWS32 |
| 2608 |
|
| 2609 |
|
| 2610 |
else if (ISSPACE (p[1]) && !unixy_shell) |
| 2611 |
{ |
| 2612 |
*ap++ = *p; |
| 2613 |
break; |
| 2614 |
} |
| 2615 |
#endif |
| 2616 |
else if (p[1] != '\0') |
| 2617 |
{ |
| 2618 |
#ifdef HAVE_DOS_PATHS |
| 2619 |
|
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
|
| 2624 |
|
| 2625 |
#ifdef __MSDOS__ |
| 2626 |
|
| 2627 |
|
| 2628 |
if (strneq (p + 1, ".\\.\\.", 5)) |
| 2629 |
{ |
| 2630 |
*ap++ = '.'; |
| 2631 |
*ap++ = '.'; |
| 2632 |
p += 4; |
| 2633 |
} |
| 2634 |
else |
| 2635 |
#endif |
| 2636 |
if (p[1] != '\\' && p[1] != '\'' |
| 2637 |
&& !ISSPACE (p[1]) |
| 2638 |
&& strchr (sh_chars_sh, p[1]) == 0) |
| 2639 |
|
| 2640 |
--p; |
| 2641 |
#endif |
| 2642 |
|
| 2643 |
|
| 2644 |
*ap++ = *++p; |
| 2645 |
} |
| 2646 |
break; |
| 2647 |
|
| 2648 |
case '\'': |
| 2649 |
case '"': |
| 2650 |
instring = *p; |
| 2651 |
break; |
| 2652 |
|
| 2653 |
case '\n': |
| 2654 |
if (restp != NULL) |
| 2655 |
{ |
| 2656 |
|
| 2657 |
*restp = p; |
| 2658 |
goto end_of_line; |
| 2659 |
} |
| 2660 |
else |
| 2661 |
|
| 2662 |
*ap++ = '\n'; |
| 2663 |
break; |
| 2664 |
|
| 2665 |
case ' ': |
| 2666 |
case '\t': |
| 2667 |
|
| 2668 |
|
| 2669 |
*ap++ = '\0'; |
| 2670 |
new_argv[++i] = ap; |
| 2671 |
last_argument_was_empty = 0; |
| 2672 |
|
| 2673 |
|
| 2674 |
|
| 2675 |
seen_nonequals |= ! word_has_equals; |
| 2676 |
if (word_has_equals && ! seen_nonequals) |
| 2677 |
|
| 2678 |
|
| 2679 |
goto slow; |
| 2680 |
word_has_equals = 0; |
| 2681 |
|
| 2682 |
|
| 2683 |
|
| 2684 |
|
| 2685 |
if (i == 1) |
| 2686 |
{ |
| 2687 |
int j; |
| 2688 |
for (j = 0; sh_cmds[j] != 0; ++j) |
| 2689 |
{ |
| 2690 |
if (streq (sh_cmds[j], new_argv[0])) |
| 2691 |
goto slow; |
| 2692 |
#if defined(WINDOWS32) |
| 2693 |
|
| 2694 |
if (!unixy_shell |
| 2695 |
&& strcasecmp (sh_cmds[j], new_argv[0]) == 0) |
| 2696 |
goto slow; |
| 2697 |
#endif |
| 2698 |
} |
| 2699 |
} |
| 2700 |
|
| 2701 |
|
| 2702 |
while (ISBLANK (p[1])) |
| 2703 |
++p; |
| 2704 |
break; |
| 2705 |
|
| 2706 |
default: |
| 2707 |
*ap++ = *p; |
| 2708 |
break; |
| 2709 |
} |
| 2710 |
} |
| 2711 |
end_of_line: |
| 2712 |
|
| 2713 |
if (instring) |
| 2714 |
|
| 2715 |
goto slow; |
| 2716 |
|
| 2717 |
|
| 2718 |
|
| 2719 |
*ap = '\0'; |
| 2720 |
if (new_argv[i][0] != '\0' || last_argument_was_empty) |
| 2721 |
++i; |
| 2722 |
new_argv[i] = 0; |
| 2723 |
|
| 2724 |
if (i == 1) |
| 2725 |
{ |
| 2726 |
int j; |
| 2727 |
for (j = 0; sh_cmds[j] != 0; ++j) |
| 2728 |
if (streq (sh_cmds[j], new_argv[0])) |
| 2729 |
goto slow; |
| 2730 |
} |
| 2731 |
|
| 2732 |
if (new_argv[0] == 0) |
| 2733 |
{ |
| 2734 |
|
| 2735 |
free (argstr); |
| 2736 |
free (new_argv); |
| 2737 |
return 0; |
| 2738 |
} |
| 2739 |
|
| 2740 |
return new_argv; |
| 2741 |
|
| 2742 |
slow:; |
| 2743 |
|
| 2744 |
|
| 2745 |
if (new_argv != 0) |
| 2746 |
{ |
| 2747 |
|
| 2748 |
free (argstr); |
| 2749 |
free (new_argv); |
| 2750 |
} |
| 2751 |
|
| 2752 |
#ifdef __MSDOS__ |
| 2753 |
execute_by_shell = 1; |
| 2754 |
#endif |
| 2755 |
|
| 2756 |
#ifdef WINDOWS32 |
| 2757 |
|
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
|
| 2762 |
|
| 2763 |
|
| 2764 |
|
| 2765 |
|
| 2766 |
|
| 2767 |
|
| 2768 |
NEXT_TOKEN (line); |
| 2769 |
if (*line == '\0') |
| 2770 |
return 0; |
| 2771 |
#endif |
| 2772 |
|
| 2773 |
{ |
| 2774 |
|
| 2775 |
|
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
char *new_line; |
| 2780 |
size_t shell_len = strlen (shell); |
| 2781 |
size_t line_len = strlen (line); |
| 2782 |
size_t sflags_len = shellflags ? strlen (shellflags) : 0; |
| 2783 |
#ifdef WINDOWS32 |
| 2784 |
char *command_ptr = NULL; |
| 2785 |
#endif |
| 2786 |
|
| 2787 |
|
| 2788 |
|
| 2789 |
|
| 2790 |
|
| 2791 |
if (one_shell) |
| 2792 |
{ |
| 2793 |
|
| 2794 |
|
| 2795 |
|
| 2796 |
|
| 2797 |
|
| 2798 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
#if defined __MSDOS__ |
| 2804 |
if (unixy_shell) |
| 2805 |
#else |
| 2806 |
if (is_bourne_compatible_shell (shell) |
| 2807 |
#ifdef WINDOWS32 |
| 2808 |
|
| 2809 |
&& !no_default_sh_exe |
| 2810 |
#endif |
| 2811 |
) |
| 2812 |
#endif |
| 2813 |
{ |
| 2814 |
const char *f = line; |
| 2815 |
char *t = line; |
| 2816 |
|
| 2817 |
|
| 2818 |
|
| 2819 |
while (f[0] != '\0') |
| 2820 |
{ |
| 2821 |
int esc = 0; |
| 2822 |
|
| 2823 |
|
| 2824 |
|
| 2825 |
while (ISBLANK (*f) || *f == '-' || *f == '@' || *f == '+') |
| 2826 |
++f; |
| 2827 |
|
| 2828 |
|
| 2829 |
while (*f != '\0') |
| 2830 |
{ |
| 2831 |
*(t++) = *(f++); |
| 2832 |
if (f[-1] == '\\') |
| 2833 |
esc = !esc; |
| 2834 |
else |
| 2835 |
{ |
| 2836 |
|
| 2837 |
if (f[-1] == '\n' && ! esc) |
| 2838 |
break; |
| 2839 |
|
| 2840 |
|
| 2841 |
esc = 0; |
| 2842 |
} |
| 2843 |
} |
| 2844 |
} |
| 2845 |
*t = '\0'; |
| 2846 |
} |
| 2847 |
#ifdef WINDOWS32 |
| 2848 |
else |
| 2849 |
{ |
| 2850 |
const char *f = line; |
| 2851 |
char *t = line; |
| 2852 |
char *tstart = t; |
| 2853 |
int temp_fd; |
| 2854 |
FILE* batch = NULL; |
| 2855 |
int id = GetCurrentProcessId (); |
| 2856 |
PATH_VAR(fbuf); |
| 2857 |
|
| 2858 |
|
| 2859 |
sprintf (fbuf, "make%d", id); |
| 2860 |
*batch_filename = create_batch_file (fbuf, 0, &temp_fd); |
| 2861 |
DB (DB_JOBS, (_("Creating temporary batch file %s\n"), |
| 2862 |
*batch_filename)); |
| 2863 |
|
| 2864 |
|
| 2865 |
|
| 2866 |
_setmode (temp_fd, _O_TEXT); |
| 2867 |
batch = _fdopen (temp_fd, "wt"); |
| 2868 |
fputs ("@echo off\n", batch); |
| 2869 |
DB (DB_JOBS, (_("Batch file contents:\n\t@echo off\n"))); |
| 2870 |
|
| 2871 |
|
| 2872 |
|
| 2873 |
while (*f != '\0') |
| 2874 |
{ |
| 2875 |
|
| 2876 |
|
| 2877 |
while (ISBLANK (*f) || *f == '-' || *f == '@' || *f == '+') |
| 2878 |
++f; |
| 2879 |
|
| 2880 |
|
| 2881 |
while (*f != '\0') |
| 2882 |
{ |
| 2883 |
|
| 2884 |
|
| 2885 |
|
| 2886 |
if (*f == '\\' && f[1] == '\n') |
| 2887 |
{ |
| 2888 |
f += 2; |
| 2889 |
while (ISBLANK (*f)) |
| 2890 |
++f; |
| 2891 |
} |
| 2892 |
*(t++) = *(f++); |
| 2893 |
|
| 2894 |
|
| 2895 |
if (f[-1] == '\n') |
| 2896 |
break; |
| 2897 |
} |
| 2898 |
|
| 2899 |
if (t > tstart) |
| 2900 |
{ |
| 2901 |
char c = *t; |
| 2902 |
*t = '\0'; |
| 2903 |
fputs (tstart, batch); |
| 2904 |
DB (DB_JOBS, ("\t%s", tstart)); |
| 2905 |
tstart = t; |
| 2906 |
*t = c; |
| 2907 |
} |
| 2908 |
} |
| 2909 |
DB (DB_JOBS, ("\n")); |
| 2910 |
fclose (batch); |
| 2911 |
|
| 2912 |
|
| 2913 |
|
| 2914 |
new_argv = xmalloc (2 * sizeof (char *)); |
| 2915 |
new_argv[0] = xstrdup (*batch_filename); |
| 2916 |
new_argv[1] = NULL; |
| 2917 |
return new_argv; |
| 2918 |
} |
| 2919 |
#endif |
| 2920 |
|
| 2921 |
{ |
| 2922 |
int n = 0; |
| 2923 |
|
| 2924 |
new_argv = xmalloc ((4 + sflags_len/2) * sizeof (char *)); |
| 2925 |
new_argv[n++] = xstrdup (shell); |
| 2926 |
|
| 2927 |
|
| 2928 |
if (! shellflags) |
| 2929 |
new_argv[n++] = xstrdup (""); |
| 2930 |
else |
| 2931 |
{ |
| 2932 |
const char *s = shellflags; |
| 2933 |
char *t; |
| 2934 |
size_t len; |
| 2935 |
while ((t = find_next_token (&s, &len)) != 0) |
| 2936 |
new_argv[n++] = xstrndup (t, len); |
| 2937 |
} |
| 2938 |
|
| 2939 |
|
| 2940 |
new_argv[n++] = line; |
| 2941 |
new_argv[n++] = NULL; |
| 2942 |
} |
| 2943 |
return new_argv; |
| 2944 |
} |
| 2945 |
|
| 2946 |
new_line = xmalloc ((shell_len*2) + 1 + sflags_len + 1 |
| 2947 |
+ (line_len*2) + 1); |
| 2948 |
ap = new_line; |
| 2949 |
|
| 2950 |
|
| 2951 |
|
| 2952 |
|
| 2953 |
for (cp = shell; *cp != '\0'; ++cp) |
| 2954 |
{ |
| 2955 |
if (strchr (sh_chars, *cp) != 0) |
| 2956 |
*(ap++) = '\\'; |
| 2957 |
*(ap++) = *cp; |
| 2958 |
} |
| 2959 |
*(ap++) = ' '; |
| 2960 |
if (shellflags) |
| 2961 |
memcpy (ap, shellflags, sflags_len); |
| 2962 |
ap += sflags_len; |
| 2963 |
*(ap++) = ' '; |
| 2964 |
#ifdef WINDOWS32 |
| 2965 |
command_ptr = ap; |
| 2966 |
#endif |
| 2967 |
for (p = line; *p != '\0'; ++p) |
| 2968 |
{ |
| 2969 |
if (restp != NULL && *p == '\n') |
| 2970 |
{ |
| 2971 |
*restp = p; |
| 2972 |
break; |
| 2973 |
} |
| 2974 |
else if (*p == '\\' && p[1] == '\n') |
| 2975 |
{ |
| 2976 |
|
| 2977 |
|
| 2978 |
|
| 2979 |
#if defined (__MSDOS__) || defined (WINDOWS32) |
| 2980 |
# define PRESERVE_BSNL unixy_shell |
| 2981 |
#else |
| 2982 |
# define PRESERVE_BSNL 1 |
| 2983 |
#endif |
| 2984 |
if (PRESERVE_BSNL) |
| 2985 |
{ |
| 2986 |
*(ap++) = '\\'; |
| 2987 |
|
| 2988 |
|
| 2989 |
|
| 2990 |
if (!batch_mode_shell) |
| 2991 |
*(ap++) = '\\'; |
| 2992 |
*(ap++) = '\n'; |
| 2993 |
} |
| 2994 |
++p; |
| 2995 |
continue; |
| 2996 |
} |
| 2997 |
|
| 2998 |
|
| 2999 |
if (unixy_shell && !batch_mode_shell && |
| 3000 |
(*p == '\\' || *p == '\'' || *p == '"' |
| 3001 |
|| ISSPACE (*p) |
| 3002 |
|| strchr (sh_chars, *p) != 0)) |
| 3003 |
*ap++ = '\\'; |
| 3004 |
#ifdef __MSDOS__ |
| 3005 |
else if (unixy_shell && strneq (p, "...", 3)) |
| 3006 |
{ |
| 3007 |
|
| 3008 |
strcpy (ap, "\\.\\.\\"); |
| 3009 |
ap += 5; |
| 3010 |
p += 2; |
| 3011 |
} |
| 3012 |
#endif |
| 3013 |
*ap++ = *p; |
| 3014 |
} |
| 3015 |
if (ap == new_line + shell_len + sflags_len + 2) |
| 3016 |
{ |
| 3017 |
|
| 3018 |
free (new_line); |
| 3019 |
return 0; |
| 3020 |
} |
| 3021 |
*ap = '\0'; |
| 3022 |
|
| 3023 |
#ifdef WINDOWS32 |
| 3024 |
|
| 3025 |
|
| 3026 |
|
| 3027 |
if (just_print_flag && !(flags & COMMANDS_RECURSE)) |
| 3028 |
{ |
| 3029 |
|
| 3030 |
|
| 3031 |
new_argv = xmalloc (2 * sizeof (char *)); |
| 3032 |
new_argv[0] = xstrdup (""); |
| 3033 |
new_argv[1] = NULL; |
| 3034 |
} |
| 3035 |
else if ((no_default_sh_exe || batch_mode_shell) && batch_filename) |
| 3036 |
{ |
| 3037 |
int temp_fd; |
| 3038 |
FILE* batch = NULL; |
| 3039 |
int id = GetCurrentProcessId (); |
| 3040 |
PATH_VAR (fbuf); |
| 3041 |
|
| 3042 |
|
| 3043 |
sprintf (fbuf, "make%d", id); |
| 3044 |
*batch_filename = create_batch_file (fbuf, unixy_shell, &temp_fd); |
| 3045 |
|
| 3046 |
DB (DB_JOBS, (_("Creating temporary batch file %s\n"), |
| 3047 |
*batch_filename)); |
| 3048 |
|
| 3049 |
|
| 3050 |
|
| 3051 |
_setmode (temp_fd, _O_TEXT); |
| 3052 |
batch = _fdopen (temp_fd, "wt"); |
| 3053 |
if (!unixy_shell) |
| 3054 |
fputs ("@echo off\n", batch); |
| 3055 |
fputs (command_ptr, batch); |
| 3056 |
fputc ('\n', batch); |
| 3057 |
fclose (batch); |
| 3058 |
DB (DB_JOBS, (_("Batch file contents:%s\n\t%s\n"), |
| 3059 |
!unixy_shell ? "\n\t@echo off" : "", command_ptr)); |
| 3060 |
|
| 3061 |
|
| 3062 |
new_argv = xmalloc (3 * sizeof (char *)); |
| 3063 |
if (unixy_shell) |
| 3064 |
{ |
| 3065 |
new_argv[0] = xstrdup (shell); |
| 3066 |
new_argv[1] = *batch_filename; |
| 3067 |
} |
| 3068 |
else |
| 3069 |
{ |
| 3070 |
new_argv[0] = xstrdup (*batch_filename); |
| 3071 |
new_argv[1] = NULL; |
| 3072 |
} |
| 3073 |
new_argv[2] = NULL; |
| 3074 |
} |
| 3075 |
else |
| 3076 |
#endif |
| 3077 |
|
| 3078 |
if (unixy_shell) |
| 3079 |
new_argv = construct_command_argv_internal (new_line, 0, 0, 0, 0, |
| 3080 |
flags, 0); |
| 3081 |
|
| 3082 |
#if defined(__MSDOS__) |
| 3083 |
else |
| 3084 |
{ |
| 3085 |
|
| 3086 |
|
| 3087 |
|
| 3088 |
new_argv = xmalloc (sizeof (char *)); |
| 3089 |
line_len = strlen (new_line) - shell_len - sflags_len - 2; |
| 3090 |
new_argv[0] = xmalloc (line_len + 1); |
| 3091 |
strncpy (new_argv[0], |
| 3092 |
new_line + shell_len + sflags_len + 2, line_len); |
| 3093 |
new_argv[0][line_len] = '\0'; |
| 3094 |
} |
| 3095 |
#else |
| 3096 |
else |
| 3097 |
fatal (NILF, CSTRLEN (__FILE__) + INTSTR_LENGTH, |
| 3098 |
_("%s (line %d) Bad shell context (!unixy && !batch_mode_shell)\n"), |
| 3099 |
__FILE__, __LINE__); |
| 3100 |
#endif |
| 3101 |
|
| 3102 |
free (new_line); |
| 3103 |
} |
| 3104 |
|
| 3105 |
return new_argv; |
| 3106 |
} |
| 3107 |
|
| 3108 |
|
| 3109 |
|
| 3110 |
|
| 3111 |
|
| 3112 |
|
| 3113 |
|
| 3114 |
|
| 3115 |
|
| 3116 |
|
| 3117 |
|
| 3118 |
|
| 3119 |
|
| 3120 |
|
| 3121 |
char ** |
| 3122 |
construct_command_argv (char *line, char **restp, struct file *file, |
| 3123 |
int cmd_flags, char **batch_filename) |
| 3124 |
{ |
| 3125 |
char *shell, *ifs, *shellflags; |
| 3126 |
char **argv; |
| 3127 |
|
| 3128 |
{ |
| 3129 |
|
| 3130 |
int save = warn_undefined_variables_flag; |
| 3131 |
warn_undefined_variables_flag = 0; |
| 3132 |
|
| 3133 |
shell = allocated_variable_expand_for_file ("$(SHELL)", file); |
| 3134 |
#ifdef WINDOWS32 |
| 3135 |
|
| 3136 |
|
| 3137 |
|
| 3138 |
|
| 3139 |
if (shell) |
| 3140 |
{ |
| 3141 |
char *p = w32ify (shell, 0); |
| 3142 |
strcpy (shell, p); |
| 3143 |
} |
| 3144 |
#endif |
| 3145 |
shellflags = allocated_variable_expand_for_file ("$(.SHELLFLAGS)", file); |
| 3146 |
ifs = allocated_variable_expand_for_file ("$(IFS)", file); |
| 3147 |
|
| 3148 |
warn_undefined_variables_flag = save; |
| 3149 |
} |
| 3150 |
|
| 3151 |
argv = construct_command_argv_internal (line, restp, shell, shellflags, ifs, |
| 3152 |
cmd_flags, batch_filename); |
| 3153 |
|
| 3154 |
free (shell); |
| 3155 |
free (shellflags); |
| 3156 |
free (ifs); |
| 3157 |
|
| 3158 |
return argv; |
| 3159 |
} |
| 3160 |
|
| 3161 |
#if !defined(HAVE_DUP2) |
| 3162 |
int |
| 3163 |
dup2 (int old, int new) |
| 3164 |
{ |
| 3165 |
int fd; |
| 3166 |
|
| 3167 |
(void) close (new); |
| 3168 |
EINTRLOOP (fd, dup (old)); |
| 3169 |
if (fd != new) |
| 3170 |
{ |
| 3171 |
(void) close (fd); |
| 3172 |
errno = EMFILE; |
| 3173 |
return -1; |
| 3174 |
} |
| 3175 |
|
| 3176 |
return fd; |
| 3177 |
} |
| 3178 |
#endif |