Ejemplo n.º 1
0
static void engine_unload(struct engine *engine, struct kr_module *module)
{
	/* Unregister module */
	auto_free char *name = strdup(module->name);
	kr_module_unload(module);
	/* Clear in Lua world */
	if (name) {
		lua_pushnil(engine->L);
		lua_setglobal(engine->L, name);
	}
	free(module);
}
Ejemplo n.º 2
0
int kr_module_load(struct kr_module *module, const char *name, const char *path)
{
    if (module == NULL || name == NULL) {
        return kr_error(EINVAL);
    }

    /* Initialize, keep userdata */
    void *data = module->data;
    memset(module, 0, sizeof(struct kr_module));
    module->data = data;
    module->name = strdup(name);
    if (module->name == NULL) {
        return kr_error(ENOMEM);
    }

    /* Search for module library, use current namespace if not found. */
    if (load_library(module, name, path) != 0) {
        /* Expand HOME env variable, as the linker may not expand it. */
        auto_free char *local_path = kr_strcatdup(2, getenv("HOME"), "/.local/lib/kdns_modules");
        if (load_library(module, name, local_path) != 0) {
            if (load_library(module, name, MODULEDIR) != 0) {
                module->lib = RTLD_DEFAULT;
            }
        }
    }

    /* Try to load module ABI. */
    int ret = load_sym_c(module, KR_MODULE_API);
    if (ret == 0 && module->init) {
        ret = module->init(module);
    }
    if (ret != 0) {
        kr_module_unload(module);
    }

    return ret;
}