2018-02-09 08:35:42 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
2022-07-05 21:09:52 +02:00
|
|
|
#define BOOST_DLL_USE_STD_FS
|
2022-06-14 23:04:51 +02:00
|
|
|
#include <boost/dll.hpp>
|
|
|
|
#include <ircd/mods/mapi.h> // Module's internal API
|
|
|
|
|
|
|
|
namespace load_mode = boost::dll::load_mode;
|
2018-02-09 08:35:42 +01:00
|
|
|
namespace ircd::mods
|
|
|
|
{
|
2019-07-14 01:38:00 +02:00
|
|
|
struct mod;
|
2018-02-09 08:35:42 +01:00
|
|
|
}
|
|
|
|
|
2019-05-09 03:28:16 +02:00
|
|
|
/// Internal module representation. This object closely wraps the dlopen()
|
|
|
|
/// handle and only one instance exists for a DSO when it's loaded. Loading
|
|
|
|
/// and unloading of the module is represented by construction and destruction
|
|
|
|
/// of this class, that involves static initialization and destruction of the
|
|
|
|
/// the module's assets, but not invocation of our user's supplied init. The
|
|
|
|
/// user's init is invoked after construction of this class which allows use of
|
|
|
|
/// the shared_ptr semantics from within the init.
|
|
|
|
///
|
|
|
|
/// It is a critical error if static initialization and destruction is not
|
|
|
|
/// congruent with the lifetime of this instance.
|
|
|
|
///
|
2022-06-29 02:40:56 +02:00
|
|
|
struct [[gnu::visibility("protected")]]
|
2022-06-14 23:04:51 +02:00
|
|
|
ircd::mods::mod
|
2018-02-09 08:35:42 +01:00
|
|
|
:std::enable_shared_from_this<mod>
|
|
|
|
{
|
2018-10-23 19:46:45 +02:00
|
|
|
static std::forward_list<mod *> loading; // State of current dlopen() recursion.
|
2018-10-23 19:59:16 +02:00
|
|
|
static std::forward_list<mod *> unloading; // dlclose() is not recursive but we have this
|
2018-03-25 23:32:24 +02:00
|
|
|
static std::map<string_view, mod *, std::less<>> loaded;
|
2018-02-09 08:35:42 +01:00
|
|
|
|
2019-02-08 05:51:38 +01:00
|
|
|
std::string path;
|
2018-02-09 08:35:42 +01:00
|
|
|
load_mode::type mode;
|
|
|
|
std::deque<mod *> children;
|
2019-02-10 00:51:55 +01:00
|
|
|
std::map<std::string, std::string> exports;
|
2018-02-09 08:35:42 +01:00
|
|
|
boost::dll::shared_library handle;
|
|
|
|
const std::string _name;
|
|
|
|
const std::string _location;
|
|
|
|
mapi::header *header;
|
|
|
|
|
2019-07-14 01:38:00 +02:00
|
|
|
public:
|
2018-02-09 08:35:42 +01:00
|
|
|
// Metadata
|
2018-10-25 22:03:07 +02:00
|
|
|
const string_view &operator[](const string_view &s) const;
|
|
|
|
string_view &operator[](const string_view &s);
|
2018-02-09 08:35:42 +01:00
|
|
|
|
2018-10-25 22:03:07 +02:00
|
|
|
// Convenience accessors
|
2022-07-28 00:10:38 +02:00
|
|
|
auto &name() const { return _name; }
|
|
|
|
auto &location() const { return _location; }
|
|
|
|
auto &description() const { return (*this)["description"]; }
|
|
|
|
auto version() const { return header->version; }
|
2018-02-09 08:35:42 +01:00
|
|
|
|
2019-02-08 05:51:38 +01:00
|
|
|
explicit mod(std::string path, const load_mode::type &);
|
2018-02-09 08:35:42 +01:00
|
|
|
|
2018-03-26 00:25:32 +02:00
|
|
|
mod(mod &&) = delete;
|
|
|
|
mod(const mod &) = delete;
|
|
|
|
mod &operator=(mod &&) = delete;
|
|
|
|
mod &operator=(const mod &) = delete;
|
2018-02-09 08:35:42 +01:00
|
|
|
~mod() noexcept;
|
|
|
|
};
|