Annotation of src/sys/modules/examples/sysctl/sysctl.c, revision 1.1

1.1     ! kamil       1: /*     $NetBSD$        */
        !             2: 
        !             3: /*-
        !             4:  * Copyright (c) 2018 The NetBSD Foundation, Inc.
        !             5:  * All rights reserved.
        !             6:  *
        !             7:  * Redistribution and use in source and binary forms, with or without
        !             8:  * modification, are permitted provided that the following conditions
        !             9:  * are met:
        !            10:  * 1. Redistributions of source code must retain the above copyright
        !            11:  *    notice, this list of conditions and the following disclaimer.
        !            12:  * 2. Redistributions in binary form must reproduce the above copyright
        !            13:  *    notice, this list of conditions and the following disclaimer in the
        !            14:  *    documentation and/or other materials provided with the distribution.
        !            15:  *
        !            16:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
        !            17:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
        !            18:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
        !            19:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
        !            20:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
        !            21:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
        !            22:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        !            23:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
        !            24:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
        !            25:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
        !            26:  * POSSIBILITY OF SUCH DAMAGE.
        !            27:  */
        !            28: 
        !            29: #include <sys/cdefs.h>
        !            30: __KERNEL_RCSID(0, "$NetBSD$");
        !            31: 
        !            32: #include <sys/param.h>
        !            33: #include <sys/module.h>
        !            34: #include <sys/sysctl.h>
        !            35: 
        !            36: /*
        !            37:  * Check if sysctl -A contains an entry
        !            38:  *    example_subroot1.sysctl_example=0
        !            39:  * to test this module
        !            40:  *
        !            41:  */
        !            42: 
        !            43: MODULE(MODULE_CLASS_MISC, sysctl, NULL);
        !            44: 
        !            45: static int sysctl_example;
        !            46: 
        !            47: static struct sysctllog *example_sysctl_log;
        !            48: 
        !            49: static void sysctl_example_setup(struct sysctllog **);
        !            50: 
        !            51: 
        !            52: /*
        !            53:  * sysctl_example_setup :
        !            54:  *    It first creates a subtree by adding a node to the tree.
        !            55:  *    This node is named as example_subroot1.
        !            56:  *
        !            57:  *    It then creates a node in subtree for the example variable which
        !            58:  *    is an integer and is defined in this file itself.
        !            59:  *
        !            60:  *                      ROOT
        !            61:  *                        |
        !            62:  *                        -------
        !            63:  *                              |
        !            64:  *                       examples_subroot1
        !            65:  *                              |
        !            66:  *                              |
        !            67:  *                        sysctl_example (INT)
        !            68:  *
        !            69:  */
        !            70: 
        !            71: static void
        !            72: sysctl_example_setup(struct sysctllog **clog)
        !            73: {
        !            74:        const struct sysctlnode *rnode;
        !            75: 
        !            76:        sysctl_createv(clog, 0, NULL, &rnode,
        !            77:                        CTLFLAG_PERMANENT,
        !            78:                        CTLTYPE_NODE, "example_subroot1",
        !            79:                        NULL,
        !            80:                        NULL, 0,
        !            81:                        NULL, 0,
        !            82:                        CTL_CREATE, CTL_EOL);
        !            83: 
        !            84:        sysctl_createv(clog, 0, &rnode, NULL,
        !            85:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
        !            86:                       CTLTYPE_INT, "sysctl_example",
        !            87:                       SYSCTL_DESCR("An example for sysctl_example"),
        !            88:                       NULL, 0,
        !            89:                       &sysctl_example, 0,
        !            90:                       CTL_CREATE, CTL_EOL);
        !            91: }
        !            92: 
        !            93: /*
        !            94:  *  The sysctl_example modcmd has two functions.
        !            95:  *  1. Call the sysctl_example_setup function to create a sysctl
        !            96:  *     handle when the module is loaded in the kernel.
        !            97:  *  2. Remove the sysctl entry from the kernel once the module
        !            98:  *     is unloaded.
        !            99:  */
        !           100: 
        !           101: 
        !           102: static int
        !           103: sysctl_modcmd(modcmd_t cmd, void *arg)
        !           104: {
        !           105:        switch(cmd) {
        !           106:        case MODULE_CMD_INIT:
        !           107:                printf("sysctl module inserted");
        !           108:                sysctl_example_setup(&example_sysctl_log);
        !           109:                break;
        !           110:        case MODULE_CMD_FINI:
        !           111:                printf("sysctl module unloaded");
        !           112:                sysctl_teardown(&example_sysctl_log);
        !           113:                break;
        !           114:        default:
        !           115:                return ENOTTY;
        !           116:        }
        !           117:        return 0;
        !           118: }

CVSweb <webmaster@jp.NetBSD.org>