diff mbox series

[OpenWrt-Devel] kmodloader: added -v and -a args to modeprobe

Message ID 20191013040315.9498-1-G.M0N3Y.2503@gmail.com
State Superseded, archived
Headers show
Series [OpenWrt-Devel] kmodloader: added -v and -a args to modeprobe | expand

Commit Message

Gerard Ryan Oct. 13, 2019, 4:03 a.m. UTC
This is primarily to satiate the usage by docker/libnetwork.
Behaviour mirrors /tools/modprobe.c from https://git.kernel.org

Signed-off-by: Gerard Ryan <G.M0N3Y.2503@gmail.com>
---
Compile tested: x86_x64, Hyper-V, OpenWrt Master
Run tested: x86_x64, Hyper-V, OpenWrt Master

You can also find this patch on GitHub if you prefer.
https://github.com/G-M0N3Y-2503/openwrt-ubox-mirror/tree/feature_extend_modprobe_options

 kmodloader.c | 76 +++++++++++++++++++++++++++++++---------------------
 1 file changed, 46 insertions(+), 30 deletions(-)

--
2.17.1

Comments

Hauke Mehrtens Oct. 19, 2019, 9:49 a.m. UTC | #1
On 10/13/19 6:03 AM, Gerard Ryan wrote:
> This is primarily to satiate the usage by docker/libnetwork.
> Behaviour mirrors /tools/modprobe.c from https://git.kernel.org
> 
> Signed-off-by: Gerard Ryan <G.M0N3Y.2503@gmail.com>
> ---
> Compile tested: x86_x64, Hyper-V, OpenWrt Master
> Run tested: x86_x64, Hyper-V, OpenWrt Master
> 
> You can also find this patch on GitHub if you prefer.
> https://github.com/G-M0N3Y-2503/openwrt-ubox-mirror/tree/feature_extend_modprobe_options
> 
>  kmodloader.c | 76 +++++++++++++++++++++++++++++++---------------------
>  1 file changed, 46 insertions(+), 30 deletions(-)

Could you please split this into two patches, that would make it easier
to review these changes.

Hauke

