1.\" $NetBSD: sysctl.9,v 1.2 2004/01/21 23:18:52 wiz Exp $ 2.\" 3.\" Copyright (c) 2004 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Andrew Brown. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 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. 17.\" 3. Neither the name of The NetBSD Foundation nor the names of its 18.\" contributors may be used to endorse or promote products derived 19.\" from this software without specific prior written permission. 20.\" 21.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31.\" POSSIBILITY OF SUCH DAMAGE. 32.\" 33.Dd January 5, 2004 34.Dt SYSCTL 9 35.Os 36.Sh NAME 37.Nm sysctl 38.Nd system variable control interfaces 39.Sh SYNOPSIS 40.In sys/param.h 41.In sys/sysctl.h 42.Pp 43Primary external interfaces: 44.Ft void 45.Fn sysctl_init void 46.Ft int 47.Fn sysctl_lock "struct lwp *l" "void *oldp" "size_t savelen" 48.Ft int 49.Fn sysctl_dispatch "const int *name" "u_int namelen" "void *oldp" \ 50"size_t *oldlenp" "const void *newp" "size_t newlen" "const int *oname" \ 51"struct lwp *l" "const struct sysctlnode *rnode" 52.Ft void 53.Fn sysctl_unlock "struct lwp *l" 54.Ft int 55.Fn sysctl_createv "int flags" "int type" "const char *namep" \ 56"struct sysctlnode **rnode" "sysctlfn func" "u_quad_t qv" "void *newp" \ 57"size_t newlen" ... 58.Ft int 59.Fn sysctl_destroyv "struct sysctlnode *rnode" ... 60.Ft void 61.Fn sysctl_free "struct sysctlnode *rnode" 62.Ft int 63.Fn old_sysctl "int *name" "u_int namelen" "void *oldp" \ 64"size_t *oldlenp" "void *newp" "size_t newlen" "struct lwp *l" 65.Pp 66Core internal functions: 67.Ft int 68.Fn sysctl_locate "struct lwp *l" "const int *name" "u_int namelen" \ 69"struct sysctlnode **rnode" "int *nip" 70.Ft int 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" "struct sysctlnode *rnode" 74.Ft int 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" "struct sysctlnode *rnode" 78.Ft int 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" "struct sysctlnode *rnode" 82.Ft int 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" 86.Pp 87Simple 88.Dq helper 89functions: 90.Ft int 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" 94.Ft int 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" 98.Ft int 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"" 102.Sh DESCRIPTION 103The SYSCTL subsystem instruments a number of kernel tunables and other 104data structures via a simple MIB-like interface, primarily for 105consumption by userland programs, but also for use internally by the 106kernel. 107.Sh LOCKING 108All operations on the SYSCTL tree must be protected by acquiring the 109main SYSCTL lock. 110The only functions that can be called when the lock is not held are 111.Fn sysctl_lock , 112.Fn sysctl_createv , 113.Fn sysctl_destroyv , 114and 115.Fn old_sysctl . 116All other functions require the tree to be locked. 117This is to prevent other users of the tree from moving nodes around 118during an add operation, or from destroying nodes or subtrees that are 119actively being used. 120The lock is acquired by calling 121.Fn sysctl_lock 122with a pointer to the process's lwp 123.Fa l 124.Dv ( NULL 125may be passed to all functions as the lwp pointer if no lwp is 126appropriate, though any changes made via 127.Fn sysctl_create , 128.Fn sysctl_destroy , 129.Fn sysctl_lookup , 130or by any helper function will be done with effective superuser 131privileges). 132The 133.Fa oldp 134and 135.Fa savelen 136arguments are a pointer to and the size of the memory region the 137caller will be using to collect data from SYSCTL. 138These may also be 139.Dv NULL 140and 0, respectively. 141.Pp 142The memory region will be locked via 143.Fn uvm_vslock 144if it is a region in userspace. 145The address and size of the region are recorded so that when the 146SYSCTL lock is to be released via 147.Fn sysctl_unlock , 148only the lwp pointer 149.Fa l 150is required. 151.Sh LOOKUPS 152Once the lock has been acquired, it is typical to call 153.Fn sysctl_dispatch 154to handle the request. 155.Fn sysctl_dispatch 156will examine the contents of 157.Fa name , 158an array of integers at least 159.Fa namelen 160long, which is to be located in kernel space, in order to determine 161which function to call to handle the specific request. 162.Pp 163.Fn sysctl_dispatch 164uses the following algorithm to determine the function to call: 165.Pp 166.Bl -bullet 167.It 168Scan the tree using 169.Fn sysctl_locate 170.It 171If the node returned has a 172.Dq helper 173function, call it 174.It 175If the requested node was found but has no function, call 176.Fn sysctl_lookup 177.It 178If the node was not found and 179.Fa name 180specifies one of 181.Fn sysctl_query , 182.Fn sysctl_create , 183or 184.Fn sysctl_destroy , 185call the appropriate function 186.It 187If none of these options applies and no other error was yet recorded, 188return 189.Er EOPNOTSUPP 190.Pp 191.El 192The 193.Fa oldp 194and 195.Fa oldlenp 196arguments to 197.Fn sysctl_dispatch , 198as with all the other core functions, describe an area into which the 199current or requested value may be copied. 200.Fa oldp 201may or may not be a pointer into userspace (as dictated by whether 202.Fa l 203is 204.Dv NULL 205or not). 206.Fa oldlenp 207is a 208.No non- Ns Dv NULL 209pointer to a size_t. 210.Fa newp 211and 212.Fa newlen 213describe an area where the new value for the request may be found; 214.Fa newp 215may also be a pointer into userspace. 216The 217.Fa oname 218argument is a 219.No non- Ns Dv NULL 220pointer to the base of the request currently 221being processed. 222By simple arithmetic on 223.Fa name , 224.Fa namelen , 225and 226.Fa oname , 227one can easily determine the entire original request and 228.Fa namelen 229values, if needed. 230The 231.Fa rnode 232value, as passed to 233.Fn sysctl_dispatch 234represents the root of the tree into which the current request is to 235be dispatched. 236If 237.Dv NULL , 238the main tree will be used. 239.Pp 240.Fn sysctl_locate 241scans a tree for the node most specific to a request. 242If the pointer referenced by 243.Fa rnode 244is not 245.Dv NULL , 246the tree indicated is searched, otherwise the main tree 247will be used. 248The address of the most relevant node will be returned via 249.Fa rnode 250and the number of MIB entries consumed will be returned via 251.Fa nip , 252if it is not 253.Dv NULL . 254.Pp 255The 256.Fn sysctl_lookup 257function takes the same arguments as 258.Fn sysctl_dispatch 259with the caveat that the value for 260.Fa namelen 261must be zero in order to indicate that the node referenced by the 262.Fa rnode 263argument is the one to which the lookup is being applied. 264.Sh CREATION AND DESTRUCTION OF NODES 265New nodes are created and destroyed by the 266.Fn sysctl_create 267and 268.Fn sysctl_destroy 269functions. 270These functions take the same arguments as 271.Fn sysctl_dispatch 272with the additional requirement that the 273.Fa namelen 274argument must be 1 and the 275.Fa name 276argument must point to an integer valued either 277.Dv CTL_CREATE 278or 279.Dv CTL_CREATESYM 280when creating a new node, or 281.Dv CTL_DESTROY 282when destroying 283a node. 284The 285.Fa newp 286and 287.Fa newlen 288arguments should point to a copy of the node to be created or 289destroyed. 290If the create or destroy operation was successful, a copy of the node 291created or destroyed will be placed in the space indicated by 292.Fa oldp 293and 294.Fa oldlenp . 295If the create operation fails because of a conflict with an existing 296node, a copy of that node will be returned instead. 297.Pp 298In order to facilitate the creation and destruction of nodes from a 299given tree by kernel subsystems, the functions 300.Fn sysctl_createv 301and 302.Fn sysctl_destroyv 303are provided. 304These functions take care of the overhead of filling in the contents 305of the create or destroy request, dealing with locking, locating the 306appropriate parent node, etc. 307.Pp 308The arguments to 309.Fn sysctl_createv 310are used to construct the new node. 311The 312.Fa flags 313and 314.Fa type 315arguments are combined into the 316.Fa sysctl_flags 317field. 318Note: the 319.Dv SYSCTL_PERMANENT 320flag can only be set from SYSCTL setup routines (see 321.Sx SETUP FUNCTIONS ) 322as called by 323.Fn sysctl_init . 324The 325.Fa namep 326argument is copied into the 327.Fa sysctl_name 328field and must be less than 329.Dv SYSCTL_NAMELEN 330characters in length. 331If the pointer referenced by the 332.Fa rnode 333argument is not 334.Dv NULL , 335it is taken to be the root of the tree into 336which the new node must be placed, otherwise the main tree will be 337used. 338If 339.Fa rnode 340is not 341.Dv NULL , 342on return it will be adjusted to point to the new node. 343The 344.Fa func 345argument is the name of a 346.Dq helper 347function (see 348.Sx HELPER FUNCTIONS AND MACROS ) . 349If the 350.Dv SYSCTL_IMMEDIATE 351flag is set, the 352.Fa qv 353argument will be interpreted as the initial value for the new 354.Dq int 355or 356.Dq quad 357node. 358This flag does not apply to any other type of node. 359The 360.Fa newp 361and 362.Fa newlen 363arguments describe the data external to SYSCTL that is to be 364instrumented. 365One of 366.Fa func , 367.Fa qv 368and the 369.Dv SYSCTL_IMMEDIATE 370flag, or 371.Fa newp 372and 373.Fa newlen 374must be given for nodes that instrument data, otherwise an error is 375returned. 376.Pp 377The remaining arguments are a list of integers specifying the path 378through the MIB to the node being created. 379The list must be terminated by the 380.Dv CTL_EOL 381value. 382The penultimate value in the list may be 383.Dv CTL_CREATE 384if a dynamic MIB entry is to be made for this node. 385.Fn sysctl_createv 386specifically does not support 387.Dv CTL_CREATESYM , 388since setup routines are 389expected to be able to use the in-kernel 390.Xr ksyms 4 391interface to discover the location of the data to be instrumented. 392If the node to be created matches a node that already exists, a return 393code of 0 is given, indicating success. 394.Pp 395When using 396.Fn sysctl_destroyv 397to destroy a given node, the 398.Fa rnode 399argument, if not 400.Dv NULL , 401is taken to be the root of the tree from which 402the node is to be destroyed, otherwise the main tree is used. 403The rest of the arguments are a list of integers specifying the path 404through the MIB to the node being destroyed. 405If the node being destroyed does not exist, a successful return code 406is given. 407Nodes marked with the 408.Dv SYSCTL_PERMANENT 409flag cannot be destroyed. 410.Sh HELPER FUNCTIONS AND MACROS 411Helper functions are invoked with the same common argument set as 412.Fn sysctl_dispatch 413except that the 414.Fa rnode 415argument will never be 416.Dv NULL . 417It will be set to point to the node that corresponds most closely to 418the current request. 419Helpers are forbidden from modifying the node they are passed; they 420should instead copy the structure if changes are required in order to 421effect access control or other checks. 422The 423.Dq helper 424prototype and function that needs to ensure that a newly assigned 425value is within a certain range (presuming external data) would look 426like the following: 427.Pp 428.Bd -literal -offset indent -compact 429static int sysctl_helper(SYSCTLFN_PROTO); 430.sp 431static int 432sysctl_helper(SYSCTLFN_ARGS) 433{ 434 struct sysctlnode node; 435 int t, error; 436.sp 437 node = *rnode; 438 node.sysctl_data = \*[Am]t; 439 error = sysctl_lookup(SYSCTLFN_CALL(\*[Am]node)); 440 if (error || newp == NULL) 441 return (error); 442.sp 443 if (t \*[Lt] 0 || t \*[Gt] 20) 444 return (EINVAL); 445.sp 446 *(int*)rnode-\*[Gt]sysctl_data = t; 447 return (0); 448} 449.Ed 450.Pp 451The use of the 452.Dv SYSCTLFN_PROTO , 453.Dv SYSCTLFN_ARGS, and 454.Dv SYSCTLFN_CALL 455 macros ensure that all arguments are passed properly. 456The 457.Dv SYSCTLFN_RWPROTO 458and 459.Dv SYSCTLFN_RWARGS 460macros are only used internally by those core SYSCTL routines that may 461have cause to modify the data in the given SYSCTL tree. 462The single argument to the 463.Dv SYSCTLFN_CALL 464macro is the pointer to the node being examined. 465.Pp 466Three basic helper functions are available for use. 467.Fn sysctl_needfunc 468will emit a warning to the system console whenever it is invoked and 469provides a simplistic read-only interface to the given node. 470.Fn sysctl_notavail 471will forward 472.Dq queries 473to 474.Fn sysctl_query 475so that subtrees can be discovered, but will return 476.Er EOPNOTSUPP 477for any other condition. 478.Fn sysctl_null 479specifically ignores any arguments given, sets the value indicated by 480.Fa oldlenp 481to zero, and returns success. 482.Sh SETUP FUNCTIONS 483Though nodes can be added to the SYSCTL tree at any time, in order to 484add nodes during the kernel bootstrap phase, a proper 485.Dq setup 486function must be used. 487Setup functions are declared using the 488.Dv SYSCTL_SETUP 489macro, which takes the name of the function and a short string 490description of the function as arguments. 491The address of the function is added to a list of functions that 492.Fn sysctl_init 493traverses during initialization. 494.Pp 495Setup functions to not have to add nodes to the main tree, but can set 496up their own trees for emulation or other purposes. 497Emulations that require use of a main tree but with some nodes changed 498to suit their own purposes can arrange to overlay a sparse private 499tree onto their main tree by making the 500.Fa e_sysctlovly 501member of their struct emul definition point to the overlaid tree. 502.Pp 503Setup functions should take care to create all nodes from the root 504down to the subtree they are creating, since the order in which setup 505functions are called is arbitrary (the order in which setup functions 506are called is only determined by the ordering of the object files as 507passed to the linker when the kernel is built). 508.Sh MISCELLANEOUS FUNCTIONS 509.Fn sysctl_init 510is called early in the kernel bootstrap process. 511It initializes the SYSCTL lock, calls all the registered setup 512functions, and marks the tree as permanent. 513.Pp 514.Fn sysctl_free 515will unconditionally delete any and all nodes below the given node. 516Its intended use is for the deletion of entire trees, not subtrees. 517If a subtree is to be removed, 518.Fn sysctl_destroy 519or 520.Fn sysctl_destroyv 521should be used to ensure that nodes not owned by the sub-system being 522deactivated are not mistakenly destroyed. 523The SYSCTL lock must be held when calling this function. 524.Pp 525.Fn old_sysctl 526provides an interface similar to the old SYSCTL implementation, with 527the exception that access checks on a per-node basis are performed if 528the 529.Fa l 530argument is 531.No non- Ns Dv NULL . 532If called with a 533.Dv NULL 534argument, the values for 535.Fa newp 536and 537.Fa oldp 538are interpreted as kernel addresses, and access is performed as for 539the superuser. 540.Sh NOTES 541It is expected that nodes will be added to (or removed from) the tree 542during the following stages of a machine's lifetime: 543.Pp 544.Bl -bullet -compact 545.It 546initialization -- when the kernel is booting 547.It 548autoconfiguration -- when devices are being probed at boot time 549.It 550.Dq plug and play 551device attachment -- when a PC-Card, USB, or other device is plugged 552in or attached 553.It 554LKM initialization -- when an LKM is being loaded 555.It 556.Dq run-time 557-- when a process creates a node via the 558.Xr sysctl 3 559interface 560.El 561.Pp 562Nodes marked with 563.Dv SYSCTL_PERMANENT 564can only be added to a tree during the first or initialization phase, 565and can never be removed. 566The initialization phase terminates when the main tree's root is 567marked with the 568.Dv SYSCTL_PERMANENT 569flag. 570Once the main tree is marked in this manner, no nodes can be added to 571any tree that is marked with 572.Dv SYSCTL_READONLY 573at its root, and no nodes can be added at all if the main tree's root 574is so marked. 575.Pp 576Nodes added by device drivers, LKMs, and at device insertion time can 577be added to (and removed from) 578.Dq read-only 579parent nodes. 580.Pp 581Nodes created by processes can only be added to 582.Dq writable 583parent nodes. 584See 585.Xr sysctl 3 586for a description of the flags that are allowed to be used by 587when creating nodes. 588.Sh SEE ALSO 589.Xr sysctl 3 590.Sh HISTORY 591The dynamic SYSCTL implementation first appeared in 592.Nx 2.0 . 593.Sh AUTHORS 594.An Andrew Brown 595.Aq atatat@NetBSD.org 596designed and implemented the dynamic SYSCTL implementation. 597