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>
38 #include <libubox/ulog.h>
40 #define DEF_MOD_PATH "/modules/%s/"
61 static struct avl_tree modules
;
63 static char **module_folders
= NULL
;
65 static int init_module_folders(void)
70 char *s
, *e
, *p
, path
[256], ldpath
[256];
73 s
= getenv("LD_LIBRARY_PATH");
76 e
+= snprintf(ldpath
, sizeof(ldpath
), "%s:", s
);
78 e
+= snprintf(e
, sizeof(ldpath
) - (e
- ldpath
), "/lib");
82 for (s
= p
= ldpath
; p
<= e
; p
++) {
83 if (*p
!= ':' && *p
!= '\0')
87 snprintf(path
, sizeof(path
), "%s" DEF_MOD_PATH
, s
, ver
.release
);
89 if (!stat(path
, &st
) && S_ISDIR(st
.st_mode
)) {
90 module_folders
= realloc(module_folders
, sizeof(p
) * (n
+ 2));
95 module_folders
[n
++] = strdup(path
);
104 module_folders
[n
] = NULL
;
108 static struct module
*find_module(const char *name
)
111 return avl_find_element(&modules
, name
, m
, avl
);
114 static void free_modules(void)
116 struct module
*m
, *tmp
;
118 avl_remove_all_elements(&modules
, m
, avl
, tmp
)
122 static char* get_module_path(char *name
)
125 static char path
[256];
128 if (!stat(name
, &s
) && S_ISREG(s
.st_mode
))
131 for (p
= module_folders
; *p
; p
++) {
132 snprintf(path
, sizeof(path
), "%s%s.ko", *p
, name
);
133 if (!stat(path
, &s
) && S_ISREG(s
.st_mode
))
140 static char* get_module_name(char *path
)
142 static char name
[32];
145 strncpy(name
, basename(path
), sizeof(name
));
147 t
= strstr(name
, ".ko");
154 static int elf64_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
156 const char *secnames
;
161 e
= (Elf64_Ehdr
*) map
;
162 sh
= (Elf64_Shdr
*) (map
+ e
->e_shoff
);
164 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
165 for (i
= 0; i
< e
->e_shnum
; i
++) {
166 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
167 *size
= sh
[i
].sh_size
;
168 *offset
= sh
[i
].sh_offset
;
176 static int elf32_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
178 const char *secnames
;
183 e
= (Elf32_Ehdr
*) map
;
184 sh
= (Elf32_Shdr
*) (map
+ e
->e_shoff
);
186 secnames
= map
+ sh
[e
->e_shstrndx
].sh_offset
;
187 for (i
= 0; i
< e
->e_shnum
; i
++) {
188 if (!strcmp(section
, secnames
+ sh
[i
].sh_name
)) {
189 *size
= sh
[i
].sh_size
;
190 *offset
= sh
[i
].sh_offset
;
198 static int elf_find_section(char *map
, const char *section
, unsigned int *offset
, unsigned int *size
)
200 int clazz
= map
[EI_CLASS
];
202 if (clazz
== ELFCLASS32
)
203 return elf32_find_section(map
, section
, offset
, size
);
204 else if (clazz
== ELFCLASS64
)
205 return elf64_find_section(map
, section
, offset
, size
);
207 ULOG_ERR("unknown elf format %d\n", clazz
);
212 static struct module
*
213 alloc_module(const char *name
, const char *depends
, int size
)
218 m
= calloc_a(sizeof(*m
),
219 &_name
, strlen(name
) + 1,
220 &_dep
, depends
? strlen(depends
) + 2 : 0);
224 m
->avl
.key
= m
->name
= strcpy(_name
, name
);
228 m
->depends
= strcpy(_dep
, depends
);
237 avl_insert(&modules
, &m
->avl
);
242 static int scan_loaded_modules(void)
248 fp
= fopen("/proc/modules", "r");
250 ULOG_ERR("failed to open /proc/modules\n");
254 while (getline(&buf
, &buf_len
, fp
) > 0) {
258 m
.name
= strtok(buf
, " ");
259 m
.size
= atoi(strtok(NULL
, " "));
260 m
.usage
= atoi(strtok(NULL
, " "));
261 m
.depends
= strtok(NULL
, " ");
263 if (!m
.name
|| !m
.depends
)
266 n
= alloc_module(m
.name
, m
.depends
, m
.size
);
276 static struct module
* get_module_info(const char *module
, const char *name
)
278 int fd
= open(module
, O_RDONLY
);
279 unsigned int offset
, size
;
280 char *map
= MAP_FAILED
, *strings
, *dep
= NULL
;
281 struct module
*m
= NULL
;
285 ULOG_ERR("failed to open %s\n", module
);
289 if (fstat(fd
, &s
) == -1) {
290 ULOG_ERR("failed to stat %s\n", module
);
294 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
295 if (map
== MAP_FAILED
) {
296 ULOG_ERR("failed to mmap %s\n", module
);
300 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
301 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
305 strings
= map
+ offset
;
306 while (strings
&& (strings
< map
+ offset
+ size
)) {
312 sep
= strstr(strings
, "=");
317 if (!strncmp(strings
, "depends=", len
+ 1))
319 strings
= &sep
[strlen(sep
)];
322 m
= alloc_module(name
, dep
, s
.st_size
);
328 if (map
!= MAP_FAILED
)
329 munmap(map
, s
.st_size
);
337 static int scan_module_folder(const char *dir
)
339 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
346 path
= alloca(strlen(dir
) + sizeof("*.ko") + 1);
347 sprintf(path
, "%s*.ko", dir
);
349 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
352 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
353 char *name
= get_module_name(gl
.gl_pathv
[j
]);
359 m
= find_module(name
);
361 get_module_info(gl
.gl_pathv
[j
], name
);
369 static int scan_module_folders(void)
374 if (init_module_folders())
377 for (p
= module_folders
; *p
; p
++)
378 rv
|= scan_module_folder(*p
);
383 static int print_modinfo(char *module
)
385 int fd
= open(module
, O_RDONLY
);
386 unsigned int offset
, size
;
388 char *map
= MAP_FAILED
, *strings
;
392 ULOG_ERR("failed to open %s\n", module
);
396 if (fstat(fd
, &s
) == -1) {
397 ULOG_ERR("failed to stat %s\n", module
);
401 map
= mmap(NULL
, s
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
402 if (map
== MAP_FAILED
) {
403 ULOG_ERR("failed to mmap %s\n", module
);
407 if (elf_find_section(map
, ".modinfo", &offset
, &size
)) {
408 ULOG_ERR("failed to load the .modinfo section from %s\n", module
);
412 strings
= map
+ offset
;
413 printf("module:\t\t%s\n", module
);
414 while (strings
&& (strings
< map
+ offset
+ size
)) {
420 sep
= strstr(strings
, "=");
423 dup
= strndup(strings
, sep
- strings
);
425 if (strncmp(strings
, "parm", 4)) {
427 printf("%s:\t\t%s\n", dup
, sep
);
429 printf("%s:\t%s\n", dup
, sep
);
431 strings
= &sep
[strlen(sep
)];
439 if (map
!= MAP_FAILED
)
440 munmap(map
, s
.st_size
);
448 static int deps_available(struct module
*m
, int verbose
)
453 if (!m
->depends
|| !strcmp(m
->depends
, "-") || !strcmp(m
->depends
, ""))
459 m
= find_module(dep
);
462 ULOG_ERR("missing dependency %s\n", dep
);
463 if (verbose
&& m
&& (m
->state
!= LOADED
))
464 ULOG_ERR("dependency not loaded %s\n", dep
);
465 if (!m
|| (m
->state
!= LOADED
))
467 dep
+= strlen(dep
) + 1;
473 static int insert_module(char *path
, const char *options
)
479 if (stat(path
, &s
)) {
480 ULOG_ERR("missing module %s\n", path
);
484 fd
= open(path
, O_RDONLY
);
486 ULOG_ERR("cannot open %s\n", path
);
490 data
= malloc(s
.st_size
);
491 if (read(fd
, data
, s
.st_size
) == s
.st_size
)
492 ret
= syscall(__NR_init_module
, data
, (unsigned long) s
.st_size
, options
);
494 ULOG_ERR("failed to read full module %s\n", path
);
502 static void load_moddeps(struct module
*_m
)
507 if (!strcmp(_m
->depends
, "-") || !strcmp(_m
->depends
, ""))
513 m
= find_module(dep
);
516 ULOG_ERR("failed to find dependency %s\n", dep
);
517 if (m
&& (m
->state
!= LOADED
)) {
522 dep
= dep
+ strlen(dep
) + 1;
526 static int iterations
= 0;
527 static int load_modprobe(void)
532 avl_for_each_element(&modules
, m
, avl
)
533 if (m
->state
== PROBE
)
539 avl_for_each_element(&modules
, m
, avl
) {
540 if ((m
->state
== PROBE
) && (!deps_available(m
, 0))) {
541 if (!insert_module(get_module_path(m
->name
), (m
->opts
) ? (m
->opts
) : (""))) {
550 if ((m
->state
== PROBE
) || m
->error
)
559 static int print_insmod_usage(void)
561 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
566 static int print_usage(char *arg
)
568 ULOG_INFO("Usage:\n\t%s module\n", arg
);
573 static int main_insmod(int argc
, char **argv
)
575 char *name
, *cur
, *options
;
579 return print_insmod_usage();
581 name
= get_module_name(argv
[1]);
583 ULOG_ERR("cannot find module - %s\n", argv
[1]);
587 if (scan_loaded_modules())
590 if (find_module(name
)) {
591 ULOG_ERR("module is already loaded - %s\n", name
);
598 for (len
= 0, i
= 2; i
< argc
; i
++)
599 len
+= strlen(argv
[i
]) + 1;
601 options
= malloc(len
);
604 for (i
= 2; i
< argc
; i
++) {
609 cur
+= sprintf(cur
, "%s", argv
[i
]);
612 init_module_folders();
614 if (get_module_path(argv
[1])) {
616 } else if (!get_module_path(name
)) {
617 fprintf(stderr
, "Failed to find %s. Maybe it is a built in module ?\n", name
);
621 ret
= insert_module(get_module_path(name
), options
);
625 ULOG_ERR("failed to insert %s\n", get_module_path(name
));
630 static int main_rmmod(int argc
, char **argv
)
637 return print_usage("rmmod");
639 if (scan_loaded_modules())
642 name
= get_module_name(argv
[1]);
643 m
= find_module(name
);
645 ULOG_ERR("module is not loaded\n");
648 ret
= syscall(__NR_delete_module
, m
->name
, 0);
651 ULOG_ERR("unloading the module failed\n");
658 static int main_lsmod(int argc
, char **argv
)
662 if (scan_loaded_modules())
665 avl_for_each_element(&modules
, m
, avl
)
666 if (m
->state
== LOADED
)
667 printf("%-20s%8d%3d %s\n",
668 m
->name
, m
->size
, m
->usage
,
669 (*m
->depends
== '-') ? ("") : (m
->depends
));
676 static int main_modinfo(int argc
, char **argv
)
682 return print_usage("modinfo");
684 if (scan_module_folders())
687 name
= get_module_name(argv
[1]);
688 m
= find_module(name
);
690 ULOG_ERR("cannot find module - %s\n", argv
[1]);
694 name
= get_module_path(m
->name
);
696 ULOG_ERR("cannot find path of module - %s\n", m
->name
);
705 static int main_modprobe(int argc
, char **argv
)
711 return print_usage("modprobe");
713 if (scan_loaded_modules())
716 if (scan_module_folders())
719 name
= get_module_name(argv
[1]);
720 m
= find_module(name
);
721 if (m
&& m
->state
== LOADED
) {
722 ULOG_ERR("%s is already loaded\n", name
);
725 ULOG_ERR("failed to find a module named %s\n", name
);
731 fail
= load_modprobe();
734 ULOG_ERR("%d module%s could not be probed\n",
735 fail
, (fail
== 1) ? ("") : ("s"));
737 avl_for_each_element(&modules
, m
, avl
)
738 if ((m
->state
== PROBE
) || m
->error
)
739 ULOG_ERR("- %s\n", m
->name
);
748 static int main_loader(int argc
, char **argv
)
750 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
751 char *dir
= "/etc/modules.d/*";
760 path
= malloc(strlen(dir
) + 2);
764 if (scan_loaded_modules())
767 if (scan_module_folders())
770 syslog(LOG_INFO
, "kmodloader: loading kernel modules from %s\n", path
);
772 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
775 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
776 FILE *fp
= fopen(gl
.gl_pathv
[j
], "r");
781 ULOG_ERR("failed to open %s\n", gl
.gl_pathv
[j
]);
785 while (getline(&mod
, &mod_len
, fp
) > 0) {
786 char *nl
= strchr(mod
, '\n');
793 opts
= strchr(mod
, ' ');
797 m
= find_module(get_module_name(mod
));
798 if (!m
|| (m
->state
== LOADED
))
802 m
->opts
= strdup(opts
);
804 if (basename(gl
.gl_pathv
[j
])[0] - '0' <= 9)
812 fail
= load_modprobe();
815 ULOG_ERR("%d module%s could not be probed\n",
816 fail
, (fail
== 1) ? ("") : ("s"));
818 avl_for_each_element(&modules
, m
, avl
)
819 if ((m
->state
== PROBE
) || (m
->error
))
820 ULOG_ERR("- %s - %d\n", m
->name
, deps_available(m
, 1));
830 static int avl_modcmp(const void *k1
, const void *k2
, void *ptr
)
835 while (*s1
&& ((*s1
== *s2
) ||
836 ((*s1
== '_') && (*s2
== '-')) ||
837 ((*s1
== '-') && (*s2
== '_'))))
843 return *(const unsigned char *)s1
- *(const unsigned char *)s2
;
846 int main(int argc
, char **argv
)
848 char *exec
= basename(*argv
);
850 avl_init(&modules
, avl_modcmp
, false, NULL
);
851 if (!strcmp(exec
, "insmod"))
852 return main_insmod(argc
, argv
);
854 if (!strcmp(exec
, "rmmod"))
855 return main_rmmod(argc
, argv
);
857 if (!strcmp(exec
, "lsmod"))
858 return main_lsmod(argc
, argv
);
860 if (!strcmp(exec
, "modinfo"))
861 return main_modinfo(argc
, argv
);
863 if (!strcmp(exec
, "modprobe"))
864 return main_modprobe(argc
, argv
);
866 return main_loader(argc
, argv
);