1dba9e9ccSRobert Watson.\" 2dba9e9ccSRobert Watson.\" Copyright (c) 2006 Robert N. M. Watson 3dba9e9ccSRobert Watson.\" All rights reserved. 4dba9e9ccSRobert Watson.\" 5dba9e9ccSRobert Watson.\" Redistribution and use in source and binary forms, with or without 6dba9e9ccSRobert Watson.\" modification, are permitted provided that the following conditions 7dba9e9ccSRobert Watson.\" are met: 8dba9e9ccSRobert Watson.\" 1. Redistributions of source code must retain the above copyright 9dba9e9ccSRobert Watson.\" notice, this list of conditions and the following disclaimer. 10dba9e9ccSRobert Watson.\" 2. Redistributions in binary form must reproduce the above copyright 11dba9e9ccSRobert Watson.\" notice, this list of conditions and the following disclaimer in the 12dba9e9ccSRobert Watson.\" documentation and/or other materials provided with the distribution. 13dba9e9ccSRobert Watson.\" 14dba9e9ccSRobert Watson.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15dba9e9ccSRobert Watson.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16dba9e9ccSRobert Watson.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17dba9e9ccSRobert Watson.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18dba9e9ccSRobert Watson.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19dba9e9ccSRobert Watson.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20dba9e9ccSRobert Watson.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21dba9e9ccSRobert Watson.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22dba9e9ccSRobert Watson.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23dba9e9ccSRobert Watson.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24dba9e9ccSRobert Watson.\" SUCH DAMAGE. 25dba9e9ccSRobert Watson.\" 26dba9e9ccSRobert Watson.\" $FreeBSD$ 27dba9e9ccSRobert Watson.\" 28f3066f2bSGavin Atkinson.Dd January 16, 2010 29dba9e9ccSRobert Watson.Dt SYSCTL 9 30dba9e9ccSRobert Watson.Os 31dba9e9ccSRobert Watson.Sh NAME 32dba9e9ccSRobert Watson.Nm SYSCTL_DECL , 33dba9e9ccSRobert Watson.Nm SYSCTL_INT , 34dba9e9ccSRobert Watson.Nm SYSCTL_LONG , 35dba9e9ccSRobert Watson.Nm SYSCTL_NODE , 36dba9e9ccSRobert Watson.Nm SYSCTL_OPAQUE , 37dba9e9ccSRobert Watson.Nm SYSCTL_PROC , 38dba9e9ccSRobert Watson.Nm SYSCTL_STRING , 39dba9e9ccSRobert Watson.Nm SYSCTL_STRUCT , 40dba9e9ccSRobert Watson.Nm SYSCTL_UINT , 41c157a036SDavid E. O'Brien.Nm SYSCTL_ULONG , 42c157a036SDavid E. O'Brien.Nm SYSCTL_XINT , 437f41115eSPawel Jakub Dawidek.Nm SYSCTL_XLONG , 447f41115eSPawel Jakub Dawidek.Nm SYSCTL_QUAD 45dba9e9ccSRobert Watson.Nd Static sysctl declaration functions 46dba9e9ccSRobert Watson.Sh SYNOPSIS 47dba9e9ccSRobert Watson.In sys/types.h 48dba9e9ccSRobert Watson.In sys/sysctl.h 49bd84dd2fSRuslan Ermilov.Fn SYSCTL_DECL name 50bd84dd2fSRuslan Ermilov.Fn SYSCTL_INT parent nbr name access ptr val descr 51bd84dd2fSRuslan Ermilov.Fn SYSCTL_LONG parent nbr name access ptr val descr 52bd84dd2fSRuslan Ermilov.Fn SYSCTL_NODE parent nbr name access handler descr 53bd84dd2fSRuslan Ermilov.Fn SYSCTL_OPAQUE parent nbr name access ptr len fmt descr 54bd84dd2fSRuslan Ermilov.Fn SYSCTL_PROC parent nbr name access ptr arg handler fmt descr 55bd84dd2fSRuslan Ermilov.Fn SYSCTL_STRING parent nbr name access arg len descr 56bd84dd2fSRuslan Ermilov.Fn SYSCTL_STRUCT parent nbr name access ptr type descr 57bd84dd2fSRuslan Ermilov.Fn SYSCTL_UINT parent nbr name access ptr val descr 58bd84dd2fSRuslan Ermilov.Fn SYSCTL_ULONG parent nbr name access ptr val descr 59bd84dd2fSRuslan Ermilov.Fn SYSCTL_XINT parent nbr name access ptr val descr 60bd84dd2fSRuslan Ermilov.Fn SYSCTL_XLONG parent nbr name access ptr val descr 617f41115eSPawel Jakub Dawidek.Fn SYSCTL_QUAD parent nbr name access ptr val descr 62dba9e9ccSRobert Watson.Sh DESCRIPTION 63dba9e9ccSRobert WatsonThe 64bd84dd2fSRuslan Ermilov.Nm SYSCTL 65dba9e9ccSRobert Watsonkernel interfaces allow code to statically declare 66dba9e9ccSRobert Watson.Xr sysctl 8 67dba9e9ccSRobert WatsonMIB entries, which will be initialized when the kernel module containing the 68dba9e9ccSRobert Watsondeclaration is initialized. 69dba9e9ccSRobert WatsonWhen the module is unloaded, the sysctl will be automatically destroyed. 70dba9e9ccSRobert Watson.Pp 71f4006affSGiorgos KeramidasSysctl nodes are created in a hierarchical tree, with all static nodes being 72dba9e9ccSRobert Watsonrepresented by named C data structures; in order to create a new node under 73dba9e9ccSRobert Watsonan existing node in the tree, the structure representing the desired parent 74dba9e9ccSRobert Watsonnode must be declared in the current context using 75dba9e9ccSRobert Watson.Fn SYSCTL_DECL . 76dba9e9ccSRobert Watson.Pp 77dba9e9ccSRobert WatsonNew nodes are declared using one of 78bd84dd2fSRuslan Ermilov.Fn SYSCTL_INT , 79bd84dd2fSRuslan Ermilov.Fn SYSCTL_LONG , 80bd84dd2fSRuslan Ermilov.Fn SYSCTL_NODE , 81bd84dd2fSRuslan Ermilov.Fn SYSCTL_OPAQUE , 82bd84dd2fSRuslan Ermilov.Fn SYSCTL_PROC , 83bd84dd2fSRuslan Ermilov.Fn SYSCTL_STRING , 84bd84dd2fSRuslan Ermilov.Fn SYSCTL_STRUCT , 85bd84dd2fSRuslan Ermilov.Fn SYSCTL_UINT , 86bd84dd2fSRuslan Ermilov.Fn SYSCTL_ULONG , 87bd84dd2fSRuslan Ermilov.Fn SYSCTL_XINT , 887f41115eSPawel Jakub Dawidek.Fn SYSCTL_XLONG , 89dba9e9ccSRobert Watsonand 907f41115eSPawel Jakub Dawidek.Fn SYSCTL_QUAD . 91dba9e9ccSRobert WatsonEach macro accepts a parent name, as declared using 92bd84dd2fSRuslan Ermilov.Fn SYSCTL_DECL , 93dba9e9ccSRobert Watsonan OID number, typically 94dba9e9ccSRobert Watson.Dv OID_AUTO , 95dba9e9ccSRobert Watsona node name, a set of control and access flags, and a description. 96dba9e9ccSRobert WatsonDepending on the macro, a pointer to a variable supporting the MIB entry, a 97dba9e9ccSRobert Watsonsize, a value, and a function pointer implementing the MIB entry may also be 98dba9e9ccSRobert Watsonpresent. 99dba9e9ccSRobert Watson.Pp 100dba9e9ccSRobert WatsonFor most of the above macros, declaring a type as part of the access flags is 101bd84dd2fSRuslan Ermilovnot necessary \[em] however, when declaring a sysctl implemented by a function, 102dba9e9ccSRobert Watsonincluding a type in the access mask is required: 103bd84dd2fSRuslan Ermilov.Bl -tag -width ".Dv CTLTYPE_STRING" 104dba9e9ccSRobert Watson.It Dv CTLTYPE_NODE 105dba9e9ccSRobert WatsonThis is a node intended to be a parent for other nodes. 106dba9e9ccSRobert Watson.It Dv CTLTYPE_INT 107dba9e9ccSRobert WatsonThis is a signed integer. 108dba9e9ccSRobert Watson.It Dv CTLTYPE_STRING 109dba9e9ccSRobert WatsonThis is a nul-terminated string stored in a character array. 110dba9e9ccSRobert Watson.It Dv CTLTYPE_QUAD 111dba9e9ccSRobert WatsonThis is a 64-bit signed integer. 112dba9e9ccSRobert Watson.It Dv CTLTYPE_OPAQUE 113dba9e9ccSRobert WatsonThis is an opaque data structure. 114dba9e9ccSRobert Watson.It Dv CTLTYPE_STRUCT 115dba9e9ccSRobert WatsonAlias for 116dba9e9ccSRobert Watson.Dv CTLTYPE_OPAQUE. 117dba9e9ccSRobert Watson.It Dv CTLTYPE_UINT 118dba9e9ccSRobert WatsonThis is an unsigned integer. 119dba9e9ccSRobert Watson.It Dv CTLTYPE_LONG 120dba9e9ccSRobert WatsonThis is a signed long. 121dba9e9ccSRobert Watson.It Dv CTLTYPE_ULONG 1228a1f4129SRobert WatsonThis is an unsigned long. 123dba9e9ccSRobert Watson.El 124dba9e9ccSRobert Watson.Pp 125dba9e9ccSRobert WatsonAll sysctl types except for new node declarations require one or more flags 126dba9e9ccSRobert Watsonto be set indicating the read and write disposition of the sysctl: 127bd84dd2fSRuslan Ermilov.Bl -tag -width ".Dv CTLFLAG_ANYBODY" 128dba9e9ccSRobert Watson.It Dv CTLFLAG_RD 129dba9e9ccSRobert WatsonThis is a read-only sysctl. 130b470b929SDavid E. O'Brien.It Dv CTLFLAG_WR 131dba9e9ccSRobert WatsonThis is a writable sysctl. 132dba9e9ccSRobert Watson.It Dv CTLFLAG_RW 133dba9e9ccSRobert WatsonThis sysctl is readable and writable. 134dba9e9ccSRobert Watson.It Dv CTLFLAG_ANYBODY 135dba9e9ccSRobert WatsonAny user or process can write to this sysctl. 136dba9e9ccSRobert Watson.It Dv CTLFLAG_SECURE 137dba9e9ccSRobert WatsonThis sysctl can be written to only if the effective securelevel of the 138bd84dd2fSRuslan Ermilovprocess is \[<=] 0. 139dba9e9ccSRobert Watson.It Dv CTLFLAG_PRISON 140dba9e9ccSRobert WatsonThis sysctl can be written to by processes in 141dba9e9ccSRobert Watson.Xr jail 2 . 142dba9e9ccSRobert Watson.It Dv CTLFLAG_SKIP 143dba9e9ccSRobert WatsonWhen iterating the sysctl name space, do not list this sysctl. 144dba9e9ccSRobert Watson.It Dv CTLFLAG_TUN 145dba9e9ccSRobert WatsonAlso declare a system tunable with the same name to initialize this variable. 146dba9e9ccSRobert Watson.It Dv CTLFLAG_RDTUN 1479280e5faSMike PritchardAlso declare a system tunable with the same name to initialize this variable; 148dba9e9ccSRobert Watsonhowever, the run-time variable is read-only. 149dba9e9ccSRobert Watson.El 150dba9e9ccSRobert Watson.Pp 151dba9e9ccSRobert WatsonWhen creating new sysctls, careful attention should be paid to the security 152dba9e9ccSRobert Watsonimplications of the monitoring or management interface being created. 153dba9e9ccSRobert WatsonMost sysctls present in the kernel are read-only or writable only by the 154dba9e9ccSRobert Watsonsuperuser. 155dba9e9ccSRobert WatsonSysctls exporting extensive information on system data structures and 156dba9e9ccSRobert Watsonoperation, especially those implemented using procedures, will wish to 157dba9e9ccSRobert Watsonimplement access control to limit the undesired exposure of information about 158dba9e9ccSRobert Watsonother processes, network connections, etc. 159dba9e9ccSRobert Watson.Pp 160dba9e9ccSRobert WatsonThe following top level sysctl name spaces are commonly used: 161bd84dd2fSRuslan Ermilov.Bl -tag -width ".Va regression" 162bd84dd2fSRuslan Ermilov.It Va compat 163dba9e9ccSRobert WatsonCompatibility layer information. 164bd84dd2fSRuslan Ermilov.It Va debug 165dba9e9ccSRobert WatsonDebugging information. 166dba9e9ccSRobert WatsonVarious name spaces exist under 167bd84dd2fSRuslan Ermilov.Va debug . 168bd84dd2fSRuslan Ermilov.It Va hw 169dba9e9ccSRobert WatsonHardware and device driver information. 170bd84dd2fSRuslan Ermilov.It Va kern 171dba9e9ccSRobert WatsonKernel behavior tuning; generally deprecated in favor of more specific 172dba9e9ccSRobert Watsonname spaces. 173bd84dd2fSRuslan Ermilov.It Va machdep 174dba9e9ccSRobert WatsonMachine-dependent configuration parameters. 175bd84dd2fSRuslan Ermilov.It Va net 176dba9e9ccSRobert WatsonNetwork subsystem. 177dba9e9ccSRobert WatsonVarious protocols have name spaces under 178bd84dd2fSRuslan Ermilov.Va net . 179bd84dd2fSRuslan Ermilov.It Va regression 180dba9e9ccSRobert WatsonRegression test configuration and information. 181bd84dd2fSRuslan Ermilov.It Va security 182f4006affSGiorgos KeramidasSecurity and security-policy configuration and information. 183bd84dd2fSRuslan Ermilov.It Va sysctl 184dba9e9ccSRobert WatsonReserved name space for the implementation of sysctl. 185bd84dd2fSRuslan Ermilov.It Va user 186dba9e9ccSRobert WatsonConfiguration settings relating to user application behavior. 187f4006affSGiorgos KeramidasGenerally, configuring applications using kernel sysctls is discouraged. 188bd84dd2fSRuslan Ermilov.It Va vfs 189dba9e9ccSRobert WatsonVirtual file system configuration and information. 190bd84dd2fSRuslan Ermilov.It Va vm 191dba9e9ccSRobert WatsonVirtual memory subsystem configuration and information. 192dba9e9ccSRobert Watson.El 193dba9e9ccSRobert Watson.Sh EXAMPLES 194dba9e9ccSRobert WatsonSample use of 195bd84dd2fSRuslan Ermilov.Fn SYSCTL_DECL 196bd84dd2fSRuslan Ermilovto declare the 197bd84dd2fSRuslan Ermilov.Va security 198bd84dd2fSRuslan Ermilovsysctl tree for use by new nodes: 199dba9e9ccSRobert Watson.Bd -literal -offset indent 200dba9e9ccSRobert WatsonSYSCTL_DECL(_security); 201dba9e9ccSRobert Watson.Ed 202dba9e9ccSRobert Watson.Pp 203dba9e9ccSRobert WatsonExamples of integer, opaque, string, and procedure sysctls follow: 204dba9e9ccSRobert Watson.Bd -literal -offset indent 205dba9e9ccSRobert Watson/* 206dba9e9ccSRobert Watson * Example of a constant integer value. Notice that the control 207dba9e9ccSRobert Watson * flags are CTLFLAG_RD, the variable pointer is NULL, and the 208dba9e9ccSRobert Watson * value is declared. 209c157a036SDavid E. O'Brien * If sysctl(8) should print this value in hex, use 'SYSCTL_XINT'. 210dba9e9ccSRobert Watson */ 211dba9e9ccSRobert WatsonSYSCTL_INT(_debug_sizeof, OID_AUTO, bio, CTLFLAG_RD, NULL, 212dba9e9ccSRobert Watson sizeof(struct bio), "sizeof(struct bio)"); 213dba9e9ccSRobert Watson 214dba9e9ccSRobert Watson/* 215dba9e9ccSRobert Watson * Example of a variable integer value. Notice that the control 216dba9e9ccSRobert Watson * flags are CTLFLAG_RW, the variable pointer is set, and the 217dba9e9ccSRobert Watson * value is 0. 218dba9e9ccSRobert Watson */ 219dba9e9ccSRobert Watsonstatic int doingcache = 1; /* 1 => enable the cache */ 220dba9e9ccSRobert WatsonSYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, 221dba9e9ccSRobert Watson "Enable name cache"); 222dba9e9ccSRobert Watson 223dba9e9ccSRobert Watson/* 224dba9e9ccSRobert Watson * Example of a variable string value. Notice that the control 225dba9e9ccSRobert Watson * flags are CTLFLAG_RW, that the variable pointer and string 226dba9e9ccSRobert Watson * size are set. Unlike newer sysctls, this older sysctl uses a 227dba9e9ccSRobert Watson * static oid number. 228dba9e9ccSRobert Watson */ 229dba9e9ccSRobert Watsonchar kernelname[MAXPATHLEN] = "/kernel"; /* XXX bloat */ 230dba9e9ccSRobert WatsonSYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, CTLFLAG_RW, 231dba9e9ccSRobert Watson kernelname, sizeof(kernelname), "Name of kernel file booted"); 232dba9e9ccSRobert Watson 233dba9e9ccSRobert Watson/* 234dba9e9ccSRobert Watson * Example of an opaque data type exported by sysctl. Notice that 235dba9e9ccSRobert Watson * the variable pointer and size are provided, as well as a format 236dba9e9ccSRobert Watson * string for sysctl(8). 237dba9e9ccSRobert Watson */ 238dba9e9ccSRobert Watsonstatic l_fp pps_freq; /* scaled frequence offset (ns/s) */ 239dba9e9ccSRobert WatsonSYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD, 240dba9e9ccSRobert Watson &pps_freq, sizeof(pps_freq), "I", ""); 241dba9e9ccSRobert Watson 242dba9e9ccSRobert Watson/* 243dba9e9ccSRobert Watson * Example of a procedure based sysctl exporting string 244dba9e9ccSRobert Watson * information. Notice that the data type is declared, the NULL 245dba9e9ccSRobert Watson * variable pointer and 0 size, the function pointer, and the 246dba9e9ccSRobert Watson * format string for sysctl(8). 247dba9e9ccSRobert Watson */ 248dba9e9ccSRobert WatsonSYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | 249dba9e9ccSRobert Watson CTLFLAG_RW, NULL, 0, sysctl_kern_timecounter_hardware, "A", 250dba9e9ccSRobert Watson ""); 251dba9e9ccSRobert Watson.Ed 25291f3a6dbSYaroslav Tykhiy.Sh SYSCTL NAMING 253dba9e9ccSRobert WatsonWhen adding, modifying, or removing sysctl names, it is important to be 254dba9e9ccSRobert Watsonaware that these interfaces may be used by users, libraries, applications, 255f4006affSGiorgos Keramidasor documentation (such as published books), and are implicitly published application interfaces. 256dba9e9ccSRobert WatsonAs with other application interfaces, caution must be taken not to break 257dba9e9ccSRobert Watsonexisting applications, and to think about future use of new name spaces so as 258dba9e9ccSRobert Watsonto avoid the need to rename or remove interfaces that might be depended on in 259dba9e9ccSRobert Watsonthe future. 26091f3a6dbSYaroslav Tykhiy.Pp 26191f3a6dbSYaroslav TykhiyThe semantics chosen for a new sysctl should be as clear as possible, 26291f3a6dbSYaroslav Tykhiyand the name of the sysctl must closely reflect its semantics. 26391f3a6dbSYaroslav TykhiyTherefore the sysctl name deserves a fair amount of consideration. 26491f3a6dbSYaroslav TykhiyIt should be short but yet representative of the sysctl meaning. 26591f3a6dbSYaroslav TykhiyIf the name consists of several words, they should be separated by 26691f3a6dbSYaroslav Tykhiyunderscore characters, as in 26791f3a6dbSYaroslav Tykhiy.Va compute_summary_at_mount . 26891f3a6dbSYaroslav TykhiyUnderscore characters may be omitted only if the name consists of not more 26991f3a6dbSYaroslav Tykhiythan two words, each being not longer than four characters, as in 27091f3a6dbSYaroslav Tykhiy.Va bootfile . 27191f3a6dbSYaroslav TykhiyFor boolean sysctls, negative logic should be totally avoided. 27291f3a6dbSYaroslav TykhiyThat is, do not use names like 27391f3a6dbSYaroslav Tykhiy.Va no_foobar 27491f3a6dbSYaroslav Tykhiyor 27591f3a6dbSYaroslav Tykhiy.Va foobar_disable . 27691f3a6dbSYaroslav TykhiyThey are confusing and lead to configuration errors. 27791f3a6dbSYaroslav TykhiyUse positive logic instead: 27891f3a6dbSYaroslav Tykhiy.Va foobar , 27991f3a6dbSYaroslav Tykhiy.Va foobar_enable . 28091f3a6dbSYaroslav Tykhiy.Pp 28191f3a6dbSYaroslav TykhiyA temporary sysctl node that should not be relied upon must be designated 28291f3a6dbSYaroslav Tykhiyas such by a leading underscore character in its name. For example: 28391f3a6dbSYaroslav Tykhiy.Va _dirty_hack . 284dba9e9ccSRobert Watson.Sh SEE ALSO 285f3066f2bSGavin Atkinson.Xr sysctl 3 , 286dba9e9ccSRobert Watson.Xr sysctl 8 , 287dba9e9ccSRobert Watson.Xr sysctl_add_oid 9 , 288dba9e9ccSRobert Watson.Xr sysctl_ctx_free 9 , 289dba9e9ccSRobert Watson.Xr sysctl_ctx_init 9 , 290dba9e9ccSRobert Watson.Xr sysctl_remove_oid 9 291dba9e9ccSRobert Watson.Sh HISTORY 292bd84dd2fSRuslan ErmilovThe 293dba9e9ccSRobert Watson.Xr sysctl 8 294bd84dd2fSRuslan Ermilovutility first appeared in 295dba9e9ccSRobert Watson.Bx 4.4 . 296dba9e9ccSRobert Watson.Sh AUTHORS 297bd84dd2fSRuslan Ermilov.An -nosplit 298bd84dd2fSRuslan ErmilovThe 299bd84dd2fSRuslan Ermilov.Nm sysctl 300bd84dd2fSRuslan Ermilovimplementation originally found in 301dba9e9ccSRobert Watson.Bx 302dba9e9ccSRobert Watsonhas been extensively rewritten by 303dba9e9ccSRobert Watson.An Poul-Henning Kamp 304dba9e9ccSRobert Watsonin order to add support for name lookups, name space iteration, and dynamic 305dba9e9ccSRobert Watsonaddition of MIB nodes. 306dba9e9ccSRobert Watson.Pp 307dba9e9ccSRobert WatsonThis man page was written by 308dba9e9ccSRobert Watson.An Robert N. M. Watson . 309