-
Notifications
You must be signed in to change notification settings - Fork 139
/
plugin.cpp
161 lines (145 loc) · 5.17 KB
/
plugin.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
157
158
159
160
161
/*
* Copyright (C) 2018 Red Hat, Inc.
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "plugin-private.hpp"
#include "../log.hpp"
#include "bgettext/bgettext-lib.h"
#include "tinyformat/tinyformat.hpp"
#include <utils.hpp>
#include <stdexcept>
#include <dirent.h>
#include <dlfcn.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define PLUGIN_API_VERSION 1
namespace libdnf {
Library::Library(const char * path)
: path(path)
{
handle = dlopen(path, RTLD_LAZY);
if (!handle) {
const char * errMsg = dlerror();
throw std::runtime_error(tfm::format(_("Can't load shared library \"%s\": %s"), path, errMsg));
}
}
Library::~Library()
{
dlclose(handle);
}
Plugin::Plugin(const char * path) : Library(path)
{
getInfo = reinterpret_cast<decltype(&pluginGetInfo)>(dlsym(handle, "pluginGetInfo"));
if (!getInfo)
{
const char * errMsg = dlerror();
throw std::runtime_error(tfm::format(_("Can't obtain address of symbol \"%s\": %s"), "pluginGetInfo", errMsg));
}
initHandle = reinterpret_cast<decltype(&pluginInitHandle)>(dlsym(handle, "pluginInitHandle"));
if (!initHandle)
{
const char * errMsg = dlerror();
throw std::runtime_error(tfm::format(_("Can't obtain address of symbol \"%s\": %s"), "pluginInitHandle", errMsg));
}
freeHandle = reinterpret_cast<decltype(&pluginFreeHandle)>(dlsym(handle, "pluginFreeHandle"));
if (!freeHandle)
{
const char * errMsg = dlerror();
throw std::runtime_error(tfm::format(_("Can't obtain address of symbol \"%s\": %s"), "pluginFreeHandle", errMsg));
}
hook = reinterpret_cast<decltype(&pluginHook)>(dlsym(handle, "pluginHook"));
if (!hook)
{
const char * errMsg = dlerror();
throw std::runtime_error(tfm::format(_("Can't obtain address of symbol \"%s\": %s"), "pluginHook", errMsg));
}
}
void Plugins::loadPlugin(const std::string & path)
{
auto logger(Log::getLogger());
logger->debug(tfm::format(_("Loading plugin file=\"%s\""), path));
pluginsWithData.emplace_back(PluginWithData{std::unique_ptr<Plugin>(new Plugin(path.c_str())), true, nullptr});
auto info = pluginsWithData.back().plugin->getInfo();
logger->debug(tfm::format(_("Loaded plugin name=\"%s\", version=\"%s\""), info->name, info->version));
}
void Plugins::loadPlugins(std::string dirPath)
{
auto logger(Log::getLogger());
if (dirPath.empty())
throw std::runtime_error(_("Plugins::loadPlugins() dirPath cannot be empty"));
if (dirPath.back() != '/')
dirPath.push_back('/');
struct dirent **namelist;
auto count = scandir(dirPath.c_str(), &namelist,
[](const struct dirent *dent)->int{return string::endsWith(dent->d_name, ".so");},
alphasort);
if (count == -1) {
int errnum = errno;
logger->warning(tfm::format(_("Can't read plugin directory \"%s\": %s"), dirPath, strerror(errnum)));
return;
}
std::string errorMsgs;
for (int idx = 0; idx < count; ++idx) {
try {
loadPlugin((dirPath + namelist[idx]->d_name).c_str());
} catch (const std::exception & ex) {
std::string msg = tfm::format(_("Can't load plugin \"%s\": %s"), namelist[idx]->d_name, ex.what());
logger->error(msg);
errorMsgs += msg + '\n';
}
::free(namelist[idx]);
}
::free(namelist);
if (!errorMsgs.empty())
throw std::runtime_error(errorMsgs);
}
bool Plugins::init(PluginMode mode, PluginInitData * initData)
{
for (auto & pluginWithData : pluginsWithData) {
if (!pluginWithData.enabled) {
continue;
}
pluginWithData.handle = pluginWithData.plugin->initHandle(PLUGIN_API_VERSION, mode, initData);
if (!pluginWithData.handle) {
return false;
}
}
return true;
}
void Plugins::free()
{
for (auto it = pluginsWithData.rbegin(); it != pluginsWithData.rend(); ++it) {
if (it->handle) {
it->plugin->freeHandle(it->handle);
}
}
}
bool Plugins::hook(PluginHookId id, PluginHookData * hookData, DnfPluginError * error)
{
for (auto & pluginWithData : pluginsWithData) {
if (!pluginWithData.enabled || !pluginWithData.handle) {
continue;
}
if (!pluginWithData.plugin->hook(pluginWithData.handle, id, hookData, error)) {
return false;
}
}
return true;
}
}