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

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:        struct list_head *list;
        !           134:        LIST_HEAD(tmp_list);
        !           135:        module_conf_t *m;
        !           136: 
        !           137:        if (!name || !strcasecmp(name, "list")) {
        !           138:                module_list(ss, &ss->s_mod_list);
        !           139:                return 1;
        !           140:        }
        !           141:        
        !           142:        parse_module_param(name, &tmp_list);
        !           143: 
        !           144:        list_for_each_entry(m, &tmp_list, m_list) {
        !           145:                if (!(mod = module_lookup(ss, m->m_name)))
        !           146:                        quit("Unknown %s module: %s\n", ss->s_name, m->m_name);
        !           147: 
        !           148:                if (module_configure(mod, m) == 0)
        !           149:                        DBG("Enabled module %s", mod->m_name);
        !           150:        }
        !           151: 
        !           152:        module_foreach(ss, __auto_load);
        !           153: 
        !           154:        if (!ss->s_nmod)
        !           155:                quit("No working %s module found\n", ss->s_name);
        !           156: 
        !           157:        DBG("Configured modules");
        !           158: 
        !           159:        return 0;
        !           160: }
        !           161: 
        !           162: static void __module_init(struct bmon_module *m)
        !           163: {
        !           164:        if (m->m_init) {
        !           165:                DBG("Initializing %s...", m->m_name);
        !           166:                if (m->m_init())
        !           167:                        m->m_flags &= ~BMON_MODULE_ENABLED;
        !           168:        }
        !           169: }
        !           170: 
        !           171: void module_init(void)
        !           172: {
        !           173:        struct bmon_subsys *ss;
        !           174: 
        !           175:        DBG("Initializing modules");
        !           176: 
        !           177:        list_for_each_entry(ss, &subsys_list, s_list) {
        !           178:                if (ss->s_activate_default)
        !           179:                        ss->s_activate_default();
        !           180: 
        !           181:                module_foreach(ss, __module_init);
        !           182:        }
        !           183: }
        !           184: 
        !           185: static void __module_shutdown(struct bmon_module *m)
        !           186: {
        !           187:        if (m->m_shutdown) {
        !           188:                DBG("Shutting down %s...", m->m_name);
        !           189:                m->m_shutdown();
        !           190:                m->m_flags &= ~BMON_MODULE_ENABLED;
        !           191:        }
        !           192: }
        !           193: 
        !           194: void module_shutdown(void)
        !           195: {
        !           196:        DBG("Shutting down modules");
        !           197: 
        !           198:        struct bmon_subsys *ss;
        !           199: 
        !           200:        list_for_each_entry(ss, &subsys_list, s_list)
        !           201:                module_foreach(ss, __module_shutdown);
        !           202: }
        !           203: 
        !           204: void module_register_subsys(struct bmon_subsys *ss)
        !           205: {
        !           206:        DBG("Module %s registered", ss->s_name);
        !           207: 
        !           208:        list_add_tail(&ss->s_list, &subsys_list);
        !           209: }

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