OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/nacl/nacl_listener.h" | 5 #include "chrome/nacl/nacl_listener.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 // This is ugly. We need an interface header file for the exported | 27 // This is ugly. We need an interface header file for the exported |
28 // sel_ldr interfaces. | 28 // sel_ldr interfaces. |
29 // TODO(gregoryd,sehr): Add an interface header. | 29 // TODO(gregoryd,sehr): Add an interface header. |
30 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
31 typedef HANDLE NaClHandle; | 31 typedef HANDLE NaClHandle; |
32 #else | 32 #else |
33 typedef int NaClHandle; | 33 typedef int NaClHandle; |
34 #endif // NaClHandle | 34 #endif // NaClHandle |
35 | 35 |
36 #if defined(OS_POSIX) | 36 #if defined(OS_MACOSX) |
37 namespace { | 37 namespace { |
38 | 38 |
39 // On Mac OS X, shm_open() works in the sandbox but does not give us an FD | 39 // On Mac OS X, shm_open() works in the sandbox but does not give us |
40 // that we can map as PROT_EXEC. On Linux, shm_open() can only be used | 40 // an FD that we can map as PROT_EXEC. Rather than doing an IPC to |
41 // outside the sandbox anyway. Rather than doing an IPC to get an | 41 // get an executable SHM region when CreateMemoryObject() is called, |
42 // executable SHM region when CreateMemoryObject() is called, we | 42 // we preallocate one on startup, since NaCl's sel_ldr only needs one |
43 // preallocate one on startup, since NaCl's sel_ldr only needs one of them. | 43 // of them. This saves a round trip. |
44 // This saves a round trip. | |
45 | |
46 bool SetShmFdSize(int fd, size_t size) { | |
47 #if defined(OS_MACOSX) | |
48 // ftruncate() is disallowed by the Mac OS X sandbox and returns EPERM. | |
49 // Luckily, we can get the same effect with lseek() + write(). | |
50 if (lseek(fd, size - 1, SEEK_SET) == -1) { | |
51 LOG(ERROR) << "lseek() failed: " << errno; | |
52 close(fd); | |
53 return false; | |
54 } | |
55 if (write(fd, "", 1) != 1) { | |
56 LOG(ERROR) << "write() failed: " << errno; | |
57 close(fd); | |
58 return -1; | |
59 } | |
60 #else | |
61 if (ftruncate(fd, size) < 0) { | |
62 LOG(ERROR) << "ftruncate() failed: " << errno; | |
63 close(fd); | |
64 return false; | |
65 } | |
66 #endif // defined(OS_MACOSX) | |
67 return true; | |
68 } | |
69 | 44 |
70 base::subtle::Atomic32 g_shm_fd = -1; | 45 base::subtle::Atomic32 g_shm_fd = -1; |
71 | 46 |
72 int CreateMemoryObject(size_t size, bool executable) { | 47 int CreateMemoryObject(size_t size, bool executable) { |
73 if (executable && size > 0) { | 48 if (executable && size > 0) { |
74 int result_fd = base::subtle::NoBarrier_AtomicExchange(&g_shm_fd, -1); | 49 int result_fd = base::subtle::NoBarrier_AtomicExchange(&g_shm_fd, -1); |
75 if (result_fd != -1 && SetShmFdSize(result_fd, size)) | 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 } |
76 return result_fd; | 62 return result_fd; |
| 63 } |
77 } | 64 } |
78 #if defined(OS_LINUX) | |
79 // Use the proxied implementation. It doesn't really support executability. | |
80 CHECK(!executable); | |
81 return content::MakeSharedMemorySegmentViaIPC(size, executable); | |
82 #endif | |
83 // Fall back to NaCl's default implementation. | 65 // Fall back to NaCl's default implementation. |
84 return -1; | 66 return -1; |
85 } | 67 } |
86 | 68 |
87 } // namespace | 69 } // namespace |
88 #endif // defined(OS_POSIX) | 70 #endif // defined(OS_MACOSX) |
89 | 71 |
90 extern "C" void NaClMainForChromium(int handle_count, | 72 extern "C" void NaClMainForChromium(int handle_count, |
91 const NaClHandle* handles, | 73 const NaClHandle* handles, |
92 int debug); | 74 int debug); |
93 extern "C" void NaClSetIrtFileDesc(int fd); | 75 extern "C" void NaClSetIrtFileDesc(int fd); |
94 | 76 |
95 NaClListener::NaClListener() : debug_enabled_(false) {} | 77 NaClListener::NaClListener() : debug_enabled_(false) {} |
96 | 78 |
97 NaClListener::~NaClListener() {} | 79 NaClListener::~NaClListener() {} |
98 | 80 |
99 void NaClListener::Listen() { | 81 void NaClListener::Listen() { |
100 std::string channel_name = | 82 std::string channel_name = |
101 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 83 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
102 switches::kProcessChannelID); | 84 switches::kProcessChannelID); |
103 IPC::Channel channel(channel_name, IPC::Channel::MODE_CLIENT, this); | 85 IPC::Channel channel(channel_name, IPC::Channel::MODE_CLIENT, this); |
104 CHECK(channel.Connect()); | 86 CHECK(channel.Connect()); |
105 MessageLoop::current()->Run(); | 87 MessageLoop::current()->Run(); |
106 } | 88 } |
107 | 89 |
108 bool NaClListener::OnMessageReceived(const IPC::Message& msg) { | 90 bool NaClListener::OnMessageReceived(const IPC::Message& msg) { |
109 bool handled = true; | 91 bool handled = true; |
110 IPC_BEGIN_MESSAGE_MAP(NaClListener, msg) | 92 IPC_BEGIN_MESSAGE_MAP(NaClListener, msg) |
111 IPC_MESSAGE_HANDLER(NaClProcessMsg_Start, OnStartSelLdr) | 93 IPC_MESSAGE_HANDLER(NaClProcessMsg_Start, OnStartSelLdr) |
112 IPC_MESSAGE_UNHANDLED(handled = false) | 94 IPC_MESSAGE_UNHANDLED(handled = false) |
113 IPC_END_MESSAGE_MAP() | 95 IPC_END_MESSAGE_MAP() |
114 return handled; | 96 return handled; |
115 } | 97 } |
116 | 98 |
117 void NaClListener::OnStartSelLdr(std::vector<nacl::FileDescriptor> handles) { | 99 void NaClListener::OnStartSelLdr(std::vector<nacl::FileDescriptor> handles) { |
118 #if defined(OS_POSIX) | 100 #if defined(OS_LINUX) |
| 101 nacl::SetCreateMemoryObjectFunc(content::MakeSharedMemorySegmentViaIPC); |
| 102 #elif defined(OS_MACOSX) |
119 nacl::SetCreateMemoryObjectFunc(CreateMemoryObject); | 103 nacl::SetCreateMemoryObjectFunc(CreateMemoryObject); |
120 CHECK(!handles.empty()); | 104 CHECK(handles.size() >= 1); |
121 g_shm_fd = nacl::ToNativeHandle(handles.back()); | 105 g_shm_fd = nacl::ToNativeHandle(handles[handles.size() - 1]); |
122 handles.pop_back(); | 106 handles.pop_back(); |
123 #endif | 107 #endif |
124 | 108 |
125 CHECK(!handles.empty()); | 109 CHECK(handles.size() >= 1); |
126 NaClHandle irt_handle = nacl::ToNativeHandle(handles.back()); | 110 NaClHandle irt_handle = nacl::ToNativeHandle(handles[handles.size() - 1]); |
127 handles.pop_back(); | 111 handles.pop_back(); |
128 | 112 |
129 #if defined(OS_WIN) | 113 #if defined(OS_WIN) |
130 int irt_desc = _open_osfhandle(reinterpret_cast<intptr_t>(irt_handle), | 114 int irt_desc = _open_osfhandle(reinterpret_cast<intptr_t>(irt_handle), |
131 _O_RDONLY | _O_BINARY); | 115 _O_RDONLY | _O_BINARY); |
132 if (irt_desc < 0) { | 116 if (irt_desc < 0) { |
133 LOG(ERROR) << "_open_osfhandle() failed"; | 117 LOG(ERROR) << "_open_osfhandle() failed"; |
134 return; | 118 return; |
135 } | 119 } |
136 #else | 120 #else |
137 int irt_desc = irt_handle; | 121 int irt_desc = irt_handle; |
138 #endif | 122 #endif |
139 | 123 |
140 NaClSetIrtFileDesc(irt_desc); | 124 NaClSetIrtFileDesc(irt_desc); |
141 | 125 |
142 scoped_array<NaClHandle> array(new NaClHandle[handles.size()]); | 126 scoped_array<NaClHandle> array(new NaClHandle[handles.size()]); |
143 for (size_t i = 0; i < handles.size(); i++) { | 127 for (size_t i = 0; i < handles.size(); i++) { |
144 array[i] = nacl::ToNativeHandle(handles[i]); | 128 array[i] = nacl::ToNativeHandle(handles[i]); |
145 } | 129 } |
146 NaClMainForChromium(static_cast<int>(handles.size()), array.get(), | 130 NaClMainForChromium(static_cast<int>(handles.size()), array.get(), |
147 debug_enabled_); | 131 debug_enabled_); |
148 NOTREACHED(); | 132 NOTREACHED(); |
149 } | 133 } |
OLD | NEW |