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