blob: b7d220b725fbc76fa4dea78128d1f450c89852bb [file] [log] [blame]
Lucas De Marchie701e382012-01-26 17:01:41 -02001/*
Lucas De Marchie6b0e492013-01-16 11:27:21 -02002 * Copyright (C) 2012-2013 ProFUSION embedded systems
Lucas De Marchi0ae58602013-04-09 05:21:42 -03003 * Copyright (C) 2012-2013 Lucas De Marchi <lucas.de.marchi@gmail.com>
Lucas De Marchie701e382012-01-26 17:01:41 -02004 *
Lucas De Marchie1b1ab22012-07-10 09:42:24 -03005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
Lucas De Marchie701e382012-01-26 17:01:41 -02009 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Lucas De Marchie1b1ab22012-07-10 09:42:24 -030012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
Lucas De Marchie701e382012-01-26 17:01:41 -020014 *
Lucas De Marchie1b1ab22012-07-10 09:42:24 -030015 * You should have received a copy of the GNU Lesser General Public
Lucas De Marchidea2dfe2014-12-25 23:32:03 -020016 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Lucas De Marchie701e382012-01-26 17:01:41 -020017 */
18
Lucas De Marchi55112d12013-04-09 04:16:57 -030019#ifndef HAVE_FINIT_MODULE
20#define HAVE_FINIT_MODULE 1
21#endif
22
Lucas De Marchi53646fc2012-01-26 02:09:28 -020023#include <assert.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030024#include <dirent.h>
25#include <dlfcn.h>
Lucas De Marchifca5b9b2012-06-21 11:30:56 -030026#include <elf.h>
Lucas De Marchi53646fc2012-01-26 02:09:28 -020027#include <errno.h>
Lucas De Marchi53646fc2012-01-26 02:09:28 -020028#include <fcntl.h>
Lucas De Marchi53646fc2012-01-26 02:09:28 -020029#include <limits.h>
Lucas De Marchi53646fc2012-01-26 02:09:28 -020030#include <stdarg.h>
31#include <stddef.h>
Lucas De Marchi53646fc2012-01-26 02:09:28 -020032#include <stdio.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030033#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
Kees Cooke87352d2013-02-18 12:02:33 -080036#include <sys/mman.h>
Lucas De Marchi53646fc2012-01-26 02:09:28 -020037#include <sys/stat.h>
Lucas De Marchi0ae58602013-04-09 05:21:42 -030038#include <sys/syscall.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030039#include <sys/types.h>
Michal Marek063086e2014-02-28 13:05:30 +010040#include <sys/utsname.h>
Lucas De Marchi53646fc2012-01-26 02:09:28 -020041
Lucas De Marchi96573a02014-10-03 00:01:35 -030042#include <shared/util.h>
43
Lucas De Marchi53646fc2012-01-26 02:09:28 -020044/* kmod_elf_get_section() is not exported, we need the private header */
Lucas De Marchi13151232014-12-25 23:07:48 -020045#include <libkmod/libkmod-internal.h>
Lucas De Marchi53646fc2012-01-26 02:09:28 -020046
47/* FIXME: hack, change name so we don't clash */
48#undef ERR
49#include "testsuite.h"
50#include "stripped-module.h"
51
52struct mod {
53 struct mod *next;
54 int ret;
55 int errcode;
56 char name[];
57};
58
59static struct mod *modules;
60static bool need_init = true;
Lucas De Marchia6553702012-06-05 00:53:15 -030061static struct kmod_ctx *ctx;
Lucas De Marchi53646fc2012-01-26 02:09:28 -020062
63static void parse_retcodes(struct mod *_modules, const char *s)
64{
65 const char *p;
66
67 if (s == NULL)
68 return;
69
70 for (p = s;;) {
71 struct mod *mod;
72 const char *modname;
73 char *end;
74 size_t modnamelen;
75 int ret, errcode;
76 long l;
77
78 modname = p;
79 if (modname == NULL || modname[0] == '\0')
80 break;
81
82 modnamelen = strcspn(s, ":");
83 if (modname[modnamelen] != ':')
84 break;
85
86 p = modname + modnamelen + 1;
87 if (p == NULL)
88 break;
89
90 l = strtol(p, &end, 0);
91 if (end == p || *end != ':')
92 break;
93 ret = (int) l;
94 p = end + 1;
95
96 l = strtol(p, &end, 0);
97 if (*end == ':')
98 p = end + 1;
99 else if (*end != '\0')
100 break;
101
102 errcode = (int) l;
103
104 mod = malloc(sizeof(*mod) + modnamelen + 1);
105 if (mod == NULL)
106 break;
107
108 memcpy(mod->name, modname, modnamelen);
109 mod->name[modnamelen] = '\0';
110 mod->ret = ret;
111 mod->errcode = errcode;
112 mod->next = _modules;
113 _modules = mod;
114 }
115}
116
Lucas De Marchi5a2949c2012-05-25 00:30:37 -0300117static int write_one_line_file(const char *fn, const char *line, int len)
118{
119 FILE *f;
120 int r;
121
122 assert(fn);
123 assert(line);
124
125 f = fopen(fn, "we");
126 if (!f)
127 return -errno;
128
129 errno = 0;
130 if (fputs(line, f) < 0) {
131 r = -errno;
132 goto finish;
133 }
134
135 fflush(f);
136
137 if (ferror(f)) {
138 if (errno != 0)
139 r = -errno;
140 else
141 r = -EIO;
142 } else
143 r = 0;
144
145finish:
146 fclose(f);
147 return r;
148}
149
150static int create_sysfs_files(const char *modname)
151{
152 char buf[PATH_MAX];
153 const char *sysfsmod = "/sys/module/";
154 int len = strlen(sysfsmod);
155
156 memcpy(buf, sysfsmod, len);
157 strcpy(buf + len, modname);
158 len += strlen(modname);
159
Lucas De Marchic493b932013-07-15 01:58:44 -0300160 assert(mkdir_p(buf, len, 0755) >= 0);
Lucas De Marchi5a2949c2012-05-25 00:30:37 -0300161
162 strcpy(buf + len, "/initstate");
163 return write_one_line_file(buf, "live\n", strlen("live\n"));
164}
165
Lucas De Marchi53646fc2012-01-26 02:09:28 -0200166static struct mod *find_module(struct mod *_modules, const char *modname)
167{
168 struct mod *mod;
169
170 for (mod = _modules; mod != NULL; mod = mod->next) {
Lucas De Marchi5c42c5f2015-01-14 14:22:09 -0200171 if (streq(mod->name, modname))
Lucas De Marchi53646fc2012-01-26 02:09:28 -0200172 return mod;
173 }
174
175 return NULL;
176}
177
178static void init_retcodes(void)
179{
180 const char *s;
181
182 if (!need_init)
183 return;
184
185 need_init = false;
186 s = getenv(S_TC_INIT_MODULE_RETCODES);
187 if (s == NULL) {
188 fprintf(stderr, "TRAP init_module(): missing export %s?\n",
189 S_TC_INIT_MODULE_RETCODES);
190 }
191
Lucas De Marchia6553702012-06-05 00:53:15 -0300192 ctx = kmod_new(NULL, NULL);
193
Lucas De Marchi53646fc2012-01-26 02:09:28 -0200194 parse_retcodes(modules, s);
195}
196
Lucas De Marchia6553702012-06-05 00:53:15 -0300197static inline bool module_is_inkernel(const char *modname)
198{
199 struct kmod_module *mod;
200 int state;
201 bool ret;
202
203 if (kmod_module_new_from_name(ctx, modname, &mod) < 0)
204 return false;
205
206 state = kmod_module_get_initstate(mod);
207
208 if (state == KMOD_MODULE_LIVE ||
209 state == KMOD_MODULE_BUILTIN)
210 ret = true;
211 else
212 ret = false;
213
214 kmod_module_unref(mod);
215
216 return ret;
217}
218
Lucas De Marchifca5b9b2012-06-21 11:30:56 -0300219static uint8_t elf_identify(void *mem)
220{
221 uint8_t *p = mem;
222 return p[EI_CLASS];
223}
224
Lucas De Marchi53646fc2012-01-26 02:09:28 -0200225TS_EXPORT long init_module(void *mem, unsigned long len, const char *args);
226
227/*
Lucas De Marchiddf1e7a2012-06-05 00:20:42 -0300228 * Default behavior is to try to mimic init_module behavior inside the kernel.
229 * If it is a simple test that you know the error code, set the return code
230 * in TESTSUITE_INIT_MODULE_RETCODES env var instead.
231 *
232 * The exception is when the module name is not find in the memory passed.
233 * This is because we want to be able to pass dummy modules (and not real
234 * ones) and it still work.
Lucas De Marchi53646fc2012-01-26 02:09:28 -0200235 */
236long init_module(void *mem, unsigned long len, const char *args)
237{
238 const char *modname;
239 struct kmod_elf *elf;
240 struct mod *mod;
241 const void *buf;
242 uint64_t bufsize;
243 int err;
Lucas De Marchifca5b9b2012-06-21 11:30:56 -0300244 uint8_t class;
245 off_t offset;
Lucas De Marchi53646fc2012-01-26 02:09:28 -0200246
247 init_retcodes();
248
249 elf = kmod_elf_new(mem, len);
250 if (elf == NULL)
251 return 0;
252
253 err = kmod_elf_get_section(elf, ".gnu.linkonce.this_module", &buf,
254 &bufsize);
255 kmod_elf_unref(elf);
256
Lucas De Marchifca5b9b2012-06-21 11:30:56 -0300257 /* We couldn't parse the ELF file. Just exit as if it was successful */
Lucas De Marchi53646fc2012-01-26 02:09:28 -0200258 if (err < 0)
259 return 0;
260
Lucas De Marchifca5b9b2012-06-21 11:30:56 -0300261 /* We need to open both 32 and 64 bits module - hack! */
262 class = elf_identify(mem);
263 if (class == ELFCLASS64)
264 offset = MODULE_NAME_OFFSET_64;
265 else
266 offset = MODULE_NAME_OFFSET_32;
267
268 modname = (char *)buf + offset;
Lucas De Marchi53646fc2012-01-26 02:09:28 -0200269 mod = find_module(modules, modname);
Lucas De Marchiddf1e7a2012-06-05 00:20:42 -0300270 if (mod != NULL) {
271 errno = mod->errcode;
272 err = mod->ret;
Lucas De Marchia6553702012-06-05 00:53:15 -0300273 } else if (module_is_inkernel(modname)) {
274 err = -1;
275 errno = EEXIST;
276 } else
Lucas De Marchiddf1e7a2012-06-05 00:20:42 -0300277 err = 0;
Lucas De Marchi53646fc2012-01-26 02:09:28 -0200278
Lucas De Marchiddf1e7a2012-06-05 00:20:42 -0300279 if (err == 0)
280 create_sysfs_files(modname);
281
282 return err;
Lucas De Marchi53646fc2012-01-26 02:09:28 -0200283}
284
Michal Marek063086e2014-02-28 13:05:30 +0100285static int check_kernel_version(int major, int minor)
286{
287 struct utsname u;
288 const char *p;
289 int maj = 0, min = 0;
290
291 if (uname(&u) < 0)
292 return false;
293 for (p = u.release; *p >= '0' && *p <= '9'; p++)
294 maj = maj * 10 + *p - '0';
295 if (*p == '.')
296 for (p++; *p >= '0' && *p <= '9'; p++)
297 min = min * 10 + *p - '0';
298 if (maj > major || (maj == major && min >= minor))
299 return true;
300 return false;
301}
302
303
Kees Cooke87352d2013-02-18 12:02:33 -0800304TS_EXPORT int finit_module(const int fd, const char *args, const int flags);
305
306int finit_module(const int fd, const char *args, const int flags)
307{
308 int err;
309 void *mem;
310 unsigned long len;
311 struct stat st;
312
Michal Marek063086e2014-02-28 13:05:30 +0100313 if (!check_kernel_version(3, 8)) {
314 errno = ENOSYS;
315 return -1;
316 }
Kees Cooke87352d2013-02-18 12:02:33 -0800317 if (fstat(fd, &st) < 0)
318 return -1;
319
320 len = st.st_size;
321 mem = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
322 if (mem == MAP_FAILED)
323 return -1;
324
325 err = init_module(mem, len, args);
326 munmap(mem, len);
327
328 return err;
329}
330
Lucas De Marchi0ae58602013-04-09 05:21:42 -0300331TS_EXPORT long int syscall(long int __sysno, ...)
332{
333 va_list ap;
334 long ret;
335
Lucas De Marchi8e00db92013-04-09 11:54:05 -0300336 if (__sysno == -1) {
337 errno = ENOSYS;
Lucas De Marchi0ae58602013-04-09 05:21:42 -0300338 return -1;
Lucas De Marchi8e00db92013-04-09 11:54:05 -0300339 }
340
341 if (__sysno == __NR_finit_module) {
Lucas De Marchi0ae58602013-04-09 05:21:42 -0300342 const char *args;
343 int flags;
344 int fd;
345
346 va_start(ap, __sysno);
347
348 fd = va_arg(ap, int);
349 args = va_arg(ap, const char *);
350 flags = va_arg(ap, int);
351
352 ret = finit_module(fd, args, flags);
353
354 va_end(ap);
355 return ret;
356 }
Lucas De Marchi0ae58602013-04-09 05:21:42 -0300357
358 /*
359 * FIXME: no way to call the libc function - let's hope there are no
360 * other users.
361 */
362 abort();
363}
364
Lucas De Marchi53646fc2012-01-26 02:09:28 -0200365/* the test is going away anyway, but lets keep valgrind happy */
366void free_resources(void) __attribute__((destructor));
367void free_resources(void)
368{
369 while (modules) {
370 struct mod *mod = modules->next;
371 free(modules);
372 modules = mod;
373 }
Lucas De Marchia6553702012-06-05 00:53:15 -0300374
375 if (ctx)
376 kmod_unref(ctx);
Lucas De Marchi53646fc2012-01-26 02:09:28 -0200377}