2024-09-14 12:18:26 +08:00
|
|
|
#include "module_manager_interna.h"
|
|
|
|
|
|
|
|
|
|
#include "stellar/module_manager.h"
|
|
|
|
|
#include "stellar/utils.h"
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
#include <stdbool.h>
|
2024-09-19 16:51:03 +08:00
|
|
|
#include <assert.h>
|
2024-09-25 17:44:27 +08:00
|
|
|
#include <string.h>
|
2024-09-14 12:18:26 +08:00
|
|
|
|
|
|
|
|
|
2024-09-14 16:00:24 +08:00
|
|
|
/*******************************************
|
|
|
|
|
* module manager internal API *
|
|
|
|
|
*******************************************/
|
|
|
|
|
|
|
|
|
|
toml_table_t *toml_parse_file_path(const char *toml_conf_path)
|
2024-09-14 12:18:26 +08:00
|
|
|
{
|
2024-09-14 16:00:24 +08:00
|
|
|
if(toml_conf_path==NULL)return NULL;
|
2024-09-14 12:18:26 +08:00
|
|
|
FILE* fp = fopen(toml_conf_path, "r");
|
|
|
|
|
if(fp==NULL)return NULL;
|
|
|
|
|
char errbuf[256];
|
|
|
|
|
toml_table_t* conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
|
|
|
|
|
fclose(fp);
|
2024-09-14 16:00:24 +08:00
|
|
|
if(conf==NULL) fprintf(stderr, "Error parsing toml: %s\n", errbuf);
|
|
|
|
|
return conf;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 17:44:27 +08:00
|
|
|
int module_specs_load(toml_table_t* conf, struct module_spec_load **mod_spec)
|
2024-09-14 16:00:24 +08:00
|
|
|
{
|
2024-09-25 17:44:27 +08:00
|
|
|
if(conf==NULL|| mod_spec==NULL) return 0;
|
|
|
|
|
int mod_num = 0;
|
2024-09-14 12:18:26 +08:00
|
|
|
toml_array_t* mod_array = toml_array_in(conf, "module");
|
|
|
|
|
if(mod_array==NULL)goto MODULE_SPEC_LOAD_ERROR;
|
2024-09-25 17:44:27 +08:00
|
|
|
mod_num = toml_array_nelem(mod_array);
|
|
|
|
|
*mod_spec = CALLOC(struct module_spec_load, mod_num);
|
2024-09-14 12:18:26 +08:00
|
|
|
|
2024-09-25 17:44:27 +08:00
|
|
|
for (int i = 0; i < mod_num; i++) {
|
2024-09-14 12:18:26 +08:00
|
|
|
toml_table_t* toml_mod = toml_table_at(mod_array, i);
|
|
|
|
|
|
|
|
|
|
const char *path_raw = toml_raw_in(toml_mod, "path");
|
|
|
|
|
const char *init_func_name_raw = toml_raw_in(toml_mod, "init");
|
|
|
|
|
const char *exit_func_name_raw = toml_raw_in(toml_mod, "exit");
|
|
|
|
|
char *path = NULL;
|
|
|
|
|
char *init_func_name = NULL;
|
|
|
|
|
char *exit_func_name = NULL;
|
|
|
|
|
if (toml_rtos(path_raw, &path) || toml_rtos(init_func_name_raw, &init_func_name) ||
|
|
|
|
|
toml_rtos(exit_func_name_raw, &exit_func_name))
|
|
|
|
|
{
|
|
|
|
|
goto MODULE_SPEC_LOAD_ERROR;
|
|
|
|
|
}
|
|
|
|
|
void* handle = dlopen(path, RTLD_NOW|RTLD_LAZY|RTLD_GLOBAL);
|
|
|
|
|
if (!handle) {
|
2024-09-19 15:58:39 +08:00
|
|
|
fprintf(stderr, "Error loading module %s: %s\n", path, dlerror());
|
2024-09-14 12:18:26 +08:00
|
|
|
goto MODULE_SPEC_LOAD_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 17:44:27 +08:00
|
|
|
mod_spec[i]->on_init_cb = (module_on_init_func *) dlsym(handle, init_func_name);
|
|
|
|
|
if (!mod_spec[i]->on_init_cb) {
|
2024-09-14 12:18:26 +08:00
|
|
|
fprintf(stderr, "Could not load init function %s: %s\n", init_func_name, dlerror());
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 17:44:27 +08:00
|
|
|
mod_spec[i]->on_exit_cb = (module_on_exit_func *) dlsym(handle, exit_func_name);
|
|
|
|
|
if (!mod_spec[i]->on_exit_cb) {
|
2024-09-14 12:18:26 +08:00
|
|
|
fprintf(stderr, "Could not load exit function %s: %s\n", exit_func_name, dlerror());
|
|
|
|
|
}
|
2024-09-25 17:44:27 +08:00
|
|
|
mod_spec[i]->path=path;
|
|
|
|
|
mod_spec[i]->init_cb_name=init_func_name;
|
|
|
|
|
mod_spec[i]->exit_cb_name=exit_func_name;
|
2024-09-14 12:18:26 +08:00
|
|
|
}
|
2024-09-25 17:44:27 +08:00
|
|
|
return mod_num;
|
2024-09-14 12:18:26 +08:00
|
|
|
MODULE_SPEC_LOAD_ERROR:
|
2024-09-25 17:44:27 +08:00
|
|
|
if(*mod_spec)FREE(*mod_spec);
|
|
|
|
|
return 0;
|
2024-09-14 12:18:26 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-14 16:00:24 +08:00
|
|
|
struct stellar_module_manager *stellar_module_manager_new_with_toml(toml_table_t *conf, int max_thread_num, struct mq_schema *mq_schema)
|
2024-09-14 12:18:26 +08:00
|
|
|
{
|
2024-09-25 17:44:27 +08:00
|
|
|
|
2024-09-14 12:18:26 +08:00
|
|
|
struct stellar_module_manager *mod_mgr = CALLOC(struct stellar_module_manager, 1);
|
|
|
|
|
mod_mgr->schema.max_thread_num=max_thread_num;
|
|
|
|
|
mod_mgr->schema.mq_schema=mq_schema;
|
2024-09-25 17:44:27 +08:00
|
|
|
mod_mgr->schema.load_module_num = module_specs_load(conf, &mod_mgr->schema.module_specs);
|
2024-09-14 12:18:26 +08:00
|
|
|
|
2024-09-19 16:51:03 +08:00
|
|
|
// TODO: store module specific in hash
|
2024-09-25 17:44:27 +08:00
|
|
|
for(int i = 0; i < mod_mgr->schema.load_module_num; i++)
|
2024-09-14 12:18:26 +08:00
|
|
|
{
|
2024-09-25 17:44:27 +08:00
|
|
|
if (mod_mgr->schema.module_specs[i].on_init_cb != NULL)
|
2024-09-14 12:18:26 +08:00
|
|
|
{
|
2024-09-25 17:44:27 +08:00
|
|
|
mod_mgr->schema.module_specs[i].mod=mod_mgr->schema.module_specs[i].on_init_cb(mod_mgr);
|
|
|
|
|
if(stellar_module_manager_get_module(mod_mgr, mod_mgr->schema.module_specs[i].mod->name)==NULL)
|
2024-09-19 16:51:03 +08:00
|
|
|
{
|
2024-09-25 17:44:27 +08:00
|
|
|
mod_mgr->schema.module_specs[i].is_init_succ=true;
|
2024-09-19 16:51:03 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-09-25 17:44:27 +08:00
|
|
|
fprintf(stderr, "Module %s already exists\n", mod_mgr->schema.module_specs[i].mod->name);
|
|
|
|
|
if(mod_mgr->schema.module_specs[i].on_exit_cb)mod_mgr->schema.module_specs[i].on_exit_cb(mod_mgr, mod_mgr->schema.module_specs[i].mod);
|
|
|
|
|
assert(0);
|
2024-09-19 16:51:03 +08:00
|
|
|
}
|
2024-09-14 12:18:26 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mod_mgr;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-14 16:00:24 +08:00
|
|
|
/*******************************************
|
|
|
|
|
* stellar module manager API *
|
|
|
|
|
*******************************************/
|
|
|
|
|
|
|
|
|
|
struct stellar_module_manager *stellar_module_manager_new(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema)
|
|
|
|
|
{
|
|
|
|
|
toml_table_t *conf = toml_parse_file_path(module_spec_toml_path);
|
|
|
|
|
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_toml(conf, max_thread_num, mq_schema);
|
|
|
|
|
if(conf)toml_free(conf);
|
2024-09-19 15:58:39 +08:00
|
|
|
if(module_spec_toml_path)mod_mgr->module_spec_toml_path=strdup(module_spec_toml_path);
|
2024-09-14 16:00:24 +08:00
|
|
|
return mod_mgr;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-14 12:18:26 +08:00
|
|
|
void stellar_module_manager_free(struct stellar_module_manager *mod_mgr)
|
|
|
|
|
{
|
|
|
|
|
if(mod_mgr==NULL)return;
|
2024-09-19 15:58:39 +08:00
|
|
|
if(mod_mgr->module_spec_toml_path)FREE(mod_mgr->module_spec_toml_path);
|
2024-09-25 17:44:27 +08:00
|
|
|
if (mod_mgr->schema.module_specs)
|
2024-09-14 12:18:26 +08:00
|
|
|
{
|
2024-09-25 17:44:27 +08:00
|
|
|
for (int i = 0; i < mod_mgr->schema.load_module_num; i++)
|
2024-09-14 12:18:26 +08:00
|
|
|
{
|
2024-09-25 17:44:27 +08:00
|
|
|
if (mod_mgr->schema.module_specs[i].on_exit_cb != NULL &&
|
|
|
|
|
mod_mgr->schema.module_specs[i].is_init_succ)
|
|
|
|
|
{
|
|
|
|
|
mod_mgr->schema.module_specs[i].on_exit_cb(mod_mgr, mod_mgr->schema.module_specs[i].mod);
|
|
|
|
|
}
|
2024-09-25 17:44:27 +08:00
|
|
|
if(mod_mgr->schema.module_specs[i].path)FREE(mod_mgr->schema.module_specs[i].path);
|
|
|
|
|
if(mod_mgr->schema.module_specs[i].init_cb_name)FREE(mod_mgr->schema.module_specs[i].init_cb_name);
|
|
|
|
|
if(mod_mgr->schema.module_specs[i].exit_cb_name)FREE(mod_mgr->schema.module_specs[i].exit_cb_name);
|
2024-09-14 12:18:26 +08:00
|
|
|
}
|
2024-09-25 17:44:27 +08:00
|
|
|
FREE(mod_mgr->schema.module_specs);
|
2024-09-14 12:18:26 +08:00
|
|
|
}
|
2024-09-25 17:44:27 +08:00
|
|
|
FREE(mod_mgr);
|
2024-09-14 12:18:26 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int stellar_module_manager_get_max_thread_num(struct stellar_module_manager*mod_mgr)
|
|
|
|
|
{
|
|
|
|
|
if(mod_mgr==NULL)return -1;
|
|
|
|
|
return mod_mgr->schema.max_thread_num;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 16:56:26 +08:00
|
|
|
struct mq_schema *stellar_module_manager_get_mq_schema(struct stellar_module_manager *mod_mgr)
|
2024-09-14 12:18:26 +08:00
|
|
|
{
|
|
|
|
|
if(mod_mgr==NULL)return NULL;
|
|
|
|
|
return mod_mgr->schema.mq_schema;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 15:58:39 +08:00
|
|
|
const char *stellar_module_manager_get_toml_path(struct stellar_module_manager *mod_mgr)
|
|
|
|
|
{
|
|
|
|
|
if(mod_mgr==NULL)return NULL;
|
|
|
|
|
return mod_mgr->module_spec_toml_path;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-14 12:18:26 +08:00
|
|
|
__thread int local_thread_id=-1;
|
|
|
|
|
__thread struct mq_runtime *local_mq_rt=NULL;
|
|
|
|
|
|
|
|
|
|
int stellar_module_manager_get_thread_id(struct stellar_module_manager* mod_mgr __unused)
|
|
|
|
|
{
|
|
|
|
|
return local_thread_id;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 16:56:26 +08:00
|
|
|
struct mq_runtime *stellar_module_manager_get_mq_runtime(struct stellar_module_manager *mod_mgr __unused)
|
2024-09-14 12:18:26 +08:00
|
|
|
{
|
|
|
|
|
return local_mq_rt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stellar_module_manager_register_thread(struct stellar_module_manager* mod_mgr __unused, int thread_id, struct mq_runtime *mq_rt)
|
|
|
|
|
{
|
|
|
|
|
local_thread_id=thread_id;
|
|
|
|
|
local_mq_rt=mq_rt;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct stellar_module *stellar_module_manager_get_module(struct stellar_module_manager *mod_mgr, const char *module_name)
|
|
|
|
|
{
|
2024-09-14 16:00:24 +08:00
|
|
|
if(mod_mgr==NULL || module_name == NULL)return NULL;
|
2024-09-25 17:44:27 +08:00
|
|
|
if (mod_mgr->schema.module_specs)
|
2024-09-14 12:18:26 +08:00
|
|
|
{
|
2024-09-25 17:44:27 +08:00
|
|
|
for(int i=0; i<mod_mgr->schema.load_module_num; i++)
|
2024-09-14 12:18:26 +08:00
|
|
|
{
|
2024-09-25 17:44:27 +08:00
|
|
|
if(mod_mgr->schema.module_specs[i].mod == NULL)break;
|
|
|
|
|
if(strcmp(mod_mgr->schema.module_specs[i].mod->name, module_name)==0 && mod_mgr->schema.module_specs[i].is_init_succ)
|
|
|
|
|
{
|
|
|
|
|
return mod_mgr->schema.module_specs[i].mod;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-14 12:18:26 +08:00
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************************************
|
|
|
|
|
* stellar module API *
|
|
|
|
|
*******************************************/
|
|
|
|
|
|
|
|
|
|
|
2024-09-19 15:58:39 +08:00
|
|
|
struct stellar_module *stellar_module_new(const char *name, void *ctx)
|
2024-09-14 12:18:26 +08:00
|
|
|
{
|
|
|
|
|
struct stellar_module *mod = CALLOC(struct stellar_module, 1);
|
2024-09-14 17:41:07 +08:00
|
|
|
memcpy(mod->name, name, MIN(NAME_MAX, strlen(name)));
|
2024-09-19 15:58:39 +08:00
|
|
|
mod->module_ctx=ctx;
|
2024-09-14 12:18:26 +08:00
|
|
|
return mod;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stellar_module_free(struct stellar_module *mod)
|
|
|
|
|
{
|
|
|
|
|
if(mod==NULL)return;
|
|
|
|
|
FREE(mod);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void * stellar_module_get_ctx(struct stellar_module *mod)
|
|
|
|
|
{
|
|
|
|
|
if(mod==NULL)return NULL;
|
|
|
|
|
return mod->module_ctx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stellar_module_set_ctx(struct stellar_module *mod, void *ctx)
|
|
|
|
|
{
|
|
|
|
|
if(mod==NULL)return;
|
|
|
|
|
mod->module_ctx=ctx;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *stellar_module_get_name(struct stellar_module* mod)
|
|
|
|
|
{
|
|
|
|
|
if(mod==NULL)return NULL;
|
|
|
|
|
return mod->name;
|
|
|
|
|
}
|
2024-09-14 16:00:24 +08:00
|
|
|
|
|
|
|
|
void stellar_module_set_name(struct stellar_module* mod, const char *name)
|
|
|
|
|
{
|
|
|
|
|
if(mod==NULL)return;
|
2024-09-14 17:41:07 +08:00
|
|
|
memcpy(mod->name, name, MIN(NAME_MAX, strlen(name)));
|
2024-09-14 16:00:24 +08:00
|
|
|
return;
|
2024-09-25 17:44:27 +08:00
|
|
|
}
|
|
|
|
|
|