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