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>
34 #include <libubox/avl.h>
35 #include <libubox/avl-cmp.h>
36 #include <libubox/utils.h>
37 #include <libubox/ulog.h>
39 #define DEF_MOD_PATH "/modules/%s/"
56 int refcnt
; /* number of references from module_node.m */
65 static struct avl_tree modules
;
67 static char **module_folders
= NULL
;
69 static void free_module(struct module
*m
);
71 static int init_module_folders(void)
76 char *s
, *e
, *p
, path
[256], ldpath
[256];
79 s
= getenv("LD_LIBRARY_PATH");
82 e
+= snprintf(ldpath
, sizeof(ldpath
), "%s:", s
);
84 e
+= snprintf(e
, sizeof(ldpath
) - (e
- ldpath
), "/lib");
88 for (s
= p
= ldpath
; p
<= e
; p
++) {
89 if (*p
!= ':' && *p
!= '\0')
93 snprintf(path
, sizeof(path
), "%s" DEF_MOD_PATH
, s
, ver
.release
);
95 if (!stat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
96 module_folders
= realloc(module_folders
, sizeof(p
) * (n
+ 2));
98 if (!module_folders
) {
99 ULOG_ERR("out of memory\n");
103 module_folders
[n
++] = strdup(path
);
112 module_folders
[n
] = NULL
;
116 static struct module
*find_module(const char *name
)
118 struct module_node
*mn
;
119 mn
= avl_find_element(&modules
, name
, mn
, avl
);
126 static void free_modules(void)
128 struct module_node
*mn
, *tmp
;
130 avl_remove_all_elements(&modules
, mn
, avl
, tmp
) {
131 struct module
*m
= mn
->m
;
140 static char* get_module_path(char *name
)
143 static char path
[256];
146 if (!stat(name
, &s
) && S_ISREG(s
.st_mode
))
149 for (p
= module_folders
; *p
; p
++) {
150 snprintf(path
, sizeof(path
), "%s%s.ko", *p
, name
);
151 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
158 static char* get_module_name(char *path
)
160 static char name
[33];
163 strncpy(name
, basename(path
), sizeof(name
) - 1);
165 t
= strstr(name
, ".ko");
172 static int elf64_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
174 const char *secnames
;
179 e
= (Elf64_Ehdr
*) map
;
180 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
182 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
183 for (i
= 0; i
< e
->e_shnum
; i
++) {
184 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
185 *size
= sh
[i
].sh_size
;
186 *offset
= sh
[i
].sh_offset
;
194 static int elf32_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
196 const char *secnames
;
201 e
= (Elf32_Ehdr
*) map
;
202 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
204 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
205 for (i
= 0; i
< e
->e_shnum
; i
++) {
206 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
207 *size
= sh
[i
].sh_size
;
208 *offset
= sh
[i
].sh_offset
;
216 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
218 int clazz
= map
[EI_CLASS
];
219 int endian
= map
[EI_DATA
];
221 #if __BYTE_ORDER == __LITTLE_ENDIAN
222 if (endian
!= ELFDATA2LSB
)
223 #elif __BYTE_ORDER == __BIG_ENDIAN
224 if (endian
!= ELFDATA2MSB
)
226 #error "unsupported endian"
229 ULOG_ERR("invalid endianess: %d\n", endian
);
233 if (clazz
== ELFCLASS32
)
234 return elf32_find_section(map
, section
, offset
, size
);
235 else if (clazz
== ELFCLASS64
)
236 return elf64_find_section(map
, section
, offset
, size
);
238 ULOG_ERR("unknown elf format %d\n", clazz
);
243 static struct module_node
*
244 alloc_module_node(const char *name
, struct module
*m
, bool is_alias
)
246 struct module_node
*mn
;
249 mn
= calloc_a(sizeof(*mn
),
250 &_name
, strlen(name
) + 1);
252 mn
->avl
.key
= strcpy(_name
, name
);
254 mn
->is_alias
= is_alias
;
255 avl_insert(&modules
, &mn
->avl
);
261 static struct module
*
262 alloc_module(const char *name
, const char * const *aliases
, int naliases
, const char *depends
, int size
)
268 m
= calloc_a(sizeof(*m
),
269 &_name
, strlen(name
) + 1,
270 &_dep
, depends
? strlen(depends
) + 2 : 0);
274 m
->name
= strcpy(_name
, name
);
278 m
->depends
= strcpy(_dep
, depends
);
288 alloc_module_node(m
->name
, m
, false);
289 for (i
= 0; i
< naliases
; i
++)
290 alloc_module_node(aliases
[i
], m
, true);
295 static void free_module(struct module
*m
)
302 static int scan_loaded_modules(void)
308 fp
= fopen("/proc/modules", "r");
310 ULOG_ERR("failed to open /proc/modules\n");
314 while (getline(&buf
, &buf_len
, fp
) > 0) {
318 m
.name
= strtok(buf
, " ");
319 m
.size
= atoi(strtok(NULL
, " "));
320 m
.usage
= atoi(strtok(NULL
, " "));
321 m
.depends
= strtok(NULL
, " ");
323 if (!m
.name
|| !m
.depends
)
326 n
= find_module(m
.name
);
328 /* possibly a module outside /lib/modules/<ver>/ */
329 n
= alloc_module(m
.name
, NULL
, 0, m
.depends
, m
.size
);
340 static struct module
* get_module_info(const char *module
, const char *name
)
342 int fd
= open(module
, O_RDONLY
);
343 unsigned int offset
, size
;
344 char *map
= MAP_FAILED
, *strings
, *dep
= NULL
;
345 const char **aliases
= NULL
;
347 struct module
*m
= NULL
;
351 ULOG_ERR("failed to open %s\n", module
);
355 if (fstat(fd
, &s
) == -1) {
356 ULOG_ERR("failed to stat %s\n", module
);
360 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
361 if (map
== MAP_FAILED
) {
362 ULOG_ERR("failed to mmap %s\n", module
);
366 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
367 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
371 strings
= map
+ offset
;
378 if (strings
>= map
+ offset
+ size
)
380 sep
= strstr(strings
, "=");
385 if (!strncmp(strings
, "depends=", len
+ 1))
387 else if (!strncmp(strings
, "alias=", len
+ 1)) {
388 aliases
= realloc(aliases
, sizeof(sep
) * (naliases
+ 1));
390 ULOG_ERR("out of memory\n");
394 aliases
[naliases
++] = sep
;
396 strings
= &sep
[strlen(sep
)];
399 m
= alloc_module(name
, aliases
, naliases
, dep
, s
.st_size
);
405 if (map
!= MAP_FAILED
)
406 munmap(map
, s
.st_size
);
416 static int scan_module_folder(const char *dir
)
418 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
425 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
426 sprintf(path
, "%s*.ko", dir
);
428 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
431 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
432 char *name
= get_module_name(gl
.gl_pathv
[j
]);
438 m
= find_module(name
);
440 if (!get_module_info(gl
.gl_pathv
[j
], name
))
450 static int scan_module_folders(void)
455 if (init_module_folders())
458 for (p
= module_folders
; *p
; p
++)
459 rv
|= scan_module_folder(*p
);
464 static int print_modinfo(char *module
)
466 int fd
= open(module
, O_RDONLY
);
467 unsigned int offset
, size
;
469 char *map
= MAP_FAILED
, *strings
;
473 ULOG_ERR("failed to open %s\n", module
);
477 if (fstat(fd
, &s
) == -1) {
478 ULOG_ERR("failed to stat %s\n", module
);
482 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
483 if (map
== MAP_FAILED
) {
484 ULOG_ERR("failed to mmap %s\n", module
);
488 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
489 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
493 strings
= map
+ offset
;
494 printf("module:\t\t%s\n", module
);
501 if (strings
>= map
+ offset
+ size
)
503 sep
= strstr(strings
, "=");
506 dup
= strndup(strings
, sep
- strings
);
508 if (strncmp(strings
, "parm", 4)) {
510 printf("%s:\t\t%s\n", dup
, sep
);
512 printf("%s:\t%s\n", dup
, sep
);
514 strings
= &sep
[strlen(sep
)];
522 if (map
!= MAP_FAILED
)
523 munmap(map
, s
.st_size
);
531 static int deps_available(struct module
*m
, int verbose
)
536 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
542 m
= find_module(dep
);
545 ULOG_ERR("missing dependency %s\n", dep
);
546 if (verbose
&& m
&& (m
->state
!= LOADED
))
547 ULOG_ERR("dependency not loaded %s\n", dep
);
548 if (!m
|| (m
->state
!= LOADED
))
550 dep
+= strlen(dep
) + 1;
556 static int insert_module(char *path
, const char *options
)
562 if (stat(path
, &s
)) {
563 ULOG_ERR("missing module %s\n", path
);
567 fd
= open(path
, O_RDONLY
);
569 ULOG_ERR("cannot open %s\n", path
);
573 data
= malloc(s
.st_size
);
575 ULOG_ERR("out of memory\n");
579 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
580 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
585 ULOG_ERR("failed to read full module %s\n", path
);
594 static void load_moddeps(struct module
*_m
)
599 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
605 m
= find_module(dep
);
608 ULOG_ERR("failed to find dependency %s\n", dep
);
609 if (m
&& (m
->state
!= LOADED
)) {
614 dep
= dep
+ strlen(dep
) + 1;
618 static int iterations
= 0;
619 static int load_modprobe(void)
622 struct module_node
*mn
;
625 avl_for_each_element(&modules
, mn
, avl
) {
629 if (m
->state
== PROBE
)
636 avl_for_each_element(&modules
, mn
, avl
) {
640 if ((m
->state
== PROBE
) && (!deps_available(m
, 0))) {
641 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
650 if ((m
->state
== PROBE
) || m
->error
)
659 static int print_insmod_usage(void)
661 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
666 static int print_modprobe_usage(void)
668 ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
673 static int print_usage(char *arg
)
675 ULOG_INFO("Usage:\n\t%s module\n", arg
);
680 static int main_insmod(int argc
, char **argv
)
682 char *name
, *cur
, *options
;
686 return print_insmod_usage();
688 name
= get_module_name(argv
[1]);
690 ULOG_ERR("cannot find module - %s\n", argv
[1]);
694 if (scan_loaded_modules())
697 if (find_module(name
)) {
698 ULOG_ERR("module is already loaded - %s\n", name
);
705 for (len
= 0, i
= 2; i
< argc
; i
++)
706 len
+= strlen(argv
[i
]) + 1;
708 options
= malloc(len
);
710 ULOG_ERR("out of memory\n");
717 for (i
= 2; i
< argc
; i
++) {
722 cur
+= sprintf(cur
, "%s", argv
[i
]);
725 if (init_module_folders()) {
726 fprintf(stderr
, "Failed to find the folder holding the modules\n");
731 if (get_module_path(argv
[1])) {
733 } else if (!get_module_path(name
)) {
734 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
739 ret
= insert_module(get_module_path(name
), options
);
741 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
748 static int main_rmmod(int argc
, char **argv
)
755 return print_usage("rmmod");
757 if (scan_loaded_modules())
760 name
= get_module_name(argv
[1]);
761 m
= find_module(name
);
763 ULOG_ERR("module is not loaded\n");
766 ret
= syscall(__NR_delete_module
, m
->name
, 0);
769 ULOG_ERR("unloading the module failed\n");
776 static int main_lsmod(int argc
, char **argv
)
778 struct module_node
*mn
;
782 if (scan_loaded_modules())
785 avl_for_each_element(&modules
, mn
, avl
) {
789 if (m
->state
== LOADED
) {
790 printf("%-20s%8d%3d ",
791 m
->name
, m
->size
, m
->usage
);
792 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
796 dep
= dep
+ strlen(dep
) + 1;
810 static int main_modinfo(int argc
, char **argv
)
816 return print_usage("modinfo");
818 if (scan_module_folders())
821 name
= get_module_name(argv
[1]);
822 m
= find_module(name
);
824 ULOG_ERR("cannot find module - %s\n", argv
[1]);
828 name
= get_module_path(m
->name
);
830 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
839 static int main_modprobe(int argc
, char **argv
)
841 struct module_node
*mn
;
848 while ((opt
= getopt(argc
, argv
, "q")) != -1 ) {
850 case 'q': /* shhhh! */
854 return print_modprobe_usage();
860 return print_modprobe_usage(); /* expected module after options */
864 if (scan_module_folders())
867 if (scan_loaded_modules())
870 name
= get_module_name(mod
);
871 m
= find_module(name
);
872 if (m
&& m
->state
== LOADED
) {
874 ULOG_ERR("%s is already loaded\n", name
);
878 ULOG_ERR("failed to find a module named %s\n", name
);
885 fail
= load_modprobe();
888 ULOG_ERR("%d module%s could not be probed\n",
889 fail
, (fail
== 1) ? ("") : ("s"));
891 avl_for_each_element(&modules
, mn
, avl
) {
895 if ((m
->state
== PROBE
) || m
->error
)
896 ULOG_ERR("- %s\n", m
->name
);
906 static int main_loader(int argc
, char **argv
)
908 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
909 char *dir
= "/etc/modules.d/";
910 struct module_node
*mn
;
919 path
= malloc(strlen(dir
) + 2);
921 ULOG_ERR("out of memory\n");
928 if (scan_module_folders()) {
933 if (scan_loaded_modules()) {
938 ULOG_INFO("loading kernel modules from %s\n", path
);
940 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
943 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
944 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
949 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
953 while (getline(&mod
, &mod_len
, fp
) > 0) {
954 char *nl
= strchr(mod
, '\n');
961 opts
= strchr(mod
, ' ');
965 m
= find_module(get_module_name(mod
));
966 if (!m
|| (m
->state
== LOADED
))
970 m
->opts
= strdup(opts
);
972 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
980 fail
= load_modprobe();
983 ULOG_ERR("%d module%s could not be probed\n",
984 fail
, (fail
== 1) ? ("") : ("s"));
986 avl_for_each_element(&modules
, mn
, avl
) {
990 if ((m
->state
== PROBE
) || (m
->error
))
991 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
994 ULOG_INFO("done loading kernel modules from %s\n", path
);
1004 static inline char weight(char c
)
1006 return c
== '_' ? '-' : c
;
1009 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
1011 const char *s1
= k1
;
1012 const char *s2
= k2
;
1014 while (*s1
&& (weight(*s1
) == weight(*s2
)))
1020 return (unsigned char)weight(*s1
) - (unsigned char)weight(*s2
);
1023 int main(int argc
, char **argv
)
1025 char *exec
= basename(*argv
);
1027 avl_init(&modules
, avl_modcmp
, false, NULL
);
1028 if (!strcmp(exec
, "insmod"))
1029 return main_insmod(argc
, argv
);
1031 if (!strcmp(exec
, "rmmod"))
1032 return main_rmmod(argc
, argv
);
1034 if (!strcmp(exec
, "lsmod"))
1035 return main_lsmod(argc
, argv
);
1037 if (!strcmp(exec
, "modinfo"))
1038 return main_modinfo(argc
, argv
);
1040 if (!strcmp(exec
, "modprobe"))
1041 return main_modprobe(argc
, argv
);
1043 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
1044 return main_loader(argc
, argv
);