1bd3cdc31SAndrzej Bialecki.\" 2e87b9f28SJeroen Ruigrok van der Werven.\" Copyright (c) 2000, Andrzej Bialecki <abial@FreeBSD.org> 3bd3cdc31SAndrzej Bialecki.\" All rights reserved. 4bd3cdc31SAndrzej Bialecki.\" 5bd3cdc31SAndrzej Bialecki.\" Redistribution and use in source and binary forms, with or without 6bd3cdc31SAndrzej Bialecki.\" modification, are permitted provided that the following conditions 7bd3cdc31SAndrzej Bialecki.\" are met: 8bd3cdc31SAndrzej Bialecki.\" 1. Redistributions of source code must retain the above copyright 9bd3cdc31SAndrzej Bialecki.\" notice, this list of conditions and the following disclaimer. 10bd3cdc31SAndrzej Bialecki.\" 2. Redistributions in binary form must reproduce the above copyright 11bd3cdc31SAndrzej Bialecki.\" notice, this list of conditions and the following disclaimer in the 12bd3cdc31SAndrzej Bialecki.\" documentation and/or other materials provided with the distribution. 13bd3cdc31SAndrzej Bialecki.\" 3. The name of the author may not be used to endorse or promote products 14bd3cdc31SAndrzej Bialecki.\" derived from this software without specific prior written permission. 15bd3cdc31SAndrzej Bialecki.\" 16bd3cdc31SAndrzej Bialecki.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17bd3cdc31SAndrzej Bialecki.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18bd3cdc31SAndrzej Bialecki.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19bd3cdc31SAndrzej Bialecki.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20bd3cdc31SAndrzej Bialecki.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21bd3cdc31SAndrzej Bialecki.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22bd3cdc31SAndrzej Bialecki.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23bd3cdc31SAndrzej Bialecki.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24bd3cdc31SAndrzej Bialecki.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25bd3cdc31SAndrzej Bialecki.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26bd3cdc31SAndrzej Bialecki.\" SUCH DAMAGE. 27bd3cdc31SAndrzej Bialecki.\" 28*1e1f3941SEd Schouten.Dd December 13, 2016 293ae5a454SSheldon Hearn.Dt SYSCTL_ADD_OID 9 303ae5a454SSheldon Hearn.Os 31bd3cdc31SAndrzej Bialecki.Sh NAME 32bd3cdc31SAndrzej Bialecki.Nm sysctl_add_oid , 33c7ea226dSDag-Erling Smørgrav.Nm sysctl_move_oid , 34b5177e00SHans Petter Selasky.Nm sysctl_remove_oid , 35b5177e00SHans Petter Selasky.Nm sysctl_remove_name 36bd3cdc31SAndrzej Bialecki.Nd runtime sysctl tree manipulation 37bd3cdc31SAndrzej Bialecki.Sh SYNOPSIS 38c042007eSBruce Evans.In sys/types.h 3932eef9aeSRuslan Ermilov.In sys/sysctl.h 40bd3cdc31SAndrzej Bialecki.Ft struct sysctl_oid * 41bd3cdc31SAndrzej Bialecki.Fo sysctl_add_oid 42bd3cdc31SAndrzej Bialecki.Fa "struct sysctl_ctx_list *ctx" 43bd3cdc31SAndrzej Bialecki.Fa "struct sysctl_oid_list *parent" 44bd3cdc31SAndrzej Bialecki.Fa "int number" 45c042007eSBruce Evans.Fa "const char *name" 46bd3cdc31SAndrzej Bialecki.Fa "int kind" 47bd3cdc31SAndrzej Bialecki.Fa "void *arg1" 48be87839eSConrad Meyer.Fa "intmax_t arg2" 49bd3cdc31SAndrzej Bialecki.Fa "int (*handler) (SYSCTL_HANDLER_ARGS)" 50c042007eSBruce Evans.Fa "const char *format" 51c042007eSBruce Evans.Fa "const char *descr" 52*1e1f3941SEd Schouten.Fa "const char *label" 53bd3cdc31SAndrzej Bialecki.Fc 54bd3cdc31SAndrzej Bialecki.Ft int 55c7ea226dSDag-Erling Smørgrav.Fo sysctl_move_oid 56c7ea226dSDag-Erling Smørgrav.Fa "struct sysctl_oid *oidp" 57c7ea226dSDag-Erling Smørgrav.Fa "struct sysctl_oid_list *parent" 58c7ea226dSDag-Erling Smørgrav.Fc 59c7ea226dSDag-Erling Smørgrav.Ft int 60bd3cdc31SAndrzej Bialecki.Fo sysctl_remove_oid 61bd3cdc31SAndrzej Bialecki.Fa "struct sysctl_oid *oidp" 62bd3cdc31SAndrzej Bialecki.Fa "int del" 63bd3cdc31SAndrzej Bialecki.Fa "int recurse" 64bd3cdc31SAndrzej Bialecki.Fc 65b5177e00SHans Petter Selasky.Ft int 66b5177e00SHans Petter Selasky.Fo sysctl_remove_name 67b5177e00SHans Petter Selasky.Fa "struct sysctl_oid *oidp" 68b5177e00SHans Petter Selasky.Fa "const char *name" 69b5177e00SHans Petter Selasky.Fa "int del" 70b5177e00SHans Petter Selasky.Fa "int recurse" 71b5177e00SHans Petter Selasky.Fc 72bd3cdc31SAndrzej Bialecki.Sh DESCRIPTION 735eb1caa8SHans Petter SelaskyThese functions provide the interface for creating and deleting sysctl 745eb1caa8SHans Petter SelaskyOIDs at runtime for example during the lifetime of a module. 755eb1caa8SHans Petter SelaskyThe wrapper macros defined by 765eb1caa8SHans Petter Selasky.Xr sysctl 9 775eb1caa8SHans Petter Selaskyare recommended when creating new OIDs. 785eb1caa8SHans Petter Selasky.Fn sysctl_add_oid 795eb1caa8SHans Petter Selaskyshould not be called directly from the code. 80bd3cdc31SAndrzej Bialecki.Pp 815eb1caa8SHans Petter SelaskyDynamic OIDs of type 823ae5a454SSheldon Hearn.Dv CTLTYPE_NODE 833ae5a454SSheldon Hearnare reusable 843ae5a454SSheldon Hearnso that several code sections can create and delete them, 853ae5a454SSheldon Hearnbut in reality they are allocated and freed 863ae5a454SSheldon Hearnbased on their reference count. 873ae5a454SSheldon HearnAs a consequence, 883ae5a454SSheldon Hearnit is possible for two or more code sections 893ae5a454SSheldon Hearnto create partially overlapping trees that they both can use. 903ae5a454SSheldon HearnIt is not possible to create overlapping leaves, 91bdd87f4fSDima Dorfmannor to create different child types with the same name and parent. 92bd3cdc31SAndrzej Bialecki.Pp 933ae5a454SSheldon HearnThe 94bd3cdc31SAndrzej Bialecki.Fn sysctl_add_oid 955eb1caa8SHans Petter Selaskyfunction creates a raw OID of any type and connects it to its parent node, if any. 965eb1caa8SHans Petter SelaskyIf the OID is successfully created, 975eb1caa8SHans Petter Selaskythe function returns a pointer to it else 985eb1caa8SHans Petter Selaskyit returns 993ae5a454SSheldon Hearn.Dv NULL . 1003ae5a454SSheldon HearnMany of the arguments for 101bd3cdc31SAndrzej Bialecki.Fn sysctl_add_oid 1025eb1caa8SHans Petter Selaskyare common to the wrapper macros defined by 1035eb1caa8SHans Petter Selasky.Xr sysctl 9 . 104bd3cdc31SAndrzej Bialecki.Pp 1053ae5a454SSheldon HearnThe 106c7ea226dSDag-Erling Smørgrav.Fn sysctl_move_oid 1075eb1caa8SHans Petter Selaskyfunction reparents an existing OID. 1085eb1caa8SHans Petter SelaskyThe OID is assigned a new number as if it had been created with 109c7ea226dSDag-Erling Smørgrav.Fa number 110c7ea226dSDag-Erling Smørgravset to 111c7ea226dSDag-Erling Smørgrav.Dv OID_AUTO . 112c7ea226dSDag-Erling Smørgrav.Pp 113c7ea226dSDag-Erling SmørgravThe 114bd3cdc31SAndrzej Bialecki.Fn sysctl_remove_oid 1155eb1caa8SHans Petter Selaskyfunction removes a dynamically created OID from the tree and 1163ae5a454SSheldon Hearnoptionally freeing its resources. 1173ae5a454SSheldon HearnIt takes the following arguments: 118bd3cdc31SAndrzej Bialecki.Bl -tag -width recurse 1193ae5a454SSheldon Hearn.It Fa oidp 1205eb1caa8SHans Petter SelaskyA pointer to the dynamic OID to be removed. 1215eb1caa8SHans Petter SelaskyIf the OID is not dynamic, or the pointer is 1223ae5a454SSheldon Hearn.Dv NULL , 1233ae5a454SSheldon Hearnthe function returns 1243ae5a454SSheldon Hearn.Er EINVAL . 1253ae5a454SSheldon Hearn.It Fa del 1263ae5a454SSheldon HearnIf non-zero, 127bd3cdc31SAndrzej Bialecki.Fn sysctl_remove_oid 1285eb1caa8SHans Petter Selaskywill try to free the OID's resources 1295eb1caa8SHans Petter Selaskywhen the reference count of the OID becomes zero. 1303ae5a454SSheldon HearnHowever, if 1313ae5a454SSheldon Hearn.Fa del 1323ae5a454SSheldon Hearnis set to 0, 1335eb1caa8SHans Petter Selaskythe routine will only deregister the OID from the tree, 1343ae5a454SSheldon Hearnwithout freeing its resources. 1353ae5a454SSheldon HearnThis behaviour is useful when the caller expects to rollback 1363ae5a454SSheldon Hearn(possibly partially failed) 1375eb1caa8SHans Petter Selaskydeletion of many OIDs later. 1383ae5a454SSheldon Hearn.It Fa recurse 1393ae5a454SSheldon HearnIf non-zero, attempt to remove the node and all its children. 140bd3cdc31SAndrzej BialeckiIf 141bd3cdc31SAndrzej Bialecki.Pa recurse 1423ae5a454SSheldon Hearnis set to 0, 1433ae5a454SSheldon Hearnany attempt to remove a node that contains any children 1443ae5a454SSheldon Hearnwill result in a 1453ae5a454SSheldon Hearn.Er ENOTEMPTY 1463ae5a454SSheldon Hearnerror. 147d0353b83SRuslan Ermilov.Em WARNING : "use recursive deletion with extreme caution" ! 1483ae5a454SSheldon HearnNormally it should not be needed if contexts are used. 1493ae5a454SSheldon HearnContexts take care of tracking inter-dependencies 1503ae5a454SSheldon Hearnbetween users of the tree. 1513ae5a454SSheldon HearnHowever, in some extreme cases it might be necessary 1523ae5a454SSheldon Hearnto remove part of the subtree no matter how it was created, 1533ae5a454SSheldon Hearnin order to free some other resources. 1543ae5a454SSheldon HearnBe aware, though, that this may result in a system 1553ae5a454SSheldon Hearn.Xr panic 9 1563ae5a454SSheldon Hearnif other code sections continue to use removed subtrees. 157bd3cdc31SAndrzej Bialecki.El 158bd3cdc31SAndrzej Bialecki.Pp 159b5177e00SHans Petter SelaskyThe 160b5177e00SHans Petter Selasky.Fn sysctl_remove_name 161b5177e00SHans Petter Selaskyfunction looks up the child node matching the 162b5177e00SHans Petter Selasky.Fa name 163b5177e00SHans Petter Selaskyargument and then invokes the 164b5177e00SHans Petter Selasky.Fn sysctl_remove_oid 165b5177e00SHans Petter Selaskyfunction on that node, passing along the 166b5177e00SHans Petter Selasky.Fa del 167b5177e00SHans Petter Selaskyand 168b5177e00SHans Petter Selasky.Fa recurse 169b5177e00SHans Petter Selaskyarguments. 170b5177e00SHans Petter SelaskyIf a node having the specified name does not exist an error code of 171b5177e00SHans Petter Selasky.Er ENOENT 172b5177e00SHans Petter Selaskyis returned. 173b5177e00SHans Petter SelaskyElse the error code from 174b5177e00SHans Petter Selasky.Fn sysctl_remove_oid 175b5177e00SHans Petter Selaskyis returned. 176b5177e00SHans Petter Selasky.Pp 177b5177e00SHans Petter SelaskyIn most cases the programmer should use contexts, 1783ae5a454SSheldon Hearnas described in 1793ae5a454SSheldon Hearn.Xr sysctl_ctx_init 9 , 1805eb1caa8SHans Petter Selaskyto keep track of created OIDs, 1813ae5a454SSheldon Hearnand to delete them later in orderly fashion. 182bd3cdc31SAndrzej Bialecki.Sh SEE ALSO 1839400b289SSheldon Hearn.Xr sysctl 8 , 184dba9e9ccSRobert Watson.Xr sysctl 9 , 185bd3cdc31SAndrzej Bialecki.Xr sysctl_ctx_free 9 , 1869400b289SSheldon Hearn.Xr sysctl_ctx_init 9 187bd3cdc31SAndrzej Bialecki.Sh HISTORY 1883ae5a454SSheldon HearnThese functions first appeared in 18986f47875SSheldon Hearn.Fx 4.2 . 190bd3cdc31SAndrzej Bialecki.Sh AUTHORS 1918a7314fcSBaptiste Daroussin.An Andrzej Bialecki Aq Mt abial@FreeBSD.org 192bd3cdc31SAndrzej Bialecki.Sh BUGS 1933ae5a454SSheldon HearnSharing nodes between many code sections 1943ae5a454SSheldon Hearncauses interdependencies that sometimes may lock the resources. 1953ae5a454SSheldon HearnFor example, 1965eb1caa8SHans Petter Selaskyif module A hooks up a subtree to an OID created by module B, 1975eb1caa8SHans Petter Selaskymodule B will be unable to delete that OID. 1983ae5a454SSheldon HearnThese issues are handled properly by sysctl contexts. 1993ae5a454SSheldon Hearn.Pp 2003ae5a454SSheldon HearnMany operations on the tree involve traversing linked lists. 2015eb1caa8SHans Petter SelaskyFor this reason, OID creation and removal is relatively costly. 202