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
)
93 static void replace_underscore(char *s
)
102 static char* get_module_path(char *name
)
104 static char path
[256];
112 snprintf(path
, 256, "%s" DEF_MOD_PATH
"%s.ko", prefix
, ver
.release
, name
);
118 snprintf(path
, 256, "%s" DEF_MOD_PATH
"%s.ko", prefix
, ver
.release
, name
);
119 replace_underscore(path
);
127 static char* get_module_name(char *path
)
129 static char name
[32];
132 strncpy(name
, basename(path
), sizeof(name
));
134 t
= strstr(name
, ".ko");
143 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
145 const char *secnames
;
150 e
= (Elf64_Ehdr
*) map
;
151 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
153 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
154 for (i
= 0; i
< e
->e_shnum
; i
++) {
155 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
156 *size
= sh
[i
].sh_size
;
157 *offset
= sh
[i
].sh_offset
;
165 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
167 const char *secnames
;
172 e
= (Elf32_Ehdr
*) map
;
173 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
175 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
176 for (i
= 0; i
< e
->e_shnum
; i
++) {
177 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
178 *size
= sh
[i
].sh_size
;
179 *offset
= sh
[i
].sh_offset
;
188 static struct module
*
189 alloc_module(const char *name
, const char *depends
, int size
)
194 m
= calloc_a(sizeof(*m
),
195 &_name
, strlen(name
) + 1,
196 &_dep
, depends
? strlen(depends
) + 2 : 0);
200 m
->avl
.key
= m
->name
= strcpy(_name
, name
);
204 m
->depends
= strcpy(_dep
, depends
);
214 avl_insert(&modules
, &m
->avl
);
219 static int scan_loaded_modules(void)
225 fp
= fopen("/proc/modules", "r");
227 LOG("failed to open /proc/modules\n");
231 while (getline(&buf
, &buf_len
, fp
) > 0) {
235 m
.name
= strtok(buf
, " ");
236 m
.size
= atoi(strtok(NULL
, " "));
237 m
.usage
= atoi(strtok(NULL
, " "));
238 m
.depends
= strtok(NULL
, " ");
240 if (!m
.name
|| !m
.depends
)
243 n
= alloc_module(m
.name
, m
.depends
, m
.size
);
253 static struct module
* get_module_info(const char *module
, const char *name
)
255 int fd
= open(module
, O_RDONLY
);
256 unsigned int offset
, size
;
257 char *map
, *strings
, *dep
= NULL
;
262 LOG("failed to open %s\n", module
);
266 if (fstat(fd
, &s
) == -1) {
267 LOG("failed to stat %s\n", module
);
271 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
272 if (map
== MAP_FAILED
) {
273 LOG("failed to mmap %s\n", module
);
277 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
278 LOG("failed to load the .modinfo section from %s\n", module
);
282 strings
= map
+ offset
;
283 while (strings
&& (strings
< map
+ offset
+ size
)) {
289 sep
= strstr(strings
, "=");
294 if (!strncmp(strings
, "depends=", len
+ 1))
296 strings
= &sep
[strlen(sep
)];
299 m
= alloc_module(name
, dep
, s
.st_size
);
308 static int scan_module_folder(void)
310 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
317 path
= alloca(sizeof(DEF_MOD_PATH
"*.ko") + strlen(ver
.release
) + 1);
318 sprintf(path
, DEF_MOD_PATH
"*.ko", ver
.release
);
320 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
323 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
324 char *name
= get_module_name(gl
.gl_pathv
[j
]);
330 m
= find_module(name
);
332 get_module_info(gl
.gl_pathv
[j
], name
);
340 static int print_modinfo(char *module
)
342 int fd
= open(module
, O_RDONLY
);
343 unsigned int offset
, size
;
348 LOG("failed to open %s\n", module
);
352 if (fstat(fd
, &s
) == -1) {
353 LOG("failed to stat %s\n", module
);
357 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
358 if (map
== MAP_FAILED
) {
359 LOG("failed to mmap %s\n", module
);
363 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
364 LOG("failed to load the .modinfo section from %s\n", module
);
368 strings
= map
+ offset
;
369 printf("module:\t\t%s\n", module
);
370 while (strings
&& (strings
< map
+ offset
+ size
)) {
376 sep
= strstr(strings
, "=");
379 dup
= strndup(strings
, sep
- strings
);
381 if (strncmp(strings
, "parm", 4)) {
383 printf("%s:\t\t%s\n", dup
, sep
);
385 printf("%s:\t%s\n", dup
, sep
);
387 strings
= &sep
[strlen(sep
)];
395 static int deps_available(struct module
*m
, int verbose
)
400 if (!strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
406 m
= find_module(dep
);
409 LOG("missing dependency %s\n", dep
);
410 if (verbose
&& m
&& (m
->state
!= LOADED
))
411 LOG("dependency not loaded %s\n", dep
);
412 if (!m
|| (m
->state
!= LOADED
))
414 dep
+= strlen(dep
) + 1;
420 static int insert_module(char *path
, const char *options
)
426 if (stat(path
, &s
)) {
427 LOG("missing module %s\n", path
);
431 fd
= open(path
, O_RDONLY
);
433 LOG("cannot open %s\n", path
);
437 data
= malloc(s
.st_size
);
438 if (read(fd
, data
, s
.st_size
) == s
.st_size
)
439 ret
= syscall(__NR_init_module
, data
, s
.st_size
, options
);
441 LOG("failed to read full module %s\n", path
);
449 static void load_moddeps(struct module
*_m
)
454 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
460 m
= find_module(dep
);
463 LOG("failed to find dependency %s\n", dep
);
464 if (m
&& (m
->state
!= LOADED
)) {
469 dep
= dep
+ strlen(dep
) + 1;
473 static int iterations
= 0;
474 static int load_modprobe(void)
479 avl_for_each_element(&modules
, m
, avl
)
480 if (m
->state
== PROBE
)
486 avl_for_each_element(&modules
, m
, avl
) {
487 if ((m
->state
== PROBE
) && (!deps_available(m
, 0))) {
488 if (!insert_module(get_module_path(m
->name
), "")) {
497 if ((m
->state
== PROBE
) || m
->error
)
506 static int print_insmod_usage(void)
508 LOG("Usage:\n\tinsmod filename [args]\n");
513 static int print_usage(char *arg
)
515 LOG("Usage:\n\t%s module\n", arg
);
520 static int main_insmod(int argc
, char **argv
)
522 char *name
, *cur
, *options
;
526 return print_insmod_usage();
528 name
= get_module_name(argv
[1]);
530 LOG("cannot find module - %s\n", argv
[1]);
534 if (scan_loaded_modules())
537 if (find_module(name
)) {
538 LOG("module is already loaded - %s\n", name
);
545 for (len
= 0, i
= 2; i
< argc
; i
++)
546 len
+= strlen(argv
[i
]) + 1;
548 options
= malloc(len
);
551 for (i
= 2; i
< argc
; i
++) {
556 cur
+= sprintf(cur
, "%s", argv
[i
]);
559 ret
= insert_module(get_module_path(name
), options
);
563 LOG("failed to insert %s\n", get_module_path(name
));
568 static int main_rmmod(int argc
, char **argv
)
575 return print_usage("rmmod");
577 if (scan_loaded_modules())
580 name
= get_module_name(argv
[1]);
581 m
= find_module(name
);
583 LOG("module is not loaded\n");
588 ret
= syscall(__NR_delete_module
, name
, 0);
591 LOG("unloading the module failed\n");
596 static int main_lsmod(int argc
, char **argv
)
600 if (scan_loaded_modules())
603 avl_for_each_element(&modules
, m
, avl
)
604 if (m
->state
== LOADED
)
605 printf("%-20s%8d%3d %s\n",
606 m
->name
, m
->size
, m
->usage
,
607 (*m
->depends
== '-') ? ("") : (m
->depends
));
614 static int main_modinfo(int argc
, char **argv
)
619 return print_usage("modinfo");
621 module
= get_module_path(argv
[1]);
623 LOG("cannot find module - %s\n", argv
[1]);
627 print_modinfo(module
);
632 static int main_modprobe(int argc
, char **argv
)
638 return print_usage("modprobe");
640 if (scan_loaded_modules())
643 if (scan_module_folder())
646 name
= get_module_name(argv
[1]);
647 m
= find_module(name
);
648 if (m
&& m
->state
== LOADED
) {
649 LOG("%s is already loaded\n", name
);
652 LOG("failed to find a module named %s\n", name
);
658 fail
= load_modprobe();
661 LOG("%d module%s could not be probed\n",
662 fail
, (fail
== 1) ? ("") : ("s"));
664 avl_for_each_element(&modules
, m
, avl
)
665 if ((m
->state
== PROBE
) || m
->error
)
666 LOG("- %s\n", m
->name
);
675 static int main_loader(int argc
, char **argv
)
677 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
678 char *dir
= "/etc/modules.d/*";
690 path
= malloc(strlen(dir
) + 2);
694 if (scan_loaded_modules())
697 if (scan_module_folder())
700 syslog(0, "kmodloader: loading kernel modules from %s\n", path
);
702 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
705 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
706 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
711 LOG("failed to open %s\n", gl
.gl_pathv
[j
]);
715 while (getline(&mod
, &mod_len
, fp
) > 0) {
716 char *nl
= strchr(mod
, '\n');
723 opts
= strchr(mod
, ' ');
727 m
= find_module(get_module_name(mod
));
728 if (!m
|| (m
->state
== LOADED
))
732 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
740 fail
= load_modprobe();
741 LOG("ran %d iterations\n", iterations
);
744 LOG("%d module%s could not be probed\n",
745 fail
, (fail
== 1) ? ("") : ("s"));
747 avl_for_each_element(&modules
, m
, avl
)
748 if ((m
->state
== PROBE
) || (m
->error
))
749 LOG("- %s - %d\n", m
->name
, deps_available(m
, 1));
759 int main(int argc
, char **argv
)
761 char *exec
= basename(*argv
);
763 avl_init(&modules
, avl_strcmp
, false, NULL
);
764 if (!strcmp(exec
, "insmod"))
765 return main_insmod(argc
, argv
);
767 if (!strcmp(exec
, "rmmod"))
768 return main_rmmod(argc
, argv
);
770 if (!strcmp(exec
, "lsmod"))
771 return main_lsmod(argc
, argv
);
773 if (!strcmp(exec
, "modinfo"))
774 return main_modinfo(argc
, argv
);
776 if (!strcmp(exec
, "modprobe"))
777 return main_modprobe(argc
, argv
);
779 return main_loader(argc
, argv
);