0afc79560693e9a67bf0ca74a0dfd934b5387e75
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)
317 fp
= fopen("/proc/modules", "r");
319 ULOG_ERR("failed to open /proc/modules\n");
323 while (getline(&buf
, &buf_len
, fp
) > 0) {
327 m
.name
= strtok(buf
, " ");
328 m
.size
= atoi(strtok(NULL
, " "));
329 m
.usage
= atoi(strtok(NULL
, " "));
330 m
.depends
= strtok(NULL
, " ");
332 if (!m
.name
|| !m
.depends
)
335 n
= find_module(m
.name
);
337 /* possibly a module outside /lib/modules/<ver>/ */
338 n
= alloc_module(m
.name
, NULL
, 0, m
.depends
, m
.size
);
341 ULOG_ERR("Failed to allocate memory for module\n");
356 static struct module
* get_module_info(const char *module
, const char *name
)
358 int fd
= open(module
, O_RDONLY
);
359 unsigned int offset
, size
;
360 char *map
= MAP_FAILED
, *strings
, *dep
= NULL
;
361 const char **aliases
= NULL
;
362 const char **aliasesr
;
364 struct module
*m
= NULL
;
368 ULOG_ERR("failed to open %s\n", module
);
372 if (fstat(fd
, &s
) == -1) {
373 ULOG_ERR("failed to stat %s\n", module
);
377 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
378 if (map
== MAP_FAILED
) {
379 ULOG_ERR("failed to mmap %s\n", module
);
383 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
384 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
388 strings
= map
+ offset
;
395 if (strings
>= map
+ offset
+ size
)
397 sep
= strstr(strings
, "=");
402 if (!strncmp(strings
, "depends=", len
+ 1))
404 else if (!strncmp(strings
, "alias=", len
+ 1)) {
405 aliasesr
= realloc(aliases
, sizeof(sep
) * (naliases
+ 1));
407 ULOG_ERR("out of memory\n");
412 aliases
[naliases
++] = sep
;
414 strings
= &sep
[strlen(sep
)];
417 m
= alloc_module(name
, aliases
, naliases
, dep
, s
.st_size
);
423 if (map
!= MAP_FAILED
)
424 munmap(map
, s
.st_size
);
434 static int scan_module_folder(const char *dir
)
436 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
441 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
442 sprintf(path
, "%s*.ko", dir
);
444 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
447 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
448 char *name
= get_module_name(gl
.gl_pathv
[j
]);
455 m
= find_module(name
);
459 m
= get_module_info(gl
.gl_pathv
[j
], name
);
465 opts
= kvlist_get(&options
, name
);
470 m
->state
= BLACKLISTED
;
472 m
->opts
= strdup(opts
);
480 static int scan_module_folders(void)
485 if (init_module_folders())
488 for (p
= module_folders
; *p
; p
++)
489 rv
|= scan_module_folder(*p
);
494 static int print_modinfo(char *module
)
496 int fd
= open(module
, O_RDONLY
);
497 unsigned int offset
, size
;
499 char *map
= MAP_FAILED
, *strings
;
503 ULOG_ERR("failed to open %s\n", module
);
507 if (fstat(fd
, &s
) == -1) {
508 ULOG_ERR("failed to stat %s\n", module
);
512 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
513 if (map
== MAP_FAILED
) {
514 ULOG_ERR("failed to mmap %s\n", module
);
518 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
519 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
523 strings
= map
+ offset
;
524 printf("module:\t\t%s\n", module
);
531 if (strings
>= map
+ offset
+ size
)
533 sep
= strstr(strings
, "=");
536 dup
= strndup(strings
, sep
- strings
);
538 if (strncmp(strings
, "parm", 4)) {
540 printf("%s:\t\t%s\n", dup
, sep
);
542 printf("%s:\t%s\n", dup
, sep
);
544 strings
= &sep
[strlen(sep
)];
552 if (map
!= MAP_FAILED
)
553 munmap(map
, s
.st_size
);
561 static int deps_available(struct module
*m
, int verbose
)
566 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
572 m
= find_module(dep
);
575 ULOG_ERR("missing dependency %s\n", dep
);
576 if (verbose
&& m
&& (m
->state
!= LOADED
))
577 ULOG_ERR("dependency not loaded %s\n", dep
);
578 if (!m
|| (m
->state
!= LOADED
))
580 dep
+= strlen(dep
) + 1;
586 static int insert_module(char *path
, const char *options
)
593 ULOG_ERR("Path not specified\n");
597 if (stat(path
, &s
)) {
598 ULOG_ERR("missing module %s\n", path
);
602 fd
= open(path
, O_RDONLY
);
604 ULOG_ERR("cannot open %s\n", path
);
608 data
= malloc(s
.st_size
);
610 ULOG_ERR("out of memory\n");
614 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
615 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
620 ULOG_ERR("failed to read full module %s\n", path
);
629 static void load_moddeps(struct module
*_m
)
634 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
640 m
= find_module(dep
);
643 ULOG_ERR("failed to find dependency %s\n", dep
);
644 if (m
&& (m
->state
!= LOADED
)) {
649 dep
= dep
+ strlen(dep
) + 1;
653 static int load_modprobe(bool allow_load_retry
)
655 int loaded
, skipped
, failed
;
656 struct module_node
*mn
;
658 bool load_retry
= false;
659 static bool first_iteration
= true;
661 avl_for_each_element(&modules
, mn
, avl
) {
665 if (m
->state
== PROBE
)
673 avl_for_each_element(&modules
, mn
, avl
) {
677 if ((m
->state
== PROBE
) && (!deps_available(m
, 0)) && (!m
->error
|| load_retry
)) {
678 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
690 else if (m
->state
== PROBE
)
694 if (allow_load_retry
) {
695 /* if we can't load anything else let's try to load failed modules */
696 load_retry
= loaded
? (failed
&& !skipped
) : (failed
&& !load_retry
&& !first_iteration
);
699 first_iteration
= false;
700 } while (loaded
|| load_retry
);
702 return skipped
+ failed
;
705 static int print_insmod_usage(void)
707 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
712 static int print_modprobe_usage(void)
716 "\tmodprobe [-q] [-v] filename\n"
717 "\tmodprobe -a [-q] [-v] filename [filename...]\n"
723 static int print_usage(char *arg
)
725 ULOG_INFO("Usage:\n\t%s module\n", arg
);
730 static int main_insmod(int argc
, char **argv
)
732 char *name
, *cur
, *options
;
736 return print_insmod_usage();
738 name
= get_module_name(argv
[1]);
740 ULOG_ERR("cannot find module - %s\n", argv
[1]);
744 if (scan_loaded_modules())
747 if (find_module(name
)) {
748 ULOG_ERR("module is already loaded - %s\n", name
);
755 for (len
= 0, i
= 2; i
< argc
; i
++)
756 len
+= strlen(argv
[i
]) + 1;
758 options
= malloc(len
);
760 ULOG_ERR("out of memory\n");
767 for (i
= 2; i
< argc
; i
++) {
772 cur
+= sprintf(cur
, "%s", argv
[i
]);
775 if (init_module_folders()) {
776 fprintf(stderr
, "Failed to find the folder holding the modules\n");
781 if (get_module_path(argv
[1])) {
783 } else if (!get_module_path(name
)) {
784 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
789 ret
= insert_module(get_module_path(name
), options
);
791 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
798 static int main_rmmod(int argc
, char **argv
)
805 return print_usage("rmmod");
807 if (scan_loaded_modules())
810 name
= get_module_name(argv
[1]);
811 m
= find_module(name
);
813 ULOG_ERR("module is not loaded\n");
816 ret
= syscall(__NR_delete_module
, m
->name
, 0);
819 ULOG_ERR("unloading the module failed\n");
826 static int main_lsmod(int argc
, char **argv
)
828 struct module_node
*mn
;
832 if (scan_loaded_modules())
835 avl_for_each_element(&modules
, mn
, avl
) {
839 if (m
->state
== LOADED
) {
840 printf("%-20s%8d%3d ",
841 m
->name
, m
->size
, m
->usage
);
842 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
846 dep
= dep
+ strlen(dep
) + 1;
860 static int main_modinfo(int argc
, char **argv
)
866 return print_usage("modinfo");
868 if (scan_module_folders())
871 name
= get_module_name(argv
[1]);
872 m
= find_module(name
);
874 ULOG_ERR("cannot find module - %s\n", argv
[1]);
878 name
= get_module_path(m
->name
);
880 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
889 static int main_modprobe(int argc
, char **argv
)
891 struct module_node
*mn
;
895 int log_level
= LOG_WARNING
;
898 bool use_all
= false;
900 while ((opt
= getopt(argc
, argv
, "aqv")) != -1 ) {
905 case 'q': /* shhhh! */
909 log_level
= LOG_DEBUG
;
912 return print_modprobe_usage();
918 return print_modprobe_usage(); /* expected module after options */
920 /* after print_modprobe_usage() so it won't be filtered out */
921 ulog_threshold(log_level
);
923 if (scan_module_folders())
926 if (scan_loaded_modules())
932 name
= get_module_name(argv
[optind
]);
933 m
= find_module(name
);
935 if (m
&& m
->state
== BLACKLISTED
) {
937 ULOG_INFO("%s is blacklisted\n", name
);
938 } else if (m
&& m
->state
== LOADED
) {
940 ULOG_INFO("%s is already loaded\n", name
);
943 ULOG_ERR("failed to find a module named %s\n", name
);
950 } while (use_all
&& optind
< argc
);
952 load_fail
= load_modprobe(true);
954 ULOG_ERR("%d module%s could not be probed\n",
955 load_fail
, (load_fail
== 1) ? ("") : ("s"));
957 avl_for_each_element(&modules
, mn
, avl
) {
961 if ((m
->state
== PROBE
) || m
->error
)
962 ULOG_ERR("- %s\n", m
->name
);
973 static int main_loader(int argc
, char **argv
)
975 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
976 char *dir
= "/etc/modules.d/";
977 struct module_node
*mn
;
981 int ret
= 0, fail
, j
;
986 path
= malloc(strlen(dir
) + 2);
988 ULOG_ERR("out of memory\n");
995 if (scan_module_folders()) {
1000 if (scan_loaded_modules()) {
1005 ULOG_INFO("loading kernel modules from %s\n", path
);
1007 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
1010 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
1011 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
1016 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
1020 while (getline(&mod
, &mod_len
, fp
) > 0) {
1021 char *nl
= strchr(mod
, '\n');
1028 opts
= strchr(mod
, ' ');
1032 m
= find_module(get_module_name(mod
));
1033 if (!m
|| m
->state
== LOADED
|| m
->state
== BLACKLISTED
)
1038 char *prev
= m
->opts
;
1040 fail
= asprintf(&m
->opts
, "%s %s", prev
, opts
);
1043 ULOG_ERR("out of memory for opts %s\n", opts
);
1050 m
->opts
= strdup(opts
);
1054 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
1055 load_modprobe(false);
1062 fail
= load_modprobe(true);
1065 ULOG_ERR("%d module%s could not be probed\n",
1066 fail
, (fail
== 1) ? ("") : ("s"));
1068 avl_for_each_element(&modules
, mn
, avl
) {
1072 if ((m
->state
== PROBE
) || (m
->error
))
1073 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
1076 ULOG_INFO("done loading kernel modules from %s\n", path
);
1087 static inline char weight(char c
)
1089 return c
== '_' ? '-' : c
;
1092 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
1094 const char *s1
= k1
;
1095 const char *s2
= k2
;
1097 while (*s1
&& (weight(*s1
) == weight(*s2
)))
1103 return (unsigned char)weight(*s1
) - (unsigned char)weight(*s2
);
1109 static char buf
[512];
1113 f
= fopen("/etc/modules.conf", "r");
1117 while ((s
= fgets(buf
, sizeof(buf
), f
)) != NULL
) {
1118 char *c
, *cmd
, *mod
;
1131 while (c
> s
&& isspace(c
[-1])) {
1136 cmd
= strsep(&s
, " \t");
1143 mod
= strsep(&s
, " \t");
1147 if (!strcmp(cmd
, "blacklist")) {
1148 kvlist_set(&options
, mod
, "\x01");
1152 if (!strcmp(cmd
, "options")) {
1153 char *prev
= kvlist_get(&options
, mod
);
1162 if (prev
&& prev
[0] == '\x01')
1166 kvlist_set(&options
, mod
, s
);
1170 if (asprintf(&val
, "%s %s", prev
, s
) < 0)
1173 kvlist_set(&options
, mod
, val
);
1182 int main(int argc
, char **argv
)
1184 char *exec
= basename(*argv
);
1186 avl_init(&modules
, avl_modcmp
, false, NULL
);
1187 if (!strcmp(exec
, "insmod"))
1188 return main_insmod(argc
, argv
);
1190 if (!strcmp(exec
, "rmmod"))
1191 return main_rmmod(argc
, argv
);
1193 if (!strcmp(exec
, "lsmod"))
1194 return main_lsmod(argc
, argv
);
1196 if (!strcmp(exec
, "modinfo"))
1197 return main_modinfo(argc
, argv
);
1201 if (!strcmp(exec
, "modprobe"))
1202 return main_modprobe(argc
, argv
);
1204 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
1205 return main_loader(argc
, argv
);