OLD | NEW |
(Empty) | |
| 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( |
| 34 ResourceDispatcherHost *resource_dispatcher_host) |
| 35 : ChildProcessHost(NACL_PROCESS, resource_dispatcher_host), |
| 36 resource_dispatcher_host_(resource_dispatcher_host) { |
| 37 } |
| 38 |
| 39 bool NaClProcessHost::Launch(ResourceMessageFilter* renderer_msg_filter, |
| 40 const int descriptor, |
| 41 nacl::FileDescriptor* handle) { |
| 42 nacl::Handle pair[2]; |
| 43 bool success = false; |
| 44 // Create a connected socket |
| 45 if (nacl::SocketPair(pair) == -1) { |
| 46 NATIVE_HANDLE(*handle) = nacl::kInvalidHandle; |
| 47 return false; |
| 48 } |
| 49 |
| 50 // Launch the process |
| 51 success = LaunchSelLdr(renderer_msg_filter, descriptor, pair[1]); |
| 52 |
| 53 if (!success) { |
| 54 nacl::Close(pair[0]); |
| 55 NATIVE_HANDLE(*handle) = nacl::kInvalidHandle; |
| 56 return false; |
| 57 } |
| 58 |
| 59 nacl::Handle duplicate_handle = nacl::kInvalidHandle; |
| 60 #if NACL_WINDOWS |
| 61 DuplicateHandle(base::GetCurrentProcessHandle(), |
| 62 reinterpret_cast<HANDLE>(pair[0]), |
| 63 renderer_msg_filter->handle(), |
| 64 reinterpret_cast<HANDLE*>(&duplicate_handle), |
| 65 GENERIC_READ | GENERIC_WRITE, |
| 66 FALSE, |
| 67 DUPLICATE_CLOSE_SOURCE); |
| 68 *handle = duplicate_handle; |
| 69 #else |
| 70 duplicate_handle = pair[0]; |
| 71 int flags = fcntl(duplicate_handle, F_GETFD); |
| 72 if (flags != -1) { |
| 73 flags |= FD_CLOEXEC; |
| 74 fcntl(duplicate_handle, F_SETFD, flags); |
| 75 } |
| 76 // No need to dup the handle - we don't pass it anywhere else so |
| 77 // it cannot be closed. |
| 78 handle->fd = duplicate_handle; |
| 79 handle->auto_close = true; |
| 80 #endif |
| 81 |
| 82 return true; |
| 83 } |
| 84 |
| 85 bool NaClProcessHost::LaunchSelLdr(ResourceMessageFilter* renderer_msg_filter, |
| 86 const int descriptor, |
| 87 const nacl::Handle handle) { |
| 88 if (!CreateChannel()) |
| 89 return false; |
| 90 |
| 91 // Build command line for nacl. |
| 92 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); |
| 93 std::wstring exe_path = |
| 94 browser_command_line.GetSwitchValue(switches::kBrowserSubprocessPath); |
| 95 if (exe_path.empty() && !PathService::Get(base::FILE_EXE, &exe_path)) |
| 96 return false; |
| 97 |
| 98 CommandLine cmd_line(exe_path); |
| 99 if (logging::DialogsAreSuppressed()) |
| 100 cmd_line.AppendSwitch(switches::kNoErrorDialogs); |
| 101 |
| 102 // propagate the following switches to the plugin command line (along with |
| 103 // any associated values) if present in the browser command line |
| 104 // TODO(gregoryd): check which flags of those below can be supported. |
| 105 static const wchar_t* const switch_names[] = { |
| 106 switches::kNoSandbox, |
| 107 switches::kTestSandbox, |
| 108 switches::kDisableBreakpad, |
| 109 switches::kFullMemoryCrashReport, |
| 110 switches::kEnableLogging, |
| 111 switches::kDisableLogging, |
| 112 switches::kLoggingLevel, |
| 113 switches::kEnableDCHECK, |
| 114 switches::kSilentDumpOnDCHECK, |
| 115 switches::kMemoryProfiling, |
| 116 }; |
| 117 |
| 118 for (size_t i = 0; i < arraysize(switch_names); ++i) { |
| 119 if (browser_command_line.HasSwitch(switch_names[i])) { |
| 120 cmd_line.AppendSwitchWithValue( |
| 121 switch_names[i], |
| 122 browser_command_line.GetSwitchValue(switch_names[i])); |
| 123 } |
| 124 } |
| 125 |
| 126 cmd_line.AppendSwitchWithValue(switches::kProcessType, |
| 127 switches::kNaClProcess); |
| 128 |
| 129 cmd_line.AppendSwitchWithValue(switches::kProcessChannelID, |
| 130 ASCIIToWide(channel_id())); |
| 131 |
| 132 base::ProcessHandle process = 0; |
| 133 #if defined(OS_WIN) |
| 134 process = sandbox::StartProcess(&cmd_line); |
| 135 #else |
| 136 base::file_handle_mapping_vector fds_to_map; |
| 137 const int ipcfd = channel().GetClientFileDescriptor(); |
| 138 if (ipcfd > -1) |
| 139 fds_to_map.push_back(std::pair<int, int>( |
| 140 ipcfd, kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
| 141 base::LaunchApp(cmd_line.argv(), fds_to_map, false, &process); |
| 142 #endif |
| 143 |
| 144 if (!process) |
| 145 return false; |
| 146 SetHandle(process); |
| 147 |
| 148 // send a message with duplicated handle to sel_ldr |
| 149 return SendStartMessage(process, descriptor, handle); |
| 150 } |
| 151 |
| 152 bool NaClProcessHost::SendStartMessage(base::ProcessHandle process, |
| 153 int descriptor, |
| 154 nacl::Handle handle) { |
| 155 nacl::FileDescriptor channel; |
| 156 #if defined(OS_WIN) |
| 157 if (!DuplicateHandle(GetCurrentProcess(), |
| 158 reinterpret_cast<HANDLE>(handle), |
| 159 process, |
| 160 reinterpret_cast<HANDLE*>(&channel), |
| 161 GENERIC_READ | GENERIC_WRITE, |
| 162 FALSE, DUPLICATE_CLOSE_SOURCE)) { |
| 163 return false; |
| 164 } |
| 165 #else |
| 166 channel.fd = dup(handle); |
| 167 channel.auto_close = true; |
| 168 #endif |
| 169 NaClProcessMsg_StartSelLdr* msg = new NaClProcessMsg_StartSelLdr( |
| 170 descriptor, channel); |
| 171 |
| 172 if (!Send(msg)) { |
| 173 return false; |
| 174 } |
| 175 return true; |
| 176 } |
| 177 |
| 178 void NaClProcessHost::OnMessageReceived(const IPC::Message& msg) { |
| 179 NOTREACHED() << "Invalid message with type = " << msg.type(); |
| 180 } |
| 181 |
| 182 URLRequestContext* NaClProcessHost::GetRequestContext( |
| 183 uint32 request_id, |
| 184 const ViewHostMsg_Resource_Request& request_data) { |
| 185 return NULL; |
| 186 } |
| 187 |
OLD | NEW |