Timo Teräs wrote:
> Harald Küthe wrote:
>>> Could you put the .o searching code to insmod.c instead of modutils-24.c.
>>> modutils-24.c is inteded to be only and only the module loader code for
>>> 2.4 kernels. insmod.c is the applet that calls either the 2.6 syscall
>>> or modutils-24.c depending on running kernel; thus your changes would
>>> not work on 2.6 kernels.
>> Here it is (including the vector-alloc stuff).
>> Be aware that I do not really know what I'm doing :-). Just backporting
>> the old implementation. But it works here for kernel 2.4.
> 
> Right. The modutils-24.c stuff seems to be already in SVN. And the .o
> searching code looks ok, but is a bit bloaty. I can try to clean it up
> and resend.

Ok, this is what I came up with so far.

I originally wrote the modutils replacement in accordance with
module-init-tools. In module-init-tools insmod does no attempt to search
the module from directories. But since bb used to do this, I guess we
should keep the behavior.

- Timo

Index: modutils/insmod.c
===================================================================
--- modutils/insmod.c	(revision 24112)
+++ modutils/insmod.c	(working copy)
@@ -7,13 +7,59 @@
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
+#include <sys/utsname.h>
 #include "libbb.h"
 #include "modutils.h"
 
+struct search_data {
+	char *pattern;
+	char *options;
+};
+
+static int FAST_FUNC check_module_name_match(const char *filename,
+		struct stat *statbuf UNUSED_PARAM,
+		void *userdata, int depth UNUSED_PARAM)
+{
+	struct search_data *sd = (struct search_data *) userdata;
+	char *bn;
+	int rc;
+
+	bn = bb_get_last_path_component_nostrip(filename);
+	if (strncmp(bn, sd->pattern, strlen(sd->pattern)) == 0 &&
+	    bn[strlen(sd->pattern)] == '.') {
+		/* Match, load this module */
+		rc = bb_init_module(filename, sd->options);
+		if (rc)
+			bb_error_msg("cannot insert '%s': %s",
+				     filename, moderror(rc));
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
+static int try_dir(char *path, struct search_data *sd)
+{
+	char *rpath;
+	int r;
+
+	rpath = xmalloc_readlink(path);
+	r = recursive_action(rpath ? rpath : path, ACTION_RECURSE,
+			     check_module_name_match, NULL, sd, 0);
+	if (ENABLE_FEATURE_CLEAN_UP) {
+		if (rpath)
+			free(rpath);
+		free(path);
+	}
+	return r;
+}
+
 int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int insmod_main(int argc UNUSED_PARAM, char **argv)
 {
-	char *filename;
+	struct search_data sd;
+	char *filename, *ext;
+	struct utsname uts;
 	int rc;
 
 	USE_FEATURE_2_4_MODULES(
@@ -25,9 +71,30 @@
 	if (!filename)
 		bb_show_usage();
 
-	rc = bb_init_module(filename, parse_cmdline_module_options(argv));
-	if (rc)
-		bb_error_msg("cannot insert '%s': %s", filename, moderror(rc));
+	sd.options = parse_cmdline_module_options(argv);
 
+	/* Try opening without searching */
+	rc = bb_init_module(filename, sd.options);
+	if (rc == 0)
+		return 0;
+
+	/* File not found? Try searching it a bit harder */
+	sd.pattern = strdup(filename);
+	ext = strchr(sd.pattern, '.');
+	if (ext &&
+	    (strcmp(ext, ".ko") == 0 ||
+	     (ENABLE_FEATURE_2_4_MODULES && strcmp(ext, ".o") == 0)))
+		*ext = 0;
+
+	uname(&uts);
+	rc = try_dir(concat_path_file(CONFIG_DEFAULT_MODULES_DIR, uts.release), &sd);
+	if (rc != 0)
+		rc = try_dir(xstrdup(CONFIG_DEFAULT_MODULES_DIR), &sd);
+	if (rc != 0)
+		bb_error_msg("cannot locate module '%s'", filename);
+
+	if (ENABLE_FEATURE_CLEAN_UP)
+		free(sd.pattern);
+
 	return rc;
 }
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to