xref: /freebsd/share/man/man9/sysctl.9 (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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$
27.\"
28.Dd April 28, 2006
29.Dt SYSCTL 9
30.Os
31.Sh NAME
32.Nm SYSCTL_DECL ,
33.Nm SYSCTL_INT ,
34.Nm SYSCTL_LONG ,
35.Nm SYSCTL_NODE ,
36.Nm SYSCTL_OPAQUE ,
37.Nm SYSCTL_PROC ,
38.Nm SYSCTL_STRING ,
39.Nm SYSCTL_STRUCT ,
40.Nm SYSCTL_UINT ,
41.Nm SYSCTL_ULONG
42.Nd Static sysctl declaration functions
43.Sh SYNOPSIS
44.In sys/types.h
45.In sys/sysctl.h
46.Fo SYSCTL_DECL
47.Fa "name"
48.Fc
49.Fo SYSCTL_INT
50.Fa "parent"
51.Fa "nbr"
52.Fa "name"
53.Fa "access"
54.Fa "ptr"
55.Fa "val"
56.Fa "descr"
57.Fc
58.Fo SYSCTL_LONG
59.Fa "parent"
60.Fa "nbr"
61.Fa "name"
62.Fa "access"
63.Fa "ptr"
64.Fa "val"
65.Fa "descr"
66.Fc
67.Fo SYSCTL_NODE
68.Fa "parent"
69.Fa "nbr"
70.Fa "name"
71.Fa "access"
72.Fa "handler"
73.Fa "descr"
74.Fc
75.Fo SYSCTL_OPAQUE
76.Fa "parent"
77.Fa "nbr"
78.Fa "name"
79.Fa "access"
80.Fa "ptr"
81.Fa "len"
82.Fa "fmt"
83.Fa "descr"
84.Fc
85.Fo SYSCTL_PROC
86.Fa "parent"
87.Fa "nbr"
88.Fa "name"
89.Fa "access"
90.Fa "ptr"
91.Fa "arg"
92.Fa "handler"
93.Fa "fmt"
94.Fa "descr"
95.Fc
96.Fo SYSCTL_STRING
97.Fa "parent"
98.Fa "nbr"
99.Fa "name"
100.Fa "access"
101.Fa "arg"
102.Fa "len"
103.Fa "descr"
104.Fc
105.Fo SYSCTL_STRUCT
106.Fa "parent"
107.Fa "nbr"
108.Fa "name"
109.Fa "access"
110.Fa "ptr"
111.Fa "type"
112.Fa "descr"
113.Fc
114.Fo SYSCTL_UINT
115.Fa "parent"
116.Fa "nbr"
117.Fa "name"
118.Fa "access"
119.Fa "ptr"
120.Fa "val"
121.Fa "descr"
122.Fc
123.Fo SYSCTL_ULONG
124.Fa "parent"
125.Fa "nbr"
126.Fa "name"
127.Fa "access"
128.Fa "ptr"
129.Fa "val"
130.Fa "descr"
131.Fc
132.Sh DESCRIPTION
133The
134.Nm
135kernel interfaces allow code to statically declare
136.Xr sysctl 8
137MIB entries, which will be initialized when the kernel module containing the
138declaration is initialized.
139When the module is unloaded, the sysctl will be automatically destroyed.
140.Pp
141Sysctl nodes are created in a hierarchical tree, with all static nodes being
142represented by named C data structures; in order to create a new node under
143an existing node in the tree, the structure representing the desired parent
144node must be declared in the current context using
145.Fn SYSCTL_DECL .
146.Pp
147New nodes are declared using one of
148.Nm SYSCTL_INT ,
149.Nm SYSCTL_LONG ,
150.Nm SYSCTL_NODE ,
151.Nm SYSCTL_OPAQUE ,
152.Nm SYSCTL_PROC ,
153.Nm SYSCTL_STRING ,
154.Nm SYSCTL_STRUCT ,
155.Nm SYSCTL_UINT ,
156and
157.Nm SYSCTL_ULONG .
158Each macro accepts a parent name, as declared using
159.Nm SYSCTL_DECL ,
160an OID number, typically
161.Dv OID_AUTO ,
162a node name, a set of control and access flags, and a description.
163Depending on the macro, a pointer to a variable supporting the MIB entry, a
164size, a value, and a function pointer implementing the MIB entry may also be
165present.
166.Pp
167For most of the above macros, declaring a type as part of the access flags is
168not necessary -- however, when declaring a sysctl implemented by a function,
169including a type in the access mask is required:
170.Bl -tag -width CTLTYPE_STRING
171.It Dv CTLTYPE_NODE
172This is a node intended to be a parent for other nodes.
173.It Dv CTLTYPE_INT
174This is a signed integer.
175.It Dv CTLTYPE_STRING
176This is a nul-terminated string stored in a character array.
177.It Dv CTLTYPE_QUAD
178This is a 64-bit signed integer.
179.It Dv CTLTYPE_OPAQUE
180This is an opaque data structure.
181.It Dv CTLTYPE_STRUCT
182Alias for
183.Dv CTLTYPE_OPAQUE.
184.It Dv CTLTYPE_UINT
185This is an unsigned integer.
186.It Dv CTLTYPE_LONG
187This is a signed long.
188.It Dv CTLTYPE_ULONG
189This is an unsigned long.
190.El
191.Pp
192All sysctl types except for new node declarations require one or more flags
193to be set indicating the read and write disposition of the sysctl:
194.Bl -tag -width CTLFLAG_ANYBODY
195.It Dv CTLFLAG_RD
196This is a read-only sysctl.
197It Dv CTLFLAG_WR
198This is a writable sysctl.
199.It Dv CTLFLAG_RW
200This sysctl is readable and writable.
201.It Dv CTLFLAG_ANYBODY
202Any user or process can write to this sysctl.
203.It Dv CTLFLAG_SECURE
204This sysctl can be written to only if the effective securelevel of the
205process is <= 0.
206.It Dv CTLFLAG_PRISON
207This sysctl can be written to by processes in
208.Xr jail 2 .
209.It Dv CTLFLAG_SKIP
210When iterating the sysctl name space, do not list this sysctl.
211.It Dv CTLFLAG_TUN
212Also declare a system tunable with the same name to initialize this variable.
213.It Dv CTLFLAG_RDTUN
214Also declare a system tunable with the same name to initalize this variable;
215however, the run-time variable is read-only.
216.El
217.Pp
218When creating new sysctls, careful attention should be paid to the security
219implications of the monitoring or management interface being created.
220Most sysctls present in the kernel are read-only or writable only by the
221superuser.
222Sysctls exporting extensive information on system data structures and
223operation, especially those implemented using procedures, will wish to
224implement access control to limit the undesired exposure of information about
225other processes, network connections, etc.
226.Pp
227The following top level sysctl name spaces are commonly used:
228.Bl -tag -width regression
229.It Dv compat
230Compatibility layer information.
231.It Dv debug
232Debugging information.
233Various name spaces exist under
234.Dv debug .
235.It Dv hw
236Hardware and device driver information.
237.It Dv kern
238Kernel behavior tuning; generally deprecated in favor of more specific
239name spaces.
240.It Dv machdep
241Machine-dependent configuration parameters.
242.It Dv net
243Network subsystem.
244Various protocols have name spaces under
245.Dv net .
246.It Dv regression
247Regression test configuration and information.
248.It Dv security
249Security and security-policy configuration and information.
250.It Dv sysctl
251Reserved name space for the implementation of sysctl.
252.It Dv user
253Configuration settings relating to user application behavior.
254Generally, configuring applications using kernel sysctls is discouraged.
255.It Dv vfs
256Virtual file system configuration and information.
257.It Dv vm
258Virtual memory subsystem configuration and information.
259.El
260.Sh EXAMPLES
261Sample use of
262.Nm SYSCTL_DECL
263to declare the "security" sysctl tree for use by new nodes:
264.Bd -literal -offset indent
265SYSCTL_DECL(_security);
266.Ed
267.Pp
268Examples of integer, opaque, string, and procedure sysctls follow:
269.Bd -literal -offset indent
270/*
271 * Example of a constant integer value.  Notice that the control
272 * flags are CTLFLAG_RD, the variable pointer is NULL, and the
273 * value is declared.
274 */
275SYSCTL_INT(_debug_sizeof, OID_AUTO, bio, CTLFLAG_RD, NULL,
276    sizeof(struct bio), "sizeof(struct bio)");
277
278/*
279 * Example of a variable integer value.  Notice that the control
280 * flags are CTLFLAG_RW, the variable pointer is set, and the
281 * value is 0.
282 */
283static int	doingcache = 1;		/* 1 => enable the cache */
284SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
285    "Enable name cache");
286
287/*
288 * Example of a variable string value.  Notice that the control
289 * flags are CTLFLAG_RW, that the variable pointer and string
290 * size are set.  Unlike newer sysctls, this older sysctl uses a
291 * static oid number.
292 */
293char kernelname[MAXPATHLEN] = "/kernel";	/* XXX bloat */
294SYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, CTLFLAG_RW,
295    kernelname, sizeof(kernelname), "Name of kernel file booted");
296
297/*
298 * Example of an opaque data type exported by sysctl.  Notice that
299 * the variable pointer and size are provided, as well as a format
300 * string for sysctl(8).
301 */
302static l_fp pps_freq;	/* scaled frequence offset (ns/s) */
303SYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD,
304    &pps_freq, sizeof(pps_freq), "I", "");
305
306/*
307 * Example of a procedure based sysctl exporting string
308 * information.  Notice that the data type is declared, the NULL
309 * variable pointer and 0 size, the function pointer, and the
310 * format string for sysctl(8).
311 */
312SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING |
313    CTLFLAG_RW, NULL, 0, sysctl_kern_timecounter_hardware, "A",
314    "");
315.Ed
316.Pp
317When adding, modifying, or removing sysctl names, it is important to be
318aware that these interfaces may be used by users, libraries, applications,
319or documentation (such as published books), and are implicitly published application interfaces.
320As with other application interfaces, caution must be taken not to break
321existing applications, and to think about future use of new name spaces so as
322to avoid the need to rename or remove interfaces that might be depended on in
323the future.
324.Sh SEE ALSO
325.Xr sysctl 8 ,
326.Xr sysctl_add_oid 9 ,
327.Xr sysctl_ctx_free 9 ,
328.Xr sysctl_ctx_init 9 ,
329.Xr sysctl_remove_oid 9
330.Sh HISTORY
331.Xr sysctl 8
332first appeared in
333.Bx 4.4 .
334.Sh AUTHORS
335The sysctl implementation originally found in
336.Bx
337has been extensively rewritten by
338.An Poul-Henning Kamp
339in order to add support for name lookups, name space iteration, and dynamic
340addition of MIB nodes.
341.Pp
342This man page was written by
343.An Robert N. M. Watson .
344