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)
194 FILE *fp
= fopen("/proc/modules", "r");
198 fprintf(stderr
, "failed to open /proc/modules\n");
202 while (fgets(buf
, sizeof(buf
), fp
)) {
206 m
.name
= strtok(buf
, " ");
207 m
.size
= atoi(strtok(NULL
, " "));
208 m
.usage
= atoi(strtok(NULL
, " "));
209 m
.depends
= strtok(NULL
, " ");
211 if (!m
.name
|| !m
.depends
)
214 n
= alloc_module(m
.name
, m
.depends
, m
.size
);
222 static struct module
* get_module_info(const char *module
, const char *name
)
224 int fd
= open(module
, O_RDONLY
);
225 unsigned int offset
, size
;
226 char *map
, *strings
, *dep
= NULL
;
231 fprintf(stderr
, "failed to open %s\n", module
);
235 if (fstat(fd
, &s
) == -1) {
236 fprintf(stderr
, "failed to stat %s\n", module
);
240 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
241 if (map
== MAP_FAILED
) {
242 fprintf(stderr
, "failed to mmap %s\n", module
);
246 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
247 fprintf(stderr
, "failed to load the .modinfo section from %s\n", module
);
251 strings
= map
+ offset
;
252 while (strings
&& (strings
< map
+ offset
+ size
)) {
258 sep
= strstr(strings
, "=");
263 if (!strncmp(strings
, "depends=", len
+ 1))
265 strings
= &sep
[strlen(sep
)];
268 m
= alloc_module(name
, dep
, s
.st_size
);
277 static int scan_module_folder(char *dir
)
279 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
283 if (glob(dir
, gl_flags
, NULL
, &gl
) < 0)
286 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
287 char *name
= get_module_name(gl
.gl_pathv
[j
]);
293 m
= find_module(name
);
295 get_module_info(gl
.gl_pathv
[j
], name
);
303 static int print_modinfo(char *module
)
305 int fd
= open(module
, O_RDONLY
);
306 unsigned int offset
, size
;
311 fprintf(stderr
, "failed to open %s\n", module
);
315 if (fstat(fd
, &s
) == -1) {
316 fprintf(stderr
, "failed to stat %s\n", module
);
320 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
321 if (map
== MAP_FAILED
) {
322 fprintf(stderr
, "failed to mmap %s\n", module
);
326 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
327 fprintf(stderr
, "failed to load the .modinfo section from %s\n", module
);
331 strings
= map
+ offset
;
332 printf("module:\t\t%s\n", module
);
333 while (strings
&& (strings
< map
+ offset
+ size
)) {
339 sep
= strstr(strings
, "=");
342 dup
= strndup(strings
, sep
- strings
);
344 if (strncmp(strings
, "parm", 4)) {
346 printf("%s:\t\t%s\n", dup
, sep
);
348 printf("%s:\t%s\n", dup
, sep
);
350 strings
= &sep
[strlen(sep
)];
358 static int insert_module(char *path
, const char *options
)
364 if (stat(path
, &s
)) {
365 fprintf(stderr
, "missing module %s\n", path
);
369 fd
= open(path
, O_RDONLY
);
371 fprintf(stderr
, "cannot open %s\n", path
);
375 data
= malloc(s
.st_size
);
376 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
377 ret
= syscall(__NR_init_module
, data
, s
.st_size
, options
);
379 fprintf(stderr
, "failed to insert %s\n", path
);
381 fprintf(stderr
, "failed to read full module %s\n", path
);
390 static int deps_available(struct module
*m
)
392 char *deps
= m
->depends
;
395 if (!strcmp(deps
, "-"))
397 while (*deps
&& (NULL
!= ((comma
= strstr(deps
, ","))))) {
400 m
= find_module(deps
);
402 if (!m
|| (m
->state
!= LOADED
))
411 static int load_depmod(void)
419 avl_for_each_element(&modules
, m
, avl
) {
420 if ((m
->state
== PROBE
) && (!deps_available(m
))) {
421 if (!insert_module(get_module_path(m
->name
), "")) {
427 } else if (m
->state
== PROBE
) {
431 // printf("loaded %d modules this pass\n", loaded);
434 // printf("missing todos %d\n", todo);
439 static int print_insmod_usage(void)
441 fprintf(stderr
, "Usage:\n\tinsmod filename [args]\n");
446 static int print_usage(char *arg
)
448 fprintf(stderr
, "Usage:\n\t%s module\n", arg
);
453 static int main_insmod(int argc
, char **argv
)
455 char options
[256] = "";
460 return print_insmod_usage();
462 name
= get_module_name(argv
[1]);
464 fprintf(stderr
, "cannot find module - %s\n", argv
[1]);
468 if (scan_loaded_modules())
471 if (find_module(name
)) {
472 fprintf(stderr
, "module is already loaded - %s\n", name
);
479 for (i
= 2; i
< argc
; i
++)
480 if (snprintf(options
, sizeof(options
), "%s %s", options
, argv
[i
]) >= sizeof(options
)) {
481 fprintf(stderr
, "argument line too long - %s\n", options
);
485 return insert_module(get_module_path(name
), options
);
488 static int main_rmmod(int argc
, char **argv
)
495 return print_usage("rmmod");
497 if (scan_loaded_modules())
500 name
= get_module_name(argv
[1]);
501 m
= find_module(name
);
503 fprintf(stderr
, "module is not loaded\n");
508 ret
= syscall(__NR_delete_module
, name
, 0);
511 fprintf(stderr
, "unloading the module failed\n");
516 static int main_lsmod(int argc
, char **argv
)
520 if (scan_loaded_modules())
523 avl_for_each_element(&modules
, m
, avl
)
524 if (m
->state
== LOADED
)
525 printf("%-20s%8d%3d %s\n",
526 m
->name
, m
->size
, m
->usage
,
527 (*m
->depends
== '-') ? ("") : (m
->depends
));
534 static int main_modinfo(int argc
, char **argv
)
539 return print_usage("modinfo");
541 module
= get_module_path(argv
[1]);
543 fprintf(stderr
, "cannot find module - %s\n", argv
[1]);
547 print_modinfo(module
);
552 static int main_depmod(int argc
, char **argv
)
560 return print_usage("depmod");
562 if (scan_loaded_modules())
566 snprintf(path
, sizeof(path
), DEF_MOD_PATH
"*.ko", ver
.release
);
568 scan_module_folder(path
);
570 name
= get_module_name(argv
[1]);
571 m
= find_module(name
);
572 if (m
&& m
->state
== LOADED
) {
573 fprintf(stderr
, "%s is already loaded\n", name
);
576 fprintf(stderr
, "failed to find a module named %s\n", name
);
587 static int main_loader(int argc
, char **argv
)
589 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
590 char *dir
= "/etc/modules.d/*";
598 path
= malloc(strlen(dir
) + 2);
602 scan_loaded_modules();
604 syslog(0, "kmodloader: loading kernel modules from %s\n", path
);
606 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
609 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
610 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
614 fprintf(stderr
, "failed to open %s\n", gl
.gl_pathv
[j
]);
618 while (fgets(mod
, sizeof(mod
), fp
)) {
619 char *nl
= strchr(mod
, '\n');
626 opts
= strchr(mod
, ' ');
630 m
= find_module(get_module_name(mod
));
633 insert_module(get_module_path(mod
), (opts
) ? (opts
) : (""));
645 int main(int argc
, char **argv
)
647 char *exec
= basename(*argv
);
649 avl_init(&modules
, avl_strcmp
, false, NULL
);
650 if (!strcmp(exec
, "insmod"))
651 return main_insmod(argc
, argv
);
653 if (!strcmp(exec
, "rmmod"))
654 return main_rmmod(argc
, argv
);
656 if (!strcmp(exec
, "lsmod"))
657 return main_lsmod(argc
, argv
);
659 if (!strcmp(exec
, "modinfo"))
660 return main_modinfo(argc
, argv
);
662 if (!strcmp(exec
, "depmod"))
663 return main_depmod(argc
, argv
);
665 return main_loader(argc
, argv
);