-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule.c
359 lines (305 loc) · 7.4 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dlfcn.h>
#include <kernel.h>
#include <kernel-priv.h>
extern list_t modules;
static ssize_t module_desc(const kobject_t * object, char * buffer, size_t length)
{
const module_t * module = (const module_t *)object;
return meta_yamlinfo(module_meta(module), buffer, length);
}
static void module_destroy(kobject_t * object)
{
module_t * module = (module_t *)object;
// Destroy the configs
{
list_t * pos = NULL, * n = NULL;
list_foreach_safe(pos, n, &module->configs)
{
config_t * config = list_entry(pos, config_t, module_list);
list_remove(pos);
kobj_destroy(kobj_cast(config));
}
}
// Destroy all syscalls
{
list_t * pos = NULL, * n = NULL;
list_foreach_safe(pos, n, &module->syscalls)
{
syscall_t * syscall = list_entry(pos, syscall_t, module_list);
list_remove(pos);
kobj_destroy(kobj_cast(syscall));
}
}
// Destroy all blocks
{
list_t * pos = NULL, * n = NULL;
list_foreach_safe(pos, n, &module->blocks)
{
block_t * block = list_entry(pos, block_t, module_list);
list_remove(pos);
kobj_destroy(kobj_cast(block));
}
}
//remove from modules list
list_remove(&module->global_list);
// Call destroyer
{
meta_destroyer destroyer = NULL;
meta_getactivators(module_meta(module), NULL, &destroyer, NULL, NULL);
if (destroyer != NULL)
{
LOGK(LOG_DEBUG, "Calling destroy function for module %s", kobj_objectname(kobj_cast(module)));
destroyer();
}
}
// Destroy the meta backing object
meta_destroy(module->meta);
//do not dlclose module because that address space might still be in use
// TODO - we can check that and unload if not in use!
}
module_t * module_lookup(const char * name)
{
// Sanity check
{
if unlikely(name == NULL)
{
return NULL;
}
}
const char * prefix = path_resolve(name, P_MODULE);
if (prefix == NULL)
{
return NULL;
}
string_t path = path_join(prefix, name);
if (!strsuffix(path.string, ".mo"))
{
string_append(&path, ".mo");
}
list_t * pos;
list_foreach(pos, &modules)
{
module_t * tester = list_entry(pos, module_t, global_list);
const char * testerpath = NULL;
meta_getinfo(module_meta(tester), &testerpath, NULL, NULL, NULL, NULL);
if (testerpath != NULL && strcmp(testerpath, path.string) == 0)
{
return tester;
}
}
return NULL;
}
block_t * module_lookupblock(module_t * module, const char * blockname)
{
// Sanity check
{
if unlikely(module == NULL || blockname == NULL)
{
return NULL;
}
}
list_t * pos;
list_foreach(pos, &module->blocks)
{
block_t * block = list_entry(pos, block_t, module_list);
if (strcmp(blockname, block->name) == 0)
{
return block;
}
}
return NULL;
}
bool module_exists(const char * name)
{
return module_lookup(name) != NULL;
}
module_t * module_load(meta_t * meta, metalookup_f lookup, exception_t ** err)
{
// Sanity check
{
if unlikely(exception_check(err))
{
return NULL;
}
if unlikely(meta == NULL || lookup == NULL)
{
exception_set(err, EINVAL, "Bad arguments!");
return NULL;
}
}
const char * path = NULL;
const char * name = NULL;
const version_t * version = NULL;
meta_getinfo(meta, &path, &name, &version, NULL, NULL);
// Check to see if module has already been loaded
{
const module_t * existing = module_lookup(path);
if (existing != NULL)
{
// Remove const qualifier
return (module_t *)existing;
}
}
// Print diagnostic message
{
string_t version_str = version_tostring(*version);
LOGK(LOG_DEBUG, "Loading module %s v%s", path, version_str.string);
}
// Handle dependencies
// TODO IMPORTANT - handle circular dependencies, somehow...
{
iterator_t ditr = meta_itrdependency(meta);
{
const meta_dependency_t * dependency = NULL;
while (meta_nextdependency(ditr, &dependency))
{
const char * depname = NULL;
meta_getdependency(dependency, &depname);
// Get the dependency meta
meta_t * depmeta = lookup(depname);
if (depmeta == NULL)
{
exception_set(err, ENOENT, "Module dependency resolve failed for %s", depname);
return NULL;
}
module_t * depmodule = module_load(depmeta, lookup, err);
if (depmodule == NULL || exception_check(err))
{
return NULL;
}
meta_destroy(depmeta);
}
}
iterator_free(ditr);
}
// Try loading it
if (!meta_loadmodule(meta, err))
{
return NULL;
}
module_t * module = kobj_new("Module", name, module_desc, module_destroy, sizeof(module_t));
module->meta = meta_copy(meta);
list_init(&module->syscalls);
list_init(&module->configs);
list_init(&module->blocks);
list_init(&module->blockinsts);
// Create configs
{
iterator_t citr = meta_itrconfig(meta);
{
const meta_variable_t * variable = NULL;
while (meta_nextconfig(citr, &variable))
{
config_t * config = config_new(meta, variable, err);
if (config == NULL || exception_check(err))
{
return NULL;
}
kobj_makechild(kobj_cast(module), kobj_cast(config));
list_add(&module->configs, &config->module_list);
}
}
iterator_free(citr);
}
// Create syscalls
{
iterator_t sitr = meta_itrsyscall(meta);
{
const meta_callback_t * callback = NULL;
while(meta_nextsyscall(sitr, &callback))
{
const char * name = NULL, * sig = NULL, * desc = NULL;
syscall_f func = NULL;
meta_getcallback(callback, &name, &sig, &desc, &func);
syscall_t * syscall = syscall_new(name, sig, func, desc, err);
if (syscall == NULL || exception_check(err))
{
return NULL;
}
kobj_makechild(kobj_cast(module), kobj_cast(syscall));
list_add(&module->syscalls, &syscall->module_list);
}
}
iterator_free(sitr);
}
// Create blocks
{
const meta_block_t * block = NULL;
iterator_t bitr = meta_itrblock(meta);
{
while (meta_nextblock(bitr, &block))
{
block_t * blk = block_new(module, block, err);
if (blk == NULL || exception_check(err))
{
return NULL;
}
kobj_makechild(kobj_cast(module), kobj_cast(blk));
list_add(&module->blocks, &blk->module_list);
}
}
iterator_free(bitr);
}
list_add(&modules, &module->global_list);
return module;
}
void module_init(const module_t * module)
{
// Sanity check
{
if unlikely(module == NULL)
{
LOGK(LOG_ERR, "Module is NULL (module_init)");
return;
}
}
// Call initializer function
{
meta_initializer initializer = NULL;
meta_getactivators(module_meta(module), &initializer, NULL, NULL, NULL);
if (initializer != NULL)
{
LOGK(LOG_DEBUG, "Calling initializer function for module %s", kobj_objectname(kobj_cast(module)));
if (!initializer())
{
LOGK(LOG_FATAL, "Module initialization failed (%s).", kobj_objectname(kobj_cast(module)));
// Will exit
}
}
}
}
void module_activate(const module_t * module, moduleact_t act)
{
// Sanity check
{
if unlikely(module == NULL)
{
LOGK(LOG_ERR, "Bad arguments!");
return;
}
}
// Call activator function
{
meta_activator activator = NULL;
const char * activator_name = "(unknown)";
switch (act)
{
case act_preact:
meta_getactivators(module_meta(module), NULL, NULL, &activator, NULL);
activator_name = "preactivator";
break;
case act_postact:
meta_getactivators(module_meta(module), NULL, NULL, NULL, &activator);
activator_name = "postactivator";
break;
}
if (activator != NULL)
{
LOGK(LOG_DEBUG, "Calling %s function for module %s", activator_name, module->kobject.object_name);
activator();
}
}
}