module: Support auto enabling and enforce single registration of module

Enforces a single registration of a module with a subsystem.
module_register() now returns -EBUSY if module is already
registered.

Adds support for BMON_MODULE_AUTO to flag modules that should
be autoloaded upon the first call to module_set(). Loading
will succeed if the probe() function succeeds.
This commit is contained in:
Thomas Graf 2013-07-13 13:05:20 +02:00
parent 2c047d2af3
commit 509d64bd18
2 changed files with 31 additions and 14 deletions

View File

@ -29,8 +29,11 @@
#include <bmon/bmon.h>
#include <bmon/conf.h>
#define BMON_MODULE_ENABLED 1
#define BMON_MODULE_DEFAULT 2
#define BMON_MODULE_ENABLED (1 << 0) /* Enabled */
#define BMON_MODULE_DEFAULT (1 << 1) /* Suitable as default */
#define BMON_MODULE_AUTO (1 << 2) /* Auto enable */
struct bmon_subsys;
struct bmon_module
{
@ -48,6 +51,7 @@ struct bmon_module
int m_flags;
struct list_head m_list;
struct bmon_subsys *m_subsys;
};
struct bmon_subsys
@ -65,7 +69,7 @@ extern void module_foreach_run_enabled_pre(struct bmon_subsys *);
extern void module_foreach_run_enabled(struct bmon_subsys *);
extern void module_foreach_run_enabled_post(struct bmon_subsys *);
extern void module_register(struct bmon_subsys *, struct bmon_module *);
extern int module_register(struct bmon_subsys *, struct bmon_module *);
extern int module_set(struct bmon_subsys *, const char *);
extern void module_init(void);

View File

@ -92,7 +92,7 @@ static int module_configure(struct bmon_module *m, module_conf_t *cfg)
{
DBG("Configuring module %s", m->m_name);
if (m->m_parse_opt) {
if (m->m_parse_opt && cfg) {
tv_t *tv;
list_for_each_entry(tv, &cfg->m_attrs, tv_list)
@ -102,12 +102,28 @@ static int module_configure(struct bmon_module *m, module_conf_t *cfg)
if (m->m_probe && !m->m_probe())
return -EINVAL;
m->m_flags |= BMON_MODULE_ENABLED;
m->m_subsys->s_nmod++;
return 0;
}
void module_register(struct bmon_subsys *ss, struct bmon_module *m)
int module_register(struct bmon_subsys *ss, struct bmon_module *m)
{
if (m->m_subsys)
return -EBUSY;
list_add_tail(&m->m_list, &ss->s_mod_list);
m->m_subsys = ss;
}
static void __auto_load(struct bmon_module *m)
{
if ((m->m_flags & BMON_MODULE_AUTO) &&
!(m->m_flags & BMON_MODULE_ENABLED)) {
if (module_configure(m, NULL) == 0)
DBG("Auto-enabled module %s", m->m_name);
}
}
int module_set(struct bmon_subsys *ss, const char *name)
@ -116,7 +132,6 @@ int module_set(struct bmon_subsys *ss, const char *name)
struct list_head *list;
LIST_HEAD(tmp_list);
module_conf_t *m;
int nmod = 0;
if (!name || !strcasecmp(name, "list")) {
module_list(ss, &ss->s_mod_list);
@ -129,18 +144,16 @@ int module_set(struct bmon_subsys *ss, const char *name)
if (!(mod = module_lookup(ss, m->m_name)))
quit("Unknown %s module: %s\n", ss->s_name, m->m_name);
if (module_configure(mod, m) == 0) {
DBG("Enabling module %s", mod->m_name);
mod->m_flags |= BMON_MODULE_ENABLED;
nmod++;
}
if (module_configure(mod, m) == 0)
DBG("Enabled module %s", mod->m_name);
}
if (!nmod)
module_foreach(ss, __auto_load);
if (!ss->s_nmod)
quit("No working %s module found\n", ss->s_name);
DBG("Configured %d modules", nmod);
ss->s_nmod = nmod;
DBG("Configured modules");
return 0;
}