forked from mentebinaria/readpe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins.c
More file actions
216 lines (182 loc) · 6.32 KB
/
plugins.c
File metadata and controls
216 lines (182 loc) · 6.32 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
pev - the PE file analyzer toolkit
plugins.c - Implementation for the plugins subsystem.
Copyright (C) 2012 - 2014 pev authors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
In addition, as a special exception, the copyright holders give
permission to link the code of portions of this program with the
OpenSSL library under certain conditions as described in each
individual source file, and distribute linked combinations
including the two.
You must obey the GNU General Public License in all respects
for all of the code used other than OpenSSL. If you modify
file(s) with this exception, you may extend this exception to your
version of the file(s), but you are not obligated to do so. If you
do not wish to do so, delete this exception statement from your
version. If you delete this exception statement from all source
files in the program, then also delete it here.
*/
#include "plugins.h"
#include "plugin.h"
#include "dylib.h"
#include "common.h"
#include "compat/sys/queue.h"
#include "utils.h"
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include "config.h"
#include "pev_api.h"
typedef struct _plugins_entry {
dylib_t library;
plugin_loaded_fn_t plugin_loaded_fn;
plugin_initialize_fn_t plugin_initialize_fn;
plugin_shutdown_fn_t plugin_shutdown_fn;
plugin_unloaded_fn_t plugin_unloaded_fn;
SLIST_ENTRY(_plugins_entry) entries;
} plugins_entry_t;
static SLIST_HEAD(_plugins_t_list, _plugins_entry) g_loaded_plugins = SLIST_HEAD_INITIALIZER(g_loaded_plugins);
int plugins_load(const char *path) {
plugins_entry_t *entry = malloc(sizeof *entry);
if (entry == NULL) {
fprintf(stderr, "plugin: allocation failed for entry\n");
return -1;
}
memset(entry, 0, sizeof *entry);
dylib_t *library = &entry->library;
//fprintf(stdout, "plugins: Loading '%s'... ", path);
int ret = dylib_load(library, path);
//fprintf(stdout, "%s.\n", ret < 0 ? "failed" : "ok");
if (ret < 0) {
free(entry);
return -2;
}
*(void **)(&entry->plugin_loaded_fn) = dylib_get_symbol(library, "plugin_loaded");
*(void **)(&entry->plugin_initialize_fn) = dylib_get_symbol(library, "plugin_initialize");
*(void **)(&entry->plugin_shutdown_fn) = dylib_get_symbol(library, "plugin_shutdown");
*(void **)(&entry->plugin_unloaded_fn) = dylib_get_symbol(library, "plugin_unloaded");
// Only plugin_initialize_fn and plugin_shutdown_fn are required.
if (entry->plugin_initialize_fn == NULL || entry->plugin_shutdown_fn == NULL) {
fprintf(stderr, "plugins: %s is incompatible with this version.\n", path);
dylib_unload(library);
free(entry);
return -3;
}
if (entry->plugin_loaded_fn != NULL) {
const int loaded = entry->plugin_loaded_fn();
if (loaded < 0) {
fprintf(stderr, "plugins: plugin didn't load correctly\n");
dylib_unload(library);
free(entry);
return -4;
}
}
const pev_api_t *pev_api = pev_api_ptr();
const int initialized = entry->plugin_initialize_fn(pev_api);
if (initialized < 0) {
fprintf(stderr, "plugins: plugin didn't initialize correctly\n");
dylib_unload(library);
free(entry);
return -5;
}
SLIST_INSERT_HEAD(&g_loaded_plugins, entry, entries);
return 0;
}
static void plugin_unload_without_removal(plugins_entry_t *entry) {
dylib_t *library = &entry->library;
entry->plugin_shutdown_fn();
if (entry->plugin_unloaded_fn != NULL) {
entry->plugin_unloaded_fn();
}
int ret = dylib_unload(library);
if (ret < 0) {
// TODO(jweyrich): What should we do?
}
}
#if 0
static void plugin_unload(plugins_entry_t *entry) {
plugin_unload_without_removal(entry);
SLIST_REMOVE(&g_loaded_plugins, entry, _plugins_entry, entries);
free(entry);
}
#endif
int plugins_load_all_from_directory(const char *path) {
DIR *dir = opendir(path);
if (dir == NULL) {
fprintf(stderr, "plugins: could not open directory '%s' -- %s\n",
path, strerror(errno));
return -1;
}
long path_max = pathconf(path, _PC_PATH_MAX);
char *relative_path = malloc(path_max);
if (relative_path == NULL) {
fprintf(stderr, "plugins: allocation failed for relative path\n");
closedir(dir);
return -2;
}
size_t load_count = 0;
struct dirent *dir_entry;
// print all the files and directories within directory
// SECURITY: Don't use readdir_r because it will introduce a
// race condition between the opendir and pathconf calls.
// MORE: http://womble.decadent.org.uk/readdir_r-advisory.html
// NOTE: readdir is not thread-safe.
while ((dir_entry = readdir(dir)) != NULL) {
switch (dir_entry->d_type) {
default: // Unhandled
break;
case DT_REG: // Regular file
{
const char *filename = dir_entry->d_name;
// TODO(jweyrich): Use macro conditions for each system: .so, .dylib, .dll
#if defined(__linux__)
const bool possible_plugin = utils_str_ends_with(filename, ".so") != 0;
#elif defined(__APPLE__)
const bool possible_plugin = utils_str_ends_with(filename, ".dylib") != 0;
#elif defined(__CYGWIN__)
const bool possible_plugin = utils_str_ends_with(filename, ".dll") != 0;
#else
#error Not supported
#endif
if (!possible_plugin)
break;
snprintf(relative_path, path_max, "%s/%s", path, filename);
//printf("relative_path = %s\n", relative_path);
int ret = plugins_load(relative_path);
if (ret < 0) {
free(relative_path);
closedir(dir);
return ret;
}
load_count++;
break;
}
case DT_DIR: // Directory
break;
}
}
free(relative_path);
closedir(dir);
return load_count;
}
int plugins_load_all(pev_config_t *config) {
return plugins_load_all_from_directory(config->plugins_path);
}
void plugins_unload_all(void) {
while (!SLIST_EMPTY(&g_loaded_plugins)) {
plugins_entry_t *entry = SLIST_FIRST(&g_loaded_plugins);
plugin_unload_without_removal(entry);
SLIST_REMOVE_HEAD(&g_loaded_plugins, entries);
free(entry);
}
}