blob: f3ae20ba79c92a9deb900730eb8191aee47d22f7 [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 Marchie701e382012-01-26 17:01:41 -02003 *
Lucas De Marchie1b1ab22012-07-10 09:42:24 -03004 * 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.
Lucas De Marchie701e382012-01-26 17:01:41 -02008 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Lucas De Marchie1b1ab22012-07-10 09:42:24 -030011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
Lucas De Marchie701e382012-01-26 17:01:41 -020013 *
Lucas De Marchie1b1ab22012-07-10 09:42:24 -030014 * You should have received a copy of the GNU Lesser General Public
Lucas De Marchidea2dfe2014-12-25 23:32:03 -020015 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Lucas De Marchie701e382012-01-26 17:01:41 -020016 */
17
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020018#include <assert.h>
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020019#include <dirent.h>
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020020#include <dlfcn.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030021#include <errno.h>
22#include <fcntl.h>
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020023#include <limits.h>
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020024#include <stdarg.h>
25#include <stddef.h>
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020026#include <stdio.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030027#include <stdlib.h>
28#include <string.h>
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020029#include <sys/stat.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030030#include <sys/types.h>
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020031#include <unistd.h>
32
Lucas De Marchiaf878742015-01-14 14:05:24 -020033#include <shared/util.h>
34
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020035#include "testsuite.h"
36
37struct mod {
38 struct mod *next;
39 int ret;
40 int errcode;
41 char name[];
42};
43
44static struct mod *modules;
45static bool need_init = true;
46
Lucas De Marchiaf878742015-01-14 14:05:24 -020047static void parse_retcodes(struct mod **_modules, const char *s)
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020048{
49 const char *p;
50
51 if (s == NULL)
52 return;
53
54 for (p = s;;) {
55 struct mod *mod;
56 const char *modname;
57 char *end;
58 size_t modnamelen;
59 int ret, errcode;
60 long l;
61
62 modname = p;
63 if (modname == NULL || modname[0] == '\0')
64 break;
65
Lucas De Marchiaf878742015-01-14 14:05:24 -020066 modnamelen = strcspn(p, ":");
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020067 if (modname[modnamelen] != ':')
68 break;
69
70 p = modname + modnamelen + 1;
71 if (p == NULL)
72 break;
73
74 l = strtol(p, &end, 0);
75 if (end == p || *end != ':')
76 break;
Lucas De Marchiaf878742015-01-14 14:05:24 -020077
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020078 ret = (int) l;
79 p = end + 1;
80
81 l = strtol(p, &end, 0);
82 if (*end == ':')
83 p = end + 1;
84 else if (*end != '\0')
85 break;
86
87 errcode = (int) l;
88
89 mod = malloc(sizeof(*mod) + modnamelen + 1);
90 if (mod == NULL)
91 break;
92
93 memcpy(mod->name, modname, modnamelen);
94 mod->name[modnamelen] = '\0';
95 mod->ret = ret;
96 mod->errcode = errcode;
Lucas De Marchiaf878742015-01-14 14:05:24 -020097 mod->next = *_modules;
98 *_modules = mod;
Lucas De Marchif6ef5d62012-01-26 16:10:41 -020099 }
100}
101
102static struct mod *find_module(struct mod *_modules, const char *modname)
103{
104 struct mod *mod;
105
106 for (mod = _modules; mod != NULL; mod = mod->next) {
Lucas De Marchiaf878742015-01-14 14:05:24 -0200107 if (streq(mod->name, modname))
Lucas De Marchif6ef5d62012-01-26 16:10:41 -0200108 return mod;
109 }
110
111 return NULL;
112}
113
114static void init_retcodes(void)
115{
116 const char *s;
Lucas De Marchiaf878742015-01-14 14:05:24 -0200117 struct mod *mod;
Lucas De Marchif6ef5d62012-01-26 16:10:41 -0200118
119 if (!need_init)
120 return;
121
122 need_init = false;
123 s = getenv(S_TC_DELETE_MODULE_RETCODES);
124 if (s == NULL) {
Lucas De Marchiaf878742015-01-14 14:05:24 -0200125 ERR("TRAP delete_module(): missing export %s?\n",
Lucas De Marchif6ef5d62012-01-26 16:10:41 -0200126 S_TC_DELETE_MODULE_RETCODES);
127 }
128
Lucas De Marchiaf878742015-01-14 14:05:24 -0200129 parse_retcodes(&modules, s);
130
131 for (mod = modules; mod != NULL; mod = mod->next) {
132 LOG("Added module to test delete_module:\n");
133 LOG("\tname=%s ret=%d errcode=%d\n",
134 mod->name, mod->ret, mod->errcode);
135 }
Lucas De Marchif6ef5d62012-01-26 16:10:41 -0200136}
137
138TS_EXPORT long delete_module(const char *name, unsigned int flags);
139
140/*
141 * FIXME: change /sys/module/<modname> to fake-remove a module
142 *
143 * Default behavior is to exit successfully. If this is not the intended
144 * behavior, set TESTSUITE_DELETE_MODULE_RETCODES env var.
145 */
146long delete_module(const char *modname, unsigned int flags)
147{
148 struct mod *mod;
149
150 init_retcodes();
151 mod = find_module(modules, modname);
152 if (mod == NULL)
153 return 0;
154
155 errno = mod->errcode;
156 return mod->ret;
157}
158
159/* the test is going away anyway, but lets keep valgrind happy */
160void free_resources(void) __attribute__((destructor));
161void free_resources(void)
162{
163 while (modules) {
164 struct mod *mod = modules->next;
165 free(modules);
166 modules = mod;
167 }
168}