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>
39 #define DEF_MOD_PATH "/modules/%s/"
41 #define INFO(fmt, ...) do { \
42 syslog(LOG_INFO, fmt, ## __VA_ARGS__); \
43 printf("kmod: "fmt, ## __VA_ARGS__); \
45 #define ERROR(fmt, ...) do { \
46 syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
47 fprintf(stderr,"kmod: "fmt, ## __VA_ARGS__); \
49 #define DEBUG(fmt, ...) do { \
50 syslog(LOG_DEBUG, fmt, ## __VA_ARGS__); \
73 static struct avl_tree modules;
75 static char **module_folders = NULL;
77 static int init_module_folders(void)
82 char *s, *e, *p, path[256], ldpath[256];
85 s = getenv("LD_LIBRARY_PATH");
88 e += snprintf(ldpath, sizeof(ldpath), "%s:", s);
90 e += snprintf(e, sizeof(ldpath) - (e - ldpath), "/lib");
94 for (s = p = ldpath; p <= e; p++) {
95 if (*p != ':' && *p != '\0')
99 snprintf(path, sizeof(path), "%s" DEF_MOD_PATH, s, ver.release);
101 if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
102 module_folders = realloc(module_folders, sizeof(p) * (n + 2));
107 module_folders[n++] = strdup(path);
116 module_folders[n] = NULL;
120 static struct module *find_module(const char *name)
123 return avl_find_element(&modules, name, m, avl);
126 static void free_modules(void)
128 struct module *m, *tmp;
130 avl_remove_all_elements(&modules, m, avl, tmp)
134 static char* get_module_path(char *name)
137 static char path[256];
140 if (!stat(name, &s) && S_ISREG(s.st_mode))
143 for (p = module_folders; *p; p++) {
144 snprintf(path, sizeof(path), "%s%s.ko", *p, name);
145 if (!stat(path, &s) && S_ISREG(s.st_mode))
152 static char* get_module_name(char *path)
154 static char name[32];
157 strncpy(name, basename(path), sizeof(name));
159 t = strstr(name, ".ko");
166 static int elf64_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
168 const char *secnames;
173 e = (Elf64_Ehdr *) map;
174 sh = (Elf64_Shdr *) (map + e->e_shoff);
176 secnames = map + sh[e->e_shstrndx].sh_offset;
177 for (i = 0; i < e->e_shnum; i++) {
178 if (!strcmp(section, secnames + sh[i].sh_name)) {
179 *size = sh[i].sh_size;
180 *offset = sh[i].sh_offset;
188 static int elf32_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
190 const char *secnames;
195 e = (Elf32_Ehdr *) map;
196 sh = (Elf32_Shdr *) (map + e->e_shoff);
198 secnames = map + sh[e->e_shstrndx].sh_offset;
199 for (i = 0; i < e->e_shnum; i++) {
200 if (!strcmp(section, secnames + sh[i].sh_name)) {
201 *size = sh[i].sh_size;
202 *offset = sh[i].sh_offset;
210 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
212 int clazz = map[EI_CLASS];
214 if (clazz == ELFCLASS32)
215 return elf32_find_section(map, section, offset, size);
216 else if (clazz == ELFCLASS64)
217 return elf64_find_section(map, section, offset, size);
219 ERROR("unknown elf format %d\n", clazz);
224 static struct module *
225 alloc_module(const char *name, const char *depends, int size)
230 m = calloc_a(sizeof(*m),
231 &_name, strlen(name) + 1,
232 &_dep, depends ? strlen(depends) + 2 : 0);
236 m->avl.key = m->name = strcpy(_name, name);
240 m->depends = strcpy(_dep, depends);
249 avl_insert(&modules, &m->avl);
254 static int scan_loaded_modules(void)
260 fp = fopen("/proc/modules", "r");
262 ERROR("failed to open /proc/modules\n");
266 while (getline(&buf, &buf_len, fp) > 0) {
270 m.name = strtok(buf, " ");
271 m.size = atoi(strtok(NULL, " "));
272 m.usage = atoi(strtok(NULL, " "));
273 m.depends = strtok(NULL, " ");
275 if (!m.name || !m.depends)
278 n = alloc_module(m.name, m.depends, m.size);
288 static struct module* get_module_info(const char *module, const char *name)
290 int fd = open(module, O_RDONLY);
291 unsigned int offset, size;
292 char *map, *strings, *dep = NULL;
297 ERROR("failed to open %s\n", module);
301 if (fstat(fd, &s) == -1) {
302 ERROR("failed to stat %s\n", module);
306 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
307 if (map == MAP_FAILED) {
308 ERROR("failed to mmap %s\n", module);
312 if (elf_find_section(map, ".modinfo", &offset, &size)) {
313 ERROR("failed to load the .modinfo section from %s\n", module);
317 strings = map + offset;
318 while (strings && (strings < map + offset + size)) {
324 sep = strstr(strings, "=");
329 if (!strncmp(strings, "depends=", len + 1))
331 strings = &sep[strlen(sep)];
334 m = alloc_module(name, dep, s.st_size);
343 static int scan_module_folder(const char *dir)
345 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
352 path = alloca(strlen(dir) + sizeof("*.ko") + 1);
353 sprintf(path, "%s*.ko", dir);
355 if (glob(path, gl_flags, NULL, &gl) < 0)
358 for (j = 0; j < gl.gl_pathc; j++) {
359 char *name = get_module_name(gl.gl_pathv[j]);
365 m = find_module(name);
367 get_module_info(gl.gl_pathv[j], name);
375 static int scan_module_folders(void)
380 if (init_module_folders())
383 for (p = module_folders; *p; p++)
384 rv |= scan_module_folder(*p);
389 static int print_modinfo(char *module)
391 int fd = open(module, O_RDONLY);
392 unsigned int offset, size;
397 ERROR("failed to open %s\n", module);
401 if (fstat(fd, &s) == -1) {
402 ERROR("failed to stat %s\n", module);
406 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
407 if (map == MAP_FAILED) {
408 ERROR("failed to mmap %s\n", module);
412 if (elf_find_section(map, ".modinfo", &offset, &size)) {
413 ERROR("failed to load the .modinfo section from %s\n", module);
417 strings = map + offset;
418 printf("module:\t\t%s\n", module);
419 while (strings && (strings < map + offset + size)) {
425 sep = strstr(strings, "=");
428 dup = strndup(strings, sep - strings);
430 if (strncmp(strings, "parm", 4)) {
432 printf("%s:\t\t%s\n", dup, sep);
434 printf("%s:\t%s\n", dup, sep);
436 strings = &sep[strlen(sep)];
444 static int deps_available(struct module *m, int verbose)
449 if (!strcmp(m->depends, "-") || !strcmp(m->depends, ""))
455 m = find_module(dep);
458 ERROR("missing dependency %s\n", dep);
459 if (verbose && m && (m->state != LOADED))
460 ERROR("dependency not loaded %s\n", dep);
461 if (!m || (m->state != LOADED))
463 dep += strlen(dep) + 1;
469 static int insert_module(char *path, const char *options)
475 if (stat(path, &s)) {
476 ERROR("missing module %s\n", path);
480 fd = open(path, O_RDONLY);
482 ERROR("cannot open %s\n", path);
486 data = malloc(s.st_size);
487 if (read(fd, data, s.st_size) == s.st_size)
488 ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
490 ERROR("failed to read full module %s\n", path);
498 static void load_moddeps(struct module *_m)
503 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
509 m = find_module(dep);
512 ERROR("failed to find dependency %s\n", dep);
513 if (m && (m->state != LOADED)) {
518 dep = dep + strlen(dep) + 1;
522 static int iterations = 0;
523 static int load_modprobe(void)
528 avl_for_each_element(&modules, m, avl)
529 if (m->state == PROBE)
535 avl_for_each_element(&modules, m, avl) {
536 if ((m->state == PROBE) && (!deps_available(m, 0))) {
537 if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
546 if ((m->state == PROBE) || m->error)
555 static int print_insmod_usage(void)
557 INFO("Usage:\n\tinsmod filename [args]\n");
562 static int print_usage(char *arg)
564 INFO("Usage:\n\t%s module\n", arg);
569 static int main_insmod(int argc, char **argv)
571 char *name, *cur, *options;
575 return print_insmod_usage();
577 name = get_module_name(argv[1]);
579 ERROR("cannot find module - %s\n", argv[1]);
583 if (scan_loaded_modules())
586 if (find_module(name)) {
587 ERROR("module is already loaded - %s\n", name);
594 for (len = 0, i = 2; i < argc; i++)
595 len += strlen(argv[i]) + 1;
597 options = malloc(len);
600 for (i = 2; i < argc; i++) {
605 cur += sprintf(cur, "%s", argv[i]);
608 if (get_module_path(argv[1])) {
610 } else if (!get_module_path(name)) {
611 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
615 ret = insert_module(get_module_path(name), options);
619 ERROR("failed to insert %s\n", get_module_path(name));
624 static int main_rmmod(int argc, char **argv)
631 return print_usage("rmmod");
633 if (scan_loaded_modules())
636 name = get_module_name(argv[1]);
637 m = find_module(name);
639 ERROR("module is not loaded\n");
642 ret = syscall(__NR_delete_module, m->name, 0);
645 ERROR("unloading the module failed\n");
652 static int main_lsmod(int argc, char **argv)
656 if (scan_loaded_modules())
659 avl_for_each_element(&modules, m, avl)
660 if (m->state == LOADED)
661 printf("%-20s%8d%3d %s\n",
662 m->name, m->size, m->usage,
663 (*m->depends == '-') ? ("") : (m->depends));
670 static int main_modinfo(int argc, char **argv)
676 return print_usage("modinfo");
678 if (scan_module_folders())
681 name = get_module_name(argv[1]);
682 m = find_module(name);
684 ERROR("cannot find module - %s\n", argv[1]);
688 name = get_module_path(m->name);
690 ERROR("cannot find path of module - %s\n", m->name);
699 static int main_modprobe(int argc, char **argv)
705 return print_usage("modprobe");
707 if (scan_loaded_modules())
710 if (scan_module_folders())
713 name = get_module_name(argv[1]);
714 m = find_module(name);
715 if (m && m->state == LOADED) {
716 ERROR("%s is already loaded\n", name);
719 ERROR("failed to find a module named %s\n", name);
725 fail = load_modprobe();
728 ERROR("%d module%s could not be probed\n",
729 fail, (fail == 1) ? ("") : ("s"));
731 avl_for_each_element(&modules, m, avl)
732 if ((m->state == PROBE) || m->error)
733 ERROR("- %s\n", m->name);
742 static int main_loader(int argc, char **argv)
744 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
745 char *dir = "/etc/modules.d/*";
754 path = malloc(strlen(dir) + 2);
758 if (scan_loaded_modules())
761 if (scan_module_folders())
764 syslog(0, "kmodloader: loading kernel modules from %s\n", path);
766 if (glob(path, gl_flags, NULL, &gl) < 0)
769 for (j = 0; j < gl.gl_pathc; j++) {
770 FILE *fp = fopen(gl.gl_pathv[j], "r");
775 ERROR("failed to open %s\n", gl.gl_pathv[j]);
779 while (getline(&mod, &mod_len, fp) > 0) {
780 char *nl = strchr(mod, '\n');
787 opts = strchr(mod, ' ');
791 m = find_module(get_module_name(mod));
792 if (!m || (m->state == LOADED))
796 m->opts = strdup(opts);
798 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
806 fail = load_modprobe();
807 DEBUG("ran %d iterations\n", iterations);
810 ERROR("%d module%s could not be probed\n",
811 fail, (fail == 1) ? ("") : ("s"));
813 avl_for_each_element(&modules, m, avl)
814 if ((m->state == PROBE) || (m->error))
815 ERROR("- %s - %d\n", m->name, deps_available(m, 1));
825 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
830 while (*s1 && ((*s1 == *s2) ||
831 ((*s1 == '_') && (*s2 == '-')) ||
832 ((*s1 == '-') && (*s2 == '_'))))
838 return *(const unsigned char *)s1 - *(const unsigned char *)s2;
841 int main(int argc, char **argv)
843 char *exec = basename(*argv);
845 avl_init(&modules, avl_modcmp, false, NULL);
846 if (!strcmp(exec, "insmod"))
847 return main_insmod(argc, argv);
849 if (!strcmp(exec, "rmmod"))
850 return main_rmmod(argc, argv);
852 if (!strcmp(exec, "lsmod"))
853 return main_lsmod(argc, argv);
855 if (!strcmp(exec, "modinfo"))
856 return main_modinfo(argc, argv);
858 if (!strcmp(exec, "modprobe"))
859 return main_modprobe(argc, argv);
861 return main_loader(argc, argv);