xref: /freebsd-12.1/share/man/man9/sysctl.9 (revision 01b476c8)
1.\"
2.\" Copyright (c) 2006 Robert N. M. Watson
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.\"
14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24.\" SUCH DAMAGE.
25.\"
26.\" $FreeBSD$
27.\"
28.Dd June 11, 2019
29.Dt SYSCTL 9
30.Os
31.Sh NAME
32.Nm SYSCTL_DECL ,
33.Nm SYSCTL_ADD_INT ,
34.Nm SYSCTL_ADD_LONG ,
35.Nm SYSCTL_ADD_NODE ,
36.Nm SYSCTL_ADD_NODE_WITH_LABEL ,
37.Nm SYSCTL_ADD_OPAQUE ,
38.Nm SYSCTL_ADD_PROC ,
39.Nm SYSCTL_ADD_QUAD ,
40.Nm SYSCTL_ADD_ROOT_NODE ,
41.Nm SYSCTL_ADD_S8 ,
42.Nm SYSCTL_ADD_S16 ,
43.Nm SYSCTL_ADD_S32 ,
44.Nm SYSCTL_ADD_S64 ,
45.Nm SYSCTL_ADD_STRING ,
46.Nm SYSCTL_ADD_CONST_STRING ,
47.Nm SYSCTL_ADD_STRUCT ,
48.Nm SYSCTL_ADD_TIMEVAL_SEC ,
49.Nm SYSCTL_ADD_U8 ,
50.Nm SYSCTL_ADD_U16 ,
51.Nm SYSCTL_ADD_U32 ,
52.Nm SYSCTL_ADD_U64 ,
53.Nm SYSCTL_ADD_UAUTO ,
54.Nm SYSCTL_ADD_UINT ,
55.Nm SYSCTL_ADD_ULONG ,
56.Nm SYSCTL_ADD_UQUAD ,
57.Nm SYSCTL_CHILDREN ,
58.Nm SYSCTL_STATIC_CHILDREN ,
59.Nm SYSCTL_NODE_CHILDREN ,
60.Nm SYSCTL_PARENT ,
61.Nm SYSCTL_INT ,
62.Nm SYSCTL_INT_WITH_LABEL ,
63.Nm SYSCTL_LONG ,
64.Nm sysctl_msec_to_ticks ,
65.Nm SYSCTL_NODE ,
66.Nm SYSCTL_NODE_WITH_LABEL ,
67.Nm SYSCTL_OPAQUE ,
68.Nm SYSCTL_PROC ,
69.Nm SYSCTL_QUAD ,
70.Nm SYSCTL_ROOT_NODE ,
71.Nm SYSCTL_S8 ,
72.Nm SYSCTL_S16 ,
73.Nm SYSCTL_S32 ,
74.Nm SYSCTL_S64 ,
75.Nm SYSCTL_STRING ,
76.Nm SYSCTL_CONST_STRING ,
77.Nm SYSCTL_STRUCT ,
78.Nm SYSCTL_TIMEVAL_SEC ,
79.Nm SYSCTL_U8 ,
80.Nm SYSCTL_U16 ,
81.Nm SYSCTL_U32 ,
82.Nm SYSCTL_U64 ,
83.Nm SYSCTL_UINT ,
84.Nm SYSCTL_ULONG ,
85.Nm SYSCTL_UQUAD
86.Nd Dynamic and static sysctl MIB creation functions
87.Sh SYNOPSIS
88.In sys/param.h
89.In sys/sysctl.h
90.Fn SYSCTL_DECL name
91.Ft struct sysctl_oid *
92.Fo SYSCTL_ADD_INT
93.Fa "struct sysctl_ctx_list *ctx"
94.Fa "struct sysctl_oid_list *parent"
95.Fa "int number"
96.Fa "const char *name"
97.Fa "int ctlflags"
98.Fa "int *ptr"
99.Fa "int val"
100.Fa "const char *descr"
101.Fc
102.Ft struct sysctl_oid *
103.Fo SYSCTL_ADD_LONG
104.Fa "struct sysctl_ctx_list *ctx"
105.Fa "struct sysctl_oid_list *parent"
106.Fa "int number"
107.Fa "const char *name"
108.Fa "int ctlflags"
109.Fa "long *ptr"
110.Fa "const char *descr"
111.Fc
112.Ft struct sysctl_oid *
113.Fo SYSCTL_ADD_NODE
114.Fa "struct sysctl_ctx_list *ctx"
115.Fa "struct sysctl_oid_list *parent"
116.Fa "int number"
117.Fa "const char *name"
118.Fa "int ctlflags"
119.Fa "int (*handler)(SYSCTL_HANDLER_ARGS)"
120.Fa "const char *descr"
121.Fc
122.Ft struct sysctl_oid *
123.Fo SYSCTL_ADD_NODE_WITH_LABEL
124.Fa "struct sysctl_ctx_list *ctx"
125.Fa "struct sysctl_oid_list *parent"
126.Fa "int number"
127.Fa "const char *name"
128.Fa "int ctlflags"
129.Fa "int (*handler)(SYSCTL_HANDLER_ARGS)"
130.Fa "const char *descr"
131.Fa "const char *label"
132.Fc
133.Ft struct sysctl_oid *
134.Fo SYSCTL_ADD_OPAQUE
135.Fa "struct sysctl_ctx_list *ctx"
136.Fa "struct sysctl_oid_list *parent"
137.Fa "int number"
138.Fa "const char *name"
139.Fa "int ctlflags"
140.Fa "void *ptr"
141.Fa "intptr_t len"
142.Fa "const char *format"
143.Fa "const char *descr"
144.Fc
145.Ft struct sysctl_oid *
146.Fo SYSCTL_ADD_PROC
147.Fa "struct sysctl_ctx_list *ctx"
148.Fa "struct sysctl_oid_list *parent"
149.Fa "int number"
150.Fa "const char *name"
151.Fa "int ctlflags"
152.Fa "void *arg1"
153.Fa "intptr_t arg2"
154.Fa "int (*handler) (SYSCTL_HANDLER_ARGS)"
155.Fa "const char *format"
156.Fa "const char *descr"
157.Fc
158.Ft struct sysctl_oid *
159.Fo SYSCTL_ADD_QUAD
160.Fa "struct sysctl_ctx_list *ctx"
161.Fa "struct sysctl_oid_list *parent"
162.Fa "int number"
163.Fa "const char *name"
164.Fa "int ctlflags"
165.Fa "int64_t *ptr"
166.Fa "const char *descr"
167.Fc
168.Ft struct sysctl_oid *
169.Fo SYSCTL_ADD_ROOT_NODE
170.Fa "struct sysctl_ctx_list *ctx"
171.Fa "int number"
172.Fa "const char *name"
173.Fa "int ctlflags"
174.Fa "int (*handler)(SYSCTL_HANDLER_ARGS)"
175.Fa "const char *descr"
176.Fc
177.Ft struct sysctl_oid *
178.Fo SYSCTL_ADD_S8
179.Fa "struct sysctl_ctx_list *ctx"
180.Fa "struct sysctl_oid_list *parent"
181.Fa "int number"
182.Fa "const char *name"
183.Fa "int ctlflags"
184.Fa "int8_t *ptr"
185.Fa "int8_t val"
186.Fa "const char *descr"
187.Fc
188.Ft struct sysctl_oid *
189.Fo SYSCTL_ADD_S16
190.Fa "struct sysctl_ctx_list *ctx"
191.Fa "struct sysctl_oid_list *parent"
192.Fa "int number"
193.Fa "const char *name"
194.Fa "int ctlflags"
195.Fa "int16_t *ptr"
196.Fa "int16_t val"
197.Fa "const char *descr"
198.Fc
199.Ft struct sysctl_oid *
200.Fo SYSCTL_ADD_S32
201.Fa "struct sysctl_ctx_list *ctx"
202.Fa "struct sysctl_oid_list *parent"
203.Fa "int number"
204.Fa "const char *name"
205.Fa "int ctlflags"
206.Fa "int32_t *ptr"
207.Fa "int32_t val"
208.Fa "const char *descr"
209.Fc
210.Ft struct sysctl_oid *
211.Fo SYSCTL_ADD_S64
212.Fa "struct sysctl_ctx_list *ctx"
213.Fa "struct sysctl_oid_list *parent"
214.Fa "int number"
215.Fa "const char *name"
216.Fa "int ctlflags"
217.Fa "int64_t *ptr"
218.Fa "int64_t val"
219.Fa "const char *descr"
220.Fc
221.Ft struct sysctl_oid *
222.Fo SYSCTL_ADD_STRING
223.Fa "struct sysctl_ctx_list *ctx"
224.Fa "struct sysctl_oid_list *parent"
225.Fa "int number"
226.Fa "const char *name"
227.Fa "int ctlflags"
228.Fa "char *ptr"
229.Fa "intptr_t len"
230.Fa "const char *descr"
231.Fc
232.Ft struct sysctl_oid *
233.Fo SYSCTL_ADD_CONST_STRING
234.Fa "struct sysctl_ctx_list *ctx"
235.Fa "struct sysctl_oid_list *parent"
236.Fa "int number"
237.Fa "const char *name"
238.Fa "int ctlflags"
239.Fa "const char *ptr"
240.Fa "const char *descr"
241.Fc
242.Ft struct sysctl_oid *
243.Fo SYSCTL_ADD_STRUCT
244.Fa "struct sysctl_ctx_list *ctx"
245.Fa "struct sysctl_oid_list *parent"
246.Fa "int number"
247.Fa "const char *name"
248.Fa "int ctlflags"
249.Fa "void *ptr"
250.Fa struct_type
251.Fa "const char *descr"
252.Fc
253.Ft struct sysctl_oid *
254.Fo SYSCTL_ADD_TIMEVAL_SEC
255.Fa "struct sysctl_ctx_list *ctx"
256.Fa "struct sysctl_oid_list *parent"
257.Fa "int number"
258.Fa "const char *name"
259.Fa "int ctlflags"
260.Fa "struct timeval *ptr"
261.Fa "const char *descr"
262.Fc
263.Ft struct sysctl_oid *
264.Fo SYSCTL_ADD_U8
265.Fa "struct sysctl_ctx_list *ctx"
266.Fa "struct sysctl_oid_list *parent"
267.Fa "int number"
268.Fa "const char *name"
269.Fa "int ctlflags"
270.Fa "uint8_t *ptr"
271.Fa "uint8_t val"
272.Fa "const char *descr"
273.Fc
274.Ft struct sysctl_oid *
275.Fo SYSCTL_ADD_U16
276.Fa "struct sysctl_ctx_list *ctx"
277.Fa "struct sysctl_oid_list *parent"
278.Fa "int number"
279.Fa "const char *name"
280.Fa "int ctlflags"
281.Fa "uint16_t *ptr"
282.Fa "uint16_t val"
283.Fa "const char *descr"
284.Fc
285.Ft struct sysctl_oid *
286.Fo SYSCTL_ADD_U32
287.Fa "struct sysctl_ctx_list *ctx"
288.Fa "struct sysctl_oid_list *parent"
289.Fa "int number"
290.Fa "const char *name"
291.Fa "int ctlflags"
292.Fa "uint32_t *ptr"
293.Fa "uint32_t val"
294.Fa "const char *descr"
295.Fc
296.Ft struct sysctl_oid *
297.Fo SYSCTL_ADD_U64
298.Fa "struct sysctl_ctx_list *ctx"
299.Fa "struct sysctl_oid_list *parent"
300.Fa "int number"
301.Fa "const char *name"
302.Fa "int ctlflags"
303.Fa "uint64_t *ptr"
304.Fa "uint64_t val"
305.Fa "const char *descr"
306.Fc
307.Ft struct sysctl_oid *
308.Fo SYSCTL_ADD_UINT
309.Fa "struct sysctl_ctx_list *ctx"
310.Fa "struct sysctl_oid_list *parent"
311.Fa "int number"
312.Fa "const char *name"
313.Fa "int ctlflags"
314.Fa "unsigned int *ptr"
315.Fa "unsigned int val"
316.Fa "const char *descr"
317.Fc
318.Ft struct sysctl_oid *
319.Fo SYSCTL_ADD_ULONG
320.Fa "struct sysctl_ctx_list *ctx"
321.Fa "struct sysctl_oid_list *parent"
322.Fa "int number"
323.Fa "const char *name"
324.Fa "int ctlflags"
325.Fa "unsigned long *ptr"
326.Fa "const char *descr"
327.Fc
328.Ft struct sysctl_oid *
329.Fo SYSCTL_ADD_UQUAD
330.Fa "struct sysctl_ctx_list *ctx"
331.Fa "struct sysctl_oid_list *parent"
332.Fa "int number"
333.Fa "const char *name"
334.Fa "int ctlflags"
335.Fa "uint64_t *ptr"
336.Fa "const char *descr"
337.Fc
338.Ft struct sysctl_oid *
339.Fo SYSCTL_ADD_UAUTO
340.Fa "struct sysctl_ctx_list *ctx"
341.Fa "struct sysctl_oid_list *parent"
342.Fa "int number"
343.Fa "const char *name"
344.Fa "int ctlflags"
345.Fa "void *ptr"
346.Fa "const char *descr"
347.Fc
348.Ft struct sysctl_oid_list *
349.Fo SYSCTL_CHILDREN
350.Fa "struct sysctl_oid *oidp"
351.Fc
352.Ft struct sysctl_oid_list *
353.Fo SYSCTL_STATIC_CHILDREN
354.Fa "struct sysctl_oid_list OID_NAME"
355.Fc
356.Ft struct sysctl_oid_list *
357.Fo SYSCTL_NODE_CHILDREN
358.Fa "parent"
359.Fa "name"
360.Fc
361.Ft struct sysctl_oid *
362.Fo SYSCTL_PARENT
363.Fa "struct sysctl_oid *oid"
364.Fc
365.Fn SYSCTL_INT parent number name ctlflags ptr val descr
366.Fn SYSCTL_INT_WITH_LABEL parent number name ctlflags ptr val descr label
367.Fn SYSCTL_LONG parent number name ctlflags ptr val descr
368.Ft int
369.Fn sysctl_msec_to_ticks SYSCTL_HANDLER_ARGS
370.Fn SYSCTL_NODE parent number name ctlflags handler descr
371.Fn SYSCTL_NODE_WITH_LABEL parent number name ctlflags handler descr label
372.Fn SYSCTL_OPAQUE parent number name ctlflags ptr len format descr
373.Fn SYSCTL_PROC parent number name ctlflags arg1 arg2 handler format descr
374.Fn SYSCTL_QUAD parent number name ctlflags ptr val descr
375.Fn SYSCTL_ROOT_NODE number name ctlflags handler descr
376.Fn SYSCTL_S8 parent number name ctlflags ptr val descr
377.Fn SYSCTL_S16 parent number name ctlflags ptr val descr
378.Fn SYSCTL_S32 parent number name ctlflags ptr val descr
379.Fn SYSCTL_S64 parent number name ctlflags ptr val descr
380.Fn SYSCTL_STRING parent number name ctlflags arg len descr
381.Fn SYSCTL_CONST_STRING parent number name ctlflags arg descr
382.Fn SYSCTL_STRUCT parent number name ctlflags ptr struct_type descr
383.Fn SYSCTL_TIMEVAL_SEC parent number name ctlflags ptr descr
384.Fn SYSCTL_U8 parent number name ctlflags ptr val descr
385.Fn SYSCTL_U16 parent number name ctlflags ptr val descr
386.Fn SYSCTL_U32 parent number name ctlflags ptr val descr
387.Fn SYSCTL_U64 parent number name ctlflags ptr val descr
388.Fn SYSCTL_UINT parent number name ctlflags ptr val descr
389.Fn SYSCTL_ULONG parent number name ctlflags ptr val descr
390.Fn SYSCTL_UQUAD parent number name ctlflags ptr val descr
391
392.Sh DESCRIPTION
393The
394.Nm SYSCTL
395kernel interface allows dynamic or static creation of
396.Xr sysctl 8
397MIB entries.
398All static sysctls are automatically destroyed when the module which
399they are part of is unloaded.
400Most top level categories are created statically and are available to
401all kernel code and its modules.
402.Sh DESCRIPTION OF ARGUMENTS
403.Bl -tag -width ctlflags
404.It Fa ctx
405Pointer to sysctl context or NULL, if no context.
406See
407.Xr sysctl_ctx_init 9
408for how to create a new sysctl context.
409Programmers are strongly advised to use contexts to organize the
410dynamic OIDs which they create because when a context is destroyed all
411belonging sysctls are destroyed as well.
412This makes the sysctl cleanup code much simpler.
413Else deletion of all created OIDs is required at module unload.
414.It Fa parent
415A pointer to a
416.Li struct sysctl_oid_list ,
417which is the head of the parent's list of children.
418This pointer is retrieved using the
419.Fn SYSCTL_STATIC_CHILDREN
420macro for static sysctls and the
421.Fn SYSCTL_CHILDREN
422macro for dynamic sysctls.
423The
424.Fn SYSCTL_PARENT
425macro can be used to get the parent of an OID.
426The macro returns NULL if there is no parent.
427.It Fa number
428The OID number that will be assigned to this OID.
429In almost all cases this should be set to
430.Dv OID_AUTO ,
431which will result in the assignment of the next available OID number.
432.It Fa name
433The name of the OID.
434The newly created OID will contain a copy of the name.
435.It Fa ctlflags
436A bit mask of sysctl control flags.
437See the section below describing all the control flags.
438.It Fa arg1
439First callback argument for procedure sysctls.
440.It Fa arg2
441Second callback argument for procedure sysctls.
442.It Fa len
443The length of the data pointed to by the
444.Fa ptr
445argument.
446For string type OIDs a length of zero means that
447.Xr strlen 3
448will be used to get the length of the string at each access to the OID.
449.It Fa ptr
450Pointer to sysctl variable or string data.
451For sysctl values the pointer can be SYSCTL_NULL_XXX_PTR which means the OID is read-only and the returned value should be taken from the
452.Fa val
453argument.
454.It Fa val
455If the
456.Fa ptr
457argument is SYSCTL_NULL_XXX_PTR, gives the constant value returned by this OID.
458Else this argument is not used.
459.It Fa struct_type
460Name of structure type.
461.It Fa handler
462A pointer to the function
463that is responsible for handling read and write requests
464to this OID.
465There are several standard handlers
466that support operations on nodes,
467integers, strings and opaque objects.
468It is possible to define custom handlers using the
469.Fn SYSCTL_PROC
470macro or the
471.Fn SYSCTL_ADD_PROC
472function.
473.It Fa format
474A pointer to a string
475which specifies the format of the OID in a symbolic way.
476This format is used as a hint by
477.Xr sysctl 8
478to apply proper data formatting for display purposes.
479.Pp
480Current formats:
481.Bl -tag -width "S,TYPE" -compact -offset indent
482.It Cm N
483node
484.It Cm A
485.Li "char *"
486.It Cm I
487.Li "int"
488.It Cm IK Ns Op Ar n
489temperature in Kelvin, multiplied by an optional single digit
490power of ten scaling factor: 1 (default) gives deciKelvin, 0 gives Kelvin, 3
491gives milliKelvin
492.It Cm IU
493.Li "unsigned int"
494.It Cm L
495.Li "long"
496.It Cm LU
497.Li "unsigned long"
498.It Cm Q
499.Li "quad_t"
500.It Cm QU
501.Li "u_quad_t"
502.It Cm "S,TYPE"
503.Li "struct TYPE"
504structures
505.El
506.It Fa descr
507A pointer to a textual description of the OID.
508.It Fa label
509A pointer to an aggregation label for this component of the OID.
510To make it easier to export sysctl data to monitoring systems that
511support aggregations through labels (e.g., Prometheus),
512this argument can be used to attach a label name to an OID.
513The label acts as a hint that this component's name should not be part
514of the metric's name,
515but attached to the metric as a label instead.
516.Pp
517Labels should only be applied to siblings that are structurally similar
518and encode the same type of value,
519as aggregation is of no use otherwise.
520.El
521.Sh NODE VALUE TYPES
522Most of the macros and functions used to create sysctl nodes export a
523read-only constant or in-kernel variable whose type matches the type
524of the node's value.
525For example,
526.Fn SYSCTL_INT
527reports the raw value of an associated variable of type
528.Vt int .
529However, nodes may also export a value that is a translatation of an internal
530representation.
531.Pp
532The
533.Fn sysctl_msec_to_ticks
534handler can be used with
535.Fn SYSCTL_PROC
536or
537.Fn SYSCTL_ADD_PROC
538to export a millisecond time interval.
539When using this handler,
540the
541.Fa arg2
542parameter points to an in-kernel variable of type
543.Vt int
544which stores a tick count suitable for use with functions like
545.Xr tsleep 9 .
546The
547.Fn sysctl_msec_to_ticks
548function converts this value to milliseconds when reporting the node's value.
549Similarly,
550.Fn sysctl_msec_to_ticks
551accepts new values in milliseconds and stores an equivalent value in ticks to
552.Fa *arg2 .
553.Pp
554The
555.Fn SYSCTL_ADD_TIMEVAL_SEC
556function and
557.Fn SYSCTL_TIMEVAL_SEC
558macro create nodes which export an in-kernel variable of type
559.Vt struct timeval .
560These nodes do not export full value of the associated structure.
561Instead, they export a count in seconds as a simple integer which is
562stored in the
563.Fa tv_sec
564field of the associated variable.
565This function and macro are intended to be used with variables which
566store a non-negative interval rather than an absolute time.
567As a result, they reject attempts to store negative values.
568.Sh CREATING ROOT NODES
569Sysctl MIBs or OIDs are created in a hierarchical tree.
570The nodes at the bottom of the tree are called root nodes, and have no
571parent OID.
572To create bottom tree nodes the
573.Fn SYSCTL_ROOT_NODE
574macro or the
575.Fn SYSCTL_ADD_ROOT_NODE
576function needs to be used.
577By default all static sysctl node OIDs are global and need a
578.Fn SYSCTL_DECL
579statement prior to their
580.Fn SYSCTL_NODE
581definition statement, typically in a so-called header file.
582.Sh CREATING SYSCTL STRINGS
583Zero terminated character strings sysctls are created either using the
584.Fn SYSCTL_STRING
585macro or the
586.Fn SYSCTL_ADD_STRING
587function.
588If the
589.Fa len
590argument in zero, the string length is computed at every access to the OID using
591.Xr strlen 3 .
592Use the
593.Fn SYSCTL_CONST_STRING
594macro or the
595.Fn SYSCTL_ADD_CONST_STRING
596function to add a sysctl for a constant string.
597.Sh CREATING OPAQUE SYSCTLS
598The
599.Fn SYSCTL_OPAQUE
600or
601.Fn SYSCTL_STRUCT
602macros or the
603.Fn SYSCTL_ADD_OPAQUE
604or
605.Fn SYSCTL_ADD_STRUCT
606functions create an OID that handle any chunk of data
607of the size specified by the
608.Fa len
609argument and data pointed to by the
610.Fa ptr
611argument.
612When using the structure version the type is encoded as part of the
613created sysctl.
614.Sh CREATING CUSTOM SYSCTLS
615The
616.Fn SYSCTL_PROC
617macro and the
618.Fn SYSCTL_ADD_PROC
619function
620create OIDs with the specified
621.Pa handler
622function.
623The handler is responsible for handling all read and write requests to
624the OID.
625This OID type is especially useful if the kernel data is not easily
626accessible, or needs to be processed before exporting.
627.Sh CREATING A STATIC SYSCTL
628Static sysctls are declared using one of the
629.Fn SYSCTL_INT ,
630.Fn SYSCTL_INT_WITH_LABEL ,
631.Fn SYSCTL_LONG ,
632.Fn SYSCTL_NODE ,
633.Fn SYSCTL_NODE_WITH_LABEL ,
634.Fn SYSCTL_OPAQUE ,
635.Fn SYSCTL_PROC ,
636.Fn SYSCTL_QUAD ,
637.Fn SYSCTL_ROOT_NODE ,
638.Fn SYSCTL_S8 ,
639.Fn SYSCTL_S16 ,
640.Fn SYSCTL_S32 ,
641.Fn SYSCTL_S64 ,
642.Fn SYSCTL_STRING ,
643.Fn SYSCTL_CONST_STRING ,
644.Fn SYSCTL_STRUCT ,
645.Fn SYSCTL_TIMEVAL_SEC ,
646.Fn SYSCTL_U8 ,
647.Fn SYSCTL_U16 ,
648.Fn SYSCTL_U32 ,
649.Fn SYSCTL_U64 ,
650.Fn SYSCTL_UINT ,
651.Fn SYSCTL_ULONG
652or
653.Fn SYSCTL_UQUAD
654macros.
655.Sh CREATING A DYNAMIC SYSCTL
656Dynamic nodes are created using one of the
657.Fn SYSCTL_ADD_INT ,
658.Fn SYSCTL_ADD_LONG ,
659.Fn SYSCTL_ADD_NODE ,
660.Fn SYSCTL_ADD_NODE_WITH_LABEL ,
661.Fn SYSCTL_ADD_OPAQUE ,
662.Fn SYSCTL_ADD_PROC ,
663.Fn SYSCTL_ADD_QUAD ,
664.Fn SYSCTL_ADD_ROOT_NODE ,
665.Fn SYSCTL_ADD_S8 ,
666.Fn SYSCTL_ADD_S16 ,
667.Fn SYSCTL_ADD_S32 ,
668.Fn SYSCTL_ADD_S64 ,
669.Fn SYSCTL_ADD_STRING ,
670.Fn SYSCTL_ADD_CONST_STRING ,
671.Fn SYSCTL_ADD_STRUCT ,
672.Fn SYSCTL_ADD_TIMEVAL_SEC ,
673.Fn SYSCTL_ADD_U8 ,
674.Fn SYSCTL_ADD_U16 ,
675.Fn SYSCTL_ADD_U32 ,
676.Fn SYSCTL_ADD_U64 ,
677.Fn SYSCTL_ADD_UAUTO ,
678.Fn SYSCTL_ADD_UINT ,
679.Fn SYSCTL_ADD_ULONG ,
680or
681.Fn SYSCTL_UQUAD
682functions.
683See
684.Xr sysctl_remove_oid 9
685or
686.Xr sysctl_ctx_free 9
687for more information on how to destroy a dynamically created OID.
688.Sh CONTROL FLAGS
689For most of the above functions and macros, declaring a type as part
690of the access flags is not necessary \[em] however, when declaring a
691sysctl implemented by a function, including a type in the access mask
692is required:
693.Bl -tag -width ".Dv CTLTYPE_NOFETCH"
694.It Dv CTLTYPE_NODE
695This is a node intended to be a parent for other nodes.
696.It Dv CTLTYPE_INT
697This is a signed integer.
698.It Dv CTLTYPE_STRING
699This is a nul-terminated string stored in a character array.
700.It Dv CTLTYPE_S8
701This is an 8-bit signed integer.
702.It Dv CTLTYPE_S16
703This is a 16-bit signed integer.
704.It Dv CTLTYPE_S32
705This is a 32-bit signed integer.
706.It Dv CTLTYPE_S64
707This is a 64-bit signed integer.
708.It Dv CTLTYPE_OPAQUE
709This is an opaque data structure.
710.It Dv CTLTYPE_STRUCT
711Alias for
712.Dv CTLTYPE_OPAQUE .
713.It Dv CTLTYPE_U8
714This is an 8-bit unsigned integer.
715.It Dv CTLTYPE_U16
716This is a 16-bit unsigned integer.
717.It Dv CTLTYPE_U32
718This is a 32-bit unsigned integer.
719.It Dv CTLTYPE_U64
720This is a 64-bit unsigned integer.
721.It Dv CTLTYPE_UINT
722This is an unsigned integer.
723.It Dv CTLTYPE_LONG
724This is a signed long.
725.It Dv CTLTYPE_ULONG
726This is an unsigned long.
727.El
728.Pp
729All sysctl types except for new node declarations require one of the following
730flags to be set indicating the read and write disposition of the sysctl:
731.Bl -tag -width ".Dv CTLFLAG_ANYBODY"
732.It Dv CTLFLAG_RD
733This is a read-only sysctl.
734.It Dv CTLFLAG_RDTUN
735This is a read-only sysctl and tunable which is tried fetched once
736from the system environment early during module load or system boot.
737.It Dv CTLFLAG_WR
738This is a writable sysctl.
739.It Dv CTLFLAG_RW
740This sysctl is readable and writable.
741.It Dv CTLFLAG_RWTUN
742This is a readable and writeable sysctl and tunable which is tried
743fetched once from the system environment early during module load or
744system boot.
745.It Dv CTLFLAG_NOFETCH
746In case the node is marked as a tunable using the CTLFLAG_[XX]TUN,
747this flag will prevent fetching the initial value from the system
748environment.
749Typically this flag should only be used for very early
750low level system setup code, and not by common drivers and modules.
751.It Dv CTLFLAG_MPSAFE
752This
753.Xr sysctl 9
754handler is MP safe.
755Do not grab Giant around calls to this handler.
756This should only be used for
757.Fn SYSCTL_PROC
758entries.
759.El
760.Pp
761Additionally, any of the following optional flags may also be specified:
762.Bl -tag -width ".Dv CTLFLAG_ANYBODY"
763.It Dv CTLFLAG_ANYBODY
764Any user or process can write to this sysctl.
765.It Dv CTLFLAG_CAPRD
766A process in capability mode can read from this sysctl.
767.It Dv CTLFLAG_CAPWR
768A process in capability mode can write to this sysctl.
769.It Dv CTLFLAG_SECURE
770This sysctl can be written to only if the effective securelevel of the
771process is \[<=] 0.
772.It Dv CTLFLAG_PRISON
773This sysctl can be written to by processes in
774.Xr jail 2 .
775.It Dv CTLFLAG_SKIP
776When iterating the sysctl name space, do not list this sysctl.
777.It Dv CTLFLAG_TUN
778Advisory flag that a system tunable also exists for this variable.
779The initial sysctl value is tried fetched once from the system
780environment early during module load or system boot.
781.It Dv CTLFLAG_DYN
782Dynamically created OIDs automatically get this flag set.
783.It Dv CTLFLAG_VNET
784OID references a VIMAGE-enabled variable.
785.El
786.Sh EXAMPLES
787Sample use of
788.Fn SYSCTL_DECL
789to declare the
790.Va security
791sysctl tree for use by new nodes:
792.Bd -literal -offset indent
793SYSCTL_DECL(_security);
794.Ed
795.Pp
796Examples of integer, opaque, string, and procedure sysctls follow:
797.Bd -literal -offset indent
798/*
799 * Example of a constant integer value.  Notice that the control
800 * flags are CTLFLAG_RD, the variable pointer is SYSCTL_NULL_INT_PTR,
801 * and the value is declared.
802 */
803SYSCTL_INT(_debug_sizeof, OID_AUTO, bio, CTLFLAG_RD, SYSCTL_NULL_INT_PTR,
804    sizeof(struct bio), "sizeof(struct bio)");
805
806/*
807 * Example of a variable integer value.  Notice that the control
808 * flags are CTLFLAG_RW, the variable pointer is set, and the
809 * value is 0.
810 */
811static int	doingcache = 1;		/* 1 => enable the cache */
812SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0,
813    "Enable name cache");
814
815/*
816 * Example of a variable string value.  Notice that the control
817 * flags are CTLFLAG_RW, that the variable pointer and string
818 * size are set.  Unlike newer sysctls, this older sysctl uses a
819 * static oid number.
820 */
821char kernelname[MAXPATHLEN] = "/kernel";	/* XXX bloat */
822SYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, CTLFLAG_RW,
823    kernelname, sizeof(kernelname), "Name of kernel file booted");
824
825/*
826 * Example of an opaque data type exported by sysctl.  Notice that
827 * the variable pointer and size are provided, as well as a format
828 * string for sysctl(8).
829 */
830static l_fp pps_freq;	/* scaled frequency offset (ns/s) */
831SYSCTL_OPAQUE(_kern_ntp_pll, OID_AUTO, pps_freq, CTLFLAG_RD,
832    &pps_freq, sizeof(pps_freq), "I", "");
833
834/*
835 * Example of a procedure based sysctl exporting string
836 * information.  Notice that the data type is declared, the NULL
837 * variable pointer and 0 size, the function pointer, and the
838 * format string for sysctl(8).
839 */
840SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING |
841    CTLFLAG_RW, NULL, 0, sysctl_kern_timecounter_hardware, "A",
842    "");
843.Ed
844.Pp
845The following is an example of
846how to create a new top-level category
847and how to hook up another subtree to an existing static node.
848This example does not use contexts,
849which results in tedious management of all intermediate oids,
850as they need to be freed later on:
851.Bd -literal -offset indent
852#include <sys/sysctl.h>
853 ...
854/*
855 * Need to preserve pointers to newly created subtrees,
856 * to be able to free them later:
857 */
858static struct sysctl_oid *root1;
859static struct sysctl_oid *root2;
860static struct sysctl_oid *oidp;
861static int a_int;
862static char *string = "dynamic sysctl";
863 ...
864
865root1 = SYSCTL_ADD_ROOT_NODE(NULL,
866	OID_AUTO, "newtree", CTLFLAG_RW, 0, "new top level tree");
867oidp = SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(root1),
868	OID_AUTO, "newint", CTLFLAG_RW, &a_int, 0, "new int leaf");
869 ...
870root2 = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_debug),
871	OID_AUTO, "newtree", CTLFLAG_RW, 0, "new tree under debug");
872oidp = SYSCTL_ADD_STRING(NULL, SYSCTL_CHILDREN(root2),
873	OID_AUTO, "newstring", CTLFLAG_RD, string, 0, "new string leaf");
874.Ed
875.Pp
876This example creates the following subtrees:
877.Bd -literal -offset indent
878debug.newtree.newstring
879newtree.newint
880.Ed
881.Pp
882.Em "Care should be taken to free all OIDs once they are no longer needed!"
883.Sh SYSCTL NAMING
884When adding, modifying, or removing sysctl names, it is important to be
885aware that these interfaces may be used by users, libraries, applications,
886or documentation (such as published books), and are implicitly published application interfaces.
887As with other application interfaces, caution must be taken not to break
888existing applications, and to think about future use of new name spaces so as
889to avoid the need to rename or remove interfaces that might be depended on in
890the future.
891.Pp
892The semantics chosen for a new sysctl should be as clear as possible,
893and the name of the sysctl must closely reflect its semantics.
894Therefore the sysctl name deserves a fair amount of consideration.
895It should be short but yet representative of the sysctl meaning.
896If the name consists of several words, they should be separated by
897underscore characters, as in
898.Va compute_summary_at_mount .
899Underscore characters may be omitted only if the name consists of not more
900than two words, each being not longer than four characters, as in
901.Va bootfile .
902For boolean sysctls, negative logic should be totally avoided.
903That is, do not use names like
904.Va no_foobar
905or
906.Va foobar_disable .
907They are confusing and lead to configuration errors.
908Use positive logic instead:
909.Va foobar ,
910.Va foobar_enable .
911.Pp
912A temporary sysctl node OID that should not be relied upon must be designated
913as such by a leading underscore character in its name.
914For example:
915.Va _dirty_hack .
916.Sh SEE ALSO
917.Xr sysctl 3 ,
918.Xr sysctl 8 ,
919.Xr sysctl_add_oid 9 ,
920.Xr sysctl_ctx_free 9 ,
921.Xr sysctl_ctx_init 9 ,
922.Xr sysctl_remove_oid 9
923.Sh HISTORY
924The
925.Xr sysctl 8
926utility first appeared in
927.Bx 4.4 .
928.Sh AUTHORS
929.An -nosplit
930The
931.Nm sysctl
932implementation originally found in
933.Bx
934has been extensively rewritten by
935.An Poul-Henning Kamp
936in order to add support for name lookups, name space iteration, and dynamic
937addition of MIB nodes.
938.Pp
939This man page was written by
940.An Robert N. M. Watson .
941.Sh SECURITY CONSIDERATIONS
942When creating new sysctls, careful attention should be paid to the security
943implications of the monitoring or management interface being created.
944Most sysctls present in the kernel are read-only or writable only by the
945superuser.
946Sysctls exporting extensive information on system data structures and
947operation, especially those implemented using procedures, will wish to
948implement access control to limit the undesired exposure of information about
949other processes, network connections, etc.
950.Pp
951The following top level sysctl name spaces are commonly used:
952.Bl -tag -width ".Va regression"
953.It Va compat
954Compatibility layer information.
955.It Va debug
956Debugging information.
957Various name spaces exist under
958.Va debug .
959.It Va hw
960Hardware and device driver information.
961.It Va kern
962Kernel behavior tuning; generally deprecated in favor of more specific
963name spaces.
964.It Va machdep
965Machine-dependent configuration parameters.
966.It Va net
967Network subsystem.
968Various protocols have name spaces under
969.Va net .
970.It Va regression
971Regression test configuration and information.
972.It Va security
973Security and security-policy configuration and information.
974.It Va sysctl
975Reserved name space for the implementation of sysctl.
976.It Va user
977Configuration settings relating to user application behavior.
978Generally, configuring applications using kernel sysctls is discouraged.
979.It Va vfs
980Virtual file system configuration and information.
981.It Va vm
982Virtual memory subsystem configuration and information.
983.El
984