422c895f1f40daf19d8e9aacfc44db5964496ae6
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/"
40 /* duplicated from in-kernel include/linux/module.h */
41 #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
58 int refcnt
; /* number of references from module_node.m */
67 static struct avl_tree modules
;
69 static char **module_folders
= NULL
;
71 static void free_module(struct module
*m
);
73 static int init_module_folders(void)
78 char *s
, *e
, *p
, path
[330], ldpath
[256];
81 s
= getenv("LD_LIBRARY_PATH");
84 e
+= snprintf(ldpath
, sizeof(ldpath
), "%s:", s
);
86 e
+= snprintf(e
, sizeof(ldpath
) - (e
- ldpath
), "/lib");
90 for (s
= p
= ldpath
; p
<= e
; p
++) {
91 if (*p
!= ':' && *p
!= '\0')
95 snprintf(path
, sizeof(path
), "%s" DEF_MOD_PATH
, s
, ver
.release
);
97 if (!stat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
98 module_folders
= realloc(module_folders
, sizeof(p
) * (n
+ 2));
100 if (!module_folders
) {
101 ULOG_ERR("out of memory\n");
105 module_folders
[n
++] = strdup(path
);
114 module_folders
[n
] = NULL
;
118 static struct module
*find_module(const char *name
)
120 struct module_node
*mn
;
121 mn
= avl_find_element(&modules
, name
, mn
, avl
);
128 static void free_modules(void)
130 struct module_node
*mn
, *tmp
;
132 avl_remove_all_elements(&modules
, mn
, avl
, tmp
) {
133 struct module
*m
= mn
->m
;
142 static char* get_module_path(char *name
)
145 static char path
[256];
148 if (!stat(name
, &s
) && S_ISREG(s
.st_mode
))
151 for (p
= module_folders
; *p
; p
++) {
152 snprintf(path
, sizeof(path
), "%s%s.ko", *p
, name
);
153 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
160 static char* get_module_name(char *path
)
162 static char name
[MODULE_NAME_LEN
];
165 strncpy(name
, basename(path
), sizeof(name
) - 1);
167 t
= strstr(name
, ".ko");
174 static int elf64_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
176 const char *secnames
;
181 e
= (Elf64_Ehdr
*) map
;
182 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
184 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
185 for (i
= 0; i
< e
->e_shnum
; i
++) {
186 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
187 *size
= sh
[i
].sh_size
;
188 *offset
= sh
[i
].sh_offset
;
196 static int elf32_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
198 const char *secnames
;
203 e
= (Elf32_Ehdr
*) map
;
204 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
206 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
207 for (i
= 0; i
< e
->e_shnum
; i
++) {
208 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
209 *size
= sh
[i
].sh_size
;
210 *offset
= sh
[i
].sh_offset
;
218 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
220 int clazz
= map
[EI_CLASS
];
221 int endian
= map
[EI_DATA
];
223 #if __BYTE_ORDER == __LITTLE_ENDIAN
224 if (endian
!= ELFDATA2LSB
)
225 #elif __BYTE_ORDER == __BIG_ENDIAN
226 if (endian
!= ELFDATA2MSB
)
228 #error "unsupported endian"
231 ULOG_ERR("invalid endianess: %d\n", endian
);
235 if (clazz
== ELFCLASS32
)
236 return elf32_find_section(map
, section
, offset
, size
);
237 else if (clazz
== ELFCLASS64
)
238 return elf64_find_section(map
, section
, offset
, size
);
240 ULOG_ERR("unknown elf format %d\n", clazz
);
245 static struct module_node
*
246 alloc_module_node(const char *name
, struct module
*m
, bool is_alias
)
248 struct module_node
*mn
;
251 mn
= calloc_a(sizeof(*mn
),
252 &_name
, strlen(name
) + 1);
254 mn
->avl
.key
= strcpy(_name
, name
);
256 mn
->is_alias
= is_alias
;
257 avl_insert(&modules
, &mn
->avl
);
263 static struct module
*
264 alloc_module(const char *name
, const char * const *aliases
, int naliases
, const char *depends
, int size
)
270 m
= calloc_a(sizeof(*m
),
271 &_name
, strlen(name
) + 1,
272 &_dep
, depends
? strlen(depends
) + 2 : 0);
276 m
->name
= strcpy(_name
, name
);
280 m
->depends
= strcpy(_dep
, depends
);
290 alloc_module_node(m
->name
, m
, false);
291 for (i
= 0; i
< naliases
; i
++)
292 alloc_module_node(aliases
[i
], m
, true);
297 static void free_module(struct module
*m
)
304 static int scan_loaded_modules(void)
310 fp
= fopen("/proc/modules", "r");
312 ULOG_ERR("failed to open /proc/modules\n");
316 while (getline(&buf
, &buf_len
, fp
) > 0) {
320 m
.name
= strtok(buf
, " ");
321 m
.size
= atoi(strtok(NULL
, " "));
322 m
.usage
= atoi(strtok(NULL
, " "));
323 m
.depends
= strtok(NULL
, " ");
325 if (!m
.name
|| !m
.depends
)
328 n
= find_module(m
.name
);
330 /* possibly a module outside /lib/modules/<ver>/ */
331 n
= alloc_module(m
.name
, NULL
, 0, m
.depends
, m
.size
);
342 static struct module
* get_module_info(const char *module
, const char *name
)
344 int fd
= open(module
, O_RDONLY
);
345 unsigned int offset
, size
;
346 char *map
= MAP_FAILED
, *strings
, *dep
= NULL
;
347 const char **aliases
= NULL
;
349 struct module
*m
= NULL
;
353 ULOG_ERR("failed to open %s\n", module
);
357 if (fstat(fd
, &s
) == -1) {
358 ULOG_ERR("failed to stat %s\n", module
);
362 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
363 if (map
== MAP_FAILED
) {
364 ULOG_ERR("failed to mmap %s\n", module
);
368 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
369 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
373 strings
= map
+ offset
;
380 if (strings
>= map
+ offset
+ size
)
382 sep
= strstr(strings
, "=");
387 if (!strncmp(strings
, "depends=", len
+ 1))
389 else if (!strncmp(strings
, "alias=", len
+ 1)) {
390 aliases
= realloc(aliases
, sizeof(sep
) * (naliases
+ 1));
392 ULOG_ERR("out of memory\n");
396 aliases
[naliases
++] = sep
;
398 strings
= &sep
[strlen(sep
)];
401 m
= alloc_module(name
, aliases
, naliases
, dep
, s
.st_size
);
407 if (map
!= MAP_FAILED
)
408 munmap(map
, s
.st_size
);
418 static int scan_module_folder(const char *dir
)
420 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
427 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
428 sprintf(path
, "%s*.ko", dir
);
430 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
433 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
434 char *name
= get_module_name(gl
.gl_pathv
[j
]);
440 m
= find_module(name
);
442 if (!get_module_info(gl
.gl_pathv
[j
], name
))
452 static int scan_module_folders(void)
457 if (init_module_folders())
460 for (p
= module_folders
; *p
; p
++)
461 rv
|= scan_module_folder(*p
);
466 static int print_modinfo(char *module
)
468 int fd
= open(module
, O_RDONLY
);
469 unsigned int offset
, size
;
471 char *map
= MAP_FAILED
, *strings
;
475 ULOG_ERR("failed to open %s\n", module
);
479 if (fstat(fd
, &s
) == -1) {
480 ULOG_ERR("failed to stat %s\n", module
);
484 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
485 if (map
== MAP_FAILED
) {
486 ULOG_ERR("failed to mmap %s\n", module
);
490 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
491 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
495 strings
= map
+ offset
;
496 printf("module:\t\t%s\n", module
);
503 if (strings
>= map
+ offset
+ size
)
505 sep
= strstr(strings
, "=");
508 dup
= strndup(strings
, sep
- strings
);
510 if (strncmp(strings
, "parm", 4)) {
512 printf("%s:\t\t%s\n", dup
, sep
);
514 printf("%s:\t%s\n", dup
, sep
);
516 strings
= &sep
[strlen(sep
)];
524 if (map
!= MAP_FAILED
)
525 munmap(map
, s
.st_size
);
533 static int deps_available(struct module
*m
, int verbose
)
538 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
544 m
= find_module(dep
);
547 ULOG_ERR("missing dependency %s\n", dep
);
548 if (verbose
&& m
&& (m
->state
!= LOADED
))
549 ULOG_ERR("dependency not loaded %s\n", dep
);
550 if (!m
|| (m
->state
!= LOADED
))
552 dep
+= strlen(dep
) + 1;
558 static int insert_module(char *path
, const char *options
)
564 if (stat(path
, &s
)) {
565 ULOG_ERR("missing module %s\n", path
);
569 fd
= open(path
, O_RDONLY
);
571 ULOG_ERR("cannot open %s\n", path
);
575 data
= malloc(s
.st_size
);
577 ULOG_ERR("out of memory\n");
581 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
582 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
587 ULOG_ERR("failed to read full module %s\n", path
);
596 static void load_moddeps(struct module
*_m
)
601 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
607 m
= find_module(dep
);
610 ULOG_ERR("failed to find dependency %s\n", dep
);
611 if (m
&& (m
->state
!= LOADED
)) {
616 dep
= dep
+ strlen(dep
) + 1;
620 static int load_modprobe(bool allow_load_retry
)
622 int loaded
, skipped
, failed
;
623 struct module_node
*mn
;
625 bool load_retry
= false;
626 static bool first_iteration
= true;
628 avl_for_each_element(&modules
, mn
, avl
) {
632 if (m
->state
== PROBE
)
640 avl_for_each_element(&modules
, mn
, avl
) {
644 if ((m
->state
== PROBE
) && (!deps_available(m
, 0)) && (!m
->error
|| load_retry
)) {
645 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
657 else if (m
->state
== PROBE
)
661 if (allow_load_retry
) {
662 /* if we can't load anything else let's try to load failed modules */
663 load_retry
= loaded
? (failed
&& !skipped
) : (failed
&& !load_retry
&& !first_iteration
);
666 first_iteration
= false;
667 } while (loaded
|| load_retry
);
669 return skipped
+ failed
;
672 static int print_insmod_usage(void)
674 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
679 static int print_modprobe_usage(void)
681 ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
686 static int print_usage(char *arg
)
688 ULOG_INFO("Usage:\n\t%s module\n", arg
);
693 static int main_insmod(int argc
, char **argv
)
695 char *name
, *cur
, *options
;
699 return print_insmod_usage();
701 name
= get_module_name(argv
[1]);
703 ULOG_ERR("cannot find module - %s\n", argv
[1]);
707 if (scan_loaded_modules())
710 if (find_module(name
)) {
711 ULOG_ERR("module is already loaded - %s\n", name
);
718 for (len
= 0, i
= 2; i
< argc
; i
++)
719 len
+= strlen(argv
[i
]) + 1;
721 options
= malloc(len
);
723 ULOG_ERR("out of memory\n");
730 for (i
= 2; i
< argc
; i
++) {
735 cur
+= sprintf(cur
, "%s", argv
[i
]);
738 if (init_module_folders()) {
739 fprintf(stderr
, "Failed to find the folder holding the modules\n");
744 if (get_module_path(argv
[1])) {
746 } else if (!get_module_path(name
)) {
747 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
752 ret
= insert_module(get_module_path(name
), options
);
754 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
761 static int main_rmmod(int argc
, char **argv
)
768 return print_usage("rmmod");
770 if (scan_loaded_modules())
773 name
= get_module_name(argv
[1]);
774 m
= find_module(name
);
776 ULOG_ERR("module is not loaded\n");
779 ret
= syscall(__NR_delete_module
, m
->name
, 0);
782 ULOG_ERR("unloading the module failed\n");
789 static int main_lsmod(int argc
, char **argv
)
791 struct module_node
*mn
;
795 if (scan_loaded_modules())
798 avl_for_each_element(&modules
, mn
, avl
) {
802 if (m
->state
== LOADED
) {
803 printf("%-20s%8d%3d ",
804 m
->name
, m
->size
, m
->usage
);
805 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
809 dep
= dep
+ strlen(dep
) + 1;
823 static int main_modinfo(int argc
, char **argv
)
829 return print_usage("modinfo");
831 if (scan_module_folders())
834 name
= get_module_name(argv
[1]);
835 m
= find_module(name
);
837 ULOG_ERR("cannot find module - %s\n", argv
[1]);
841 name
= get_module_path(m
->name
);
843 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
852 static int main_modprobe(int argc
, char **argv
)
854 struct module_node
*mn
;
861 while ((opt
= getopt(argc
, argv
, "q")) != -1 ) {
863 case 'q': /* shhhh! */
867 return print_modprobe_usage();
873 return print_modprobe_usage(); /* expected module after options */
877 if (scan_module_folders())
880 if (scan_loaded_modules())
883 name
= get_module_name(mod
);
884 m
= find_module(name
);
885 if (m
&& m
->state
== LOADED
) {
887 ULOG_ERR("%s is already loaded\n", name
);
891 ULOG_ERR("failed to find a module named %s\n", name
);
898 fail
= load_modprobe(true);
901 ULOG_ERR("%d module%s could not be probed\n",
902 fail
, (fail
== 1) ? ("") : ("s"));
904 avl_for_each_element(&modules
, mn
, avl
) {
908 if ((m
->state
== PROBE
) || m
->error
)
909 ULOG_ERR("- %s\n", m
->name
);
919 static int main_loader(int argc
, char **argv
)
921 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
922 char *dir
= "/etc/modules.d/";
923 struct module_node
*mn
;
932 path
= malloc(strlen(dir
) + 2);
934 ULOG_ERR("out of memory\n");
941 if (scan_module_folders()) {
946 if (scan_loaded_modules()) {
951 ULOG_INFO("loading kernel modules from %s\n", path
);
953 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
956 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
957 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
962 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
966 while (getline(&mod
, &mod_len
, fp
) > 0) {
967 char *nl
= strchr(mod
, '\n');
974 opts
= strchr(mod
, ' ');
978 m
= find_module(get_module_name(mod
));
979 if (!m
|| (m
->state
== LOADED
))
983 m
->opts
= strdup(opts
);
985 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
986 load_modprobe(false);
993 fail
= load_modprobe(true);
996 ULOG_ERR("%d module%s could not be probed\n",
997 fail
, (fail
== 1) ? ("") : ("s"));
999 avl_for_each_element(&modules
, mn
, avl
) {
1003 if ((m
->state
== PROBE
) || (m
->error
))
1004 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
1007 ULOG_INFO("done loading kernel modules from %s\n", path
);
1017 static inline char weight(char c
)
1019 return c
== '_' ? '-' : c
;
1022 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
1024 const char *s1
= k1
;
1025 const char *s2
= k2
;
1027 while (*s1
&& (weight(*s1
) == weight(*s2
)))
1033 return (unsigned char)weight(*s1
) - (unsigned char)weight(*s2
);
1036 int main(int argc
, char **argv
)
1038 char *exec
= basename(*argv
);
1040 avl_init(&modules
, avl_modcmp
, false, NULL
);
1041 if (!strcmp(exec
, "insmod"))
1042 return main_insmod(argc
, argv
);
1044 if (!strcmp(exec
, "rmmod"))
1045 return main_rmmod(argc
, argv
);
1047 if (!strcmp(exec
, "lsmod"))
1048 return main_lsmod(argc
, argv
);
1050 if (!strcmp(exec
, "modinfo"))
1051 return main_modinfo(argc
, argv
);
1053 if (!strcmp(exec
, "modprobe"))
1054 return main_modprobe(argc
, argv
);
1056 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
1057 return main_loader(argc
, argv
);