Loading libs/binder/RpcServer.cpp +18 −0 Original line number Diff line number Diff line Loading @@ -272,8 +272,26 @@ void RpcServer::onSessionTerminating(const sp<RpcSession>& session) { } bool RpcServer::hasServer() { LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); std::lock_guard<std::mutex> _l(mLock); return mServer.ok(); } unique_fd RpcServer::releaseServer() { LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); std::lock_guard<std::mutex> _l(mLock); return std::move(mServer); } bool RpcServer::setupExternalServer(base::unique_fd serverFd) { LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); std::lock_guard<std::mutex> _l(mLock); if (mServer.ok()) { ALOGE("Each RpcServer can only have one server."); return false; } mServer = std::move(serverFd); return true; } } // namespace android libs/binder/include/binder/RpcServer.h +11 −0 Original line number Diff line number Diff line Loading @@ -79,6 +79,17 @@ public: */ [[nodiscard]] bool hasServer(); /** * If hasServer(), return the server FD. Otherwise return invalid FD. */ [[nodiscard]] base::unique_fd releaseServer(); /** * Set up server using an external FD previously set up by releaseServer(). * Return false if there's already a server. */ bool setupExternalServer(base::unique_fd serverFd); void iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); /** Loading libs/binder/tests/binderRpcTest.cpp +13 −0 Original line number Diff line number Diff line Loading @@ -50,6 +50,19 @@ TEST(BinderRpcParcel, EntireParcelFormatted) { EXPECT_DEATH(p.markForBinder(sp<BBinder>::make()), ""); } TEST(BinderRpc, SetExternalServer) { base::unique_fd sink(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR))); int sinkFd = sink.get(); auto server = RpcServer::make(); server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); ASSERT_FALSE(server->hasServer()); ASSERT_TRUE(server->setupExternalServer(std::move(sink))); ASSERT_TRUE(server->hasServer()); base::unique_fd retrieved = server->releaseServer(); ASSERT_FALSE(server->hasServer()); ASSERT_EQ(sinkFd, retrieved.get()); } using android::binder::Status; #define EXPECT_OK(status) \ Loading Loading
libs/binder/RpcServer.cpp +18 −0 Original line number Diff line number Diff line Loading @@ -272,8 +272,26 @@ void RpcServer::onSessionTerminating(const sp<RpcSession>& session) { } bool RpcServer::hasServer() { LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); std::lock_guard<std::mutex> _l(mLock); return mServer.ok(); } unique_fd RpcServer::releaseServer() { LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); std::lock_guard<std::mutex> _l(mLock); return std::move(mServer); } bool RpcServer::setupExternalServer(base::unique_fd serverFd) { LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); std::lock_guard<std::mutex> _l(mLock); if (mServer.ok()) { ALOGE("Each RpcServer can only have one server."); return false; } mServer = std::move(serverFd); return true; } } // namespace android
libs/binder/include/binder/RpcServer.h +11 −0 Original line number Diff line number Diff line Loading @@ -79,6 +79,17 @@ public: */ [[nodiscard]] bool hasServer(); /** * If hasServer(), return the server FD. Otherwise return invalid FD. */ [[nodiscard]] base::unique_fd releaseServer(); /** * Set up server using an external FD previously set up by releaseServer(). * Return false if there's already a server. */ bool setupExternalServer(base::unique_fd serverFd); void iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); /** Loading
libs/binder/tests/binderRpcTest.cpp +13 −0 Original line number Diff line number Diff line Loading @@ -50,6 +50,19 @@ TEST(BinderRpcParcel, EntireParcelFormatted) { EXPECT_DEATH(p.markForBinder(sp<BBinder>::make()), ""); } TEST(BinderRpc, SetExternalServer) { base::unique_fd sink(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR))); int sinkFd = sink.get(); auto server = RpcServer::make(); server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); ASSERT_FALSE(server->hasServer()); ASSERT_TRUE(server->setupExternalServer(std::move(sink))); ASSERT_TRUE(server->hasServer()); base::unique_fd retrieved = server->releaseServer(); ASSERT_FALSE(server->hasServer()); ASSERT_EQ(sinkFd, retrieved.get()); } using android::binder::Status; #define EXPECT_OK(status) \ Loading