-
Notifications
You must be signed in to change notification settings - Fork 1
/
channel.c
85 lines (70 loc) · 1.53 KB
/
channel.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
#include <channel.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <dlfcn.h>
#include <ev.h>
struct module {
struct module *next;
char *name;
void *handle;
struct channel *channel;
};
static struct module *modules;
struct channel *channel_lookup(const char *name)
{
struct module *module;
struct channel *(*setup)(void);
void *handle;
char str[256];
for (module = modules; module; module = module->next)
if (!strcmp(module->name, name))
return module->channel;
snprintf(str, sizeof(str), "%s.so", name);
handle = dlopen(str, RTLD_LAZY);
if (!handle) {
syslog(LOG_WARNING, "unable to load module %s", name);
return NULL;
}
snprintf(str, sizeof(str), "%s_setup", name);
setup = dlsym(handle, str);
if (!setup) {
syslog(LOG_WARNING, "module %s has no setup function", name);
dlclose(handle);
return NULL;
}
module = malloc(sizeof(*module));
if (!module) {
syslog(LOG_WARNING, "%s", strerror(errno));
dlclose(handle);
return NULL;
}
module->next = modules;
module->name = strdup(name);
module->handle = handle;
module->channel = setup();
modules = module;
return module->channel;
}
static void unload_modules(void)
{
struct module *next;
while (modules) {
dlclose(modules->handle);
next = modules->next;
free(modules);
modules = next;
}
}
int main(int argc, char **argv)
{
struct ev_loop *loop = EV_DEFAULT;
int i;
for (i = 1; i < argc; i++)
channel_lookup(argv[i]);
ev_run(loop, 0);
unload_modules();
return EXIT_SUCCESS;
}