00001 #include "SymbolManager.h" 00002 #include "IEvaluatorExpressionBase.h" 00003 #include "IFilterExpressionBase.h" 00004 00005 #include <cstdlib> 00006 #include <dlfcn.h> 00007 00008 #ifdef OLDSTREAMS 00009 # include <strstream> 00010 # define ostringstream ostrstream 00011 #else 00012 # include <sstream> 00013 #endif 00014 00015 Anaphe::AIDA_Tuple_native::SymbolManager::SymbolManager() 00016 {} 00017 00018 Anaphe::AIDA_Tuple_native::SymbolManager::~SymbolManager() 00019 { 00020 for ( std::map< IEvaluatorExpressionBaseFactory*, void* >::const_iterator iHandle = m_evaluatorHandles.begin(); 00021 iHandle != m_evaluatorHandles.end(); ++iHandle ) { 00022 dlclose( iHandle->second ); 00023 } 00024 for ( std::map< IFilterExpressionBaseFactory*, void* >::const_iterator iHandle = m_filterHandles.begin(); 00025 iHandle != m_filterHandles.end(); ++iHandle ) { 00026 dlclose( iHandle->second ); 00027 } 00028 } 00029 00030 bool 00031 Anaphe::AIDA_Tuple_native::SymbolManager::setIncludePaths( const std::set<std::string>& includes ) 00032 { 00033 m_includePaths = includes; 00034 return true; 00035 } 00036 00037 const std::set<std::string>& 00038 Anaphe::AIDA_Tuple_native::SymbolManager::includePaths() const 00039 { 00040 return m_includePaths; 00041 } 00042 00043 Anaphe::AIDA_Tuple_native::IEvaluatorExpressionBaseFactory* 00044 Anaphe::AIDA_Tuple_native::SymbolManager::makeAndLoadEvaluator( const std::string& fileName, 00045 const std::string& symbolName ) 00046 { 00047 const std::string libraryName = fileName + ".so"; 00048 std::string systemCall; 00049 #if defined LINUX 00050 systemCall = "g++ -shared "; 00051 #elif defined SOLARIS 00052 systemCall = "CC -G -lCrun "; 00053 #endif 00054 systemCall += fileName + " -o " + libraryName; 00055 for ( std::set<std::string>::const_iterator iPath = m_includePaths.begin(); 00056 iPath != m_includePaths.end(); ++iPath ) { 00057 systemCall += " -I" + (*iPath); 00058 } 00059 00060 const std::string rmSystemCall = "rm -f " + fileName; 00061 const std::string rmLibSystemCall = "rm -f " + libraryName; 00062 00063 if ( std::system( systemCall.c_str() ) != 0 ) { 00064 std::system( rmSystemCall.c_str() ); 00065 return 0; 00066 } 00067 00068 // load the symbol 00069 void* ldHandle = dlopen( ( "./" + libraryName ).c_str(), RTLD_LAZY ); 00070 char* error = dlerror(); 00071 if ( error != NULL ) return 0; 00072 void* p = dlsym( ldHandle, symbolName.c_str() ); 00073 if ( dlerror() != NULL ) { 00074 dlclose( ldHandle ); 00075 std::system( rmSystemCall.c_str() ); 00076 std::system( rmLibSystemCall.c_str() ); 00077 return 0; 00078 } 00079 00080 Anaphe::AIDA_Tuple_native::IEvaluatorExpressionBaseFactory* factory = *(reinterpret_cast<Anaphe::AIDA_Tuple_native::IEvaluatorExpressionBaseFactory**>( p ) ); 00081 std::system( rmSystemCall.c_str() ); 00082 std::system( rmLibSystemCall.c_str() ); 00083 00084 // Register the symbol 00085 m_evaluatorHandles[ factory ] = ldHandle; 00086 return factory; 00087 } 00088 00089 bool 00090 Anaphe::AIDA_Tuple_native::SymbolManager::destroyAndUnload( IEvaluatorExpressionBaseFactory* factory ) 00091 { 00092 dlclose( m_evaluatorHandles[ factory ] ); 00093 m_evaluatorHandles.erase( factory ); 00094 return true; 00095 } 00096 00097 Anaphe::AIDA_Tuple_native::IFilterExpressionBaseFactory* 00098 Anaphe::AIDA_Tuple_native::SymbolManager::makeAndLoadFilter( const std::string& fileName, 00099 const std::string& symbolName ) 00100 { 00101 const std::string libraryName = fileName + ".so"; 00102 std::string systemCall; 00103 #if defined LINUX 00104 systemCall = "g++ -shared "; 00105 #elif defined SOLARIS 00106 systemCall = "CC -G -lCrun "; 00107 #endif 00108 systemCall += fileName + " -o " + libraryName; 00109 for ( std::set<std::string>::const_iterator iPath = m_includePaths.begin(); 00110 iPath != m_includePaths.end(); ++iPath ) { 00111 systemCall += " -I" + (*iPath); 00112 } 00113 00114 const std::string rmSystemCall = "rm -f " + fileName; 00115 const std::string rmLibSystemCall = "rm -f " + libraryName; 00116 00117 if ( std::system( systemCall.c_str() ) != 0 ) { 00118 std::system( rmSystemCall.c_str() ); 00119 return 0; 00120 } 00121 00122 // load the symbol 00123 void* ldHandle = dlopen( ( "./" + libraryName ).c_str(), RTLD_LAZY ); 00124 char* error = dlerror(); 00125 if ( error != NULL ) return 0; 00126 void* p = dlsym( ldHandle, symbolName.c_str() ); 00127 if ( dlerror() != NULL ) { 00128 dlclose( ldHandle ); 00129 std::system( rmSystemCall.c_str() ); 00130 std::system( rmLibSystemCall.c_str() ); 00131 return 0; 00132 } 00133 00134 Anaphe::AIDA_Tuple_native::IFilterExpressionBaseFactory* factory = *(reinterpret_cast<Anaphe::AIDA_Tuple_native::IFilterExpressionBaseFactory**>( p ) ); 00135 std::system( rmSystemCall.c_str() ); 00136 std::system( rmLibSystemCall.c_str() ); 00137 00138 // Register the symbol 00139 m_filterHandles[ factory ] = ldHandle; 00140 return factory; 00141 } 00142 00143 bool 00144 Anaphe::AIDA_Tuple_native::SymbolManager::destroyAndUnload( IFilterExpressionBaseFactory* factory ) 00145 { 00146 dlclose( m_filterHandles[ factory ] ); 00147 m_filterHandles.erase( factory ); 00148 return true; 00149 }