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