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