Loading init/devices.cpp +38 −0 Original line number Diff line number Diff line Loading @@ -21,8 +21,14 @@ #include <sys/sysmacros.h> #include <unistd.h> #include <chrono> #include <map> #include <memory> #include <string> #include <thread> #include <android-base/chrono_utils.h> #include <android-base/file.h> #include <android-base/logging.h> #include <android-base/stringprintf.h> #include <android-base/strings.h> Loading @@ -37,12 +43,16 @@ #error "Do not include init.h in files used by ueventd; it will expose init's globals" #endif using namespace std::chrono_literals; using android::base::Basename; using android::base::Dirname; using android::base::ReadFileToString; using android::base::Readlink; using android::base::Realpath; using android::base::StartsWith; using android::base::StringPrintf; using android::base::Trim; namespace android { namespace init { Loading Loading @@ -101,6 +111,31 @@ static bool FindVbdDevicePrefix(const std::string& path, std::string* result) { return true; } // Given a path that may start with a virtual dm block device, populate // the supplied buffer with the dm module's instantiated name. // If it doesn't start with a virtual block device, or there is some // error, return false. static bool FindDmDevicePartition(const std::string& path, std::string* result) { result->clear(); if (!StartsWith(path, "/devices/virtual/block/dm-")) return false; if (getpid() == 1) return false; // first_stage_init has no sepolicy needs static std::map<std::string, std::string> cache; // wait_for_file will not work, the content is also delayed ... for (android::base::Timer t; t.duration() < 200ms; std::this_thread::sleep_for(10ms)) { if (ReadFileToString("/sys" + path + "/dm/name", result) && !result->empty()) { // Got it, set cache with result, when node arrives cache[path] = *result = Trim(*result); return true; } } auto it = cache.find(path); if ((it == cache.end()) || (it->second.empty())) return false; // Return cached results, when node goes away *result = it->second; return true; } Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid) : name_(name), perm_(perm), uid_(uid), gid_(gid), prefix_(false), wildcard_(false) { // Set 'prefix_' or 'wildcard_' based on the below cases: Loading Loading @@ -293,6 +328,7 @@ void SanitizePartitionName(std::string* string) { std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uevent) const { std::string device; std::string type; std::string partition; if (FindPlatformDevice(uevent.path, &device)) { // Skip /devices/platform or /devices/ if present Loading @@ -310,6 +346,8 @@ std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uev type = "pci"; } else if (FindVbdDevicePrefix(uevent.path, &device)) { type = "vbd"; } else if (FindDmDevicePartition(uevent.path, &partition)) { return {"/dev/block/mapper/" + partition}; } else { return {}; } Loading Loading
init/devices.cpp +38 −0 Original line number Diff line number Diff line Loading @@ -21,8 +21,14 @@ #include <sys/sysmacros.h> #include <unistd.h> #include <chrono> #include <map> #include <memory> #include <string> #include <thread> #include <android-base/chrono_utils.h> #include <android-base/file.h> #include <android-base/logging.h> #include <android-base/stringprintf.h> #include <android-base/strings.h> Loading @@ -37,12 +43,16 @@ #error "Do not include init.h in files used by ueventd; it will expose init's globals" #endif using namespace std::chrono_literals; using android::base::Basename; using android::base::Dirname; using android::base::ReadFileToString; using android::base::Readlink; using android::base::Realpath; using android::base::StartsWith; using android::base::StringPrintf; using android::base::Trim; namespace android { namespace init { Loading Loading @@ -101,6 +111,31 @@ static bool FindVbdDevicePrefix(const std::string& path, std::string* result) { return true; } // Given a path that may start with a virtual dm block device, populate // the supplied buffer with the dm module's instantiated name. // If it doesn't start with a virtual block device, or there is some // error, return false. static bool FindDmDevicePartition(const std::string& path, std::string* result) { result->clear(); if (!StartsWith(path, "/devices/virtual/block/dm-")) return false; if (getpid() == 1) return false; // first_stage_init has no sepolicy needs static std::map<std::string, std::string> cache; // wait_for_file will not work, the content is also delayed ... for (android::base::Timer t; t.duration() < 200ms; std::this_thread::sleep_for(10ms)) { if (ReadFileToString("/sys" + path + "/dm/name", result) && !result->empty()) { // Got it, set cache with result, when node arrives cache[path] = *result = Trim(*result); return true; } } auto it = cache.find(path); if ((it == cache.end()) || (it->second.empty())) return false; // Return cached results, when node goes away *result = it->second; return true; } Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid) : name_(name), perm_(perm), uid_(uid), gid_(gid), prefix_(false), wildcard_(false) { // Set 'prefix_' or 'wildcard_' based on the below cases: Loading Loading @@ -293,6 +328,7 @@ void SanitizePartitionName(std::string* string) { std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uevent) const { std::string device; std::string type; std::string partition; if (FindPlatformDevice(uevent.path, &device)) { // Skip /devices/platform or /devices/ if present Loading @@ -310,6 +346,8 @@ std::vector<std::string> DeviceHandler::GetBlockDeviceSymlinks(const Uevent& uev type = "pci"; } else if (FindVbdDevicePrefix(uevent.path, &device)) { type = "vbd"; } else if (FindDmDevicePartition(uevent.path, &partition)) { return {"/dev/block/mapper/" + partition}; } else { return {}; } Loading