1a63c9810a99e30009ef9a85c39dba6a65fb3264
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));
101 module_folders
[n
++] = strdup(path
);
110 module_folders
[n
] = NULL
;
114 static struct module
*find_module(const char *name
)
116 struct module_node
*mn
;
117 mn
= avl_find_element(&modules
, name
, mn
, avl
);
124 static void free_modules(void)
126 struct module_node
*mn
, *tmp
;
128 avl_remove_all_elements(&modules
, mn
, avl
, tmp
) {
129 struct module
*m
= mn
->m
;
138 static char* get_module_path(char *name
)
141 static char path
[256];
144 if (!stat(name
, &s
) && S_ISREG(s
.st_mode
))
147 for (p
= module_folders
; *p
; p
++) {
148 snprintf(path
, sizeof(path
), "%s%s.ko", *p
, name
);
149 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
156 static char* get_module_name(char *path
)
158 static char name
[33];
161 strncpy(name
, basename(path
), sizeof(name
) - 1);
163 t
= strstr(name
, ".ko");
170 static int elf64_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
172 const char *secnames
;
177 e
= (Elf64_Ehdr
*) map
;
178 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
180 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
181 for (i
= 0; i
< e
->e_shnum
; i
++) {
182 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
183 *size
= sh
[i
].sh_size
;
184 *offset
= sh
[i
].sh_offset
;
192 static int elf32_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
194 const char *secnames
;
199 e
= (Elf32_Ehdr
*) map
;
200 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
202 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
203 for (i
= 0; i
< e
->e_shnum
; i
++) {
204 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
205 *size
= sh
[i
].sh_size
;
206 *offset
= sh
[i
].sh_offset
;
214 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
216 int clazz
= map
[EI_CLASS
];
218 if (clazz
== ELFCLASS32
)
219 return elf32_find_section(map
, section
, offset
, size
);
220 else if (clazz
== ELFCLASS64
)
221 return elf64_find_section(map
, section
, offset
, size
);
223 ULOG_ERR("unknown elf format %d\n", clazz
);
228 static struct module_node
*
229 alloc_module_node(const char *name
, struct module
*m
, bool is_alias
)
231 struct module_node
*mn
;
234 mn
= calloc_a(sizeof(*mn
),
235 &_name
, strlen(name
) + 1);
237 mn
->avl
.key
= strcpy(_name
, name
);
239 mn
->is_alias
= is_alias
;
240 avl_insert(&modules
, &mn
->avl
);
246 static struct module
*
247 alloc_module(const char *name
, const char * const *aliases
, int naliases
, const char *depends
, int size
)
253 m
= calloc_a(sizeof(*m
),
254 &_name
, strlen(name
) + 1,
255 &_dep
, depends
? strlen(depends
) + 2 : 0);
259 m
->name
= strcpy(_name
, name
);
263 m
->depends
= strcpy(_dep
, depends
);
273 alloc_module_node(m
->name
, m
, false);
274 for (i
= 0; i
< naliases
; i
++)
275 alloc_module_node(aliases
[i
], m
, true);
280 static void free_module(struct module
*m
)
287 static int scan_loaded_modules(void)
293 fp
= fopen("/proc/modules", "r");
295 ULOG_ERR("failed to open /proc/modules\n");
299 while (getline(&buf
, &buf_len
, fp
) > 0) {
303 m
.name
= strtok(buf
, " ");
304 m
.size
= atoi(strtok(NULL
, " "));
305 m
.usage
= atoi(strtok(NULL
, " "));
306 m
.depends
= strtok(NULL
, " ");
308 if (!m
.name
|| !m
.depends
)
311 n
= find_module(m
.name
);
313 /* possibly a module outside /lib/modules/<ver>/ */
314 n
= alloc_module(m
.name
, NULL
, 0, m
.depends
, m
.size
);
325 static struct module
* get_module_info(const char *module
, const char *name
)
327 int fd
= open(module
, O_RDONLY
);
328 unsigned int offset
, size
;
329 char *map
= MAP_FAILED
, *strings
, *dep
= NULL
;
330 const char *aliases
[32] = { 0 };
332 struct module
*m
= NULL
;
336 ULOG_ERR("failed to open %s\n", module
);
340 if (fstat(fd
, &s
) == -1) {
341 ULOG_ERR("failed to stat %s\n", module
);
345 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
346 if (map
== MAP_FAILED
) {
347 ULOG_ERR("failed to mmap %s\n", module
);
351 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
352 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
356 strings
= map
+ offset
;
363 if (strings
>= map
+ offset
+ size
)
365 sep
= strstr(strings
, "=");
370 if (!strncmp(strings
, "depends=", len
+ 1))
372 else if (!strncmp(strings
, "alias=", len
+ 1)) {
373 if (naliases
< ARRAY_SIZE(aliases
))
374 aliases
[naliases
++] = sep
;
376 ULOG_WARN("module %s has more than %d aliases: truncated",
377 name
, ARRAY_SIZE(aliases
));
379 strings
= &sep
[strlen(sep
)];
382 m
= alloc_module(name
, aliases
, naliases
, dep
, s
.st_size
);
388 if (map
!= MAP_FAILED
)
389 munmap(map
, s
.st_size
);
397 static int scan_module_folder(const char *dir
)
399 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
406 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
407 sprintf(path
, "%s*.ko", dir
);
409 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
412 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
413 char *name
= get_module_name(gl
.gl_pathv
[j
]);
419 m
= find_module(name
);
421 get_module_info(gl
.gl_pathv
[j
], name
);
429 static int scan_module_folders(void)
434 if (init_module_folders())
437 for (p
= module_folders
; *p
; p
++)
438 rv
|= scan_module_folder(*p
);
443 static int print_modinfo(char *module
)
445 int fd
= open(module
, O_RDONLY
);
446 unsigned int offset
, size
;
448 char *map
= MAP_FAILED
, *strings
;
452 ULOG_ERR("failed to open %s\n", module
);
456 if (fstat(fd
, &s
) == -1) {
457 ULOG_ERR("failed to stat %s\n", module
);
461 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
462 if (map
== MAP_FAILED
) {
463 ULOG_ERR("failed to mmap %s\n", module
);
467 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
468 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
472 strings
= map
+ offset
;
473 printf("module:\t\t%s\n", module
);
480 if (strings
>= map
+ offset
+ size
)
482 sep
= strstr(strings
, "=");
485 dup
= strndup(strings
, sep
- strings
);
487 if (strncmp(strings
, "parm", 4)) {
489 printf("%s:\t\t%s\n", dup
, sep
);
491 printf("%s:\t%s\n", dup
, sep
);
493 strings
= &sep
[strlen(sep
)];
501 if (map
!= MAP_FAILED
)
502 munmap(map
, s
.st_size
);
510 static int deps_available(struct module
*m
, int verbose
)
515 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
521 m
= find_module(dep
);
524 ULOG_ERR("missing dependency %s\n", dep
);
525 if (verbose
&& m
&& (m
->state
!= LOADED
))
526 ULOG_ERR("dependency not loaded %s\n", dep
);
527 if (!m
|| (m
->state
!= LOADED
))
529 dep
+= strlen(dep
) + 1;
535 static int insert_module(char *path
, const char *options
)
541 if (stat(path
, &s
)) {
542 ULOG_ERR("missing module %s\n", path
);
546 fd
= open(path
, O_RDONLY
);
548 ULOG_ERR("cannot open %s\n", path
);
552 data
= malloc(s
.st_size
);
553 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
554 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
559 ULOG_ERR("failed to read full module %s\n", path
);
567 static void load_moddeps(struct module
*_m
)
572 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
578 m
= find_module(dep
);
581 ULOG_ERR("failed to find dependency %s\n", dep
);
582 if (m
&& (m
->state
!= LOADED
)) {
587 dep
= dep
+ strlen(dep
) + 1;
591 static int iterations
= 0;
592 static int load_modprobe(void)
595 struct module_node
*mn
;
598 avl_for_each_element(&modules
, mn
, avl
) {
602 if (m
->state
== PROBE
)
609 avl_for_each_element(&modules
, mn
, avl
) {
613 if ((m
->state
== PROBE
) && (!deps_available(m
, 0))) {
614 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
623 if ((m
->state
== PROBE
) || m
->error
)
632 static int print_insmod_usage(void)
634 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
639 static int print_modprobe_usage(void)
641 ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
646 static int print_usage(char *arg
)
648 ULOG_INFO("Usage:\n\t%s module\n", arg
);
653 static int main_insmod(int argc
, char **argv
)
655 char *name
, *cur
, *options
;
659 return print_insmod_usage();
661 name
= get_module_name(argv
[1]);
663 ULOG_ERR("cannot find module - %s\n", argv
[1]);
667 if (scan_loaded_modules())
670 if (find_module(name
)) {
671 ULOG_ERR("module is already loaded - %s\n", name
);
678 for (len
= 0, i
= 2; i
< argc
; i
++)
679 len
+= strlen(argv
[i
]) + 1;
681 options
= malloc(len
);
684 for (i
= 2; i
< argc
; i
++) {
689 cur
+= sprintf(cur
, "%s", argv
[i
]);
692 if (init_module_folders()) {
693 fprintf(stderr
, "Failed to find the folder holding the modules\n");
698 if (get_module_path(argv
[1])) {
700 } else if (!get_module_path(name
)) {
701 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
706 ret
= insert_module(get_module_path(name
), options
);
708 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
715 static int main_rmmod(int argc
, char **argv
)
722 return print_usage("rmmod");
724 if (scan_loaded_modules())
727 name
= get_module_name(argv
[1]);
728 m
= find_module(name
);
730 ULOG_ERR("module is not loaded\n");
733 ret
= syscall(__NR_delete_module
, m
->name
, 0);
736 ULOG_ERR("unloading the module failed\n");
743 static int main_lsmod(int argc
, char **argv
)
745 struct module_node
*mn
;
749 if (scan_loaded_modules())
752 avl_for_each_element(&modules
, mn
, avl
) {
756 if (m
->state
== LOADED
) {
757 printf("%-20s%8d%3d ",
758 m
->name
, m
->size
, m
->usage
);
759 if (m
->depends
&& strcmp(m
->depends
, "-") && strcmp(m
->depends
, "")) {
763 dep
= dep
+ strlen(dep
) + 1;
777 static int main_modinfo(int argc
, char **argv
)
783 return print_usage("modinfo");
785 if (scan_module_folders())
788 name
= get_module_name(argv
[1]);
789 m
= find_module(name
);
791 ULOG_ERR("cannot find module - %s\n", argv
[1]);
795 name
= get_module_path(m
->name
);
797 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
806 static int main_modprobe(int argc
, char **argv
)
808 struct module_node
*mn
;
815 while ((opt
= getopt(argc
, argv
, "q")) != -1 ) {
817 case 'q': /* shhhh! */
821 return print_modprobe_usage();
827 return print_modprobe_usage(); /* expected module after options */
831 if (scan_module_folders())
834 if (scan_loaded_modules())
837 name
= get_module_name(mod
);
838 m
= find_module(name
);
839 if (m
&& m
->state
== LOADED
) {
841 ULOG_ERR("%s is already loaded\n", name
);
845 ULOG_ERR("failed to find a module named %s\n", name
);
852 fail
= load_modprobe();
855 ULOG_ERR("%d module%s could not be probed\n",
856 fail
, (fail
== 1) ? ("") : ("s"));
858 avl_for_each_element(&modules
, mn
, avl
) {
862 if ((m
->state
== PROBE
) || m
->error
)
863 ULOG_ERR("- %s\n", m
->name
);
873 static int main_loader(int argc
, char **argv
)
875 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
876 char *dir
= "/etc/modules.d/";
877 struct module_node
*mn
;
886 path
= malloc(strlen(dir
) + 2);
890 if (scan_module_folders()) {
895 if (scan_loaded_modules()) {
900 ULOG_INFO("loading kernel modules from %s\n", path
);
902 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
905 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
906 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
911 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
915 while (getline(&mod
, &mod_len
, fp
) > 0) {
916 char *nl
= strchr(mod
, '\n');
923 opts
= strchr(mod
, ' ');
927 m
= find_module(get_module_name(mod
));
928 if (!m
|| (m
->state
== LOADED
))
932 m
->opts
= strdup(opts
);
934 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
942 fail
= load_modprobe();
945 ULOG_ERR("%d module%s could not be probed\n",
946 fail
, (fail
== 1) ? ("") : ("s"));
948 avl_for_each_element(&modules
, mn
, avl
) {
952 if ((m
->state
== PROBE
) || (m
->error
))
953 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
956 ULOG_INFO("done loading kernel modules from %s\n", path
);
966 static inline char weight(char c
)
968 return c
== '_' ? '-' : c
;
971 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
976 while (*s1
&& (weight(*s1
) == weight(*s2
)))
982 return (unsigned char)weight(*s1
) - (unsigned char)weight(*s2
);
985 int main(int argc
, char **argv
)
987 char *exec
= basename(*argv
);
989 avl_init(&modules
, avl_modcmp
, false, NULL
);
990 if (!strcmp(exec
, "insmod"))
991 return main_insmod(argc
, argv
);
993 if (!strcmp(exec
, "rmmod"))
994 return main_rmmod(argc
, argv
);
996 if (!strcmp(exec
, "lsmod"))
997 return main_lsmod(argc
, argv
);
999 if (!strcmp(exec
, "modinfo"))
1000 return main_modinfo(argc
, argv
);
1002 if (!strcmp(exec
, "modprobe"))
1003 return main_modprobe(argc
, argv
);
1005 ulog_open(ULOG_KMSG
, LOG_USER
, "kmodloader");
1006 return main_loader(argc
, argv
);