diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index cc90da3db1..155fcea58d 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -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 #include #include +#include #include @@ -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; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/db/dlLibraryTable/dlLibraryTable.C b/src/OpenFOAM/db/dlLibraryTable/dlLibraryTable.C index a0fe132aaf..c98ba120a4 100644 --- a/src/OpenFOAM/db/dlLibraryTable/dlLibraryTable.C +++ b/src/OpenFOAM/db/dlLibraryTable/dlLibraryTable.C @@ -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 +#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, diff --git a/src/OpenFOAM/db/dlLibraryTable/dlLibraryTable.H b/src/OpenFOAM/db/dlLibraryTable/dlLibraryTable.H index 7b19a32d1d..1b64897dd0 100644 --- a/src/OpenFOAM/db/dlLibraryTable/dlLibraryTable.H +++ b/src/OpenFOAM/db/dlLibraryTable/dlLibraryTable.H @@ -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); diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H index 1c825984fa..7ad1bec12b 100644 --- a/src/OpenFOAM/include/OSspecific.H +++ b/src/OpenFOAM/include/OSspecific.H @@ -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