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