2002-04-10 06:42:52 +02:00
|
|
|
/*
|
|
|
|
* mod_loader.cpp
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../jrd/os/mod_loader.h"
|
2002-06-23 21:51:37 +02:00
|
|
|
#include "../../common.h"
|
2002-08-26 14:18:16 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2002-04-10 06:42:52 +02:00
|
|
|
#include <unistd.h>
|
2002-08-26 14:18:16 +02:00
|
|
|
#endif
|
2002-04-10 06:42:52 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
/// This is the POSIX (dlopen) implementation of the mod_loader abstraction.
|
|
|
|
|
|
|
|
class DlfcnModule : public ModuleLoader::Module
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DlfcnModule(void *m) : module(m) {}
|
|
|
|
~DlfcnModule();
|
|
|
|
void *findSymbol(const Firebird::string&);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void *module;
|
|
|
|
};
|
|
|
|
|
2004-02-08 19:47:47 +01:00
|
|
|
bool ModuleLoader::isLoadableModule(const Firebird::PathName& module)
|
2002-04-10 06:42:52 +02:00
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
if (-1 == stat(module.c_str(), &sb))
|
|
|
|
return false;
|
|
|
|
if ( ! (sb.st_mode & S_IFREG) ) // Make sure it is a plain file
|
|
|
|
return false;
|
|
|
|
if ( -1 == access(module.c_str(), R_OK | X_OK))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-02-08 19:47:47 +01:00
|
|
|
void ModuleLoader::doctorModuleExtention(Firebird::PathName& name)
|
2002-04-10 06:42:52 +02:00
|
|
|
{
|
2004-02-08 19:47:47 +01:00
|
|
|
Firebird::PathName::size_type pos = name.rfind(".so");
|
|
|
|
if (pos != Firebird::PathName::npos && pos == name.length() - 3)
|
2002-04-10 06:42:52 +02:00
|
|
|
return; // No doctoring necessary
|
|
|
|
name += ".so";
|
|
|
|
}
|
|
|
|
|
2004-02-08 19:47:47 +01:00
|
|
|
ModuleLoader::Module *ModuleLoader::loadModule(const Firebird::PathName& modPath)
|
2002-04-10 06:42:52 +02:00
|
|
|
{
|
|
|
|
void *module;
|
|
|
|
|
|
|
|
module = dlopen(modPath.c_str(), RTLD_LAZY);
|
|
|
|
if (module == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2002-09-25 19:12:16 +02:00
|
|
|
return FB_NEW(*getDefaultMemoryPool()) DlfcnModule(module);
|
2002-04-10 06:42:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DlfcnModule::~DlfcnModule()
|
|
|
|
{
|
|
|
|
if (module)
|
|
|
|
dlclose(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *DlfcnModule::findSymbol(const Firebird::string& symName)
|
|
|
|
{
|
|
|
|
void *result;
|
|
|
|
|
|
|
|
result = dlsym(module, symName.c_str());
|
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
Firebird::string newSym = '_' + symName;
|
|
|
|
|
|
|
|
result = dlsym(module, newSym.c_str());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|