Chromium Code Reviews| 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 "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
| 7 | 7 |
| 8 #include "bin/process.h" | 8 #include "bin/process.h" |
| 9 | 9 |
| 10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
| (...skipping 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 561 TEMP_FAILURE_RETRY(close(write_out[0])); | 561 TEMP_FAILURE_RETRY(close(write_out[0])); |
| 562 FDUtils::SetNonBlocking(read_err[0]); | 562 FDUtils::SetNonBlocking(read_err[0]); |
| 563 *err = read_err[0]; | 563 *err = read_err[0]; |
| 564 TEMP_FAILURE_RETRY(close(read_err[1])); | 564 TEMP_FAILURE_RETRY(close(read_err[1])); |
| 565 | 565 |
| 566 *id = pid; | 566 *id = pid; |
| 567 return 0; | 567 return 0; |
| 568 } | 568 } |
| 569 | 569 |
| 570 | 570 |
| 571 static bool ReadProcessBuffer(uint8_t** buffer, | |
| 572 intptr_t* buffer_length, | |
| 573 intptr_t available, | |
| 574 int fd) { | |
| 575 uint8_t* tmp = new uint8_t[*buffer_length + available]; | |
| 576 if (tmp == NULL) FATAL("Allocation failed"); | |
| 577 memmove(tmp, *buffer, *buffer_length); | |
| 578 delete[] *buffer; | |
| 579 *buffer = tmp; | |
| 580 intptr_t bytes = TEMP_FAILURE_RETRY(read( | |
| 581 fd, | |
| 582 reinterpret_cast<void*>(*buffer + *buffer_length), | |
| 583 available)); | |
| 584 if (bytes < 0) return false; | |
| 585 *buffer_length += bytes; | |
| 586 return true; | |
| 587 } | |
| 588 | |
| 589 | |
| 590 static bool CloseProcessBuffers(struct pollfd fds[3]) { | |
| 591 int e = errno; | |
| 592 VOID_TEMP_FAILURE_RETRY(close(fds[0].fd)); | |
| 593 VOID_TEMP_FAILURE_RETRY(close(fds[1].fd)); | |
| 594 VOID_TEMP_FAILURE_RETRY(close(fds[2].fd)); | |
| 595 errno = e; | |
| 596 return false; | |
| 597 } | |
| 598 | |
| 599 | |
| 600 bool Process::Wait(intptr_t pid, | |
| 601 intptr_t in, | |
| 602 intptr_t out, | |
| 603 intptr_t err, | |
| 604 intptr_t exit_event, | |
| 605 ProcessResult* result) { | |
| 606 // Close input to the process right away. | |
| 607 VOID_TEMP_FAILURE_RETRY(close(in)); | |
| 608 | |
| 609 uint8_t* out_data = NULL; | |
| 610 intptr_t out_data_length = 0; | |
| 611 uint8_t* err_data = NULL; | |
| 612 intptr_t err_data_length = 0; | |
| 613 union { | |
| 614 uint8_t bytes[8]; | |
| 615 int32_t ints[2]; | |
| 616 } exit_code_data; | |
| 617 | |
| 618 struct pollfd fds[3]; | |
| 619 fds[0].fd = out; | |
| 620 fds[1].fd = err; | |
| 621 fds[2].fd = exit_event; | |
| 622 | |
| 623 for (int i = 0; i < 3; i++) { | |
| 624 fds[i].events = POLLIN; | |
| 625 } | |
| 626 | |
| 627 int alive = 3; | |
| 628 while (alive > 0) { | |
| 629 if (TEMP_FAILURE_RETRY(poll(fds, alive, -1)) <= 0) { | |
|
Bill Hesse
2013/08/09 15:32:23
Add comment - blocking call. This implements a Da
Søren Gjesse
2013/08/12 06:52:38
Done.
| |
| 630 return CloseProcessBuffers(fds); | |
| 631 } | |
| 632 for (int i = 0; i < alive; i++) { | |
| 633 if (fds[i].revents & POLLIN) { | |
| 634 intptr_t avail = FDUtils::AvailableBytes(fds[i].fd); | |
| 635 if (fds[i].fd == out) { | |
| 636 if (!ReadProcessBuffer(&out_data, &out_data_length, avail, out)) { | |
| 637 return CloseProcessBuffers(fds); | |
| 638 } | |
| 639 } else if (fds[i].fd == err) { | |
| 640 if (!ReadProcessBuffer(&err_data, &err_data_length, avail, err)) { | |
| 641 return CloseProcessBuffers(fds); | |
| 642 } | |
| 643 } else if (fds[i].fd == exit_event) { | |
| 644 intptr_t avail = FDUtils::AvailableBytes(fds[i].fd); | |
| 645 if (avail == 8) { | |
| 646 intptr_t bytes = 0; | |
| 647 do { | |
| 648 intptr_t b = TEMP_FAILURE_RETRY(read(fds[i].fd, | |
| 649 exit_code_data.bytes, 8)); | |
|
Bill Hesse
2013/08/09 15:32:23
shouldn't this be read(fds[i].fd, exit_code_data.b
Søren Gjesse
2013/08/12 06:52:38
Removed the while, as we know that there are 8 byt
| |
| 650 if (b > 0) { | |
| 651 bytes += b; | |
| 652 } else { | |
| 653 return CloseProcessBuffers(fds); | |
| 654 } | |
| 655 } while (bytes < 8); | |
| 656 } | |
| 657 } else { | |
| 658 UNREACHABLE(); | |
| 659 } | |
| 660 continue; | |
| 661 } | |
| 662 | |
| 663 if (fds[i].revents & POLLHUP) { | |
| 664 VOID_TEMP_FAILURE_RETRY(close(fds[i].fd)); | |
| 665 alive--; | |
| 666 if (i < alive) { | |
|
Bill Hesse
2013/08/09 15:32:23
This guard is not really needed. But it is OK, si
Søren Gjesse
2013/08/12 06:52:38
This is needed as calling poll with closed file de
| |
| 667 fds[i] = fds[alive]; | |
| 668 } | |
| 669 } | |
| 670 } | |
| 671 } | |
| 672 | |
| 673 // All handles closed and all data read. | |
| 674 result->SetStdoutData(out_data, out_data_length); | |
| 675 result->SetStderrData(err_data, err_data_length); | |
| 676 | |
| 677 // Calculate the exit code. | |
| 678 intptr_t exit_code = exit_code_data.ints[0]; | |
| 679 intptr_t negative = exit_code_data.ints[1]; | |
| 680 if (negative) exit_code = -exit_code; | |
| 681 result->set_exit_code(exit_code); | |
| 682 | |
| 683 return true; | |
| 684 } | |
| 685 | |
| 686 | |
| 571 bool Process::Kill(intptr_t id, int signal) { | 687 bool Process::Kill(intptr_t id, int signal) { |
| 572 return (TEMP_FAILURE_RETRY(kill(id, signal)) != -1); | 688 return (TEMP_FAILURE_RETRY(kill(id, signal)) != -1); |
| 573 } | 689 } |
| 574 | 690 |
| 575 | 691 |
| 576 void Process::TerminateExitCodeHandler() { | 692 void Process::TerminateExitCodeHandler() { |
| 577 ExitCodeHandler::TerminateExitCodeThread(); | 693 ExitCodeHandler::TerminateExitCodeThread(); |
| 578 } | 694 } |
| 579 | 695 |
| 580 | 696 |
| 581 intptr_t Process::CurrentProcessId() { | 697 intptr_t Process::CurrentProcessId() { |
| 582 return static_cast<intptr_t>(getpid()); | 698 return static_cast<intptr_t>(getpid()); |
| 583 } | 699 } |
| 584 | 700 |
| 585 } // namespace bin | 701 } // namespace bin |
| 586 } // namespace dart | 702 } // namespace dart |
| 587 | 703 |
| 588 #endif // defined(TARGET_OS_LINUX) | 704 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |