1a63c9810a99e30009ef9a85c39dba6a65fb3264
[project/ubox.git] / kmodloader.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #define _GNU_SOURCE
16 #include <sys/syscall.h>
17 #include <sys/mman.h>
18 #include <sys/utsname.h>
19
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <sys/syscall.h>
23 #include <sys/types.h>
24 #include <values.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <libgen.h>
31 #include <glob.h>
32 #include <elf.h>
33
34 #include <libubox/avl.h>
35 #include <libubox/avl-cmp.h>
36 #include <libubox/utils.h>
37 #include <libubox/ulog.h>
38
39 #define DEF_MOD_PATH "/modules/%s/"
40
41 enum {
42         SCANNED,
43         PROBE,
44         LOADED,
45 };
46
47 struct module {
48         char *name;
49         char *depends;
50         char *opts;
51
52         int size;
53         int usage;
54         int state;
55         int error;
56         int refcnt;                     /* number of references from module_node.m */
57 };
58
59 struct module_node {
60         struct avl_node avl;
61         struct module *m;
62         bool is_alias;
63 };
64
65 static struct avl_tree modules;
66
67 static char **module_folders = NULL;
68
69 static void free_module(struct module *m);
70
71 static int init_module_folders(void)
72 {
73         int n = 0;
74         struct stat st;
75         struct utsname ver;
76         char *s, *e, *p, path[256], ldpath[256];
77
78         e = ldpath;
79         s = getenv("LD_LIBRARY_PATH");
80
81         if (s)
82                 e += snprintf(ldpath, sizeof(ldpath), "%s:", s);
83
84         e += snprintf(e, sizeof(ldpath) - (e - ldpath), "/lib");
85
86         uname(&ver);
87
88         for (s = p = ldpath; p <= e; p++) {
89                 if (*p != ':' && *p != '\0')
90                         continue;
91
92                 *p = 0;
93                 snprintf(path, sizeof(path), "%s" DEF_MOD_PATH, s, ver.release);
94
95                 if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
96                         module_folders = realloc(module_folders, sizeof(p) * (n + 2));
97
98                         if (!module_folders)
99                                 return -1;
100
101                         module_folders[n++] = strdup(path);
102                 }
103
104                 s = p + 1;
105         }
106
107         if (!module_folders)
108                 return -1;
109
110         module_folders[n] = NULL;
111         return 0;
112 }
113
114 static struct module *find_module(const char *name)
115 {
116         struct module_node *mn;
117         mn = avl_find_element(&modules, name, mn, avl);
118         if (mn)
119                 return mn->m;
120         else
121                 return NULL;
122 }
123
124 static void free_modules(void)
125 {
126         struct module_node *mn, *tmp;
127
128         avl_remove_all_elements(&modules, mn, avl, tmp) {
129                 struct module *m = mn->m;
130
131                 m->refcnt -= 1;
132                 if (m->refcnt == 0)
133                         free_module(m);
134                 free(mn);
135         }
136 }
137
138 static char* get_module_path(char *name)
139 {
140         char **p;
141         static char path[256];
142         struct stat s;
143
144         if (!stat(name, &s) && S_ISREG(s.st_mode))
145                 return name;
146
147         for (p = module_folders; *p; p++) {
148                 snprintf(path, sizeof(path), "%s%s.ko", *p, name);
149                 if (!stat(path, &s) && S_ISREG(s.st_mode))
150                         return path;
151         }
152
153         return NULL;
154 }
155
156 static char* get_module_name(char *path)
157 {
158         static char name[33];
159         char *t;
160
161         strncpy(name, basename(path), sizeof(name) - 1);
162
163         t = strstr(name, ".ko");
164         if (t)
165                 *t = '\0';
166
167         return name;
168 }
169
170 static int elf64_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
171 {
172         const char *secnames;
173         Elf64_Ehdr *e;
174         Elf64_Shdr *sh;
175         int i;
176
177         e = (Elf64_Ehdr *) map;
178         sh = (Elf64_Shdr *) (map + e->e_shoff);
179
180         secnames = map + sh[e->e_shstrndx].sh_offset;
181         for (i = 0; i < e->e_shnum; i++) {
182                 if (!strcmp(section, secnames + sh[i].sh_name)) {
183                         *size = sh[i].sh_size;
184                         *offset = sh[i].sh_offset;
185                         return 0;
186                 }
187         }
188
189         return -1;
190 }
191
192 static int elf32_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
193 {
194         const char *secnames;
195         Elf32_Ehdr *e;
196         Elf32_Shdr *sh;
197         int i;
198
199         e = (Elf32_Ehdr *) map;
200         sh = (Elf32_Shdr *) (map + e->e_shoff);
201
202         secnames = map + sh[e->e_shstrndx].sh_offset;
203         for (i = 0; i < e->e_shnum; i++) {
204                 if (!strcmp(section, secnames + sh[i].sh_name)) {
205                         *size = sh[i].sh_size;
206                         *offset = sh[i].sh_offset;
207                         return 0;
208                 }
209         }
210
211         return -1;
212 }
213
214 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
215 {
216         int clazz = map[EI_CLASS];
217
218         if (clazz == ELFCLASS32)
219                 return elf32_find_section(map, section, offset, size);
220         else if (clazz == ELFCLASS64)
221                 return elf64_find_section(map, section, offset, size);
222
223         ULOG_ERR("unknown elf format %d\n", clazz);
224
225         return -1;
226 }
227
228 static struct module_node *
229 alloc_module_node(const char *name, struct module *m, bool is_alias)
230 {
231         struct module_node *mn;
232         char *_name;
233
234         mn = calloc_a(sizeof(*mn),
235                 &_name, strlen(name) + 1);
236         if (mn) {
237                 mn->avl.key = strcpy(_name, name);
238                 mn->m = m;
239                 mn->is_alias = is_alias;
240                 avl_insert(&modules, &mn->avl);
241                 m->refcnt += 1;
242         }
243         return mn;
244 }
245
246 static struct module *
247 alloc_module(const char *name, const char * const *aliases, int naliases, const char *depends, int size)
248 {
249         struct module *m;
250         char *_name, *_dep;
251         int i;
252
253         m = calloc_a(sizeof(*m),
254                 &_name, strlen(name) + 1,
255                 &_dep, depends ? strlen(depends) + 2 : 0);
256         if (!m)
257                 return NULL;
258
259         m->name = strcpy(_name, name);
260         m->opts = 0;
261
262         if (depends) {
263                 m->depends = strcpy(_dep, depends);
264                 while (*_dep) {
265                         if (*_dep == ',')
266                                 *_dep = '\0';
267                         _dep++;
268                 }
269         }
270         m->size = size;
271
272         m->refcnt = 0;
273         alloc_module_node(m->name, m, false);
274         for (i = 0; i < naliases; i++)
275                 alloc_module_node(aliases[i], m, true);
276
277         return m;
278 }
279
280 static void free_module(struct module *m)
281 {
282         if (m->opts)
283                 free(m->opts);
284         free(m);
285 }
286
287 static int scan_loaded_modules(void)
288 {
289         size_t buf_len = 0;
290         char *buf = NULL;
291         FILE *fp;
292
293         fp = fopen("/proc/modules", "r");
294         if (!fp) {
295                 ULOG_ERR("failed to open /proc/modules\n");
296                 return -1;
297         }
298
299         while (getline(&buf, &buf_len, fp) > 0) {
300                 struct module m;
301                 struct module *n;
302
303                 m.name = strtok(buf, " ");
304                 m.size = atoi(strtok(NULL, " "));
305                 m.usage = atoi(strtok(NULL, " "));
306                 m.depends = strtok(NULL, " ");
307
308                 if (!m.name || !m.depends)
309                         continue;
310
311                 n = find_module(m.name);
312                 if (!n) {
313                         /* possibly a module outside /lib/modules/<ver>/ */
314                         n = alloc_module(m.name, NULL, 0, m.depends, m.size);
315                 }
316                 n->usage = m.usage;
317                 n->state = LOADED;
318         }
319         free(buf);
320         fclose(fp);
321
322         return 0;
323 }
324
325 static struct module* get_module_info(const char *module, const char *name)
326 {
327         int fd = open(module, O_RDONLY);
328         unsigned int offset, size;
329         char *map = MAP_FAILED, *strings, *dep = NULL;
330         const char *aliases[32] = { 0 };
331         int naliases = 0;
332         struct module *m = NULL;
333         struct stat s;
334
335         if (fd < 0) {
336                 ULOG_ERR("failed to open %s\n", module);
337                 goto out;
338         }
339
340         if (fstat(fd, &s) == -1) {
341                 ULOG_ERR("failed to stat %s\n", module);
342                 goto out;
343         }
344
345         map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
346         if (map == MAP_FAILED) {
347                 ULOG_ERR("failed to mmap %s\n", module);
348                 goto out;
349         }
350
351         if (elf_find_section(map, ".modinfo", &offset, &size)) {
352                 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
353                 goto out;
354         }
355
356         strings = map + offset;
357         while (true) {
358                 char *sep;
359                 int len;
360
361                 while (!strings[0])
362                         strings++;
363                 if (strings >= map + offset + size)
364                         break;
365                 sep = strstr(strings, "=");
366                 if (!sep)
367                         break;
368                 len = sep - strings;
369                 sep++;
370                 if (!strncmp(strings, "depends=", len + 1))
371                         dep = sep;
372                 else if (!strncmp(strings, "alias=", len + 1)) {
373                         if (naliases < ARRAY_SIZE(aliases))
374                                 aliases[naliases++] = sep;
375                         else
376                                 ULOG_WARN("module %s has more than %d aliases: truncated",
377                                                 name, ARRAY_SIZE(aliases));
378                 }
379                 strings = &sep[strlen(sep)];
380         }
381
382         m = alloc_module(name, aliases, naliases, dep, s.st_size);
383
384         if (m)
385                 m->state = SCANNED;
386
387 out:
388         if (map != MAP_FAILED)
389                 munmap(map, s.st_size);
390
391         if (fd >= 0)
392                 close(fd);
393
394         return m;
395 }
396
397 static int scan_module_folder(const char *dir)
398 {
399         int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
400         struct utsname ver;
401         char *path;
402         glob_t gl;
403         int j;
404
405         uname(&ver);
406         path = alloca(strlen(dir) + sizeof("*.ko") + 1);
407         sprintf(path, "%s*.ko", dir);
408
409         if (glob(path, gl_flags, NULL, &gl) < 0)
410                 return -1;
411
412         for (j = 0; j < gl.gl_pathc; j++) {
413                 char *name = get_module_name(gl.gl_pathv[j]);
414                 struct module *m;
415
416                 if (!name)
417                         continue;
418
419                 m = find_module(name);
420                 if (!m)
421                         get_module_info(gl.gl_pathv[j], name);
422         }
423
424         globfree(&gl);
425
426         return 0;
427 }
428
429 static int scan_module_folders(void)
430 {
431         int rv = 0;
432         char **p;
433
434         if (init_module_folders())
435                 return -1;
436
437         for (p = module_folders; *p; p++)
438                 rv |= scan_module_folder(*p);
439
440         return rv;
441 }
442
443 static int print_modinfo(char *module)
444 {
445         int fd = open(module, O_RDONLY);
446         unsigned int offset, size;
447         struct stat s;
448         char *map = MAP_FAILED, *strings;
449         int rv = -1;
450
451         if (fd < 0) {
452                 ULOG_ERR("failed to open %s\n", module);
453                 goto out;
454         }
455
456         if (fstat(fd, &s) == -1) {
457                 ULOG_ERR("failed to stat %s\n", module);
458                 goto out;
459         }
460
461         map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
462         if (map == MAP_FAILED) {
463                 ULOG_ERR("failed to mmap %s\n", module);
464                 goto out;
465         }
466
467         if (elf_find_section(map, ".modinfo", &offset, &size)) {
468                 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
469                 goto out;
470         }
471
472         strings = map + offset;
473         printf("module:\t\t%s\n", module);
474         while (true) {
475                 char *dup = NULL;
476                 char *sep;
477
478                 while (!strings[0])
479                         strings++;
480                 if (strings >= map + offset + size)
481                         break;
482                 sep = strstr(strings, "=");
483                 if (!sep)
484                         break;
485                 dup = strndup(strings, sep - strings);
486                 sep++;
487                 if (strncmp(strings, "parm", 4)) {
488                         if (strlen(dup) < 7)
489                                 printf("%s:\t\t%s\n",  dup, sep);
490                         else
491                                 printf("%s:\t%s\n",  dup, sep);
492                 }
493                 strings = &sep[strlen(sep)];
494                 if (dup)
495                         free(dup);
496         }
497
498         rv = 0;
499
500 out:
501         if (map != MAP_FAILED)
502                 munmap(map, s.st_size);
503
504         if (fd >= 0)
505                 close(fd);
506
507         return rv;
508 }
509
510 static int deps_available(struct module *m, int verbose)
511 {
512         char *dep;
513         int err = 0;
514
515         if (!m->depends || !strcmp(m->depends, "-") || !strcmp(m->depends, ""))
516                 return 0;
517
518         dep = m->depends;
519
520         while (*dep) {
521                 m = find_module(dep);
522
523                 if (verbose && !m)
524                         ULOG_ERR("missing dependency %s\n", dep);
525                 if (verbose && m && (m->state != LOADED))
526                         ULOG_ERR("dependency not loaded %s\n", dep);
527                 if (!m || (m->state != LOADED))
528                         err++;
529                 dep += strlen(dep) + 1;
530         }
531
532         return err;
533 }
534
535 static int insert_module(char *path, const char *options)
536 {
537         void *data = 0;
538         struct stat s;
539         int fd, ret = -1;
540
541         if (stat(path, &s)) {
542                 ULOG_ERR("missing module %s\n", path);
543                 return ret;
544         }
545
546         fd = open(path, O_RDONLY);
547         if (fd < 0) {
548                 ULOG_ERR("cannot open %s\n", path);
549                 return ret;
550         }
551
552         data = malloc(s.st_size);
553         if (read(fd, data, s.st_size) == s.st_size) {
554                 ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
555                 if (errno == EEXIST)
556                         ret = 0;
557         }
558         else
559                 ULOG_ERR("failed to read full module %s\n", path);
560
561         close(fd);
562         free(data);
563
564         return ret;
565 }
566
567 static void load_moddeps(struct module *_m)
568 {
569         char *dep;
570         struct module *m;
571
572         if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
573                 return;
574
575         dep = _m->depends;
576
577         while (*dep) {
578                 m = find_module(dep);
579
580                 if (!m)
581                         ULOG_ERR("failed to find dependency %s\n", dep);
582                 if (m && (m->state != LOADED)) {
583                         m->state = PROBE;
584                         load_moddeps(m);
585                 }
586
587                 dep = dep + strlen(dep) + 1;
588         }
589 }
590
591 static int iterations = 0;
592 static int load_modprobe(void)
593 {
594         int loaded, todo;
595         struct module_node *mn;
596         struct module *m;
597
598         avl_for_each_element(&modules, mn, avl) {
599                 if (mn->is_alias)
600                         continue;
601                 m = mn->m;
602                 if (m->state == PROBE)
603                         load_moddeps(m);
604         }
605
606         do {
607                 loaded = 0;
608                 todo = 0;
609                 avl_for_each_element(&modules, mn, avl) {
610                         if (mn->is_alias)
611                                 continue;
612                         m = mn->m;
613                         if ((m->state == PROBE) && (!deps_available(m, 0))) {
614                                 if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
615                                         m->state = LOADED;
616                                         m->error = 0;
617                                         loaded++;
618                                         continue;
619                                 }
620                                 m->error = 1;
621                         }
622
623                         if ((m->state == PROBE) || m->error)
624                                 todo++;
625                 }
626                 iterations++;
627         } while (loaded);
628
629         return todo;
630 }
631
632 static int print_insmod_usage(void)
633 {
634         ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
635
636         return -1;
637 }
638
639 static int print_modprobe_usage(void)
640 {
641         ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
642
643         return -1;
644 }
645
646 static int print_usage(char *arg)
647 {
648         ULOG_INFO("Usage:\n\t%s module\n", arg);
649
650         return -1;
651 }
652
653 static int main_insmod(int argc, char **argv)
654 {
655         char *name, *cur, *options;
656         int i, ret, len;
657
658         if (argc < 2)
659                 return print_insmod_usage();
660
661         name = get_module_name(argv[1]);
662         if (!name) {
663                 ULOG_ERR("cannot find module - %s\n", argv[1]);
664                 return -1;
665         }
666
667         if (scan_loaded_modules())
668                 return -1;
669
670         if (find_module(name)) {
671                 ULOG_ERR("module is already loaded - %s\n", name);
672                 return -1;
673
674         }
675
676         free_modules();
677
678         for (len = 0, i = 2; i < argc; i++)
679                 len += strlen(argv[i]) + 1;
680
681         options = malloc(len);
682         options[0] = 0;
683         cur = options;
684         for (i = 2; i < argc; i++) {
685                 if (options[0]) {
686                         *cur = ' ';
687                         cur++;
688                 }
689                 cur += sprintf(cur, "%s", argv[i]);
690         }
691
692         if (init_module_folders()) {
693                 fprintf(stderr, "Failed to find the folder holding the modules\n");
694                 ret = -1;
695                 goto err;
696         }
697
698         if (get_module_path(argv[1])) {
699                 name = argv[1];
700         } else if (!get_module_path(name)) {
701                 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
702                 ret = -1;
703                 goto err;
704         }
705
706         ret = insert_module(get_module_path(name), options);
707         if (ret)
708                 ULOG_ERR("failed to insert %s\n", get_module_path(name));
709
710 err:
711         free(options);
712         return ret;
713 }
714
715 static int main_rmmod(int argc, char **argv)
716 {
717         struct module *m;
718         char *name;
719         int ret;
720
721         if (argc != 2)
722                 return print_usage("rmmod");
723
724         if (scan_loaded_modules())
725                 return -1;
726
727         name = get_module_name(argv[1]);
728         m = find_module(name);
729         if (!m) {
730                 ULOG_ERR("module is not loaded\n");
731                 return -1;
732         }
733         ret = syscall(__NR_delete_module, m->name, 0);
734
735         if (ret)
736                 ULOG_ERR("unloading the module failed\n");
737
738         free_modules();
739
740         return ret;
741 }
742
743 static int main_lsmod(int argc, char **argv)
744 {
745         struct module_node *mn;
746         struct module *m;
747         char *dep;
748
749         if (scan_loaded_modules())
750                 return -1;
751
752         avl_for_each_element(&modules, mn, avl) {
753                 if (mn->is_alias)
754                         continue;
755                 m = mn->m;
756                 if (m->state == LOADED) {
757                         printf("%-20s%8d%3d ",
758                                 m->name, m->size, m->usage);
759                         if (m->depends && strcmp(m->depends, "-") && strcmp(m->depends, "")) {
760                                 dep = m->depends;
761                                 while (*dep) {
762                                         printf("%s", dep);
763                                         dep = dep + strlen(dep) + 1;
764                                         if (*dep)
765                                                 printf(",");
766                                 }
767                         }
768                         printf("\n");
769                 }
770         }
771
772         free_modules();
773
774         return 0;
775 }
776
777 static int main_modinfo(int argc, char **argv)
778 {
779         struct module *m;
780         char *name;
781
782         if (argc != 2)
783                 return print_usage("modinfo");
784
785         if (scan_module_folders())
786                 return -1;
787
788         name = get_module_name(argv[1]);
789         m = find_module(name);
790         if (!m) {
791                 ULOG_ERR("cannot find module - %s\n", argv[1]);
792                 return -1;
793         }
794
795         name = get_module_path(m->name);
796         if (!name) {
797                 ULOG_ERR("cannot find path of module - %s\n", m->name);
798                 return -1;
799         }
800
801         print_modinfo(name);
802
803         return 0;
804 }
805
806 static int main_modprobe(int argc, char **argv)
807 {
808         struct module_node *mn;
809         struct module *m;
810         char *name;
811         char *mod = NULL;
812         int opt;
813         bool quiet = false;
814
815         while ((opt = getopt(argc, argv, "q")) != -1 ) {
816                 switch (opt) {
817                         case 'q': /* shhhh! */
818                                 quiet = true;
819                                 break;
820                         default: /* '?' */
821                                 return print_modprobe_usage();
822                                 break;
823                         }
824         }
825
826         if (optind >= argc)
827                 return print_modprobe_usage(); /* expected module after options */
828
829         mod = argv[optind];
830
831         if (scan_module_folders())
832                 return -1;
833
834         if (scan_loaded_modules())
835                 return -1;
836
837         name = get_module_name(mod);
838         m = find_module(name);
839         if (m && m->state == LOADED) {
840                 if (!quiet)
841                         ULOG_ERR("%s is already loaded\n", name);
842                 return 0;
843         } else if (!m) {
844                 if (!quiet)
845                         ULOG_ERR("failed to find a module named %s\n", name);
846                 return -1;
847         } else {
848                 int fail;
849
850                 m->state = PROBE;
851
852                 fail = load_modprobe();
853
854                 if (fail) {
855                         ULOG_ERR("%d module%s could not be probed\n",
856                                  fail, (fail == 1) ? ("") : ("s"));
857
858                         avl_for_each_element(&modules, mn, avl) {
859                                 if (mn->is_alias)
860                                         continue;
861                                 m = mn->m;
862                                 if ((m->state == PROBE) || m->error)
863                                         ULOG_ERR("- %s\n", m->name);
864                         }
865                 }
866         }
867
868         free_modules();
869
870         return 0;
871 }
872
873 static int main_loader(int argc, char **argv)
874 {
875         int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
876         char *dir = "/etc/modules.d/";
877         struct module_node *mn;
878         struct module *m;
879         glob_t gl;
880         char *path;
881         int fail, j;
882
883         if (argc > 1)
884                 dir = argv[1];
885
886         path = malloc(strlen(dir) + 2);
887         strcpy(path, dir);
888         strcat(path, "*");
889
890         if (scan_module_folders()) {
891                 free (path);
892                 return -1;
893         }
894
895         if (scan_loaded_modules()) {
896                 free (path);
897                 return -1;
898         }
899
900         ULOG_INFO("loading kernel modules from %s\n", path);
901
902         if (glob(path, gl_flags, NULL, &gl) < 0)
903                 goto out;
904
905         for (j = 0; j < gl.gl_pathc; j++) {
906                 FILE *fp = fopen(gl.gl_pathv[j], "r");
907                 size_t mod_len = 0;
908                 char *mod = NULL;
909
910                 if (!fp) {
911                         ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
912                         continue;
913                 }
914
915                 while (getline(&mod, &mod_len, fp) > 0) {
916                         char *nl = strchr(mod, '\n');
917                         struct module *m;
918                         char *opts;
919
920                         if (nl)
921                                 *nl = '\0';
922
923                         opts = strchr(mod, ' ');
924                         if (opts)
925                                 *opts++ = '\0';
926
927                         m = find_module(get_module_name(mod));
928                         if (!m || (m->state == LOADED))
929                                 continue;
930
931                         if (opts)
932                                 m->opts = strdup(opts);
933                         m->state = PROBE;
934                         if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
935                                 load_modprobe();
936
937                 }
938                 free(mod);
939                 fclose(fp);
940         }
941
942         fail = load_modprobe();
943
944         if (fail) {
945                 ULOG_ERR("%d module%s could not be probed\n",
946                          fail, (fail == 1) ? ("") : ("s"));
947
948                 avl_for_each_element(&modules, mn, avl) {
949                         if (mn->is_alias)
950                                 continue;
951                         m = mn->m;
952                         if ((m->state == PROBE) || (m->error))
953                                 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
954                 }
955         } else {
956                 ULOG_INFO("done loading kernel modules from %s\n", path);
957         }
958
959 out:
960         globfree(&gl);
961         free(path);
962
963         return 0;
964 }
965
966 static inline char weight(char c)
967 {
968         return c == '_' ? '-' : c;
969 }
970
971 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
972 {
973         const char *s1 = k1;
974         const char *s2 = k2;
975
976         while (*s1 && (weight(*s1) == weight(*s2)))
977         {
978                 s1++;
979                 s2++;
980         }
981
982         return (unsigned char)weight(*s1) - (unsigned char)weight(*s2);
983 }
984
985 int main(int argc, char **argv)
986 {
987         char *exec = basename(*argv);
988
989         avl_init(&modules, avl_modcmp, false, NULL);
990         if (!strcmp(exec, "insmod"))
991                 return main_insmod(argc, argv);
992
993         if (!strcmp(exec, "rmmod"))
994                 return main_rmmod(argc, argv);
995
996         if (!strcmp(exec, "lsmod"))
997                 return main_lsmod(argc, argv);
998
999         if (!strcmp(exec, "modinfo"))
1000                 return main_modinfo(argc, argv);
1001
1002         if (!strcmp(exec, "modprobe"))
1003                 return main_modprobe(argc, argv);
1004
1005         ulog_open(ULOG_KMSG, LOG_USER, "kmodloader");
1006         return main_loader(argc, argv);
1007 }