summaryrefslogtreecommitdiffstats
path: root/share/examples/kld
diff options
context:
space:
mode:
authorabial <abial@FreeBSD.org>2000-07-15 10:26:04 +0000
committerabial <abial@FreeBSD.org>2000-07-15 10:26:04 +0000
commitc7bf2569fa53882ac3f6a6d420425de0c7f8a490 (patch)
treef296f4156d4f32d397b59fa2297d1fc077e7b275 /share/examples/kld
parentba6bef6211a46c4c72c4df1b86bfb8b591437184 (diff)
downloadFreeBSD-src-c7bf2569fa53882ac3f6a6d420425de0c7f8a490.zip
FreeBSD-src-c7bf2569fa53882ac3f6a6d420425de0c7f8a490.tar.gz
These patches implement dynamic sysctls. It's possible now to add
and remove sysctl oids at will during runtime - they don't rely on linker sets. Also, the node oids can be referenced by more than one kernel user, which means that it's possible to create partially overlapping trees. Add sysctl contexts to help programmers manage multiple dynamic oids in convenient way. Please see the manpages for detailed discussion, and example module for typical use. This work is based on ideas and code snippets coming from many people, among them: Arun Sharma, Jonathan Lemon, Doug Rabson, Brian Feldman, Kelly Yancey, Poul-Henning Kamp and others. I'd like to specially thank Brian Feldman for detailed review and style fixes. PR: kern/16928 Reviewed by: dfr, green, phk
Diffstat (limited to 'share/examples/kld')
-rw-r--r--share/examples/kld/Makefile4
-rw-r--r--share/examples/kld/dyn_sysctl/Makefile18
-rw-r--r--share/examples/kld/dyn_sysctl/README8
-rw-r--r--share/examples/kld/dyn_sysctl/dyn_sysctl.c168
4 files changed, 197 insertions, 1 deletions
diff --git a/share/examples/kld/Makefile b/share/examples/kld/Makefile
index 68504bd..6c539bd 100644
--- a/share/examples/kld/Makefile
+++ b/share/examples/kld/Makefile
@@ -64,7 +64,9 @@
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
+# $FreeBSD$
+#
-SUBDIR= cdev syscall
+SUBDIR= cdev syscall dyn_sysctl
.include <bsd.subdir.mk>
diff --git a/share/examples/kld/dyn_sysctl/Makefile b/share/examples/kld/dyn_sysctl/Makefile
new file mode 100644
index 0000000..3c63a8a
--- /dev/null
+++ b/share/examples/kld/dyn_sysctl/Makefile
@@ -0,0 +1,18 @@
+# $FreeBSD$
+
+SRCS = dyn_sysctl.c
+CFLAGS = -I/sys
+KMOD = dyn_sysctl
+KO = ${KMOD}.ko
+KLDMOD = t
+
+KLDLOAD = /sbin/kldload
+KLDUNLOAD = /sbin/kldunload
+
+load: ${KO}
+ ${KLDLOAD} -v ./${KO}
+
+unload: ${KO}
+ ${KLDUNLOAD} -v -n ${KO}
+
+.include <bsd.kmod.mk>
diff --git a/share/examples/kld/dyn_sysctl/README b/share/examples/kld/dyn_sysctl/README
new file mode 100644
index 0000000..4dfa3c6
--- /dev/null
+++ b/share/examples/kld/dyn_sysctl/README
@@ -0,0 +1,8 @@
+This example module creates partially overlapping subtrees to demonstrate
+reference counting. It also contains example of attaching a subtree to the
+wrong place, i.e. to a dynamic oid that could belong to someone else.
+The framework should deal with this case gracefully.
+
+Andrzej Bialecki <abial@freebsd.org>
+
+$FreeBSD$
diff --git a/share/examples/kld/dyn_sysctl/dyn_sysctl.c b/share/examples/kld/dyn_sysctl/dyn_sysctl.c
new file mode 100644
index 0000000..e066b56
--- /dev/null
+++ b/share/examples/kld/dyn_sysctl/dyn_sysctl.c
@@ -0,0 +1,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);
OpenPOWER on IntegriCloud