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, 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(name)) {
544 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
548 ret = insert_module(get_module_path(name), options);
552 LOG("failed to insert %s\n", get_module_path(name));
557 static int main_rmmod(int argc, char **argv)
564 return print_usage("rmmod");
566 if (scan_loaded_modules())
569 name = get_module_name(argv[1]);
570 m = find_module(name);
572 LOG("module is not loaded\n");
575 ret = syscall(__NR_delete_module, m->name, 0);
578 LOG("unloading the module failed\n");
585 static int main_lsmod(int argc, char **argv)
589 if (scan_loaded_modules())
592 avl_for_each_element(&modules, m, avl)
593 if (m->state == LOADED)
594 printf("%-20s%8d%3d %s\n",
595 m->name, m->size, m->usage,
596 (*m->depends == '-') ? ("") : (m->depends));
603 static int main_modinfo(int argc, char **argv)
609 return print_usage("modinfo");
611 if (scan_module_folder())
614 name = get_module_name(argv[1]);
615 m = find_module(name);
617 LOG("cannot find module - %s\n", argv[1]);
621 name = get_module_path(m->name);
623 LOG("cannot find path of module - %s\n", m->name);
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 m->opts = strdup(opts);
734 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
742 fail = load_modprobe();
743 LOG("ran %d iterations\n", iterations);
746 LOG("%d module%s could not be probed\n",
747 fail, (fail == 1) ? ("") : ("s"));
749 avl_for_each_element(&modules, m, avl)
750 if ((m->state == PROBE) || (m->error))
751 LOG("- %s - %d\n", m->name, deps_available(m, 1));
761 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
766 while (*s1 && ((*s1 == *s2) ||
767 ((*s1 == '_') && (*s2 == '-')) ||
768 ((*s1 == '-') && (*s2 == '_'))))
774 return *(const unsigned char *)s1 - *(const unsigned char *)s2;
777 int main(int argc, char **argv)
779 char *exec = basename(*argv);
781 avl_init(&modules, avl_modcmp, false, NULL);
782 if (!strcmp(exec, "insmod"))
783 return main_insmod(argc, argv);
785 if (!strcmp(exec, "rmmod"))
786 return main_rmmod(argc, argv);
788 if (!strcmp(exec, "lsmod"))
789 return main_lsmod(argc, argv);
791 if (!strcmp(exec, "modinfo"))
792 return main_modinfo(argc, argv);
794 if (!strcmp(exec, "modprobe"))
795 return main_modprobe(argc, argv);
797 return main_loader(argc, argv);