259ac52d8fa9ce1a4aab43e704525d8583a756c0
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
);
340 ULOG_ERR("Failed to allocate memory for module\n");
353 static struct module
* get_module_info(const char *module
, const char *name
)
355 int fd
= open(module
, O_RDONLY
);
356 unsigned int offset
, size
;
357 char *map
= MAP_FAILED
, *strings
, *dep
= NULL
;
358 const char **aliases
= NULL
;
359 const char **aliasesr
;
361 struct module
*m
= NULL
;
365 ULOG_ERR("failed to open %s\n", module
);
369 if (fstat(fd
, &s
) == -1) {
370 ULOG_ERR("failed to stat %s\n", module
);
374 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
375 if (map
== MAP_FAILED
) {
376 ULOG_ERR("failed to mmap %s\n", module
);
380 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
381 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
385 strings
= map
+ offset
;
392 if (strings
>= map
+ offset
+ size
)
394 sep
= strstr(strings
, "=");
399 if (!strncmp(strings
, "depends=", len
+ 1))
401 else if (!strncmp(strings
, "alias=", len
+ 1)) {
402 aliasesr
= realloc(aliases
, sizeof(sep
) * (naliases
+ 1));
404 ULOG_ERR("out of memory\n");
409 aliases
[naliases
++] = sep
;
411 strings
= &sep
[strlen(sep
)];
414 m
= alloc_module(name
, aliases
, naliases
, dep
, s
.st_size
);
420 if (map
!= MAP_FAILED
)
421 munmap(map
, s
.st_size
);
431 static int scan_module_folder(const char *dir
)
433 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
438 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
439 sprintf(path
, "%s*.ko", dir
);
441 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
444 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
445 char *name
= get_module_name(gl
.gl_pathv
[j
]);
452 m
= find_module(name
);
456 m
= get_module_info(gl
.gl_pathv
[j
], name
);
462 opts
= kvlist_get(&options
, name
);
467 m
->state
= BLACKLISTED
;
469 m
->opts
= strdup(opts
);
477 static int scan_module_folders(void)
482 if (init_module_folders())
485 for (p
= module_folders
; *p
; p
++)
486 rv
|= scan_module_folder(*p
);
491 static int print_modinfo(char *module
)
493 int fd
= open(module
, O_RDONLY
);
494 unsigned int offset
, size
;
496 char *map
= MAP_FAILED
, *strings
;
500 ULOG_ERR("failed to open %s\n", module
);
504 if (fstat(fd
, &s
) == -1) {
505 ULOG_ERR("failed to stat %s\n", module
);
509 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
510 if (map
== MAP_FAILED
) {
511 ULOG_ERR("failed to mmap %s\n", module
);
515 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
516 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
520 strings
= map
+ offset
;
521 printf("module:\t\t%s\n", module
);
528 if (strings
>= map
+ offset
+ size
)
530 sep
= strstr(strings
, "=");
533 dup
= strndup(strings
, sep
- strings
);
535 if (strncmp(strings
, "parm", 4)) {
537 printf("%s:\t\t%s\n", dup
, sep
);
539 printf("%s:\t%s\n", dup
, sep
);
541 strings
= &sep
[strlen(sep
)];
549 if (map
!= MAP_FAILED
)
550 munmap(map
, s
.st_size
);
558 static int deps_available(struct module
*m
, int verbose
)
563 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
569 m
= find_module(dep
);
572 ULOG_ERR("missing dependency %s\n", dep
);
573 if (verbose
&& m
&& (m
->state
!= LOADED
))
574 ULOG_ERR("dependency not loaded %s\n", dep
);
575 if (!m
|| (m
->state
!= LOADED
))
577 dep
+= strlen(dep
) + 1;
583 static int insert_module(char *path
, const char *options
)
590 ULOG_ERR("Path not specified\n");
594 if (stat(path
, &s
)) {
595 ULOG_ERR("missing module %s\n", path
);
599 fd
= open(path
, O_RDONLY
);
601 ULOG_ERR("cannot open %s\n", path
);
605 data
= malloc(s
.st_size
);
607 ULOG_ERR("out of memory\n");
611 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
612 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
617 ULOG_ERR("failed to read full module %s\n", path
);
626 static void load_moddeps(struct module
*_m
)
631 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
637 m
= find_module(dep
);
640 ULOG_ERR("failed to find dependency %s\n", dep
);
641 if (m
&& (m
->state
!= LOADED
)) {
646 dep
= dep
+ strlen(dep
) + 1;
650 static int load_modprobe(bool allow_load_retry
)
652 int loaded
, skipped
, failed
;
653 struct module_node
*mn
;
655 bool load_retry
= false;
656 static bool first_iteration
= true;
658 avl_for_each_element(&modules
, mn
, avl
) {
662 if (m
->state
== PROBE
)
670 avl_for_each_element(&modules
, mn
, avl
) {
674 if ((m
->state
== PROBE
) && (!deps_available(m
, 0)) && (!m
->error
|| load_retry
)) {
675 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
687 else if (m
->state
== PROBE
)
691 if (allow_load_retry
) {
692 /* if we can't load anything else let's try to load failed modules */
693 load_retry
= loaded
? (failed
&& !skipped
) : (failed
&& !load_retry
&& !first_iteration
);
696 first_iteration
= false;
697 } while (loaded
|| load_retry
);
699 return skipped
+ failed
;
702 static int print_insmod_usage(void)
704 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
709 static int print_modprobe_usage(void)
713 "\tmodprobe [-q] [-v] filename\n"
714 "\tmodprobe -a [-q] [-v] filename [filename...]\n"
720 static int print_usage(char *arg
)
722 ULOG_INFO("Usage:\n\t%s module\n", arg
);
727 static int main_insmod(int argc
, char **argv
)
729 char *name
, *cur
, *options
;
733 return print_insmod_usage();
735 name
= get_module_name(argv
[1]);
737 ULOG_ERR("cannot find module - %s\n", argv
[1]);
741 if (scan_loaded_modules())
744 if (find_module(name
)) {
745 ULOG_ERR("module is already loaded - %s\n", name
);
752 for (len
= 0, i
= 2; i
< argc
; i
++)
753 len
+= strlen(argv
[i
]) + 1;
755 options
= malloc(len
);
757 ULOG_ERR("out of memory\n");
764 for (i
= 2; i
< argc
; i
++) {
769 cur
+= sprintf(cur
, "%s", argv
[i
]);
772 if (init_module_folders()) {
773 fprintf(stderr
, "Failed to find the folder holding the modules\n");
778 if (get_module_path(argv
[1])) {
780 } else if (!get_module_path(name
)) {
781 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
786 ret
= insert_module(get_module_path(name
), options
);
788 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
795 static int main_rmmod(int argc
, char **argv
)
802 return print_usage("rmmod");
804 if (scan_loaded_modules())
807 name
= get_module_name(argv
[1]);
808 m
= find_module(name
);
810 ULOG_ERR("module is not loaded\n");
813 ret
= syscall(__NR_delete_module
, m
->name
, 0);
816 ULOG_ERR("unloading the module failed\n");
823 static int main_lsmod(int argc
, char **argv
)
825 struct module_node
*mn
;
829 if (scan_loaded_modules())
832 avl_for_each_element(&modules
, mn
, avl
) {
836 if (m
->state
== LOADED
) {
837 printf("%-20s%8d%3d ",
838 m
->name
, m
->size
, m
->usage
);
839 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
843 dep
= dep
+ strlen(dep
) + 1;
857 static int main_modinfo(int argc
, char **argv
)
863 return print_usage("modinfo");
865 if (scan_module_folders())
868 name
= get_module_name(argv
[1]);
869 m
= find_module(name
);
871 ULOG_ERR("cannot find module - %s\n", argv
[1]);
875 name
= get_module_path(m
->name
);
877 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
886 static int main_modprobe(int argc
, char **argv
)
888 struct module_node
*mn
;
892 int log_level
= LOG_WARNING
;
895 bool use_all
= false;
897 while ((opt
= getopt(argc
, argv
, "aqv")) != -1 ) {
902 case 'q': /* shhhh! */
906 log_level
= LOG_DEBUG
;
909 return print_modprobe_usage();
915 return print_modprobe_usage(); /* expected module after options */
917 /* after print_modprobe_usage() so it won't be filtered out */
918 ulog_threshold(log_level
);
920 if (scan_module_folders())
923 if (scan_loaded_modules())
929 name
= get_module_name(argv
[optind
]);
930 m
= find_module(name
);
932 if (m
&& m
->state
== BLACKLISTED
) {
934 ULOG_INFO("%s is blacklisted\n", name
);
935 } else if (m
&& m
->state
== LOADED
) {
937 ULOG_INFO("%s is already loaded\n", name
);
940 ULOG_ERR("failed to find a module named %s\n", name
);
947 } while (use_all
&& optind
< argc
);
949 load_fail
= load_modprobe(true);
951 ULOG_ERR("%d module%s could not be probed\n",
952 load_fail
, (load_fail
== 1) ? ("") : ("s"));
954 avl_for_each_element(&modules
, mn
, avl
) {
958 if ((m
->state
== PROBE
) || m
->error
)
959 ULOG_ERR("- %s\n", m
->name
);
970 static int main_loader(int argc
, char **argv
)
972 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
973 char *dir
= "/etc/modules.d/";
974 struct module_node
*mn
;
978 int ret
= 0, fail
, j
;
983 path
= malloc(strlen(dir
) + 2);
985 ULOG_ERR("out of memory\n");
992 if (scan_module_folders()) {
997 if (scan_loaded_modules()) {
1002 ULOG_INFO("loading kernel modules from %s\n", path
);
1004 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
1007 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
1008 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
1013 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
1017 while (getline(&mod
, &mod_len
, fp
) > 0) {
1018 char *nl
= strchr(mod
, '\n');
1025 opts
= strchr(mod
, ' ');
1029 m
= find_module(get_module_name(mod
));
1030 if (!m
|| m
->state
== LOADED
|| m
->state
== BLACKLISTED
)
1035 char *prev
= m
->opts
;
1037 fail
= asprintf(&m
->opts
, "%s %s", prev
, opts
);
1040 ULOG_ERR("out of memory for opts %s\n", opts
);
1047 m
->opts
= strdup(opts
);
1051 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
1052 load_modprobe(false);
1059 fail
= load_modprobe(true);
1062 ULOG_ERR("%d module%s could not be probed\n",
1063 fail
, (fail
== 1) ? ("") : ("s"));
1065 avl_for_each_element(&modules
, mn
, avl
) {
1069 if ((m
->state
== PROBE
) || (m
->error
))
1070 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
1073 ULOG_INFO("done loading kernel modules from %s\n", path
);
1084 static inline char weight(char c
)
1086 return c
== '_' ? '-' : c
;
1089 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
1091 const char *s1
= k1
;
1092 const char *s2
= k2
;
1094 while (*s1
&& (weight(*s1
) == weight(*s2
)))
1100 return (unsigned char)weight(*s1
) - (unsigned char)weight(*s2
);
1106 static char buf
[512];
1110 f
= fopen("/etc/modules.conf", "r");
1114 while ((s
= fgets(buf
, sizeof(buf
), f
)) != NULL
) {
1115 char *c
, *cmd
, *mod
;
1128 while (c
> s
&& isspace(c
[-1])) {
1133 cmd
= strsep(&s
, " \t");
1140 mod
= strsep(&s
, " \t");
1144 if (!strcmp(cmd
, "blacklist")) {
1145 kvlist_set(&options
, mod
, "\x01");
1149 if (!strcmp(cmd
, "options")) {
1150 char *prev
= kvlist_get(&options
, mod
);
1159 if (prev
&& prev
[0] == '\x01')
1163 kvlist_set(&options
, mod
, s
);
1167 if (asprintf(&val
, "%s %s", prev
, s
) < 0)
1170 kvlist_set(&options
, mod
, val
);
1179 int main(int argc
, char **argv
)
1181 char *exec
= basename(*argv
);
1183 avl_init(&modules
, avl_modcmp
, false, NULL
);
1184 if (!strcmp(exec
, "insmod"))
1185 return main_insmod(argc
, argv
);
1187 if (!strcmp(exec
, "rmmod"))
1188 return main_rmmod(argc
, argv
);
1190 if (!strcmp(exec
, "lsmod"))
1191 return main_lsmod(argc
, argv
);
1193 if (!strcmp(exec
, "modinfo"))
1194 return main_modinfo(argc
, argv
);
1198 if (!strcmp(exec
, "modprobe"))
1199 return main_modprobe(argc
, argv
);
1201 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
1202 return main_loader(argc
, argv
);