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