blob: 9d7f4455ff333714943ff455018036df249e5120 [file] [log] [blame]
[email protected]d032f492009-09-29 00:33:461// 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
33NaClProcessHost::NaClProcessHost(
[email protected]f08e47d2009-10-15 21:46:1534 ResourceDispatcherHost *resource_dispatcher_host,
35 const std::wstring& url)
[email protected]d032f492009-09-29 00:33:4636 : ChildProcessHost(NACL_PROCESS, resource_dispatcher_host),
37 resource_dispatcher_host_(resource_dispatcher_host) {
[email protected]f08e47d2009-10-15 21:46:1538 set_name(url);
[email protected]d032f492009-09-29 00:33:4639}
40
41bool NaClProcessHost::Launch(ResourceMessageFilter* renderer_msg_filter,
42 const int descriptor,
[email protected]f08e47d2009-10-15 21:46:1543 nacl::FileDescriptor* imc_handle,
44 nacl::FileDescriptor* nacl_process_handle,
45 int* nacl_process_id) {
[email protected]d8c7cbcc2009-10-02 19:00:3146#ifdef DISABLE_NACL
47 NOTIMPLEMENTED() << "Native Client disabled at build time";
48 return false;
49#else
[email protected]d032f492009-09-29 00:33:4650 nacl::Handle pair[2];
51 bool success = false;
[email protected]f08e47d2009-10-15 21:46:1552
53 NATIVE_HANDLE(*imc_handle) = nacl::kInvalidHandle;
54 NATIVE_HANDLE(*nacl_process_handle) = nacl::kInvalidHandle;
55 *nacl_process_id = 0;
56
[email protected]d032f492009-09-29 00:33:4657 // Create a connected socket
58 if (nacl::SocketPair(pair) == -1) {
[email protected]d032f492009-09-29 00:33:4659 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]d032f492009-09-29 00:33:4667 return false;
68 }
69
[email protected]d032f492009-09-29 00:33:4670#if NACL_WINDOWS
[email protected]f08e47d2009-10-15 21:46:1571 // Duplicate the IMC handle
[email protected]d032f492009-09-29 00:33:4672 DuplicateHandle(base::GetCurrentProcessHandle(),
73 reinterpret_cast<HANDLE>(pair[0]),
74 renderer_msg_filter->handle(),
[email protected]f08e47d2009-10-15 21:46:1575 imc_handle,
[email protected]d032f492009-09-29 00:33:4676 GENERIC_READ | GENERIC_WRITE,
77 FALSE,
78 DUPLICATE_CLOSE_SOURCE);
[email protected]f08e47d2009-10-15 21:46:1579
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]d032f492009-09-29 00:33:4689#else
[email protected]f08e47d2009-10-15 21:46:1590 int flags = fcntl(pair[0], F_GETFD);
[email protected]d032f492009-09-29 00:33:4691 if (flags != -1) {
92 flags |= FD_CLOEXEC;
[email protected]f08e47d2009-10-15 21:46:1593 fcntl(pair[0], F_SETFD, flags);
[email protected]d032f492009-09-29 00:33:4694 }
[email protected]f08e47d2009-10-15 21:46:1595 // No need to dup the imc_handle - we don't pass it anywhere else so
[email protected]d032f492009-09-29 00:33:4696 // it cannot be closed.
[email protected]f08e47d2009-10-15 21:46:1597 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]d032f492009-09-29 00:33:46102#endif
103
[email protected]f08e47d2009-10-15 21:46:15104 // Get the pid of the NaCl process
105 *nacl_process_id = base::GetProcId(handle());
106
[email protected]d032f492009-09-29 00:33:46107 return true;
[email protected]d8c7cbcc2009-10-02 19:00:31108#endif // DISABLE_NACL
[email protected]d032f492009-09-29 00:33:46109}
110
111bool NaClProcessHost::LaunchSelLdr(ResourceMessageFilter* renderer_msg_filter,
112 const int descriptor,
[email protected]f08e47d2009-10-15 21:46:15113 const nacl::Handle imc_handle) {
[email protected]d032f492009-09-29 00:33:46114 if (!CreateChannel())
115 return false;
116
117 // Build command line for nacl.
118 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
[email protected]ab926d552009-10-19 22:54:41119 FilePath exe_path =
120 browser_command_line.GetSwitchValuePath(switches::kBrowserSubprocessPath);
[email protected]d032f492009-09-29 00:33:46121 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]ab926d552009-10-19 22:54:41128 // Propagate the following switches to the plugin command line (along with
129 // any associated values) if present in the browser command line.
[email protected]d032f492009-09-29 00:33:46130 // TODO(gregoryd): check which flags of those below can be supported.
[email protected]b7e0a2a2009-10-13 02:07:25131 static const char* const switch_names[] = {
[email protected]d032f492009-09-29 00:33:46132 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]f08e47d2009-10-15 21:46:15174 // send a message with duplicated imc_handle to sel_ldr
175 return SendStartMessage(process, descriptor, imc_handle);
[email protected]d032f492009-09-29 00:33:46176}
177
178bool NaClProcessHost::SendStartMessage(base::ProcessHandle process,
179 int descriptor,
[email protected]f08e47d2009-10-15 21:46:15180 nacl::Handle imc_handle) {
[email protected]d032f492009-09-29 00:33:46181 nacl::FileDescriptor channel;
182#if defined(OS_WIN)
183 if (!DuplicateHandle(GetCurrentProcess(),
[email protected]f08e47d2009-10-15 21:46:15184 reinterpret_cast<HANDLE>(imc_handle),
[email protected]d032f492009-09-29 00:33:46185 process,
186 reinterpret_cast<HANDLE*>(&channel),
187 GENERIC_READ | GENERIC_WRITE,
188 FALSE, DUPLICATE_CLOSE_SOURCE)) {
189 return false;
190 }
191#else
[email protected]f08e47d2009-10-15 21:46:15192 channel.fd = dup(imc_handle);
[email protected]d032f492009-09-29 00:33:46193 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
204void NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
205 NOTREACHED() << "Invalid message with type = " << msg.type();
206}
207
208URLRequestContext* NaClProcessHost::GetRequestContext(
209 uint32 request_id,
210 const ViewHostMsg_Resource_Request& request_data) {
211 return NULL;
212}