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