2 * Copyright (C) 2012 ProFUSION embedded systems
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.
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.
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/>.
29 #include <sys/types.h>
33 /* kmod_elf_get_section() is not exported, we need the private header */
34 #include <libkmod-private.h>
36 /* FIXME: hack, change name so we don't clash */
38 #include "testsuite.h"
39 #include "stripped-module.h"
48 static struct mod
*modules
;
49 static bool need_init
= true;
51 static void parse_retcodes(struct mod
*_modules
, const char *s
)
67 if (modname
== NULL
|| modname
[0] == '\0')
70 modnamelen
= strcspn(s
, ":");
71 if (modname
[modnamelen
] != ':')
74 p
= modname
+ modnamelen
+ 1;
78 l
= strtol(p
, &end
, 0);
79 if (end
== p
|| *end
!= ':')
84 l
= strtol(p
, &end
, 0);
87 else if (*end
!= '\0')
92 mod
= malloc(sizeof(*mod
) + modnamelen
+ 1);
96 memcpy(mod
->name
, modname
, modnamelen
);
97 mod
->name
[modnamelen
] = '\0';
99 mod
->errcode
= errcode
;
100 mod
->next
= _modules
;
105 static struct mod
*find_module(struct mod
*_modules
, const char *modname
)
109 for (mod
= _modules
; mod
!= NULL
; mod
= mod
->next
) {
110 if (strcmp(mod
->name
, modname
))
117 static void init_retcodes(void)
125 s
= getenv(S_TC_INIT_MODULE_RETCODES
);
127 fprintf(stderr
, "TRAP init_module(): missing export %s?\n",
128 S_TC_INIT_MODULE_RETCODES
);
131 parse_retcodes(modules
, s
);
134 TS_EXPORT
long init_module(void *mem
, unsigned long len
, const char *args
);
137 * FIXME: change /sys/module/<modname> to fake-insert a module
139 * Default behavior is to exit successfully. If this is not the intended
140 * behavior, set TESTSUITE_INIT_MODULE_RETCODES env var.
142 long init_module(void *mem
, unsigned long len
, const char *args
)
145 struct kmod_elf
*elf
;
153 elf
= kmod_elf_new(mem
, len
);
157 err
= kmod_elf_get_section(elf
, ".gnu.linkonce.this_module", &buf
,
162 * We couldn't find the module's name inside the ELF file. Just exit
163 * as if it was successful
168 modname
= (char *)buf
+ offsetof(struct module
, name
);
169 mod
= find_module(modules
, modname
);
173 errno
= mod
->errcode
;
177 /* the test is going away anyway, but lets keep valgrind happy */
178 void free_resources(void) __attribute__((destructor
));
179 void free_resources(void)
182 struct mod
*mod
= modules
->next
;