[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "build/build_config.h" |
| 6 | |
| 7 | #include "chrome/browser/nacl_process_host.h" |
| 8 | |
| 9 | #if defined(OS_POSIX) |
| 10 | #include <fcntl.h> |
| 11 | #endif |
| 12 | |
| 13 | #if defined(OS_POSIX) |
| 14 | #include "base/global_descriptors_posix.h" |
| 15 | #endif |
| 16 | #include "base/path_service.h" |
| 17 | #include "base/process_util.h" |
| 18 | #include "chrome/browser/renderer_host/resource_message_filter.h" |
| 19 | #include "chrome/common/chrome_descriptors.h" |
| 20 | #include "chrome/common/chrome_switches.h" |
| 21 | #include "chrome/common/logging_chrome.h" |
| 22 | #include "chrome/common/nacl_messages.h" |
| 23 | #include "ipc/ipc_switches.h" |
| 24 | |
| 25 | #if defined(OS_WIN) |
| 26 | #include "chrome/browser/sandbox_policy.h" |
| 27 | #endif |
| 28 | |
| 29 | #if defined(OS_POSIX) |
| 30 | #include "ipc/ipc_channel_posix.h" |
| 31 | #endif |
| 32 | |
| 33 | NaClProcessHost::NaClProcessHost( |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 34 | ResourceDispatcherHost *resource_dispatcher_host, |
| 35 | const std::wstring& url) |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 36 | : ChildProcessHost(NACL_PROCESS, resource_dispatcher_host), |
| 37 | resource_dispatcher_host_(resource_dispatcher_host) { |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 38 | set_name(url); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | bool NaClProcessHost::Launch(ResourceMessageFilter* renderer_msg_filter, |
| 42 | const int descriptor, |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 43 | nacl::FileDescriptor* imc_handle, |
| 44 | nacl::FileDescriptor* nacl_process_handle, |
| 45 | int* nacl_process_id) { |
[email protected] | d8c7cbcc | 2009-10-02 19:00:31 | [diff] [blame] | 46 | #ifdef DISABLE_NACL |
| 47 | NOTIMPLEMENTED() << "Native Client disabled at build time"; |
| 48 | return false; |
| 49 | #else |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 50 | nacl::Handle pair[2]; |
| 51 | bool success = false; |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 52 | |
| 53 | NATIVE_HANDLE(*imc_handle) = nacl::kInvalidHandle; |
| 54 | NATIVE_HANDLE(*nacl_process_handle) = nacl::kInvalidHandle; |
| 55 | *nacl_process_id = 0; |
| 56 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 57 | // Create a connected socket |
| 58 | if (nacl::SocketPair(pair) == -1) { |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | |
| 62 | // Launch the process |
| 63 | success = LaunchSelLdr(renderer_msg_filter, descriptor, pair[1]); |
| 64 | |
| 65 | if (!success) { |
| 66 | nacl::Close(pair[0]); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 70 | #if NACL_WINDOWS |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 71 | // Duplicate the IMC handle |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 72 | DuplicateHandle(base::GetCurrentProcessHandle(), |
| 73 | reinterpret_cast<HANDLE>(pair[0]), |
| 74 | renderer_msg_filter->handle(), |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 75 | imc_handle, |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 76 | GENERIC_READ | GENERIC_WRITE, |
| 77 | FALSE, |
| 78 | DUPLICATE_CLOSE_SOURCE); |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 79 | |
| 80 | // Duplicate the process handle |
| 81 | DuplicateHandle(base::GetCurrentProcessHandle(), |
| 82 | handle(), |
| 83 | renderer_msg_filter->handle(), |
| 84 | nacl_process_handle, |
| 85 | PROCESS_DUP_HANDLE, |
| 86 | FALSE, |
| 87 | 0); |
| 88 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 89 | #else |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 90 | int flags = fcntl(pair[0], F_GETFD); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 91 | if (flags != -1) { |
| 92 | flags |= FD_CLOEXEC; |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 93 | fcntl(pair[0], F_SETFD, flags); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 94 | } |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 95 | // No need to dup the imc_handle - we don't pass it anywhere else so |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 96 | // it cannot be closed. |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 97 | imc_handle->fd = pair[0]; |
| 98 | imc_handle->auto_close = true; |
| 99 | |
| 100 | // Process handles are used on Windows only |
| 101 | NATIVE_HANDLE(*nacl_process_handle) = nacl::kInvalidHandle; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 102 | #endif |
| 103 | |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 104 | // Get the pid of the NaCl process |
| 105 | *nacl_process_id = base::GetProcId(handle()); |
| 106 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 107 | return true; |
[email protected] | d8c7cbcc | 2009-10-02 19:00:31 | [diff] [blame] | 108 | #endif // DISABLE_NACL |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool NaClProcessHost::LaunchSelLdr(ResourceMessageFilter* renderer_msg_filter, |
| 112 | const int descriptor, |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 113 | const nacl::Handle imc_handle) { |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 114 | if (!CreateChannel()) |
| 115 | return false; |
| 116 | |
| 117 | // Build command line for nacl. |
| 118 | const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); |
[email protected] | ab926d55 | 2009-10-19 22:54:41 | [diff] [blame^] | 119 | FilePath exe_path = |
| 120 | browser_command_line.GetSwitchValuePath(switches::kBrowserSubprocessPath); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 121 | if (exe_path.empty() && !PathService::Get(base::FILE_EXE, &exe_path)) |
| 122 | return false; |
| 123 | |
| 124 | CommandLine cmd_line(exe_path); |
| 125 | if (logging::DialogsAreSuppressed()) |
| 126 | cmd_line.AppendSwitch(switches::kNoErrorDialogs); |
| 127 | |
[email protected] | ab926d55 | 2009-10-19 22:54:41 | [diff] [blame^] | 128 | // Propagate the following switches to the plugin command line (along with |
| 129 | // any associated values) if present in the browser command line. |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 130 | // TODO(gregoryd): check which flags of those below can be supported. |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 131 | static const char* const switch_names[] = { |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 132 | switches::kNoSandbox, |
| 133 | switches::kTestSandbox, |
| 134 | switches::kDisableBreakpad, |
| 135 | switches::kFullMemoryCrashReport, |
| 136 | switches::kEnableLogging, |
| 137 | switches::kDisableLogging, |
| 138 | switches::kLoggingLevel, |
| 139 | switches::kEnableDCHECK, |
| 140 | switches::kSilentDumpOnDCHECK, |
| 141 | switches::kMemoryProfiling, |
| 142 | }; |
| 143 | |
| 144 | for (size_t i = 0; i < arraysize(switch_names); ++i) { |
| 145 | if (browser_command_line.HasSwitch(switch_names[i])) { |
| 146 | cmd_line.AppendSwitchWithValue( |
| 147 | switch_names[i], |
| 148 | browser_command_line.GetSwitchValue(switch_names[i])); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | cmd_line.AppendSwitchWithValue(switches::kProcessType, |
| 153 | switches::kNaClProcess); |
| 154 | |
| 155 | cmd_line.AppendSwitchWithValue(switches::kProcessChannelID, |
| 156 | ASCIIToWide(channel_id())); |
| 157 | |
| 158 | base::ProcessHandle process = 0; |
| 159 | #if defined(OS_WIN) |
| 160 | process = sandbox::StartProcess(&cmd_line); |
| 161 | #else |
| 162 | base::file_handle_mapping_vector fds_to_map; |
| 163 | const int ipcfd = channel().GetClientFileDescriptor(); |
| 164 | if (ipcfd > -1) |
| 165 | fds_to_map.push_back(std::pair<int, int>( |
| 166 | ipcfd, kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
| 167 | base::LaunchApp(cmd_line.argv(), fds_to_map, false, &process); |
| 168 | #endif |
| 169 | |
| 170 | if (!process) |
| 171 | return false; |
| 172 | SetHandle(process); |
| 173 | |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 174 | // send a message with duplicated imc_handle to sel_ldr |
| 175 | return SendStartMessage(process, descriptor, imc_handle); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | bool NaClProcessHost::SendStartMessage(base::ProcessHandle process, |
| 179 | int descriptor, |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 180 | nacl::Handle imc_handle) { |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 181 | nacl::FileDescriptor channel; |
| 182 | #if defined(OS_WIN) |
| 183 | if (!DuplicateHandle(GetCurrentProcess(), |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 184 | reinterpret_cast<HANDLE>(imc_handle), |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 185 | process, |
| 186 | reinterpret_cast<HANDLE*>(&channel), |
| 187 | GENERIC_READ | GENERIC_WRITE, |
| 188 | FALSE, DUPLICATE_CLOSE_SOURCE)) { |
| 189 | return false; |
| 190 | } |
| 191 | #else |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 192 | channel.fd = dup(imc_handle); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 193 | channel.auto_close = true; |
| 194 | #endif |
| 195 | NaClProcessMsg_Start* msg = new NaClProcessMsg_Start(descriptor, |
| 196 | channel); |
| 197 | |
| 198 | if (!Send(msg)) { |
| 199 | return false; |
| 200 | } |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | void NaClProcessHost::OnMessageReceived(const IPC::Message& msg) { |
| 205 | NOTREACHED() << "Invalid message with type = " << msg.type(); |
| 206 | } |
| 207 | |
| 208 | URLRequestContext* NaClProcessHost::GetRequestContext( |
| 209 | uint32 request_id, |
| 210 | const ViewHostMsg_Resource_Request& request_data) { |
| 211 | return NULL; |
| 212 | } |