diff options
author | 2023-11-01 09:46:07 -0700 | |
---|---|---|
committer | 2023-11-03 21:05:51 -0700 | |
commit | 88aa8c3c7a97fda3a3fb200d9c71323844f97617 (patch) | |
tree | ac842d6ed827d67e9d198ca830c9dd6f9f31f5f3 /libs/binder/UtilsHost.cpp | |
parent | 1d1f7ac538473bc368a5358c9fb9f4d8c25ad142 (diff) |
Don't depend on libbase result.h
Test: mma
Bug: 302723053
Change-Id: Iea797d6af825d58543ba899f6e712b27e48d859a
Diffstat (limited to 'libs/binder/UtilsHost.cpp')
-rw-r--r-- | libs/binder/UtilsHost.cpp | 45 |
1 files changed, 32 insertions, 13 deletions
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; |