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);
322 static int print_modinfo(char *module)
324 int fd = open(module, O_RDONLY);
325 unsigned int offset, size;
330 LOG("failed to open %s\n", module);
334 if (fstat(fd, &s) == -1) {
335 LOG("failed to stat %s\n", module);
339 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
340 if (map == MAP_FAILED) {
341 LOG("failed to mmap %s\n", module);
345 if (elf_find_section(map, ".modinfo", &offset, &size)) {
346 LOG("failed to load the .modinfo section from %s\n", module);
350 strings = map + offset;
351 printf("module:\t\t%s\n", module);
352 while (strings && (strings < map + offset + size)) {
358 sep = strstr(strings, "=");
361 dup = strndup(strings, sep - strings);
363 if (strncmp(strings, "parm", 4)) {
365 printf("%s:\t\t%s\n", dup, sep);
367 printf("%s:\t%s\n", dup, sep);
369 strings = &sep[strlen(sep)];
377 static int deps_available(struct module *m, int verbose)
383 if (!strcmp(m->depends, "-") || !strcmp(m->depends, ""))
386 _deps = deps = strdup(m->depends);
389 comma = strstr(deps, ",");
393 m = find_module(deps);
396 LOG("missing dependency %s\n", deps);
397 if (verbose && m && (m->state != LOADED))
398 LOG("dependency not loaded %s\n", deps);
399 if (!m || (m->state != LOADED))
410 static int insert_module(char *path, const char *options)
416 if (stat(path, &s)) {
417 LOG("missing module %s\n", path);
421 fd = open(path, O_RDONLY);
423 LOG("cannot open %s\n", path);
427 data = malloc(s.st_size);
428 if (read(fd, data, s.st_size) == s.st_size)
429 ret = syscall(__NR_init_module, data, s.st_size, options);
431 LOG("failed to read full module %s\n", path);
439 static void load_moddeps(struct module *_m)
445 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
448 _deps = deps = strdup(_m->depends);
453 comma = strstr(deps, ",");
457 m = find_module(deps);
465 m = find_module(deps);
469 LOG("failed to find dependency %s\n", deps);
470 if (m && (m->state != LOADED)) {
482 static int load_modprobe(void)
487 avl_for_each_element(&modules, m, avl)
488 if (m->state == PROBE)
494 avl_for_each_element(&modules, m, avl) {
495 if ((m->state == PROBE) && (!deps_available(m, 0))) {
496 if (!insert_module(get_module_path(m->name), "")) {
505 if ((m->state == PROBE) || m->error)
513 static int print_insmod_usage(void)
515 LOG("Usage:\n\tinsmod filename [args]\n");
520 static int print_usage(char *arg)
522 LOG("Usage:\n\t%s module\n", arg);
527 static int main_insmod(int argc, char **argv)
529 char *name, *cur, *options;
533 return print_insmod_usage();
535 name = get_module_name(argv[1]);
537 LOG("cannot find module - %s\n", argv[1]);
541 if (scan_loaded_modules())
544 if (find_module(name)) {
545 LOG("module is already loaded - %s\n", name);
552 for (len = 0, i = 2; i < argc; i++)
553 len += strlen(argv[i]) + 1;
555 options = malloc(len);
558 for (i = 2; i < argc; i++) {
563 cur += sprintf(cur, "%s", argv[i]);
566 ret = insert_module(get_module_path(name), options);
570 LOG("failed to insert %s\n", get_module_path(name));
575 static int main_rmmod(int argc, char **argv)
582 return print_usage("rmmod");
584 if (scan_loaded_modules())
587 name = get_module_name(argv[1]);
588 m = find_module(name);
590 LOG("module is not loaded\n");
595 ret = syscall(__NR_delete_module, name, 0);
598 LOG("unloading the module failed\n");
603 static int main_lsmod(int argc, char **argv)
607 if (scan_loaded_modules())
610 avl_for_each_element(&modules, m, avl)
611 if (m->state == LOADED)
612 printf("%-20s%8d%3d %s\n",
613 m->name, m->size, m->usage,
614 (*m->depends == '-') ? ("") : (m->depends));
621 static int main_modinfo(int argc, char **argv)
626 return print_usage("modinfo");
628 module = get_module_path(argv[1]);
630 LOG("cannot find module - %s\n", argv[1]);
634 print_modinfo(module);
639 static int main_modprobe(int argc, char **argv)
645 return print_usage("modprobe");
647 if (scan_loaded_modules())
650 if (scan_module_folder())
653 name = get_module_name(argv[1]);
654 m = find_module(name);
655 if (m && m->state == LOADED) {
656 LOG("%s is already loaded\n", name);
659 LOG("failed to find a module named %s\n", name);
665 fail = load_modprobe();
668 LOG("%d module%s could not be probed\n",
669 fail, (fail == 1) ? ("") : ("s"));
671 avl_for_each_element(&modules, m, avl)
672 if ((m->state == PROBE) || m->error)
673 LOG("- %s\n", m->name);
682 static int main_loader(int argc, char **argv)
684 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
685 char *dir = "/etc/modules.d/*";
697 path = malloc(strlen(dir) + 2);
701 if (scan_loaded_modules())
704 if (scan_module_folder())
707 syslog(0, "kmodloader: loading kernel modules from %s\n", path);
709 if (glob(path, gl_flags, NULL, &gl) < 0)
712 for (j = 0; j < gl.gl_pathc; j++) {
713 FILE *fp = fopen(gl.gl_pathv[j], "r");
718 LOG("failed to open %s\n", gl.gl_pathv[j]);
722 while (getline(&mod, &mod_len, fp) > 0) {
723 char *nl = strchr(mod, '\n');
730 opts = strchr(mod, ' ');
734 m = find_module(get_module_name(mod));
735 if (!m || (m->state == LOADED))
739 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
747 fail = load_modprobe();
750 LOG("%d module%s could not be probed\n",
751 fail, (fail == 1) ? ("") : ("s"));
753 avl_for_each_element(&modules, m, avl)
754 if ((m->state == PROBE) || (m->error))
755 LOG("- %s - %d\n", m->name, deps_available(m, 1));
765 int main(int argc, char **argv)
767 char *exec = basename(*argv);
769 avl_init(&modules, avl_strcmp, false, NULL);
770 if (!strcmp(exec, "insmod"))
771 return main_insmod(argc, argv);
773 if (!strcmp(exec, "rmmod"))
774 return main_rmmod(argc, argv);
776 if (!strcmp(exec, "lsmod"))
777 return main_lsmod(argc, argv);
779 if (!strcmp(exec, "modinfo"))
780 return main_modinfo(argc, argv);
782 if (!strcmp(exec, "modprobe"))
783 return main_modprobe(argc, argv);
785 return main_loader(argc, argv);