binder_server.cc
Go to the documentation of this file.
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
18 
19 #ifndef GRPC_NO_BINDER
20 
21 #include <memory>
22 #include <string>
23 #include <utility>
24 
25 #include "absl/memory/memory.h"
26 
27 #include <grpc/grpc.h>
28 
35 
36 #ifdef GPR_SUPPORT_BINDER_TRANSPORT
37 
38 #include <jni.h>
39 
40 extern "C" {
41 
42 // This will be invoked from
43 // src/core/ext/transport/binder/java/io/grpc/binder/cpp/GrpcCppServerBuilder.java
44 JNIEXPORT jobject JNICALL
45 Java_io_grpc_binder_cpp_GrpcCppServerBuilder_GetEndpointBinderInternal__Ljava_lang_String_2(
46  JNIEnv* jni_env, jobject, jstring conn_id_jstring) {
47  grpc_binder::ndk_util::AIBinder* ai_binder = nullptr;
48 
49  {
50  // This block is the scope of conn_id c-string
51  jboolean isCopy;
52  const char* conn_id = jni_env->GetStringUTFChars(conn_id_jstring, &isCopy);
53  ai_binder = static_cast<grpc_binder::ndk_util::AIBinder*>(
55  if (ai_binder == nullptr) {
56  gpr_log(GPR_ERROR, "Cannot find endpoint binder with connection id = %s",
57  conn_id);
58  }
59  if (isCopy == JNI_TRUE) {
60  jni_env->ReleaseStringUTFChars(conn_id_jstring, conn_id);
61  }
62  }
63 
64  if (ai_binder == nullptr) {
65  return nullptr;
66  }
67 
68  return grpc_binder::ndk_util::AIBinder_toJavaBinder(jni_env, ai_binder);
69 }
70 }
71 
72 #endif
73 
74 namespace grpc {
75 namespace experimental {
76 namespace binder {
77 
80 }
81 
82 void AddEndpointBinder(const std::string& service, void* endpoint_binder) {
83  grpc_add_endpoint_binder(service, endpoint_binder);
84 }
85 
88 }
89 
90 } // namespace binder
91 } // namespace experimental
92 } // namespace grpc
93 
95  nullptr;
96 
97 namespace {
98 
99 grpc_core::Mutex* GetBinderPoolMutex() {
100  static grpc_core::Mutex* mu = new grpc_core::Mutex();
101  return mu;
102 }
103 
104 } // namespace
105 
107  void* endpoint_binder) {
108  grpc_core::MutexLock lock(GetBinderPoolMutex());
109  if (g_endpoint_binder_pool == nullptr) {
111  }
112  (*g_endpoint_binder_pool)[service] = endpoint_binder;
113 }
114 
116  grpc_core::MutexLock lock(GetBinderPoolMutex());
117  if (g_endpoint_binder_pool == nullptr) {
118  return;
119  }
121 }
122 
124  grpc_core::MutexLock lock(GetBinderPoolMutex());
125  if (g_endpoint_binder_pool == nullptr) {
126  return nullptr;
127  }
128  auto iter = g_endpoint_binder_pool->find(service);
129  return iter == g_endpoint_binder_pool->end() ? nullptr : iter->second;
130 }
131 
132 namespace grpc_core {
133 
135  public:
138  std::shared_ptr<grpc::experimental::binder::SecurityPolicy>
139  security_policy)
140  : server_(server),
141  addr_(std::move(addr)),
142  factory_(std::move(factory)),
143  security_policy_(security_policy) {}
144 
145  void Start(Server* /*server*/,
146  const std::vector<grpc_pollset*>* /*pollsets*/) override {
149  int uid) { return OnSetupTransport(code, parcel, uid); });
150  endpoint_binder_ = tx_receiver_->GetRawBinder();
152  }
153 
155  return nullptr;
156  }
157 
158  void SetOnDestroyDone(grpc_closure* on_destroy_done) override {
159  on_destroy_done_ = on_destroy_done;
160  }
161 
162  void Orphan() override { delete this; }
163 
165  ExecCtx::Get()->Flush();
166  if (on_destroy_done_) {
168  ExecCtx::Get()->Flush();
169  }
171  }
172 
173  private:
175  grpc_binder::ReadableParcel* parcel, int uid) {
179  return absl::InvalidArgumentError("Not a SETUP_TRANSPORT request");
180  }
181 
182  gpr_log(GPR_INFO, "BinderServerListener calling uid = %d", uid);
183  if (!security_policy_->IsAuthorized(uid)) {
184  // TODO(mingcl): For now we just ignore this unauthorized
185  // SETUP_TRANSPORT transaction and ghost the client. Check if we should
186  // send back a SHUTDOWN_TRANSPORT in this case.
188  "UID " + std::to_string(uid) +
189  " is not allowed to connect to this "
190  "server according to security policy.");
191  }
192 
193  int version;
194  absl::Status status = parcel->ReadInt32(&version);
195  if (!status.ok()) {
196  return status;
197  }
198  gpr_log(GPR_INFO, "BinderTransport client protocol version = %d", version);
199  // TODO(mingcl): Make sure we only give client a version that is not newer
200  // than the version they specify. For now, we always tell client that we
201  // only support version=1.
202  std::unique_ptr<grpc_binder::Binder> client_binder{};
203  status = parcel->ReadBinder(&client_binder);
204  if (!status.ok()) {
205  return status;
206  }
207  if (!client_binder) {
208  return absl::InvalidArgumentError("NULL binder read from the parcel");
209  }
210  client_binder->Initialize();
211  // Finish the second half of SETUP_TRANSPORT in
212  // grpc_create_binder_transport_server().
214  std::move(client_binder), security_policy_);
215  GPR_ASSERT(server_transport);
218  server_->SetupTransport(server_transport, nullptr, args, nullptr);
221  }
222 
227  std::shared_ptr<grpc::experimental::binder::SecurityPolicy> security_policy_;
228  void* endpoint_binder_ = nullptr;
229  std::unique_ptr<grpc_binder::TransactionReceiver> tx_receiver_;
230 };
231 
233  BinderTxReceiverFactory factory,
234  std::shared_ptr<grpc::experimental::binder::SecurityPolicy>
235  security_policy) {
236  // TODO(mingcl): Check if the addr is valid here after binder address resolver
237  // related code are merged.
238  const std::string kBinderUriScheme = "binder:";
239  if (addr.compare(0, kBinderUriScheme.size(), kBinderUriScheme) != 0) {
240  return false;
241  }
242  std::string conn_id = addr.substr(kBinderUriScheme.size());
243  Server* core_server = Server::FromC(server);
244  core_server->AddListener(
246  core_server, conn_id, std::move(factory), security_policy)));
247  return true;
248 }
249 
250 } // namespace grpc_core
251 #endif
ndk_binder.h
grpc_add_endpoint_binder
void grpc_add_endpoint_binder(const std::string &service, void *endpoint_binder)
Definition: binder_server.cc:106
absl::InvalidArgumentError
Status InvalidArgumentError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:351
grpc_core::Server::SetupTransport
grpc_error_handle SetupTransport(grpc_transport *transport, grpc_pollset *accepting_pollset, const grpc_channel_args *args, const RefCountedPtr< channelz::SocketNode > &socket_node)
Definition: src/core/lib/surface/server.cc:605
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
grpc_core::BinderServerListener::BinderServerListener
BinderServerListener(Server *server, std::string addr, BinderTxReceiverFactory factory, std::shared_ptr< grpc::experimental::binder::SecurityPolicy > security_policy)
Definition: binder_server.cc:136
grpc_binder::BinderTransportTxCode::SETUP_TRANSPORT
@ SETUP_TRANSPORT
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
grpc
Definition: grpcpp/alarm.h:33
grpc_core::BinderServerListener::security_policy_
std::shared_ptr< grpc::experimental::binder::SecurityPolicy > security_policy_
Definition: binder_server.cc:227
grpc_get_endpoint_binder
void * grpc_get_endpoint_binder(const std::string &service)
Definition: binder_server.cc:123
binder_server.h
grpc_core::AddBinderPort
bool AddBinderPort(const std::string &addr, grpc_server *server, BinderTxReceiverFactory factory, std::shared_ptr< grpc::experimental::binder::SecurityPolicy > security_policy)
Definition: binder_server.cc:232
grpc_core::BinderServerListener::channelz_listen_socket_node
channelz::ListenSocketNode * channelz_listen_socket_node() const override
Definition: binder_server.cc:154
grpc_core::Server::channel_args
const grpc_channel_args * channel_args() const
Definition: src/core/lib/surface/server.h:132
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_core::BinderServerListener::factory_
BinderTxReceiverFactory factory_
Definition: binder_server.cc:226
transaction_code_t
uint32_t transaction_code_t
Definition: binder_constants.h:24
grpc_core::BinderServerListener
Definition: binder_server.cc:134
grpc::experimental::binder::GetEndpointBinder
void * GetEndpointBinder(const std::string &service)
Definition: binder_server.cc:78
status
absl::Status status
Definition: rls.cc:251
version
Definition: version.py:1
grpc_channel_args
Definition: grpc_types.h:132
absl::PermissionDeniedError
Status PermissionDeniedError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:363
grpc_core::BinderServerListener::endpoint_binder_
void * endpoint_binder_
Definition: binder_server.cc:228
grpc_binder::BinderTransportTxCode
BinderTransportTxCode
Definition: binder_constants.h:31
grpc_core::BinderServerListener::~BinderServerListener
~BinderServerListener() override
Definition: binder_server.cc:164
grpc_core::BinderServerListener::SetOnDestroyDone
void SetOnDestroyDone(grpc_closure *on_destroy_done) override
Definition: binder_server.cc:158
grpc_core::BinderServerListener::on_destroy_done_
grpc_closure * on_destroy_done_
Definition: binder_server.cc:224
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::BinderServerListener::server_
Server * server_
Definition: binder_server.cc:223
binder_android.h
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_core::BinderServerListener::tx_receiver_
std::unique_ptr< grpc_binder::TransactionReceiver > tx_receiver_
Definition: binder_server.cc:229
conf.version
string version
Definition: doc/python/sphinx/conf.py:36
grpc.h
grpc_core::Server::AddListener
void AddListener(OrphanablePtr< ListenerInterface > listener)
Definition: src/core/lib/surface/server.cc:559
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
grpc_binder::ReadableParcel::ReadBinder
virtual absl::Status ReadBinder(std::unique_ptr< Binder > *data)=0
grpc_channel_args_copy
grpc_channel_args * grpc_channel_args_copy(const grpc_channel_args *src)
Definition: channel_args.cc:285
grpc_server
struct grpc_server grpc_server
Definition: grpc_types.h:65
grpc_core::BinderServerListener::OnSetupTransport
absl::Status OnSetupTransport(transaction_code_t code, grpc_binder::ReadableParcel *parcel, int uid)
Definition: binder_server.cc:174
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_create_binder_transport_server
grpc_transport * grpc_create_binder_transport_server(std::unique_ptr< grpc_binder::Binder > client_binder, std::shared_ptr< grpc::experimental::binder::SecurityPolicy > security_policy)
Definition: binder_transport.cc:752
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_core::BinderTxReceiverFactory
std::function< std::unique_ptr< grpc_binder::TransactionReceiver >(grpc_binder::TransactionReceiver::OnTransactCb)> BinderTxReceiverFactory
Definition: binder_server.h:58
g_endpoint_binder_pool
static absl::flat_hash_map< std::string, void * > * g_endpoint_binder_pool
Definition: binder_server.cc:94
grpc::experimental::binder::RemoveEndpointBinder
void RemoveEndpointBinder(const std::string &service)
Definition: binder_server.cc:86
grpc::experimental::binder::AddEndpointBinder
void AddEndpointBinder(const std::string &service, void *endpoint_binder)
Definition: binder_server.cc:82
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
grpc_core::Server
Definition: src/core/lib/surface/server.h:75
server
Definition: examples/python/async_streaming/server.py:1
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
google::protobuf.internal::Mutex
WrappedMutex Mutex
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/mutex.h:113
grpc_core::OrphanablePtr
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:64
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
absl::flat_hash_map
Definition: abseil-cpp/absl/container/flat_hash_map.h:113
grpc_remove_endpoint_binder
void grpc_remove_endpoint_binder(const std::string &service)
Definition: binder_server.cc:115
exec_ctx.h
server.h
grpc_core::BinderServerListener::addr_
std::string addr_
Definition: binder_server.cc:225
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
grpc_binder::ReadableParcel::ReadInt32
virtual absl::Status ReadInt32(int32_t *data)=0
grpc_transport
Definition: transport_impl.h:89
grpc_core::BinderServerListener::Orphan
void Orphan() override
Definition: binder_server.cc:162
iter
Definition: test_winkernel.cpp:47
grpc_core::channelz::ListenSocketNode
Definition: channelz.h:351
code
Definition: bloaty/third_party/zlib/contrib/infback9/inftree9.h:24
grpc_binder::ReadableParcel
Definition: binder.h:66
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
grpc_error
Definition: error_internal.h:42
grpc_error_to_absl_status
absl::Status grpc_error_to_absl_status(grpc_error_handle error)
Definition: error_utils.cc:156
grpc_core::CppImplOf< Server, grpc_server >::FromC
static Server * FromC(grpc_server *c_type)
Definition: cpp_impl_of.h:30
grpc_core::Server::ListenerInterface
Definition: src/core/lib/surface/server.h:109
to_string
static bool to_string(zval *from)
Definition: protobuf/php/ext/google/protobuf/convert.c:333
grpc_closure
Definition: closure.h:56
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
grpc_core::BinderServerListener::Start
void Start(Server *, const std::vector< grpc_pollset * > *) override
Definition: binder_server.cc:145
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
error_utils.h
binder_transport.h
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:38