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
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 *
160 .Fo SYSCTL_ADD_OPAQUE
161 .Fa "struct sysctl_ctx_list *ctx"
162 .Fa "struct sysctl_oid_list *parent"
164 .Fa "const char *name"
168 .Fa "const char *format"
169 .Fa "const char *descr"
171 .Ft struct sysctl_oid *
172 .Fo SYSCTL_ADD_STRUCT
173 .Fa "struct sysctl_ctx_list *ctx"
174 .Fa "struct sysctl_oid_list *parent"
176 .Fa "const char *name"
180 .Fa "const char *descr"
182 .Ft struct sysctl_oid *
184 .Fa "struct sysctl_ctx_list *ctx"
185 .Fa "struct sysctl_oid_list *parent"
187 .Fa "const char *name"
191 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
192 .Fa "const char *format"
193 .Fa "const char *descr"
196 These functions and macros provide an interface
197 for creating and deleting sysctl oids at runtime
198 (e.g.\& during lifetime of a module).
199 The alternative method,
200 based on linker sets (see
203 .\" XXX Manual pages should avoid referencing source files
204 .Pa src/sys/kern/kern_sysctl.c
205 for details), only allows creation and deletion
206 on module load and unload respectively.
211 so that several code sections can create and delete them,
212 but in reality they are allocated and freed
213 based on their reference count.
215 it is possible for two or more code sections
216 to create partially overlapping trees that they both can use.
217 It is not possible to create overlapping leaves,
218 nor to create different child types with the same name and parent.
220 Newly created oids are connected to their parent nodes.
221 In all these functions and macros
222 (with the exception of
223 .Fn sysctl_remove_oid ) ,
224 one of the required parameters is
226 which points to the head of the parent's list of children.
228 Most top level categories are created statically.
229 When connecting to existing static oids,
230 this pointer can be obtained with the
231 .Fn SYSCTL_STATIC_CHILDREN
234 argument is name of the parent oid of type
236 (i.e., the name displayed by
238 preceded by underscore, and with all dots replaced with underscores).
240 When connecting to an existing dynamic oid, this pointer
241 can be obtained with the
245 argument points to the parent oid of type
250 function creates raw oids of any type.
251 If the oid is successfully created,
252 the function returns a pointer to it;
255 Many of the arguments for
257 are common to the macros.
258 The arguments are as follows:
259 .Bl -tag -width handler
261 A pointer to an optional sysctl context, or
264 .Xr sysctl_ctx_init 9
266 Programmers are strongly advised to use contexts
267 to organize the dynamic oids which they create,
268 unless special creation and deletion sequences are required.
273 the newly created oid will be added to this context
277 .Li struct sysctl_oid_list ,
278 which is the head of the parent's list of children.
280 The oid number that will be assigned to this oid.
281 In almost all cases this should be set to
283 which will result in the assignment of the next available oid number.
286 The newly created oid will contain a copy of the name.
289 specified as a bit mask of the type and access values defined in the
292 Oids created dynamically always have the
295 Access flags specify whether this oid is read-only or read-write,
296 and whether it may be modified by all users
297 or by the superuser only.
299 A pointer to any data that the oid should reference, or
309 A pointer to the function
310 that is responsible for handling read and write requests
312 There are several standard handlers
313 that support operations on nodes,
314 integers, strings and opaque objects.
315 It is possible also to define new handlers using the
319 A pointer to a string
320 which specifies the format of the oid symbolically.
321 This format is used as a hint by
323 to apply proper data formatting for display purposes.
324 Currently used format names are:
348 A pointer to a textual description of the oid.
353 function reparents an existing oid.
354 The oid is assigned a new number as if it had been created with
360 .Fn sysctl_remove_oid
361 function removes a dynamically created oid from the tree,
362 optionally freeing its resources.
363 It takes the following arguments:
364 .Bl -tag -width recurse
366 A pointer to the dynamic oid to be removed.
367 If the oid is not dynamic, or the pointer is
373 .Fn sysctl_remove_oid
374 will try to free the oid's resources
375 when the reference count of the oid becomes zero.
379 the routine will only deregister the oid from the tree,
380 without freeing its resources.
381 This behaviour is useful when the caller expects to rollback
382 (possibly partially failed)
383 deletion of many oids later.
385 If non-zero, attempt to remove the node and all its children.
389 any attempt to remove a node that contains any children
393 .Em WARNING : "use recursive deletion with extreme caution" !
394 Normally it should not be needed if contexts are used.
395 Contexts take care of tracking inter-dependencies
396 between users of the tree.
397 However, in some extreme cases it might be necessary
398 to remove part of the subtree no matter how it was created,
399 in order to free some other resources.
400 Be aware, though, that this may result in a system
402 if other code sections continue to use removed subtrees.
405 .\" XXX sheldonh finished up to here
406 Again, in most cases the programmer should use contexts,
408 .Xr sysctl_ctx_init 9 ,
409 to keep track of created oids,
410 and to delete them later in orderly fashion.
412 There is a set of macros defined
413 that helps to create oids of given type.
415 .Bl -tag -width SYSCTL_ADD_STRINGXX
416 .It Fn SYSCTL_ADD_OID
418 This macro is functionally equivalent to the
421 .It Fn SYSCTL_ADD_NODE
422 creates an oid of type
424 to which child oids may be added.
425 .It Fn SYSCTL_ADD_STRING
426 creates an oid that handles a zero-terminated character string.
427 .It Fn SYSCTL_ADD_INT
428 creates an oid that handles an
431 .It Fn SYSCTL_ADD_UINT
432 creates an oid that handles an
435 .It Fn SYSCTL_ADD_LONG
436 creates an oid that handles a
439 .It Fn SYSCTL_ADD_ULONG
440 creates an oid that handles an
443 .It Fn SYSCTL_ADD_QUAD
444 creates an oid that handles an
447 .It Fn SYSCTL_ADD_OPAQUE
448 creates an oid that handles any chunk of opaque data
449 of the size specified by the
452 which is a pointer to a
454 .It Fn SYSCTL_ADD_STRUCT
455 creates an oid that handles a
460 parameter will be set to
462 to provide proper hints to the
465 .It Fn SYSCTL_ADD_PROC
466 creates an oid with the specified
469 The handler is responsible for handling read and write requests
471 This oid type is especially useful
472 if the kernel data is not easily accessible,
473 or needs to be processed before exporting.
476 The following is an example of
477 how to create a new top-level category
478 and how to hook up another subtree to an existing static node.
479 This example does not use contexts,
480 which results in tedious management of all intermediate oids,
481 as they need to be freed later on:
483 #include <sys/sysctl.h>
485 /* Need to preserve pointers to newly created subtrees, to be able
486 * to free them later.
488 struct sysctl_oid *root1, *root2, *oidp;
490 char *string = "dynamic sysctl";
493 root1 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(/* tree top */),
494 OID_AUTO, "newtree", CTLFLAG_RW, 0, "new top level tree");
495 oidp = SYSCTL_ADD_INT( NULL, SYSCTL_CHILDREN(root1),
496 OID_AUTO, "newint", CTLFLAG_RW, &a_int, 0, "new int leaf");
498 root2 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(_debug),
499 OID_AUTO, "newtree", CTLFLAG_RW, 0, "new tree under debug");
500 oidp = SYSCTL_ADD_STRING( NULL, SYSCTL_CHILDREN(root2),
501 OID_AUTO, "newstring", CTLFLAG_RD, string, 0, "new string leaf");
504 This example creates the following subtrees:
505 .Bd -literal -offset indent
506 debug.newtree.newstring
510 .Em "Care should be taken to free all oids once they are no longer needed!"
514 .Xr sysctl_ctx_free 9 ,
515 .Xr sysctl_ctx_init 9
517 These functions first appeared in
520 .An Andrzej Bialecki Aq abial@FreeBSD.org
522 Sharing nodes between many code sections
523 causes interdependencies that sometimes may lock the resources.
525 if module A hooks up a subtree to an oid created by module B,
526 module B will be unable to delete that oid.
527 These issues are handled properly by sysctl contexts.
529 Many operations on the tree involve traversing linked lists.
530 For this reason, oid creation and removal is relatively costly.