]> git.ipfire.org Git - thirdparty/kmod.git/blame - testsuite/delete_module.c
testsuite: Add assert_return
[thirdparty/kmod.git] / testsuite / delete_module.c
CommitLineData
e701e381 1/*
e6b0e49b 2 * Copyright (C) 2012-2013 ProFUSION embedded systems
e701e381 3 *
e1b1ab24
LDM
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
e701e381
LDM
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
e1b1ab24
LDM
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
e701e381 13 *
e1b1ab24
LDM
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
e701e381
LDM
17 */
18
f6ef5d6b
LDM
19#include <assert.h>
20#include <errno.h>
21#include <dirent.h>
22#include <fcntl.h>
23#include <dlfcn.h>
24#include <limits.h>
25#include <stdlib.h>
26#include <stdarg.h>
27#include <stddef.h>
28#include <string.h>
29#include <stdio.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <unistd.h>
33
34#include "testsuite.h"
35
36struct mod {
37 struct mod *next;
38 int ret;
39 int errcode;
40 char name[];
41};
42
43static struct mod *modules;
44static bool need_init = true;
45
46static void parse_retcodes(struct mod *_modules, const char *s)
47{
48 const char *p;
49
50 if (s == NULL)
51 return;
52
53 for (p = s;;) {
54 struct mod *mod;
55 const char *modname;
56 char *end;
57 size_t modnamelen;
58 int ret, errcode;
59 long l;
60
61 modname = p;
62 if (modname == NULL || modname[0] == '\0')
63 break;
64
65 modnamelen = strcspn(s, ":");
66 if (modname[modnamelen] != ':')
67 break;
68
69 p = modname + modnamelen + 1;
70 if (p == NULL)
71 break;
72
73 l = strtol(p, &end, 0);
74 if (end == p || *end != ':')
75 break;
76 ret = (int) l;
77 p = end + 1;
78
79 l = strtol(p, &end, 0);
80 if (*end == ':')
81 p = end + 1;
82 else if (*end != '\0')
83 break;
84
85 errcode = (int) l;
86
87 mod = malloc(sizeof(*mod) + modnamelen + 1);
88 if (mod == NULL)
89 break;
90
91 memcpy(mod->name, modname, modnamelen);
92 mod->name[modnamelen] = '\0';
93 mod->ret = ret;
94 mod->errcode = errcode;
95 mod->next = _modules;
96 _modules = mod;
97 }
98}
99
100static struct mod *find_module(struct mod *_modules, const char *modname)
101{
102 struct mod *mod;
103
104 for (mod = _modules; mod != NULL; mod = mod->next) {
105 if (strcmp(mod->name, modname))
106 return mod;
107 }
108
109 return NULL;
110}
111
112static void init_retcodes(void)
113{
114 const char *s;
115
116 if (!need_init)
117 return;
118
119 need_init = false;
120 s = getenv(S_TC_DELETE_MODULE_RETCODES);
121 if (s == NULL) {
122 fprintf(stderr, "TRAP delete_module(): missing export %s?\n",
123 S_TC_DELETE_MODULE_RETCODES);
124 }
125
126 parse_retcodes(modules, s);
127}
128
129TS_EXPORT long delete_module(const char *name, unsigned int flags);
130
131/*
132 * FIXME: change /sys/module/<modname> to fake-remove a module
133 *
134 * Default behavior is to exit successfully. If this is not the intended
135 * behavior, set TESTSUITE_DELETE_MODULE_RETCODES env var.
136 */
137long delete_module(const char *modname, unsigned int flags)
138{
139 struct mod *mod;
140
141 init_retcodes();
142 mod = find_module(modules, modname);
143 if (mod == NULL)
144 return 0;
145
146 errno = mod->errcode;
147 return mod->ret;
148}
149
150/* the test is going away anyway, but lets keep valgrind happy */
151void free_resources(void) __attribute__((destructor));
152void free_resources(void)
153{
154 while (modules) {
155 struct mod *mod = modules->next;
156 free(modules);
157 modules = mod;
158 }
159}