Revert "ueventd: remove PlatformDeviceList"

Bug: 62864413

This reverts commit f51657ccef9d49b3dc037aae5a072bce795fcf99.

Change-Id: I1397ca2de716395782139688542c69848a31d62f
diff --git a/init/devices.cpp b/init/devices.cpp
index 2943fb7..c52d8f8 100644
--- a/init/devices.cpp
+++ b/init/devices.cpp
@@ -147,34 +147,21 @@
     }
 }
 
-// Given a path that may start with a platform device, find the parent platform device by finding a
-// parent directory with a 'subsystem' symlink that points to the platform bus.
-// If it doesn't start with a platform device, return false
-bool DeviceHandler::FindPlatformDevice(std::string path, std::string* platform_device_path) const {
-    platform_device_path->clear();
-
-    // Uevents don't contain the mount point, so we need to add it here.
-    path.insert(0, sysfs_mount_point_);
-
-    std::string directory = android::base::Dirname(path);
-
-    while (directory != "/" && directory != ".") {
-        std::string subsystem_link_path;
-        if (android::base::Realpath(directory + "/subsystem", &subsystem_link_path) &&
-            subsystem_link_path == sysfs_mount_point_ + "/bus/platform") {
-            // We need to remove the mount point that we added above before returning.
-            directory.erase(0, sysfs_mount_point_.size());
-            *platform_device_path = directory;
+// Given a path that may start with a platform device, find the length of the
+// platform device prefix.  If it doesn't start with a platform device, return false
+bool PlatformDeviceList::Find(const std::string& path, std::string* out_path) const {
+    out_path->clear();
+    // platform_devices is searched backwards, since parents are added before their children,
+    // and we want to match as deep of a child as we can.
+    for (auto it = platform_devices_.crbegin(); it != platform_devices_.crend(); ++it) {
+        auto platform_device_path_length = it->length();
+        if (platform_device_path_length < path.length() &&
+            path[platform_device_path_length] == '/' &&
+            android::base::StartsWith(path, it->c_str())) {
+            *out_path = *it;
             return true;
         }
-
-        auto last_slash = path.rfind('/');
-        if (last_slash == std::string::npos) return false;
-
-        path.erase(last_slash);
-        directory = android::base::Dirname(path);
     }
-
     return false;
 }
 
@@ -271,7 +258,7 @@
 
 std::vector<std::string> DeviceHandler::GetCharacterDeviceSymlinks(const Uevent& uevent) const {
     std::string parent_device;
-    if (!FindPlatformDevice(uevent.path, &parent_device)) return {};
+    if (!platform_devices_.Find(uevent.path, &parent_device)) return {};
 
     // skip path to the parent driver
     std::string path = uevent.path.substr(parent_device.length());
@@ -329,7 +316,7 @@
     std::string device;
     std::string type;
 
-    if (FindPlatformDevice(uevent.path, &device)) {
+    if (platform_devices_.Find(uevent.path, &device)) {
         // Skip /devices/platform or /devices/ if present
         static const std::string devices_platform_prefix = "/devices/platform/";
         static const std::string devices_prefix = "/devices/";
@@ -401,6 +388,14 @@
     }
 }
 
+void DeviceHandler::HandlePlatformDeviceEvent(const Uevent& uevent) {
+    if (uevent.action == "add") {
+        platform_devices_.Add(uevent.path);
+    } else if (uevent.action == "remove") {
+        platform_devices_.Remove(uevent.path);
+    }
+}
+
 void DeviceHandler::HandleBlockDeviceEvent(const Uevent& uevent) const {
     // if it's not a /dev device, nothing to do
     if (uevent.major < 0 || uevent.minor < 0) return;
@@ -463,6 +458,8 @@
 
     if (uevent.subsystem == "block") {
         HandleBlockDeviceEvent(uevent);
+    } else if (uevent.subsystem == "platform") {
+        HandlePlatformDeviceEvent(uevent);
     } else {
         HandleGenericDeviceEvent(uevent);
     }
@@ -475,8 +472,7 @@
       sysfs_permissions_(std::move(sysfs_permissions)),
       subsystems_(std::move(subsystems)),
       sehandle_(selinux_android_file_context_handle()),
-      skip_restorecon_(skip_restorecon),
-      sysfs_mount_point_("/sys") {}
+      skip_restorecon_(skip_restorecon) {}
 
 DeviceHandler::DeviceHandler()
     : DeviceHandler(std::vector<Permissions>{}, std::vector<SysfsPermissions>{},