-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.c
117 lines (95 loc) · 1.87 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
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
#include "mcc.h"
#include "module.h"
struct module_list_t s_modules;
void module_load(const char *name)
{
if (module_get_by_name(name) != NULL)
{
LOG("Module %s already registered\n", name);
return;
}
struct module_t *m = malloc(sizeof *m);
strncpy(m->name, name, sizeof m->name);
m->handle = dlopen(name, RTLD_LAZY);
if (m->handle == NULL)
{
LOG("dlopen: %s\n", dlerror());
free(m);
return;
}
m->init_func = dlsym(m->handle, "module_init");
m->deinit_func = dlsym(m->handle, "module_deinit");
if (m->init_func == NULL || m->deinit_func == NULL)
{
LOG("Could not find module_init and/or module_deinit entry point(s)\n");
dlclose(m->handle);
free(m);
}
else
{
module_list_add(&s_modules, m);
module_init(m);
}
}
void module_unload(struct module_t *m)
{
module_deinit(m);
dlclose(m->handle);
free(m);
module_list_del_item(&s_modules, m);
}
struct module_t *module_get_by_name(const char *name)
{
unsigned i;
for (i = 0; i < s_modules.used; i++)
{
struct module_t *m = s_modules.items[i];
if (strcasecmp(m->name, name) == 0) return m;
}
return NULL;
}
void module_init(struct module_t *m)
{
LOG("Initializing module %s\n", m->name);
m->init_func(&m->data);
}
void module_deinit(struct module_t *m)
{
if (m->deinit_func != NULL)
{
m->deinit_func(m->data);
}
}
void modules_init(void)
{
FILE *f = fopen("modules.txt", "r");
if (f == NULL)
{
LOG("No module list to load\n");
return;
}
while (!feof(f))
{
char buf[1024];
if (fgets(buf, sizeof buf, f) > 0)
{
if (buf[0] == '#') continue;
/* Remove newline characters */
char *eol = strchr(buf, '\n');
if (eol != NULL) *eol = '\0';
module_load(buf);
}
}
fclose(f);
}
void modules_deinit(void)
{
while (s_modules.used > 0)
{
module_unload(s_modules.items[0]);
}
module_list_free(&s_modules);
}