]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
testsuite: export environment with flags and LD_PRELOAD
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Wed, 25 Jan 2012 01:31:46 +0000 (23:31 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Thu, 26 Jan 2012 18:05:04 +0000 (16:05 -0200)
A certain config can add flags and each flag may be associated with a
lib to LD_PRELOAD. It's now done for uname(2), which requires uname.so
in order to trap the calls.

Other trap will be added in later commits.

testsuite/testsuite.c

index 57949ef78bef6db29c22b4ca783fd24a8bf1b2d9..f26e358d1991cba634a694d1da54272a1a0fa5c4 100644 (file)
@@ -21,6 +21,14 @@ static const struct option options[] = {
        { NULL, 0, 0, 0 }
 };
 
+struct _env_config {
+       const char *key;
+       const char *ldpreload;
+} env_config[_TC_LAST] = {
+       [TC_UNAME_R] = { S_TC_UNAME_R, "./testsuite/uname.so" },
+       [TC_ROOTFS] = { S_TC_ROOTFS, "./testsuite/path.so" },
+};
+
 static void help(void)
 {
        const struct option *itr;
@@ -112,7 +120,44 @@ int test_spawn_prog(const char *prog, const char *args[])
        return EXIT_FAILURE;
 }
 
+static void test_export_environ(const struct test *t)
 {
+       char *preload = NULL;
+       size_t preloadlen = 0;
+       size_t i;
+
+       unsetenv("LD_PRELOAD");
+
+       for (i = 0; i < _TC_LAST; i++) {
+               const char *ldpreload;
+               size_t ldpreloadlen;
+               char *tmp;
+
+               if (t->config[i] == NULL)
+                       continue;
+
+               setenv(env_config[i].key, t->config[i], 1);
+
+               ldpreload = env_config[i].ldpreload;
+               ldpreloadlen = strlen(ldpreload);
+               tmp = realloc(preload, preloadlen + 2 + ldpreloadlen);
+               if (tmp == NULL) {
+                       ERR("oom: test_export_environ()\n");
+                       return;
+               }
+               preload = tmp;
+
+               if (preloadlen > 0)
+                       preload[preloadlen++] = ' ';
+               memcpy(preload + preloadlen, ldpreload, ldpreloadlen);
+               preloadlen += ldpreloadlen;
+               preload[preloadlen] = '\0';
+       }
+
+       if (preload != NULL)
+               setenv("LD_PRELOAD", preload, 1);
+
+       free(preload);
 }
 
 int test_run(const struct test *t)
@@ -151,6 +196,8 @@ int test_run(const struct test *t)
         /* kill child if parent dies */
         prctl(PR_SET_PDEATHSIG, SIGTERM);
 
+       test_export_environ(t);
+
        if (t->need_spawn)
                return test_spawn_test(t);
        else