| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
| 6 | 6 |
| 7 #include "platform/globals.h" | 7 #include "platform/globals.h" |
| 8 #if defined(HOST_OS_FUCHSIA) | 8 #if defined(HOST_OS_FUCHSIA) |
| 9 | 9 |
| 10 #include "bin/process.h" | 10 #include "bin/process.h" |
| 11 | 11 |
| 12 #include <errno.h> | 12 #include <errno.h> |
| 13 #include <fcntl.h> | 13 #include <fcntl.h> |
| 14 #include <launchpad/launchpad.h> | 14 #include <launchpad/launchpad.h> |
| 15 #include <launchpad/vmo.h> | 15 #include <launchpad/vmo.h> |
| 16 #include <magenta/process.h> | 16 #include <magenta/process.h> |
| 17 #include <magenta/status.h> | 17 #include <magenta/status.h> |
| 18 #include <magenta/syscalls.h> | 18 #include <magenta/syscalls.h> |
| 19 #include <magenta/syscalls/object.h> | 19 #include <magenta/syscalls/object.h> |
| 20 #include <magenta/types.h> | 20 #include <magenta/types.h> |
| 21 #include <mxio/io.h> |
| 21 #include <mxio/private.h> | 22 #include <mxio/private.h> |
| 22 #include <mxio/util.h> | 23 #include <mxio/util.h> |
| 23 #include <poll.h> | 24 #include <poll.h> |
| 24 #include <pthread.h> | 25 #include <pthread.h> |
| 25 #include <stdbool.h> | 26 #include <stdbool.h> |
| 26 #include <stdio.h> | 27 #include <stdio.h> |
| 27 #include <stdlib.h> | 28 #include <stdlib.h> |
| 28 #include <string.h> | 29 #include <string.h> |
| 29 #include <unistd.h> | 30 #include <unistd.h> |
| 30 | 31 |
| (...skipping 724 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 755 read_err_ = -1; | 756 read_err_ = -1; |
| 756 FDUtils::SetNonBlocking(write_out_); | 757 FDUtils::SetNonBlocking(write_out_); |
| 757 *out_ = reinterpret_cast<intptr_t>(new IOHandle(write_out_)); | 758 *out_ = reinterpret_cast<intptr_t>(new IOHandle(write_out_)); |
| 758 write_out_ = -1; | 759 write_out_ = -1; |
| 759 FDUtils::SetNonBlocking(exit_pipe_fds[0]); | 760 FDUtils::SetNonBlocking(exit_pipe_fds[0]); |
| 760 *exit_event_ = reinterpret_cast<intptr_t>(new IOHandle(exit_pipe_fds[0])); | 761 *exit_event_ = reinterpret_cast<intptr_t>(new IOHandle(exit_pipe_fds[0])); |
| 761 return 0; | 762 return 0; |
| 762 } | 763 } |
| 763 | 764 |
| 764 private: | 765 private: |
| 765 #define CHECK_FOR_ERROR(status, msg) \ | |
| 766 if (status < 0) { \ | |
| 767 const intptr_t kMaxMessageSize = 256; \ | |
| 768 char* message = DartUtils::ScopedCString(kMaxMessageSize); \ | |
| 769 snprintf(message, kMaxMessageSize, "%s:%d: %s: %s\n", __FILE__, __LINE__, \ | |
| 770 msg, mx_status_get_string(status)); \ | |
| 771 *os_error_message_ = message; \ | |
| 772 return status; \ | |
| 773 } | |
| 774 | |
| 775 mx_status_t SetupLaunchpad(launchpad_t** launchpad) { | 766 mx_status_t SetupLaunchpad(launchpad_t** launchpad) { |
| 776 // Set up a vmo for the binary. | 767 // TODO(zra): Use the supplied working directory when launchpad adds an |
| 777 mx_handle_t binary_vmo = launchpad_vmo_from_file(path_); | 768 // API to set it. |
| 778 CHECK_FOR_ERROR(binary_vmo, "launchpad_vmo_from_file"); | 769 ASSERT(launchpad != NULL); |
| 779 | |
| 780 // Run the child process in the same "job". | |
| 781 mx_handle_t job = MX_HANDLE_INVALID; | |
| 782 mx_status_t status = | |
| 783 mx_handle_duplicate(mx_job_default(), MX_RIGHT_SAME_RIGHTS, &job); | |
| 784 if (status != MX_OK) { | |
| 785 mx_handle_close(binary_vmo); | |
| 786 } | |
| 787 CHECK_FOR_ERROR(status, "mx_handle_duplicate"); | |
| 788 | |
| 789 // Set up the launchpad. | |
| 790 launchpad_t* lp = NULL; | 770 launchpad_t* lp = NULL; |
| 791 launchpad_create(job, program_arguments_[0], &lp); | 771 launchpad_create(MX_HANDLE_INVALID, program_arguments_[0], &lp); |
| 792 launchpad_set_args(lp, program_arguments_count_, program_arguments_); | 772 launchpad_set_args(lp, program_arguments_count_, program_arguments_); |
| 793 launchpad_set_environ(lp, program_environment_); | 773 launchpad_set_environ(lp, program_environment_); |
| 794 launchpad_clone(lp, LP_CLONE_MXIO_NAMESPACE); | 774 launchpad_clone(lp, LP_CLONE_MXIO_NAMESPACE); |
| 795 // TODO(zra): Use the supplied working directory when launchpad adds an | |
| 796 // API to set it. | |
| 797 launchpad_clone(lp, LP_CLONE_MXIO_CWD); | 775 launchpad_clone(lp, LP_CLONE_MXIO_CWD); |
| 798 launchpad_add_pipe(lp, &write_out_, 0); | 776 launchpad_add_pipe(lp, &write_out_, 0); |
| 799 launchpad_add_pipe(lp, &read_in_, 1); | 777 launchpad_add_pipe(lp, &read_in_, 1); |
| 800 launchpad_add_pipe(lp, &read_err_, 2); | 778 launchpad_add_pipe(lp, &read_err_, 2); |
| 801 launchpad_add_vdso_vmo(lp); | 779 launchpad_add_vdso_vmo(lp); |
| 802 launchpad_elf_load(lp, binary_vmo); | 780 launchpad_load_from_file(lp, path_); |
| 803 launchpad_load_vdso(lp, MX_HANDLE_INVALID); | 781 |
| 782 // If there were any errors, grab launchpad's error message and put it in |
| 783 // the os_error_message_ field. |
| 784 mx_status_t status = launchpad_get_status(lp); |
| 785 if (status != MX_OK) { |
| 786 const intptr_t kMaxMessageSize = 256; |
| 787 char* message = DartUtils::ScopedCString(kMaxMessageSize); |
| 788 snprintf(message, kMaxMessageSize, "launchpad failed: %s, %s", |
| 789 mx_status_get_string(status), launchpad_error_message(lp)); |
| 790 *os_error_message_ = message; |
| 791 return status; |
| 792 } |
| 793 |
| 804 *launchpad = lp; | 794 *launchpad = lp; |
| 805 return MX_OK; | 795 return MX_OK; |
| 806 } | 796 } |
| 807 | 797 |
| 808 #undef CHECK_FOR_ERROR | |
| 809 | |
| 810 int read_in_; // Pipe for stdout to child process. | 798 int read_in_; // Pipe for stdout to child process. |
| 811 int read_err_; // Pipe for stderr to child process. | 799 int read_err_; // Pipe for stderr to child process. |
| 812 int write_out_; // Pipe for stdin to child process. | 800 int write_out_; // Pipe for stdin to child process. |
| 813 | 801 |
| 814 char** program_arguments_; | 802 char** program_arguments_; |
| 815 intptr_t program_arguments_count_; | 803 intptr_t program_arguments_count_; |
| 816 char** program_environment_; | 804 char** program_environment_; |
| 817 | 805 |
| 818 const char* path_; | 806 const char* path_; |
| 819 const char* working_directory_; | 807 const char* working_directory_; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 859 } | 847 } |
| 860 | 848 |
| 861 void Process::ClearSignalHandler(intptr_t signal, Dart_Port port) {} | 849 void Process::ClearSignalHandler(intptr_t signal, Dart_Port port) {} |
| 862 | 850 |
| 863 } // namespace bin | 851 } // namespace bin |
| 864 } // namespace dart | 852 } // namespace dart |
| 865 | 853 |
| 866 #endif // defined(HOST_OS_FUCHSIA) | 854 #endif // defined(HOST_OS_FUCHSIA) |
| 867 | 855 |
| 868 #endif // !defined(DART_IO_DISABLED) | 856 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |