#include <unistd.h>
#include <shared/util.h>
+#include <shared/strbuf.h>
#include "testsuite.h"
}
}
-TS_EXPORT long delete_module(const char *name, unsigned int flags);
+static int remove_directory(const char *path)
+{
+ struct stat st;
+ DIR *dir;
+ struct dirent *entry;
+ char full_path[PATH_MAX];
+
+ if (stat(path, &st) != 0 || !S_ISDIR(st.st_mode)) {
+ LOG("Directory %s not found, skip remove.\n", path);
+ return 0;
+ }
+
+ dir = opendir(path);
+ if (!dir) {
+ ERR("Failed to open directory %s: %s (errno: %d)\n", path,
+ strerror(errno), errno);
+ return -1;
+ }
+
+ while ((entry = readdir(dir)) != NULL) {
+ if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
+ continue;
+
+ snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
+
+ if (entry->d_type == DT_DIR) {
+ if (remove_directory(full_path) != 0) {
+ ERR("Failed to remove directory %s: %m\n", full_path);
+ goto fail;
+ }
+ } else {
+ if (remove(full_path) != 0) {
+ ERR("Failed to remove file %s: %m\n", full_path);
+ goto fail;
+ }
+ }
+ }
+
+ closedir(dir);
+
+ if (rmdir(path) != 0) {
+ ERR("Failed to remove directory %s: %m\n", path);
+ return -1;
+ }
+
+ return 0;
+
+fail:
+ closedir(dir);
+
+ return -1;
+}
/*
- * FIXME: change /sys/module/<modname> to fake-remove a module
- *
* Default behavior is to exit successfully. If this is not the intended
* behavior, set TESTSUITE_DELETE_MODULE_RETCODES env var.
*/
+TS_EXPORT long delete_module(const char *name, unsigned int flags);
+
long delete_module(const char *modname, unsigned int flags)
{
+ DECLARE_STRBUF_WITH_STACK(buf, PATH_MAX);
struct mod *mod;
+ int ret = 0;
init_retcodes();
mod = find_module(modules, modname);
if (mod == NULL)
return 0;
+ if (!strbuf_pushchars(&buf, "/sys/module/") ||
+ !strbuf_pushchars(&buf, modname)) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ ret = remove_directory(strbuf_str(&buf));
+ if (ret != 0)
+ return ret;
+
errno = mod->errcode;
+
return mod->ret;
}
WRAP_1ARG(DIR *, NULL, opendir);
WRAP_1ARG(int, -1, chdir);
+WRAP_1ARG(int, -1, remove);
+WRAP_1ARG(int, -1, rmdir);
WRAP_2ARGS(FILE *, NULL, fopen, const char *);
WRAP_2ARGS(int, -1, mkdir, mode_t);
--- /dev/null
+#include <errno.h>
+#include <inttypes.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <shared/macro.h>
+
+#include <libkmod/libkmod.h>
+
+#include "testsuite.h"
+
+static noreturn int test_remove(const struct test *t)
+{
+ struct kmod_ctx *ctx;
+ struct kmod_module *mod;
+ const char *null_config = NULL;
+ int err;
+ struct stat st;
+
+ ctx = kmod_new(NULL, &null_config);
+ if (ctx == NULL)
+ exit(EXIT_FAILURE);
+
+ err = kmod_module_new_from_path(ctx, "/mod-simple.ko", &mod);
+ if (err != 0) {
+ ERR("could not create module from path: %m\n");
+ exit(EXIT_FAILURE);
+ }
+
+ err = kmod_module_insert_module(mod, 0, NULL);
+ if (err != 0) {
+ ERR("could not insert module: %m\n");
+ exit(EXIT_FAILURE);
+ }
+
+ err = kmod_module_remove_module(mod, 0);
+ if (err != 0) {
+ ERR("could not remove module: %m\n");
+ exit(EXIT_FAILURE);
+ }
+
+ if (stat("/sys/module/mod_simple", &st) == 0 && S_ISDIR(st.st_mode)) {
+ ERR("could not remove module directory.\n");
+ exit(EXIT_FAILURE);
+ }
+ kmod_unref(ctx);
+
+ exit(EXIT_SUCCESS);
+}
+DEFINE_TEST(test_remove,
+ .description = "test if libkmod's delete_module removes module directory",
+ .config = {
+ [TC_ROOTFS] = TESTSUITE_ROOTFS "test-remove/",
+ [TC_INIT_MODULE_RETCODES] = "",
+ [TC_DELETE_MODULE_RETCODES] = "mod_simple:0:0" STRINGIFY(ENOENT),
+ });
+
+TESTSUITE_MAIN();