1.\" 2.\" Copyright (c) 2000, Andrzej Bialecki <abial@FreeBSD.org> 3.\" All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 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. 15.\" 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 26.\" SUCH DAMAGE. 27.\" 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 $ 30.\" 31.Dd June 22, 2006 32.Dt SYSCTL_ADD_OID 9 33.Os 34.Sh NAME 35.Nm sysctl_add_oid , 36.Nm sysctl_remove_oid 37.Nd runtime sysctl tree manipulation 38.Sh SYNOPSIS 39.In sys/sysctl.h 40.Ft struct sysctl_oid * 41.Fo sysctl_add_oid 42.Fa "struct sysctl_ctx_list *ctx" 43.Fa "struct sysctl_oid_list *parent" 44.Fa "int number" 45.Fa "char *name" 46.Fa "int kind" 47.Fa "void *arg1" 48.Fa "int arg2" 49.Fa "int (*handler) (SYSCTL_HANDLER_ARGS)" 50.Fa "char *format" 51.Fa "char *descr" 52.Fc 53.Ft int 54.Fo sysctl_remove_oid 55.Fa "struct sysctl_oid *oidp" 56.Fa "int del" 57.Fa "int recurse" 58.Fc 59.Ft struct sysctl_oid_list * 60.Fo SYSCTL_CHILDREN 61.Fa "struct sysctl_oid *oidp" 62.Fc 63.Ft struct sysctl_oid_list * 64.Fo SYSCTL_STATIC_CHILDREN 65.Fa "OID_NAME" 66.Fc 67.Ft struct sysctl_oid * 68.Fo SYSCTL_ADD_OID 69.Fa "struct sysctl_ctx_list *ctx" 70.Fa "struct sysctl_oid_list *parent" 71.Fa "int number" 72.Fa "NAME" 73.Fa "int kind" 74.Fa "void *arg1" 75.Fa "int arg2" 76.Fa "int (*handler) (SYSCTL_HANDLER_ARGS)" 77.Fa "char *format" 78.Fa "char *descr" 79.Fc 80.Ft struct sysctl_oid * 81.Fo SYSCTL_ADD_NODE 82.Fa "struct sysctl_ctx_list *ctx" 83.Fa "struct sysctl_oid_list *parent" 84.Fa "int number" 85.Fa "NAME" 86.Fa "int access" 87.Fa "int (*handler) (SYSCTL_HANDLER_ARGS)" 88.Fa "char *descr" 89.Fc 90.Ft struct sysctl_oid * 91.Fo SYSCTL_ADD_STRING 92.Fa "struct sysctl_ctx_list *ctx" 93.Fa "struct sysctl_oid_list *parent" 94.Fa "int number" 95.Fa "NAME" 96.Fa "int access" 97.Fa "char *arg" 98.Fa "0" 99.Fa "char *descr" 100.Fc 101.Ft struct sysctl_oid * 102.Fo SYSCTL_ADD_INT 103.Fa "struct sysctl_ctx_list *ctx" 104.Fa "struct sysctl_oid_list *parent" 105.Fa "int number" 106.Fa "NAME" 107.Fa "int access" 108.Fa "int *arg" 109.Fa "0" 110.Fa "char *descr" 111.Fc 112.Ft struct sysctl_oid * 113.Fo SYSCTL_ADD_QUAD 114.Fa "struct sysctl_ctx_list *ctx" 115.Fa "struct sysctl_oid_list *parent" 116.Fa "int number" 117.Fa "NAME" 118.Fa "int access" 119.Fa "quad_t *arg" 120.Fa "0" 121.Fa "char *descr" 122.Fc 123.Ft struct sysctl_oid * 124.Fo SYSCTL_ADD_UQUAD 125.Fa "struct sysctl_ctx_list *ctx" 126.Fa "struct sysctl_oid_list *parent" 127.Fa "int number" 128.Fa "NAME" 129.Fa "int access" 130.Fa "u_quad_t *arg" 131.Fa "0" 132.Fa "char *descr" 133.Fc 134.Ft struct sysctl_oid * 135.Fo SYSCTL_ADD_UINT 136.Fa "struct sysctl_ctx_list *ctx" 137.Fa "struct sysctl_oid_list *parent" 138.Fa "int number" 139.Fa "NAME" 140.Fa "int access" 141.Fa "unsigned int *arg" 142.Fa "0" 143.Fa "char *descr" 144.Fc 145.Ft struct sysctl_oid * 146.Fo SYSCTL_ADD_LONG 147.Fa "struct sysctl_ctx_list *ctx" 148.Fa "struct sysctl_oid_list *parent" 149.Fa "int number" 150.Fa "NAME" 151.Fa "int access" 152.Fa "long *arg" 153.Fa "0" 154.Fa "char *descr" 155.Fc 156.Ft struct sysctl_oid * 157.Fo SYSCTL_ADD_ULONG 158.Fa "struct sysctl_ctx_list *ctx" 159.Fa "struct sysctl_oid_list *parent" 160.Fa "int number" 161.Fa "NAME" 162.Fa "int access" 163.Fa "unsigned long *arg" 164.Fa "0" 165.Fa "char *descr" 166.Fc 167.Ft struct sysctl_oid * 168.Fo SYSCTL_ADD_OPAQUE 169.Fa "struct sysctl_ctx_list *ctx" 170.Fa "struct sysctl_oid_list *parent" 171.Fa "int number" 172.Fa "NAME" 173.Fa "int access" 174.Fa "void *arg" 175.Fa "size_t *len" 176.Fa "char *descr" 177.Fc 178.Ft struct sysctl_oid * 179.Fo SYSCTL_ADD_STRUCT 180.Fa "struct sysctl_ctx_list *ctx" 181.Fa "struct sysctl_oid_list *parent" 182.Fa "int number" 183.Fa "NAME" 184.Fa "int access" 185.Fa "struct TYPE *arg" 186.Fa "TYPE" 187.Fa "char *descr" 188.Fc 189.Ft struct sysctl_oid * 190.Fo SYSCTL_ADD_PROC 191.Fa "struct sysctl_ctx_list *ctx" 192.Fa "struct sysctl_oid_list *parent" 193.Fa "int number" 194.Fa "NAME" 195.Fa "int access" 196.Fa "0" 197.Fa "0" 198.Fa "int (*handler) (SYSCTL_HANDLER_ARGS)" 199.Fa "char *format" 200.Fa "char *descr" 201.Fc 202.Sh DESCRIPTION 203These functions and macros provide an interface 204for creating and deleting sysctl oids at runtime 205(e.g. during lifetime of a module). 206The alternative method, 207based on linker sets (see 208.In sys/linker_set.h 209and 210.\" XXX Manual pages should avoid referencing source files 211.Pa src/sys/kern/kern_sysctl.c 212for details), only allows creation and deletion 213on module load and unload respectively. 214.Pp 215Dynamic oids of type 216.Dv CTLTYPE_NODE 217are reusable 218so that several code sections can create and delete them, 219but in reality they are allocated and freed 220based on their reference count. 221As a consequence, 222it is possible for two or more code sections 223to create partially overlapping trees that they both can use. 224It is not possible to create overlapping leaves, 225nor to create different child types with the same name and parent. 226.Pp 227Newly created oids are connected to their parent nodes. 228In all these functions and macros 229(with the exception of 230.Fn sysctl_remove_oid ) , 231one of the required parameters is 232.Fa parent , 233which points to the head of the parent's list of children. 234.Pp 235Most top level categories are created statically. 236When connecting to existing static oids, 237this pointer can be obtained with the 238.Fn SYSCTL_STATIC_CHILDREN 239macro, where the 240.Fa OID_NAME 241argumwent is name of the parent oid of type 242.Dv CTLTYPE_NODE 243(i.e. the name displayed by 244.Xr sysctl 8 , 245preceded by underscore, and with all dots replaced with underscores). 246.Pp 247When connecting to an existing dynamic oid, this pointer 248can be obtained with the 249.Fn SYSCTL_CHILDREN 250macro, where the 251.Fa oidp 252argument points to the parent oid of type 253.Dv CTLTYPE_NODE . 254.Pp 255The 256.Fn sysctl_add_oid 257function creates raw oids of any type. 258If the oid is successfuly created, 259the function returns a pointer to it; 260otherwise it returns 261.Dv NULL . 262Many of the arguments for 263.Fn sysctl_add_oid 264are common to the macros. 265The arguments are as follows: 266.Bl -tag -width handler 267.It Fa ctx 268A pointer to an optional sysctl context, or 269.Dv NULL . 270See 271.Xr sysctl_ctx_init 9 272for details. 273Programmers are strongly advised to use contexts 274to organize the dynamic oids which they create, 275unless special creation and deletion sequences are required. 276If 277.Fa ctx 278is not 279.Dv NULL , 280the newly created oid will be added to this context 281as its first entry. 282.It Fa parent 283A pointer to a 284.Li struct sysctl_oid_list , 285which is the head of the parent's list of children. 286.It Fa number 287The oid number that will be assigned to this oid. 288In almost all cases this should be set to 289.Dv OID_AUTO , 290which will result in the assignment of the next available oid number. 291.It Fa name 292The name of the oid. 293The newly created oid will contain a copy of the name. 294.It Fa kind 295The kind of oid, 296specified as a bitmask of the type and access values defined in the 297.In sys/sysctl.h 298header file. 299Oids created dynamically always have the 300.Dv CTLTYPE_DYN 301flag set. 302Access flags specify whether this oid is read-only or read-write, 303and whether it may be modified by all users 304or by the supseruser only. 305.It Fa arg1 306A pointer to any data that the oid should reference, or 307.Dv NULL . 308.It Fa arg2 309The size of 310.Fa arg1 , 311or 0 if 312.Fa arg1 313is 314.Dv NULL . 315.It Fa handler 316A pointer to the function 317that is responsible for handling read and write requests 318to this oid. 319There are several standard handlers 320that support operations on nodes, 321integers, strings and opaque objects. 322It is possible also to define new handlers using the 323.Fn SYSCTL_ADD_PROC 324macro. 325.It Fa format 326A pointer to a string 327which specifies the format of the oid symbolically. 328This format is used as a hint by 329.Xr sysctl 8 330to apply proper data formatting for display purposes. 331Currently used format names are: 332.Dq N 333for node, 334.Dq A 335for 336.Li "char *" , 337.Dq I 338for 339.Li "int" , 340.Dq IU 341for 342.Li "unsigned int" , 343.Dq L 344for 345.Li "long" , 346.Dq LU 347for 348.Li "unsigned long" 349and 350.Dq S,TYPE 351for 352.Li "struct TYPE" 353structures. 354.It Fa descr 355A pointer to a textual description of the oid. 356.El 357.Pp 358The 359.Fn sysctl_remove_oid 360function removes a dynamically created oid from the tree, 361optionally freeing its resources. 362It takes the following arguments: 363.Bl -tag -width recurse 364.It Fa oidp 365A pointer to the dynamic oid to be removed. 366If the oid is not dynamic, or the pointer is 367.Dv NULL , 368the function returns 369.Er EINVAL . 370.It Fa del 371If non-zero, 372.Fn sysctl_remove_oid 373will try to free the oid's resources 374when the reference count of the oid becomes zero. 375However, if 376.Fa del 377is set to 0, 378the routine will only deregister the oid from the tree, 379without freeing its resources. 380This behaviour is useful when the caller expects to rollback 381(possibly partially failed) 382deletion of many oids later. 383.It Fa recurse 384If non-zero, attempt to remove the node and all its children. 385If 386.Pa recurse 387is set to 0, 388any attempt to remove a node that contains any children 389will result in a 390.Er ENOTEMPTY 391error. 392.Em WARNING : "use recursive deletion with extreme caution" ! 393Normally it should not be needed if contexts are used. 394Contexts take care of tracking inter-dependencies 395between users of the tree. 396However, in some extreme cases it might be necessary 397to remove part of the subtree no matter how it was created, 398in order to free some other resources. 399Be aware, though, that this may result in a system 400.Xr panic 9 401if other code sections continue to use removed subtrees. 402.El 403.Pp 404.\" XXX sheldonh finished up to here 405Again, in most cases the programmer should use contexts, 406as described in 407.Xr sysctl_ctx_init 9 , 408to keep track of created oids, 409and to delete them later in orderly fashion. 410.Pp 411There is a set of macros defined 412that helps to create oids of given type. 413.Bl -tag -width SYSCTL_ADD_STRINGXX 414They are as follows: 415.It Fn SYSCTL_ADD_OID 416creates a raw oid. 417This macro is functionally equivalent to the 418.Fn sysctl_add_oid 419function. 420.It Fn SYSCTL_ADD_NODE 421creates an oid of type 422.Dv CTLTYPE_NODE , 423to which child oids may be added. 424.It Fn SYSCTL_ADD_STRING 425creates an oid that handles a zero-terminated character string. 426.It Fn SYSCTL_ADD_INT 427creates an oid that handles an 428.Li int 429variable. 430.It Fn SYSCTL_ADD_QUAD 431creates an oid that handles a 64-bit 432.Li int 433variable. 434.It Fn SYSCTL_ADD_UQUAD 435creates an oid that handles a 64-bit 436.Li unsigned int 437variable. 438.It Fn SYSCTL_ADD_UINT 439creates an oid that handles an 440.Li unsigned int 441variable. 442.It Fn SYSCTL_ADD_LONG 443creates an oid that handles a 444.Li long 445variable. 446.It Fn SYSCTL_ADD_ULONG 447creates an oid that handles an 448.Li unsigned long 449variable. 450.It Fn SYSCTL_ADD_OPAQUE 451creates an oid that handles any chunk of opaque data 452of the size specified by the 453.Fa len 454argument, 455which is a pointer to a 456.Li "size_t *" . 457.It Fn SYSCTL_ADD_STRUCT 458creates an oid that handles a 459.Li "struct TYPE" 460structure. 461The 462.Fa format 463parameter will be set to 464.Dq S,TYPE 465to provide proper hints to the 466.Xr sysctl 8 467utility. 468.It Fn SYSCTL_ADD_PROC 469creates an oid with the specified 470.Pa handler 471function. 472The handler is responsible for handling read and write requests 473to the oid. 474This oid type is especially useful 475if the kernel data is not easily accessible, 476or needs to be processed before exporting. 477.El 478.Sh EXAMPLES 479The following is an example of 480how to create a new top-level category 481and how to hook up another subtree to an existing static node. 482This example does not use contexts, 483which results in tedious management of all intermediate oids, 484as they need to be freed later on: 485.Bd -literal 486#include <sys/sysctl.h> 487 ... 488/* Need to preserve pointers to newly created subtrees, to be able 489 * to free them later. 490 */ 491struct sysctl_oid *root1, *root2, *oidp; 492int a_int; 493char *string = "dynamic sysctl"; 494 ... 495 496root1 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(/* tree top */), 497 OID_AUTO, newtree, CTFLAG_RW, 0, "new top level tree"); 498oidp = SYSCTL_ADD_INT( NULL, SYSCTL_CHILDREN(root1), 499 OID_AUTO, newint, CTLFLAG_RW, &a_int, 0, "new int leaf"); 500 ... 501root2 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(_debug), 502 OID_AUTO, newtree, CTFLAG_RW, 0, "new tree under debug"); 503oidp = SYSCTL_ADD_STRING( NULL, SYSCTL_CHILDREN(root2), 504 OID_AUTO, newstring, CTLFLAG_R, string, 0, "new string leaf"); 505.Ed 506.Pp 507This example creates the following subtrees: 508.Bd -literal -offset indent 509debug.newtree.newstring 510newtree.newint 511.Ed 512.Pp 513.Em "Care should be taken to free all oids once they are no longer needed!" 514.Sh SEE ALSO 515.Xr sysctl 8 , 516.Xr sysctl 9 , 517.Xr sysctl_ctx_free 9 , 518.Xr sysctl_ctx_init 9 519.Sh HISTORY 520These functions first appeared in 521.Fx 4.2 . 522.Sh AUTHORS 523.An Andrzej Bialecki Aq abial@FreeBSD.org 524.Sh BUGS 525Sharing nodes between many code sections 526causes interdependencies that sometimes may lock the resources. 527For example, 528if module A hooks up a subtree to an oid created by module B, 529module B will be unable to delete that oid. 530These issues are handled properly by sysctl contexts. 531.Pp 532Many operations on the tree involve traversing linked lists. 533For this reason, oid creation and removal is relatively costly. 534