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
This commit is contained in:
Andrzej Bialecki 2000-07-15 10:26:04 +00:00
parent 65594d9e65
commit bd3cdc3105
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=63212
9 changed files with 1199 additions and 3 deletions

View file

@ -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>

View file

@ -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>

View file

@ -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$

View file

@ -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);

View file

@ -37,7 +37,8 @@ MAN9+= device.9 device_add_child.9 device_delete_child.9 device_enable.9 \
bus_generic_shutdown.9 \
bus_alloc_resource.9 bus_release_resource.9 \
VOP_ACLCHECK.9 VOP_GETACL.9 VOP_GETEXTATTR.9 VOP_SETACL.9 \
VOP_SETEXTATTR.9 acl.9 extattr.9 kobj.9 taskqueue.9 accept_filter.9
VOP_SETEXTATTR.9 acl.9 extattr.9 kobj.9 taskqueue.9 accept_filter.9 \
sysctl_add_oid.9 sysctl_ctx_init.9
MLINKS+=MD5.9 MD5Init.9 MD5.9 MD5Transform.9
MLINKS+=VOP_ATTRIB.9 VOP_GETATTR.9
@ -122,4 +123,20 @@ MLINKS+=taskqueue.9 TASK_INIT.9
MLINKS+=taskqueue.9 TASKQUEUE_DECLARE.9
MLINKS+=taskqueue.9 TASKQUEUE_DEFINE.9
MLINKS+=sysctl_add_oid.9 sysctl_remove_oid.9
MLINKS+=sysctl_add_oid.9 SYSCTL_ADD_OID.9
MLINKS+=sysctl_add_oid.9 SYSCTL_ADD_NODE.9
MLINKS+=sysctl_add_oid.9 SYSCTL_ADD_STRING.9
MLINKS+=sysctl_add_oid.9 SYSCTL_ADD_INT.9
MLINKS+=sysctl_add_oid.9 SYSCTL_ADD_UINT.9
MLINKS+=sysctl_add_oid.9 SYSCTL_ADD_LONG.9
MLINKS+=sysctl_add_oid.9 SYSCTL_ADD_ULONG.9
MLINKS+=sysctl_add_oid.9 SYSCTL_ADD_OPAQUE.9
MLINKS+=sysctl_add_oid.9 SYSCTL_ADD_STRUCT.9
MLINKS+=sysctl_add_oid.9 SYSCTL_ADD_PROC.9
MLINKS+=sysctl_ctx_init.9 sysctl_ctx_free.9
MLINKS+=sysctl_ctx_init.9 sysctl_ctx_entry_add.9
MLINKS+=sysctl_ctx_init.9 sysctl_ctx_entry_del.9
MLINKS+=sysctl_ctx_init.9 sysctl_ctx_entry_find.9
.include <bsd.prog.mk>

View file

