initial import of uboox utilities
[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 <stdlib.h>
18 #include <unistd.h>
19 #include <sys/syscall.h>
20 #include <sys/types.h>
21 #include <values.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <syslog.h>
28 #include <glob.h>
29 #include <sys/utsname.h>
30
31 #define DEF_MOD_PATH "/lib/modules/%s/%s.ko"
32 static int insmod(char *module, const char *options)
33 {
34         struct utsname ver;
35         char path[256];
36         void *data = 0;
37         struct stat s;
38         int fd, ret = -1;
39
40         uname(&ver);
41         snprintf(path, 256, DEF_MOD_PATH, ver.release, module);
42
43         if (stat(path, &s)) {
44                 fprintf(stderr, "missing module %s\n", path);
45                 return ret;
46         }
47
48         fd = open(path, O_RDONLY);
49         if (!fd) {
50                 fprintf(stderr, "cannot open %s\n", path);
51                 return ret;
52         }
53
54         data = malloc(s.st_size);
55         if (read(fd, data, s.st_size) == s.st_size) {
56                 ret = syscall(__NR_init_module, data, s.st_size, options);
57                 if (ret)
58                         fprintf(stderr, "failed insert %s\n", module);
59         } else {
60                 fprintf(stderr, "failed to read full module %s\n", path);
61         }
62
63         close(fd);
64         free(data);
65
66         return ret;
67 }
68
69 /*static void rmmod(char *module)
70 {
71         syscall(__NR_delete_module, module, 0);
72 }*/
73
74 int main(int argc, char **argv)
75 {
76         glob_t gl;
77         int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
78         char *tmp = malloc(256);
79         char *dir = "/etc/modules.d/*";
80
81         if (argc > 1)
82                 dir = argv[1];
83
84         syslog(0, "kmodloader: loading kernel modules from %s\n", dir);
85
86         if (glob(dir, gl_flags, NULL, &gl) >= 0) {
87                 int j;
88
89                 for (j = 0; j < gl.gl_pathc; j++) {
90                         FILE *fp = fopen(gl.gl_pathv[j], "r");
91
92                         if (!fp) {
93                                 fprintf(stderr, "failed to open %s\n", gl.gl_pathv[j]);
94                         } else {
95                                 char mod[64];
96
97                                 while (fgets(mod, 64, fp)) {
98                                         mod[strlen(mod) - 1] = '\0';
99                                         insmod(mod, "");
100                                 }
101                                 fclose(fp);
102                         }
103                 }
104         }
105
106         globfree(&gl);
107         free(tmp);
108
109         return 0;
110 }