diff mbox series

[v3] mdopen: add sbin path to env PATH when call system("modprobe md_mod")

Message ID 20250122151859.254365-1-colyli@suse.de (mailing list archive)
State Accepted
Headers show
Series [v3] mdopen: add sbin path to env PATH when call system("modprobe md_mod") | expand

Checks

Context Check Description
mdraidci/vmtest-md-6_14-PR fail merge-conflict

Commit Message

Coly Li Jan. 22, 2025, 3:18 p.m. UTC
During the boot process if mdadm is called in udev context, sbin paths
like /sbin, /usr/sbin, /usr/local/sbin normally not defined in PATH env
variable, calling system("modprobe md_mod") in create_named_array() may
fail with 'sh: modprobe: command not found' error message.

We don't want to move modprobe binary into udev private directory, so
setting the PATH env is a more proper method to avoid the above issue.

This patch sets PATH env variable with "/sbin:/usr/sbin:/usr/local/sbin"
before calling system("modprobe md_mod"). The change only takes effect
within the udev worker context, not seen by global udev environment.

Signed-off-by: Coly Li <colyli@suse.de>
---
Changelog,
v3, check return value of getenv().
v2: set buf[PATH_MAX] to 0 in stack variable announcement.
v1: the original version.

 mdopen.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Comments

Mariusz Tkaczyk Jan. 27, 2025, 9:14 a.m. UTC | #1
On Wed, 22 Jan 2025 23:18:59 +0800
Coly Li <colyli@suse.de> wrote:

> During the boot process if mdadm is called in udev context, sbin paths
> like /sbin, /usr/sbin, /usr/local/sbin normally not defined in PATH
> env variable, calling system("modprobe md_mod") in
> create_named_array() may fail with 'sh: modprobe: command not found'
> error message.
> 
> We don't want to move modprobe binary into udev private directory, so
> setting the PATH env is a more proper method to avoid the above issue.
> 
> This patch sets PATH env variable with
> "/sbin:/usr/sbin:/usr/local/sbin" before calling system("modprobe
> md_mod"). The change only takes effect within the udev worker
> context, not seen by global udev environment.
> 
> Signed-off-by: Coly Li <colyli@suse.de>
> ---
> Changelog,
> v3, check return value of getenv().
> v2: set buf[PATH_MAX] to 0 in stack variable announcement.
> v1: the original version.
> 

Applied!
Thanks,
Mariusz
diff mbox series

Patch

diff --git a/mdopen.c b/mdopen.c
index 26f0c716..57252b64 100644
--- a/mdopen.c
+++ b/mdopen.c
@@ -39,6 +39,24 @@  int create_named_array(char *devnm)
 
 	fd = open(new_array_file, O_WRONLY);
 	if (fd < 0 && errno == ENOENT) {
+		char buf[PATH_MAX] = {0};
+		char *env_ptr;
+
+		env_ptr = getenv("PATH");
+		/*
+		 * When called by udev worker context, path of modprobe
+		 * might not be in env PATH. Set sbin paths into PATH
+		 * env to avoid potential failure when run modprobe here.
+		 */
+		if (env_ptr)
+			snprintf(buf, PATH_MAX - 1, "%s:%s", env_ptr,
+				 "/sbin:/usr/sbin:/usr/local/sbin");
+		else
+			snprintf(buf, PATH_MAX - 1, "%s",
+				 "/sbin:/usr/sbin:/usr/local/sbin");
+
+		setenv("PATH", buf, 1);
+
 		if (system("modprobe md_mod") == 0)
 			fd = open(new_array_file, O_WRONLY);
 	}