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