-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.c
231 lines (186 loc) · 5.33 KB
/
module.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <dlfcn.h>
#define AEM_INTERNAL
#include <aem/pathutil.h>
#include <aem/translate.h>
#include "module.h"
int aem_module_disable_dereg(struct aem_module *mod)
{
(void)mod;
return 0;
}
struct aem_stringbuf aem_module_path = {0};
struct aem_log_module *aem_module_logmodule = &aem_log_module_default;
void aem_module_path_set(const char *dir)
{
aem_assert(dir);
// If empty, aem_sandbox_path uses current working directory.
aem_stringbuf_reset(&aem_module_path);
aem_stringbuf_puts(&aem_module_path, dir);
aem_logf_ctx(AEM_LOG_DEBUG, "Module path: %s", aem_stringbuf_get(&aem_module_path));
}
void aem_module_init(struct aem_module *mod)
{
aem_assert(mod);
aem_stringbuf_init(&mod->name);
aem_stringbuf_init(&mod->path);
mod->handle = NULL;
mod->def = NULL;
mod->logmodule = aem_module_logmodule;
mod->state = AEM_MODULE_UNREGISTERED;
}
void aem_module_dtor(struct aem_module *mod)
{
aem_assert(mod);
// TODO: And if this returns <0?
aem_module_unload(mod);
aem_stringbuf_dtor(&mod->name);
aem_stringbuf_dtor(&mod->path);
}
int aem_module_resolve_path(struct aem_module *mod)
{
aem_assert(mod);
struct aem_stringslice name = aem_stringslice_new_str(&mod->name);
// Determine path to module file
aem_stringbuf_reset(&mod->path);
if (aem_sandbox_path(&mod->path, aem_stringslice_new_str(&aem_module_path), name, ".so")) {
AEM_LOG_MULTI(out, AEM_LOG_ERROR) {
aem_stringbuf_puts(out, "Invalid module name: ");
aem_string_escape(out, name);
}
return -1;
}
return 0;
}
int aem_module_open(struct aem_module *mod)
{
aem_assert(mod);
if (!mod->path.n) {
aem_logf_ctx(AEM_LOG_BUG, "Module path not set! Set mod->path yourself, or set mod->name and then call aem_module_resolve_path ");
return -1;
}
// Load module
void *handle = dlopen(aem_stringbuf_get(&mod->path), RTLD_NOW | RTLD_GLOBAL);
if (!handle) {
aem_logf_ctx(AEM_LOG_ERROR, "Failed to load module \"%s\": %s", aem_stringbuf_get(&mod->path), dlerror());
return -1;
}
mod->handle = handle;
mod->def = aem_module_get_sym(mod, "aem_module_def");
// Fill in default name
if (mod->def && mod->def->name) {
aem_stringbuf_reset(&mod->name);
aem_stringbuf_puts(&mod->name, mod->def->name);
}
// Make sure module definition is sane
const struct aem_module_def *def = mod->def;
if (!def) {
aem_logf_ctx(AEM_LOG_ERROR, "Couldn't find module definition for \"%s\": %s", aem_stringbuf_get(&mod->path), dlerror());
return -1;
}
if (!def->name) {
aem_logf_ctx(AEM_LOG_ERROR, "Module %s has NULL name!", aem_stringbuf_get(&mod->name));
return -1;
}
if (!def->version) {
aem_logf_ctx(AEM_LOG_WARN, "Module %s has NULL version!", aem_stringbuf_get(&mod->name));
}
if (def->check_reg) {
int rc = def->check_reg(mod);
if (!rc) {
return -1;
}
}
mod->state = AEM_MODULE_LOADED;
return 0;
}
int aem_module_register(struct aem_module *mod, struct aem_stringslice args)
{
aem_assert(mod);
const struct aem_module_def *def = mod->def;
aem_assert(def);
aem_assert(mod->state == AEM_MODULE_LOADED);
// Register module
AEM_LOG_MULTI(out, AEM_LOG_DEBUG) {
aem_stringbuf_puts(out, "Registering module ");
aem_module_identify(out, mod);
}
int rc = 0;
if (def->reg && (rc = def->reg(mod, args))) {
aem_logf_ctx(AEM_LOG_ERROR, "Error %d while registering module \"%s\"", rc, aem_stringbuf_get(&mod->name));
return rc;
}
mod->state = AEM_MODULE_REGISTERED;
AEM_LOG_MULTI(out, AEM_LOG_NOTICE) {
aem_stringbuf_puts(out, "Registered module ");
aem_module_identify(out, mod);
}
return 0;
}
int aem_module_unload_check(struct aem_module *mod)
{
if (!mod)
return -1;
const struct aem_module_def *def = mod->def;
if (!def)
return -1;
if (!def->check_dereg)
return 1;
return def->check_dereg(mod);
}
int aem_module_unload(struct aem_module *mod)
{
if (!mod)
return -1;
if (mod->state == AEM_MODULE_REGISTERED) {
const struct aem_module_def *def = mod->def;
aem_assert(def);
aem_logf_ctx(AEM_LOG_DEBUG, "Deregistering module \"%s\"", aem_stringbuf_get(&mod->name));
if (def->dereg) {
def->dereg(mod);
aem_logf_ctx(AEM_LOG_DEBUG, "Deregistered module \"%s\"", aem_stringbuf_get(&mod->name));
} else {
aem_logf_ctx(AEM_LOG_DEBUG2, "Module \"%s\" didn't need to deregister anything.", aem_stringbuf_get(&mod->name));
}
mod->state = AEM_MODULE_UNREGISTERED;
}
mod->def = NULL;
if (mod->handle) {
aem_logf_ctx(AEM_LOG_NOTICE, "Unloading module \"%s\"", aem_stringbuf_get(&mod->name));
if (dlclose(mod->handle)) {
aem_logf_ctx(AEM_LOG_ERROR, "Failed to dlclose() module \"%s\": %s", aem_stringbuf_get(&mod->name), dlerror());
// TODO: Clear mod->handle?
return -1;
}
}
mod->handle = NULL;
return 0;
}
void *aem_module_get_sym(struct aem_module *mod, const char *symbol)
{
if (!mod->handle)
return NULL;
return dlsym(mod->handle, symbol);
}
void aem_module_identify(struct aem_stringbuf *out, struct aem_module *mod)
{
aem_assert(out);
if (!mod) {
aem_stringbuf_puts(out, "(null module)");
return;
}
aem_stringbuf_putc(out, '"');
aem_stringbuf_append(out, &mod->name);
aem_stringbuf_putc(out, '"');
const struct aem_module_def *def = mod->def;
if (def) {
aem_stringbuf_puts(out, " (");
aem_stringbuf_puts(out, def->name);
if (def->version) {
aem_stringbuf_puts(out, "-");
aem_stringbuf_puts(out, def->version);
}
aem_stringbuf_puts(out, ")");
} else {
aem_stringbuf_puts(out, " (null def)");
}
}