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.21 2006/04/28 10:45:27 rwatson Exp $
36 .Nd runtime sysctl tree manipulation
39 .Ft struct sysctl_oid *
41 .Fa "struct sysctl_ctx_list *ctx"
42 .Fa "struct sysctl_oid_list *parent"
44 .Fa "const char *name"
48 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
49 .Fa "const char *format"
50 .Fa "const char *descr"
54 .Fa "struct sysctl_oid *oidp"
58 .Ft struct sysctl_oid_list *
60 .Fa "struct sysctl_oid *oidp"
62 .Ft struct sysctl_oid_list *
63 .Fo SYSCTL_STATIC_CHILDREN
64 .Fa "struct sysctl_oid_list oid_name"
66 .Ft struct sysctl_oid *
68 .Fa "struct sysctl_ctx_list *ctx"
69 .Fa "struct sysctl_oid_list *parent"
71 .Fa "const char *name"
75 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
76 .Fa "const char *format"
77 .Fa "const char *descr"
79 .Ft struct sysctl_oid *
81 .Fa "struct sysctl_ctx_list *ctx"
82 .Fa "struct sysctl_oid_list *parent"
84 .Fa "const char *name"
86 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
87 .Fa "const char *descr"
89 .Ft struct sysctl_oid *
91 .Fa "struct sysctl_ctx_list *ctx"
92 .Fa "struct sysctl_oid_list *parent"
94 .Fa "const char *name"
98 .Fa "const char *descr"
100 .Ft struct sysctl_oid *
102 .Fa "struct sysctl_ctx_list *ctx"
103 .Fa "struct sysctl_oid_list *parent"
105 .Fa "const char *name"
109 .Fa "const char *descr"
111 .Ft struct sysctl_oid *
113 .Fa "struct sysctl_ctx_list *ctx"
114 .Fa "struct sysctl_oid_list *parent"
116 .Fa "const char *name"
118 .Fa "unsigned int *arg"
120 .Fa "const char *descr"
122 .Ft struct sysctl_oid *
124 .Fa "struct sysctl_ctx_list *ctx"
125 .Fa "struct sysctl_oid_list *parent"
127 .Fa "const char *name"
130 .Fa "const char *descr"
132 .Ft struct sysctl_oid *
134 .Fa "struct sysctl_ctx_list *ctx"
135 .Fa "struct sysctl_oid_list *parent"
137 .Fa "const char *name"
139 .Fa "unsigned long *arg"
140 .Fa "const char *descr"
142 .Ft struct sysctl_oid *
144 .Fa "struct sysctl_ctx_list *ctx"
145 .Fa "struct sysctl_oid_list *parent"
147 .Fa "const char *name"
151 .Fa "const char *descr"
153 .Ft struct sysctl_oid *
155 .Fa "struct sysctl_ctx_list *ctx"
156 .Fa "struct sysctl_oid_list *parent"
158 .Fa "const char *name"
162 .Fa "const char *descr"
164 .Ft struct sysctl_oid *
165 .Fo SYSCTL_ADD_OPAQUE
166 .Fa "struct sysctl_ctx_list *ctx"
167 .Fa "struct sysctl_oid_list *parent"
169 .Fa "const char *name"
173 .Fa "const char *format"
174 .Fa "const char *descr"
176 .Ft struct sysctl_oid *
177 .Fo SYSCTL_ADD_STRUCT
178 .Fa "struct sysctl_ctx_list *ctx"
179 .Fa "struct sysctl_oid_list *parent"
181 .Fa "const char *name"
185 .Fa "const char *descr"
187 .Ft struct sysctl_oid *
189 .Fa "struct sysctl_ctx_list *ctx"
190 .Fa "struct sysctl_oid_list *parent"
192 .Fa "const char *name"
196 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
197 .Fa "const char *format"
198 .Fa "const char *descr"
201 These functions and macros provide an interface
202 for creating and deleting sysctl oids at runtime
203 (e.g.\& during lifetime of a module).
204 The alternative method,
205 based on linker sets (see
208 .\" XXX Manual pages should avoid referencing source files
209 .Pa /sys/kern/kern_sysctl.c
210 for details), only allows creation and deletion
211 on module load and unload respectively.
216 so that several code sections can create and delete them,
217 but in reality they are allocated and freed
218 based on their reference count.
220 it is possible for two or more code sections
221 to create partially overlapping trees that they both can use.
222 It is not possible to create overlapping leaves,
223 nor to create different child types with the same name and parent.
225 Newly created oids are connected to their parent nodes.
226 In all these functions and macros
227 (with the exception of
228 .Fn sysctl_remove_oid ) ,
229 one of the required parameters is
231 which points to the head of the parent's list of children.
233 Most top level categories are created statically.
234 When connecting to existing static oids,
235 this pointer can be obtained with the
236 .Fn SYSCTL_STATIC_CHILDREN
239 argument is name of the parent oid of type
241 (i.e., the name displayed by
243 preceded by underscore, and with all dots replaced with underscores).
245 When connecting to an existing dynamic oid, this pointer
246 can be obtained with the
250 argument points to the parent oid of type
255 function creates raw oids of any type.
256 If the oid is successfully created,
257 the function returns a pointer to it;
260 Many of the arguments for
262 are common to the macros.
263 The arguments are as follows:
264 .Bl -tag -width handler
266 A pointer to an optional sysctl context, or
269 .Xr sysctl_ctx_init 9
271 Programmers are strongly advised to use contexts
272 to organize the dynamic oids which they create,
273 unless special creation and deletion sequences are required.
278 the newly created oid will be added to this context
282 .Li struct sysctl_oid_list ,
283 which is the head of the parent's list of children.
285 The oid number that will be assigned to this oid.
286 In almost all cases this should be set to
288 which will result in the assignment of the next available oid number.
291 The newly created oid will contain a copy of the name.
294 specified as a bit mask of the type and access values defined in the
297 Oids created dynamically always have the
300 Access flags specify whether this oid is read-only or read-write,
301 and whether it may be modified by all users
302 or by the superuser only.
304 A pointer to any data that the oid should reference, or
314 A pointer to the function
315 that is responsible for handling read and write requests
317 There are several standard handlers
318 that support operations on nodes,
319 integers, strings and opaque objects.
320 It is possible also to define new handlers using the
324 A pointer to a string
325 which specifies the format of the oid symbolically.
326 This format is used as a hint by
328 to apply proper data formatting for display purposes.
329 Currently used format names are:
342 for temperature in tenths of kelvins,
355 A pointer to a textual description of the oid.
359 .Fn sysctl_remove_oid
360 function removes a dynamically created oid from the tree,
361 optionally freeing its resources.
362 It takes the following arguments:
363 .Bl -tag -width recurse
365 A pointer to the dynamic oid to be removed.
366 If the oid is not dynamic, or the pointer is
372 .Fn sysctl_remove_oid
373 will try to free the oid's resources
374 when the reference count of the oid becomes zero.
378 the routine will only deregister the oid from the tree,
379 without freeing its resources.
380 This behaviour is useful when the caller expects to rollback
381 (possibly partially failed)
382 deletion of many oids later.
384 If non-zero, attempt to remove the node and all its children.
388 any attempt to remove a node that contains any children
392 .Em WARNING : "use recursive deletion with extreme caution" !
393 Normally it should not be needed if contexts are used.
394 Contexts take care of tracking inter-dependencies
395 between users of the tree.
396 However, in some extreme cases it might be necessary
397 to remove part of the subtree no matter how it was created,
398 in order to free some other resources.
399 Be aware, though, that this may result in a system
401 if other code sections continue to use removed subtrees.
404 .\" XXX sheldonh finished up to here
405 Again, in most cases the programmer should use contexts,
407 .Xr sysctl_ctx_init 9 ,
408 to keep track of created oids,
409 and to delete them later in orderly fashion.
411 There is a set of macros defined
412 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 a 64-bit
447 .It Fn SYSCTL_ADD_UQUAD
448 creates an oid that handles a 64-bit
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 Mt 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.