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>
34 #include <libubox/avl.h>
35 #include <libubox/avl-cmp.h>
36 #include <libubox/utils.h>
37 #include <libubox/ulog.h>
39 #define DEF_MOD_PATH "/modules/%s/"
60 static struct avl_tree modules;
62 static char **module_folders = NULL;
64 static int init_module_folders(void)
69 char *s, *e, *p, path[256], ldpath[256];
72 s = getenv("LD_LIBRARY_PATH");
75 e += snprintf(ldpath, sizeof(ldpath), "%s:", s);
77 e += snprintf(e, sizeof(ldpath) - (e - ldpath), "/lib");
81 for (s = p = ldpath; p <= e; p++) {
82 if (*p != ':' && *p != '\0')
86 snprintf(path, sizeof(path), "%s" DEF_MOD_PATH, s, ver.release);
88 if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
89 module_folders = realloc(module_folders, sizeof(p) * (n + 2));
94 module_folders[n++] = strdup(path);
103 module_folders[n] = NULL;
107 static struct module *find_module(const char *name)
110 return avl_find_element(&modules, name, m, avl);
113 static void free_modules(void)
115 struct module *m, *tmp;
117 avl_remove_all_elements(&modules, m, avl, tmp)
121 static char* get_module_path(char *name)
124 static char path[256];
127 if (!stat(name, &s) && S_ISREG(s.st_mode))
130 for (p = module_folders; *p; p++) {
131 snprintf(path, sizeof(path), "%s%s.ko", *p, name);
132 if (!stat(path, &s) && S_ISREG(s.st_mode))
139 static char* get_module_name(char *path)
141 static char name[32];
144 strncpy(name, basename(path), sizeof(name));
146 t = strstr(name, ".ko");
153 static int elf64_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
155 const char *secnames;
160 e = (Elf64_Ehdr *) map;
161 sh = (Elf64_Shdr *) (map + e->e_shoff);
163 secnames = map + sh[e->e_shstrndx].sh_offset;
164 for (i = 0; i < e->e_shnum; i++) {
165 if (!strcmp(section, secnames + sh[i].sh_name)) {
166 *size = sh[i].sh_size;
167 *offset = sh[i].sh_offset;
175 static int elf32_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
177 const char *secnames;
182 e = (Elf32_Ehdr *) map;
183 sh = (Elf32_Shdr *) (map + e->e_shoff);
185 secnames = map + sh[e->e_shstrndx].sh_offset;
186 for (i = 0; i < e->e_shnum; i++) {
187 if (!strcmp(section, secnames + sh[i].sh_name)) {
188 *size = sh[i].sh_size;
189 *offset = sh[i].sh_offset;
197 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
199 int clazz = map[EI_CLASS];
201 if (clazz == ELFCLASS32)
202 return elf32_find_section(map, section, offset, size);
203 else if (clazz == ELFCLASS64)
204 return elf64_find_section(map, section, offset, size);
206 ULOG_ERR("unknown elf format %d\n", clazz);
211 static struct module *
212 alloc_module(const char *name, const char *depends, int size)
217 m = calloc_a(sizeof(*m),
218 &_name, strlen(name) + 1,
219 &_dep, depends ? strlen(depends) + 2 : 0);
223 m->avl.key = m->name = strcpy(_name, name);
227 m->depends = strcpy(_dep, depends);
236 avl_insert(&modules, &m->avl);
241 static int scan_loaded_modules(void)
247 fp = fopen("/proc/modules", "r");
249 ULOG_ERR("failed to open /proc/modules\n");
253 while (getline(&buf, &buf_len, fp) > 0) {
257 m.name = strtok(buf, " ");
258 m.size = atoi(strtok(NULL, " "));
259 m.usage = atoi(strtok(NULL, " "));
260 m.depends = strtok(NULL, " ");
262 if (!m.name || !m.depends)
265 n = alloc_module(m.name, m.depends, m.size);
275 static struct module* get_module_info(const char *module, const char *name)
277 int fd = open(module, O_RDONLY);
278 unsigned int offset, size;
279 char *map = MAP_FAILED, *strings, *dep = NULL;
280 struct module *m = NULL;
284 ULOG_ERR("failed to open %s\n", module);
288 if (fstat(fd, &s) == -1) {
289 ULOG_ERR("failed to stat %s\n", module);
293 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
294 if (map == MAP_FAILED) {
295 ULOG_ERR("failed to mmap %s\n", module);
299 if (elf_find_section(map, ".modinfo", &offset, &size)) {
300 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
304 strings = map + offset;
305 while (strings && (strings < map + offset + size)) {
311 sep = strstr(strings, "=");
316 if (!strncmp(strings, "depends=", len + 1))
318 strings = &sep[strlen(sep)];
321 m = alloc_module(name, dep, s.st_size);
327 if (map != MAP_FAILED)
328 munmap(map, s.st_size);
336 static int scan_module_folder(const char *dir)
338 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
345 path = alloca(strlen(dir) + sizeof("*.ko") + 1);
346 sprintf(path, "%s*.ko", dir);
348 if (glob(path, gl_flags, NULL, &gl) < 0)
351 for (j = 0; j < gl.gl_pathc; j++) {
352 char *name = get_module_name(gl.gl_pathv[j]);
358 m = find_module(name);
360 get_module_info(gl.gl_pathv[j], name);
368 static int scan_module_folders(void)
373 if (init_module_folders())
376 for (p = module_folders; *p; p++)
377 rv |= scan_module_folder(*p);
382 static int print_modinfo(char *module)
384 int fd = open(module, O_RDONLY);
385 unsigned int offset, size;
387 char *map = MAP_FAILED, *strings;
391 ULOG_ERR("failed to open %s\n", module);
395 if (fstat(fd, &s) == -1) {
396 ULOG_ERR("failed to stat %s\n", module);
400 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
401 if (map == MAP_FAILED) {
402 ULOG_ERR("failed to mmap %s\n", module);
406 if (elf_find_section(map, ".modinfo", &offset, &size)) {
407 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
411 strings = map + offset;
412 printf("module:\t\t%s\n", module);
413 while (strings && (strings < map + offset + size)) {
419 sep = strstr(strings, "=");
422 dup = strndup(strings, sep - strings);
424 if (strncmp(strings, "parm", 4)) {
426 printf("%s:\t\t%s\n", dup, sep);
428 printf("%s:\t%s\n", dup, sep);
430 strings = &sep[strlen(sep)];
438 if (map != MAP_FAILED)
439 munmap(map, s.st_size);
447 static int deps_available(struct module *m, int verbose)
452 if (!m->depends || !strcmp(m->depends, "-") || !strcmp(m->depends, ""))
458 m = find_module(dep);
461 ULOG_ERR("missing dependency %s\n", dep);
462 if (verbose && m && (m->state != LOADED))
463 ULOG_ERR("dependency not loaded %s\n", dep);
464 if (!m || (m->state != LOADED))
466 dep += strlen(dep) + 1;
472 static int insert_module(char *path, const char *options)
478 if (stat(path, &s)) {
479 ULOG_ERR("missing module %s\n", path);
483 fd = open(path, O_RDONLY);
485 ULOG_ERR("cannot open %s\n", path);
489 data = malloc(s.st_size);
490 if (read(fd, data, s.st_size) == s.st_size)
491 ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
493 ULOG_ERR("failed to read full module %s\n", path);
501 static void load_moddeps(struct module *_m)
506 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
512 m = find_module(dep);
515 ULOG_ERR("failed to find dependency %s\n", dep);
516 if (m && (m->state != LOADED)) {
521 dep = dep + strlen(dep) + 1;
525 static int iterations = 0;
526 static int load_modprobe(void)
531 avl_for_each_element(&modules, m, avl)
532 if (m->state == PROBE)
538 avl_for_each_element(&modules, m, avl) {
539 if ((m->state == PROBE) && (!deps_available(m, 0))) {
540 if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
549 if ((m->state == PROBE) || m->error)
558 static int print_insmod_usage(void)
560 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
565 static int print_usage(char *arg)
567 ULOG_INFO("Usage:\n\t%s module\n", arg);
572 static int main_insmod(int argc, char **argv)
574 char *name, *cur, *options;
578 return print_insmod_usage();
580 name = get_module_name(argv[1]);
582 ULOG_ERR("cannot find module - %s\n", argv[1]);
586 if (scan_loaded_modules())
589 if (find_module(name)) {
590 ULOG_ERR("module is already loaded - %s\n", name);
597 for (len = 0, i = 2; i < argc; i++)
598 len += strlen(argv[i]) + 1;
600 options = malloc(len);
603 for (i = 2; i < argc; i++) {
608 cur += sprintf(cur, "%s", argv[i]);
611 if (init_module_folders()) {
612 fprintf(stderr, "Failed to find the folder holding the modules\n");
616 if (get_module_path(argv[1])) {
618 } else if (!get_module_path(name)) {
619 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
623 ret = insert_module(get_module_path(name), options);
627 ULOG_ERR("failed to insert %s\n", get_module_path(name));
632 static int main_rmmod(int argc, char **argv)
639 return print_usage("rmmod");
641 if (scan_loaded_modules())
644 name = get_module_name(argv[1]);
645 m = find_module(name);
647 ULOG_ERR("module is not loaded\n");
650 ret = syscall(__NR_delete_module, m->name, 0);
653 ULOG_ERR("unloading the module failed\n");
660 static int main_lsmod(int argc, char **argv)
665 if (scan_loaded_modules())
668 avl_for_each_element(&modules, m, avl)
669 if (m->state == LOADED) {
670 printf("%-20s%8d%3d ",
671 m->name, m->size, m->usage);
672 if (m->depends && strcmp(m->depends, "-") && strcmp(m->depends, "")) {
676 dep = dep + strlen(dep) + 1;
689 static int main_modinfo(int argc, char **argv)
695 return print_usage("modinfo");
697 if (scan_module_folders())
700 name = get_module_name(argv[1]);
701 m = find_module(name);
703 ULOG_ERR("cannot find module - %s\n", argv[1]);
707 name = get_module_path(m->name);
709 ULOG_ERR("cannot find path of module - %s\n", m->name);
718 static int main_modprobe(int argc, char **argv)
724 return print_usage("modprobe");
726 if (scan_loaded_modules())
729 if (scan_module_folders())
732 name = get_module_name(argv[1]);
733 m = find_module(name);
734 if (m && m->state == LOADED) {
735 ULOG_ERR("%s is already loaded\n", name);
738 ULOG_ERR("failed to find a module named %s\n", name);
744 fail = load_modprobe();
747 ULOG_ERR("%d module%s could not be probed\n",
748 fail, (fail == 1) ? ("") : ("s"));
750 avl_for_each_element(&modules, m, avl)
751 if ((m->state == PROBE) || m->error)
752 ULOG_ERR("- %s\n", m->name);
761 static int main_loader(int argc, char **argv)
763 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
764 char *dir = "/etc/modules.d/";
773 path = malloc(strlen(dir) + 2);
777 if (scan_loaded_modules()) {
782 if (scan_module_folders()) {
787 ULOG_INFO("loading kernel modules from %s\n", path);
789 if (glob(path, gl_flags, NULL, &gl) < 0)
792 for (j = 0; j < gl.gl_pathc; j++) {
793 FILE *fp = fopen(gl.gl_pathv[j], "r");
798 ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
802 while (getline(&mod, &mod_len, fp) > 0) {
803 char *nl = strchr(mod, '\n');
810 opts = strchr(mod, ' ');
814 m = find_module(get_module_name(mod));
815 if (!m || (m->state == LOADED))
819 m->opts = strdup(opts);
821 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
829 fail = load_modprobe();
832 ULOG_ERR("%d module%s could not be probed\n",
833 fail, (fail == 1) ? ("") : ("s"));
835 avl_for_each_element(&modules, m, avl)
836 if ((m->state == PROBE) || (m->error))
837 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
839 ULOG_INFO("done loading kernel modules from %s\n", path);
849 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
854 while (*s1 && ((*s1 == *s2) ||
855 ((*s1 == '_') && (*s2 == '-')) ||
856 ((*s1 == '-') && (*s2 == '_'))))
862 return *(const unsigned char *)s1 - *(const unsigned char *)s2;
865 int main(int argc, char **argv)
867 char *exec = basename(*argv);
869 avl_init(&modules, avl_modcmp, false, NULL);
870 if (!strcmp(exec, "insmod"))
871 return main_insmod(argc, argv);
873 if (!strcmp(exec, "rmmod"))
874 return main_rmmod(argc, argv);
876 if (!strcmp(exec, "lsmod"))
877 return main_lsmod(argc, argv);
879 if (!strcmp(exec, "modinfo"))
880 return main_modinfo(argc, argv);
882 if (!strcmp(exec, "modprobe"))
883 return main_modprobe(argc, argv);
885 ulog_open(ULOG_KMSG, LOG_USER, "kmodloader");
886 return main_loader(argc, argv);