diff mbox series

[OpenWrt-Devel,V2,fstools] block: generate hotplug.d mount events

Message ID 20181206125155.32381-1-zajec5@gmail.com
State Superseded
Delegated to: Rafał Miłecki
Headers show
Series [OpenWrt-Devel,V2,fstools] block: generate hotplug.d mount events | expand

Commit Message

Rafał Miłecki Dec. 6, 2018, 12:51 p.m. UTC
From: Rafał Miłecki <rafal@milecki.pl>

With this change block generates 2 "mount" hotplug.d subsystem events:
1) "add" when block device gets mounted
2) "remove" when block device gets unmounted

This allows e.g. controlling USB storage dependant software using
hotplug.d listeners.

A very similar solution was implemented in mountd which was replaced by
blockd.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
V2: Use hotplug_call() helper. It requires
    [PATCH libubox] hotplug: add hotplug_call() helper
---
 block.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
diff mbox series

Patch

diff --git a/block.c b/block.c
index 46050b4..99e02b0 100644
--- a/block.c
+++ b/block.c
@@ -38,6 +38,7 @@ 
 #include <uci.h>
 #include <uci_blob.h>
 
+#include <libubox/hotplug.h>
 #include <libubox/ulog.h>
 #include <libubox/list.h>
 #include <libubox/vlist.h>
@@ -880,6 +881,24 @@  static int exec_mount(const char *source, const char *target,
 	return err;
 }
 
+static int hotplug_call_mount(const char *action, const char *device)
+{
+	char actionenv[] = "ACTION=xxxxxx";
+	char deviceenv[32];
+	char *envp[] = { actionenv, deviceenv, NULL };
+	int err;
+
+	snprintf(actionenv, sizeof(actionenv), "ACTION=%s", action);
+	snprintf(deviceenv, sizeof(deviceenv), "DEVICE=%s", device);
+
+	err = hotplug_call("mount", envp);
+	if (err) {
+		ULOG_ERR("hotplug-call call failed: %d\n", err);
+	}
+
+	return err;
+}
+
 static int handle_mount(const char *source, const char *target,
                         const char *fstype, struct mount *m)
 {
@@ -1079,6 +1098,8 @@  static int mount_device(struct probe_info *pr, int type)
 
 	handle_swapfiles(true);
 
+	hotplug_call_mount("add", device);
+
 	return 0;
 }
 
@@ -1091,6 +1112,8 @@  static int umount_device(char *path)
 	if (!mp)
 		return -1;
 
+	hotplug_call_mount("remove", basename(path));
+
 	err = umount2(mp, MNT_DETACH);
 	if (err)
 		ULOG_ERR("unmounting %s (%s) failed (%d) - %m\n", path, mp,