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 /* duplicated from in-kernel include/linux/module.h */
44 #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
50 struct list_head list
;
69 int refcnt
; /* number of references from module_node.m */
78 static struct avl_tree modules
;
79 static KVLIST(options
, kvlist_strlen
);
81 static char **module_folders
= NULL
;
83 static void free_module(struct module
*m
);
85 static int init_module_folders(void)
90 char *s
, *e
, *p
, path
[330], ldpath
[256];
93 s
= getenv("LD_LIBRARY_PATH");
96 e
+= snprintf(ldpath
, sizeof(ldpath
), "%s:", s
);
98 e
+= snprintf(e
, sizeof(ldpath
) - (e
- ldpath
), "/lib");
102 for (s
= p
= ldpath
; p
<= e
; p
++) {
103 if (*p
!= ':' && *p
!= '\0')
107 snprintf(path
, sizeof(path
), "%s" DEF_MOD_PATH
, s
, ver
.release
);
109 if (!stat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
110 module_folders
= realloc(module_folders
, sizeof(p
) * (n
+ 2));
112 if (!module_folders
) {
113 ULOG_ERR("out of memory\n");
117 module_folders
[n
++] = strdup(path
);
123 if (!module_folders
) {
124 ULOG_ERR("no module folders for kernel version %s found\n", ver
.release
);
128 module_folders
[n
] = NULL
;
132 static struct module
*find_module(const char *name
)
134 struct module_node
*mn
;
135 mn
= avl_find_element(&modules
, name
, mn
, avl
);
142 static void free_modules(void)
144 struct module_node
*mn
, *tmp
;
146 avl_remove_all_elements(&modules
, mn
, avl
, tmp
) {
147 struct module
*m
= mn
->m
;
156 static char* get_module_path(char *name
)
159 static char path
[256];
162 if (!stat(name
, &s
) && S_ISREG(s
.st_mode
))
165 for (p
= module_folders
; *p
; p
++) {
166 snprintf(path
, sizeof(path
), "%s%s.ko", *p
, name
);
167 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
174 static char* get_module_name(char *path
)
176 static char name
[MODULE_NAME_LEN
];
179 strncpy(name
, basename(path
), sizeof(name
) - 1);
181 t
= strstr(name
, ".ko");
188 static int elf64_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
190 const char *secnames
;
195 e
= (Elf64_Ehdr
*) map
;
196 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
198 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
199 for (i
= 0; i
< e
->e_shnum
; i
++) {
200 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
201 *size
= sh
[i
].sh_size
;
202 *offset
= sh
[i
].sh_offset
;
210 static int elf32_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
212 const char *secnames
;
217 e
= (Elf32_Ehdr
*) map
;
218 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
220 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
221 for (i
= 0; i
< e
->e_shnum
; i
++) {
222 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
223 *size
= sh
[i
].sh_size
;
224 *offset
= sh
[i
].sh_offset
;
232 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
234 int clazz
= map
[EI_CLASS
];
235 int endian
= map
[EI_DATA
];
237 #if __BYTE_ORDER == __LITTLE_ENDIAN
238 if (endian
!= ELFDATA2LSB
)
239 #elif __BYTE_ORDER == __BIG_ENDIAN
240 if (endian
!= ELFDATA2MSB
)
242 #error "unsupported endian"
245 ULOG_ERR("invalid endianess: %d\n", endian
);
249 if (clazz
== ELFCLASS32
)
250 return elf32_find_section(map
, section
, offset
, size
);
251 else if (clazz
== ELFCLASS64
)
252 return elf64_find_section(map
, section
, offset
, size
);
254 ULOG_ERR("unknown elf format %d\n", clazz
);
259 static struct module_node
*
260 alloc_module_node(const char *name
, struct module
*m
, bool is_alias
)
262 struct module_node
*mn
;
265 mn
= calloc_a(sizeof(*mn
),
266 &_name
, strlen(name
) + 1);
268 mn
->avl
.key
= strcpy(_name
, name
);
270 mn
->is_alias
= is_alias
;
271 avl_insert(&modules
, &mn
->avl
);
277 static struct module
*
278 alloc_module(const char *name
, const char * const *aliases
, int naliases
, const char *depends
, int size
)
284 m
= calloc_a(sizeof(*m
),
285 &_name
, strlen(name
) + 1,
286 &_dep
, depends
? strlen(depends
) + 2 : 0);
290 m
->name
= strcpy(_name
, name
);
294 m
->depends
= strcpy(_dep
, depends
);
304 alloc_module_node(m
->name
, m
, false);
305 for (i
= 0; i
< naliases
; i
++)
306 alloc_module_node(aliases
[i
], m
, true);
311 static void free_module(struct module
*m
)
318 static int scan_loaded_modules(void)
325 fp
= fopen("/proc/modules", "r");
327 ULOG_ERR("failed to open /proc/modules\n");
331 while (getline(&buf
, &buf_len
, fp
) > 0) {
335 m
.name
= strtok(buf
, " ");
336 m
.size
= atoi(strtok(NULL
, " "));
337 m
.usage
= atoi(strtok(NULL
, " "));
338 m
.depends
= strtok(NULL
, " ");
340 if (!m
.name
|| !m
.depends
)
343 n
= find_module(m
.name
);
345 /* possibly a module outside /lib/modules/<ver>/ */
346 n
= alloc_module(m
.name
, NULL
, 0, m
.depends
, m
.size
);
349 ULOG_ERR("Failed to allocate memory for module\n");
364 static struct module
* get_module_info(const char *module
, const char *name
)
366 int fd
= open(module
, O_RDONLY
);
367 unsigned int offset
, size
;
368 char *map
= MAP_FAILED
, *strings
, *dep
= NULL
;
369 const char **aliases
= NULL
;
370 const char **aliasesr
;
372 struct module
*m
= NULL
;
376 ULOG_ERR("failed to open %s\n", module
);
380 if (fstat(fd
, &s
) == -1) {
381 ULOG_ERR("failed to stat %s\n", module
);
385 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
386 if (map
== MAP_FAILED
) {
387 ULOG_ERR("failed to mmap %s\n", module
);
391 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
392 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
396 strings
= map
+ offset
;
403 if (strings
>= map
+ offset
+ size
)
405 sep
= strstr(strings
, "=");
410 if (!strncmp(strings
, "depends=", len
+ 1))
412 else if (!strncmp(strings
, "alias=", len
+ 1)) {
413 aliasesr
= realloc(aliases
, sizeof(sep
) * (naliases
+ 1));
415 ULOG_ERR("out of memory\n");
420 aliases
[naliases
++] = sep
;
422 strings
= &sep
[strlen(sep
)];
425 m
= alloc_module(name
, aliases
, naliases
, dep
, s
.st_size
);
431 if (map
!= MAP_FAILED
)
432 munmap(map
, s
.st_size
);
442 static int scan_module_folder(const char *dir
)
444 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
449 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
450 sprintf(path
, "%s*.ko", dir
);
452 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
455 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
456 char *name
= get_module_name(gl
.gl_pathv
[j
]);
463 m
= find_module(name
);
467 m
= get_module_info(gl
.gl_pathv
[j
], name
);
473 opts
= kvlist_get(&options
, name
);
478 m
->state
= BLACKLISTED
;
480 m
->opts
= strdup(opts
);
488 static int scan_module_folders(void)
493 if (init_module_folders())
496 for (p
= module_folders
; *p
; p
++)
497 rv
|= scan_module_folder(*p
);
502 static int print_modinfo(char *module
)
504 int fd
= open(module
, O_RDONLY
);
505 unsigned int offset
, size
;
508 char *map
= MAP_FAILED
, *strings
;
514 ULOG_ERR("failed to open %s\n", module
);
518 if (fstat(fd
, &s
) == -1) {
519 ULOG_ERR("failed to stat %s\n", module
);
523 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
524 if (map
== MAP_FAILED
) {
525 ULOG_ERR("failed to mmap %s\n", module
);
529 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
530 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
534 strings
= map
+ offset
;
535 printf("module:\t\t%s\n", module
);
543 if (strings
>= map
+ offset
+ size
)
545 sep
= strstr(strings
, "=");
548 dup
= strndup(strings
, sep
- strings
);
550 if (strncmp(strings
, "parm", 4)) {
552 printf("%s:\t\t%s\n", dup
, sep
);
554 printf("%s:\t%s\n", dup
, sep
);
556 sep2
= strstr(sep
, ":");
559 pname
= strndup(sep
, sep2
- sep
);
561 pdata
= strdup(sep2
);
563 list_for_each_entry(p
, ¶ms
, list
)
564 if (!strcmp(pname
, p
->name
))
567 if (list_entry_is_h(p
, ¶ms
, list
)) {
568 p
= alloca(sizeof(*p
));
570 p
->desc
= p
->type
= NULL
;
571 list_add(&p
->list
, ¶ms
);
576 if (!strcmp(dup
, "parmtype"))
581 strings
= &sep
[strlen(sep
)];
586 list_for_each_entry(p
, ¶ms
, list
) {
587 printf("parm:\t\t%s", p
->name
);
589 printf(":%s", p
->desc
);
591 printf(" (%s)", p
->type
);
601 if (map
!= MAP_FAILED
)
602 munmap(map
, s
.st_size
);
610 static int deps_available(struct module
*m
, int verbose
)
615 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
621 m
= find_module(dep
);
624 ULOG_ERR("missing dependency %s\n", dep
);
625 if (verbose
&& m
&& (m
->state
!= LOADED
))
626 ULOG_ERR("dependency not loaded %s\n", dep
);
627 if (!m
|| (m
->state
!= LOADED
))
629 dep
+= strlen(dep
) + 1;
635 static int insert_module(char *path
, const char *options
)
642 ULOG_ERR("Path not specified\n");
646 if (stat(path
, &s
)) {
647 ULOG_ERR("missing module %s\n", path
);
651 fd
= open(path
, O_RDONLY
);
653 ULOG_ERR("cannot open %s\n", path
);
657 data
= malloc(s
.st_size
);
659 ULOG_ERR("out of memory\n");
663 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
664 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
669 ULOG_ERR("failed to read full module %s\n", path
);
678 static void load_moddeps(struct module
*_m
)
683 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
689 m
= find_module(dep
);
692 ULOG_ERR("failed to find dependency %s\n", dep
);
693 if (m
&& (m
->state
!= LOADED
)) {
698 dep
= dep
+ strlen(dep
) + 1;
702 static int load_modprobe(bool allow_load_retry
)
704 int loaded
, skipped
, failed
;
705 struct module_node
*mn
;
707 bool load_retry
= false;
708 static bool first_iteration
= true;
710 avl_for_each_element(&modules
, mn
, avl
) {
714 if (m
->state
== PROBE
)
722 avl_for_each_element(&modules
, mn
, avl
) {
726 if ((m
->state
== PROBE
) && (!deps_available(m
, 0)) && (!m
->error
|| load_retry
)) {
727 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
739 else if (m
->state
== PROBE
)
743 if (allow_load_retry
) {
744 /* if we can't load anything else let's try to load failed modules */
745 load_retry
= loaded
? (failed
&& !skipped
) : (failed
&& !load_retry
&& !first_iteration
);
748 first_iteration
= false;
749 } while (loaded
|| load_retry
);
751 return skipped
+ failed
;
754 static int print_insmod_usage(void)
756 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
761 static int print_modprobe_usage(void)
765 "\tmodprobe [-q] [-v] filename\n"
766 "\tmodprobe -a [-q] [-v] filename [filename...]\n"
772 static int print_usage(char *arg
)
774 ULOG_INFO("Usage:\n\t%s module\n", arg
);
779 static int main_insmod(int argc
, char **argv
)
781 char *name
, *cur
, *options
;
785 return print_insmod_usage();
787 name
= get_module_name(argv
[1]);
789 ULOG_ERR("cannot find module - %s\n", argv
[1]);
793 if (scan_loaded_modules())
796 if (find_module(name
)) {
797 ULOG_ERR("module is already loaded - %s\n", name
);
804 for (len
= 0, i
= 2; i
< argc
; i
++)
805 len
+= strlen(argv
[i
]) + 1;
807 options
= malloc(len
);
809 ULOG_ERR("out of memory\n");
816 for (i
= 2; i
< argc
; i
++) {
821 cur
+= sprintf(cur
, "%s", argv
[i
]);
824 if (init_module_folders()) {
825 fprintf(stderr
, "Failed to find the folder holding the modules\n");
830 if (get_module_path(argv
[1])) {
832 } else if (!get_module_path(name
)) {
833 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
838 ret
= insert_module(get_module_path(name
), options
);
840 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
847 static int main_rmmod(int argc
, char **argv
)
854 return print_usage("rmmod");
856 if (scan_loaded_modules())
859 name
= get_module_name(argv
[1]);
860 m
= find_module(name
);
862 ULOG_ERR("module is not loaded\n");
865 ret
= syscall(__NR_delete_module
, m
->name
, 0);
868 ULOG_ERR("unloading the module failed\n");
875 static int main_lsmod(int argc
, char **argv
)
877 struct module_node
*mn
;
881 if (scan_loaded_modules())
884 avl_for_each_element(&modules
, mn
, avl
) {
888 if (m
->state
== LOADED
) {
889 printf("%-20s%8d%3d ",
890 m
->name
, m
->size
, m
->usage
);
891 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
895 dep
= dep
+ strlen(dep
) + 1;
909 static int main_modinfo(int argc
, char **argv
)
915 return print_usage("modinfo");
917 if (scan_module_folders())
920 name
= get_module_name(argv
[1]);
921 m
= find_module(name
);
923 ULOG_ERR("cannot find module - %s\n", argv
[1]);
927 name
= get_module_path(m
->name
);
929 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
938 static int main_modprobe(int argc
, char **argv
)
940 struct module_node
*mn
;
944 int log_level
= LOG_WARNING
;
947 bool use_all
= false;
949 while ((opt
= getopt(argc
, argv
, "aqv")) != -1 ) {
954 case 'q': /* shhhh! */
958 log_level
= LOG_DEBUG
;
961 return print_modprobe_usage();
967 return print_modprobe_usage(); /* expected module after options */
969 /* after print_modprobe_usage() so it won't be filtered out */
970 ulog_threshold(log_level
);
972 if (scan_module_folders())
975 if (scan_loaded_modules())
981 name
= get_module_name(argv
[optind
]);
982 m
= find_module(name
);
984 if (m
&& m
->state
== BLACKLISTED
) {
986 ULOG_INFO("%s is blacklisted\n", name
);
987 } else if (m
&& m
->state
== LOADED
) {
989 ULOG_INFO("%s is already loaded\n", name
);
992 ULOG_ERR("failed to find a module named %s\n", name
);
999 } while (use_all
&& optind
< argc
);
1001 load_fail
= load_modprobe(true);
1003 ULOG_ERR("%d module%s could not be probed\n",
1004 load_fail
, (load_fail
== 1) ? ("") : ("s"));
1006 avl_for_each_element(&modules
, mn
, avl
) {
1010 if ((m
->state
== PROBE
) || m
->error
)
1011 ULOG_ERR("- %s\n", m
->name
);
1022 static int main_loader(int argc
, char **argv
)
1024 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
1025 char *dir
= "/etc/modules.d/";
1026 struct module_node
*mn
;
1030 int ret
= 0, fail
, j
;
1035 path
= malloc(strlen(dir
) + 2);
1037 ULOG_ERR("out of memory\n");
1044 if (scan_module_folders()) {
1049 if (scan_loaded_modules()) {
1054 ULOG_INFO("loading kernel modules from %s\n", path
);
1056 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
1059 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
1060 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
1065 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
1069 while (getline(&mod
, &mod_len
, fp
) > 0) {
1070 char *nl
= strchr(mod
, '\n');
1077 opts
= strchr(mod
, ' ');
1081 m
= find_module(get_module_name(mod
));
1082 if (!m
|| m
->state
== LOADED
|| m
->state
== BLACKLISTED
)
1087 char *prev
= m
->opts
;
1089 fail
= asprintf(&m
->opts
, "%s %s", prev
, opts
);
1092 ULOG_ERR("out of memory for opts %s\n", opts
);
1099 m
->opts
= strdup(opts
);
1103 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
1104 load_modprobe(false);
1111 fail
= load_modprobe(true);
1114 ULOG_ERR("%d module%s could not be probed\n",
1115 fail
, (fail
== 1) ? ("") : ("s"));
1117 avl_for_each_element(&modules
, mn
, avl
) {
1121 if ((m
->state
== PROBE
) || (m
->error
))
1122 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
1125 ULOG_INFO("done loading kernel modules from %s\n", path
);
1136 static inline char weight(char c
)
1138 return c
== '_' ? '-' : c
;
1141 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
1143 const char *s1
= k1
;
1144 const char *s2
= k2
;
1146 while (*s1
&& (weight(*s1
) == weight(*s2
)))
1152 return (unsigned char)weight(*s1
) - (unsigned char)weight(*s2
);
1158 static char buf
[512];
1162 f
= fopen("/etc/modules.conf", "r");
1166 while ((s
= fgets(buf
, sizeof(buf
), f
)) != NULL
) {
1167 char *c
, *cmd
, *mod
;
1180 while (c
> s
&& isspace(c
[-1])) {
1185 cmd
= strsep(&s
, " \t");
1192 mod
= strsep(&s
, " \t");
1196 if (!strcmp(cmd
, "blacklist")) {
1197 kvlist_set(&options
, mod
, "\x01");
1201 if (!strcmp(cmd
, "options")) {
1202 char *prev
= kvlist_get(&options
, mod
);
1211 if (prev
&& prev
[0] == '\x01')
1215 kvlist_set(&options
, mod
, s
);
1219 if (asprintf(&val
, "%s %s", prev
, s
) < 0)
1222 kvlist_set(&options
, mod
, val
);
1231 int main(int argc
, char **argv
)
1233 char *exec
= basename(*argv
);
1235 avl_init(&modules
, avl_modcmp
, false, NULL
);
1236 if (!strcmp(exec
, "insmod"))
1237 return main_insmod(argc
, argv
);
1239 if (!strcmp(exec
, "rmmod"))
1240 return main_rmmod(argc
, argv
);
1242 if (!strcmp(exec
, "lsmod"))
1243 return main_lsmod(argc
, argv
);
1245 if (!strcmp(exec
, "modinfo"))
1246 return main_modinfo(argc
, argv
);
1250 if (!strcmp(exec
, "modprobe"))
1251 return main_modprobe(argc
, argv
);
1253 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
1254 return main_loader(argc
, argv
);