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
;
59 static char *prefix
= "";
61 static struct module
*find_module(const char *name
)
64 return avl_find_element(&modules
, name
, m
, avl
);
67 static void free_modules(void)
69 struct module
*m
, *tmp
;
71 avl_remove_all_elements(&modules
, m
, avl
, tmp
)
75 static char* get_module_path(char *name
)
77 static char path
[256];
86 snprintf(path
, 256, "%s" DEF_MOD_PATH
"%s.ko", prefix
, ver
.release
, name
);
98 snprintf(path
, 256, "%s" DEF_MOD_PATH
"%s.ko", prefix
, ver
.release
, name
);
106 static char* get_module_name(char *path
)
108 static char name
[32];
111 strncpy(name
, basename(path
), sizeof(name
));
113 t
= strstr(name
, ".ko");
127 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
129 const char *secnames
;
134 e
= (Elf64_Ehdr
*) map
;
135 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
137 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
138 for (i
= 0; i
< e
->e_shnum
; i
++) {
139 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
140 *size
= sh
[i
].sh_size
;
141 *offset
= sh
[i
].sh_offset
;
149 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
151 const char *secnames
;
156 e
= (Elf32_Ehdr
*) map
;
157 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
159 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
160 for (i
= 0; i
< e
->e_shnum
; i
++) {
161 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
162 *size
= sh
[i
].sh_size
;
163 *offset
= sh
[i
].sh_offset
;
172 static struct module
*
173 alloc_module(const char *name
, const char *depends
, int size
)
178 m
= calloc_a(sizeof(*m
),
179 &_name
, strlen(name
) + 1,
180 &_dep
, depends
? strlen(depends
) + 1 : 0);
184 m
->avl
.key
= m
->name
= strcpy(_name
, name
);
186 m
->depends
= strcpy(_dep
, depends
);
189 avl_insert(&modules
, &m
->avl
);
193 static int scan_loaded_modules(void)
199 fp
= fopen("/proc/modules", "r");
201 fprintf(stderr
, "failed to open /proc/modules\n");
205 while (getline(&buf
, &buf_len
, fp
) > 0) {
209 m
.name
= strtok(buf
, " ");
210 m
.size
= atoi(strtok(NULL
, " "));
211 m
.usage
= atoi(strtok(NULL
, " "));
212 m
.depends
= strtok(NULL
, " ");
214 if (!m
.name
|| !m
.depends
)
217 n
= alloc_module(m
.name
, m
.depends
, m
.size
);
227 static struct module
* get_module_info(const char *module
, const char *name
)
229 int fd
= open(module
, O_RDONLY
);
230 unsigned int offset
, size
;
231 char *map
, *strings
, *dep
= NULL
;
236 fprintf(stderr
, "failed to open %s\n", module
);
240 if (fstat(fd
, &s
) == -1) {
241 fprintf(stderr
, "failed to stat %s\n", module
);
245 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
246 if (map
== MAP_FAILED
) {
247 fprintf(stderr
, "failed to mmap %s\n", module
);
251 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
252 fprintf(stderr
, "failed to load the .modinfo section from %s\n", module
);
256 strings
= map
+ offset
;
257 while (strings
&& (strings
< map
+ offset
+ size
)) {
263 sep
= strstr(strings
, "=");
268 if (!strncmp(strings
, "depends=", len
+ 1))
270 strings
= &sep
[strlen(sep
)];
273 m
= alloc_module(name
, dep
, s
.st_size
);
282 static int scan_module_folder(char *dir
)
284 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
288 if (glob(dir
, gl_flags
, NULL
, &gl
) < 0)
291 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
292 char *name
= get_module_name(gl
.gl_pathv
[j
]);
298 m
= find_module(name
);
300 get_module_info(gl
.gl_pathv
[j
], name
);
308 static int print_modinfo(char *module
)
310 int fd
= open(module
, O_RDONLY
);
311 unsigned int offset
, size
;
316 fprintf(stderr
, "failed to open %s\n", module
);
320 if (fstat(fd
, &s
) == -1) {
321 fprintf(stderr
, "failed to stat %s\n", module
);
325 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
326 if (map
== MAP_FAILED
) {
327 fprintf(stderr
, "failed to mmap %s\n", module
);
331 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
332 fprintf(stderr
, "failed to load the .modinfo section from %s\n", module
);
336 strings
= map
+ offset
;
337 printf("module:\t\t%s\n", module
);
338 while (strings
&& (strings
< map
+ offset
+ size
)) {
344 sep
= strstr(strings
, "=");
347 dup
= strndup(strings
, sep
- strings
);
349 if (strncmp(strings
, "parm", 4)) {
351 printf("%s:\t\t%s\n", dup
, sep
);
353 printf("%s:\t%s\n", dup
, sep
);
355 strings
= &sep
[strlen(sep
)];
363 static int insert_module(char *path
, const char *options
)
369 if (stat(path
, &s
)) {
370 fprintf(stderr
, "missing module %s\n", path
);
374 fd
= open(path
, O_RDONLY
);
376 fprintf(stderr
, "cannot open %s\n", path
);
380 data
= malloc(s
.st_size
);
381 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
382 ret
= syscall(__NR_init_module
, data
, s
.st_size
, options
);
384 fprintf(stderr
, "failed to insert %s\n", path
);
386 fprintf(stderr
, "failed to read full module %s\n", path
);
395 static int deps_available(struct module
*m
)
397 char *deps
= m
->depends
;
400 if (!strcmp(deps
, "-"))
402 while (*deps
&& (NULL
!= ((comma
= strstr(deps
, ","))))) {
405 m
= find_module(deps
);
407 if (!m
|| (m
->state
!= LOADED
))
416 static int load_depmod(void)
424 avl_for_each_element(&modules
, m
, avl
) {
425 if ((m
->state
== PROBE
) && (!deps_available(m
))) {
426 if (!insert_module(get_module_path(m
->name
), "")) {
432 } else if (m
->state
== PROBE
) {
436 // printf("loaded %d modules this pass\n", loaded);
439 // printf("missing todos %d\n", todo);
444 static int print_insmod_usage(void)
446 fprintf(stderr
, "Usage:\n\tinsmod filename [args]\n");
451 static int print_usage(char *arg
)
453 fprintf(stderr
, "Usage:\n\t%s module\n", arg
);
458 static int main_insmod(int argc
, char **argv
)
460 char *name
, *cur
, *options
;
464 return print_insmod_usage();
466 name
= get_module_name(argv
[1]);
468 fprintf(stderr
, "cannot find module - %s\n", argv
[1]);
472 if (scan_loaded_modules())
475 if (find_module(name
)) {
476 fprintf(stderr
, "module is already loaded - %s\n", name
);
483 for (len
= 0, i
= 2; i
< argc
; i
++)
484 len
+= strlen(argv
[i
]) + 1;
486 options
= malloc(len
);
489 for (i
= 2; i
< argc
; i
++) {
494 cur
+= sprintf(cur
, "%s", argv
[i
]);
497 ret
= insert_module(get_module_path(name
), options
);
503 static int main_rmmod(int argc
, char **argv
)
510 return print_usage("rmmod");
512 if (scan_loaded_modules())
515 name
= get_module_name(argv
[1]);
516 m
= find_module(name
);
518 fprintf(stderr
, "module is not loaded\n");
523 ret
= syscall(__NR_delete_module
, name
, 0);
526 fprintf(stderr
, "unloading the module failed\n");
531 static int main_lsmod(int argc
, char **argv
)
535 if (scan_loaded_modules())
538 avl_for_each_element(&modules
, m
, avl
)
539 if (m
->state
== LOADED
)
540 printf("%-20s%8d%3d %s\n",
541 m
->name
, m
->size
, m
->usage
,
542 (*m
->depends
== '-') ? ("") : (m
->depends
));
549 static int main_modinfo(int argc
, char **argv
)
554 return print_usage("modinfo");
556 module
= get_module_path(argv
[1]);
558 fprintf(stderr
, "cannot find module - %s\n", argv
[1]);
562 print_modinfo(module
);
567 static int main_depmod(int argc
, char **argv
)
575 return print_usage("depmod");
577 if (scan_loaded_modules())
581 path
= alloca(sizeof(DEF_MOD_PATH
"*.ko") + strlen(ver
.release
) + 1);
582 sprintf(path
, DEF_MOD_PATH
"*.ko", ver
.release
);
584 scan_module_folder(path
);
586 name
= get_module_name(argv
[1]);
587 m
= find_module(name
);
588 if (m
&& m
->state
== LOADED
) {
589 fprintf(stderr
, "%s is already loaded\n", name
);
592 fprintf(stderr
, "failed to find a module named %s\n", name
);
603 static int main_loader(int argc
, char **argv
)
605 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
606 char *dir
= "/etc/modules.d/*";
617 path
= malloc(strlen(dir
) + 2);
621 scan_loaded_modules();
623 syslog(0, "kmodloader: loading kernel modules from %s\n", path
);
625 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
628 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
629 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
634 fprintf(stderr
, "failed to open %s\n", gl
.gl_pathv
[j
]);
638 while (getline(&mod
, &mod_len
, fp
) > 0) {
639 char *nl
= strchr(mod
, '\n');
646 opts
= strchr(mod
, ' ');
650 m
= find_module(get_module_name(mod
));
653 insert_module(get_module_path(mod
), (opts
) ? (opts
) : (""));
666 int main(int argc
, char **argv
)
668 char *exec
= basename(*argv
);
670 avl_init(&modules
, avl_strcmp
, false, NULL
);
671 if (!strcmp(exec
, "insmod"))
672 return main_insmod(argc
, argv
);
674 if (!strcmp(exec
, "rmmod"))
675 return main_rmmod(argc
, argv
);
677 if (!strcmp(exec
, "lsmod"))
678 return main_lsmod(argc
, argv
);
680 if (!strcmp(exec
, "modinfo"))
681 return main_modinfo(argc
, argv
);
683 if (!strcmp(exec
, "depmod"))
684 return main_depmod(argc
, argv
);
686 return main_loader(argc
, argv
);