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 $
35 .Nm sysctl_remove_oid ,
37 .Nm SYSCTL_STATIC_CHILDREN ,
40 .Nm SYSCTL_ADD_STRING ,
45 .Nm SYSCTL_ADD_ULONG ,
47 .Nm SYSCTL_ADD_UQUAD ,
48 .Nm SYSCTL_ADD_OPAQUE ,
49 .Nm SYSCTL_ADD_STRUCT ,
51 .Nd runtime sysctl tree manipulation
54 .Ft struct sysctl_oid *
56 .Fa "struct sysctl_ctx_list *ctx"
57 .Fa "struct sysctl_oid_list *parent"
59 .Fa "const char *name"
63 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
64 .Fa "const char *format"
65 .Fa "const char *descr"
69 .Fa "struct sysctl_oid *oidp"
73 .Ft struct sysctl_oid_list *
75 .Fa "struct sysctl_oid *oidp"
77 .Ft struct sysctl_oid_list *
78 .Fo SYSCTL_STATIC_CHILDREN
79 .Fa "struct sysctl_oid_list oid_name"
81 .Ft struct sysctl_oid *
83 .Fa "struct sysctl_ctx_list *ctx"
84 .Fa "struct sysctl_oid_list *parent"
86 .Fa "const char *name"
90 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
91 .Fa "const char *format"
92 .Fa "const char *descr"
94 .Ft struct sysctl_oid *
96 .Fa "struct sysctl_ctx_list *ctx"
97 .Fa "struct sysctl_oid_list *parent"
99 .Fa "const char *name"
101 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
102 .Fa "const char *descr"
104 .Ft struct sysctl_oid *
106 .Fa "struct sysctl_ctx_list *ctx"
107 .Fa "struct sysctl_oid_list *parent"
109 .Fa "const char *name"
113 .Fa "const char *descr"
115 .Ft struct sysctl_oid *
117 .Fa "struct sysctl_ctx_list *ctx"
118 .Fa "struct sysctl_oid_list *parent"
120 .Fa "const char *name"
124 .Fa "const char *descr"
126 .Ft struct sysctl_oid *
128 .Fa "struct sysctl_ctx_list *ctx"
129 .Fa "struct sysctl_oid_list *parent"
131 .Fa "const char *name"
135 .Fa "const char *descr"
137 .Ft struct sysctl_oid *
139 .Fa "struct sysctl_ctx_list *ctx"
140 .Fa "struct sysctl_oid_list *parent"
142 .Fa "const char *name"
146 .Fa "const char *descr"
148 .Ft struct sysctl_oid *
149 .Fo SYSCTL_ADD_STRING
150 .Fa "struct sysctl_ctx_list *ctx"
151 .Fa "struct sysctl_oid_list *parent"
153 .Fa "const char *name"
157 .Fa "const char *descr"
159 .Ft struct sysctl_oid *
161 .Fa "struct sysctl_ctx_list *ctx"
162 .Fa "struct sysctl_oid_list *parent"
164 .Fa "const char *name"
168 .Fa "const char *descr"
170 .Ft struct sysctl_oid *
172 .Fa "struct sysctl_ctx_list *ctx"
173 .Fa "struct sysctl_oid_list *parent"
175 .Fa "const char *name"
179 .Fa "const char *descr"
181 .Ft struct sysctl_oid *
183 .Fa "struct sysctl_ctx_list *ctx"
184 .Fa "struct sysctl_oid_list *parent"
186 .Fa "const char *name"
190 .Fa "const char *descr"
192 .Ft struct sysctl_oid *
194 .Fa "struct sysctl_ctx_list *ctx"
195 .Fa "struct sysctl_oid_list *parent"
197 .Fa "const char *name"
200 .Fa "const char *descr"
202 .Ft struct sysctl_oid *
204 .Fa "struct sysctl_ctx_list *ctx"
205 .Fa "struct sysctl_oid_list *parent"
207 .Fa "const char *name"
211 .Fa "const char *descr"
213 .Ft struct sysctl_oid *
215 .Fa "struct sysctl_ctx_list *ctx"
216 .Fa "struct sysctl_oid_list *parent"
218 .Fa "const char *name"
220 .Fa "unsigned int *arg"
222 .Fa "const char *descr"
224 .Ft struct sysctl_oid *
226 .Fa "struct sysctl_ctx_list *ctx"
227 .Fa "struct sysctl_oid_list *parent"
229 .Fa "const char *name"
232 .Fa "const char *descr"
234 .Ft struct sysctl_oid *
236 .Fa "struct sysctl_ctx_list *ctx"
237 .Fa "struct sysctl_oid_list *parent"
239 .Fa "const char *name"
241 .Fa "unsigned long *arg"
242 .Fa "const char *descr"
244 .Ft struct sysctl_oid *
246 .Fa "struct sysctl_ctx_list *ctx"
247 .Fa "struct sysctl_oid_list *parent"
249 .Fa "const char *name"
253 .Fa "const char *descr"
255 .Ft struct sysctl_oid *
257 .Fa "struct sysctl_ctx_list *ctx"
258 .Fa "struct sysctl_oid_list *parent"
260 .Fa "const char *name"
264 .Fa "const char *descr"
266 .Ft struct sysctl_oid *
267 .Fo SYSCTL_ADD_OPAQUE
268 .Fa "struct sysctl_ctx_list *ctx"
269 .Fa "struct sysctl_oid_list *parent"
271 .Fa "const char *name"
275 .Fa "const char *format"
276 .Fa "const char *descr"
278 .Ft struct sysctl_oid *
279 .Fo SYSCTL_ADD_STRUCT
280 .Fa "struct sysctl_ctx_list *ctx"
281 .Fa "struct sysctl_oid_list *parent"
283 .Fa "const char *name"
287 .Fa "const char *descr"
289 .Ft struct sysctl_oid *
291 .Fa "struct sysctl_ctx_list *ctx"
292 .Fa "struct sysctl_oid_list *parent"
294 .Fa "const char *name"
298 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
299 .Fa "const char *format"
300 .Fa "const char *descr"
303 These functions and macros provide an interface
304 for creating and deleting sysctl oids at runtime
305 (e.g.\& during lifetime of a module).
306 The alternative method,
307 based on linker sets (see
310 .\" XXX Manual pages should avoid referencing source files
311 .Pa /sys/kern/kern_sysctl.c
312 for details), only allows creation and deletion
313 on module load and unload respectively.
318 so that several code sections can create and delete them,
319 but in reality they are allocated and freed
320 based on their reference count.
322 it is possible for two or more code sections
323 to create partially overlapping trees that they both can use.
324 It is not possible to create overlapping leaves,
325 nor to create different child types with the same name and parent.
327 Newly created oids are connected to their parent nodes.
328 In all these functions and macros
329 (with the exception of
330 .Fn sysctl_remove_oid ) ,
331 one of the required parameters is
333 which points to the head of the parent's list of children.
335 Most top level categories are created statically.
336 When connecting to existing static oids,
337 this pointer can be obtained with the
338 .Fn SYSCTL_STATIC_CHILDREN
341 argument is name of the parent oid of type
343 (i.e., the name displayed by
345 preceded by underscore, and with all dots replaced with underscores).
347 When connecting to an existing dynamic oid, this pointer
348 can be obtained with the
352 argument points to the parent oid of type
357 function creates raw oids of any type.
358 If the oid is successfully created,
359 the function returns a pointer to it;
362 Many of the arguments for
364 are common to the macros.
365 The arguments are as follows:
366 .Bl -tag -width handler
368 A pointer to an optional sysctl context, or
371 .Xr sysctl_ctx_init 9
373 Programmers are strongly advised to use contexts
374 to organize the dynamic oids which they create,
375 unless special creation and deletion sequences are required.
380 the newly created oid will be added to this context
384 .Li struct sysctl_oid_list ,
385 which is the head of the parent's list of children.
387 The oid number that will be assigned to this oid.
388 In almost all cases this should be set to
390 which will result in the assignment of the next available oid number.
393 The newly created oid will contain a copy of the name.
396 specified as a bit mask of the type and access values defined in the
399 Oids created dynamically always have the
402 Access flags specify whether this oid is read-only or read-write,
403 and whether it may be modified by all users
404 or by the superuser only.
406 A pointer to any data that the oid should reference, or
416 A pointer to the function
417 that is responsible for handling read and write requests
419 There are several standard handlers
420 that support operations on nodes,
421 integers, strings and opaque objects.
422 It is possible also to define new handlers using the
426 A pointer to a string
427 which specifies the format of the oid symbolically.
428 This format is used as a hint by
430 to apply proper data formatting for display purposes.
431 Currently used format names are:
444 for temperature in tenths of kelvins,
457 A pointer to a textual description of the oid.
461 .Fn sysctl_remove_oid
462 function removes a dynamically created oid from the tree,
463 optionally freeing its resources.
464 It takes the following arguments:
465 .Bl -tag -width recurse
467 A pointer to the dynamic oid to be removed.
468 If the oid is not dynamic, or the pointer is
474 .Fn sysctl_remove_oid
475 will try to free the oid's resources
476 when the reference count of the oid becomes zero.
480 the routine will only deregister the oid from the tree,
481 without freeing its resources.
482 This behaviour is useful when the caller expects to rollback
483 (possibly partially failed)
484 deletion of many oids later.
486 If non-zero, attempt to remove the node and all its children.
490 any attempt to remove a node that contains any children
494 .Em WARNING : "use recursive deletion with extreme caution" !
495 Normally it should not be needed if contexts are used.
496 Contexts take care of tracking inter-dependencies
497 between users of the tree.
498 However, in some extreme cases it might be necessary
499 to remove part of the subtree no matter how it was created,
500 in order to free some other resources.
501 Be aware, though, that this may result in a system
503 if other code sections continue to use removed subtrees.
506 .\" XXX sheldonh finished up to here
507 Again, in most cases the programmer should use contexts,
509 .Xr sysctl_ctx_init 9 ,
510 to keep track of created oids,
511 and to delete them later in orderly fashion.
513 There is a set of macros defined
514 that helps to create oids of given type.
517 .Bl -tag -width SYSCTL_ADD_STRINGXX
518 .It Fn SYSCTL_ADD_OID
520 This macro is functionally equivalent to the
523 .It Fn SYSCTL_ADD_NODE
524 creates an oid of type
526 to which child oids may be added.
528 creates an oid that handles an
531 .It Fn SYSCTL_ADD_S16
532 creates an oid that handles an
535 .It Fn SYSCTL_ADD_S32
536 creates an oid that handles an
539 .It Fn SYSCTL_ADD_S64
540 creates an oid that handles an
543 .It Fn SYSCTL_ADD_STRING
544 creates an oid that handles a zero-terminated character string.
545 .It Fn SYSCTL_ADD_INT
546 creates an oid that handles an
550 creates an oid that handles a
553 .It Fn SYSCTL_ADD_U16
554 creates an oid that handles a
557 .It Fn SYSCTL_ADD_U32
558 creates an oid that handles a
561 .It Fn SYSCTL_ADD_U64
562 creates an oid that handles a
565 .It Fn SYSCTL_ADD_UINT
566 creates an oid that handles an
569 .It Fn SYSCTL_ADD_LONG
570 creates an oid that handles a
573 .It Fn SYSCTL_ADD_ULONG
574 creates an oid that handles an
577 .It Fn SYSCTL_ADD_QUAD
578 creates an oid that handles a 64-bit
581 .It Fn SYSCTL_ADD_UQUAD
582 creates an oid that handles a 64-bit
585 .It Fn SYSCTL_ADD_OPAQUE
586 creates an oid that handles any chunk of opaque data
587 of the size specified by the
590 which is a pointer to a
592 .It Fn SYSCTL_ADD_STRUCT
593 creates an oid that handles a
598 parameter will be set to
600 to provide proper hints to the
603 .It Fn SYSCTL_ADD_PROC
604 creates an oid with the specified
607 The handler is responsible for handling read and write requests
609 This oid type is especially useful
610 if the kernel data is not easily accessible,
611 or needs to be processed before exporting.
614 The following is an example of
615 how to create a new top-level category
616 and how to hook up another subtree to an existing static node.
617 This example does not use contexts,
618 which results in tedious management of all intermediate oids,
619 as they need to be freed later on:
621 #include <sys/sysctl.h>
623 /* Need to preserve pointers to newly created subtrees, to be able
624 * to free them later.
626 struct sysctl_oid *root1, *root2, *oidp;
628 char *string = "dynamic sysctl";
631 root1 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(/* tree top */),
632 OID_AUTO, "newtree", CTLFLAG_RW, 0, "new top level tree");
633 oidp = SYSCTL_ADD_INT( NULL, SYSCTL_CHILDREN(root1),
634 OID_AUTO, "newint", CTLFLAG_RW, &a_int, 0, "new int leaf");
636 root2 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(_debug),
637 OID_AUTO, "newtree", CTLFLAG_RW, 0, "new tree under debug");
638 oidp = SYSCTL_ADD_STRING( NULL, SYSCTL_CHILDREN(root2),
639 OID_AUTO, "newstring", CTLFLAG_RD, string, 0, "new string leaf");
642 This example creates the following subtrees:
643 .Bd -literal -offset indent
644 debug.newtree.newstring
648 .Em "Care should be taken to free all oids once they are no longer needed!"
652 .Xr sysctl_ctx_free 9 ,
653 .Xr sysctl_ctx_init 9
655 These functions first appeared in
658 .An Andrzej Bialecki Aq Mt abial@FreeBSD.org
660 Sharing nodes between many code sections
661 causes interdependencies that sometimes may lock the resources.
663 if module A hooks up a subtree to an oid created by module B,
664 module B will be unable to delete that oid.
665 These issues are handled properly by sysctl contexts.
667 Many operations on the tree involve traversing linked lists.
668 For this reason, oid creation and removal is relatively costly.