2e920ef40ca6c6e966ece78784ecd35c4420e68d
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
[256], 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 iterations
= 0;
621 static int load_modprobe(void)
624 struct module_node
*mn
;
627 avl_for_each_element(&modules
, mn
, avl
) {
631 if (m
->state
== PROBE
)
638 avl_for_each_element(&modules
, mn
, avl
) {
642 if ((m
->state
== PROBE
) && (!deps_available(m
, 0)) && m
->error
< 2) {
643 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
651 ULOG_ERR("failed to load %s\n", m
->name
);
654 if ((m
->state
== PROBE
) || m
->error
)
663 static int print_insmod_usage(void)
665 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
670 static int print_modprobe_usage(void)
672 ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
677 static int print_usage(char *arg
)
679 ULOG_INFO("Usage:\n\t%s module\n", arg
);
684 static int main_insmod(int argc
, char **argv
)
686 char *name
, *cur
, *options
;
690 return print_insmod_usage();
692 name
= get_module_name(argv
[1]);
694 ULOG_ERR("cannot find module - %s\n", argv
[1]);
698 if (scan_loaded_modules())
701 if (find_module(name
)) {
702 ULOG_ERR("module is already loaded - %s\n", name
);
709 for (len
= 0, i
= 2; i
< argc
; i
++)
710 len
+= strlen(argv
[i
]) + 1;
712 options
= malloc(len
);
714 ULOG_ERR("out of memory\n");
721 for (i
= 2; i
< argc
; i
++) {
726 cur
+= sprintf(cur
, "%s", argv
[i
]);
729 if (init_module_folders()) {
730 fprintf(stderr
, "Failed to find the folder holding the modules\n");
735 if (get_module_path(argv
[1])) {
737 } else if (!get_module_path(name
)) {
738 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
743 ret
= insert_module(get_module_path(name
), options
);
745 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
752 static int main_rmmod(int argc
, char **argv
)
759 return print_usage("rmmod");
761 if (scan_loaded_modules())
764 name
= get_module_name(argv
[1]);
765 m
= find_module(name
);
767 ULOG_ERR("module is not loaded\n");
770 ret
= syscall(__NR_delete_module
, m
->name
, 0);
773 ULOG_ERR("unloading the module failed\n");
780 static int main_lsmod(int argc
, char **argv
)
782 struct module_node
*mn
;
786 if (scan_loaded_modules())
789 avl_for_each_element(&modules
, mn
, avl
) {
793 if (m
->state
== LOADED
) {
794 printf("%-20s%8d%3d ",
795 m
->name
, m
->size
, m
->usage
);
796 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
800 dep
= dep
+ strlen(dep
) + 1;
814 static int main_modinfo(int argc
, char **argv
)
820 return print_usage("modinfo");
822 if (scan_module_folders())
825 name
= get_module_name(argv
[1]);
826 m
= find_module(name
);
828 ULOG_ERR("cannot find module - %s\n", argv
[1]);
832 name
= get_module_path(m
->name
);
834 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
843 static int main_modprobe(int argc
, char **argv
)
845 struct module_node
*mn
;
852 while ((opt
= getopt(argc
, argv
, "q")) != -1 ) {
854 case 'q': /* shhhh! */
858 return print_modprobe_usage();
864 return print_modprobe_usage(); /* expected module after options */
868 if (scan_module_folders())
871 if (scan_loaded_modules())
874 name
= get_module_name(mod
);
875 m
= find_module(name
);
876 if (m
&& m
->state
== LOADED
) {
878 ULOG_ERR("%s is already loaded\n", name
);
882 ULOG_ERR("failed to find a module named %s\n", name
);
889 fail
= load_modprobe();
892 ULOG_ERR("%d module%s could not be probed\n",
893 fail
, (fail
== 1) ? ("") : ("s"));
895 avl_for_each_element(&modules
, mn
, avl
) {
899 if ((m
->state
== PROBE
) || m
->error
)
900 ULOG_ERR("- %s\n", m
->name
);
910 static int main_loader(int argc
, char **argv
)
912 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
913 char *dir
= "/etc/modules.d/";
914 struct module_node
*mn
;
923 path
= malloc(strlen(dir
) + 2);
925 ULOG_ERR("out of memory\n");
932 if (scan_module_folders()) {
937 if (scan_loaded_modules()) {
942 ULOG_INFO("loading kernel modules from %s\n", path
);
944 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
947 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
948 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
953 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
957 while (getline(&mod
, &mod_len
, fp
) > 0) {
958 char *nl
= strchr(mod
, '\n');
965 opts
= strchr(mod
, ' ');
969 m
= find_module(get_module_name(mod
));
970 if (!m
|| (m
->state
== LOADED
))
974 m
->opts
= strdup(opts
);
976 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
984 fail
= load_modprobe();
987 ULOG_ERR("%d module%s could not be probed\n",
988 fail
, (fail
== 1) ? ("") : ("s"));
990 avl_for_each_element(&modules
, mn
, avl
) {
994 if ((m
->state
== PROBE
) || (m
->error
))
995 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
998 ULOG_INFO("done loading kernel modules from %s\n", path
);
1008 static inline char weight(char c
)
1010 return c
== '_' ? '-' : c
;
1013 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
1015 const char *s1
= k1
;
1016 const char *s2
= k2
;
1018 while (*s1
&& (weight(*s1
) == weight(*s2
)))
1024 return (unsigned char)weight(*s1
) - (unsigned char)weight(*s2
);
1027 int main(int argc
, char **argv
)
1029 char *exec
= basename(*argv
);
1031 avl_init(&modules
, avl_modcmp
, false, NULL
);
1032 if (!strcmp(exec
, "insmod"))
1033 return main_insmod(argc
, argv
);
1035 if (!strcmp(exec
, "rmmod"))
1036 return main_rmmod(argc
, argv
);
1038 if (!strcmp(exec
, "lsmod"))
1039 return main_lsmod(argc
, argv
);
1041 if (!strcmp(exec
, "modinfo"))
1042 return main_modinfo(argc
, argv
);
1044 if (!strcmp(exec
, "modprobe"))
1045 return main_modprobe(argc
, argv
);
1047 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
1048 return main_loader(argc
, argv
);