Skip to contentSkip to conversation

lib: Fix kernel module detection on BusyBox

PATCH Superseded Petr Vorel 3 participants 6 messages Jan 18, 2021 – Jan 20, 2021 Library code

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH v2 1/2 lib: Fix kernel module detection on BusyBox

BusyBox modprobe implementation does not support -n switch.

It does support -D, which could be used, *but* unless is busybox binary configured with CONFIG_MODPROBE_SMALL=y (IMHO the default).

We could use modinfo and grep output for 'filename:', but we agreed on ML that having our own implementation will be the best as it also does not require modinfo as external dependency.

Implementation searches for for module presence in /lib/modules/$(uname -r)/modules.{dep,builtin}. On Android expect files in /system/lib/modules directory.

On Android still assume all drivers are available as config files might not be available).

This fixes many tests on BusyBox, e.g. *all* network tests (tests using tst_net.sh) after 305a78e4c ("tst_net.sh: Require veth for netns").

---
Changes v1->v2:
* Check also modules.builtin (built-in dependency).

Kind regards,
Petr

 lib/tst_kernel.c | 81 ++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 72 insertions(+), 9 deletions(-)
lib/tst_kernel.c +72 -9
index 57fa4b2be..ab324a643 100644
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2017 Cyril Hrubis <[email protected]>
+ * Copyright (c) 2021 Petr Vorel <[email protected]>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,8 +18,11 @@
 
 #include <sys/personality.h>
 #include <sys/utsname.h>
+#include <limits.h>
+
 #include "test.h"
 #include "tst_kernel.h"
+#include "old_safe_stdio.h"
 
 static int get_kernel_bits_from_uname(struct utsname *buf)
 {
@@ -81,20 +85,79 @@ int tst_kernel_bits(void)
 	return kernel_bits;
 }
 
-int tst_check_driver(const char *name)
+#ifndef __ANDROID__
+# define MODULES_DIR "/lib/modules"
+#else
+# define MODULES_DIR "/system/lib/modules"
+#endif
+
+
+int tst_search_driver(const char *driver, const char *file)
 {
+	struct stat st;
+	char *path = NULL;
+	char buf[PATH_MAX], module[PATH_MAX], search[PATH_MAX] = "/";
+	FILE *f;
+
 #ifndef __ANDROID__
-	const char * const argv[] = { "modprobe", "-n", name, NULL };
-	int res = tst_cmd_(NULL, argv, "/dev/null", "/dev/null",
-			       TST_CMD_PASS_RETVAL);
+	struct utsname uts;
 
-	/* 255 - it looks like modprobe not available */
-	return (res == 255) ? 0 : res;
+	if (uname(&uts)) {
+		tst_brkm(TBROK | TERRNO, NULL, "uname() failed");
+		return -1;
+	}
+	SAFE_ASPRINTF(NULL, &path, "%s/%s/%s", MODULES_DIR, uts.release, file);
 #else
-	/* Android modprobe may not have '-n', or properly installed
-	 * module.*.bin files to determine built-in drivers. Assume
-	 * all drivers are available.
+	SAFE_ASPRINTF(NULL, &path, "%s/%s", MODULES_DIR, file);
+#endif
+
+	if (stat(path, &st) || !(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
+#ifndef __ANDROID__
+		tst_resm(TWARN, "expected file %s does not exist or not a file", path);
+#endif
+		return -1;
+	}
+
+	if (access(path, R_OK)) {
+#ifndef __ANDROID__
+		tst_resm(TWARN, "file %s cannot be read", path);
+#endif
+		return -1;
+	}
+
+	strcat(search, driver);
+	strcat(search, ".ko");
+
+	f = SAFE_FOPEN(NULL, path, "r");
+
+	while (fgets(buf, sizeof(buf), f)) {
+		if (sscanf(buf, "%s", module) != 1)
+			continue;
+
+		if (strstr(module, search) != NULL) {
+			SAFE_FCLOSE(NULL, f);
+			return 0;
+		}
+	}
+
+	SAFE_FCLOSE(NULL, f);
+
+	return -1;
+}
+
+int tst_check_driver(const char *driver)
+{
+#ifdef __ANDROID__
+	/*
+	 * Android may not have properly installed modules.* files to determine
+	 * built-in drivers. Assume all drivers are available.
 	 */
 	return 0;
 #endif
+
+	if (!tst_search_driver(driver, "modules.dep") ||
+		!tst_search_driver(driver, "modules.builtin"))
+		return 0;
+
+	return 1;
 }
