3196deb94e17c6fab7e8810b51f9b0be8469f5c8
[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 ULOG_ERR("out of memory\n");
100 return -1;
101 }
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[33];
161 char *t;
162
163 strncpy(name, basename(path), sizeof(name) - 1);
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 int endian = map[EI_DATA];
220
221 #if __BYTE_ORDER == __LITTLE_ENDIAN
222 if (endian != ELFDATA2LSB)
223 #elif __BYTE_ORDER == __BIG_ENDIAN
224 if (endian != ELFDATA2MSB)
225 #else
226 #error "unsupported endian"
227 #endif
228 {
229 ULOG_ERR("invalid endianess: %d\n", endian);
230 return -1;
231 }
232
233 if (clazz == ELFCLASS32)
234 return elf32_find_section(map, section, offset, size);
235 else if (clazz == ELFCLASS64)
236 return elf64_find_section(map, section, offset, size);
237
238 ULOG_ERR("unknown elf format %d\n", clazz);
239
240 return -1;
241 }
242
243 static struct module_node *
244 alloc_module_node(const char *name, struct module *m, bool is_alias)
245 {
246 struct module_node *mn;
247 char *_name;
248
249 mn = calloc_a(sizeof(*mn),
250 &_name, strlen(name) + 1);
251 if (mn) {
252 mn->avl.key = strcpy(_name, name);
253 mn->m = m;
254 mn->is_alias = is_alias;
255 avl_insert(&modules, &mn->avl);
256 m->refcnt += 1;
257 }
258 return mn;
259 }
260
261 static struct module *
262 alloc_module(const char *name, const char * const *aliases, int naliases, const char *depends, int size)
263 {
264 struct module *m;
265 char *_name, *_dep;
266 int i;
267
268 m = calloc_a(sizeof(*m),
269 &_name, strlen(name) + 1,
270 &_dep, depends ? strlen(depends) + 2 : 0);
271 if (!m)
272 return NULL;
273
274 m->name = strcpy(_name, name);
275 m->opts = 0;
276
277 if (depends) {
278 m->depends = strcpy(_dep, depends);
279 while (*_dep) {
280 if (*_dep == ',')
281 *_dep = '\0';
282 _dep++;
283 }
284 }
285 m->size = size;
286
287 m->refcnt = 0;
288 alloc_module_node(m->name, m, false);
289 for (i = 0; i < naliases; i++)
290 alloc_module_node(aliases[i], m, true);
291
292 return m;
293 }
294
295 static void free_module(struct module *m)
296 {
297 if (m->opts)
298 free(m->opts);
299 free(m);
300 }
301
302 static int scan_loaded_modules(void)
303 {
304 size_t buf_len = 0;
305 char *buf = NULL;
306 FILE *fp;
307
308 fp = fopen("/proc/modules", "r");
309 if (!fp) {
310 ULOG_ERR("failed to open /proc/modules\n");
311 return -1;
312 }
313
314 while (getline(&buf, &buf_len, fp) > 0) {
315 struct module m;
316 struct module *n;
317
318 m.name = strtok(buf, " ");
319 m.size = atoi(strtok(NULL, " "));
320 m.usage = atoi(strtok(NULL, " "));
321 m.depends = strtok(NULL, " ");
322
323 if (!m.name || !m.depends)
324 continue;
325
326 n = find_module(m.name);
327 if (!n) {
328 /* possibly a module outside /lib/modules/<ver>/ */
329 n = alloc_module(m.name, NULL, 0, m.depends, m.size);
330 }
331 n->usage = m.usage;
332 n->state = LOADED;
333 }
334 free(buf);
335 fclose(fp);
336
337 return 0;
338 }
339
340 static struct module* get_module_info(const char *module, const char *name)
341 {
342 int fd = open(module, O_RDONLY);
343 unsigned int offset, size;
344 char *map = MAP_FAILED, *strings, *dep = NULL;
345 const char **aliases = NULL;
346 int naliases = 0;
347 struct module *m = NULL;
348 struct stat s;
349
350 if (fd < 0) {
351 ULOG_ERR("failed to open %s\n", module);
352 goto out;
353 }
354
355 if (fstat(fd, &s) == -1) {
356 ULOG_ERR("failed to stat %s\n", module);
357 goto out;
358 }
359
360 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
361 if (map == MAP_FAILED) {
362 ULOG_ERR("failed to mmap %s\n", module);
363 goto out;
364 }
365
366 if (elf_find_section(map, ".modinfo", &offset, &size)) {
367 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
368 goto out;
369 }
370
371 strings = map + offset;
372 while (true) {
373 char *sep;
374 int len;
375
376 while (!strings[0])
377 strings++;
378 if (strings >= map + offset + size)
379 break;
380 sep = strstr(strings, "=");
381 if (!sep)
382 break;
383 len = sep - strings;
384 sep++;
385 if (!strncmp(strings, "depends=", len + 1))
386 dep = sep;
387 else if (!strncmp(strings, "alias=", len + 1)) {
388 aliases = realloc(aliases, sizeof(sep) * (naliases + 1));
389 if (!aliases) {
390 ULOG_ERR("out of memory\n");
391 goto out;
392 }
393
394 aliases[naliases++] = sep;
395 }
396 strings = &sep[strlen(sep)];
397 }
398
399 m = alloc_module(name, aliases, naliases, dep, s.st_size);
400
401 if (m)
402 m->state = SCANNED;
403
404 out:
405 if (map != MAP_FAILED)
406 munmap(map, s.st_size);
407
408 if (fd >= 0)
409 close(fd);
410
411 free(aliases);
412
413 return m;
414 }
415
416 static int scan_module_folder(const char *dir)
417 {
418 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
419 struct utsname ver;
420 char *path;
421 glob_t gl;
422 int j, rv = 0;
423
424 uname(&ver);
425 path = alloca(strlen(dir) + sizeof("*.ko") + 1);
426 sprintf(path, "%s*.ko", dir);
427
428 if (glob(path, gl_flags, NULL, &gl) < 0)
429 return -1;
430
431 for (j = 0; j < gl.gl_pathc; j++) {
432 char *name = get_module_name(gl.gl_pathv[j]);
433 struct module *m;
434
435 if (!name)
436 continue;
437
438 m = find_module(name);
439 if (!m) {
440 if (!get_module_info(gl.gl_pathv[j], name))
441 rv |= -1;
442 }
443 }
444
445 globfree(&gl);
446
447 return rv;
448 }
449
450 static int scan_module_folders(void)
451 {
452 int rv = 0;
453 char **p;
454
455 if (init_module_folders())
456 return -1;
457
458 for (p = module_folders; *p; p++)
459 rv |= scan_module_folder(*p);
460
461 return rv;
462 }
463
464 static int print_modinfo(char *module)
465 {
466 int fd = open(module, O_RDONLY);
467 unsigned int offset, size;
468 struct stat s;
469 char *map = MAP_FAILED, *strings;
470 int rv = -1;
471
472 if (fd < 0) {
473 ULOG_ERR("failed to open %s\n", module);
474 goto out;
475 }
476
477 if (fstat(fd, &s) == -1) {
478 ULOG_ERR("failed to stat %s\n", module);
479 goto out;
480 }
481
482 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
483 if (map == MAP_FAILED) {
484 ULOG_ERR("failed to mmap %s\n", module);
485 goto out;
486 }
487
488 if (elf_find_section(map, ".modinfo", &offset, &size)) {
489 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
490 goto out;
491 }
492
493 strings = map + offset;
494 printf("module:\t\t%s\n", module);
495 while (true) {
496 char *dup = NULL;
497 char *sep;
498
499 while (!strings[0])
500 strings++;
501 if (strings >= map + offset + size)
502 break;
503 sep = strstr(strings, "=");
504 if (!sep)
505 break;
506 dup = strndup(strings, sep - strings);
507 sep++;
508 if (strncmp(strings, "parm", 4)) {
509 if (strlen(dup) < 7)
510 printf("%s:\t\t%s\n", dup, sep);
511 else
512 printf("%s:\t%s\n", dup, sep);
513 }
514 strings = &sep[strlen(sep)];
515 if (dup)
516 free(dup);
517 }
518
519 rv = 0;
520
521 out:
522 if (map != MAP_FAILED)
523 munmap(map, s.st_size);
524
525 if (fd >= 0)
526 close(fd);
527
528 return rv;
529 }
530
531 static int deps_available(struct module *m, int verbose)
532 {
533 char *dep;
534 int err = 0;
535
536 if (!m->depends || !strcmp(m->depends, "-") || !strcmp(m->depends, ""))
537 return 0;
538
539 dep = m->depends;
540
541 while (*dep) {
542 m = find_module(dep);
543
544 if (verbose && !m)
545 ULOG_ERR("missing dependency %s\n", dep);
546 if (verbose && m && (m->state != LOADED))
547 ULOG_ERR("dependency not loaded %s\n", dep);
548 if (!m || (m->state != LOADED))
549 err++;
550 dep += strlen(dep) + 1;
551 }
552
553 return err;
554 }
555
556 static int insert_module(char *path, const char *options)
557 {
558 void *data = 0;
559 struct stat s;
560 int fd, ret = -1;
561
562 if (stat(path, &s)) {
563 ULOG_ERR("missing module %s\n", path);
564 return ret;
565 }
566
567 fd = open(path, O_RDONLY);
568 if (fd < 0) {
569 ULOG_ERR("cannot open %s\n", path);
570 return ret;
571 }
572
573 data = malloc(s.st_size);
574 if (!data) {
575 ULOG_ERR("out of memory\n");
576 goto out;
577 }
578
579 if (read(fd, data, s.st_size) == s.st_size) {
580 ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
581 if (errno == EEXIST)
582 ret = 0;
583 }
584 else
585 ULOG_ERR("failed to read full module %s\n", path);
586
587 out:
588 close(fd);
589 free(data);
590
591 return ret;
592 }
593
594 static void load_moddeps(struct module *_m)
595 {
596 char *dep;
597 struct module *m;
598
599 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
600 return;
601
602 dep = _m->depends;
603
604 while (*dep) {
605 m = find_module(dep);
606
607 if (!m)
608 ULOG_ERR("failed to find dependency %s\n", dep);
609 if (m && (m->state != LOADED)) {
610 m->state = PROBE;
611 load_moddeps(m);
612 }
613
614 dep = dep + strlen(dep) + 1;
615 }
616 }
617
618 static int iterations = 0;
619 static int load_modprobe(void)
620 {
621 int loaded, todo;
622 struct module_node *mn;
623 struct module *m;
624
625 avl_for_each_element(&modules, mn, avl) {
626 if (mn->is_alias)
627 continue;
628 m = mn->m;
629 if (m->state == PROBE)
630 load_moddeps(m);
631 }
632
633 do {
634 loaded = 0;
635 todo = 0;
636 avl_for_each_element(&modules, mn, avl) {
637 if (mn->is_alias)
638 continue;
639 m = mn->m;
640 if ((m->state == PROBE) && (!deps_available(m, 0)) && m->error < 2) {
641 if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
642 m->state = LOADED;
643 m->error = 0;
644 loaded++;
645 continue;
646 }
647
648 if (++m->error > 1)
649 ULOG_ERR("failed to load %s\n", m->name);
650 }
651
652 if ((m->state == PROBE) || m->error)
653 todo++;
654 }
655 iterations++;
656 } while (loaded);
657
658 return todo;
659 }
660
661 static int print_insmod_usage(void)
662 {
663 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
664
665 return -1;
666 }
667
668 static int print_modprobe_usage(void)
669 {
670 ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
671
672 return -1;
673 }
674
675 static int print_usage(char *arg)
676 {
677 ULOG_INFO("Usage:\n\t%s module\n", arg);
678
679 return -1;
680 }
681
682 static int main_insmod(int argc, char **argv)
683 {
684 char *name, *cur, *options;
685 int i, ret, len;
686
687 if (argc < 2)
688 return print_insmod_usage();
689
690 name = get_module_name(argv[1]);
691 if (!name) {
692 ULOG_ERR("cannot find module - %s\n", argv[1]);
693 return -1;
694 }
695
696 if (scan_loaded_modules())
697 return -1;
698
699 if (find_module(name)) {
700 ULOG_ERR("module is already loaded - %s\n", name);
701 return -1;
702
703 }
704
705 free_modules();
706
707 for (len = 0, i = 2; i < argc; i++)
708 len += strlen(argv[i]) + 1;
709
710 options = malloc(len);
711 if (!options) {
712 ULOG_ERR("out of memory\n");
713 ret = -1;
714 goto err;
715 }
716
717 options[0] = 0;
718 cur = options;
719 for (i = 2; i < argc; i++) {
720 if (options[0]) {
721 *cur = ' ';
722 cur++;
723 }
724 cur += sprintf(cur, "%s", argv[i]);
725 }
726
727 if (init_module_folders()) {
728 fprintf(stderr, "Failed to find the folder holding the modules\n");
729 ret = -1;
730 goto err;
731 }
732
733 if (get_module_path(argv[1])) {
734 name = argv[1];
735 } else if (!get_module_path(name)) {
736 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
737 ret = -1;
738 goto err;
739 }
740
741 ret = insert_module(get_module_path(name), options);
742 if (ret)
743 ULOG_ERR("failed to insert %s\n", get_module_path(name));
744
745 err:
746 free(options);
747 return ret;
748 }
749
750 static int main_rmmod(int argc, char **argv)
751 {
752 struct module *m;
753 char *name;
754 int ret;
755
756 if (argc != 2)
757 return print_usage("rmmod");
758
759 if (scan_loaded_modules())
760 return -1;
761
762 name = get_module_name(argv[1]);
763 m = find_module(name);
764 if (!m) {
765 ULOG_ERR("module is not loaded\n");
766 return -1;
767 }
768 ret = syscall(__NR_delete_module, m->name, 0);
769
770 if (ret)
771 ULOG_ERR("unloading the module failed\n");
772
773 free_modules();
774
775 return ret;
776 }
777
778 static int main_lsmod(int argc, char **argv)
779 {
780 struct module_node *mn;
781 struct module *m;
782 char *dep;
783
784 if (scan_loaded_modules())
785 return -1;
786
787 avl_for_each_element(&modules, mn, avl) {
788 if (mn->is_alias)
789 continue;
790 m = mn->m;
791 if (m->state == LOADED) {
792 printf("%-20s%8d%3d ",
793 m->name, m->size, m->usage);
794 if (m->depends && strcmp(m->depends, "-") && strcmp(m->depends, "")) {
795 dep = m->depends;
796 while (*dep) {
797 printf("%s", dep);
798 dep = dep + strlen(dep) + 1;
799 if (*dep)
800 printf(",");
801 }
802 }
803 printf("\n");
804 }
805 }
806
807 free_modules();
808
809 return 0;
810 }
811
812 static int main_modinfo(int argc, char **argv)
813 {
814 struct module *m;
815 char *name;
816
817 if (argc != 2)
818 return print_usage("modinfo");
819
820 if (scan_module_folders())
821 return -1;
822
823 name = get_module_name(argv[1]);
824 m = find_module(name);
825 if (!m) {
826 ULOG_ERR("cannot find module - %s\n", argv[1]);
827 return -1;
828 }
829
830 name = get_module_path(m->name);
831 if (!name) {
832 ULOG_ERR("cannot find path of module - %s\n", m->name);
833 return -1;
834 }
835
836 print_modinfo(name);
837
838 return 0;
839 }
840
841 static int main_modprobe(int argc, char **argv)
842 {
843 struct module_node *mn;
844 struct module *m;
845 char *name;
846 char *mod = NULL;
847 int opt;
848 bool quiet = false;
849
850 while ((opt = getopt(argc, argv, "q")) != -1 ) {
851 switch (opt) {
852 case 'q': /* shhhh! */
853 quiet = true;
854 break;
855 default: /* '?' */
856 return print_modprobe_usage();
857 break;
858 }
859 }
860
861 if (optind >= argc)
862 return print_modprobe_usage(); /* expected module after options */
863
864 mod = argv[optind];
865
866 if (scan_module_folders())
867 return -1;
868
869 if (scan_loaded_modules())
870 return -1;
871
872 name = get_module_name(mod);
873 m = find_module(name);
874 if (m && m->state == LOADED) {
875 if (!quiet)
876 ULOG_ERR("%s is already loaded\n", name);
877 return 0;
878 } else if (!m) {
879 if (!quiet)
880 ULOG_ERR("failed to find a module named %s\n", name);
881 return -1;
882 } else {
883 int fail;
884
885 m->state = PROBE;
886
887 fail = load_modprobe();
888
889 if (fail) {
890 ULOG_ERR("%d module%s could not be probed\n",
891 fail, (fail == 1) ? ("") : ("s"));
892
893 avl_for_each_element(&modules, mn, avl) {
894 if (mn->is_alias)
895 continue;
896 m = mn->m;
897 if ((m->state == PROBE) || m->error)
898 ULOG_ERR("- %s\n", m->name);
899 }
900 }
901 }
902
903 free_modules();
904
905 return 0;
906 }
907
908 static int main_loader(int argc, char **argv)
909 {
910 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
911 char *dir = "/etc/modules.d/";
912 struct module_node *mn;
913 struct module *m;
914 glob_t gl;
915 char *path;
916 int fail, j;
917
918 if (argc > 1)
919 dir = argv[1];
920
921 path = malloc(strlen(dir) + 2);
922 if (!path) {
923 ULOG_ERR("out of memory\n");
924 return -1;
925 }
926
927 strcpy(path, dir);
928 strcat(path, "*");
929
930 if (scan_module_folders()) {
931 free (path);
932 return -1;
933 }
934
935 if (scan_loaded_modules()) {
936 free (path);
937 return -1;
938 }
939
940 ULOG_INFO("loading kernel modules from %s\n", path);
941
942 if (glob(path, gl_flags, NULL, &gl) < 0)
943 goto out;
944
945 for (j = 0; j < gl.gl_pathc; j++) {
946 FILE *fp = fopen(gl.gl_pathv[j], "r");
947 size_t mod_len = 0;
948 char *mod = NULL;
949
950 if (!fp) {
951 ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
952 continue;
953 }
954
955 while (getline(&mod, &mod_len, fp) > 0) {
956 char *nl = strchr(mod, '\n');
957 struct module *m;
958 char *opts;
959
960 if (nl)
961 *nl = '\0';
962
963 opts = strchr(mod, ' ');
964 if (opts)
965 *opts++ = '\0';
966
967 m = find_module(get_module_name(mod));
968 if (!m || (m->state == LOADED))
969 continue;
970
971 if (opts)
972 m->opts = strdup(opts);
973 m->state = PROBE;
974 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
975 load_modprobe();
976
977 }
978 free(mod);
979 fclose(fp);
980 }
981
982 fail = load_modprobe();
983
984 if (fail) {
985 ULOG_ERR("%d module%s could not be probed\n",
986 fail, (fail == 1) ? ("") : ("s"));
987
988 avl_for_each_element(&modules, mn, avl) {
989 if (mn->is_alias)
990 continue;
991 m = mn->m;
992 if ((m->state == PROBE) || (m->error))
993 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
994 }
995 } else {
996 ULOG_INFO("done loading kernel modules from %s\n", path);
997 }
998
999 out:
1000 globfree(&gl);
1001 free(path);
1002
1003 return 0;
1004 }
1005
1006 static inline char weight(char c)
1007 {
1008 return c == '_' ? '-' : c;
1009 }
1010
1011 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
1012 {
1013 const char *s1 = k1;
1014 const char *s2 = k2;
1015
1016 while (*s1 && (weight(*s1) == weight(*s2)))
1017 {
1018 s1++;
1019 s2++;
1020 }
1021
1022 return (unsigned char)weight(*s1) - (unsigned char)weight(*s2);
1023 }
1024
1025 int main(int argc, char **argv)
1026 {
1027 char *exec = basename(*argv);
1028
1029 avl_init(&modules, avl_modcmp, false, NULL);
1030 if (!strcmp(exec, "insmod"))
1031 return main_insmod(argc, argv);
1032
1033 if (!strcmp(exec, "rmmod"))
1034 return main_rmmod(argc, argv);
1035
1036 if (!strcmp(exec, "lsmod"))
1037 return main_lsmod(argc, argv);
1038
1039 if (!strcmp(exec, "modinfo"))
1040 return main_modinfo(argc, argv);
1041
1042 if (!strcmp(exec, "modprobe"))
1043 return main_modprobe(argc, argv);
1044
1045 ulog_open(ULOG_KMSG, LOG_USER, "kmodloader");
1046 return main_loader(argc, argv);
1047 }