Add binary to test rmmod feature

It doesn't run with `make check' since
	o It's dangerous
	o It needs to be run as root
	o It needs an argument, otherwise it removes the first module
	  with use_count==0
This commit is contained in:
Lucas De Marchi 2011-11-23 17:22:09 -02:00
parent 6806a0437f
commit eee1345cf2
3 changed files with 63 additions and 1 deletions

View File

@ -45,9 +45,13 @@ pkgconfig_DATA = libkmod/libkmod.pc
TESTS = test/test-init test/test-loaded
check_PROGRAMS = test/test-init test/test-loaded
check_PROGRAMS = test/test-init test/test-loaded test/test-rmmod
test_test_init_SOURCES = test/test-init.c
test_test_init_LDADD = libkmod/libkmod.la
test_test_loaded_SOURCES = test/test-loaded.c
test_test_loaded_LDADD = libkmod/libkmod.la
noinst_PROGRAMS = test/test-rmmod $(check_PROGRAMS)
test_test_rmmod_SOURCES = test/test-rmmod.c
test_test_rmmod_LDADD = libkmod/libkmod.la

1
test/.gitignore vendored
View File

@ -1,3 +1,4 @@
.dirstamp
test-init
test-loaded
test-rmmod

57
test/test-rmmod.c Normal file
View File

@ -0,0 +1,57 @@
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>
#include <unistd.h>
#include <inttypes.h>
#include <string.h>
#include <libkmod.h>
int main(int argc, char *argv[])
{
const char *modname = NULL;
struct kmod_ctx *ctx;
struct kmod_loaded *mod;
struct kmod_list *list, *itr;
int err;
if (argc == 2)
modname = argv[1];
ctx = kmod_new();
if (ctx == NULL)
exit(EXIT_FAILURE);
printf("libkmod version %s\n", VERSION);
err = kmod_loaded_new(ctx, &mod);
if (err < 0)
exit(EXIT_FAILURE);
err = kmod_loaded_get_list(mod, &list);
if (err < 0) {
fprintf(stderr, "%s\n", strerror(-err));
exit(EXIT_FAILURE);
}
kmod_list_foreach(itr, list) {
const char *name;
int use_count;
kmod_loaded_get_module_info(itr, &name, NULL, &use_count,
NULL, NULL);
if ((modname && !strcmp(modname, name)) ||
(modname == NULL && use_count == 0)) {
printf("Trying to remove '%s'\n", name);
kmod_loaded_remove_module(mod, itr, 0);
break;
}
}
kmod_loaded_unref(mod);
kmod_unref(ctx);
return EXIT_SUCCESS;
}