2 .\" Copyright (c) 2006 Robert N. M. Watson
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
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.
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
26 .\" $FreeBSD: src/share/man/man9/sysctl.9,v 1.3 2006/04/28 23:21:36 keramida Exp $
27 .\" $DragonFly: src/share/man/man9/sysctl.9,v 1.6 2008/09/02 11:50:46 matthias Exp $
45 .Nd Static sysctl declaration functions
156 kernel interfaces allow code to statically declare
158 MIB entries, which will be initialized when the kernel module containing the
159 declaration is initialized.
160 When the module is unloaded, the sysctl will be automatically destroyed.
162 Sysctl nodes are created in a hierarchical tree, with all static nodes being
163 represented by named C data structures; in order to create a new node under
164 an existing node in the tree, the structure representing the desired parent
165 node must be declared in the current context using
168 New nodes are declared using one of
181 Each macro accepts a parent name, as declared using
183 an OID number, typically
185 a node name, a set of control and access flags, and a description.
186 Depending on the macro, a pointer to a variable supporting the MIB entry, a
187 size, a value, and a function pointer implementing the MIB entry may also be
190 For most of the above macros, declaring a type as part of the access flags is
191 not necessary -- however, when declaring a sysctl implemented by a function,
192 including a type in the access mask is required:
193 .Bl -tag -width ".Dv CTLTYPE_STRING"
195 This is a node intended to be a parent for other nodes.
197 This is a signed integer.
198 .It Dv CTLTYPE_STRING
199 This is a nul-terminated string stored in a character array.
201 This is a 64-bit signed integer.
202 .It Dv CTLTYPE_OPAQUE
203 This is an opaque data structure.
204 .It Dv CTLTYPE_STRUCT
208 This is an unsigned integer.
210 This is a signed long.
212 This is an unsigned long.
214 This is a 64-bit unsigned integer.
217 All sysctl types except for new node declarations require one or more flags
218 to be set indicating the read and write disposition of the sysctl:
219 .Bl -tag -width ".Dv CTLFLAG_ANYBODY"
221 This is a read-only sysctl.
223 This is a writable sysctl.
225 This sysctl is readable and writable.
226 .It Dv CTLFLAG_ANYBODY
227 Any user or process can write to this sysctl.
228 .It Dv CTLFLAG_SECURE
229 This sysctl can be written to only if the effective securelevel of the
231 .It Dv CTLFLAG_PRISON
232 This sysctl can be written to by processes in
234 .\".It Dv CTLFLAG_SKIP
235 .\"When iterating the sysctl name space, do not list this sysctl.
238 When creating new sysctls, careful attention should be paid to the security
239 implications of the monitoring or management interface being created.
240 Most sysctls present in the kernel are read-only or writable only by the
242 Sysctls exporting extensive information on system data structures and
243 operation, especially those implemented using procedures, will wish to
244 implement access control to limit the undesired exposure of information about
245 other processes, network connections, etc.
247 The following top level sysctl name spaces are commonly used:
248 .Bl -tag -width ".Va machdep"
250 Compatibility layer information.
252 Debugging information.
253 Various name spaces exist under
256 Hardware and device driver information.
258 Information about the
262 Kernel behavior tuning; generally deprecated in favor of more specific
265 Machine-dependent configuration parameters.
268 Various protocols have name spaces under
271 Reserved name space for the implementation of sysctl.
273 Configuration settings relating to user application behavior.
274 Generally, configuring applications using kernel sysctls is discouraged.
276 Virtual file system configuration and information.
278 Virtual memory subsystem configuration and information.
283 to declare the "machdep" sysctl tree for use by new nodes:
284 .Bd -literal -offset indent
285 SYSCTL_DECL(_machdep);
288 Examples of integer, opaque, string, and procedure sysctls follow:
289 .Bd -literal -offset indent
291 * Example of a constant integer value. Notice that the control
292 * flags are CTLFLAG_RD, the variable pointer is NULL, and the
295 SYSCTL_INT(_debug_sizeof, OID_AUTO, bio, CTLFLAG_RD, NULL,
296 sizeof(struct bio), "sizeof(struct bio)");
299 * Example of a variable integer value. Notice that the control
300 * flags are CTLFLAG_RW, the variable pointer is set, and the
303 static int doingcache = 1; /* 1 => enable the cache */
304 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
305 "Enable name cache");
308 * Example of a variable string value. Notice that the control
309 * flags are CTLFLAG_RW, that the variable pointer and string
310 * size are set. Unlike newer sysctls, this older sysctl uses a
313 char kernelname[MAXPATHLEN] = "/boot/kernel"; /* XXX bloat */
314 SYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, CTLFLAG_RW,
315 kernelname, sizeof(kernelname), "Name of kernel file booted");
318 * Example of an opaque data type exported by sysctl. Notice that
319 * the variable pointer and size are provided, as well as a format
320 * string for sysctl(8).
322 static l_fp pps_freq; /* scaled frequence offset (ns/s) */
323 SYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD,
324 &pps_freq, sizeof(pps_freq), "I", "");
327 * Example of a procedure based sysctl exporting string
328 * information. Notice that the data type is declared, the NULL
329 * variable pointer and 0 size, the function pointer, and the
330 * format string for sysctl(8).
332 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
333 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
336 When adding, modifying, or removing sysctl names, it is important to be
337 aware that these interfaces may be used by users, libraries, applications,
338 or documentation (such as published books), and are implicitly published
339 application interfaces.
340 As with other application interfaces, caution must be taken not to break
341 existing applications, and to think about future use of new name spaces so as
342 to avoid the need to rename or remove interfaces that might be depended on in
346 .Xr sysctl_add_oid 9 ,
347 .Xr sysctl_ctx_free 9 ,
348 .Xr sysctl_ctx_init 9 ,
349 .Xr sysctl_remove_oid 9
356 The sysctl implementation originally found in
358 has been extensively rewritten by
359 .An Poul-Henning Kamp
360 in order to add support for name lookups, name space iteration, and dynamic
361 addition of MIB nodes.
363 This man page was written by
364 .An Robert N. M. Watson .