Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9aa49895 authored by Steven Moreland's avatar Steven Moreland Committed by Automerger Merge Worker
Browse files

Merge "RpcServer: expose server fd" am: e81f29d2 am: fbbcccc7 am:...

Merge "RpcServer: expose server fd" am: e81f29d2 am: fbbcccc7 am: 8eba7888 am: 8c68daf6 am: 728b8eab

Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/1705663

Change-Id: Ic2e8d287ecd70e504c6f510709ede087d496b662
parents 60a8a47e 728b8eab
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -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
+11 −0
Original line number Diff line number Diff line
@@ -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();

    /**
+13 −0
Original line number Diff line number Diff line
@@ -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)                 \