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