]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - share/man/man9/sysctl_add_oid.9
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / share / man / man9 / sysctl_add_oid.9
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
206 These functions and macros provide an interface
207 for creating and deleting sysctl oids at runtime
208 (e.g.\& during lifetime of a module).
209 The alternative method,
210 based on linker sets (see
211 .In sys/linker_set.h
212 and
213 .\" XXX Manual pages should avoid referencing source files
214 .Pa src/sys/kern/kern_sysctl.c
215 for details), only allows creation and deletion
216 on module load and unload respectively.
217 .Pp
218 Dynamic oids of type
219 .Dv CTLTYPE_NODE
220 are reusable
221 so that several code sections can create and delete them,
222 but in reality they are allocated and freed
223 based on their reference count.
224 As a consequence,
225 it is possible for two or more code sections
226 to create partially overlapping trees that they both can use.
227 It is not possible to create overlapping leaves,
228 nor to create different child types with the same name and parent.
229 .Pp
230 Newly created oids are connected to their parent nodes.
231 In all these functions and macros
232 (with the exception of
233 .Fn sysctl_remove_oid ) ,
234 one of the required parameters is
235 .Fa parent ,
236 which points to the head of the parent's list of children.
237 .Pp
238 Most top level categories are created statically.
239 When connecting to existing static oids,
240 this pointer can be obtained with the
241 .Fn SYSCTL_STATIC_CHILDREN
242 macro, where the
243 .Fa OID_NAME
244 argument is name of the parent oid of type
245 .Dv CTLTYPE_NODE
246 (i.e., the name displayed by
247 .Xr sysctl 8 ,
248 preceded by underscore, and with all dots replaced with underscores).
249 .Pp
250 When connecting to an existing dynamic oid, this pointer
251 can be obtained with the
252 .Fn SYSCTL_CHILDREN
253 macro, where the
254 .Fa oidp
255 argument points to the parent oid of type
256 .Dv CTLTYPE_NODE .
257 .Pp
258 The
259 .Fn sysctl_add_oid
260 function creates raw oids of any type.
261 If the oid is successfully created,
262 the function returns a pointer to it;
263 otherwise it returns
264 .Dv NULL .
265 Many of the arguments for
266 .Fn sysctl_add_oid
267 are common to the macros.
268 The arguments are as follows:
269 .Bl -tag -width handler
270 .It Fa ctx
271 A pointer to an optional sysctl context, or
272 .Dv NULL .
273 See
274 .Xr sysctl_ctx_init 9
275 for details.
276 Programmers are strongly advised to use contexts
277 to organize the dynamic oids which they create,
278 unless special creation and deletion sequences are required.
279 If
280 .Fa ctx
281 is not
282 .Dv NULL ,
283 the newly created oid will be added to this context
284 as its first entry.
285 .It Fa parent
286 A pointer to a
287 .Li struct sysctl_oid_list ,
288 which is the head of the parent's list of children.
289 .It Fa number
290 The oid number that will be assigned to this oid.
291 In almost all cases this should be set to
292 .Dv OID_AUTO ,
293 which will result in the assignment of the next available oid number.
294 .It Fa name
295 The name of the oid.
296 The newly created oid will contain a copy of the name.
297 .It Fa kind
298 The kind of oid,
299 specified as a bit mask of the type and access values defined in the
300 .In sys/sysctl.h
301 header file.
302 Oids created dynamically always have the
303 .Dv CTLFLAG_DYN
304 flag set.
305 Access flags specify whether this oid is read-only or read-write,
306 and whether it may be modified by all users
307 or by the superuser only.
308 .It Fa arg1
309 A pointer to any data that the oid should reference, or
310 .Dv NULL .
311 .It Fa arg2
312 The size of
313 .Fa arg1 ,
314 or 0 if
315 .Fa arg1
316 is
317 .Dv NULL .
318 .It Fa handler
319 A pointer to the function
320 that is responsible for handling read and write requests
321 to this oid.
322 There are several standard handlers
323 that support operations on nodes,
324 integers, strings and opaque objects.
325 It is possible also to define new handlers using the
326 .Fn SYSCTL_ADD_PROC
327 macro.
328 .It Fa format
329 A pointer to a string
330 which specifies the format of the oid symbolically.
331 This format is used as a hint by
332 .Xr sysctl 8
333 to apply proper data formatting for display purposes.
334 Currently used format names are:
335 .Dq N
336 for node,
337 .Dq A
338 for
339 .Li "char *" ,
340 .Dq I
341 for
342 .Li "int" ,
343 .Dq IU
344 for
345 .Li "unsigned int" ,
346 .Dq L
347 for
348 .Li "long" ,
349 .Dq LU
350 for
351 .Li "unsigned long"
352 and
353 .Dq S,TYPE
354 for
355 .Li "struct TYPE"
356 structures.
357 .It Fa descr
358 A pointer to a textual description of the oid.
359 .El
360 .Pp
361 The
362 .Fn sysctl_move_oid
363 function reparents an existing oid.
364 The oid is assigned a new number as if it had been created with
365 .Fa number
366 set to
367 .Dv OID_AUTO .
368 .Pp
369 The
370 .Fn sysctl_remove_oid
371 function removes a dynamically created oid from the tree,
372 optionally freeing its resources.
373 It takes the following arguments:
374 .Bl -tag -width recurse
375 .It Fa oidp
376 A pointer to the dynamic oid to be removed.
377 If the oid is not dynamic, or the pointer is
378 .Dv NULL ,
379 the function returns
380 .Er EINVAL .
381 .It Fa del
382 If non-zero,
383 .Fn sysctl_remove_oid
384 will try to free the oid's resources
385 when the reference count of the oid becomes zero.
386 However, if
387 .Fa del
388 is set to 0,
389 the routine will only deregister the oid from the tree,
390 without freeing its resources.
391 This behaviour is useful when the caller expects to rollback
392 (possibly partially failed)
393 deletion of many oids later.
394 .It Fa recurse
395 If non-zero, attempt to remove the node and all its children.
396 If
397 .Pa recurse
398 is set to 0,
399 any attempt to remove a node that contains any children
400 will result in a
401 .Er ENOTEMPTY
402 error.
403 .Em WARNING : "use recursive deletion with extreme caution" !
404 Normally it should not be needed if contexts are used.
405 Contexts take care of tracking inter-dependencies
406 between users of the tree.
407 However, in some extreme cases it might be necessary
408 to remove part of the subtree no matter how it was created,
409 in order to free some other resources.
410 Be aware, though, that this may result in a system
411 .Xr panic 9
412 if other code sections continue to use removed subtrees.
413 .El
414 .Pp
415 .\" XXX sheldonh finished up to here
416 Again, in most cases the programmer should use contexts,
417 as described in
418 .Xr sysctl_ctx_init 9 ,
419 to keep track of created oids,
420 and to delete them later in orderly fashion.
421 .Pp
422 There is a set of macros defined
423 that helps to create oids of given type.
424 They are as follows:
425 .Bl -tag -width SYSCTL_ADD_STRINGXX
426 .It Fn SYSCTL_ADD_OID
427 creates a raw oid.
428 This macro is functionally equivalent to the
429 .Fn sysctl_add_oid
430 function.
431 .It Fn SYSCTL_ADD_NODE
432 creates an oid of type
433 .Dv CTLTYPE_NODE ,
434 to which child oids may be added.
435 .It Fn SYSCTL_ADD_STRING
436 creates an oid that handles a zero-terminated character string.
437 .It Fn SYSCTL_ADD_INT
438 creates an oid that handles an
439 .Li int
440 variable.
441 .It Fn SYSCTL_ADD_UINT
442 creates an oid that handles an
443 .Li unsigned int
444 variable.
445 .It Fn SYSCTL_ADD_LONG
446 creates an oid that handles a
447 .Li long
448 variable.
449 .It Fn SYSCTL_ADD_ULONG
450 creates an oid that handles an
451 .Li unsigned long
452 variable.
453 .It Fn SYSCTL_ADD_QUAD
454 creates an oid that handles an
455 .Li int64_t
456 variable.
457 .It Fn SYSCTL_ADD_OPAQUE
458 creates an oid that handles any chunk of opaque data
459 of the size specified by the
460 .Fa len
461 argument,
462 which is a pointer to a
463 .Li "size_t *" .
464 .It Fn SYSCTL_ADD_STRUCT
465 creates an oid that handles a
466 .Li "struct TYPE"
467 structure.
468 The
469 .Fa format
470 parameter will be set to
471 .Dq S,TYPE
472 to provide proper hints to the
473 .Xr sysctl 8
474 utility.
475 .It Fn SYSCTL_ADD_PROC
476 creates an oid with the specified
477 .Pa handler
478 function.
479 The handler is responsible for handling read and write requests
480 to the oid.
481 This oid type is especially useful
482 if the kernel data is not easily accessible,
483 or needs to be processed before exporting.
484 .El
485 .Sh EXAMPLES
486 The following is an example of
487 how to create a new top-level category
488 and how to hook up another subtree to an existing static node.
489 This example does not use contexts,
490 which results in tedious management of all intermediate oids,
491 as 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  */
498 struct sysctl_oid *root1, *root2, *oidp;
499 int a_int;
500 char *string = "dynamic sysctl";
501  ...
502
503 root1 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(/* tree top */),
504         OID_AUTO, "newtree", CTLFLAG_RW, 0, "new top level tree");
505 oidp = SYSCTL_ADD_INT( NULL, SYSCTL_CHILDREN(root1),
506         OID_AUTO, "newint", CTLFLAG_RW, &a_int, 0, "new int leaf");
507  ...
508 root2 = SYSCTL_ADD_NODE( NULL, SYSCTL_STATIC_CHILDREN(_debug),
509         OID_AUTO, "newtree", CTLFLAG_RW, 0, "new tree under debug");
510 oidp = SYSCTL_ADD_STRING( NULL, SYSCTL_CHILDREN(root2),
511         OID_AUTO, "newstring", CTLFLAG_RD, string, 0, "new string leaf");
512 .Ed
513 .Pp
514 This example creates the following subtrees:
515 .Bd -literal -offset indent
516 debug.newtree.newstring
517 newtree.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
527 These functions first appeared in
528 .Fx 4.2 .
529 .Sh AUTHORS
530 .An Andrzej Bialecki Aq abial@FreeBSD.org
531 .Sh BUGS
532 Sharing nodes between many code sections
533 causes interdependencies that sometimes may lock the resources.
534 For example,
535 if module A hooks up a subtree to an oid created by module B,
536 module B will be unable to delete that oid.
537 These issues are handled properly by sysctl contexts.
538 .Pp
539 Many operations on the tree involve traversing linked lists.
540 For this reason, oid creation and removal is relatively costly.