kmodloader: fix insmod path logic
[project/ubox.git] / kmodloader.c
index 2cb6088c1fe21f0cb28e641a93244cb2b4265eb4..8fd989f189933ba4d471fd506a9d6d76e3966936 100644 (file)
@@ -132,6 +132,17 @@ static int init_module_folders(void)
        return 0;
 }
 
+static void free_module_folders(void)
+{
+       int n = 0;
+
+       if (!module_folders)
+               return;
+       while (module_folders[n])
+               free(module_folders[n++]);
+       free(module_folders);
+}
+
 static struct module *find_module(const char *name)
 {
        struct module_node *mn;
@@ -271,12 +282,16 @@ alloc_module_node(const char *name, struct module *m, bool is_alias)
                mn->avl.key = strcpy(_name, name);
                mn->m = m;
                mn->is_alias = is_alias;
-               avl_insert(&modules, &mn->avl);
-               m->refcnt += 1;
+               if (avl_insert(&modules, &mn->avl) == 0)
+                       m->refcnt += 1;
+               else
+                       free(mn);
        }
        return mn;
 }
 
+static int avl_modcmp(const void *k1, const void *k2, void *ptr);
+
 static struct module *
 alloc_module(const char *name, const char * const *aliases, int naliases, const char *depends, int size)
 {
@@ -306,7 +321,8 @@ alloc_module(const char *name, const char * const *aliases, int naliases, const
        m->refcnt = 0;
        alloc_module_node(m->name, m, false);
        for (i = 0; i < naliases; i++)
-               alloc_module_node(aliases[i], m, true);
+               if (avl_modcmp(m->name, aliases[i], NULL))
+                       alloc_module_node(aliases[i], m, true);
 
        return m;
 }
@@ -437,12 +453,13 @@ static struct module* get_module_info(const char *module, const char *name)
 
        strings = map + offset;
        while (true) {
+               char *end = map + offset + size;
                char *sep;
                int len;
 
-               while (!strings[0])
+               while ((strings < end) && !strings[0])
                        strings++;
-               if (strings >= map + offset + size)
+               if (strings >= end)
                        break;
                if (is_builtin) {
                        sep = strstr(strings, ".");
@@ -502,11 +519,16 @@ static int scan_builtin_modules(void)
                return -1;
        for (p = module_folders; *p; p++) {
                snprintf(path, sizeof(path), "%s%s", *p, MOD_BUILTIN);
-               if (!stat(path, &st) && S_ISREG(st.st_mode)) {
-                       fp = fopen(path, "r");
-                       if (fp)
-                               break;
-               }
+               fp = fopen(path, "r");
+               if (!fp)
+                       continue;
+
+               if (!fstat(fileno(fp), &st) && S_ISREG(st.st_mode))
+                       break;
+
+               /* Not regular file, close it and check next */
+               fclose(fp);
+               fp = NULL;
        }
        if (!fp)
                return 0;       /* OK if modules.builtin unavailable */
@@ -619,13 +641,14 @@ static int print_modinfo(const struct module *m)
                printf("name:\t\t%s\n", m->name);
        printf("filename:\t%s\n", is_builtin ? "(builtin)" : mpath);
        while (true) {
+               char *end = map + offset + size;
                char *pname, *pdata;
                char *dup = NULL;
                char *sep, *sep2;
 
-               while (!strings[0])
+               while ((strings < end) && !strings[0])
                        strings++;
-               if (strings >= map + offset + size)
+               if (strings >= end)
                        break;
                if (is_builtin) {
                        sep = strstr(strings, ".");
@@ -649,8 +672,11 @@ static int print_modinfo(const struct module *m)
                                printf("%s:\t%s\n",  dup, sep);
                } else {
                        sep2 = strstr(sep, ":");
-                       if (!sep2)
+                       if (!sep2) {
+                               free(dup);
                                break;
+                       }
+
                        pname = strndup(sep, sep2 - sep);
                        sep2++;
                        pdata = strdup(sep2);
@@ -673,10 +699,10 @@ static int print_modinfo(const struct module *m)
                        else
                                p->desc = pdata;
                }
+
+               free(dup);
 next_string:
                strings = &sep[strlen(sep)];
-               if (dup)
-                       free(dup);
        }
 
        list_for_each_entry(p, &params, list) {
@@ -871,8 +897,8 @@ static int print_usage(char *arg)
 
 static int main_insmod(int argc, char **argv)
 {
-       char *name, *cur, *options;
-       int i, ret, len;
+       char *name, *path, *cur, *opts = NULL;
+       int i, ret = -1, len;
 
        if (argc < 2)
                return print_insmod_usage();
@@ -888,26 +914,23 @@ static int main_insmod(int argc, char **argv)
 
        if (find_module(name)) {
                ULOG_ERR("module is already loaded - %s\n", name);
-               return -1;
+               goto err;
 
        }
 
-       free_modules();
-
-       for (len = 0, i = 2; i < argc; i++)
+       for (len = 1, i = 2; i < argc; i++)
                len += strlen(argv[i]) + 1;
 
-       options = malloc(len);
-       if (!options) {
+       opts = malloc(len);
+       if (!opts) {
                ULOG_ERR("out of memory\n");
-               ret = -1;
                goto err;
        }
 
-       options[0] = 0;
-       cur = options;
+       opts[0] = 0;
+       cur = opts;
        for (i = 2; i < argc; i++) {
-               if (options[0]) {
+               if (opts[0]) {
                        *cur = ' ';
                        cur++;
                }
@@ -916,24 +939,24 @@ static int main_insmod(int argc, char **argv)
 
        if (init_module_folders()) {
                fprintf(stderr, "Failed to find the folder holding the modules\n");
-               ret = -1;
                goto err;
        }
 
-       if (get_module_path(argv[1])) {
-               name = argv[1];
-       } else if (!get_module_path(name)) {
+       if (!((path = get_module_path(argv[1])) ||
+             (path = get_module_path(name)))) {
                fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
-               ret = -1;
                goto err;
        }
 
-       ret = insert_module(get_module_path(name), options);
+       ret = insert_module(path, opts);
        if (ret)
                ULOG_ERR("failed to insert %s\n", get_module_path(name));
 
 err:
-       free(options);
+       free(opts);
+       free_modules();
+       free_module_folders();
+
        return ret;
 }
 
@@ -941,7 +964,7 @@ static int main_rmmod(int argc, char **argv)
 {
        struct module *m;
        char *name;
-       int ret;
+       int ret = -1;
 
        if (argc != 2)
                return print_usage("rmmod");
@@ -956,18 +979,20 @@ static int main_rmmod(int argc, char **argv)
        m = find_module(name);
        if (!m) {
                ULOG_ERR("module is not loaded\n");
-               return -1;
+               goto err;
        }
        if (m->state == BUILTIN) {
                ULOG_ERR("module is builtin\n");
-               return -1;
+               goto err;
        }
        ret = syscall(__NR_delete_module, m->name, 0);
 
        if (ret)
                ULOG_ERR("unloading the module failed\n");
 
+err:
        free_modules();
+       free_module_folders();
 
        return ret;
 }
@@ -1008,7 +1033,8 @@ static int main_lsmod(int argc, char **argv)
 
 static int main_modinfo(int argc, char **argv)
 {
-       struct module *m;
+       struct module_node *mn;
+       int rv = -1;
        char *name;
 
        if (argc != 2)
@@ -1021,15 +1047,25 @@ static int main_modinfo(int argc, char **argv)
                return -1;
 
        name = get_module_name(argv[1]);
-       m = find_module(name);
-       if (!m) {
+       mn = avl_find_element(&modules, name, mn, avl);
+       if (!mn) {
                ULOG_ERR("cannot find module - %s\n", argv[1]);
-               return -1;
+               goto err;
        }
 
-       print_modinfo(m);
+       if (!mn->avl.leader)
+               print_modinfo(mn->m);
+       else
+               do {
+                       print_modinfo(mn->m);
+                       mn = (struct module_node *) mn->avl.list.next;
+               } while (!avl_modcmp(name, mn->avl.key, NULL));
+       rv = 0;
+err:
+       free_modules();
+       free_module_folders();
 
-       return 0;
+       return rv;
 }
 
 static int main_modprobe(int argc, char **argv)
@@ -1118,6 +1154,7 @@ static int main_modprobe(int argc, char **argv)
        }
 
        free_modules();
+       free_module_folders();
 
        return exit_code;
 }
@@ -1230,6 +1267,8 @@ static int main_loader(int argc, char **argv)
 
 out:
        globfree(&gl);
+       free_modules();
+       free_module_folders();
 free_path:
        free(path);
 
@@ -1335,7 +1374,7 @@ int main(int argc, char **argv)
 {
        char *exec = basename(*argv);
 
-       avl_init(&modules, avl_modcmp, false, NULL);
+       avl_init(&modules, avl_modcmp, true, NULL);
        if (!strcmp(exec, "insmod"))
                return main_insmod(argc, argv);