@ -0,0 +1,441 @@
.\"
.\" 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.
.\" 3. The name of the author may not be used to endorse or promote products
.\" derived from this software without specific prior written permission.
.\"
.\" 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$
.\"
.Dd Jul 15, 2000
.Dt sysctl_add_oid 9
.Os FreeBSD 5.0
.Sh NAME
.Nm sysctl_add_oid ,
.Nm sysctl_remove_oid
.Nd runtime sysctl tree manipulation
.Sh SYNOPSIS
.Fd #include <sys/sysctl.h>
.Ft struct sysctl_oid *
.Fo sysctl_add_oid
.Fa "struct sysctl_ctx_list *ctx"
.Fa "struct sysctl_oid_list *parent"
.Fa "int number"
.Fa "char *name"
.Fa "int kind"
.Fa "void *arg1"
.Fa "int arg2"
.Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
.Fa "char *format"
.Fa "char *descr"
.Fc
.Ft int
.Fo sysctl_remove_oid
.Fa "struct sysctl_oid *oidp"
.Fa "int del"
.Fa "int recurse"
.Fc
.Ft struct sysctl_oid_list *
.Fo SYSCTL_CHILDREN
.Fa "struct sysctl_oid *oidp"
.Fc
.Ft struct sysctl_oid_list *
.Fo SYSCTL_STATIC_CHILDREN
.Fa "OID_NAME"
.Fc
.Ft struct sysctl_oid *
.Fo SYSCTL_ADD_OID
.Fa "struct sysctl_ctx_list *ctx"
.Fa "struct sysctl_oid_list *parent"
.Fa "int number"
.Fa "NAME"
.Fa "int kind"
.Fa "void *arg1"
.Fa "int arg2"
.Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
.Fa "char *format"
.Fa "char *descr"
.Fc
.Ft struct sysctl_oid *
.Fo SYSCTL_ADD_NODE
.Fa "struct sysctl_ctx_list *ctx"
.Fa "struct sysctl_oid_list *parent"
.Fa "int number"
.Fa "NAME"
.Fa "int access"
.Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
.Fa "char *descr"
.Fc
.Ft struct sysctl_oid *
.Fo SYSCTL_ADD_STRING
.Fa "struct sysctl_ctx_list *ctx"
.Fa "struct sysctl_oid_list *parent"
.Fa "int number"
.Fa "NAME"
.Fa "int access"
.Fa "char *arg"
.Fa "0"
.Fa "char *descr"
.Fc
.Ft struct sysctl_oid *
.Fo SYSCTL_ADD_INT
.Fa "struct sysctl_ctx_list *ctx"
.Fa "struct sysctl_oid_list *parent"
.Fa "int number"
.Fa "NAME"
.Fa "int access"
.Fa "int *arg"
.Fa "0"
.Fa "char *descr"
.Fc
.Ft struct sysctl_oid *
.Fo SYSCTL_ADD_UINT
.Fa "struct sysctl_ctx_list *ctx"
.Fa "struct sysctl_oid_list *parent"
.Fa "int number"
.Fa "NAME"
.Fa "int access"
.Fa "unsigned int *arg"
.Fa "0"
.Fa "char *descr"
.Fc
.Ft struct sysctl_oid *
.Fo SYSCTL_ADD_LONG
.Fa "struct sysctl_ctx_list *ctx"
.Fa "struct sysctl_oid_list *parent"
.Fa "int number"
.Fa "NAME"
.Fa "int access"
.Fa "long *arg"
.Fa "0"
.Fa "char *descr"
.Fc
.Ft struct sysctl_oid *
.Fo SYSCTL_ADD_ULONG
.Fa "struct sysctl_ctx_list *ctx"
.Fa "struct sysctl_oid_list *parent"
.Fa "int number"
.Fa "NAME"
.Fa "int access"
.Fa "unsigned long *arg"
.Fa "0"
.Fa "char *descr"
.Fc
.Ft struct sysctl_oid *
.Fo SYSCTL_ADD_OPAQUE
.Fa "struct sysctl_ctx_list *ctx"
.Fa "struct sysctl_oid_list *parent"
.Fa "int number"
.Fa "NAME"
.Fa "int access"
.Fa "void *arg"
.Fa "size_t *len"
.Fa "char *descr"
.Fc
.Ft struct sysctl_oid *
.Fo SYSCTL_ADD_STRUCT
.Fa "struct sysctl_ctx_list *ctx"
.Fa "struct sysctl_oid_list *parent"
.Fa "int number"
.Fa "NAME"
.Fa "int access"
.Fa "struct TYPE *arg"
.Fa "TYPE"
.Fa "char *descr"
.Fc
.Ft struct sysctl_oid *
.Fo SYSCTL_ADD_PROC
.Fa "struct sysctl_ctx_list *ctx"
.Fa "struct sysctl_oid_list *parent"
.Fa "int number"
.Fa "NAME"
.Fa "int access"
.Fa "0"
.Fa "0"
.Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
.Fa "char *format"
.Fa "char *descr"
.Fc
.Sh DESCRIPTION
These two functions and set of macros allow to create and delete sysctl
oids during runtime (e.g. during lifetime of a module). Alternative method,
based on linker sets (see
.Aq sys/linker_set.h
and
.Pa sys/kern/kern_sysctl.c
for details), allows only to create and delete them on module load/unload.
.Pp
Dynamic oids of type CTLTYPE_NODE are reusable, so that several
code sections can create and delete them, but in reality they are allocated
and freed based on their reference count. As a consequence, it's possible
for two or more code sections to create partially overlapping trees that
they both can use. It's not possible to create overlapping leaves, or
to create different child type with the same parent and the same name as
one of already existing children.
.Pp
Newly created oids are connected to their parent nodes. In all functions
and macros one of the required parameters is
.Va "struct sysctl_oid_list *parent"
that points to the list of children of the parent node.
.Pp
In case of connecting to already existing static oid (most top level
categories are created statically), this pointer can be obtained by
.Fn SYSCTL_STATIC_CHILDREN "NAME"
macro, where
.Fa "NAME"
is name of the parent oid of type CTLTYPE_NODE (the name displayed by
.Xr sysctl 8
preceded by underscore, and with all dots replaced by underscores).
.Pp
In case of connecting to already existing dynamic oid, this pointer
can be obtained through
.Fn SYSCTL_CHILDREN "struct sysctl_oid *oidp"
macro, where
.Fa "oidp"
points to the parent oid of type CTLTYPE_NODE.
.Fn sysctl_add_oid
is a function to create raw oids of any type. If the oid was successfuly
created, the function returns a pointer to it, otherwise it returns NULL.
Many of the arguments that
.Fn sysctl_add_oid
takes are common also with the macros. The arguments are as follows:
.Bl -tag -width handler
.It ctx
The pointer to (optional) sysctl context, or NULL. See
.Xr sysctl_ctx_init 9
for details. It is strongly advised that programmers use
contexts to organize the dynamic oids they create, unless special
creation and deletion sequences are required. If
.Fa "ctx"
is not NULL, the newly created oid will be added to this context as
its first entry.
.It parent
The pointer to
.Va sysctl_oid_list
structure containing all parent's children.
.It number
The oid number that will be assigned to this oid. In almost all cases this
should be set to
.Va OID_AUTO
which will result in assigning next available oid number.
.It name
Name of the oid. Newly created oid will contain copy of the name.
.It kind
The oid kind, specified as OR of type and access values defined in
.Aq sys/sysctl.h
header file. Oids created dynamically always have CTLTYPE_DYN flag set.
.Pa access
related flags specify whether this oid is read-only or read-write, and
who may modify it (all users or superuser only).
.It arg1
points to any data that the oid should reference, or is set to NULL.
.It arg2
usually contains the information about size of
.Fa "arg1"
, or is set to 0.
.It handler
points to a function that is responsible for handling read and
write requests to this oid. There are several standard handlers that
support operations on nodes, integers, strings and opaque objects.
It's possible also to define new handlers (cf.
.Fn SYSCTL_ADD_PROC
macro).
.It format
Specifies the format of the oid in a symbolic way. This format is
used as a hint by
.Xr sysctl 8
to apply proper data formatting for display purposes. Currently used
format names are:
.Dq N
for node,
.Dq A
for
.Pa "char *"
,
.Dq I
for
.Pa "int"
,
.Dq IU
for
.Pa "unsigned int"
,
.Dq L
for
.Pa "long"
,
.Dq LU
for
.Pa "unsigned long"
and
.Dq S,TYPE
for
.Pa "struct TYPE"
structures.
.It descr
Textual description of the oid.
.El
.Pp
.Fn sysctl_remove_oid
removes dynamically created oid from the tree, optionally freeing
its resources. It takes the following arguments:
.Bl -tag -width recurse
.It oidp
pointer to the dynamic oid to be removed. If the oid is not dynamic,
or NULL, the function returns EINVAL error code.
.It del
If set to non-zero,
.Fn sysctl_remove_oid
will try to free the oid's resources, when the reference count of the oid
becomes zero. However, if
.Va del
is set to 0, the routine will only deregister oid from the tree, without
freeing its resources. This case is useful when the caller wants later
to rollback (possibly partially failed) deletion of many oids.
.It recurse
if set to non-zero, attempt to remove current node and all its children.
If
.Pa recurse
is set to 0, attempt to remove node that contains any children will result
in ENOTEMPTY error.
\fBWARNING: use recursive delete with extreme caution! Normally it
shouldn't be needed if you use contexts.\fR Contexts take care of
tracking inter-dependencies between users of the tree. However, in some
extreme cases it might be needed to remove part of the subtree no matter
how it was created, in order to free some other resources. Be aware,
though, that it may result in system panic if other code section still
was using removed subtree.
.El
.Pp
Again, in most cases programmer should use contexts, as described in
.Xr sysctl_ctx_init 9
to keep track of created oids, and later to delete them in orderly fashion.
.Pp
There is a set of macros defined that helps to create oids of given type.
They are as follows:
.Pp
.Fn SYSCTL_ADD_OID
creates raw oid. This macro is functionally equivalent to
.Fn sysctl_add_oid
function.
.Pp
.Fn SYSCTL_ADD_NODE
creates oid of type CTLFLAG_NODE, to which you can add children oids.
.Pp
.Fn SYSCTL_ADD_STRING
creates oid that handles a zero-terminated string.
.Pp
.Fn SYSCTL_ADD_INT
creates oid that handles
.Va int
variable.
.Pp
.Fn SYSCTL_ADD_UINT
creates oid that handles
.Va unsigned int
variable.
.Pp
.Fn SYSCTL_ADD_LONG
creates oid that handles
.Va long
variable.
.Pp
.Fn SYSCTL_ADD_ULONG
creates oid that handles
.Va unsigned long
variable.
.Pp
.Fn SYSCTL_ADD_OPAQUE
creates oid that handles any chunk of opaque data of specified size.
.Pp
.Fn SYSCTL_ADD_STRUCT
creates oid that handles
.Va "struct TYPE"
structure. The
.Pa format
parameter will be set to
.Dq S,TYPE
to provide proper hints to
.Xr sysctl 8
utlity.
.Pp
.Fn SYSCTL_ADD_PROC
creates oid with specified
.Pa handler
function. The handler is responsible to handle read and write requests
to the oid. This oid type is especially useful if the kernel data is not
easily accessible, or needs to be processed before exporting.
.Pp
.Sh EXAMPLES
The following code example shows how to create new top-level category
and how to hook up another subtree to already existing (static) node:
.Bd -literal
#include <sys/sysctl.h>
...
/* Need to preserve pointers to newly created subtrees, to be able
* to free them later.
*/
struct sysctl_oid *root1, *root2, *oidp;
int a_int;
char *string = "dynamic sysctl";
...
root1 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(/* tree top */),
OID_AUTO, newtree, CTFLAG_RW, 0, "new top level tree");
oidp = SYSCTL_ADD_INT( NULL, SYSCTL_CHILDREN(root1),
OID_AUTO, newint, CTLFLAG_RW, &a_int, 0, "new int leaf");
...
root2 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(_debug),
OID_AUTO, newtree, CTFLAG_RW, 0, "new tree under debug");
oidp = SYSCTL_ADD_STRING( NULL, SYSCTL_CHILDREN(root2),
OID_AUTO, newstring, CTLFLAG_R, string, 0, "new string leaf");
.Ed
.Pp
This example creates the following subtrees:
.Bd -literal -offset indent
debug.newtree.newstring
newtree.newint
.Ed
.Pp
Don't forget to free all oids when you don't need them!
.Pp
.Sh SEE ALSO
.Xr sysctl_ctx_init 9 ,
.Xr sysctl_ctx_free 9 ,
.Xr sysctl 8
.Sh HISTORY
These functions appeared in
.Fx 5.0 .
.Sh AUTHORS
.An Andrzej Bialecki Aq abial@FreeBSD.org
.Sh BUGS
Sharing nodes between many code sections causes interdependencies that
sometimes may lock the resources (e.g. it will be impossible to
delete some oids, if other module hooked up its subtree to oids created
by this module). These issues are handled properly by sysctl contexts.
Many operations on the tree involve traversing linked lists. For this
reason oid creation and removal is relatively costly.

View file

@ -0,0 +1,214 @@
.\"
.\" 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.
.\" 3. The name of the author may not be used to endorse or promote products
.\" derived from this software without specific prior written permission.
.\"
.\" 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$
.\"
.Dd Jul 15, 2000
.Dt sysctl_ctx_init 9
.Os FreeBSD 5.0
.Sh NAME
.Nm sysctl_ctx_init ,
.Nm sysctl_ctx_free ,
.Nm sysctl_ctx_entry_add ,
.Nm sysctl_ctx_entry_find ,
.Nm sysctl_ctx_entry_del ,
.Nd sysctl context for managing dynamically created sysctl oids.
.Sh SYNOPSIS
.Fd #include <sys/sysctl.h>
.Ft int
.Fo sysctl_ctx_init
.Fa "struct sysctl_ctx_list *clist"
.Fc
.Ft int
.Fo sysctl_ctx_free
.Fa "struct sysctl_ctx_list *clist"
.Fc
.Ft struct sysctl_ctx_entry *
.Fo sysctl_ctx_entry_add
.Fa "struct sysctl_ctx_list *clist"
.Fa "struct sysctl_oid *oidp"
.Fc
.Ft struct sysctl_ctx_entry *
.Fo sysctl_ctx_entry_find
.Fa "struct sysctl_ctx_list *clist"
.Fa "struct sysctl_oid *oidp"
.Fc
.Ft int
.Fo sysctl_ctx_entry_del
.Fa "struct sysctl_ctx_list *clist"
.Fa "struct sysctl_oid *oidp"
.Fc
.Sh DESCRIPTION
These functions allow to conveniently manage dynamically created oids.
The sysctl context is responsible for keeping track of created oids,
as well as their proper removal when needed. It adds simple transactional
aspect to oid removal operation, i.e. if the removal operation fails in
the middle, it's possible to rollback the sysctl tree to its previous
state.
.Fn sysctl_ctx_init
initializes context. The
.Va clist
argument must point to already allocated variable. A context MUST be
initialized before use. Once it's initialized, the pointer to the context
can be passed as an argument to all SYSCTL_ADD_* macros, and it will
be updated with entries pointing to newly created oids.
.Pp
Internally the context is represented as TAILQ linked list. The list
consists of
.Pa struct sysctl_ctx_entry
entries:
.Bd -literal -offset indent
struct sysctl_ctx_entry {
struct sysctl_oid *entry;
TAILQ_ENTRY(sysctl_ctx_entry) link;
};
TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry);
.Ed
.Pp
Each context entry points to one dynamic oid that it manages. Newly
created oids are always inserted in the front of the list.
.Pp
.Fn sysctl_ctx_free
removes the context and the oids it manages. If the function completes
successfuly, all managed oids have been unregistered (removed from the
tree) and freed, together with all their allocated memory, and the
entries of the context have been freed as well.
Removal operation is performed in two steps. First, for each context
entry the function
.Fn sysctl_remove_oid
is executed, with parameter
.Va del
set to 0 (don't free resources, only unregister from the tree).
If there are no errors during this step,
.Fn sysctl_remove_oid
proceeds to the next step. If the first step fails, all unregistered oids
kept in the context are registered again.
(NOTE: in most cases programmer specifies OID_AUTO as oid number
when creating an oid. However, during registration of the oid in
the tree, this number is changed to the first available number
greater than 99. If the first step of context deletion fails,
re-registration of the oid doesn't change the already assigned oid
number (different from OID_AUTO), so that we are sure the re-registered
entries are exactly on the same positions in the tree).
The second step actually preforms deletion of dynamic oids.
.Fn sysctl_remove_oid
goes through the context list, starting from beginning (newest entries).
\fBIMPORTANT:\fR this time the function not only deletes the oids
from the tree, but also frees their memory (if oid_refcnt == 0), as
well as all context entries.
.Pp
The
.Fn sysctl_ctx_entry_add
function allows to add existing dynamic oid to context.
.Pp
The
.Fn sysctl_ctx_entry_del
function removes entry from the context. \fBIMPORTANT:\fR in this
case, only the corresponding
.Pa sysctl_ctx_entry
struct is freed, but
.Pa sysctl_oid *oidp
stays intact.
.Pp
The
.Fn sysctl_ctx_entry_find
function scans through the context list looking for given
.Pa oidp
, and either returns found
.Pa sysctl_ctx_entry
, or NULL.
.Pp
.Pp
.Sh EXAMPLES
The following code example shows how to create new top-level category
and how to hook up another subtree to already existing (static) node,
using contexts to keep track of the oids:
.Bd -literal
#include <sys/sysctl.h>
...
struct sysctl_ctx_list clist;
struct sysctl_oid *oidp;
int a_int;
char *string = "dynamic sysctl";
...
sysctl_ctx_init(&clist);
oidp = SYSCTL_ADD_NODE( &clist, SYSCTL_STATIC_CHILDREN(/* tree top */),
OID_AUTO, newtree, CTFLAG_RW, 0, "new top level tree");
oidp = SYSCTL_ADD_INT( &clist, SYSCTL_CHILDREN(oidp),
OID_AUTO, newint, CTLFLAG_RW, &a_int, 0, "new int leaf");
...
oidp = SYSCTL_ADD_NODE( &clist, SYSCTL_STATIC_CHILDREN(_debug),
OID_AUTO, newtree, CTFLAG_RW, 0, "new tree under debug");
oidp = SYSCTL_ADD_STRING( &clist, SYSCTL_CHILDREN(oidp),
OID_AUTO, newstring, CTLFLAG_R, string, 0, "new string leaf");
...
/* Now we can free up the oids */
if(sysctl_ctx_free(&clist)) {
printf("can't free this context - other oids depend on it");
return(ENOTEMPTY);
} else {
printf("Success!\n"):
return(0);
}
.Ed
.Pp
This example creates the following subtrees:
.Bd -literal -offset indent
debug.newtree.newstring
newtree.newint
.Ed
.Pp
Then both trees are removed and their resources freed through one
.Fn sysctl_ctx_free
call, which starts with freeing the newest entried (leaves) and then
proceeds to free the older entries (in this case the nodes).
.Pp
.Sh SEE ALSO
.Xr sysctl_add_oid 9 ,
.Xr sysctl_remove_oid 9 ,
.Xr sysctl 8 ,
.Xr queue 3
.Sh HISTORY
These functions appeared in
.Fx 5.0 .
.Sh AUTHORS
.An Andrzej Bialecki Aq abial@FreeBSD.org
.Sh BUGS
Currently used removal algorithm is somewhat heavy (in the worst
case all oids need to be unregistered, registered again, and then
unregistered and deleted), but guarantees transactional properties
for removal operation.
All operations on the contexts involve traversing linked lists. For this
reason creation and removal of entries is relatively costly.

View file

@ -53,6 +53,7 @@
#include <vm/vm_extern.h>
static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
/*
* Locking and stats
@ -67,6 +68,19 @@ static int sysctl_root(SYSCTL_HANDLER_ARGS);
struct sysctl_oid_list sysctl__children; /* root list */
static struct sysctl_oid *
sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
{
struct sysctl_oid *oidp;
SLIST_FOREACH(oidp, list, oid_link) {
if (strcmp(oidp->oid_name, name) == 0) {
return (oidp);
}
}
return (NULL);
}
/*
* Initialization of the MIB tree.
*
@ -80,6 +94,20 @@ void sysctl_register_oid(struct sysctl_oid *oidp)
struct sysctl_oid *q;
int n;
/*
* First check if another oid with the same name already
* exists in the parent's list.
*/
p = sysctl_find_oidname(oidp->oid_name, parent);
if (p != NULL) {
if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
p->oid_refcnt++;
return;
} else {
printf("can't re-use a leaf (%s)!\n", p->oid_name);
return;
}
}
/*
* If this oid has a number OID_AUTO, give it a number which
* is greater than any current oid. Make sure it is at least
@ -115,6 +143,230 @@ void sysctl_unregister_oid(struct sysctl_oid *oidp)
SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
}
/* Initialize a new context to keep track of dynamically added sysctls. */
int
sysctl_ctx_init(struct sysctl_ctx_list *c)
{
if (c == NULL) {
return (EINVAL);
}
TAILQ_INIT(c);
return (0);
}
/* Free the context, and destroy all dynamic oids registered in this context */
int
sysctl_ctx_free(struct sysctl_ctx_list *clist)
{
struct sysctl_ctx_entry *e, *e1;
int error;
error = 0;
/*
* First perform a "dry run" to check if it's ok to remove oids.
* XXX FIXME
* XXX This algorithm is a hack. But I don't know any
* XXX better solution for now...
*/
TAILQ_FOREACH(e, clist, link) {
error = sysctl_remove_oid(e->entry, 0, 0);
if (error)
break;
}
/*
* Restore deregistered entries, either from the end,
* or from the place where error occured.
* e contains the entry that was not unregistered
*/
if (error)
e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
else
e1 = TAILQ_LAST(clist, sysctl_ctx_list);
while (e1 != NULL) {
sysctl_register_oid(e1->entry);
e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
}
if (error)
return(EBUSY);
/* Now really delete the entries */
e = TAILQ_FIRST(clist);
while (e != NULL) {
e1 = TAILQ_NEXT(e, link);
error = sysctl_remove_oid(e->entry, 1, 0);
if (error)
panic("sysctl_remove_oid: corrupt tree, entry: %s",
e->entry->oid_name);
free(e, M_SYSCTLOID);
e = e1;
}
return (error);
}
/* Add an entry to the context */
struct sysctl_ctx_entry *
sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
{
struct sysctl_ctx_entry *e;
if (clist == NULL || oidp == NULL)
return(NULL);
e = malloc (sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
e->entry = oidp;
TAILQ_INSERT_HEAD(clist, e, link);
return (e);
}
/* Find an entry in the context */
struct sysctl_ctx_entry *
sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
{
struct sysctl_ctx_entry *e;
if (clist == NULL || oidp == NULL)
return(NULL);
for (e = TAILQ_FIRST(clist); e != NULL; e = TAILQ_NEXT(e, link)) {
if(e->entry == oidp)
return(e);
}
return (e);
}
/*
* Delete an entry from the context.
* NOTE: this function doesn't free oidp! You have to remove it
* with sysctl_remove_oid().
*/
int
sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
{
struct sysctl_ctx_entry *e;
if (clist == NULL || oidp == NULL)
return (EINVAL);
e = sysctl_ctx_entry_find(clist, oidp);
if (e != NULL) {
TAILQ_REMOVE(clist, e, link);
free(e, M_SYSCTLOID);
return (0);
} else
return (ENOENT);
}
/*
* Remove dynamically created sysctl trees.
* oidp - top of the tree to be removed
* del - if 0 - just deregister, otherwise free up entries as well
* recurse - if != 0 traverse the subtree to be deleted
*/
int
sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
{
struct sysctl_oid *p;
int error;
if (oidp == NULL)
return(EINVAL);
if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
printf("can't remove non-dynamic nodes!\n");
return (EINVAL);
}
/*
* WARNING: normal method to do this should be through
* sysctl_ctx_free(). Use recursing as the last resort
* method to purge your sysctl tree of leftovers...
* However, if some other code still references these nodes,
* it will panic.
*/
if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
if (oidp->oid_refcnt == 1) {
SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
if (!recurse)
return (ENOTEMPTY);
error = sysctl_remove_oid(p, del, recurse);
if (error)
return (error);
}
if (del)
free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
}
}
if (oidp->oid_refcnt > 1 ) {
oidp->oid_refcnt--;
} else {
if (oidp->oid_refcnt == 0) {
printf("Warning: bad oid_refcnt=%u (%s)!\n",
oidp->oid_refcnt, oidp->oid_name);
return (EINVAL);
}
sysctl_unregister_oid(oidp);
if (del) {
free ((char *)oidp->oid_name, M_SYSCTLOID);
free(oidp, M_SYSCTLOID);
}
}
return (0);
}
/*
* Create new sysctls at run time.
* clist may point to a valid context initialized with sysctl_ctx_init().
*/
struct sysctl_oid *
sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
int number, char *name, int kind, void *arg1, int arg2,
int (*handler)(SYSCTL_HANDLER_ARGS), char *fmt, char *descr)
{
struct sysctl_oid *oidp;
ssize_t len;
/* You have to hook up somewhere.. */
if (parent == NULL)
return(NULL);
/* Check if the node already exists, otherwise create it */
oidp = sysctl_find_oidname(name, parent);
if (oidp != NULL) {
if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
oidp->oid_refcnt++;
/* Update the context */
if (clist != NULL)
sysctl_ctx_entry_add(clist, oidp);
return (oidp);
} else {
printf("can't re-use a leaf (%s)!\n", name);
return (NULL);
}
}
oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK);
bzero(oidp, sizeof(struct sysctl_oid));
oidp->oid_parent = parent;
SLIST_NEXT(oidp, oid_link) = NULL;
oidp->oid_number = number;
oidp->oid_refcnt = 1;
len = strlen(name);
oidp->oid_name = (const char *)malloc(len + 1, M_SYSCTLOID, M_WAITOK);
bcopy(name, (char *)oidp->oid_name, len + 1);
(char)oidp->oid_name[len] = '\0';
oidp->oid_handler = handler;
oidp->oid_kind = CTLFLAG_DYN | kind;
if ((kind & CTLTYPE) == CTLTYPE_NODE) {
/* Allocate space for children */
SYSCTL_CHILDREN(oidp) = malloc(sizeof(struct sysctl_oid_list),
M_SYSCTLOID, M_WAITOK);
SLIST_INIT(SYSCTL_CHILDREN(oidp));
} else {
oidp->oid_arg1 = arg1;
oidp->oid_arg2 = arg2;
}
oidp->oid_fmt = fmt;
/* Update the context, if used */
if (clist != NULL)
sysctl_ctx_entry_add(clist, oidp);
/* Register this oid */
sysctl_register_oid(oidp);
return (oidp);
}
/*
* Bulk-register all the oids in a linker_set.
*/

View file

@ -81,6 +81,7 @@ struct ctlname {
#define CTLFLAG_ANYBODY 0x10000000 /* All users can set this var */
#define CTLFLAG_SECURE 0x08000000 /* Permit set only if securelevel<=0 */
#define CTLFLAG_PRISON 0x04000000 /* Prisoned roots can fiddle */
#define CTLFLAG_DYN 0x02000000 /* Dynamic oid - can be freed */
/*
* USE THIS instead of a hardwired number from the categories below
@ -128,6 +129,7 @@ struct sysctl_oid {
const char *oid_name;
int (*oid_handler)(SYSCTL_HANDLER_ARGS);
const char *oid_fmt;
int oid_refcnt;
};
#define SYSCTL_IN(r, p, l) (r->newfunc)(r, p, l)
@ -145,10 +147,26 @@ int sysctl_handle_opaque(SYSCTL_HANDLER_ARGS);
void sysctl_register_oid(struct sysctl_oid *oidp);
void sysctl_unregister_oid(struct sysctl_oid *oidp);
/* Declare an oid to allow child oids to be added to it. */
/* Declare a static oid to allow child oids to be added to it. */
#define SYSCTL_DECL(name) \
extern struct sysctl_oid_list sysctl_##name##_children
/* Hide these in macros */
#define SYSCTL_CHILDREN(oid_ptr) (struct sysctl_oid_list *) \
(oid_ptr)->oid_arg1
#define SYSCTL_STATIC_CHILDREN(oid_name) \
(&sysctl_##oid_name##_children)
/* === Structs and macros related to context handling === */
/* All dynamically created sysctls can be tracked in a context list. */
struct sysctl_ctx_entry {
struct sysctl_oid *entry;
TAILQ_ENTRY(sysctl_ctx_entry) link;
};
TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry);
/* This constructs a "raw" MIB oid. */
#define SYSCTL_OID(parent, nbr, name, kind, a1, a2, handler, fmt, descr) \
static struct sysctl_oid sysctl__##parent##_##name = { \
@ -156,6 +174,9 @@ void sysctl_unregister_oid(struct sysctl_oid *oidp);
nbr, kind, a1, a2, #name, handler, fmt }; \
DATA_SET(sysctl_set, sysctl__##parent##_##name);
#define SYSCTL_ADD_OID(ctx, parent, nbr, name, kind, a1, a2, handler, fmt, descr) \
sysctl_add_oid(ctx, parent, nbr, #name, kind, a1, a2, handler, fmt, descr);
/* This constructs a node from which other oids can hang. */
#define SYSCTL_NODE(parent, nbr, name, access, handler, descr) \
struct sysctl_oid_list sysctl_##parent##_##name##_children; \
@ -163,46 +184,83 @@ void sysctl_unregister_oid(struct sysctl_oid *oidp);
(void*)&sysctl_##parent##_##name##_children, 0, handler, \
"N", descr);
#define SYSCTL_ADD_NODE(ctx, parent, nbr, name, access, handler, descr) \
sysctl_add_oid(ctx, parent, nbr, #name, CTLTYPE_NODE|access, \
0, 0, handler, "N", descr);
/* Oid for a string. len can be 0 to indicate '\0' termination. */
#define SYSCTL_STRING(parent, nbr, name, access, arg, len, descr) \
SYSCTL_OID(parent, nbr, name, CTLTYPE_STRING|access, \
arg, len, sysctl_handle_string, "A", descr)
#define SYSCTL_ADD_STRING(ctx, parent, nbr, name, access, arg, len, descr) \
sysctl_add_oid(ctx, parent, nbr, #name, CTLTYPE_STRING|access, \
arg, len, sysctl_handle_string, "A", descr);
/* Oid for an int. If ptr is NULL, val is returned. */
#define SYSCTL_INT(parent, nbr, name, access, ptr, val, descr) \
SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|access, \
ptr, val, sysctl_handle_int, "I", descr)
#define SYSCTL_ADD_INT(ctx, parent, nbr, name, access, ptr, val, descr) \
sysctl_add_oid(ctx, parent, nbr, #name, CTLTYPE_INT|access, \
ptr, val, sysctl_handle_int, "I", descr);
/* Oid for an unsigned int. If ptr is NULL, val is returned. */
#define SYSCTL_UINT(parent, nbr, name, access, ptr, val, descr) \
SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|access, \
ptr, val, sysctl_handle_int, "IU", descr)
#define SYSCTL_ADD_UINT(ctx, parent, nbr, name, access, ptr, val, descr) \
sysctl_add_oid(ctx, parent, nbr, #name, CTLTYPE_INT|access, \
ptr, val, sysctl_handle_int, "IU", descr);
/* Oid for a long. The pointer must be non NULL. */
#define SYSCTL_LONG(parent, nbr, name, access, ptr, val, descr) \
SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|access, \
ptr, val, sysctl_handle_long, "L", descr)
#define SYSCTL_ADD_LONG(ctx, parent, nbr, name, access, ptr, descr) \
sysctl_add_oid(ctx, parent, nbr, #name, CTLTYPE_INT|access, \
ptr, 0, sysctl_handle_long, "L", descr);
/* Oid for a long. The pointer must be non NULL. */
#define SYSCTL_ULONG(parent, nbr, name, access, ptr, val, descr) \
SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|access, \
ptr, val, sysctl_handle_long, "LU", descr)
#define SYSCTL_ADD_ULONG(ctx, parent, nbr, name, access, ptr, descr) \
sysctl_add_oid(ctx, parent, nbr, #name, CTLTYPE_INT|access, \
ptr, 0, sysctl_handle_long, "LU", descr);
/* Oid for an opaque object. Specified by a pointer and a length. */
#define SYSCTL_OPAQUE(parent, nbr, name, access, ptr, len, fmt, descr) \
SYSCTL_OID(parent, nbr, name, CTLTYPE_OPAQUE|access, \
ptr, len, sysctl_handle_opaque, fmt, descr)
#define SYSCTL_ADD_OPAQUE(ctx, parent, nbr, name, access, ptr, len, fmt, descr)\
sysctl_add_oid(ctx, parent, nbr, #name, CTLTYPE_OPAQUE|access, \
ptr, len, sysctl_handle_opaque, fmt, descr);
/* Oid for a struct. Specified by a pointer and a type. */
#define SYSCTL_STRUCT(parent, nbr, name, access, ptr, type, descr) \
SYSCTL_OID(parent, nbr, name, CTLTYPE_OPAQUE|access, \
ptr, sizeof(struct type), sysctl_handle_opaque, \
"S," #type, descr)
#define SYSCTL_ADD_STRUCT(ctx, parent, nbr, name, access, ptr, type, descr) \
sysctl_add_oid(ctx, parent, nbr, #name, CTLTYPE_OPAQUE|access, \
ptr, sizeof(struct type), sysctl_handle_opaque, "S," #type, descr);
/* Oid for a procedure. Specified by a pointer and an arg. */
#define SYSCTL_PROC(parent, nbr, name, access, ptr, arg, handler, fmt, descr) \
SYSCTL_OID(parent, nbr, name, access, \
ptr, arg, handler, fmt, descr)
#define SYSCTL_ADD_PROC(ctx, parent, nbr, name, access, ptr, arg, handler, fmt, descr) \
sysctl_add_oid(ctx, parent, nbr, #name, access, \
ptr, arg, handler, fmt, descr);
#endif /* _KERNEL */
/*
@ -501,8 +559,26 @@ extern char ostype[];
struct linker_set;
/* Dynamic oid handling */
struct sysctl_oid *sysctl_add_oid(struct sysctl_ctx_list *clist,
struct sysctl_oid_list *parent, int nbr, char *name,
int kind, void *arg1, int arg2,
int (*handler) (SYSCTL_HANDLER_ARGS),
char *fmt, char *descr);
int sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse);
int sysctl_ctx_init(struct sysctl_ctx_list *clist);
int sysctl_ctx_free(struct sysctl_ctx_list *clist);
struct sysctl_ctx_entry *sysctl_ctx_entry_add(struct sysctl_ctx_list *clist,
struct sysctl_oid *oidp);
struct sysctl_ctx_entry *sysctl_ctx_entry_find(struct sysctl_ctx_list *clist,
struct sysctl_oid *oidp);
int sysctl_ctx_entry_del(struct sysctl_ctx_list *clist,
struct sysctl_oid *oidp);
/* Linker set based oid handling */
void sysctl_register_set(struct linker_set *lsp);
void sysctl_unregister_set(struct linker_set *lsp);
int kernel_sysctl(struct proc *p, int *name, u_int namelen, void *old,
size_t *oldlenp, void *new, size_t newlen,
size_t *retval);