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 if (get_module_path(argv[1])) {
598 } else if (!get_module_path(name)) {
599 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
603 ret = insert_module(get_module_path(name), options);
607 ULOG_ERR("failed to insert %s\n", get_module_path(name));
612 static int main_rmmod(int argc, char **argv)
619 return print_usage("rmmod");
621 if (scan_loaded_modules())
624 name = get_module_name(argv[1]);
625 m = find_module(name);
627 ULOG_ERR("module is not loaded\n");
630 ret = syscall(__NR_delete_module, m->name, 0);
633 ULOG_ERR("unloading the module failed\n");
640 static int main_lsmod(int argc, char **argv)
644 if (scan_loaded_modules())
647 avl_for_each_element(&modules, m, avl)
648 if (m->state == LOADED)
649 printf("%-20s%8d%3d %s\n",
650 m->name, m->size, m->usage,
651 (*m->depends == '-') ? ("") : (m->depends));
658 static int main_modinfo(int argc, char **argv)
664 return print_usage("modinfo");
666 if (scan_module_folders())
669 name = get_module_name(argv[1]);
670 m = find_module(name);
672 ULOG_ERR("cannot find module - %s\n", argv[1]);
676 name = get_module_path(m->name);
678 ULOG_ERR("cannot find path of module - %s\n", m->name);
687 static int main_modprobe(int argc, char **argv)
693 return print_usage("modprobe");
695 if (scan_loaded_modules())
698 if (scan_module_folders())
701 name = get_module_name(argv[1]);
702 m = find_module(name);
703 if (m && m->state == LOADED) {
704 ULOG_ERR("%s is already loaded\n", name);
707 ULOG_ERR("failed to find a module named %s\n", name);
713 fail = load_modprobe();
716 ULOG_ERR("%d module%s could not be probed\n",
717 fail, (fail == 1) ? ("") : ("s"));
719 avl_for_each_element(&modules, m, avl)
720 if ((m->state == PROBE) || m->error)
721 ULOG_ERR("- %s\n", m->name);
730 static int main_loader(int argc, char **argv)
732 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
733 char *dir = "/etc/modules.d/*";
742 path = malloc(strlen(dir) + 2);
746 if (scan_loaded_modules())
749 if (scan_module_folders())
752 syslog(0, "kmodloader: loading kernel modules from %s\n", path);
754 if (glob(path, gl_flags, NULL, &gl) < 0)
757 for (j = 0; j < gl.gl_pathc; j++) {
758 FILE *fp = fopen(gl.gl_pathv[j], "r");
763 ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
767 while (getline(&mod, &mod_len, fp) > 0) {
768 char *nl = strchr(mod, '\n');
775 opts = strchr(mod, ' ');
779 m = find_module(get_module_name(mod));
780 if (!m || (m->state == LOADED))
784 m->opts = strdup(opts);
786 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
794 fail = load_modprobe();
797 ULOG_ERR("%d module%s could not be probed\n",
798 fail, (fail == 1) ? ("") : ("s"));
800 avl_for_each_element(&modules, m, avl)
801 if ((m->state == PROBE) || (m->error))
802 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
812 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
817 while (*s1 && ((*s1 == *s2) ||
818 ((*s1 == '_') && (*s2 == '-')) ||
819 ((*s1 == '-') && (*s2 == '_'))))
825 return *(const unsigned char *)s1 - *(const unsigned char *)s2;
828 int main(int argc, char **argv)
830 char *exec = basename(*argv);
832 avl_init(&modules, avl_modcmp, false, NULL);
833 if (!strcmp(exec, "insmod"))
834 return main_insmod(argc, argv);
836 if (!strcmp(exec, "rmmod"))
837 return main_rmmod(argc, argv);
839 if (!strcmp(exec, "lsmod"))
840 return main_lsmod(argc, argv);
842 if (!strcmp(exec, "modinfo"))
843 return main_modinfo(argc, argv);
845 if (!strcmp(exec, "modprobe"))
846 return main_modprobe(argc, argv);
848 return main_loader(argc, argv);