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.5 2006/06/22 20:44:46 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"
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"
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 int *arg"
145 .Ft struct sysctl_oid *
147 .Fa "struct sysctl_ctx_list *ctx"
148 .Fa "struct sysctl_oid_list *parent"
156 .Ft struct sysctl_oid *
158 .Fa "struct sysctl_ctx_list *ctx"
159 .Fa "struct sysctl_oid_list *parent"
163 .Fa "unsigned long *arg"
167 .Ft struct sysctl_oid *
168 .Fo SYSCTL_ADD_OPAQUE
169 .Fa "struct sysctl_ctx_list *ctx"
170 .Fa "struct sysctl_oid_list *parent"
178 .Ft struct sysctl_oid *
179 .Fo SYSCTL_ADD_STRUCT
180 .Fa "struct sysctl_ctx_list *ctx"
181 .Fa "struct sysctl_oid_list *parent"
185 .Fa "struct TYPE *arg"
189 .Ft struct sysctl_oid *
191 .Fa "struct sysctl_ctx_list *ctx"
192 .Fa "struct sysctl_oid_list *parent"
198 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
203 These functions and macros provide an interface
204 for creating and deleting sysctl oids at runtime
205 (e.g. during lifetime of a module).
206 The alternative method,
207 based on linker sets (see
210 .\" XXX Manual pages should avoid referencing source files
211 .Pa src/sys/kern/kern_sysctl.c
212 for details), only allows creation and deletion
213 on module load and unload respectively.
218 so that several code sections can create and delete them,
219 but in reality they are allocated and freed
220 based on their reference count.
222 it is possible for two or more code sections
223 to create partially overlapping trees that they both can use.
224 It is not possible to create overlapping leaves,
225 nor to create different child types with the same name and parent.
227 Newly created oids are connected to their parent nodes.
228 In all these functions and macros
229 (with the exception of
230 .Fn sysctl_remove_oid ) ,
231 one of the required parameters is
233 which points to the head of the parent's list of children.
235 Most top level categories are created statically.
236 When connecting to existing static oids,
237 this pointer can be obtained with the
238 .Fn SYSCTL_STATIC_CHILDREN
241 argumwent is name of the parent oid of type
243 (i.e. the name displayed by
245 preceded by underscore, and with all dots replaced with underscores).
247 When connecting to an existing dynamic oid, this pointer
248 can be obtained with the
252 argument points to the parent oid of type
257 function creates raw oids of any type.
258 If the oid is successfuly created,
259 the function returns a pointer to it;
262 Many of the arguments for
264 are common to the macros.
265 The arguments are as follows:
266 .Bl -tag -width handler
268 A pointer to an optional sysctl context, or
271 .Xr sysctl_ctx_init 9
273 Programmers are strongly advised to use contexts
274 to organize the dynamic oids which they create,
275 unless special creation and deletion sequences are required.
280 the newly created oid will be added to this context
284 .Li struct sysctl_oid_list ,
285 which is the head of the parent's list of children.
287 The oid number that will be assigned to this oid.
288 In almost all cases this should be set to
290 which will result in the assignment of the next available oid number.
293 The newly created oid will contain a copy of the name.
296 specified as a bitmask of the type and access values defined in the
299 Oids created dynamically always have the
302 Access flags specify whether this oid is read-only or read-write,
303 and whether it may be modified by all users
304 or by the supseruser only.
306 A pointer to any data that the oid should reference, or
316 A pointer to the function
317 that is responsible for handling read and write requests
319 There are several standard handlers
320 that support operations on nodes,
321 integers, strings and opaque objects.
322 It is possible also to define new handlers using the
326 A pointer to a string
327 which specifies the format of the oid symbolically.
328 This format is used as a hint by
330 to apply proper data formatting for display purposes.
331 Currently used format names are:
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.
413 .Bl -tag -width SYSCTL_ADD_STRINGXX
415 .It Fn SYSCTL_ADD_OID
417 This macro is functionally equivalent to the
420 .It Fn SYSCTL_ADD_NODE
421 creates an oid of type
423 to which child oids may be added.
424 .It Fn SYSCTL_ADD_STRING
425 creates an oid that handles a zero-terminated character string.
426 .It Fn SYSCTL_ADD_INT
427 creates an oid that handles an
430 .It Fn SYSCTL_ADD_QUAD
431 creates an oid that handles a 64-bit
434 .It Fn SYSCTL_ADD_UQUAD
435 creates an oid that handles a 64-bit
438 .It Fn SYSCTL_ADD_UINT
439 creates an oid that handles an
442 .It Fn SYSCTL_ADD_LONG
443 creates an oid that handles a
446 .It Fn SYSCTL_ADD_ULONG
447 creates an oid that handles an
450 .It Fn SYSCTL_ADD_OPAQUE
451 creates an oid that handles any chunk of opaque data
452 of the size specified by the
455 which is a pointer to a
457 .It Fn SYSCTL_ADD_STRUCT
458 creates an oid that handles a
463 parameter will be set to
465 to provide proper hints to the
468 .It Fn SYSCTL_ADD_PROC
469 creates an oid with the specified
472 The handler is responsible for handling read and write requests
474 This oid type is especially useful
475 if the kernel data is not easily accessible,
476 or needs to be processed before exporting.
479 The following is an example of
480 how to create a new top-level category
481 and how to hook up another subtree to an existing static node.
482 This example does not use contexts,
483 which results in tedious management of all intermediate oids,
484 as they need to be freed later on:
486 #include <sys/sysctl.h>
488 /* Need to preserve pointers to newly created subtrees, to be able
489 * to free them later.
491 struct sysctl_oid *root1, *root2, *oidp;
493 char *string = "dynamic sysctl";
496 root1 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(/* tree top */),
497 OID_AUTO, newtree, CTFLAG_RW, 0, "new top level tree");
498 oidp = SYSCTL_ADD_INT( NULL, SYSCTL_CHILDREN(root1),
499 OID_AUTO, newint, CTLFLAG_RW, &a_int, 0, "new int leaf");
501 root2 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(_debug),
502 OID_AUTO, newtree, CTFLAG_RW, 0, "new tree under debug");
503 oidp = SYSCTL_ADD_STRING( NULL, SYSCTL_CHILDREN(root2),
504 OID_AUTO, newstring, CTLFLAG_R, string, 0, "new string leaf");
507 This example creates the following subtrees:
508 .Bd -literal -offset indent
509 debug.newtree.newstring
513 .Em "Care should be taken to free all oids once they are no longer needed!"
517 .Xr sysctl_ctx_free 9 ,
518 .Xr sysctl_ctx_init 9
520 These functions first appeared in
523 .An Andrzej Bialecki Aq abial@FreeBSD.org
525 Sharing nodes between many code sections
526 causes interdependencies that sometimes may lock the resources.
528 if module A hooks up a subtree to an oid created by module B,
529 module B will be unable to delete that oid.
530 These issues are handled properly by sysctl contexts.
532 Many operations on the tree involve traversing linked lists.
533 For this reason, oid creation and removal is relatively costly.