diff options
| author | 2023-05-24 17:48:59 +0000 | |
|---|---|---|
| committer | 2023-05-24 18:00:32 +0000 | |
| commit | dfb9218b34625b1639d28910848a5c206163cbab (patch) | |
| tree | ddd66dd0b71d427270c848b2147cc640d3fbca4f | |
| parent | 265e9e2841b4fe3b49dc365a7a2a70e3e62376a6 (diff) | |
RPC binder: expose session to per-client roots
Bug: 274156888
Test: N/A
Change-Id: Iad721ebea7ddd461ed41f2ba1fc08849309f7611
| -rw-r--r-- | libs/binder/RpcServer.cpp | 5 | ||||
| -rw-r--r-- | libs/binder/include/binder/RpcServer.h | 20 | ||||
| -rw-r--r-- | libs/binder/tests/binderRpcTestService.cpp | 7 | ||||
| -rw-r--r-- | libs/binder/tests/binderRpcTestServiceTrusty.cpp | 17 | ||||
| -rw-r--r-- | libs/binder/trusty/include/binder/RpcServerTrusty.h | 3 |
5 files changed, 32 insertions, 20 deletions
diff --git a/libs/binder/RpcServer.cpp b/libs/binder/RpcServer.cpp index 00e040f3a4..55fc16de45 100644 --- a/libs/binder/RpcServer.cpp +++ b/libs/binder/RpcServer.cpp @@ -154,7 +154,7 @@ void RpcServer::setRootObjectWeak(const wp<IBinder>& binder) { mRootObjectWeak = binder; } void RpcServer::setPerSessionRootObject( - std::function<sp<IBinder>(const void*, size_t)>&& makeObject) { + std::function<sp<IBinder>(wp<RpcSession> session, const void*, size_t)>&& makeObject) { RpcMutexLockGuard _l(mLock); mRootObject.clear(); mRootObjectWeak.clear(); @@ -515,7 +515,8 @@ void RpcServer::establishConnection( // if null, falls back to server root sp<IBinder> sessionSpecificRoot; if (server->mRootObjectFactory != nullptr) { - sessionSpecificRoot = server->mRootObjectFactory(addr.data(), addrLen); + sessionSpecificRoot = + server->mRootObjectFactory(wp<RpcSession>(session), addr.data(), addrLen); if (sessionSpecificRoot == nullptr) { ALOGE("Warning: server returned null from root object factory"); } diff --git a/libs/binder/include/binder/RpcServer.h b/libs/binder/include/binder/RpcServer.h index 56ac7b0c4f..b804f7b92a 100644 --- a/libs/binder/include/binder/RpcServer.h +++ b/libs/binder/include/binder/RpcServer.h @@ -163,14 +163,18 @@ public: * Allows a root object to be created for each session. * * Takes one argument: a callable that is invoked once per new session. - * The callable takes two arguments: a type-erased pointer to an OS- and - * transport-specific address structure, e.g., sockaddr_vm for vsock, and - * an integer representing the size in bytes of that structure. The - * callable should validate the size, then cast the type-erased pointer - * to a pointer to the actual type of the address, e.g., const void* to - * const sockaddr_vm*. + * The callable takes three arguments: + * - a weak pointer to the session. If you want to hold onto this in the root object, then + * you should keep a weak pointer, and promote it when needed. For instance, if you refer + * to this from the root object, then you could get ahold of transport-specific information. + * - a type-erased pointer to an OS- and transport-specific address structure, e.g., + * sockaddr_vm for vsock + * - an integer representing the size in bytes of that structure. The callable should + * validate the size, then cast the type-erased pointer to a pointer to the actual type of the + * address, e.g., const void* to const sockaddr_vm*. */ - void setPerSessionRootObject(std::function<sp<IBinder>(const void*, size_t)>&& object); + void setPerSessionRootObject( + std::function<sp<IBinder>(wp<RpcSession> session, const void*, size_t)>&& object); sp<IBinder> getRootObject(); /** @@ -272,7 +276,7 @@ private: sp<IBinder> mRootObject; wp<IBinder> mRootObjectWeak; - std::function<sp<IBinder>(const void*, size_t)> mRootObjectFactory; + std::function<sp<IBinder>(wp<RpcSession>, const void*, size_t)> mRootObjectFactory; std::function<bool(const void*, size_t)> mConnectionFilter; std::function<void(base::borrowed_fd)> mServerSocketModifier; std::map<std::vector<uint8_t>, sp<RpcSession>> mSessions; diff --git a/libs/binder/tests/binderRpcTestService.cpp b/libs/binder/tests/binderRpcTestService.cpp index 5e83fbfab2..cb09a7fbab 100644 --- a/libs/binder/tests/binderRpcTestService.cpp +++ b/libs/binder/tests/binderRpcTestService.cpp @@ -164,7 +164,12 @@ int main(int argc, char* argv[]) { } } - server->setPerSessionRootObject([&](const void* addrPtr, size_t len) { + server->setPerSessionRootObject([&](wp<RpcSession> session, const void* addrPtr, size_t len) { + { + sp<RpcSession> spSession = session.promote(); + CHECK_NE(nullptr, spSession.get()); + } + // UNIX sockets with abstract addresses return // sizeof(sa_family_t)==2 in addrlen CHECK_GE(len, sizeof(sa_family_t)); diff --git a/libs/binder/tests/binderRpcTestServiceTrusty.cpp b/libs/binder/tests/binderRpcTestServiceTrusty.cpp index 5c7a96af32..cb632e95bf 100644 --- a/libs/binder/tests/binderRpcTestServiceTrusty.cpp +++ b/libs/binder/tests/binderRpcTestServiceTrusty.cpp @@ -93,14 +93,15 @@ int main(void) { if (!serverInfo.server->setProtocolVersion(serverVersion)) { return EXIT_FAILURE; } - serverInfo.server->setPerSessionRootObject([=](const void* /*addrPtr*/, size_t /*len*/) { - auto service = sp<MyBinderRpcTestTrusty>::make(); - // Assign a unique connection identifier to service->port so - // getClientPort returns a unique value per connection - service->port = ++gConnectionCounter; - service->server = server; - return service; - }); + serverInfo.server->setPerSessionRootObject( + [=](wp<RpcSession> /*session*/, const void* /*addrPtr*/, size_t /*len*/) { + auto service = sp<MyBinderRpcTestTrusty>::make(); + // Assign a unique connection identifier to service->port so + // getClientPort returns a unique value per connection + service->port = ++gConnectionCounter; + service->server = server; + return service; + }); servers.push_back(std::move(serverInfo)); } diff --git a/libs/binder/trusty/include/binder/RpcServerTrusty.h b/libs/binder/trusty/include/binder/RpcServerTrusty.h index 119f2a393f..8924b3679f 100644 --- a/libs/binder/trusty/include/binder/RpcServerTrusty.h +++ b/libs/binder/trusty/include/binder/RpcServerTrusty.h @@ -68,7 +68,8 @@ public: } void setRootObject(const sp<IBinder>& binder) { mRpcServer->setRootObject(binder); } void setRootObjectWeak(const wp<IBinder>& binder) { mRpcServer->setRootObjectWeak(binder); } - void setPerSessionRootObject(std::function<sp<IBinder>(const void*, size_t)>&& object) { + void setPerSessionRootObject( + std::function<sp<IBinder>(wp<RpcSession> session, const void*, size_t)>&& object) { mRpcServer->setPerSessionRootObject(std::move(object)); } sp<IBinder> getRootObject() { return mRpcServer->getRootObject(); } |