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");
114 static int elf64_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
116 const char *secnames;
121 e = (Elf64_Ehdr *) map;
122 sh = (Elf64_Shdr *) (map + e->e_shoff);
124 secnames = map + sh[e->e_shstrndx].sh_offset;
125 for (i = 0; i < e->e_shnum; i++) {
126 if (!strcmp(section, secnames + sh[i].sh_name)) {
127 *size = sh[i].sh_size;
128 *offset = sh[i].sh_offset;
136 static int elf32_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
138 const char *secnames;
143 e = (Elf32_Ehdr *) map;
144 sh = (Elf32_Shdr *) (map + e->e_shoff);
146 secnames = map + sh[e->e_shstrndx].sh_offset;
147 for (i = 0; i < e->e_shnum; i++) {
148 if (!strcmp(section, secnames + sh[i].sh_name)) {
149 *size = sh[i].sh_size;
150 *offset = sh[i].sh_offset;
158 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
160 int clazz = map[EI_CLASS];
162 if (clazz == ELFCLASS32)
163 return elf32_find_section(map, section, offset, size);
164 else if (clazz == ELFCLASS64)
165 return elf64_find_section(map, section, offset, size);
167 LOG("unknown elf format %d\n", clazz);
172 static struct module *
173 alloc_module(const char *name, const char *depends, int size)
178 m = calloc_a(sizeof(*m),
179 &_name, strlen(name) + 1,
180 &_dep, depends ? strlen(depends) + 2 : 0);
184 m->avl.key = m->name = strcpy(_name, name);
187 m->depends = strcpy(_dep, depends);
196 avl_insert(&modules, &m->avl);
201 static int scan_loaded_modules(void)
207 fp = fopen("/proc/modules", "r");
209 LOG("failed to open /proc/modules\n");
213 while (getline(&buf, &buf_len, fp) > 0) {
217 m.name = strtok(buf, " ");
218 m.size = atoi(strtok(NULL, " "));
219 m.usage = atoi(strtok(NULL, " "));
220 m.depends = strtok(NULL, " ");
222 if (!m.name || !m.depends)
225 n = alloc_module(m.name, m.depends, m.size);
235 static struct module* get_module_info(const char *module, const char *name)
237 int fd = open(module, O_RDONLY);
238 unsigned int offset, size;
239 char *map, *strings, *dep = NULL;
244 LOG("failed to open %s\n", module);
248 if (fstat(fd, &s) == -1) {
249 LOG("failed to stat %s\n", module);
253 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
254 if (map == MAP_FAILED) {
255 LOG("failed to mmap %s\n", module);
259 if (elf_find_section(map, ".modinfo", &offset, &size)) {
260 LOG("failed to load the .modinfo section from %s\n", module);
264 strings = map + offset;
265 while (strings && (strings < map + offset + size)) {
271 sep = strstr(strings, "=");
276 if (!strncmp(strings, "depends=", len + 1))
278 strings = &sep[strlen(sep)];
281 m = alloc_module(name, dep, s.st_size);
290 static int scan_module_folder(void)
292 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
299 path = alloca(sizeof(DEF_MOD_PATH "*.ko") + strlen(prefix) + strlen(ver.release) + 1);
300 sprintf(path, "%s" DEF_MOD_PATH "*.ko", prefix, ver.release);
302 if (glob(path, gl_flags, NULL, &gl) < 0)
305 for (j = 0; j < gl.gl_pathc; j++) {
306 char *name = get_module_name(gl.gl_pathv[j]);
312 m = find_module(name);
314 get_module_info(gl.gl_pathv[j], name);
322 static int print_modinfo(char *module)
324 int fd = open(module, O_RDONLY);
325 unsigned int offset, size;
330 LOG("failed to open %s\n", module);
334 if (fstat(fd, &s) == -1) {
335 LOG("failed to stat %s\n", module);
339 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
340 if (map == MAP_FAILED) {
341 LOG("failed to mmap %s\n", module);
345 if (elf_find_section(map, ".modinfo", &offset, &size)) {
346 LOG("failed to load the .modinfo section from %s\n", module);
350 strings = map + offset;
351 printf("module:\t\t%s\n", module);
352 while (strings && (strings < map + offset + size)) {
358 sep = strstr(strings, "=");
361 dup = strndup(strings, sep - strings);
363 if (strncmp(strings, "parm", 4)) {
365 printf("%s:\t\t%s\n", dup, sep);
367 printf("%s:\t%s\n", dup, sep);
369 strings = &sep[strlen(sep)];
377 static int deps_available(struct module *m, int verbose)
382 if (!strcmp(m->depends, "-") || !strcmp(m->depends, ""))
388 m = find_module(dep);
391 LOG("missing dependency %s\n", dep);
392 if (verbose && m && (m->state != LOADED))
393 LOG("dependency not loaded %s\n", dep);
394 if (!m || (m->state != LOADED))
396 dep += strlen(dep) + 1;
402 static int insert_module(char *path, const char *options)
408 if (stat(path, &s)) {
409 LOG("missing module %s\n", path);
413 fd = open(path, O_RDONLY);
415 LOG("cannot open %s\n", path);
419 data = malloc(s.st_size);
420 if (read(fd, data, s.st_size) == s.st_size)
421 ret = syscall(__NR_init_module, data, s.st_size, options);
423 LOG("failed to read full module %s\n", path);
431 static void load_moddeps(struct module *_m)
436 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
442 m = find_module(dep);
445 LOG("failed to find dependency %s\n", dep);
446 if (m && (m->state != LOADED)) {
451 dep = dep + strlen(dep) + 1;
455 static int iterations = 0;
456 static int load_modprobe(void)
461 avl_for_each_element(&modules, m, avl)
462 if (m->state == PROBE)
468 avl_for_each_element(&modules, m, avl) {
469 if ((m->state == PROBE) && (!deps_available(m, 0))) {
470 if (!insert_module(get_module_path(m->name), "")) {
479 if ((m->state == PROBE) || m->error)
488 static int print_insmod_usage(void)
490 LOG("Usage:\n\tinsmod filename [args]\n");
495 static int print_usage(char *arg)
497 LOG("Usage:\n\t%s module\n", arg);
502 static int main_insmod(int argc, char **argv)
504 char *name, *cur, *options;
508 return print_insmod_usage();
510 name = get_module_name(argv[1]);
512 LOG("cannot find module - %s\n", argv[1]);
516 if (scan_loaded_modules())
519 if (find_module(name)) {
520 LOG("module is already loaded - %s\n", name);
527 for (len = 0, i = 2; i < argc; i++)
528 len += strlen(argv[i]) + 1;
530 options = malloc(len);
533 for (i = 2; i < argc; i++) {
538 cur += sprintf(cur, "%s", argv[i]);
541 if (!get_module_path(name)) {
542 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
546 ret = insert_module(get_module_path(name), options);
550 LOG("failed to insert %s\n", get_module_path(name));
555 static int main_rmmod(int argc, char **argv)
562 return print_usage("rmmod");
564 if (scan_loaded_modules())
567 name = get_module_name(argv[1]);
568 m = find_module(name);
570 LOG("module is not loaded\n");
573 ret = syscall(__NR_delete_module, m->name, 0);
576 LOG("unloading the module failed\n");
583 static int main_lsmod(int argc, char **argv)
587 if (scan_loaded_modules())
590 avl_for_each_element(&modules, m, avl)
591 if (m->state == LOADED)
592 printf("%-20s%8d%3d %s\n",
593 m->name, m->size, m->usage,
594 (*m->depends == '-') ? ("") : (m->depends));
601 static int main_modinfo(int argc, char **argv)
607 return print_usage("modinfo");
609 if (scan_module_folder())
612 name = get_module_name(argv[1]);
613 m = find_module(name);
615 LOG("cannot find module - %s\n", argv[1]);
619 name = get_module_path(m->name);
621 LOG("cannot find path of module - %s\n", m->name);
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 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
762 while (*s1 && ((*s1 == *s2) ||
763 ((*s1 == '_') && (*s2 == '-')) ||
764 ((*s1 == '-') && (*s2 == '_'))))
770 return *(const unsigned char *)s1 - *(const unsigned char *)s2;
773 int main(int argc, char **argv)
775 char *exec = basename(*argv);
777 avl_init(&modules, avl_modcmp, false, NULL);
778 if (!strcmp(exec, "insmod"))
779 return main_insmod(argc, argv);
781 if (!strcmp(exec, "rmmod"))
782 return main_rmmod(argc, argv);
784 if (!strcmp(exec, "lsmod"))
785 return main_lsmod(argc, argv);
787 if (!strcmp(exec, "modinfo"))
788 return main_modinfo(argc, argv);
790 if (!strcmp(exec, "modprobe"))
791 return main_modprobe(argc, argv);
793 return main_loader(argc, argv);