xref: /freebsd/share/man/man9/sysctl.9 (revision 5eb1caa8c84577ae8d92c0c2142abec4c31160e9)
1dba9e9ccSRobert Watson.\"
2dba9e9ccSRobert Watson.\" Copyright (c) 2006 Robert N. M. Watson
3dba9e9ccSRobert Watson.\" All rights reserved.
4dba9e9ccSRobert Watson.\"
5dba9e9ccSRobert Watson.\" Redistribution and use in source and binary forms, with or without
6dba9e9ccSRobert Watson.\" modification, are permitted provided that the following conditions
7dba9e9ccSRobert Watson.\" are met:
8dba9e9ccSRobert Watson.\" 1. Redistributions of source code must retain the above copyright
9dba9e9ccSRobert Watson.\"    notice, this list of conditions and the following disclaimer.
10dba9e9ccSRobert Watson.\" 2. Redistributions in binary form must reproduce the above copyright
11dba9e9ccSRobert Watson.\"    notice, this list of conditions and the following disclaimer in the
12dba9e9ccSRobert Watson.\"    documentation and/or other materials provided with the distribution.
13dba9e9ccSRobert Watson.\"
14dba9e9ccSRobert Watson.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15dba9e9ccSRobert Watson.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16dba9e9ccSRobert Watson.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17dba9e9ccSRobert Watson.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18dba9e9ccSRobert Watson.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19dba9e9ccSRobert Watson.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20dba9e9ccSRobert Watson.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21dba9e9ccSRobert Watson.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22dba9e9ccSRobert Watson.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23dba9e9ccSRobert Watson.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24dba9e9ccSRobert Watson.\" SUCH DAMAGE.
25dba9e9ccSRobert Watson.\"
26dba9e9ccSRobert Watson.\" $FreeBSD$
27dba9e9ccSRobert Watson.\"
28*5eb1caa8SHans Petter Selasky.Dd July 31, 2014
29dba9e9ccSRobert Watson.Dt SYSCTL 9
30dba9e9ccSRobert Watson.Os
31dba9e9ccSRobert Watson.Sh NAME
32dba9e9ccSRobert Watson.Nm SYSCTL_DECL ,
33*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ADD_INT ,
34*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ADD_LONG ,
35*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ADD_NODE ,
36*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ADD_OPAQUE ,
37*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ADD_PROC ,
38*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ADD_QUAD ,
39*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ADD_ROOT_NODE ,
40*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ADD_STRING ,
41*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ADD_STRUCT ,
42*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ADD_UINT ,
43*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ADD_ULONG ,
44*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ADD_UQUAD ,
45*5eb1caa8SHans Petter Selasky.Nm SYSCTL_CHILDREN ,
46*5eb1caa8SHans Petter Selasky.Nm SYSCTL_STATIC_CHILDREN ,
47*5eb1caa8SHans Petter Selasky.Nm SYSCTL_NODE_CHILDREN ,
48*5eb1caa8SHans Petter Selasky.Nm SYSCTL_PARENT ,
49dba9e9ccSRobert Watson.Nm SYSCTL_INT ,
50dba9e9ccSRobert Watson.Nm SYSCTL_LONG ,
51dba9e9ccSRobert Watson.Nm SYSCTL_NODE ,
52dba9e9ccSRobert Watson.Nm SYSCTL_OPAQUE ,
53dba9e9ccSRobert Watson.Nm SYSCTL_PROC ,
5460a77442SJohn Baldwin.Nm SYSCTL_QUAD ,
55*5eb1caa8SHans Petter Selasky.Nm SYSCTL_ROOT_NODE ,
56dba9e9ccSRobert Watson.Nm SYSCTL_STRING ,
57dba9e9ccSRobert Watson.Nm SYSCTL_STRUCT ,
58dba9e9ccSRobert Watson.Nm SYSCTL_UINT ,
59c157a036SDavid E. O'Brien.Nm SYSCTL_ULONG ,
6060a77442SJohn Baldwin.Nm SYSCTL_UQUAD
61*5eb1caa8SHans Petter Selasky.Nd Dynamic and static sysctl MIB creation functions
62dba9e9ccSRobert Watson.Sh SYNOPSIS
63dba9e9ccSRobert Watson.In sys/types.h
64dba9e9ccSRobert Watson.In sys/sysctl.h
65bd84dd2fSRuslan Ermilov.Fn SYSCTL_DECL name
66*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
67*5eb1caa8SHans Petter Selasky.Fo SYSCTL_ADD_INT
68*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_ctx_list *ctx"
69*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid_list *parent"
70*5eb1caa8SHans Petter Selasky.Fa "int number"
71*5eb1caa8SHans Petter Selasky.Fa "const char *name"
72*5eb1caa8SHans Petter Selasky.Fa "int ctlflags"
73*5eb1caa8SHans Petter Selasky.Fa "int *ptr"
74*5eb1caa8SHans Petter Selasky.Fa "intptr_t val"
75*5eb1caa8SHans Petter Selasky.Fa "const char *descr"
76*5eb1caa8SHans Petter Selasky.Fc
77*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
78*5eb1caa8SHans Petter Selasky.Fo SYSCTL_ADD_LONG
79*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_ctx_list *ctx"
80*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid_list *parent"
81*5eb1caa8SHans Petter Selasky.Fa "int number"
82*5eb1caa8SHans Petter Selasky.Fa "const char *name"
83*5eb1caa8SHans Petter Selasky.Fa "int ctlflags"
84*5eb1caa8SHans Petter Selasky.Fa "long *ptr"
85*5eb1caa8SHans Petter Selasky.Fa "intptr_t val"
86*5eb1caa8SHans Petter Selasky.Fa "const char *descr"
87*5eb1caa8SHans Petter Selasky.Fc
88*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
89*5eb1caa8SHans Petter Selasky.Fo SYSCTL_ADD_NODE
90*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_ctx_list *ctx"
91*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid_list *parent"
92*5eb1caa8SHans Petter Selasky.Fa "int number"
93*5eb1caa8SHans Petter Selasky.Fa "const char *name"
94*5eb1caa8SHans Petter Selasky.Fa "int ctlflags"
95*5eb1caa8SHans Petter Selasky.Fa "int (*handler)(SYSCTL_HANDLER_ARGS)"
96*5eb1caa8SHans Petter Selasky.Fa "const char *descr"
97*5eb1caa8SHans Petter Selasky.Fc
98*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
99*5eb1caa8SHans Petter Selasky.Fo SYSCTL_ADD_OPAQUE
100*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_ctx_list *ctx"
101*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid_list *parent"
102*5eb1caa8SHans Petter Selasky.Fa "int number"
103*5eb1caa8SHans Petter Selasky.Fa "const char *name"
104*5eb1caa8SHans Petter Selasky.Fa "int ctlflags"
105*5eb1caa8SHans Petter Selasky.Fa "void *ptr"
106*5eb1caa8SHans Petter Selasky.Fa "intptr_t len"
107*5eb1caa8SHans Petter Selasky.Fa "const char *format"
108*5eb1caa8SHans Petter Selasky.Fa "const char *descr
109*5eb1caa8SHans Petter Selasky.Fc
110*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
111*5eb1caa8SHans Petter Selasky.Fo SYSCTL_ADD_PROC
112*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_ctx_list *ctx"
113*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid_list *parent"
114*5eb1caa8SHans Petter Selasky.Fa "int number"
115*5eb1caa8SHans Petter Selasky.Fa "const char *name"
116*5eb1caa8SHans Petter Selasky.Fa "int ctlflags"
117*5eb1caa8SHans Petter Selasky.Fa "void *arg1"
118*5eb1caa8SHans Petter Selasky.Fa "intptr_t arg2"
119*5eb1caa8SHans Petter Selasky.Fa "int (*handler) (SYSCTL_HANDLERARGS)"
120*5eb1caa8SHans Petter Selasky.Fa "const char *format"
121*5eb1caa8SHans Petter Selasky.Fa "const char *descr"
122*5eb1caa8SHans Petter Selasky.Fc
123*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
124*5eb1caa8SHans Petter Selasky.Fo SYSCTL_ADD_QUAD
125*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_ctx_list *ctx"
126*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid_list *parent"
127*5eb1caa8SHans Petter Selasky.Fa "int number"
128*5eb1caa8SHans Petter Selasky.Fa "const char *name"
129*5eb1caa8SHans Petter Selasky.Fa "int ctlflags"
130*5eb1caa8SHans Petter Selasky.Fa "quad_t *ptr"
131*5eb1caa8SHans Petter Selasky.Fa "intptr_t val"
132*5eb1caa8SHans Petter Selasky.Fa "const char *descr"
133*5eb1caa8SHans Petter Selasky.Fc
134*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
135*5eb1caa8SHans Petter Selasky.Fo SYSCTL_ADD_ROOT_NODE
136*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_ctx_list *ctx"
137*5eb1caa8SHans Petter Selasky.Fa "int number"
138*5eb1caa8SHans Petter Selasky.Fa "const char *name"
139*5eb1caa8SHans Petter Selasky.Fa "int ctlflags"
140*5eb1caa8SHans Petter Selasky.Fa "int (*handler)(SYSCTL_HANDLER_ARGS)"
141*5eb1caa8SHans Petter Selasky.Fa "const char *descr"
142*5eb1caa8SHans Petter Selasky.Fc
143*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
144*5eb1caa8SHans Petter Selasky.Fo SYSCTL_ADD_STRING
145*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_ctx_list *ctx"
146*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid_list *parent"
147*5eb1caa8SHans Petter Selasky.Fa "int number"
148*5eb1caa8SHans Petter Selasky.Fa "const char *name"
149*5eb1caa8SHans Petter Selasky.Fa "int ctlflags"
150*5eb1caa8SHans Petter Selasky.Fa "char *ptr"
151*5eb1caa8SHans Petter Selasky.Fa "intptr_t len"
152*5eb1caa8SHans Petter Selasky.Fa "const char *descr"
153*5eb1caa8SHans Petter Selasky.Fc
154*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
155*5eb1caa8SHans Petter Selasky.Fo SYSCTL_ADD_STRUCT
156*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_ctx_list *ctx"
157*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid_list *parent"
158*5eb1caa8SHans Petter Selasky.Fa "int number"
159*5eb1caa8SHans Petter Selasky.Fa "const char *name"
160*5eb1caa8SHans Petter Selasky.Fa "int ctlflags"
161*5eb1caa8SHans Petter Selasky.Fa "void *ptr"
162*5eb1caa8SHans Petter Selasky.Fa struct_type
163*5eb1caa8SHans Petter Selasky.Fa "const char *descr"
164*5eb1caa8SHans Petter Selasky.Fc
165*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
166*5eb1caa8SHans Petter Selasky.Fo SYSCTL_ADD_UINT
167*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_ctx_list *ctx"
168*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid_list *parent"
169*5eb1caa8SHans Petter Selasky.Fa "int number"
170*5eb1caa8SHans Petter Selasky.Fa "const char *name"
171*5eb1caa8SHans Petter Selasky.Fa "int ctlflags"
172*5eb1caa8SHans Petter Selasky.Fa "unsigned int *ptr"
173*5eb1caa8SHans Petter Selasky.Fa "intptr_t val"
174*5eb1caa8SHans Petter Selasky.Fa "const char *descr"
175*5eb1caa8SHans Petter Selasky.Fc
176*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
177*5eb1caa8SHans Petter Selasky.Fo SYSCTL_ADD_ULONG
178*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_ctx_list *ctx"
179*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid_list *parent"
180*5eb1caa8SHans Petter Selasky.Fa "int number"
181*5eb1caa8SHans Petter Selasky.Fa "const char *name"
182*5eb1caa8SHans Petter Selasky.Fa "int ctlflags"
183*5eb1caa8SHans Petter Selasky.Fa "unsigned long *ptr"
184*5eb1caa8SHans Petter Selasky.Fa "intptr_t val"
185*5eb1caa8SHans Petter Selasky.Fa "const char *descr"
186*5eb1caa8SHans Petter Selasky.Fc
187*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
188*5eb1caa8SHans Petter Selasky.Fo SYSCTL_ADD_UQUAD
189*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_ctx_list *ctx"
190*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid_list *parent"
191*5eb1caa8SHans Petter Selasky.Fa "int number"
192*5eb1caa8SHans Petter Selasky.Fa "const char *name"
193*5eb1caa8SHans Petter Selasky.Fa "int ctlflags"
194*5eb1caa8SHans Petter Selasky.Fa "u_quad_t *ptr"
195*5eb1caa8SHans Petter Selasky.Fa "intptr_t val"
196*5eb1caa8SHans Petter Selasky.Fa "const char *descr"
197*5eb1caa8SHans Petter Selasky.Fc
198*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid_list *
199*5eb1caa8SHans Petter Selasky.Fo SYSCTL_CHILDREN
200*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid *oidp"
201*5eb1caa8SHans Petter Selasky.Fc
202*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid_list *
203*5eb1caa8SHans Petter Selasky.Fo SYSCTL_STATIC_CHILDREN
204*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid_list OID_NAME"
205*5eb1caa8SHans Petter Selasky.Fc
206*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid_list *
207*5eb1caa8SHans Petter Selasky.Fo SYSCTL_NODE_CHILDREN
208*5eb1caa8SHans Petter Selasky.Fa "parent"
209*5eb1caa8SHans Petter Selasky.Fa "name"
210*5eb1caa8SHans Petter Selasky.Fc
211*5eb1caa8SHans Petter Selasky.Ft struct sysctl_oid *
212*5eb1caa8SHans Petter Selasky.Fo SYSCTL_PARENT
213*5eb1caa8SHans Petter Selasky.Fa "struct sysctl_oid *oid"
214*5eb1caa8SHans Petter Selasky.Fc
215*5eb1caa8SHans Petter Selasky.Fn SYSCTL_INT parent number name ctlflags ptr val descr
216*5eb1caa8SHans Petter Selasky.Fn SYSCTL_LONG parent number name ctlflags ptr val descr
217*5eb1caa8SHans Petter Selasky.Fn SYSCTL_NODE parent number name ctlflags handler descr
218*5eb1caa8SHans Petter Selasky.Fn SYSCTL_OPAQUE parent number name ctlflags ptr len format descr
219*5eb1caa8SHans Petter Selasky.Fn SYSCTL_PROC parent number name ctlflags arg1 arg2 handler format descr
220*5eb1caa8SHans Petter Selasky.Fn SYSCTL_QUAD parent number name ctlflags ptr val descr
221*5eb1caa8SHans Petter Selasky.Fn SYSCTL_STRING parent number name ctlflags arg len descr
222*5eb1caa8SHans Petter Selasky.Fn SYSCTL_STRUCT parent number name ctlflags ptr struct_type descr
223*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ROOT_NODE number name ctlflags handler descr
224*5eb1caa8SHans Petter Selasky.Fn SYSCTL_UINT parent number name ctlflags ptr val descr
225*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ULONG parent number name ctlflags ptr val descr
226*5eb1caa8SHans Petter Selasky.Fn SYSCTL_UQUAD parent number name ctlflags ptr val descr
227dba9e9ccSRobert Watson.Sh DESCRIPTION
228dba9e9ccSRobert WatsonThe
229bd84dd2fSRuslan Ermilov.Nm SYSCTL
230*5eb1caa8SHans Petter Selaskykernel interface allows dynamic or static creation of
231dba9e9ccSRobert Watson.Xr sysctl 8
232*5eb1caa8SHans Petter SelaskyMIB entries.
233*5eb1caa8SHans Petter SelaskyAll static sysctls are automatically destroyed when the module which
234*5eb1caa8SHans Petter Selaskythey are part of is unloaded.
235*5eb1caa8SHans Petter SelaskyMost top level categories are created statically and are available to
236*5eb1caa8SHans Petter Selaskyall kernel code and its modules.
237*5eb1caa8SHans Petter Selasky.Sh DESCRIPTION OF ARGUMENTS
238*5eb1caa8SHans Petter Selasky.Bl -tag -width ctlflags
239*5eb1caa8SHans Petter Selasky.It Fa ctx
240*5eb1caa8SHans Petter SelaskyPointer to sysctl context or NULL, if no context.
241*5eb1caa8SHans Petter SelaskySee
242*5eb1caa8SHans Petter Selasky.Xr sysctl_ctx_init 9
243*5eb1caa8SHans Petter Selaskyfor how to create a new sysctl context.
244*5eb1caa8SHans Petter SelaskyProgrammers are strongly advised to use contexts to organize the
245*5eb1caa8SHans Petter Selaskydynamic OIDs which they create because when a context is destroyed all
246*5eb1caa8SHans Petter Selaskybelonging sysctls are destroyed aswell.
247*5eb1caa8SHans Petter SelaskyThis makes the sysctl cleanup code much simpler.
248*5eb1caa8SHans Petter SelaskyElse deletion of all created OIDs is required at module unload typically.
249*5eb1caa8SHans Petter Selasky.It Fa parent
250*5eb1caa8SHans Petter SelaskyA pointer to a
251*5eb1caa8SHans Petter Selasky.Li struct sysctl_oid_list ,
252*5eb1caa8SHans Petter Selaskywhich is the head of the parent's list of children.
253*5eb1caa8SHans Petter SelaskyThis pointer is retrieved by using the
254*5eb1caa8SHans Petter Selasky.Fn SYSCTL_STATIC_CHILDREN
255*5eb1caa8SHans Petter Selaskymacro for static sysctls and the
256*5eb1caa8SHans Petter Selasky.Fn SYSCTL_CHILDREN
257*5eb1caa8SHans Petter Selaskymacro for dynamic sysctls.
258*5eb1caa8SHans Petter SelaskyTo get the parent of an OID the
259*5eb1caa8SHans Petter Selasky.Fn SYSCTL_PARENT
260*5eb1caa8SHans Petter Selaskymacro shall be used.
261*5eb1caa8SHans Petter SelaskyIf there is no parent this macro returns NULL.
262*5eb1caa8SHans Petter Selasky.It Fa number
263*5eb1caa8SHans Petter SelaskyThe OID number that will be assigned to this OID.
264*5eb1caa8SHans Petter SelaskyIn almost all cases this should be set to
265*5eb1caa8SHans Petter Selasky.Dv OID_AUTO ,
266*5eb1caa8SHans Petter Selaskywhich will result in the assignment of the next available OID number.
267*5eb1caa8SHans Petter Selasky.It Fa name
268*5eb1caa8SHans Petter SelaskyThe name of the OID.
269*5eb1caa8SHans Petter SelaskyThe newly created OID will contain a copy of the name.
270*5eb1caa8SHans Petter Selasky.It Fa ctlflags
271*5eb1caa8SHans Petter SelaskyA bit mask of sysctl control flags.
272*5eb1caa8SHans Petter SelaskySee the section below describing all the control flags.
273*5eb1caa8SHans Petter Selasky.It Fa arg1
274*5eb1caa8SHans Petter SelaskyFirst callback argument for procedure sysctls.
275*5eb1caa8SHans Petter Selasky.It Fa arg2
276*5eb1caa8SHans Petter SelaskySecond callback argument for procedure sysctls.
277*5eb1caa8SHans Petter Selasky.It Fa len
278*5eb1caa8SHans Petter SelaskyThe length of the data pointed to by the
279*5eb1caa8SHans Petter Selasky.Fa ptr
280*5eb1caa8SHans Petter Selaskyargument.
281*5eb1caa8SHans Petter SelaskyFor string type OIDs a length of zero means that
282*5eb1caa8SHans Petter Selasky.Xr strlen 3
283*5eb1caa8SHans Petter Selaskywill be used to get the length of the string at each access to the OID.
284*5eb1caa8SHans Petter Selasky.It Fa ptr
285*5eb1caa8SHans Petter SelaskyPointer to sysctl variable or string data.
286*5eb1caa8SHans Petter SelaskyFor sysctl values the pointer can be NULL which means the OID is read-only and the returned value should be taken from the
287*5eb1caa8SHans Petter Selasky.Fa val
288*5eb1caa8SHans Petter Selaskyargument.
289*5eb1caa8SHans Petter Selasky.It Fa val
290*5eb1caa8SHans Petter SelaskyIf the
291*5eb1caa8SHans Petter Selasky.Fa ptr
292*5eb1caa8SHans Petter Selaskyargument is NULL, gives the constant value returned by this OID.
293*5eb1caa8SHans Petter SelaskyElse this argument is not used.
294*5eb1caa8SHans Petter Selasky.It Fa struct_type
295*5eb1caa8SHans Petter SelaskyName of structure type.
296*5eb1caa8SHans Petter Selasky.It Fa handler
297*5eb1caa8SHans Petter SelaskyA pointer to the function
298*5eb1caa8SHans Petter Selaskythat is responsible for handling read and write requests
299*5eb1caa8SHans Petter Selaskyto this OID.
300*5eb1caa8SHans Petter SelaskyThere are several standard handlers
301*5eb1caa8SHans Petter Selaskythat support operations on nodes,
302*5eb1caa8SHans Petter Selaskyintegers, strings and opaque objects.
303*5eb1caa8SHans Petter SelaskyIt is possible to define custom handlers using the
304*5eb1caa8SHans Petter Selasky.Fn SYSCTL_PROC
305*5eb1caa8SHans Petter Selaskymacro or the
306*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_PROC
307*5eb1caa8SHans Petter Selaskyfunction.
308*5eb1caa8SHans Petter Selasky.It Fa format
309*5eb1caa8SHans Petter SelaskyA pointer to a string
310*5eb1caa8SHans Petter Selaskywhich specifies the format of the OID in a symbolic way.
311*5eb1caa8SHans Petter SelaskyThis format is used as a hint by
312*5eb1caa8SHans Petter Selasky.Xr sysctl 8
313*5eb1caa8SHans Petter Selaskyto apply proper data formatting for display purposes.
314*5eb1caa8SHans Petter SelaskyCurrently used format names are:
315*5eb1caa8SHans Petter Selasky.Dq N
316*5eb1caa8SHans Petter Selaskyfor node,
317*5eb1caa8SHans Petter Selasky.Dq A
318*5eb1caa8SHans Petter Selaskyfor
319*5eb1caa8SHans Petter Selasky.Li "char *" ,
320*5eb1caa8SHans Petter Selasky.Dq I
321*5eb1caa8SHans Petter Selaskyfor
322*5eb1caa8SHans Petter Selasky.Li "int" ,
323*5eb1caa8SHans Petter Selasky.Dq IU
324*5eb1caa8SHans Petter Selaskyfor
325*5eb1caa8SHans Petter Selasky.Li "unsigned int" ,
326*5eb1caa8SHans Petter Selasky.Dq L
327*5eb1caa8SHans Petter Selaskyfor
328*5eb1caa8SHans Petter Selasky.Li "long" ,
329*5eb1caa8SHans Petter Selasky.Dq LU
330*5eb1caa8SHans Petter Selaskyfor
331*5eb1caa8SHans Petter Selasky.Li "unsigned long" ,
332*5eb1caa8SHans Petter Selasky.Dq Q
333*5eb1caa8SHans Petter Selaskyfor
334*5eb1caa8SHans Petter Selasky.Li "quad_t" ,
335*5eb1caa8SHans Petter Selasky.Dq QU
336*5eb1caa8SHans Petter Selaskyfor
337*5eb1caa8SHans Petter Selasky.Li "u_quad_t"
338*5eb1caa8SHans Petter Selaskyand
339*5eb1caa8SHans Petter Selasky.Dq S,TYPE
340*5eb1caa8SHans Petter Selaskyfor
341*5eb1caa8SHans Petter Selasky.Li "struct TYPE"
342*5eb1caa8SHans Petter Selaskystructures.
343*5eb1caa8SHans Petter Selasky.It Fa descr
344*5eb1caa8SHans Petter SelaskyA pointer to a textual description of the OID.
345*5eb1caa8SHans Petter Selasky.El
346*5eb1caa8SHans Petter Selasky.Sh CREATING ROOT NODES
347*5eb1caa8SHans Petter SelaskySysctl MIBs or OIDs are created in a hierarchical tree.
348*5eb1caa8SHans Petter SelaskyThe nodes at the bottom of the tree are called root nodes, and have no
349*5eb1caa8SHans Petter Selaskyparent OID.
350*5eb1caa8SHans Petter SelaskyTo create bottom tree nodes the
351*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ROOT_NODE
352*5eb1caa8SHans Petter Selaskymacro or the
353*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_ROOT_NODE
354*5eb1caa8SHans Petter Selaskyfunction needs to be used.
355*5eb1caa8SHans Petter SelaskyBy default all static sysctl node OIDs are global and need a
356*5eb1caa8SHans Petter Selasky.Fn SYSCTL_DECL
357*5eb1caa8SHans Petter Selaskystatement prior to their
358*5eb1caa8SHans Petter Selasky.Fn SYSCTL_NODE
359*5eb1caa8SHans Petter Selaskydefinition statement, typically in a so-called header file.
360*5eb1caa8SHans Petter Selasky.Sh CREATING SYSCTL STRINGS
361*5eb1caa8SHans Petter SelaskyZero terminated character strings sysctls are created either using the
362*5eb1caa8SHans Petter Selasky.Fn SYSCTL_STRING
363*5eb1caa8SHans Petter Selaskymacro or the
364*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_STRING
365*5eb1caa8SHans Petter Selaskyfunction.
366*5eb1caa8SHans Petter SelaskyIf the
367*5eb1caa8SHans Petter Selasky.Fa len
368*5eb1caa8SHans Petter Selaskyargument in zero, the string length is computed at every access to the OID using
369*5eb1caa8SHans Petter Selasky.Xr strlen 3 .
370*5eb1caa8SHans Petter Selasky.Sh CREATING OPAQUE SYSCTLS
371*5eb1caa8SHans Petter SelaskyThe
372*5eb1caa8SHans Petter Selasky.Fn SYSCTL_OPAQUE
373*5eb1caa8SHans Petter Selaskyor
374*5eb1caa8SHans Petter Selasky.Fn SYSCTL_STRUCT
375*5eb1caa8SHans Petter Selaskymacros or the
376*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_OPAQUE
377*5eb1caa8SHans Petter Selaskyor
378*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_STRUCT
379*5eb1caa8SHans Petter Selaskyfunctions create an OID that handle any chunk of data
380*5eb1caa8SHans Petter Selaskyof the size specified by the
381*5eb1caa8SHans Petter Selasky.Fa len
382*5eb1caa8SHans Petter Selaskyargument and data pointed to by the
383*5eb1caa8SHans Petter Selasky.Fa ptr
384*5eb1caa8SHans Petter Selaskyargument.
385*5eb1caa8SHans Petter SelaskyWhen using the structure version the type is encoded as part of the
386*5eb1caa8SHans Petter Selaskycreated sysctl.
387*5eb1caa8SHans Petter Selasky.Sh CREATING CUSTOM SYSCTLS
388*5eb1caa8SHans Petter SelaskyThe
389*5eb1caa8SHans Petter Selasky.Fn SYSCTL_PROC
390*5eb1caa8SHans Petter Selaskymacro and the
391*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_PROC
392*5eb1caa8SHans Petter Selaskyfunction
393*5eb1caa8SHans Petter Selaskycreate OIDs with the specified
394*5eb1caa8SHans Petter Selasky.Pa handler
395*5eb1caa8SHans Petter Selaskyfunction.
396*5eb1caa8SHans Petter SelaskyThe handler is responsible for handling all read and write requests to
397*5eb1caa8SHans Petter Selaskythe OID.
398*5eb1caa8SHans Petter SelaskyThis OID type is especially useful if the kernel data is not easily
399*5eb1caa8SHans Petter Selaskyaccessible, or needs to be processed before exporting.
400*5eb1caa8SHans Petter Selasky.Sh CREATING A STATIC SYSCTL
401*5eb1caa8SHans Petter SelaskyStatic sysctls are declared using one of the
402bd84dd2fSRuslan Ermilov.Fn SYSCTL_INT ,
403bd84dd2fSRuslan Ermilov.Fn SYSCTL_LONG ,
404bd84dd2fSRuslan Ermilov.Fn SYSCTL_NODE ,
405bd84dd2fSRuslan Ermilov.Fn SYSCTL_OPAQUE ,
406bd84dd2fSRuslan Ermilov.Fn SYSCTL_PROC ,
40760a77442SJohn Baldwin.Fn SYSCTL_QUAD ,
408*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ROOT_NODE ,
409bd84dd2fSRuslan Ermilov.Fn SYSCTL_STRING ,
410bd84dd2fSRuslan Ermilov.Fn SYSCTL_STRUCT ,
411bd84dd2fSRuslan Ermilov.Fn SYSCTL_UINT ,
412*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ULONG
413*5eb1caa8SHans Petter Selaskyor
414*5eb1caa8SHans Petter Selasky.Fn SYSCTL_UQUAD
415*5eb1caa8SHans Petter Selaskymacros.
416*5eb1caa8SHans Petter Selasky.Sh CREATING A DYNAMIC SYSCTL
417*5eb1caa8SHans Petter SelaskyDynamic nodes are created using one of the
418*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_INT ,
419*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_LONG ,
420*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_NODE ,
421*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_OPAQUE ,
422*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_PROC ,
423*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_QUAD ,
424*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_ROOT_NODE ,
425*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_STRING ,
426*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_STRUCT ,
427*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_UINT ,
428*5eb1caa8SHans Petter Selasky.Fn SYSCTL_ADD_ULONG
429*5eb1caa8SHans Petter Selaskyor
430*5eb1caa8SHans Petter Selasky.Fn SYSCTL_UQUAD
431*5eb1caa8SHans Petter Selaskyfunctions.
432*5eb1caa8SHans Petter SelaskySee
433*5eb1caa8SHans Petter Selasky.Xr sysctl_remove_oid 9
434*5eb1caa8SHans Petter Selaskyor
435*5eb1caa8SHans Petter Selasky.Xr sysctl_ctx_free 9
436*5eb1caa8SHans Petter Selaskyfor more information on how to destroy a dynamically created OID.
437*5eb1caa8SHans Petter Selasky.Sh CONTROL FLAGS
438*5eb1caa8SHans Petter SelaskyFor most of the above functions and macros, declaring a type as part
439*5eb1caa8SHans Petter Selaskyof the access flags is not necessary \[em] however, when declaring a
440*5eb1caa8SHans Petter Selaskysysctl implemented by a function, including a type in the access mask
441*5eb1caa8SHans Petter Selaskyis required:
442*5eb1caa8SHans Petter Selasky.Bl -tag -width ".Dv CTLTYPE_NOFETCH"
443dba9e9ccSRobert Watson.It Dv CTLTYPE_NODE
444dba9e9ccSRobert WatsonThis is a node intended to be a parent for other nodes.
445dba9e9ccSRobert Watson.It Dv CTLTYPE_INT
446dba9e9ccSRobert WatsonThis is a signed integer.
447dba9e9ccSRobert Watson.It Dv CTLTYPE_STRING
448dba9e9ccSRobert WatsonThis is a nul-terminated string stored in a character array.
449cbc134adSMatthew D Fleming.It Dv CTLTYPE_S64
450dba9e9ccSRobert WatsonThis is a 64-bit signed integer.
451dba9e9ccSRobert Watson.It Dv CTLTYPE_OPAQUE
452dba9e9ccSRobert WatsonThis is an opaque data structure.
453dba9e9ccSRobert Watson.It Dv CTLTYPE_STRUCT
454dba9e9ccSRobert WatsonAlias for
455dba9e9ccSRobert Watson.Dv CTLTYPE_OPAQUE .
456dba9e9ccSRobert Watson.It Dv CTLTYPE_UINT
457dba9e9ccSRobert WatsonThis is an unsigned integer.
458dba9e9ccSRobert Watson.It Dv CTLTYPE_LONG
459dba9e9ccSRobert WatsonThis is a signed long.
460dba9e9ccSRobert Watson.It Dv CTLTYPE_ULONG
4618a1f4129SRobert WatsonThis is an unsigned long.
462cbc134adSMatthew D Fleming.It Dv CTLTYPE_U64
463cbc134adSMatthew D FlemingThis is a 64-bit unsigned integer.
464dba9e9ccSRobert Watson.El
465dba9e9ccSRobert Watson.Pp
46660a77442SJohn BaldwinAll sysctl types except for new node declarations require one of the following
46760a77442SJohn Baldwinflags to be set indicating the read and write disposition of the sysctl:
468bd84dd2fSRuslan Ermilov.Bl -tag -width ".Dv CTLFLAG_ANYBODY"
469dba9e9ccSRobert Watson.It Dv CTLFLAG_RD
470dba9e9ccSRobert WatsonThis is a read-only sysctl.
47160a77442SJohn Baldwin.It Dv CTLFLAG_RDTUN
472*5eb1caa8SHans Petter SelaskyThis is a read-only sysctl and tunable which is tried fetched once
473*5eb1caa8SHans Petter Selaskyfrom the system enviroment early during module load or system boot.
474b470b929SDavid E. O'Brien.It Dv CTLFLAG_WR
475dba9e9ccSRobert WatsonThis is a writable sysctl.
476dba9e9ccSRobert Watson.It Dv CTLFLAG_RW
477dba9e9ccSRobert WatsonThis sysctl is readable and writable.
47860a77442SJohn Baldwin.It Dv CTLFLAG_RWTUN
479*5eb1caa8SHans Petter SelaskyThis is a readable and writeable sysctl and tunable which is tried
480*5eb1caa8SHans Petter Selaskyfetched once from the system enviroment early during module load or
481*5eb1caa8SHans Petter Selaskysystem boot.
482*5eb1caa8SHans Petter Selasky.It Dv CTLFLAG_NOFETCH
483*5eb1caa8SHans Petter SelaskyIn case the node is marked as a tunable using the CTLFLAG_[XX]TUN,
484*5eb1caa8SHans Petter Selaskythis flag will prevent fetching the initial value from the system
485*5eb1caa8SHans Petter Selaskyenvironment. Typically this flag should only be used for very early
486*5eb1caa8SHans Petter Selaskylow level system setup code, and not by common drivers and modules.
48760a77442SJohn Baldwin.El
48860a77442SJohn Baldwin.Pp
48960a77442SJohn BaldwinAdditionally, any of the following optional flags may also be specified:
49060a77442SJohn Baldwin.Bl -tag -width ".Dv CTLFLAG_ANYBODY"
491dba9e9ccSRobert Watson.It Dv CTLFLAG_ANYBODY
492dba9e9ccSRobert WatsonAny user or process can write to this sysctl.
493dba9e9ccSRobert Watson.It Dv CTLFLAG_SECURE
494dba9e9ccSRobert WatsonThis sysctl can be written to only if the effective securelevel of the
495bd84dd2fSRuslan Ermilovprocess is \[<=] 0.
496dba9e9ccSRobert Watson.It Dv CTLFLAG_PRISON
497dba9e9ccSRobert WatsonThis sysctl can be written to by processes in
498dba9e9ccSRobert Watson.Xr jail 2 .
499dba9e9ccSRobert Watson.It Dv CTLFLAG_SKIP
500dba9e9ccSRobert WatsonWhen iterating the sysctl name space, do not list this sysctl.
501dba9e9ccSRobert Watson.It Dv CTLFLAG_TUN
5026b9597aaSAndrew ThompsonAdvisory flag that a system tunable also exists for this variable.
503*5eb1caa8SHans Petter SelaskyThe initial sysctl value is tried fetched once from the system
504*5eb1caa8SHans Petter Selaskyenviroment early during module load or system boot.
505*5eb1caa8SHans Petter Selasky.It Dv CTLFLAG_DYN
506*5eb1caa8SHans Petter SelaskyDynamically created OIDs automatically get this flag set.
507dba9e9ccSRobert Watson.El
508*5eb1caa8SHans Petter Selasky.Sh SECURITY CONSIDERATIONS
509dba9e9ccSRobert WatsonWhen creating new sysctls, careful attention should be paid to the security
510dba9e9ccSRobert Watsonimplications of the monitoring or management interface being created.
511dba9e9ccSRobert WatsonMost sysctls present in the kernel are read-only or writable only by the
512dba9e9ccSRobert Watsonsuperuser.
513dba9e9ccSRobert WatsonSysctls exporting extensive information on system data structures and
514dba9e9ccSRobert Watsonoperation, especially those implemented using procedures, will wish to
515dba9e9ccSRobert Watsonimplement access control to limit the undesired exposure of information about
516dba9e9ccSRobert Watsonother processes, network connections, etc.
517dba9e9ccSRobert Watson.Pp
518dba9e9ccSRobert WatsonThe following top level sysctl name spaces are commonly used:
519bd84dd2fSRuslan Ermilov.Bl -tag -width ".Va regression"
520bd84dd2fSRuslan Ermilov.It Va compat
521dba9e9ccSRobert WatsonCompatibility layer information.
522bd84dd2fSRuslan Ermilov.It Va debug
523dba9e9ccSRobert WatsonDebugging information.
524dba9e9ccSRobert WatsonVarious name spaces exist under
525bd84dd2fSRuslan Ermilov.Va debug .
526bd84dd2fSRuslan Ermilov.It Va hw
527dba9e9ccSRobert WatsonHardware and device driver information.
528bd84dd2fSRuslan Ermilov.It Va kern
529dba9e9ccSRobert WatsonKernel behavior tuning; generally deprecated in favor of more specific
530dba9e9ccSRobert Watsonname spaces.
531bd84dd2fSRuslan Ermilov.It Va machdep
532dba9e9ccSRobert WatsonMachine-dependent configuration parameters.
533bd84dd2fSRuslan Ermilov.It Va net
534dba9e9ccSRobert WatsonNetwork subsystem.
535dba9e9ccSRobert WatsonVarious protocols have name spaces under
536bd84dd2fSRuslan Ermilov.Va net .
537bd84dd2fSRuslan Ermilov.It Va regression
538dba9e9ccSRobert WatsonRegression test configuration and information.
539bd84dd2fSRuslan Ermilov.It Va security
540f4006affSGiorgos KeramidasSecurity and security-policy configuration and information.
541bd84dd2fSRuslan Ermilov.It Va sysctl
542dba9e9ccSRobert WatsonReserved name space for the implementation of sysctl.
543bd84dd2fSRuslan Ermilov.It Va user
544dba9e9ccSRobert WatsonConfiguration settings relating to user application behavior.
545f4006affSGiorgos KeramidasGenerally, configuring applications using kernel sysctls is discouraged.
546bd84dd2fSRuslan Ermilov.It Va vfs
547dba9e9ccSRobert WatsonVirtual file system configuration and information.
548bd84dd2fSRuslan Ermilov.It Va vm
549dba9e9ccSRobert WatsonVirtual memory subsystem configuration and information.
550dba9e9ccSRobert Watson.El
551dba9e9ccSRobert Watson.Sh EXAMPLES
552dba9e9ccSRobert WatsonSample use of
553bd84dd2fSRuslan Ermilov.Fn SYSCTL_DECL
554bd84dd2fSRuslan Ermilovto declare the
555bd84dd2fSRuslan Ermilov.Va security
556bd84dd2fSRuslan Ermilovsysctl tree for use by new nodes:
557dba9e9ccSRobert Watson.Bd -literal -offset indent
558dba9e9ccSRobert WatsonSYSCTL_DECL(_security);
559dba9e9ccSRobert Watson.Ed
560dba9e9ccSRobert Watson.Pp
561dba9e9ccSRobert WatsonExamples of integer, opaque, string, and procedure sysctls follow:
562dba9e9ccSRobert Watson.Bd -literal -offset indent
563dba9e9ccSRobert Watson/*
564dba9e9ccSRobert Watson * Example of a constant integer value.  Notice that the control
565dba9e9ccSRobert Watson * flags are CTLFLAG_RD, the variable pointer is NULL, and the
566dba9e9ccSRobert Watson * value is declared.
567dba9e9ccSRobert Watson */
568dba9e9ccSRobert WatsonSYSCTL_INT(_debug_sizeof, OID_AUTO, bio, CTLFLAG_RD, NULL,
569dba9e9ccSRobert Watson    sizeof(struct bio), "sizeof(struct bio)");
570dba9e9ccSRobert Watson
571dba9e9ccSRobert Watson/*
572dba9e9ccSRobert Watson * Example of a variable integer value.  Notice that the control
573dba9e9ccSRobert Watson * flags are CTLFLAG_RW, the variable pointer is set, and the
574dba9e9ccSRobert Watson * value is 0.
575dba9e9ccSRobert Watson */
576dba9e9ccSRobert Watsonstatic int	doingcache = 1;		/* 1 => enable the cache */
577dba9e9ccSRobert WatsonSYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
578dba9e9ccSRobert Watson    "Enable name cache");
579dba9e9ccSRobert Watson
580dba9e9ccSRobert Watson/*
581dba9e9ccSRobert Watson * Example of a variable string value.  Notice that the control
582dba9e9ccSRobert Watson * flags are CTLFLAG_RW, that the variable pointer and string
583dba9e9ccSRobert Watson * size are set.  Unlike newer sysctls, this older sysctl uses a
584dba9e9ccSRobert Watson * static oid number.
585dba9e9ccSRobert Watson */
586dba9e9ccSRobert Watsonchar kernelname[MAXPATHLEN] = "/kernel";	/* XXX bloat */
587dba9e9ccSRobert WatsonSYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, CTLFLAG_RW,
588dba9e9ccSRobert Watson    kernelname, sizeof(kernelname), "Name of kernel file booted");
589dba9e9ccSRobert Watson
590dba9e9ccSRobert Watson/*
591dba9e9ccSRobert Watson * Example of an opaque data type exported by sysctl.  Notice that
592dba9e9ccSRobert Watson * the variable pointer and size are provided, as well as a format
593dba9e9ccSRobert Watson * string for sysctl(8).
594dba9e9ccSRobert Watson */
595dba9e9ccSRobert Watsonstatic l_fp pps_freq;	/* scaled frequence offset (ns/s) */
596dba9e9ccSRobert WatsonSYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD,
597dba9e9ccSRobert Watson    &pps_freq, sizeof(pps_freq), "I", "");
598dba9e9ccSRobert Watson
599dba9e9ccSRobert Watson/*
600dba9e9ccSRobert Watson * Example of a procedure based sysctl exporting string
601dba9e9ccSRobert Watson * information.  Notice that the data type is declared, the NULL
602dba9e9ccSRobert Watson * variable pointer and 0 size, the function pointer, and the
603dba9e9ccSRobert Watson * format string for sysctl(8).
604dba9e9ccSRobert Watson */
605dba9e9ccSRobert WatsonSYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING |
606dba9e9ccSRobert Watson    CTLFLAG_RW, NULL, 0, sysctl_kern_timecounter_hardware, "A",
607dba9e9ccSRobert Watson    "");
608dba9e9ccSRobert Watson.Ed
609*5eb1caa8SHans Petter Selasky.Pp
610*5eb1caa8SHans Petter SelaskyThe following is an example of
611*5eb1caa8SHans Petter Selaskyhow to create a new top-level category
612*5eb1caa8SHans Petter Selaskyand how to hook up another subtree to an existing static node.
613*5eb1caa8SHans Petter SelaskyThis example does not use contexts,
614*5eb1caa8SHans Petter Selaskywhich results in tedious management of all intermediate oids,
615*5eb1caa8SHans Petter Selaskyas they need to be freed later on:
616*5eb1caa8SHans Petter Selasky.Bd -literal -offset indent
617*5eb1caa8SHans Petter Selasky#include <sys/sysctl.h>
618*5eb1caa8SHans Petter Selasky ...
619*5eb1caa8SHans Petter Selasky/*
620*5eb1caa8SHans Petter Selasky * Need to preserve pointers to newly created subtrees,
621*5eb1caa8SHans Petter Selasky * to be able to free them later:
622*5eb1caa8SHans Petter Selasky */
623*5eb1caa8SHans Petter Selaskystatic struct sysctl_oid *root1;
624*5eb1caa8SHans Petter Selaskystatic struct sysctl_oid *root2;
625*5eb1caa8SHans Petter Selaskystatic struct sysctl_oid *oidp;
626*5eb1caa8SHans Petter Selaskystatic int a_int;
627*5eb1caa8SHans Petter Selaskystatic char *string = "dynamic sysctl";
628*5eb1caa8SHans Petter Selasky ...
629*5eb1caa8SHans Petter Selasky
630*5eb1caa8SHans Petter Selaskyroot1 = SYSCTL_ADD_ROOT_NODE(NULL,
631*5eb1caa8SHans Petter Selasky	OID_AUTO, "newtree", CTLFLAG_RW, 0, "new top level tree");
632*5eb1caa8SHans Petter Selaskyoidp = SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(root1),
633*5eb1caa8SHans Petter Selasky	OID_AUTO, "newint", CTLFLAG_RW, &a_int, 0, "new int leaf");
634*5eb1caa8SHans Petter Selasky ...
635*5eb1caa8SHans Petter Selaskyroot2 = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_debug),
636*5eb1caa8SHans Petter Selasky	OID_AUTO, "newtree", CTLFLAG_RW, 0, "new tree under debug");
637*5eb1caa8SHans Petter Selaskyoidp = SYSCTL_ADD_STRING(NULL, SYSCTL_CHILDREN(root2),
638*5eb1caa8SHans Petter Selasky	OID_AUTO, "newstring", CTLFLAG_RD, string, 0, "new string leaf");
639*5eb1caa8SHans Petter Selasky.Ed
640*5eb1caa8SHans Petter Selasky.Pp
641*5eb1caa8SHans Petter SelaskyThis example creates the following subtrees:
642*5eb1caa8SHans Petter Selasky.Bd -literal -offset indent
643*5eb1caa8SHans Petter Selaskydebug.newtree.newstring
644*5eb1caa8SHans Petter Selaskynewtree.newint
645*5eb1caa8SHans Petter Selasky.Ed
646*5eb1caa8SHans Petter Selasky.Pp
647*5eb1caa8SHans Petter Selasky.Em "Care should be taken to free all OIDs once they are no longer needed!"
64891f3a6dbSYaroslav Tykhiy.Sh SYSCTL NAMING
649dba9e9ccSRobert WatsonWhen adding, modifying, or removing sysctl names, it is important to be
650dba9e9ccSRobert Watsonaware that these interfaces may be used by users, libraries, applications,
651f4006affSGiorgos Keramidasor documentation (such as published books), and are implicitly published application interfaces.
652dba9e9ccSRobert WatsonAs with other application interfaces, caution must be taken not to break
653dba9e9ccSRobert Watsonexisting applications, and to think about future use of new name spaces so as
654dba9e9ccSRobert Watsonto avoid the need to rename or remove interfaces that might be depended on in
655dba9e9ccSRobert Watsonthe future.
65691f3a6dbSYaroslav Tykhiy.Pp
65791f3a6dbSYaroslav TykhiyThe semantics chosen for a new sysctl should be as clear as possible,
65891f3a6dbSYaroslav Tykhiyand the name of the sysctl must closely reflect its semantics.
65991f3a6dbSYaroslav TykhiyTherefore the sysctl name deserves a fair amount of consideration.
66091f3a6dbSYaroslav TykhiyIt should be short but yet representative of the sysctl meaning.
66191f3a6dbSYaroslav TykhiyIf the name consists of several words, they should be separated by
66291f3a6dbSYaroslav Tykhiyunderscore characters, as in
66391f3a6dbSYaroslav Tykhiy.Va compute_summary_at_mount .
66491f3a6dbSYaroslav TykhiyUnderscore characters may be omitted only if the name consists of not more
66591f3a6dbSYaroslav Tykhiythan two words, each being not longer than four characters, as in
66691f3a6dbSYaroslav Tykhiy.Va bootfile .
66791f3a6dbSYaroslav TykhiyFor boolean sysctls, negative logic should be totally avoided.
66891f3a6dbSYaroslav TykhiyThat is, do not use names like
66991f3a6dbSYaroslav Tykhiy.Va no_foobar
67091f3a6dbSYaroslav Tykhiyor
67191f3a6dbSYaroslav Tykhiy.Va foobar_disable .
67291f3a6dbSYaroslav TykhiyThey are confusing and lead to configuration errors.
67391f3a6dbSYaroslav TykhiyUse positive logic instead:
67491f3a6dbSYaroslav Tykhiy.Va foobar ,
67591f3a6dbSYaroslav Tykhiy.Va foobar_enable .
67691f3a6dbSYaroslav Tykhiy.Pp
677*5eb1caa8SHans Petter SelaskyA temporary sysctl node OID that should not be relied upon must be designated
67891f3a6dbSYaroslav Tykhiyas such by a leading underscore character in its name. For example:
67991f3a6dbSYaroslav Tykhiy.Va _dirty_hack .
680dba9e9ccSRobert Watson.Sh SEE ALSO
681f3066f2bSGavin Atkinson.Xr sysctl 3 ,
682dba9e9ccSRobert Watson.Xr sysctl 8 ,
683dba9e9ccSRobert Watson.Xr sysctl_add_oid 9 ,
684dba9e9ccSRobert Watson.Xr sysctl_ctx_free 9 ,
685dba9e9ccSRobert Watson.Xr sysctl_ctx_init 9 ,
686dba9e9ccSRobert Watson.Xr sysctl_remove_oid 9
687dba9e9ccSRobert Watson.Sh HISTORY
688bd84dd2fSRuslan ErmilovThe
689dba9e9ccSRobert Watson.Xr sysctl 8
690bd84dd2fSRuslan Ermilovutility first appeared in
691dba9e9ccSRobert Watson.Bx 4.4 .
692dba9e9ccSRobert Watson.Sh AUTHORS
693bd84dd2fSRuslan Ermilov.An -nosplit
694bd84dd2fSRuslan ErmilovThe
695bd84dd2fSRuslan Ermilov.Nm sysctl
696bd84dd2fSRuslan Ermilovimplementation originally found in
697dba9e9ccSRobert Watson.Bx
698dba9e9ccSRobert Watsonhas been extensively rewritten by
699dba9e9ccSRobert Watson.An Poul-Henning Kamp
700dba9e9ccSRobert Watsonin order to add support for name lookups, name space iteration, and dynamic
701dba9e9ccSRobert Watsonaddition of MIB nodes.
702dba9e9ccSRobert Watson.Pp
703dba9e9ccSRobert WatsonThis man page was written by
704dba9e9ccSRobert Watson.An Robert N. M. Watson .
705