abb - исходный текст
#include <sys/wait.h>
#include <android-base/cmsg.h>
#include <cmd.h>
#include "adb.h"
#include "adb_io.h"
#include "adb_utils.h"
#include "shell_service.h"
namespace {
class AdbFdTextOutput : public android::TextOutput {
public:
explicit AdbFdTextOutput(int fd) : mFD(fd) {}
private:
android::status_t print(const char* txt, size_t len) override {
return WriteFdExactly(mFD, txt, len) ? android::OK : -errno;
}
void moveIndent(int delta) override { /*not implemented*/
}
void pushBundle() override { /*not implemented*/
}
void popBundle() override { /*not implemented*/
}
private:
int mFD;
};
std::vector<std::string_view> parseCmdArgs(std::string_view args) {
std::vector<std::string_view> argv;
char delim = ABB_ARG_DELIMETER;
size_t size = args.size();
size_t base = 0;
while (base < size) {
size_t found;
for (found = base; found < size && args[found] && args[found] != delim; ++found)
;
if (found > base) {
argv.emplace_back(args.substr(base, found - base));
}
base = found + 1;
}
return argv;
}
} // namespace
static int execCmd(std::string_view args, int in, int out, int err) {
AdbFdTextOutput oin(out);
AdbFdTextOutput oerr(err);
return cmdMain(parseCmdArgs(args), oin, oerr, in, out, err, RunMode::kLibrary);
}
int main(int argc, char* const argv[]) {
signal(SIGPIPE, SIG_IGN);
int fd = STDIN_FILENO;
std::string data;
while (true) {
std::string error;
if (!ReadProtocolString(fd, &data, &error)) {
PLOG(ERROR) << "Failed to read message: " << error;
break;
}
std::string_view name = data;
auto protocol = SubprocessProtocol::kShell;
if (ConsumePrefix(&name, "abb:")) {
protocol = SubprocessProtocol::kShell;
} else if (ConsumePrefix(&name, "abb_exec:")) {
protocol = SubprocessProtocol::kNone;
} else {
LOG(FATAL) << "Unknown command prefix for abb: " << data;
}
unique_fd result = StartCommandInProcess(std::string(name), &execCmd, protocol);
if (android::base::SendFileDescriptors(fd, "", 1, result.get()) != 1) {
PLOG(ERROR) << "Failed to send an inprocess fd for command: " << data;
break;
}
}
}
abb_service.cpp
#include "adb.h"
#include "adb_io.h"
#include "adb_unique_fd.h"
#include "adb_utils.h"
#include "shell_service.h"
#include <android-base/cmsg.h>
namespace {
struct AbbProcess;
static auto& abbp = *new std::unique_ptr<AbbProcess>(std::make_unique<AbbProcess>());
struct AbbProcess {
unique_fd sendCommand(std::string_view command);
private:
static unique_fd startAbbProcess(unique_fd* error_fd);
static constexpr auto kRetries = 2;
static constexpr auto kErrorProtocol = SubprocessProtocol::kShell;
std::mutex locker_;
unique_fd socket_fd_;
};
unique_fd AbbProcess::sendCommand(std::string_view command) {
std::unique_lock lock{locker_};
for (int i = 0; i < kRetries; ++i) {
unique_fd error_fd;
if (socket_fd_ == -1) {
socket_fd_ = startAbbProcess(&error_fd);
}
if (socket_fd_ == -1) {
LOG(ERROR) << "failed to start abb process";
return error_fd;
}
if (!SendProtocolString(socket_fd_, std::string(command))) {
PLOG(ERROR) << "failed to send command to abb";
socket_fd_.reset();
continue;
}
unique_fd fd;
std::string error;
char buf;
if (android::base::ReceiveFileDescriptors(socket_fd_, &buf, 1, &fd) != 1) {
PLOG(ERROR) << "failed to receive FD from abb";
socket_fd_.reset();
continue;
}
return fd;
}
LOG(ERROR) << "abb is unavailable";
socket_fd_.reset();
return ReportError(kErrorProtocol, "abb is unavailable");
}
unique_fd AbbProcess::startAbbProcess(unique_fd* error_fd) {
constexpr auto abb_process_type = SubprocessType::kRaw;
constexpr auto abb_protocol = SubprocessProtocol::kNone;
constexpr auto make_pty_raw = false;
return StartSubprocess("abb", "dumb", abb_process_type, abb_protocol, make_pty_raw,
kErrorProtocol, error_fd);
}
} // namespace
unique_fd execute_abb_command(std::string_view command) {
return abbp->sendCommand(command);
}