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/"
58 int refcnt; /* number of references from module_node.m */
67 static struct avl_tree modules;
69 static char **module_folders = NULL;
71 static void free_module(struct module *m);
73 static int init_module_folders(void)
78 char *s, *e, *p, path[256], ldpath[256];
81 s = getenv("LD_LIBRARY_PATH");
84 e += snprintf(ldpath, sizeof(ldpath), "%s:", s);
86 e += snprintf(e, sizeof(ldpath) - (e - ldpath), "/lib");
90 for (s = p = ldpath; p <= e; p++) {
91 if (*p != ':' && *p != '\0')
95 snprintf(path, sizeof(path), "%s" DEF_MOD_PATH, s, ver.release);
97 if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
98 module_folders = realloc(module_folders, sizeof(p) * (n + 2));
103 module_folders[n++] = strdup(path);
112 module_folders[n] = NULL;
116 static struct module *find_module(const char *name)
118 struct module_node *mn;
119 mn = avl_find_element(&modules, name, mn, avl);
126 static void free_modules(void)
128 struct module_node *mn, *tmp;
130 avl_remove_all_elements(&modules, mn, avl, tmp) {
131 struct module *m = mn->m;
140 static char* get_module_path(char *name)
143 static char path[256];
146 if (!stat(name, &s) && S_ISREG(s.st_mode))
149 for (p = module_folders; *p; p++) {
150 snprintf(path, sizeof(path), "%s%s.ko", *p, name);
151 if (!stat(path, &s) && S_ISREG(s.st_mode))
158 static char* get_module_name(char *path)
160 static char name[32];
163 strncpy(name, basename(path), sizeof(name));
165 t = strstr(name, ".ko");
172 static int elf64_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
174 const char *secnames;
179 e = (Elf64_Ehdr *) map;
180 sh = (Elf64_Shdr *) (map + e->e_shoff);
182 secnames = map + sh[e->e_shstrndx].sh_offset;
183 for (i = 0; i < e->e_shnum; i++) {
184 if (!strcmp(section, secnames + sh[i].sh_name)) {
185 *size = sh[i].sh_size;
186 *offset = sh[i].sh_offset;
194 static int elf32_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
196 const char *secnames;
201 e = (Elf32_Ehdr *) map;
202 sh = (Elf32_Shdr *) (map + e->e_shoff);
204 secnames = map + sh[e->e_shstrndx].sh_offset;
205 for (i = 0; i < e->e_shnum; i++) {
206 if (!strcmp(section, secnames + sh[i].sh_name)) {
207 *size = sh[i].sh_size;
208 *offset = sh[i].sh_offset;
216 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
218 int clazz = map[EI_CLASS];
220 if (clazz == ELFCLASS32)
221 return elf32_find_section(map, section, offset, size);
222 else if (clazz == ELFCLASS64)
223 return elf64_find_section(map, section, offset, size);
225 ULOG_ERR("unknown elf format %d\n", clazz);
230 static struct module_node *
231 alloc_module_node(const char *name, struct module *m, bool is_alias)
233 struct module_node *mn;
236 mn = calloc_a(sizeof(*mn),
237 &_name, strlen(name) + 1);
239 mn->avl.key = strcpy(_name, name);
241 mn->is_alias = is_alias;
242 avl_insert(&modules, &mn->avl);
248 static struct module *
249 alloc_module(const char *name, const char * const *aliases, int naliases, const char *depends, int size)
256 len_aliases = naliases * sizeof(aliases[0]);
257 for (i = 0; i < naliases; i++)
258 len_aliases += strlen(aliases[i]) + 1;
259 m = calloc_a(sizeof(*m),
260 &_name, strlen(name) + 1,
261 &_dep, depends ? strlen(depends) + 2 : 0,
262 &_aliases, len_aliases);
266 m->name = strcpy(_name, name);
270 m->depends = strcpy(_dep, depends);
278 m->naliases = naliases;
282 char *ptr = (char *)_aliases + naliases * sizeof(_aliases[0]);
287 len = strlen(aliases[i]) + 1;
288 memcpy(ptr, aliases[i], len);
292 } while (i < naliases);
293 m->aliases = _aliases;
297 alloc_module_node(m->name, m, false);
298 for (i = 0; i < m->naliases; i++)
299 alloc_module_node(m->aliases[i], m, true);
304 static void free_module(struct module *m)
311 static int scan_loaded_modules(void)
317 fp = fopen("/proc/modules", "r");
319 ULOG_ERR("failed to open /proc/modules\n");
323 while (getline(&buf, &buf_len, fp) > 0) {
327 m.name = strtok(buf, " ");
328 m.size = atoi(strtok(NULL, " "));
329 m.usage = atoi(strtok(NULL, " "));
330 m.depends = strtok(NULL, " ");
332 if (!m.name || !m.depends)
335 n = find_module(m.name);
337 /* possibly a module outside /lib/modules/<ver>/ */
338 n = alloc_module(m.name, NULL, 0, m.depends, m.size);
349 static struct module* get_module_info(const char *module, const char *name)
351 int fd = open(module, O_RDONLY);
352 unsigned int offset, size;
353 char *map = MAP_FAILED, *strings, *dep = NULL;
354 const char *aliases[32];
356 struct module *m = NULL;
360 ULOG_ERR("failed to open %s\n", module);
364 if (fstat(fd, &s) == -1) {
365 ULOG_ERR("failed to stat %s\n", module);
369 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
370 if (map == MAP_FAILED) {
371 ULOG_ERR("failed to mmap %s\n", module);
375 if (elf_find_section(map, ".modinfo", &offset, &size)) {
376 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
380 strings = map + offset;
387 if (strings >= map + offset + size)
389 sep = strstr(strings, "=");
394 if (!strncmp(strings, "depends=", len + 1))
396 else if (!strncmp(strings, "alias=", len + 1)) {
397 if (naliases < ARRAY_SIZE(aliases))
398 aliases[naliases++] = sep;
400 ULOG_WARN("module %s has more than %d aliases: truncated",
401 name, ARRAY_SIZE(aliases));
403 strings = &sep[strlen(sep)];
406 m = alloc_module(name, aliases, naliases, dep, s.st_size);
412 if (map != MAP_FAILED)
413 munmap(map, s.st_size);
421 static int scan_module_folder(const char *dir)
423 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
430 path = alloca(strlen(dir) + sizeof("*.ko") + 1);
431 sprintf(path, "%s*.ko", dir);
433 if (glob(path, gl_flags, NULL, &gl) < 0)
436 for (j = 0; j < gl.gl_pathc; j++) {
437 char *name = get_module_name(gl.gl_pathv[j]);
443 m = find_module(name);
445 get_module_info(gl.gl_pathv[j], name);
453 static int scan_module_folders(void)
458 if (init_module_folders())
461 for (p = module_folders; *p; p++)
462 rv |= scan_module_folder(*p);
467 static int print_modinfo(char *module)
469 int fd = open(module, O_RDONLY);
470 unsigned int offset, size;
472 char *map = MAP_FAILED, *strings;
476 ULOG_ERR("failed to open %s\n", module);
480 if (fstat(fd, &s) == -1) {
481 ULOG_ERR("failed to stat %s\n", module);
485 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
486 if (map == MAP_FAILED) {
487 ULOG_ERR("failed to mmap %s\n", module);
491 if (elf_find_section(map, ".modinfo", &offset, &size)) {
492 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
496 strings = map + offset;
497 printf("module:\t\t%s\n", module);
504 if (strings >= map + offset + size)
506 sep = strstr(strings, "=");
509 dup = strndup(strings, sep - strings);
511 if (strncmp(strings, "parm", 4)) {
513 printf("%s:\t\t%s\n", dup, sep);
515 printf("%s:\t%s\n", dup, sep);
517 strings = &sep[strlen(sep)];
525 if (map != MAP_FAILED)
526 munmap(map, s.st_size);
534 static int deps_available(struct module *m, int verbose)
539 if (!m->depends || !strcmp(m->depends, "-") || !strcmp(m->depends, ""))
545 m = find_module(dep);
548 ULOG_ERR("missing dependency %s\n", dep);
549 if (verbose && m && (m->state != LOADED))
550 ULOG_ERR("dependency not loaded %s\n", dep);
551 if (!m || (m->state != LOADED))
553 dep += strlen(dep) + 1;
559 static int insert_module(char *path, const char *options)
565 if (stat(path, &s)) {
566 ULOG_ERR("missing module %s\n", path);
570 fd = open(path, O_RDONLY);
572 ULOG_ERR("cannot open %s\n", path);
576 data = malloc(s.st_size);
577 if (read(fd, data, s.st_size) == s.st_size) {
578 ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
583 ULOG_ERR("failed to read full module %s\n", path);
591 static void load_moddeps(struct module *_m)
596 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
602 m = find_module(dep);
605 ULOG_ERR("failed to find dependency %s\n", dep);
606 if (m && (m->state != LOADED)) {
611 dep = dep + strlen(dep) + 1;
615 static int iterations = 0;
616 static int load_modprobe(void)
619 struct module_node *mn;
622 avl_for_each_element(&modules, mn, avl) {
626 if (m->state == PROBE)
633 avl_for_each_element(&modules, mn, avl) {
637 if ((m->state == PROBE) && (!deps_available(m, 0))) {
638 if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
647 if ((m->state == PROBE) || m->error)
656 static int print_insmod_usage(void)
658 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
663 static int print_usage(char *arg)
665 ULOG_INFO("Usage:\n\t%s module\n", arg);
670 static int main_insmod(int argc, char **argv)
672 char *name, *cur, *options;
676 return print_insmod_usage();
678 name = get_module_name(argv[1]);
680 ULOG_ERR("cannot find module - %s\n", argv[1]);
684 if (scan_loaded_modules())
687 if (find_module(name)) {
688 ULOG_ERR("module is already loaded - %s\n", name);
695 for (len = 0, i = 2; i < argc; i++)
696 len += strlen(argv[i]) + 1;
698 options = malloc(len);
701 for (i = 2; i < argc; i++) {
706 cur += sprintf(cur, "%s", argv[i]);
709 if (init_module_folders()) {
710 fprintf(stderr, "Failed to find the folder holding the modules\n");
714 if (get_module_path(argv[1])) {
716 } else if (!get_module_path(name)) {
717 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
721 ret = insert_module(get_module_path(name), options);
725 ULOG_ERR("failed to insert %s\n", get_module_path(name));
730 static int main_rmmod(int argc, char **argv)
737 return print_usage("rmmod");
739 if (scan_loaded_modules())
742 name = get_module_name(argv[1]);
743 m = find_module(name);
745 ULOG_ERR("module is not loaded\n");
748 ret = syscall(__NR_delete_module, m->name, 0);
751 ULOG_ERR("unloading the module failed\n");
758 static int main_lsmod(int argc, char **argv)
760 struct module_node *mn;
764 if (scan_loaded_modules())
767 avl_for_each_element(&modules, mn, avl) {
771 if (m->state == LOADED) {
772 printf("%-20s%8d%3d ",
773 m->name, m->size, m->usage);
774 if (m->depends && strcmp(m->depends, "-") && strcmp(m->depends, "")) {
778 dep = dep + strlen(dep) + 1;
792 static int main_modinfo(int argc, char **argv)
798 return print_usage("modinfo");
800 if (scan_module_folders())
803 name = get_module_name(argv[1]);
804 m = find_module(name);
806 ULOG_ERR("cannot find module - %s\n", argv[1]);
810 name = get_module_path(m->name);
812 ULOG_ERR("cannot find path of module - %s\n", m->name);
821 static int main_modprobe(int argc, char **argv)
823 struct module_node *mn;
829 for (i = 1; i < argc; i++)
830 if (argv[i][0] != '-') {
835 return print_usage("modprobe");
837 if (scan_module_folders())
840 if (scan_loaded_modules())
843 name = get_module_name(mod);
844 m = find_module(name);
845 if (m && m->state == LOADED) {
846 ULOG_ERR("%s is already loaded\n", name);
849 ULOG_ERR("failed to find a module named %s\n", name);
855 fail = load_modprobe();
858 ULOG_ERR("%d module%s could not be probed\n",
859 fail, (fail == 1) ? ("") : ("s"));
861 avl_for_each_element(&modules, mn, avl) {
865 if ((m->state == PROBE) || m->error)
866 ULOG_ERR("- %s\n", m->name);
876 static int main_loader(int argc, char **argv)
878 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
879 char *dir = "/etc/modules.d/";
880 struct module_node *mn;
889 path = malloc(strlen(dir) + 2);
893 if (scan_module_folders()) {
898 if (scan_loaded_modules()) {
903 ULOG_INFO("loading kernel modules from %s\n", path);
905 if (glob(path, gl_flags, NULL, &gl) < 0)
908 for (j = 0; j < gl.gl_pathc; j++) {
909 FILE *fp = fopen(gl.gl_pathv[j], "r");
914 ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
918 while (getline(&mod, &mod_len, fp) > 0) {
919 char *nl = strchr(mod, '\n');
926 opts = strchr(mod, ' ');
930 m = find_module(get_module_name(mod));
931 if (!m || (m->state == LOADED))
935 m->opts = strdup(opts);
937 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
945 fail = load_modprobe();
948 ULOG_ERR("%d module%s could not be probed\n",
949 fail, (fail == 1) ? ("") : ("s"));
951 avl_for_each_element(&modules, mn, avl) {
955 if ((m->state == PROBE) || (m->error))
956 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
959 ULOG_INFO("done loading kernel modules from %s\n", path);
969 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
974 while (*s1 && ((*s1 == *s2) ||
975 ((*s1 == '_') && (*s2 == '-')) ||
976 ((*s1 == '-') && (*s2 == '_'))))
982 return *(const unsigned char *)s1 - *(const unsigned char *)s2;
985 int main(int argc, char **argv)
987 char *exec = basename(*argv);
989 avl_init(&modules, avl_modcmp, false, NULL);
990 if (!strcmp(exec, "insmod"))
991 return main_insmod(argc, argv);
993 if (!strcmp(exec, "rmmod"))
994 return main_rmmod(argc, argv);
996 if (!strcmp(exec, "lsmod"))
997 return main_lsmod(argc, argv);
999 if (!strcmp(exec, "modinfo"))
1000 return main_modinfo(argc, argv);
1002 if (!strcmp(exec, "modprobe"))
1003 return main_modprobe(argc, argv);
1005 ulog_open(ULOG_KMSG, LOG_USER, "kmodloader");
1006 return main_loader(argc, argv);