diff options
| author | 2023-11-04 05:32:52 +0000 | |
|---|---|---|
| committer | 2023-11-04 05:32:52 +0000 | |
| commit | 91174e23170452d88aad185edf48f79d3b9fb03a (patch) | |
| tree | e84100c50218caa7652834b3657f2f6178d17d5d | |
| parent | 540b30ab88b5561aa5cff0a056da3a665d84e192 (diff) | |
| parent | 6c7467787a3f2d5d2228837dada5be137633667f (diff) | |
Merge "Don't depend on libbase result.h" into main am: 6c7467787a
Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/2814533
Change-Id: I3b23359b152d53e19b0f752b64f335aae1d89240
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
| -rw-r--r-- | libs/binder/FdTrigger.h | 1 | ||||
| -rw-r--r-- | libs/binder/OS.h | 3 | ||||
| -rw-r--r-- | libs/binder/OS_unix_base.cpp | 14 | ||||
| -rw-r--r-- | libs/binder/ProcessState.cpp | 45 | ||||
| -rw-r--r-- | libs/binder/RpcServer.cpp | 5 | ||||
| -rw-r--r-- | libs/binder/RpcSession.cpp | 5 | ||||
| -rw-r--r-- | libs/binder/RpcTransportTipcAndroid.cpp | 3 | ||||
| -rw-r--r-- | libs/binder/RpcTransportTls.cpp | 2 | ||||
| -rw-r--r-- | libs/binder/ServiceManagerHost.cpp | 13 | ||||
| -rw-r--r-- | libs/binder/Utils.h | 3 | ||||
| -rw-r--r-- | libs/binder/UtilsHost.cpp | 45 | ||||
| -rw-r--r-- | libs/binder/UtilsHost.h | 7 | ||||
| -rw-r--r-- | libs/binder/include/binder/RpcSession.h | 1 | ||||
| -rw-r--r-- | libs/binder/tests/binderHostDeviceTest.cpp | 6 | ||||
| -rw-r--r-- | libs/binder/tests/binderLibTest.cpp | 1 | ||||
| -rw-r--r-- | libs/binder/tests/binderUtilsHostTest.cpp | 8 | ||||
| -rw-r--r-- | libs/binder/trusty/OS.cpp | 6 |
17 files changed, 87 insertions, 81 deletions
diff --git a/libs/binder/FdTrigger.h b/libs/binder/FdTrigger.h index 5fbf2908ad..dba1dc9f6d 100644 --- a/libs/binder/FdTrigger.h +++ b/libs/binder/FdTrigger.h @@ -17,7 +17,6 @@ #include <memory> -#include <android-base/result.h> #include <android-base/unique_fd.h> #include <utils/Errors.h> diff --git a/libs/binder/OS.h b/libs/binder/OS.h index 8dc1f6ae70..bb7caa951b 100644 --- a/libs/binder/OS.h +++ b/libs/binder/OS.h @@ -18,14 +18,13 @@ #include <stddef.h> #include <cstdint> -#include <android-base/result.h> #include <android-base/unique_fd.h> #include <binder/RpcTransport.h> #include <utils/Errors.h> namespace android::binder::os { -android::base::Result<void> setNonBlocking(android::base::borrowed_fd fd); +status_t setNonBlocking(android::base::borrowed_fd fd); status_t getRandomBytes(uint8_t* data, size_t size); diff --git a/libs/binder/OS_unix_base.cpp b/libs/binder/OS_unix_base.cpp index 81933d5c6b..a3cf117326 100644 --- a/libs/binder/OS_unix_base.cpp +++ b/libs/binder/OS_unix_base.cpp @@ -15,29 +15,29 @@ */ #include "OS.h" +#include "Utils.h" #include <android-base/file.h> #include <binder/RpcTransportRaw.h> #include <log/log.h> #include <string.h> -using android::base::ErrnoError; -using android::base::Result; - namespace android::binder::os { // Linux kernel supports up to 253 (from SCM_MAX_FD) for unix sockets. constexpr size_t kMaxFdsPerMsg = 253; -Result<void> setNonBlocking(android::base::borrowed_fd fd) { +status_t setNonBlocking(android::base::borrowed_fd fd) { int flags = TEMP_FAILURE_RETRY(fcntl(fd.get(), F_GETFL)); if (flags == -1) { - return ErrnoError() << "Could not get flags for fd"; + PLOGE("Failed setNonBlocking: Could not get flags for fd"); + return -errno; } if (int ret = TEMP_FAILURE_RETRY(fcntl(fd.get(), F_SETFL, flags | O_NONBLOCK)); ret == -1) { - return ErrnoError() << "Could not set non-blocking flag for fd"; + PLOGE("Failed setNonBlocking: Could not set non-blocking flag for fd"); + return -errno; } - return {}; + return OK; } status_t getRandomBytes(uint8_t* data, size_t size) { diff --git a/libs/binder/ProcessState.cpp b/libs/binder/ProcessState.cpp index 58203c1373..7af2845f5d 100644 --- a/libs/binder/ProcessState.cpp +++ b/libs/binder/ProcessState.cpp @@ -18,7 +18,6 @@ #include <binder/ProcessState.h> -#include <android-base/result.h> #include <android-base/scopeguard.h> #include <android-base/strings.h> #include <binder/BpBinder.h> @@ -32,6 +31,7 @@ #include <utils/Thread.h> #include "Static.h" +#include "Utils.h" #include "binder_module.h" #include <errno.h> @@ -512,31 +512,31 @@ String8 ProcessState::getDriverName() { return mDriverName; } -static base::Result<int> open_driver(const char* driver) { - int fd = open(driver, O_RDWR | O_CLOEXEC); - if (fd < 0) { - return base::ErrnoError() << "Opening '" << driver << "' failed"; +static base::unique_fd open_driver(const char* driver) { + auto fd = base::unique_fd(open(driver, O_RDWR | O_CLOEXEC)); + if (!fd.ok()) { + PLOGE("Opening '%s' failed", driver); + return {}; } int vers = 0; - status_t result = ioctl(fd, BINDER_VERSION, &vers); + int result = ioctl(fd.get(), BINDER_VERSION, &vers); if (result == -1) { - close(fd); - return base::ErrnoError() << "Binder ioctl to obtain version failed"; + PLOGE("Binder ioctl to obtain version failed"); + return {}; } if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) { - close(fd); - return base::Error() << "Binder driver protocol(" << vers - << ") does not match user space protocol(" - << BINDER_CURRENT_PROTOCOL_VERSION - << ")! ioctl() return value: " << result; + ALOGE("Binder driver protocol(%d) does not match user space protocol(%d)! " + "ioctl() return value: %d", + vers, BINDER_CURRENT_PROTOCOL_VERSION, result); + return {}; } size_t maxThreads = DEFAULT_MAX_BINDER_THREADS; - result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads); + result = ioctl(fd.get(), BINDER_SET_MAX_THREADS, &maxThreads); if (result == -1) { ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno)); } uint32_t enable = DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION; - result = ioctl(fd, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable); + result = ioctl(fd.get(), BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable); if (result == -1) { ALOGE_IF(ProcessState::isDriverFeatureEnabled( ProcessState::DriverFeature::ONEWAY_SPAM_DETECTION), @@ -561,28 +561,27 @@ ProcessState::ProcessState(const char* driver) mThreadPoolStarted(false), mThreadPoolSeq(1), mCallRestriction(CallRestriction::NONE) { - base::Result<int> opened = open_driver(driver); + base::unique_fd opened = open_driver(driver); if (opened.ok()) { // mmap the binder, providing a chunk of virtual address space to receive transactions. mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, - opened.value(), 0); + opened.get(), 0); if (mVMStart == MAP_FAILED) { - close(opened.value()); // *sigh* - opened = base::Error() - << "Using " << driver << " failed: unable to mmap transaction memory."; + ALOGE("Using %s failed: unable to mmap transaction memory.", driver); + opened.reset(); mDriverName.clear(); } } #ifdef __ANDROID__ - LOG_ALWAYS_FATAL_IF(!opened.ok(), "Binder driver '%s' could not be opened. Terminating: %s", - driver, opened.error().message().c_str()); + LOG_ALWAYS_FATAL_IF(!opened.ok(), "Binder driver '%s' could not be opened. Terminating.", + driver); #endif if (opened.ok()) { - mDriverFD = opened.value(); + mDriverFD = opened.release(); } } diff --git a/libs/binder/RpcServer.cpp b/libs/binder/RpcServer.cpp index 1011571408..8037f0863c 100644 --- a/libs/binder/RpcServer.cpp +++ b/libs/binder/RpcServer.cpp @@ -230,10 +230,7 @@ status_t RpcServer::recvmsgSocketConnection(const RpcServer& server, RpcTranspor } unique_fd fd(std::move(std::get<unique_fd>(fds.back()))); - 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(); - } + if (status_t res = binder::os::setNonBlocking(fd); res != OK) return res; *out = RpcTransportFd(std::move(fd)); return OK; diff --git a/libs/binder/RpcSession.cpp b/libs/binder/RpcSession.cpp index 7e181941a9..70382c0722 100644 --- a/libs/binder/RpcSession.cpp +++ b/libs/binder/RpcSession.cpp @@ -192,10 +192,7 @@ status_t RpcSession::setupPreconnectedClient(base::unique_fd fd, fd = request(); if (!fd.ok()) return BAD_VALUE; } - 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(); - } + if (status_t res = binder::os::setNonBlocking(fd); res != OK) return res; RpcTransportFd transportFd(std::move(fd)); status_t status = initAndAddConnection(std::move(transportFd), sessionId, incoming); diff --git a/libs/binder/RpcTransportTipcAndroid.cpp b/libs/binder/RpcTransportTipcAndroid.cpp index 0c81d83032..3d26a3d119 100644 --- a/libs/binder/RpcTransportTipcAndroid.cpp +++ b/libs/binder/RpcTransportTipcAndroid.cpp @@ -26,9 +26,6 @@ #include "RpcState.h" #include "RpcTransportUtils.h" -using android::base::Error; -using android::base::Result; - namespace android { // RpcTransport for writing Trusty IPC clients in Android. diff --git a/libs/binder/RpcTransportTls.cpp b/libs/binder/RpcTransportTls.cpp index efb09e9004..11e080f71a 100644 --- a/libs/binder/RpcTransportTls.cpp +++ b/libs/binder/RpcTransportTls.cpp @@ -29,6 +29,8 @@ #include "RpcState.h" #include "Utils.h" +#include <sstream> + #define SHOULD_LOG_TLS_DETAIL false #if SHOULD_LOG_TLS_DETAIL diff --git a/libs/binder/ServiceManagerHost.cpp b/libs/binder/ServiceManagerHost.cpp index 7f43bb64a0..9482e3e118 100644 --- a/libs/binder/ServiceManagerHost.cpp +++ b/libs/binder/ServiceManagerHost.cpp @@ -64,9 +64,8 @@ private: std::optional<AdbForwarder> AdbForwarder::forward(unsigned int devicePort) { auto result = execute({"adb", "forward", "tcp:0", "tcp:" + std::to_string(devicePort)}, nullptr); - if (!result.ok()) { - ALOGE("Unable to run `adb forward tcp:0 tcp:%d`: %s", devicePort, - result.error().message().c_str()); + if (!result.has_value()) { + ALOGE("Unable to run `adb forward tcp:0 tcp:%d`", devicePort); return std::nullopt; } // Must end with exit code 0 (`has_value() && value() == 0`) @@ -95,9 +94,8 @@ AdbForwarder::~AdbForwarder() { if (!mPort.has_value()) return; auto result = execute({"adb", "forward", "--remove", "tcp:" + std::to_string(*mPort)}, nullptr); - if (!result.ok()) { - ALOGE("Unable to run `adb forward --remove tcp:%d`: %s", *mPort, - result.error().message().c_str()); + if (!result.has_value()) { + ALOGE("Unable to run `adb forward --remove tcp:%d`", *mPort); return; } // Must end with exit code 0 (`has_value() && value() == 0`) @@ -131,8 +129,7 @@ sp<IBinder> getDeviceService(std::vector<std::string>&& serviceDispatcherArgs, serviceDispatcherArgs.insert(serviceDispatcherArgs.begin(), prefix.begin(), prefix.end()); auto result = execute(std::move(serviceDispatcherArgs), &CommandResult::stdoutEndsWithNewLine); - if (!result.ok()) { - ALOGE("%s", result.error().message().c_str()); + if (!result.has_value()) { return nullptr; } diff --git a/libs/binder/Utils.h b/libs/binder/Utils.h index 68d639208e..c8431aab43 100644 --- a/libs/binder/Utils.h +++ b/libs/binder/Utils.h @@ -22,6 +22,9 @@ #include <log/log.h> #include <utils/Errors.h> +#define PLOGE_VA_ARGS(...) , ##__VA_ARGS__ +#define PLOGE(fmt, ...) ALOGE(fmt ": %s" PLOGE_VA_ARGS(__VA_ARGS__), strerror(errno)) + /* TEMP_FAILURE_RETRY is not available on macOS and Trusty. */ #ifndef TEMP_FAILURE_RETRY /* Used to retry syscalls that can return EINTR. */ diff --git a/libs/binder/UtilsHost.cpp b/libs/binder/UtilsHost.cpp index 52b8f69d36..3db038f03f 100644 --- a/libs/binder/UtilsHost.cpp +++ b/libs/binder/UtilsHost.cpp @@ -25,6 +25,8 @@ #include <log/log.h> +#include "Utils.h" + namespace android { CommandResult::~CommandResult() { @@ -72,8 +74,8 @@ std::string CommandResult::toString() const { return ss.str(); } -android::base::Result<CommandResult> execute(std::vector<std::string> argStringVec, - const std::function<bool(const CommandResult&)>& end) { +std::optional<CommandResult> execute(std::vector<std::string> argStringVec, + const std::function<bool(const CommandResult&)>& end) { // turn vector<string> into null-terminated char* vector. std::vector<char*> argv; argv.reserve(argStringVec.size() + 1); @@ -82,14 +84,21 @@ android::base::Result<CommandResult> execute(std::vector<std::string> argStringV CommandResult ret; android::base::unique_fd outWrite; - if (!android::base::Pipe(&ret.outPipe, &outWrite)) - return android::base::ErrnoError() << "pipe() for outPipe"; + if (!android::base::Pipe(&ret.outPipe, &outWrite)) { + PLOGE("pipe() for outPipe"); + return {}; + } android::base::unique_fd errWrite; - if (!android::base::Pipe(&ret.errPipe, &errWrite)) - return android::base::ErrnoError() << "pipe() for errPipe"; + if (!android::base::Pipe(&ret.errPipe, &errWrite)) { + PLOGE("pipe() for errPipe"); + return {}; + } int pid = fork(); - if (pid == -1) return android::base::ErrnoError() << "fork()"; + if (pid == -1) { + PLOGE("fork()"); + return {}; + } if (pid == 0) { // child ret.outPipe.reset(); @@ -140,12 +149,19 @@ android::base::Result<CommandResult> execute(std::vector<std::string> argStringV *errPollFd = {.fd = ret.errPipe.get(), .events = POLLIN}; } int pollRet = poll(fds, nfds, 1000 /* ms timeout */); - if (pollRet == -1) return android::base::ErrnoError() << "poll()"; + if (pollRet == -1) { + PLOGE("poll()"); + return {}; + } - if (!handlePoll(&ret.outPipe, outPollFd, &ret.stdoutStr)) - return android::base::ErrnoError() << "read(stdout)"; - if (!handlePoll(&ret.errPipe, errPollFd, &ret.stderrStr)) - return android::base::ErrnoError() << "read(stderr)"; + if (!handlePoll(&ret.outPipe, outPollFd, &ret.stdoutStr)) { + PLOGE("read(stdout)"); + return {}; + } + if (!handlePoll(&ret.errPipe, errPollFd, &ret.stderrStr)) { + PLOGE("read(stderr)"); + return {}; + } if (end && end(ret)) return ret; } @@ -154,7 +170,10 @@ android::base::Result<CommandResult> execute(std::vector<std::string> argStringV while (ret.pid.has_value()) { int status; auto exitPid = waitpid(pid, &status, 0); - if (exitPid == -1) return android::base::ErrnoError() << "waitpid(" << pid << ")"; + if (exitPid == -1) { + PLOGE("waitpid(%d)", pid); + return {}; + } if (exitPid == pid) { if (WIFEXITED(status)) { ret.pid = std::nullopt; diff --git a/libs/binder/UtilsHost.h b/libs/binder/UtilsHost.h index 14a8dcba57..5de0980d8e 100644 --- a/libs/binder/UtilsHost.h +++ b/libs/binder/UtilsHost.h @@ -22,8 +22,9 @@ #include <variant> #include <vector> -#include <android-base/result.h> +#include <android-base/macros.h> #include <android-base/unique_fd.h> +#include <utils/Errors.h> /** * Log a lot more information about host-device binder communication, when debugging issues. @@ -94,6 +95,6 @@ std::ostream& operator<<(std::ostream& os, const CommandResult& res); // // If the parent process has encountered any errors for system calls, return ExecuteError with // the proper errno set. -android::base::Result<CommandResult> execute(std::vector<std::string> argStringVec, - const std::function<bool(const CommandResult&)>& end); +std::optional<CommandResult> execute(std::vector<std::string> argStringVec, + const std::function<bool(const CommandResult&)>& end); } // namespace android diff --git a/libs/binder/include/binder/RpcSession.h b/libs/binder/include/binder/RpcSession.h index cb6460398d..e3805ac888 100644 --- a/libs/binder/include/binder/RpcSession.h +++ b/libs/binder/include/binder/RpcSession.h @@ -15,7 +15,6 @@ */ #pragma once -#include <android-base/threads.h> #include <android-base/unique_fd.h> #include <binder/IBinder.h> #include <binder/RpcThreads.h> diff --git a/libs/binder/tests/binderHostDeviceTest.cpp b/libs/binder/tests/binderHostDeviceTest.cpp index 0075688ed3..0ae536c6bc 100644 --- a/libs/binder/tests/binderHostDeviceTest.cpp +++ b/libs/binder/tests/binderHostDeviceTest.cpp @@ -75,7 +75,7 @@ class HostDeviceTest : public ::testing::Test { public: void SetUp() override { auto debuggableResult = execute(Split("adb shell getprop ro.debuggable", " "), nullptr); - ASSERT_THAT(debuggableResult, Ok()); + ASSERT_TRUE(debuggableResult.has_value()); ASSERT_EQ(0, debuggableResult->exitCode) << *debuggableResult; auto debuggableBool = ParseBool(Trim(debuggableResult->stdoutStr)); ASSERT_NE(ParseBoolResult::kError, debuggableBool) << Trim(debuggableResult->stdoutStr); @@ -84,7 +84,7 @@ public: } auto lsResult = execute(Split("adb shell which servicedispatcher", " "), nullptr); - ASSERT_THAT(lsResult, Ok()); + ASSERT_TRUE(lsResult.has_value()); if (lsResult->exitCode != 0) { GTEST_SKIP() << "b/182914638: until feature is fully enabled, skip test on devices " "without servicedispatcher"; @@ -95,7 +95,7 @@ public: auto service = execute({"adb", "shell", kServiceBinary, kServiceName, kDescriptor}, &CommandResult::stdoutEndsWithNewLine); - ASSERT_THAT(service, Ok()); + ASSERT_TRUE(service.has_value()); ASSERT_EQ(std::nullopt, service->exitCode) << *service; mService = std::move(*service); } diff --git a/libs/binder/tests/binderLibTest.cpp b/libs/binder/tests/binderLibTest.cpp index 659943a718..1ca75d1d72 100644 --- a/libs/binder/tests/binderLibTest.cpp +++ b/libs/binder/tests/binderLibTest.cpp @@ -29,7 +29,6 @@ #include <android-base/properties.h> #include <android-base/result-gmock.h> -#include <android-base/result.h> #include <android-base/scopeguard.h> #include <android-base/strings.h> #include <android-base/unique_fd.h> diff --git a/libs/binder/tests/binderUtilsHostTest.cpp b/libs/binder/tests/binderUtilsHostTest.cpp index 25e286ce0c..6301c74842 100644 --- a/libs/binder/tests/binderUtilsHostTest.cpp +++ b/libs/binder/tests/binderUtilsHostTest.cpp @@ -32,7 +32,7 @@ namespace android { TEST(UtilsHost, ExecuteImmediately) { auto result = execute({"echo", "foo"}, nullptr); - ASSERT_THAT(result, Ok()); + ASSERT_TRUE(result.has_value()); EXPECT_THAT(result->exitCode, Optional(EX_OK)); EXPECT_EQ(result->stdoutStr, "foo\n"); } @@ -58,7 +58,7 @@ TEST(UtilsHost, ExecuteLongRunning) { EXPECT_GE(elapsedMs, 1000); EXPECT_LT(elapsedMs, 2000); - ASSERT_THAT(result, Ok()); + ASSERT_TRUE(result.has_value()); EXPECT_EQ(std::nullopt, result->exitCode); EXPECT_EQ(result->stdoutStr, "foo\n"); } @@ -83,7 +83,7 @@ TEST(UtilsHost, ExecuteLongRunning2) { EXPECT_GE(elapsedMs, 4000); EXPECT_LT(elapsedMs, 6000); - ASSERT_THAT(result, Ok()); + ASSERT_TRUE(result.has_value()); EXPECT_EQ(std::nullopt, result->exitCode); EXPECT_EQ(result->stdoutStr, "foo\n"); } @@ -104,7 +104,7 @@ TEST(UtilsHost, KillWithSigKill) { return false; }); - ASSERT_THAT(result, Ok()); + ASSERT_TRUE(result.has_value()); EXPECT_EQ(std::nullopt, result->exitCode); EXPECT_THAT(result->signal, Optional(SIGKILL)); } diff --git a/libs/binder/trusty/OS.cpp b/libs/binder/trusty/OS.cpp index 43e06e013d..0d18b0b94d 100644 --- a/libs/binder/trusty/OS.cpp +++ b/libs/binder/trusty/OS.cpp @@ -26,13 +26,11 @@ #include "../OS.h" #include "TrustyStatus.h" -using android::base::Result; - namespace android::binder::os { -Result<void> setNonBlocking(android::base::borrowed_fd /*fd*/) { +status_t setNonBlocking(android::base::borrowed_fd /*fd*/) { // Trusty IPC syscalls are all non-blocking by default. - return {}; + return OK; } status_t getRandomBytes(uint8_t* data, size_t size) { |