c780379ed47f4349a2bc0c96d3bd9481943ea79f
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
16 #include <sys/syscall.h>
18 #include <sys/utsname.h>
22 #include <sys/syscall.h>
23 #include <sys/types.h>
34 #include <libubox/avl.h>
35 #include <libubox/avl-cmp.h>
36 #include <libubox/utils.h>
37 #include <libubox/ulog.h>
39 #define DEF_MOD_PATH "/modules/%s/"
60 static struct avl_tree modules
;
62 static char **module_folders
= NULL
;
64 static int init_module_folders(void)
69 char *s
, *e
, *p
, path
[256], ldpath
[256];
72 s
= getenv("LD_LIBRARY_PATH");
75 e
+= snprintf(ldpath
, sizeof(ldpath
), "%s:", s
);
77 e
+= snprintf(e
, sizeof(ldpath
) - (e
- ldpath
), "/lib");
81 for (s
= p
= ldpath
; p
<= e
; p
++) {
82 if (*p
!= ':' && *p
!= '\0')
86 snprintf(path
, sizeof(path
), "%s" DEF_MOD_PATH
, s
, ver
.release
);
88 if (!stat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
89 module_folders
= realloc(module_folders
, sizeof(p
) * (n
+ 2));
94 module_folders
[n
++] = strdup(path
);
103 module_folders
[n
] = NULL
;
107 static struct module
*find_module(const char *name
)
110 return avl_find_element(&modules
, name
, m
, avl
);
113 static void free_modules(void)
115 struct module
*m
, *tmp
;
117 avl_remove_all_elements(&modules
, m
, avl
, tmp
)
121 static char* get_module_path(char *name
)
124 static char path
[256];
127 if (!stat(name
, &s
) && S_ISREG(s
.st_mode
))
130 for (p
= module_folders
; *p
; p
++) {
131 snprintf(path
, sizeof(path
), "%s%s.ko", *p
, name
);
132 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
139 static char* get_module_name(char *path
)
141 static char name
[32];
144 strncpy(name
, basename(path
), sizeof(name
));
146 t
= strstr(name
, ".ko");
153 static int elf64_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
155 const char *secnames
;
160 e
= (Elf64_Ehdr
*) map
;
161 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
163 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
164 for (i
= 0; i
< e
->e_shnum
; i
++) {
165 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
166 *size
= sh
[i
].sh_size
;
167 *offset
= sh
[i
].sh_offset
;
175 static int elf32_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
177 const char *secnames
;
182 e
= (Elf32_Ehdr
*) map
;
183 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
185 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
186 for (i
= 0; i
< e
->e_shnum
; i
++) {
187 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
188 *size
= sh
[i
].sh_size
;
189 *offset
= sh
[i
].sh_offset
;
197 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
199 int clazz
= map
[EI_CLASS
];
201 if (clazz
== ELFCLASS32
)
202 return elf32_find_section(map
, section
, offset
, size
);
203 else if (clazz
== ELFCLASS64
)
204 return elf64_find_section(map
, section
, offset
, size
);
206 ULOG_ERR("unknown elf format %d\n", clazz
);
211 static struct module
*
212 alloc_module(const char *name
, const char *depends
, int size
)
217 m
= calloc_a(sizeof(*m
),
218 &_name
, strlen(name
) + 1,
219 &_dep
, depends
? strlen(depends
) + 2 : 0);
223 m
->avl
.key
= m
->name
= strcpy(_name
, name
);
227 m
->depends
= strcpy(_dep
, depends
);
236 avl_insert(&modules
, &m
->avl
);
241 static int scan_loaded_modules(void)
247 fp
= fopen("/proc/modules", "r");
249 ULOG_ERR("failed to open /proc/modules\n");
253 while (getline(&buf
, &buf_len
, fp
) > 0) {
257 m
.name
= strtok(buf
, " ");
258 m
.size
= atoi(strtok(NULL
, " "));
259 m
.usage
= atoi(strtok(NULL
, " "));
260 m
.depends
= strtok(NULL
, " ");
262 if (!m
.name
|| !m
.depends
)
265 n
= alloc_module(m
.name
, m
.depends
, m
.size
);
275 static struct module
* get_module_info(const char *module
, const char *name
)
277 int fd
= open(module
, O_RDONLY
);
278 unsigned int offset
, size
;
279 char *map
= MAP_FAILED
, *strings
, *dep
= NULL
;
280 struct module
*m
= NULL
;
284 ULOG_ERR("failed to open %s\n", module
);
288 if (fstat(fd
, &s
) == -1) {
289 ULOG_ERR("failed to stat %s\n", module
);
293 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
294 if (map
== MAP_FAILED
) {
295 ULOG_ERR("failed to mmap %s\n", module
);
299 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
300 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
304 strings
= map
+ offset
;
311 if (strings
>= map
+ offset
+ size
)
313 sep
= strstr(strings
, "=");
318 if (!strncmp(strings
, "depends=", len
+ 1))
320 strings
= &sep
[strlen(sep
)];
323 m
= alloc_module(name
, dep
, s
.st_size
);
329 if (map
!= MAP_FAILED
)
330 munmap(map
, s
.st_size
);
338 static int scan_module_folder(const char *dir
)
340 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
347 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
348 sprintf(path
, "%s*.ko", dir
);
350 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
353 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
354 char *name
= get_module_name(gl
.gl_pathv
[j
]);
360 m
= find_module(name
);
362 get_module_info(gl
.gl_pathv
[j
], name
);
370 static int scan_module_folders(void)
375 if (init_module_folders())
378 for (p
= module_folders
; *p
; p
++)
379 rv
|= scan_module_folder(*p
);
384 static int print_modinfo(char *module
)
386 int fd
= open(module
, O_RDONLY
);
387 unsigned int offset
, size
;
389 char *map
= MAP_FAILED
, *strings
;
393 ULOG_ERR("failed to open %s\n", module
);
397 if (fstat(fd
, &s
) == -1) {
398 ULOG_ERR("failed to stat %s\n", module
);
402 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
403 if (map
== MAP_FAILED
) {
404 ULOG_ERR("failed to mmap %s\n", module
);
408 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
409 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
413 strings
= map
+ offset
;
414 printf("module:\t\t%s\n", module
);
421 if (strings
>= map
+ offset
+ size
)
423 sep
= strstr(strings
, "=");
426 dup
= strndup(strings
, sep
- strings
);
428 if (strncmp(strings
, "parm", 4)) {
430 printf("%s:\t\t%s\n", dup
, sep
);
432 printf("%s:\t%s\n", dup
, sep
);
434 strings
= &sep
[strlen(sep
)];
442 if (map
!= MAP_FAILED
)
443 munmap(map
, s
.st_size
);
451 static int deps_available(struct module
*m
, int verbose
)
456 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
462 m
= find_module(dep
);
465 ULOG_ERR("missing dependency %s\n", dep
);
466 if (verbose
&& m
&& (m
->state
!= LOADED
))
467 ULOG_ERR("dependency not loaded %s\n", dep
);
468 if (!m
|| (m
->state
!= LOADED
))
470 dep
+= strlen(dep
) + 1;
476 static int insert_module(char *path
, const char *options
)
482 if (stat(path
, &s
)) {
483 ULOG_ERR("missing module %s\n", path
);
487 fd
= open(path
, O_RDONLY
);
489 ULOG_ERR("cannot open %s\n", path
);
493 data
= malloc(s
.st_size
);
494 if (read(fd
, data
, s
.st_size
) == s
.st_size
)
495 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
497 ULOG_ERR("failed to read full module %s\n", path
);
505 static void load_moddeps(struct module
*_m
)
510 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
516 m
= find_module(dep
);
519 ULOG_ERR("failed to find dependency %s\n", dep
);
520 if (m
&& (m
->state
!= LOADED
)) {
525 dep
= dep
+ strlen(dep
) + 1;
529 static int iterations
= 0;
530 static int load_modprobe(void)
535 avl_for_each_element(&modules
, m
, avl
)
536 if (m
->state
== PROBE
)
542 avl_for_each_element(&modules
, m
, avl
) {
543 if ((m
->state
== PROBE
) && (!deps_available(m
, 0))) {
544 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
553 if ((m
->state
== PROBE
) || m
->error
)
562 static int print_insmod_usage(void)
564 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
569 static int print_usage(char *arg
)
571 ULOG_INFO("Usage:\n\t%s module\n", arg
);
576 static int main_insmod(int argc
, char **argv
)
578 char *name
, *cur
, *options
;
582 return print_insmod_usage();
584 name
= get_module_name(argv
[1]);
586 ULOG_ERR("cannot find module - %s\n", argv
[1]);
590 if (scan_loaded_modules())
593 if (find_module(name
)) {
594 ULOG_ERR("module is already loaded - %s\n", name
);
601 for (len
= 0, i
= 2; i
< argc
; i
++)
602 len
+= strlen(argv
[i
]) + 1;
604 options
= malloc(len
);
607 for (i
= 2; i
< argc
; i
++) {
612 cur
+= sprintf(cur
, "%s", argv
[i
]);
615 if (init_module_folders()) {
616 fprintf(stderr
, "Failed to find the folder holding the modules\n");
620 if (get_module_path(argv
[1])) {
622 } else if (!get_module_path(name
)) {
623 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
627 ret
= insert_module(get_module_path(name
), options
);
631 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
636 static int main_rmmod(int argc
, char **argv
)
643 return print_usage("rmmod");
645 if (scan_loaded_modules())
648 name
= get_module_name(argv
[1]);
649 m
= find_module(name
);
651 ULOG_ERR("module is not loaded\n");
654 ret
= syscall(__NR_delete_module
, m
->name
, 0);
657 ULOG_ERR("unloading the module failed\n");
664 static int main_lsmod(int argc
, char **argv
)
669 if (scan_loaded_modules())
672 avl_for_each_element(&modules
, m
, avl
)
673 if (m
->state
== LOADED
) {
674 printf("%-20s%8d%3d ",
675 m
->name
, m
->size
, m
->usage
);
676 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
680 dep
= dep
+ strlen(dep
) + 1;
693 static int main_modinfo(int argc
, char **argv
)
699 return print_usage("modinfo");
701 if (scan_module_folders())
704 name
= get_module_name(argv
[1]);
705 m
= find_module(name
);
707 ULOG_ERR("cannot find module - %s\n", argv
[1]);
711 name
= get_module_path(m
->name
);
713 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
722 static int main_modprobe(int argc
, char **argv
)
729 for (i
= 1; i
< argc
; i
++)
730 if (argv
[i
][0] != '-') {
735 return print_usage("modprobe");
737 if (scan_loaded_modules())
740 if (scan_module_folders())
743 name
= get_module_name(mod
);
744 m
= find_module(name
);
745 if (m
&& m
->state
== LOADED
) {
746 ULOG_ERR("%s is already loaded\n", name
);
749 ULOG_ERR("failed to find a module named %s\n", name
);
755 fail
= load_modprobe();
758 ULOG_ERR("%d module%s could not be probed\n",
759 fail
, (fail
== 1) ? ("") : ("s"));
761 avl_for_each_element(&modules
, m
, avl
)
762 if ((m
->state
== PROBE
) || m
->error
)
763 ULOG_ERR("- %s\n", m
->name
);
772 static int main_loader(int argc
, char **argv
)
774 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
775 char *dir
= "/etc/modules.d/";
784 path
= malloc(strlen(dir
) + 2);
788 if (scan_loaded_modules()) {
793 if (scan_module_folders()) {
798 ULOG_INFO("loading kernel modules from %s\n", path
);
800 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
803 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
804 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
809 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
813 while (getline(&mod
, &mod_len
, fp
) > 0) {
814 char *nl
= strchr(mod
, '\n');
821 opts
= strchr(mod
, ' ');
825 m
= find_module(get_module_name(mod
));
826 if (!m
|| (m
->state
== LOADED
))
830 m
->opts
= strdup(opts
);
832 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
840 fail
= load_modprobe();
843 ULOG_ERR("%d module%s could not be probed\n",
844 fail
, (fail
== 1) ? ("") : ("s"));
846 avl_for_each_element(&modules
, m
, avl
)
847 if ((m
->state
== PROBE
) || (m
->error
))
848 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
850 ULOG_INFO("done loading kernel modules from %s\n", path
);
860 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
865 while (*s1
&& ((*s1
== *s2
) ||
866 ((*s1
== '_') && (*s2
== '-')) ||
867 ((*s1
== '-') && (*s2
== '_'))))
873 return *(const unsigned char *)s1
- *(const unsigned char *)s2
;
876 int main(int argc
, char **argv
)
878 char *exec
= basename(*argv
);
880 avl_init(&modules
, avl_modcmp
, false, NULL
);
881 if (!strcmp(exec
, "insmod"))
882 return main_insmod(argc
, argv
);
884 if (!strcmp(exec
, "rmmod"))
885 return main_rmmod(argc
, argv
);
887 if (!strcmp(exec
, "lsmod"))
888 return main_lsmod(argc
, argv
);
890 if (!strcmp(exec
, "modinfo"))
891 return main_modinfo(argc
, argv
);
893 if (!strcmp(exec
, "modprobe"))
894 return main_modprobe(argc
, argv
);
896 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
897 return main_loader(argc
, argv
);