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
53 .Nd Static sysctl declaration functions
256 kernel interfaces allow code to statically declare
258 MIB entries, which will be initialized when the kernel module containing the
259 declaration is initialized.
260 When the module is unloaded, the sysctl will be automatically destroyed.
262 Sysctl nodes are created in a hierarchical tree, with all static nodes being
263 represented by named C data structures; in order to create a new node under
264 an existing node in the tree, the structure representing the desired parent
265 node must be declared in the current context using
268 New nodes are declared using one of
291 Each macro accepts a parent name, as declared using
293 an OID number, typically
295 a node name, a set of control and access flags, and a description.
296 Depending on the macro, a pointer to a variable supporting the MIB entry, a
297 size, a value, and a function pointer implementing the MIB entry may also be
300 For most of the above macros, declaring a type as part of the access flags is
301 not necessary -- however, when declaring a sysctl implemented by a function,
302 including a type in the access mask is required:
303 .Bl -tag -width ".Dv CTLTYPE_STRING"
305 This is a node intended to be a parent for other nodes.
307 This is a signed integer.
309 This is an 8-bit signed integer.
311 This is a 16-bit signed integer.
313 This is a 32-bit signed integer.
315 This is a 64-bit signed integer.
316 .It Dv CTLTYPE_STRING
317 This is a nul-terminated string stored in a character array.
319 This is a 64-bit signed integer.
320 .It Dv CTLTYPE_OPAQUE
321 This is an opaque data structure.
322 .It Dv CTLTYPE_STRUCT
326 This is an 8-bit unsigned integer.
328 This is a 16-bit unsigned integer.
330 This is a 32-bit unsigned integer.
332 This is a 64-bit unsigned integer.
334 This is an unsigned integer.
336 This is a signed long.
338 This is an unsigned long.
340 This is a 64-bit unsigned integer.
343 All sysctl types except for new node declarations require one or more flags
344 to be set indicating the read and write disposition of the sysctl:
345 .Bl -tag -width ".Dv CTLFLAG_ANYBODY"
347 This is a read-only sysctl.
349 This is a writable sysctl.
351 This sysctl is readable and writable.
352 .It Dv CTLFLAG_ANYBODY
353 Any user or process can write to this sysctl.
354 .It Dv CTLFLAG_SECURE
355 This sysctl can be written to only if the effective securelevel of the
357 .It Dv CTLFLAG_PRISON
358 This sysctl can be written to by processes in
360 .\".It Dv CTLFLAG_SKIP
361 .\"When iterating the sysctl name space, do not list this sysctl.
364 When creating new sysctls, careful attention should be paid to the security
365 implications of the monitoring or management interface being created.
366 Most sysctls present in the kernel are read-only or writable only by the
368 Sysctls exporting extensive information on system data structures and
369 operation, especially those implemented using procedures, will wish to
370 implement access control to limit the undesired exposure of information about
371 other processes, network connections, etc.
373 The following top level sysctl name spaces are commonly used:
374 .Bl -tag -width ".Va machdep"
376 Compatibility layer information.
378 Debugging information.
379 Various name spaces exist under
382 Hardware and device driver information.
384 Information about the
388 Kernel behavior tuning; generally deprecated in favor of more specific
391 Machine-dependent configuration parameters.
394 Various protocols have name spaces under
397 Reserved name space for the implementation of sysctl.
399 Configuration settings relating to user application behavior.
400 Generally, configuring applications using kernel sysctls is discouraged.
402 Virtual file system configuration and information.
404 Virtual memory subsystem configuration and information.
409 to declare the "machdep" sysctl tree for use by new nodes:
410 .Bd -literal -offset indent
411 SYSCTL_DECL(_machdep);
414 Examples of integer, opaque, string, and procedure sysctls follow:
415 .Bd -literal -offset indent
417 * Example of a constant integer value. Notice that the control
418 * flags are CTLFLAG_RD, the variable pointer is NULL, and the
421 SYSCTL_INT(_debug_sizeof, OID_AUTO, bio, CTLFLAG_RD, NULL,
422 sizeof(struct bio), "sizeof(struct bio)");
425 * Example of a variable integer value. Notice that the control
426 * flags are CTLFLAG_RW, the variable pointer is set, and the
429 static int doingcache = 1; /* 1 => enable the cache */
430 SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
431 "Enable name cache");
434 * Example of a variable string value. Notice that the control
435 * flags are CTLFLAG_RW, that the variable pointer and string
436 * size are set. Unlike newer sysctls, this older sysctl uses a
439 char kernelname[MAXPATHLEN] = "/boot/kernel"; /* XXX bloat */
440 SYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, CTLFLAG_RW,
441 kernelname, sizeof(kernelname), "Name of kernel file booted");
444 * Example of an opaque data type exported by sysctl. Notice that
445 * the variable pointer and size are provided, as well as a format
446 * string for sysctl(8).
448 static l_fp pps_freq; /* scaled frequency offset (ns/s) */
449 SYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD,
450 &pps_freq, sizeof(pps_freq), "I", "");
453 * Example of a procedure based sysctl exporting string
454 * information. Notice that the data type is declared, the NULL
455 * variable pointer and 0 size, the function pointer, and the
456 * format string for sysctl(8).
458 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
459 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
462 When adding, modifying, or removing sysctl names, it is important to be
463 aware that these interfaces may be used by users, libraries, applications,
464 or documentation (such as published books), and are implicitly published
465 application interfaces.
466 As with other application interfaces, caution must be taken not to break
467 existing applications, and to think about future use of new name spaces so as
468 to avoid the need to rename or remove interfaces that might be depended on in
472 .Xr sysctl_add_oid 9 ,
473 .Xr sysctl_ctx_free 9 ,
474 .Xr sysctl_ctx_init 9 ,
475 .Xr sysctl_remove_oid 9
482 The sysctl implementation originally found in
484 has been extensively rewritten by
485 .An Poul-Henning Kamp
486 in order to add support for name lookups, name space iteration, and dynamic
487 addition of MIB nodes.
489 This man page was written by
490 .An Robert N. M. Watson .