1 // Copyright 2014 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.
5 #include "ppapi/nacl_irt/manifest_service.h"
7 #include "base/message_loop/message_loop_proxy.h"
8 #include "ipc/ipc_channel_handle.h"
9 #include "ipc/ipc_channel_proxy.h"
10 #include "ipc/ipc_sync_message_filter.h"
11 #include "native_client/src/trusted/service_runtime/include/sys/errno.h"
12 #include "ppapi/nacl_irt/irt_manifest.h"
13 #include "ppapi/nacl_irt/plugin_startup.h"
14 #include "ppapi/proxy/ppapi_messages.h"
16 #if !defined(OS_NACL_SFI)
24 const char kFilePrefix
[] = "files/";
26 // IPC channel is asynchronously set up. So, the NaCl process may try to
27 // send a OpenResource message to the host before the connection is
28 // established. In such a case, it is necessary to wait for the set up
30 class ManifestMessageFilter
: public IPC::SyncMessageFilter
{
32 ManifestMessageFilter(base::WaitableEvent
* shutdown_event
)
33 : SyncMessageFilter(shutdown_event
),
35 true /* manual_reset */, false /* initially_signaled */) {
38 virtual bool Send(IPC::Message
* message
) override
{
39 // Wait until set up is actually done.
40 connected_event_
.Wait();
41 return SyncMessageFilter::Send(message
);
44 // When set up is done, OnFilterAdded is called on IO thread. Unblocks the
46 virtual void OnFilterAdded(IPC::Sender
* sender
) override
{
47 SyncMessageFilter::OnFilterAdded(sender
);
48 connected_event_
.Signal();
51 // If an error is found, unblocks the Send(), too, to return an error.
52 virtual void OnChannelError() override
{
53 SyncMessageFilter::OnChannelError();
54 connected_event_
.Signal();
57 // Similar to OnChannelError, unblocks the Send() on the channel closing.
58 virtual void OnChannelClosing() override
{
59 SyncMessageFilter::OnChannelClosing();
60 connected_event_
.Signal();
64 base::WaitableEvent connected_event_
;
66 DISALLOW_COPY_AND_ASSIGN(ManifestMessageFilter
);
69 ManifestService::ManifestService(
70 const IPC::ChannelHandle
& handle
,
71 scoped_refptr
<base::MessageLoopProxy
> io_message_loop
,
72 base::WaitableEvent
* shutdown_event
) {
73 filter_
= new ManifestMessageFilter(shutdown_event
);
74 channel_
= IPC::ChannelProxy::Create(handle
,
75 IPC::Channel::MODE_SERVER
,
77 io_message_loop
.get());
78 channel_
->AddFilter(filter_
.get());
81 ManifestService::~ManifestService() {
84 void ManifestService::StartupInitializationComplete() {
85 filter_
->Send(new PpapiHostMsg_StartupInitializationComplete
);
88 bool ManifestService::OpenResource(const char* file
, int* fd
) {
89 // We currently restrict to only allow one concurrent open_resource() call
90 // per plugin. This could be fixed by doing a token lookup with
91 // NaClProcessMsg_ResolveFileTokenAsyncReply instead of using a
92 // global inside components/nacl/loader/nacl_listener.cc
93 base::AutoLock
lock(open_resource_lock_
);
95 // OpenResource will return INVALID SerializedHandle, if it is not supported.
96 // Specifically, PNaCl doesn't support open resource.
97 ppapi::proxy::SerializedHandle ipc_fd
;
99 // File tokens are ignored here, but needed when the message is processed
100 // inside NaClIPCAdapter.
101 uint64_t file_token_lo
= 0;
102 uint64_t file_token_hi
= 0;
103 if (!filter_
->Send(new PpapiHostMsg_OpenResource(
104 std::string(kFilePrefix
) + file
,
108 LOG(ERROR
) << "ManifestService::OpenResource failed:" << file
;
113 // File tokens are used internally by NaClIPCAdapter and should have
114 // been cleared from the message when it is received here.
115 // These tokens should never be set for Non-SFI mode.
116 CHECK(file_token_lo
== 0);
117 CHECK(file_token_hi
== 0);
119 // Copy the file if we received a valid file descriptor. Otherwise, if we got
120 // a reply, the file doesn't exist, so provide an fd of -1.
121 // See IrtOpenResource() for how this function's result is interpreted.
122 if (ipc_fd
.is_file())
123 *fd
= ipc_fd
.descriptor().fd
;
129 #if !defined(OS_NACL_SFI)
132 pthread_mutex_t g_mu
= PTHREAD_MUTEX_INITIALIZER
;
133 std::map
<std::string
, int>* g_prefetched_fds
;
137 void RegisterPreopenedDescriptorsNonSfi(
138 std::map
<std::string
, int>* key_fd_map
) {
139 pthread_mutex_lock(&g_mu
);
140 DCHECK(!g_prefetched_fds
);
141 g_prefetched_fds
= key_fd_map
;
142 pthread_mutex_unlock(&g_mu
);
146 int IrtOpenResource(const char* file
, int* fd
) {
147 // Remove leading '/' character.
151 #if !defined(OS_NACL_SFI)
152 // Fast path for prefetched FDs.
153 pthread_mutex_lock(&g_mu
);
154 if (g_prefetched_fds
) {
155 std::map
<std::string
, int>::iterator it
= g_prefetched_fds
->find(file
);
156 if (it
!= g_prefetched_fds
->end()) {
158 g_prefetched_fds
->erase(it
);
159 pthread_mutex_unlock(&g_mu
);
163 pthread_mutex_unlock(&g_mu
);
166 ManifestService
* manifest_service
= GetManifestService();
167 if (manifest_service
== NULL
||
168 !manifest_service
->OpenResource(file
, fd
)) {
171 return (*fd
== -1) ? NACL_ABI_ENOENT
: 0;