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