63bae5e827bdd25bbd60a4800451928fdb8c0c55
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>
35 #include <libubox/avl.h>
36 #include <libubox/avl-cmp.h>
37 #include <libubox/utils.h>
38 #include <libubox/ulog.h>
39 #include <libubox/kvlist.h>
41 #define DEF_MOD_PATH "/modules/%s/"
42 /* duplicated from in-kernel include/linux/module.h */
43 #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
61 int refcnt
; /* number of references from module_node.m */
70 static struct avl_tree modules
;
71 static KVLIST(options
, kvlist_strlen
);
73 static char **module_folders
= NULL
;
75 static void free_module(struct module
*m
);
77 static int init_module_folders(void)
82 char *s
, *e
, *p
, path
[330], ldpath
[256];
85 s
= getenv("LD_LIBRARY_PATH");
88 e
+= snprintf(ldpath
, sizeof(ldpath
), "%s:", s
);
90 e
+= snprintf(e
, sizeof(ldpath
) - (e
- ldpath
), "/lib");
94 for (s
= p
= ldpath
; p
<= e
; p
++) {
95 if (*p
!= ':' && *p
!= '\0')
99 snprintf(path
, sizeof(path
), "%s" DEF_MOD_PATH
, s
, ver
.release
);
101 if (!stat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
102 module_folders
= realloc(module_folders
, sizeof(p
) * (n
+ 2));
104 if (!module_folders
) {
105 ULOG_ERR("out of memory\n");
109 module_folders
[n
++] = strdup(path
);
115 if (!module_folders
) {
116 ULOG_ERR("no module folders for kernel version %s found\n", ver
.release
);
120 module_folders
[n
] = NULL
;
124 static struct module
*find_module(const char *name
)
126 struct module_node
*mn
;
127 mn
= avl_find_element(&modules
, name
, mn
, avl
);
134 static void free_modules(void)
136 struct module_node
*mn
, *tmp
;
138 avl_remove_all_elements(&modules
, mn
, avl
, tmp
) {
139 struct module
*m
= mn
->m
;
148 static char* get_module_path(char *name
)
151 static char path
[256];
154 if (!stat(name
, &s
) && S_ISREG(s
.st_mode
))
157 for (p
= module_folders
; *p
; p
++) {
158 snprintf(path
, sizeof(path
), "%s%s.ko", *p
, name
);
159 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
166 static char* get_module_name(char *path
)
168 static char name
[MODULE_NAME_LEN
];
171 strncpy(name
, basename(path
), sizeof(name
) - 1);
173 t
= strstr(name
, ".ko");
180 static int elf64_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
182 const char *secnames
;
187 e
= (Elf64_Ehdr
*) map
;
188 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
190 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
191 for (i
= 0; i
< e
->e_shnum
; i
++) {
192 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
193 *size
= sh
[i
].sh_size
;
194 *offset
= sh
[i
].sh_offset
;
202 static int elf32_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
204 const char *secnames
;
209 e
= (Elf32_Ehdr
*) map
;
210 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
212 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
213 for (i
= 0; i
< e
->e_shnum
; i
++) {
214 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
215 *size
= sh
[i
].sh_size
;
216 *offset
= sh
[i
].sh_offset
;
224 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
226 int clazz
= map
[EI_CLASS
];
227 int endian
= map
[EI_DATA
];
229 #if __BYTE_ORDER == __LITTLE_ENDIAN
230 if (endian
!= ELFDATA2LSB
)
231 #elif __BYTE_ORDER == __BIG_ENDIAN
232 if (endian
!= ELFDATA2MSB
)
234 #error "unsupported endian"
237 ULOG_ERR("invalid endianess: %d\n", endian
);
241 if (clazz
== ELFCLASS32
)
242 return elf32_find_section(map
, section
, offset
, size
);
243 else if (clazz
== ELFCLASS64
)
244 return elf64_find_section(map
, section
, offset
, size
);
246 ULOG_ERR("unknown elf format %d\n", clazz
);
251 static struct module_node
*
252 alloc_module_node(const char *name
, struct module
*m
, bool is_alias
)
254 struct module_node
*mn
;
257 mn
= calloc_a(sizeof(*mn
),
258 &_name
, strlen(name
) + 1);
260 mn
->avl
.key
= strcpy(_name
, name
);
262 mn
->is_alias
= is_alias
;
263 avl_insert(&modules
, &mn
->avl
);
269 static struct module
*
270 alloc_module(const char *name
, const char * const *aliases
, int naliases
, const char *depends
, int size
)
276 m
= calloc_a(sizeof(*m
),
277 &_name
, strlen(name
) + 1,
278 &_dep
, depends
? strlen(depends
) + 2 : 0);
282 m
->name
= strcpy(_name
, name
);
286 m
->depends
= strcpy(_dep
, depends
);
296 alloc_module_node(m
->name
, m
, false);
297 for (i
= 0; i
< naliases
; i
++)
298 alloc_module_node(aliases
[i
], m
, true);
303 static void free_module(struct module
*m
)
310 static int scan_loaded_modules(void)
316 fp
= fopen("/proc/modules", "r");
318 ULOG_ERR("failed to open /proc/modules\n");
322 while (getline(&buf
, &buf_len
, fp
) > 0) {
326 m
.name
= strtok(buf
, " ");
327 m
.size
= atoi(strtok(NULL
, " "));
328 m
.usage
= atoi(strtok(NULL
, " "));
329 m
.depends
= strtok(NULL
, " ");
331 if (!m
.name
|| !m
.depends
)
334 n
= find_module(m
.name
);
336 /* possibly a module outside /lib/modules/<ver>/ */
337 n
= alloc_module(m
.name
, NULL
, 0, m
.depends
, m
.size
);
348 static struct module
* get_module_info(const char *module
, const char *name
)
350 int fd
= open(module
, O_RDONLY
);
351 unsigned int offset
, size
;
352 char *map
= MAP_FAILED
, *strings
, *dep
= NULL
;
353 const char **aliases
= NULL
;
355 struct module
*m
= NULL
;
359 ULOG_ERR("failed to open %s\n", module
);
363 if (fstat(fd
, &s
) == -1) {
364 ULOG_ERR("failed to stat %s\n", module
);
368 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
369 if (map
== MAP_FAILED
) {
370 ULOG_ERR("failed to mmap %s\n", module
);
374 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
375 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
379 strings
= map
+ offset
;
386 if (strings
>= map
+ offset
+ size
)
388 sep
= strstr(strings
, "=");
393 if (!strncmp(strings
, "depends=", len
+ 1))
395 else if (!strncmp(strings
, "alias=", len
+ 1)) {
396 aliases
= realloc(aliases
, sizeof(sep
) * (naliases
+ 1));
398 ULOG_ERR("out of memory\n");
402 aliases
[naliases
++] = sep
;
404 strings
= &sep
[strlen(sep
)];
407 m
= alloc_module(name
, aliases
, naliases
, dep
, s
.st_size
);
413 if (map
!= MAP_FAILED
)
414 munmap(map
, s
.st_size
);
424 static int scan_module_folder(const char *dir
)
426 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
433 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
434 sprintf(path
, "%s*.ko", dir
);
436 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
439 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
440 char *name
= get_module_name(gl
.gl_pathv
[j
]);
447 m
= find_module(name
);
451 m
= get_module_info(gl
.gl_pathv
[j
], name
);
457 opts
= kvlist_get(&options
, name
);
462 m
->state
= BLACKLISTED
;
464 m
->opts
= strdup(opts
);
472 static int scan_module_folders(void)
477 if (init_module_folders())
480 for (p
= module_folders
; *p
; p
++)
481 rv
|= scan_module_folder(*p
);
486 static int print_modinfo(char *module
)
488 int fd
= open(module
, O_RDONLY
);
489 unsigned int offset
, size
;
491 char *map
= MAP_FAILED
, *strings
;
495 ULOG_ERR("failed to open %s\n", module
);
499 if (fstat(fd
, &s
) == -1) {
500 ULOG_ERR("failed to stat %s\n", module
);
504 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
505 if (map
== MAP_FAILED
) {
506 ULOG_ERR("failed to mmap %s\n", module
);
510 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
511 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
515 strings
= map
+ offset
;
516 printf("module:\t\t%s\n", module
);
523 if (strings
>= map
+ offset
+ size
)
525 sep
= strstr(strings
, "=");
528 dup
= strndup(strings
, sep
- strings
);
530 if (strncmp(strings
, "parm", 4)) {
532 printf("%s:\t\t%s\n", dup
, sep
);
534 printf("%s:\t%s\n", dup
, sep
);
536 strings
= &sep
[strlen(sep
)];
544 if (map
!= MAP_FAILED
)
545 munmap(map
, s
.st_size
);
553 static int deps_available(struct module
*m
, int verbose
)
558 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
564 m
= find_module(dep
);
567 ULOG_ERR("missing dependency %s\n", dep
);
568 if (verbose
&& m
&& (m
->state
!= LOADED
))
569 ULOG_ERR("dependency not loaded %s\n", dep
);
570 if (!m
|| (m
->state
!= LOADED
))
572 dep
+= strlen(dep
) + 1;
578 static int insert_module(char *path
, const char *options
)
584 if (stat(path
, &s
)) {
585 ULOG_ERR("missing module %s\n", path
);
589 fd
= open(path
, O_RDONLY
);
591 ULOG_ERR("cannot open %s\n", path
);
595 data
= malloc(s
.st_size
);
597 ULOG_ERR("out of memory\n");
601 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
602 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
607 ULOG_ERR("failed to read full module %s\n", path
);
616 static void load_moddeps(struct module
*_m
)
621 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
627 m
= find_module(dep
);
630 ULOG_ERR("failed to find dependency %s\n", dep
);
631 if (m
&& (m
->state
!= LOADED
)) {
636 dep
= dep
+ strlen(dep
) + 1;
640 static int load_modprobe(bool allow_load_retry
)
642 int loaded
, skipped
, failed
;
643 struct module_node
*mn
;
645 bool load_retry
= false;
646 static bool first_iteration
= true;
648 avl_for_each_element(&modules
, mn
, avl
) {
652 if (m
->state
== PROBE
)
660 avl_for_each_element(&modules
, mn
, avl
) {
664 if ((m
->state
== PROBE
) && (!deps_available(m
, 0)) && (!m
->error
|| load_retry
)) {
665 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
677 else if (m
->state
== PROBE
)
681 if (allow_load_retry
) {
682 /* if we can't load anything else let's try to load failed modules */
683 load_retry
= loaded
? (failed
&& !skipped
) : (failed
&& !load_retry
&& !first_iteration
);
686 first_iteration
= false;
687 } while (loaded
|| load_retry
);
689 return skipped
+ failed
;
692 static int print_insmod_usage(void)
694 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
699 static int print_modprobe_usage(void)
703 "\tmodprobe [-q] [-v] filename\n"
704 "\tmodprobe -a [-q] [-v] filename [filename...]\n"
710 static int print_usage(char *arg
)
712 ULOG_INFO("Usage:\n\t%s module\n", arg
);
717 static int main_insmod(int argc
, char **argv
)
719 char *name
, *cur
, *options
;
723 return print_insmod_usage();
725 name
= get_module_name(argv
[1]);
727 ULOG_ERR("cannot find module - %s\n", argv
[1]);
731 if (scan_loaded_modules())
734 if (find_module(name
)) {
735 ULOG_ERR("module is already loaded - %s\n", name
);
742 for (len
= 0, i
= 2; i
< argc
; i
++)
743 len
+= strlen(argv
[i
]) + 1;
745 options
= malloc(len
);
747 ULOG_ERR("out of memory\n");
754 for (i
= 2; i
< argc
; i
++) {
759 cur
+= sprintf(cur
, "%s", argv
[i
]);
762 if (init_module_folders()) {
763 fprintf(stderr
, "Failed to find the folder holding the modules\n");
768 if (get_module_path(argv
[1])) {
770 } else if (!get_module_path(name
)) {
771 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
776 ret
= insert_module(get_module_path(name
), options
);
778 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
785 static int main_rmmod(int argc
, char **argv
)
792 return print_usage("rmmod");
794 if (scan_loaded_modules())
797 name
= get_module_name(argv
[1]);
798 m
= find_module(name
);
800 ULOG_ERR("module is not loaded\n");
803 ret
= syscall(__NR_delete_module
, m
->name
, 0);
806 ULOG_ERR("unloading the module failed\n");
813 static int main_lsmod(int argc
, char **argv
)
815 struct module_node
*mn
;
819 if (scan_loaded_modules())
822 avl_for_each_element(&modules
, mn
, avl
) {
826 if (m
->state
== LOADED
) {
827 printf("%-20s%8d%3d ",
828 m
->name
, m
->size
, m
->usage
);
829 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
833 dep
= dep
+ strlen(dep
) + 1;
847 static int main_modinfo(int argc
, char **argv
)
853 return print_usage("modinfo");
855 if (scan_module_folders())
858 name
= get_module_name(argv
[1]);
859 m
= find_module(name
);
861 ULOG_ERR("cannot find module - %s\n", argv
[1]);
865 name
= get_module_path(m
->name
);
867 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
876 static int main_modprobe(int argc
, char **argv
)
878 struct module_node
*mn
;
882 int log_level
= LOG_WARNING
;
885 bool use_all
= false;
887 while ((opt
= getopt(argc
, argv
, "aqv")) != -1 ) {
892 case 'q': /* shhhh! */
896 log_level
= LOG_DEBUG
;
899 return print_modprobe_usage();
905 return print_modprobe_usage(); /* expected module after options */
907 /* after print_modprobe_usage() so it won't be filtered out */
908 ulog_threshold(log_level
);
910 if (scan_module_folders())
913 if (scan_loaded_modules())
919 name
= get_module_name(argv
[optind
]);
920 m
= find_module(name
);
922 if (m
&& m
->state
== BLACKLISTED
) {
924 ULOG_INFO("%s is blacklisted\n", name
);
925 } else if (m
&& m
->state
== LOADED
) {
927 ULOG_INFO("%s is already loaded\n", name
);
930 ULOG_ERR("failed to find a module named %s\n", name
);
937 } while (use_all
&& optind
< argc
);
939 load_fail
= load_modprobe(true);
941 ULOG_ERR("%d module%s could not be probed\n",
942 load_fail
, (load_fail
== 1) ? ("") : ("s"));
944 avl_for_each_element(&modules
, mn
, avl
) {
948 if ((m
->state
== PROBE
) || m
->error
)
949 ULOG_ERR("- %s\n", m
->name
);
960 static int main_loader(int argc
, char **argv
)
962 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
963 char *dir
= "/etc/modules.d/";
964 struct module_node
*mn
;
968 int ret
= 0, fail
, j
;
973 path
= malloc(strlen(dir
) + 2);
975 ULOG_ERR("out of memory\n");
982 if (scan_module_folders()) {
987 if (scan_loaded_modules()) {
992 ULOG_INFO("loading kernel modules from %s\n", path
);
994 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
997 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
998 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
1003 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
1007 while (getline(&mod
, &mod_len
, fp
) > 0) {
1008 char *nl
= strchr(mod
, '\n');
1015 opts
= strchr(mod
, ' ');
1019 m
= find_module(get_module_name(mod
));
1020 if (!m
|| m
->state
== LOADED
|| m
->state
== BLACKLISTED
)
1025 char *prev
= m
->opts
;
1027 fail
= asprintf(&m
->opts
, "%s %s", prev
, opts
);
1030 ULOG_ERR("out of memory for opts %s\n", opts
);
1037 m
->opts
= strdup(opts
);
1041 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
1042 load_modprobe(false);
1049 fail
= load_modprobe(true);
1052 ULOG_ERR("%d module%s could not be probed\n",
1053 fail
, (fail
== 1) ? ("") : ("s"));
1055 avl_for_each_element(&modules
, mn
, avl
) {
1059 if ((m
->state
== PROBE
) || (m
->error
))
1060 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
1063 ULOG_INFO("done loading kernel modules from %s\n", path
);
1074 static inline char weight(char c
)
1076 return c
== '_' ? '-' : c
;
1079 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
1081 const char *s1
= k1
;
1082 const char *s2
= k2
;
1084 while (*s1
&& (weight(*s1
) == weight(*s2
)))
1090 return (unsigned char)weight(*s1
) - (unsigned char)weight(*s2
);
1096 static char buf
[512];
1100 f
= fopen("/etc/modules.conf", "r");
1104 while ((s
= fgets(buf
, sizeof(buf
), f
)) != NULL
) {
1105 char *c
, *cmd
, *mod
;
1118 while (c
> s
&& isspace(c
[-1])) {
1123 cmd
= strsep(&s
, " \t");
1130 mod
= strsep(&s
, " \t");
1134 if (!strcmp(cmd
, "blacklist")) {
1135 kvlist_set(&options
, mod
, "\x01");
1139 if (!strcmp(cmd
, "options")) {
1140 char *prev
= kvlist_get(&options
, mod
);
1149 if (prev
&& prev
[0] == '\x01')
1153 kvlist_set(&options
, mod
, s
);
1157 if (asprintf(&val
, "%s %s", prev
, s
) < 0)
1160 kvlist_set(&options
, mod
, val
);
1167 int main(int argc
, char **argv
)
1169 char *exec
= basename(*argv
);
1171 avl_init(&modules
, avl_modcmp
, false, NULL
);
1172 if (!strcmp(exec
, "insmod"))
1173 return main_insmod(argc
, argv
);
1175 if (!strcmp(exec
, "rmmod"))
1176 return main_rmmod(argc
, argv
);
1178 if (!strcmp(exec
, "lsmod"))
1179 return main_lsmod(argc
, argv
);
1181 if (!strcmp(exec
, "modinfo"))
1182 return main_modinfo(argc
, argv
);
1186 if (!strcmp(exec
, "modprobe"))
1187 return main_modprobe(argc
, argv
);
1189 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
1190 return main_loader(argc
, argv
);