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>
38 #define DEF_MOD_PATH "/lib/modules/%s/"
58 static struct avl_tree modules;
60 static struct module *find_module(const char *name)
63 return avl_find_element(&modules, name, m, avl);
66 static void free_modules(void)
68 struct module *m, *tmp;
70 avl_remove_all_elements(&modules, m, avl, tmp)
74 static char* get_module_path(char *name)
76 static char path[256];
85 snprintf(path, 256, DEF_MOD_PATH "%s.ko", ver.release, name);
97 snprintf(path, 256, DEF_MOD_PATH "%s.ko", ver.release, name);
105 static char* get_module_name(char *path)
107 static char name[32];
110 strncpy(name, basename(path), sizeof(name));
112 t = strstr(name, ".ko");
126 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
128 const char *secnames;
133 e = (Elf64_Ehdr *) map;
134 sh = (Elf64_Shdr *) (map + e->e_shoff);
136 secnames = map + sh[e->e_shstrndx].sh_offset;
137 for (i = 0; i < e->e_shnum; i++) {
138 if (!strcmp(section, secnames + sh[i].sh_name)) {
139 *size = sh[i].sh_size;
140 *offset = sh[i].sh_offset;
148 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
150 const char *secnames;
155 e = (Elf32_Ehdr *) map;
156 sh = (Elf32_Shdr *) (map + e->e_shoff);
158 secnames = map + sh[e->e_shstrndx].sh_offset;
159 for (i = 0; i < e->e_shnum; i++) {
160 if (!strcmp(section, secnames + sh[i].sh_name)) {
161 *size = sh[i].sh_size;
162 *offset = sh[i].sh_offset;
171 static struct module *
172 alloc_module(const char *name, const char *depends, int size)
177 m = calloc_a(sizeof(*m),
178 &_name, strlen(name) + 1,
179 &_dep, depends ? strlen(depends) + 1 : 0);
183 m->avl.key = m->name = strcpy(_name, name);
185 m->depends = strcpy(_dep, depends);
188 avl_insert(&modules, &m->avl);
192 static int scan_loaded_modules(void)
198 fp = fopen("/proc/modules", "r");
200 fprintf(stderr, "failed to open /proc/modules\n");
204 while (getline(&buf, &buf_len, fp) > 0) {
208 m.name = strtok(buf, " ");
209 m.size = atoi(strtok(NULL, " "));
210 m.usage = atoi(strtok(NULL, " "));
211 m.depends = strtok(NULL, " ");
213 if (!m.name || !m.depends)
216 n = alloc_module(m.name, m.depends, m.size);
226 static struct module* get_module_info(const char *module, const char *name)
228 int fd = open(module, O_RDONLY);
229 unsigned int offset, size;
230 char *map, *strings, *dep = NULL;
235 fprintf(stderr, "failed to open %s\n", module);
239 if (fstat(fd, &s) == -1) {
240 fprintf(stderr, "failed to stat %s\n", module);
244 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
245 if (map == MAP_FAILED) {
246 fprintf(stderr, "failed to mmap %s\n", module);
250 if (elf_find_section(map, ".modinfo", &offset, &size)) {
251 fprintf(stderr, "failed to load the .modinfo section from %s\n", module);
255 strings = map + offset;
256 while (strings && (strings < map + offset + size)) {
262 sep = strstr(strings, "=");
267 if (!strncmp(strings, "depends=", len + 1))
269 strings = &sep[strlen(sep)];
272 m = alloc_module(name, dep, s.st_size);
281 static int scan_module_folder(char *dir)
283 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
287 if (glob(dir, gl_flags, NULL, &gl) < 0)
290 for (j = 0; j < gl.gl_pathc; j++) {
291 char *name = get_module_name(gl.gl_pathv[j]);
297 m = find_module(name);
299 get_module_info(gl.gl_pathv[j], name);
307 static int print_modinfo(char *module)
309 int fd = open(module, O_RDONLY);
310 unsigned int offset, size;
315 fprintf(stderr, "failed to open %s\n", module);
319 if (fstat(fd, &s) == -1) {
320 fprintf(stderr, "failed to stat %s\n", module);
324 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
325 if (map == MAP_FAILED) {
326 fprintf(stderr, "failed to mmap %s\n", module);
330 if (elf_find_section(map, ".modinfo", &offset, &size)) {
331 fprintf(stderr, "failed to load the .modinfo section from %s\n", module);
335 strings = map + offset;
336 printf("module:\t\t%s\n", module);
337 while (strings && (strings < map + offset + size)) {
343 sep = strstr(strings, "=");
346 dup = strndup(strings, sep - strings);
348 if (strncmp(strings, "parm", 4)) {
350 printf("%s:\t\t%s\n", dup, sep);
352 printf("%s:\t%s\n", dup, sep);
354 strings = &sep[strlen(sep)];
362 static int insert_module(char *path, const char *options)
368 if (stat(path, &s)) {
369 fprintf(stderr, "missing module %s\n", path);
373 fd = open(path, O_RDONLY);
375 fprintf(stderr, "cannot open %s\n", path);
379 data = malloc(s.st_size);
380 if (read(fd, data, s.st_size) == s.st_size) {
381 ret = syscall(__NR_init_module, data, s.st_size, options);
383 fprintf(stderr, "failed to insert %s\n", path);
385 fprintf(stderr, "failed to read full module %s\n", path);
394 static int deps_available(struct module *m)
396 char *deps = m->depends;
399 if (!strcmp(deps, "-"))
401 while (*deps && (NULL != ((comma = strstr(deps, ","))))) {
404 m = find_module(deps);
406 if (!m || (m->state != LOADED))
415 static int load_depmod(void)
423 avl_for_each_element(&modules, m, avl) {
424 if ((m->state == PROBE) && (!deps_available(m))) {
425 if (!insert_module(get_module_path(m->name), "")) {
431 } else if (m->state == PROBE) {
435 // printf("loaded %d modules this pass\n", loaded);
438 // printf("missing todos %d\n", todo);
443 static int print_insmod_usage(void)
445 fprintf(stderr, "Usage:\n\tinsmod filename [args]\n");
450 static int print_usage(char *arg)
452 fprintf(stderr, "Usage:\n\t%s module\n", arg);
457 static int main_insmod(int argc, char **argv)
459 char *name, *cur, *options;
463 return print_insmod_usage();
465 name = get_module_name(argv[1]);
467 fprintf(stderr, "cannot find module - %s\n", argv[1]);
471 if (scan_loaded_modules())
474 if (find_module(name)) {
475 fprintf(stderr, "module is already loaded - %s\n", name);
482 for (len = 0, i = 2; i < argc; i++)
483 len += strlen(argv[i]) + 1;
485 options = malloc(len);
488 for (i = 2; i < argc; i++) {
493 cur += sprintf(cur, "%s", argv[i]);
496 ret = insert_module(get_module_path(name), options);
502 static int main_rmmod(int argc, char **argv)
509 return print_usage("rmmod");
511 if (scan_loaded_modules())
514 name = get_module_name(argv[1]);
515 m = find_module(name);
517 fprintf(stderr, "module is not loaded\n");
522 ret = syscall(__NR_delete_module, name, 0);
525 fprintf(stderr, "unloading the module failed\n");
530 static int main_lsmod(int argc, char **argv)
534 if (scan_loaded_modules())
537 avl_for_each_element(&modules, m, avl)
538 if (m->state == LOADED)
539 printf("%-20s%8d%3d %s\n",
540 m->name, m->size, m->usage,
541 (*m->depends == '-') ? ("") : (m->depends));
548 static int main_modinfo(int argc, char **argv)
553 return print_usage("modinfo");
555 module = get_module_path(argv[1]);
557 fprintf(stderr, "cannot find module - %s\n", argv[1]);
561 print_modinfo(module);
566 static int main_depmod(int argc, char **argv)
574 return print_usage("depmod");
576 if (scan_loaded_modules())
580 path = alloca(sizeof(DEF_MOD_PATH "*.ko") + strlen(ver.release) + 1);
581 snprintf(path, sizeof(path), DEF_MOD_PATH "*.ko", ver.release);
583 scan_module_folder(path);
585 name = get_module_name(argv[1]);
586 m = find_module(name);
587 if (m && m->state == LOADED) {
588 fprintf(stderr, "%s is already loaded\n", name);
591 fprintf(stderr, "failed to find a module named %s\n", name);
602 static int main_loader(int argc, char **argv)
604 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
605 char *dir = "/etc/modules.d/*";
613 path = malloc(strlen(dir) + 2);
617 scan_loaded_modules();
619 syslog(0, "kmodloader: loading kernel modules from %s\n", path);
621 if (glob(path, gl_flags, NULL, &gl) < 0)
624 for (j = 0; j < gl.gl_pathc; j++) {
625 FILE *fp = fopen(gl.gl_pathv[j], "r");
630 fprintf(stderr, "failed to open %s\n", gl.gl_pathv[j]);
634 while (getline(&mod, &mod_len, fp) > 0) {
635 char *nl = strchr(mod, '\n');
642 opts = strchr(mod, ' ');
646 m = find_module(get_module_name(mod));
649 insert_module(get_module_path(mod), (opts) ? (opts) : (""));
662 int main(int argc, char **argv)
664 char *exec = basename(*argv);
666 avl_init(&modules, avl_strcmp, false, NULL);
667 if (!strcmp(exec, "insmod"))
668 return main_insmod(argc, argv);
670 if (!strcmp(exec, "rmmod"))
671 return main_rmmod(argc, argv);
673 if (!strcmp(exec, "lsmod"))
674 return main_lsmod(argc, argv);
676 if (!strcmp(exec, "modinfo"))
677 return main_modinfo(argc, argv);
679 if (!strcmp(exec, "depmod"))
680 return main_depmod(argc, argv);
682 return main_loader(argc, argv);