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/single_thread_task_runner.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 // IPC channel is asynchronously set up. So, the NaCl process may try to
25 // send a OpenResource message to the host before the connection is
26 // established. In such a case, it is necessary to wait for the set up
28 class ManifestMessageFilter
: public IPC::SyncMessageFilter
{
30 ManifestMessageFilter(base::WaitableEvent
* shutdown_event
)
31 : SyncMessageFilter(shutdown_event
),
33 true /* manual_reset */, false /* initially_signaled */) {
36 bool Send(IPC::Message
* message
) override
{
37 // Wait until set up is actually done.
38 connected_event_
.Wait();
39 return SyncMessageFilter::Send(message
);
42 // When set up is done, OnFilterAdded is called on IO thread. Unblocks the
44 void OnFilterAdded(IPC::Sender
* sender
) override
{
45 SyncMessageFilter::OnFilterAdded(sender
);
46 connected_event_
.Signal();
49 // If an error is found, unblocks the Send(), too, to return an error.
50 void OnChannelError() override
{
51 SyncMessageFilter::OnChannelError();
52 connected_event_
.Signal();
55 // Similar to OnChannelError, unblocks the Send() on the channel closing.
56 void OnChannelClosing() override
{
57 SyncMessageFilter::OnChannelClosing();
58 connected_event_
.Signal();
62 base::WaitableEvent connected_event_
;
64 DISALLOW_COPY_AND_ASSIGN(ManifestMessageFilter
);
67 ManifestService::ManifestService(
68 const IPC::ChannelHandle
& handle
,
69 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
70 base::WaitableEvent
* shutdown_event
) {
71 filter_
= new ManifestMessageFilter(shutdown_event
);
72 channel_
= IPC::ChannelProxy::Create(handle
, IPC::Channel::MODE_SERVER
,
74 io_task_runner
.get());
75 channel_
->AddFilter(filter_
.get());
78 ManifestService::~ManifestService() {
81 void ManifestService::StartupInitializationComplete() {
82 filter_
->Send(new PpapiHostMsg_StartupInitializationComplete
);
85 bool ManifestService::OpenResource(const char* file
, int* fd
) {
86 // We currently restrict to only allow one concurrent open_resource() call
87 // per plugin. This could be fixed by doing a token lookup with
88 // NaClProcessMsg_ResolveFileTokenAsyncReply instead of using a
89 // global inside components/nacl/loader/nacl_listener.cc
90 base::AutoLock
lock(open_resource_lock_
);
92 // OpenResource will return INVALID SerializedHandle, if it is not supported.
93 // Specifically, PNaCl doesn't support open resource.
94 ppapi::proxy::SerializedHandle ipc_fd
;
96 // File tokens are ignored here, but needed when the message is processed
97 // inside NaClIPCAdapter.
98 uint64_t file_token_lo
= 0;
99 uint64_t file_token_hi
= 0;
100 if (!filter_
->Send(new PpapiHostMsg_OpenResource(
105 LOG(ERROR
) << "ManifestService::OpenResource failed:" << file
;
110 // File tokens are used internally by NaClIPCAdapter and should have
111 // been cleared from the message when it is received here.
112 // These tokens should never be set for Non-SFI mode.
113 CHECK(file_token_lo
== 0);
114 CHECK(file_token_hi
== 0);
116 // Copy the file if we received a valid file descriptor. Otherwise, if we got
117 // a reply, the file doesn't exist, so provide an fd of -1.
118 // See IrtOpenResource() for how this function's result is interpreted.
119 if (ipc_fd
.is_file())
120 *fd
= ipc_fd
.descriptor().fd
;
126 #if !defined(OS_NACL_SFI)
129 pthread_mutex_t g_mu
= PTHREAD_MUTEX_INITIALIZER
;
130 std::map
<std::string
, int>* g_prefetched_fds
;
134 void RegisterPreopenedDescriptorsNonSfi(
135 std::map
<std::string
, int>* key_fd_map
) {
136 pthread_mutex_lock(&g_mu
);
137 DCHECK(!g_prefetched_fds
);
138 g_prefetched_fds
= key_fd_map
;
139 pthread_mutex_unlock(&g_mu
);
143 int IrtOpenResource(const char* file
, int* fd
) {
144 // Remove leading '/' character.
148 #if !defined(OS_NACL_SFI)
149 // Fast path for prefetched FDs.
150 pthread_mutex_lock(&g_mu
);
151 if (g_prefetched_fds
) {
152 std::map
<std::string
, int>::iterator it
= g_prefetched_fds
->find(file
);
153 if (it
!= g_prefetched_fds
->end()) {
155 g_prefetched_fds
->erase(it
);
156 pthread_mutex_unlock(&g_mu
);
160 pthread_mutex_unlock(&g_mu
);
163 ManifestService
* manifest_service
= GetManifestService();
164 if (manifest_service
== NULL
||
165 !manifest_service
->OpenResource(file
, fd
)) {
168 return (*fd
== -1) ? NACL_ABI_ENOENT
: 0;