diff mbox series

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

Message ID 20181205213549.13138-1-zajec5@gmail.com
State Changes Requested
Delegated to: John Crispin
Headers show
Series [OpenWrt-Devel] block: generate hotplug.d mount events | expand

Commit Message

Rafał Miłecki Dec. 5, 2018, 9:35 p.m. UTC
From: Rafał Miłecki <rafal@milecki.pl>

This is what was implemented in mountd and what some scripts used to
use. It's a pretty generic solution for managing software that may use
e.g. USB storage.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
 block.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

Comments

Rafał Miłecki Dec. 5, 2018, 9:39 p.m. UTC | #1
On 2018-12-05 22:35, Rafał Miłecki wrote:
> From: Rafał Miłecki <rafal@milecki.pl>
> 
> This is what was implemented in mountd and what some scripts used to
> use. It's a pretty generic solution for managing software that may use
> e.g. USB storage.
> 
> Signed-off-by: Rafał Miłecki <rafal@milecki.pl>

Forgot to say, it requires
[PATCH fstools] blockd: don't unmount device when removing it from the 
list
to work correctly.

Without above patch blockd unmounts device, block can't find a mount
point and gives up.
Alberto Bursi Dec. 5, 2018, 10:29 p.m. UTC | #2
On 05/12/18 22:35, Rafał Miłecki wrote:
> From: Rafał Miłecki <rafal@milecki.pl>
>
> This is what was implemented in mountd and what some scripts used to
> use. It's a pretty generic solution for managing software that may use
> e.g. USB storage.


Could you explain a bit more about what is this doing instead of just 
mentioning mountd?

There is pretty scarce info about mountd.

-Alberto

>
> Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
> ---
>   block.c | 29 +++++++++++++++++++++++++++++
>   1 file changed, 29 insertions(+)
>
> diff --git a/block.c b/block.c
> index 46050b4..fe63fb0 100644
> --- a/block.c
> +++ b/block.c
> @@ -880,6 +880,31 @@ static int exec_mount(const char *source, const char *target,
>   	return err;
>   }
>   
> +static void hotplug_call_mount(const char *action, const char *device)
> +{
> +	pid_t pid;
> +
> +	pid = fork();
> +	if (!pid) {
> +		char * const argv[] = { "hotplug-call", "mount", NULL };
> +		char actionenv[] = "ACTION=xxxxxx";
> +		char deviceenv[32];
> +		char *envp[] = { actionenv, deviceenv, NULL };
> +
> +		snprintf(actionenv, sizeof(actionenv), "ACTION=%s", action);
> +		snprintf(deviceenv, sizeof(deviceenv), "DEVICE=%s", device);
> +
> +		execve("/sbin/hotplug-call", argv, envp);
> +		exit(-1);
> +	} else if (pid > 0) {
> +		int status;
> +
> +		waitpid(pid, &status, 0);
> +		if (WEXITSTATUS(status))
> +			ULOG_ERR("hotplug-call call failed: %d\n", WEXITSTATUS(status));
> +	}
> +}
> +
>   static int handle_mount(const char *source, const char *target,
>                           const char *fstype, struct mount *m)
>   {
> @@ -1079,6 +1104,8 @@ static int mount_device(struct probe_info *pr, int type)
>   
>   	handle_swapfiles(true);
>   
> +	hotplug_call_mount("add", device);
> +
>   	return 0;
>   }
>   
> @@ -1091,6 +1118,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,
Rafał Miłecki Dec. 6, 2018, 5:57 a.m. UTC | #3
On Wed, 5 Dec 2018 at 23:30, Alberto Bursi <bobafetthotmail@gmail.com> wrote:
> On 05/12/18 22:35, Rafał Miłecki wrote:
> > From: Rafał Miłecki <rafal@milecki.pl>
> >
> > This is what was implemented in mountd and what some scripts used to
> > use. It's a pretty generic solution for managing software that may use
> > e.g. USB storage.
>
>
> Could you explain a bit more about what is this doing instead of just
> mentioning mountd?
>
> There is pretty scarce info about mountd.

This generates following hotplug.d "mount" events:
1) "add" when block device gets mounted
2) "remove" when block device gets unmounted

It allows one to put custom scripts/executables in the
/etc/hotplug.d/mount/ and react to appearing/disappearing mount
points.
diff mbox series

Patch

diff --git a/block.c b/block.c
index 46050b4..fe63fb0 100644
--- a/block.c
+++ b/block.c
@@ -880,6 +880,31 @@  static int exec_mount(const char *source, const char *target,
 	return err;
 }
 
+static void hotplug_call_mount(const char *action, const char *device)
+{
+	pid_t pid;
+
+	pid = fork();
+	if (!pid) {
+		char * const argv[] = { "hotplug-call", "mount", NULL };
+		char actionenv[] = "ACTION=xxxxxx";
+		char deviceenv[32];
+		char *envp[] = { actionenv, deviceenv, NULL };
+
+		snprintf(actionenv, sizeof(actionenv), "ACTION=%s", action);
+		snprintf(deviceenv, sizeof(deviceenv), "DEVICE=%s", device);
+
+		execve("/sbin/hotplug-call", argv, envp);
+		exit(-1);
+	} else if (pid > 0) {
+		int status;
+
+		waitpid(pid, &status, 0);
+		if (WEXITSTATUS(status))
+			ULOG_ERR("hotplug-call call failed: %d\n", WEXITSTATUS(status));
+	}
+}
+
 static int handle_mount(const char *source, const char *target,
                         const char *fstype, struct mount *m)
 {
@@ -1079,6 +1104,8 @@  static int mount_device(struct probe_info *pr, int type)
 
 	handle_swapfiles(true);
 
+	hotplug_call_mount("add", device);
+
 	return 0;
 }
 
@@ -1091,6 +1118,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,