ed8f833fc3932caa7bfcc54228e21bd460a1a728
[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[32];
159 char *t;
160
161 strncpy(name, basename(path), sizeof(name));
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];
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 return -1;
695 }
696
697 if (get_module_path(argv[1])) {
698 name = argv[1];
699 } else if (!get_module_path(name)) {
700 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
701 return -1;
702 }
703
704 ret = insert_module(get_module_path(name), options);
705 free(options);
706
707 if (ret)
708 ULOG_ERR("failed to insert %s\n", get_module_path(name));
709
710 return ret;
711 }
712
713 static int main_rmmod(int argc, char **argv)
714 {
715 struct module *m;
716 char *name;
717 int ret;
718
719 if (argc != 2)
720 return print_usage("rmmod");
721
722 if (scan_loaded_modules())
723 return -1;
724
725 name = get_module_name(argv[1]);
726 m = find_module(name);
727 if (!m) {
728 ULOG_ERR("module is not loaded\n");
729 return -1;
730 }
731 ret = syscall(__NR_delete_module, m->name, 0);
732
733 if (ret)
734 ULOG_ERR("unloading the module failed\n");
735
736 free_modules();
737
738 return ret;
739 }
740
741 static int main_lsmod(int argc, char **argv)
742 {
743 struct module_node *mn;
744 struct module *m;
745 char *dep;
746
747 if (scan_loaded_modules())
748 return -1;
749
750 avl_for_each_element(&modules, mn, avl) {
751 if (mn->is_alias)
752 continue;
753 m = mn->m;
754 if (m->state == LOADED) {
755 printf("%-20s%8d%3d ",
756 m->name, m->size, m->usage);
757 if (m->depends && strcmp(m->depends, "-") && strcmp(m->depends, "")) {
758 dep = m->depends;
759 while (*dep) {
760 printf("%s", dep);
761 dep = dep + strlen(dep) + 1;
762 if (*dep)
763 printf(",");
764 }
765 }
766 printf("\n");
767 }
768 }
769
770 free_modules();
771
772 return 0;
773 }
774
775 static int main_modinfo(int argc, char **argv)
776 {
777 struct module *m;
778 char *name;
779
780 if (argc != 2)
781 return print_usage("modinfo");
782
783 if (scan_module_folders())
784 return -1;
785
786 name = get_module_name(argv[1]);
787 m = find_module(name);
788 if (!m) {
789 ULOG_ERR("cannot find module - %s\n", argv[1]);
790 return -1;
791 }
792
793 name = get_module_path(m->name);
794 if (!name) {
795 ULOG_ERR("cannot find path of module - %s\n", m->name);
796 return -1;
797 }
798
799 print_modinfo(name);
800
801 return 0;
802 }
803
804 static int main_modprobe(int argc, char **argv)
805 {
806 struct module_node *mn;
807 struct module *m;
808 char *name;
809 char *mod = NULL;
810 int opt;
811 bool quiet = false;
812
813 while ((opt = getopt(argc, argv, "q")) != -1 ) {
814 switch (opt) {
815 case 'q': /* shhhh! */
816 quiet = true;
817 break;
818 default: /* '?' */
819 return print_modprobe_usage();
820 break;
821 }
822 }
823
824 if (optind >= argc)
825 return print_modprobe_usage(); /* expected module after options */
826
827 mod = argv[optind];
828
829 if (scan_module_folders())
830 return -1;
831
832 if (scan_loaded_modules())
833 return -1;
834
835 name = get_module_name(mod);
836 m = find_module(name);
837 if (m && m->state == LOADED) {
838 if (!quiet)
839 ULOG_ERR("%s is already loaded\n", name);
840 return 0;
841 } else if (!m) {
842 if (!quiet)
843 ULOG_ERR("failed to find a module named %s\n", name);
844 return -1;
845 } else {
846 int fail;
847
848 m->state = PROBE;
849
850 fail = load_modprobe();
851
852 if (fail) {
853 ULOG_ERR("%d module%s could not be probed\n",
854 fail, (fail == 1) ? ("") : ("s"));
855
856 avl_for_each_element(&modules, mn, avl) {
857 if (mn->is_alias)
858 continue;
859 m = mn->m;
860 if ((m->state == PROBE) || m->error)
861 ULOG_ERR("- %s\n", m->name);
862 }
863 }
864 }
865
866 free_modules();
867
868 return 0;
869 }
870
871 static int main_loader(int argc, char **argv)
872 {
873 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
874 char *dir = "/etc/modules.d/";
875 struct module_node *mn;
876 struct module *m;
877 glob_t gl;
878 char *path;
879 int fail, j;
880
881 if (argc > 1)
882 dir = argv[1];
883
884 path = malloc(strlen(dir) + 2);
885 strcpy(path, dir);
886 strcat(path, "*");
887
888 if (scan_module_folders()) {
889 free (path);
890 return -1;
891 }
892
893 if (scan_loaded_modules()) {
894 free (path);
895 return -1;
896 }
897
898 ULOG_INFO("loading kernel modules from %s\n", path);
899
900 if (glob(path, gl_flags, NULL, &gl) < 0)
901 goto out;
902
903 for (j = 0; j < gl.gl_pathc; j++) {
904 FILE *fp = fopen(gl.gl_pathv[j], "r");
905 size_t mod_len = 0;
906 char *mod = NULL;
907
908 if (!fp) {
909 ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
910 continue;
911 }
912
913 while (getline(&mod, &mod_len, fp) > 0) {
914 char *nl = strchr(mod, '\n');
915 struct module *m;
916 char *opts;
917
918 if (nl)
919 *nl = '\0';
920
921 opts = strchr(mod, ' ');
922 if (opts)
923 *opts++ = '\0';
924
925 m = find_module(get_module_name(mod));
926 if (!m || (m->state == LOADED))
927 continue;
928
929 if (opts)
930 m->opts = strdup(opts);
931 m->state = PROBE;
932 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
933 load_modprobe();
934
935 }
936 free(mod);
937 fclose(fp);
938 }
939
940 fail = load_modprobe();
941
942 if (fail) {
943 ULOG_ERR("%d module%s could not be probed\n",
944 fail, (fail == 1) ? ("") : ("s"));
945
946 avl_for_each_element(&modules, mn, avl) {
947 if (mn->is_alias)
948 continue;
949 m = mn->m;
950 if ((m->state == PROBE) || (m->error))
951 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
952 }
953 } else {
954 ULOG_INFO("done loading kernel modules from %s\n", path);
955 }
956
957 out:
958 globfree(&gl);
959 free(path);
960
961 return 0;
962 }
963
964 static inline char weight(char c)
965 {
966 return c == '_' ? '-' : c;
967 }
968
969 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
970 {
971 const char *s1 = k1;
972 const char *s2 = k2;
973
974 while (*s1 && (weight(*s1) == weight(*s2)))
975 {
976 s1++;
977 s2++;
978 }
979
980 return (unsigned char)weight(*s1) - (unsigned char)weight(*s2);
981 }
982
983 int main(int argc, char **argv)
984 {
985 char *exec = basename(*argv);
986
987 avl_init(&modules, avl_modcmp, false, NULL);
988 if (!strcmp(exec, "insmod"))
989 return main_insmod(argc, argv);
990
991 if (!strcmp(exec, "rmmod"))
992 return main_rmmod(argc, argv);
993
994 if (!strcmp(exec, "lsmod"))
995 return main_lsmod(argc, argv);
996
997 if (!strcmp(exec, "modinfo"))
998 return main_modinfo(argc, argv);
999
1000 if (!strcmp(exec, "modprobe"))
1001 return main_modprobe(argc, argv);
1002
1003 ulog_open(ULOG_KMSG, LOG_USER, "kmodloader");
1004 return main_loader(argc, argv);
1005 }