Merge "ueventd: populate /dev/block/mapper link" am: 5c811fd01a

am: ee32edb306

Change-Id: I8278fa28d430dc17f4e45b263314643ecb27631d
This commit is contained in:
Mark Salyzyn 2019-04-02 20:27:17 -07:00 committed by android-build-merger
commit ae0c91dad4

View file

@ -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>
@ -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 {
@ -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:
@ -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
@ -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 {};
}