/**
 * \file dynamiclibrary.cpp
 * \author Thibault Schueller <ryp.sqrt@gmail.com>
 * \brief DynamicLibrary class
 */

#include "dynamiclibrary.hpp"
#include "exception/dynlibexception.hpp"

DynamicLibrary::DynamicLibrary(const std::string& file)
:   _file(file),
    _handle(nullptr)
{}

void DynamicLibrary::open(RelocationMode mode)
{
    char*   err;

    if (!(_handle = dlopen(_file.c_str(), static_cast<int>(mode))))
    {
        if ((err = dlerror()))
            throw (DynLibException(std::string("dlopen(): ") + err));
        else
            throw (DynLibException("dlopen(): Unknown error"));
    }
}

void DynamicLibrary::close()
{
    if (dlclose(_handle))
        throw (DynLibException(std::string("dlclose(): ") + dlerror()));
}

void* DynamicLibrary::getSymbol(const std::string& symbol)
{
    void*   sym;
    char*   err;

    sym = dlsym(_handle, symbol.c_str());
    if ((err = dlerror()))
        throw (DynLibException(std::string("dlsym(): ") + err));
    return (sym);
}

const std::string &DynamicLibrary::getFilePath() const
    {
    return _file;
    }

/**
 * \file dynamiclibrary.cpp
 * \author Thibault Schueller <ryp.sqrt@gmail.com>
 * \brief DynamicLibrary class
 */

#include "dynamiclibrary.hpp"
#include "exception/dynlibexception.hpp"

DynamicLibrary::DynamicLibrary(const std::string& file)
:   _file(file),
    _handle(nullptr)
{}

void DynamicLibrary::open(RelocationMode mode)
{
    char*   err;

    if (!(_handle = dlopen(_file.c_str(), static_cast<int>(mode) | RTLD_NODELETE)))
    {
        if ((err = dlerror()))
            throw (DynLibException(std::string("dlopen(): ") + err));
        else
            throw (DynLibException("dlopen(): Unknown error"));
    }
}

void DynamicLibrary::close()
{
    if (dlclose(_handle))
        throw (DynLibException(std::string("dlclose(): ") + dlerror()));
}

void* DynamicLibrary::getSymbol(const std::string& symbol)
{
    void*   sym;
    char*   err;

    sym = dlsym(_handle, symbol.c_str());
    if ((err = dlerror()))
        throw (DynLibException(std::string("dlsym(): ") + err));
    return (sym);
}

const std::string &DynamicLibrary::getFilePath() const
    {
    return _file;
    }

Add RTLD_NODELETE flag when closing dynamic library.
Add RTLD_NODELETE flag when closing dynamic library.

This has the effect to prevent nasty bug with object destructor
not being available (eg logger object created from modules,
or expectiong propagated from module).
