5667654ee1e2d5c910d7f4e839541a672d5e184a
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/list.h>
36 #define DEF_MOD_PATH "/lib/modules/%s/"
46 struct list_head list
;
56 static LIST_HEAD(modules
);
58 static struct module
* find_module(char *name
)
60 struct module
*m
= NULL
;
62 list_for_each_entry(m
, &modules
, list
)
63 if (!strcmp(m
->name
, name
))
68 static void free_module(struct module
*m
)
77 static void free_modules(void)
79 struct list_head
*p
, *n
;
81 list_for_each_safe(p
, n
, &modules
) {
82 struct module
*m
= container_of(p
, struct module
, list
);
88 static char* get_module_path(char *name
)
90 static char path
[256];
99 snprintf(path
, 256, DEF_MOD_PATH
"%s.ko", ver
.release
, name
);
111 snprintf(path
, 256, DEF_MOD_PATH
"%s.ko", ver
.release
, name
);
119 static char* get_module_name(char *path
)
121 static char name
[32];
124 strncpy(name
, basename(path
), sizeof(name
));
126 t
= strstr(name
, ".ko");
140 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
142 const char *secnames
;
147 e
= (Elf64_Ehdr
*) map
;
148 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
150 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
151 for (i
= 0; i
< e
->e_shnum
; i
++) {
152 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
153 *size
= sh
[i
].sh_size
;
154 *offset
= sh
[i
].sh_offset
;
162 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
164 const char *secnames
;
169 e
= (Elf32_Ehdr
*) map
;
170 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
172 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
173 for (i
= 0; i
< e
->e_shnum
; i
++) {
174 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
175 *size
= sh
[i
].sh_size
;
176 *offset
= sh
[i
].sh_offset
;
185 static int scan_loaded_modules(void)
187 FILE *fp
= fopen("/proc/modules", "r");
191 fprintf(stderr
, "failed to open /proc/modules\n");
195 while (fgets(buf
, sizeof(buf
), fp
)) {
199 m
.name
= strtok(buf
, " ");
200 m
.size
= atoi(strtok(NULL
, " "));
201 m
.usage
= atoi(strtok(NULL
, " "));
202 m
.depends
= strtok(NULL
, " ");
204 if (!m
.name
|| !m
.depends
)
207 n
= malloc(sizeof(struct module
));
211 n
->name
= strdup(m
.name
);
212 n
->depends
= strdup(m
.depends
);
217 list_add_tail(&n
->list
, &modules
);
223 static struct module
* get_module_info(char *module
)
225 int fd
= open(module
, O_RDONLY
);
226 unsigned int offset
, size
;
232 fprintf(stderr
, "failed to open %s\n", module
);
236 if (fstat(fd
, &s
) == -1) {
237 fprintf(stderr
, "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 fprintf(stderr
, "failed to mmap %s\n", module
);
247 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
248 fprintf(stderr
, "failed to load the .modinfo section from %s\n", module
);
252 strings
= map
+ offset
;
253 m
= malloc(sizeof(struct module
));
257 memset(m
, 0, sizeof(struct module
));
259 while (strings
&& (strings
< map
+ offset
+ size
)) {
265 sep
= strstr(strings
, "=");
270 if (!strncmp(strings
, "depends=", len
+ 1))
271 m
->depends
= strdup(sep
);
272 strings
= &sep
[strlen(sep
)];
280 static int scan_module_folder(char *dir
)
282 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
286 if (glob(dir
, gl_flags
, NULL
, &gl
) < 0)
289 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
290 char *name
= get_module_name(gl
.gl_pathv
[j
]);
296 m
= find_module(name
);
298 m
= get_module_info(gl
.gl_pathv
[j
]);
299 m
->name
= strdup(name
);
300 list_add_tail(&m
->list
, &modules
);
309 static int print_modinfo(char *module
)
311 int fd
= open(module
, O_RDONLY
);
312 unsigned int offset
, size
;
317 fprintf(stderr
, "failed to open %s\n", module
);
321 if (fstat(fd
, &s
) == -1) {
322 fprintf(stderr
, "failed to stat %s\n", module
);
326 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
327 if (map
== MAP_FAILED
) {
328 fprintf(stderr
, "failed to mmap %s\n", module
);
332 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
333 fprintf(stderr
, "failed to load the .modinfo section from %s\n", module
);
337 strings
= map
+ offset
;
338 printf("module:\t\t%s\n", module
);
339 while (strings
&& (strings
< map
+ offset
+ size
)) {
345 sep
= strstr(strings
, "=");
348 dup
= strndup(strings
, sep
- strings
);
350 if (strncmp(strings
, "parm", 4)) {
352 printf("%s:\t\t%s\n", dup
, sep
);
354 printf("%s:\t%s\n", dup
, sep
);
356 strings
= &sep
[strlen(sep
)];
364 static int insert_module(char *path
, const char *options
)
370 if (stat(path
, &s
)) {
371 fprintf(stderr
, "missing module %s\n", path
);
375 fd
= open(path
, O_RDONLY
);
377 fprintf(stderr
, "cannot open %s\n", path
);
381 data
= malloc(s
.st_size
);
382 if (read(fd
, data
, s
.st_size
) == s
.st_size
) {
383 ret
= syscall(__NR_init_module
, data
, s
.st_size
, options
);
385 fprintf(stderr
, "failed to insert %s\n", path
);
387 fprintf(stderr
, "failed to read full module %s\n", path
);
396 static int deps_available(struct module
*m
)
398 char *deps
= m
->depends
;
401 if (!strcmp(deps
, "-"))
403 while (*deps
&& (NULL
!= ((comma
= strstr(deps
, ","))))) {
406 m
= find_module(deps
);
408 if (!m
|| (m
->state
!= LOADED
))
417 static int load_depmod(void)
425 list_for_each_entry(m
, &modules
, list
) {
426 if ((m
->state
== PROBE
) && (!deps_available(m
))) {
427 if (!insert_module(get_module_path(m
->name
), "")) {
433 } else if (m
->state
== PROBE
) {
437 // printf("loaded %d modules this pass\n", loaded);
440 // printf("missing todos %d\n", todo);
445 static int print_insmod_usage(void)
447 fprintf(stderr
, "Usage:\n\tinsmod filename [args]\n");
452 static int print_usage(char *arg
)
454 fprintf(stderr
, "Usage:\n\t%s module\n", arg
);
459 static int main_insmod(int argc
, char **argv
)
461 char options
[256] = "";
466 return print_insmod_usage();
468 name
= get_module_name(argv
[1]);
470 fprintf(stderr
, "cannot find module - %s\n", argv
[1]);
474 if (scan_loaded_modules())
477 if (find_module(name
)) {
478 fprintf(stderr
, "module is already loaded - %s\n", name
);
485 for (i
= 2; i
< argc
; i
++)
486 if (snprintf(options
, sizeof(options
), "%s %s", options
, argv
[i
]) >= sizeof(options
)) {
487 fprintf(stderr
, "argument line too long - %s\n", options
);
491 return insert_module(get_module_path(name
), options
);
494 static int main_rmmod(int argc
, char **argv
)
501 return print_usage("rmmod");
503 if (scan_loaded_modules())
506 name
= get_module_name(argv
[1]);
507 m
= find_module(name
);
509 fprintf(stderr
, "module is not loaded\n");
514 ret
= syscall(__NR_delete_module
, name
, 0);
517 fprintf(stderr
, "unloading the module failed\n");
522 static int main_lsmod(int argc
, char **argv
)
526 if (scan_loaded_modules())
529 list_for_each_entry(m
, &modules
, list
)
530 if (m
->state
== LOADED
)
531 printf("%-20s%8d%3d %s\n",
532 m
->name
, m
->size
, m
->usage
,
533 (*m
->depends
== '-') ? ("") : (m
->depends
));
540 static int main_modinfo(int argc
, char **argv
)
545 return print_usage("modinfo");
547 module
= get_module_path(argv
[1]);
549 fprintf(stderr
, "cannot find module - %s\n", argv
[1]);
553 print_modinfo(module
);
558 static int main_depmod(int argc
, char **argv
)
566 return print_usage("depmod");
568 if (scan_loaded_modules())
572 snprintf(path
, sizeof(path
), DEF_MOD_PATH
"*.ko", ver
.release
);
574 scan_module_folder(path
);
576 name
= get_module_name(argv
[1]);
577 m
= find_module(name
);
578 if (m
&& m
->state
== LOADED
) {
579 fprintf(stderr
, "%s is already loaded\n", name
);
582 fprintf(stderr
, "failed to find a module named %s\n", name
);
593 static int main_loader(int argc
, char **argv
)
595 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
596 char *dir
= "/etc/modules.d/*";
603 path
= malloc(strlen(dir
) + 2);
607 scan_loaded_modules();
609 syslog(0, "kmodloader: loading kernel modules from %s\n", path
);
611 if (glob(path
, gl_flags
, NULL
, &gl
) >= 0) {
614 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
615 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
618 fprintf(stderr
, "failed to open %s\n", gl
.gl_pathv
[j
]);
622 while (fgets(mod
, sizeof(mod
), fp
)) {
623 char *nl
= strchr(mod
, '\n');
630 opts
= strchr(mod
, ' ');
634 m
= find_module(get_module_name(mod
));
637 insert_module(get_module_path(mod
), (opts
) ? (opts
) : (""));
650 int main(int argc
, char **argv
)
652 char *exec
= basename(*argv
);
654 if (!strcmp(exec
, "insmod"))
655 return main_insmod(argc
, argv
);
657 if (!strcmp(exec
, "rmmod"))
658 return main_rmmod(argc
, argv
);
660 if (!strcmp(exec
, "lsmod"))
661 return main_lsmod(argc
, argv
);
663 if (!strcmp(exec
, "modinfo"))
664 return main_modinfo(argc
, argv
);
666 if (!strcmp(exec
, "depmod"))
667 return main_depmod(argc
, argv
);
669 return main_loader(argc
, argv
);