2 .\" Copyright (c) 2000, Andrzej Bialecki <abial@FreeBSD.org>
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
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 .\" 3. The name of the author may not be used to endorse or promote products
14 .\" derived from this software without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .Dd September 12, 2013
37 .Nd runtime sysctl tree manipulation
41 .Ft struct sysctl_oid *
43 .Fa "struct sysctl_ctx_list *ctx"
44 .Fa "struct sysctl_oid_list *parent"
46 .Fa "const char *name"
50 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
51 .Fa "const char *format"
52 .Fa "const char *descr"
56 .Fa "struct sysctl_oid *oidp"
57 .Fa "struct sysctl_oid_list *parent"
61 .Fa "struct sysctl_oid *oidp"
65 .Ft struct sysctl_oid_list *
67 .Fa "struct sysctl_oid *oidp"
69 .Ft struct sysctl_oid_list *
70 .Fo SYSCTL_STATIC_CHILDREN
71 .Fa "struct sysctl_oid_list OID_NAME"
73 .Ft struct sysctl_oid *
75 .Fa "struct sysctl_ctx_list *ctx"
76 .Fa "struct sysctl_oid_list *parent"
78 .Fa "const char *name"
82 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
83 .Fa "const char *format"
84 .Fa "const char *descr"
86 .Ft struct sysctl_oid *
88 .Fa "struct sysctl_ctx_list *ctx"
89 .Fa "struct sysctl_oid_list *parent"
91 .Fa "const char *name"
93 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
94 .Fa "const char *descr"
96 .Ft struct sysctl_oid *
98 .Fa "struct sysctl_ctx_list *ctx"
99 .Fa "struct sysctl_oid_list *parent"
101 .Fa "const char *name"
105 .Fa "const char *descr"
107 .Ft struct sysctl_oid *
109 .Fa "struct sysctl_ctx_list *ctx"
110 .Fa "struct sysctl_oid_list *parent"
112 .Fa "const char *name"
116 .Fa "const char *descr"
118 .Ft struct sysctl_oid *
120 .Fa "struct sysctl_ctx_list *ctx"
121 .Fa "struct sysctl_oid_list *parent"
123 .Fa "const char *name"
125 .Fa "unsigned int *arg"
127 .Fa "const char *descr"
129 .Ft struct sysctl_oid *
131 .Fa "struct sysctl_ctx_list *ctx"
132 .Fa "struct sysctl_oid_list *parent"
134 .Fa "const char *name"
137 .Fa "const char *descr"
139 .Ft struct sysctl_oid *
141 .Fa "struct sysctl_ctx_list *ctx"
142 .Fa "struct sysctl_oid_list *parent"
144 .Fa "const char *name"
146 .Fa "unsigned long *arg"
147 .Fa "const char *descr"
149 .Ft struct sysctl_oid *
151 .Fa "struct sysctl_ctx_list *ctx"
152 .Fa "struct sysctl_oid_list *parent"
154 .Fa "const char *name"
157 .Fa "const char *descr"
159 .Ft struct sysctl_oid *
161 .Fa "struct sysctl_ctx_list *ctx"
162 .Fa "struct sysctl_oid_list *parent"
164 .Fa "const char *name"
167 .Fa "const char *descr"
169 .Ft struct sysctl_oid *
170 .Fo SYSCTL_ADD_OPAQUE
171 .Fa "struct sysctl_ctx_list *ctx"
172 .Fa "struct sysctl_oid_list *parent"
174 .Fa "const char *name"
178 .Fa "const char *format"
179 .Fa "const char *descr"
181 .Ft struct sysctl_oid *
182 .Fo SYSCTL_ADD_STRUCT
183 .Fa "struct sysctl_ctx_list *ctx"
184 .Fa "struct sysctl_oid_list *parent"
186 .Fa "const char *name"
190 .Fa "const char *descr"
192 .Ft struct sysctl_oid *
194 .Fa "struct sysctl_ctx_list *ctx"
195 .Fa "struct sysctl_oid_list *parent"
197 .Fa "const char *name"
201 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
202 .Fa "const char *format"
203 .Fa "const char *descr"
206 These functions and macros provide an interface
207 for creating and deleting sysctl oids at runtime
208 (e.g.\& during lifetime of a module).
209 The alternative method,
210 based on linker sets (see
213 .\" XXX Manual pages should avoid referencing source files
214 .Pa src/sys/kern/kern_sysctl.c
215 for details), only allows creation and deletion
216 on module load and unload respectively.
221 so that several code sections can create and delete them,
222 but in reality they are allocated and freed
223 based on their reference count.
225 it is possible for two or more code sections
226 to create partially overlapping trees that they both can use.
227 It is not possible to create overlapping leaves,
228 nor to create different child types with the same name and parent.
230 Newly created oids are connected to their parent nodes.
231 In all these functions and macros
232 (with the exception of
233 .Fn sysctl_remove_oid ) ,
234 one of the required parameters is
236 which points to the head of the parent's list of children.
238 Most top level categories are created statically.
239 When connecting to existing static oids,
240 this pointer can be obtained with the
241 .Fn SYSCTL_STATIC_CHILDREN
244 argument is name of the parent oid of type
246 (i.e., the name displayed by
248 preceded by underscore, and with all dots replaced with underscores).
250 When connecting to an existing dynamic oid, this pointer
251 can be obtained with the
255 argument points to the parent oid of type
260 function creates raw oids of any type.
261 If the oid is successfully created,
262 the function returns a pointer to it;
265 Many of the arguments for
267 are common to the macros.
268 The arguments are as follows:
269 .Bl -tag -width handler
271 A pointer to an optional sysctl context, or
274 .Xr sysctl_ctx_init 9
276 Programmers are strongly advised to use contexts
277 to organize the dynamic oids which they create,
278 unless special creation and deletion sequences are required.
283 the newly created oid will be added to this context
287 .Li struct sysctl_oid_list ,
288 which is the head of the parent's list of children.
290 The oid number that will be assigned to this oid.
291 In almost all cases this should be set to
293 which will result in the assignment of the next available oid number.
296 The newly created oid will contain a copy of the name.
299 specified as a bit mask of the type and access values defined in the
302 Oids created dynamically always have the
305 Access flags specify whether this oid is read-only or read-write,
306 and whether it may be modified by all users
307 or by the superuser only.
309 A pointer to any data that the oid should reference, or
319 A pointer to the function
320 that is responsible for handling read and write requests
322 There are several standard handlers
323 that support operations on nodes,
324 integers, strings and opaque objects.
325 It is possible also to define new handlers using the
329 A pointer to a string
330 which specifies the format of the oid symbolically.
331 This format is used as a hint by
333 to apply proper data formatting for display purposes.
334 Currently used format names are:
358 A pointer to a textual description of the oid.
363 function reparents an existing oid.
364 The oid is assigned a new number as if it had been created with
370 .Fn sysctl_remove_oid
371 function removes a dynamically created oid from the tree,
372 optionally freeing its resources.
373 It takes the following arguments:
374 .Bl -tag -width recurse
376 A pointer to the dynamic oid to be removed.
377 If the oid is not dynamic, or the pointer is
383 .Fn sysctl_remove_oid
384 will try to free the oid's resources
385 when the reference count of the oid becomes zero.
389 the routine will only deregister the oid from the tree,
390 without freeing its resources.
391 This behaviour is useful when the caller expects to rollback
392 (possibly partially failed)
393 deletion of many oids later.
395 If non-zero, attempt to remove the node and all its children.
399 any attempt to remove a node that contains any children
403 .Em WARNING : "use recursive deletion with extreme caution" !
404 Normally it should not be needed if contexts are used.
405 Contexts take care of tracking inter-dependencies
406 between users of the tree.
407 However, in some extreme cases it might be necessary
408 to remove part of the subtree no matter how it was created,
409 in order to free some other resources.
410 Be aware, though, that this may result in a system
412 if other code sections continue to use removed subtrees.
415 .\" XXX sheldonh finished up to here
416 Again, in most cases the programmer should use contexts,
418 .Xr sysctl_ctx_init 9 ,
419 to keep track of created oids,
420 and to delete them later in orderly fashion.
422 There is a set of macros defined
423 that helps to create oids of given type.
425 .Bl -tag -width SYSCTL_ADD_STRINGXX
426 .It Fn SYSCTL_ADD_OID
428 This macro is functionally equivalent to the
431 .It Fn SYSCTL_ADD_NODE
432 creates an oid of type
434 to which child oids may be added.
435 .It Fn SYSCTL_ADD_STRING
436 creates an oid that handles a zero-terminated character string.
437 .It Fn SYSCTL_ADD_INT
438 creates an oid that handles an
441 .It Fn SYSCTL_ADD_UINT
442 creates an oid that handles an
445 .It Fn SYSCTL_ADD_LONG
446 creates an oid that handles a
449 .It Fn SYSCTL_ADD_ULONG
450 creates an oid that handles an
453 .It Fn SYSCTL_ADD_QUAD
454 creates an oid that handles an
457 .It Fn SYSCTL_ADD_OPAQUE
458 creates an oid that handles any chunk of opaque data
459 of the size specified by the
462 which is a pointer to a
464 .It Fn SYSCTL_ADD_STRUCT
465 creates an oid that handles a
470 parameter will be set to
472 to provide proper hints to the
475 .It Fn SYSCTL_ADD_PROC
476 creates an oid with the specified
479 The handler is responsible for handling read and write requests
481 This oid type is especially useful
482 if the kernel data is not easily accessible,
483 or needs to be processed before exporting.
486 The following is an example of
487 how to create a new top-level category
488 and how to hook up another subtree to an existing static node.
489 This example does not use contexts,
490 which results in tedious management of all intermediate oids,
491 as they need to be freed later on:
493 #include <sys/sysctl.h>
495 /* Need to preserve pointers to newly created subtrees, to be able
496 * to free them later.
498 struct sysctl_oid *root1, *root2, *oidp;
500 char *string = "dynamic sysctl";
503 root1 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(/* tree top */),
504 OID_AUTO, "newtree", CTLFLAG_RW, 0, "new top level tree");
505 oidp = SYSCTL_ADD_INT( NULL, SYSCTL_CHILDREN(root1),
506 OID_AUTO, "newint", CTLFLAG_RW, &a_int, 0, "new int leaf");
508 root2 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(_debug),
509 OID_AUTO, "newtree", CTLFLAG_RW, 0, "new tree under debug");
510 oidp = SYSCTL_ADD_STRING( NULL, SYSCTL_CHILDREN(root2),
511 OID_AUTO, "newstring", CTLFLAG_RD, string, 0, "new string leaf");
514 This example creates the following subtrees:
515 .Bd -literal -offset indent
516 debug.newtree.newstring
520 .Em "Care should be taken to free all oids once they are no longer needed!"
524 .Xr sysctl_ctx_free 9 ,
525 .Xr sysctl_ctx_init 9
527 These functions first appeared in
530 .An Andrzej Bialecki Aq abial@FreeBSD.org
532 Sharing nodes between many code sections
533 causes interdependencies that sometimes may lock the resources.
535 if module A hooks up a subtree to an oid created by module B,
536 module B will be unable to delete that oid.
537 These issues are handled properly by sysctl contexts.
539 Many operations on the tree involve traversing linked lists.
540 For this reason, oid creation and removal is relatively costly.