]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/delete_module.c
testsuite: add GPL license
[thirdparty/kmod.git] / testsuite / delete_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 #include "testsuite.h"
34
35 struct mod {
36 struct mod *next;
37 int ret;
38 int errcode;
39 char name[];
40 };
41
42 static struct mod *modules;
43 static bool need_init = true;
44
45 static void parse_retcodes(struct mod *_modules, const char *s)
46 {
47 const char *p;
48
49 if (s == NULL)
50 return;
51
52 for (p = s;;) {
53 struct mod *mod;
54 const char *modname;
55 char *end;
56 size_t modnamelen;
57 int ret, errcode;
58 long l;
59
60 modname = p;
61 if (modname == NULL || modname[0] == '\0')
62 break;
63
64 modnamelen = strcspn(s, ":");
65 if (modname[modnamelen] != ':')
66 break;
67
68 p = modname + modnamelen + 1;
69 if (p == NULL)
70 break;
71
72 l = strtol(p, &end, 0);
73 if (end == p || *end != ':')
74 break;
75 ret = (int) l;
76 p = end + 1;
77
78 l = strtol(p, &end, 0);
79 if (*end == ':')
80 p = end + 1;
81 else if (*end != '\0')
82 break;
83
84 errcode = (int) l;
85
86 mod = malloc(sizeof(*mod) + modnamelen + 1);
87 if (mod == NULL)
88 break;
89
90 memcpy(mod->name, modname, modnamelen);
91 mod->name[modnamelen] = '\0';
92 mod->ret = ret;
93 mod->errcode = errcode;
94 mod->next = _modules;
95 _modules = mod;
96 }
97 }
98
99 static struct mod *find_module(struct mod *_modules, const char *modname)
100 {
101 struct mod *mod;
102
103 for (mod = _modules; mod != NULL; mod = mod->next) {
104 if (strcmp(mod->name, modname))
105 return mod;
106 }
107
108 return NULL;
109 }
110
111 static void init_retcodes(void)
112 {
113 const char *s;
114
115 if (!need_init)
116 return;
117
118 need_init = false;
119 s = getenv(S_TC_DELETE_MODULE_RETCODES);
120 if (s == NULL) {
121 fprintf(stderr, "TRAP delete_module(): missing export %s?\n",
122 S_TC_DELETE_MODULE_RETCODES);
123 }
124
125 parse_retcodes(modules, s);
126 }
127
128 TS_EXPORT long delete_module(const char *name, unsigned int flags);
129
130 /*
131 * FIXME: change /sys/module/<modname> to fake-remove a module
132 *
133 * Default behavior is to exit successfully. If this is not the intended
134 * behavior, set TESTSUITE_DELETE_MODULE_RETCODES env var.
135 */
136 long delete_module(const char *modname, unsigned int flags)
137 {
138 struct mod *mod;
139
140 init_retcodes();
141 mod = find_module(modules, modname);
142 if (mod == NULL)
143 return 0;
144
145 errno = mod->errcode;
146 return mod->ret;
147 }
148
149 /* the test is going away anyway, but lets keep valgrind happy */
150 void free_resources(void) __attribute__((destructor));
151 void free_resources(void)
152 {
153 while (modules) {
154 struct mod *mod = modules->next;
155 free(modules);
156 modules = mod;
157 }
158 }