diff options
author | 2021-05-01 02:55:20 +0000 | |
---|---|---|
committer | 2021-05-03 18:17:22 +0000 | |
commit | 7c5e6c2d4f6efc0887812bb7888a55aab3df23fa (patch) | |
tree | 9ce4055acc2de9c677db5782d40d4dffdf64bf71 /libs/binder/RpcConnection.cpp | |
parent | 611d15fe78f51d61226382f34f03b0834a3f60ea (diff) |
libbinder: RpcConnection: add ID
In preparation to have the server be able to distinguish clients and
clients to be able to dynamically create threads that are assigned to
them.
Future considerations:
- make ID impossible to guess (right now, one client might be able to
get ahold of a thread from a server). We may implement something here
or go for something existing like TLS.
- combine getting max threads and this? will wait until dynamic threads
are actually implemented and we know we need this ID and we're looking
at performance. For now this is a placeholder to enable dynamic client
APIs.
Bug: 185167543
Test: binderRpcTest
Change-Id: If8563c69930c23b9ca91090b4f59ef1f51073f24
Diffstat (limited to 'libs/binder/RpcConnection.cpp')
-rw-r--r-- | libs/binder/RpcConnection.cpp | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/libs/binder/RpcConnection.cpp b/libs/binder/RpcConnection.cpp index 930bcbde62..95eba87b68 100644 --- a/libs/binder/RpcConnection.cpp +++ b/libs/binder/RpcConnection.cpp @@ -115,6 +115,24 @@ status_t RpcConnection::sendDecStrong(const RpcAddress& address) { return state()->sendDecStrong(socket.fd(), address); } +status_t RpcConnection::readId() { + { + std::lock_guard<std::mutex> _l(mSocketMutex); + LOG_ALWAYS_FATAL_IF(mForServer != nullptr, "Can only update ID for client."); + } + + int32_t id; + + ExclusiveSocket socket(sp<RpcConnection>::fromExisting(this), SocketUse::CLIENT); + status_t status = + state()->getConnectionId(socket.fd(), sp<RpcConnection>::fromExisting(this), &id); + if (status != OK) return status; + + LOG_RPC_DETAIL("RpcConnection %p has id %d", this, id); + mId = id; + return OK; +} + void RpcConnection::join(unique_fd client) { // must be registered to allow arbitrary client code executing commands to // be able to do nested calls (we can't only read from it) @@ -134,10 +152,6 @@ void RpcConnection::join(unique_fd client) { "bad state: socket object guaranteed to be in list"); } -void RpcConnection::setForServer(const wp<RpcServer>& server) { - mForServer = server; -} - wp<RpcServer> RpcConnection::server() { return mForServer; } @@ -162,6 +176,12 @@ bool RpcConnection::setupSocketClient(const RpcSocketAddress& addr) { return false; } + if (status_t status = readId(); status != OK) { + ALOGE("Could not get connection id after initial connection to %s; %s", + addr.toString().c_str(), statusToString(status).c_str()); + return false; + } + // we've already setup one client for (size_t i = 0; i + 1 < numThreadsAvailable; i++) { // TODO(b/185167543): avoid race w/ accept4 not being called on server @@ -202,6 +222,11 @@ void RpcConnection::addClient(unique_fd fd) { mClients.push_back(connection); } +void RpcConnection::setForServer(const wp<RpcServer>& server, int32_t connectionId) { + mId = connectionId; + mForServer = server; +} + sp<RpcConnection::ConnectionSocket> RpcConnection::assignServerToThisThread(unique_fd fd) { std::lock_guard<std::mutex> _l(mSocketMutex); sp<ConnectionSocket> connection = sp<ConnectionSocket>::make(); |