xref: /dragonfly/share/man/man9/sysctl.9 (revision 7d3e9a5b)
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.\"
27.Dd December 2, 2019
28.Dt SYSCTL 9
29.Os
30.Sh NAME
31.Nm SYSCTL_BIT32 ,
32.Nm SYSCTL_BIT64 ,
33.Nm SYSCTL_DECL ,
34.Nm SYSCTL_INT ,
35.Nm SYSCTL_LONG ,
36.Nm SYSCTL_QUAD ,
37.Nm SYSCTL_NODE ,
38.Nm SYSCTL_OPAQUE ,
39.Nm SYSCTL_PROC ,
40.Nm SYSCTL_S8 ,
41.Nm SYSCTL_S16 ,
42.Nm SYSCTL_S32 ,
43.Nm SYSCTL_S64 ,
44.Nm SYSCTL_STRING ,
45.Nm SYSCTL_STRUCT ,
46.Nm SYSCTL_U8 ,
47.Nm SYSCTL_U16 ,
48.Nm SYSCTL_U32 ,
49.Nm SYSCTL_U64 ,
50.Nm SYSCTL_UINT ,
51.Nm SYSCTL_ULONG ,
52.Nm SYSCTL_UQUAD
53.Nd Static sysctl declaration functions
54.Sh SYNOPSIS
55.In sys/types.h
56.In sys/sysctl.h
57.Fo SYSCTL_BIT32
58.Fa "parent"
59.Fa "nbr"
60.Fa "name"
61.Fa "access"
62.Fa "ptr"
63.Fa "val"
64.Fa "bit"
65.Fa "descr"
66.Fc
67.Fo SYSCTL_BIT64
68.Fa "parent"
69.Fa "nbr"
70.Fa "name"
71.Fa "access"
72.Fa "ptr"
73.Fa "val"
74.Fa "bit"
75.Fa "descr"
76.Fc
77.Fo SYSCTL_DECL
78.Fa "name"
79.Fc
80.Fo SYSCTL_INT
81.Fa "parent"
82.Fa "nbr"
83.Fa "name"
84.Fa "access"
85.Fa "ptr"
86.Fa "val"
87.Fa "descr"
88.Fc
89.Fo SYSCTL_LONG
90.Fa "parent"
91.Fa "nbr"
92.Fa "name"
93.Fa "access"
94.Fa "ptr"
95.Fa "val"
96.Fa "descr"
97.Fc
98.Fo SYSCTL_NODE
99.Fa "parent"
100.Fa "nbr"
101.Fa "name"
102.Fa "access"
103.Fa "handler"
104.Fa "descr"
105.Fc
106.Fo SYSCTL_OPAQUE
107.Fa "parent"
108.Fa "nbr"
109.Fa "name"
110.Fa "access"
111.Fa "ptr"
112.Fa "len"
113.Fa "fmt"
114.Fa "descr"
115.Fc
116.Fo SYSCTL_PROC
117.Fa "parent"
118.Fa "nbr"
119.Fa "name"
120.Fa "access"
121.Fa "ptr"
122.Fa "arg"
123.Fa "handler"
124.Fa "fmt"
125.Fa "descr"
126.Fc
127.Fo SYSCTL_QUAD
128.Fa "parent"
129.Fa "nbr"
130.Fa "name"
131.Fa "access"
132.Fa "ptr"
133.Fa "val"
134.Fa "descr"
135.Fc
136.Fo SYSCTL_S8
137.Fa "parent"
138.Fa "number"
139.Fa "name"
140.Fa "access"
141.Fa "ptr"
142.Fa "val"
143.Fa "descr"
144.Fc
145.Fo SYSCTL_S16
146.Fa "parent"
147.Fa "number"
148.Fa "name"
149.Fa "access"
150.Fa "ptr"
151.Fa "val"
152.Fa "descr"
153.Fc
154.Fo SYSCTL_S32
155.Fa "parent"
156.Fa "number"
157.Fa "name"
158.Fa "access"
159.Fa "ptr"
160.Fa "val"
161.Fa "descr"
162.Fc
163.Fo SYSCTL_S64
164.Fa "parent"
165.Fa "number"
166.Fa "name"
167.Fa "access"
168.Fa "ptr"
169.Fa "val"
170.Fa "descr"
171.Fc
172.Fo SYSCTL_STRING
173.Fa "parent"
174.Fa "nbr"
175.Fa "name"
176.Fa "access"
177.Fa "arg"
178.Fa "len"
179.Fa "descr"
180.Fc
181.Fo SYSCTL_STRUCT
182.Fa "parent"
183.Fa "nbr"
184.Fa "name"
185.Fa "access"
186.Fa "ptr"
187.Fa "type"
188.Fa "descr"
189.Fc
190.Fo SYSCTL_U8
191.Fa "parent"
192.Fa "number"
193.Fa "name"
194.Fa "access"
195.Fa "ptr"
196.Fa "val"
197.Fa "descr"
198.Fc
199.Fo SYSCTL_U16
200.Fa "parent"
201.Fa "number"
202.Fa "name"
203.Fa "access"
204.Fa "ptr"
205.Fa "val"
206.Fa "descr"
207.Fc
208.Fo SYSCTL_U32
209.Fa "parent"
210.Fa "number"
211.Fa "name"
212.Fa "access"
213.Fa "ptr"
214.Fa "val"
215.Fa "descr"
216.Fc
217.Fo SYSCTL_U64
218.Fa "parent"
219.Fa "number"
220.Fa "name"
221.Fa "access"
222.Fa "ptr"
223.Fa "val"
224.Fa "descr"
225.Fc
226.Fo SYSCTL_UINT
227.Fa "parent"
228.Fa "nbr"
229.Fa "name"
230.Fa "access"
231.Fa "ptr"
232.Fa "val"
233.Fa "descr"
234.Fc
235.Fo SYSCTL_ULONG
236.Fa "parent"
237.Fa "nbr"
238.Fa "name"
239.Fa "access"
240.Fa "ptr"
241.Fa "val"
242.Fa "descr"
243.Fc
244.Fo SYSCTL_UQUAD
245.Fa "parent"
246.Fa "nbr"
247.Fa "name"
248.Fa "access"
249.Fa "ptr"
250.Fa "val"
251.Fa "descr"
252.Fc
253.Sh DESCRIPTION
254The
255.Nm
256kernel interfaces allow code to statically declare
257.Xr sysctl 8
258MIB entries, which will be initialized when the kernel module containing the
259declaration is initialized.
260When the module is unloaded, the sysctl will be automatically destroyed.
261.Pp
262Sysctl nodes are created in a hierarchical tree, with all static nodes being
263represented by named C data structures; in order to create a new node under
264an existing node in the tree, the structure representing the desired parent
265node must be declared in the current context using
266.Fn SYSCTL_DECL .
267.Pp
268New nodes are declared using one of
269.Nm SYSCTL_BIT32 ,
270.Nm SYSCTL_BIT64 ,
271.Nm SYSCTL_INT ,
272.Nm SYSCTL_LONG ,
273.Nm SYSCTL_QUAD ,
274.Nm SYSCTL_NODE ,
275.Nm SYSCTL_OPAQUE ,
276.Nm SYSCTL_PROC ,
277.Nm SYSCTL_S8 ,
278.Nm SYSCTL_S16 ,
279.Nm SYSCTL_S32 ,
280.Nm SYSCTL_S64 ,
281.Nm SYSCTL_STRING ,
282.Nm SYSCTL_STRUCT ,
283.Nm SYSCTL_U8 ,
284.Nm SYSCTL_U16 ,
285.Nm SYSCTL_U32 ,
286.Nm SYSCTL_U64 ,
287.Nm SYSCTL_UINT ,
288.Nm SYSCTL_ULONG ,
289and
290.Nm SYSCTL_UQUAD .
291Each macro accepts a parent name, as declared using
292.Nm SYSCTL_DECL ,
293an OID number, typically
294.Dv OID_AUTO ,
295a node name, a set of control and access flags, and a description.
296Depending on the macro, a pointer to a variable supporting the MIB entry, a
297size, a value, and a function pointer implementing the MIB entry may also be
298present.
299.Pp
300For most of the above macros, declaring a type as part of the access flags is
301not necessary -- however, when declaring a sysctl implemented by a function,
302including a type in the access mask is required:
303.Bl -tag -width ".Dv CTLTYPE_STRING"
304.It Dv CTLTYPE_NODE
305This is a node intended to be a parent for other nodes.
306.It Dv CTLTYPE_INT
307This is a signed integer.
308.It Dv CTLTYPE_S8
309This is an 8-bit signed integer.
310.It Dv CTLTYPE_S16
311This is a 16-bit signed integer.
312.It Dv CTLTYPE_S32
313This is a 32-bit signed integer.
314.It Dv CTLTYPE_S64
315This is a 64-bit signed integer.
316.It Dv CTLTYPE_STRING
317This is a nul-terminated string stored in a character array.
318.It Dv CTLTYPE_QUAD
319This is a 64-bit signed integer.
320.It Dv CTLTYPE_OPAQUE
321This is an opaque data structure.
322.It Dv CTLTYPE_STRUCT
323Alias for
324.Dv CTLTYPE_OPAQUE .
325.It Dv CTLTYPE_U8
326This is an 8-bit unsigned integer.
327.It Dv CTLTYPE_U16
328This is a 16-bit unsigned integer.
329.It Dv CTLTYPE_U32
330This is a 32-bit unsigned integer.
331.It Dv CTLTYPE_U64
332This is a 64-bit unsigned integer.
333.It Dv CTLTYPE_UINT
334This is an unsigned integer.
335.It Dv CTLTYPE_LONG
336This is a signed long.
337.It Dv CTLTYPE_ULONG
338This is an unsigned long.
339.It Dv CTLTYPE_UQUAD
340This is a 64-bit unsigned integer.
341.El
342.Pp
343All sysctl types except for new node declarations require one or more flags
344to be set indicating the read and write disposition of the sysctl:
345.Bl -tag -width ".Dv CTLFLAG_ANYBODY"
346.It Dv CTLFLAG_RD
347This is a read-only sysctl.
348.It Dv CTLFLAG_WR
349This is a writable sysctl.
350.It Dv CTLFLAG_RW
351This sysctl is readable and writable.
352.It Dv CTLFLAG_ANYBODY
353Any user or process can write to this sysctl.
354.It Dv CTLFLAG_SECURE
355This sysctl can be written to only if the effective securelevel of the
356process is <= 0.
357.It Dv CTLFLAG_PRISON
358This sysctl can be written to by processes in
359.Xr jail 2 .
360.\".It Dv CTLFLAG_SKIP
361.\"When iterating the sysctl name space, do not list this sysctl.
362.El
363.Pp
364When creating new sysctls, careful attention should be paid to the security
365implications of the monitoring or management interface being created.
366Most sysctls present in the kernel are read-only or writable only by the
367superuser.
368Sysctls exporting extensive information on system data structures and
369operation, especially those implemented using procedures, will wish to
370implement access control to limit the undesired exposure of information about
371other processes, network connections, etc.
372.Pp
373The following top level sysctl name spaces are commonly used:
374.Bl -tag -width ".Va machdep"
375.It Va compat
376Compatibility layer information.
377.It Va debug
378Debugging information.
379Various name spaces exist under
380.Va debug .
381.It Va hw
382Hardware and device driver information.
383.It Va lwkt
384Information about the LWKT subsystem.
385.It Va kern
386Kernel behavior tuning; generally deprecated in favor of more specific
387name spaces.
388.It Va machdep
389Machine-dependent configuration parameters.
390.It Va net
391Network subsystem.
392Various protocols have name spaces under
393.Va net .
394.It Va sysctl
395Reserved name space for the implementation of sysctl.
396.It Va user
397Configuration settings relating to user application behavior.
398Generally, configuring applications using kernel sysctls is discouraged.
399.It Va vfs
400Virtual file system configuration and information.
401.It Va vm
402Virtual memory subsystem configuration and information.
403.El
404.Sh EXAMPLES
405Sample use of
406.Nm SYSCTL_DECL
407to declare the "machdep" sysctl tree for use by new nodes:
408.Bd -literal -offset indent
409SYSCTL_DECL(_machdep);
410.Ed
411.Pp
412Examples of integer, opaque, string, and procedure sysctls follow:
413.Bd -literal -offset indent
414/*
415 * Example of a constant integer value.  Notice that the control
416 * flags are CTLFLAG_RD, the variable pointer is NULL, and the
417 * value is declared.
418 */
419SYSCTL_INT(_debug_sizeof, OID_AUTO, bio, CTLFLAG_RD, NULL,
420    sizeof(struct bio), "sizeof(struct bio)");
421
422/*
423 * Example of a variable integer value.  Notice that the control
424 * flags are CTLFLAG_RW, the variable pointer is set, and the
425 * value is 0.
426 */
427static int	doingcache = 1;		/* 1 => enable the cache */
428SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
429    "Enable name cache");
430
431/*
432 * Example of a variable string value.  Notice that the control
433 * flags are CTLFLAG_RW, that the variable pointer and string
434 * size are set.  Unlike newer sysctls, this older sysctl uses a
435 * static oid number.
436 */
437char kernelname[MAXPATHLEN] = "/boot/kernel";	/* XXX bloat */
438SYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, CTLFLAG_RW,
439    kernelname, sizeof(kernelname), "Name of kernel file booted");
440
441/*
442 * Example of an opaque data type exported by sysctl.  Notice that
443 * the variable pointer and size are provided, as well as a format
444 * string for sysctl(8).
445 */
446static l_fp pps_freq;	/* scaled frequency offset (ns/s) */
447SYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD,
448    &pps_freq, sizeof(pps_freq), "I", "");
449
450/*
451 * Example of a procedure based sysctl exporting string
452 * information.  Notice that the data type is declared, the NULL
453 * variable pointer and 0 size, the function pointer, and the
454 * format string for sysctl(8).
455 */
456SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
457    0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
458.Ed
459.Pp
460When adding, modifying, or removing sysctl names, it is important to be
461aware that these interfaces may be used by users, libraries, applications,
462or documentation (such as published books), and are implicitly published
463application interfaces.
464As with other application interfaces, caution must be taken not to break
465existing applications, and to think about future use of new name spaces so as
466to avoid the need to rename or remove interfaces that might be depended on in
467the future.
468.Sh SEE ALSO
469.Xr sysctl 8 ,
470.Xr sysctl_add_oid 9 ,
471.Xr sysctl_ctx_free 9 ,
472.Xr sysctl_ctx_init 9 ,
473.Xr sysctl_remove_oid 9
474.Sh HISTORY
475.Xr sysctl 8
476first appeared in
477.Bx 4.4 .
478.Sh AUTHORS
479.An -nosplit
480The sysctl implementation originally found in
481.Bx
482has been extensively rewritten by
483.An Poul-Henning Kamp
484in order to add support for name lookups, name space iteration, and dynamic
485addition of MIB nodes.
486.Pp
487This man page was written by
488.An Robert N. M. Watson .
489