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
28 .\" $FreeBSD: src/share/man/man9/sysctl_add_oid.9,v 1.3.2.10 2001/12/17 11:30:19 ru Exp $
29 .\" $DragonFly: src/share/man/man9/sysctl_add_oid.9,v 1.7 2007/04/07 19:29:52 swildner Exp $
37 .Nd runtime sysctl tree manipulation
40 .Ft struct sysctl_oid *
42 .Fa "struct sysctl_ctx_list *ctx"
43 .Fa "struct sysctl_oid_list *parent"
45 .Fa "const char *name"
49 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
50 .Fa "const char *format"
51 .Fa "const char *descr"
55 .Fa "struct sysctl_oid *oidp"
59 .Ft struct sysctl_oid_list *
61 .Fa "struct sysctl_oid *oidp"
63 .Ft struct sysctl_oid_list *
64 .Fo SYSCTL_STATIC_CHILDREN
67 .Ft struct sysctl_oid *
69 .Fa "struct sysctl_ctx_list *ctx"
70 .Fa "struct sysctl_oid_list *parent"
76 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
77 .Fa "const char *format"
78 .Fa "const char *descr"
80 .Ft struct sysctl_oid *
82 .Fa "struct sysctl_ctx_list *ctx"
83 .Fa "struct sysctl_oid_list *parent"
87 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
88 .Fa "const char *descr"
90 .Ft struct sysctl_oid *
92 .Fa "struct sysctl_ctx_list *ctx"
93 .Fa "struct sysctl_oid_list *parent"
99 .Fa "const char *descr"
101 .Ft struct sysctl_oid *
103 .Fa "struct sysctl_ctx_list *ctx"
104 .Fa "struct sysctl_oid_list *parent"
110 .Fa "const char *descr"
112 .Ft struct sysctl_oid *
114 .Fa "struct sysctl_ctx_list *ctx"
115 .Fa "struct sysctl_oid_list *parent"
121 .Fa "const char *descr"
123 .Ft struct sysctl_oid *
125 .Fa "struct sysctl_ctx_list *ctx"
126 .Fa "struct sysctl_oid_list *parent"
132 .Fa "const char *descr"
134 .Ft struct sysctl_oid *
136 .Fa "struct sysctl_ctx_list *ctx"
137 .Fa "struct sysctl_oid_list *parent"
141 .Fa "unsigned int *arg"
143 .Fa "const char *descr"
145 .Ft struct sysctl_oid *
147 .Fa "struct sysctl_ctx_list *ctx"
148 .Fa "struct sysctl_oid_list *parent"
154 .Fa "const char *descr"
156 .Ft struct sysctl_oid *
158 .Fa "struct sysctl_ctx_list *ctx"
159 .Fa "struct sysctl_oid_list *parent"
163 .Fa "unsigned long *arg"
165 .Fa "const char *descr"
167 .Ft struct sysctl_oid *
168 .Fo SYSCTL_ADD_OPAQUE
169 .Fa "struct sysctl_ctx_list *ctx"
170 .Fa "struct sysctl_oid_list *parent"
176 .Fa "const char *format"
177 .Fa "const char *descr"
179 .Ft struct sysctl_oid *
180 .Fo SYSCTL_ADD_STRUCT
181 .Fa "struct sysctl_ctx_list *ctx"
182 .Fa "struct sysctl_oid_list *parent"
186 .Fa "struct TYPE *arg"
188 .Fa "const char *descr"
190 .Ft struct sysctl_oid *
192 .Fa "struct sysctl_ctx_list *ctx"
193 .Fa "struct sysctl_oid_list *parent"
199 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
200 .Fa "const char *format"
201 .Fa "const char *descr"
204 These functions and macros provide an interface
205 for creating and deleting sysctl oids at runtime
206 (e.g.\& during lifetime of a module).
207 The alternative method,
208 based on linker sets (see
211 .\" XXX Manual pages should avoid referencing source files
212 .Pa src/sys/kern/kern_sysctl.c
213 for details), only allows creation and deletion
214 on module load and unload respectively.
219 so that several code sections can create and delete them,
220 but in reality they are allocated and freed
221 based on their reference count.
223 it is possible for two or more code sections
224 to create partially overlapping trees that they both can use.
225 It is not possible to create overlapping leaves,
226 nor to create different child types with the same name and parent.
228 Newly created oids are connected to their parent nodes.
229 In all these functions and macros
230 (with the exception of
231 .Fn sysctl_remove_oid ) ,
232 one of the required parameters is
234 which points to the head of the parent's list of children.
236 Most top level categories are created statically.
237 When connecting to existing static oids,
238 this pointer can be obtained with the
239 .Fn SYSCTL_STATIC_CHILDREN
242 argument is name of the parent oid of type
244 (i.e., the name displayed by
246 preceded by underscore, and with all dots replaced with underscores).
248 When connecting to an existing dynamic oid, this pointer
249 can be obtained with the
253 argument points to the parent oid of type
258 function creates raw oids of any type.
259 If the oid is successfully created,
260 the function returns a pointer to it;
263 Many of the arguments for
265 are common to the macros.
266 The arguments are as follows:
267 .Bl -tag -width handler
269 A pointer to an optional sysctl context, or
272 .Xr sysctl_ctx_init 9
274 Programmers are strongly advised to use contexts
275 to organize the dynamic oids which they create,
276 unless special creation and deletion sequences are required.
281 the newly created oid will be added to this context
285 .Li struct sysctl_oid_list ,
286 which is the head of the parent's list of children.
288 The oid number that will be assigned to this oid.
289 In almost all cases this should be set to
291 which will result in the assignment of the next available oid number.
294 The newly created oid will contain a copy of the name.
297 specified as a bit mask of the type and access values defined in the
300 Oids created dynamically always have the
303 Access flags specify whether this oid is read-only or read-write,
304 and whether it may be modified by all users
305 or by the superuser only.
307 A pointer to any data that the oid should reference, or
317 A pointer to the function
318 that is responsible for handling read and write requests
320 There are several standard handlers
321 that support operations on nodes,
322 integers, strings and opaque objects.
323 It is possible also to define new handlers using the
327 A pointer to a string
328 which specifies the format of the oid symbolically.
329 This format is used as a hint by
331 to apply proper data formatting for display purposes.
332 Currently used format names are:
356 A pointer to a textual description of the oid.
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.
414 .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_QUAD
432 creates an oid that handles a 64-bit
435 .It Fn SYSCTL_ADD_UQUAD
436 creates an oid that handles a 64-bit
439 .It Fn SYSCTL_ADD_UINT
440 creates an oid that handles an
443 .It Fn SYSCTL_ADD_LONG
444 creates an oid that handles a
447 .It Fn SYSCTL_ADD_ULONG
448 creates an oid that handles an
451 .It Fn SYSCTL_ADD_OPAQUE
452 creates an oid that handles any chunk of opaque data
453 of the size specified by the
456 which is a pointer to a
458 .It Fn SYSCTL_ADD_STRUCT
459 creates an oid that handles a
464 parameter will be set to
466 to provide proper hints to the
469 .It Fn SYSCTL_ADD_PROC
470 creates an oid with the specified
473 The handler is responsible for handling read and write requests
475 This oid type is especially useful
476 if the kernel data is not easily accessible,
477 or needs to be processed before exporting.
480 The following is an example of
481 how to create a new top-level category
482 and how to hook up another subtree to an existing static node.
483 This example does not use contexts,
484 which results in tedious management of all intermediate oids,
485 as they need to be freed later on:
487 #include <sys/sysctl.h>
489 /* Need to preserve pointers to newly created subtrees, to be able
490 * to free them later.
492 struct sysctl_oid *root1, *root2, *oidp;
494 char *string = "dynamic sysctl";
497 root1 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(/* tree top */),
498 OID_AUTO, "newtree", CTLFLAG_RW, 0, "new top level tree");
499 oidp = SYSCTL_ADD_INT( NULL, SYSCTL_CHILDREN(root1),
500 OID_AUTO, "newint", CTLFLAG_RW, &a_int, 0, "new int leaf");
502 root2 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(_debug),
503 OID_AUTO, "newtree", CTLFLAG_RW, 0, "new tree under debug");
504 oidp = SYSCTL_ADD_STRING( NULL, SYSCTL_CHILDREN(root2),
505 OID_AUTO, "newstring", CTLFLAG_RD, string, 0, "new string leaf");
508 This example creates the following subtrees:
509 .Bd -literal -offset indent
510 debug.newtree.newstring
514 .Em "Care should be taken to free all oids once they are no longer needed!"
518 .Xr sysctl_ctx_free 9 ,
519 .Xr sysctl_ctx_init 9
521 These functions first appeared in
524 .An Andrzej Bialecki Aq abial@FreeBSD.org
526 Sharing nodes between many code sections
527 causes interdependencies that sometimes may lock the resources.
529 if module A hooks up a subtree to an oid created by module B,
530 module B will be unable to delete that oid.
531 These issues are handled properly by sysctl contexts.
533 Many operations on the tree involve traversing linked lists.
534 For this reason, oid creation and removal is relatively costly.