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 = NULL;
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 aliases = realloc(aliases, sizeof(sep) * (naliases + 1));
388 ULOG_ERR("out of memory\n");
392 aliases[naliases++] = sep;
394 strings = &sep[strlen(sep)];
397 m = alloc_module(name, aliases, naliases, dep, s.st_size);
403 if (map != MAP_FAILED)
404 munmap(map, s.st_size);
414 static int scan_module_folder(const char *dir)
416 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
423 path = alloca(strlen(dir) + sizeof("*.ko") + 1);
424 sprintf(path, "%s*.ko", dir);
426 if (glob(path, gl_flags, NULL, &gl) < 0)
429 for (j = 0; j < gl.gl_pathc; j++) {
430 char *name = get_module_name(gl.gl_pathv[j]);
436 m = find_module(name);
438 if (!get_module_info(gl.gl_pathv[j], name))
448 static int scan_module_folders(void)
453 if (init_module_folders())
456 for (p = module_folders; *p; p++)
457 rv |= scan_module_folder(*p);
462 static int print_modinfo(char *module)
464 int fd = open(module, O_RDONLY);
465 unsigned int offset, size;
467 char *map = MAP_FAILED, *strings;
471 ULOG_ERR("failed to open %s\n", module);
475 if (fstat(fd, &s) == -1) {
476 ULOG_ERR("failed to stat %s\n", module);
480 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
481 if (map == MAP_FAILED) {
482 ULOG_ERR("failed to mmap %s\n", module);
486 if (elf_find_section(map, ".modinfo", &offset, &size)) {
487 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
491 strings = map + offset;
492 printf("module:\t\t%s\n", module);
499 if (strings >= map + offset + size)
501 sep = strstr(strings, "=");
504 dup = strndup(strings, sep - strings);
506 if (strncmp(strings, "parm", 4)) {
508 printf("%s:\t\t%s\n", dup, sep);
510 printf("%s:\t%s\n", dup, sep);
512 strings = &sep[strlen(sep)];
520 if (map != MAP_FAILED)
521 munmap(map, s.st_size);
529 static int deps_available(struct module *m, int verbose)
534 if (!m->depends || !strcmp(m->depends, "-") || !strcmp(m->depends, ""))
540 m = find_module(dep);
543 ULOG_ERR("missing dependency %s\n", dep);
544 if (verbose && m && (m->state != LOADED))
545 ULOG_ERR("dependency not loaded %s\n", dep);
546 if (!m || (m->state != LOADED))
548 dep += strlen(dep) + 1;
554 static int insert_module(char *path, const char *options)
560 if (stat(path, &s)) {
561 ULOG_ERR("missing module %s\n", path);
565 fd = open(path, O_RDONLY);
567 ULOG_ERR("cannot open %s\n", path);
571 data = malloc(s.st_size);
575 if (read(fd, data, s.st_size) == s.st_size) {
576 ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
581 ULOG_ERR("failed to read full module %s\n", path);
590 static void load_moddeps(struct module *_m)
595 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
601 m = find_module(dep);
604 ULOG_ERR("failed to find dependency %s\n", dep);
605 if (m && (m->state != LOADED)) {
610 dep = dep + strlen(dep) + 1;
614 static int iterations = 0;
615 static int load_modprobe(void)
618 struct module_node *mn;
621 avl_for_each_element(&modules, mn, avl) {
625 if (m->state == PROBE)
632 avl_for_each_element(&modules, mn, avl) {
636 if ((m->state == PROBE) && (!deps_available(m, 0))) {
637 if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
646 if ((m->state == PROBE) || m->error)
655 static int print_insmod_usage(void)
657 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
662 static int print_modprobe_usage(void)
664 ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
669 static int print_usage(char *arg)
671 ULOG_INFO("Usage:\n\t%s module\n", arg);
676 static int main_insmod(int argc, char **argv)
678 char *name, *cur, *options;
682 return print_insmod_usage();
684 name = get_module_name(argv[1]);
686 ULOG_ERR("cannot find module - %s\n", argv[1]);
690 if (scan_loaded_modules())
693 if (find_module(name)) {
694 ULOG_ERR("module is already loaded - %s\n", name);
701 for (len = 0, i = 2; i < argc; i++)
702 len += strlen(argv[i]) + 1;
704 options = malloc(len);
712 for (i = 2; i < argc; i++) {
717 cur += sprintf(cur, "%s", argv[i]);
720 if (init_module_folders()) {
721 fprintf(stderr, "Failed to find the folder holding the modules\n");
726 if (get_module_path(argv[1])) {
728 } else if (!get_module_path(name)) {
729 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
734 ret = insert_module(get_module_path(name), options);
736 ULOG_ERR("failed to insert %s\n", get_module_path(name));
743 static int main_rmmod(int argc, char **argv)
750 return print_usage("rmmod");
752 if (scan_loaded_modules())
755 name = get_module_name(argv[1]);
756 m = find_module(name);
758 ULOG_ERR("module is not loaded\n");
761 ret = syscall(__NR_delete_module, m->name, 0);
764 ULOG_ERR("unloading the module failed\n");
771 static int main_lsmod(int argc, char **argv)
773 struct module_node *mn;
777 if (scan_loaded_modules())
780 avl_for_each_element(&modules, mn, avl) {
784 if (m->state == LOADED) {
785 printf("%-20s%8d%3d ",
786 m->name, m->size, m->usage);
787 if (m->depends && strcmp(m->depends, "-") && strcmp(m->depends, "")) {
791 dep = dep + strlen(dep) + 1;
805 static int main_modinfo(int argc, char **argv)
811 return print_usage("modinfo");
813 if (scan_module_folders())
816 name = get_module_name(argv[1]);
817 m = find_module(name);
819 ULOG_ERR("cannot find module - %s\n", argv[1]);
823 name = get_module_path(m->name);
825 ULOG_ERR("cannot find path of module - %s\n", m->name);
834 static int main_modprobe(int argc, char **argv)
836 struct module_node *mn;
843 while ((opt = getopt(argc, argv, "q")) != -1 ) {
845 case 'q': /* shhhh! */
849 return print_modprobe_usage();
855 return print_modprobe_usage(); /* expected module after options */
859 if (scan_module_folders())
862 if (scan_loaded_modules())
865 name = get_module_name(mod);
866 m = find_module(name);
867 if (m && m->state == LOADED) {
869 ULOG_ERR("%s is already loaded\n", name);
873 ULOG_ERR("failed to find a module named %s\n", name);
880 fail = load_modprobe();
883 ULOG_ERR("%d module%s could not be probed\n",
884 fail, (fail == 1) ? ("") : ("s"));
886 avl_for_each_element(&modules, mn, avl) {
890 if ((m->state == PROBE) || m->error)
891 ULOG_ERR("- %s\n", m->name);
901 static int main_loader(int argc, char **argv)
903 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
904 char *dir = "/etc/modules.d/";
905 struct module_node *mn;
914 path = malloc(strlen(dir) + 2);
921 if (scan_module_folders()) {
926 if (scan_loaded_modules()) {
931 ULOG_INFO("loading kernel modules from %s\n", path);
933 if (glob(path, gl_flags, NULL, &gl) < 0)
936 for (j = 0; j < gl.gl_pathc; j++) {
937 FILE *fp = fopen(gl.gl_pathv[j], "r");
942 ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
946 while (getline(&mod, &mod_len, fp) > 0) {
947 char *nl = strchr(mod, '\n');
954 opts = strchr(mod, ' ');
958 m = find_module(get_module_name(mod));
959 if (!m || (m->state == LOADED))
963 m->opts = strdup(opts);
965 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
973 fail = load_modprobe();
976 ULOG_ERR("%d module%s could not be probed\n",
977 fail, (fail == 1) ? ("") : ("s"));
979 avl_for_each_element(&modules, mn, avl) {
983 if ((m->state == PROBE) || (m->error))
984 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
987 ULOG_INFO("done loading kernel modules from %s\n", path);
997 static inline char weight(char c)
999 return c == '_' ? '-' : c;
1002 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
1004 const char *s1 = k1;
1005 const char *s2 = k2;
1007 while (*s1 && (weight(*s1) == weight(*s2)))
1013 return (unsigned char)weight(*s1) - (unsigned char)weight(*s2);
1016 int main(int argc, char **argv)
1018 char *exec = basename(*argv);
1020 avl_init(&modules, avl_modcmp, false, NULL);
1021 if (!strcmp(exec, "insmod"))
1022 return main_insmod(argc, argv);
1024 if (!strcmp(exec, "rmmod"))
1025 return main_rmmod(argc, argv);
1027 if (!strcmp(exec, "lsmod"))
1028 return main_lsmod(argc, argv);
1030 if (!strcmp(exec, "modinfo"))
1031 return main_modinfo(argc, argv);
1033 if (!strcmp(exec, "modprobe"))
1034 return main_modprobe(argc, argv);
1036 ulog_open(ULOG_KMSG, LOG_USER, "kmodloader");
1037 return main_loader(argc, argv);