[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame^] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 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 | |
[email protected] | e15a4fa | 2010-02-11 23:09:29 | [diff] [blame] | 7 | #include "chrome/browser/nacl_host/nacl_process_host.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 8 | |
| 9 | #if defined(OS_POSIX) |
| 10 | #include <fcntl.h> |
| 11 | #endif |
| 12 | |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 13 | #include "base/command_line.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 14 | #include "chrome/browser/renderer_host/resource_message_filter.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 15 | #include "chrome/common/chrome_switches.h" |
| 16 | #include "chrome/common/logging_chrome.h" |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 17 | #include "chrome/common/nacl_cmd_line.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 18 | #include "chrome/common/nacl_messages.h" |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 19 | #include "chrome/common/render_messages.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 20 | #include "ipc/ipc_switches.h" |
| 21 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 22 | #if defined(OS_POSIX) |
| 23 | #include "ipc/ipc_channel_posix.h" |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame^] | 24 | #elif defined(OS_WIN) |
| 25 | #include "chrome/browser/nacl_host/nacl_broker_service_win.h" |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | NaClProcessHost::NaClProcessHost( |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 29 | ResourceDispatcherHost *resource_dispatcher_host, |
| 30 | const std::wstring& url) |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 31 | : ChildProcessHost(NACL_LOADER_PROCESS, resource_dispatcher_host), |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 32 | resource_dispatcher_host_(resource_dispatcher_host), |
| 33 | reply_msg_(NULL), |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 34 | descriptor_(0), |
| 35 | running_on_wow64_(false) { |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 36 | set_name(url); |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 37 | #if defined(OS_WIN) |
| 38 | CheckIsWow64(); |
| 39 | #endif |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 42 | NaClProcessHost::~NaClProcessHost() { |
| 43 | if (!reply_msg_) |
| 44 | return; |
| 45 | |
| 46 | // OnProcessLaunched didn't get called because the process couldn't launch. |
| 47 | // Don't keep the renderer hanging. |
| 48 | reply_msg_->set_reply_error(); |
| 49 | resource_message_filter_->Send(reply_msg_); |
| 50 | } |
| 51 | |
| 52 | bool NaClProcessHost::Launch(ResourceMessageFilter* resource_message_filter, |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 53 | const int descriptor, |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 54 | IPC::Message* reply_msg) { |
[email protected] | d8c7cbcc | 2009-10-02 19:00:31 | [diff] [blame] | 55 | #ifdef DISABLE_NACL |
| 56 | NOTIMPLEMENTED() << "Native Client disabled at build time"; |
| 57 | return false; |
| 58 | #else |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 59 | // Create a connected socket |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 60 | if (nacl::SocketPair(pair_) == -1) |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 61 | return false; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 62 | |
| 63 | // Launch the process |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 64 | descriptor_ = descriptor; |
| 65 | if (!LaunchSelLdr()) { |
| 66 | nacl::Close(pair_[0]); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 70 | resource_message_filter_ = resource_message_filter; |
| 71 | reply_msg_ = reply_msg; |
[email protected] | f08e47d | 2009-10-15 21:46:15 | [diff] [blame] | 72 | |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 73 | return true; |
[email protected] | d8c7cbcc | 2009-10-02 19:00:31 | [diff] [blame] | 74 | #endif // DISABLE_NACL |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 77 | bool NaClProcessHost::LaunchSelLdr() { |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 78 | if (!CreateChannel()) |
| 79 | return false; |
| 80 | |
| 81 | // Build command line for nacl. |
[email protected] | 7c4ea14 | 2010-01-26 05:15:42 | [diff] [blame] | 82 | FilePath exe_path = GetChildPath(true); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 83 | if (exe_path.empty()) |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 84 | return false; |
| 85 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 86 | CommandLine* cmd_line = new CommandLine(exe_path); |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 87 | nacl::CopyNaClCommandLineArguments(cmd_line); |
[email protected] | 599e664 | 2010-01-27 18:52:13 | [diff] [blame] | 88 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 89 | cmd_line->AppendSwitchWithValue(switches::kProcessType, |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 90 | switches::kNaClLoaderProcess); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 91 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 92 | cmd_line->AppendSwitchWithValue(switches::kProcessChannelID, |
| 93 | ASCIIToWide(channel_id())); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 94 | |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 95 | // On Windows we might need to start the broker process to launch a new loader |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 96 | #if defined(OS_WIN) |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 97 | if (running_on_wow64_) { |
| 98 | NaClBrokerService::GetInstance()->Init(resource_dispatcher_host_); |
[email protected] | 2a4d754 | 2010-03-17 02:06:33 | [diff] [blame] | 99 | return NaClBrokerService::GetInstance()->LaunchLoader(this, |
| 100 | ASCIIToWide(channel_id())); |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame^] | 101 | } else { |
| 102 | ChildProcessHost::Launch(FilePath(), cmd_line); |
| 103 | } |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 104 | #elif defined(OS_POSIX) |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame^] | 105 | ChildProcessHost::Launch(true, // use_zygote |
| 106 | base::environment_vector(), |
| 107 | cmd_line); |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 108 | #endif |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 109 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 110 | return true; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 113 | void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) { |
| 114 | set_handle(handle); |
| 115 | OnProcessLaunched(); |
| 116 | } |
| 117 | |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 118 | bool NaClProcessHost::DidChildCrash() { |
| 119 | if (running_on_wow64_) |
| 120 | return base::DidProcessCrash(NULL, handle()); |
| 121 | return ChildProcessHost::DidChildCrash(); |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | 16e70ae | 2010-03-08 21:41:28 | [diff] [blame] | 124 | void NaClProcessHost::OnChildDied() { |
| 125 | #if defined(OS_WIN) |
| 126 | NaClBrokerService::GetInstance()->OnLoaderDied(); |
| 127 | #endif |
| 128 | ChildProcessHost::OnChildDied(); |
| 129 | } |
| 130 | |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 131 | void NaClProcessHost::OnProcessLaunched() { |
| 132 | nacl::FileDescriptor imc_handle; |
| 133 | base::ProcessHandle nacl_process_handle; |
[email protected] | 16e70ae | 2010-03-08 21:41:28 | [diff] [blame] | 134 | #if defined(OS_WIN) |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 135 | // Duplicate the IMC handle |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 136 | // We assume the size of imc_handle has the same size as HANDLE, so the cast |
| 137 | // below is safe. |
| 138 | DCHECK(sizeof(HANDLE) == sizeof(imc_handle)); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 139 | DuplicateHandle(base::GetCurrentProcessHandle(), |
| 140 | reinterpret_cast<HANDLE>(pair_[0]), |
| 141 | resource_message_filter_->handle(), |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 142 | reinterpret_cast<HANDLE*>(&imc_handle), |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 143 | GENERIC_READ | GENERIC_WRITE, |
| 144 | FALSE, |
| 145 | DUPLICATE_CLOSE_SOURCE); |
| 146 | |
| 147 | // Duplicate the process handle |
| 148 | DuplicateHandle(base::GetCurrentProcessHandle(), |
| 149 | handle(), |
| 150 | resource_message_filter_->handle(), |
| 151 | &nacl_process_handle, |
| 152 | PROCESS_DUP_HANDLE, |
| 153 | FALSE, |
| 154 | 0); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 155 | #else |
| 156 | int flags = fcntl(pair_[0], F_GETFD); |
| 157 | if (flags != -1) { |
| 158 | flags |= FD_CLOEXEC; |
| 159 | fcntl(pair_[0], F_SETFD, flags); |
| 160 | } |
| 161 | // No need to dup the imc_handle - we don't pass it anywhere else so |
| 162 | // it cannot be closed. |
| 163 | imc_handle.fd = pair_[0]; |
| 164 | imc_handle.auto_close = true; |
| 165 | |
| 166 | // We use pid as process handle on Posix |
| 167 | nacl_process_handle = handle(); |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 168 | #endif |
| 169 | |
| 170 | // Get the pid of the NaCl process |
| 171 | base::ProcessId nacl_process_id = base::GetProcId(handle()); |
| 172 | |
| 173 | ViewHostMsg_LaunchNaCl::WriteReplyParams( |
| 174 | reply_msg_, imc_handle, nacl_process_handle, nacl_process_id); |
| 175 | resource_message_filter_->Send(reply_msg_); |
| 176 | resource_message_filter_ = NULL; |
| 177 | reply_msg_ = NULL; |
| 178 | |
| 179 | SendStartMessage(); |
| 180 | } |
| 181 | |
| 182 | void NaClProcessHost::SendStartMessage() { |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 183 | nacl::FileDescriptor channel; |
| 184 | #if defined(OS_WIN) |
| 185 | if (!DuplicateHandle(GetCurrentProcess(), |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 186 | reinterpret_cast<HANDLE>(pair_[1]), |
| 187 | handle(), |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 188 | reinterpret_cast<HANDLE*>(&channel), |
| 189 | GENERIC_READ | GENERIC_WRITE, |
| 190 | FALSE, DUPLICATE_CLOSE_SOURCE)) { |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 191 | return; |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 192 | } |
| 193 | #else |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 194 | channel.fd = dup(pair_[1]); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 195 | channel.auto_close = true; |
| 196 | #endif |
[email protected] | fb1277e8 | 2009-11-21 20:32:30 | [diff] [blame] | 197 | Send(new NaClProcessMsg_Start(descriptor_, channel)); |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void NaClProcessHost::OnMessageReceived(const IPC::Message& msg) { |
| 201 | NOTREACHED() << "Invalid message with type = " << msg.type(); |
| 202 | } |
| 203 | |
| 204 | URLRequestContext* NaClProcessHost::GetRequestContext( |
| 205 | uint32 request_id, |
| 206 | const ViewHostMsg_Resource_Request& request_data) { |
| 207 | return NULL; |
| 208 | } |
[email protected] | 103607e | 2010-02-01 18:57:09 | [diff] [blame] | 209 | |
| 210 | #if defined(OS_WIN) |
| 211 | // TODO(gregoryd): invoke CheckIsWow64 only once, not for each NaClProcessHost |
| 212 | typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); |
| 213 | void NaClProcessHost::CheckIsWow64() { |
| 214 | LPFN_ISWOW64PROCESS fnIsWow64Process; |
| 215 | |
| 216 | fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress( |
| 217 | GetModuleHandle(TEXT("kernel32")), |
| 218 | "IsWow64Process"); |
| 219 | |
| 220 | if (fnIsWow64Process != NULL) { |
| 221 | BOOL bIsWow64 = FALSE; |
| 222 | if (fnIsWow64Process(GetCurrentProcess(),&bIsWow64)) { |
| 223 | if (bIsWow64) { |
| 224 | running_on_wow64_ = true; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | #endif |