Signature · 1 line
2.29.2
author
#1 lore raw

PATCH v2 1/2 lib: Fix kernel module detection on BusyBox

Hi,

Quoted 1 line
lib/tst_kernel.c +0 -0

...

lib/tst_kernel.c int tst_kernel_bits(void) · line 92

Quoted 6 lines
lib/tst_kernel.c +5 -1
-int tst_check_driver(const char *name)
+#ifndef __ANDROID__
+# define MODULES_DIR "/lib/modules"
+#else
+# define MODULES_DIR "/system/lib/modules"
+#endif

OK, MODULES_DIR is not needed now as I kept Android skipped (unless somebody contributes code or share algorithm for Android).

Kind regards,
Petr

#2 lore raw

PATCH v2 1/2 lib: Fix kernel module detection on BusyBox

Hi,

Quoted 4 lines · Petr Vorel

Hi,

Quoted 1 line
lib/tst_kernel.c +0 -0

...

Quoted 8 lines

lib/tst_kernel.c int tst_kernel_bits(void) · line 92

Quoted 6 lines
lib/tst_kernel.c +5 -1
-int tst_check_driver(const char *name)
+#ifndef __ANDROID__
+# define MODULES_DIR "/lib/modules"
+#else
+# define MODULES_DIR "/system/lib/modules"
+#endif

OK, MODULES_DIR is not needed now as I kept Android skipped (unless somebody contributes code or share algorithm for Android).

I don't get this comment. MODULES_DIR is used in both code paths in tst_search_driver.
But you don't call it from tst_check_driver only if it is not android. If tst_search_driver is supposed to be a new public interface, it should be added to the header,
otherwise it should be marked static.

J?rg

author
#3 lore raw

PATCH v2 1/2 lib: Fix kernel module detection on BusyBox

Hi J?rg,

Quoted 15 lines

Hi,

Quoted 4 lines · Petr Vorel

Hi,

Quoted 1 line
lib/tst_kernel.c +0 -0

...

Quoted 8 lines

lib/tst_kernel.c int tst_kernel_bits(void) · line 92

Quoted 6 lines
lib/tst_kernel.c +5 -1
-int tst_check_driver(const char *name)
+#ifndef __ANDROID__
+# define MODULES_DIR "/lib/modules"
+#else
+# define MODULES_DIR "/system/lib/modules"
+#endif

OK, MODULES_DIR is not needed now as I kept Android skipped (unless somebody contributes code or share algorithm for Android).

Quoted 2 lines

I don't get this comment. MODULES_DIR is used in both code paths in tst_search_driver.

Please have look at v3, where I removed MODULES_DIR
https://patchwork.ozlabs.org/project/ltp/patch/[email protected]/

Quoted 4 lines

But you don't call it from tst_check_driver only if it is not android. If tst_search_driver is supposed to be a new public interface, it should be added to the header,
otherwise it should be marked static.

+1 I'll make it static.

Kind regards,
Petr

Quoted 1 line

J?rg

#4 lore raw

PATCH v2 1/2 lib: Fix kernel module detection on BusyBox

In reply to Petr Vorel

Hi!

Quoted 55 lines

#include <sys/personality.h>
#include <sys/utsname.h>

lib/tst_kernel.c +31 -9
+#include <limits.h>
+
 #include "test.h"
 #include "tst_kernel.h"
+#include "old_safe_stdio.h"
 
 static int get_kernel_bits_from_uname(struct utsname *buf)
 {
@@ -81,20 +85,79 @@ int tst_kernel_bits(void)
 	return kernel_bits;
 }
 
