1.\" 2.\" Copyright (c) 2006 Robert N. M. Watson 3.\" All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24.\" SUCH DAMAGE. 25.\" 26.\" $FreeBSD: src/share/man/man9/sysctl.9,v 1.3 2006/04/28 23:21:36 keramida Exp $ 27.\" 28.Dd July 20, 2018 29.Dt SYSCTL 9 30.Os 31.Sh NAME 32.Nm SYSCTL_DECL , 33.Nm SYSCTL_INT , 34.Nm SYSCTL_LONG , 35.Nm SYSCTL_QUAD , 36.Nm SYSCTL_NODE , 37.Nm SYSCTL_OPAQUE , 38.Nm SYSCTL_PROC , 39.Nm SYSCTL_STRING , 40.Nm SYSCTL_STRUCT , 41.Nm SYSCTL_U32 , 42.Nm SYSCTL_UINT , 43.Nm SYSCTL_ULONG , 44.Nm SYSCTL_UQUAD 45.Nd Static sysctl declaration functions 46.Sh SYNOPSIS 47.In sys/types.h 48.In sys/sysctl.h 49.Fo SYSCTL_DECL 50.Fa "name" 51.Fc 52.Fo SYSCTL_INT 53.Fa "parent" 54.Fa "nbr" 55.Fa "name" 56.Fa "access" 57.Fa "ptr" 58.Fa "val" 59.Fa "descr" 60.Fc 61.Fo SYSCTL_LONG 62.Fa "parent" 63.Fa "nbr" 64.Fa "name" 65.Fa "access" 66.Fa "ptr" 67.Fa "val" 68.Fa "descr" 69.Fc 70.Fo SYSCTL_QUAD 71.Fa "parent" 72.Fa "nbr" 73.Fa "name" 74.Fa "access" 75.Fa "ptr" 76.Fa "val" 77.Fa "descr" 78.Fc 79.Fo SYSCTL_NODE 80.Fa "parent" 81.Fa "nbr" 82.Fa "name" 83.Fa "access" 84.Fa "handler" 85.Fa "descr" 86.Fc 87.Fo SYSCTL_OPAQUE 88.Fa "parent" 89.Fa "nbr" 90.Fa "name" 91.Fa "access" 92.Fa "ptr" 93.Fa "len" 94.Fa "fmt" 95.Fa "descr" 96.Fc 97.Fo SYSCTL_PROC 98.Fa "parent" 99.Fa "nbr" 100.Fa "name" 101.Fa "access" 102.Fa "ptr" 103.Fa "arg" 104.Fa "handler" 105.Fa "fmt" 106.Fa "descr" 107.Fc 108.Fo SYSCTL_STRING 109.Fa "parent" 110.Fa "nbr" 111.Fa "name" 112.Fa "access" 113.Fa "arg" 114.Fa "len" 115.Fa "descr" 116.Fc 117.Fo SYSCTL_STRUCT 118.Fa "parent" 119.Fa "nbr" 120.Fa "name" 121.Fa "access" 122.Fa "ptr" 123.Fa "type" 124.Fa "descr" 125.Fc 126.Fo SYSCTL_U32 127.Fa "parent" 128.Fa "number" 129.Fa "name" 130.Fa "access" 131.Fa "ptr" 132.Fa "val" 133.Fa "descr" 134.Fc 135.Fo SYSCTL_UINT 136.Fa "parent" 137.Fa "nbr" 138.Fa "name" 139.Fa "access" 140.Fa "ptr" 141.Fa "val" 142.Fa "descr" 143.Fc 144.Fo SYSCTL_ULONG 145.Fa "parent" 146.Fa "nbr" 147.Fa "name" 148.Fa "access" 149.Fa "ptr" 150.Fa "val" 151.Fa "descr" 152.Fc 153.Fo SYSCTL_UQUAD 154.Fa "parent" 155.Fa "nbr" 156.Fa "name" 157.Fa "access" 158.Fa "ptr" 159.Fa "val" 160.Fa "descr" 161.Fc 162.Sh DESCRIPTION 163The 164.Nm 165kernel interfaces allow code to statically declare 166.Xr sysctl 8 167MIB entries, which will be initialized when the kernel module containing the 168declaration is initialized. 169When the module is unloaded, the sysctl will be automatically destroyed. 170.Pp 171Sysctl nodes are created in a hierarchical tree, with all static nodes being 172represented by named C data structures; in order to create a new node under 173an existing node in the tree, the structure representing the desired parent 174node must be declared in the current context using 175.Fn SYSCTL_DECL . 176.Pp 177New nodes are declared using one of 178.Nm SYSCTL_INT , 179.Nm SYSCTL_LONG , 180.Nm SYSCTL_QUAD , 181.Nm SYSCTL_NODE , 182.Nm SYSCTL_OPAQUE , 183.Nm SYSCTL_PROC , 184.Nm SYSCTL_STRING , 185.Nm SYSCTL_STRUCT , 186.Nm SYSCTL_U32 , 187.Nm SYSCTL_UINT , 188.Nm SYSCTL_ULONG , 189and 190.Nm SYSCTL_UQUAD . 191Each macro accepts a parent name, as declared using 192.Nm SYSCTL_DECL , 193an OID number, typically 194.Dv OID_AUTO , 195a node name, a set of control and access flags, and a description. 196Depending on the macro, a pointer to a variable supporting the MIB entry, a 197size, a value, and a function pointer implementing the MIB entry may also be 198present. 199.Pp 200For most of the above macros, declaring a type as part of the access flags is 201not necessary -- however, when declaring a sysctl implemented by a function, 202including a type in the access mask is required: 203.Bl -tag -width ".Dv CTLTYPE_STRING" 204.It Dv CTLTYPE_NODE 205This is a node intended to be a parent for other nodes. 206.It Dv CTLTYPE_INT 207This is a signed integer. 208.It Dv CTLTYPE_STRING 209This is a nul-terminated string stored in a character array. 210.It Dv CTLTYPE_QUAD 211This is a 64-bit signed integer. 212.It Dv CTLTYPE_OPAQUE 213This is an opaque data structure. 214.It Dv CTLTYPE_STRUCT 215Alias for 216.Dv CTLTYPE_OPAQUE . 217.It Dv CTLTYPE_U32 218This is a 32-bit unsigned integer. 219.It Dv CTLTYPE_UINT 220This is an unsigned integer. 221.It Dv CTLTYPE_LONG 222This is a signed long. 223.It Dv CTLTYPE_ULONG 224This is an unsigned long. 225.It Dv CTLTYPE_UQUAD 226This is a 64-bit unsigned integer. 227.El 228.Pp 229All sysctl types except for new node declarations require one or more flags 230to be set indicating the read and write disposition of the sysctl: 231.Bl -tag -width ".Dv CTLFLAG_ANYBODY" 232.It Dv CTLFLAG_RD 233This is a read-only sysctl. 234.It Dv CTLFLAG_WR 235This is a writable sysctl. 236.It Dv CTLFLAG_RW 237This sysctl is readable and writable. 238.It Dv CTLFLAG_ANYBODY 239Any user or process can write to this sysctl. 240.It Dv CTLFLAG_SECURE 241This sysctl can be written to only if the effective securelevel of the 242process is <= 0. 243.It Dv CTLFLAG_PRISON 244This sysctl can be written to by processes in 245.Xr jail 2 . 246.\".It Dv CTLFLAG_SKIP 247.\"When iterating the sysctl name space, do not list this sysctl. 248.El 249.Pp 250When creating new sysctls, careful attention should be paid to the security 251implications of the monitoring or management interface being created. 252Most sysctls present in the kernel are read-only or writable only by the 253superuser. 254Sysctls exporting extensive information on system data structures and 255operation, especially those implemented using procedures, will wish to 256implement access control to limit the undesired exposure of information about 257other processes, network connections, etc. 258.Pp 259The following top level sysctl name spaces are commonly used: 260.Bl -tag -width ".Va machdep" 261.It Va compat 262Compatibility layer information. 263.It Va debug 264Debugging information. 265Various name spaces exist under 266.Va debug . 267.It Va hw 268Hardware and device driver information. 269.It Va lwkt 270Information about the 271.Xr lwkt 9 272subsystem. 273.It Va kern 274Kernel behavior tuning; generally deprecated in favor of more specific 275name spaces. 276.It Va machdep 277Machine-dependent configuration parameters. 278.It Va net 279Network subsystem. 280Various protocols have name spaces under 281.Va net . 282.It Va sysctl 283Reserved name space for the implementation of sysctl. 284.It Va user 285Configuration settings relating to user application behavior. 286Generally, configuring applications using kernel sysctls is discouraged. 287.It Va vfs 288Virtual file system configuration and information. 289.It Va vm 290Virtual memory subsystem configuration and information. 291.El 292.Sh EXAMPLES 293Sample use of 294.Nm SYSCTL_DECL 295to declare the "machdep" sysctl tree for use by new nodes: 296.Bd -literal -offset indent 297SYSCTL_DECL(_machdep); 298.Ed 299.Pp 300Examples of integer, opaque, string, and procedure sysctls follow: 301.Bd -literal -offset indent 302/* 303 * Example of a constant integer value. Notice that the control 304 * flags are CTLFLAG_RD, the variable pointer is NULL, and the 305 * value is declared. 306 */ 307SYSCTL_INT(_debug_sizeof, OID_AUTO, bio, CTLFLAG_RD, NULL, 308 sizeof(struct bio), "sizeof(struct bio)"); 309 310/* 311 * Example of a variable integer value. Notice that the control 312 * flags are CTLFLAG_RW, the variable pointer is set, and the 313 * value is 0. 314 */ 315static int doingcache = 1; /* 1 => enable the cache */ 316SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, 317 "Enable name cache"); 318 319/* 320 * Example of a variable string value. Notice that the control 321 * flags are CTLFLAG_RW, that the variable pointer and string 322 * size are set. Unlike newer sysctls, this older sysctl uses a 323 * static oid number. 324 */ 325char kernelname[MAXPATHLEN] = "/boot/kernel"; /* XXX bloat */ 326SYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, CTLFLAG_RW, 327 kernelname, sizeof(kernelname), "Name of kernel file booted"); 328 329/* 330 * Example of an opaque data type exported by sysctl. Notice that 331 * the variable pointer and size are provided, as well as a format 332 * string for sysctl(8). 333 */ 334static l_fp pps_freq; /* scaled frequency offset (ns/s) */ 335SYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD, 336 &pps_freq, sizeof(pps_freq), "I", ""); 337 338/* 339 * Example of a procedure based sysctl exporting string 340 * information. Notice that the data type is declared, the NULL 341 * variable pointer and 0 size, the function pointer, and the 342 * format string for sysctl(8). 343 */ 344SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD, 345 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer"); 346.Ed 347.Pp 348When adding, modifying, or removing sysctl names, it is important to be 349aware that these interfaces may be used by users, libraries, applications, 350or documentation (such as published books), and are implicitly published 351application interfaces. 352As with other application interfaces, caution must be taken not to break 353existing applications, and to think about future use of new name spaces so as 354to avoid the need to rename or remove interfaces that might be depended on in 355the future. 356.Sh SEE ALSO 357.Xr sysctl 8 , 358.Xr sysctl_add_oid 9 , 359.Xr sysctl_ctx_free 9 , 360.Xr sysctl_ctx_init 9 , 361.Xr sysctl_remove_oid 9 362.Sh HISTORY 363.Xr sysctl 8 364first appeared in 365.Bx 4.4 . 366.Sh AUTHORS 367.An -nosplit 368The sysctl implementation originally found in 369.Bx 370has been extensively rewritten by 371.An Poul-Henning Kamp 372in order to add support for name lookups, name space iteration, and dynamic 373addition of MIB nodes. 374.Pp 375This man page was written by 376.An Robert N. M. Watson . 377