f45f3dc0883827c705b2271b2d8639aee1fbb830
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__); \
65 static struct avl_tree modules
;
66 static char *prefix
= "";
68 static struct module
*find_module(const char *name
)
71 return avl_find_element(&modules
, name
, m
, avl
);
74 static void free_modules(void)
76 struct module
*m
, *tmp
;
78 avl_remove_all_elements(&modules
, m
, avl
, tmp
)
82 static void replace_dash(char *s
)
91 static void replace_underscore(char *s
)
100 static char* get_module_path(char *name
)
102 static char path
[256];
110 snprintf(path
, 256, "%s" DEF_MOD_PATH
"%s.ko", prefix
, ver
.release
, name
);
116 snprintf(path
, 256, "%s" DEF_MOD_PATH
"%s.ko", prefix
, ver
.release
, name
);
117 replace_underscore(path
);
125 static char* get_module_name(char *path
)
127 static char name
[32];
130 strncpy(name
, basename(path
), sizeof(name
));
132 t
= strstr(name
, ".ko");
141 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
143 const char *secnames
;
148 e
= (Elf64_Ehdr
*) map
;
149 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
151 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
152 for (i
= 0; i
< e
->e_shnum
; i
++) {
153 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
154 *size
= sh
[i
].sh_size
;
155 *offset
= sh
[i
].sh_offset
;
163 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
165 const char *secnames
;
170 e
= (Elf32_Ehdr
*) map
;
171 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
173 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
174 for (i
= 0; i
< e
->e_shnum
; i
++) {
175 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
176 *size
= sh
[i
].sh_size
;
177 *offset
= sh
[i
].sh_offset
;
186 static struct module
*
187 alloc_module(const char *name
, const char *depends
, int size
)
192 m
= calloc_a(sizeof(*m
),
193 &_name
, strlen(name
) + 1,
194 &_dep
, depends
? strlen(depends
) + 2 : 0);
198 m
->avl
.key
= m
->name
= strcpy(_name
, name
);
202 m
->depends
= strcpy(_dep
, depends
);
212 avl_insert(&modules
, &m
->avl
);
217 static int scan_loaded_modules(void)
223 fp
= fopen("/proc/modules", "r");
225 LOG("failed to open /proc/modules\n");
229 while (getline(&buf
, &buf_len
, fp
) > 0) {
233 m
.name
= strtok(buf
, " ");
234 m
.size
= atoi(strtok(NULL
, " "));
235 m
.usage
= atoi(strtok(NULL
, " "));
236 m
.depends
= strtok(NULL
, " ");
238 if (!m
.name
|| !m
.depends
)
241 n
= alloc_module(m
.name
, m
.depends
, m
.size
);
251 static struct module
* get_module_info(const char *module
, const char *name
)
253 int fd
= open(module
, O_RDONLY
);
254 unsigned int offset
, size
;
255 char *map
, *strings
, *dep
= NULL
;
260 LOG("failed to open %s\n", module
);
264 if (fstat(fd
, &s
) == -1) {
265 LOG("failed to stat %s\n", module
);
269 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
270 if (map
== MAP_FAILED
) {
271 LOG("failed to mmap %s\n", module
);
275 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
276 LOG("failed to load the .modinfo section from %s\n", module
);
280 strings
= map
+ offset
;
281 while (strings
&& (strings
< map
+ offset
+ size
)) {
287 sep
= strstr(strings
, "=");
292 if (!strncmp(strings
, "depends=", len
+ 1))
294 strings
= &sep
[strlen(sep
)];
297 m
= alloc_module(name
, dep
, s
.st_size
);
306 static int scan_module_folder(void)
308 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
315 path
= alloca(sizeof(DEF_MOD_PATH
"*.ko") + strlen(ver
.release
) + 1);
316 sprintf(path
, DEF_MOD_PATH
"*.ko", ver
.release
);
318 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
321 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
322 char *name
= get_module_name(gl
.gl_pathv
[j
]);
328 m
= find_module(name
);
330 get_module_info(gl
.gl_pathv
[j
], name
);
338 static int print_modinfo(char *module
)
340 int fd
= open(module
, O_RDONLY
);
341 unsigned int offset
, size
;
346 LOG("failed to open %s\n", module
);
350 if (fstat(fd
, &s
) == -1) {
351 LOG("failed to stat %s\n", module
);
355 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
356 if (map
== MAP_FAILED
) {
357 LOG("failed to mmap %s\n", module
);
361 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
362 LOG("failed to load the .modinfo section from %s\n", module
);
366 strings
= map
+ offset
;
367 printf("module:\t\t%s\n", module
);
368 while (strings
&& (strings
< map
+ offset
+ size
)) {
374 sep
= strstr(strings
, "=");
377 dup
= strndup(strings
, sep
- strings
);
379 if (strncmp(strings
, "parm", 4)) {
381 printf("%s:\t\t%s\n", dup
, sep
);
383 printf("%s:\t%s\n", dup
, sep
);
385 strings
= &sep
[strlen(sep
)];
393 static int deps_available(struct module
*m
, int verbose
)
398 if (!strcmp(m
->depends
, "_") || !strcmp(m
->depends
, ""))
404 m
= find_module(dep
);
407 LOG("missing dependency %s\n", dep
);
408 if (verbose
&& m
&& (m
->state
!= LOADED
))
409 LOG("dependency not loaded %s\n", dep
);
410 if (!m
|| (m
->state
!= LOADED
))
412 dep
+= strlen(dep
) + 1;
418 static int insert_module(char *path
, const char *options
)
424 if (stat(path
, &s
)) {
425 LOG("missing module %s\n", path
);
429 fd
= open(path
, O_RDONLY
);
431 LOG("cannot open %s\n", path
);
435 data
= malloc(s
.st_size
);
436 if (read(fd
, data
, s
.st_size
) == s
.st_size
)
437 ret
= syscall(__NR_init_module
, data
, s
.st_size
, options
);
439 LOG("failed to read full module %s\n", path
);
447 static void load_moddeps(struct module
*_m
)
452 if (!strcmp(_m
->depends
, "_") || !strcmp(_m
->depends
, ""))
458 m
= find_module(dep
);
461 LOG("failed to find dependency %s\n", dep
);
462 if (m
&& (m
->state
!= LOADED
)) {
467 dep
= dep
+ strlen(dep
) + 1;
471 static int iterations
= 0;
472 static int load_modprobe(void)
477 avl_for_each_element(&modules
, m
, avl
)
478 if (m
->state
== PROBE
)
484 avl_for_each_element(&modules
, m
, avl
) {
485 if ((m
->state
== PROBE
) && (!deps_available(m
, 0))) {
486 if (!insert_module(get_module_path(m
->name
), "")) {
495 if ((m
->state
== PROBE
) || m
->error
)
504 static int print_insmod_usage(void)
506 LOG("Usage:\n\tinsmod filename [args]\n");
511 static int print_usage(char *arg
)
513 LOG("Usage:\n\t%s module\n", arg
);
518 static int main_insmod(int argc
, char **argv
)
520 char *name
, *cur
, *options
;
524 return print_insmod_usage();
526 name
= get_module_name(argv
[1]);
528 LOG("cannot find module - %s\n", argv
[1]);
532 if (scan_loaded_modules())
535 if (find_module(name
)) {
536 LOG("module is already loaded - %s\n", name
);
543 for (len
= 0, i
= 2; i
< argc
; i
++)
544 len
+= strlen(argv
[i
]) + 1;
546 options
= malloc(len
);
549 for (i
= 2; i
< argc
; i
++) {
554 cur
+= sprintf(cur
, "%s", argv
[i
]);
557 ret
= insert_module(get_module_path(name
), options
);
561 LOG("failed to insert %s\n", get_module_path(name
));
566 static int main_rmmod(int argc
, char **argv
)
573 return print_usage("rmmod");
575 if (scan_loaded_modules())
578 name
= get_module_name(argv
[1]);
579 m
= find_module(name
);
581 LOG("module is not loaded\n");
586 ret
= syscall(__NR_delete_module
, name
, 0);
589 LOG("unloading the module failed\n");
594 static int main_lsmod(int argc
, char **argv
)
598 if (scan_loaded_modules())
601 avl_for_each_element(&modules
, m
, avl
)
602 if (m
->state
== LOADED
)
603 printf("%-20s%8d%3d %s\n",
604 m
->name
, m
->size
, m
->usage
,
605 (*m
->depends
== '-') ? ("") : (m
->depends
));
612 static int main_modinfo(int argc
, char **argv
)
617 return print_usage("modinfo");
619 module
= get_module_path(argv
[1]);
621 LOG("cannot find module - %s\n", argv
[1]);
625 print_modinfo(module
);
630 static int main_modprobe(int argc
, char **argv
)
636 return print_usage("modprobe");
638 if (scan_loaded_modules())
641 if (scan_module_folder())
644 name
= get_module_name(argv
[1]);
645 m
= find_module(name
);
646 if (m
&& m
->state
== LOADED
) {
647 LOG("%s is already loaded\n", name
);
650 LOG("failed to find a module named %s\n", name
);
656 fail
= load_modprobe();
659 LOG("%d module%s could not be probed\n",
660 fail
, (fail
== 1) ? ("") : ("s"));
662 avl_for_each_element(&modules
, m
, avl
)
663 if ((m
->state
== PROBE
) || m
->error
)
664 LOG("- %s\n", m
->name
);
673 static int main_loader(int argc
, char **argv
)
675 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
676 char *dir
= "/etc/modules.d/*";
688 path
= malloc(strlen(dir
) + 2);
692 if (scan_loaded_modules())
695 if (scan_module_folder())
698 syslog(0, "kmodloader: loading kernel modules from %s\n", path
);
700 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
703 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
704 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
709 LOG("failed to open %s\n", gl
.gl_pathv
[j
]);
713 while (getline(&mod
, &mod_len
, fp
) > 0) {
714 char *nl
= strchr(mod
, '\n');
721 opts
= strchr(mod
, ' ');
725 m
= find_module(get_module_name(mod
));
726 if (!m
|| (m
->state
== LOADED
))
730 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
738 fail
= load_modprobe();
739 LOG("ran %d iterations\n", iterations
);
742 LOG("%d module%s could not be probed\n",
743 fail
, (fail
== 1) ? ("") : ("s"));
745 avl_for_each_element(&modules
, m
, avl
)
746 if ((m
->state
== PROBE
) || (m
->error
))
747 LOG("- %s - %d\n", m
->name
, deps_available(m
, 1));
757 int main(int argc
, char **argv
)
759 char *exec
= basename(*argv
);
761 avl_init(&modules
, avl_strcmp
, false, NULL
);
762 if (!strcmp(exec
, "insmod"))
763 return main_insmod(argc
, argv
);
765 if (!strcmp(exec
, "rmmod"))
766 return main_rmmod(argc
, argv
);
768 if (!strcmp(exec
, "lsmod"))
769 return main_lsmod(argc
, argv
);
771 if (!strcmp(exec
, "modinfo"))
772 return main_modinfo(argc
, argv
);
774 if (!strcmp(exec
, "modprobe"))
775 return main_modprobe(argc
, argv
);
777 return main_loader(argc
, argv
);