| 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_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
| 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 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 VOID_TEMP_FAILURE_RETRY(close(write_out[0])); | 560 VOID_TEMP_FAILURE_RETRY(close(write_out[0])); |
| 561 FDUtils::SetNonBlocking(read_err[0]); | 561 FDUtils::SetNonBlocking(read_err[0]); |
| 562 *err = read_err[0]; | 562 *err = read_err[0]; |
| 563 VOID_TEMP_FAILURE_RETRY(close(read_err[1])); | 563 VOID_TEMP_FAILURE_RETRY(close(read_err[1])); |
| 564 | 564 |
| 565 *id = pid; | 565 *id = pid; |
| 566 return 0; | 566 return 0; |
| 567 } | 567 } |
| 568 | 568 |
| 569 | 569 |
| 570 class BufferList: public BufferListBase { |
| 571 public: |
| 572 bool Read(int fd, intptr_t available) { |
| 573 // Read all available bytes. |
| 574 while (available > 0) { |
| 575 if (free_size_ == 0) Allocate(); |
| 576 ASSERT(free_size_ > 0); |
| 577 ASSERT(free_size_ <= kBufferSize); |
| 578 intptr_t block_size = dart::Utils::Minimum(free_size_, available); |
| 579 intptr_t bytes = TEMP_FAILURE_RETRY(read( |
| 580 fd, |
| 581 reinterpret_cast<void*>(FreeSpaceAddress()), |
| 582 block_size)); |
| 583 if (bytes < 0) return false; |
| 584 data_size_ += bytes; |
| 585 free_size_ -= bytes; |
| 586 available -= bytes; |
| 587 } |
| 588 return true; |
| 589 } |
| 590 }; |
| 591 |
| 592 |
| 593 static bool CloseProcessBuffers(struct pollfd fds[3]) { |
| 594 int e = errno; |
| 595 VOID_TEMP_FAILURE_RETRY(close(fds[0].fd)); |
| 596 VOID_TEMP_FAILURE_RETRY(close(fds[1].fd)); |
| 597 VOID_TEMP_FAILURE_RETRY(close(fds[2].fd)); |
| 598 errno = e; |
| 599 return false; |
| 600 } |
| 601 |
| 602 |
| 603 bool Process::Wait(intptr_t pid, |
| 604 intptr_t in, |
| 605 intptr_t out, |
| 606 intptr_t err, |
| 607 intptr_t exit_event, |
| 608 ProcessResult* result) { |
| 609 // Close input to the process right away. |
| 610 VOID_TEMP_FAILURE_RETRY(close(in)); |
| 611 |
| 612 // There is no return from this function using Dart_PropagateError |
| 613 // as memory used by the buffer lists is freed through their |
| 614 // destructors. |
| 615 BufferList out_data; |
| 616 BufferList err_data; |
| 617 union { |
| 618 uint8_t bytes[8]; |
| 619 int32_t ints[2]; |
| 620 } exit_code_data; |
| 621 |
| 622 struct pollfd fds[3]; |
| 623 fds[0].fd = out; |
| 624 fds[1].fd = err; |
| 625 fds[2].fd = exit_event; |
| 626 |
| 627 for (int i = 0; i < 3; i++) { |
| 628 fds[i].events = POLLIN; |
| 629 } |
| 630 |
| 631 int alive = 3; |
| 632 while (alive > 0) { |
| 633 // Blocking call waiting for events from the child process. |
| 634 if (TEMP_FAILURE_RETRY(poll(fds, alive, -1)) <= 0) { |
| 635 return CloseProcessBuffers(fds); |
| 636 } |
| 637 |
| 638 // Process incoming data. |
| 639 for (int i = 0; i < alive; i++) { |
| 640 if (fds[i].revents & POLLIN) { |
| 641 intptr_t avail = FDUtils::AvailableBytes(fds[i].fd); |
| 642 // On Mac OS POLLIN can be set with zero available |
| 643 // bytes. POLLHUP is most likely also set in this case. |
| 644 if (avail > 0) { |
| 645 if (fds[i].fd == out) { |
| 646 if (!out_data.Read(out, avail)) { |
| 647 return CloseProcessBuffers(fds); |
| 648 } |
| 649 } else if (fds[i].fd == err) { |
| 650 if (!err_data.Read(err, avail)) { |
| 651 return CloseProcessBuffers(fds); |
| 652 } |
| 653 } else if (fds[i].fd == exit_event) { |
| 654 if (avail == 8) { |
| 655 intptr_t b = TEMP_FAILURE_RETRY(read(fds[i].fd, |
| 656 exit_code_data.bytes, 8)); |
| 657 if (b != 8) { |
| 658 return CloseProcessBuffers(fds); |
| 659 } |
| 660 } |
| 661 } else { |
| 662 UNREACHABLE(); |
| 663 } |
| 664 } |
| 665 } |
| 666 } |
| 667 |
| 668 // Process closed. |
| 669 for (int i = 0; i < alive; i++) { |
| 670 if (fds[i].revents & POLLHUP) { |
| 671 VOID_TEMP_FAILURE_RETRY(close(fds[i].fd)); |
| 672 alive--; |
| 673 if (i < alive) { |
| 674 fds[i] = fds[alive]; |
| 675 } |
| 676 } |
| 677 } |
| 678 } |
| 679 |
| 680 // All handles closed and all data read. |
| 681 result->set_stdout_data(out_data.GetData()); |
| 682 result->set_stderr_data(err_data.GetData()); |
| 683 |
| 684 // Calculate the exit code. |
| 685 intptr_t exit_code = exit_code_data.ints[0]; |
| 686 intptr_t negative = exit_code_data.ints[1]; |
| 687 if (negative) exit_code = -exit_code; |
| 688 result->set_exit_code(exit_code); |
| 689 |
| 690 return true; |
| 691 } |
| 692 |
| 693 |
| 570 bool Process::Kill(intptr_t id, int signal) { | 694 bool Process::Kill(intptr_t id, int signal) { |
| 571 return (TEMP_FAILURE_RETRY(kill(id, signal)) != -1); | 695 return (TEMP_FAILURE_RETRY(kill(id, signal)) != -1); |
| 572 } | 696 } |
| 573 | 697 |
| 574 | 698 |
| 575 void Process::TerminateExitCodeHandler() { | 699 void Process::TerminateExitCodeHandler() { |
| 576 ExitCodeHandler::TerminateExitCodeThread(); | 700 ExitCodeHandler::TerminateExitCodeThread(); |
| 577 } | 701 } |
| 578 | 702 |
| 579 | 703 |
| 580 intptr_t Process::CurrentProcessId() { | 704 intptr_t Process::CurrentProcessId() { |
| 581 return static_cast<intptr_t>(getpid()); | 705 return static_cast<intptr_t>(getpid()); |
| 582 } | 706 } |
| 583 | 707 |
| 584 } // namespace bin | 708 } // namespace bin |
| 585 } // namespace dart | 709 } // namespace dart |
| 586 | 710 |
| 587 #endif // defined(TARGET_OS_MACOS) | 711 #endif // defined(TARGET_OS_MACOS) |
| OLD | NEW |