Loading libs/binder/Android.bp +21 −1 Original line number Diff line number Diff line Loading @@ -58,13 +58,13 @@ cc_library_headers { // transport itself and should be moved to AIDL or in domain-specific libs. // // Currently, these are only on system android (not vendor, not host) // TODO(b/183654927) - move these into separate libraries libbinder_device_interface_sources = [ "ActivityManager.cpp", "AppOpsManager.cpp", "IActivityManager.cpp", "IAppOpsCallback.cpp", "IAppOpsService.cpp", "IBatteryStats.cpp", "IMediaResourceMonitor.cpp", "IPermissionController.cpp", "IProcessInfoService.cpp", Loading Loading @@ -263,3 +263,23 @@ aidl_interface { }, }, } // libbinder historically contained additional interfaces that provided specific // functionality in the platform but have nothing to do with binder itself. These // are moved out of libbinder in order to avoid the overhead of their vtables. // If you are working on or own one of these interfaces, the responsible things // to would be: // - give them a new home // - convert them to AIDL instead of having manually written parceling code cc_library { name: "libbatterystats_aidl", srcs: [ "IBatteryStats.cpp", ], export_include_dirs: ["include_batterystats"], shared_libs: [ "libbinder", "libutils", ], } libs/binder/IBatteryStats.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -14,7 +14,7 @@ * limitations under the License. */ #include <binder/IBatteryStats.h> #include <batterystats/IBatteryStats.h> #include <utils/Log.h> #include <binder/Parcel.h> Loading libs/binder/RpcConnection.cpp +54 −104 Original line number Diff line number Diff line Loading @@ -20,7 +20,6 @@ #include <binder/Parcel.h> #include <binder/Stability.h> #include <utils/String8.h> #include "RpcState.h" #include "RpcWireFormat.h" Loading @@ -30,20 +29,14 @@ #include <sys/un.h> #include <unistd.h> #ifdef __GLIBC__ #if defined(__GLIBC__) extern "C" pid_t gettid(); #endif #ifdef __BIONIC__ #include <linux/vm_sockets.h> #endif namespace android { using base::unique_fd; RpcConnection::SocketAddress::~SocketAddress() {} RpcConnection::RpcConnection() { LOG_RPC_DETAIL("RpcConnection created %p", this); Loading @@ -57,67 +50,64 @@ sp<RpcConnection> RpcConnection::make() { return new RpcConnection; } class UnixSocketAddress : public RpcConnection::SocketAddress { public: explicit UnixSocketAddress(const char* path) : mAddr({.sun_family = AF_UNIX}) { unsigned int pathLen = strlen(path) + 1; LOG_ALWAYS_FATAL_IF(pathLen > sizeof(mAddr.sun_path), "%u %s", pathLen, path); memcpy(mAddr.sun_path, path, pathLen); } virtual ~UnixSocketAddress() {} std::string toString() const override { return String8::format("path '%.*s'", static_cast<int>(sizeof(mAddr.sun_path)), mAddr.sun_path) .c_str(); bool RpcConnection::setupUnixDomainServer(const char* path) { LOG_ALWAYS_FATAL_IF(mServer.get() != -1, "Only supports one server now"); unique_fd serverFd(TEMP_FAILURE_RETRY(socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0))); if (serverFd == -1) { ALOGE("Could not create socket at %s: %s", path, strerror(errno)); return false; } const sockaddr* addr() const override { return reinterpret_cast<const sockaddr*>(&mAddr); } size_t addrSize() const override { return sizeof(mAddr); } private: sockaddr_un mAddr; struct sockaddr_un addr = { .sun_family = AF_UNIX, }; bool RpcConnection::setupUnixDomainServer(const char* path) { return addServer(UnixSocketAddress(path)); unsigned int pathLen = strlen(path) + 1; LOG_ALWAYS_FATAL_IF(pathLen > sizeof(addr.sun_path), "%u", pathLen); memcpy(addr.sun_path, path, pathLen); if (0 != TEMP_FAILURE_RETRY(bind(serverFd.get(), (struct sockaddr*)&addr, sizeof(addr)))) { ALOGE("Could not bind socket at %s: %s", path, strerror(errno)); return false; } bool RpcConnection::addUnixDomainClient(const char* path) { return addClient(UnixSocketAddress(path)); if (0 != TEMP_FAILURE_RETRY(listen(serverFd.get(), 1 /*backlog*/))) { ALOGE("Could not listen socket at %s: %s", path, strerror(errno)); return false; } #ifdef __BIONIC__ mServer = std::move(serverFd); return true; } bool RpcConnection::addUnixDomainClient(const char* path) { LOG_RPC_DETAIL("Connecting on path: %s", path); class VsockSocketAddress : public RpcConnection::SocketAddress { public: VsockSocketAddress(unsigned int cid, unsigned int port) : mAddr({ .svm_family = AF_VSOCK, .svm_port = port, .svm_cid = cid, }) {} virtual ~VsockSocketAddress() {} std::string toString() const override { return String8::format("cid %du port %du", mAddr.svm_cid, mAddr.svm_port).c_str(); unique_fd serverFd(TEMP_FAILURE_RETRY(socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0))); if (serverFd == -1) { ALOGE("Could not create socket at %s: %s", path, strerror(errno)); return false; } const sockaddr* addr() const override { return reinterpret_cast<const sockaddr*>(&mAddr); } size_t addrSize() const override { return sizeof(mAddr); } private: sockaddr_vm mAddr; struct sockaddr_un addr = { .sun_family = AF_UNIX, }; bool RpcConnection::setupVsockServer(unsigned int port) { // realizing value w/ this type at compile time to avoid ubsan abort constexpr unsigned int kAnyCid = VMADDR_CID_ANY; unsigned int pathLen = strlen(path) + 1; LOG_ALWAYS_FATAL_IF(pathLen > sizeof(addr.sun_path), "%u", pathLen); memcpy(addr.sun_path, path, pathLen); return addServer(VsockSocketAddress(kAnyCid, port)); if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), (struct sockaddr*)&addr, sizeof(addr)))) { ALOGE("Could not connect socket at %s: %s", path, strerror(errno)); return false; } bool RpcConnection::addVsockClient(unsigned int cid, unsigned int port) { return addClient(VsockSocketAddress(cid, port)); } LOG_RPC_DETAIL("Unix domain client with fd %d", serverFd.get()); #endif // __BIONIC__ addClient(std::move(serverFd)); return true; } sp<IBinder> RpcConnection::getRootObject() { ExclusiveSocket socket(this, SocketUse::CLIENT); Loading @@ -140,8 +130,11 @@ status_t RpcConnection::sendDecStrong(const RpcAddress& address) { void RpcConnection::join() { // establish a connection { unique_fd clientFd( TEMP_FAILURE_RETRY(accept4(mServer.get(), nullptr, 0 /*length*/, SOCK_CLOEXEC))); struct sockaddr_un clientSa; socklen_t clientSaLen = sizeof(clientSa); unique_fd clientFd(TEMP_FAILURE_RETRY( accept4(mServer.get(), (struct sockaddr*)&clientSa, &clientSaLen, SOCK_CLOEXEC))); if (clientFd < 0) { // If this log becomes confusing, should save more state from setupUnixDomainServer // in order to output here. Loading @@ -151,7 +144,7 @@ void RpcConnection::join() { LOG_RPC_DETAIL("accept4 on fd %d yields fd %d", mServer.get(), clientFd.get()); assignServerToThisThread(std::move(clientFd)); addServer(std::move(clientFd)); } // We may not use the connection we just established (two threads might Loading @@ -177,57 +170,14 @@ wp<RpcServer> RpcConnection::server() { return mForServer; } bool RpcConnection::addServer(const SocketAddress& addr) { LOG_ALWAYS_FATAL_IF(mServer.get() != -1, "Each RpcConnection can only have one server."); unique_fd serverFd( TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); if (serverFd == -1) { ALOGE("Could not create socket: %s", strerror(errno)); return false; } if (0 != TEMP_FAILURE_RETRY(bind(serverFd.get(), addr.addr(), addr.addrSize()))) { int savedErrno = errno; ALOGE("Could not bind socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); return false; } if (0 != TEMP_FAILURE_RETRY(listen(serverFd.get(), 1 /*backlog*/))) { int savedErrno = errno; ALOGE("Could not listen socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); return false; } mServer = std::move(serverFd); return true; } bool RpcConnection::addClient(const SocketAddress& addr) { unique_fd serverFd( TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); if (serverFd == -1) { int savedErrno = errno; ALOGE("Could not create socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); return false; } if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) { int savedErrno = errno; ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); return false; } LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get()); void RpcConnection::addClient(base::unique_fd&& fd) { std::lock_guard<std::mutex> _l(mSocketMutex); sp<ConnectionSocket> connection = new ConnectionSocket(); connection->fd = std::move(serverFd); connection->fd = std::move(fd); mClients.push_back(connection); return true; } void RpcConnection::assignServerToThisThread(base::unique_fd&& fd) { void RpcConnection::addServer(base::unique_fd&& fd) { std::lock_guard<std::mutex> _l(mSocketMutex); sp<ConnectionSocket> connection = new ConnectionSocket(); connection->fd = std::move(fd); Loading libs/binder/include/binder/RpcConnection.h +2 −23 Original line number Diff line number Diff line Loading @@ -61,18 +61,6 @@ public: */ [[nodiscard]] bool addUnixDomainClient(const char* path); #ifdef __BIONIC__ /** * Creates an RPC server at the current port. */ [[nodiscard]] bool setupVsockServer(unsigned int port); /** * Connects to an RPC server at the CVD & port. */ [[nodiscard]] bool addVsockClient(unsigned int cvd, unsigned int port); #endif // __BIONIC__ /** * Query the other side of the connection for the root object hosted by that * process's RpcServer (if one exists) Loading @@ -97,20 +85,11 @@ public: // internal only const std::unique_ptr<RpcState>& state() { return mState; } class SocketAddress { public: virtual ~SocketAddress(); virtual std::string toString() const = 0; virtual const sockaddr* addr() const = 0; virtual size_t addrSize() const = 0; }; private: RpcConnection(); bool addServer(const SocketAddress& address); bool addClient(const SocketAddress& address); void assignServerToThisThread(base::unique_fd&& fd); void addServer(base::unique_fd&& fd); void addClient(base::unique_fd&& fd); struct ConnectionSocket : public RefBase { base::unique_fd fd; Loading libs/binder/include/binder/IBatteryStats.h→libs/binder/include_batterystats/batterystats/IBatteryStats.h +0 −0 File moved. View file Loading
libs/binder/Android.bp +21 −1 Original line number Diff line number Diff line Loading @@ -58,13 +58,13 @@ cc_library_headers { // transport itself and should be moved to AIDL or in domain-specific libs. // // Currently, these are only on system android (not vendor, not host) // TODO(b/183654927) - move these into separate libraries libbinder_device_interface_sources = [ "ActivityManager.cpp", "AppOpsManager.cpp", "IActivityManager.cpp", "IAppOpsCallback.cpp", "IAppOpsService.cpp", "IBatteryStats.cpp", "IMediaResourceMonitor.cpp", "IPermissionController.cpp", "IProcessInfoService.cpp", Loading Loading @@ -263,3 +263,23 @@ aidl_interface { }, }, } // libbinder historically contained additional interfaces that provided specific // functionality in the platform but have nothing to do with binder itself. These // are moved out of libbinder in order to avoid the overhead of their vtables. // If you are working on or own one of these interfaces, the responsible things // to would be: // - give them a new home // - convert them to AIDL instead of having manually written parceling code cc_library { name: "libbatterystats_aidl", srcs: [ "IBatteryStats.cpp", ], export_include_dirs: ["include_batterystats"], shared_libs: [ "libbinder", "libutils", ], }
libs/binder/IBatteryStats.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -14,7 +14,7 @@ * limitations under the License. */ #include <binder/IBatteryStats.h> #include <batterystats/IBatteryStats.h> #include <utils/Log.h> #include <binder/Parcel.h> Loading
libs/binder/RpcConnection.cpp +54 −104 Original line number Diff line number Diff line Loading @@ -20,7 +20,6 @@ #include <binder/Parcel.h> #include <binder/Stability.h> #include <utils/String8.h> #include "RpcState.h" #include "RpcWireFormat.h" Loading @@ -30,20 +29,14 @@ #include <sys/un.h> #include <unistd.h> #ifdef __GLIBC__ #if defined(__GLIBC__) extern "C" pid_t gettid(); #endif #ifdef __BIONIC__ #include <linux/vm_sockets.h> #endif namespace android { using base::unique_fd; RpcConnection::SocketAddress::~SocketAddress() {} RpcConnection::RpcConnection() { LOG_RPC_DETAIL("RpcConnection created %p", this); Loading @@ -57,67 +50,64 @@ sp<RpcConnection> RpcConnection::make() { return new RpcConnection; } class UnixSocketAddress : public RpcConnection::SocketAddress { public: explicit UnixSocketAddress(const char* path) : mAddr({.sun_family = AF_UNIX}) { unsigned int pathLen = strlen(path) + 1; LOG_ALWAYS_FATAL_IF(pathLen > sizeof(mAddr.sun_path), "%u %s", pathLen, path); memcpy(mAddr.sun_path, path, pathLen); } virtual ~UnixSocketAddress() {} std::string toString() const override { return String8::format("path '%.*s'", static_cast<int>(sizeof(mAddr.sun_path)), mAddr.sun_path) .c_str(); bool RpcConnection::setupUnixDomainServer(const char* path) { LOG_ALWAYS_FATAL_IF(mServer.get() != -1, "Only supports one server now"); unique_fd serverFd(TEMP_FAILURE_RETRY(socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0))); if (serverFd == -1) { ALOGE("Could not create socket at %s: %s", path, strerror(errno)); return false; } const sockaddr* addr() const override { return reinterpret_cast<const sockaddr*>(&mAddr); } size_t addrSize() const override { return sizeof(mAddr); } private: sockaddr_un mAddr; struct sockaddr_un addr = { .sun_family = AF_UNIX, }; bool RpcConnection::setupUnixDomainServer(const char* path) { return addServer(UnixSocketAddress(path)); unsigned int pathLen = strlen(path) + 1; LOG_ALWAYS_FATAL_IF(pathLen > sizeof(addr.sun_path), "%u", pathLen); memcpy(addr.sun_path, path, pathLen); if (0 != TEMP_FAILURE_RETRY(bind(serverFd.get(), (struct sockaddr*)&addr, sizeof(addr)))) { ALOGE("Could not bind socket at %s: %s", path, strerror(errno)); return false; } bool RpcConnection::addUnixDomainClient(const char* path) { return addClient(UnixSocketAddress(path)); if (0 != TEMP_FAILURE_RETRY(listen(serverFd.get(), 1 /*backlog*/))) { ALOGE("Could not listen socket at %s: %s", path, strerror(errno)); return false; } #ifdef __BIONIC__ mServer = std::move(serverFd); return true; } bool RpcConnection::addUnixDomainClient(const char* path) { LOG_RPC_DETAIL("Connecting on path: %s", path); class VsockSocketAddress : public RpcConnection::SocketAddress { public: VsockSocketAddress(unsigned int cid, unsigned int port) : mAddr({ .svm_family = AF_VSOCK, .svm_port = port, .svm_cid = cid, }) {} virtual ~VsockSocketAddress() {} std::string toString() const override { return String8::format("cid %du port %du", mAddr.svm_cid, mAddr.svm_port).c_str(); unique_fd serverFd(TEMP_FAILURE_RETRY(socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0))); if (serverFd == -1) { ALOGE("Could not create socket at %s: %s", path, strerror(errno)); return false; } const sockaddr* addr() const override { return reinterpret_cast<const sockaddr*>(&mAddr); } size_t addrSize() const override { return sizeof(mAddr); } private: sockaddr_vm mAddr; struct sockaddr_un addr = { .sun_family = AF_UNIX, }; bool RpcConnection::setupVsockServer(unsigned int port) { // realizing value w/ this type at compile time to avoid ubsan abort constexpr unsigned int kAnyCid = VMADDR_CID_ANY; unsigned int pathLen = strlen(path) + 1; LOG_ALWAYS_FATAL_IF(pathLen > sizeof(addr.sun_path), "%u", pathLen); memcpy(addr.sun_path, path, pathLen); return addServer(VsockSocketAddress(kAnyCid, port)); if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), (struct sockaddr*)&addr, sizeof(addr)))) { ALOGE("Could not connect socket at %s: %s", path, strerror(errno)); return false; } bool RpcConnection::addVsockClient(unsigned int cid, unsigned int port) { return addClient(VsockSocketAddress(cid, port)); } LOG_RPC_DETAIL("Unix domain client with fd %d", serverFd.get()); #endif // __BIONIC__ addClient(std::move(serverFd)); return true; } sp<IBinder> RpcConnection::getRootObject() { ExclusiveSocket socket(this, SocketUse::CLIENT); Loading @@ -140,8 +130,11 @@ status_t RpcConnection::sendDecStrong(const RpcAddress& address) { void RpcConnection::join() { // establish a connection { unique_fd clientFd( TEMP_FAILURE_RETRY(accept4(mServer.get(), nullptr, 0 /*length*/, SOCK_CLOEXEC))); struct sockaddr_un clientSa; socklen_t clientSaLen = sizeof(clientSa); unique_fd clientFd(TEMP_FAILURE_RETRY( accept4(mServer.get(), (struct sockaddr*)&clientSa, &clientSaLen, SOCK_CLOEXEC))); if (clientFd < 0) { // If this log becomes confusing, should save more state from setupUnixDomainServer // in order to output here. Loading @@ -151,7 +144,7 @@ void RpcConnection::join() { LOG_RPC_DETAIL("accept4 on fd %d yields fd %d", mServer.get(), clientFd.get()); assignServerToThisThread(std::move(clientFd)); addServer(std::move(clientFd)); } // We may not use the connection we just established (two threads might Loading @@ -177,57 +170,14 @@ wp<RpcServer> RpcConnection::server() { return mForServer; } bool RpcConnection::addServer(const SocketAddress& addr) { LOG_ALWAYS_FATAL_IF(mServer.get() != -1, "Each RpcConnection can only have one server."); unique_fd serverFd( TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); if (serverFd == -1) { ALOGE("Could not create socket: %s", strerror(errno)); return false; } if (0 != TEMP_FAILURE_RETRY(bind(serverFd.get(), addr.addr(), addr.addrSize()))) { int savedErrno = errno; ALOGE("Could not bind socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); return false; } if (0 != TEMP_FAILURE_RETRY(listen(serverFd.get(), 1 /*backlog*/))) { int savedErrno = errno; ALOGE("Could not listen socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); return false; } mServer = std::move(serverFd); return true; } bool RpcConnection::addClient(const SocketAddress& addr) { unique_fd serverFd( TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); if (serverFd == -1) { int savedErrno = errno; ALOGE("Could not create socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); return false; } if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) { int savedErrno = errno; ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); return false; } LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get()); void RpcConnection::addClient(base::unique_fd&& fd) { std::lock_guard<std::mutex> _l(mSocketMutex); sp<ConnectionSocket> connection = new ConnectionSocket(); connection->fd = std::move(serverFd); connection->fd = std::move(fd); mClients.push_back(connection); return true; } void RpcConnection::assignServerToThisThread(base::unique_fd&& fd) { void RpcConnection::addServer(base::unique_fd&& fd) { std::lock_guard<std::mutex> _l(mSocketMutex); sp<ConnectionSocket> connection = new ConnectionSocket(); connection->fd = std::move(fd); Loading
libs/binder/include/binder/RpcConnection.h +2 −23 Original line number Diff line number Diff line Loading @@ -61,18 +61,6 @@ public: */ [[nodiscard]] bool addUnixDomainClient(const char* path); #ifdef __BIONIC__ /** * Creates an RPC server at the current port. */ [[nodiscard]] bool setupVsockServer(unsigned int port); /** * Connects to an RPC server at the CVD & port. */ [[nodiscard]] bool addVsockClient(unsigned int cvd, unsigned int port); #endif // __BIONIC__ /** * Query the other side of the connection for the root object hosted by that * process's RpcServer (if one exists) Loading @@ -97,20 +85,11 @@ public: // internal only const std::unique_ptr<RpcState>& state() { return mState; } class SocketAddress { public: virtual ~SocketAddress(); virtual std::string toString() const = 0; virtual const sockaddr* addr() const = 0; virtual size_t addrSize() const = 0; }; private: RpcConnection(); bool addServer(const SocketAddress& address); bool addClient(const SocketAddress& address); void assignServerToThisThread(base::unique_fd&& fd); void addServer(base::unique_fd&& fd); void addClient(base::unique_fd&& fd); struct ConnectionSocket : public RefBase { base::unique_fd fd; Loading
libs/binder/include/binder/IBatteryStats.h→libs/binder/include_batterystats/batterystats/IBatteryStats.h +0 −0 File moved. View file