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_MACOSX) | 36 #if defined(OS_POSIX) |
37 namespace { | 37 namespace { |
38 | 38 |
39 // On Mac OS X, shm_open() works in the sandbox but does not give us | 39 // On Mac OS X, shm_open() works in the sandbox but does not give us an FD |
40 // an FD that we can map as PROT_EXEC. Rather than doing an IPC to | 40 // that we can map as PROT_EXEC. On Linux, shm_open() can only be used |
41 // get an executable SHM region when CreateMemoryObject() is called, | 41 // outside the sandbox anyway. Rather than doing an IPC to get an |
42 // we preallocate one on startup, since NaCl's sel_ldr only needs one | 42 // executable SHM region when CreateMemoryObject() is called, we |
43 // of them. This saves a round trip. | 43 // preallocate one on startup, since NaCl's sel_ldr only needs one of them. |
| 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 } |
44 | 69 |
45 base::subtle::Atomic32 g_shm_fd = -1; | 70 base::subtle::Atomic32 g_shm_fd = -1; |
46 | 71 |
47 int CreateMemoryObject(size_t size, bool executable) { | 72 int CreateMemoryObject(size_t size, bool executable) { |
48 if (executable && size > 0) { | 73 if (executable && size > 0) { |
49 int result_fd = base::subtle::NoBarrier_AtomicExchange(&g_shm_fd, -1); | 74 int result_fd = base::subtle::NoBarrier_AtomicExchange(&g_shm_fd, -1); |
50 if (result_fd != -1) { | 75 if (result_fd != -1 && SetShmFdSize(result_fd, size)) |
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; | 76 return result_fd; |
63 } | |
64 } | 77 } |
| 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 |
65 // Fall back to NaCl's default implementation. | 83 // Fall back to NaCl's default implementation. |
66 return -1; | 84 return -1; |
67 } | 85 } |
68 | 86 |
69 } // namespace | 87 } // namespace |
70 #endif // defined(OS_MACOSX) | 88 #endif // defined(OS_POSIX) |
71 | 89 |
72 extern "C" void NaClMainForChromium(int handle_count, | 90 extern "C" void NaClMainForChromium(int handle_count, |
73 const NaClHandle* handles, | 91 const NaClHandle* handles, |
74 int debug); | 92 int debug); |
75 extern "C" void NaClSetIrtFileDesc(int fd); | 93 extern "C" void NaClSetIrtFileDesc(int fd); |
76 | 94 |
77 NaClListener::NaClListener() : debug_enabled_(false) {} | 95 NaClListener::NaClListener() : debug_enabled_(false) {} |
78 | 96 |
79 NaClListener::~NaClListener() {} | 97 NaClListener::~NaClListener() {} |
80 | 98 |
81 void NaClListener::Listen() { | 99 void NaClListener::Listen() { |
82 std::string channel_name = | 100 std::string channel_name = |
83 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 101 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
84 switches::kProcessChannelID); | 102 switches::kProcessChannelID); |
85 IPC::Channel channel(channel_name, IPC::Channel::MODE_CLIENT, this); | 103 IPC::Channel channel(channel_name, IPC::Channel::MODE_CLIENT, this); |
86 CHECK(channel.Connect()); | 104 CHECK(channel.Connect()); |
87 MessageLoop::current()->Run(); | 105 MessageLoop::current()->Run(); |
88 } | 106 } |
89 | 107 |
90 bool NaClListener::OnMessageReceived(const IPC::Message& msg) { | 108 bool NaClListener::OnMessageReceived(const IPC::Message& msg) { |
91 bool handled = true; | 109 bool handled = true; |
92 IPC_BEGIN_MESSAGE_MAP(NaClListener, msg) | 110 IPC_BEGIN_MESSAGE_MAP(NaClListener, msg) |
93 IPC_MESSAGE_HANDLER(NaClProcessMsg_Start, OnStartSelLdr) | 111 IPC_MESSAGE_HANDLER(NaClProcessMsg_Start, OnStartSelLdr) |
94 IPC_MESSAGE_UNHANDLED(handled = false) | 112 IPC_MESSAGE_UNHANDLED(handled = false) |
95 IPC_END_MESSAGE_MAP() | 113 IPC_END_MESSAGE_MAP() |
96 return handled; | 114 return handled; |
97 } | 115 } |
98 | 116 |
99 void NaClListener::OnStartSelLdr(std::vector<nacl::FileDescriptor> handles) { | 117 void NaClListener::OnStartSelLdr(std::vector<nacl::FileDescriptor> handles) { |
100 #if defined(OS_LINUX) | 118 #if defined(OS_POSIX) |
101 nacl::SetCreateMemoryObjectFunc(content::MakeSharedMemorySegmentViaIPC); | |
102 #elif defined(OS_MACOSX) | |
103 nacl::SetCreateMemoryObjectFunc(CreateMemoryObject); | 119 nacl::SetCreateMemoryObjectFunc(CreateMemoryObject); |
104 CHECK(handles.size() >= 1); | 120 CHECK(!handles.empty()); |
105 g_shm_fd = nacl::ToNativeHandle(handles[handles.size() - 1]); | 121 g_shm_fd = nacl::ToNativeHandle(handles.back()); |
106 handles.pop_back(); | 122 handles.pop_back(); |
107 #endif | 123 #endif |
108 | 124 |
109 CHECK(handles.size() >= 1); | 125 CHECK(!handles.empty()); |
110 NaClHandle irt_handle = nacl::ToNativeHandle(handles[handles.size() - 1]); | 126 NaClHandle irt_handle = nacl::ToNativeHandle(handles.back()); |
111 handles.pop_back(); | 127 handles.pop_back(); |
112 | 128 |
113 #if defined(OS_WIN) | 129 #if defined(OS_WIN) |
114 int irt_desc = _open_osfhandle(reinterpret_cast<intptr_t>(irt_handle), | 130 int irt_desc = _open_osfhandle(reinterpret_cast<intptr_t>(irt_handle), |
115 _O_RDONLY | _O_BINARY); | 131 _O_RDONLY | _O_BINARY); |
116 if (irt_desc < 0) { | 132 if (irt_desc < 0) { |
117 LOG(ERROR) << "_open_osfhandle() failed"; | 133 LOG(ERROR) << "_open_osfhandle() failed"; |
118 return; | 134 return; |
119 } | 135 } |
120 #else | 136 #else |
121 int irt_desc = irt_handle; | 137 int irt_desc = irt_handle; |
122 #endif | 138 #endif |
123 | 139 |
124 NaClSetIrtFileDesc(irt_desc); | 140 NaClSetIrtFileDesc(irt_desc); |
125 | 141 |
126 scoped_array<NaClHandle> array(new NaClHandle[handles.size()]); | 142 scoped_array<NaClHandle> array(new NaClHandle[handles.size()]); |
127 for (size_t i = 0; i < handles.size(); i++) { | 143 for (size_t i = 0; i < handles.size(); i++) { |
128 array[i] = nacl::ToNativeHandle(handles[i]); | 144 array[i] = nacl::ToNativeHandle(handles[i]); |
129 } | 145 } |
130 NaClMainForChromium(static_cast<int>(handles.size()), array.get(), | 146 NaClMainForChromium(static_cast<int>(handles.size()), array.get(), |
131 debug_enabled_); | 147 debug_enabled_); |
132 NOTREACHED(); | 148 NOTREACHED(); |
133 } | 149 } |
OLD | NEW |