1 /*-
2  * Copyright (c) 2000 Andrzej Bialecki <abial@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *     $FreeBSD: src/share/examples/kld/dyn_sysctl/dyn_sysctl.c,v 1.1.2.2 2002/03/27 20:45:11 abial Exp $
27  *     $DragonFly: src/share/examples/kld/dyn_sysctl/dyn_sysctl.c,v 1.2 2003/06/17 04:36:57 dillon Exp $
28  */
29 
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/sysctl.h>
35 #include <sys/kernel.h>
36 
37 
38 /* Some example data */
39 static long a = 100;
40 static int b = 200;
41 static char *c = "hi there from dyn_sysctl";
42 static struct sysctl_oid *a_root, *a_root1, *b_root;
43 static struct sysctl_ctx_list clist, clist1, clist2;
44 
45 static int
46 sysctl_dyn_sysctl_test (SYSCTL_HANDLER_ARGS)
47 {
48 	char *buf = "let's produce some text...";
49 
50 	return (sysctl_handle_string(oidp, buf, strlen(buf), req));
51 }
52 
53 /*
54  * The function called at load/unload.
55  */
56 static int
57 load (module_t mod, int cmd, void *arg)
58 {
59 	int error;
60 
61 	error = 0;
62 	switch (cmd) {
63 	case MOD_LOAD :
64 		/* Initialize the contexts */
65 		kprintf("Initializing contexts and creating subtrees.\n\n");
66 		sysctl_ctx_init(&clist);
67 		sysctl_ctx_init(&clist1);
68 		sysctl_ctx_init(&clist2);
69 		/*
70 		 * Create two partially overlapping subtrees, belonging
71 		 * to different contexts.
72 		 */
73 		kprintf("TREE		ROOT		  NAME\n");
74 		a_root = SYSCTL_ADD_NODE(&clist,
75 			SYSCTL_STATIC_CHILDREN(/* top of sysctl tree */),
76 			OID_AUTO, "dyn_sysctl", CTLFLAG_RW, 0,
77 			"dyn_sysctl root node");
78 		a_root = SYSCTL_ADD_NODE(&clist1,
79 			SYSCTL_STATIC_CHILDREN(/* top of sysctl tree */),
80 			OID_AUTO, "dyn_sysctl", CTLFLAG_RW, 0,
81 			"dyn_sysctl root node");
82 		if(a_root == NULL) {
83 			kprintf("SYSCTL_ADD_NODE failed!\n");
84 			return (EINVAL);
85 		}
86 		SYSCTL_ADD_LONG(&clist, SYSCTL_CHILDREN(a_root),
87 		 OID_AUTO, "long_a", CTLFLAG_RW, &a, "just to try");
88 		SYSCTL_ADD_INT(&clist, SYSCTL_CHILDREN(a_root),
89 		 OID_AUTO, "int_b", CTLFLAG_RW, &b, 0, "just to try 1");
90 		a_root1=SYSCTL_ADD_NODE(&clist, SYSCTL_CHILDREN(a_root),
91 		 OID_AUTO, "nextlevel", CTLFLAG_RD, 0, "one level down");
92 		SYSCTL_ADD_STRING(&clist, SYSCTL_CHILDREN(a_root1),
93 		 OID_AUTO, "string_c", CTLFLAG_RD, c, 0, "just to try 2");
94 		kprintf("1. (%p)	/		  dyn_sysctl\n", &clist);
95 
96 		/* Add a subtree under already existing category */
97 		a_root1 = SYSCTL_ADD_NODE(&clist, SYSCTL_STATIC_CHILDREN(_kern),
98 		 OID_AUTO, "dyn_sysctl", CTLFLAG_RW, 0, "dyn_sysctl root node");
99 		if(a_root1 == NULL) {
100 			kprintf("SYSCTL_ADD_NODE failed!\n");
101 			return (EINVAL);
102 		}
103 		SYSCTL_ADD_PROC(&clist, SYSCTL_CHILDREN(a_root1),
104 		 OID_AUTO, "procedure", CTLFLAG_RD, 0, 0,
105 		 sysctl_dyn_sysctl_test, "A", "I can be here, too");
106 		kprintf("   (%p)	/kern		  dyn_sysctl\n", &clist);
107 
108 		/* Overlap second tree with the first. */
109 		b_root = SYSCTL_ADD_NODE(&clist1, SYSCTL_CHILDREN(a_root),
110 		 OID_AUTO, "nextlevel", CTLFLAG_RD, 0, "one level down");
111 		SYSCTL_ADD_STRING(&clist1, SYSCTL_CHILDREN(b_root),
112 		 OID_AUTO, "string_c1", CTLFLAG_RD, c, 0, "just to try 2");
113 		kprintf("2. (%p)	/		  dyn_sysctl	(overlapping #1)\n", &clist1);
114 
115 		/*
116 		 * And now do something stupid. Connect another subtree to
117 		 * dynamic oid.
118 		 * WARNING: this is an example of WRONG use of dynamic sysctls.
119 		 */
120 		b_root=SYSCTL_ADD_NODE(&clist2, SYSCTL_CHILDREN(a_root1),
121 		 OID_AUTO, "bad", CTLFLAG_RW, 0, "dependent node");
122 		SYSCTL_ADD_STRING(&clist2, SYSCTL_CHILDREN(b_root),
123 		 OID_AUTO, "string_c", CTLFLAG_RD, c, 0, "shouldn't panic");
124 		kprintf("3. (%p)	/kern/dyn_sysctl  bad		(WRONG!)\n", &clist2);
125 		break;
126 	case MOD_UNLOAD :
127 		kprintf("1. Try to free ctx1 (%p): ", &clist);
128 		if(sysctl_ctx_free(&clist))
129 			kprintf("failed: expected. Need to remove ctx3 first.\n");
130 		else
131 			kprintf("HELP! sysctl_ctx_free(%p) succeeded. EXPECT PANIC!!!\n", &clist);
132 		kprintf("2. Try to free ctx3 (%p): ", &clist2);
133 		if(sysctl_ctx_free(&clist2)) {
134 			kprintf("sysctl_ctx_free(%p) failed!\n", &clist2);
135 			/* Remove subtree forcefully... */
136 			sysctl_remove_oid(b_root, 1, 1);
137 			kprintf("sysctl_remove_oid(%p) succeeded\n", b_root);
138 		} else
139 			kprintf("Ok\n");
140 		kprintf("3. Try to free ctx1 (%p) again: ", &clist);
141 		if(sysctl_ctx_free(&clist)) {
142 			kprintf("sysctl_ctx_free(%p) failed!\n", &clist);
143 			/* Remove subtree forcefully... */
144 			sysctl_remove_oid(a_root1, 1, 1);
145 			kprintf("sysctl_remove_oid(%p) succeeded\n", a_root1);
146 		} else
147 			kprintf("Ok\n");
148 		kprintf("4. Try to free ctx2 (%p): ", &clist1);
149 		if(sysctl_ctx_free(&clist1)) {
150 			kprintf("sysctl_ctx_free(%p) failed!\n", &clist1);
151 			/* Remove subtree forcefully... */
152 			sysctl_remove_oid(a_root, 1, 1);
153 		} else
154 			kprintf("Ok\n");
155 		break;
156 	default :
157 		error = EINVAL;
158 		break;
159 	}
160 	return error;
161 }
162 
163 static moduledata_t mod_data= {
164 	"dyn_sysctl",
165 	load,
166 	0
167 };
168 
169 DECLARE_MODULE(dyn_sysctl, mod_data, SI_SUB_EXEC, SI_ORDER_ANY);
170