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