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.2 2003/06/17 04:37:01 dillon 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"
49 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
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)"
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)"
90 .Ft struct sysctl_oid *
92 .Fa "struct sysctl_ctx_list *ctx"
93 .Fa "struct sysctl_oid_list *parent"
101 .Ft struct sysctl_oid *
103 .Fa "struct sysctl_ctx_list *ctx"
104 .Fa "struct sysctl_oid_list *parent"
112 .Ft struct sysctl_oid *
114 .Fa "struct sysctl_ctx_list *ctx"
115 .Fa "struct sysctl_oid_list *parent"
119 .Fa "unsigned int *arg"
123 .Ft struct sysctl_oid *
125 .Fa "struct sysctl_ctx_list *ctx"
126 .Fa "struct sysctl_oid_list *parent"
134 .Ft struct sysctl_oid *
136 .Fa "struct sysctl_ctx_list *ctx"
137 .Fa "struct sysctl_oid_list *parent"
141 .Fa "unsigned long *arg"
145 .Ft struct sysctl_oid *
146 .Fo SYSCTL_ADD_OPAQUE
147 .Fa "struct sysctl_ctx_list *ctx"
148 .Fa "struct sysctl_oid_list *parent"
156 .Ft struct sysctl_oid *
157 .Fo SYSCTL_ADD_STRUCT
158 .Fa "struct sysctl_ctx_list *ctx"
159 .Fa "struct sysctl_oid_list *parent"
163 .Fa "struct TYPE *arg"
167 .Ft struct sysctl_oid *
169 .Fa "struct sysctl_ctx_list *ctx"
170 .Fa "struct sysctl_oid_list *parent"
176 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
181 These functions and macros provide an interface
182 for creating and deleting sysctl oids at runtime
183 (e.g. during lifetime of a module).
184 The alternative method,
185 based on linker sets (see
188 .\" XXX Manual pages should avoid referencing source files
189 .Pa src/sys/kern/kern_sysctl.c
190 for details), only allows creation and deletion
191 on module load and unload respectively.
196 so that several code sections can create and delete them,
197 but in reality they are allocated and freed
198 based on their reference count.
200 it is possible for two or more code sections
201 to create partially overlapping trees that they both can use.
202 It is not possible to create overlapping leaves,
203 nor to create different child types with the same name and parent.
205 Newly created oids are connected to their parent nodes.
206 In all these functions and macros
207 (with the exception of
208 .Fn sysctl_remove_oid ) ,
209 one of the required parameters is
211 which points to the head of the parent's list of children.
213 Most top level categories are created statically.
214 When connecting to existing static oids,
215 this pointer can be obtained with the
216 .Fn SYSCTL_STATIC_CHILDREN
219 argumwent is name of the parent oid of type
221 (i.e. the name displayed by
223 preceded by underscore, and with all dots replaced with underscores).
225 When connecting to an existing dynamic oid, this pointer
226 can be obtained with the
230 argument points to the parent oid of type
235 function creates raw oids of any type.
236 If the oid is successfuly created,
237 the function returns a pointer to it;
240 Many of the arguments for
242 are common to the macros.
243 The arguments are as follows:
244 .Bl -tag -width handler
246 A pointer to an optional sysctl context, or
249 .Xr sysctl_ctx_init 9
251 Programmers are strongly advised to use contexts
252 to organize the dynamic oids which they create,
253 unless special creation and deletion sequences are required.
258 the newly created oid will be added to this context
262 .Li struct sysctl_oid_list ,
263 which is the head of the parent's list of children.
265 The oid number that will be assigned to this oid.
266 In almost all cases this should be set to
268 which will result in the assignment of the next available oid number.
271 The newly created oid will contain a copy of the name.
274 specified as a bitmask of the type and access values defined in the
277 Oids created dynamically always have the
280 Access flags specify whether this oid is read-only or read-write,
281 and whether it may be modified by all users
282 or by the supseruser only.
284 A pointer to any data that the oid should reference, or
294 A pointer to the function
295 that is responsible for handling read and write requests
297 There are several standard handlers
298 that support operations on nodes,
299 integers, strings and opaque objects.
300 It is possible also to define new handlers using the
304 A pointer to a string
305 which specifies the format of the oid symbolically.
306 This format is used as a hint by
308 to apply proper data formatting for display purposes.
309 Currently used format names are:
333 A pointer to a textual description of the oid.
337 .Fn sysctl_remove_oid
338 function removes a dynamically created oid from the tree,
339 optionally freeing its resources.
340 It takes the following arguments:
341 .Bl -tag -width recurse
343 A pointer to the dynamic oid to be removed.
344 If the oid is not dynamic, or the pointer is
350 .Fn sysctl_remove_oid
351 will try to free the oid's resources
352 when the reference count of the oid becomes zero.
356 the routine will only deregister the oid from the tree,
357 without freeing its resources.
358 This behaviour is useful when the caller expects to rollback
359 (possibly partially failed)
360 deletion of many oids later.
362 If non-zero, attempt to remove the node and all its children.
366 any attempt to remove a node that contains any children
370 .Em WARNING : "use recursive deletion with extreme caution" !
371 Normally it should not be needed if contexts are used.
372 Contexts take care of tracking inter-dependencies
373 between users of the tree.
374 However, in some extreme cases it might be necessary
375 to remove part of the subtree no matter how it was created,
376 in order to free some other resources.
377 Be aware, though, that this may result in a system
379 if other code sections continue to use removed subtrees.
382 .\" XXX sheldonh finished up to here
383 Again, in most cases the programmer should use contexts,
385 .Xr sysctl_ctx_init 9 ,
386 to keep track of created oids,
387 and to delete them later in orderly fashion.
389 There is a set of macros defined
390 that helps to create oids of given type.
391 .Bl -tag -width SYSCTL_ADD_STRINGXX
393 .It Fn SYSCTL_ADD_OID
395 This macro is functionally equivalent to the
398 .It Fn SYSCTL_ADD_NODE
399 creates an oid of type
401 to which child oids may be added.
402 .It Fn SYSCTL_ADD_STRING
403 creates an oid that handles a zero-terminated character string.
404 .It Fn SYSCTL_ADD_INT
405 creates an oid that handles an
408 .It Fn SYSCTL_ADD_UINT
409 creates an oid that handles an
412 .It Fn SYSCTL_ADD_LONG
413 creates an oid that handles a
416 .It Fn SYSCTL_ADD_ULONG
417 creates an oid that handles an
420 .It Fn SYSCTL_ADD_OPAQUE
421 creates an oid that handles any chunk of opaque data
422 of the size specified by the
425 which is a pointer to a
427 .It Fn SYSCTL_ADD_STRUCT
428 creates an oid that handles a
433 parameter will be set to
435 to provide proper hints to the
438 .It Fn SYSCTL_ADD_PROC
439 creates an oid with the specified
442 The handler is responsible for handling read and write requests
444 This oid type is especially useful
445 if the kernel data is not easily accessible,
446 or needs to be processed before exporting.
449 The following is an example of
450 how to create a new top-level category
451 and how to hook up another subtree to an existing static node.
452 This example does not use contexts,
453 which results in tedious management of all intermediate oids,
454 as they need to be freed later on:
456 #include <sys/sysctl.h>
458 /* Need to preserve pointers to newly created subtrees, to be able
459 * to free them later.
461 struct sysctl_oid *root1, *root2, *oidp;
463 char *string = "dynamic sysctl";
466 root1 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(/* tree top */),
467 OID_AUTO, newtree, CTFLAG_RW, 0, "new top level tree");
468 oidp = SYSCTL_ADD_INT( NULL, SYSCTL_CHILDREN(root1),
469 OID_AUTO, newint, CTLFLAG_RW, &a_int, 0, "new int leaf");
471 root2 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(_debug),
472 OID_AUTO, newtree, CTFLAG_RW, 0, "new tree under debug");
473 oidp = SYSCTL_ADD_STRING( NULL, SYSCTL_CHILDREN(root2),
474 OID_AUTO, newstring, CTLFLAG_R, string, 0, "new string leaf");
477 This example creates the following subtrees:
478 .Bd -literal -offset indent
479 debug.newtree.newstring
483 .Em "Care should be taken to free all oids once they are no longer needed!"
486 .Xr sysctl_ctx_free 9 ,
487 .Xr sysctl_ctx_init 9
489 These functions first appeared in
492 .An Andrzej Bialecki Aq abial@FreeBSD.org
494 Sharing nodes between many code sections
495 causes interdependencies that sometimes may lock the resources.
497 if module A hooks up a subtree to an oid created by module B,
498 module B will be unable to delete that oid.
499 These issues are handled properly by sysctl contexts.
501 Many operations on the tree involve traversing linked lists.
502 For this reason, oid creation and removal is relatively costly.