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];
92 snprintf(path, 256, "%s" DEF_MOD_PATH "%s.ko", prefix, ver.release, name);
100 static char* get_module_name(char *path)
102 static char name[32];
105 strncpy(name, basename(path), sizeof(name));
107 t = strstr(name, ".ko");
115 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
117 const char *secnames;
122 e = (Elf64_Ehdr *) map;
123 sh = (Elf64_Shdr *) (map + e->e_shoff);
125 secnames = map + sh[e->e_shstrndx].sh_offset;
126 for (i = 0; i < e->e_shnum; i++) {
127 if (!strcmp(section, secnames + sh[i].sh_name)) {
128 *size = sh[i].sh_size;
129 *offset = sh[i].sh_offset;
137 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
139 const char *secnames;
144 e = (Elf32_Ehdr *) map;
145 sh = (Elf32_Shdr *) (map + e->e_shoff);
147 secnames = map + sh[e->e_shstrndx].sh_offset;
148 for (i = 0; i < e->e_shnum; i++) {
149 if (!strcmp(section, secnames + sh[i].sh_name)) {
150 *size = sh[i].sh_size;
151 *offset = sh[i].sh_offset;
160 static struct module *
161 alloc_module(const char *name, const char *depends, int size)
166 m = calloc_a(sizeof(*m),
167 &_name, strlen(name) + 1,
168 &_dep, depends ? strlen(depends) + 2 : 0);
172 m->avl.key = m->name = strcpy(_name, name);
175 m->depends = strcpy(_dep, depends);
184 avl_insert(&modules, &m->avl);
189 static int scan_loaded_modules(void)
195 fp = fopen("/proc/modules", "r");
197 LOG("failed to open /proc/modules\n");
201 while (getline(&buf, &buf_len, fp) > 0) {
205 m.name = strtok(buf, " ");
206 m.size = atoi(strtok(NULL, " "));
207 m.usage = atoi(strtok(NULL, " "));
208 m.depends = strtok(NULL, " ");
210 if (!m.name || !m.depends)
213 n = alloc_module(m.name, m.depends, m.size);
223 static struct module* get_module_info(const char *module, const char *name)
225 int fd = open(module, O_RDONLY);
226 unsigned int offset, size;
227 char *map, *strings, *dep = NULL;
232 LOG("failed to open %s\n", module);
236 if (fstat(fd, &s) == -1) {
237 LOG("failed to stat %s\n", module);
241 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
242 if (map == MAP_FAILED) {
243 LOG("failed to mmap %s\n", module);
247 if (elf_find_section(map, ".modinfo", &offset, &size)) {
248 LOG("failed to load the .modinfo section from %s\n", module);
252 strings = map + offset;
253 while (strings && (strings < map + offset + size)) {
259 sep = strstr(strings, "=");
264 if (!strncmp(strings, "depends=", len + 1))
266 strings = &sep[strlen(sep)];
269 m = alloc_module(name, dep, s.st_size);
278 static int scan_module_folder(void)
280 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
287 path = alloca(sizeof(DEF_MOD_PATH "*.ko") + strlen(prefix) + strlen(ver.release) + 1);
288 sprintf(path, "%s" DEF_MOD_PATH "*.ko", prefix, ver.release);
290 if (glob(path, gl_flags, NULL, &gl) < 0)
293 for (j = 0; j < gl.gl_pathc; j++) {
294 char *name = get_module_name(gl.gl_pathv[j]);
300 m = find_module(name);
302 get_module_info(gl.gl_pathv[j], name);
310 static int print_modinfo(char *module)
312 int fd = open(module, O_RDONLY);
313 unsigned int offset, size;
318 LOG("failed to open %s\n", module);
322 if (fstat(fd, &s) == -1) {
323 LOG("failed to stat %s\n", module);
327 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
328 if (map == MAP_FAILED) {
329 LOG("failed to mmap %s\n", module);
333 if (elf_find_section(map, ".modinfo", &offset, &size)) {
334 LOG("failed to load the .modinfo section from %s\n", module);
338 strings = map + offset;
339 printf("module:\t\t%s\n", module);
340 while (strings && (strings < map + offset + size)) {
346 sep = strstr(strings, "=");
349 dup = strndup(strings, sep - strings);
351 if (strncmp(strings, "parm", 4)) {
353 printf("%s:\t\t%s\n", dup, sep);
355 printf("%s:\t%s\n", dup, sep);
357 strings = &sep[strlen(sep)];
365 static int deps_available(struct module *m, int verbose)
370 if (!strcmp(m->depends, "-") || !strcmp(m->depends, ""))
376 m = find_module(dep);
379 LOG("missing dependency %s\n", dep);
380 if (verbose && m && (m->state != LOADED))
381 LOG("dependency not loaded %s\n", dep);
382 if (!m || (m->state != LOADED))
384 dep += strlen(dep) + 1;
390 static int insert_module(char *path, const char *options)
396 if (stat(path, &s)) {
397 LOG("missing module %s\n", path);
401 fd = open(path, O_RDONLY);
403 LOG("cannot open %s\n", path);
407 data = malloc(s.st_size);
408 if (read(fd, data, s.st_size) == s.st_size)
409 ret = syscall(__NR_init_module, data, s.st_size, options);
411 LOG("failed to read full module %s\n", path);
419 static void load_moddeps(struct module *_m)
424 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
430 m = find_module(dep);
433 LOG("failed to find dependency %s\n", dep);
434 if (m && (m->state != LOADED)) {
439 dep = dep + strlen(dep) + 1;
443 static int iterations = 0;
444 static int load_modprobe(void)
449 avl_for_each_element(&modules, m, avl)
450 if (m->state == PROBE)
456 avl_for_each_element(&modules, m, avl) {
457 if ((m->state == PROBE) && (!deps_available(m, 0))) {
458 if (!insert_module(get_module_path(m->name), "")) {
467 if ((m->state == PROBE) || m->error)
476 static int print_insmod_usage(void)
478 LOG("Usage:\n\tinsmod filename [args]\n");
483 static int print_usage(char *arg)
485 LOG("Usage:\n\t%s module\n", arg);
490 static int main_insmod(int argc, char **argv)
492 char *name, *cur, *options;
496 return print_insmod_usage();
498 name = get_module_name(argv[1]);
500 LOG("cannot find module - %s\n", argv[1]);
504 if (scan_loaded_modules())
507 if (find_module(name)) {
508 LOG("module is already loaded - %s\n", name);
515 for (len = 0, i = 2; i < argc; i++)
516 len += strlen(argv[i]) + 1;
518 options = malloc(len);
521 for (i = 2; i < argc; i++) {
526 cur += sprintf(cur, "%s", argv[i]);
529 ret = insert_module(get_module_path(name), options);
533 LOG("failed to insert %s\n", get_module_path(name));
538 static int main_rmmod(int argc, char **argv)
545 return print_usage("rmmod");
547 if (scan_loaded_modules())
550 name = get_module_name(argv[1]);
551 m = find_module(name);
553 LOG("module is not loaded\n");
556 ret = syscall(__NR_delete_module, m->name, 0);
559 LOG("unloading the module failed\n");
566 static int main_lsmod(int argc, char **argv)
570 if (scan_loaded_modules())
573 avl_for_each_element(&modules, m, avl)
574 if (m->state == LOADED)
575 printf("%-20s%8d%3d %s\n",
576 m->name, m->size, m->usage,
577 (*m->depends == '-') ? ("") : (m->depends));
584 static int main_modinfo(int argc, char **argv)
590 return print_usage("modinfo");
592 if (scan_module_folder())
595 name = get_module_name(argv[1]);
596 m = find_module(name);
598 LOG("cannot find module - %s\n", argv[1]);
602 name = get_module_path(m->name);
604 LOG("cannot find path of module - %s\n", m->name);
613 static int main_modprobe(int argc, char **argv)
619 return print_usage("modprobe");
621 if (scan_loaded_modules())
624 if (scan_module_folder())
627 name = get_module_name(argv[1]);
628 m = find_module(name);
629 if (m && m->state == LOADED) {
630 LOG("%s is already loaded\n", name);
633 LOG("failed to find a module named %s\n", name);
639 fail = load_modprobe();
642 LOG("%d module%s could not be probed\n",
643 fail, (fail == 1) ? ("") : ("s"));
645 avl_for_each_element(&modules, m, avl)
646 if ((m->state == PROBE) || m->error)
647 LOG("- %s\n", m->name);
656 static int main_loader(int argc, char **argv)
658 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
659 char *dir = "/etc/modules.d/*";
671 path = malloc(strlen(dir) + 2);
675 if (scan_loaded_modules())
678 if (scan_module_folder())
681 syslog(0, "kmodloader: loading kernel modules from %s\n", path);
683 if (glob(path, gl_flags, NULL, &gl) < 0)
686 for (j = 0; j < gl.gl_pathc; j++) {
687 FILE *fp = fopen(gl.gl_pathv[j], "r");
692 LOG("failed to open %s\n", gl.gl_pathv[j]);
696 while (getline(&mod, &mod_len, fp) > 0) {
697 char *nl = strchr(mod, '\n');
704 opts = strchr(mod, ' ');
708 m = find_module(get_module_name(mod));
709 if (!m || (m->state == LOADED))
713 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
721 fail = load_modprobe();
722 LOG("ran %d iterations\n", iterations);
725 LOG("%d module%s could not be probed\n",
726 fail, (fail == 1) ? ("") : ("s"));
728 avl_for_each_element(&modules, m, avl)
729 if ((m->state == PROBE) || (m->error))
730 LOG("- %s - %d\n", m->name, deps_available(m, 1));
740 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
745 while (*s1 && ((*s1 == *s2) ||
746 ((*s1 == '_') && (*s2 == '-')) ||
747 ((*s1 == '-') && (*s2 == '_'))))
753 return *(const unsigned char *)s1 - *(const unsigned char *)s2;
756 int main(int argc, char **argv)
758 char *exec = basename(*argv);
760 avl_init(&modules, avl_modcmp, false, NULL);
761 if (!strcmp(exec, "insmod"))
762 return main_insmod(argc, argv);
764 if (!strcmp(exec, "rmmod"))
765 return main_rmmod(argc, argv);
767 if (!strcmp(exec, "lsmod"))
768 return main_lsmod(argc, argv);
770 if (!strcmp(exec, "modinfo"))
771 return main_modinfo(argc, argv);
773 if (!strcmp(exec, "modprobe"))
774 return main_modprobe(argc, argv);
776 return main_loader(argc, argv);