SYSCTL_ADD_OID(9)
π SYSCTL_ADD_OID π
okay bestie, so this is from the Kernel Developer's Manual β section 9, kernel-adjacent girlies only. it's giving runtime sysctl tree manipulation and honestly? it's kind of a lot. but we're gonna get through it together, no cap. π
β¨ NAME β¨
sysctl_add_oid, sysctl_move_oid, sysctl_remove_oid, sysctl_remove_name β runtime sysctl tree manipulation π
πΈ SYNOPSIS πΈ
#include <sys/types.h>
#include <sys/sysctl.h>
struct sysctl_oid *
sysctl_add_oid(struct sysctl_ctx_list *ctx,
struct sysctl_oid_list *parent, int number, const char *name,
int kind, void *arg1, intmax_t arg2,
int (*handler) (SYSCTL_HANDLER_ARGS), const char *format,
const char *descr, const char *label);
int
sysctl_move_oid(struct sysctl_oid *oidp,
struct sysctl_oid_list *parent);
int
sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse);
int
sysctl_remove_name(struct sysctl_oid *oidp, const char *name, int del,
int recurse);
π¦ DESCRIPTION π¦
so basically these functions are the interface for creating and deleting sysctl OIDs at runtime β like, say, while a module is just out here living its life. β¨
real talk though: the wrapper macros defined bysysctl(9)are what you should be using when making new OIDs.sysctl_add_oid()should not be called directly from your code. like, please don't. π
Dynamic OIDs of type CTLTYPE_NODE are reusable, so several code sections can create and delete them β but in reality they get allocated and freed based on their reference count. as a consequence, it's possible for two or more code sections to create partially overlapping trees that they both can use. iconic, collaborative, main character energy. π
but β and this is the tea β it is not possible to create overlapping leaves, nor to create different child types with the same name and parent. periodt.
π«Ά the functions, one by one
The sysctl_add_oid() function creates a raw OID of any type and connects it to its parent node, if any. if the OID is successfully created, the function returns a pointer to it β else it returns NULL. π many of the arguments for sysctl_add_oid() are common to the wrapper macros defined by sysctl(9).
The sysctl_move_oid() function reparents an existing OID. the OID gets assigned a new number as if it had been created with number set to OID_AUTO. moving out, rebranding, slay. π
The sysctl_remove_oid() function removes a dynamically created OID from the tree and optionally frees its resources. it takes the following arguments:
oidpβ a pointer to the dynamic OID to be removed. if the OID is not dynamic, or the pointer isNULL, the function returnsEINVAL.delβ if non-zero,sysctl_remove_oid()will try to free the OID's resources when the reference count of the OID becomes zero. however, ifdelis set to0, the routine will only deregister the OID from the tree, without freeing its resources. this behaviour is useful when the caller expects to rollback (possibly partially failed) deletion of many OIDs later. π§recurseβ if non-zero, attempt to remove the node and all its children. ifrecurseis set to0, any attempt to remove a node that contains any children will result in aENOTEMPTYerror.
β οΈ WARNING: use recursive deletion with extreme caution! normally it should not be needed if contexts are used β contexts take care of tracking inter-dependencies between users of the tree. however, in some extreme cases it might be necessary to remove part of the subtree no matter how it was created, in order to free some other resources. be aware, though, that this may result in a panic(9) if other code sections continue to use removed subtrees. π like, genuinely, that's the whole system going down. not cute.
The sysctl_remove_name() function looks up the child node matching the name argument and then invokes the sysctl_remove_oid() function on that node, passing along the del and recurse arguments. if a node having the specified name does not exist, an error code of ENOENT is returned. else the error code from sysctl_remove_oid() is returned. π₯
In most cases the programmer should use contexts, as described in sysctl_ctx_init(9), to keep track of created OIDs, and to delete them later in orderly fashion. it's giving organized. it's giving responsible. we love that for you. π
π SEE ALSO π
β HISTORY β
These functions first appeared in FreeBSD 4.2. vintage, honestly. π
π AUTHORS π
Andrzej Bialecki <abial@FreeBSD.org>
π BUGS π
Sharing nodes between many code sections causes interdependencies that sometimes may lock the resources. for example, if module A hooks up a subtree to an OID created by module B, module B will be unable to delete that OID. π these issues are handled properly by sysctl contexts β so like, use them, bestie.
Many operations on the tree involve traversing linked lists. for this reason, OID creation and removal is relatively costly. π not the fastest girl in the group chat, but she gets there.
and that's the whole page, bestie!! β¨ this one's from the Debian edition, dated December 13, 2016, and it's SYSCTL_ADD_OID(9) β ily, go write some safe sysctls π«ΆππΈ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2000, Andrzej Bialecki <abial@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms