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
37 .Nd runtime sysctl tree manipulation
41 .Ft struct sysctl_oid *
43 .Fa "struct sysctl_ctx_list *ctx"
44 .Fa "struct sysctl_oid_list *parent"
46 .Fa "const char *name"
50 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
51 .Fa "const char *format"
52 .Fa "const char *descr"
56 .Fa "struct sysctl_oid *oidp"
57 .Fa "struct sysctl_oid_list *parent"
61 .Fa "struct sysctl_oid *oidp"
65 .Ft struct sysctl_oid_list *
67 .Fa "struct sysctl_oid *oidp"
69 .Ft struct sysctl_oid_list *
70 .Fo SYSCTL_STATIC_CHILDREN
71 .Fa "struct sysctl_oid_list OID_NAME"
73 .Ft struct sysctl_oid *
75 .Fa "struct sysctl_ctx_list *ctx"
76 .Fa "struct sysctl_oid_list *parent"
78 .Fa "const char *name"
82 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
83 .Fa "const char *format"
84 .Fa "const char *descr"
86 .Ft struct sysctl_oid *
88 .Fa "struct sysctl_ctx_list *ctx"
89 .Fa "struct sysctl_oid_list *parent"
91 .Fa "const char *name"
93 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
94 .Fa "const char *descr"
96 .Ft struct sysctl_oid *
98 .Fa "struct sysctl_ctx_list *ctx"
99 .Fa "struct sysctl_oid_list *parent"
101 .Fa "const char *name"
105 .Fa "const char *descr"
107 .Ft struct sysctl_oid *
109 .Fa "struct sysctl_ctx_list *ctx"
110 .Fa "struct sysctl_oid_list *parent"
112 .Fa "const char *name"
116 .Fa "const char *descr"
118 .Ft struct sysctl_oid *
120 .Fa "struct sysctl_ctx_list *ctx"
121 .Fa "struct sysctl_oid_list *parent"
123 .Fa "const char *name"
125 .Fa "unsigned int *arg"
127 .Fa "const char *descr"
129 .Ft struct sysctl_oid *
131 .Fa "struct sysctl_ctx_list *ctx"
132 .Fa "struct sysctl_oid_list *parent"
134 .Fa "const char *name"
137 .Fa "const char *descr"
139 .Ft struct sysctl_oid *
141 .Fa "struct sysctl_ctx_list *ctx"
142 .Fa "struct sysctl_oid_list *parent"
144 .Fa "const char *name"
146 .Fa "unsigned long *arg"
147 .Fa "const char *descr"
149 .Ft struct sysctl_oid *
150 .Fo SYSCTL_ADD_OPAQUE
151 .Fa "struct sysctl_ctx_list *ctx"
152 .Fa "struct sysctl_oid_list *parent"
154 .Fa "const char *name"
158 .Fa "const char *format"
159 .Fa "const char *descr"
161 .Ft struct sysctl_oid *
162 .Fo SYSCTL_ADD_STRUCT
163 .Fa "struct sysctl_ctx_list *ctx"
164 .Fa "struct sysctl_oid_list *parent"
166 .Fa "const char *name"
170 .Fa "const char *descr"
172 .Ft struct sysctl_oid *
174 .Fa "struct sysctl_ctx_list *ctx"
175 .Fa "struct sysctl_oid_list *parent"
177 .Fa "const char *name"
181 .Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
182 .Fa "const char *format"
183 .Fa "const char *descr"
186 These functions and macros provide an interface
187 for creating and deleting sysctl oids at runtime
188 (e.g.\& during lifetime of a module).
189 The alternative method,
190 based on linker sets (see
193 .\" XXX Manual pages should avoid referencing source files
194 .Pa src/sys/kern/kern_sysctl.c
195 for details), only allows creation and deletion
196 on module load and unload respectively.
201 so that several code sections can create and delete them,
202 but in reality they are allocated and freed
203 based on their reference count.
205 it is possible for two or more code sections
206 to create partially overlapping trees that they both can use.
207 It is not possible to create overlapping leaves,
208 nor to create different child types with the same name and parent.
210 Newly created oids are connected to their parent nodes.
211 In all these functions and macros
212 (with the exception of
213 .Fn sysctl_remove_oid ) ,
214 one of the required parameters is
216 which points to the head of the parent's list of children.
218 Most top level categories are created statically.
219 When connecting to existing static oids,
220 this pointer can be obtained with the
221 .Fn SYSCTL_STATIC_CHILDREN
224 argument is name of the parent oid of type
226 (i.e., the name displayed by
228 preceded by underscore, and with all dots replaced with underscores).
230 When connecting to an existing dynamic oid, this pointer
231 can be obtained with the
235 argument points to the parent oid of type
240 function creates raw oids of any type.
241 If the oid is successfully created,
242 the function returns a pointer to it;
245 Many of the arguments for
247 are common to the macros.
248 The arguments are as follows:
249 .Bl -tag -width handler
251 A pointer to an optional sysctl context, or
254 .Xr sysctl_ctx_init 9
256 Programmers are strongly advised to use contexts
257 to organize the dynamic oids which they create,
258 unless special creation and deletion sequences are required.
263 the newly created oid will be added to this context
267 .Li struct sysctl_oid_list ,
268 which is the head of the parent's list of children.
270 The oid number that will be assigned to this oid.
271 In almost all cases this should be set to
273 which will result in the assignment of the next available oid number.
276 The newly created oid will contain a copy of the name.
279 specified as a bit mask of the type and access values defined in the
282 Oids created dynamically always have the
285 Access flags specify whether this oid is read-only or read-write,
286 and whether it may be modified by all users
287 or by the superuser only.
289 A pointer to any data that the oid should reference, or
299 A pointer to the function
300 that is responsible for handling read and write requests
302 There are several standard handlers
303 that support operations on nodes,
304 integers, strings and opaque objects.
305 It is possible also to define new handlers using the
309 A pointer to a string
310 which specifies the format of the oid symbolically.
311 This format is used as a hint by
313 to apply proper data formatting for display purposes.
314 Currently used format names are:
338 A pointer to a textual description of the oid.
343 function reparents an existing oid.
344 The oid is assigned a new number as if it had been created with
350 .Fn sysctl_remove_oid
351 function removes a dynamically created oid from the tree,
352 optionally freeing its resources.
353 It takes the following arguments:
354 .Bl -tag -width recurse
356 A pointer to the dynamic oid to be removed.
357 If the oid is not dynamic, or the pointer is
363 .Fn sysctl_remove_oid
364 will try to free the oid's resources
365 when the reference count of the oid becomes zero.
369 the routine will only deregister the oid from the tree,
370 without freeing its resources.
371 This behaviour is useful when the caller expects to rollback
372 (possibly partially failed)
373 deletion of many oids later.
375 If non-zero, attempt to remove the node and all its children.
379 any attempt to remove a node that contains any children
383 .Em WARNING : "use recursive deletion with extreme caution" !
384 Normally it should not be needed if contexts are used.
385 Contexts take care of tracking inter-dependencies
386 between users of the tree.
387 However, in some extreme cases it might be necessary
388 to remove part of the subtree no matter how it was created,
389 in order to free some other resources.
390 Be aware, though, that this may result in a system
392 if other code sections continue to use removed subtrees.
395 .\" XXX sheldonh finished up to here
396 Again, in most cases the programmer should use contexts,
398 .Xr sysctl_ctx_init 9 ,
399 to keep track of created oids,
400 and to delete them later in orderly fashion.
402 There is a set of macros defined
403 that helps to create oids of given type.
404 .Bl -tag -width SYSCTL_ADD_STRINGXX
406 .It Fn SYSCTL_ADD_OID
408 This macro is functionally equivalent to the
411 .It Fn SYSCTL_ADD_NODE
412 creates an oid of type
414 to which child oids may be added.
415 .It Fn SYSCTL_ADD_STRING
416 creates an oid that handles a zero-terminated character string.
417 .It Fn SYSCTL_ADD_INT
418 creates an oid that handles an
421 .It Fn SYSCTL_ADD_UINT
422 creates an oid that handles an
425 .It Fn SYSCTL_ADD_LONG
426 creates an oid that handles a
429 .It Fn SYSCTL_ADD_ULONG
430 creates an oid that handles an
433 .It Fn SYSCTL_ADD_OPAQUE
434 creates an oid that handles any chunk of opaque data
435 of the size specified by the
438 which is a pointer to a
440 .It Fn SYSCTL_ADD_STRUCT
441 creates an oid that handles a
446 parameter will be set to
448 to provide proper hints to the
451 .It Fn SYSCTL_ADD_PROC
452 creates an oid with the specified
455 The handler is responsible for handling read and write requests
457 This oid type is especially useful
458 if the kernel data is not easily accessible,
459 or needs to be processed before exporting.
462 The following is an example of
463 how to create a new top-level category
464 and how to hook up another subtree to an existing static node.
465 This example does not use contexts,
466 which results in tedious management of all intermediate oids,
467 as they need to be freed later on:
469 #include <sys/sysctl.h>
471 /* Need to preserve pointers to newly created subtrees, to be able
472 * to free them later.
474 struct sysctl_oid *root1, *root2, *oidp;
476 char *string = "dynamic sysctl";
479 root1 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(/* tree top */),
480 OID_AUTO, "newtree", CTLFLAG_RW, 0, "new top level tree");
481 oidp = SYSCTL_ADD_INT( NULL, SYSCTL_CHILDREN(root1),
482 OID_AUTO, "newint", CTLFLAG_RW, &a_int, 0, "new int leaf");
484 root2 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(_debug),
485 OID_AUTO, "newtree", CTLFLAG_RW, 0, "new tree under debug");
486 oidp = SYSCTL_ADD_STRING( NULL, SYSCTL_CHILDREN(root2),
487 OID_AUTO, "newstring", CTLFLAG_RD, string, 0, "new string leaf");
490 This example creates the following subtrees:
491 .Bd -literal -offset indent
492 debug.newtree.newstring
496 .Em "Care should be taken to free all oids once they are no longer needed!"
500 .Xr sysctl_ctx_free 9 ,
501 .Xr sysctl_ctx_init 9
503 These functions first appeared in
506 .An Andrzej Bialecki Aq abial@FreeBSD.org
508 Sharing nodes between many code sections
509 causes interdependencies that sometimes may lock the resources.
511 if module A hooks up a subtree to an oid created by module B,
512 module B will be unable to delete that oid.
513 These issues are handled properly by sysctl contexts.
515 Many operations on the tree involve traversing linked lists.
516 For this reason, oid creation and removal is relatively costly.