]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
testsuite: create initstate file upon fake init_module()
authorLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 25 May 2012 03:30:37 +0000 (00:30 -0300)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 5 Jun 2012 03:54:47 +0000 (00:54 -0300)
TODO
testsuite/init_module.c

diff --git a/TODO b/TODO
index 71f2a47e2a047761d5472097b82bf4e7fab07c9b..221172dabd15eca185fa1fc3292340bb9a0cfc73 100644 (file)
--- a/TODO
+++ b/TODO
@@ -2,7 +2,6 @@ Features:
 =========
 
 * testsuite:
-   - when fake init_module() succeeds, create an entry in /sys/module
    - when fake delete_module() succeeds, remove its entry from /sys/module
    - add test for dependency loop _with install commands_ relying on module
      already been loaded in order to succeed. E.g.: alsa install rules on
index 8a963cce23fb253b377fbd7d9b2ed8d6119ee888..a76ca2dbec79e5155a24e40c54f10bc694a3f644 100644 (file)
@@ -35,6 +35,7 @@
 
 /* FIXME: hack, change name so we don't clash */
 #undef ERR
+#include "mkdir.h"
 #include "testsuite.h"
 #include "stripped-module.h"
 
@@ -102,6 +103,55 @@ static void parse_retcodes(struct mod *_modules, const char *s)
        }
 }
 
+static int write_one_line_file(const char *fn, const char *line, int len)
+{
+        FILE *f;
+        int r;
+
+        assert(fn);
+        assert(line);
+
+        f = fopen(fn, "we");
+        if (!f)
+                return -errno;
+
+        errno = 0;
+        if (fputs(line, f) < 0) {
+                r = -errno;
+                goto finish;
+        }
+
+        fflush(f);
+
+        if (ferror(f)) {
+                if (errno != 0)
+                        r = -errno;
+                else
+                        r = -EIO;
+        } else
+                r = 0;
+
+finish:
+        fclose(f);
+        return r;
+}
+
+static int create_sysfs_files(const char *modname)
+{
+       char buf[PATH_MAX];
+       const char *sysfsmod = "/sys/module/";
+       int len = strlen(sysfsmod);
+
+       memcpy(buf, sysfsmod, len);
+       strcpy(buf + len, modname);
+       len += strlen(modname);
+
+       mkdir_p(buf, 0755);
+
+       strcpy(buf + len, "/initstate");
+       return write_one_line_file(buf, "live\n", strlen("live\n"));
+}
+
 static struct mod *find_module(struct mod *_modules, const char *modname)
 {
        struct mod *mod;
@@ -134,8 +184,6 @@ static void init_retcodes(void)
 TS_EXPORT long init_module(void *mem, unsigned long len, const char *args);
 
 /*
- * FIXME: change /sys/module/<modname> to fake-insert a module
- *
  * Default behavior is to exit successfully. If this is not the intended
  * behavior, set TESTSUITE_INIT_MODULE_RETCODES env var.
  */
@@ -167,8 +215,10 @@ long init_module(void *mem, unsigned long len, const char *args)
 
        modname = (char *)buf + offsetof(struct module, name);
        mod = find_module(modules, modname);
-       if (mod == NULL)
+       if (mod == NULL) {
+               create_sysfs_files(modname);
                return 0;
+       }
 
        errno = mod->errcode;
        return mod->ret;