From 395478cbbbd5b68a65514ee1e41e634d3d5d62db Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Tue, 24 Jan 2012 23:31:46 -0200 Subject: [PATCH] testsuite: export environment with flags and LD_PRELOAD 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 | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c index 57949ef..f26e358 100644 --- a/testsuite/testsuite.c +++ b/testsuite/testsuite.c @@ -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 -- 2.7.4