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 "/lib/modules/%s/"
41 #define LOG(fmt, ...) do { \
42 syslog(LOG_INFO, fmt, ## __VA_ARGS__); \
43 printf("kmod: "fmt, ## __VA_ARGS__); \
65 static struct avl_tree modules;
66 static char *prefix = "";
68 static struct module *find_module(const char *name)
71 return avl_find_element(&modules, name, m, avl);
74 static void free_modules(void)
76 struct module *m, *tmp;
78 avl_remove_all_elements(&modules, m, avl, tmp)
82 static char* get_module_path(char *name)
84 static char path[256];
93 snprintf(path, 256, "%s" DEF_MOD_PATH "%s.ko", prefix, ver.release, name);
105 snprintf(path, 256, "%s" DEF_MOD_PATH "%s.ko", prefix, ver.release, name);
113 static char* get_module_name(char *path)
115 static char name[32];
118 strncpy(name, basename(path), sizeof(name));
120 t = strstr(name, ".ko");
134 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
136 const char *secnames;
141 e = (Elf64_Ehdr *) map;
142 sh = (Elf64_Shdr *) (map + e->e_shoff);
144 secnames = map + sh[e->e_shstrndx].sh_offset;
145 for (i = 0; i < e->e_shnum; i++) {
146 if (!strcmp(section, secnames + sh[i].sh_name)) {
147 *size = sh[i].sh_size;
148 *offset = sh[i].sh_offset;
156 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
158 const char *secnames;
163 e = (Elf32_Ehdr *) map;
164 sh = (Elf32_Shdr *) (map + e->e_shoff);
166 secnames = map + sh[e->e_shstrndx].sh_offset;
167 for (i = 0; i < e->e_shnum; i++) {
168 if (!strcmp(section, secnames + sh[i].sh_name)) {
169 *size = sh[i].sh_size;
170 *offset = sh[i].sh_offset;
179 static struct module *
180 alloc_module(const char *name, const char *depends, int size)
185 m = calloc_a(sizeof(*m),
186 &_name, strlen(name) + 1,
187 &_dep, depends ? strlen(depends) + 1 : 0);
191 m->avl.key = m->name = strcpy(_name, name);
194 m->depends = strcpy(_dep, depends);
197 avl_insert(&modules, &m->avl);
201 static int scan_loaded_modules(void)
207 fp = fopen("/proc/modules", "r");
209 LOG("failed to open /proc/modules\n");
213 while (getline(&buf, &buf_len, fp) > 0) {
217 m.name = strtok(buf, " ");
218 m.size = atoi(strtok(NULL, " "));
219 m.usage = atoi(strtok(NULL, " "));
220 m.depends = strtok(NULL, " ");
222 if (!m.name || !m.depends)
225 n = alloc_module(m.name, m.depends, m.size);
235 static struct module* get_module_info(const char *module, const char *name)
237 int fd = open(module, O_RDONLY);
238 unsigned int offset, size;
239 char *map, *strings, *dep = NULL;
244 LOG("failed to open %s\n", module);
248 if (fstat(fd, &s) == -1) {
249 LOG("failed to stat %s\n", module);
253 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
254 if (map == MAP_FAILED) {
255 LOG("failed to mmap %s\n", module);
259 if (elf_find_section(map, ".modinfo", &offset, &size)) {
260 LOG("failed to load the .modinfo section from %s\n", module);
264 strings = map + offset;
265 while (strings && (strings < map + offset + size)) {
271 sep = strstr(strings, "=");
276 if (!strncmp(strings, "depends=", len + 1))
278 strings = &sep[strlen(sep)];
281 m = alloc_module(name, dep, s.st_size);
290 static int scan_module_folder(void)
292 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
299 path = alloca(sizeof(DEF_MOD_PATH "*.ko") + strlen(ver.release) + 1);
300 sprintf(path, DEF_MOD_PATH "*.ko", ver.release);
302 if (glob(path, gl_flags, NULL, &gl) < 0)
305 for (j = 0; j < gl.gl_pathc; j++) {
306 char *name = get_module_name(gl.gl_pathv[j]);
312 m = find_module(name);
314 get_module_info(gl.gl_pathv[j], name);
323 static int print_modinfo(char *module)
325 int fd = open(module, O_RDONLY);
326 unsigned int offset, size;
331 LOG("failed to open %s\n", module);
335 if (fstat(fd, &s) == -1) {
336 LOG("failed to stat %s\n", module);
340 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
341 if (map == MAP_FAILED) {
342 LOG("failed to mmap %s\n", module);
346 if (elf_find_section(map, ".modinfo", &offset, &size)) {
347 LOG("failed to load the .modinfo section from %s\n", module);
351 strings = map + offset;
352 printf("module:\t\t%s\n", module);
353 while (strings && (strings < map + offset + size)) {
359 sep = strstr(strings, "=");
362 dup = strndup(strings, sep - strings);
364 if (strncmp(strings, "parm", 4)) {
366 printf("%s:\t\t%s\n", dup, sep);
368 printf("%s:\t%s\n", dup, sep);
370 strings = &sep[strlen(sep)];
378 static int deps_available(struct module *m, int verbose)
384 if (!strcmp(m->depends, "-") || !strcmp(m->depends, ""))
387 _deps = deps = strdup(m->depends);
390 comma = strstr(deps, ",");
394 m = find_module(deps);
397 LOG("missing dependency %s\n", deps);
398 if (verbose && m && (m->state != LOADED))
399 LOG("dependency not loaded %s\n", deps);
400 if (!m || (m->state != LOADED))
411 static int insert_module(char *path, const char *options)
417 if (stat(path, &s)) {
418 LOG("missing module %s\n", path);
422 fd = open(path, O_RDONLY);
424 LOG("cannot open %s\n", path);
428 data = malloc(s.st_size);
429 if (read(fd, data, s.st_size) == s.st_size)
430 ret = syscall(__NR_init_module, data, s.st_size, options);
432 LOG("failed to read full module %s\n", path);
440 static void load_moddeps(struct module *_m)
446 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
449 _deps = deps = strdup(_m->depends);
454 comma = strstr(deps, ",");
458 m = find_module(deps);
466 m = find_module(deps);
470 LOG("failed to find dependency %s\n", deps);
471 if (m && (m->state != LOADED)) {
483 static int load_modprobe(void)
488 avl_for_each_element(&modules, m, avl)
489 if (m->state == PROBE)
495 avl_for_each_element(&modules, m, avl) {
496 if ((m->state == PROBE) && (!deps_available(m, 0))) {
497 if (!insert_module(get_module_path(m->name), "")) {
506 if ((m->state == PROBE) || m->error)
514 static int print_insmod_usage(void)
516 LOG("Usage:\n\tinsmod filename [args]\n");
521 static int print_usage(char *arg)
523 LOG("Usage:\n\t%s module\n", arg);
528 static int main_insmod(int argc, char **argv)
530 char *name, *cur, *options;
534 return print_insmod_usage();
536 name = get_module_name(argv[1]);
538 LOG("cannot find module - %s\n", argv[1]);
542 if (scan_loaded_modules())
545 if (find_module(name)) {
546 LOG("module is already loaded - %s\n", name);
553 for (len = 0, i = 2; i < argc; i++)
554 len += strlen(argv[i]) + 1;
556 options = malloc(len);
559 for (i = 2; i < argc; i++) {
564 cur += sprintf(cur, "%s", argv[i]);
567 ret = insert_module(get_module_path(name), options);
571 LOG("failed to insert %s\n", get_module_path(name));
576 static int main_rmmod(int argc, char **argv)
583 return print_usage("rmmod");
585 if (scan_loaded_modules())
588 name = get_module_name(argv[1]);
589 m = find_module(name);
591 LOG("module is not loaded\n");
596 ret = syscall(__NR_delete_module, name, 0);
599 LOG("unloading the module failed\n");
604 static int main_lsmod(int argc, char **argv)
608 if (scan_loaded_modules())
611 avl_for_each_element(&modules, m, avl)
612 if (m->state == LOADED)
613 printf("%-20s%8d%3d %s\n",
614 m->name, m->size, m->usage,
615 (*m->depends == '-') ? ("") : (m->depends));
622 static int main_modinfo(int argc, char **argv)
627 return print_usage("modinfo");
629 module = get_module_path(argv[1]);
631 LOG("cannot find module - %s\n", argv[1]);
635 print_modinfo(module);
640 static int main_modprobe(int argc, char **argv)
646 return print_usage("modprobe");
648 if (scan_loaded_modules())
651 if (scan_module_folder())
654 name = get_module_name(argv[1]);
655 m = find_module(name);
656 if (m && m->state == LOADED) {
657 LOG("%s is already loaded\n", name);
660 LOG("failed to find a module named %s\n", name);
666 fail = load_modprobe();
669 LOG("%d module%s could not be probed\n",
670 fail, (fail == 1) ? ("") : ("s"));
672 avl_for_each_element(&modules, m, avl)
673 if ((m->state == PROBE) || m->error)
674 LOG("- %s\n", m->name);
683 static int main_loader(int argc, char **argv)
685 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
686 char *dir = "/etc/modules.d/*";
698 path = malloc(strlen(dir) + 2);
702 if (scan_loaded_modules())
705 if (scan_module_folder())
708 syslog(0, "kmodloader: loading kernel modules from %s\n", path);
710 if (glob(path, gl_flags, NULL, &gl) < 0)
713 for (j = 0; j < gl.gl_pathc; j++) {
714 FILE *fp = fopen(gl.gl_pathv[j], "r");
719 LOG("failed to open %s\n", gl.gl_pathv[j]);
723 while (getline(&mod, &mod_len, fp) > 0) {
724 char *nl = strchr(mod, '\n');
731 opts = strchr(mod, ' ');
735 m = find_module(get_module_name(mod));
736 if (!m || (m->state == LOADED))
740 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
748 fail = load_modprobe();
751 LOG("%d module%s could not be probed\n",
752 fail, (fail == 1) ? ("") : ("s"));
754 avl_for_each_element(&modules, m, avl)
755 if ((m->state == PROBE) || (m->error))
756 LOG("- %s - %d\n", m->name, deps_available(m, 1));
766 int main(int argc, char **argv)
768 char *exec = basename(*argv);
770 avl_init(&modules, avl_strcmp, false, NULL);
771 if (!strcmp(exec, "insmod"))
772 return main_insmod(argc, argv);
774 if (!strcmp(exec, "rmmod"))
775 return main_rmmod(argc, argv);
777 if (!strcmp(exec, "lsmod"))
778 return main_lsmod(argc, argv);
780 if (!strcmp(exec, "modinfo"))
781 return main_modinfo(argc, argv);
783 if (!strcmp(exec, "modprobe"))
784 return main_modprobe(argc, argv);
786 return main_loader(argc, argv);