COMP: dlLibraryTable: moved dlopen etc into OSspecific

This commit is contained in:
mattijs
2011-02-18 18:05:40 +00:00
parent c37defa5e2
commit 93f408d584
4 changed files with 98 additions and 10 deletions

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -51,6 +51,7 @@ Description
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netdb.h> #include <netdb.h>
#include <dlfcn.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -164,6 +165,12 @@ Foam::word Foam::userName()
} }
bool Foam::isAdministrator()
{
return (geteuid() == 0);
}
// use $HOME environment variable or passwd info // use $HOME environment variable or passwd info
Foam::fileName Foam::home() Foam::fileName Foam::home()
{ {
@ -240,7 +247,7 @@ Foam::fileName Foam::cwd()
bool Foam::chDir(const fileName& dir) bool Foam::chDir(const fileName& dir)
{ {
return chdir(dir.c_str()) != 0; return chdir(dir.c_str()) == 0;
} }
@ -1065,4 +1072,31 @@ int Foam::system(const string& command)
} }
void* Foam::dlOpen(const fileName& lib)
{
return dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
}
bool Foam::dlClose(void* handle)
{
return dlclose(handle) == 0;
}
void* Foam::dlSym(void* handle, const string& symbol)
{
void* fun = dlsym(handle, symbol.c_str());
char *error;
if ((error = dlerror()) != NULL)
{
WarningIn("dlSym(void*, const string&)")
<< "Cannot lookup symbol " << symbol << " : " << error
<< endl;
}
return fun;
}
// ************************************************************************* // // ************************************************************************* //

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -24,8 +24,7 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "dlLibraryTable.H" #include "dlLibraryTable.H"
#include "OSspecific.H"
#include <dlfcn.h>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -56,7 +55,7 @@ Foam::dlLibraryTable::~dlLibraryTable()
{ {
forAllConstIter(dlLibraryTable, *this, iter) forAllConstIter(dlLibraryTable, *this, iter)
{ {
dlclose(iter.key()); dlClose(iter.key());
} }
} }
@ -67,15 +66,14 @@ bool Foam::dlLibraryTable::open(const fileName& functionLibName)
{ {
if (functionLibName.size()) if (functionLibName.size())
{ {
void* functionLibPtr = void* functionLibPtr = dlOpen(functionLibName);
dlopen(functionLibName.c_str(), RTLD_LAZY|RTLD_GLOBAL);
if (!functionLibPtr) if (!functionLibPtr)
{ {
WarningIn WarningIn
( (
"dlLibraryTable::open(const fileName& functionLibName)" "dlLibraryTable::open(const fileName& functionLibName)"
) << "could not load " << dlerror() ) << "could not load " << functionLibName
<< endl; << endl;
return false; return false;
@ -99,6 +97,43 @@ bool Foam::dlLibraryTable::open(const fileName& functionLibName)
} }
bool Foam::dlLibraryTable::close(const fileName& functionLibName)
{
void* libPtr = findLibrary(functionLibName);
if (libPtr)
{
loadedLibraries.erase(libPtr);
if (!dlClose(libPtr))
{
WarningIn
(
"dlLibraryTable::close(const fileName& functionLibName)"
) << "could not close " << functionLibName
<< endl;
return false;
}
return true;
}
return false;
}
void* Foam::dlLibraryTable::findLibrary(const fileName& functionLibName)
{
forAllConstIter(dlLibraryTable, loadedLibraries, iter)
{
if (iter() == functionLibName)
{
return iter.key();
}
}
return NULL;
}
bool Foam::dlLibraryTable::open bool Foam::dlLibraryTable::open
( (
const dictionary& dict, const dictionary& dict,

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -97,6 +97,12 @@ public:
//- Open the named library //- Open the named library
static bool open(const fileName& name); static bool open(const fileName& name);
//- Close the named library
static bool close(const fileName& name);
//- Find the handle of the named library
static void* findLibrary(const fileName& name);
//- Open all the libraries listed in the 'libsEntry' entry in the //- Open all the libraries listed in the 'libsEntry' entry in the
// given dictionary if present // given dictionary if present
static bool open(const dictionary&, const word& libsEntry); static bool open(const dictionary&, const word& libsEntry);

View File

@ -77,6 +77,9 @@ word domainName();
//- Return the user's login name //- Return the user's login name
word userName(); word userName();
//- Is user administrator
bool isAdministrator();
//- Return home directory path name for the current user //- Return home directory path name for the current user
fileName home(); fileName home();
@ -181,6 +184,16 @@ bool ping(const word&, const label timeOut=10);
//- Execute the specified command //- Execute the specified command
int system(const string& command); int system(const string& command);
//- open a shared library. Return handle to library
void* dlOpen(const fileName& lib);
//- Close a dlopened library using handle. Return true if successful
bool dlClose(void*);
//- Lookup a symbol in a dlopened library using handle
void* dlSym(void* handle, const string& symbol);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam } // End namespace Foam