1 .\" $NetBSD: sysctl.9,v 1.13 2008/11/12 12:35:54 ad Exp $
3 .\" Copyright (c) 2004 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
35 .Nd system variable control interfaces
40 Primary external interfaces:
44 .Fn sysctl_lock "struct lwp *l" "void *oldp" "size_t savelen"
46 .Fn sysctl_dispatch "const int *name" "u_int namelen" "void *oldp" \
47 "size_t *oldlenp" "const void *newp" "size_t newlen" "const int *oname" \
48 "struct lwp *l" "const struct sysctlnode *rnode"
50 .Fn sysctl_unlock "struct lwp *l"
52 .Fn sysctl_createv "struct sysctllog **log" "int cflags" \
53 "const struct sysctlnode **rnode" "const struct sysctlnode **cnode" \
54 "int flags" "int type" "const char *namep" "const char *desc" \
55 "sysctlfn func" "u_quad_t qv" "void *newp" "size_t newlen" ...
57 .Fn sysctl_destroyv "struct sysctlnode *rnode" ...
59 .Fn sysctl_free "struct sysctlnode *rnode"
61 .Fn sysctl_teardown "struct sysctllog **"
63 .Fn old_sysctl "int *name" "u_int namelen" "void *oldp" \
64 "size_t *oldlenp" "void *newp" "size_t newlen" "struct lwp *l"
66 Core internal functions:
68 .Fn sysctl_locate "struct lwp *l" "const int *name" "u_int namelen" \
69 "const struct sysctlnode **rnode" "int *nip"
71 .Fn sysctl_lookup "const int *name" "u_int namelen" "void *oldp" \
72 "size_t *oldlenp" "const void *newp" "size_t newlen" "const int *oname" \
73 "struct lwp *l" "const struct sysctlnode *rnode"
75 .Fn sysctl_create "const int *name" "u_int namelen" "void *oldp" \
76 "size_t *oldlenp" "const void *newp" "size_t newlen" "const int *oname" \
77 "struct lwp *l" "const struct sysctlnode *rnode"
79 .Fn sysctl_destroy "const int *name" "u_int namelen" "void *oldp" \
80 "size_t *oldlenp" "const void *newp" "size_t newlen" "const int *oname" \
81 "struct lwp *l" "const struct sysctlnode *rnode"
83 .Fn sysctl_query "const int *name" "u_int namelen" "void *oldp" \
84 "size_t *oldlenp" "const void *newp" "size_t newlen" "const int *oname" \
85 "struct lwp *l" "const struct sysctlnode *rnode"
91 .Fn sysctl_needfunc "const int *name" "u_int namelen" "void *oldp" \
92 "size_t *oldlenp" "const void *newp" "size_t newlen" "const int *oname" \
93 "struct lwp *l" "const struct sysctlnode *rnode"
95 .Fn sysctl_notavail "const int *name" "u_int namelen" "void *oldp" \
96 "size_t *oldlenp" "const void *newp" "size_t newlen" "const int *oname" \
97 "struct lwp *l" "const struct sysctlnode *rnode"
99 .Fn sysctl_null "const int *name" "u_int namelen" "void *oldp" \
100 "size_t *oldlenp" "const void *newp" "size_t newlen" "const int *oname" \
101 "struct lwp *l" "const struct sysctlnode *rnode"
103 The SYSCTL subsystem instruments a number of kernel tunables and other
104 data structures via a simple MIB-like interface, primarily for
105 consumption by userland programs, but also for use internally by the
108 All operations on the SYSCTL tree must be protected by acquiring the
110 The only functions that can be called when the lock is not held are
113 .Fn sysctl_destroyv ,
116 All other functions require the tree to be locked.
117 This is to prevent other users of the tree from moving nodes around
118 during an add operation, or from destroying nodes or subtrees that are
120 The lock is acquired by calling
122 with a pointer to the process's lwp
125 may be passed to all functions as the lwp pointer if no lwp is
126 appropriate, though any changes made via
130 or by any helper function will be done with effective superuser
136 arguments are a pointer to and the size of the memory region the
137 caller will be using to collect data from SYSCTL.
142 The memory region will be locked via
144 if it is a region in userspace.
145 The address and size of the region are recorded so that when the
146 SYSCTL lock is to be released via
152 Once the lock has been acquired, it is typical to call
154 to handle the request.
156 will examine the contents of
158 an array of integers at least
160 long, which is to be located in kernel space, in order to determine
161 which function to call to handle the specific request.
164 uses the following algorithm to determine the function to call:
171 If the node returned has a
175 If the requested node was found but has no function, call
178 If the node was not found and
185 call the appropriate function
187 If none of these options applies and no other error was yet recorded,
197 .Fn sysctl_dispatch ,
198 as with all the other core functions, describe an area into which the
199 current or requested value may be copied.
201 may or may not be a pointer into userspace (as dictated by whether
213 describe an area where the new value for the request may be found;
215 may also be a pointer into userspace.
220 pointer to the base of the request currently
222 By simple arithmetic on
227 one can easily determine the entire original request and
234 represents the root of the tree into which the current request is to
238 the main tree will be used.
241 scans a tree for the node most specific to a request.
242 If the pointer referenced by
246 the tree indicated is searched, otherwise the main tree
248 The address of the most relevant node will be returned via
250 and the number of MIB entries consumed will be returned via
257 function takes the same arguments as
259 with the caveat that the value for
261 must be zero in order to indicate that the node referenced by the
263 argument is the one to which the lookup is being applied.
264 .Sh CREATION AND DESTRUCTION OF NODES
265 New nodes are created and destroyed by the
270 These functions take the same arguments as
272 with the additional requirement that the
274 argument must be 1 and the
276 argument must point to an integer valued either
280 when creating a new node, or
288 arguments should point to a copy of the node to be created or
290 If the create or destroy operation was successful, a copy of the node
291 created or destroyed will be placed in the space indicated by
295 If the create operation fails because of a conflict with an existing
296 node, a copy of that node will be returned instead.
298 In order to facilitate the creation and destruction of nodes from a
299 given tree by kernel subsystems, the functions
304 These functions take care of the overhead of filling in the contents
305 of the create or destroy request, dealing with locking, locating the
306 appropriate parent node, etc.
310 are used to construct the new node.
315 a sysctllog structure will be allocated and the pointer referenced
316 will be changed to address it.
317 The same log may be used for any number of nodes, provided they are
318 all inserted into the same tree.
319 This allows for a series of nodes to be created and later removed from
320 the tree in a single transaction (via
321 .Fn sysctl_teardown )
322 without the need for any record
323 keeping on the caller's part.
326 argument is currently unused and must be zero.
329 argument must either be
331 or a valid pointer to a reference to the root of the tree into which
332 the new node must be placed.
335 the main tree will be used.
345 on return it will be adjusted to point to the address of the new node.
351 arguments are combined into the
353 field, and the current value for
357 .Dv CTLFLAG_PERMANENT
358 flag can only be set from SYSCTL setup routines (see
359 .Sx SETUP FUNCTIONS )
364 argument is copied into the
366 field and must be less than
368 characters in length.
369 The string indicated by
371 will be copied if the
373 flag is set, and will be used as the node's description.
376 attempts to delete a node that does not own its own description (and
377 is not marked as permanent), but the deletion fails, the description
386 argument is the name of a
389 .Sx HELPER FUNCTIONS AND MACROS ) .
391 .Dv CTLFLAG_IMMEDIATE
394 argument will be interpreted as the initial value for the new
399 This flag does not apply to any other type of node.
404 arguments describe the data external to SYSCTL that is to be
410 .Dv CTLFLAG_IMMEDIATE
415 must be given for nodes that instrument data, otherwise an error is
418 The remaining arguments are a list of integers specifying the path
419 through the MIB to the node being created.
420 The list must be terminated by the
423 The penultimate value in the list may be
425 if a dynamic MIB entry is to be made for this node.
427 specifically does not support
429 since setup routines are
430 expected to be able to use the in-kernel
432 interface to discover the location of the data to be instrumented.
433 If the node to be created matches a node that already exists, a return
434 code of 0 is given, indicating success.
438 to destroy a given node, the
442 is taken to be the root of the tree from which
443 the node is to be destroyed, otherwise the main tree is used.
444 The rest of the arguments are a list of integers specifying the path
445 through the MIB to the node being destroyed.
446 If the node being destroyed does not exist, a successful return code
448 Nodes marked with the
449 .Dv CTLFLAG_PERMANENT
450 flag cannot be destroyed.
451 .Sh HELPER FUNCTIONS AND MACROS
452 Helper functions are invoked with the same common argument set as
456 argument will never be
458 It will be set to point to the node that corresponds most closely to
460 Helpers are forbidden from modifying the node they are passed; they
461 should instead copy the structure if changes are required in order to
462 effect access control or other checks.
465 prototype and function that needs to ensure that a newly assigned
466 value is within a certain range (presuming external data) would look
469 .Bd -literal -offset indent -compact
470 static int sysctl_helper(SYSCTLFN_PROTO);
473 sysctl_helper(SYSCTLFN_ARGS)
475 struct sysctlnode node;
479 node.sysctl_data = \*[Am]t;
480 error = sysctl_lookup(SYSCTLFN_CALL(\*[Am]node));
481 if (error || newp == NULL)
484 if (t \*[Lt] 0 || t \*[Gt] 20)
487 *(int*)rnode-\*[Gt]sysctl_data = t;
494 .Dv SYSCTLFN_ARGS, and
496 macros ensure that all arguments are passed properly.
497 The single argument to the
499 macro is the pointer to the node being examined.
501 Three basic helper functions are available for use.
503 will emit a warning to the system console whenever it is invoked and
504 provides a simplistic read-only interface to the given node.
510 so that subtrees can be discovered, but will return
512 for any other condition.
514 specifically ignores any arguments given, sets the value indicated by
516 to zero, and returns success.
518 Though nodes can be added to the SYSCTL tree at any time, in order to
519 add nodes during the kernel bootstrap phase, a proper
521 function must be used.
522 Setup functions are declared using the
524 macro, which takes the name of the function and a short string
525 description of the function as arguments.
528 .Dv SYSCTL_DEBUG_SETUP
529 kernel configuration in
532 The address of the function is added to a list of functions that
534 traverses during initialization.
536 Setup functions do not have to add nodes to the main tree, but can set
537 up their own trees for emulation or other purposes.
538 Emulations that require use of a main tree but with some nodes changed
539 to suit their own purposes can arrange to overlay a sparse private
540 tree onto their main tree by making the
542 member of their struct emul definition point to the overlaid tree.
544 Setup functions should take care to create all nodes from the root
545 down to the subtree they are creating, since the order in which setup
546 functions are called is arbitrary (the order in which setup functions
547 are called is only determined by the ordering of the object files as
548 passed to the linker when the kernel is built).
549 .Sh MISCELLANEOUS FUNCTIONS
551 is called early in the kernel bootstrap process.
552 It initializes the SYSCTL lock, calls all the registered setup
553 functions, and marks the tree as permanent.
556 will unconditionally delete any and all nodes below the given node.
557 Its intended use is for the deletion of entire trees, not subtrees.
558 If a subtree is to be removed,
562 should be used to ensure that nodes not owned by the sub-system being
563 deactivated are not mistakenly destroyed.
564 The SYSCTL lock must be held when calling this function.
567 unwinds a sysctllog and deletes the nodes in the opposite order in
568 which they were created.
571 provides an interface similar to the old SYSCTL implementation, with
572 the exception that access checks on a per-node basis are performed if
576 .No non- Ns Dv NULL .
579 argument, the values for
583 are interpreted as kernel addresses, and access is performed as for
586 It is expected that nodes will be added to (or removed from) the tree
587 during the following stages of a machine's lifetime:
591 initialization -- when the kernel is booting
593 autoconfiguration -- when devices are being probed at boot time
596 device attachment -- when a PC-Card, USB, or other device is plugged
599 module initialization -- when a module is being loaded
602 -- when a process creates a node via the
608 .Dv CTLFLAG_PERMANENT
609 can only be added to a tree during the first or initialization phase,
610 and can never be removed.
611 The initialization phase terminates when the main tree's root is
613 .Dv CTLFLAG_PERMANENT
615 Once the main tree is marked in this manner, no nodes can be added to
616 any tree that is marked with
618 at its root, and no nodes can be added at all if the main tree's root
621 Nodes added by device drivers, modules, and at device insertion time can
622 be added to (and removed from)
626 Nodes created by processes can only be added to
631 for a description of the flags that are allowed to be used by
636 The dynamic SYSCTL implementation first appeared in
640 .Aq atatat@NetBSD.org
641 designed and implemented the dynamic SYSCTL implementation.