-
Notifications
You must be signed in to change notification settings - Fork 23
/
pluginloader.cpp
156 lines (123 loc) · 3.75 KB
/
pluginloader.cpp
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
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#include "pluginloader.h"
#include "logger.h"
#include "exceptions.h"
PluginLoader::PluginLoader()
{
}
bool PluginLoader::loaded() const
{
return handle != nullptr;
}
void PluginLoader::loadPlugin(const std::string &pathToSoFile)
{
if (pathToSoFile.empty())
return;
Logger *logger = Logger::getInstance();
logger->logf(LOG_NOTICE, "Loading auth plugin %s", pathToSoFile.c_str());
pluginFamily = PluginFamily::Determining;
if (access(pathToSoFile.c_str(), R_OK) != 0)
{
std::ostringstream oss;
oss << "Error loading auth plugin: The file " << pathToSoFile << " is not there or not readable";
throw FatalError(oss.str());
}
handle = dlopen(pathToSoFile.c_str(), RTLD_LAZY|RTLD_GLOBAL);
if (handle == NULL)
{
const char *err = dlerror();
std::string errmsg = err ? err : "";
throw FatalError(errmsg);
}
if ((version = (F_plugin_version)loadSymbol("flashmq_plugin_version", false)) != nullptr)
{
pluginFamily = PluginFamily::FlashMQ;
flashmqPluginVersionNumber = version();
if (flashmqPluginVersionNumber < 1 || flashmqPluginVersionNumber > 3)
{
throw FatalError("FlashMQ plugin only supports version 1, 2 or 3.");
}
}
else if ((version = (F_plugin_version)loadSymbol("mosquitto_auth_plugin_version", false)) != nullptr)
{
if (version() != 2)
{
throw FatalError("Only Mosquitto plugin version 2 is supported at this time.");
}
pluginFamily = PluginFamily::MosquittoV2;
}
else
{
throw FatalError("This does not seem to be a FlashMQ native plugin or Mosquitto plugin version 2.");
}
if (dlclose(handle) != 0)
{
std::string errmsg(dlerror());
throw FatalError(errmsg);
}
version = nullptr;
handle = dlopen(pathToSoFile.c_str(), RTLD_NOW|RTLD_GLOBAL);
if (handle == NULL)
{
std::string errmsg(dlerror());
throw FatalError(errmsg);
}
main_init_v1 = (F_flashmq_plugin_main_init_v1)loadSymbol("flashmq_plugin_main_init", false);
main_deinit_v1 = (F_flashmq_plugin_main_deinit_v1)loadSymbol("flashmq_plugin_main_deinit", false);
}
void *PluginLoader::loadSymbol(const char *symbol, bool exceptionOnError) const
{
void *r = dlsym(handle, symbol);
if (r == NULL && exceptionOnError)
{
std::string errmsg(dlerror());
throw FatalError(errmsg);
}
return r;
}
PluginFamily PluginLoader::getPluginFamily() const
{
return this->pluginFamily;
}
int PluginLoader::getFlashMQPluginVersion() const
{
return this->flashmqPluginVersionNumber;
}
void PluginLoader::mainInit(std::unordered_map<std::string, std::string> &plugin_opts)
{
if (!main_init_v1)
return;
std::optional<std::string> error;
try
{
main_init_v1(plugin_opts);
}
catch(std::exception &ex)
{
error = ex.what();
Logger *logger = Logger::getInstance();
logger->log(LOG_ERR) << "Exception in flashmq_plugin_main_init(): " << ex.what();
}
if (error)
throw std::runtime_error(error.value());
}
void PluginLoader::mainDeinit(std::unordered_map<std::string, std::string> &plugin_opts)
{
if (!main_deinit_v1)
return;
try
{
main_deinit_v1(plugin_opts);
}
catch(std::exception &ex)
{
Logger *logger = Logger::getInstance();
logger->log(LOG_ERR) << "Exception in flashmq_plugin_main_deinit(): " << ex.what();
}
}