Merge "adbd: cleanups in remount/verity."

am: 98efc485de

Change-Id: Ideadb9d6633d5529710932e849bdf3a4bc9a9999
This commit is contained in:
Josh Gao 2019-03-06 18:37:26 -08:00 committed by android-build-merger
commit d659bb49be
2 changed files with 31 additions and 38 deletions

View file

@ -27,68 +27,62 @@
#include "adb_io.h" #include "adb_io.h"
#include "adb_unique_fd.h" #include "adb_unique_fd.h"
void remount_service(unique_fd fd, const std::string& cmd) { static constexpr char kRemountCmd[] = "/system/bin/remount";
static constexpr char remount_cmd[] = "/system/bin/remount";
static constexpr char remount_failed[] = "remount failed\n";
static bool do_remount(int fd, const std::string& cmd) {
if (getuid() != 0) { if (getuid() != 0) {
WriteFdExactly(fd.get(), "Not running as root. Try \"adb root\" first.\n"); WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n");
WriteFdExactly(fd.get(), remount_failed); return false;
return;
} }
auto pid = vfork(); auto pid = fork();
if (pid < 0) { if (pid < 0) {
WriteFdFmt(fd.get(), "Failed to fork to %s: %s\n", remount_cmd, strerror(errno)); WriteFdFmt(fd, "Failed to fork to %s: %s\n", kRemountCmd, strerror(errno));
WriteFdExactly(fd.get(), remount_failed); return false;
return;
} }
if (pid == 0) { if (pid == 0) {
// child side of the fork // child side of the fork
fcntl(fd.get(), F_SETFD, 0); dup2(fd, STDIN_FILENO);
dup2(fd.get(), STDIN_FILENO); dup2(fd, STDOUT_FILENO);
dup2(fd.get(), STDOUT_FILENO); dup2(fd, STDERR_FILENO);
dup2(fd.get(), STDERR_FILENO);
execl(remount_cmd, remount_cmd, cmd.empty() ? nullptr : cmd.c_str(), nullptr); execl(kRemountCmd, kRemountCmd, cmd.empty() ? nullptr : cmd.c_str(), nullptr);
_exit(-errno ?: 42); _exit(errno);
} }
int wstatus = 0; int wstatus = 0;
auto ret = waitpid(pid, &wstatus, 0); auto ret = waitpid(pid, &wstatus, 0);
if (ret == -1) { if (ret == -1) {
WriteFdFmt(fd.get(), "Failed to wait for %s: %s\n", remount_cmd, strerror(errno)); WriteFdFmt(fd, "Failed to wait for %s: %s\n", kRemountCmd, strerror(errno));
goto err; return false;
} } else if (ret != pid) {
WriteFdFmt(fd, "pid %d and waitpid return %d do not match for %s\n",
if (ret != pid) { static_cast<int>(pid), static_cast<int>(ret), kRemountCmd);
WriteFdFmt(fd.get(), "pid %d and waitpid return %d do not match for %s\n", return false;
static_cast<int>(pid), static_cast<int>(ret), remount_cmd);
goto err;
} }
if (WIFSIGNALED(wstatus)) { if (WIFSIGNALED(wstatus)) {
WriteFdFmt(fd.get(), "%s terminated with signal %s\n", remount_cmd, WriteFdFmt(fd, "%s terminated with signal %s\n", kRemountCmd,
strsignal(WTERMSIG(wstatus))); strsignal(WTERMSIG(wstatus)));
goto err; return false;
} }
if (!WIFEXITED(wstatus)) { if (!WIFEXITED(wstatus)) {
WriteFdFmt(fd.get(), "%s stopped with status 0x%x\n", remount_cmd, wstatus); WriteFdFmt(fd, "%s stopped with status 0x%x\n", kRemountCmd, wstatus);
goto err; return false;
} }
if (WEXITSTATUS(wstatus)) { if (WEXITSTATUS(wstatus)) {
WriteFdFmt(fd.get(), "%s exited with status %d\n", remount_cmd, WriteFdFmt(fd, "%s exited with status %d\n", kRemountCmd, WEXITSTATUS(wstatus));
static_cast<signed char>(WEXITSTATUS(wstatus))); return false;
goto err;
} }
WriteFdExactly(fd.get(), "remount succeeded\n"); return true;
return; }
err: void remount_service(unique_fd fd, const std::string& cmd) {
WriteFdExactly(fd.get(), remount_failed); const char* success = do_remount(fd.get(), cmd) ? "succeeded" : "failed";
WriteFdFmt(fd.get(), "remount %s\n", success);
} }

View file

@ -52,14 +52,13 @@ void suggest_run_adb_root(int fd) {
} }
static bool make_block_device_writable(const std::string& dev) { static bool make_block_device_writable(const std::string& dev) {
int fd = unix_open(dev, O_RDONLY | O_CLOEXEC); unique_fd fd(unix_open(dev, O_RDONLY | O_CLOEXEC));
if (fd == -1) { if (fd == -1) {
return false; return false;
} }
int OFF = 0; int OFF = 0;
bool result = (ioctl(fd, BLKROSET, &OFF) != -1); bool result = (ioctl(fd, BLKROSET, &OFF) != -1);
unix_close(fd);
return result; return result;
} }