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>
38 #include <libubox/ulog.h>
40 #define DEF_MOD_PATH "/modules/%s/"
61 static struct avl_tree modules;
63 static char **module_folders = NULL;
65 static int init_module_folders(void)
70 char *s, *e, *p, path[256], ldpath[256];
73 s = getenv("LD_LIBRARY_PATH");
76 e += snprintf(ldpath, sizeof(ldpath), "%s:", s);
78 e += snprintf(e, sizeof(ldpath) - (e - ldpath), "/lib");
82 for (s = p = ldpath; p <= e; p++) {
83 if (*p != ':' && *p != '\0')
87 snprintf(path, sizeof(path), "%s" DEF_MOD_PATH, s, ver.release);
89 if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
90 module_folders = realloc(module_folders, sizeof(p) * (n + 2));
95 module_folders[n++] = strdup(path);
104 module_folders[n] = NULL;
108 static struct module *find_module(const char *name)
111 return avl_find_element(&modules, name, m, avl);
114 static void free_modules(void)
116 struct module *m, *tmp;
118 avl_remove_all_elements(&modules, m, avl, tmp)
122 static char* get_module_path(char *name)
125 static char path[256];
128 if (!stat(name, &s) && S_ISREG(s.st_mode))
131 for (p = module_folders; *p; p++) {
132 snprintf(path, sizeof(path), "%s%s.ko", *p, name);
133 if (!stat(path, &s) && S_ISREG(s.st_mode))
140 static char* get_module_name(char *path)
142 static char name[32];
145 strncpy(name, basename(path), sizeof(name));
147 t = strstr(name, ".ko");
154 static int elf64_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
156 const char *secnames;
161 e = (Elf64_Ehdr *) map;
162 sh = (Elf64_Shdr *) (map + e->e_shoff);
164 secnames = map + sh[e->e_shstrndx].sh_offset;
165 for (i = 0; i < e->e_shnum; i++) {
166 if (!strcmp(section, secnames + sh[i].sh_name)) {
167 *size = sh[i].sh_size;
168 *offset = sh[i].sh_offset;
176 static int elf32_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
178 const char *secnames;
183 e = (Elf32_Ehdr *) map;
184 sh = (Elf32_Shdr *) (map + e->e_shoff);
186 secnames = map + sh[e->e_shstrndx].sh_offset;
187 for (i = 0; i < e->e_shnum; i++) {
188 if (!strcmp(section, secnames + sh[i].sh_name)) {
189 *size = sh[i].sh_size;
190 *offset = sh[i].sh_offset;
198 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
200 int clazz = map[EI_CLASS];
202 if (clazz == ELFCLASS32)
203 return elf32_find_section(map, section, offset, size);
204 else if (clazz == ELFCLASS64)
205 return elf64_find_section(map, section, offset, size);
207 ULOG_ERR("unknown elf format %d\n", clazz);
212 static struct module *
213 alloc_module(const char *name, const char *depends, int size)
218 m = calloc_a(sizeof(*m),
219 &_name, strlen(name) + 1,
220 &_dep, depends ? strlen(depends) + 2 : 0);
224 m->avl.key = m->name = strcpy(_name, name);
228 m->depends = strcpy(_dep, depends);
237 avl_insert(&modules, &m->avl);
242 static int scan_loaded_modules(void)
248 fp = fopen("/proc/modules", "r");
250 ULOG_ERR("failed to open /proc/modules\n");
254 while (getline(&buf, &buf_len, fp) > 0) {
258 m.name = strtok(buf, " ");
259 m.size = atoi(strtok(NULL, " "));
260 m.usage = atoi(strtok(NULL, " "));
261 m.depends = strtok(NULL, " ");
263 if (!m.name || !m.depends)
266 n = alloc_module(m.name, m.depends, m.size);
276 static struct module* get_module_info(const char *module, const char *name)
278 int fd = open(module, O_RDONLY);
279 unsigned int offset, size;
280 char *map, *strings, *dep = NULL;
285 ULOG_ERR("failed to open %s\n", module);
289 if (fstat(fd, &s) == -1) {
290 ULOG_ERR("failed to stat %s\n", module);
294 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
295 if (map == MAP_FAILED) {
296 ULOG_ERR("failed to mmap %s\n", module);
300 if (elf_find_section(map, ".modinfo", &offset, &size)) {
301 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
305 strings = map + offset;
306 while (strings && (strings < map + offset + size)) {
312 sep = strstr(strings, "=");
317 if (!strncmp(strings, "depends=", len + 1))
319 strings = &sep[strlen(sep)];
322 m = alloc_module(name, dep, s.st_size);
331 static int scan_module_folder(const char *dir)
333 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
340 path = alloca(strlen(dir) + sizeof("*.ko") + 1);
341 sprintf(path, "%s*.ko", dir);
343 if (glob(path, gl_flags, NULL, &gl) < 0)
346 for (j = 0; j < gl.gl_pathc; j++) {
347 char *name = get_module_name(gl.gl_pathv[j]);
353 m = find_module(name);
355 get_module_info(gl.gl_pathv[j], name);
363 static int scan_module_folders(void)
368 if (init_module_folders())
371 for (p = module_folders; *p; p++)
372 rv |= scan_module_folder(*p);
377 static int print_modinfo(char *module)
379 int fd = open(module, O_RDONLY);
380 unsigned int offset, size;
385 ULOG_ERR("failed to open %s\n", module);
389 if (fstat(fd, &s) == -1) {
390 ULOG_ERR("failed to stat %s\n", module);
394 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
395 if (map == MAP_FAILED) {
396 ULOG_ERR("failed to mmap %s\n", module);
400 if (elf_find_section(map, ".modinfo", &offset, &size)) {
401 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
405 strings = map + offset;
406 printf("module:\t\t%s\n", module);
407 while (strings && (strings < map + offset + size)) {
413 sep = strstr(strings, "=");
416 dup = strndup(strings, sep - strings);
418 if (strncmp(strings, "parm", 4)) {
420 printf("%s:\t\t%s\n", dup, sep);
422 printf("%s:\t%s\n", dup, sep);
424 strings = &sep[strlen(sep)];
432 static int deps_available(struct module *m, int verbose)
437 if (!strcmp(m->depends, "-") || !strcmp(m->depends, ""))
443 m = find_module(dep);
446 ULOG_ERR("missing dependency %s\n", dep);
447 if (verbose && m && (m->state != LOADED))
448 ULOG_ERR("dependency not loaded %s\n", dep);
449 if (!m || (m->state != LOADED))
451 dep += strlen(dep) + 1;
457 static int insert_module(char *path, const char *options)
463 if (stat(path, &s)) {
464 ULOG_ERR("missing module %s\n", path);
468 fd = open(path, O_RDONLY);
470 ULOG_ERR("cannot open %s\n", path);
474 data = malloc(s.st_size);
475 if (read(fd, data, s.st_size) == s.st_size)
476 ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
478 ULOG_ERR("failed to read full module %s\n", path);
486 static void load_moddeps(struct module *_m)
491 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
497 m = find_module(dep);
500 ULOG_ERR("failed to find dependency %s\n", dep);
501 if (m && (m->state != LOADED)) {
506 dep = dep + strlen(dep) + 1;
510 static int iterations = 0;
511 static int load_modprobe(void)
516 avl_for_each_element(&modules, m, avl)
517 if (m->state == PROBE)
523 avl_for_each_element(&modules, m, avl) {
524 if ((m->state == PROBE) && (!deps_available(m, 0))) {
525 if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
534 if ((m->state == PROBE) || m->error)
543 static int print_insmod_usage(void)
545 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
550 static int print_usage(char *arg)
552 ULOG_INFO("Usage:\n\t%s module\n", arg);
557 static int main_insmod(int argc, char **argv)
559 char *name, *cur, *options;
563 return print_insmod_usage();
565 name = get_module_name(argv[1]);
567 ULOG_ERR("cannot find module - %s\n", argv[1]);
571 if (scan_loaded_modules())
574 if (find_module(name)) {
575 ULOG_ERR("module is already loaded - %s\n", name);
582 for (len = 0, i = 2; i < argc; i++)
583 len += strlen(argv[i]) + 1;
585 options = malloc(len);
588 for (i = 2; i < argc; i++) {
593 cur += sprintf(cur, "%s", argv[i]);
596 init_module_folders();
598 if (get_module_path(argv[1])) {
600 } else if (!get_module_path(name)) {
601 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
605 ret = insert_module(get_module_path(name), options);
609 ULOG_ERR("failed to insert %s\n", get_module_path(name));
614 static int main_rmmod(int argc, char **argv)
621 return print_usage("rmmod");
623 if (scan_loaded_modules())
626 name = get_module_name(argv[1]);
627 m = find_module(name);
629 ULOG_ERR("module is not loaded\n");
632 ret = syscall(__NR_delete_module, m->name, 0);
635 ULOG_ERR("unloading the module failed\n");
642 static int main_lsmod(int argc, char **argv)
646 if (scan_loaded_modules())
649 avl_for_each_element(&modules, m, avl)
650 if (m->state == LOADED)
651 printf("%-20s%8d%3d %s\n",
652 m->name, m->size, m->usage,
653 (*m->depends == '-') ? ("") : (m->depends));
660 static int main_modinfo(int argc, char **argv)
666 return print_usage("modinfo");
668 if (scan_module_folders())
671 name = get_module_name(argv[1]);
672 m = find_module(name);
674 ULOG_ERR("cannot find module - %s\n", argv[1]);
678 name = get_module_path(m->name);
680 ULOG_ERR("cannot find path of module - %s\n", m->name);
689 static int main_modprobe(int argc, char **argv)
695 return print_usage("modprobe");
697 if (scan_loaded_modules())
700 if (scan_module_folders())
703 name = get_module_name(argv[1]);
704 m = find_module(name);
705 if (m && m->state == LOADED) {
706 ULOG_ERR("%s is already loaded\n", name);
709 ULOG_ERR("failed to find a module named %s\n", name);
715 fail = load_modprobe();
718 ULOG_ERR("%d module%s could not be probed\n",
719 fail, (fail == 1) ? ("") : ("s"));
721 avl_for_each_element(&modules, m, avl)
722 if ((m->state == PROBE) || m->error)
723 ULOG_ERR("- %s\n", m->name);
732 static int main_loader(int argc, char **argv)
734 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
735 char *dir = "/etc/modules.d/*";
744 path = malloc(strlen(dir) + 2);
748 if (scan_loaded_modules())
751 if (scan_module_folders())
754 syslog(LOG_INFO, "kmodloader: loading kernel modules from %s\n", path);
756 if (glob(path, gl_flags, NULL, &gl) < 0)
759 for (j = 0; j < gl.gl_pathc; j++) {
760 FILE *fp = fopen(gl.gl_pathv[j], "r");
765 ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
769 while (getline(&mod, &mod_len, fp) > 0) {
770 char *nl = strchr(mod, '\n');
777 opts = strchr(mod, ' ');
781 m = find_module(get_module_name(mod));
782 if (!m || (m->state == LOADED))
786 m->opts = strdup(opts);
788 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
796 fail = load_modprobe();
799 ULOG_ERR("%d module%s could not be probed\n",
800 fail, (fail == 1) ? ("") : ("s"));
802 avl_for_each_element(&modules, m, avl)
803 if ((m->state == PROBE) || (m->error))
804 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
814 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
819 while (*s1 && ((*s1 == *s2) ||
820 ((*s1 == '_') && (*s2 == '-')) ||
821 ((*s1 == '-') && (*s2 == '_'))))
827 return *(const unsigned char *)s1 - *(const unsigned char *)s2;
830 int main(int argc, char **argv)
832 char *exec = basename(*argv);
834 avl_init(&modules, avl_modcmp, false, NULL);
835 if (!strcmp(exec, "insmod"))
836 return main_insmod(argc, argv);
838 if (!strcmp(exec, "rmmod"))
839 return main_rmmod(argc, argv);
841 if (!strcmp(exec, "lsmod"))
842 return main_lsmod(argc, argv);
844 if (!strcmp(exec, "modinfo"))
845 return main_modinfo(argc, argv);
847 if (!strcmp(exec, "modprobe"))
848 return main_modprobe(argc, argv);
850 return main_loader(argc, argv);