From 6c4ef7fd473cb40640631d9611338dae775dc3b1 Mon Sep 17 00:00:00 2001 From: Ethereal Date: Sun, 14 Nov 2010 18:37:39 -0700 Subject: [PATCH] I apparently had amnesia regarding dlopen(). Of course one cannot overwrite already-resolved symbols using dlopen(). Of course . . . Back to the old system. --- include/informer/Informer.h | 5 ++-- modules/cpuTime/src/collector/cpuTime.c | 11 ++++++-- modules/informer/src/collector/informer.c | 43 ++++++++++++++++++++++++++++--- monitor/src/Coordinator.cpp | 3 ++- monitor/src/program/Launcher.cpp | 18 ++++++++----- tests/exitTest.c | 8 ++++++ tests/mallocTest.c | 10 +++++++ 7 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 tests/exitTest.c create mode 100644 tests/mallocTest.c diff --git a/include/informer/Informer.h b/include/informer/Informer.h index 309515f..2dc4a2f 100644 --- a/include/informer/Informer.h +++ b/include/informer/Informer.h @@ -20,9 +20,10 @@ void __attribute__((constructor)) AI_Construct(); void __attribute__((destructor)) AI_Destruct(); -void AI_createSHM(); +void AI_CreateSHM(); +void AI_LoadModule(const char *moduleName); -void AI_sendPacket(Packet *packet); +void AI_SendPacket(Packet *packet); const char *AI_ConfigurationString(const char *name); int32_t AI_ConfigurationLong(const char *name); diff --git a/modules/cpuTime/src/collector/cpuTime.c b/modules/cpuTime/src/collector/cpuTime.c index 1ecd0eb..b869d9a 100644 --- a/modules/cpuTime/src/collector/cpuTime.c +++ b/modules/cpuTime/src/collector/cpuTime.c @@ -1,10 +1,17 @@ #include +#include +#include +#include +#include -void __attribute__((constructor)) Construct() { +void __attribute__((constructor)) AC_Construct() { printf("cpuTime constructing . . .\n"); } -void __attribute__((destructor)) Destruct() { +void __attribute__((destructor)) AC_Destruct() { } +void *malloc(size_t size) { + return NULL; +} diff --git a/modules/informer/src/collector/informer.c b/modules/informer/src/collector/informer.c index 1fd0a1b..0c1c071 100644 --- a/modules/informer/src/collector/informer.c +++ b/modules/informer/src/collector/informer.c @@ -15,7 +15,19 @@ void __attribute__((constructor)) AI_Construct() { printf("**** Constructing Informer . . .\n"); - AI_createSHM(); + AI_CreateSHM(); + + const char *p = AI_ConfigurationString("::moduleList"); + char moduleList[BUFSIZ]; + strcpy(moduleList, p); + const char *moduleName; + char *save = NULL; + moduleName = strtok_r(moduleList, ", ", &save); + do { + if(moduleName == NULL) break; + printf("\"%s\"\n", moduleName); + if(strcmp(moduleName, "informer")) AI_LoadModule(moduleName); + } while((moduleName = strtok_r(NULL, ", ", &save))); } void __attribute__((destructor)) AI_Destruct() { @@ -26,7 +38,7 @@ void __attribute__((destructor)) AI_Destruct() { } } -void AI_createSHM() { +void AI_CreateSHM() { char shmName[256] = {0}; sprintf(shmName, "AI-%i", getpid()); @@ -55,6 +67,31 @@ void AI_SendPacket(Packet *packet) { } +void AI_LoadModule(const char *moduleName) { + char buffer[BUFSIZ]; + strcpy(buffer, moduleName); + strcat(buffer, ":root"); + const char *root = AI_ConfigurationString(buffer); + strcpy(buffer, moduleName); + strcat(buffer, ":collectorPath"); + const char *collectorPath = AI_ConfigurationString(buffer); + strcpy(buffer, root); + strcat(buffer, collectorPath); +#ifdef RTLD_DEEPBIND + void *handle = dlopen(buffer, RTLD_GLOBAL | RTLD_DEEPBIND | RTLD_NOW); +#else + void *handle = dlopen(buffer, RTLD_GLOBAL | RTLD_NOW); +#endif + + int conductorFd = AI_ConfigurationLong("::conductorFd"); + + uint8_t header = ConductorPacket_ModuleLoaded; + write(conductorFd, &header, sizeof(header)); + uint16_t length = strlen(moduleName) + 1; + write(conductorFd, &length, sizeof(length)); + write(conductorFd, moduleName, length); +} + const char *AI_ConfigurationString(const char *name) { char realname[256] = {"AC_"}; int i = 0; @@ -86,7 +123,7 @@ pid_t fork() { *(void **)(&realFork) = dlsym(RTLD_NEXT, "fork"); pid_t value = realFork(); - if(value == 0) AI_createSHM(); + if(value == 0) AI_CreateSHM(); return value; } diff --git a/monitor/src/Coordinator.cpp b/monitor/src/Coordinator.cpp index 0e866e3..53a6d7d 100644 --- a/monitor/src/Coordinator.cpp +++ b/monitor/src/Coordinator.cpp @@ -45,7 +45,8 @@ void Coordinator::run() { std::vector list; m_vault->match("*", list); for(std::vector::iterator i = list.begin(); i != list.end(); ++i) { - if(i->first[0] == ':' && i->first[1] == ':') continue; + // NOTE: left out for debugging purposes + //if(i->first[0] == ':' && i->first[1] == ':') continue; std::cout << " * \"" << i->first << "\" ==> \"" << i->second << "\"\n"; } diff --git a/monitor/src/program/Launcher.cpp b/monitor/src/program/Launcher.cpp index 6a97200..b787aa9 100644 --- a/monitor/src/program/Launcher.cpp +++ b/monitor/src/program/Launcher.cpp @@ -90,15 +90,17 @@ void Launcher::setupEnvironment() { preload += ":"; } + preload += Coordinator::instance()->vault()->get("informer:root"); + preload += Coordinator::instance()->vault()->get("informer:collectorPath"); + + setenv("LD_PRELOAD", preload.c_str(), 1); + + std::string moduleEnv; + for(std::vector::iterator i = modules.begin(); i != modules.end(); ++i) { std::string moduleRoot = Coordinator::instance()->vault()->get(*i + ":root"); std::string collectorPath = Coordinator::instance()->vault()->get(*i + ":collectorPath"); if(collectorPath.length()) { - if(preload.length()) { - preload += ":"; - } - preload += moduleRoot + collectorPath; - std::vector configItems; Coordinator::instance()->vault()->match(*i + ":*", configItems); @@ -110,9 +112,11 @@ void Launcher::setupEnvironment() { setenv(envName.c_str(), i->second.c_str(), 1); } } + moduleEnv += ","; + moduleEnv += *i; } - - setenv("LD_PRELOAD", preload.c_str(), 1); + + setenv("AC___moduleList", moduleEnv.c_str(), 1); } } // namespace Program diff --git a/tests/exitTest.c b/tests/exitTest.c new file mode 100644 index 0000000..822ecc1 --- /dev/null +++ b/tests/exitTest.c @@ -0,0 +1,8 @@ +#include +#include + +int main(int argc, char *argv[]) { + printf("In exitTest . . .\n"); + exit(1); + return 0; +} diff --git a/tests/mallocTest.c b/tests/mallocTest.c new file mode 100644 index 0000000..3c20be1 --- /dev/null +++ b/tests/mallocTest.c @@ -0,0 +1,10 @@ +#include +#include + +int main(int argc, char *argv[]) { + char *block = malloc(sizeof(char) * 1024); + printf("---- mallocTest: block address: %p\n", block); + free(block); + return 0; +} + -- 2.11.4.GIT