OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "chrome/nacl/nacl_listener.h" | |
6 | |
7 #include <errno.h> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/logging.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/message_loop.h" | |
13 #include "chrome/common/nacl_messages.h" | |
14 #include "ipc/ipc_channel.h" | |
15 #include "ipc/ipc_switches.h" | |
16 #include "native_client/src/shared/imc/nacl_imc.h" | |
17 | |
18 #if defined(OS_LINUX) | |
19 #include "content/common/child_process_sandbox_support_linux.h" | |
20 #endif | |
21 | |
22 #if defined(OS_WIN) | |
23 #include <fcntl.h> | |
24 #include <io.h> | |
25 #endif | |
26 | |
27 // This is ugly. We need an interface header file for the exported | |
28 // sel_ldr interfaces. | |
29 // TODO(gregoryd,sehr): Add an interface header. | |
30 #if defined(OS_WIN) | |
31 typedef HANDLE NaClHandle; | |
32 #else | |
33 typedef int NaClHandle; | |
34 #endif // NaClHandle | |
35 | |
36 #if defined(OS_MACOSX) | |
37 namespace { | |
38 | |
39 // On Mac OS X, shm_open() works in the sandbox but does not give us | |
40 // an FD that we can map as PROT_EXEC. Rather than doing an IPC to | |
41 // get an executable SHM region when CreateMemoryObject() is called, | |
42 // we preallocate one on startup, since NaCl's sel_ldr only needs one | |
43 // of them. This saves a round trip. | |
44 | |
45 base::subtle::Atomic32 g_shm_fd = -1; | |
46 | |
47 int CreateMemoryObject(size_t size, bool executable) { | |
48 if (executable && size > 0) { | |
49 int result_fd = base::subtle::NoBarrier_AtomicExchange(&g_shm_fd, -1); | |
50 if (result_fd != -1) { | |
51 // ftruncate() is disallowed by the Mac OS X sandbox and | |
52 // returns EPERM. Luckily, we can get the same effect with | |
53 // lseek() + write(). | |
54 if (lseek(result_fd, size - 1, SEEK_SET) == -1) { | |
55 LOG(ERROR) << "lseek() failed: " << errno; | |
56 return -1; | |
57 } | |
58 if (write(result_fd, "", 1) != 1) { | |
59 LOG(ERROR) << "write() failed: " << errno; | |
60 return -1; | |
61 } | |
62 return result_fd; | |
63 } | |
64 } | |
65 // Fall back to NaCl's default implementation. | |
66 return -1; | |
67 } | |
68 | |
69 } // namespace | |
70 #endif // defined(OS_MACOSX) | |
71 | |
72 extern "C" int NaClMainForChromium(int handle_count, | |
73 const NaClHandle* handles, | |
74 int debug); | |
75 extern "C" void NaClSetIrtFileDesc(int fd); | |
76 | |
77 NaClListener::NaClListener() {} | |
78 | |
79 NaClListener::~NaClListener() {} | |
80 | |
81 void NaClListener::Listen() { | |
82 std::string channel_name = | |
83 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
84 switches::kProcessChannelID); | |
85 IPC::Channel channel(channel_name, IPC::Channel::MODE_CLIENT, this); | |
86 CHECK(channel.Connect()); | |
87 MessageLoop::current()->Run(); | |
88 } | |
89 | |
90 bool NaClListener::OnMessageReceived(const IPC::Message& msg) { | |
91 bool handled = true; | |
92 IPC_BEGIN_MESSAGE_MAP(NaClListener, msg) | |
93 IPC_MESSAGE_HANDLER(NaClProcessMsg_Start, OnStartSelLdr) | |
94 IPC_MESSAGE_UNHANDLED(handled = false) | |
95 IPC_END_MESSAGE_MAP() | |
96 return handled; | |
97 } | |
98 | |
99 void NaClListener::OnStartSelLdr( | |
100 std::vector<nacl::FileDescriptor> handles, | |
101 bool have_irt_file) { | |
102 #if defined(OS_LINUX) | |
103 nacl::SetCreateMemoryObjectFunc( | |
104 child_process_sandbox_support::MakeSharedMemorySegmentViaIPC); | |
105 #elif defined(OS_MACOSX) | |
106 nacl::SetCreateMemoryObjectFunc(CreateMemoryObject); | |
107 CHECK(handles.size() >= 1); | |
108 g_shm_fd = nacl::ToNativeHandle(handles[handles.size() - 1]); | |
109 handles.pop_back(); | |
110 #endif | |
111 | |
112 if (have_irt_file) { | |
113 CHECK(handles.size() >= 1); | |
114 NaClHandle irt_handle = nacl::ToNativeHandle(handles[handles.size() - 1]); | |
115 handles.pop_back(); | |
116 #if defined(OS_WIN) | |
117 int irt_desc = _open_osfhandle(reinterpret_cast<intptr_t>(irt_handle), | |
118 _O_RDWR | _O_BINARY); | |
119 if (irt_desc < 0) { | |
120 LOG(ERROR) << "_open_osfhandle() failed"; | |
121 return; | |
122 } | |
123 #else | |
124 int irt_desc = irt_handle; | |
125 #endif | |
126 NaClSetIrtFileDesc(irt_desc); | |
127 } | |
128 | |
129 scoped_array<NaClHandle> array(new NaClHandle[handles.size()]); | |
130 for (size_t i = 0; i < handles.size(); i++) { | |
131 array[i] = nacl::ToNativeHandle(handles[i]); | |
132 } | |
133 NaClMainForChromium(static_cast<int>(handles.size()), array.get(), | |
134 false /* debug */); | |
135 } | |
OLD | NEW |