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[32];
161 strncpy(name, basename(path), sizeof(name));
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];
218 if (clazz == ELFCLASS32)
219 return elf32_find_section(map, section, offset, size);
220 else if (clazz == ELFCLASS64)
221 return elf64_find_section(map, section, offset, size);
223 ULOG_ERR("unknown elf format %d\n", clazz);
228 static struct module_node *
229 alloc_module_node(const char *name, struct module *m, bool is_alias)
231 struct module_node *mn;
234 mn = calloc_a(sizeof(*mn),
235 &_name, strlen(name) + 1);
237 mn->avl.key = strcpy(_name, name);
239 mn->is_alias = is_alias;
240 avl_insert(&modules, &mn->avl);
246 static struct module *
247 alloc_module(const char *name, const char * const *aliases, int naliases, const char *depends, int size)
253 m = calloc_a(sizeof(*m),
254 &_name, strlen(name) + 1,
255 &_dep, depends ? strlen(depends) + 2 : 0);
259 m->name = strcpy(_name, name);
263 m->depends = strcpy(_dep, depends);
273 alloc_module_node(m->name, m, false);
274 for (i = 0; i < naliases; i++)
275 alloc_module_node(aliases[i], m, true);
280 static void free_module(struct module *m)
287 static int scan_loaded_modules(void)
293 fp = fopen("/proc/modules", "r");
295 ULOG_ERR("failed to open /proc/modules\n");
299 while (getline(&buf, &buf_len, fp) > 0) {
303 m.name = strtok(buf, " ");
304 m.size = atoi(strtok(NULL, " "));
305 m.usage = atoi(strtok(NULL, " "));
306 m.depends = strtok(NULL, " ");
308 if (!m.name || !m.depends)
311 n = find_module(m.name);
313 /* possibly a module outside /lib/modules/<ver>/ */
314 n = alloc_module(m.name, NULL, 0, m.depends, m.size);
325 static struct module* get_module_info(const char *module, const char *name)
327 int fd = open(module, O_RDONLY);
328 unsigned int offset, size;
329 char *map = MAP_FAILED, *strings, *dep = NULL;
330 const char *aliases[32];
332 struct module *m = NULL;
336 ULOG_ERR("failed to open %s\n", module);
340 if (fstat(fd, &s) == -1) {
341 ULOG_ERR("failed to stat %s\n", module);
345 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
346 if (map == MAP_FAILED) {
347 ULOG_ERR("failed to mmap %s\n", module);
351 if (elf_find_section(map, ".modinfo", &offset, &size)) {
352 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
356 strings = map + offset;
363 if (strings >= map + offset + size)
365 sep = strstr(strings, "=");
370 if (!strncmp(strings, "depends=", len + 1))
372 else if (!strncmp(strings, "alias=", len + 1)) {
373 if (naliases < ARRAY_SIZE(aliases))
374 aliases[naliases++] = sep;
376 ULOG_WARN("module %s has more than %d aliases: truncated",
377 name, ARRAY_SIZE(aliases));
379 strings = &sep[strlen(sep)];
382 m = alloc_module(name, aliases, naliases, dep, s.st_size);
388 if (map != MAP_FAILED)
389 munmap(map, s.st_size);
397 static int scan_module_folder(const char *dir)
399 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
406 path = alloca(strlen(dir) + sizeof("*.ko") + 1);
407 sprintf(path, "%s*.ko", dir);
409 if (glob(path, gl_flags, NULL, &gl) < 0)
412 for (j = 0; j < gl.gl_pathc; j++) {
413 char *name = get_module_name(gl.gl_pathv[j]);
419 m = find_module(name);
421 get_module_info(gl.gl_pathv[j], name);
429 static int scan_module_folders(void)
434 if (init_module_folders())
437 for (p = module_folders; *p; p++)
438 rv |= scan_module_folder(*p);
443 static int print_modinfo(char *module)
445 int fd = open(module, O_RDONLY);
446 unsigned int offset, size;
448 char *map = MAP_FAILED, *strings;
452 ULOG_ERR("failed to open %s\n", module);
456 if (fstat(fd, &s) == -1) {
457 ULOG_ERR("failed to stat %s\n", module);
461 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
462 if (map == MAP_FAILED) {
463 ULOG_ERR("failed to mmap %s\n", module);
467 if (elf_find_section(map, ".modinfo", &offset, &size)) {
468 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
472 strings = map + offset;
473 printf("module:\t\t%s\n", module);
480 if (strings >= map + offset + size)
482 sep = strstr(strings, "=");
485 dup = strndup(strings, sep - strings);
487 if (strncmp(strings, "parm", 4)) {
489 printf("%s:\t\t%s\n", dup, sep);
491 printf("%s:\t%s\n", dup, sep);
493 strings = &sep[strlen(sep)];
501 if (map != MAP_FAILED)
502 munmap(map, s.st_size);
510 static int deps_available(struct module *m, int verbose)
515 if (!m->depends || !strcmp(m->depends, "-") || !strcmp(m->depends, ""))
521 m = find_module(dep);
524 ULOG_ERR("missing dependency %s\n", dep);
525 if (verbose && m && (m->state != LOADED))
526 ULOG_ERR("dependency not loaded %s\n", dep);
527 if (!m || (m->state != LOADED))
529 dep += strlen(dep) + 1;
535 static int insert_module(char *path, const char *options)
541 if (stat(path, &s)) {
542 ULOG_ERR("missing module %s\n", path);
546 fd = open(path, O_RDONLY);
548 ULOG_ERR("cannot open %s\n", path);
552 data = malloc(s.st_size);
553 if (read(fd, data, s.st_size) == s.st_size) {
554 ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
559 ULOG_ERR("failed to read full module %s\n", path);
567 static void load_moddeps(struct module *_m)
572 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
578 m = find_module(dep);
581 ULOG_ERR("failed to find dependency %s\n", dep);
582 if (m && (m->state != LOADED)) {
587 dep = dep + strlen(dep) + 1;
591 static int iterations = 0;
592 static int load_modprobe(void)
595 struct module_node *mn;
598 avl_for_each_element(&modules, mn, avl) {
602 if (m->state == PROBE)
609 avl_for_each_element(&modules, mn, avl) {
613 if ((m->state == PROBE) && (!deps_available(m, 0))) {
614 if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
623 if ((m->state == PROBE) || m->error)
632 static int print_insmod_usage(void)
634 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
639 static int print_modprobe_usage(void)
641 ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
646 static int print_usage(char *arg)
648 ULOG_INFO("Usage:\n\t%s module\n", arg);
653 static int main_insmod(int argc, char **argv)
655 char *name, *cur, *options;
659 return print_insmod_usage();
661 name = get_module_name(argv[1]);
663 ULOG_ERR("cannot find module - %s\n", argv[1]);
667 if (scan_loaded_modules())
670 if (find_module(name)) {
671 ULOG_ERR("module is already loaded - %s\n", name);
678 for (len = 0, i = 2; i < argc; i++)
679 len += strlen(argv[i]) + 1;
681 options = malloc(len);
684 for (i = 2; i < argc; i++) {
689 cur += sprintf(cur, "%s", argv[i]);
692 if (init_module_folders()) {
693 fprintf(stderr, "Failed to find the folder holding the modules\n");
697 if (get_module_path(argv[1])) {
699 } else if (!get_module_path(name)) {
700 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
704 ret = insert_module(get_module_path(name), options);
708 ULOG_ERR("failed to insert %s\n", get_module_path(name));
713 static int main_rmmod(int argc, char **argv)
720 return print_usage("rmmod");
722 if (scan_loaded_modules())
725 name = get_module_name(argv[1]);
726 m = find_module(name);
728 ULOG_ERR("module is not loaded\n");
731 ret = syscall(__NR_delete_module, m->name, 0);
734 ULOG_ERR("unloading the module failed\n");
741 static int main_lsmod(int argc, char **argv)
743 struct module_node *mn;
747 if (scan_loaded_modules())
750 avl_for_each_element(&modules, mn, avl) {
754 if (m->state == LOADED) {
755 printf("%-20s%8d%3d ",
756 m->name, m->size, m->usage);
757 if (m->depends && strcmp(m->depends, "-") && strcmp(m->depends, "")) {
761 dep = dep + strlen(dep) + 1;
775 static int main_modinfo(int argc, char **argv)
781 return print_usage("modinfo");
783 if (scan_module_folders())
786 name = get_module_name(argv[1]);
787 m = find_module(name);
789 ULOG_ERR("cannot find module - %s\n", argv[1]);
793 name = get_module_path(m->name);
795 ULOG_ERR("cannot find path of module - %s\n", m->name);
804 static int main_modprobe(int argc, char **argv)
806 struct module_node *mn;
813 while ((opt = getopt(argc, argv, "q")) != -1 ) {
815 case 'q': /* shhhh! */
819 return print_modprobe_usage();
825 return print_modprobe_usage(); /* expected module after options */
829 if (scan_module_folders())
832 if (scan_loaded_modules())
835 name = get_module_name(mod);
836 m = find_module(name);
837 if (m && m->state == LOADED) {
839 ULOG_ERR("%s is already loaded\n", name);
843 ULOG_ERR("failed to find a module named %s\n", name);
850 fail = load_modprobe();
853 ULOG_ERR("%d module%s could not be probed\n",
854 fail, (fail == 1) ? ("") : ("s"));
856 avl_for_each_element(&modules, mn, avl) {
860 if ((m->state == PROBE) || m->error)
861 ULOG_ERR("- %s\n", m->name);
871 static int main_loader(int argc, char **argv)
873 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
874 char *dir = "/etc/modules.d/";
875 struct module_node *mn;
884 path = malloc(strlen(dir) + 2);
888 if (scan_module_folders()) {
893 if (scan_loaded_modules()) {
898 ULOG_INFO("loading kernel modules from %s\n", path);
900 if (glob(path, gl_flags, NULL, &gl) < 0)
903 for (j = 0; j < gl.gl_pathc; j++) {
904 FILE *fp = fopen(gl.gl_pathv[j], "r");
909 ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
913 while (getline(&mod, &mod_len, fp) > 0) {
914 char *nl = strchr(mod, '\n');
921 opts = strchr(mod, ' ');
925 m = find_module(get_module_name(mod));
926 if (!m || (m->state == LOADED))
930 m->opts = strdup(opts);
932 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
940 fail = load_modprobe();
943 ULOG_ERR("%d module%s could not be probed\n",
944 fail, (fail == 1) ? ("") : ("s"));
946 avl_for_each_element(&modules, mn, avl) {
950 if ((m->state == PROBE) || (m->error))
951 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
954 ULOG_INFO("done loading kernel modules from %s\n", path);
964 static inline char weight(char c)
966 return c == '_' ? '-' : c;
969 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
974 while (*s1 && (weight(*s1) == weight(*s2)))
980 return (unsigned char)weight(*s1) - (unsigned char)weight(*s2);
983 int main(int argc, char **argv)
985 char *exec = basename(*argv);
987 avl_init(&modules, avl_modcmp, false, NULL);
988 if (!strcmp(exec, "insmod"))
989 return main_insmod(argc, argv);
991 if (!strcmp(exec, "rmmod"))
992 return main_rmmod(argc, argv);
994 if (!strcmp(exec, "lsmod"))
995 return main_lsmod(argc, argv);
997 if (!strcmp(exec, "modinfo"))
998 return main_modinfo(argc, argv);
1000 if (!strcmp(exec, "modprobe"))
1001 return main_modprobe(argc, argv);
1003 ulog_open(ULOG_KMSG, LOG_USER, "kmodloader");
1004 return main_loader(argc, argv);