xref: /freebsd/share/man/man9/sysctl_ctx_init.9 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
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*5eb1caa8SHans Petter Selasky.Dd July 31, 2014
29785c4f1aSSheldon Hearn.Dt SYSCTL_CTX_INIT 9
30785c4f1aSSheldon Hearn.Os
31bd3cdc31SAndrzej Bialecki.Sh NAME
32bd3cdc31SAndrzej Bialecki.Nm sysctl_ctx_init ,
33bd3cdc31SAndrzej Bialecki.Nm sysctl_ctx_free ,
34bd3cdc31SAndrzej Bialecki.Nm sysctl_ctx_entry_add ,
35bd3cdc31SAndrzej Bialecki.Nm sysctl_ctx_entry_find ,
36785c4f1aSSheldon Hearn.Nm sysctl_ctx_entry_del
37*5eb1caa8SHans Petter Selasky.Nd "sysctl context for managing dynamically created sysctl OIDs"
38bd3cdc31SAndrzej Bialecki.Sh SYNOPSIS
3932eef9aeSRuslan Ermilov.In sys/types.h
4032eef9aeSRuslan Ermilov.In sys/sysctl.h
41bd3cdc31SAndrzej Bialecki.Ft int
42bd3cdc31SAndrzej Bialecki.Fo sysctl_ctx_init
43bd3cdc31SAndrzej Bialecki.Fa "struct sysctl_ctx_list *clist"
44bd3cdc31SAndrzej Bialecki.Fc
45bd3cdc31SAndrzej Bialecki.Ft int
46bd3cdc31SAndrzej Bialecki.Fo sysctl_ctx_free
47bd3cdc31SAndrzej Bialecki.Fa "struct sysctl_ctx_list *clist"
48bd3cdc31SAndrzej Bialecki.Fc
49bd3cdc31SAndrzej Bialecki.Ft struct sysctl_ctx_entry *
50bd3cdc31SAndrzej Bialecki.Fo sysctl_ctx_entry_add
51bd3cdc31SAndrzej Bialecki.Fa "struct sysctl_ctx_list *clist"
52bd3cdc31SAndrzej Bialecki.Fa "struct sysctl_oid *oidp"
53bd3cdc31SAndrzej Bialecki.Fc
54bd3cdc31SAndrzej Bialecki.Ft struct sysctl_ctx_entry *
55bd3cdc31SAndrzej Bialecki.Fo sysctl_ctx_entry_find
56bd3cdc31SAndrzej Bialecki.Fa "struct sysctl_ctx_list *clist"
57bd3cdc31SAndrzej Bialecki.Fa "struct sysctl_oid *oidp"
58bd3cdc31SAndrzej Bialecki.Fc
59bd3cdc31SAndrzej Bialecki.Ft int
60bd3cdc31SAndrzej Bialecki.Fo sysctl_ctx_entry_del
61bd3cdc31SAndrzej Bialecki.Fa "struct sysctl_ctx_list *clist"
62bd3cdc31SAndrzej Bialecki.Fa "struct sysctl_oid *oidp"
63bd3cdc31SAndrzej Bialecki.Fc
64bd3cdc31SAndrzej Bialecki.Sh DESCRIPTION
65785c4f1aSSheldon HearnThese functions provide an interface
66*5eb1caa8SHans Petter Selaskyfor managing dynamically created OIDs.
67*5eb1caa8SHans Petter SelaskyThe sysctl context is responsible for keeping track of created OIDs,
68785c4f1aSSheldon Hearnas well as their proper removal when needed.
69*5eb1caa8SHans Petter SelaskyIt adds a simple transactional aspect to OID removal operations;
705203edcdSRuslan Ermilovi.e., if a removal operation fails part way,
71785c4f1aSSheldon Hearnit is possible to roll back the sysctl tree
72785c4f1aSSheldon Hearnto its previous state.
73bd3cdc31SAndrzej Bialecki.Pp
74785c4f1aSSheldon HearnThe
75785c4f1aSSheldon Hearn.Fn sysctl_ctx_init
76785c4f1aSSheldon Hearnfunction initializes a sysctl context.
77785c4f1aSSheldon HearnThe
78785c4f1aSSheldon Hearn.Fa clist
79785c4f1aSSheldon Hearnargument must point to an already allocated variable.
80785c4f1aSSheldon HearnA context
81785c4f1aSSheldon Hearn.Em must
82785c4f1aSSheldon Hearnbe initialized before use.
83785c4f1aSSheldon HearnOnce it is initialized,
84785c4f1aSSheldon Hearna pointer to the context can be passed as an argument to all the
85785c4f1aSSheldon Hearn.Fa SYSCTL_ADD_*
86785c4f1aSSheldon Hearnmacros (see
87785c4f1aSSheldon Hearn.Xr sysctl_add_oid 9 ) ,
88*5eb1caa8SHans Petter Selaskyand it will be updated with entries pointing to newly created OIDS.
89785c4f1aSSheldon Hearn.Pp
90785c4f1aSSheldon HearnInternally, the context is represented as a
91785c4f1aSSheldon Hearn.Xr queue 3
92785c4f1aSSheldon HearnTAILQ linked list.
93785c4f1aSSheldon HearnThe list consists of
94785c4f1aSSheldon Hearn.Li struct sysctl_ctx_entry
95bd3cdc31SAndrzej Bialeckientries:
96bd3cdc31SAndrzej Bialecki.Bd -literal -offset indent
97bd3cdc31SAndrzej Bialeckistruct sysctl_ctx_entry {
98bd3cdc31SAndrzej Bialecki	struct sysctl_oid *entry;
99bd3cdc31SAndrzej Bialecki	TAILQ_ENTRY(sysctl_ctx_entry) link;
100bd3cdc31SAndrzej Bialecki};
101bd3cdc31SAndrzej Bialecki
102bd3cdc31SAndrzej BialeckiTAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry);
103bd3cdc31SAndrzej Bialecki.Ed
104bd3cdc31SAndrzej Bialecki.Pp
105*5eb1caa8SHans Petter SelaskyEach context entry points to one dynamic OID that it manages.
106*5eb1caa8SHans Petter SelaskyNewly created OIDs are always inserted in the front of the list.
107bd3cdc31SAndrzej Bialecki.Pp
108785c4f1aSSheldon HearnThe
109bd3cdc31SAndrzej Bialecki.Fn sysctl_ctx_free
110*5eb1caa8SHans Petter Selaskyfunction removes the context and associated OIDs it manages.
111b82af3f5SMike PritchardIf the function completes successfully,
112*5eb1caa8SHans Petter Selaskyall managed OIDs have been unregistered
113785c4f1aSSheldon Hearn(removed from the tree)
114785c4f1aSSheldon Hearnand freed,
115785c4f1aSSheldon Hearntogether with all their allocated memory,
116785c4f1aSSheldon Hearnand the entries of the context have been freed as well.
117785c4f1aSSheldon Hearn.Pp
118785c4f1aSSheldon HearnThe removal operation is performed in two steps.
119785c4f1aSSheldon HearnFirst, for each context entry, the function
120785c4f1aSSheldon Hearn.Xr sysctl_remove_oid 9
121bd3cdc31SAndrzej Bialeckiis executed, with parameter
122785c4f1aSSheldon Hearn.Fa del
123785c4f1aSSheldon Hearnset to 0, which inhibits the freeing of resources.
124bd3cdc31SAndrzej BialeckiIf there are no errors during this step,
125785c4f1aSSheldon Hearn.Fn sysctl_ctx_free
126785c4f1aSSheldon Hearnproceeds to the next step.
127785c4f1aSSheldon HearnIf the first step fails,
128*5eb1caa8SHans Petter Selaskyall unregistered OIDs associated with the context are registered again.
129785c4f1aSSheldon Hearn.Pp
130785c4f1aSSheldon Hearn.Em Note :
131785c4f1aSSheldon Hearnin most cases, the programmer specifies
132785c4f1aSSheldon Hearn.Dv OID_AUTO
133*5eb1caa8SHans Petter Selaskyas the OID number when creating an OID.
134*5eb1caa8SHans Petter SelaskyHowever, during registration of the OID in the tree,
135785c4f1aSSheldon Hearnthis number is changed to the first available number
13631a35b79SRemko Loddergreater than or equal to
13731a35b79SRemko Lodder.Dv CTL_AUTO_START .
138785c4f1aSSheldon HearnIf the first step of context deletion fails,
139*5eb1caa8SHans Petter Selaskyre-registration of the OID does not change the already assigned OID number
140785c4f1aSSheldon Hearn(which is different from OID_AUTO).
141785c4f1aSSheldon HearnThis ensures that re-registered entries
142785c4f1aSSheldon Hearnmaintain their original positions in the tree.
143785c4f1aSSheldon Hearn.Pp
144*5eb1caa8SHans Petter SelaskyThe second step actually performs the deletion of the dynamic OIDs.
145785c4f1aSSheldon Hearn.Xr sysctl_remove_oid 9
146785c4f1aSSheldon Hearniterates through the context list,
1475203edcdSRuslan Ermilovstarting from beginning (i.e., the newest entries).
148785c4f1aSSheldon Hearn.Em Important :
149*5eb1caa8SHans Petter Selaskythis time, the function not only deletes the OIDs from the tree,
150785c4f1aSSheldon Hearnbut also frees their memory (provided that oid_refcnt == 0),
151785c4f1aSSheldon Hearnas well as the memory of all context entries.
152bd3cdc31SAndrzej Bialecki.Pp
153bd3cdc31SAndrzej BialeckiThe
154bd3cdc31SAndrzej Bialecki.Fn sysctl_ctx_entry_add
155*5eb1caa8SHans Petter Selaskyfunction allows the addition of an existing dynamic OID to a context.
156bd3cdc31SAndrzej Bialecki.Pp
157bd3cdc31SAndrzej BialeckiThe
158bd3cdc31SAndrzej Bialecki.Fn sysctl_ctx_entry_del
159785c4f1aSSheldon Hearnfunction removes an entry from the context.
160785c4f1aSSheldon Hearn.Em Important :
161785c4f1aSSheldon Hearnin this case, only the corresponding
162785c4f1aSSheldon Hearn.Li struct sysctl_ctx_entry
163785c4f1aSSheldon Hearnis freed, but the
164785c4f1aSSheldon Hearn.Fa oidp
165785c4f1aSSheldon Hearnpointer remains intact.
166785c4f1aSSheldon HearnThereafter, the programmer is responsible for managing the resources
167*5eb1caa8SHans Petter Selaskyallocated to this OID.
168bd3cdc31SAndrzej Bialecki.Pp
169bd3cdc31SAndrzej BialeckiThe
170bd3cdc31SAndrzej Bialecki.Fn sysctl_ctx_entry_find
171785c4f1aSSheldon Hearnfunction searches for a given
172785c4f1aSSheldon Hearn.Fa oidp
173b82af3f5SMike Pritchardwithin a context list,
174785c4f1aSSheldon Hearneither returning a pointer to the
175785c4f1aSSheldon Hearn.Fa struct sysctl_ctx_entry
176785c4f1aSSheldon Hearnfound,
177785c4f1aSSheldon Hearnor
178785c4f1aSSheldon Hearn.Dv NULL .
179bd3cdc31SAndrzej Bialecki.Sh EXAMPLES
180785c4f1aSSheldon HearnThe following is an example of how to create a new top-level category
181785c4f1aSSheldon Hearnand how to hook up another subtree to an existing static node.
182*5eb1caa8SHans Petter SelaskyThis example uses contexts to keep track of the OIDs.
183bd3cdc31SAndrzej Bialecki.Bd -literal
184bd3cdc31SAndrzej Bialecki#include <sys/sysctl.h>
185bd3cdc31SAndrzej Bialecki ...
186*5eb1caa8SHans Petter Selaskystatic struct sysctl_ctx_list clist;
187*5eb1caa8SHans Petter Selaskystatic struct sysctl_oid *oidp;
188*5eb1caa8SHans Petter Selaskystatic int a_int;
189*5eb1caa8SHans Petter Selaskystatic const char *string = "dynamic sysctl";
190bd3cdc31SAndrzej Bialecki ...
191bd3cdc31SAndrzej Bialecki
192bd3cdc31SAndrzej Bialeckisysctl_ctx_init(&clist);
193*5eb1caa8SHans Petter Selaskyoidp = SYSCTL_ADD_ROOT_NODE(&clist,
194bd19a23bSJohn Baldwin	OID_AUTO, "newtree", CTLFLAG_RW, 0, "new top level tree");
195bd3cdc31SAndrzej Bialeckioidp = SYSCTL_ADD_INT(&clist, SYSCTL_CHILDREN(oidp),
196dccd431fSMark Murray	OID_AUTO, "newint", CTLFLAG_RW, &a_int, 0, "new int leaf");
197bd3cdc31SAndrzej Bialecki ...
198bd3cdc31SAndrzej Bialeckioidp = SYSCTL_ADD_NODE(&clist, SYSCTL_STATIC_CHILDREN(_debug),
199bd19a23bSJohn Baldwin	OID_AUTO, "newtree", CTLFLAG_RW, 0, "new tree under debug");
200bd3cdc31SAndrzej Bialeckioidp = SYSCTL_ADD_STRING(&clist, SYSCTL_CHILDREN(oidp),
201bd19a23bSJohn Baldwin	OID_AUTO, "newstring", CTLFLAG_RD, string, 0, "new string leaf");
202bd3cdc31SAndrzej Bialecki ...
203*5eb1caa8SHans Petter Selasky/* Now we can free up the OIDs */
204bd3cdc31SAndrzej Bialeckiif (sysctl_ctx_free(&clist)) {
205*5eb1caa8SHans Petter Selasky	printf("can't free this context - other OIDs depend on it");
206bd3cdc31SAndrzej Bialecki	return (ENOTEMPTY);
207bd3cdc31SAndrzej Bialecki} else {
208bd19a23bSJohn Baldwin	printf("Success!\\n");
209bd3cdc31SAndrzej Bialecki	return (0);
210bd3cdc31SAndrzej Bialecki}
211bd3cdc31SAndrzej Bialecki.Ed
212bd3cdc31SAndrzej Bialecki.Pp
213bd3cdc31SAndrzej BialeckiThis example creates the following subtrees:
214bd3cdc31SAndrzej Bialecki.Bd -literal -offset indent
215bd3cdc31SAndrzej Bialeckidebug.newtree.newstring
216bd3cdc31SAndrzej Bialeckinewtree.newint
217bd3cdc31SAndrzej Bialecki.Ed
218bd3cdc31SAndrzej Bialecki.Pp
219785c4f1aSSheldon HearnNote that both trees are removed, and their resources freed,
220785c4f1aSSheldon Hearnthrough one
221bd3cdc31SAndrzej Bialecki.Fn sysctl_ctx_free
222785c4f1aSSheldon Hearncall, which starts by freeing the newest entries (leaves)
223785c4f1aSSheldon Hearnand then proceeds to free the older entries (in this case the nodes).
224bd3cdc31SAndrzej Bialecki.Sh SEE ALSO
225785c4f1aSSheldon Hearn.Xr queue 3 ,
226bd3cdc31SAndrzej Bialecki.Xr sysctl 8 ,
227dba9e9ccSRobert Watson.Xr sysctl 9 ,
228785c4f1aSSheldon Hearn.Xr sysctl_add_oid 9 ,
229785c4f1aSSheldon Hearn.Xr sysctl_remove_oid 9
230bd3cdc31SAndrzej Bialecki.Sh HISTORY
231785c4f1aSSheldon HearnThese functions first appeared in
23286f47875SSheldon Hearn.Fx 4.2 .
233bd3cdc31SAndrzej Bialecki.Sh AUTHORS
2348a7314fcSBaptiste Daroussin.An Andrzej Bialecki Aq Mt abial@FreeBSD.org
235bd3cdc31SAndrzej Bialecki.Sh BUGS
236785c4f1aSSheldon HearnThe current removal algorithm is somewhat heavy.
237785c4f1aSSheldon HearnIn the worst case,
238*5eb1caa8SHans Petter Selaskyall OIDs need to be unregistered, registered again,
239785c4f1aSSheldon Hearnand then unregistered and deleted.
240785c4f1aSSheldon HearnHowever, the algorithm does guarantee transactional properties
241785c4f1aSSheldon Hearnfor removal operations.
242785c4f1aSSheldon Hearn.Pp
243785c4f1aSSheldon HearnAll operations on contexts involve linked list traversal.
244785c4f1aSSheldon HearnFor this reason,
245785c4f1aSSheldon Hearncreation and removal of entries is relatively costly.
246