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>
39 #define DEF_MOD_PATH "/lib/modules/%s/"
41 #define LOG(fmt, ...) do { \
42 syslog(LOG_INFO, fmt, ## __VA_ARGS__); \
43 printf("kmod: "fmt, ## __VA_ARGS__); \
66 static struct avl_tree modules
;
67 static char *prefix
= "";
69 static struct module
*find_module(const char *name
)
72 return avl_find_element(&modules
, name
, m
, avl
);
75 static void free_modules(void)
77 struct module
*m
, *tmp
;
79 avl_remove_all_elements(&modules
, m
, avl
, tmp
)
83 static char* get_module_path(char *name
)
85 static char path
[256];
93 snprintf(path
, 256, "%s" DEF_MOD_PATH
"%s.ko", prefix
, ver
.release
, name
);
101 static char* get_module_name(char *path
)
103 static char name
[32];
106 strncpy(name
, basename(path
), sizeof(name
));
108 t
= strstr(name
, ".ko");
116 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
118 const char *secnames
;
123 e
= (Elf64_Ehdr
*) map
;
124 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
126 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
127 for (i
= 0; i
< e
->e_shnum
; i
++) {
128 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
129 *size
= sh
[i
].sh_size
;
130 *offset
= sh
[i
].sh_offset
;
138 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
140 const char *secnames
;
145 e
= (Elf32_Ehdr
*) map
;
146 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
148 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
149 for (i
= 0; i
< e
->e_shnum
; i
++) {
150 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
151 *size
= sh
[i
].sh_size
;
152 *offset
= sh
[i
].sh_offset
;
161 static struct module
*
162 alloc_module(const char *name
, const char *depends
, int size
)
167 m
= calloc_a(sizeof(*m
),
168 &_name
, strlen(name
) + 1,
169 &_dep
, depends
? strlen(depends
) + 2 : 0);
173 m
->avl
.key
= m
->name
= strcpy(_name
, name
);
176 m
->depends
= strcpy(_dep
, depends
);
185 avl_insert(&modules
, &m
->avl
);
190 static int scan_loaded_modules(void)
196 fp
= fopen("/proc/modules", "r");
198 LOG("failed to open /proc/modules\n");
202 while (getline(&buf
, &buf_len
, fp
) > 0) {
206 m
.name
= strtok(buf
, " ");
207 m
.size
= atoi(strtok(NULL
, " "));
208 m
.usage
= atoi(strtok(NULL
, " "));
209 m
.depends
= strtok(NULL
, " ");
211 if (!m
.name
|| !m
.depends
)
214 n
= alloc_module(m
.name
, m
.depends
, m
.size
);
224 static struct module
* get_module_info(const char *module
, const char *name
)
226 int fd
= open(module
, O_RDONLY
);
227 unsigned int offset
, size
;
228 char *map
, *strings
, *dep
= NULL
;
233 LOG("failed to open %s\n", module
);
237 if (fstat(fd
, &s
) == -1) {
238 LOG("failed to stat %s\n", module
);
242 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
243 if (map
== MAP_FAILED
) {
244 LOG("failed to mmap %s\n", module
);
248 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
249 LOG("failed to load the .modinfo section from %s\n", module
);
253 strings
= map
+ offset
;
254 while (strings
&& (strings
< map
+ offset
+ size
)) {
260 sep
= strstr(strings
, "=");
265 if (!strncmp(strings
, "depends=", len
+ 1))
267 strings
= &sep
[strlen(sep
)];
270 m
= alloc_module(name
, dep
, s
.st_size
);
279 static int scan_module_folder(void)
281 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
288 path
= alloca(sizeof(DEF_MOD_PATH
"*.ko") + strlen(prefix
) + strlen(ver
.release
) + 1);
289 sprintf(path
, "%s" DEF_MOD_PATH
"*.ko", prefix
, ver
.release
);
291 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
294 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
295 char *name
= get_module_name(gl
.gl_pathv
[j
]);
301 m
= find_module(name
);
303 get_module_info(gl
.gl_pathv
[j
], name
);
311 static int print_modinfo(char *module
)
313 int fd
= open(module
, O_RDONLY
);
314 unsigned int offset
, size
;
319 LOG("failed to open %s\n", module
);
323 if (fstat(fd
, &s
) == -1) {
324 LOG("failed to stat %s\n", module
);
328 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
329 if (map
== MAP_FAILED
) {
330 LOG("failed to mmap %s\n", module
);
334 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
335 LOG("failed to load the .modinfo section from %s\n", module
);
339 strings
= map
+ offset
;
340 printf("module:\t\t%s\n", module
);
341 while (strings
&& (strings
< map
+ offset
+ size
)) {
347 sep
= strstr(strings
, "=");
350 dup
= strndup(strings
, sep
- strings
);
352 if (strncmp(strings
, "parm", 4)) {
354 printf("%s:\t\t%s\n", dup
, sep
);
356 printf("%s:\t%s\n", dup
, sep
);
358 strings
= &sep
[strlen(sep
)];
366 static int deps_available(struct module
*m
, int verbose
)
371 if (!strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
377 m
= find_module(dep
);
380 LOG("missing dependency %s\n", dep
);
381 if (verbose
&& m
&& (m
->state
!= LOADED
))
382 LOG("dependency not loaded %s\n", dep
);
383 if (!m
|| (m
->state
!= LOADED
))
385 dep
+= strlen(dep
) + 1;
391 static int insert_module(char *path
, const char *options
)
397 if (stat(path
, &s
)) {
398 LOG("missing module %s\n", path
);
402 fd
= open(path
, O_RDONLY
);
404 LOG("cannot open %s\n", path
);
408 data
= malloc(s
.st_size
);
409 if (read(fd
, data
, s
.st_size
) == s
.st_size
)
410 ret
= syscall(__NR_init_module
, data
, s
.st_size
, options
);
412 LOG("failed to read full module %s\n", path
);
420 static void load_moddeps(struct module
*_m
)
425 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
431 m
= find_module(dep
);
434 LOG("failed to find dependency %s\n", dep
);
435 if (m
&& (m
->state
!= LOADED
)) {
440 dep
= dep
+ strlen(dep
) + 1;
444 static int iterations
= 0;
445 static int load_modprobe(void)
450 avl_for_each_element(&modules
, m
, avl
)
451 if (m
->state
== PROBE
)
457 avl_for_each_element(&modules
, m
, avl
) {
458 if ((m
->state
== PROBE
) && (!deps_available(m
, 0))) {
459 if (!insert_module(get_module_path(m
->name
), m
->options
)) {
468 if ((m
->state
== PROBE
) || m
->error
)
477 static int print_insmod_usage(void)
479 LOG("Usage:\n\tinsmod filename [args]\n");
484 static int print_usage(char *arg
)
486 LOG("Usage:\n\t%s module\n", arg
);
491 static int main_insmod(int argc
, char **argv
)
493 char *name
, *cur
, *options
;
497 return print_insmod_usage();
499 name
= get_module_name(argv
[1]);
501 LOG("cannot find module - %s\n", argv
[1]);
505 if (scan_loaded_modules())
508 if (find_module(name
)) {
509 LOG("module is already loaded - %s\n", name
);
516 for (len
= 0, i
= 2; i
< argc
; i
++)
517 len
+= strlen(argv
[i
]) + 1;
519 options
= malloc(len
);
522 for (i
= 2; i
< argc
; i
++) {
527 cur
+= sprintf(cur
, "%s", argv
[i
]);
530 ret
= insert_module(get_module_path(name
), options
);
534 LOG("failed to insert %s\n", get_module_path(name
));
539 static int main_rmmod(int argc
, char **argv
)
546 return print_usage("rmmod");
548 if (scan_loaded_modules())
551 name
= get_module_name(argv
[1]);
552 m
= find_module(name
);
554 LOG("module is not loaded\n");
557 ret
= syscall(__NR_delete_module
, m
->name
, 0);
560 LOG("unloading the module failed\n");
567 static int main_lsmod(int argc
, char **argv
)
571 if (scan_loaded_modules())
574 avl_for_each_element(&modules
, m
, avl
)
575 if (m
->state
== LOADED
)
576 printf("%-20s%8d%3d %s\n",
577 m
->name
, m
->size
, m
->usage
,
578 (*m
->depends
== '-') ? ("") : (m
->depends
));
585 static int main_modinfo(int argc
, char **argv
)
591 return print_usage("modinfo");
593 if (scan_module_folder())
596 name
= get_module_name(argv
[1]);
597 m
= find_module(name
);
599 LOG("cannot find module - %s\n", argv
[1]);
603 name
= get_module_path(m
->name
);
605 LOG("cannot find path of module - %s\n", m
->name
);
614 static int main_modprobe(int argc
, char **argv
)
620 return print_usage("modprobe");
622 if (scan_loaded_modules())
625 if (scan_module_folder())
628 name
= get_module_name(argv
[1]);
629 m
= find_module(name
);
630 if (m
&& m
->state
== LOADED
) {
631 LOG("%s is already loaded\n", name
);
634 LOG("failed to find a module named %s\n", name
);
640 fail
= load_modprobe();
643 LOG("%d module%s could not be probed\n",
644 fail
, (fail
== 1) ? ("") : ("s"));
646 avl_for_each_element(&modules
, m
, avl
)
647 if ((m
->state
== PROBE
) || m
->error
)
648 LOG("- %s\n", m
->name
);
657 static int main_loader(int argc
, char **argv
)
659 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
660 char *dir
= "/etc/modules.d/*";
672 path
= malloc(strlen(dir
) + 2);
676 if (scan_loaded_modules())
679 if (scan_module_folder())
682 syslog(0, "kmodloader: loading kernel modules from %s\n", path
);
684 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
687 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
688 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
693 LOG("failed to open %s\n", gl
.gl_pathv
[j
]);
697 while (getline(&mod
, &mod_len
, fp
) > 0) {
698 char *nl
= strchr(mod
, '\n');
705 opts
= strchr(mod
, ' ');
709 m
= find_module(get_module_name(mod
));
710 if (!m
|| (m
->state
== LOADED
))
715 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
723 fail
= load_modprobe();
724 LOG("ran %d iterations\n", iterations
);
727 LOG("%d module%s could not be probed\n",
728 fail
, (fail
== 1) ? ("") : ("s"));
730 avl_for_each_element(&modules
, m
, avl
)
731 if ((m
->state
== PROBE
) || (m
->error
))
732 LOG("- %s - %d\n", m
->name
, deps_available(m
, 1));
742 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
747 while (*s1
&& ((*s1
== *s2
) ||
748 ((*s1
== '_') && (*s2
== '-')) ||
749 ((*s1
== '-') && (*s2
== '_'))))
755 return *(const unsigned char *)s1
- *(const unsigned char *)s2
;
758 int main(int argc
, char **argv
)
760 char *exec
= basename(*argv
);
762 avl_init(&modules
, avl_modcmp
, false, NULL
);
763 if (!strcmp(exec
, "insmod"))
764 return main_insmod(argc
, argv
);
766 if (!strcmp(exec
, "rmmod"))
767 return main_rmmod(argc
, argv
);
769 if (!strcmp(exec
, "lsmod"))
770 return main_lsmod(argc
, argv
);
772 if (!strcmp(exec
, "modinfo"))
773 return main_modinfo(argc
, argv
);
775 if (!strcmp(exec
, "modprobe"))
776 return main_modprobe(argc
, argv
);
778 return main_loader(argc
, argv
);