2009-03-04 03:32:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-04-30 16:10:31 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2009-03-04 03:32:55 +00:00
|
|
|
#include <string.h>
|
2019-02-15 15:45:26 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2014-04-30 16:10:31 +00:00
|
|
|
#include <unistd.h>
|
2009-03-04 03:32:55 +00:00
|
|
|
|
2015-03-16 21:58:32 +00:00
|
|
|
#include <string>
|
2016-09-23 22:40:03 +00:00
|
|
|
|
2009-03-04 03:32:55 +00:00
|
|
|
#include "adb.h"
|
2015-02-25 05:26:58 +00:00
|
|
|
#include "adb_io.h"
|
2018-04-26 21:32:48 +00:00
|
|
|
#include "adb_unique_fd.h"
|
2015-05-08 06:37:40 +00:00
|
|
|
|
2019-03-05 21:01:25 +00:00
|
|
|
static constexpr char kRemountCmd[] = "/system/bin/remount";
|
2019-01-28 20:34:33 +00:00
|
|
|
|
2019-03-05 21:01:25 +00:00
|
|
|
static bool do_remount(int fd, const std::string& cmd) {
|
2019-02-15 15:45:26 +00:00
|
|
|
if (getuid() != 0) {
|
2019-03-05 21:01:25 +00:00
|
|
|
WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n");
|
|
|
|
return false;
|
2018-10-25 01:41:29 +00:00
|
|
|
}
|
2015-06-30 00:30:28 +00:00
|
|
|
|
2019-03-05 21:01:25 +00:00
|
|
|
auto pid = fork();
|
2019-02-15 15:45:26 +00:00
|
|
|
if (pid < 0) {
|
2019-03-05 21:01:25 +00:00
|
|
|
WriteFdFmt(fd, "Failed to fork to %s: %s\n", kRemountCmd, strerror(errno));
|
|
|
|
return false;
|
2015-06-30 00:30:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 15:45:26 +00:00
|
|
|
if (pid == 0) {
|
|
|
|
// child side of the fork
|
2019-03-05 21:01:25 +00:00
|
|
|
dup2(fd, STDIN_FILENO);
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
|
|
|
dup2(fd, STDERR_FILENO);
|
2018-11-06 16:27:05 +00:00
|
|
|
|
2019-03-05 21:01:25 +00:00
|
|
|
execl(kRemountCmd, kRemountCmd, cmd.empty() ? nullptr : cmd.c_str(), nullptr);
|
2019-07-11 21:15:32 +00:00
|
|
|
const char* msg = "failed to exec remount\n";
|
|
|
|
write(STDERR_FILENO, msg, strlen(msg));
|
2019-03-05 21:01:25 +00:00
|
|
|
_exit(errno);
|
2014-12-03 23:31:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 15:45:26 +00:00
|
|
|
int wstatus = 0;
|
|
|
|
auto ret = waitpid(pid, &wstatus, 0);
|
2018-05-11 23:34:55 +00:00
|
|
|
|
2019-02-15 15:45:26 +00:00
|
|
|
if (ret == -1) {
|
2019-03-05 21:01:25 +00:00
|
|
|
WriteFdFmt(fd, "Failed to wait for %s: %s\n", kRemountCmd, strerror(errno));
|
|
|
|
return false;
|
|
|
|
} else if (ret != pid) {
|
|
|
|
WriteFdFmt(fd, "pid %d and waitpid return %d do not match for %s\n",
|
|
|
|
static_cast<int>(pid), static_cast<int>(ret), kRemountCmd);
|
|
|
|
return false;
|
2018-07-19 00:43:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 15:45:26 +00:00
|
|
|
if (WIFSIGNALED(wstatus)) {
|
2019-03-05 21:01:25 +00:00
|
|
|
WriteFdFmt(fd, "%s terminated with signal %s\n", kRemountCmd,
|
2019-02-15 15:45:26 +00:00
|
|
|
strsignal(WTERMSIG(wstatus)));
|
2019-03-05 21:01:25 +00:00
|
|
|
return false;
|
2018-11-06 16:27:05 +00:00
|
|
|
}
|
2018-07-19 00:43:28 +00:00
|
|
|
|
2019-02-15 15:45:26 +00:00
|
|
|
if (!WIFEXITED(wstatus)) {
|
2019-03-05 21:01:25 +00:00
|
|
|
WriteFdFmt(fd, "%s stopped with status 0x%x\n", kRemountCmd, wstatus);
|
|
|
|
return false;
|
2018-02-22 05:19:07 +00:00
|
|
|
}
|
2018-05-17 19:12:54 +00:00
|
|
|
|
2019-02-15 15:45:26 +00:00
|
|
|
if (WEXITSTATUS(wstatus)) {
|
2019-03-05 21:01:25 +00:00
|
|
|
WriteFdFmt(fd, "%s exited with status %d\n", kRemountCmd, WEXITSTATUS(wstatus));
|
|
|
|
return false;
|
2018-05-17 19:12:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 21:01:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-02-25 23:48:06 +00:00
|
|
|
|
2019-03-05 21:01:25 +00:00
|
|
|
void remount_service(unique_fd fd, const std::string& cmd) {
|
2019-07-11 21:15:32 +00:00
|
|
|
do_remount(fd.get(), cmd);
|
|
|
|
// The remount command will print success or failure for us.
|
2009-03-04 03:32:55 +00:00
|
|
|
}
|