diff options
-rw-r--r-- | libs/binder/Android.bp | 4 | ||||
-rw-r--r-- | libs/binder/OS.h | 6 | ||||
-rw-r--r-- | libs/binder/OS_android.cpp | 31 | ||||
-rw-r--r-- | libs/binder/OS_unix_base.cpp (renamed from libs/binder/OS.cpp) | 4 | ||||
-rw-r--r-- | libs/binder/Parcel.cpp | 10 | ||||
-rw-r--r-- | libs/binder/RpcServer.cpp | 8 | ||||
-rw-r--r-- | libs/binder/RpcSession.cpp | 10 | ||||
-rw-r--r-- | libs/binder/RpcTransportRaw.cpp | 6 | ||||
-rw-r--r-- | libs/binder/include/binder/RpcThreads.h | 10 | ||||
-rw-r--r-- | libs/binder/tests/binderRpcTest.cpp | 4 | ||||
-rw-r--r-- | libs/binder/trusty/OS.cpp | 4 | ||||
-rw-r--r-- | libs/binder/trusty/rules.mk | 1 |
12 files changed, 62 insertions, 36 deletions
diff --git a/libs/binder/Android.bp b/libs/binder/Android.bp index 6c2b313f8a..fd2b27f839 100644 --- a/libs/binder/Android.bp +++ b/libs/binder/Android.bp @@ -118,7 +118,8 @@ cc_defaults { ], srcs: [ - "OS.cpp", + "OS_android.cpp", + "OS_unix_base.cpp", "RpcTransportRaw.cpp", ], @@ -249,6 +250,7 @@ cc_library_shared { srcs: [ // Trusty-specific files + "OS_android.cpp", "trusty/logging.cpp", "trusty/OS.cpp", "trusty/RpcServerTrusty.cpp", diff --git a/libs/binder/OS.h b/libs/binder/OS.h index fecae31763..db4b7a5379 100644 --- a/libs/binder/OS.h +++ b/libs/binder/OS.h @@ -23,7 +23,7 @@ #include <binder/RpcTransport.h> #include <utils/Errors.h> -namespace android { +namespace android::binder::os { android::base::Result<void> setNonBlocking(android::base::borrowed_fd fd); @@ -41,4 +41,6 @@ ssize_t receiveMessageFromSocket( const RpcTransportFd& socket, iovec* iovs, int niovs, std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds); -} // namespace android +uint64_t GetThreadId(); + +} // namespace android::binder::os diff --git a/libs/binder/OS_android.cpp b/libs/binder/OS_android.cpp new file mode 100644 index 0000000000..1e1442b7c0 --- /dev/null +++ b/libs/binder/OS_android.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "OS.h" + +#include <android-base/threads.h> + +namespace android::binder::os { + +uint64_t GetThreadId() { +#ifdef BINDER_RPC_SINGLE_THREADED + return 0; +#else + return base::GetThreadId(); +#endif +} + +} // namespace android::binder::os diff --git a/libs/binder/OS.cpp b/libs/binder/OS_unix_base.cpp index ce60e33ba7..81933d5c6b 100644 --- a/libs/binder/OS.cpp +++ b/libs/binder/OS_unix_base.cpp @@ -24,7 +24,7 @@ using android::base::ErrnoError; using android::base::Result; -namespace android { +namespace android::binder::os { // Linux kernel supports up to 253 (from SCM_MAX_FD) for unix sockets. constexpr size_t kMaxFdsPerMsg = 253; @@ -162,4 +162,4 @@ ssize_t receiveMessageFromSocket( return TEMP_FAILURE_RETRY(recvmsg(socket.fd.get(), &msg, MSG_NOSIGNAL)); } -} // namespace android +} // namespace android::binder::os diff --git a/libs/binder/Parcel.cpp b/libs/binder/Parcel.cpp index d92de1b151..16944a6221 100644 --- a/libs/binder/Parcel.cpp +++ b/libs/binder/Parcel.cpp @@ -621,7 +621,7 @@ status_t Parcel::appendFrom(const Parcel* parcel, size_t offset, size_t len) { // To match kernel binder behavior, we always dup, even if the // FD was unowned in the source parcel. int newFd = -1; - if (status_t status = dupFileDescriptor(oldFd, &newFd); status != OK) { + if (status_t status = binder::os::dupFileDescriptor(oldFd, &newFd); status != OK) { ALOGW("Failed to duplicate file descriptor %d: %s", oldFd, strerror(-status)); } rpcFields->mFds->emplace_back(base::unique_fd(newFd)); @@ -1513,7 +1513,7 @@ status_t Parcel::writeFileDescriptor(int fd, bool takeOwnership) { status_t Parcel::writeDupFileDescriptor(int fd) { int dupFd; - if (status_t err = dupFileDescriptor(fd, &dupFd); err != OK) { + if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) { return err; } status_t err = writeFileDescriptor(dupFd, true /*takeOwnership*/); @@ -1532,7 +1532,7 @@ status_t Parcel::writeParcelFileDescriptor(int fd, bool takeOwnership) status_t Parcel::writeDupParcelFileDescriptor(int fd) { int dupFd; - if (status_t err = dupFileDescriptor(fd, &dupFd); err != OK) { + if (status_t err = binder::os::dupFileDescriptor(fd, &dupFd); err != OK) { return err; } status_t err = writeParcelFileDescriptor(dupFd, true /*takeOwnership*/); @@ -2345,7 +2345,7 @@ status_t Parcel::readUniqueFileDescriptor(base::unique_fd* val) const } int dupFd; - if (status_t err = dupFileDescriptor(got, &dupFd); err != OK) { + if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) { return BAD_VALUE; } @@ -2367,7 +2367,7 @@ status_t Parcel::readUniqueParcelFileDescriptor(base::unique_fd* val) const } int dupFd; - if (status_t err = dupFileDescriptor(got, &dupFd); err != OK) { + if (status_t err = binder::os::dupFileDescriptor(got, &dupFd); err != OK) { return BAD_VALUE; } diff --git a/libs/binder/RpcServer.cpp b/libs/binder/RpcServer.cpp index 9622313699..db31737aef 100644 --- a/libs/binder/RpcServer.cpp +++ b/libs/binder/RpcServer.cpp @@ -56,7 +56,7 @@ RpcServer::~RpcServer() { sp<RpcServer> RpcServer::make(std::unique_ptr<RpcTransportCtxFactory> rpcTransportCtxFactory) { // Default is without TLS. if (rpcTransportCtxFactory == nullptr) - rpcTransportCtxFactory = makeDefaultRpcTransportCtxFactory(); + rpcTransportCtxFactory = binder::os::makeDefaultRpcTransportCtxFactory(); auto ctx = rpcTransportCtxFactory->newServerCtx(); if (ctx == nullptr) return nullptr; return sp<RpcServer>::make(std::move(ctx)); @@ -215,7 +215,7 @@ status_t RpcServer::recvmsgSocketConnection(const RpcServer& server, RpcTranspor iovec iov{&zero, sizeof(zero)}; std::vector<std::variant<base::unique_fd, base::borrowed_fd>> fds; - ssize_t num_bytes = receiveMessageFromSocket(server.mServer, &iov, 1, &fds); + ssize_t num_bytes = binder::os::receiveMessageFromSocket(server.mServer, &iov, 1, &fds); if (num_bytes < 0) { int savedErrno = errno; ALOGE("Failed recvmsg: %s", strerror(savedErrno)); @@ -230,7 +230,7 @@ status_t RpcServer::recvmsgSocketConnection(const RpcServer& server, RpcTranspor } unique_fd fd(std::move(std::get<unique_fd>(fds.back()))); - if (auto res = setNonBlocking(fd); !res.ok()) { + if (auto res = binder::os::setNonBlocking(fd); !res.ok()) { ALOGE("Failed setNonBlocking: %s", res.error().message().c_str()); return res.error().code() == 0 ? UNKNOWN_ERROR : -res.error().code(); } @@ -487,7 +487,7 @@ void RpcServer::establishConnection( return; } - auto status = getRandomBytes(sessionId.data(), sessionId.size()); + auto status = binder::os::getRandomBytes(sessionId.data(), sessionId.size()); if (status != OK) { ALOGE("Failed to read random session ID: %s", strerror(-status)); return; diff --git a/libs/binder/RpcSession.cpp b/libs/binder/RpcSession.cpp index edac56ba3b..fa8f2b51ac 100644 --- a/libs/binder/RpcSession.cpp +++ b/libs/binder/RpcSession.cpp @@ -69,7 +69,7 @@ RpcSession::~RpcSession() { sp<RpcSession> RpcSession::make() { // Default is without TLS. - return make(makeDefaultRpcTransportCtxFactory()); + return make(binder::os::makeDefaultRpcTransportCtxFactory()); } sp<RpcSession> RpcSession::make(std::unique_ptr<RpcTransportCtxFactory> rpcTransportCtxFactory) { @@ -194,7 +194,7 @@ status_t RpcSession::setupPreconnectedClient(base::unique_fd fd, fd = request(); if (!fd.ok()) return BAD_VALUE; } - if (auto res = setNonBlocking(fd); !res.ok()) { + if (auto res = binder::os::setNonBlocking(fd); !res.ok()) { ALOGE("setupPreconnectedClient: %s", res.error().message().c_str()); return res.error().code() == 0 ? UNKNOWN_ERROR : -res.error().code(); } @@ -768,7 +768,7 @@ status_t RpcSession::addOutgoingConnection(std::unique_ptr<RpcTransport> rpcTran { RpcMutexLockGuard _l(mMutex); connection->rpcTransport = std::move(rpcTransport); - connection->exclusiveTid = rpcGetThreadId(); + connection->exclusiveTid = binder::os::GetThreadId(); mConnections.mOutgoing.push_back(connection); } @@ -823,7 +823,7 @@ sp<RpcSession::RpcConnection> RpcSession::assignIncomingConnectionToThisThread( sp<RpcConnection> session = sp<RpcConnection>::make(); session->rpcTransport = std::move(rpcTransport); - session->exclusiveTid = rpcGetThreadId(); + session->exclusiveTid = binder::os::GetThreadId(); mConnections.mIncoming.push_back(session); mConnections.mMaxIncoming = mConnections.mIncoming.size(); @@ -868,7 +868,7 @@ status_t RpcSession::ExclusiveConnection::find(const sp<RpcSession>& session, Co connection->mConnection = nullptr; connection->mReentrant = false; - uint64_t tid = rpcGetThreadId(); + uint64_t tid = binder::os::GetThreadId(); RpcMutexUniqueLock _l(session->mMutex); session->mConnections.mWaitingThreads++; diff --git a/libs/binder/RpcTransportRaw.cpp b/libs/binder/RpcTransportRaw.cpp index ddbcb573b1..c089811561 100644 --- a/libs/binder/RpcTransportRaw.cpp +++ b/libs/binder/RpcTransportRaw.cpp @@ -59,8 +59,8 @@ public: override { bool sentFds = false; auto send = [&](iovec* iovs, int niovs) -> ssize_t { - ssize_t ret = - sendMessageOnSocket(mSocket, iovs, niovs, sentFds ? nullptr : ancillaryFds); + ssize_t ret = binder::os::sendMessageOnSocket(mSocket, iovs, niovs, + sentFds ? nullptr : ancillaryFds); sentFds |= ret > 0; return ret; }; @@ -73,7 +73,7 @@ public: const std::optional<android::base::function_ref<status_t()>>& altPoll, std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds) override { auto recv = [&](iovec* iovs, int niovs) -> ssize_t { - return receiveMessageFromSocket(mSocket, iovs, niovs, ancillaryFds); + return binder::os::receiveMessageFromSocket(mSocket, iovs, niovs, ancillaryFds); }; return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, recv, "recvmsg", POLLIN, altPoll); diff --git a/libs/binder/include/binder/RpcThreads.h b/libs/binder/include/binder/RpcThreads.h index b80d116e1c..d25f29277c 100644 --- a/libs/binder/include/binder/RpcThreads.h +++ b/libs/binder/include/binder/RpcThreads.h @@ -17,8 +17,6 @@ #include <pthread.h> -#include <android-base/threads.h> - #include <condition_variable> #include <functional> #include <memory> @@ -121,10 +119,6 @@ static inline RpcMaybeThread::id get_id() { } } // namespace rpc_this_thread -static inline uint64_t rpcGetThreadId() { - return 0; -} - static inline void rpcJoinIfSingleThreaded(RpcMaybeThread& t) { t.join(); } @@ -136,10 +130,6 @@ using RpcConditionVariable = std::condition_variable; using RpcMaybeThread = std::thread; namespace rpc_this_thread = std::this_thread; -static inline uint64_t rpcGetThreadId() { - return base::GetThreadId(); -} - static inline void rpcJoinIfSingleThreaded(RpcMaybeThread&) {} #endif // BINDER_RPC_SINGLE_THREADED diff --git a/libs/binder/tests/binderRpcTest.cpp b/libs/binder/tests/binderRpcTest.cpp index 4c3c68e2e7..5884dbe66f 100644 --- a/libs/binder/tests/binderRpcTest.cpp +++ b/libs/binder/tests/binderRpcTest.cpp @@ -227,7 +227,7 @@ static base::unique_fd connectToUnixBootstrap(const RpcTransportFd& transportFd) std::vector<std::variant<base::unique_fd, base::borrowed_fd>> fds; fds.emplace_back(std::move(sockServer)); - if (sendMessageOnSocket(transportFd, &iov, 1, &fds) < 0) { + if (binder::os::sendMessageOnSocket(transportFd, &iov, 1, &fds) < 0) { int savedErrno = errno; LOG(FATAL) << "Failed sendMessageOnSocket: " << strerror(savedErrno); } @@ -1589,7 +1589,7 @@ public: int buf; iovec iov{&buf, sizeof(buf)}; - if (receiveMessageFromSocket(mFd, &iov, 1, &fds) < 0) { + if (binder::os::receiveMessageFromSocket(mFd, &iov, 1, &fds) < 0) { int savedErrno = errno; LOG(FATAL) << "Failed receiveMessage: " << strerror(savedErrno); } diff --git a/libs/binder/trusty/OS.cpp b/libs/binder/trusty/OS.cpp index 8ec982345d..43e06e013d 100644 --- a/libs/binder/trusty/OS.cpp +++ b/libs/binder/trusty/OS.cpp @@ -28,7 +28,7 @@ using android::base::Result; -namespace android { +namespace android::binder::os { Result<void> setNonBlocking(android::base::borrowed_fd /*fd*/) { // Trusty IPC syscalls are all non-blocking by default. @@ -73,4 +73,4 @@ ssize_t receiveMessageFromSocket( return -1; } -} // namespace android +} // namespace android::binder::os diff --git a/libs/binder/trusty/rules.mk b/libs/binder/trusty/rules.mk index 9df9a5501f..2e56cbd21b 100644 --- a/libs/binder/trusty/rules.mk +++ b/libs/binder/trusty/rules.mk @@ -35,6 +35,7 @@ MODULE_SRCS := \ $(LIBBINDER_DIR)/FdTrigger.cpp \ $(LIBBINDER_DIR)/IInterface.cpp \ $(LIBBINDER_DIR)/IResultReceiver.cpp \ + $(LIBBINDER_DIR)/OS_android.cpp \ $(LIBBINDER_DIR)/Parcel.cpp \ $(LIBBINDER_DIR)/ParcelFileDescriptor.cpp \ $(LIBBINDER_DIR)/RpcServer.cpp \ |