-int tst_check_driver(const char *name)
+#ifndef __ANDROID__
+# define MODULES_DIR "/lib/modules"
+#else
+# define MODULES_DIR "/system/lib/modules"
+#endif
+
+
+int tst_search_driver(const char *driver, const char *file)
 {
+	struct stat st;
+	char *path = NULL;
+	char buf[PATH_MAX], module[PATH_MAX], search[PATH_MAX] = "/";
+	FILE *f;
+
 #ifndef __ANDROID__
-	const char * const argv[] = { "modprobe", "-n", name, NULL };
-	int res = tst_cmd_(NULL, argv, "/dev/null", "/dev/null",
-			       TST_CMD_PASS_RETVAL);
+	struct utsname uts;
 
-	/* 255 - it looks like modprobe not available */
-	return (res == 255) ? 0 : res;
+	if (uname(&uts)) {
+		tst_brkm(TBROK | TERRNO, NULL, "uname() failed");
+		return -1;
+	}
+	SAFE_ASPRINTF(NULL, &path, "%s/%s/%s", MODULES_DIR, uts.release, file);
 #else
-	/* Android modprobe may not have '-n', or properly installed
-	 * module.*.bin files to determine built-in drivers. Assume
-	 * all drivers are available.
+	SAFE_ASPRINTF(NULL, &path, "%s/%s", MODULES_DIR, file);
+#endif
+
+	if (stat(path, &st) || !(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
+#ifndef __ANDROID__
+		tst_resm(TWARN, "expected file %s does not exist or not a file", path);
+#endif
+		return -1;
+	}

lib/tst_kernel.c int tst_kernel_bits(void) · line 129

Quoted 10 lines
lib/tst_kernel.c +10 -0
+
+	if (access(path, R_OK)) {
+#ifndef __ANDROID__
+		tst_resm(TWARN, "file %s cannot be read", path);
+#endif
+		return -1;
+	}
+
+	strcat(search, driver);
+	strcat(search, ".ko");

Why not just snprintf() or SAFE_ASPRINTF() here as well?

lib/tst_kernel.c int tst_kernel_bits(void) · line 137

Quoted 7 lines
lib/tst_kernel.c +7 -0
+	f = SAFE_FOPEN(NULL, path, "r");
+
+	while (fgets(buf, sizeof(buf), f)) {
+		if (sscanf(buf, "%s", module) != 1)
+			continue;
+
+		if (strstr(module, search) != NULL) {

And I'm not sure that this is safe either, what about the case that one module name is a substring of another.

E.g. if we look for "foo.ko" and the file contains "this_is_not_foo.ko" it will still match here.

Also this seems to be rather distruptive change, so I guess it would be safer to apply after the release.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#5 lore raw

PATCH v2 1/2 lib: Fix kernel module detection on BusyBox

Hi Cyril,

Quoted 1 line
Quoted 1 line
lib/tst_kernel.c +1 -0
+	char buf[PATH_MAX], module[PATH_MAX], search[PATH_MAX] = "/";

...

Quoted 4 lines

lib/tst_kernel.c int tst_kernel_bits(void) · line 129

Quoted 2 lines
lib/tst_kernel.c +2 -0
+	strcat(search, driver);
+	strcat(search, ".ko");

Why not just snprintf() or SAFE_ASPRINTF() here as well?

+1

Quoted 11 lines

lib/tst_kernel.c int tst_kernel_bits(void) · line 137

Quoted 7 lines
lib/tst_kernel.c +7 -0
+	f = SAFE_FOPEN(NULL, path, "r");
+
+	while (fgets(buf, sizeof(buf), f)) {
+		if (sscanf(buf, "%s", module) != 1)
+			continue;
+
+		if (strstr(module, search) != NULL) {

And I'm not sure that this is safe either, what about the case that one module name is a substring of another.

Quoted 2 lines

E.g. if we look for "foo.ko" and the file contains "this_is_not_foo.ko" it will still match here.

char search[PATH_MAX] = "/"; => we search for "/foo.ko" But that will be more obvious when I use SAFE_ASPRINTF() for search.

Quoted 2 lines

Also this seems to be rather distruptive change, so I guess it would be safer to apply after the release.

I'll send v3, but no problem to postpone it.

But I'll revert 305a78e4c ("tst_net.sh: Require veth for netns") with explanation that it wait for this fix, ok?

Kind regards,
Petr