blob: f9ac0f54775d20435ac6ded89f728ccc8e0d5249 [file] [log] [blame]
[email protected]1e67c2b2011-03-04 01:17:371// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]d032f492009-09-29 00:33:462// 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]e15a4fa2010-02-11 23:09:297#include "chrome/browser/nacl_host/nacl_process_host.h"
[email protected]d032f492009-09-29 00:33:468
9#if defined(OS_POSIX)
10#include <fcntl.h>
11#endif
12
[email protected]30c1eea2011-10-17 18:40:3013#include "base/bind.h"
[email protected]103607e2010-02-01 18:57:0914#include "base/command_line.h"
[email protected]338466a82011-05-03 04:27:4315#include "base/path_service.h"
[email protected]a0a69bf2011-09-23 21:40:2816#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2217#include "base/utf_string_conversions.h"
[email protected]1e67c2b2011-03-04 01:17:3718#include "base/win/windows_version.h"
[email protected]338466a82011-05-03 04:27:4319#include "chrome/common/chrome_paths.h"
[email protected]d032f492009-09-29 00:33:4620#include "chrome/common/chrome_switches.h"
21#include "chrome/common/logging_chrome.h"
[email protected]103607e2010-02-01 18:57:0922#include "chrome/common/nacl_cmd_line.h"
[email protected]d032f492009-09-29 00:33:4623#include "chrome/common/nacl_messages.h"
[email protected]fb1277e82009-11-21 20:32:3024#include "chrome/common/render_messages.h"
[email protected]92d56412011-03-24 20:53:5225#include "chrome/browser/renderer_host/chrome_render_message_filter.h"
[email protected]4cb43102011-12-02 20:24:4926#include "content/common/child_process_host.h"
[email protected]d032f492009-09-29 00:33:4627#include "ipc/ipc_switches.h"
[email protected]1d8a3d1f2011-02-19 07:11:5228#include "native_client/src/shared/imc/nacl_imc.h"
[email protected]d032f492009-09-29 00:33:4629
[email protected]d032f492009-09-29 00:33:4630#if defined(OS_POSIX)
31#include "ipc/ipc_channel_posix.h"
[email protected]4bdde602010-06-16 03:17:3532#elif defined(OS_WIN)
33#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
[email protected]d032f492009-09-29 00:33:4634#endif
35
[email protected]631bb742011-11-02 11:29:3936using content::BrowserThread;
37
[email protected]c47ec402010-07-29 10:20:4938namespace {
39
[email protected]c47ec402010-07-29 10:20:4940void SetCloseOnExec(nacl::Handle fd) {
41#if defined(OS_POSIX)
42 int flags = fcntl(fd, F_GETFD);
[email protected]773ebb92011-11-15 19:06:5243 CHECK_NE(flags, -1);
[email protected]c47ec402010-07-29 10:20:4944 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
[email protected]773ebb92011-11-15 19:06:5245 CHECK_EQ(rc, 0);
[email protected]c47ec402010-07-29 10:20:4946#endif
47}
[email protected]c47ec402010-07-29 10:20:4948
[email protected]773ebb92011-11-15 19:06:5249// Represents shared state for all NaClProcessHost objects in the browser.
50// Currently this just handles holding onto the file descriptor for the IRT.
51class NaClBrowser {
52 public:
53 static NaClBrowser* GetInstance() {
54 return Singleton<NaClBrowser>::get();
55 }
56
57 bool IrtAvailable() const {
58 return irt_platform_file_ != base::kInvalidPlatformFileValue;
59 }
60
61 base::PlatformFile IrtFile() const {
62 CHECK_NE(irt_platform_file_, base::kInvalidPlatformFileValue);
63 return irt_platform_file_;
64 }
65
66 // Asynchronously attempt to get the IRT open.
67 bool EnsureIrtAvailable();
68
69 // Make sure the IRT gets opened and follow up with the reply when it's ready.
70 bool MakeIrtAvailable(const base::Closure& reply);
71
72 private:
73 base::PlatformFile irt_platform_file_;
74
75 friend struct DefaultSingletonTraits<NaClBrowser>;
76
77 NaClBrowser()
78 : irt_platform_file_(base::kInvalidPlatformFileValue)
79 {}
80
81 ~NaClBrowser() {
82 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
83 base::ClosePlatformFile(irt_platform_file_);
84 }
85
86 void OpenIrtLibraryFile();
87
88 static void DoOpenIrtLibraryFile() {
89 GetInstance()->OpenIrtLibraryFile();
90 }
91
92 DISALLOW_COPY_AND_ASSIGN(NaClBrowser);
93};
94
[email protected]c47ec402010-07-29 10:20:4995} // namespace
96
[email protected]1d8a3d1f2011-02-19 07:11:5297struct NaClProcessHost::NaClInternal {
98 std::vector<nacl::Handle> sockets_for_renderer;
99 std::vector<nacl::Handle> sockets_for_sel_ldr;
100};
101
[email protected]773ebb92011-11-15 19:06:52102static bool RunningOnWOW64() {
103#if defined(OS_WIN)
104 return (base::win::OSInfo::GetInstance()->wow64_status() ==
105 base::win::OSInfo::WOW64_ENABLED);
106#else
107 return false;
108#endif
109}
110
[email protected]1dbaaa702011-04-06 17:43:42111NaClProcessHost::NaClProcessHost(const std::wstring& url)
[email protected]bd5d6cf2011-12-01 00:39:12112 : BrowserChildProcessHost(content::PROCESS_TYPE_NACL_LOADER),
[email protected]fb1277e82009-11-21 20:32:30113 reply_msg_(NULL),
[email protected]1d8a3d1f2011-02-19 07:11:52114 internal_(new NaClInternal()),
[email protected]30c1eea2011-10-17 18:40:30115 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
[email protected]68b9e72b2011-08-05 23:08:22116 set_name(WideToUTF16Hack(url));
[email protected]d032f492009-09-29 00:33:46117}
118
[email protected]fb1277e82009-11-21 20:32:30119NaClProcessHost::~NaClProcessHost() {
[email protected]4cb43102011-12-02 20:24:49120 int exit_code;
121 GetChildTerminationStatus(&exit_code);
122 std::string message =
123 base::StringPrintf("NaCl process exited with status %i (0x%x)",
124 exit_code, exit_code);
125 if (exit_code == 0) {
126 LOG(INFO) << message;
127 } else {
128 LOG(ERROR) << message;
129 }
130
131#if defined(OS_WIN)
132 NaClBrokerService::GetInstance()->OnLoaderDied();
133#endif
134
[email protected]1d8a3d1f2011-02-19 07:11:52135 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]909c2402011-05-09 11:39:04136 if (nacl::Close(internal_->sockets_for_renderer[i]) != 0) {
137 LOG(ERROR) << "nacl::Close() failed";
138 }
[email protected]c47ec402010-07-29 10:20:49139 }
[email protected]1d8a3d1f2011-02-19 07:11:52140 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]909c2402011-05-09 11:39:04141 if (nacl::Close(internal_->sockets_for_sel_ldr[i]) != 0) {
142 LOG(ERROR) << "nacl::Close() failed";
143 }
[email protected]c47ec402010-07-29 10:20:49144 }
[email protected]c47ec402010-07-29 10:20:49145
[email protected]909c2402011-05-09 11:39:04146 if (reply_msg_) {
147 // The process failed to launch for some reason.
148 // Don't keep the renderer hanging.
149 reply_msg_->set_reply_error();
150 chrome_render_message_filter_->Send(reply_msg_);
151 }
[email protected]463ea5f2011-12-03 06:57:47152
153#if defined(OS_WIN)
154 NaClBrokerService::GetInstance()->OnLoaderDied();
155#endif
[email protected]fb1277e82009-11-21 20:32:30156}
157
[email protected]773ebb92011-11-15 19:06:52158// Attempt to ensure the IRT will be available when we need it, but don't wait.
159bool NaClBrowser::EnsureIrtAvailable() {
160 if (IrtAvailable())
161 return true;
162
163 return BrowserThread::PostTask(
164 BrowserThread::FILE, FROM_HERE,
165 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile));
166}
167
168// We really need the IRT to be available now, so make sure that it is.
169// When it's ready, we'll run the reply closure.
170bool NaClBrowser::MakeIrtAvailable(const base::Closure& reply) {
171 return BrowserThread::PostTaskAndReply(
172 BrowserThread::FILE, FROM_HERE,
173 base::Bind(&NaClBrowser::DoOpenIrtLibraryFile), reply);
174}
175
176// This is called at browser startup.
177// static
178void NaClProcessHost::EarlyStartup() {
179#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
180 // Open the IRT file early to make sure that it isn't replaced out from
181 // under us by autoupdate.
182 NaClBrowser::GetInstance()->EnsureIrtAvailable();
183#endif
184}
185
[email protected]92d56412011-03-24 20:53:52186bool NaClProcessHost::Launch(
187 ChromeRenderMessageFilter* chrome_render_message_filter,
188 int socket_count,
189 IPC::Message* reply_msg) {
[email protected]c47ec402010-07-29 10:20:49190 // Place an arbitrary limit on the number of sockets to limit
191 // exposure in case the renderer is compromised. We can increase
192 // this if necessary.
193 if (socket_count > 8) {
[email protected]d032f492009-09-29 00:33:46194 return false;
[email protected]c47ec402010-07-29 10:20:49195 }
196
[email protected]773ebb92011-11-15 19:06:52197 // Start getting the IRT open asynchronously while we launch the NaCl process.
198 // We'll make sure this actually finished in OnProcessLaunched, below.
199 if (!NaClBrowser::GetInstance()->EnsureIrtAvailable()) {
200 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
201 return false;
202 }
203
[email protected]c47ec402010-07-29 10:20:49204 // Rather than creating a socket pair in the renderer, and passing
205 // one side through the browser to sel_ldr, socket pairs are created
206 // in the browser and then passed to the renderer and sel_ldr.
207 //
208 // This is mainly for the benefit of Windows, where sockets cannot
209 // be passed in messages, but are copied via DuplicateHandle().
210 // This means the sandboxed renderer cannot send handles to the
211 // browser process.
212
213 for (int i = 0; i < socket_count; i++) {
214 nacl::Handle pair[2];
215 // Create a connected socket
216 if (nacl::SocketPair(pair) == -1)
217 return false;
[email protected]1d8a3d1f2011-02-19 07:11:52218 internal_->sockets_for_renderer.push_back(pair[0]);
219 internal_->sockets_for_sel_ldr.push_back(pair[1]);
[email protected]c47ec402010-07-29 10:20:49220 SetCloseOnExec(pair[0]);
221 SetCloseOnExec(pair[1]);
222 }
[email protected]d032f492009-09-29 00:33:46223
224 // Launch the process
[email protected]fb1277e82009-11-21 20:32:30225 if (!LaunchSelLdr()) {
[email protected]d032f492009-09-29 00:33:46226 return false;
227 }
[email protected]92d56412011-03-24 20:53:52228 chrome_render_message_filter_ = chrome_render_message_filter;
[email protected]fb1277e82009-11-21 20:32:30229 reply_msg_ = reply_msg;
[email protected]f08e47d2009-10-15 21:46:15230
[email protected]d032f492009-09-29 00:33:46231 return true;
[email protected]d032f492009-09-29 00:33:46232}
233
[email protected]fb1277e82009-11-21 20:32:30234bool NaClProcessHost::LaunchSelLdr() {
[email protected]4cb43102011-12-02 20:24:49235 if (!child_process_host()->CreateChannel())
[email protected]d032f492009-09-29 00:33:46236 return false;
237
[email protected]e3fc75a2011-05-05 08:20:42238 CommandLine::StringType nacl_loader_prefix;
239#if defined(OS_POSIX)
240 nacl_loader_prefix = CommandLine::ForCurrentProcess()->GetSwitchValueNative(
241 switches::kNaClLoaderCmdPrefix);
242#endif // defined(OS_POSIX)
243
[email protected]d032f492009-09-29 00:33:46244 // Build command line for nacl.
[email protected]8c40f322011-08-24 03:33:36245
246#if defined(OS_MACOSX)
247 // The Native Client process needs to be able to allocate a 1GB contiguous
248 // region to use as the client environment's virtual address space. ASLR
249 // (PIE) interferes with this by making it possible that no gap large enough
250 // to accomodate this request will exist in the child process' address
251 // space. Disable PIE for NaCl processes. See http://crbug.com/90221 and
252 // http://code.google.com/p/nativeclient/issues/detail?id=2043.
[email protected]4cb43102011-12-02 20:24:49253 int flags = ChildProcessHost::CHILD_NO_PIE;
[email protected]8c40f322011-08-24 03:33:36254#elif defined(OS_LINUX)
[email protected]4cb43102011-12-02 20:24:49255 int flags = nacl_loader_prefix.empty() ? ChildProcessHost::CHILD_ALLOW_SELF :
256 ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36257#else
[email protected]4cb43102011-12-02 20:24:49258 int flags = ChildProcessHost::CHILD_NORMAL;
[email protected]8c40f322011-08-24 03:33:36259#endif
260
[email protected]4cb43102011-12-02 20:24:49261 FilePath exe_path = ChildProcessHost::GetChildPath(flags);
[email protected]fb1277e82009-11-21 20:32:30262 if (exe_path.empty())
[email protected]d032f492009-09-29 00:33:46263 return false;
264
[email protected]fb1277e82009-11-21 20:32:30265 CommandLine* cmd_line = new CommandLine(exe_path);
[email protected]103607e2010-02-01 18:57:09266 nacl::CopyNaClCommandLineArguments(cmd_line);
[email protected]599e6642010-01-27 18:52:13267
[email protected]05076ba22010-07-30 05:59:57268 cmd_line->AppendSwitchASCII(switches::kProcessType,
269 switches::kNaClLoaderProcess);
[email protected]4cb43102011-12-02 20:24:49270 cmd_line->AppendSwitchASCII(switches::kProcessChannelID,
271 child_process_host()->channel_id());
[email protected]93156cec2011-09-12 21:14:44272 if (logging::DialogsAreSuppressed())
273 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
[email protected]d032f492009-09-29 00:33:46274
[email protected]e3fc75a2011-05-05 08:20:42275 if (!nacl_loader_prefix.empty())
276 cmd_line->PrependWrapper(nacl_loader_prefix);
277
[email protected]103607e2010-02-01 18:57:09278 // On Windows we might need to start the broker process to launch a new loader
[email protected]d032f492009-09-29 00:33:46279#if defined(OS_WIN)
[email protected]773ebb92011-11-15 19:06:52280 if (RunningOnWOW64()) {
[email protected]1dbaaa702011-04-06 17:43:42281 return NaClBrokerService::GetInstance()->LaunchLoader(
[email protected]4cb43102011-12-02 20:24:49282 this, ASCIIToWide(child_process_host()->channel_id()));
[email protected]4bdde602010-06-16 03:17:35283 } else {
[email protected]d27893f62010-07-03 05:47:42284 BrowserChildProcessHost::Launch(FilePath(), cmd_line);
[email protected]4bdde602010-06-16 03:17:35285 }
[email protected]103607e2010-02-01 18:57:09286#elif defined(OS_POSIX)
[email protected]e3fc75a2011-05-05 08:20:42287 BrowserChildProcessHost::Launch(nacl_loader_prefix.empty(), // use_zygote
[email protected]d27893f62010-07-03 05:47:42288 base::environment_vector(),
289 cmd_line);
[email protected]103607e2010-02-01 18:57:09290#endif
[email protected]d032f492009-09-29 00:33:46291
[email protected]fb1277e82009-11-21 20:32:30292 return true;
[email protected]d032f492009-09-29 00:33:46293}
294
[email protected]103607e2010-02-01 18:57:09295void NaClProcessHost::OnProcessLaunchedByBroker(base::ProcessHandle handle) {
296 set_handle(handle);
297 OnProcessLaunched();
298}
299
[email protected]463ea5f2011-12-03 06:57:47300void NaClProcessHost::OnProcessCrashed(int exit_code) {
301 std::string message = base::StringPrintf(
302 "NaCl process exited with status %i (0x%x)", exit_code, exit_code);
303 LOG(ERROR) << message;
[email protected]103607e2010-02-01 18:57:09304}
305
[email protected]773ebb92011-11-15 19:06:52306// This only ever runs on the BrowserThread::FILE thread.
307// If multiple tasks are posted, the later ones are no-ops.
308void NaClBrowser::OpenIrtLibraryFile() {
309 if (irt_platform_file_ != base::kInvalidPlatformFileValue)
310 // We've already run.
311 return;
[email protected]338466a82011-05-03 04:27:43312
[email protected]773ebb92011-11-15 19:06:52313 FilePath irt_filepath;
314
[email protected]88e15832011-07-19 01:18:24315 // Allow the IRT library to be overridden via an environment
316 // variable. This allows the NaCl/Chromium integration bot to
317 // specify a newly-built IRT rather than using a prebuilt one
318 // downloaded via Chromium's DEPS file. We use the same environment
319 // variable that the standalone NaCl PPAPI plugin accepts.
320 const char* irt_path_var = getenv("NACL_IRT_LIBRARY");
321 if (irt_path_var != NULL) {
[email protected]773ebb92011-11-15 19:06:52322 FilePath::StringType path_string(
323 irt_path_var, const_cast<const char*>(strchr(irt_path_var, '\0')));
324 irt_filepath = FilePath(path_string);
[email protected]88e15832011-07-19 01:18:24325 } else {
326 FilePath plugin_dir;
327 if (!PathService::Get(chrome::DIR_INTERNAL_PLUGINS, &plugin_dir)) {
328 LOG(ERROR) << "Failed to locate the plugins directory";
[email protected]88e15832011-07-19 01:18:24329 return;
330 }
[email protected]773ebb92011-11-15 19:06:52331
332 bool on_x86_64 = RunningOnWOW64();
333#if defined(__x86_64__)
334 on_x86_64 = true;
335#endif
336 FilePath::StringType irt_name;
337 if (on_x86_64) {
338 irt_name = FILE_PATH_LITERAL("nacl_irt_x86_64.nexe");
339 } else {
340 irt_name = FILE_PATH_LITERAL("nacl_irt_x86_32.nexe");
341 }
342
343 irt_filepath = plugin_dir.Append(irt_name);
[email protected]338466a82011-05-03 04:27:43344 }
[email protected]88e15832011-07-19 01:18:24345
[email protected]773ebb92011-11-15 19:06:52346 base::PlatformFileError error_code;
347 irt_platform_file_ = base::CreatePlatformFile(irt_filepath,
348 base::PLATFORM_FILE_OPEN |
349 base::PLATFORM_FILE_READ,
350 NULL,
351 &error_code);
352 if (error_code != base::PLATFORM_FILE_OK) {
353 LOG(ERROR) << "Failed to open NaCl IRT file \""
354 << irt_filepath.LossyDisplayName()
355 << "\": " << error_code;
356 }
357}
358
359void NaClProcessHost::OnProcessLaunched() {
360 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
361
362 if (nacl_browser->IrtAvailable()) {
363 // The IRT is already open. Away we go.
364 SendStart(nacl_browser->IrtFile());
365 } else {
366 // We're waiting for the IRT to be open.
367 nacl_browser->MakeIrtAvailable(base::Bind(&NaClProcessHost::IrtReady,
368 weak_factory_.GetWeakPtr()));
369 }
370}
371
372// The asynchronous attempt to get the IRT file open has completed.
373void NaClProcessHost::IrtReady() {
374 NaClBrowser* nacl_browser = NaClBrowser::GetInstance();
375
376 if (nacl_browser->IrtAvailable()) {
377 SendStart(nacl_browser->IrtFile());
378 } else {
379 LOG(ERROR) << "Cannot launch NaCl process after IRT file open failed";
[email protected]338466a82011-05-03 04:27:43380 delete this;
381 }
382}
383
[email protected]773ebb92011-11-15 19:06:52384static bool SendHandleToSelLdr(
385 base::ProcessHandle processh,
386 nacl::Handle sourceh, bool close_source,
387 std::vector<nacl::FileDescriptor> *handles_for_sel_ldr) {
388#if defined(OS_WIN)
389 HANDLE channel;
390 int flags = DUPLICATE_SAME_ACCESS;
391 if (close_source)
392 flags |= DUPLICATE_CLOSE_SOURCE;
393 if (!DuplicateHandle(GetCurrentProcess(),
394 reinterpret_cast<HANDLE>(sourceh),
395 processh,
396 &channel,
397 0, // Unused given DUPLICATE_SAME_ACCESS.
398 FALSE,
399 flags)) {
400 LOG(ERROR) << "DuplicateHandle() failed";
401 return false;
402 }
403 handles_for_sel_ldr->push_back(
404 reinterpret_cast<nacl::FileDescriptor>(channel));
405#else
406 nacl::FileDescriptor channel;
407 channel.fd = sourceh;
408 channel.auto_close = close_source;
409 handles_for_sel_ldr->push_back(channel);
410#endif
411 return true;
412}
413
414void NaClProcessHost::SendStart(base::PlatformFile irt_file) {
415 CHECK_NE(irt_file, base::kInvalidPlatformFileValue);
416
[email protected]c47ec402010-07-29 10:20:49417 std::vector<nacl::FileDescriptor> handles_for_renderer;
[email protected]fb1277e82009-11-21 20:32:30418 base::ProcessHandle nacl_process_handle;
[email protected]fb1277e82009-11-21 20:32:30419
[email protected]1d8a3d1f2011-02-19 07:11:52420 for (size_t i = 0; i < internal_->sockets_for_renderer.size(); i++) {
[email protected]c47ec402010-07-29 10:20:49421#if defined(OS_WIN)
422 // Copy the handle into the renderer process.
423 HANDLE handle_in_renderer;
[email protected]909c2402011-05-09 11:39:04424 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
425 reinterpret_cast<HANDLE>(
426 internal_->sockets_for_renderer[i]),
427 chrome_render_message_filter_->peer_handle(),
428 &handle_in_renderer,
429 0, // Unused given DUPLICATE_SAME_ACCESS.
430 FALSE,
431 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
432 LOG(ERROR) << "DuplicateHandle() failed";
433 delete this;
434 return;
435 }
[email protected]c47ec402010-07-29 10:20:49436 handles_for_renderer.push_back(
437 reinterpret_cast<nacl::FileDescriptor>(handle_in_renderer));
438#else
439 // No need to dup the imc_handle - we don't pass it anywhere else so
440 // it cannot be closed.
441 nacl::FileDescriptor imc_handle;
[email protected]1d8a3d1f2011-02-19 07:11:52442 imc_handle.fd = internal_->sockets_for_renderer[i];
[email protected]c47ec402010-07-29 10:20:49443 imc_handle.auto_close = true;
444 handles_for_renderer.push_back(imc_handle);
445#endif
446 }
447
448#if defined(OS_WIN)
449 // Copy the process handle into the renderer process.
[email protected]909c2402011-05-09 11:39:04450 if (!DuplicateHandle(base::GetCurrentProcessHandle(),
451 handle(),
452 chrome_render_message_filter_->peer_handle(),
453 &nacl_process_handle,
454 PROCESS_DUP_HANDLE,
455 FALSE,
456 0)) {
457 LOG(ERROR) << "DuplicateHandle() failed";
458 delete this;
459 return;
460 }
[email protected]fb1277e82009-11-21 20:32:30461#else
[email protected]fb1277e82009-11-21 20:32:30462 // We use pid as process handle on Posix
463 nacl_process_handle = handle();
[email protected]fb1277e82009-11-21 20:32:30464#endif
465
466 // Get the pid of the NaCl process
467 base::ProcessId nacl_process_id = base::GetProcId(handle());
468
[email protected]2ccf45c2011-08-19 23:35:50469 ChromeViewHostMsg_LaunchNaCl::WriteReplyParams(
[email protected]c47ec402010-07-29 10:20:49470 reply_msg_, handles_for_renderer, nacl_process_handle, nacl_process_id);
[email protected]92d56412011-03-24 20:53:52471 chrome_render_message_filter_->Send(reply_msg_);
472 chrome_render_message_filter_ = NULL;
[email protected]fb1277e82009-11-21 20:32:30473 reply_msg_ = NULL;
[email protected]1d8a3d1f2011-02-19 07:11:52474 internal_->sockets_for_renderer.clear();
[email protected]fb1277e82009-11-21 20:32:30475
[email protected]c47ec402010-07-29 10:20:49476 std::vector<nacl::FileDescriptor> handles_for_sel_ldr;
[email protected]1d8a3d1f2011-02-19 07:11:52477 for (size_t i = 0; i < internal_->sockets_for_sel_ldr.size(); i++) {
[email protected]773ebb92011-11-15 19:06:52478 if (!SendHandleToSelLdr(handle(),
479 internal_->sockets_for_sel_ldr[i], true,
480 &handles_for_sel_ldr)) {
[email protected]909c2402011-05-09 11:39:04481 delete this;
[email protected]c47ec402010-07-29 10:20:49482 return;
483 }
[email protected]773ebb92011-11-15 19:06:52484 }
485
486 // Send over the IRT file handle. We don't close our own copy!
487 if (!SendHandleToSelLdr(handle(), irt_file, false, &handles_for_sel_ldr)) {
488 delete this;
489 return;
[email protected]c47ec402010-07-29 10:20:49490 }
491
[email protected]ab88d1542011-11-18 22:52:00492#if defined(OS_MACOSX)
493 // For dynamic loading support, NaCl requires a file descriptor that
494 // was created in /tmp, since those created with shm_open() are not
495 // mappable with PROT_EXEC. Rather than requiring an extra IPC
496 // round trip out of the sandbox, we create an FD here.
[email protected]cbbe7842011-11-17 22:01:25497 base::SharedMemory memory_buffer;
[email protected]b05df6b2011-12-01 23:19:31498 base::SharedMemoryCreateOptions options;
499 options.size = 1;
500 options.executable = true;
501 if (!memory_buffer.Create(options)) {
[email protected]2c68bf032010-11-11 23:16:30502 LOG(ERROR) << "Failed to allocate memory buffer";
[email protected]909c2402011-05-09 11:39:04503 delete this;
[email protected]2c68bf032010-11-11 23:16:30504 return;
505 }
[email protected]cbbe7842011-11-17 22:01:25506 nacl::FileDescriptor memory_fd;
507 memory_fd.fd = dup(memory_buffer.handle().fd);
508 if (memory_fd.fd < 0) {
509 LOG(ERROR) << "Failed to dup() a file descriptor";
510 delete this;
511 return;
512 }
513 memory_fd.auto_close = true;
[email protected]2c68bf032010-11-11 23:16:30514 handles_for_sel_ldr.push_back(memory_fd);
515#endif
516
[email protected]773ebb92011-11-15 19:06:52517 Send(new NaClProcessMsg_Start(handles_for_sel_ldr));
[email protected]1d8a3d1f2011-02-19 07:11:52518 internal_->sockets_for_sel_ldr.clear();
[email protected]d032f492009-09-29 00:33:46519}
520
[email protected]a95986a82010-12-24 06:19:28521bool NaClProcessHost::OnMessageReceived(const IPC::Message& msg) {
[email protected]d032f492009-09-29 00:33:46522 NOTREACHED() << "Invalid message with type = " << msg.type();
[email protected]a95986a82010-12-24 06:19:28523 return false;
[email protected]d032f492009-09-29 00:33:46524}