properly handle return code of pipe() syscall
[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 init_module_folders();
597
598 if (get_module_path(argv[1])) {
599 name = argv[1];
600 } else if (!get_module_path(name)) {
601 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
602 return -1;
603 }
604
605 ret = insert_module(get_module_path(name), options);
606 free(options);
607
608 if (ret)
609 ULOG_ERR("failed to insert %s\n", get_module_path(name));
610
611 return ret;
612 }
613
614 static int main_rmmod(int argc, char **argv)
615 {
616 struct module *m;
617 char *name;
618 int ret;
619
620 if (argc != 2)
621 return print_usage("rmmod");
622
623 if (scan_loaded_modules())
624 return -1;
625
626 name = get_module_name(argv[1]);
627 m = find_module(name);
628 if (!m) {
629 ULOG_ERR("module is not loaded\n");
630 return -1;
631 }
632 ret = syscall(__NR_delete_module, m->name, 0);
633
634 if (ret)
635 ULOG_ERR("unloading the module failed\n");
636
637 free_modules();
638
639 return ret;
640 }
641
642 static int main_lsmod(int argc, char **argv)
643 {
644 struct module *m;
645
646 if (scan_loaded_modules())
647 return -1;
648
649 avl_for_each_element(&modules, m, avl)
650 if (m->state == LOADED)
651 printf("%-20s%8d%3d %s\n",
652 m->name, m->size, m->usage,
653 (*m->depends == '-') ? ("") : (m->depends));
654
655 free_modules();
656
657 return 0;
658 }
659
660 static int main_modinfo(int argc, char **argv)
661 {
662 struct module *m;
663 char *name;
664
665 if (argc != 2)
666 return print_usage("modinfo");
667
668 if (scan_module_folders())
669 return -1;
670
671 name = get_module_name(argv[1]);
672 m = find_module(name);
673 if (!m) {
674 ULOG_ERR("cannot find module - %s\n", argv[1]);
675 return -1;
676 }
677
678 name = get_module_path(m->name);
679 if (!name) {
680 ULOG_ERR("cannot find path of module - %s\n", m->name);
681 return -1;
682 }
683
684 print_modinfo(name);
685
686 return 0;
687 }
688
689 static int main_modprobe(int argc, char **argv)
690 {
691 struct module *m;
692 char *name;
693
694 if (argc != 2)
695 return print_usage("modprobe");
696
697 if (scan_loaded_modules())
698 return -1;
699
700 if (scan_module_folders())
701 return -1;
702
703 name = get_module_name(argv[1]);
704 m = find_module(name);
705 if (m && m->state == LOADED) {
706 ULOG_ERR("%s is already loaded\n", name);
707 return -1;
708 } else if (!m) {
709 ULOG_ERR("failed to find a module named %s\n", name);
710 } else {
711 int fail;
712
713 m->state = PROBE;
714
715 fail = load_modprobe();
716
717 if (fail) {
718 ULOG_ERR("%d module%s could not be probed\n",
719 fail, (fail == 1) ? ("") : ("s"));
720
721 avl_for_each_element(&modules, m, avl)
722 if ((m->state == PROBE) || m->error)
723 ULOG_ERR("- %s\n", m->name);
724 }
725 }
726
727 free_modules();
728
729 return 0;
730 }
731
732 static int main_loader(int argc, char **argv)
733 {
734 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
735 char *dir = "/etc/modules.d/*";
736 struct module *m;
737 glob_t gl;
738 char *path;
739 int fail, j;
740
741 if (argc > 1)
742 dir = argv[1];
743
744 path = malloc(strlen(dir) + 2);
745 strcpy(path, dir);
746 strcat(path, "*");
747
748 if (scan_loaded_modules())
749 return -1;
750
751 if (scan_module_folders())
752 return -1;
753
754 syslog(LOG_INFO, "kmodloader: loading kernel modules from %s\n", path);
755
756 if (glob(path, gl_flags, NULL, &gl) < 0)
757 goto out;
758
759 for (j = 0; j < gl.gl_pathc; j++) {
760 FILE *fp = fopen(gl.gl_pathv[j], "r");
761 size_t mod_len = 0;
762 char *mod = NULL;
763
764 if (!fp) {
765 ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
766 continue;
767 }
768
769 while (getline(&mod, &mod_len, fp) > 0) {
770 char *nl = strchr(mod, '\n');
771 struct module *m;
772 char *opts;
773
774 if (nl)
775 *nl = '\0';
776
777 opts = strchr(mod, ' ');
778 if (opts)
779 *opts++ = '\0';
780
781 m = find_module(get_module_name(mod));
782 if (!m || (m->state == LOADED))
783 continue;
784
785 if (opts)
786 m->opts = strdup(opts);
787 m->state = PROBE;
788 if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
789 load_modprobe();
790
791 }
792 free(mod);
793 fclose(fp);
794 }
795
796 fail = load_modprobe();
797
798 if (fail) {
799 ULOG_ERR("%d module%s could not be probed\n",
800 fail, (fail == 1) ? ("") : ("s"));
801
802 avl_for_each_element(&modules, m, avl)
803 if ((m->state == PROBE) || (m->error))
804 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
805 }
806
807 out:
808 globfree(&gl);
809 free(path);
810
811 return 0;
812 }
813
814 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
815 {
816 const char *s1 = k1;
817 const char *s2 = k2;
818
819 while (*s1 && ((*s1 == *s2) ||
820 ((*s1 == '_') && (*s2 == '-')) ||
821 ((*s1 == '-') && (*s2 == '_'))))
822 {
823 s1++;
824 s2++;
825 }
826
827 return *(const unsigned char *)s1 - *(const unsigned char *)s2;
828 }
829
830 int main(int argc, char **argv)
831 {
832 char *exec = basename(*argv);
833
834 avl_init(&modules, avl_modcmp, false, NULL);
835 if (!strcmp(exec, "insmod"))
836 return main_insmod(argc, argv);
837
838 if (!strcmp(exec, "rmmod"))
839 return main_rmmod(argc, argv);
840
841 if (!strcmp(exec, "lsmod"))
842 return main_lsmod(argc, argv);
843
844 if (!strcmp(exec, "modinfo"))
845 return main_modinfo(argc, argv);
846
847 if (!strcmp(exec, "modprobe"))
848 return main_modprobe(argc, argv);
849
850 return main_loader(argc, argv);
851 }