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

Commit ebafe331 authored by Steven Moreland's avatar Steven Moreland
Browse files

RpcServer: be thread safe

It's tempting to add an RpcServerConfig object, but we would also need
to allocate it on the heap. After some consideration, we don't really
care if a server changes dynamically. So, lifting all restrictions.

Bug: 185167543
Test: N/A
Change-Id: I134813203d0a2d8256ed88c6e59b4bb8d0bafc72
parent 007d5b68
Loading
Loading
Loading
Loading
+6 −2
Original line number Original line Diff line number Diff line
@@ -48,16 +48,20 @@ sp<RpcConnection> RpcServer::addClientConnection() {


    auto connection = RpcConnection::make();
    auto connection = RpcConnection::make();
    connection->setForServer(sp<RpcServer>::fromExisting(this));
    connection->setForServer(sp<RpcServer>::fromExisting(this));
    {
        std::lock_guard<std::mutex> _l(mLock);
        mConnections.push_back(connection);
        mConnections.push_back(connection);
    }
    return connection;
    return connection;
}
}


void RpcServer::setRootObject(const sp<IBinder>& binder) {
void RpcServer::setRootObject(const sp<IBinder>& binder) {
    LOG_ALWAYS_FATAL_IF(mRootObject != nullptr, "There can only be one root object");
    std::lock_guard<std::mutex> _l(mLock);
    mRootObject = binder;
    mRootObject = binder;
}
}


sp<IBinder> RpcServer::getRootObject() {
sp<IBinder> RpcServer::getRootObject() {
    std::lock_guard<std::mutex> _l(mLock);
    return mRootObject;
    return mRootObject;
}
}


+4 −13
Original line number Original line Diff line number Diff line
@@ -21,6 +21,8 @@
#include <utils/Errors.h>
#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <utils/RefBase.h>


#include <mutex>

// WARNING: This is a feature which is still in development, and it is subject
// WARNING: This is a feature which is still in development, and it is subject
// to radical change. Any production use of this may subject your code to any
// to radical change. Any production use of this may subject your code to any
// number of problems.
// number of problems.
@@ -30,9 +32,6 @@ namespace android {
/**
/**
 * This represents a server of an interface, which may be connected to by any
 * This represents a server of an interface, which may be connected to by any
 * number of clients over sockets.
 * number of clients over sockets.
 *
 * This object is not (currently) thread safe. All calls to it are expected to
 * happen at process startup.
 */
 */
class RpcServer final : public virtual RefBase {
class RpcServer final : public virtual RefBase {
public:
public:
@@ -50,17 +49,9 @@ public:
     */
     */
    sp<RpcConnection> addClientConnection();
    sp<RpcConnection> addClientConnection();


    /**
     * Allowing a server to explicitly drop clients would be easy to add here,
     * but it is not currently implemented, since users of this functionality
     * could not use similar functionality if they are running under real
     * binder.
     */
    // void drop(const sp<RpcConnection>& connection);

    /**
    /**
     * The root object can be retrieved by any client, without any
     * The root object can be retrieved by any client, without any
     * authentication.
     * authentication. TODO(b/183988761)
     */
     */
    void setRootObject(const sp<IBinder>& binder);
    void setRootObject(const sp<IBinder>& binder);


@@ -77,8 +68,8 @@ private:


    bool mAgreedExperimental = false;
    bool mAgreedExperimental = false;


    std::mutex mLock;
    sp<IBinder> mRootObject;
    sp<IBinder> mRootObject;

    std::vector<sp<RpcConnection>> mConnections; // per-client
    std::vector<sp<RpcConnection>> mConnections; // per-client
};
};