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
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -51,6 +51,7 @@ Description
#include <sys/stat.h>
#include <sys/socket.h>
#include <netdb.h>
#include <dlfcn.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
Foam::fileName Foam::home()
{
@ -240,7 +247,7 @@ Foam::fileName Foam::cwd()
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
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -24,8 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "dlLibraryTable.H"
#include <dlfcn.h>
#include "OSspecific.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -56,7 +55,7 @@ Foam::dlLibraryTable::~dlLibraryTable()
{
forAllConstIter(dlLibraryTable, *this, iter)
{
dlclose(iter.key());
dlClose(iter.key());
}
}
@ -67,15 +66,14 @@ bool Foam::dlLibraryTable::open(const fileName& functionLibName)
{
if (functionLibName.size())
{
void* functionLibPtr =
dlopen(functionLibName.c_str(), RTLD_LAZY|RTLD_GLOBAL);
void* functionLibPtr = dlOpen(functionLibName);
if (!functionLibPtr)
{
WarningIn
(
"dlLibraryTable::open(const fileName& functionLibName)"
) << "could not load " << dlerror()
) << "could not load " << functionLibName
<< endl;
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
(
const dictionary& dict,

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -97,6 +97,12 @@ public:
//- Open the named library
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
// given dictionary if present
static bool open(const dictionary&, const word& libsEntry);

View File

@ -77,6 +77,9 @@ word domainName();
//- Return the user's login name
word userName();
//- Is user administrator
bool isAdministrator();
//- Return home directory path name for the current user
fileName home();
@ -181,6 +184,16 @@ bool ping(const word&, const label timeOut=10);
//- Execute the specified 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