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