43105b38adea37a86df77caf55c65b385466ec13
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 #define MOD_BUILTIN_MODINFO "modules.builtin.modinfo"
45 /* duplicated from in-kernel include/linux/module.h */
46 #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
52 struct list_head list
;
72 int refcnt
; /* number of references from module_node.m */
81 static struct avl_tree modules
;
82 static KVLIST(options
, kvlist_strlen
);
84 static char **module_folders
= NULL
;
86 static void free_module(struct module
*m
);
88 static int init_module_folders(void)
93 char *s
, *e
, *p
, path
[330], ldpath
[256];
96 s
= getenv("LD_LIBRARY_PATH");
99 e
+= snprintf(ldpath
, sizeof(ldpath
), "%s:", s
);
101 e
+= snprintf(e
, sizeof(ldpath
) - (e
- ldpath
), "/lib");
105 for (s
= p
= ldpath
; p
<= e
; p
++) {
106 if (*p
!= ':' && *p
!= '\0')
110 snprintf(path
, sizeof(path
), "%s" DEF_MOD_PATH
, s
, ver
.release
);
112 if (!stat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
113 module_folders
= realloc(module_folders
, sizeof(p
) * (n
+ 2));
115 if (!module_folders
) {
116 ULOG_ERR("out of memory\n");
120 module_folders
[n
++] = strdup(path
);
126 if (!module_folders
) {
127 ULOG_ERR("no module folders for kernel version %s found\n", ver
.release
);
131 module_folders
[n
] = NULL
;
135 static struct module
*find_module(const char *name
)
137 struct module_node
*mn
;
138 mn
= avl_find_element(&modules
, name
, mn
, avl
);
145 static void free_modules(void)
147 struct module_node
*mn
, *tmp
;
149 avl_remove_all_elements(&modules
, mn
, avl
, tmp
) {
150 struct module
*m
= mn
->m
;
159 static char* get_module_path(char *name
)
162 static char path
[256];
165 if (!stat(name
, &s
) && S_ISREG(s
.st_mode
))
168 for (p
= module_folders
; *p
; p
++) {
169 snprintf(path
, sizeof(path
), "%s%s.ko", *p
, name
);
170 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
177 static char* get_module_name(char *path
)
179 static char name
[MODULE_NAME_LEN
];
182 strncpy(name
, basename(path
), sizeof(name
) - 1);
184 t
= strstr(name
, ".ko");
191 static int elf64_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
193 const char *secnames
;
198 e
= (Elf64_Ehdr
*) map
;
199 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
201 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
202 for (i
= 0; i
< e
->e_shnum
; i
++) {
203 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
204 *size
= sh
[i
].sh_size
;
205 *offset
= sh
[i
].sh_offset
;
213 static int elf32_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
215 const char *secnames
;
220 e
= (Elf32_Ehdr
*) map
;
221 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
223 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
224 for (i
= 0; i
< e
->e_shnum
; i
++) {
225 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
226 *size
= sh
[i
].sh_size
;
227 *offset
= sh
[i
].sh_offset
;
235 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
237 int clazz
= map
[EI_CLASS
];
238 int endian
= map
[EI_DATA
];
240 #if __BYTE_ORDER == __LITTLE_ENDIAN
241 if (endian
!= ELFDATA2LSB
)
242 #elif __BYTE_ORDER == __BIG_ENDIAN
243 if (endian
!= ELFDATA2MSB
)
245 #error "unsupported endian"
248 ULOG_ERR("invalid endianess: %d\n", endian
);
252 if (clazz
== ELFCLASS32
)
253 return elf32_find_section(map
, section
, offset
, size
);
254 else if (clazz
== ELFCLASS64
)
255 return elf64_find_section(map
, section
, offset
, size
);
257 ULOG_ERR("unknown elf format %d\n", clazz
);
262 static struct module_node
*
263 alloc_module_node(const char *name
, struct module
*m
, bool is_alias
)
265 struct module_node
*mn
;
268 mn
= calloc_a(sizeof(*mn
),
269 &_name
, strlen(name
) + 1);
271 mn
->avl
.key
= strcpy(_name
, name
);
273 mn
->is_alias
= is_alias
;
274 avl_insert(&modules
, &mn
->avl
);
280 static struct module
*
281 alloc_module(const char *name
, const char * const *aliases
, int naliases
, const char *depends
, int size
)
287 m
= calloc_a(sizeof(*m
),
288 &_name
, strlen(name
) + 1,
289 &_dep
, depends
? strlen(depends
) + 2 : 0);
293 m
->name
= strcpy(_name
, name
);
297 m
->depends
= strcpy(_dep
, depends
);
307 alloc_module_node(m
->name
, m
, false);
308 for (i
= 0; i
< naliases
; i
++)
309 alloc_module_node(aliases
[i
], m
, true);
314 static void free_module(struct module
*m
)
321 static int scan_loaded_modules(void)
328 fp
= fopen("/proc/modules", "r");
330 ULOG_ERR("failed to open /proc/modules\n");
334 while (getline(&buf
, &buf_len
, fp
) > 0) {
338 m
.name
= strtok(buf
, " ");
339 m
.size
= atoi(strtok(NULL
, " "));
340 m
.usage
= atoi(strtok(NULL
, " "));
341 m
.depends
= strtok(NULL
, " ");
343 if (!m
.name
|| !m
.depends
)
346 n
= find_module(m
.name
);
348 /* possibly a module outside /lib/modules/<ver>/ */
349 n
= alloc_module(m
.name
, NULL
, 0, m
.depends
, m
.size
);
352 ULOG_ERR("Failed to allocate memory for module\n");
367 static char *mmap_modinfo(const char *module
, const char *name
, struct stat
*s
, unsigned int *offset
, unsigned int *size
)
369 const bool is_builtin
= (module
== NULL
);
370 const char *mpath
= NULL
;
371 char *map
= MAP_FAILED
;
376 for (f
= module_folders
; *f
; f
++) {
377 snprintf(path
, sizeof(path
), "%s%s", *f
, MOD_BUILTIN_MODINFO
);
378 if (!stat(path
, s
) && S_ISREG(s
->st_mode
)) {
387 ULOG_ERR("cannot find modinfo path of module - %s\n", name
);
391 fd
= open(mpath
, O_RDONLY
);
393 ULOG_ERR("failed to open %s\n", mpath
);
397 if (fstat(fd
, s
) == -1) {
398 ULOG_ERR("failed to stat %s\n", mpath
);
402 map
= mmap(NULL
, s
->st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
403 if (map
== MAP_FAILED
) {
404 ULOG_ERR("failed to mmap %s\n", mpath
);
411 } else if (elf_find_section(map
, ".modinfo", offset
, size
)) {
412 ULOG_ERR("failed to load the .modinfo section from %s\n", mpath
);
413 munmap(map
, s
->st_size
);
423 static struct module
* get_module_info(const char *module
, const char *name
)
425 const bool is_builtin
= (module
== NULL
);
426 unsigned int offset
, size
;
427 char *map
, *strings
, *dep
= NULL
;
428 const char **aliases
= NULL
;
429 const char **aliasesr
;
431 struct module
*m
= NULL
;
434 map
= mmap_modinfo(module
, name
, &s
, &offset
, &size
);
435 if (map
== MAP_FAILED
)
438 strings
= map
+ offset
;
445 if (strings
>= map
+ offset
+ size
)
448 sep
= strstr(strings
, ".");
451 if (strlen(name
) == (sep
- strings
) &&
452 !strncmp(strings
, name
, sep
- strings
))
457 sep
= strstr(strings
, "=");
462 if (!strncmp(strings
, "depends=", len
+ 1))
464 else if (!strncmp(strings
, "alias=", len
+ 1)) {
465 aliasesr
= realloc(aliases
, sizeof(sep
) * (naliases
+ 1));
467 ULOG_ERR("out of memory\n");
472 aliases
[naliases
++] = sep
;
475 strings
= &sep
[strlen(sep
)];
478 m
= alloc_module(name
, aliases
, naliases
, dep
, is_builtin
? 0 : s
.st_size
);
481 m
->state
= is_builtin
? BUILTIN
: SCANNED
;
484 if (map
!= MAP_FAILED
)
485 munmap(map
, s
.st_size
);
492 static int scan_builtin_modules(void)
501 if (!module_folders
&& init_module_folders())
503 for (p
= module_folders
; *p
; p
++) {
504 snprintf(path
, sizeof(path
), "%s%s", *p
, MOD_BUILTIN
);
505 if (!stat(path
, &st
) && S_ISREG(st
.st_mode
)) {
506 fp
= fopen(path
, "r");
512 return 0; /* OK if modules.builtin unavailable */
514 while (getline(&buf
, &buf_len
, fp
) > 0) {
518 name
= get_module_name(buf
);
521 m
= find_module(name
);
522 if (m
&& !strcmp(m
->name
, name
)) {
523 ULOG_WARN("found duplicate builtin module %s\n", name
);
526 m
= get_module_info(NULL
, name
);
528 ULOG_ERR("failed to find info for builtin module %s\n", name
);
541 static int scan_module_folder(const char *dir
)
543 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
548 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
549 sprintf(path
, "%s*.ko", dir
);
551 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
554 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
555 char *name
= get_module_name(gl
.gl_pathv
[j
]);
562 m
= find_module(name
);
566 m
= get_module_info(gl
.gl_pathv
[j
], name
);
572 opts
= kvlist_get(&options
, name
);
577 m
->state
= BLACKLISTED
;
579 m
->opts
= strdup(opts
);
587 static int scan_module_folders(void)
592 if (init_module_folders())
595 for (p
= module_folders
; *p
; p
++)
596 rv
|= scan_module_folder(*p
);
601 static int print_modinfo(const struct module
*m
)
603 const bool is_builtin
= (m
->state
== BUILTIN
);
604 unsigned int offset
, size
;
607 char *map
, *strings
, *mpath
;
612 mpath
= get_module_path(m
->name
);
613 map
= mmap_modinfo(mpath
, m
->name
, &s
, &offset
, &size
);
614 if (map
== MAP_FAILED
)
617 strings
= map
+ offset
;
619 printf("name:\t\t%s\n", m
->name
);
620 printf("filename:\t%s\n", is_builtin
? "(builtin)" : mpath
);
628 if (strings
>= map
+ offset
+ size
)
631 sep
= strstr(strings
, ".");
634 if (strlen(m
->name
) == (sep
- strings
) &&
635 !strncmp(strings
, m
->name
, sep
- strings
))
640 sep
= strstr(strings
, "=");
643 dup
= strndup(strings
, sep
- strings
);
645 if (strncmp(strings
, "parm", 4)) {
647 printf("%s:\t\t%s\n", dup
, sep
);
649 printf("%s:\t%s\n", dup
, sep
);
651 sep2
= strstr(sep
, ":");
657 pname
= strndup(sep
, sep2
- sep
);
659 pdata
= strdup(sep2
);
661 list_for_each_entry(p
, ¶ms
, list
)
662 if (!strcmp(pname
, p
->name
))
665 if (list_entry_is_h(p
, ¶ms
, list
)) {
666 p
= alloca(sizeof(*p
));
668 p
->desc
= p
->type
= NULL
;
669 list_add(&p
->list
, ¶ms
);
674 if (!strcmp(dup
, "parmtype"))
682 strings
= &sep
[strlen(sep
)];
685 list_for_each_entry(p
, ¶ms
, list
) {
686 printf("parm:\t\t%s", p
->name
);
688 printf(":%s", p
->desc
);
690 printf(" (%s)", p
->type
);
700 if (map
!= MAP_FAILED
)
701 munmap(map
, s
.st_size
);
706 static int deps_available(struct module
*m
, int verbose
)
711 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
717 m
= find_module(dep
);
720 ULOG_ERR("missing dependency %s\n", dep
);
721 if (verbose
&& m
&& (m
->state
!= LOADED
))
722 ULOG_ERR("dependency not loaded %s\n", dep
);
723 if (!m
|| (m
->state
!= LOADED
))
725 dep
+= strlen(dep
) + 1;
731 static int insert_module(char *path
, const char *options
)
738 ULOG_ERR("Path not specified\n");
742 if (stat(path
, &s
)) {
743 ULOG_ERR("missing module %s\n", path
);
747 fd
= open(path
, O_RDONLY
);
749 ULOG_ERR("cannot open %s\n", path
);
753 data
= malloc(s
.st_size
);
755 ULOG_ERR("out of memory\n");
759 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
760 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
765 ULOG_ERR("failed to read full module %s\n", path
);
774 static void load_moddeps(struct module
*_m
)
779 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
785 m
= find_module(dep
);
788 ULOG_ERR("failed to find dependency %s\n", dep
);
789 if (m
&& (m
->state
!= LOADED
)) {
794 dep
= dep
+ strlen(dep
) + 1;
798 static int load_modprobe(bool allow_load_retry
)
800 int loaded
, skipped
, failed
;
801 struct module_node
*mn
;
803 bool load_retry
= false;
804 static bool first_iteration
= true;
806 avl_for_each_element(&modules
, mn
, avl
) {
810 if (m
->state
== PROBE
)
818 avl_for_each_element(&modules
, mn
, avl
) {
822 if ((m
->state
== PROBE
) && (!deps_available(m
, 0)) && (!m
->error
|| load_retry
)) {
823 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
835 else if (m
->state
== PROBE
)
839 if (allow_load_retry
) {
840 /* if we can't load anything else let's try to load failed modules */
841 load_retry
= loaded
? (failed
&& !skipped
) : (failed
&& !load_retry
&& !first_iteration
);
844 first_iteration
= false;
845 } while (loaded
|| load_retry
);
847 return skipped
+ failed
;
850 static int print_insmod_usage(void)
852 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
857 static int print_modprobe_usage(void)
861 "\tmodprobe [-q] [-v] filename\n"
862 "\tmodprobe -a [-q] [-v] filename [filename...]\n"
868 static int print_usage(char *arg
)
870 ULOG_INFO("Usage:\n\t%s module\n", arg
);
875 static int main_insmod(int argc
, char **argv
)
877 char *name
, *cur
, *options
;
881 return print_insmod_usage();
883 name
= get_module_name(argv
[1]);
885 ULOG_ERR("cannot find module - %s\n", argv
[1]);
889 if (scan_loaded_modules())
892 if (find_module(name
)) {
893 ULOG_ERR("module is already loaded - %s\n", name
);
900 for (len
= 0, i
= 2; i
< argc
; i
++)
901 len
+= strlen(argv
[i
]) + 1;
903 options
= malloc(len
);
905 ULOG_ERR("out of memory\n");
912 for (i
= 2; i
< argc
; i
++) {
917 cur
+= sprintf(cur
, "%s", argv
[i
]);
920 if (init_module_folders()) {
921 fprintf(stderr
, "Failed to find the folder holding the modules\n");
926 if (get_module_path(argv
[1])) {
928 } else if (!get_module_path(name
)) {
929 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
934 ret
= insert_module(get_module_path(name
), options
);
936 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
943 static int main_rmmod(int argc
, char **argv
)
950 return print_usage("rmmod");
952 if (scan_loaded_modules())
955 if (scan_builtin_modules())
958 name
= get_module_name(argv
[1]);
959 m
= find_module(name
);
961 ULOG_ERR("module is not loaded\n");
964 if (m
->state
== BUILTIN
) {
965 ULOG_ERR("module is builtin\n");
968 ret
= syscall(__NR_delete_module
, m
->name
, 0);
971 ULOG_ERR("unloading the module failed\n");
978 static int main_lsmod(int argc
, char **argv
)
980 struct module_node
*mn
;
984 if (scan_loaded_modules())
987 avl_for_each_element(&modules
, mn
, avl
) {
991 if (m
->state
== LOADED
) {
992 printf("%-20s%8d%3d ",
993 m
->name
, m
->size
, m
->usage
);
994 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
998 dep
= dep
+ strlen(dep
) + 1;
1012 static int main_modinfo(int argc
, char **argv
)
1018 return print_usage("modinfo");
1020 if (scan_module_folders())
1023 if (scan_builtin_modules())
1026 name
= get_module_name(argv
[1]);
1027 m
= find_module(name
);
1029 ULOG_ERR("cannot find module - %s\n", argv
[1]);
1038 static int main_modprobe(int argc
, char **argv
)
1040 struct module_node
*mn
;
1044 int log_level
= LOG_WARNING
;
1047 bool use_all
= false;
1049 while ((opt
= getopt(argc
, argv
, "aqv")) != -1 ) {
1054 case 'q': /* shhhh! */
1058 log_level
= LOG_DEBUG
;
1061 return print_modprobe_usage();
1067 return print_modprobe_usage(); /* expected module after options */
1069 /* after print_modprobe_usage() so it won't be filtered out */
1070 ulog_threshold(log_level
);
1072 if (scan_module_folders())
1075 if (scan_loaded_modules())
1078 if (scan_builtin_modules())
1084 name
= get_module_name(argv
[optind
]);
1085 m
= find_module(name
);
1087 if (m
&& m
->state
== BLACKLISTED
) {
1089 ULOG_INFO("%s is blacklisted\n", name
);
1090 } else if (m
&& m
->state
== LOADED
) {
1092 ULOG_INFO("%s is already loaded\n", name
);
1093 } else if (m
&& m
->state
== BUILTIN
) {
1095 ULOG_INFO("%s is builtin\n", name
);
1098 ULOG_ERR("failed to find a module named %s\n", name
);
1105 } while (use_all
&& optind
< argc
);
1107 load_fail
= load_modprobe(true);
1109 ULOG_ERR("%d module%s could not be probed\n",
1110 load_fail
, (load_fail
== 1) ? ("") : ("s"));
1112 avl_for_each_element(&modules
, mn
, avl
) {
1116 if ((m
->state
== PROBE
) || m
->error
)
1117 ULOG_ERR("- %s\n", m
->name
);
1128 static int main_loader(int argc
, char **argv
)
1130 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
1131 char *dir
= "/etc/modules.d/";
1132 struct module_node
*mn
;
1136 int ret
= 0, fail
, j
;
1141 path
= malloc(strlen(dir
) + 2);
1143 ULOG_ERR("out of memory\n");
1150 if (scan_module_folders()) {
1155 if (scan_loaded_modules()) {
1160 ULOG_INFO("loading kernel modules from %s\n", path
);
1162 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
1165 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
1166 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
1171 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
1175 while (getline(&mod
, &mod_len
, fp
) > 0) {
1176 char *nl
= strchr(mod
, '\n');
1183 opts
= strchr(mod
, ' ');
1187 m
= find_module(get_module_name(mod
));
1188 if (!m
|| m
->state
== LOADED
|| m
->state
== BLACKLISTED
)
1193 char *prev
= m
->opts
;
1195 fail
= asprintf(&m
->opts
, "%s %s", prev
, opts
);
1198 ULOG_ERR("out of memory for opts %s\n", opts
);
1205 m
->opts
= strdup(opts
);
1209 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
1210 load_modprobe(false);
1217 fail
= load_modprobe(true);
1220 ULOG_ERR("%d module%s could not be probed\n",
1221 fail
, (fail
== 1) ? ("") : ("s"));
1223 avl_for_each_element(&modules
, mn
, avl
) {
1227 if ((m
->state
== PROBE
) || (m
->error
))
1228 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
1231 ULOG_INFO("done loading kernel modules from %s\n", path
);
1242 static inline char weight(char c
)
1244 return c
== '_' ? '-' : c
;
1247 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
1249 const char *s1
= k1
;
1250 const char *s2
= k2
;
1252 while (*s1
&& (weight(*s1
) == weight(*s2
)))
1258 return (unsigned char)weight(*s1
) - (unsigned char)weight(*s2
);
1264 static char buf
[512];
1268 f
= fopen("/etc/modules.conf", "r");
1272 while ((s
= fgets(buf
, sizeof(buf
), f
)) != NULL
) {
1273 char *c
, *cmd
, *mod
;
1286 while (c
> s
&& isspace(c
[-1])) {
1291 cmd
= strsep(&s
, " \t");
1298 mod
= strsep(&s
, " \t");
1302 if (!strcmp(cmd
, "blacklist")) {
1303 kvlist_set(&options
, mod
, "\x01");
1307 if (!strcmp(cmd
, "options")) {
1308 char *prev
= kvlist_get(&options
, mod
);
1317 if (prev
&& prev
[0] == '\x01')
1321 kvlist_set(&options
, mod
, s
);
1325 if (asprintf(&val
, "%s %s", prev
, s
) < 0)
1328 kvlist_set(&options
, mod
, val
);
1337 int main(int argc
, char **argv
)
1339 char *exec
= basename(*argv
);
1341 avl_init(&modules
, avl_modcmp
, false, NULL
);
1342 if (!strcmp(exec
, "insmod"))
1343 return main_insmod(argc
, argv
);
1345 if (!strcmp(exec
, "rmmod"))
1346 return main_rmmod(argc
, argv
);
1348 if (!strcmp(exec
, "lsmod"))
1349 return main_lsmod(argc
, argv
);
1351 if (!strcmp(exec
, "modinfo"))
1352 return main_modinfo(argc
, argv
);
1356 if (!strcmp(exec
, "modprobe"))
1357 return main_modprobe(argc
, argv
);
1359 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
1360 return main_loader(argc
, argv
);