2009-03-04 11:32:55 +08: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.
|
|
|
|
*/
|
|
|
|
|
2015-09-23 06:52:57 +08:00
|
|
|
#define TRACE_TAG ADB
|
2015-03-20 06:21:08 +08:00
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
2014-05-01 00:10:31 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-01-03 06:02:14 +08:00
|
|
|
#include <mntent.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <stdio.h>
|
2014-05-01 00:10:31 +08:00
|
|
|
#include <stdlib.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mount.h>
|
2014-05-01 00:10:31 +08:00
|
|
|
#include <unistd.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
#include <string>
|
|
|
|
|
2016-09-24 06:40:03 +08:00
|
|
|
#include <android-base/properties.h>
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
#include "adb.h"
|
2015-02-25 13:26:58 +08:00
|
|
|
#include "adb_io.h"
|
2015-04-18 04:57:15 +08:00
|
|
|
#include "adb_utils.h"
|
2015-06-30 08:30:28 +08:00
|
|
|
#include "fs_mgr.h"
|
|
|
|
|
2015-05-08 14:37:40 +08:00
|
|
|
// Returns the device used to mount a directory in /proc/mounts.
|
2015-06-30 08:30:28 +08:00
|
|
|
static std::string find_proc_mount(const char* dir) {
|
2015-05-08 14:37:40 +08:00
|
|
|
std::unique_ptr<FILE, int(*)(FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
|
|
|
|
if (!fp) {
|
|
|
|
return "";
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2015-05-08 14:37:40 +08:00
|
|
|
|
|
|
|
mntent* e;
|
|
|
|
while ((e = getmntent(fp.get())) != nullptr) {
|
|
|
|
if (strcmp(dir, e->mnt_dir) == 0) {
|
|
|
|
return e->mnt_fsname;
|
2015-01-03 06:02:14 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-08 14:37:40 +08:00
|
|
|
return "";
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-06-30 08:30:28 +08:00
|
|
|
// Returns the device used to mount a directory in the fstab.
|
|
|
|
static std::string find_fstab_mount(const char* dir) {
|
2016-09-24 06:40:03 +08:00
|
|
|
std::string fstab_filename = "/fstab." + android::base::GetProperty("ro.hardware", "");
|
2015-06-30 08:30:28 +08:00
|
|
|
struct fstab* fstab = fs_mgr_read_fstab(fstab_filename.c_str());
|
|
|
|
struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, dir);
|
|
|
|
std::string dev = rec ? std::string(rec->blk_device) : "";
|
|
|
|
fs_mgr_free_fstab(fstab);
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The proc entry for / is full of lies, so check fstab instead.
|
|
|
|
// /proc/mounts lists rootfs and /dev/root, neither of which is what we want.
|
|
|
|
static std::string find_mount(const char* dir) {
|
|
|
|
if (strcmp(dir, "/") == 0) {
|
|
|
|
return find_fstab_mount(dir);
|
|
|
|
} else {
|
|
|
|
return find_proc_mount(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 02:55:25 +08:00
|
|
|
bool make_block_device_writable(const std::string& dev) {
|
2015-03-17 05:58:32 +08:00
|
|
|
int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC);
|
|
|
|
if (fd == -1) {
|
2015-05-12 02:55:25 +08:00
|
|
|
return false;
|
2014-12-04 07:31:57 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
int OFF = 0;
|
2015-05-12 02:55:25 +08:00
|
|
|
bool result = (ioctl(fd, BLKROSET, &OFF) != -1);
|
2015-05-23 11:09:06 +08:00
|
|
|
unix_close(fd);
|
2015-03-17 05:58:32 +08:00
|
|
|
return result;
|
|
|
|
}
|
2015-03-17 05:35:53 +08:00
|
|
|
|
2015-05-12 02:55:25 +08:00
|
|
|
static bool remount_partition(int fd, const char* dir) {
|
|
|
|
if (!directory_exists(dir)) {
|
|
|
|
return true;
|
2015-01-02 21:30:50 +08:00
|
|
|
}
|
2015-05-12 02:55:25 +08:00
|
|
|
std::string dev = find_mount(dir);
|
|
|
|
if (dev.empty()) {
|
2015-05-08 14:37:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-05-12 02:55:25 +08:00
|
|
|
if (!make_block_device_writable(dev)) {
|
|
|
|
WriteFdFmt(fd, "remount of %s failed; couldn't make block device %s writable: %s\n",
|
|
|
|
dir, dev.c_str(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mount(dev.c_str(), dir, "none", MS_REMOUNT, nullptr) == -1) {
|
|
|
|
WriteFdFmt(fd, "remount of %s failed: %s\n", dir, strerror(errno));
|
2015-05-08 14:37:40 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-03-17 05:58:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remount_service(int fd, void* cookie) {
|
2015-02-26 07:48:06 +08:00
|
|
|
if (getuid() != 0) {
|
2015-05-01 08:32:03 +08:00
|
|
|
WriteFdExactly(fd, "Not running as root. Try \"adb root\" first.\n");
|
2015-02-26 07:48:06 +08:00
|
|
|
adb_close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-24 06:40:03 +08:00
|
|
|
bool system_verified = !(android::base::GetProperty("partition.system.verified", "").empty());
|
|
|
|
bool vendor_verified = !(android::base::GetProperty("partition.vendor.verified", "").empty());
|
2014-10-28 01:37:59 +08:00
|
|
|
|
|
|
|
if (system_verified || vendor_verified) {
|
|
|
|
// Allow remount but warn of likely bad effects
|
|
|
|
bool both = system_verified && vendor_verified;
|
2015-05-02 08:04:38 +08:00
|
|
|
WriteFdFmt(fd,
|
|
|
|
"dm_verity is enabled on the %s%s%s partition%s.\n",
|
|
|
|
system_verified ? "system" : "",
|
|
|
|
both ? " and " : "",
|
|
|
|
vendor_verified ? "vendor" : "",
|
|
|
|
both ? "s" : "");
|
2015-05-01 08:32:03 +08:00
|
|
|
WriteFdExactly(fd,
|
|
|
|
"Use \"adb disable-verity\" to disable verity.\n"
|
|
|
|
"If you do not, remount may succeed, however, you will still "
|
|
|
|
"not be able to write to these volumes.\n");
|
2014-10-28 01:37:59 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 05:58:32 +08:00
|
|
|
bool success = true;
|
2016-09-24 06:40:03 +08:00
|
|
|
if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
|
2015-06-30 08:30:28 +08:00
|
|
|
success &= remount_partition(fd, "/");
|
|
|
|
} else {
|
|
|
|
success &= remount_partition(fd, "/system");
|
|
|
|
}
|
2015-05-12 02:55:25 +08:00
|
|
|
success &= remount_partition(fd, "/vendor");
|
|
|
|
success &= remount_partition(fd, "/oem");
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-05-01 08:32:03 +08:00
|
|
|
WriteFdExactly(fd, success ? "remount succeeded\n" : "remount failed\n");
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
adb_close(fd);
|
|
|
|
}
|