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 , 427f41115eSPawel Jakub Dawidek.Nm SYSCTL_QUAD 43dba9e9ccSRobert Watson.Nd Static sysctl declaration functions 44dba9e9ccSRobert Watson.Sh SYNOPSIS 45dba9e9ccSRobert Watson.In sys/types.h 46dba9e9ccSRobert Watson.In sys/sysctl.h 47bd84dd2fSRuslan Ermilov.Fn SYSCTL_DECL name 48bd84dd2fSRuslan Ermilov.Fn SYSCTL_INT parent nbr name access ptr val descr 49bd84dd2fSRuslan Ermilov.Fn SYSCTL_LONG parent nbr name access ptr val descr 50bd84dd2fSRuslan Ermilov.Fn SYSCTL_NODE parent nbr name access handler descr 51bd84dd2fSRuslan Ermilov.Fn SYSCTL_OPAQUE parent nbr name access ptr len fmt descr 52bd84dd2fSRuslan Ermilov.Fn SYSCTL_PROC parent nbr name access ptr arg handler fmt descr 53bd84dd2fSRuslan Ermilov.Fn SYSCTL_STRING parent nbr name access arg len descr 54bd84dd2fSRuslan Ermilov.Fn SYSCTL_STRUCT parent nbr name access ptr type descr 55bd84dd2fSRuslan Ermilov.Fn SYSCTL_UINT parent nbr name access ptr val descr 56bd84dd2fSRuslan Ermilov.Fn SYSCTL_ULONG parent nbr name access ptr val descr 577f41115eSPawel Jakub Dawidek.Fn SYSCTL_QUAD parent nbr name access ptr val descr 58dba9e9ccSRobert Watson.Sh DESCRIPTION 59dba9e9ccSRobert WatsonThe 60bd84dd2fSRuslan Ermilov.Nm SYSCTL 61dba9e9ccSRobert Watsonkernel interfaces allow code to statically declare 62dba9e9ccSRobert Watson.Xr sysctl 8 63dba9e9ccSRobert WatsonMIB entries, which will be initialized when the kernel module containing the 64dba9e9ccSRobert Watsondeclaration is initialized. 65dba9e9ccSRobert WatsonWhen the module is unloaded, the sysctl will be automatically destroyed. 66dba9e9ccSRobert Watson.Pp 67f4006affSGiorgos KeramidasSysctl nodes are created in a hierarchical tree, with all static nodes being 68dba9e9ccSRobert Watsonrepresented by named C data structures; in order to create a new node under 69dba9e9ccSRobert Watsonan existing node in the tree, the structure representing the desired parent 70dba9e9ccSRobert Watsonnode must be declared in the current context using 71dba9e9ccSRobert Watson.Fn SYSCTL_DECL . 72dba9e9ccSRobert Watson.Pp 73dba9e9ccSRobert WatsonNew nodes are declared using one of 74bd84dd2fSRuslan Ermilov.Fn SYSCTL_INT , 75bd84dd2fSRuslan Ermilov.Fn SYSCTL_LONG , 76bd84dd2fSRuslan Ermilov.Fn SYSCTL_NODE , 77bd84dd2fSRuslan Ermilov.Fn SYSCTL_OPAQUE , 78bd84dd2fSRuslan Ermilov.Fn SYSCTL_PROC , 79bd84dd2fSRuslan Ermilov.Fn SYSCTL_STRING , 80bd84dd2fSRuslan Ermilov.Fn SYSCTL_STRUCT , 81bd84dd2fSRuslan Ermilov.Fn SYSCTL_UINT , 82bd84dd2fSRuslan Ermilov.Fn SYSCTL_ULONG , 83dba9e9ccSRobert Watsonand 847f41115eSPawel Jakub Dawidek.Fn SYSCTL_QUAD . 85dba9e9ccSRobert WatsonEach macro accepts a parent name, as declared using 86bd84dd2fSRuslan Ermilov.Fn SYSCTL_DECL , 87dba9e9ccSRobert Watsonan OID number, typically 88dba9e9ccSRobert Watson.Dv OID_AUTO , 89dba9e9ccSRobert Watsona node name, a set of control and access flags, and a description. 90dba9e9ccSRobert WatsonDepending on the macro, a pointer to a variable supporting the MIB entry, a 91dba9e9ccSRobert Watsonsize, a value, and a function pointer implementing the MIB entry may also be 92dba9e9ccSRobert Watsonpresent. 93dba9e9ccSRobert Watson.Pp 94dba9e9ccSRobert WatsonFor most of the above macros, declaring a type as part of the access flags is 95bd84dd2fSRuslan Ermilovnot necessary \[em] however, when declaring a sysctl implemented by a function, 96dba9e9ccSRobert Watsonincluding a type in the access mask is required: 97bd84dd2fSRuslan Ermilov.Bl -tag -width ".Dv CTLTYPE_STRING" 98dba9e9ccSRobert Watson.It Dv CTLTYPE_NODE 99dba9e9ccSRobert WatsonThis is a node intended to be a parent for other nodes. 100dba9e9ccSRobert Watson.It Dv CTLTYPE_INT 101dba9e9ccSRobert WatsonThis is a signed integer. 102dba9e9ccSRobert Watson.It Dv CTLTYPE_STRING 103dba9e9ccSRobert WatsonThis is a nul-terminated string stored in a character array. 104*cbc134adSMatthew D Fleming.It Dv CTLTYPE_S64 105dba9e9ccSRobert WatsonThis is a 64-bit signed integer. 106dba9e9ccSRobert Watson.It Dv CTLTYPE_OPAQUE 107dba9e9ccSRobert WatsonThis is an opaque data structure. 108dba9e9ccSRobert Watson.It Dv CTLTYPE_STRUCT 109dba9e9ccSRobert WatsonAlias for 110dba9e9ccSRobert Watson.Dv CTLTYPE_OPAQUE. 111dba9e9ccSRobert Watson.It Dv CTLTYPE_UINT 112dba9e9ccSRobert WatsonThis is an unsigned integer. 113dba9e9ccSRobert Watson.It Dv CTLTYPE_LONG 114dba9e9ccSRobert WatsonThis is a signed long. 115dba9e9ccSRobert Watson.It Dv CTLTYPE_ULONG 1168a1f4129SRobert WatsonThis is an unsigned long. 117*cbc134adSMatthew D Fleming.It Dv CTLTYPE_U64 118*cbc134adSMatthew D FlemingThis is a 64-bit unsigned integer. 119dba9e9ccSRobert Watson.El 120dba9e9ccSRobert Watson.Pp 121dba9e9ccSRobert WatsonAll sysctl types except for new node declarations require one or more flags 122dba9e9ccSRobert Watsonto be set indicating the read and write disposition of the sysctl: 123bd84dd2fSRuslan Ermilov.Bl -tag -width ".Dv CTLFLAG_ANYBODY" 124dba9e9ccSRobert Watson.It Dv CTLFLAG_RD 125dba9e9ccSRobert WatsonThis is a read-only sysctl. 126b470b929SDavid E. O'Brien.It Dv CTLFLAG_WR 127dba9e9ccSRobert WatsonThis is a writable sysctl. 128dba9e9ccSRobert Watson.It Dv CTLFLAG_RW 129dba9e9ccSRobert WatsonThis sysctl is readable and writable. 130dba9e9ccSRobert Watson.It Dv CTLFLAG_ANYBODY 131dba9e9ccSRobert WatsonAny user or process can write to this sysctl. 132dba9e9ccSRobert Watson.It Dv CTLFLAG_SECURE 133dba9e9ccSRobert WatsonThis sysctl can be written to only if the effective securelevel of the 134bd84dd2fSRuslan Ermilovprocess is \[<=] 0. 135dba9e9ccSRobert Watson.It Dv CTLFLAG_PRISON 136dba9e9ccSRobert WatsonThis sysctl can be written to by processes in 137dba9e9ccSRobert Watson.Xr jail 2 . 138dba9e9ccSRobert Watson.It Dv CTLFLAG_SKIP 139dba9e9ccSRobert WatsonWhen iterating the sysctl name space, do not list this sysctl. 140dba9e9ccSRobert Watson.It Dv CTLFLAG_TUN 141dba9e9ccSRobert WatsonAlso declare a system tunable with the same name to initialize this variable. 142dba9e9ccSRobert Watson.It Dv CTLFLAG_RDTUN 1439280e5faSMike PritchardAlso declare a system tunable with the same name to initialize this variable; 144dba9e9ccSRobert Watsonhowever, the run-time variable is read-only. 145dba9e9ccSRobert Watson.El 146dba9e9ccSRobert Watson.Pp 147dba9e9ccSRobert WatsonWhen creating new sysctls, careful attention should be paid to the security 148dba9e9ccSRobert Watsonimplications of the monitoring or management interface being created. 149dba9e9ccSRobert WatsonMost sysctls present in the kernel are read-only or writable only by the 150dba9e9ccSRobert Watsonsuperuser. 151dba9e9ccSRobert WatsonSysctls exporting extensive information on system data structures and 152dba9e9ccSRobert Watsonoperation, especially those implemented using procedures, will wish to 153dba9e9ccSRobert Watsonimplement access control to limit the undesired exposure of information about 154dba9e9ccSRobert Watsonother processes, network connections, etc. 155dba9e9ccSRobert Watson.Pp 156dba9e9ccSRobert WatsonThe following top level sysctl name spaces are commonly used: 157bd84dd2fSRuslan Ermilov.Bl -tag -width ".Va regression" 158bd84dd2fSRuslan Ermilov.It Va compat 159dba9e9ccSRobert WatsonCompatibility layer information. 160bd84dd2fSRuslan Ermilov.It Va debug 161dba9e9ccSRobert WatsonDebugging information. 162dba9e9ccSRobert WatsonVarious name spaces exist under 163bd84dd2fSRuslan Ermilov.Va debug . 164bd84dd2fSRuslan Ermilov.It Va hw 165dba9e9ccSRobert WatsonHardware and device driver information. 166bd84dd2fSRuslan Ermilov.It Va kern 167dba9e9ccSRobert WatsonKernel behavior tuning; generally deprecated in favor of more specific 168dba9e9ccSRobert Watsonname spaces. 169bd84dd2fSRuslan Ermilov.It Va machdep 170dba9e9ccSRobert WatsonMachine-dependent configuration parameters. 171bd84dd2fSRuslan Ermilov.It Va net 172dba9e9ccSRobert WatsonNetwork subsystem. 173dba9e9ccSRobert WatsonVarious protocols have name spaces under 174bd84dd2fSRuslan Ermilov.Va net . 175bd84dd2fSRuslan Ermilov.It Va regression 176dba9e9ccSRobert WatsonRegression test configuration and information. 177bd84dd2fSRuslan Ermilov.It Va security 178f4006affSGiorgos KeramidasSecurity and security-policy configuration and information. 179bd84dd2fSRuslan Ermilov.It Va sysctl 180dba9e9ccSRobert WatsonReserved name space for the implementation of sysctl. 181bd84dd2fSRuslan Ermilov.It Va user 182dba9e9ccSRobert WatsonConfiguration settings relating to user application behavior. 183f4006affSGiorgos KeramidasGenerally, configuring applications using kernel sysctls is discouraged. 184bd84dd2fSRuslan Ermilov.It Va vfs 185dba9e9ccSRobert WatsonVirtual file system configuration and information. 186bd84dd2fSRuslan Ermilov.It Va vm 187dba9e9ccSRobert WatsonVirtual memory subsystem configuration and information. 188dba9e9ccSRobert Watson.El 189dba9e9ccSRobert Watson.Sh EXAMPLES 190dba9e9ccSRobert WatsonSample use of 191bd84dd2fSRuslan Ermilov.Fn SYSCTL_DECL 192bd84dd2fSRuslan Ermilovto declare the 193bd84dd2fSRuslan Ermilov.Va security 194bd84dd2fSRuslan Ermilovsysctl tree for use by new nodes: 195dba9e9ccSRobert Watson.Bd -literal -offset indent 196dba9e9ccSRobert WatsonSYSCTL_DECL(_security); 197dba9e9ccSRobert Watson.Ed 198dba9e9ccSRobert Watson.Pp 199dba9e9ccSRobert WatsonExamples of integer, opaque, string, and procedure sysctls follow: 200dba9e9ccSRobert Watson.Bd -literal -offset indent 201dba9e9ccSRobert Watson/* 202dba9e9ccSRobert Watson * Example of a constant integer value. Notice that the control 203dba9e9ccSRobert Watson * flags are CTLFLAG_RD, the variable pointer is NULL, and the 204dba9e9ccSRobert Watson * value is declared. 205dba9e9ccSRobert Watson */ 206dba9e9ccSRobert WatsonSYSCTL_INT(_debug_sizeof, OID_AUTO, bio, CTLFLAG_RD, NULL, 207dba9e9ccSRobert Watson sizeof(struct bio), "sizeof(struct bio)"); 208dba9e9ccSRobert Watson 209dba9e9ccSRobert Watson/* 210dba9e9ccSRobert Watson * Example of a variable integer value. Notice that the control 211dba9e9ccSRobert Watson * flags are CTLFLAG_RW, the variable pointer is set, and the 212dba9e9ccSRobert Watson * value is 0. 213dba9e9ccSRobert Watson */ 214dba9e9ccSRobert Watsonstatic int doingcache = 1; /* 1 => enable the cache */ 215dba9e9ccSRobert WatsonSYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, 216dba9e9ccSRobert Watson "Enable name cache"); 217dba9e9ccSRobert Watson 218dba9e9ccSRobert Watson/* 219dba9e9ccSRobert Watson * Example of a variable string value. Notice that the control 220dba9e9ccSRobert Watson * flags are CTLFLAG_RW, that the variable pointer and string 221dba9e9ccSRobert Watson * size are set. Unlike newer sysctls, this older sysctl uses a 222dba9e9ccSRobert Watson * static oid number. 223dba9e9ccSRobert Watson */ 224dba9e9ccSRobert Watsonchar kernelname[MAXPATHLEN] = "/kernel"; /* XXX bloat */ 225dba9e9ccSRobert WatsonSYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, CTLFLAG_RW, 226dba9e9ccSRobert Watson kernelname, sizeof(kernelname), "Name of kernel file booted"); 227dba9e9ccSRobert Watson 228dba9e9ccSRobert Watson/* 229dba9e9ccSRobert Watson * Example of an opaque data type exported by sysctl. Notice that 230dba9e9ccSRobert Watson * the variable pointer and size are provided, as well as a format 231dba9e9ccSRobert Watson * string for sysctl(8). 232dba9e9ccSRobert Watson */ 233dba9e9ccSRobert Watsonstatic l_fp pps_freq; /* scaled frequence offset (ns/s) */ 234dba9e9ccSRobert WatsonSYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD, 235dba9e9ccSRobert Watson &pps_freq, sizeof(pps_freq), "I", ""); 236dba9e9ccSRobert Watson 237dba9e9ccSRobert Watson/* 238dba9e9ccSRobert Watson * Example of a procedure based sysctl exporting string 239dba9e9ccSRobert Watson * information. Notice that the data type is declared, the NULL 240dba9e9ccSRobert Watson * variable pointer and 0 size, the function pointer, and the 241dba9e9ccSRobert Watson * format string for sysctl(8). 242dba9e9ccSRobert Watson */ 243dba9e9ccSRobert WatsonSYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | 244dba9e9ccSRobert Watson CTLFLAG_RW, NULL, 0, sysctl_kern_timecounter_hardware, "A", 245dba9e9ccSRobert Watson ""); 246dba9e9ccSRobert Watson.Ed 24791f3a6dbSYaroslav Tykhiy.Sh SYSCTL NAMING 248dba9e9ccSRobert WatsonWhen adding, modifying, or removing sysctl names, it is important to be 249dba9e9ccSRobert Watsonaware that these interfaces may be used by users, libraries, applications, 250f4006affSGiorgos Keramidasor documentation (such as published books), and are implicitly published application interfaces. 251dba9e9ccSRobert WatsonAs with other application interfaces, caution must be taken not to break 252dba9e9ccSRobert Watsonexisting applications, and to think about future use of new name spaces so as 253dba9e9ccSRobert Watsonto avoid the need to rename or remove interfaces that might be depended on in 254dba9e9ccSRobert Watsonthe future. 25591f3a6dbSYaroslav Tykhiy.Pp 25691f3a6dbSYaroslav TykhiyThe semantics chosen for a new sysctl should be as clear as possible, 25791f3a6dbSYaroslav Tykhiyand the name of the sysctl must closely reflect its semantics. 25891f3a6dbSYaroslav TykhiyTherefore the sysctl name deserves a fair amount of consideration. 25991f3a6dbSYaroslav TykhiyIt should be short but yet representative of the sysctl meaning. 26091f3a6dbSYaroslav TykhiyIf the name consists of several words, they should be separated by 26191f3a6dbSYaroslav Tykhiyunderscore characters, as in 26291f3a6dbSYaroslav Tykhiy.Va compute_summary_at_mount . 26391f3a6dbSYaroslav TykhiyUnderscore characters may be omitted only if the name consists of not more 26491f3a6dbSYaroslav Tykhiythan two words, each being not longer than four characters, as in 26591f3a6dbSYaroslav Tykhiy.Va bootfile . 26691f3a6dbSYaroslav TykhiyFor boolean sysctls, negative logic should be totally avoided. 26791f3a6dbSYaroslav TykhiyThat is, do not use names like 26891f3a6dbSYaroslav Tykhiy.Va no_foobar 26991f3a6dbSYaroslav Tykhiyor 27091f3a6dbSYaroslav Tykhiy.Va foobar_disable . 27191f3a6dbSYaroslav TykhiyThey are confusing and lead to configuration errors. 27291f3a6dbSYaroslav TykhiyUse positive logic instead: 27391f3a6dbSYaroslav Tykhiy.Va foobar , 27491f3a6dbSYaroslav Tykhiy.Va foobar_enable . 27591f3a6dbSYaroslav Tykhiy.Pp 27691f3a6dbSYaroslav TykhiyA temporary sysctl node that should not be relied upon must be designated 27791f3a6dbSYaroslav Tykhiyas such by a leading underscore character in its name. For example: 27891f3a6dbSYaroslav Tykhiy.Va _dirty_hack . 279dba9e9ccSRobert Watson.Sh SEE ALSO 280f3066f2bSGavin Atkinson.Xr sysctl 3 , 281dba9e9ccSRobert Watson.Xr sysctl 8 , 282dba9e9ccSRobert Watson.Xr sysctl_add_oid 9 , 283dba9e9ccSRobert Watson.Xr sysctl_ctx_free 9 , 284dba9e9ccSRobert Watson.Xr sysctl_ctx_init 9 , 285dba9e9ccSRobert Watson.Xr sysctl_remove_oid 9 286dba9e9ccSRobert Watson.Sh HISTORY 287bd84dd2fSRuslan ErmilovThe 288dba9e9ccSRobert Watson.Xr sysctl 8 289bd84dd2fSRuslan Ermilovutility first appeared in 290dba9e9ccSRobert Watson.Bx 4.4 . 291dba9e9ccSRobert Watson.Sh AUTHORS 292bd84dd2fSRuslan Ermilov.An -nosplit 293bd84dd2fSRuslan ErmilovThe 294bd84dd2fSRuslan Ermilov.Nm sysctl 295bd84dd2fSRuslan Ermilovimplementation originally found in 296dba9e9ccSRobert Watson.Bx 297dba9e9ccSRobert Watsonhas been extensively rewritten by 298dba9e9ccSRobert Watson.An Poul-Henning Kamp 299dba9e9ccSRobert Watsonin order to add support for name lookups, name space iteration, and dynamic 300dba9e9ccSRobert Watsonaddition of MIB nodes. 301dba9e9ccSRobert Watson.Pp 302dba9e9ccSRobert WatsonThis man page was written by 303dba9e9ccSRobert Watson.An Robert N. M. Watson . 304