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

Commit d9e3619d authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I3d165ae7,I112021ed into main

* changes:
  Refactors client acceptance logic in RpcServer::join
  Add ifdefs to compile out Parcel::Blob code
parents 60be4e8c da5e6078
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -732,7 +732,7 @@ cc_library {
}

cc_library {
    name: "libbinder_rpc_no_blob",
    name: "libbinder_rpc_no_legacy",
    vendor_available: true,
    defaults: [
        "libbinder_common_defaults",
+4 −2
Original line number Diff line number Diff line
@@ -24,9 +24,9 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef BINDER_DISABLE_BLOB
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/stat.h>
#endif // BINDER_DISABLE_BLOB
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
@@ -3500,6 +3500,7 @@ size_t Parcel::getOpenAshmemSize() const

// --- Parcel::Blob ---

#ifndef BINDER_DISABLE_BLOB
Parcel::Blob::Blob() :
        mFd(-1), mData(nullptr), mSize(0), mMutable(false) {
}
@@ -3530,5 +3531,6 @@ void Parcel::Blob::clear() {
    mSize = 0;
    mMutable = false;
}
#endif // BINDER_DISABLE_BLOB

} // namespace android
+43 −37
Original line number Diff line number Diff line
@@ -273,56 +273,62 @@ void RpcServer::join() {

    status_t status;
    while ((status = mShutdownTrigger->triggerablePoll(mServer, POLLIN)) == OK) {
        status = acceptConnection(RpcSession::join);
        if (status == DEAD_OBJECT) {
            break;
        }
    }
    LOG_RPC_DETAIL("RpcServer::join exiting with %s", statusToString(status).c_str());

    if constexpr (kEnableRpcThreads) {
        RpcMutexLockGuard _l(mLock);
        mJoinThreadRunning = false;
    } else {
        // Multi-threaded builds clear this in shutdown(), but we need it valid
        // so the loop above exits cleanly
        mShutdownTrigger = nullptr;
    }
    mShutdownCv.notify_all();
}

status_t RpcServer::acceptConnection(
        std::function<void(sp<RpcSession>&&, RpcSession::PreJoinSetupResult&&)>&& joinFn) {
    RpcTransportFd clientFd;
    std::array<uint8_t, kRpcAddressSize> addr;
    static_assert(addr.size() >= sizeof(sockaddr_storage), "kRpcAddressSize is too small");
    socklen_t addrLen = addr.size();

        RpcTransportFd clientSocket;
        if ((status = mAcceptFn(*this, &clientSocket)) != OK) {
            if (status == DEAD_OBJECT) {
                break;
            } else {
    status_t status;
    if ((status = mAcceptFn(*this, &clientFd)) != OK) {
        if (status != DEAD_OBJECT) {
            ALOGE("Accept returned error %s", statusToString(status).c_str());
                continue;
        }
        return status;
    }

        LOG_RPC_DETAIL("accept on fd %d yields fd %d", mServer.fd.get(), clientSocket.fd.get());
    LOG_RPC_DETAIL("accept on fd %d yields fd %d", mServer.fd.get(), clientFd.fd.get());

        if (getpeername(clientSocket.fd.get(), reinterpret_cast<sockaddr*>(addr.data()),
                        &addrLen)) {
    if (getpeername(clientFd.fd.get(), reinterpret_cast<sockaddr*>(addr.data()), &addrLen)) {
        ALOGE("Could not getpeername socket: %s", strerror(errno));
            continue;
        return EINVAL;
    }

    if (mConnectionFilter != nullptr && !mConnectionFilter(addr.data(), addrLen)) {
            ALOGE("Dropped client connection fd %d", clientSocket.fd.get());
            continue;
        ALOGE("Dropped client connection fd %d", clientFd.fd.get());
        return EINVAL;
    }

    {
        RpcMutexLockGuard _l(mLock);
        RpcMaybeThread thread =
                    RpcMaybeThread(&RpcServer::establishConnection,
                                   sp<RpcServer>::fromExisting(this), std::move(clientSocket), addr,
                                   addrLen, RpcSession::join);

                RpcMaybeThread(&RpcServer::establishConnection, sp<RpcServer>::fromExisting(this),
                               std::move(clientFd), addr, addrLen, std::move(joinFn));
        auto& threadRef = mConnectingThreads[thread.get_id()];
        threadRef = std::move(thread);
        rpcJoinIfSingleThreaded(threadRef);
    }
    }
    LOG_RPC_DETAIL("RpcServer::join exiting with %s", statusToString(status).c_str());

    if constexpr (kEnableRpcThreads) {
        RpcMutexLockGuard _l(mLock);
        mJoinThreadRunning = false;
    } else {
        // Multi-threaded builds clear this in shutdown(), but we need it valid
        // so the loop above exits cleanly
        mShutdownTrigger = nullptr;
    }
    mShutdownCv.notify_all();
    return OK;
}

bool RpcServer::shutdown() {
+3 −0
Original line number Diff line number Diff line
@@ -98,6 +98,9 @@
    },
    {
      "name": "binderStatsPusherUnitTest"
    },
    {
      "name": "binderRpcTestNoLegacy"
    }
  ],
  "presubmit-large": [
+8 −4
Original line number Diff line number Diff line
@@ -1399,6 +1399,7 @@ private:

    size_t mReserved;

#ifndef BINDER_DISABLE_BLOB
    class Blob {
    public:
        LIBBINDER_EXPORTED Blob();
@@ -1418,6 +1419,7 @@ private:
        size_t mSize;
        bool mMutable;
    };
#endif // BINDER_DISABLE_BLOB

#if defined(__clang__)
#pragma clang diagnostic push
@@ -1469,6 +1471,7 @@ private:
    LIBBINDER_EXPORTED status_t read(FlattenableHelperInterface& val) const;

public:
#ifndef BINDER_DISABLE_BLOB
    class ReadableBlob : public Blob {
        friend class Parcel;
    public:
@@ -1481,6 +1484,7 @@ public:
    public:
        LIBBINDER_EXPORTED inline void* data() { return mData; }
    };
#endif // BINDER_DISABLE_BLOB

    /**
     * Returns the total amount of ashmem memory owned by this object.
Loading