-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathxmodule.c
More file actions
179 lines (127 loc) · 2.9 KB
/
xmodule.c
File metadata and controls
179 lines (127 loc) · 2.9 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dlfcn.h>
#include "list.h"
#include "mux_log.h"
#include "xmodule.h"
struct xmodule {
char *name;
char *file;
void *handle;
struct list link;
};
static LIST_HEAD(xmodule_list);
static void init_xmodule(struct xmodule *p_mod)
{
p_mod->name = NULL;
p_mod->file = NULL;
p_mod->handle = NULL;
list_init(&p_mod->link);
}
static void release_xmodule(struct xmodule *p_mod)
{
free(p_mod->name);
free(p_mod->file);
free(p_mod);
}
static struct xmodule *find_xmodule_by_name(const char *name)
{
struct xmodule *p_mod;
list_for_each_entry(p_mod, &xmodule_list, link) {
if (!strcmp(p_mod->name, name))
return p_mod;
}
return NULL;
}
static int load_xmodule(struct xmodule *p_mod)
{
void *h;
xmodule_init_t init_func;
h = dlopen(p_mod->file, RTLD_NOW);
if (h == NULL) {
log_error("load module [%s] failed. reason [%s]\n", p_mod->file,
dlerror());
return -1;
}
init_func = dlsym(h, INIT_XMODULE_FUNC_NAME);
if (init_func == NULL) {
log_error("failed to locate function [%s]. reason [%s]\n",
INIT_XMODULE_FUNC_NAME, dlerror());
dlclose(h);
return -1;
}
if (init_func() < 0) {
log_error("failed to execution init function for mdoule [%s]\n",
p_mod->file);
dlclose(h);
return -1;
}
p_mod->handle = h;
return 0;
}
static int unload_xmodule(struct xmodule *p_mod)
{
xmodule_release_t release_func;
release_func = dlsym(p_mod->handle, RELEASE_XMODULE_FUNC_NAME);
if (release_func == NULL)
return -1;
if (release_func() < 0)
return -1;
dlclose(p_mod->handle);
return 0;
}
int register_xmodule(const char *mod_name, const char *mod_file)
{
struct xmodule *p_mod;
log_debug("register new module [%s] file [%s]\n", mod_name, mod_file);
if (find_xmodule_by_name(mod_name))
return -1;
p_mod = malloc(sizeof(struct xmodule));
if (p_mod == NULL)
return -1;
init_xmodule(p_mod);
p_mod->name = strdup(mod_name);
p_mod->file = strdup(mod_file);
if (load_xmodule(p_mod) < 0) {
release_xmodule(p_mod);
return -1;
}
list_prepend(&p_mod->link, &xmodule_list);
return 0;
}
int unregister_xmodule(const char *mod_name)
{
struct xmodule *p_mod;
p_mod = find_xmodule_by_name(mod_name);
if (p_mod == NULL)
return -1;
if (unload_xmodule(p_mod) < 0)
return -1;
list_remove(&p_mod->link);
release_xmodule(p_mod);
return 0;
}
void unregister_all_xmodule(void)
{
struct xmodule *p_mod;
struct xmodule *p_dummy;
list_for_each_entry_safe(p_mod, p_dummy, &xmodule_list, link) {
unregister_xmodule(p_mod->name);
}
}
static void dump_xmodule_entry(struct xmodule *p_mod)
{
log_info("module: name [%s] file[%s]\n", p_mod->name, p_mod->file);
}
void dump_all_xmodules(void)
{
int count = 0;
struct xmodule *p_mod;
list_for_each_entry(p_mod, &xmodule_list, link) {
dump_xmodule_entry(p_mod);
count++;
}
log_info("total [%d] extension module registerd\n", count);
}