> 
> diff --git a/kmodloader.c b/kmodloader.c
> index 422c895..a437509 100644
> --- a/kmodloader.c
> +++ b/kmodloader.c
> @@ -678,7 +678,9 @@ static int print_insmod_usage(void)
> 
>  static int print_modprobe_usage(void)
>  {
> -	ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
> +	ULOG_INFO("Usage:\n"
> +			  "\tmodprobe [-q] [-v] filename\n"
> +			  "\tmodprobe -a [-q] [-v] filename [filename...]\n");
> 
>  	return -1;
>  }
> @@ -851,18 +853,26 @@ static int main_modinfo(int argc, char **argv)
> 
>  static int main_modprobe(int argc, char **argv)
>  {
> +	int exit_code = EXIT_SUCCESS;
>  	struct module_node *mn;
>  	struct module *m;
> -	char *name;
> -	char *mod = NULL;
> +	int load_fail;
> +	int log_level = LOG_WARNING;
>  	int opt;
>  	bool quiet = false;
> +	bool use_all = false;
> 
> -	while ((opt = getopt(argc, argv, "q")) != -1 ) {
> +	while ((opt = getopt(argc, argv, "aqv")) != -1 ) {
>  		switch (opt) {
> +			case 'a':
> +				use_all = true;
> +				break;
>  			case 'q': /* shhhh! */
>  				quiet = true;
>  				break;
> +			case 'v':
> +				log_level = LOG_DEBUG;
> +				break;
>  			default: /* '?' */
>  				return print_modprobe_usage();
>  				break;
> @@ -872,7 +882,8 @@ static int main_modprobe(int argc, char **argv)
>  	if (optind >= argc)
>  		return print_modprobe_usage(); /* expected module after options */
> 
> -	mod = argv[optind];
> +	/* after print_modprobe_usage() so it won't be filtered out */
> +	ulog_threshold(log_level);
> 
>  	if (scan_module_folders())
>  		return -1;
> @@ -880,40 +891,45 @@ static int main_modprobe(int argc, char **argv)
>  	if (scan_loaded_modules())
>  		return -1;
> 
> -	name = get_module_name(mod);
> -	m = find_module(name);
> -	if (m && m->state == LOADED) {
> -		if (!quiet)
> -			ULOG_ERR("%s is already loaded\n", name);
> -		return 0;
> -	} else if (!m) {
> -		if (!quiet)
> -			ULOG_ERR("failed to find a module named %s\n", name);
> -		return -1;
> -	} else {
> -		int fail;
> +	do {
> +		char *name;
> 
> -		m->state = PROBE;
> +		name = get_module_name(argv[optind]);
> +		m = find_module(name);
> 
> -		fail = load_modprobe(true);
> +		if (m && m->state == LOADED) {
> +			if (!quiet)
> +				ULOG_INFO("%s is already loaded\n", name);
> +		} else if (!m) {
> +			if (!quiet)
> +				ULOG_ERR("failed to find a module named %s\n", name);
> +			exit_code = EXIT_FAILURE;
> +		} else {
> +			m->state = PROBE;
> +		}
> 
> -		if (fail) {
> -			ULOG_ERR("%d module%s could not be probed\n",
> -			         fail, (fail == 1) ? ("") : ("s"));
> +		optind++;
> +	} while (use_all && optind < argc);
> 
> -			avl_for_each_element(&modules, mn, avl) {
> -				if (mn->is_alias)
> -					continue;
> -				m = mn->m;
> -				if ((m->state == PROBE) || m->error)
> -					ULOG_ERR("- %s\n", m->name);
> -			}
> +	load_fail = load_modprobe(true);
> +	if (load_fail) {
> +		ULOG_ERR("%d module%s could not be probed\n",
> +					load_fail, (load_fail == 1) ? ("") : ("s"));
> +
> +		avl_for_each_element(&modules, mn, avl) {
> +			if (mn->is_alias)
> +				continue;
> +			m = mn->m;
> +			if ((m->state == PROBE) || m->error)
> +				ULOG_ERR("- %s\n", m->name);
>  		}
> +
> +		exit_code = EXIT_FAILURE;
>  	}
> 
>  	free_modules();
> 
> -	return 0;
> +	return exit_code;
>  }
> 
>  static int main_loader(int argc, char **argv)
> --
> 2.17.1
diff mbox series

Patch

diff --git a/kmodloader.c b/kmodloader.c
index 422c895..a437509 100644
--- a/kmodloader.c
+++ b/kmodloader.c
@@ -678,7 +678,9 @@  static int print_insmod_usage(void)

 static int print_modprobe_usage(void)
 {
-	ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
+	ULOG_INFO("Usage:\n"
+			  "\tmodprobe [-q] [-v] filename\n"
+			  "\tmodprobe -a [-q] [-v] filename [filename...]\n");

 	return -1;
 }
@@ -851,18 +853,26 @@  static int main_modinfo(int argc, char **argv)

 static int main_modprobe(int argc, char **argv)
 {
+	int exit_code = EXIT_SUCCESS;
 	struct module_node *mn;
 	struct module *m;
-	char *name;
-	char *mod = NULL;
+	int load_fail;
+	int log_level = LOG_WARNING;
 	int opt;
 	bool quiet = false;
+	bool use_all = false;

-	while ((opt = getopt(argc, argv, "q")) != -1 ) {
+	while ((opt = getopt(argc, argv, "aqv")) != -1 ) {
 		switch (opt) {
+			case 'a':
+				use_all = true;
+				break;
 			case 'q': /* shhhh! */
 				quiet = true;
 				break;
+			case 'v':
+				log_level = LOG_DEBUG;
+				break;
 			default: /* '?' */
 				return print_modprobe_usage();
 				break;
@@ -872,7 +882,8 @@  static int main_modprobe(int argc, char **argv)
 	if (optind >= argc)
 		return print_modprobe_usage(); /* expected module after options */

-	mod = argv[optind];
+	/* after print_modprobe_usage() so it won't be filtered out */
+	ulog_threshold(log_level);

 	if (scan_module_folders())
 		return -1;
@@ -880,40 +891,45 @@  static int main_modprobe(int argc, char **argv)
 	if (scan_loaded_modules())
 		return -1;

-	name = get_module_name(mod);
-	m = find_module(name);
-	if (m && m->state == LOADED) {
-		if (!quiet)
-			ULOG_ERR("%s is already loaded\n", name);
-		return 0;
-	} else if (!m) {
-		if (!quiet)
-			ULOG_ERR("failed to find a module named %s\n", name);
-		return -1;
-	} else {
-		int fail;
+	do {
+		char *name;

-		m->state = PROBE;
+		name = get_module_name(argv[optind]);
+		m = find_module(name);

-		fail = load_modprobe(true);
+		if (m && m->state == LOADED) {
+			if (!quiet)
+				ULOG_INFO("%s is already loaded\n", name);
+		} else if (!m) {
+			if (!quiet)
+				ULOG_ERR("failed to find a module named %s\n", name);
+			exit_code = EXIT_FAILURE;
+		} else {
+			m->state = PROBE;
+		}

-		if (fail) {
-			ULOG_ERR("%d module%s could not be probed\n",
-			         fail, (fail == 1) ? ("") : ("s"));
+		optind++;
+	} while (use_all && optind < argc);

-			avl_for_each_element(&modules, mn, avl) {
-				if (mn->is_alias)
-					continue;
-				m = mn->m;
-				if ((m->state == PROBE) || m->error)
-					ULOG_ERR("- %s\n", m->name);
-			}
+	load_fail = load_modprobe(true);
+	if (load_fail) {
+		ULOG_ERR("%d module%s could not be probed\n",
+					load_fail, (load_fail == 1) ? ("") : ("s"));
+
+		avl_for_each_element(&modules, mn, avl) {
+			if (mn->is_alias)
+				continue;
+			m = mn->m;
+			if ((m->state == PROBE) || m->error)
+				ULOG_ERR("- %s\n", m->name);
 		}
+
+		exit_code = EXIT_FAILURE;
 	}

 	free_modules();

-	return 0;
+	return exit_code;
 }

 static int main_loader(int argc, char **argv)