Files
@ a13c26fb9289
Branch filter:
Location: libtransport.git/include/boost/dll/detail/posix/shared_library_impl.hpp
a13c26fb9289
6.0 KiB
text/x-c++hdr
spectrum2_manager: use get_http_var to get post/get variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
// Copyright 2015 Antony Polukhin.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
#define BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
#include <boost/config.hpp>
#include <boost/dll/shared_library_load_mode.hpp>
#include <boost/dll/detail/posix/path_from_handle.hpp>
#include <boost/dll/detail/posix/program_location_impl.hpp>
#include <boost/move/utility.hpp>
#include <boost/swap.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/predef/os.h>
#include <dlfcn.h>
#include <cstring> // strncmp
#if !BOOST_OS_MACOS && !BOOST_OS_IOS && !BOOST_OS_QNX
# include <link.h>
#elif BOOST_OS_QNX
// QNX's copy of <elf.h> and <link.h> reside in sys folder
# include <sys/link.h>
#endif
#ifdef BOOST_HAS_PRAGMA_ONCE
# pragma once
#endif
namespace boost { namespace dll { namespace detail {
class shared_library_impl {
BOOST_MOVABLE_BUT_NOT_COPYABLE(shared_library_impl)
public:
typedef void* native_handle_t;
shared_library_impl() BOOST_NOEXCEPT
: handle_(NULL)
{}
~shared_library_impl() BOOST_NOEXCEPT {
unload();
}
shared_library_impl(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT
: handle_(sl.handle_)
{
sl.handle_ = NULL;
}
shared_library_impl & operator=(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT {
handle_ = sl.handle_;
sl.handle_ = NULL;
return *this;
}
void load(const boost::filesystem::path &sl, load_mode::type mode, boost::system::error_code &ec) {
typedef int native_mode_t;
unload();
// Do not allow opening NULL paths. User must use program_location() instead
if (sl.empty()) {
ec = boost::system::error_code(
boost::system::errc::bad_file_descriptor,
boost::system::generic_category()
);
return;
}
// Fixing modes
if (!(mode & load_mode::rtld_now)) {
mode |= load_mode::rtld_lazy;
}
if (!(mode & load_mode::rtld_global)) {
mode |= load_mode::rtld_local;
}
// Trying to open with appended decorations
if (!!(mode & load_mode::append_decorations)) {
mode = static_cast<load_mode::type>(
static_cast<native_mode_t>(mode) & (~static_cast<native_mode_t>(load_mode::append_decorations))
);
boost::filesystem::path actual_path = (
std::strncmp(sl.filename().string().c_str(), "lib", 3)
? (sl.parent_path() / "lib").native() + sl.filename().native()
: sl
);
actual_path += suffix();
handle_ = dlopen(actual_path.c_str(), static_cast<native_mode_t>(mode));
if (handle_) {
return;
}
}
// Opening by exactly specified path
handle_ = dlopen(sl.c_str(), static_cast<native_mode_t>(mode));
if (handle_) {
return;
}
ec = boost::system::error_code(
boost::system::errc::bad_file_descriptor,
boost::system::generic_category()
);
// Maybe user wanted to load the executable itself? Checking...
// We assume that usually user wants to load a dynamic library not the executable itself, that's why
// we try this only after traditional load fails.
boost::system::error_code prog_loc_err;
boost::filesystem::path loc = boost::dll::detail::program_location_impl(prog_loc_err);
if (!prog_loc_err && boost::filesystem::equivalent(sl, loc, prog_loc_err) && !prog_loc_err) {
// As is known the function dlopen() loads the dynamic library file
// named by the null-terminated string filename and returns an opaque
// "handle" for the dynamic library. If filename is NULL, then the
// returned handle is for the main program.
ec.clear();
handle_ = dlopen(NULL, static_cast<native_mode_t>(mode));
if (!handle_) {
ec = boost::system::error_code(
boost::system::errc::bad_file_descriptor,
boost::system::generic_category()
);
}
}
}
bool is_loaded() const BOOST_NOEXCEPT {
return (handle_ != 0);
}
void unload() BOOST_NOEXCEPT {
if (!is_loaded()) {
return;
}
dlclose(handle_);
handle_ = 0;
}
void swap(shared_library_impl& rhs) BOOST_NOEXCEPT {
boost::swap(handle_, rhs.handle_);
}
boost::filesystem::path full_module_path(boost::system::error_code &ec) const {
return boost::dll::detail::path_from_handle(handle_, ec);
}
static boost::filesystem::path suffix() {
// https://sourceforge.net/p/predef/wiki/OperatingSystems/
#if BOOST_OS_MACOS || BOOST_OS_IOS
return ".dylib";
#else
return ".so";
#endif
}
void* symbol_addr(const char* sb, boost::system::error_code &ec) const BOOST_NOEXCEPT {
// dlsym - obtain the address of a symbol from a dlopen object
void* const symbol = dlsym(handle_, sb);
if (symbol == NULL) {
ec = boost::system::error_code(
boost::system::errc::invalid_seek,
boost::system::generic_category()
);
}
// If handle does not refer to a valid object opened by dlopen(),
// or if the named symbol cannot be found within any of the objects
// associated with handle, dlsym() shall return NULL.
// More detailed diagnostic information shall be available through dlerror().
return symbol;
}
native_handle_t native() const BOOST_NOEXCEPT {
return handle_;
}
private:
native_handle_t handle_;
};
}}} // boost::dll::detail
#endif // BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
|