diff options
author | Alfred E. Heggestad <alfred.heggestad@gmail.com> | 2017-07-06 13:23:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-06 13:23:08 +0200 |
commit | eb6b93a8820041ec0bf587eece7c281c40c1da14 (patch) | |
tree | d28827ef49958657f3b53f688de152f9049e8bdc /src | |
parent | 006aa0baca64fed6e85c816ba67cc9c30e2cd6b4 (diff) |
add commands to load/unload module (#275)
* add commands to load/unload module
* fix warning
* module: update doxygen
Diffstat (limited to 'src')
-rw-r--r-- | src/baresip.c | 28 | ||||
-rw-r--r-- | src/module.c | 80 |
2 files changed, 108 insertions, 0 deletions
diff --git a/src/baresip.c b/src/baresip.c index 0705f38..4ea493f 100644 --- a/src/baresip.c +++ b/src/baresip.c @@ -45,8 +45,36 @@ static int cmd_quit(struct re_printf *pf, void *unused) } +static int insmod_handler(struct re_printf *pf, void *arg) +{ + const struct cmd_arg *carg = arg; + int err; + + err = module_load(carg->prm); + if (err) { + return re_hprintf(pf, "insmod: ERROR: could not load module" + " '%s': %m\n", carg->prm, err); + } + + return re_hprintf(pf, "loaded module %s\n", carg->prm); +} + + +static int rmmod_handler(struct re_printf *pf, void *arg) +{ + const struct cmd_arg *carg = arg; + (void)pf; + + module_unload(carg->prm); + + return 0; +} + + static const struct cmd corecmdv[] = { {"quit", 'q', 0, "Quit", cmd_quit }, + {"insmod", 0, CMD_PRM, "Load module", insmod_handler }, + {"rmmod", 0, CMD_PRM, "Unload module", rmmod_handler }, }; diff --git a/src/module.c b/src/module.c index 6c22c3a..68d469e 100644 --- a/src/module.c +++ b/src/module.c @@ -8,6 +8,25 @@ #include "core.h" +/* + * Append module extension, if not exist + * + * input: foobar + * output: foobar.so + * + */ +static void append_extension(char *buf, size_t sz, const char *name) +{ + if (0 == re_regex(name, str_len(name), "[^.]+"MOD_EXT, NULL)) { + + str_ncpy(buf, name, sz); + } + else { + re_snprintf(buf, sz, "%s"MOD_EXT, name); + } +} + + #ifdef STATIC /* Declared in static.c */ @@ -176,3 +195,64 @@ int module_preload(const char *module) return load_module(NULL, &path, &name); } + + +/** + * Load a module by name or by filename + * + * @param name Module name incl/excl extension, excluding module path + * + * @return 0 if success, otherwise errorcode + * + * example: "foo" + * example: "foo.so" + */ +int module_load(const char *name) +{ + char filename[256]; + struct pl path, pl_name; + int err; + + if (!str_isset(name)) + return EINVAL; + + append_extension(filename, sizeof(filename), name); + + pl_set_str(&pl_name, filename); + + if (conf_get(conf_cur(), "module_path", &path)) + pl_set_str(&path, "."); + + err = load_module(NULL, &path, &pl_name); + + return err; +} + + +/** + * Unload a module by name or by filename + * + * @param name module name incl/excl extension, excluding module path + * + * example: "foo" + * example: "foo.so" + */ +void module_unload(const char *name) +{ + char filename[256]; + struct mod *mod; + + if (!str_isset(name)) + return; + + append_extension(filename, sizeof(filename), name); + + mod = mod_find(filename); + if (mod) { + info("unloading module: %s\n", filename); + mem_deref(mod); + return; + } + + info("ERROR: Module %s is not currently loaded\n", name); +} |