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
;
440 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
441 sprintf(path
, "%s*.ko", dir
);
443 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
446 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
447 char *name
= get_module_name(gl
.gl_pathv
[j
]);
454 m
= find_module(name
);
458 m
= get_module_info(gl
.gl_pathv
[j
], name
);
464 opts
= kvlist_get(&options
, name
);
469 m
->state
= BLACKLISTED
;
471 m
->opts
= strdup(opts
);
479 static int scan_module_folders(void)
484 if (init_module_folders())
487 for (p
= module_folders
; *p
; p
++)
488 rv
|= scan_module_folder(*p
);
493 static int print_modinfo(char *module
)
495 int fd
= open(module
, O_RDONLY
);
496 unsigned int offset
, size
;
498 char *map
= MAP_FAILED
, *strings
;
502 ULOG_ERR("failed to open %s\n", module
);
506 if (fstat(fd
, &s
) == -1) {
507 ULOG_ERR("failed to stat %s\n", module
);
511 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
512 if (map
== MAP_FAILED
) {
513 ULOG_ERR("failed to mmap %s\n", module
);
517 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
518 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
522 strings
= map
+ offset
;
523 printf("module:\t\t%s\n", module
);
530 if (strings
>= map
+ offset
+ size
)
532 sep
= strstr(strings
, "=");
535 dup
= strndup(strings
, sep
- strings
);
537 if (strncmp(strings
, "parm", 4)) {
539 printf("%s:\t\t%s\n", dup
, sep
);
541 printf("%s:\t%s\n", dup
, sep
);
543 strings
= &sep
[strlen(sep
)];
551 if (map
!= MAP_FAILED
)
552 munmap(map
, s
.st_size
);
560 static int deps_available(struct module
*m
, int verbose
)
565 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
571 m
= find_module(dep
);
574 ULOG_ERR("missing dependency %s\n", dep
);
575 if (verbose
&& m
&& (m
->state
!= LOADED
))
576 ULOG_ERR("dependency not loaded %s\n", dep
);
577 if (!m
|| (m
->state
!= LOADED
))
579 dep
+= strlen(dep
) + 1;
585 static int insert_module(char *path
, const char *options
)
592 ULOG_ERR("Path not specified\n");
596 if (stat(path
, &s
)) {
597 ULOG_ERR("missing module %s\n", path
);
601 fd
= open(path
, O_RDONLY
);
603 ULOG_ERR("cannot open %s\n", path
);
607 data
= malloc(s
.st_size
);
609 ULOG_ERR("out of memory\n");
613 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
614 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
619 ULOG_ERR("failed to read full module %s\n", path
);
628 static void load_moddeps(struct module
*_m
)
633 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
639 m
= find_module(dep
);
642 ULOG_ERR("failed to find dependency %s\n", dep
);
643 if (m
&& (m
->state
!= LOADED
)) {
648 dep
= dep
+ strlen(dep
) + 1;
652 static int load_modprobe(bool allow_load_retry
)
654 int loaded
, skipped
, failed
;
655 struct module_node
*mn
;
657 bool load_retry
= false;
658 static bool first_iteration
= true;
660 avl_for_each_element(&modules
, mn
, avl
) {
664 if (m
->state
== PROBE
)
672 avl_for_each_element(&modules
, mn
, avl
) {
676 if ((m
->state
== PROBE
) && (!deps_available(m
, 0)) && (!m
->error
|| load_retry
)) {
677 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
689 else if (m
->state
== PROBE
)
693 if (allow_load_retry
) {
694 /* if we can't load anything else let's try to load failed modules */
695 load_retry
= loaded
? (failed
&& !skipped
) : (failed
&& !load_retry
&& !first_iteration
);
698 first_iteration
= false;
699 } while (loaded
|| load_retry
);
701 return skipped
+ failed
;
704 static int print_insmod_usage(void)
706 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
711 static int print_modprobe_usage(void)
715 "\tmodprobe [-q] [-v] filename\n"
716 "\tmodprobe -a [-q] [-v] filename [filename...]\n"
722 static int print_usage(char *arg
)
724 ULOG_INFO("Usage:\n\t%s module\n", arg
);
729 static int main_insmod(int argc
, char **argv
)
731 char *name
, *cur
, *options
;
735 return print_insmod_usage();
737 name
= get_module_name(argv
[1]);
739 ULOG_ERR("cannot find module - %s\n", argv
[1]);
743 if (scan_loaded_modules())
746 if (find_module(name
)) {
747 ULOG_ERR("module is already loaded - %s\n", name
);
754 for (len
= 0, i
= 2; i
< argc
; i
++)
755 len
+= strlen(argv
[i
]) + 1;
757 options
= malloc(len
);
759 ULOG_ERR("out of memory\n");
766 for (i
= 2; i
< argc
; i
++) {
771 cur
+= sprintf(cur
, "%s", argv
[i
]);
774 if (init_module_folders()) {
775 fprintf(stderr
, "Failed to find the folder holding the modules\n");
780 if (get_module_path(argv
[1])) {
782 } else if (!get_module_path(name
)) {
783 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
788 ret
= insert_module(get_module_path(name
), options
);
790 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
797 static int main_rmmod(int argc
, char **argv
)
804 return print_usage("rmmod");
806 if (scan_loaded_modules())
809 name
= get_module_name(argv
[1]);
810 m
= find_module(name
);
812 ULOG_ERR("module is not loaded\n");
815 ret
= syscall(__NR_delete_module
, m
->name
, 0);
818 ULOG_ERR("unloading the module failed\n");
825 static int main_lsmod(int argc
, char **argv
)
827 struct module_node
*mn
;
831 if (scan_loaded_modules())
834 avl_for_each_element(&modules
, mn
, avl
) {
838 if (m
->state
== LOADED
) {
839 printf("%-20s%8d%3d ",
840 m
->name
, m
->size
, m
->usage
);
841 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
845 dep
= dep
+ strlen(dep
) + 1;
859 static int main_modinfo(int argc
, char **argv
)
865 return print_usage("modinfo");
867 if (scan_module_folders())
870 name
= get_module_name(argv
[1]);
871 m
= find_module(name
);
873 ULOG_ERR("cannot find module - %s\n", argv
[1]);
877 name
= get_module_path(m
->name
);
879 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
888 static int main_modprobe(int argc
, char **argv
)
890 struct module_node
*mn
;
894 int log_level
= LOG_WARNING
;
897 bool use_all
= false;
899 while ((opt
= getopt(argc
, argv
, "aqv")) != -1 ) {
904 case 'q': /* shhhh! */
908 log_level
= LOG_DEBUG
;
911 return print_modprobe_usage();
917 return print_modprobe_usage(); /* expected module after options */
919 /* after print_modprobe_usage() so it won't be filtered out */
920 ulog_threshold(log_level
);
922 if (scan_module_folders())
925 if (scan_loaded_modules())
931 name
= get_module_name(argv
[optind
]);
932 m
= find_module(name
);
934 if (m
&& m
->state
== BLACKLISTED
) {
936 ULOG_INFO("%s is blacklisted\n", name
);
937 } else if (m
&& m
->state
== LOADED
) {
939 ULOG_INFO("%s is already loaded\n", name
);
942 ULOG_ERR("failed to find a module named %s\n", name
);
949 } while (use_all
&& optind
< argc
);
951 load_fail
= load_modprobe(true);
953 ULOG_ERR("%d module%s could not be probed\n",
954 load_fail
, (load_fail
== 1) ? ("") : ("s"));
956 avl_for_each_element(&modules
, mn
, avl
) {
960 if ((m
->state
== PROBE
) || m
->error
)
961 ULOG_ERR("- %s\n", m
->name
);
972 static int main_loader(int argc
, char **argv
)
974 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
975 char *dir
= "/etc/modules.d/";
976 struct module_node
*mn
;
980 int ret
= 0, fail
, j
;
985 path
= malloc(strlen(dir
) + 2);
987 ULOG_ERR("out of memory\n");
994 if (scan_module_folders()) {
999 if (scan_loaded_modules()) {
1004 ULOG_INFO("loading kernel modules from %s\n", path
);
1006 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
1009 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
1010 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
1015 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
1019 while (getline(&mod
, &mod_len
, fp
) > 0) {
1020 char *nl
= strchr(mod
, '\n');
1027 opts
= strchr(mod
, ' ');
1031 m
= find_module(get_module_name(mod
));
1032 if (!m
|| m
->state
== LOADED
|| m
->state
== BLACKLISTED
)
1037 char *prev
= m
->opts
;
1039 fail
= asprintf(&m
->opts
, "%s %s", prev
, opts
);
1042 ULOG_ERR("out of memory for opts %s\n", opts
);
1049 m
->opts
= strdup(opts
);
1053 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
1054 load_modprobe(false);
1061 fail
= load_modprobe(true);
1064 ULOG_ERR("%d module%s could not be probed\n",
1065 fail
, (fail
== 1) ? ("") : ("s"));
1067 avl_for_each_element(&modules
, mn
, avl
) {
1071 if ((m
->state
== PROBE
) || (m
->error
))
1072 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
1075 ULOG_INFO("done loading kernel modules from %s\n", path
);
1086 static inline char weight(char c
)
1088 return c
== '_' ? '-' : c
;
1091 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
1093 const char *s1
= k1
;
1094 const char *s2
= k2
;
1096 while (*s1
&& (weight(*s1
) == weight(*s2
)))
1102 return (unsigned char)weight(*s1
) - (unsigned char)weight(*s2
);
1108 static char buf
[512];
1112 f
= fopen("/etc/modules.conf", "r");
1116 while ((s
= fgets(buf
, sizeof(buf
), f
)) != NULL
) {
1117 char *c
, *cmd
, *mod
;
1130 while (c
> s
&& isspace(c
[-1])) {
1135 cmd
= strsep(&s
, " \t");
1142 mod
= strsep(&s
, " \t");
1146 if (!strcmp(cmd
, "blacklist")) {
1147 kvlist_set(&options
, mod
, "\x01");
1151 if (!strcmp(cmd
, "options")) {
1152 char *prev
= kvlist_get(&options
, mod
);
1161 if (prev
&& prev
[0] == '\x01')
1165 kvlist_set(&options
, mod
, s
);
1169 if (asprintf(&val
, "%s %s", prev
, s
) < 0)
1172 kvlist_set(&options
, mod
, val
);
1181 int main(int argc
, char **argv
)
1183 char *exec
= basename(*argv
);
1185 avl_init(&modules
, avl_modcmp
, false, NULL
);
1186 if (!strcmp(exec
, "insmod"))
1187 return main_insmod(argc
, argv
);
1189 if (!strcmp(exec
, "rmmod"))
1190 return main_rmmod(argc
, argv
);
1192 if (!strcmp(exec
, "lsmod"))
1193 return main_lsmod(argc
, argv
);
1195 if (!strcmp(exec
, "modinfo"))
1196 return main_modinfo(argc
, argv
);
1200 if (!strcmp(exec
, "modprobe"))
1201 return main_modprobe(argc
, argv
);
1203 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
1204 return main_loader(argc
, argv
);