kmodloader: modprobe: return 0 for loaded modules
[project/ubox.git] / kmodloader.c
index bcb213b..ed8f833 100644 (file)
@@ -48,8 +48,6 @@ struct module {
        char *name;
        char *depends;
        char *opts;
        char *name;
        char *depends;
        char *opts;
-       char **aliases;
-       int naliases;
 
        int size;
        int usage;
 
        int size;
        int usage;
@@ -250,16 +248,11 @@ alloc_module(const char *name, const char * const *aliases, int naliases, const
 {
        struct module *m;
        char *_name, *_dep;
 {
        struct module *m;
        char *_name, *_dep;
-       char **_aliases;
-       int i, len_aliases;
+       int i;
 
 
-       len_aliases = naliases * sizeof(aliases[0]);
-       for (i = 0; i < naliases; i++)
-               len_aliases += strlen(aliases[i]) + 1;
        m = calloc_a(sizeof(*m),
                &_name, strlen(name) + 1,
        m = calloc_a(sizeof(*m),
                &_name, strlen(name) + 1,
-               &_dep, depends ? strlen(depends) + 2 : 0,
-               &_aliases, len_aliases);
+               &_dep, depends ? strlen(depends) + 2 : 0);
        if (!m)
                return NULL;
 
        if (!m)
                return NULL;
 
@@ -275,28 +268,11 @@ alloc_module(const char *name, const char * const *aliases, int naliases, const
                }
        }
        m->size = size;
                }
        }
        m->size = size;
-       m->naliases = naliases;
-       if (naliases == 0)
-               m->aliases = NULL;
-       else {
-               char *ptr = (char *)_aliases + naliases * sizeof(_aliases[0]);
-               int len;
-
-               i = 0;
-               do {
-                       len = strlen(aliases[i]) + 1;
-                       memcpy(ptr, aliases[i], len);
-                       _aliases[i] = ptr;
-                       ptr += len;
-                       i++;
-               } while (i < naliases);
-               m->aliases = _aliases;
-       }
 
        m->refcnt = 0;
        alloc_module_node(m->name, m, false);
 
        m->refcnt = 0;
        alloc_module_node(m->name, m, false);
-       for (i = 0; i < m->naliases; i++)
-               alloc_module_node(m->aliases[i], m, true);
+       for (i = 0; i < naliases; i++)
+               alloc_module_node(aliases[i], m, true);
 
        return m;
 }
 
        return m;
 }
@@ -574,8 +550,11 @@ static int insert_module(char *path, const char *options)
        }
 
        data = malloc(s.st_size);
        }
 
        data = malloc(s.st_size);
-       if (read(fd, data, s.st_size) == s.st_size)
+       if (read(fd, data, s.st_size) == s.st_size) {
                ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
                ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
+               if (errno == EEXIST)
+                       ret = 0;
+       }
        else
                ULOG_ERR("failed to read full module %s\n", path);
 
        else
                ULOG_ERR("failed to read full module %s\n", path);
 
@@ -657,6 +636,13 @@ static int print_insmod_usage(void)
        return -1;
 }
 
        return -1;
 }
 
+static int print_modprobe_usage(void)
+{
+       ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
+
+       return -1;
+}
+
 static int print_usage(char *arg)
 {
        ULOG_INFO("Usage:\n\t%s module\n", arg);
 static int print_usage(char *arg)
 {
        ULOG_INFO("Usage:\n\t%s module\n", arg);
@@ -821,15 +807,24 @@ static int main_modprobe(int argc, char **argv)
        struct module *m;
        char *name;
        char *mod = NULL;
        struct module *m;
        char *name;
        char *mod = NULL;
-       int i;
+       int opt;
+       bool quiet = false;
+
+       while ((opt = getopt(argc, argv, "q")) != -1 ) {
+               switch (opt) {
+                       case 'q': /* shhhh! */
+                               quiet = true;
+                               break;
+                       default: /* '?' */
+                               return print_modprobe_usage();
+                               break;
+                       }
+       }
 
 
-       for (i = 1; i < argc; i++)
-               if (argv[i][0] != '-') {
-                       mod = argv[i];
-                       break;
-               }
-       if (!mod)
-               return print_usage("modprobe");
+       if (optind >= argc)
+               return print_modprobe_usage(); /* expected module after options */
+
+       mod = argv[optind];
 
        if (scan_module_folders())
                return -1;
 
        if (scan_module_folders())
                return -1;
@@ -840,10 +835,13 @@ static int main_modprobe(int argc, char **argv)
        name = get_module_name(mod);
        m = find_module(name);
        if (m && m->state == LOADED) {
        name = get_module_name(mod);
        m = find_module(name);
        if (m && m->state == LOADED) {
-               ULOG_ERR("%s is already loaded\n", name);
-               return -1;
+               if (!quiet)
+                       ULOG_ERR("%s is already loaded\n", name);
+               return 0;
        } else if (!m) {
        } else if (!m) {
-               ULOG_ERR("failed to find a module named %s\n", name);
+               if (!quiet)
+                       ULOG_ERR("failed to find a module named %s\n", name);
+               return -1;
        } else {
                int fail;
 
        } else {
                int fail;
 
@@ -963,20 +961,23 @@ out:
        return 0;
 }
 
        return 0;
 }
 
+static inline char weight(char c)
+{
+       return c == '_' ? '-' : c;
+}
+
 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
 {
        const char *s1 = k1;
        const char *s2 = k2;
 
 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 == '_'))))
+       while (*s1 && (weight(*s1) == weight(*s2)))
        {
                s1++;
                s2++;
        }
 
        {
                s1++;
                s2++;
        }
 
-       return *(const unsigned char *)s1 - *(const unsigned char *)s2;
+       return (unsigned char)weight(*s1) - (unsigned char)weight(*s2);
 }
 
 int main(int argc, char **argv)
 }
 
 int main(int argc, char **argv)