11 struct module* module_load(struct core *c, const char *name, const char *argument) {
12 struct module *m = NULL;
17 m = malloc(sizeof(struct module));
20 m->name = strdup(name);
21 m->argument = argument ? strdup(argument) : NULL;
23 if (!(m->dl = lt_dlopenext(name)))
26 if (!(m->init = lt_dlsym(m->dl, "module_init")))
29 if (!(m->done = lt_dlsym(m->dl, "module_done")))
36 if (m->init(c, m) < 0)
40 c->modules = idxset_new(NULL, NULL);
43 r = idxset_put(c->modules, m, &m->index);
44 assert(r >= 0 && m->index != IDXSET_INVALID);
46 fprintf(stderr, "module: loaded %u \"%s\" with argument \"%s\".\n", m->index, m->name, m->argument);
64 static void module_free(struct module *m) {
65 assert(m && m->done && m->core);
70 fprintf(stderr, "module: unloaded %u \"%s\".\n", m->index, m->name);
78 void module_unload(struct core *c, struct module *m) {
82 if (!(m = idxset_remove_by_data(c->modules, m, NULL)))
88 void module_unload_by_index(struct core *c, uint32_t index) {
90 assert(c && index != IDXSET_INVALID);
93 if (!(m = idxset_remove_by_index(c->modules, index)))
99 void free_callback(void *p, void *userdata) {
100 struct module *m = p;
105 void module_unload_all(struct core *c) {
111 idxset_free(c->modules, free_callback, NULL);
115 char *module_list_to_string(struct core *c) {
118 uint32_t index = IDXSET_INVALID;
124 strbuf_printf(s, "%u module(s) loaded.\n", idxset_ncontents(c->modules));
126 for (m = idxset_first(c->modules, &index); m; m = idxset_next(c->modules, &index))
127 strbuf_printf(s, " index: %u, name: <%s>, argument: <%s>\n", m->index, m->name, m->argument);
129 return strbuf_tostring_free(s);
139 void module_unload_once_callback(void *userdata) {
140 struct once_info *i = userdata;
142 module_unload_by_index(i->core, i->index);
146 void module_unload_request(struct core *c, struct module *m) {
150 i = malloc(sizeof(struct once_info));
154 mainloop_once(c->mainloop, module_unload_once_callback, i);