dyn_sysctl.c revision 134520
157694Sjmas/*-
257694Sjmas * Copyright (c) 2000 Andrzej Bialecki <abial@freebsd.org>
357694Sjmas * All rights reserved.
457694Sjmas *
557694Sjmas * Redistribution and use in source and binary forms, with or without
657694Sjmas * modification, are permitted provided that the following conditions
757694Sjmas * are met:
857694Sjmas * 1. Redistributions of source code must retain the above copyright
957694Sjmas *    notice, this list of conditions and the following disclaimer.
1057694Sjmas * 2. Redistributions in binary form must reproduce the above copyright
1157694Sjmas *    notice, this list of conditions and the following disclaimer in the
1257694Sjmas *    documentation and/or other materials provided with the distribution.
1357694Sjmas *
1457694Sjmas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1557694Sjmas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1657694Sjmas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1757694Sjmas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1857694Sjmas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1957694Sjmas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2057694Sjmas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2157694Sjmas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2257694Sjmas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2357694Sjmas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2457694Sjmas * SUCH DAMAGE.
2557694Sjmas *
2657694Sjmas *     $FreeBSD: head/share/examples/kld/dyn_sysctl/dyn_sysctl.c 134520 2004-08-30 09:49:59Z pjd $
2757694Sjmas */
2857694Sjmas
2957694Sjmas#include <sys/types.h>
3057694Sjmas#include <sys/param.h>
3157694Sjmas#include <sys/systm.h>
3257694Sjmas#include <sys/module.h>
3357694Sjmas#include <sys/sysctl.h>
3457694Sjmas#include <sys/kernel.h>
3557694Sjmas
3657694Sjmas
3757694Sjmas/* Some example data */
3857694Sjmasstatic long a = 100;
3957694Sjmasstatic int b = 200;
4057694Sjmasstatic char *c = "hi there from dyn_sysctl";
4157694Sjmasstatic struct sysctl_oid *a_root, *a_root1, *b_root;
4257694Sjmasstatic struct sysctl_ctx_list clist, clist1, clist2;
4357694Sjmas
4457694Sjmasstatic int
4557694Sjmassysctl_dyn_sysctl_test (SYSCTL_HANDLER_ARGS)
4657694Sjmas{
4757694Sjmas	char *buf = "let's produce some text...";
4857694Sjmas
4957694Sjmas	return (sysctl_handle_string(oidp, buf, strlen(buf), req));
5057694Sjmas}
51
52/*
53 * The function called at load/unload.
54 */
55static int
56load (module_t mod, int cmd, void *arg)
57{
58	int error;
59
60	error = 0;
61	switch (cmd) {
62	case MOD_LOAD :
63		/* Initialize the contexts */
64		printf("Initializing contexts and creating subtrees.\n\n");
65		sysctl_ctx_init(&clist);
66		sysctl_ctx_init(&clist1);
67		sysctl_ctx_init(&clist2);
68		/*
69		 * Create two partially overlapping subtrees, belonging
70		 * to different contexts.
71		 */
72		printf("TREE		ROOT		  NAME\n");
73		a_root = SYSCTL_ADD_NODE(&clist,
74			SYSCTL_STATIC_CHILDREN(/* top of sysctl tree */),
75			OID_AUTO, "dyn_sysctl", CTLFLAG_RW, 0,
76			"dyn_sysctl root node");
77		a_root = SYSCTL_ADD_NODE(&clist1,
78			SYSCTL_STATIC_CHILDREN(/* top of sysctl tree */),
79			OID_AUTO, "dyn_sysctl", CTLFLAG_RW, 0,
80			"dyn_sysctl root node");
81		if(a_root == NULL) {
82			printf("SYSCTL_ADD_NODE failed!\n");
83			return (EINVAL);
84		}
85		SYSCTL_ADD_LONG(&clist, SYSCTL_CHILDREN(a_root),
86		 OID_AUTO, "long_a", CTLFLAG_RW, &a, "just to try");
87		SYSCTL_ADD_INT(&clist, SYSCTL_CHILDREN(a_root),
88		 OID_AUTO, "int_b", CTLFLAG_RW, &b, 0, "just to try 1");
89		a_root1=SYSCTL_ADD_NODE(&clist, SYSCTL_CHILDREN(a_root),
90		 OID_AUTO, "nextlevel", CTLFLAG_RD, 0, "one level down");
91		SYSCTL_ADD_STRING(&clist, SYSCTL_CHILDREN(a_root1),
92		 OID_AUTO, "string_c", CTLFLAG_RD, c, 0, "just to try 2");
93		printf("1. (%p)	/		  dyn_sysctl\n", &clist);
94
95		/* Add a subtree under already existing category */
96		a_root1 = SYSCTL_ADD_NODE(&clist, SYSCTL_STATIC_CHILDREN(_kern),
97		 OID_AUTO, "dyn_sysctl", CTLFLAG_RW, 0, "dyn_sysctl root node");
98		if(a_root1 == NULL) {
99			printf("SYSCTL_ADD_NODE failed!\n");
100			return (EINVAL);
101		}
102		SYSCTL_ADD_PROC(&clist, SYSCTL_CHILDREN(a_root1),
103		 OID_AUTO, "procedure", CTLFLAG_RD, 0, 0,
104		 sysctl_dyn_sysctl_test, "A", "I can be here, too");
105		printf("   (%p)	/kern		  dyn_sysctl\n", &clist);
106
107		/* Overlap second tree with the first. */
108		b_root = SYSCTL_ADD_NODE(&clist1, SYSCTL_CHILDREN(a_root),
109		 OID_AUTO, "nextlevel", CTLFLAG_RD, 0, "one level down");
110		SYSCTL_ADD_STRING(&clist1, SYSCTL_CHILDREN(b_root),
111		 OID_AUTO, "string_c1", CTLFLAG_RD, c, 0, "just to try 2");
112		printf("2. (%p)	/		  dyn_sysctl	(overlapping #1)\n", &clist1);
113
114		/*
115		 * And now do something stupid. Connect another subtree to
116		 * dynamic oid.
117		 * WARNING: this is an example of WRONG use of dynamic sysctls.
118		 */
119		b_root=SYSCTL_ADD_NODE(&clist2, SYSCTL_CHILDREN(a_root1),
120		 OID_AUTO, "bad", CTLFLAG_RW, 0, "dependent node");
121		SYSCTL_ADD_STRING(&clist2, SYSCTL_CHILDREN(b_root),
122		 OID_AUTO, "string_c", CTLFLAG_RD, c, 0, "shouldn't panic");
123		printf("3. (%p)	/kern/dyn_sysctl  bad		(WRONG!)\n", &clist2);
124		break;
125	case MOD_UNLOAD :
126		printf("1. Try to free ctx1 (%p): ", &clist);
127		if(sysctl_ctx_free(&clist))
128			printf("failed: expected. Need to remove ctx3 first.\n");
129		else
130			printf("HELP! sysctl_ctx_free(%p) succeeded. EXPECT PANIC!!!\n", &clist);
131		printf("2. Try to free ctx3 (%p): ", &clist2);
132		if(sysctl_ctx_free(&clist2)) {
133			printf("sysctl_ctx_free(%p) failed!\n", &clist2);
134			/* Remove subtree forcefully... */
135			sysctl_remove_oid(b_root, 1, 1);
136			printf("sysctl_remove_oid(%p) succeeded\n", b_root);
137		} else
138			printf("Ok\n");
139		printf("3. Try to free ctx1 (%p) again: ", &clist);
140		if(sysctl_ctx_free(&clist)) {
141			printf("sysctl_ctx_free(%p) failed!\n", &clist);
142			/* Remove subtree forcefully... */
143			sysctl_remove_oid(a_root1, 1, 1);
144			printf("sysctl_remove_oid(%p) succeeded\n", a_root1);
145		} else
146			printf("Ok\n");
147		printf("4. Try to free ctx2 (%p): ", &clist1);
148		if(sysctl_ctx_free(&clist1)) {
149			printf("sysctl_ctx_free(%p) failed!\n", &clist1);
150			/* Remove subtree forcefully... */
151			sysctl_remove_oid(a_root, 1, 1);
152		} else
153			printf("Ok\n");
154		break;
155	default :
156		error = EOPNOTSUPP;
157		break;
158	}
159	return error;
160}
161
162static moduledata_t mod_data= {
163	"dyn_sysctl",
164	load,
165	0
166};
167
168DECLARE_MODULE(dyn_sysctl, mod_data, SI_SUB_EXEC, SI_ORDER_ANY);
169