| 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 |
| 11 #include <fcntl.h> // NOLINT | 11 #include <fcntl.h> // NOLINT |
| 12 #include <poll.h> // NOLINT | 12 #include <poll.h> // NOLINT |
| 13 #include <signal.h> // NOLINT | 13 #include <signal.h> // NOLINT |
| 14 #include <stdio.h> // NOLINT | 14 #include <stdio.h> // NOLINT |
| 15 #include <stdlib.h> // NOLINT | 15 #include <stdlib.h> // NOLINT |
| 16 #include <string.h> // NOLINT | 16 #include <string.h> // NOLINT |
| 17 #include <sys/wait.h> // NOLINT | 17 #include <sys/wait.h> // NOLINT |
| 18 #include <unistd.h> // NOLINT | 18 #include <unistd.h> // NOLINT |
| 19 | 19 |
| 20 #include "bin/fdutils.h" | 20 #include "bin/fdutils.h" |
| 21 #include "bin/log.h" | 21 #include "bin/log.h" |
| 22 #include "bin/thread.h" | 22 #include "bin/thread.h" |
| 23 | 23 |
| 24 |
| 24 extern char **environ; | 25 extern char **environ; |
| 25 | 26 |
| 26 | 27 |
| 27 namespace dart { | 28 namespace dart { |
| 28 namespace bin { | 29 namespace bin { |
| 29 | 30 |
| 30 // ProcessInfo is used to map a process id to the file descriptor for | 31 // ProcessInfo is used to map a process id to the file descriptor for |
| 31 // the pipe used to communicate the exit code of the process to Dart. | 32 // the pipe used to communicate the exit code of the process to Dart. |
| 32 // ProcessInfo objects are kept in the static singly-linked | 33 // ProcessInfo objects are kept in the static singly-linked |
| 33 // ProcessInfoList. | 34 // ProcessInfoList. |
| (...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 TEMP_FAILURE_RETRY(close(write_out[0])); | 562 TEMP_FAILURE_RETRY(close(write_out[0])); |
| 562 FDUtils::SetNonBlocking(read_err[0]); | 563 FDUtils::SetNonBlocking(read_err[0]); |
| 563 *err = read_err[0]; | 564 *err = read_err[0]; |
| 564 TEMP_FAILURE_RETRY(close(read_err[1])); | 565 TEMP_FAILURE_RETRY(close(read_err[1])); |
| 565 | 566 |
| 566 *id = pid; | 567 *id = pid; |
| 567 return 0; | 568 return 0; |
| 568 } | 569 } |
| 569 | 570 |
| 570 | 571 |
| 572 class BufferList: public BufferListBase { |
| 573 public: |
| 574 bool Read(int fd, intptr_t available) { |
| 575 // Read all available bytes. |
| 576 while (available > 0) { |
| 577 if (free_size_ == 0) Allocate(); |
| 578 ASSERT(free_size_ > 0); |
| 579 ASSERT(free_size_ <= kBufferSize); |
| 580 intptr_t block_size = dart::Utils::Minimum(free_size_, available); |
| 581 intptr_t bytes = TEMP_FAILURE_RETRY(read( |
| 582 fd, |
| 583 reinterpret_cast<void*>(tail_->data_ + (kBufferSize - free_size_)), |
| 584 block_size)); |
| 585 if (bytes < 0) return false; |
| 586 data_size_ += bytes; |
| 587 free_size_ -= bytes; |
| 588 available -= bytes; |
| 589 } |
| 590 return true; |
| 591 } |
| 592 }; |
| 593 |
| 594 |
| 595 static bool CloseProcessBuffers(struct pollfd fds[3]) { |
| 596 int e = errno; |
| 597 VOID_TEMP_FAILURE_RETRY(close(fds[0].fd)); |
| 598 VOID_TEMP_FAILURE_RETRY(close(fds[1].fd)); |
| 599 VOID_TEMP_FAILURE_RETRY(close(fds[2].fd)); |
| 600 errno = e; |
| 601 return false; |
| 602 } |
| 603 |
| 604 |
| 605 bool Process::Wait(intptr_t pid, |
| 606 intptr_t in, |
| 607 intptr_t out, |
| 608 intptr_t err, |
| 609 intptr_t exit_event, |
| 610 ProcessResult* result) { |
| 611 // Close input to the process right away. |
| 612 VOID_TEMP_FAILURE_RETRY(close(in)); |
| 613 |
| 614 // There is no return from this function using Dart_PropagateError |
| 615 // as memory used by the buffer lists is freed through their |
| 616 // destructors. |
| 617 BufferList out_data; |
| 618 BufferList err_data; |
| 619 union { |
| 620 uint8_t bytes[8]; |
| 621 int32_t ints[2]; |
| 622 } exit_code_data; |
| 623 |
| 624 struct pollfd fds[3]; |
| 625 fds[0].fd = out; |
| 626 fds[1].fd = err; |
| 627 fds[2].fd = exit_event; |
| 628 |
| 629 for (int i = 0; i < 3; i++) { |
| 630 fds[i].events = POLLIN; |
| 631 } |
| 632 |
| 633 int alive = 3; |
| 634 while (alive > 0) { |
| 635 // Blocking call waiting for events from the child process. |
| 636 if (TEMP_FAILURE_RETRY(poll(fds, alive, -1)) <= 0) { |
| 637 return CloseProcessBuffers(fds); |
| 638 } |
| 639 |
| 640 // Process incoming data. |
| 641 for (int i = 0; i < alive; i++) { |
| 642 if (fds[i].revents & POLLIN) { |
| 643 intptr_t avail = FDUtils::AvailableBytes(fds[i].fd); |
| 644 if (fds[i].fd == out) { |
| 645 if (!out_data.Read(out, avail)) { |
| 646 return CloseProcessBuffers(fds); |
| 647 } |
| 648 } else if (fds[i].fd == err) { |
| 649 if (!err_data.Read(err, avail)) { |
| 650 return CloseProcessBuffers(fds); |
| 651 } |
| 652 } else if (fds[i].fd == exit_event) { |
| 653 if (avail == 8) { |
| 654 intptr_t b = TEMP_FAILURE_RETRY(read(exit_event, |
| 655 exit_code_data.bytes, 8)); |
| 656 if (b != 8) { |
| 657 return CloseProcessBuffers(fds); |
| 658 } |
| 659 } |
| 660 } else { |
| 661 UNREACHABLE(); |
| 662 } |
| 663 } |
| 664 } |
| 665 |
| 666 // Process closed. |
| 667 for (int i = 0; i < alive; i++) { |
| 668 if (fds[i].revents & POLLHUP) { |
| 669 VOID_TEMP_FAILURE_RETRY(close(fds[i].fd)); |
| 670 alive--; |
| 671 if (i < alive) { |
| 672 fds[i] = fds[alive]; |
| 673 } |
| 674 } |
| 675 } |
| 676 } |
| 677 |
| 678 // All handles closed and all data read. |
| 679 result->set_stdout_data(out_data.GetData()); |
| 680 result->set_stderr_data(err_data.GetData()); |
| 681 |
| 682 // Calculate the exit code. |
| 683 intptr_t exit_code = exit_code_data.ints[0]; |
| 684 intptr_t negative = exit_code_data.ints[1]; |
| 685 if (negative) exit_code = -exit_code; |
| 686 result->set_exit_code(exit_code); |
| 687 |
| 688 return true; |
| 689 } |
| 690 |
| 691 |
| 571 bool Process::Kill(intptr_t id, int signal) { | 692 bool Process::Kill(intptr_t id, int signal) { |
| 572 return (TEMP_FAILURE_RETRY(kill(id, signal)) != -1); | 693 return (TEMP_FAILURE_RETRY(kill(id, signal)) != -1); |
| 573 } | 694 } |
| 574 | 695 |
| 575 | 696 |
| 576 void Process::TerminateExitCodeHandler() { | 697 void Process::TerminateExitCodeHandler() { |
| 577 ExitCodeHandler::TerminateExitCodeThread(); | 698 ExitCodeHandler::TerminateExitCodeThread(); |
| 578 } | 699 } |
| 579 | 700 |
| 580 | 701 |
| 581 intptr_t Process::CurrentProcessId() { | 702 intptr_t Process::CurrentProcessId() { |
| 582 return static_cast<intptr_t>(getpid()); | 703 return static_cast<intptr_t>(getpid()); |
| 583 } | 704 } |
| 584 | 705 |
| 585 } // namespace bin | 706 } // namespace bin |
| 586 } // namespace dart | 707 } // namespace dart |
| 587 | 708 |
| 588 #endif // defined(TARGET_OS_LINUX) | 709 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |