DESCRIPTION

These functions and macros provide an interface for creating and deleting
sysctl oids at runtime (e.g. during lifetime of a module). The
alternative method, based on linker sets (see #include<sys/linker_set.h>
and src/sys/kern/kern_sysctl.c for details), only allows creation and
deletion on module load and unload respectively.
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 is
possible for two or more code sections to create partially overlapping
trees that they both can use. It is not possible to create overlapping
leaves, nor to create different child types with the same name and
parent.
Newly created oids are connected to their parent nodes. In all these
functions and macros (with the exception of sysctl_remove_oid()), one of
the required parameters is parent, which points to the head of the
parent’s list of children.
Most top level categories are created statically. When connecting to
existing static oids, this pointer can be obtained with the
SYSCTL_STATIC_CHILDREN() macro, where the OID_NAME argument is name of
the parent oid of type CTLTYPE_NODE (i.e., the name displayed by
sysctl(8), preceded by underscore, and with all dots replaced with
underscores).
When connecting to an existing dynamic oid, this pointer can be obtained
with the SYSCTL_CHILDREN() macro, where the oidp argument points to the
parent oid of type CTLTYPE_NODE.
The sysctl_add_oid() function creates raw oids of any type. If the oid
is successfully created, the function returns a pointer to it; otherwise
it returns NULL. Many of the arguments for sysctl_add_oid() are common
to the macros. The arguments are as follows:
ctx A pointer to an optional sysctl context, or NULL. See
sysctl_ctx_init(9) for details. Programmers are strongly
advised to use contexts to organize the dynamic oids which they
create, unless special creation and deletion sequences are
required. If ctx is not NULL, the newly created oid will be
added to this context as its first entry.
parent A pointer to a struct sysctl_oid_list, which is the head of the
parent’s list of children.
number The oid number that will be assigned to this oid. In almost all
cases this should be set to OID_AUTO, which will result in the
assignment of the next available oid number.
name The name of the oid. The newly created oid will contain a copy
of the name.
kind The kind of oid, specified as a bit mask of the type and access
values defined in the #include<sys/sysctl.h>
header file. Oids created dynamically always have the
CTLFLAG_DYN flag set. Access flags specify whether this oid is
read-only or read-write, and whether it may be modified by all
users or by the superuser only.
arg1 A pointer to any data that the oid should reference, or NULL.
arg2 The size of arg1, or 0 if arg1 is NULL.
handler A pointer to the 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 is possible also to define new handlers
using the SYSCTL_ADD_PROC() macro.
format A pointer to a string which specifies the format of the oid
symbolically. This format is used as a hint by sysctl(8) to
apply proper data formatting for display purposes. Currently
used format names are: “N” for node, “A” for char *, “I” for
int, “IU” for unsigned int, “L” for long, “LU” for unsigned long
and “S,TYPE” for struct TYPE structures.
descr A pointer to a textual description of the oid.
The sysctl_move_oid() function reparents an existing oid. The oid is
assigned a new number as if it had been created with number set to
OID_AUTO.
The sysctl_remove_oid() function removes a dynamically created oid from
the tree, optionally freeing its resources. It takes the following
arguments:
oidp A pointer to the dynamic oid to be removed. If the oid is not
dynamic, or the pointer is NULL, the function returns EINVAL.
del If non-zero, sysctl_remove_oid() will try to free the oid’s
resources when the reference count of the oid becomes zero.
However, if del is set to 0, the routine will only deregister
the oid from the tree, without freeing its resources. This
behaviour is useful when the caller expects to rollback
(possibly partially failed) deletion of many oids later.
recurse If non-zero, attempt to remove the node and all its children.
If recurse is set to 0, any attempt to remove a node that
contains any children will result in a ENOTEMPTY error.
WARNING: userecursivedeletionwithextremecaution! Normally
it should not be needed if contexts are used. Contexts take
care of tracking inter-dependencies between users of the tree.
However, in some extreme cases it might be necessary to remove
part of the subtree no matter how it was created, in order to
free some other resources. Be aware, though, that this may
result in a system panic(9) if other code sections continue to
use removed subtrees.
Again, in most cases the programmer should use contexts, as described in
sysctl_ctx_init(9), to keep track of created oids, and to delete them
later in orderly fashion.
There is a set of macros defined that helps to create oids of given type.
They are as follows:
SYSCTL_ADD_OID() creates a raw oid. This macro is functionally
equivalent to the sysctl_add_oid() function.
SYSCTL_ADD_NODE() creates an oid of type CTLTYPE_NODE, to which child
oids may be added.
SYSCTL_ADD_STRING() creates an oid that handles a zero-terminated
character string.
SYSCTL_ADD_INT() creates an oid that handles an int variable.
SYSCTL_ADD_UINT() creates an oid that handles an unsigned int
variable.
SYSCTL_ADD_LONG() creates an oid that handles a long variable.
SYSCTL_ADD_ULONG() creates an oid that handles an unsigned long
variable.
SYSCTL_ADD_QUAD() creates an oid that handles an int64_t variable.
SYSCTL_ADD_OPAQUE() creates an oid that handles any chunk of opaque data
of the size specified by the len argument, which is
a pointer to a size_t *.
SYSCTL_ADD_STRUCT() creates an oid that handles a struct TYPE structure.
The format parameter will be set to “S,TYPE” to
provide proper hints to the sysctl(8) utility.
SYSCTL_ADD_PROC() creates an oid with the specified handler function.
The handler is responsible for handling 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.

SEEALSO

HISTORY

AUTHORS

BUGS

Sharing nodes between many code sections causes interdependencies that
sometimes may lock the resources. For example, if module A hooks up a
subtree to an oid created by module B, module B will be unable to delete
that oid. 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.