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.8 2008/05/01 23:36:43 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.
416 .Bl -tag -width SYSCTL_ADD_STRINGXX
417 .It Fn SYSCTL_ADD_OID
419 This macro is functionally equivalent to the
422 .It Fn SYSCTL_ADD_NODE
423 creates an oid of type
425 to which child oids may be added.
426 .It Fn SYSCTL_ADD_STRING
427 creates an oid that handles a zero-terminated character string.
428 .It Fn SYSCTL_ADD_INT
429 creates an oid that handles an
432 .It Fn SYSCTL_ADD_QUAD
433 creates an oid that handles a 64-bit
436 .It Fn SYSCTL_ADD_UQUAD
437 creates an oid that handles a 64-bit
440 .It Fn SYSCTL_ADD_UINT
441 creates an oid that handles an
444 .It Fn SYSCTL_ADD_LONG
445 creates an oid that handles a
448 .It Fn SYSCTL_ADD_ULONG
449 creates an oid that handles an
452 .It Fn SYSCTL_ADD_OPAQUE
453 creates an oid that handles any chunk of opaque data
454 of the size specified by the
457 which is a pointer to a
459 .It Fn SYSCTL_ADD_STRUCT
460 creates an oid that handles a
465 parameter will be set to
467 to provide proper hints to the
470 .It Fn SYSCTL_ADD_PROC
471 creates an oid with the specified
474 The handler is responsible for handling read and write requests
476 This oid type is especially useful
477 if the kernel data is not easily accessible,
478 or needs to be processed before exporting.
481 The following is an example of
482 how to create a new top-level category
483 and how to hook up another subtree to an existing static node.
484 This example does not use contexts,
485 which results in tedious management of all intermediate oids,
486 as they need to be freed later on:
488 #include <sys/sysctl.h>
490 /* Need to preserve pointers to newly created subtrees, to be able
491 * to free them later.
493 struct sysctl_oid *root1, *root2, *oidp;
495 char *string = "dynamic sysctl";
498 root1 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(/* tree top */),
499 OID_AUTO, "newtree", CTLFLAG_RW, 0, "new top level tree");
500 oidp = SYSCTL_ADD_INT( NULL, SYSCTL_CHILDREN(root1),
501 OID_AUTO, "newint", CTLFLAG_RW, &a_int, 0, "new int leaf");
503 root2 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(_debug),
504 OID_AUTO, "newtree", CTLFLAG_RW, 0, "new tree under debug");
505 oidp = SYSCTL_ADD_STRING( NULL, SYSCTL_CHILDREN(root2),
506 OID_AUTO, "newstring", CTLFLAG_RD, string, 0, "new string leaf");
509 This example creates the following subtrees:
510 .Bd -literal -offset indent
511 debug.newtree.newstring
515 .Em "Care should be taken to free all oids once they are no longer needed!"
519 .Xr sysctl_ctx_free 9 ,
520 .Xr sysctl_ctx_init 9
522 These functions first appeared in
525 .An Andrzej Bialecki Aq abial@FreeBSD.org
527 Sharing nodes between many code sections
528 causes interdependencies that sometimes may lock the resources.
530 if module A hooks up a subtree to an oid created by module B,
531 module B will be unable to delete that oid.
532 These issues are handled properly by sysctl contexts.
534 Many operations on the tree involve traversing linked lists.
535 For this reason, oid creation and removal is relatively costly.