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/"
56 int refcnt; /* number of references from module_node.m */
65 static struct avl_tree modules;
67 static char **module_folders = NULL;
69 static void free_module(struct module *m);
71 static int init_module_folders(void)
76 char *s, *e, *p, path[256], ldpath[256];
79 s = getenv("LD_LIBRARY_PATH");
82 e += snprintf(ldpath, sizeof(ldpath), "%s:", s);
84 e += snprintf(e, sizeof(ldpath) - (e - ldpath), "/lib");
88 for (s = p = ldpath; p <= e; p++) {
89 if (*p != ':' && *p != '\0')
93 snprintf(path, sizeof(path), "%s" DEF_MOD_PATH, s, ver.release);
95 if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
96 module_folders = realloc(module_folders, sizeof(p) * (n + 2));
101 module_folders[n++] = strdup(path);
110 module_folders[n] = NULL;
114 static struct module *find_module(const char *name)
116 struct module_node *mn;
117 mn = avl_find_element(&modules, name, mn, avl);
124 static void free_modules(void)
126 struct module_node *mn, *tmp;
128 avl_remove_all_elements(&modules, mn, avl, tmp) {
129 struct module *m = mn->m;
138 static char* get_module_path(char *name)
141 static char path[256];
144 if (!stat(name, &s) && S_ISREG(s.st_mode))
147 for (p = module_folders; *p; p++) {
148 snprintf(path, sizeof(path), "%s%s.ko", *p, name);
149 if (!stat(path, &s) && S_ISREG(s.st_mode))
156 static char* get_module_name(char *path)
158 static char name[33];
161 strncpy(name, basename(path), sizeof(name) - 1);
163 t = strstr(name, ".ko");
170 static int elf64_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
172 const char *secnames;
177 e = (Elf64_Ehdr *) map;
178 sh = (Elf64_Shdr *) (map + e->e_shoff);
180 secnames = map + sh[e->e_shstrndx].sh_offset;
181 for (i = 0; i < e->e_shnum; i++) {
182 if (!strcmp(section, secnames + sh[i].sh_name)) {
183 *size = sh[i].sh_size;
184 *offset = sh[i].sh_offset;
192 static int elf32_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
194 const char *secnames;
199 e = (Elf32_Ehdr *) map;
200 sh = (Elf32_Shdr *) (map + e->e_shoff);
202 secnames = map + sh[e->e_shstrndx].sh_offset;
203 for (i = 0; i < e->e_shnum; i++) {
204 if (!strcmp(section, secnames + sh[i].sh_name)) {
205 *size = sh[i].sh_size;
206 *offset = sh[i].sh_offset;
214 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
216 int clazz = map[EI_CLASS];
217 int endian = map[EI_DATA];
219 #if __BYTE_ORDER == __LITTLE_ENDIAN
220 if (endian != ELFDATA2LSB)
221 #elif __BYTE_ORDER == __BIG_ENDIAN
222 if (endian != ELFDATA2MSB)
224 #error "unsupported endian"
227 ULOG_ERR("invalid endianess: %d\n", endian);
231 if (clazz == ELFCLASS32)
232 return elf32_find_section(map, section, offset, size);
233 else if (clazz == ELFCLASS64)
234 return elf64_find_section(map, section, offset, size);
236 ULOG_ERR("unknown elf format %d\n", clazz);
241 static struct module_node *
242 alloc_module_node(const char *name, struct module *m, bool is_alias)
244 struct module_node *mn;
247 mn = calloc_a(sizeof(*mn),
248 &_name, strlen(name) + 1);
250 mn->avl.key = strcpy(_name, name);
252 mn->is_alias = is_alias;
253 avl_insert(&modules, &mn->avl);
259 static struct module *
260 alloc_module(const char *name, const char * const *aliases, int naliases, const char *depends, int size)
266 m = calloc_a(sizeof(*m),
267 &_name, strlen(name) + 1,
268 &_dep, depends ? strlen(depends) + 2 : 0);
272 m->name = strcpy(_name, name);
276 m->depends = strcpy(_dep, depends);
286 alloc_module_node(m->name, m, false);
287 for (i = 0; i < naliases; i++)
288 alloc_module_node(aliases[i], m, true);
293 static void free_module(struct module *m)
300 static int scan_loaded_modules(void)
306 fp = fopen("/proc/modules", "r");
308 ULOG_ERR("failed to open /proc/modules\n");
312 while (getline(&buf, &buf_len, fp) > 0) {
316 m.name = strtok(buf, " ");
317 m.size = atoi(strtok(NULL, " "));
318 m.usage = atoi(strtok(NULL, " "));
319 m.depends = strtok(NULL, " ");
321 if (!m.name || !m.depends)
324 n = find_module(m.name);
326 /* possibly a module outside /lib/modules/<ver>/ */
327 n = alloc_module(m.name, NULL, 0, m.depends, m.size);
338 static struct module* get_module_info(const char *module, const char *name)
340 int fd = open(module, O_RDONLY);
341 unsigned int offset, size;
342 char *map = MAP_FAILED, *strings, *dep = NULL;
343 const char *aliases[32] = { 0 };
345 struct module *m = NULL;
349 ULOG_ERR("failed to open %s\n", module);
353 if (fstat(fd, &s) == -1) {
354 ULOG_ERR("failed to stat %s\n", module);
358 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
359 if (map == MAP_FAILED) {
360 ULOG_ERR("failed to mmap %s\n", module);
364 if (elf_find_section(map, ".modinfo", &offset, &size)) {
365 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
369 strings = map + offset;
376 if (strings >= map + offset + size)
378 sep = strstr(strings, "=");
383 if (!strncmp(strings, "depends=", len + 1))
385 else if (!strncmp(strings, "alias=", len + 1)) {
386 if (naliases < ARRAY_SIZE(aliases))
387 aliases[naliases++] = sep;
389 ULOG_WARN("module %s has more than %d aliases: truncated",
390 name, ARRAY_SIZE(aliases));
392 strings = &sep[strlen(sep)];
395 m = alloc_module(name, aliases, naliases, dep, s.st_size);
401 if (map != MAP_FAILED)
402 munmap(map, s.st_size);
410 static int scan_module_folder(const char *dir)
412 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
419 path = alloca(strlen(dir) + sizeof("*.ko") + 1);
420 sprintf(path, "%s*.ko", dir);
422 if (glob(path, gl_flags, NULL, &gl) < 0)
425 for (j = 0; j < gl.gl_pathc; j++) {
426 char *name = get_module_name(gl.gl_pathv[j]);
432 m = find_module(name);
434 get_module_info(gl.gl_pathv[j], name);
442 static int scan_module_folders(void)
447 if (init_module_folders())
450 for (p = module_folders; *p; p++)
451 rv |= scan_module_folder(*p);
456 static int print_modinfo(char *module)
458 int fd = open(module, O_RDONLY);
459 unsigned int offset, size;
461 char *map = MAP_FAILED, *strings;
465 ULOG_ERR("failed to open %s\n", module);
469 if (fstat(fd, &s) == -1) {
470 ULOG_ERR("failed to stat %s\n", module);
474 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
475 if (map == MAP_FAILED) {
476 ULOG_ERR("failed to mmap %s\n", module);
480 if (elf_find_section(map, ".modinfo", &offset, &size)) {
481 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
485 strings = map + offset;
486 printf("module:\t\t%s\n", module);
493 if (strings >= map + offset + size)
495 sep = strstr(strings, "=");
498 dup = strndup(strings, sep - strings);
500 if (strncmp(strings, "parm", 4)) {
502 printf("%s:\t\t%s\n", dup, sep);
504 printf("%s:\t%s\n", dup, sep);
506 strings = &sep[strlen(sep)];
514 if (map != MAP_FAILED)
515 munmap(map, s.st_size);
523 static int deps_available(struct module *m, int verbose)
528 if (!m->depends || !strcmp(m->depends, "-") || !strcmp(m->depends, ""))
534 m = find_module(dep);
537 ULOG_ERR("missing dependency %s\n", dep);
538 if (verbose && m && (m->state != LOADED))
539 ULOG_ERR("dependency not loaded %s\n", dep);
540 if (!m || (m->state != LOADED))
542 dep += strlen(dep) + 1;
548 static int insert_module(char *path, const char *options)
554 if (stat(path, &s)) {
555 ULOG_ERR("missing module %s\n", path);
559 fd = open(path, O_RDONLY);
561 ULOG_ERR("cannot open %s\n", path);
565 data = malloc(s.st_size);
566 if (read(fd, data, s.st_size) == s.st_size) {
567 ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
572 ULOG_ERR("failed to read full module %s\n", path);
580 static void load_moddeps(struct module *_m)
585 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
591 m = find_module(dep);
594 ULOG_ERR("failed to find dependency %s\n", dep);
595 if (m && (m->state != LOADED)) {
600 dep = dep + strlen(dep) + 1;
604 static int iterations = 0;
605 static int load_modprobe(void)
608 struct module_node *mn;
611 avl_for_each_element(&modules, mn, avl) {
615 if (m->state == PROBE)
622 avl_for_each_element(&modules, mn, avl) {
626 if ((m->state == PROBE) && (!deps_available(m, 0))) {
627 if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
636 if ((m->state == PROBE) || m->error)
645 static int print_insmod_usage(void)
647 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
652 static int print_modprobe_usage(void)
654 ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
659 static int print_usage(char *arg)
661 ULOG_INFO("Usage:\n\t%s module\n", arg);
666 static int main_insmod(int argc, char **argv)
668 char *name, *cur, *options;
672 return print_insmod_usage();
674 name = get_module_name(argv[1]);
676 ULOG_ERR("cannot find module - %s\n", argv[1]);
680 if (scan_loaded_modules())
683 if (find_module(name)) {
684 ULOG_ERR("module is already loaded - %s\n", name);
691 for (len = 0, i = 2; i < argc; i++)
692 len += strlen(argv[i]) + 1;
694 options = malloc(len);
697 for (i = 2; i < argc; i++) {
702 cur += sprintf(cur, "%s", argv[i]);
705 if (init_module_folders()) {
706 fprintf(stderr, "Failed to find the folder holding the modules\n");
711 if (get_module_path(argv[1])) {
713 } else if (!get_module_path(name)) {
714 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
719 ret = insert_module(get_module_path(name), options);
721 ULOG_ERR("failed to insert %s\n", get_module_path(name));
728 static int main_rmmod(int argc, char **argv)
735 return print_usage("rmmod");
737 if (scan_loaded_modules())
740 name = get_module_name(argv[1]);
741 m = find_module(name);
743 ULOG_ERR("module is not loaded\n");
746 ret = syscall(__NR_delete_module, m->name, 0);
749 ULOG_ERR("unloading the module failed\n");
756 static int main_lsmod(int argc, char **argv)
758 struct module_node *mn;
762 if (scan_loaded_modules())
765 avl_for_each_element(&modules, mn, avl) {
769 if (m->state == LOADED) {
770 printf("%-20s%8d%3d ",
771 m->name, m->size, m->usage);
772 if (m->depends && strcmp(m->depends, "-") && strcmp(m->depends, "")) {
776 dep = dep + strlen(dep) + 1;
790 static int main_modinfo(int argc, char **argv)
796 return print_usage("modinfo");
798 if (scan_module_folders())
801 name = get_module_name(argv[1]);
802 m = find_module(name);
804 ULOG_ERR("cannot find module - %s\n", argv[1]);
808 name = get_module_path(m->name);
810 ULOG_ERR("cannot find path of module - %s\n", m->name);
819 static int main_modprobe(int argc, char **argv)
821 struct module_node *mn;
828 while ((opt = getopt(argc, argv, "q")) != -1 ) {
830 case 'q': /* shhhh! */
834 return print_modprobe_usage();
840 return print_modprobe_usage(); /* expected module after options */
844 if (scan_module_folders())
847 if (scan_loaded_modules())
850 name = get_module_name(mod);
851 m = find_module(name);
852 if (m && m->state == LOADED) {
854 ULOG_ERR("%s is already loaded\n", name);
858 ULOG_ERR("failed to find a module named %s\n", name);
865 fail = load_modprobe();
868 ULOG_ERR("%d module%s could not be probed\n",
869 fail, (fail == 1) ? ("") : ("s"));
871 avl_for_each_element(&modules, mn, avl) {
875 if ((m->state == PROBE) || m->error)
876 ULOG_ERR("- %s\n", m->name);
886 static int main_loader(int argc, char **argv)
888 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
889 char *dir = "/etc/modules.d/";
890 struct module_node *mn;
899 path = malloc(strlen(dir) + 2);
903 if (scan_module_folders()) {
908 if (scan_loaded_modules()) {
913 ULOG_INFO("loading kernel modules from %s\n", path);
915 if (glob(path, gl_flags, NULL, &gl) < 0)
918 for (j = 0; j < gl.gl_pathc; j++) {
919 FILE *fp = fopen(gl.gl_pathv[j], "r");
924 ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
928 while (getline(&mod, &mod_len, fp) > 0) {
929 char *nl = strchr(mod, '\n');
936 opts = strchr(mod, ' ');
940 m = find_module(get_module_name(mod));
941 if (!m || (m->state == LOADED))
945 m->opts = strdup(opts);
947 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
955 fail = load_modprobe();
958 ULOG_ERR("%d module%s could not be probed\n",
959 fail, (fail == 1) ? ("") : ("s"));
961 avl_for_each_element(&modules, mn, avl) {
965 if ((m->state == PROBE) || (m->error))
966 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
969 ULOG_INFO("done loading kernel modules from %s\n", path);
979 static inline char weight(char c)
981 return c == '_' ? '-' : c;
984 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
989 while (*s1 && (weight(*s1) == weight(*s2)))
995 return (unsigned char)weight(*s1) - (unsigned char)weight(*s2);
998 int main(int argc, char **argv)
1000 char *exec = basename(*argv);
1002 avl_init(&modules, avl_modcmp, false, NULL);
1003 if (!strcmp(exec, "insmod"))
1004 return main_insmod(argc, argv);
1006 if (!strcmp(exec, "rmmod"))
1007 return main_rmmod(argc, argv);
1009 if (!strcmp(exec, "lsmod"))
1010 return main_lsmod(argc, argv);
1012 if (!strcmp(exec, "modinfo"))
1013 return main_modinfo(argc, argv);
1015 if (!strcmp(exec, "modprobe"))
1016 return main_modprobe(argc, argv);
1018 ulog_open(ULOG_KMSG, LOG_USER, "kmodloader");
1019 return main_loader(argc, argv);