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 INFO(fmt, ...) do { \
42 syslog(LOG_INFO, fmt, ## __VA_ARGS__); \
43 printf("kmod: "fmt, ## __VA_ARGS__); \
45 #define ERROR(fmt, ...) do { \
46 syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
47 fprintf(stderr,"kmod: "fmt, ## __VA_ARGS__); \
49 #define DEBUG(fmt, ...) do { \
50 syslog(LOG_DEBUG, fmt, ## __VA_ARGS__); \
73 static struct avl_tree modules
;
74 static char *prefix
= "";
76 static struct module
*find_module(const char *name
)
79 return avl_find_element(&modules
, name
, m
, avl
);
82 static void free_modules(void)
84 struct module
*m
, *tmp
;
86 avl_remove_all_elements(&modules
, m
, avl
, tmp
)
90 static char* get_module_path(char *name
)
92 static char path
[256];
100 snprintf(path
, 256, "%s" DEF_MOD_PATH
"%s.ko", prefix
, ver
.release
, name
);
108 static char* get_module_name(char *path
)
110 static char name
[32];
113 strncpy(name
, basename(path
), sizeof(name
));
115 t
= strstr(name
, ".ko");
122 static int elf64_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
124 const char *secnames
;
129 e
= (Elf64_Ehdr
*) map
;
130 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
132 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
133 for (i
= 0; i
< e
->e_shnum
; i
++) {
134 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
135 *size
= sh
[i
].sh_size
;
136 *offset
= sh
[i
].sh_offset
;
144 static int elf32_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
146 const char *secnames
;
151 e
= (Elf32_Ehdr
*) map
;
152 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
154 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
155 for (i
= 0; i
< e
->e_shnum
; i
++) {
156 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
157 *size
= sh
[i
].sh_size
;
158 *offset
= sh
[i
].sh_offset
;
166 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
168 int clazz
= map
[EI_CLASS
];
170 if (clazz
== ELFCLASS32
)
171 return elf32_find_section(map
, section
, offset
, size
);
172 else if (clazz
== ELFCLASS64
)
173 return elf64_find_section(map
, section
, offset
, size
);
175 ERROR("unknown elf format %d\n", clazz
);
180 static struct module
*
181 alloc_module(const char *name
, const char *depends
, int size
)
186 m
= calloc_a(sizeof(*m
),
187 &_name
, strlen(name
) + 1,
188 &_dep
, depends
? strlen(depends
) + 2 : 0);
192 m
->avl
.key
= m
->name
= strcpy(_name
, name
);
196 m
->depends
= strcpy(_dep
, depends
);
205 avl_insert(&modules
, &m
->avl
);
210 static int scan_loaded_modules(void)
216 fp
= fopen("/proc/modules", "r");
218 ERROR("failed to open /proc/modules\n");
222 while (getline(&buf
, &buf_len
, fp
) > 0) {
226 m
.name
= strtok(buf
, " ");
227 m
.size
= atoi(strtok(NULL
, " "));
228 m
.usage
= atoi(strtok(NULL
, " "));
229 m
.depends
= strtok(NULL
, " ");
231 if (!m
.name
|| !m
.depends
)
234 n
= alloc_module(m
.name
, m
.depends
, m
.size
);
244 static struct module
* get_module_info(const char *module
, const char *name
)
246 int fd
= open(module
, O_RDONLY
);
247 unsigned int offset
, size
;
248 char *map
, *strings
, *dep
= NULL
;
253 ERROR("failed to open %s\n", module
);
257 if (fstat(fd
, &s
) == -1) {
258 ERROR("failed to stat %s\n", module
);
262 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
263 if (map
== MAP_FAILED
) {
264 ERROR("failed to mmap %s\n", module
);
268 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
269 ERROR("failed to load the .modinfo section from %s\n", module
);
273 strings
= map
+ offset
;
274 while (strings
&& (strings
< map
+ offset
+ size
)) {
280 sep
= strstr(strings
, "=");
285 if (!strncmp(strings
, "depends=", len
+ 1))
287 strings
= &sep
[strlen(sep
)];
290 m
= alloc_module(name
, dep
, s
.st_size
);
299 static int scan_module_folder(void)
301 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
308 path
= alloca(sizeof(DEF_MOD_PATH
"*.ko") + strlen(prefix
) + strlen(ver
.release
) + 1);
309 sprintf(path
, "%s" DEF_MOD_PATH
"*.ko", prefix
, ver
.release
);
311 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
314 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
315 char *name
= get_module_name(gl
.gl_pathv
[j
]);
321 m
= find_module(name
);
323 get_module_info(gl
.gl_pathv
[j
], name
);
331 static int print_modinfo(char *module
)
333 int fd
= open(module
, O_RDONLY
);
334 unsigned int offset
, size
;
339 ERROR("failed to open %s\n", module
);
343 if (fstat(fd
, &s
) == -1) {
344 ERROR("failed to stat %s\n", module
);
348 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
349 if (map
== MAP_FAILED
) {
350 ERROR("failed to mmap %s\n", module
);
354 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
355 ERROR("failed to load the .modinfo section from %s\n", module
);
359 strings
= map
+ offset
;
360 printf("module:\t\t%s\n", module
);
361 while (strings
&& (strings
< map
+ offset
+ size
)) {
367 sep
= strstr(strings
, "=");
370 dup
= strndup(strings
, sep
- strings
);
372 if (strncmp(strings
, "parm", 4)) {
374 printf("%s:\t\t%s\n", dup
, sep
);
376 printf("%s:\t%s\n", dup
, sep
);
378 strings
= &sep
[strlen(sep
)];
386 static int deps_available(struct module
*m
, int verbose
)
391 if (!strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
397 m
= find_module(dep
);
400 ERROR("missing dependency %s\n", dep
);
401 if (verbose
&& m
&& (m
->state
!= LOADED
))
402 ERROR("dependency not loaded %s\n", dep
);
403 if (!m
|| (m
->state
!= LOADED
))
405 dep
+= strlen(dep
) + 1;
411 static int insert_module(char *path
, const char *options
)
417 if (stat(path
, &s
)) {
418 ERROR("missing module %s\n", path
);
422 fd
= open(path
, O_RDONLY
);
424 ERROR("cannot open %s\n", path
);
428 data
= malloc(s
.st_size
);
429 if (read(fd
, data
, s
.st_size
) == s
.st_size
)
430 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
432 ERROR("failed to read full module %s\n", path
);
440 static void load_moddeps(struct module
*_m
)
445 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
451 m
= find_module(dep
);
454 ERROR("failed to find dependency %s\n", dep
);
455 if (m
&& (m
->state
!= LOADED
)) {
460 dep
= dep
+ strlen(dep
) + 1;
464 static int iterations
= 0;
465 static int load_modprobe(void)
470 avl_for_each_element(&modules
, m
, avl
)
471 if (m
->state
== PROBE
)
477 avl_for_each_element(&modules
, m
, avl
) {
478 if ((m
->state
== PROBE
) && (!deps_available(m
, 0))) {
479 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
488 if ((m
->state
== PROBE
) || m
->error
)
497 static int print_insmod_usage(void)
499 INFO("Usage:\n\tinsmod filename [args]\n");
504 static int print_usage(char *arg
)
506 INFO("Usage:\n\t%s module\n", arg
);
511 static int main_insmod(int argc
, char **argv
)
513 char *name
, *cur
, *options
;
517 return print_insmod_usage();
519 name
= get_module_name(argv
[1]);
521 ERROR("cannot find module - %s\n", argv
[1]);
525 if (scan_loaded_modules())
528 if (find_module(name
)) {
529 ERROR("module is already loaded - %s\n", name
);
536 for (len
= 0, i
= 2; i
< argc
; i
++)
537 len
+= strlen(argv
[i
]) + 1;
539 options
= malloc(len
);
542 for (i
= 2; i
< argc
; i
++) {
547 cur
+= sprintf(cur
, "%s", argv
[i
]);
550 if (get_module_path(argv
[1])) {
552 } else if (!get_module_path(name
)) {
553 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
557 ret
= insert_module(get_module_path(name
), options
);
561 ERROR("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 ERROR("module is not loaded\n");
584 ret
= syscall(__NR_delete_module
, m
->name
, 0);
587 ERROR("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
)
618 return print_usage("modinfo");
620 if (scan_module_folder())
623 name
= get_module_name(argv
[1]);
624 m
= find_module(name
);
626 ERROR("cannot find module - %s\n", argv
[1]);
630 name
= get_module_path(m
->name
);
632 ERROR("cannot find path of module - %s\n", m
->name
);
641 static int main_modprobe(int argc
, char **argv
)
647 return print_usage("modprobe");
649 if (scan_loaded_modules())
652 if (scan_module_folder())
655 name
= get_module_name(argv
[1]);
656 m
= find_module(name
);
657 if (m
&& m
->state
== LOADED
) {
658 ERROR("%s is already loaded\n", name
);
661 ERROR("failed to find a module named %s\n", name
);
667 fail
= load_modprobe();
670 ERROR("%d module%s could not be probed\n",
671 fail
, (fail
== 1) ? ("") : ("s"));
673 avl_for_each_element(&modules
, m
, avl
)
674 if ((m
->state
== PROBE
) || m
->error
)
675 ERROR("- %s\n", m
->name
);
684 static int main_loader(int argc
, char **argv
)
686 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
687 char *dir
= "/etc/modules.d/*";
699 path
= malloc(strlen(dir
) + 2);
703 if (scan_loaded_modules())
706 if (scan_module_folder())
709 syslog(0, "kmodloader: loading kernel modules from %s\n", path
);
711 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
714 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
715 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
720 ERROR("failed to open %s\n", gl
.gl_pathv
[j
]);
724 while (getline(&mod
, &mod_len
, fp
) > 0) {
725 char *nl
= strchr(mod
, '\n');
732 opts
= strchr(mod
, ' ');
736 m
= find_module(get_module_name(mod
));
737 if (!m
|| (m
->state
== LOADED
))
741 m
->opts
= strdup(opts
);
743 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
751 fail
= load_modprobe();
752 DEBUG("ran %d iterations\n", iterations
);
755 ERROR("%d module%s could not be probed\n",
756 fail
, (fail
== 1) ? ("") : ("s"));
758 avl_for_each_element(&modules
, m
, avl
)
759 if ((m
->state
== PROBE
) || (m
->error
))
760 ERROR("- %s - %d\n", m
->name
, deps_available(m
, 1));
770 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
775 while (*s1
&& ((*s1
== *s2
) ||
776 ((*s1
== '_') && (*s2
== '-')) ||
777 ((*s1
== '-') && (*s2
== '_'))))
783 return *(const unsigned char *)s1
- *(const unsigned char *)s2
;
786 int main(int argc
, char **argv
)
788 char *exec
= basename(*argv
);
790 avl_init(&modules
, avl_modcmp
, false, NULL
);
791 if (!strcmp(exec
, "insmod"))
792 return main_insmod(argc
, argv
);
794 if (!strcmp(exec
, "rmmod"))
795 return main_rmmod(argc
, argv
);
797 if (!strcmp(exec
, "lsmod"))
798 return main_lsmod(argc
, argv
);
800 if (!strcmp(exec
, "modinfo"))
801 return main_modinfo(argc
, argv
);
803 if (!strcmp(exec
, "modprobe"))
804 return main_modprobe(argc
, argv
);
806 return main_loader(argc
, argv
);