633570fd5b84bfe2b8cc1fb0b87fcc21ebc24c4d
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");
115 static int elf64_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 elf32_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
;
159 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
161 int clazz
= map
[EI_CLASS
];
163 if (clazz
== ELFCLASS32
)
164 return elf32_find_section(map
, section
, offset
, size
);
165 else if (clazz
== ELFCLASS64
)
166 return elf64_find_section(map
, section
, offset
, size
);
168 LOG("unknown elf format %d\n", clazz
);
173 static struct module
*
174 alloc_module(const char *name
, const char *depends
, int size
)
179 m
= calloc_a(sizeof(*m
),
180 &_name
, strlen(name
) + 1,
181 &_dep
, depends
? strlen(depends
) + 2 : 0);
185 m
->avl
.key
= m
->name
= strcpy(_name
, name
);
189 m
->depends
= strcpy(_dep
, depends
);
198 avl_insert(&modules
, &m
->avl
);
203 static int scan_loaded_modules(void)
209 fp
= fopen("/proc/modules", "r");
211 LOG("failed to open /proc/modules\n");
215 while (getline(&buf
, &buf_len
, fp
) > 0) {
219 m
.name
= strtok(buf
, " ");
220 m
.size
= atoi(strtok(NULL
, " "));
221 m
.usage
= atoi(strtok(NULL
, " "));
222 m
.depends
= strtok(NULL
, " ");
224 if (!m
.name
|| !m
.depends
)
227 n
= alloc_module(m
.name
, m
.depends
, m
.size
);
237 static struct module
* get_module_info(const char *module
, const char *name
)
239 int fd
= open(module
, O_RDONLY
);
240 unsigned int offset
, size
;
241 char *map
, *strings
, *dep
= NULL
;
246 LOG("failed to open %s\n", module
);
250 if (fstat(fd
, &s
) == -1) {
251 LOG("failed to stat %s\n", module
);
255 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
256 if (map
== MAP_FAILED
) {
257 LOG("failed to mmap %s\n", module
);
261 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
262 LOG("failed to load the .modinfo section from %s\n", module
);
266 strings
= map
+ offset
;
267 while (strings
&& (strings
< map
+ offset
+ size
)) {
273 sep
= strstr(strings
, "=");
278 if (!strncmp(strings
, "depends=", len
+ 1))
280 strings
= &sep
[strlen(sep
)];
283 m
= alloc_module(name
, dep
, s
.st_size
);
292 static int scan_module_folder(void)
294 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
301 path
= alloca(sizeof(DEF_MOD_PATH
"*.ko") + strlen(prefix
) + strlen(ver
.release
) + 1);
302 sprintf(path
, "%s" DEF_MOD_PATH
"*.ko", prefix
, ver
.release
);
304 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
307 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
308 char *name
= get_module_name(gl
.gl_pathv
[j
]);
314 m
= find_module(name
);
316 get_module_info(gl
.gl_pathv
[j
], name
);
324 static int print_modinfo(char *module
)
326 int fd
= open(module
, O_RDONLY
);
327 unsigned int offset
, size
;
332 LOG("failed to open %s\n", module
);
336 if (fstat(fd
, &s
) == -1) {
337 LOG("failed to stat %s\n", module
);
341 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
342 if (map
== MAP_FAILED
) {
343 LOG("failed to mmap %s\n", module
);
347 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
348 LOG("failed to load the .modinfo section from %s\n", module
);
352 strings
= map
+ offset
;
353 printf("module:\t\t%s\n", module
);
354 while (strings
&& (strings
< map
+ offset
+ size
)) {
360 sep
= strstr(strings
, "=");
363 dup
= strndup(strings
, sep
- strings
);
365 if (strncmp(strings
, "parm", 4)) {
367 printf("%s:\t\t%s\n", dup
, sep
);
369 printf("%s:\t%s\n", dup
, sep
);
371 strings
= &sep
[strlen(sep
)];
379 static int deps_available(struct module
*m
, int verbose
)
384 if (!strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
390 m
= find_module(dep
);
393 LOG("missing dependency %s\n", dep
);
394 if (verbose
&& m
&& (m
->state
!= LOADED
))
395 LOG("dependency not loaded %s\n", dep
);
396 if (!m
|| (m
->state
!= LOADED
))
398 dep
+= strlen(dep
) + 1;
404 static int insert_module(char *path
, const char *options
)
410 if (stat(path
, &s
)) {
411 LOG("missing module %s\n", path
);
415 fd
= open(path
, O_RDONLY
);
417 LOG("cannot open %s\n", path
);
421 data
= malloc(s
.st_size
);
422 if (read(fd
, data
, s
.st_size
) == s
.st_size
)
423 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
425 LOG("failed to read full module %s\n", path
);
433 static void load_moddeps(struct module
*_m
)
438 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
444 m
= find_module(dep
);
447 LOG("failed to find dependency %s\n", dep
);
448 if (m
&& (m
->state
!= LOADED
)) {
453 dep
= dep
+ strlen(dep
) + 1;
457 static int iterations
= 0;
458 static int load_modprobe(void)
463 avl_for_each_element(&modules
, m
, avl
)
464 if (m
->state
== PROBE
)
470 avl_for_each_element(&modules
, m
, avl
) {
471 if ((m
->state
== PROBE
) && (!deps_available(m
, 0))) {
472 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
481 if ((m
->state
== PROBE
) || m
->error
)
490 static int print_insmod_usage(void)
492 LOG("Usage:\n\tinsmod filename [args]\n");
497 static int print_usage(char *arg
)
499 LOG("Usage:\n\t%s module\n", arg
);
504 static int main_insmod(int argc
, char **argv
)
506 char *name
, *cur
, *options
;
510 return print_insmod_usage();
512 name
= get_module_name(argv
[1]);
514 LOG("cannot find module - %s\n", argv
[1]);
518 if (scan_loaded_modules())
521 if (find_module(name
)) {
522 LOG("module is already loaded - %s\n", name
);
529 for (len
= 0, i
= 2; i
< argc
; i
++)
530 len
+= strlen(argv
[i
]) + 1;
532 options
= malloc(len
);
535 for (i
= 2; i
< argc
; i
++) {
540 cur
+= sprintf(cur
, "%s", argv
[i
]);
543 if (get_module_path(argv
[1])) {
545 } else if (!get_module_path(name
)) {
546 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
550 ret
= insert_module(get_module_path(name
), options
);
554 LOG("failed to insert %s\n", get_module_path(name
));
559 static int main_rmmod(int argc
, char **argv
)
566 return print_usage("rmmod");
568 if (scan_loaded_modules())
571 name
= get_module_name(argv
[1]);
572 m
= find_module(name
);
574 LOG("module is not loaded\n");
577 ret
= syscall(__NR_delete_module
, m
->name
, 0);
580 LOG("unloading the module failed\n");
587 static int main_lsmod(int argc
, char **argv
)
591 if (scan_loaded_modules())
594 avl_for_each_element(&modules
, m
, avl
)
595 if (m
->state
== LOADED
)
596 printf("%-20s%8d%3d %s\n",
597 m
->name
, m
->size
, m
->usage
,
598 (*m
->depends
== '-') ? ("") : (m
->depends
));
605 static int main_modinfo(int argc
, char **argv
)
611 return print_usage("modinfo");
613 if (scan_module_folder())
616 name
= get_module_name(argv
[1]);
617 m
= find_module(name
);
619 LOG("cannot find module - %s\n", argv
[1]);
623 name
= get_module_path(m
->name
);
625 LOG("cannot find path of module - %s\n", m
->name
);
634 static int main_modprobe(int argc
, char **argv
)
640 return print_usage("modprobe");
642 if (scan_loaded_modules())
645 if (scan_module_folder())
648 name
= get_module_name(argv
[1]);
649 m
= find_module(name
);
650 if (m
&& m
->state
== LOADED
) {
651 LOG("%s is already loaded\n", name
);
654 LOG("failed to find a module named %s\n", name
);
660 fail
= load_modprobe();
663 LOG("%d module%s could not be probed\n",
664 fail
, (fail
== 1) ? ("") : ("s"));
666 avl_for_each_element(&modules
, m
, avl
)
667 if ((m
->state
== PROBE
) || m
->error
)
668 LOG("- %s\n", m
->name
);
677 static int main_loader(int argc
, char **argv
)
679 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
680 char *dir
= "/etc/modules.d/*";
692 path
= malloc(strlen(dir
) + 2);
696 if (scan_loaded_modules())
699 if (scan_module_folder())
702 syslog(0, "kmodloader: loading kernel modules from %s\n", path
);
704 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
707 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
708 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
713 LOG("failed to open %s\n", gl
.gl_pathv
[j
]);
717 while (getline(&mod
, &mod_len
, fp
) > 0) {
718 char *nl
= strchr(mod
, '\n');
725 opts
= strchr(mod
, ' ');
729 m
= find_module(get_module_name(mod
));
730 if (!m
|| (m
->state
== LOADED
))
734 m
->opts
= strdup(opts
);
736 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
744 fail
= load_modprobe();
745 LOG("ran %d iterations\n", iterations
);
748 LOG("%d module%s could not be probed\n",
749 fail
, (fail
== 1) ? ("") : ("s"));
751 avl_for_each_element(&modules
, m
, avl
)
752 if ((m
->state
== PROBE
) || (m
->error
))
753 LOG("- %s - %d\n", m
->name
, deps_available(m
, 1));
763 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
768 while (*s1
&& ((*s1
== *s2
) ||
769 ((*s1
== '_') && (*s2
== '-')) ||
770 ((*s1
== '-') && (*s2
== '_'))))
776 return *(const unsigned char *)s1
- *(const unsigned char *)s2
;
779 int main(int argc
, char **argv
)
781 char *exec
= basename(*argv
);
783 avl_init(&modules
, avl_modcmp
, false, NULL
);
784 if (!strcmp(exec
, "insmod"))
785 return main_insmod(argc
, argv
);
787 if (!strcmp(exec
, "rmmod"))
788 return main_rmmod(argc
, argv
);
790 if (!strcmp(exec
, "lsmod"))
791 return main_lsmod(argc
, argv
);
793 if (!strcmp(exec
, "modinfo"))
794 return main_modinfo(argc
, argv
);
796 if (!strcmp(exec
, "modprobe"))
797 return main_modprobe(argc
, argv
);
799 return main_loader(argc
, argv
);