]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/init_module.c
testsuite: add GPL license
[thirdparty/kmod.git] / testsuite / init_module.c
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
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
38 #include "testsuite.h"
39 #include "stripped-module.h"
40
41 struct mod {
42 struct mod *next;
43 int ret;
44 int errcode;
45 char name[];
46 };
47
48 static struct mod *modules;
49 static bool need_init = true;
50
51 static void parse_retcodes(struct mod *_modules, const char *s)
52 {
53 const char *p;
54
55 if (s == NULL)
56 return;
57
58 for (p = s;;) {
59 struct mod *mod;
60 const char *modname;
61 char *end;
62 size_t modnamelen;
63 int ret, errcode;
64 long l;
65
66 modname = p;
67 if (modname == NULL || modname[0] == '\0')
68 break;
69
70 modnamelen = strcspn(s, ":");
71 if (modname[modnamelen] != ':')
72 break;
73
74 p = modname + modnamelen + 1;
75 if (p == NULL)
76 break;
77
78 l = strtol(p, &end, 0);
79 if (end == p || *end != ':')
80 break;
81 ret = (int) l;
82 p = end + 1;
83
84 l = strtol(p, &end, 0);
85 if (*end == ':')
86 p = end + 1;
87 else if (*end != '\0')
88 break;
89
90 errcode = (int) l;
91
92 mod = malloc(sizeof(*mod) + modnamelen + 1);
93 if (mod == NULL)
94 break;
95
96 memcpy(mod->name, modname, modnamelen);
97 mod->name[modnamelen] = '\0';
98 mod->ret = ret;
99 mod->errcode = errcode;
100 mod->next = _modules;
101 _modules = mod;
102 }
103 }
104
105 static struct mod *find_module(struct mod *_modules, const char *modname)
106 {
107 struct mod *mod;
108
109 for (mod = _modules; mod != NULL; mod = mod->next) {
110 if (strcmp(mod->name, modname))
111 return mod;
112 }
113
114 return NULL;
115 }
116
117 static void init_retcodes(void)
118 {
119 const char *s;
120
121 if (!need_init)
122 return;
123
124 need_init = false;
125 s = getenv(S_TC_INIT_MODULE_RETCODES);
126 if (s == NULL) {
127 fprintf(stderr, "TRAP init_module(): missing export %s?\n",
128 S_TC_INIT_MODULE_RETCODES);
129 }
130
131 parse_retcodes(modules, s);
132 }
133
134 TS_EXPORT long init_module(void *mem, unsigned long len, const char *args);
135
136 /*
137 * FIXME: change /sys/module/<modname> to fake-insert a module
138 *
139 * Default behavior is to exit successfully. If this is not the intended
140 * behavior, set TESTSUITE_INIT_MODULE_RETCODES env var.
141 */
142 long init_module(void *mem, unsigned long len, const char *args)
143 {
144 const char *modname;
145 struct kmod_elf *elf;
146 struct mod *mod;
147 const void *buf;
148 uint64_t bufsize;
149 int err;
150
151 init_retcodes();
152
153 elf = kmod_elf_new(mem, len);
154 if (elf == NULL)
155 return 0;
156
157 err = kmod_elf_get_section(elf, ".gnu.linkonce.this_module", &buf,
158 &bufsize);
159 kmod_elf_unref(elf);
160
161 /*
162 * We couldn't find the module's name inside the ELF file. Just exit
163 * as if it was successful
164 */
165 if (err < 0)
166 return 0;
167
168 modname = (char *)buf + offsetof(struct module, name);
169 mod = find_module(modules, modname);
170 if (mod == NULL)
171 return 0;
172
173 errno = mod->errcode;
174 return mod->ret;
175 }
176
177 /* the test is going away anyway, but lets keep valgrind happy */
178 void free_resources(void) __attribute__((destructor));
179 void free_resources(void)
180 {
181 while (modules) {
182 struct mod *mod = modules->next;
183 free(modules);
184 modules = mod;
185 }
186 }