kmodloader: lift restriction on module alias info
[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 <libgen.h>
31 #include <glob.h>
32 #include <elf.h>
33
34 #include <libubox/avl.h>
35 #include <libubox/avl-cmp.h>
36 #include <libubox/utils.h>
37 #include <libubox/ulog.h>
38
39 #define DEF_MOD_PATH "/modules/%s/"
40
41 enum {
42         SCANNED,
43         PROBE,
44         LOADED,
45 };
46
47 struct module {
48         char *name;
49         char *depends;
50         char *opts;
51
52         int size;
53         int usage;
54         int state;
55         int error;
56         int refcnt;                     /* number of references from module_node.m */
57 };
58
59 struct module_node {
60         struct avl_node avl;
61         struct module *m;
62         bool is_alias;
63 };
64
65 static struct avl_tree modules;
66
67 static char **module_folders = NULL;
68
69 static void free_module(struct module *m);
70
71 static int init_module_folders(void)
72 {
73         int n = 0;
74         struct stat st;
75         struct utsname ver;
76         char *s, *e, *p, path[256], ldpath[256];
77
78         e = ldpath;
79         s = getenv("LD_LIBRARY_PATH");
80
81         if (s)
82                 e += snprintf(ldpath, sizeof(ldpath), "%s:", s);
83
84         e += snprintf(e, sizeof(ldpath) - (e - ldpath), "/lib");
85
86         uname(&ver);
87
88         for (s = p = ldpath; p <= e; p++) {
89                 if (*p != ':' && *p != '\0')
90                         continue;
91
92                 *p = 0;
93                 snprintf(path, sizeof(path), "%s" DEF_MOD_PATH, s, ver.release);
94
95                 if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
96                         module_folders = realloc(module_folders, sizeof(p) * (n + 2));
97
98                         if (!module_folders)
99                                 return -1;
100
101                         module_folders[n++] = strdup(path);
102                 }
103
104                 s = p + 1;
105         }
106
107         if (!module_folders)
108                 return -1;
109
110         module_folders[n] = NULL;
111         return 0;
112 }
113
114 static struct module *find_module(const char *name)
115 {
116         struct module_node *mn;
117         mn = avl_find_element(&modules, name, mn, avl);
118         if (mn)
119                 return mn->m;
120         else
121                 return NULL;
122 }
123
124 static void free_modules(void)
125 {
126         struct module_node *mn, *tmp;
127
128         avl_remove_all_elements(&modules, mn, avl, tmp) {
129                 struct module *m = mn->m;
130
131                 m->refcnt -= 1;
132                 if (m->refcnt == 0)
133                         free_module(m);
134                 free(mn);
135         }
136 }
137
138 static char* get_module_path(char *name)
139 {
140         char **p;
141         static char path[256];
142         struct stat s;
143
144         if (!stat(name, &s) && S_ISREG(s.st_mode))
145                 return name;
146
147         for (p = module_folders; *p; p++) {
148                 snprintf(path, sizeof(path), "%s%s.ko", *p, name);
149                 if (!stat(path, &s) && S_ISREG(s.st_mode))
150                         return path;
151         }
152
153         return NULL;
154 }
155
156 static char* get_module_name(char *path)
157 {
158         static char name[33];
159         char *t;
160
161         strncpy(name, basename(path), sizeof(name) - 1);
162
163         t = strstr(name, ".ko");
164         if (t)
165                 *t = '\0';
166
167         return name;
168 }
169
170 static int elf64_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
171 {
172         const char *secnames;
173         Elf64_Ehdr *e;
174         Elf64_Shdr *sh;
175         int i;
176
177         e = (Elf64_Ehdr *) map;
178         sh = (Elf64_Shdr *) (map + e->e_shoff);
179
180         secnames = map + sh[e->e_shstrndx].sh_offset;
181         for (i = 0; i < e->e_shnum; i++) {
182                 if (!strcmp(section, secnames + sh[i].sh_name)) {
183                         *size = sh[i].sh_size;
184                         *offset = sh[i].sh_offset;
185                         return 0;
186                 }
187         }
188
189         return -1;
190 }
191
192 static int elf32_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
193 {
194         const char *secnames;
195         Elf32_Ehdr *e;
196         Elf32_Shdr *sh;
197         int i;
198
199         e = (Elf32_Ehdr *) map;
200         sh = (Elf32_Shdr *) (map + e->e_shoff);
201
202         secnames = map + sh[e->e_shstrndx].sh_offset;
203         for (i = 0; i < e->e_shnum; i++) {
204                 if (!strcmp(section, secnames + sh[i].sh_name)) {
205                         *size = sh[i].sh_size;
206                         *offset = sh[i].sh_offset;
207                         return 0;
208                 }
209         }
210
211         return -1;
212 }
213
214 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
215 {
216         int clazz = map[EI_CLASS];
217         int endian = map[EI_DATA];
218
219 #if __BYTE_ORDER == __LITTLE_ENDIAN
220         if (endian != ELFDATA2LSB)
221 #elif __BYTE_ORDER == __BIG_ENDIAN
222         if (endian != ELFDATA2MSB)
223 #else
224 #error "unsupported endian"
225 #endif
226         {
227                 ULOG_ERR("invalid endianess: %d\n", endian);
228                 return -1;
229         }
230
231         if (clazz == ELFCLASS32)
232                 return elf32_find_section(map, section, offset, size);
233         else if (clazz == ELFCLASS64)
234                 return elf64_find_section(map, section, offset, size);
235
236         ULOG_ERR("unknown elf format %d\n", clazz);
237
238         return -1;
239 }
240
241 static struct module_node *
242 alloc_module_node(const char *name, struct module *m, bool is_alias)
243 {
244         struct module_node *mn;
245         char *_name;
246
247         mn = calloc_a(sizeof(*mn),
248                 &_name, strlen(name) + 1);
249         if (mn) {
250                 mn->avl.key = strcpy(_name, name);
251                 mn->m = m;
252                 mn->is_alias = is_alias;
253                 avl_insert(&modules, &mn->avl);
254                 m->refcnt += 1;
255         }
256         return mn;
257 }
258
259 static struct module *
260 alloc_module(const char *name, const char * const *aliases, int naliases, const char *depends, int size)
261 {
262         struct module *m;
263         char *_name, *_dep;
264         int i;
265
266         m = calloc_a(sizeof(*m),
267                 &_name, strlen(name) + 1,
268                 &_dep, depends ? strlen(depends) + 2 : 0);
269         if (!m)
270                 return NULL;
271
272         m->name = strcpy(_name, name);
273         m->opts = 0;
274
275         if (depends) {
276                 m->depends = strcpy(_dep, depends);
277                 while (*_dep) {
278                         if (*_dep == ',')
279                                 *_dep = '\0';
280                         _dep++;
281                 }
282         }
283         m->size = size;
284
285         m->refcnt = 0;
286         alloc_module_node(m->name, m, false);
287         for (i = 0; i < naliases; i++)
288                 alloc_module_node(aliases[i], m, true);
289
290         return m;
291 }
292
293 static void free_module(struct module *m)
294 {
295         if (m->opts)
296                 free(m->opts);
297         free(m);
298 }
299
300 static int scan_loaded_modules(void)
301 {
302         size_t buf_len = 0;
303         char *buf = NULL;
304         FILE *fp;
305
306         fp = fopen("/proc/modules", "r");
307         if (!fp) {
308                 ULOG_ERR("failed to open /proc/modules\n");
309                 return -1;
310         }
311
312         while (getline(&buf, &buf_len, fp) > 0) {
313                 struct module m;
314                 struct module *n;
315
316                 m.name = strtok(buf, " ");
317                 m.size = atoi(strtok(NULL, " "));
318                 m.usage = atoi(strtok(NULL, " "));
319                 m.depends = strtok(NULL, " ");
320
321                 if (!m.name || !m.depends)
322                         continue;
323
324                 n = find_module(m.name);
325                 if (!n) {
326                         /* possibly a module outside /lib/modules/<ver>/ */
327                         n = alloc_module(m.name, NULL, 0, m.depends, m.size);
328                 }
329                 n->usage = m.usage;
330                 n->state = LOADED;
331         }
332         free(buf);
333         fclose(fp);
334
335         return 0;
336 }
337
338 static struct module* get_module_info(const char *module, const char *name)
339 {
340         int fd = open(module, O_RDONLY);
341         unsigned int offset, size;
342         char *map = MAP_FAILED, *strings, *dep = NULL;
343         const char **aliases = NULL;
344         int naliases = 0;
345         struct module *m = NULL;
346         struct stat s;
347
348         if (fd < 0) {
349                 ULOG_ERR("failed to open %s\n", module);
350                 goto out;
351         }
352
353         if (fstat(fd, &s) == -1) {
354                 ULOG_ERR("failed to stat %s\n", module);
355                 goto out;
356         }
357
358         map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
359         if (map == MAP_FAILED) {
360                 ULOG_ERR("failed to mmap %s\n", module);
361                 goto out;
362         }
363
364         if (elf_find_section(map, ".modinfo", &offset, &size)) {
365                 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
366                 goto out;
367         }
368
369         strings = map + offset;
370         while (true) {
371                 char *sep;
372                 int len;
373
374                 while (!strings[0])
375                         strings++;
376                 if (strings >= map + offset + size)
377                         break;
378                 sep = strstr(strings, "=");
379                 if (!sep)
380                         break;
381                 len = sep - strings;
382                 sep++;
383                 if (!strncmp(strings, "depends=", len + 1))
384                         dep = sep;
385                 else if (!strncmp(strings, "alias=", len + 1)) {
386                         aliases = realloc(aliases, sizeof(sep) * (naliases + 1));
387                         if (!aliases) {
388                                 ULOG_ERR("out of memory\n");
389                                 goto out;
390                         }
391
392                         aliases[naliases++] = sep;
393                 }
394                 strings = &sep[strlen(sep)];
395         }
396
397         m = alloc_module(name, aliases, naliases, dep, s.st_size);
398
399         if (m)
400                 m->state = SCANNED;
401
402 out:
403         if (map != MAP_FAILED)
404                 munmap(map, s.st_size);
405
406         if (fd >= 0)
407                 close(fd);
408
409         free(aliases);
410
411         return m;
412 }
413
414 static int scan_module_folder(const char *dir)
415 {
416         int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
417         struct utsname ver;
418         char *path;
419         glob_t gl;
420         int j, rv = 0;
421
422         uname(&ver);
423         path = alloca(strlen(dir) + sizeof("*.ko") + 1);
424         sprintf(path, "%s*.ko", dir);
425
426         if (glob(path, gl_flags, NULL, &gl) < 0)
427                 return -1;
428
429         for (j = 0; j < gl.gl_pathc; j++) {
430                 char *name = get_module_name(gl.gl_pathv[j]);
431                 struct module *m;
432
433                 if (!name)
434                         continue;
435
436                 m = find_module(name);
437                 if (!m) {
438                         if (!get_module_info(gl.gl_pathv[j], name))
439                                 rv |= -1;
440                 }
441         }
442
443         globfree(&gl);
444
445         return rv;
446 }
447
448 static int scan_module_folders(void)
449 {
450         int rv = 0;
451         char **p;
452
453         if (init_module_folders())
454                 return -1;
455
456         for (p = module_folders; *p; p++)
457                 rv |= scan_module_folder(*p);
458
459         return rv;
460 }
461
462 static int print_modinfo(char *module)
463 {
464         int fd = open(module, O_RDONLY);
465         unsigned int offset, size;
466         struct stat s;
467         char *map = MAP_FAILED, *strings;
468         int rv = -1;
469
470         if (fd < 0) {
471                 ULOG_ERR("failed to open %s\n", module);
472                 goto out;
473         }
474
475         if (fstat(fd, &s) == -1) {
476                 ULOG_ERR("failed to stat %s\n", module);
477                 goto out;
478         }
479
480         map = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
481         if (map == MAP_FAILED) {
482                 ULOG_ERR("failed to mmap %s\n", module);
483                 goto out;
484         }
485
486         if (elf_find_section(map, ".modinfo", &offset, &size)) {
487                 ULOG_ERR("failed to load the .modinfo section from %s\n", module);
488                 goto out;
489         }
490
491         strings = map + offset;
492         printf("module:\t\t%s\n", module);
493         while (true) {
494                 char *dup = NULL;
495                 char *sep;
496
497                 while (!strings[0])
498                         strings++;
499                 if (strings >= map + offset + size)
500                         break;
501                 sep = strstr(strings, "=");
502                 if (!sep)
503                         break;
504                 dup = strndup(strings, sep - strings);
505                 sep++;
506                 if (strncmp(strings, "parm", 4)) {
507                         if (strlen(dup) < 7)
508                                 printf("%s:\t\t%s\n",  dup, sep);
509                         else
510                                 printf("%s:\t%s\n",  dup, sep);
511                 }
512                 strings = &sep[strlen(sep)];
513                 if (dup)
514                         free(dup);
515         }
516
517         rv = 0;
518
519 out:
520         if (map != MAP_FAILED)
521                 munmap(map, s.st_size);
522
523         if (fd >= 0)
524                 close(fd);
525
526         return rv;
527 }
528
529 static int deps_available(struct module *m, int verbose)
530 {
531         char *dep;
532         int err = 0;
533
534         if (!m->depends || !strcmp(m->depends, "-") || !strcmp(m->depends, ""))
535                 return 0;
536
537         dep = m->depends;
538
539         while (*dep) {
540                 m = find_module(dep);
541
542                 if (verbose && !m)
543                         ULOG_ERR("missing dependency %s\n", dep);
544                 if (verbose && m && (m->state != LOADED))
545                         ULOG_ERR("dependency not loaded %s\n", dep);
546                 if (!m || (m->state != LOADED))
547                         err++;
548                 dep += strlen(dep) + 1;
549         }
550
551         return err;
552 }
553
554 static int insert_module(char *path, const char *options)
555 {
556         void *data = 0;
557         struct stat s;
558         int fd, ret = -1;
559
560         if (stat(path, &s)) {
561                 ULOG_ERR("missing module %s\n", path);
562                 return ret;
563         }
564
565         fd = open(path, O_RDONLY);
566         if (fd < 0) {
567                 ULOG_ERR("cannot open %s\n", path);
568                 return ret;
569         }
570
571         data = malloc(s.st_size);
572         if (!data)
573                 goto out;
574
575         if (read(fd, data, s.st_size) == s.st_size) {
576                 ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
577                 if (errno == EEXIST)
578                         ret = 0;
579         }
580         else
581                 ULOG_ERR("failed to read full module %s\n", path);
582
583 out:
584         close(fd);
585         free(data);
586
587         return ret;
588 }
589
590 static void load_moddeps(struct module *_m)
591 {
592         char *dep;
593         struct module *m;
594
595         if (!strcmp(_m->depends, "-") || !strcmp(_m->depends, ""))
596                 return;
597
598         dep = _m->depends;
599
600         while (*dep) {
601                 m = find_module(dep);
602
603                 if (!m)
604                         ULOG_ERR("failed to find dependency %s\n", dep);
605                 if (m && (m->state != LOADED)) {
606                         m->state = PROBE;
607                         load_moddeps(m);
608                 }
609
610                 dep = dep + strlen(dep) + 1;
611         }
612 }
613
614 static int iterations = 0;
615 static int load_modprobe(void)
616 {
617         int loaded, todo;
618         struct module_node *mn;
619         struct module *m;
620
621         avl_for_each_element(&modules, mn, avl) {
622                 if (mn->is_alias)
623                         continue;
624                 m = mn->m;
625                 if (m->state == PROBE)
626                         load_moddeps(m);
627         }
628
629         do {
630                 loaded = 0;
631                 todo = 0;
632                 avl_for_each_element(&modules, mn, avl) {
633                         if (mn->is_alias)
634                                 continue;
635                         m = mn->m;
636                         if ((m->state == PROBE) && (!deps_available(m, 0))) {
637                                 if (!insert_module(get_module_path(m->name), (m->opts) ? (m->opts) : (""))) {
638                                         m->state = LOADED;
639                                         m->error = 0;
640                                         loaded++;
641                                         continue;
642                                 }
643                                 m->error = 1;
644                         }
645
646                         if ((m->state == PROBE) || m->error)
647                                 todo++;
648                 }
649                 iterations++;
650         } while (loaded);
651
652         return todo;
653 }
654
655 static int print_insmod_usage(void)
656 {
657         ULOG_INFO("Usage:\n\tinsmod filename [args]\n");
658
659         return -1;
660 }
661
662 static int print_modprobe_usage(void)
663 {
664         ULOG_INFO("Usage:\n\tmodprobe [-q] filename\n");
665
666         return -1;
667 }
668
669 static int print_usage(char *arg)
670 {
671         ULOG_INFO("Usage:\n\t%s module\n", arg);
672
673         return -1;
674 }
675
676 static int main_insmod(int argc, char **argv)
677 {
678         char *name, *cur, *options;
679         int i, ret, len;
680
681         if (argc < 2)
682                 return print_insmod_usage();
683
684         name = get_module_name(argv[1]);
685         if (!name) {
686                 ULOG_ERR("cannot find module - %s\n", argv[1]);
687                 return -1;
688         }
689
690         if (scan_loaded_modules())
691                 return -1;
692
693         if (find_module(name)) {
694                 ULOG_ERR("module is already loaded - %s\n", name);
695                 return -1;
696
697         }
698
699         free_modules();
700
701         for (len = 0, i = 2; i < argc; i++)
702                 len += strlen(argv[i]) + 1;
703
704         options = malloc(len);
705         if (!options) {
706                 ret = -1;
707                 goto err;
708         }
709
710         options[0] = 0;
711         cur = options;
712         for (i = 2; i < argc; i++) {
713                 if (options[0]) {
714                         *cur = ' ';
715                         cur++;
716                 }
717                 cur += sprintf(cur, "%s", argv[i]);
718         }
719
720         if (init_module_folders()) {
721                 fprintf(stderr, "Failed to find the folder holding the modules\n");
722                 ret = -1;
723                 goto err;
724         }
725
726         if (get_module_path(argv[1])) {
727                 name = argv[1];
728         } else if (!get_module_path(name)) {
729                 fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
730                 ret = -1;
731                 goto err;
732         }
733
734         ret = insert_module(get_module_path(name), options);
735         if (ret)
736                 ULOG_ERR("failed to insert %s\n", get_module_path(name));
737
738 err:
739         free(options);
740         return ret;
741 }
742
743 static int main_rmmod(int argc, char **argv)
744 {
745         struct module *m;
746         char *name;
747         int ret;
748
749         if (argc != 2)
750                 return print_usage("rmmod");
751
752         if (scan_loaded_modules())
753                 return -1;
754
755         name = get_module_name(argv[1]);
756         m = find_module(name);
757         if (!m) {
758                 ULOG_ERR("module is not loaded\n");
759                 return -1;
760         }
761         ret = syscall(__NR_delete_module, m->name, 0);
762
763         if (ret)
764                 ULOG_ERR("unloading the module failed\n");
765
766         free_modules();
767
768         return ret;
769 }
770
771 static int main_lsmod(int argc, char **argv)
772 {
773         struct module_node *mn;
774         struct module *m;
775         char *dep;
776
777         if (scan_loaded_modules())
778                 return -1;
779
780         avl_for_each_element(&modules, mn, avl) {
781                 if (mn->is_alias)
782                         continue;
783                 m = mn->m;
784                 if (m->state == LOADED) {
785                         printf("%-20s%8d%3d ",
786                                 m->name, m->size, m->usage);
787                         if (m->depends && strcmp(m->depends, "-") && strcmp(m->depends, "")) {
788                                 dep = m->depends;
789                                 while (*dep) {
790                                         printf("%s", dep);
791                                         dep = dep + strlen(dep) + 1;
792                                         if (*dep)
793                                                 printf(",");
794                                 }
795                         }
796                         printf("\n");
797                 }
798         }
799
800         free_modules();
801
802         return 0;
803 }
804
805 static int main_modinfo(int argc, char **argv)
806 {
807         struct module *m;
808         char *name;
809
810         if (argc != 2)
811                 return print_usage("modinfo");
812
813         if (scan_module_folders())
814                 return -1;
815
816         name = get_module_name(argv[1]);
817         m = find_module(name);
818         if (!m) {
819                 ULOG_ERR("cannot find module - %s\n", argv[1]);
820                 return -1;
821         }
822
823         name = get_module_path(m->name);
824         if (!name) {
825                 ULOG_ERR("cannot find path of module - %s\n", m->name);
826                 return -1;
827         }
828
829         print_modinfo(name);
830
831         return 0;
832 }
833
834 static int main_modprobe(int argc, char **argv)
835 {
836         struct module_node *mn;
837         struct module *m;
838         char *name;
839         char *mod = NULL;
840         int opt;
841         bool quiet = false;
842
843         while ((opt = getopt(argc, argv, "q")) != -1 ) {
844                 switch (opt) {
845                         case 'q': /* shhhh! */
846                                 quiet = true;
847                                 break;
848                         default: /* '?' */
849                                 return print_modprobe_usage();
850                                 break;
851                         }
852         }
853
854         if (optind >= argc)
855                 return print_modprobe_usage(); /* expected module after options */
856
857         mod = argv[optind];
858
859         if (scan_module_folders())
860                 return -1;
861
862         if (scan_loaded_modules())
863                 return -1;
864
865         name = get_module_name(mod);
866         m = find_module(name);
867         if (m && m->state == LOADED) {
868                 if (!quiet)
869                         ULOG_ERR("%s is already loaded\n", name);
870                 return 0;
871         } else if (!m) {
872                 if (!quiet)
873                         ULOG_ERR("failed to find a module named %s\n", name);
874                 return -1;
875         } else {
876                 int fail;
877
878                 m->state = PROBE;
879
880                 fail = load_modprobe();
881
882                 if (fail) {
883                         ULOG_ERR("%d module%s could not be probed\n",
884                                  fail, (fail == 1) ? ("") : ("s"));
885
886                         avl_for_each_element(&modules, mn, avl) {
887                                 if (mn->is_alias)
888                                         continue;
889                                 m = mn->m;
890                                 if ((m->state == PROBE) || m->error)
891                                         ULOG_ERR("- %s\n", m->name);
892                         }
893                 }
894         }
895
896         free_modules();
897
898         return 0;
899 }
900
901 static int main_loader(int argc, char **argv)
902 {
903         int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
904         char *dir = "/etc/modules.d/";
905         struct module_node *mn;
906         struct module *m;
907         glob_t gl;
908         char *path;
909         int fail, j;
910
911         if (argc > 1)
912                 dir = argv[1];
913
914         path = malloc(strlen(dir) + 2);
915         if (!path)
916                 return -1;
917
918         strcpy(path, dir);
919         strcat(path, "*");
920
921         if (scan_module_folders()) {
922                 free (path);
923                 return -1;
924         }
925
926         if (scan_loaded_modules()) {
927                 free (path);
928                 return -1;
929         }
930
931         ULOG_INFO("loading kernel modules from %s\n", path);
932
933         if (glob(path, gl_flags, NULL, &gl) < 0)
934                 goto out;
935
936         for (j = 0; j < gl.gl_pathc; j++) {
937                 FILE *fp = fopen(gl.gl_pathv[j], "r");
938                 size_t mod_len = 0;
939                 char *mod = NULL;
940
941                 if (!fp) {
942                         ULOG_ERR("failed to open %s\n", gl.gl_pathv[j]);
943                         continue;
944                 }
945
946                 while (getline(&mod, &mod_len, fp) > 0) {
947                         char *nl = strchr(mod, '\n');
948                         struct module *m;
949                         char *opts;
950
951                         if (nl)
952                                 *nl = '\0';
953
954                         opts = strchr(mod, ' ');
955                         if (opts)
956                                 *opts++ = '\0';
957
958                         m = find_module(get_module_name(mod));
959                         if (!m || (m->state == LOADED))
960                                 continue;
961
962                         if (opts)
963                                 m->opts = strdup(opts);
964                         m->state = PROBE;
965                         if (basename(gl.gl_pathv[j])[0] - '0' <= 9)
966                                 load_modprobe();
967
968                 }
969                 free(mod);
970                 fclose(fp);
971         }
972
973         fail = load_modprobe();
974
975         if (fail) {
976                 ULOG_ERR("%d module%s could not be probed\n",
977                          fail, (fail == 1) ? ("") : ("s"));
978
979                 avl_for_each_element(&modules, mn, avl) {
980                         if (mn->is_alias)
981                                 continue;
982                         m = mn->m;
983                         if ((m->state == PROBE) || (m->error))
984                                 ULOG_ERR("- %s - %d\n", m->name, deps_available(m, 1));
985                 }
986         } else {
987                 ULOG_INFO("done loading kernel modules from %s\n", path);
988         }
989
990 out:
991         globfree(&gl);
992         free(path);
993
994         return 0;
995 }
996
997 static inline char weight(char c)
998 {
999         return c == '_' ? '-' : c;
1000 }
1001
1002 static int avl_modcmp(const void *k1, const void *k2, void *ptr)
1003 {
1004         const char *s1 = k1;
1005         const char *s2 = k2;
1006
1007         while (*s1 && (weight(*s1) == weight(*s2)))
1008         {
1009                 s1++;
1010                 s2++;
1011         }
1012
1013         return (unsigned char)weight(*s1) - (unsigned char)weight(*s2);
1014 }
1015
1016 int main(int argc, char **argv)
1017 {
1018         char *exec = basename(*argv);
1019
1020         avl_init(&modules, avl_modcmp, false, NULL);
1021         if (!strcmp(exec, "insmod"))
1022                 return main_insmod(argc, argv);
1023
1024         if (!strcmp(exec, "rmmod"))
1025                 return main_rmmod(argc, argv);
1026
1027         if (!strcmp(exec, "lsmod"))
1028                 return main_lsmod(argc, argv);
1029
1030         if (!strcmp(exec, "modinfo"))
1031                 return main_modinfo(argc, argv);
1032
1033         if (!strcmp(exec, "modprobe"))
1034                 return main_modprobe(argc, argv);
1035
1036         ulog_open(ULOG_KMSG, LOG_USER, "kmodloader");
1037         return main_loader(argc, argv);
1038 }