kmodloader: use the name of the found module struct for modinfo
[project/ubox.git] / kmodloader.c
index f45f3dc..623a169 100644 (file)
@@ -79,24 +79,6 @@ static void free_modules(void)
                free(m);
 }
 
-static void replace_dash(char *s)
-{
-       while (s && *s) {
-               if (*s == '-')
-                       *s = '_';
-               s++;
-       }
-}
-
-static void replace_underscore(char *s)
-{
-       while (s && *s) {
-               if (*s == '_')
-                       *s = '-';
-               s++;
-       }
-}
-
 static char* get_module_path(char *name)
 {
        static char path[256];
@@ -108,13 +90,6 @@ static char* get_module_path(char *name)
 
        uname(&ver);
        snprintf(path, 256, "%s" DEF_MOD_PATH "%s.ko", prefix, ver.release, name);
-       replace_dash(path);
-
-       if (!stat(path, &s))
-               return path;
-
-       snprintf(path, 256, "%s" DEF_MOD_PATH "%s.ko", prefix, ver.release, name);
-       replace_underscore(path);
 
        if (!stat(path, &s))
                return path;
@@ -132,7 +107,6 @@ static char* get_module_name(char *path)
        t = strstr(name, ".ko");
        if (t)
                *t = '\0';
-        replace_dash(name);
 
        return name;
 }
@@ -196,11 +170,9 @@ alloc_module(const char *name, const char *depends, int size)
                return NULL;
 
        m->avl.key = m->name = strcpy(_name, name);
-       replace_dash(_name);
 
        if (depends) {
                m->depends = strcpy(_dep, depends);
-               replace_dash(_dep);
                while (*_dep) {
                        if (*_dep == ',')
                                *_dep = '\0';
@@ -395,7 +367,7 @@ static int deps_available(struct module *m, int verbose)
        char *dep;
        int err = 0;
 
-       if (!strcmp(m->depends, "_") || !strcmp(m->depends, ""))
+       if (!strcmp(m->depends, "-") || !strcmp(m->depends, ""))
                return 0;
 
        dep = m->depends;
@@ -449,7 +421,7 @@ static void load_moddeps(struct module *_m)
        char *dep;
        struct module *m;
 
-       if (!strcmp(_m->depends, "_") || !strcmp(_m->depends, ""))
+       if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
                return;
 
        dep = _m->depends;
@@ -581,13 +553,13 @@ static int main_rmmod(int argc, char **argv)
                LOG("module is not loaded\n");
                return -1;
        }
-       free_modules();
-
-       ret = syscall(__NR_delete_module, name, 0);
+       ret = syscall(__NR_delete_module, m->name, 0);
 
        if (ret)
                LOG("unloading the module failed\n");
 
+       free_modules();
+
        return ret;
 }
 
@@ -611,18 +583,29 @@ static int main_lsmod(int argc, char **argv)
 
 static int main_modinfo(int argc, char **argv)
 {
-       char *module;
+       struct module *m;
+       char *name;
 
        if (argc != 2)
                return print_usage("modinfo");
 
-       module = get_module_path(argv[1]);
-       if (!module) {
+       if (scan_module_folder())
+               return -1;
+
+       name = get_module_name(argv[1]);
+       m = find_module(name);
+       if (!m) {
                LOG("cannot find module - %s\n", argv[1]);
                return -1;
        }
 
-       print_modinfo(module);
+       name = get_module_path(m->name);
+       if (!name) {
+               LOG("cannot find path of module - %s\n", m->name);
+               return -1;
+       }
+
+       print_modinfo(name);
 
        return 0;
 }
@@ -754,11 +737,27 @@ out:
        return 0;
 }
 
+static int avl_modcmp(const void *k1, const void *k2, void *ptr)
+{
+       const char *s1 = k1;
+       const char *s2 = k2;
+
+       while (*s1 && ((*s1 == *s2) ||
+                      ((*s1 == '_') && (*s2 == '-')) ||
+                      ((*s1 == '-') && (*s2 == '_'))))
+       {
+               s1++;
+               s2++;
+       }
+
+       return *(const unsigned char *)s1 - *(const unsigned char *)s2;
+}
+
 int main(int argc, char **argv)
 {
        char *exec = basename(*argv);
 
-       avl_init(&modules, avl_strcmp, false, NULL);
+       avl_init(&modules, avl_modcmp, false, NULL);
        if (!strcmp(exec, "insmod"))
                return main_insmod(argc, argv);