ueventd: Put dm-user devices in a folder

When a device-mapper table contains a "user" entry, a corresponding
dm-user misc control device is created. The devices are put into a
separate folder by default, which currently confuses ueventd, and we
wind up with paths like:

    /dev/dm-user!blah

Special case these devices so they wind up as:

    /dev/dm-user/blah

Test: dmctl create blah user 0 100 test-device
      /dev/dm-user/test-device exists
Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
Change-Id: I313db07c3400f14f3ed0ffa20fdac2ac3e34b6d3
diff --git a/init/devices.cpp b/init/devices.cpp
index 53ca875..f8eb16a 100644
--- a/init/devices.cpp
+++ b/init/devices.cpp
@@ -22,6 +22,7 @@
 #include <unistd.h>
 
 #include <chrono>
+#include <filesystem>
 #include <memory>
 #include <string>
 #include <thread>
@@ -439,6 +440,13 @@
             }
         }
         unlink(devpath.c_str());
+
+        if (android::base::StartsWith(devpath, "/dev/dm-user/")) {
+            std::error_code ec;
+            if (std::filesystem::is_empty("/dev/dm-user/", ec)) {
+                rmdir("/dev/dm-user");
+            }
+        }
     }
 }
 
@@ -496,6 +504,8 @@
     } else if (StartsWith(uevent.subsystem, "usb")) {
         // ignore other USB events
         return;
+    } else if (uevent.subsystem == "misc" && StartsWith(uevent.device_name, "dm-user/")) {
+        devpath = "/dev/dm-user/" + uevent.device_name.substr(8);
     } else {
         devpath = "/dev/" + Basename(uevent.path);
     }