]>
Commit | Line | Data |
---|---|---|
e701e381 LDM |
1 | /* |
2 | * Copyright (C) 2012 ProFUSION embedded systems | |
3 | * | |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 2 of the License, or | |
7 | * (at your option) any later version. | |
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 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
53646fc5 LDM |
18 | #include <assert.h> |
19 | #include <errno.h> | |
20 | #include <dirent.h> | |
21 | #include <fcntl.h> | |
22 | #include <dlfcn.h> | |
23 | #include <limits.h> | |
24 | #include <stdlib.h> | |
25 | #include <stdarg.h> | |
26 | #include <stddef.h> | |
27 | #include <string.h> | |
28 | #include <stdio.h> | |
29 | #include <sys/types.h> | |
30 | #include <sys/stat.h> | |
31 | #include <unistd.h> | |
32 | ||
33 | /* kmod_elf_get_section() is not exported, we need the private header */ | |
34 | #include <libkmod-private.h> | |
35 | ||
36 | /* FIXME: hack, change name so we don't clash */ | |
37 | #undef ERR | |
5a2949cd | 38 | #include "mkdir.h" |
53646fc5 LDM |
39 | #include "testsuite.h" |
40 | #include "stripped-module.h" | |
41 | ||
42 | struct mod { | |
43 | struct mod *next; | |
44 | int ret; | |
45 | int errcode; | |
46 | char name[]; | |
47 | }; | |
48 | ||
49 | static struct mod *modules; | |
50 | static bool need_init = true; | |
a6553705 | 51 | static struct kmod_ctx *ctx; |
53646fc5 LDM |
52 | |
53 | static void parse_retcodes(struct mod *_modules, const char *s) | |
54 | { | |
55 | const char *p; | |
56 | ||
57 | if (s == NULL) | |
58 | return; | |
59 | ||
60 | for (p = s;;) { | |
61 | struct mod *mod; | |
62 | const char *modname; | |
63 | char *end; | |
64 | size_t modnamelen; | |
65 | int ret, errcode; | |
66 | long l; | |
67 | ||
68 | modname = p; | |
69 | if (modname == NULL || modname[0] == '\0') | |
70 | break; | |
71 | ||
72 | modnamelen = strcspn(s, ":"); | |
73 | if (modname[modnamelen] != ':') | |
74 | break; | |
75 | ||
76 | p = modname + modnamelen + 1; | |
77 | if (p == NULL) | |
78 | break; | |
79 | ||
80 | l = strtol(p, &end, 0); | |
81 | if (end == p || *end != ':') | |
82 | break; | |
83 | ret = (int) l; | |
84 | p = end + 1; | |
85 | ||
86 | l = strtol(p, &end, 0); | |
87 | if (*end == ':') | |
88 | p = end + 1; | |
89 | else if (*end != '\0') | |
90 | break; | |
91 | ||
92 | errcode = (int) l; | |
93 | ||
94 | mod = malloc(sizeof(*mod) + modnamelen + 1); | |
95 | if (mod == NULL) | |
96 | break; | |
97 | ||
98 | memcpy(mod->name, modname, modnamelen); | |
99 | mod->name[modnamelen] = '\0'; | |
100 | mod->ret = ret; | |
101 | mod->errcode = errcode; | |
102 | mod->next = _modules; | |
103 | _modules = mod; | |
104 | } | |
105 | } | |
106 | ||
5a2949cd LDM |
107 | static int write_one_line_file(const char *fn, const char *line, int len) |
108 | { | |
109 | FILE *f; | |
110 | int r; | |
111 | ||
112 | assert(fn); | |
113 | assert(line); | |
114 | ||
115 | f = fopen(fn, "we"); | |
116 | if (!f) | |
117 | return -errno; | |
118 | ||
119 | errno = 0; | |
120 | if (fputs(line, f) < 0) { | |
121 | r = -errno; | |
122 | goto finish; | |
123 | } | |
124 | ||
125 | fflush(f); | |
126 | ||
127 | if (ferror(f)) { | |
128 | if (errno != 0) | |
129 | r = -errno; | |
130 | else | |
131 | r = -EIO; | |
132 | } else | |
133 | r = 0; | |
134 | ||
135 | finish: | |
136 | fclose(f); | |
137 | return r; | |
138 | } | |
139 | ||
140 | static int create_sysfs_files(const char *modname) | |
141 | { | |
142 | char buf[PATH_MAX]; | |
143 | const char *sysfsmod = "/sys/module/"; | |
144 | int len = strlen(sysfsmod); | |
145 | ||
146 | memcpy(buf, sysfsmod, len); | |
147 | strcpy(buf + len, modname); | |
148 | len += strlen(modname); | |
149 | ||
150 | mkdir_p(buf, 0755); | |
151 | ||
152 | strcpy(buf + len, "/initstate"); | |
153 | return write_one_line_file(buf, "live\n", strlen("live\n")); | |
154 | } | |
155 | ||
53646fc5 LDM |
156 | static struct mod *find_module(struct mod *_modules, const char *modname) |
157 | { | |
158 | struct mod *mod; | |
159 | ||
160 | for (mod = _modules; mod != NULL; mod = mod->next) { | |
90fc410b | 161 | if (strcmp(mod->name, modname) == 0) |
53646fc5 LDM |
162 | return mod; |
163 | } | |
164 | ||
165 | return NULL; | |
166 | } | |
167 | ||
168 | static void init_retcodes(void) | |
169 | { | |
170 | const char *s; | |
171 | ||
172 | if (!need_init) | |
173 | return; | |
174 | ||
175 | need_init = false; | |
176 | s = getenv(S_TC_INIT_MODULE_RETCODES); | |
177 | if (s == NULL) { | |
178 | fprintf(stderr, "TRAP init_module(): missing export %s?\n", | |
179 | S_TC_INIT_MODULE_RETCODES); | |
180 | } | |
181 | ||
a6553705 LDM |
182 | ctx = kmod_new(NULL, NULL); |
183 | ||
53646fc5 LDM |
184 | parse_retcodes(modules, s); |
185 | } | |
186 | ||
a6553705 LDM |
187 | static inline bool module_is_inkernel(const char *modname) |
188 | { | |
189 | struct kmod_module *mod; | |
190 | int state; | |
191 | bool ret; | |
192 | ||
193 | if (kmod_module_new_from_name(ctx, modname, &mod) < 0) | |
194 | return false; | |
195 | ||
196 | state = kmod_module_get_initstate(mod); | |
197 | ||
198 | if (state == KMOD_MODULE_LIVE || | |
199 | state == KMOD_MODULE_BUILTIN) | |
200 | ret = true; | |
201 | else | |
202 | ret = false; | |
203 | ||
204 | kmod_module_unref(mod); | |
205 | ||
206 | return ret; | |
207 | } | |
208 | ||
53646fc5 LDM |
209 | TS_EXPORT long init_module(void *mem, unsigned long len, const char *args); |
210 | ||
211 | /* | |
ddf1e7a6 LDM |
212 | * Default behavior is to try to mimic init_module behavior inside the kernel. |
213 | * If it is a simple test that you know the error code, set the return code | |
214 | * in TESTSUITE_INIT_MODULE_RETCODES env var instead. | |
215 | * | |
216 | * The exception is when the module name is not find in the memory passed. | |
217 | * This is because we want to be able to pass dummy modules (and not real | |
218 | * ones) and it still work. | |
53646fc5 LDM |
219 | */ |
220 | long init_module(void *mem, unsigned long len, const char *args) | |
221 | { | |
222 | const char *modname; | |
223 | struct kmod_elf *elf; | |
224 | struct mod *mod; | |
225 | const void *buf; | |
226 | uint64_t bufsize; | |
227 | int err; | |
228 | ||
229 | init_retcodes(); | |
230 | ||
231 | elf = kmod_elf_new(mem, len); | |
232 | if (elf == NULL) | |
233 | return 0; | |
234 | ||
235 | err = kmod_elf_get_section(elf, ".gnu.linkonce.this_module", &buf, | |
236 | &bufsize); | |
237 | kmod_elf_unref(elf); | |
238 | ||
239 | /* | |
240 | * We couldn't find the module's name inside the ELF file. Just exit | |
241 | * as if it was successful | |
242 | */ | |
243 | if (err < 0) | |
244 | return 0; | |
245 | ||
246 | modname = (char *)buf + offsetof(struct module, name); | |
247 | mod = find_module(modules, modname); | |
ddf1e7a6 LDM |
248 | if (mod != NULL) { |
249 | errno = mod->errcode; | |
250 | err = mod->ret; | |
a6553705 LDM |
251 | } else if (module_is_inkernel(modname)) { |
252 | err = -1; | |
253 | errno = EEXIST; | |
254 | } else | |
ddf1e7a6 | 255 | err = 0; |
53646fc5 | 256 | |
ddf1e7a6 LDM |
257 | if (err == 0) |
258 | create_sysfs_files(modname); | |
259 | ||
260 | return err; | |
53646fc5 LDM |
261 | } |
262 | ||
263 | /* the test is going away anyway, but lets keep valgrind happy */ | |
264 | void free_resources(void) __attribute__((destructor)); | |
265 | void free_resources(void) | |
266 | { | |
267 | while (modules) { | |
268 | struct mod *mod = modules->next; | |
269 | free(modules); | |
270 | modules = mod; | |
271 | } | |
a6553705 LDM |
272 | |
273 | if (ctx) | |
274 | kmod_unref(ctx); | |
53646fc5 | 275 | } |