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
,
32 false /* is_channel_send_thread_safe */),
34 true /* manual_reset */, false /* initially_signaled */) {
37 bool Send(IPC::Message
* message
) override
{
38 // Wait until set up is actually done.
39 connected_event_
.Wait();
40 return SyncMessageFilter::Send(message
);
43 // When set up is done, OnFilterAdded is called on IO thread. Unblocks the
45 void OnFilterAdded(IPC::Sender
* sender
) override
{
46 SyncMessageFilter::OnFilterAdded(sender
);
47 connected_event_
.Signal();
50 // If an error is found, unblocks the Send(), too, to return an error.
51 void OnChannelError() override
{
52 SyncMessageFilter::OnChannelError();
53 connected_event_
.Signal();
56 // Similar to OnChannelError, unblocks the Send() on the channel closing.
57 void OnChannelClosing() override
{
58 SyncMessageFilter::OnChannelClosing();
59 connected_event_
.Signal();
63 base::WaitableEvent connected_event_
;
65 DISALLOW_COPY_AND_ASSIGN(ManifestMessageFilter
);
68 ManifestService::ManifestService(
69 const IPC::ChannelHandle
& handle
,
70 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
71 base::WaitableEvent
* shutdown_event
) {
72 filter_
= new ManifestMessageFilter(shutdown_event
);
73 channel_
= IPC::ChannelProxy::Create(handle
, IPC::Channel::MODE_SERVER
,
75 io_task_runner
.get());
76 channel_
->AddFilter(filter_
.get());
79 ManifestService::~ManifestService() {
82 void ManifestService::StartupInitializationComplete() {
83 filter_
->Send(new PpapiHostMsg_StartupInitializationComplete
);
86 bool ManifestService::OpenResource(const char* file
, int* fd
) {
87 // We currently restrict to only allow one concurrent open_resource() call
88 // per plugin. This could be fixed by doing a token lookup with
89 // NaClProcessMsg_ResolveFileTokenAsyncReply instead of using a
90 // global inside components/nacl/loader/nacl_listener.cc
91 base::AutoLock
lock(open_resource_lock_
);
93 // OpenResource will return INVALID SerializedHandle, if it is not supported.
94 // Specifically, PNaCl doesn't support open resource.
95 ppapi::proxy::SerializedHandle ipc_fd
;
97 // File tokens are ignored here, but needed when the message is processed
98 // inside NaClIPCAdapter.
99 uint64_t file_token_lo
= 0;
100 uint64_t file_token_hi
= 0;
101 if (!filter_
->Send(new PpapiHostMsg_OpenResource(
106 LOG(ERROR
) << "ManifestService::OpenResource failed:" << file
;
111 // File tokens are used internally by NaClIPCAdapter and should have
112 // been cleared from the message when it is received here.
113 // These tokens should never be set for Non-SFI mode.
114 CHECK(file_token_lo
== 0);
115 CHECK(file_token_hi
== 0);
117 // Copy the file if we received a valid file descriptor. Otherwise, if we got
118 // a reply, the file doesn't exist, so provide an fd of -1.
119 // See IrtOpenResource() for how this function's result is interpreted.
120 if (ipc_fd
.is_file())
121 *fd
= ipc_fd
.descriptor().fd
;
127 #if !defined(OS_NACL_SFI)
130 pthread_mutex_t g_mu
= PTHREAD_MUTEX_INITIALIZER
;
131 std::map
<std::string
, int>* g_prefetched_fds
;
135 void RegisterPreopenedDescriptorsNonSfi(
136 std::map
<std::string
, int>* key_fd_map
) {
137 pthread_mutex_lock(&g_mu
);
138 DCHECK(!g_prefetched_fds
);
139 g_prefetched_fds
= key_fd_map
;
140 pthread_mutex_unlock(&g_mu
);
144 int IrtOpenResource(const char* file
, int* fd
) {
145 // Remove leading '/' character.
149 #if !defined(OS_NACL_SFI)
150 // Fast path for prefetched FDs.
151 pthread_mutex_lock(&g_mu
);
152 if (g_prefetched_fds
) {
153 std::map
<std::string
, int>::iterator it
= g_prefetched_fds
->find(file
);
154 if (it
!= g_prefetched_fds
->end()) {
156 g_prefetched_fds
->erase(it
);
157 pthread_mutex_unlock(&g_mu
);
161 pthread_mutex_unlock(&g_mu
);
164 ManifestService
* manifest_service
= GetManifestService();
165 if (manifest_service
== NULL
||
166 !manifest_service
->OpenResource(file
, fd
)) {
169 return (*fd
== -1) ? NACL_ABI_ENOENT
: 0;