3dc7665bd207237d8b9b9474e802174250d8f952
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/"
56 int refcnt
; /* number of references from module_node.m */
65 static struct avl_tree modules
;
67 static char **module_folders
= NULL
;
69 static void free_module(struct module
*m
);
71 static int init_module_folders(void)
76 char *s
, *e
, *p
, path
[256], ldpath
[256];
79 s
= getenv("LD_LIBRARY_PATH");
82 e
+= snprintf(ldpath
, sizeof(ldpath
), "%s:", s
);
84 e
+= snprintf(e
, sizeof(ldpath
) - (e
- ldpath
), "/lib");
88 for (s
= p
= ldpath
; p
<= e
; p
++) {
89 if (*p
!= ':' && *p
!= '\0')
93 snprintf(path
, sizeof(path
), "%s" DEF_MOD_PATH
, s
, ver
.release
);
95 if (!stat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
96 module_folders
= realloc(module_folders
, sizeof(p
) * (n
+ 2));
101 module_folders
[n
++] = strdup(path
);
110 module_folders
[n
] = NULL
;
114 static struct module
*find_module(const char *name
)
116 struct module_node
*mn
;
117 mn
= avl_find_element(&modules
, name
, mn
, avl
);
124 static void free_modules(void)
126 struct module_node
*mn
, *tmp
;
128 avl_remove_all_elements(&modules
, mn
, avl
, tmp
) {
129 struct module
*m
= mn
->m
;
138 static char* get_module_path(char *name
)
141 static char path
[256];
144 if (!stat(name
, &s
) && S_ISREG(s
.st_mode
))
147 for (p
= module_folders
; *p
; p
++) {
148 snprintf(path
, sizeof(path
), "%s%s.ko", *p
, name
);
149 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
156 static char* get_module_name(char *path
)
158 static char name
[33];
161 strncpy(name
, basename(path
), sizeof(name
) - 1);
163 t
= strstr(name
, ".ko");
170 static int elf64_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
172 const char *secnames
;
177 e
= (Elf64_Ehdr
*) map
;
178 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
180 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
181 for (i
= 0; i
< e
->e_shnum
; i
++) {
182 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
183 *size
= sh
[i
].sh_size
;
184 *offset
= sh
[i
].sh_offset
;
192 static int elf32_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
194 const char *secnames
;
199 e
= (Elf32_Ehdr
*) map
;
200 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
202 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
203 for (i
= 0; i
< e
->e_shnum
; i
++) {
204 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
205 *size
= sh
[i
].sh_size
;
206 *offset
= sh
[i
].sh_offset
;
214 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
216 int clazz
= map
[EI_CLASS
];
217 int endian
= map
[EI_DATA
];
219 #if __BYTE_ORDER == __LITTLE_ENDIAN
220 if (endian
!= ELFDATA2LSB
)
221 #elif __BYTE_ORDER == __BIG_ENDIAN
222 if (endian
!= ELFDATA2MSB
)
224 #error "unsupported endian"
227 ULOG_ERR("invalid endianess: %d\n", endian
);
231 if (clazz
== ELFCLASS32
)
232 return elf32_find_section(map
, section
, offset
, size
);
233 else if (clazz
== ELFCLASS64
)
234 return elf64_find_section(map
, section
, offset
, size
);
236 ULOG_ERR("unknown elf format %d\n", clazz
);
241 static struct module_node
*
242 alloc_module_node(const char *name
, struct module
*m
, bool is_alias
)
244 struct module_node
*mn
;
247 mn
= calloc_a(sizeof(*mn
),
248 &_name
, strlen(name
) + 1);
250 mn
->avl
.key
= strcpy(_name
, name
);
252 mn
->is_alias
= is_alias
;
253 avl_insert(&modules
, &mn
->avl
);
259 static struct module
*
260 alloc_module(const char *name
, const char * const *aliases
, int naliases
, const char *depends
, int size
)
266 m
= calloc_a(sizeof(*m
),
267 &_name
, strlen(name
) + 1,
268 &_dep
, depends
? strlen(depends
) + 2 : 0);
272 m
->name
= strcpy(_name
, name
);
276 m
->depends
= strcpy(_dep
, depends
);
286 alloc_module_node(m
->name
, m
, false);
287 for (i
= 0; i
< naliases
; i
++)
288 alloc_module_node(aliases
[i
], m
, true);
293 static void free_module(struct module
*m
)
300 static int scan_loaded_modules(void)
306 fp
= fopen("/proc/modules", "r");
308 ULOG_ERR("failed to open /proc/modules\n");
312 while (getline(&buf
, &buf_len
, fp
) > 0) {
316 m
.name
= strtok(buf
, " ");
317 m
.size
= atoi(strtok(NULL
, " "));
318 m
.usage
= atoi(strtok(NULL
, " "));
319 m
.depends
= strtok(NULL
, " ");
321 if (!m
.name
|| !m
.depends
)
324 n
= find_module(m
.name
);
326 /* possibly a module outside /lib/modules/<ver>/ */
327 n
= alloc_module(m
.name
, NULL
, 0, m
.depends
, m
.size
);
338 static struct module
* get_module_info(const char *module
, const char *name
)
340 int fd
= open(module
, O_RDONLY
);
341 unsigned int offset
, size
;
342 char *map
= MAP_FAILED
, *strings
, *dep
= NULL
;
343 const char *aliases
[32] = { 0 };
345 struct module
*m
= NULL
;
349 ULOG_ERR("failed to open %s\n", module
);
353 if (fstat(fd
, &s
) == -1) {
354 ULOG_ERR("failed to stat %s\n", module
);
358 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
359 if (map
== MAP_FAILED
) {
360 ULOG_ERR("failed to mmap %s\n", module
);
364 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
365 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
369 strings
= map
+ offset
;
376 if (strings
>= map
+ offset
+ size
)
378 sep
= strstr(strings
, "=");
383 if (!strncmp(strings
, "depends=", len
+ 1))
385 else if (!strncmp(strings
, "alias=", len
+ 1)) {
386 if (naliases
< ARRAY_SIZE(aliases
))
387 aliases
[naliases
++] = sep
;
389 ULOG_WARN("module %s has more than %d aliases: truncated",
390 name
, ARRAY_SIZE(aliases
));
392 strings
= &sep
[strlen(sep
)];
395 m
= alloc_module(name
, aliases
, naliases
, dep
, s
.st_size
);
401 if (map
!= MAP_FAILED
)
402 munmap(map
, s
.st_size
);
410 static int scan_module_folder(const char *dir
)
412 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
419 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
420 sprintf(path
, "%s*.ko", dir
);
422 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
425 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
426 char *name
= get_module_name(gl
.gl_pathv
[j
]);
432 m
= find_module(name
);
434 get_module_info(gl
.gl_pathv
[j
], name
);
442 static int scan_module_folders(void)
447 if (init_module_folders())
450 for (p
= module_folders
; *p
; p
++)
451 rv
|= scan_module_folder(*p
);
456 static int print_modinfo(char *module
)
458 int fd
= open(module
, O_RDONLY
);
459 unsigned int offset
, size
;
461 char *map
= MAP_FAILED
, *strings
;
465 ULOG_ERR("failed to open %s\n", module
);
469 if (fstat(fd
, &s
) == -1) {
470 ULOG_ERR("failed to stat %s\n", module
);
474 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
475 if (map
== MAP_FAILED
) {
476 ULOG_ERR("failed to mmap %s\n", module
);
480 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
481 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
485 strings
= map
+ offset
;
486 printf("module:\t\t%s\n", module
);
493 if (strings
>= map
+ offset
+ size
)
495 sep
= strstr(strings
, "=");
498 dup
= strndup(strings
, sep
- strings
);
500 if (strncmp(strings
, "parm", 4)) {
502 printf("%s:\t\t%s\n", dup
, sep
);
504 printf("%s:\t%s\n", dup
, sep
);
506 strings
= &sep
[strlen(sep
)];
514 if (map
!= MAP_FAILED
)
515 munmap(map
, s
.st_size
);
523 static int deps_available(struct module
*m
, int verbose
)
528 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
534 m
= find_module(dep
);
537 ULOG_ERR("missing dependency %s\n", dep
);
538 if (verbose
&& m
&& (m
->state
!= LOADED
))
539 ULOG_ERR("dependency not loaded %s\n", dep
);
540 if (!m
|| (m
->state
!= LOADED
))
542 dep
+= strlen(dep
) + 1;
548 static int insert_module(char *path
, const char *options
)
554 if (stat(path
, &s
)) {
555 ULOG_ERR("missing module %s\n", path
);
559 fd
= open(path
, O_RDONLY
);
561 ULOG_ERR("cannot open %s\n", path
);
565 data
= malloc(s
.st_size
);
569 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
570 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
575 ULOG_ERR("failed to read full module %s\n", path
);
584 static void load_moddeps(struct module
*_m
)
589 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
595 m
= find_module(dep
);
598 ULOG_ERR("failed to find dependency %s\n", dep
);
599 if (m
&& (m
->state
!= LOADED
)) {
604 dep
= dep
+ strlen(dep
) + 1;
608 static int iterations
= 0;
609 static int load_modprobe(void)
612 struct module_node
*mn
;
615 avl_for_each_element(&modules
, mn
, avl
) {
619 if (m
->state
== PROBE
)
626 avl_for_each_element(&modules
, mn
, avl
) {
630 if ((m
->state
== PROBE
) && (!deps_available(m
, 0))) {
631 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
640 if ((m
->state
== PROBE
) || m
->error
)
649 static int print_insmod_usage(void)
651 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
656 static int print_modprobe_usage(void)
658 ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
663 static int print_usage(char *arg
)
665 ULOG_INFO("Usage:\n\t%s module\n", arg
);
670 static int main_insmod(int argc
, char **argv
)
672 char *name
, *cur
, *options
;
676 return print_insmod_usage();
678 name
= get_module_name(argv
[1]);
680 ULOG_ERR("cannot find module - %s\n", argv
[1]);
684 if (scan_loaded_modules())
687 if (find_module(name
)) {
688 ULOG_ERR("module is already loaded - %s\n", name
);
695 for (len
= 0, i
= 2; i
< argc
; i
++)
696 len
+= strlen(argv
[i
]) + 1;
698 options
= malloc(len
);
706 for (i
= 2; i
< argc
; i
++) {
711 cur
+= sprintf(cur
, "%s", argv
[i
]);
714 if (init_module_folders()) {
715 fprintf(stderr
, "Failed to find the folder holding the modules\n");
720 if (get_module_path(argv
[1])) {
722 } else if (!get_module_path(name
)) {
723 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
728 ret
= insert_module(get_module_path(name
), options
);
730 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
737 static int main_rmmod(int argc
, char **argv
)
744 return print_usage("rmmod");
746 if (scan_loaded_modules())
749 name
= get_module_name(argv
[1]);
750 m
= find_module(name
);
752 ULOG_ERR("module is not loaded\n");
755 ret
= syscall(__NR_delete_module
, m
->name
, 0);
758 ULOG_ERR("unloading the module failed\n");
765 static int main_lsmod(int argc
, char **argv
)
767 struct module_node
*mn
;
771 if (scan_loaded_modules())
774 avl_for_each_element(&modules
, mn
, avl
) {
778 if (m
->state
== LOADED
) {
779 printf("%-20s%8d%3d ",
780 m
->name
, m
->size
, m
->usage
);
781 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
785 dep
= dep
+ strlen(dep
) + 1;
799 static int main_modinfo(int argc
, char **argv
)
805 return print_usage("modinfo");
807 if (scan_module_folders())
810 name
= get_module_name(argv
[1]);
811 m
= find_module(name
);
813 ULOG_ERR("cannot find module - %s\n", argv
[1]);
817 name
= get_module_path(m
->name
);
819 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
828 static int main_modprobe(int argc
, char **argv
)
830 struct module_node
*mn
;
837 while ((opt
= getopt(argc
, argv
, "q")) != -1 ) {
839 case 'q': /* shhhh! */
843 return print_modprobe_usage();
849 return print_modprobe_usage(); /* expected module after options */
853 if (scan_module_folders())
856 if (scan_loaded_modules())
859 name
= get_module_name(mod
);
860 m
= find_module(name
);
861 if (m
&& m
->state
== LOADED
) {
863 ULOG_ERR("%s is already loaded\n", name
);
867 ULOG_ERR("failed to find a module named %s\n", name
);
874 fail
= load_modprobe();
877 ULOG_ERR("%d module%s could not be probed\n",
878 fail
, (fail
== 1) ? ("") : ("s"));
880 avl_for_each_element(&modules
, mn
, avl
) {
884 if ((m
->state
== PROBE
) || m
->error
)
885 ULOG_ERR("- %s\n", m
->name
);
895 static int main_loader(int argc
, char **argv
)
897 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
898 char *dir
= "/etc/modules.d/";
899 struct module_node
*mn
;
908 path
= malloc(strlen(dir
) + 2);
915 if (scan_module_folders()) {
920 if (scan_loaded_modules()) {
925 ULOG_INFO("loading kernel modules from %s\n", path
);
927 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
930 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
931 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
936 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
940 while (getline(&mod
, &mod_len
, fp
) > 0) {
941 char *nl
= strchr(mod
, '\n');
948 opts
= strchr(mod
, ' ');
952 m
= find_module(get_module_name(mod
));
953 if (!m
|| (m
->state
== LOADED
))
957 m
->opts
= strdup(opts
);
959 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
967 fail
= load_modprobe();
970 ULOG_ERR("%d module%s could not be probed\n",
971 fail
, (fail
== 1) ? ("") : ("s"));
973 avl_for_each_element(&modules
, mn
, avl
) {
977 if ((m
->state
== PROBE
) || (m
->error
))
978 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
981 ULOG_INFO("done loading kernel modules from %s\n", path
);
991 static inline char weight(char c
)
993 return c
== '_' ? '-' : c
;
996 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
1001 while (*s1
&& (weight(*s1
) == weight(*s2
)))
1007 return (unsigned char)weight(*s1
) - (unsigned char)weight(*s2
);
1010 int main(int argc
, char **argv
)
1012 char *exec
= basename(*argv
);
1014 avl_init(&modules
, avl_modcmp
, false, NULL
);
1015 if (!strcmp(exec
, "insmod"))
1016 return main_insmod(argc
, argv
);
1018 if (!strcmp(exec
, "rmmod"))
1019 return main_rmmod(argc
, argv
);
1021 if (!strcmp(exec
, "lsmod"))
1022 return main_lsmod(argc
, argv
);
1024 if (!strcmp(exec
, "modinfo"))
1025 return main_modinfo(argc
, argv
);
1027 if (!strcmp(exec
, "modprobe"))
1028 return main_modprobe(argc
, argv
);
1030 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
1031 return main_loader(argc
, argv
);