summaryrefslogtreecommitdiff
path: root/Utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Utils.cpp')
-rw-r--r--Utils.cpp36
1 files changed, 34 insertions, 2 deletions
diff --git a/Utils.cpp b/Utils.cpp
index 64dc75e..5b0470b 100644
--- a/Utils.cpp
+++ b/Utils.cpp
@@ -69,6 +69,7 @@ bool sSleepOnUnmount = true;
static const char* kBlkidPath = "/system/bin/blkid";
static const char* kKeyPath = "/data/misc/vold";
+static const char* kProcDevices = "/proc/devices";
static const char* kProcFilesystems = "/proc/filesystems";
// Lock used to protect process-level SELinux changes from racing with each
@@ -799,8 +800,39 @@ bool Readlinkat(int dirfd, const std::string& path, std::string* result) {
}
}
-bool IsRunningInEmulator() {
- return android::base::GetBoolProperty("ro.kernel.qemu", false);
+static unsigned int GetMajorBlockVirtioBlk() {
+ std::string devices;
+ if (!ReadFileToString(kProcDevices, &devices)) {
+ PLOG(ERROR) << "Unable to open /proc/devices";
+ return 0;
+ }
+
+ bool blockSection = false;
+ for (auto line : android::base::Split(devices, "\n")) {
+ if (line == "Block devices:") {
+ blockSection = true;
+ } else if (line == "Character devices:") {
+ blockSection = false;
+ } else if (blockSection) {
+ auto tokens = android::base::Split(line, " ");
+ if (tokens.size() == 2 && tokens[1] == "virtblk") {
+ return std::stoul(tokens[0]);
+ }
+ }
+ }
+
+ return 0;
+}
+
+bool IsVirtioBlkDevice(unsigned int major) {
+ // Most virtualized platforms expose block devices with the virtio-blk
+ // block device driver. Unfortunately, this driver does not use a fixed
+ // major number, but relies on the kernel to assign one from a specific
+ // range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
+ // per Documentation/devices.txt. This is true even for the latest Linux
+ // kernel (4.4; see init() in drivers/block/virtio_blk.c).
+ static unsigned int kMajorBlockVirtioBlk = GetMajorBlockVirtioBlk();
+ return kMajorBlockVirtioBlk && major == kMajorBlockVirtioBlk;
}
static status_t findMountPointsWithPrefix(const std::string& prefix,