417d1210f0cf77f8d7b2d0c2477308540df62aeb
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 char* get_module_path(char *name
)
84 static char path
[256];
92 snprintf(path
, 256, "%s" DEF_MOD_PATH
"%s.ko", prefix
, ver
.release
, name
);
100 static char* get_module_name(char *path
)
102 static char name
[32];
105 strncpy(name
, basename(path
), sizeof(name
));
107 t
= strstr(name
, ".ko");
115 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
117 const char *secnames
;
122 e
= (Elf64_Ehdr
*) map
;
123 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
125 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
126 for (i
= 0; i
< e
->e_shnum
; i
++) {
127 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
128 *size
= sh
[i
].sh_size
;
129 *offset
= sh
[i
].sh_offset
;
137 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
139 const char *secnames
;
144 e
= (Elf32_Ehdr
*) map
;
145 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
147 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
148 for (i
= 0; i
< e
->e_shnum
; i
++) {
149 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
150 *size
= sh
[i
].sh_size
;
151 *offset
= sh
[i
].sh_offset
;
160 static struct module
*
161 alloc_module(const char *name
, const char *depends
, int size
)
166 m
= calloc_a(sizeof(*m
),
167 &_name
, strlen(name
) + 1,
168 &_dep
, depends
? strlen(depends
) + 2 : 0);
172 m
->avl
.key
= m
->name
= strcpy(_name
, name
);
175 m
->depends
= strcpy(_dep
, depends
);
184 avl_insert(&modules
, &m
->avl
);
189 static int scan_loaded_modules(void)
195 fp
= fopen("/proc/modules", "r");
197 LOG("failed to open /proc/modules\n");
201 while (getline(&buf
, &buf_len
, fp
) > 0) {
205 m
.name
= strtok(buf
, " ");
206 m
.size
= atoi(strtok(NULL
, " "));
207 m
.usage
= atoi(strtok(NULL
, " "));
208 m
.depends
= strtok(NULL
, " ");
210 if (!m
.name
|| !m
.depends
)
213 n
= alloc_module(m
.name
, m
.depends
, m
.size
);
223 static struct module
* get_module_info(const char *module
, const char *name
)
225 int fd
= open(module
, O_RDONLY
);
226 unsigned int offset
, size
;
227 char *map
, *strings
, *dep
= NULL
;
232 LOG("failed to open %s\n", module
);
236 if (fstat(fd
, &s
) == -1) {
237 LOG("failed to stat %s\n", module
);
241 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
242 if (map
== MAP_FAILED
) {
243 LOG("failed to mmap %s\n", module
);
247 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
248 LOG("failed to load the .modinfo section from %s\n", module
);
252 strings
= map
+ offset
;
253 while (strings
&& (strings
< map
+ offset
+ size
)) {
259 sep
= strstr(strings
, "=");
264 if (!strncmp(strings
, "depends=", len
+ 1))
266 strings
= &sep
[strlen(sep
)];
269 m
= alloc_module(name
, dep
, s
.st_size
);
278 static int scan_module_folder(void)
280 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
287 path
= alloca(sizeof(DEF_MOD_PATH
"*.ko") + strlen(ver
.release
) + 1);
288 sprintf(path
, DEF_MOD_PATH
"*.ko", ver
.release
);
290 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
293 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
294 char *name
= get_module_name(gl
.gl_pathv
[j
]);
300 m
= find_module(name
);
302 get_module_info(gl
.gl_pathv
[j
], name
);
310 static int print_modinfo(char *module
)
312 int fd
= open(module
, O_RDONLY
);
313 unsigned int offset
, size
;
318 LOG("failed to open %s\n", module
);
322 if (fstat(fd
, &s
) == -1) {
323 LOG("failed to stat %s\n", module
);
327 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
328 if (map
== MAP_FAILED
) {
329 LOG("failed to mmap %s\n", module
);
333 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
334 LOG("failed to load the .modinfo section from %s\n", module
);
338 strings
= map
+ offset
;
339 printf("module:\t\t%s\n", module
);
340 while (strings
&& (strings
< map
+ offset
+ size
)) {
346 sep
= strstr(strings
, "=");
349 dup
= strndup(strings
, sep
- strings
);
351 if (strncmp(strings
, "parm", 4)) {
353 printf("%s:\t\t%s\n", dup
, sep
);
355 printf("%s:\t%s\n", dup
, sep
);
357 strings
= &sep
[strlen(sep
)];
365 static int deps_available(struct module
*m
, int verbose
)
370 if (!strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
376 m
= find_module(dep
);
379 LOG("missing dependency %s\n", dep
);
380 if (verbose
&& m
&& (m
->state
!= LOADED
))
381 LOG("dependency not loaded %s\n", dep
);
382 if (!m
|| (m
->state
!= LOADED
))
384 dep
+= strlen(dep
) + 1;
390 static int insert_module(char *path
, const char *options
)
396 if (stat(path
, &s
)) {
397 LOG("missing module %s\n", path
);
401 fd
= open(path
, O_RDONLY
);
403 LOG("cannot open %s\n", path
);
407 data
= malloc(s
.st_size
);
408 if (read(fd
, data
, s
.st_size
) == s
.st_size
)
409 ret
= syscall(__NR_init_module
, data
, s
.st_size
, options
);
411 LOG("failed to read full module %s\n", path
);
419 static void load_moddeps(struct module
*_m
)
424 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
430 m
= find_module(dep
);
433 LOG("failed to find dependency %s\n", dep
);
434 if (m
&& (m
->state
!= LOADED
)) {
439 dep
= dep
+ strlen(dep
) + 1;
443 static int iterations
= 0;
444 static int load_modprobe(void)
449 avl_for_each_element(&modules
, m
, avl
)
450 if (m
->state
== PROBE
)
456 avl_for_each_element(&modules
, m
, avl
) {
457 if ((m
->state
== PROBE
) && (!deps_available(m
, 0))) {
458 if (!insert_module(get_module_path(m
->name
), "")) {
467 if ((m
->state
== PROBE
) || m
->error
)
476 static int print_insmod_usage(void)
478 LOG("Usage:\n\tinsmod filename [args]\n");
483 static int print_usage(char *arg
)
485 LOG("Usage:\n\t%s module\n", arg
);
490 static int main_insmod(int argc
, char **argv
)
492 char *name
, *cur
, *options
;
496 return print_insmod_usage();
498 name
= get_module_name(argv
[1]);
500 LOG("cannot find module - %s\n", argv
[1]);
504 if (scan_loaded_modules())
507 if (find_module(name
)) {
508 LOG("module is already loaded - %s\n", name
);
515 for (len
= 0, i
= 2; i
< argc
; i
++)
516 len
+= strlen(argv
[i
]) + 1;
518 options
= malloc(len
);
521 for (i
= 2; i
< argc
; i
++) {
526 cur
+= sprintf(cur
, "%s", argv
[i
]);
529 ret
= insert_module(get_module_path(name
), options
);
533 LOG("failed to insert %s\n", get_module_path(name
));
538 static int main_rmmod(int argc
, char **argv
)
545 return print_usage("rmmod");
547 if (scan_loaded_modules())
550 name
= get_module_name(argv
[1]);
551 m
= find_module(name
);
553 LOG("module is not loaded\n");
556 ret
= syscall(__NR_delete_module
, m
->name
, 0);
559 LOG("unloading the module failed\n");
566 static int main_lsmod(int argc
, char **argv
)
570 if (scan_loaded_modules())
573 avl_for_each_element(&modules
, m
, avl
)
574 if (m
->state
== LOADED
)
575 printf("%-20s%8d%3d %s\n",
576 m
->name
, m
->size
, m
->usage
,
577 (*m
->depends
== '-') ? ("") : (m
->depends
));
584 static int main_modinfo(int argc
, char **argv
)
589 return print_usage("modinfo");
591 module
= get_module_path(argv
[1]);
593 LOG("cannot find module - %s\n", argv
[1]);
597 print_modinfo(module
);
602 static int main_modprobe(int argc
, char **argv
)
608 return print_usage("modprobe");
610 if (scan_loaded_modules())
613 if (scan_module_folder())
616 name
= get_module_name(argv
[1]);
617 m
= find_module(name
);
618 if (m
&& m
->state
== LOADED
) {
619 LOG("%s is already loaded\n", name
);
622 LOG("failed to find a module named %s\n", name
);
628 fail
= load_modprobe();
631 LOG("%d module%s could not be probed\n",
632 fail
, (fail
== 1) ? ("") : ("s"));
634 avl_for_each_element(&modules
, m
, avl
)
635 if ((m
->state
== PROBE
) || m
->error
)
636 LOG("- %s\n", m
->name
);
645 static int main_loader(int argc
, char **argv
)
647 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
648 char *dir
= "/etc/modules.d/*";
660 path
= malloc(strlen(dir
) + 2);
664 if (scan_loaded_modules())
667 if (scan_module_folder())
670 syslog(0, "kmodloader: loading kernel modules from %s\n", path
);
672 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
675 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
676 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
681 LOG("failed to open %s\n", gl
.gl_pathv
[j
]);
685 while (getline(&mod
, &mod_len
, fp
) > 0) {
686 char *nl
= strchr(mod
, '\n');
693 opts
= strchr(mod
, ' ');
697 m
= find_module(get_module_name(mod
));
698 if (!m
|| (m
->state
== LOADED
))
702 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
710 fail
= load_modprobe();
711 LOG("ran %d iterations\n", iterations
);
714 LOG("%d module%s could not be probed\n",
715 fail
, (fail
== 1) ? ("") : ("s"));
717 avl_for_each_element(&modules
, m
, avl
)
718 if ((m
->state
== PROBE
) || (m
->error
))
719 LOG("- %s - %d\n", m
->name
, deps_available(m
, 1));
729 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
734 while (*s1
&& ((*s1
== *s2
) ||
735 ((*s1
== '_') && (*s2
== '-')) ||
736 ((*s1
== '-') && (*s2
== '_'))))
742 return *(const unsigned char *)s1
- *(const unsigned char *)s2
;
745 int main(int argc
, char **argv
)
747 char *exec
= basename(*argv
);
749 avl_init(&modules
, avl_modcmp
, false, NULL
);
750 if (!strcmp(exec
, "insmod"))
751 return main_insmod(argc
, argv
);
753 if (!strcmp(exec
, "rmmod"))
754 return main_rmmod(argc
, argv
);
756 if (!strcmp(exec
, "lsmod"))
757 return main_lsmod(argc
, argv
);
759 if (!strcmp(exec
, "modinfo"))
760 return main_modinfo(argc
, argv
);
762 if (!strcmp(exec
, "modprobe"))
763 return main_modprobe(argc
, argv
);
765 return main_loader(argc
, argv
);