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

1.1     ! misho       1: /*
        !             2:  * input.c            Input API
        !             3:  *
        !             4:  * Copyright (c) 2001-2005 Thomas Graf <tgraf@suug.ch>
        !             5:  *
        !             6:  * Permission is hereby granted, free of charge, to any person obtaining a
        !             7:  * copy of this software and associated documentation files (the "Software"),
        !             8:  * to deal in the Software without restriction, including without limitation
        !             9:  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
        !            10:  * and/or sell copies of the Software, and to permit persons to whom the
        !            11:  * Software is furnished to do so, subject to the following conditions:
        !            12:  *
        !            13:  * The above copyright notice and this permission notice shall be included
        !            14:  * in all copies or substantial portions of the Software.
        !            15:  *
        !            16:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
        !            17:  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        !            18:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
        !            19:  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        !            20:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
        !            21:  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
        !            22:  * DEALINGS IN THE SOFTWARE.
        !            23:  */
        !            24: 
        !            25: #include <bmon/bmon.h>
        !            26: #include <bmon/input.h>
        !            27: #include <bmon/node.h>
        !            28: #include <bmon/utils.h>
        !            29: 
        !            30: static struct input_module *reg_pri_list;
        !            31: static struct input_module *reg_sec_list;
        !            32: static struct input_module *preferred;
        !            33: 
        !            34: const char *
        !            35: get_preferred_input_name(void)
        !            36: {
        !            37:        return preferred ? preferred->im_name : "none";
        !            38: }
        !            39: 
        !            40: static inline void
        !            41: __register_input_module(struct input_module *ops, struct input_module **list)
        !            42: {
        !            43:        ops->im_next = *list;
        !            44:        *list = ops;
        !            45: }
        !            46: 
        !            47: 
        !            48: void
        !            49: register_input_module(struct input_module *ops)
        !            50: {
        !            51:        __register_input_module(ops, &reg_pri_list);
        !            52: }
        !            53: 
        !            54: void
        !            55: register_secondary_input_module(struct input_module *ops)
        !            56: {
        !            57:        __register_input_module(ops, &reg_sec_list);
        !            58: }
        !            59: 
        !            60: static inline struct input_module *
        !            61: __get_input_module(const char *name, struct input_module *list)
        !            62: {
        !            63:        struct input_module *i;
        !            64: 
        !            65:        for (i = list; i; i = i->im_next)
        !            66:                if (!strcmp(i->im_name, name))
        !            67:                        return i;
        !            68: 
        !            69:        return NULL;
        !            70: }
        !            71: 
        !            72: static struct input_module *
        !            73: get_input_module(const char *name)
        !            74: {
        !            75:        return __get_input_module(name, reg_pri_list);
        !            76: }
        !            77: 
        !            78: static struct input_module *
        !            79: get_sec_input_module(const char *name)
        !            80: {
        !            81:        return __get_input_module(name, reg_sec_list);
        !            82: }
        !            83: 
        !            84: #define FOREACH_SIM(F) \
        !            85:        do { \
        !            86:                struct input_module *i; \
        !            87:                for (i = reg_sec_list; i; i = i->im_next) \
        !            88:                        if (i->im_enable && i->im_##F) \
        !            89:                                i->im_##F (); \
        !            90:        } while (0)
        !            91: 
        !            92: static void
        !            93: find_preferred(void)
        !            94: {
        !            95:        if (NULL == preferred) {
        !            96:                struct input_module *i;
        !            97:                /*
        !            98:                 * User has not chosen a output module
        !            99:                 */
        !           100: 
        !           101: #if defined SYS_SUNOS
        !           102:                preferred = get_input_module("kstat");
        !           103: #elif defined SYS_BSD
        !           104:                preferred = get_input_module("sysctl");
        !           105: #elif defined SYS_LINUX
        !           106:                preferred = get_input_module("netlink");
        !           107: 
        !           108:                if (NULL == preferred)
        !           109:                        preferred = get_input_module("proc");
        !           110: 
        !           111:                if (NULL == preferred)
        !           112:                        preferred = get_input_module("sysfs");
        !           113: #endif
        !           114: 
        !           115:                if (NULL == preferred) {
        !           116:                        for (i = reg_pri_list; i; i = i->im_next) {
        !           117:                                if (i->im_no_default) {
        !           118:                                        preferred = i;
        !           119:                                        break;
        !           120:                                }
        !           121:                        }
        !           122:                }
        !           123: 
        !           124:                if (NULL == preferred)
        !           125:                        quit("No input method found\n");
        !           126:        }
        !           127: }
        !           128: 
        !           129: 
        !           130: void
        !           131: input_read(void)
        !           132: {
        !           133:        find_preferred();
        !           134: 
        !           135:        reset_nodes();
        !           136:        preferred->im_read();
        !           137: 
        !           138:        FOREACH_SIM(read);
        !           139: 
        !           140:        remove_unused_node_items();
        !           141: }
        !           142: 
        !           143: static void
        !           144: list_input(void)
        !           145: {
        !           146:        struct input_module *i;
        !           147: 
        !           148:        printf("Input modules:\n");
        !           149:        if (NULL == reg_pri_list)
        !           150:                printf("\tNo input modules found.\n");
        !           151:        else
        !           152:                for (i = reg_pri_list; i; i = i->im_next)
        !           153:                        printf("\t%s\n", i->im_name);
        !           154: }
        !           155: 
        !           156: void
        !           157: set_input(const char *name)
        !           158: {
        !           159:        static int set = 0;
        !           160:        module_conf_t *ml, *m;
        !           161:        
        !           162:        if (set)
        !           163:                return;
        !           164:        set = 1;
        !           165: 
        !           166:        if (NULL == name || !strcasecmp(name, "list")) {
        !           167:                list_input();
        !           168:                exit(0);
        !           169:        }
        !           170:        
        !           171:        ml = parse_module_param(name);
        !           172: 
        !           173:        for (m = ml; m; m = m->next) {
        !           174:                preferred = get_input_module(ml->name);
        !           175: 
        !           176:                if (NULL == preferred)
        !           177:                        continue;
        !           178: 
        !           179:                if (preferred->im_set_opts)
        !           180:                        preferred->im_set_opts(ml->attrs);
        !           181: 
        !           182:                if (preferred->im_probe)
        !           183:                        if (preferred->im_probe())
        !           184:                                return;
        !           185:        }
        !           186: 
        !           187:        quit("No (working) input module found\n");
        !           188: }
        !           189: 
        !           190: static void
        !           191: list_sec_input(void)
        !           192: {
        !           193:        struct input_module *i;
        !           194: 
        !           195:        printf("Secondary input modules:\n");
        !           196:        if (NULL == reg_sec_list)
        !           197:                printf("\tNo secondary input modules found.\n");
        !           198:        else
        !           199:                for (i = reg_sec_list; i; i = i->im_next)
        !           200:                        printf("\t%s\n", i->im_name);
        !           201: }
        !           202: 
        !           203: void
        !           204: set_sec_input(const char *name)
        !           205: {
        !           206:        static int set = 0;
        !           207:        module_conf_t *ml, *m;
        !           208: 
        !           209:        if (set)
        !           210:                return;
        !           211:        set = 1;
        !           212: 
        !           213:        if (NULL == name || !strcasecmp(name, "list")) {
        !           214:                list_sec_input();
        !           215:                exit(0);
        !           216:        }
        !           217:        
        !           218:        ml = parse_module_param(name);
        !           219: 
        !           220:        for (m = ml; m; m = m->next) {
        !           221:                struct input_module *i = get_sec_input_module(m->name);
        !           222: 
        !           223:                if (NULL == i)
        !           224:                        continue;
        !           225: 
        !           226:                if (i->im_set_opts)
        !           227:                        i->im_set_opts(ml->attrs);
        !           228: 
        !           229:                if (i->im_probe) {
        !           230:                        if (i->im_probe() == 1)
        !           231:                                i->im_enable = 1;
        !           232:                }
        !           233:        }
        !           234: }
        !           235: 
        !           236: void
        !           237: input_init(void)
        !           238: {
        !           239:        find_preferred();
        !           240: 
        !           241:        if (preferred->im_init)
        !           242:                preferred->im_init();
        !           243: 
        !           244:        FOREACH_SIM(init);
        !           245: }
        !           246: 
        !           247: void
        !           248: input_shutdown(void)
        !           249: {
        !           250:        find_preferred();
        !           251: 
        !           252:        if (preferred->im_shutdown)
        !           253:                preferred->im_shutdown();
        !           254: 
        !           255:        FOREACH_SIM(shutdown);
        !           256: }

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