kmodloader: convert to ulog() api
[project/ubox.git] / kmodloader.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
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
8 *
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.
13 */
14
15 #define _GNU_SOURCE
16 #include <sys/syscall.h>
17 #include <sys/mman.h>
18 #include <sys/utsname.h>
19
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <sys/syscall.h>
23 #include <sys/types.h>
24 #include <values.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <syslog.h>
31 #include <libgen.h>
32 #include <glob.h>
33 #include <elf.h>
34
35 #include <libubox/avl.h>
36 #include <libubox/avl-cmp.h>
37 #include <libubox/utils.h>
38 #include <libubox/ulog.h>
39
40 #define DEF_MOD_PATH "/modules/%s/"
41
42 enum {
43 SCANNED,
44 PROBE,
45 LOADED,
46 };
47
48 struct module {
49 struct avl_node avl;
50
51 char *name;
52 char *depends;
53 char *opts;
54
55 int size;
56 int usage;
57 int state;
58 int error;
59 };
60
61 static struct avl_tree modules;
62
63 static char **module_folders = NULL;
64
65 static int init_module_folders(void)
66 {
67 int n = 0;
68 struct stat st;
69 struct utsname ver;
70 char *s, *e, *p, path[256], ldpath[256];
71
72 e = ldpath;
73 s = getenv("LD_LIBRARY_PATH");
74
75 if (s)
76 e += snprintf(ldpath, sizeof(ldpath), "%s:", s);
77
78 e += snprintf(e, sizeof(ldpath) - (e - ldpath), "/lib");
79
80 uname(&ver);
81
82 for (s = p = ldpath; p <= e; p++) {
83 if (*p != ':' && *p != '\0')
84 continue;
85
86 *p = 0;
87 snprintf(path, sizeof(path), "%s" DEF_MOD_PATH, s, ver.release);
88
89 if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
90 module_folders = realloc(module_folders, sizeof(p) * (n + 2));
91
92 if (!module_folders)
93 return -1;
94
95 module_folders[n++] = strdup(path);
96 }
97
98 s = p + 1;
99 }
100
101 if (!module_folders)
102 return -1;
103
104 module_folders[n] = NULL;
105 return 0;
106 }
107
108 static struct module *find_module(const char *name)
109 {
110 struct module *m;
111 return avl_find_element(&modules, name, m, avl);
112 }
113
114 static void free_modules(void)
115 {
116 struct module *m, *tmp;
117
118 avl_remove_all_elements(&modules, m, avl, tmp)
119 free(m);
120 }
121
122 static char* get_module_path(char *name)
123 {
124 char **p;
125 static char path[256];
126 struct stat s;
127
128 if (!stat(name, &s) && S_ISREG(s.st_mode))
129 return name;
130
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))
134 return path;
135 }
136
137 return NULL;
138 }
139
140 static char* get_module_name(char *path)
141 {
142 static char name[32];
143 char *t;
144
145 strncpy(name, basename(path), sizeof(name));
146
147 t = strstr(name, ".ko");
148 if (t)
149 *t = '\0';
150
151 return name;
152 }
153
154 static int elf64_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
155 {
156 const char *secnames;
157 Elf64_Ehdr *e;
158 Elf64_Shdr *sh;
159 int i;
160
161 e = (Elf64_Ehdr *) map;
162 sh = (Elf64_Shdr *) (map + e->e_shoff);
163
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;
169 return 0;
170 }
171 }
172
173 return -1;
174 }
175
176 static int elf32_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
177 {
178 const char *secnames;
179 Elf32_Ehdr *e;
180 Elf32_Shdr *sh;
181 int i;
182
183 e = (Elf32_Ehdr *) map;
184 sh = (Elf32_Shdr *) (map + e->e_shoff);
185
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;
191 return 0;
192 }
193 }
194
195 return -1;
196 }
197
198 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
199 {
200 int clazz = map[EI_CLASS];
201
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);
206
207 ULOG_ERR("unknown elf format %d\n", clazz);
208
209 return -1;
210 }
211
212 static struct module *
213 alloc_module(const char *name, const char *depends, int size)
214 {
215 struct module *m;
216 char *_name, *_dep;
217
218 m = calloc_a(sizeof(*m),
219 &_name, strlen(name) + 1,
220 &_dep, depends ? strlen(depends) + 2 : 0);
221 if (!m)
222 return NULL;
223
224 m->avl.key = m->name = strcpy(_name, name);
225 m->opts = 0;
226
227 if (depends) {
228 m->depends = strcpy(_dep, depends);
229 while (*_dep) {
230 if (*_dep == ',')
231 *_dep = '\0';
232 _dep++;
233 }
234 }
235
236 m->size = size;
237 avl_insert(&modules, &m->avl);
238
239 return m;
240 }
241
242 static int scan_loaded_modules(void)
243 {
244 size_t buf_len = 0;
245 char *buf = NULL;
246 FILE *fp;
247
248 fp = fopen("/proc/modules", "r");
249 if (!fp) {
250 ULOG_ERR("failed to open /proc/modules\n");
251 return -1;
252 }
253
254 while (getline(&buf, &buf_len, fp) > 0) {
255 struct module m;
256 struct module *n;
257
258 m.name = strtok(buf, " ");
259 m.size = atoi(strtok(NULL, " "));
260 m.usage = atoi(strtok(NULL, " "));
261 m.depends = strtok(NULL, " ");
262
263 if (!m.name || !m.depends)
264 continue;
265
266 n = alloc_module(m.name, m.depends, m.size);
267 n->usage = m.usage;
268 n->state = LOADED;
269 }
270 free(buf);
271 fclose(fp);
272
273 return 0;
274 }
275
276 static struct module* get_module_info(const char *module, const char *name)
277 {
278 int fd = open(module, O_RDONLY);
279 unsigned int offset, size;
280 char *map, *strings, *dep = NULL;
281 struct module *m;
282 struct stat s;
283
284 if (!fd) {
285 ULOG_ERR("failed to open %s\n", module);
286 return NULL;
287 }
288
289 if (fstat(fd, &s) == -1) {
290 ULOG_ERR("failed to stat %s\n", module);
291 return NULL;
292 }
293
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);
297 return NULL;
298 }
299
300 if (elf_find_section(map, ".modinfo", &offset, &size)) {
301 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
302 return NULL;
303 }
304
305 strings = map + offset;
306 while (strings && (strings < map + offset + size)) {
307 char *sep;
308 int len;
309
310 while (!strings[0])
311 strings++;
312 sep = strstr(strings, "=");
313 if (!sep)
314 break;
315 len = sep - strings;
316 sep++;
317 if (!strncmp(strings, "depends=", len + 1))
318 dep = sep;
319 strings = &sep[strlen(sep)];
320 }
321
322 m = alloc_module(name, dep, s.st_size);
323 if (!m)
324 return NULL;
325
326 m->state = SCANNED;
327
328 return m;
329 }
330
331 static int scan_module_folder(const char *dir)
332 {
333 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
334 struct utsname ver;
335 char *path;
336 glob_t gl;
337 int j;
338
339 uname(&ver);
340 path = alloca(strlen(dir) + sizeof("*.ko") + 1);
341 sprintf(path, "%s*.ko", dir);
342
343 if (glob(path, gl_flags, NULL, &gl) < 0)
344 return -1;
345
346 for (j = 0; j < gl.gl_pathc; j++) {
347 char *name = get_module_name(gl.gl_pathv[j]);
348 struct module *m;
349
350 if (!name)
351 continue;
352
353 m = find_module(name);
354 if (!m)
355 get_module_info(gl.gl_pathv[j], name);
356 }
357
358 globfree(&gl);
359
360 return 0;
361 }
362
363 static int scan_module_folders(void)
364 {
365 int rv = 0;
366 char **p;
367
368 if (init_module_folders())
369 return -1;
370
371 for (p = module_folders; *p; p++)
372 rv |= scan_module_folder(*p);
373
374 return rv;
375 }
376
377 static int print_modinfo(char *module)
378 {
379 int fd = open(module, O_RDONLY);
380 unsigned int offset, size;
381 struct stat s;
382 char *map, *strings;
383
384 if (!fd) {
385 ULOG_ERR("failed to open %s\n", module);
386 return -1;
387 }
388
389 if (fstat(fd, &s) == -1) {
390 ULOG_ERR("failed to stat %s\n", module);
391 return -1;
392 }
393
394 map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
395 if (map == MAP_FAILED) {
396 ULOG_ERR("failed to mmap %s\n", module);
397 return -1;
398 }
399
400 if (elf_find_section(map, ".modinfo", &offset, &size)) {
401 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
402 return -1;
403 }
404
405 strings = map + offset;
406 printf("module:\t\t%s\n", module);
407 while (strings && (strings < map + offset + size)) {
408 char *dup = NULL;
409 char *sep;
410
411 while (!strings[0])
412 strings++;
413 sep = strstr(strings, "=");
414 if (!sep)
415 break;
416 dup = strndup(strings, sep - strings);
417 sep++;
418 if (strncmp(strings, "parm", 4)) {
419 if (strlen(dup) < 7)
420 printf("%s:\t\t%s\n", dup, sep);
421 else
422 printf("%s:\t%s\n", dup, sep);
423 }
424 strings = &sep[strlen(sep)];
425 if (dup)
426 free(dup);
427 }
428
429 return 0;
430 }
431
432 static int deps_available(struct module *m, int verbose)
433 {
434 char *dep;
435 int err = 0;
436
437 if (!strcmp(m->depends, "-") || !strcmp(m->depends, ""))
438 return 0;
439
440 dep = m->depends;
441
442 while (*dep) {
443 m = find_module(dep);
444
445 if (verbose && !m)
446 ULOG_ERR("missing dependency %s\n", dep);
447 if (verbose && m && (m->state != LOADED))
448 ULOG_ERR("dependency not loaded %s\n", dep);
449 if (!m || (m->state != LOADED))
450 err++;
451 dep += strlen(dep) + 1;
452 }
453
454 return err;
455 }
456
457 static int insert_module(char *path, const char *options)
458 {
459 void *data = 0;
460 struct stat s;
461 int fd, ret = -1;
462
463 if (stat(path, &s)) {
464 ULOG_ERR("missing module %s\n", path);
465 return ret;
466 }
467
468 fd = open(path, O_RDONLY);
469 if (!fd) {
470 ULOG_ERR("cannot open %s\n", path);
471 return ret;
472 }
473
474 data = malloc(s.st_size);
475 if (read(fd, data, s.st_size) == s.st_size)
476 ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
477 else
478 ULOG_ERR("failed to read full module %s\n", path);
479
480 close(fd);
481 free(data);
482
483 return ret;
484 }
485
486 static void load_moddeps(struct module *_m)
487 {
488 char *dep;
489 struct module *m;
490
491 if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
492 return;
493
494 dep = _m->depends;
495
496 while (*dep) {
497 m = find_module(dep);
498
499 if (!m)
500 ULOG_ERR("failed to find dependency %s\n", dep);
501 if (m && (m->state != LOADED)) {
502 m->state = PROBE;
503 load_moddeps(m);
504 }
505
506 dep = dep + strlen(dep) + 1;
507 }
508 }
509
510 static int iterations = 0;
511 static int load_modprobe(void)
512 {
513 int loaded, todo;
514 struct module *m;
515
516 avl_for_each_element(&modules, m, avl)
517 if (m->state == PROBE)
518 load_moddeps(m);
519
520 do {
521 loaded = 0;
522 todo = 0;
523 avl_for_each_element(&modules, m, avl) {
524 if ((m->state == PROBE) && (!deps_available(m, 0))) {
525 if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
526 m->state = LOADED;
527 m->error = 0;
528 loaded++;
529 continue;
530 }
531 m->error = 1;
532 }
533
534 if ((m->state == PROBE) || m->error)
535 todo++;
536 }
537 iterations++;
538 } while (loaded);
539
540 return todo;
541 }
542
543 static int print_insmod_usage(void)
544 {
545 ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
546
547 return -1;
548 }
549
550 static int print_usage(char *arg)
551 {
552 ULOG_INFO("Usage:\n\t%s module\n", arg);
553
554 return -1;
555 }
556
557 static int main_insmod(int argc, char **argv)
558 {
559 char *name, *cur, *options;
560 int i, ret, len;
561
562 if (argc < 2)
563 return print_insmod_usage();
564
565 name = get_module_name(argv[1]);
566 if (!name) {
567 ULOG_ERR("cannot find module - %s\n", argv[1]);
568 return -1;
569 }
570
571 if (scan_loaded_modules())
572 return -1;
573
574 if (find_module(name)) {
575 ULOG_ERR("module is already loaded - %s\n", name);
576 return -1;
577
578 }
579
580 free_modules();
581
582 for (len = 0, i = 2; i < argc; i++)
583 len += strlen(argv[i]) + 1;
584
585 options = malloc(len);
586 options[0] = 0;
587 cur = options;
588 for (i = 2; i < argc; i++) {
589 if (options[0]) {
590 *cur = ' ';
591 cur++;
592 }
593 cur += sprintf(cur, "%s", argv[i]);
594 }
595
596 if (get_module_path(argv[1])) {
597 name = argv[1];
598 } else if (!get_module_path(name)) {
599 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
600 return -1;
601 }
602
603 ret = insert_module(get_module_path(name), options);
604 free(options);
605
606 if (ret)
607 ULOG_ERR("failed to insert %s\n", get_module_path(name));
608
609 return ret;
610 }
611
612 static int main_rmmod(int argc, char **argv)
613 {
614 struct module *m;
615 char *name;
616 int ret;
617
618 if (argc != 2)
619 return print_usage("rmmod");
620
621 if (scan_loaded_modules())
622 return -1;
623
624 name = get_module_name(argv[1]);
625 m = find_module(name);
626 if (!m) {
627 ULOG_ERR("module is not loaded\n");
628 return -1;
629 }
630 ret = syscall(__NR_delete_module, m->name, 0);
631
632 if (ret)
633 ULOG_ERR("unloading the module failed\n");
634
635 free_modules();
636
637 return ret;
638 }
639
640 static int main_lsmod(int argc, char **argv)
641 {
642 struct module *m;
643
644 if (scan_loaded_modules())
645 return -1;
646
647 avl_for_each_element(&modules, m, avl)
648 if (m->state == LOADED)
649 printf("%-20s%8d%3d %s\n",
650 m->name, m->size, m->usage,
651 (*m->depends == '-') ? ("") : (m->depends));
652
653 free_modules();
654
655 return 0;
656 }
657
658 static int main_modinfo(int argc, char **argv)
659 {
660 struct module *m;
661 char *name;
662
663 if (argc != 2)
664 return print_usage("modinfo");
665
666 if (scan_module_folders())
667 return -1;
668
669 name = get_module_name(argv[1]);
670 m = find_module(name);
671 if (!m) {
672 ULOG_ERR("cannot find module - %s\n", argv[1]);
673 return -1;
674 }
675
676 name = get_module_path(m->name);
677 if (!name) {
678 ULOG_ERR("cannot find path of module - %s\n", m->name);
679 return -1;
680 }
681
682 print_modinfo(name);
683
684 return 0;
685 }
686
687 static int main_modprobe(int argc, char **argv)
688 {
689 struct module *m;
690 char *name;
691
692 if (argc != 2)
693 return print_usage("modprobe");
694
695 if (scan_loaded_modules())
696 return -1;
697
698 if (scan_module_folders())
699 return -1;
700
701 name = get_module_name(argv[1]);
702 m = find_module(name);
703 if (m && m->state == LOADED) {
704 ULOG_ERR("%s is already loaded\n", name);
705 return -1;
706 } else if (!m) {
707 ULOG_ERR("failed to find a module named %s\n", name);
708 } else {
709 int fail;
710
711 m->state = PROBE;
712
713 fail = load_modprobe();
714
715 if (fail) {
716 ULOG_ERR("%d module%s could not be probed\n",
717 fail, (fail == 1) ? ("") : ("s"));
718
719 avl_for_each_element(&modules, m, avl)
720 if ((m->state == PROBE) || m->error)
721 ULOG_ERR("- %s\n", m->name);
722 }
723 }
724
725 free_modules();
726
727 return 0;
728 }
729
730 static int main_loader(int argc, char **argv)
731 {
732 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
733 char *dir = "/etc/modules.d/*";
734 struct module *m;
735 glob_t gl;
736 char *path;
737 int fail, j;
738
739 if (argc > 1)
740 dir = argv[1];
741
742 path = malloc(strlen(dir) + 2);
743 strcpy(path, dir);
744 strcat(path, "*");
745
746 if (scan_loaded_modules())
747 return -1;
748
749 if (scan_module_folders())
750 return -1;
751
752 syslog(0, "kmodloader: loading kernel modules from %s\n", path);
753
754 if (glob(path, gl_flags, NULL, &gl) < 0)
755 goto out;
756
757 for (j = 0; j < gl.gl_pathc; j++) {
758 FILE *fp = fopen(gl.gl_pathv[j], "r");
759 size_t mod_len = 0;
760 char *mod = NULL;
761
762 if (!fp) {
763 ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
764 continue;
765 }
766
767 while (getline(&mod, &mod_len, fp) > 0) {
768 char *nl = strchr(mod, '\n');
769 struct module *m;
770 char *opts;
771
772 if (nl)
773 *nl = '\0';
774
775 opts = strchr(mod, ' ');
776 if (opts)
777 *opts++ = '\0';
778
779 m = find_module(get_module_name(mod));
780 if (!m || (m->state == LOADED))
781 continue;
782
783 if (opts)
784 m->opts = strdup(opts);
785 m->state = PROBE;
786 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
787 load_modprobe();
788
789 }
790 free(mod);
791 fclose(fp);
792 }
793
794 fail = load_modprobe();
795
796 if (fail) {
797 ULOG_ERR("%d module%s could not be probed\n",
798 fail, (fail == 1) ? ("") : ("s"));
799
800 avl_for_each_element(&modules, m, avl)
801 if ((m->state == PROBE) || (m->error))
802 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
803 }
804
805 out:
806 globfree(&gl);
807 free(path);
808
809 return 0;
810 }
811
812 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
813 {
814 const char *s1 = k1;
815 const char *s2 = k2;
816
817 while (*s1 && ((*s1 == *s2) ||
818 ((*s1 == '_') && (*s2 == '-')) ||
819 ((*s1 == '-') && (*s2 == '_'))))
820 {
821 s1++;
822 s2++;
823 }
824
825 return *(const unsigned char *)s1 - *(const unsigned char *)s2;
826 }
827
828 int main(int argc, char **argv)
829 {
830 char *exec = basename(*argv);
831
832 avl_init(&modules, avl_modcmp, false, NULL);
833 if (!strcmp(exec, "insmod"))
834 return main_insmod(argc, argv);
835
836 if (!strcmp(exec, "rmmod"))
837 return main_rmmod(argc, argv);
838
839 if (!strcmp(exec, "lsmod"))
840 return main_lsmod(argc, argv);
841
842 if (!strcmp(exec, "modinfo"))
843 return main_modinfo(argc, argv);
844
845 if (!strcmp(exec, "modprobe"))
846 return main_modprobe(argc, argv);
847
848 return main_loader(argc, argv);
849 }