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