ueventd: Fix a race condition in handling device-mapper events.
We've had flake in libdm_test for a long time, with no clear cause.
Lately however it has become particularly reproducible when running
the UeventAfterLoadTable test in isolation, and thus we've identified
the root cause.
uevents for device-mapper are fired when the sysfs node is added, but at
that time, the "dm" subnode has not yet been added. The root node and dm
node are added very close together, so usually it works, but sometimes
ueventd is too fast.
Instead of relying on sysfs, query the uuid/name node directly from
device-mapper.
Bug: 286011429
Test: libdm_test
(cherry picked from https://android-review.googlesource.com/q/commit:59abbfe64706a7ea0c4e932ae40bc8bde9746dce)
Merged-In: I258de5de05d813c3cb7f129e82e56dbfe8bf3117
Change-Id: I85e240807e0ce5ade211ec65453ab06d4066992a
diff --git a/init/devices.cpp b/init/devices.cpp
index 39442a0..d29ffd6 100644
--- a/init/devices.cpp
+++ b/init/devices.cpp
@@ -32,6 +32,7 @@
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
+#include <libdm/dm.h>
#include <private/android_filesystem_config.h>
#include <selinux/android.h>
#include <selinux/selinux.h>
@@ -112,17 +113,14 @@
// 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 FindDmDevice(const std::string& path, std::string* name, std::string* uuid) {
- if (!StartsWith(path, "/devices/virtual/block/dm-")) return false;
+static bool FindDmDevice(const Uevent& uevent, std::string* name, std::string* uuid) {
+ if (!StartsWith(uevent.path, "/devices/virtual/block/dm-")) return false;
+ if (uevent.action == "remove") return false; // Avoid error spam from ioctl
- if (!ReadFileToString("/sys" + path + "/dm/name", name)) {
- return false;
- }
- ReadFileToString("/sys" + path + "/dm/uuid", uuid);
+ dev_t dev = makedev(uevent.major, uevent.minor);
- *name = android::base::Trim(*name);
- *uuid = android::base::Trim(*uuid);
- return true;
+ auto& dm = android::dm::DeviceMapper::Instance();
+ return dm.GetDeviceNameAndUuid(dev, name, uuid);
}
Permissions::Permissions(const std::string& name, mode_t perm, uid_t uid, gid_t gid,
@@ -392,7 +390,7 @@
type = "pci";
} else if (FindVbdDevicePrefix(uevent.path, &device)) {
type = "vbd";
- } else if (FindDmDevice(uevent.path, &partition, &uuid)) {
+ } else if (FindDmDevice(uevent, &partition, &uuid)) {
std::vector<std::string> symlinks = {"/dev/block/mapper/" + partition};
if (!uuid.empty()) {
symlinks.emplace_back("/dev/block/mapper/by-uuid/" + uuid);