Annotation of embedaddon/bmon/src/module.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:  * module.c               Module API
                      3:  *
                      4:  * Copyright (c) 2001-2013 Thomas Graf <tgraf@suug.ch>
                      5:  * Copyright (c) 2013 Red Hat, Inc.
                      6:  *
                      7:  * Permission is hereby granted, free of charge, to any person obtaining a
                      8:  * copy of this software and associated documentation files (the "Software"),
                      9:  * to deal in the Software without restriction, including without limitation
                     10:  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
                     11:  * and/or sell copies of the Software, and to permit persons to whom the
                     12:  * Software is furnished to do so, subject to the following conditions:
                     13:  *
                     14:  * The above copyright notice and this permission notice shall be included
                     15:  * in all copies or substantial portions of the Software.
                     16:  *
                     17:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
                     18:  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
                     19:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
                     20:  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
                     21:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
                     22:  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
                     23:  * DEALINGS IN THE SOFTWARE.
                     24:  */
                     25: 
                     26: #include <bmon/bmon.h>
                     27: #include <bmon/module.h>
                     28: #include <bmon/utils.h>
                     29: 
                     30: static LIST_HEAD(subsys_list);
                     31: 
                     32: static void module_foreach(struct bmon_subsys *ss, void (*cb)(struct bmon_module *))
                     33: {
                     34:        struct bmon_module *m;
                     35: 
                     36:        list_for_each_entry(m, &ss->s_mod_list, m_list)
                     37:                cb(m);
                     38: }
                     39: 
                     40: void module_foreach_run_enabled_pre(struct bmon_subsys *ss)
                     41: {
                     42:        struct bmon_module *m;
                     43: 
                     44:        list_for_each_entry(m, &ss->s_mod_list, m_list)
                     45:                if (m->m_flags & BMON_MODULE_ENABLED && m->m_pre)
                     46:                        m->m_pre();
                     47: }
                     48: 
                     49: void module_foreach_run_enabled(struct bmon_subsys *ss)
                     50: {
                     51:        struct bmon_module *m;
                     52: 
                     53:        list_for_each_entry(m, &ss->s_mod_list, m_list)
                     54:                if (m->m_flags & BMON_MODULE_ENABLED && m->m_do)
                     55:                        m->m_do();
                     56: }
                     57: 
                     58: void module_foreach_run_enabled_post(struct bmon_subsys *ss)
                     59: {
                     60:        struct bmon_module *m;
                     61: 
                     62:        list_for_each_entry(m, &ss->s_mod_list, m_list)
                     63:                if (m->m_flags & BMON_MODULE_ENABLED && m->m_post)
                     64:                        m->m_post();
                     65: }
                     66: 
                     67: static struct bmon_module *module_lookup(struct bmon_subsys *ss, const char *name)
                     68: {
                     69:        struct bmon_module *m;
                     70: 
                     71:        list_for_each_entry(m, &ss->s_mod_list, m_list)
                     72:                if (!strcmp(m->m_name, name))
                     73:                        return m;
                     74: 
                     75:        return NULL;
                     76: }
                     77: 
                     78: static void module_list(struct bmon_subsys *ss, struct list_head *list)
                     79: {
                     80:        printf("%s modules:\n", ss->s_name);
                     81:        if (list_empty(list))
                     82:                printf("\tNo %s modules found.\n", ss->s_name);
                     83:        else {
                     84:                struct bmon_module *m;
                     85: 
                     86:                list_for_each_entry(m, list, m_list)
                     87:                        printf("\t%s\n", m->m_name);
                     88:        }
                     89: }
                     90: 
                     91: static int module_configure(struct bmon_module *m, module_conf_t *cfg)
                     92: {
                     93:        DBG("Configuring module %s", m->m_name);
                     94: 
                     95:        if (m->m_parse_opt && cfg) {
                     96:                tv_t *tv;
                     97: 
                     98:                list_for_each_entry(tv, &cfg->m_attrs, tv_list)
                     99:                        m->m_parse_opt(tv->tv_type, tv->tv_value);
                    100:        }
                    101: 
                    102:        if (m->m_probe && !m->m_probe())
                    103:                return -EINVAL;
                    104: 
                    105:        m->m_flags |= BMON_MODULE_ENABLED;
                    106:        m->m_subsys->s_nmod++;
                    107: 
                    108:        return 0;
                    109: }
                    110: 
                    111: int module_register(struct bmon_subsys *ss, struct bmon_module *m)
                    112: {
                    113:        if (m->m_subsys)
                    114:                return -EBUSY;
                    115: 
                    116:        list_add_tail(&m->m_list, &ss->s_mod_list);
                    117:        m->m_subsys = ss;
                    118:        return 0;
                    119: }
                    120: 
                    121: static void __auto_load(struct bmon_module *m)
                    122: {
                    123:        if ((m->m_flags & BMON_MODULE_AUTO) &&
                    124:            !(m->m_flags & BMON_MODULE_ENABLED)) {
                    125:                if (module_configure(m, NULL) == 0)
                    126:                        DBG("Auto-enabled module %s", m->m_name);
                    127:        }
                    128: }
                    129: 
                    130: int module_set(struct bmon_subsys *ss, const char *name)
                    131: {
                    132:        struct bmon_module *mod;
                    133:        LIST_HEAD(tmp_list);
                    134:        module_conf_t *m;
                    135: 
                    136:        if (!name || !strcasecmp(name, "list")) {
                    137:                module_list(ss, &ss->s_mod_list);
                    138:                return 1;
                    139:        }
                    140:        
                    141:        parse_module_param(name, &tmp_list);
                    142: 
                    143:        list_for_each_entry(m, &tmp_list, m_list) {
                    144:                if (!(mod = module_lookup(ss, m->m_name)))
                    145:                        quit("Unknown %s module: %s\n", ss->s_name, m->m_name);
                    146: 
                    147:                if (module_configure(mod, m) == 0)
                    148:                        DBG("Enabled module %s", mod->m_name);
                    149:        }
                    150: 
                    151:        module_foreach(ss, __auto_load);
                    152: 
                    153:        if (!ss->s_nmod)
                    154:                quit("No working %s module found\n", ss->s_name);
                    155: 
                    156:        DBG("Configured modules");
                    157: 
                    158:        return 0;
                    159: }
                    160: 
                    161: static void __module_init(struct bmon_module *m)
                    162: {
                    163:        if (m->m_init) {
                    164:                DBG("Initializing %s...", m->m_name);
                    165:                if (m->m_init())
                    166:                        m->m_flags &= ~BMON_MODULE_ENABLED;
                    167:        }
                    168: }
                    169: 
                    170: void module_init(void)
                    171: {
                    172:        struct bmon_subsys *ss;
                    173: 
                    174:        DBG("Initializing modules");
                    175: 
                    176:        list_for_each_entry(ss, &subsys_list, s_list) {
                    177:                if (ss->s_activate_default)
                    178:                        ss->s_activate_default();
                    179: 
                    180:                module_foreach(ss, __module_init);
                    181:        }
                    182: }
                    183: 
                    184: static void __module_shutdown(struct bmon_module *m)
                    185: {
                    186:        if (m->m_shutdown) {
                    187:                DBG("Shutting down %s...", m->m_name);
                    188:                m->m_shutdown();
                    189:                m->m_flags &= ~BMON_MODULE_ENABLED;
                    190:        }
                    191: }
                    192: 
                    193: void module_shutdown(void)
                    194: {
                    195:        DBG("Shutting down modules");
                    196: 
                    197:        struct bmon_subsys *ss;
                    198: 
                    199:        list_for_each_entry(ss, &subsys_list, s_list)
                    200:                module_foreach(ss, __module_shutdown);
                    201: }
                    202: 
                    203: void module_register_subsys(struct bmon_subsys *ss)
                    204: {
                    205:        DBG("Module %s registered", ss->s_name);
                    206: 
                    207:        list_add_tail(&ss->s_list, &subsys_list);
                    208: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>