| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bin/process.h" | 5 #include "bin/process.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <poll.h> | 9 #include <poll.h> |
| 10 #include <signal.h> | 10 #include <signal.h> |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 } | 299 } |
| 300 TEMP_FAILURE_RETRY(close(exec_control_fd)); | 300 TEMP_FAILURE_RETRY(close(exec_control_fd)); |
| 301 exit(1); | 301 exit(1); |
| 302 } | 302 } |
| 303 | 303 |
| 304 | 304 |
| 305 int Process::Start(const char* path, | 305 int Process::Start(const char* path, |
| 306 char* arguments[], | 306 char* arguments[], |
| 307 intptr_t arguments_length, | 307 intptr_t arguments_length, |
| 308 const char* working_directory, | 308 const char* working_directory, |
| 309 char* environment[], | |
| 310 intptr_t environment_length, | |
| 311 intptr_t* in, | 309 intptr_t* in, |
| 312 intptr_t* out, | 310 intptr_t* out, |
| 313 intptr_t* err, | 311 intptr_t* err, |
| 314 intptr_t* id, | 312 intptr_t* id, |
| 315 intptr_t* exit_event, | 313 intptr_t* exit_event, |
| 316 char* os_error_message, | 314 char* os_error_message, |
| 317 int os_error_message_len) { | 315 int os_error_message_len) { |
| 318 pid_t pid; | 316 pid_t pid; |
| 319 int read_in[2]; // Pipe for stdout to child process. | 317 int read_in[2]; // Pipe for stdout to child process. |
| 320 int read_err[2]; // Pipe for stderr to child process. | 318 int read_err[2]; // Pipe for stderr to child process. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 TEMP_FAILURE_RETRY(close(read_err[1])); | 382 TEMP_FAILURE_RETRY(close(read_err[1])); |
| 385 TEMP_FAILURE_RETRY(close(write_out[0])); | 383 TEMP_FAILURE_RETRY(close(write_out[0])); |
| 386 TEMP_FAILURE_RETRY(close(write_out[1])); | 384 TEMP_FAILURE_RETRY(close(write_out[1])); |
| 387 TEMP_FAILURE_RETRY(close(exec_control[0])); | 385 TEMP_FAILURE_RETRY(close(exec_control[0])); |
| 388 TEMP_FAILURE_RETRY(close(exec_control[1])); | 386 TEMP_FAILURE_RETRY(close(exec_control[1])); |
| 389 fprintf(stderr, "fcntl failed: %s\n", os_error_message); | 387 fprintf(stderr, "fcntl failed: %s\n", os_error_message); |
| 390 return errno; | 388 return errno; |
| 391 } | 389 } |
| 392 | 390 |
| 393 char** program_arguments = new char*[arguments_length + 2]; | 391 char** program_arguments = new char*[arguments_length + 2]; |
| 394 program_arguments[0] = const_cast<char*>(path); | 392 program_arguments[0] = const_cast<char *>(path); |
| 395 for (int i = 0; i < arguments_length; i++) { | 393 for (int i = 0; i < arguments_length; i++) { |
| 396 program_arguments[i + 1] = arguments[i]; | 394 program_arguments[i + 1] = arguments[i]; |
| 397 } | 395 } |
| 398 program_arguments[arguments_length + 1] = NULL; | 396 program_arguments[arguments_length + 1] = NULL; |
| 399 | 397 |
| 400 char** program_environment = NULL; | |
| 401 if (environment != NULL) { | |
| 402 program_environment = new char*[environment_length + 1]; | |
| 403 for (int i = 0; i < environment_length; i++) { | |
| 404 program_environment[i] = environment[i]; | |
| 405 } | |
| 406 program_arguments[environment_length] = NULL; | |
| 407 } | |
| 408 | |
| 409 struct sigaction act; | 398 struct sigaction act; |
| 410 bzero(&act, sizeof(act)); | 399 bzero(&act, sizeof(act)); |
| 411 act.sa_sigaction = SigChldHandler; | 400 act.sa_sigaction = SigChldHandler; |
| 412 act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO; | 401 act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO; |
| 413 if (sigaction(SIGCHLD, &act, 0) != 0) { | 402 if (sigaction(SIGCHLD, &act, 0) != 0) { |
| 414 perror("Process start: setting signal handler failed"); | 403 perror("Process start: setting signal handler failed"); |
| 415 } | 404 } |
| 416 pid = TEMP_FAILURE_RETRY(fork()); | 405 pid = TEMP_FAILURE_RETRY(fork()); |
| 417 if (pid < 0) { | 406 if (pid < 0) { |
| 418 SetChildOsErrorMessage(os_error_message, os_error_message_len); | 407 SetChildOsErrorMessage(os_error_message, os_error_message_len); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 if (TEMP_FAILURE_RETRY(dup2(read_err[1], STDERR_FILENO)) == -1) { | 442 if (TEMP_FAILURE_RETRY(dup2(read_err[1], STDERR_FILENO)) == -1) { |
| 454 ReportChildError(exec_control[1]); | 443 ReportChildError(exec_control[1]); |
| 455 } | 444 } |
| 456 TEMP_FAILURE_RETRY(close(read_err[1])); | 445 TEMP_FAILURE_RETRY(close(read_err[1])); |
| 457 | 446 |
| 458 if (working_directory != NULL && | 447 if (working_directory != NULL && |
| 459 TEMP_FAILURE_RETRY(chdir(working_directory)) == -1) { | 448 TEMP_FAILURE_RETRY(chdir(working_directory)) == -1) { |
| 460 ReportChildError(exec_control[1]); | 449 ReportChildError(exec_control[1]); |
| 461 } | 450 } |
| 462 | 451 |
| 463 if (environment != NULL) { | 452 TEMP_FAILURE_RETRY( |
| 464 TEMP_FAILURE_RETRY( | 453 execvp(path, const_cast<char* const*>(program_arguments))); |
| 465 execve(path, | |
| 466 const_cast<char* const*>(program_arguments), | |
| 467 program_environment)); | |
| 468 } else { | |
| 469 TEMP_FAILURE_RETRY( | |
| 470 execvp(path, const_cast<char* const*>(program_arguments))); | |
| 471 } | |
| 472 ReportChildError(exec_control[1]); | 454 ReportChildError(exec_control[1]); |
| 473 } | 455 } |
| 474 | 456 |
| 475 // The arguments and environment for the spawned process are not needed | 457 // The arguments for the spawned process are not needed any longer. |
| 476 // any longer. | |
| 477 delete[] program_arguments; | 458 delete[] program_arguments; |
| 478 delete[] program_environment; | |
| 479 | 459 |
| 480 int event_fds[2]; | 460 int event_fds[2]; |
| 481 result = TEMP_FAILURE_RETRY(pipe(event_fds)); | 461 result = TEMP_FAILURE_RETRY(pipe(event_fds)); |
| 482 if (result < 0) { | 462 if (result < 0) { |
| 483 SetChildOsErrorMessage(os_error_message, os_error_message_len); | 463 SetChildOsErrorMessage(os_error_message, os_error_message_len); |
| 484 TEMP_FAILURE_RETRY(close(read_in[0])); | 464 TEMP_FAILURE_RETRY(close(read_in[0])); |
| 485 TEMP_FAILURE_RETRY(close(read_in[1])); | 465 TEMP_FAILURE_RETRY(close(read_in[1])); |
| 486 TEMP_FAILURE_RETRY(close(read_err[0])); | 466 TEMP_FAILURE_RETRY(close(read_err[0])); |
| 487 TEMP_FAILURE_RETRY(close(read_err[1])); | 467 TEMP_FAILURE_RETRY(close(read_err[1])); |
| 488 TEMP_FAILURE_RETRY(close(write_out[0])); | 468 TEMP_FAILURE_RETRY(close(write_out[0])); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 if (result == -1) { | 535 if (result == -1) { |
| 556 return false; | 536 return false; |
| 557 } | 537 } |
| 558 return true; | 538 return true; |
| 559 } | 539 } |
| 560 | 540 |
| 561 | 541 |
| 562 void Process::TerminateExitCodeHandler() { | 542 void Process::TerminateExitCodeHandler() { |
| 563 ExitCodeHandler::TerminateExitCodeThread(); | 543 ExitCodeHandler::TerminateExitCodeThread(); |
| 564 } | 544 } |
| OLD | NEW |