Add binary to test rmmod feature
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 23 Nov 2011 19:22:09 +0000 (17:22 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 23 Nov 2011 19:22:09 +0000 (17:22 -0200)
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

Makefile.am
test/.gitignore
test/test-rmmod.c [new file with mode: 0644]

index 929de86..a55a03a 100644 (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
index 20b56b7..1cbe6b8 100644 (file)
@@ -1,3 +1,4 @@
 .dirstamp
 test-init
 test-loaded
+test-rmmod
diff --git a/test/test-rmmod.c b/test/test-rmmod.c
new file mode 100644 (file)
index 0000000..759743e
--- /dev/null
@@ -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;
+}