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>
40 #include <libubox/list.h>
42 #define DEF_MOD_PATH "/modules/%s/"
43 #define MOD_BUILTIN "modules.builtin"
44 /* duplicated from in-kernel include/linux/module.h */
45 #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
51 struct list_head list
;
71 int refcnt
; /* number of references from module_node.m */
80 static struct avl_tree modules
;
81 static KVLIST(options
, kvlist_strlen
);
83 static char **module_folders
= NULL
;
85 static void free_module(struct module
*m
);
87 static int init_module_folders(void)
92 char *s
, *e
, *p
, path
[330], ldpath
[256];
95 s
= getenv("LD_LIBRARY_PATH");
98 e
+= snprintf(ldpath
, sizeof(ldpath
), "%s:", s
);
100 e
+= snprintf(e
, sizeof(ldpath
) - (e
- ldpath
), "/lib");
104 for (s
= p
= ldpath
; p
<= e
; p
++) {
105 if (*p
!= ':' && *p
!= '\0')
109 snprintf(path
, sizeof(path
), "%s" DEF_MOD_PATH
, s
, ver
.release
);
111 if (!stat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
112 module_folders
= realloc(module_folders
, sizeof(p
) * (n
+ 2));
114 if (!module_folders
) {
115 ULOG_ERR("out of memory\n");
119 module_folders
[n
++] = strdup(path
);
125 if (!module_folders
) {
126 ULOG_ERR("no module folders for kernel version %s found\n", ver
.release
);
130 module_folders
[n
] = NULL
;
134 static struct module
*find_module(const char *name
)
136 struct module_node
*mn
;
137 mn
= avl_find_element(&modules
, name
, mn
, avl
);
144 static void free_modules(void)
146 struct module_node
*mn
, *tmp
;
148 avl_remove_all_elements(&modules
, mn
, avl
, tmp
) {
149 struct module
*m
= mn
->m
;
158 static char* get_module_path(char *name
)
161 static char path
[256];
164 if (!stat(name
, &s
) && S_ISREG(s
.st_mode
))
167 for (p
= module_folders
; *p
; p
++) {
168 snprintf(path
, sizeof(path
), "%s%s.ko", *p
, name
);
169 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
176 static char* get_module_name(char *path
)
178 static char name
[MODULE_NAME_LEN
];
181 strncpy(name
, basename(path
), sizeof(name
) - 1);
183 t
= strstr(name
, ".ko");
190 static int elf64_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
192 const char *secnames
;
197 e
= (Elf64_Ehdr
*) map
;
198 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
200 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
201 for (i
= 0; i
< e
->e_shnum
; i
++) {
202 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
203 *size
= sh
[i
].sh_size
;
204 *offset
= sh
[i
].sh_offset
;
212 static int elf32_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
214 const char *secnames
;
219 e
= (Elf32_Ehdr
*) map
;
220 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
222 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
223 for (i
= 0; i
< e
->e_shnum
; i
++) {
224 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
225 *size
= sh
[i
].sh_size
;
226 *offset
= sh
[i
].sh_offset
;
234 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
236 int clazz
= map
[EI_CLASS
];
237 int endian
= map
[EI_DATA
];
239 #if __BYTE_ORDER == __LITTLE_ENDIAN
240 if (endian
!= ELFDATA2LSB
)
241 #elif __BYTE_ORDER == __BIG_ENDIAN
242 if (endian
!= ELFDATA2MSB
)
244 #error "unsupported endian"
247 ULOG_ERR("invalid endianess: %d\n", endian
);
251 if (clazz
== ELFCLASS32
)
252 return elf32_find_section(map
, section
, offset
, size
);
253 else if (clazz
== ELFCLASS64
)
254 return elf64_find_section(map
, section
, offset
, size
);
256 ULOG_ERR("unknown elf format %d\n", clazz
);
261 static struct module_node
*
262 alloc_module_node(const char *name
, struct module
*m
, bool is_alias
)
264 struct module_node
*mn
;
267 mn
= calloc_a(sizeof(*mn
),
268 &_name
, strlen(name
) + 1);
270 mn
->avl
.key
= strcpy(_name
, name
);
272 mn
->is_alias
= is_alias
;
273 avl_insert(&modules
, &mn
->avl
);
279 static struct module
*
280 alloc_module(const char *name
, const char * const *aliases
, int naliases
, const char *depends
, int size
)
286 m
= calloc_a(sizeof(*m
),
287 &_name
, strlen(name
) + 1,
288 &_dep
, depends
? strlen(depends
) + 2 : 0);
292 m
->name
= strcpy(_name
, name
);
296 m
->depends
= strcpy(_dep
, depends
);
306 alloc_module_node(m
->name
, m
, false);
307 for (i
= 0; i
< naliases
; i
++)
308 alloc_module_node(aliases
[i
], m
, true);
313 static void free_module(struct module
*m
)
320 static int scan_loaded_modules(void)
327 fp
= fopen("/proc/modules", "r");
329 ULOG_ERR("failed to open /proc/modules\n");
333 while (getline(&buf
, &buf_len
, fp
) > 0) {
337 m
.name
= strtok(buf
, " ");
338 m
.size
= atoi(strtok(NULL
, " "));
339 m
.usage
= atoi(strtok(NULL
, " "));
340 m
.depends
= strtok(NULL
, " ");
342 if (!m
.name
|| !m
.depends
)
345 n
= find_module(m
.name
);
347 /* possibly a module outside /lib/modules/<ver>/ */
348 n
= alloc_module(m
.name
, NULL
, 0, m
.depends
, m
.size
);
351 ULOG_ERR("Failed to allocate memory for module\n");
366 static struct module
* get_module_info(const char *module
, const char *name
)
368 int fd
= open(module
, O_RDONLY
);
369 unsigned int offset
, size
;
370 char *map
= MAP_FAILED
, *strings
, *dep
= NULL
;
371 const char **aliases
= NULL
;
372 const char **aliasesr
;
374 struct module
*m
= NULL
;
378 ULOG_ERR("failed to open %s\n", module
);
382 if (fstat(fd
, &s
) == -1) {
383 ULOG_ERR("failed to stat %s\n", module
);
387 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
388 if (map
== MAP_FAILED
) {
389 ULOG_ERR("failed to mmap %s\n", module
);
393 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
394 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
398 strings
= map
+ offset
;
405 if (strings
>= map
+ offset
+ size
)
407 sep
= strstr(strings
, "=");
412 if (!strncmp(strings
, "depends=", len
+ 1))
414 else if (!strncmp(strings
, "alias=", len
+ 1)) {
415 aliasesr
= realloc(aliases
, sizeof(sep
) * (naliases
+ 1));
417 ULOG_ERR("out of memory\n");
422 aliases
[naliases
++] = sep
;
424 strings
= &sep
[strlen(sep
)];
427 m
= alloc_module(name
, aliases
, naliases
, dep
, s
.st_size
);
433 if (map
!= MAP_FAILED
)
434 munmap(map
, s
.st_size
);
444 static int scan_builtin_modules(void)
453 if (!module_folders
&& init_module_folders())
455 for (p
= module_folders
; *p
; p
++) {
456 snprintf(path
, sizeof(path
), "%s%s", *p
, MOD_BUILTIN
);
457 if (!stat(path
, &st
) && S_ISREG(st
.st_mode
)) {
458 fp
= fopen(path
, "r");
464 goto out
; /* OK if modules.builtin unavailable */
466 while (getline(&buf
, &buf_len
, fp
) > 0) {
470 name
= get_module_name(buf
);
473 m
= find_module(name
);
474 if (m
&& !strcmp(m
->name
, name
)) {
475 ULOG_WARN("found duplicate builtin module %s\n", name
);
478 m
= alloc_module(name
, NULL
, 0, NULL
, 0);
480 ULOG_ERR("failed to allocate memory for module\n");
495 static int scan_module_folder(const char *dir
)
497 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
502 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
503 sprintf(path
, "%s*.ko", dir
);
505 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
508 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
509 char *name
= get_module_name(gl
.gl_pathv
[j
]);
516 m
= find_module(name
);
520 m
= get_module_info(gl
.gl_pathv
[j
], name
);
526 opts
= kvlist_get(&options
, name
);
531 m
->state
= BLACKLISTED
;
533 m
->opts
= strdup(opts
);
541 static int scan_module_folders(void)
546 if (init_module_folders())
549 for (p
= module_folders
; *p
; p
++)
550 rv
|= scan_module_folder(*p
);
555 static int print_modinfo(char *module
)
557 int fd
= open(module
, O_RDONLY
);
558 unsigned int offset
, size
;
561 char *map
= MAP_FAILED
, *strings
;
567 ULOG_ERR("failed to open %s\n", module
);
571 if (fstat(fd
, &s
) == -1) {
572 ULOG_ERR("failed to stat %s\n", module
);
576 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
577 if (map
== MAP_FAILED
) {
578 ULOG_ERR("failed to mmap %s\n", module
);
582 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
583 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
587 strings
= map
+ offset
;
588 printf("module:\t\t%s\n", module
);
596 if (strings
>= map
+ offset
+ size
)
598 sep
= strstr(strings
, "=");
601 dup
= strndup(strings
, sep
- strings
);
603 if (strncmp(strings
, "parm", 4)) {
605 printf("%s:\t\t%s\n", dup
, sep
);
607 printf("%s:\t%s\n", dup
, sep
);
609 sep2
= strstr(sep
, ":");
612 pname
= strndup(sep
, sep2
- sep
);
614 pdata
= strdup(sep2
);
616 list_for_each_entry(p
, ¶ms
, list
)
617 if (!strcmp(pname
, p
->name
))
620 if (list_entry_is_h(p
, ¶ms
, list
)) {
621 p
= alloca(sizeof(*p
));
623 p
->desc
= p
->type
= NULL
;
624 list_add(&p
->list
, ¶ms
);
629 if (!strcmp(dup
, "parmtype"))
634 strings
= &sep
[strlen(sep
)];
639 list_for_each_entry(p
, ¶ms
, list
) {
640 printf("parm:\t\t%s", p
->name
);
642 printf(":%s", p
->desc
);
644 printf(" (%s)", p
->type
);
654 if (map
!= MAP_FAILED
)
655 munmap(map
, s
.st_size
);
663 static int deps_available(struct module
*m
, int verbose
)
668 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
674 m
= find_module(dep
);
677 ULOG_ERR("missing dependency %s\n", dep
);
678 if (verbose
&& m
&& (m
->state
!= LOADED
))
679 ULOG_ERR("dependency not loaded %s\n", dep
);
680 if (!m
|| (m
->state
!= LOADED
))
682 dep
+= strlen(dep
) + 1;
688 static int insert_module(char *path
, const char *options
)
695 ULOG_ERR("Path not specified\n");
699 if (stat(path
, &s
)) {
700 ULOG_ERR("missing module %s\n", path
);
704 fd
= open(path
, O_RDONLY
);
706 ULOG_ERR("cannot open %s\n", path
);
710 data
= malloc(s
.st_size
);
712 ULOG_ERR("out of memory\n");
716 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
717 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
722 ULOG_ERR("failed to read full module %s\n", path
);
731 static void load_moddeps(struct module
*_m
)
736 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
742 m
= find_module(dep
);
745 ULOG_ERR("failed to find dependency %s\n", dep
);
746 if (m
&& (m
->state
!= LOADED
)) {
751 dep
= dep
+ strlen(dep
) + 1;
755 static int load_modprobe(bool allow_load_retry
)
757 int loaded
, skipped
, failed
;
758 struct module_node
*mn
;
760 bool load_retry
= false;
761 static bool first_iteration
= true;
763 avl_for_each_element(&modules
, mn
, avl
) {
767 if (m
->state
== PROBE
)
775 avl_for_each_element(&modules
, mn
, avl
) {
779 if ((m
->state
== PROBE
) && (!deps_available(m
, 0)) && (!m
->error
|| load_retry
)) {
780 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
792 else if (m
->state
== PROBE
)
796 if (allow_load_retry
) {
797 /* if we can't load anything else let's try to load failed modules */
798 load_retry
= loaded
? (failed
&& !skipped
) : (failed
&& !load_retry
&& !first_iteration
);
801 first_iteration
= false;
802 } while (loaded
|| load_retry
);
804 return skipped
+ failed
;
807 static int print_insmod_usage(void)
809 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
814 static int print_modprobe_usage(void)
818 "\tmodprobe [-q] [-v] filename\n"
819 "\tmodprobe -a [-q] [-v] filename [filename...]\n"
825 static int print_usage(char *arg
)
827 ULOG_INFO("Usage:\n\t%s module\n", arg
);
832 static int main_insmod(int argc
, char **argv
)
834 char *name
, *cur
, *options
;
838 return print_insmod_usage();
840 name
= get_module_name(argv
[1]);
842 ULOG_ERR("cannot find module - %s\n", argv
[1]);
846 if (scan_loaded_modules())
849 if (find_module(name
)) {
850 ULOG_ERR("module is already loaded - %s\n", name
);
857 for (len
= 0, i
= 2; i
< argc
; i
++)
858 len
+= strlen(argv
[i
]) + 1;
860 options
= malloc(len
);
862 ULOG_ERR("out of memory\n");
869 for (i
= 2; i
< argc
; i
++) {
874 cur
+= sprintf(cur
, "%s", argv
[i
]);
877 if (init_module_folders()) {
878 fprintf(stderr
, "Failed to find the folder holding the modules\n");
883 if (get_module_path(argv
[1])) {
885 } else if (!get_module_path(name
)) {
886 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
891 ret
= insert_module(get_module_path(name
), options
);
893 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
900 static int main_rmmod(int argc
, char **argv
)
907 return print_usage("rmmod");
909 if (scan_loaded_modules())
912 if (scan_builtin_modules())
915 name
= get_module_name(argv
[1]);
916 m
= find_module(name
);
918 ULOG_ERR("module is not loaded\n");
921 if (m
->state
== BUILTIN
) {
922 ULOG_ERR("module is builtin\n");
925 ret
= syscall(__NR_delete_module
, m
->name
, 0);
928 ULOG_ERR("unloading the module failed\n");
935 static int main_lsmod(int argc
, char **argv
)
937 struct module_node
*mn
;
941 if (scan_loaded_modules())
944 avl_for_each_element(&modules
, mn
, avl
) {
948 if (m
->state
== LOADED
) {
949 printf("%-20s%8d%3d ",
950 m
->name
, m
->size
, m
->usage
);
951 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
955 dep
= dep
+ strlen(dep
) + 1;
969 static int main_modinfo(int argc
, char **argv
)
975 return print_usage("modinfo");
977 if (scan_module_folders())
980 name
= get_module_name(argv
[1]);
981 m
= find_module(name
);
983 ULOG_ERR("cannot find module - %s\n", argv
[1]);
987 name
= get_module_path(m
->name
);
989 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
998 static int main_modprobe(int argc
, char **argv
)
1000 struct module_node
*mn
;
1004 int log_level
= LOG_WARNING
;
1007 bool use_all
= false;
1009 while ((opt
= getopt(argc
, argv
, "aqv")) != -1 ) {
1014 case 'q': /* shhhh! */
1018 log_level
= LOG_DEBUG
;
1021 return print_modprobe_usage();
1027 return print_modprobe_usage(); /* expected module after options */
1029 /* after print_modprobe_usage() so it won't be filtered out */
1030 ulog_threshold(log_level
);
1032 if (scan_module_folders())
1035 if (scan_loaded_modules())
1038 if (scan_builtin_modules())
1044 name
= get_module_name(argv
[optind
]);
1045 m
= find_module(name
);
1047 if (m
&& m
->state
== BLACKLISTED
) {
1049 ULOG_INFO("%s is blacklisted\n", name
);
1050 } else if (m
&& m
->state
== LOADED
) {
1052 ULOG_INFO("%s is already loaded\n", name
);
1053 } else if (m
&& m
->state
== BUILTIN
) {
1055 ULOG_INFO("%s is builtin\n", name
);
1058 ULOG_ERR("failed to find a module named %s\n", name
);
1065 } while (use_all
&& optind
< argc
);
1067 load_fail
= load_modprobe(true);
1069 ULOG_ERR("%d module%s could not be probed\n",
1070 load_fail
, (load_fail
== 1) ? ("") : ("s"));
1072 avl_for_each_element(&modules
, mn
, avl
) {
1076 if ((m
->state
== PROBE
) || m
->error
)
1077 ULOG_ERR("- %s\n", m
->name
);
1088 static int main_loader(int argc
, char **argv
)
1090 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
1091 char *dir
= "/etc/modules.d/";
1092 struct module_node
*mn
;
1096 int ret
= 0, fail
, j
;
1101 path
= malloc(strlen(dir
) + 2);
1103 ULOG_ERR("out of memory\n");
1110 if (scan_module_folders()) {
1115 if (scan_loaded_modules()) {
1120 ULOG_INFO("loading kernel modules from %s\n", path
);
1122 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
1125 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
1126 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
1131 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
1135 while (getline(&mod
, &mod_len
, fp
) > 0) {
1136 char *nl
= strchr(mod
, '\n');
1143 opts
= strchr(mod
, ' ');
1147 m
= find_module(get_module_name(mod
));
1148 if (!m
|| m
->state
== LOADED
|| m
->state
== BLACKLISTED
)
1153 char *prev
= m
->opts
;
1155 fail
= asprintf(&m
->opts
, "%s %s", prev
, opts
);
1158 ULOG_ERR("out of memory for opts %s\n", opts
);
1165 m
->opts
= strdup(opts
);
1169 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
1170 load_modprobe(false);
1177 fail
= load_modprobe(true);
1180 ULOG_ERR("%d module%s could not be probed\n",
1181 fail
, (fail
== 1) ? ("") : ("s"));
1183 avl_for_each_element(&modules
, mn
, avl
) {
1187 if ((m
->state
== PROBE
) || (m
->error
))
1188 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
1191 ULOG_INFO("done loading kernel modules from %s\n", path
);
1202 static inline char weight(char c
)
1204 return c
== '_' ? '-' : c
;
1207 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
1209 const char *s1
= k1
;
1210 const char *s2
= k2
;
1212 while (*s1
&& (weight(*s1
) == weight(*s2
)))
1218 return (unsigned char)weight(*s1
) - (unsigned char)weight(*s2
);
1224 static char buf
[512];
1228 f
= fopen("/etc/modules.conf", "r");
1232 while ((s
= fgets(buf
, sizeof(buf
), f
)) != NULL
) {
1233 char *c
, *cmd
, *mod
;
1246 while (c
> s
&& isspace(c
[-1])) {
1251 cmd
= strsep(&s
, " \t");
1258 mod
= strsep(&s
, " \t");
1262 if (!strcmp(cmd
, "blacklist")) {
1263 kvlist_set(&options
, mod
, "\x01");
1267 if (!strcmp(cmd
, "options")) {
1268 char *prev
= kvlist_get(&options
, mod
);
1277 if (prev
&& prev
[0] == '\x01')
1281 kvlist_set(&options
, mod
, s
);
1285 if (asprintf(&val
, "%s %s", prev
, s
) < 0)
1288 kvlist_set(&options
, mod
, val
);
1297 int main(int argc
, char **argv
)
1299 char *exec
= basename(*argv
);
1301 avl_init(&modules
, avl_modcmp
, false, NULL
);
1302 if (!strcmp(exec
, "insmod"))
1303 return main_insmod(argc
, argv
);
1305 if (!strcmp(exec
, "rmmod"))
1306 return main_rmmod(argc
, argv
);
1308 if (!strcmp(exec
, "lsmod"))
1309 return main_lsmod(argc
, argv
);
1311 if (!strcmp(exec
, "modinfo"))
1312 return main_modinfo(argc
, argv
);
1316 if (!strcmp(exec
, "modprobe"))
1317 return main_modprobe(argc
, argv
);
1319 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
1320 return main_loader(argc
, argv
);