24 #include <utils/system/dynamic_module/module.h> 25 #include <utils/system/file.h> 58 const char * Module::FILE_EXTENSION = SOEXT;
66 __filename = filename;
71 __is_resident =
false;
91 if ( __handle != NULL )
return;
94 std::string full_filename =
"";
95 full_filename = __filename;
97 if ( full_filename.find(
"." SOEXT, 0) != (full_filename.length() - 1 - strlen(FILE_EXTENSION)) ) {
99 full_filename +=
"." SOEXT;
103 tflags |= ((__flags & MODULE_BIND_LAZY) != 0) ? RTLD_LAZY : RTLD_NOW;
104 tflags |= ((__flags & MODULE_BIND_NOW) != 0) ? RTLD_NOW : 0;
105 tflags |= ((__flags & MODULE_BIND_LOCAL) != 0) ? RTLD_LOCAL : 0;
106 tflags |= ((__flags & MODULE_BIND_GLOBAL) != 0) ? RTLD_GLOBAL : 0;
107 tflags |= ((__flags & MODULE_NODELETE) != 0) ? RTLD_NODELETE : 0;
109 tflags |= ((__flags & MODULE_BIND_DEEP) != 0) ? RTLD_DEEPBIND : 0;
112 if ( full_filename ==
"") {
113 __handle = dlopen (NULL, tflags);
116 __is_resident =
true;
123 __handle = dlopen(full_filename.c_str(), tflags);
125 if ( NULL == __handle) {
126 const char *err = dlerror();
131 e.
append(
"dlerror: %s", err);
135 __is_resident =
false;
140 e.
append(
"File '%s' does not exist", full_filename.c_str());
153 if ( __handle == NULL )
return true;
155 if ( __ref_count > 0 ) --__ref_count;
157 if ( (__ref_count == 0) && ! __is_resident ) {
158 if ( dlclose(__handle) != 0 ) {
181 if ( __ref_count > 0 ) {
194 return (__ref_count == 0);
216 return (__filename == cmod.__filename);
232 if( symbol_name == NULL ) {
235 if ( __handle == NULL ) {
239 return ( dlsym( __handle, symbol_name ) != NULL );
255 if( symbol_name == NULL )
return NULL;
256 if ( __handle == NULL )
return NULL;
258 return dlsym( __handle, symbol_name );
270 return FILE_EXTENSION;
291 if ( __filename.find(
"/", 0) != std::string::npos ) {
292 std::string rv = __filename.substr(__filename.rfind(
"/", __filename.length()) + 1, __filename.length());
295 return __filename.c_str();
virtual void unref()
Decrease the reference count of this module.
virtual bool close()
Close the module.
ModuleOpenException(const char *msg)
Constructor.
virtual void open()
Open the module.
Fawkes library namespace.
virtual void ref()
Increment the reference count of this module.
virtual ~Module()
Destructor.
virtual unsigned int get_ref_count()
Get the reference count of this module.
ModuleFlags
Flags for the loading process.
virtual bool notref()
Check if there are no reference to this module.
Dynamic module loader for Linux, FreeBSD, and MacOS X.
Base class for exceptions in Fawkes.
virtual std::string get_base_filename()
Get the base file name of the module.
virtual void * get_symbol(const char *symbol_name)
Get a symbol from the module.
virtual bool operator==(const Module &cmod)
Compare to another Module instance.
static bool is_regular(const char *filename)
Check if a file is a regular file.
Module(std::string filename, ModuleFlags flags=MODULE_FLAGS_DEFAULT)
Constructor.
static const char * get_file_extension()
Get file extension for dl modules.
void append(const char *format,...)
Append messages to the message list.
virtual std::string get_filename()
Get the full file name of the module.
virtual bool has_symbol(const char *symbol_name)
Check if the module has the given symbol.