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