xref: /original-bsd/lib/libc/gen/sysctl.3 (revision d70e0f4e)
1.\" Copyright (c) 1993 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.roff%
5.\"
6.\"	@(#)sysctl.3	6.9 (Berkeley) 05/24/93
7.\"
8.Dd ""
9.Dt SYSCTL 3
10.Os
11.Sh NAME
12.Nm sysctl
13.Nd get or set system information
14.Sh SYNOPSIS
15.Fd #include <sys/sysctl.h>
16.Ft int
17.Fn sysctl "int *name" "u_int namelen" "void *oldp" "size_t *oldlenp" "void *newp" "size_t newlen"
18.Sh DESCRIPTION
19The
20.Nm sysctl
21function retrieves system information and allows processes with
22appropriate privileges to set system information.
23The information available from
24.Nm sysctl
25consists of integers, strings, and tables.
26Information may be retrieved and set from the command interface
27using the
28.Xr sysctl 1
29utility.
30.Pp
31Unless explicitly noted below,
32.Nm sysctl
33returns a consistent snapshot of the data requested.
34Consistency is obtained by locking the destination
35buffer into memory so that the data may be copied out without blocking.
36Calls to
37.Nm sysctl
38are serialized to avoid deadlock.
39.Pp
40The state is described using a ``Management Information Base'' (MIB)
41style name, listed in
42.Fa name ,
43which is a
44.Fa namelen
45length array of integers.
46.Pp
47The information is copied into the buffer specified by
48.Fa oldp .
49The size of the buffer is given by the location specified by
50.Fa oldlenp
51before the call,
52and that location gives the amount of data copied after a successful call.
53If the amount of data available is greater
54than the size of the buffer supplied,
55the call supplies as much data as fits in the buffer provided
56and returns with the error code ENOMEM.
57If the old value is not desired,
58.Fa oldp
59and
60.Fa oldlenp
61should be set to NULL.
62.Pp
63The size of the available data can be determined by calling
64.Nm sysctl
65with a NULL parameter for
66.Fa oldp .
67The size of the available data will be returned in the location pointed to by
68.Fa oldlenp .
69For some operations, the amount of space may change often.
70For these operations,
71the system attempts to round up so that the returned size is
72large enough for a call to return the data shortly thereafter.
73.Pp
74To set a new value,
75.Fa newp
76is set to point to a buffer of length
77.Fa newlen
78from which the requested value is to be taken.
79If a new value is not to be set,
80.Fa newp
81should be set to NULL and
82.Fa newlen
83set to 0.
84.Pp
85The top level names are defined with a CTL_ prefix in
86.Pa <sys/sysctl.h> ,
87and are as follows.
88The next and subsequent levels down are found in the include files
89listed here, and described in separate sections below.
90.Pp
91.Bl -column CTLXMACHDEPXXX "Next level namesXXXXXX" -offset indent
92.It Sy Pa Name	Next level names	Description
93.It CTL\_DEBUG	sys/sysctl.h	Debugging
94.It CTL\_FS	sys/sysctl.h	File system
95.It CTL\_HW	sys/sysctl.h	Generic CPU, I/O
96.It CTL\_KERN	sys/sysctl.h	High kernel: processes, limits
97.It CTL\_MACHDEP	sys/sysctl.h	Machine dependent
98.It CTL\_NET	sys/socket.h	Networking
99.It CTL\_USER	sys/sysctl.h	User-level
100.It CTL\_VM	vm/vm_param.h	Virtual memory
101.El
102.Pp
103For example, the following retrieves the maximum number of processes allowed
104in the system:
105.Bd -literal -offset indent -compact
106int mib[2], maxproc;
107size_t len;
108.sp
109mib[0] = CTL_KERN;
110mib[1] = KERN_MAXPROC;
111len = sizeof(maxproc);
112sysctl(mib, 2, &maxproc, &len, NULL, 0);
113.Ed
114.sp
115To retrieve the standard search path for the system utilities:
116.Bd -literal -offset indent -compact
117int mib[2];
118size_t len;
119char *p;
120.sp
121mib[0] = CTL_USER;
122mib[1] = USER_CS_PATH;
123sysctl(mib, 2, NULL, &len, NULL, 0);
124p = malloc(len);
125sysctl(mib, 2, p, &len, NULL, 0);
126.Ed
127.Sh CTL_DEBUG
128The debugging variables vary from system to system.
129A debugging variable may be added or deleted without need to recompile
130.Nm sysctl
131to know about it.
132Each time it runs,
133.Nm sysctl
134gets the list of debugging variables from the kernel and
135displays their current values.
136The system defines twenty
137.Ns ( Va struct ctldebug )
138variables named
139.Nm debug0
140through
141.Nm debug19 .
142They are declared as separate variables so that they can be
143individually initialized at the location of their associated variable.
144The loader prevents multiple use of the same variable by issuing errors
145if a variable is initialized in more than one place.
146For example, to export the variable
147.Nm dospecialcheck
148as a debugging variable, the following declaration would be used:
149.Bd -literal -offset indent -compact
150int dospecialcheck = 1;
151struct ctldebug debug5 = { "dospecialcheck", &dospecialcheck };
152.Ed
153.Sh CTL_FS
154There are currently no second level names for the file system.
155.Sh CTL_HW
156The string and integer information available for the CTL_HW level
157is detailed below.
158The changeable column shows whether a process with appropriate
159privilege may change the value.
160.Bl -column "Second level nameXXXXXX" integerXXX -offset indent
161.It Sy Pa Second level name	Type	Changeable
162.It HW\_MACHINE	string	no
163.It HW\_MODEL	string	no
164.It HW\_NCPU	integer	no
165.It HW\_BYTEORDER	integer	no
166.It HW\_PHYSMEM	integer	no
167.It HW\_USERMEM	integer	no
168.It HW\_PAGESIZE	integer	no
169.\".It HW\_DISKNAMES	integer	no
170.\".It HW\_DISKSTATS	integer	no
171.El
172.Pp
173.Bl -tag -width "123456"
174.It Li HW_MACHINE
175The machine class.
176.It Li HW_MODEL
177The machine model
178.It Li HW_NCPU
179The number of cpus.
180.It Li HW_BYTEORDER
181The byteorder (4,321, or 1,234).
182.It Li HW_PHYSMEM
183The bytes of physical memory.
184.It Li HW_USERMEM
185The bytes of non-kernel memory.
186.It Li HW_PAGESIZE
187The software page size.
188.\".It Fa HW_DISKNAMES
189.\".It Fa HW_DISKSTATS
190.El
191.Sh CTL_KERN
192The string and integer information available for the CTL_KERN level
193is detailed below.
194The changeable column shows whether a process with appropriate
195privilege may change the value.
196The types of data currently available are process information,
197system vnodes, the open file entries, routing table entries,
198virtual memory statistics, load average history, and clock rate
199information.
200.Bl -column "KERNXCHOWNXRESTRICTEDXXX" "struct clockrateXXX" -offset indent
201.It Sy Pa Second level name	Type	Changeable
202.It KERN\_ARGMAX	integer	no
203.It KERN\_BOOTTIME	struct timeval	no
204.It KERN\_CHOWN\_RESTRICTED	integer	no
205.It KERN\_CLOCKRATE	struct clockinfo	no
206.It KERN\_FILE	struct file	no
207.It KERN\_HOSTID	integer	yes
208.It KERN\_HOSTNAME	string	yes
209.It KERN\_JOB\_CONTROL	integer	no
210.It KERN\_LINK\_MAX	integer	no
211.It KERN\_MAXFILES	integer	yes
212.It KERN\_MAXPROC	integer	yes
213.It KERN\_MAXUFILES	integer	yes
214.It KERN\_MAXUPROC	integer	yes
215.It KERN\_MAXVNODES	integer	yes
216.It KERN\_MAX\_CANON	integer	no
217.It KERN\_MAX\_INPUT	integer	no
218.It KERN\_NAME\_MAX	integer	no
219.It KERN\_NGROUPS	integer	no
220.It KERN\_NO\_TRUNC	integer	no
221.It KERN\_OSRELEASE	string	no
222.It KERN\_OSREV	integer	no
223.It KERN\_OSTYPE	string	no
224.It KERN\_PATH\_MAX	integer	no
225.It KERN\_PIPE\_BUF	integer	no
226.It KERN\_POSIX1	integer	no
227.It KERN\_PROC	struct proc	no
228.It KERN\_PROF	node	not applicable
229.It KERN\_SAVED\_IDS	integer	no
230.It KERN\_SECURELVL	integer	raise only
231.It KERN\_VDISABLE	integer	no
232.It KERN\_VERSION	string	no
233.It KERN\_VNODE	struct vnode	no
234.El
235.Pp
236.Bl -tag -width "123456"
237.It Li KERN_ARGMAX
238The maximum bytes of argument to
239.Xr exec 2 .
240.It Li KERN_BOOTTIME
241A
242.Va struct timeval
243structure is returned.
244This structure contains the time that the system was booted.
245.It Li KERN_CHOWN_RESTRICTED
246Return 1 if appropriate privileges are required for the
247.Xr chown 2
248system call, otherwise 0.
249.It Li KERN_CLOCKRATE
250A
251.Va struct clockinfo
252structure is returned.
253This structure contains the clock, statistics clock and profiling clock
254frequencies, and the number of micro-seconds per hz tick.
255.It Li KERN_FILE
256Return the entire file table.
257The returned data consists of a single
258.Va struct filehead
259followed by an array of
260.Va struct file ,
261whose size depends on the current number of such objects in the system.
262.It Li KERN_HOSTID
263Get or set the host id.
264.It Li KERN_HOSTNAME
265Get or set the hostname.
266.It Li KERN_JOB_CONTROL
267Return 1 if job control is available on this system, otherwise 0.
268.It Li KERN_LINK_MAX
269The maximum file link count.
270.It Li KERN_MAXFILES
271The maximum number of open files that may be open in the system.
272.It Li KERN_MAXPROC
273The maximum number of simultaneous processes the system will allow.
274.It Li KERN_MAXUFILES
275The maximum number of open files per user id.
276.It Li KERN_MAXUPROC
277The maximum number of simultaneous processes per user id.
278.It Li KERN_MAXVNODES
279The maximum number of vnodes available on the system.
280.It Li KERN_MAX_CANON
281The maximum number of bytes in terminal canonical input line.
282.It Li KERN_MAX_INPUT
283The minimum maximum number of bytes for which space is available in
284a terminal input queue.
285.It Li KERN_NAME_MAX
286The maximum number of bytes in a file name.
287.It Li KERN_NGROUPS
288The maximum number of supplemental groups.
289.It Li KERN_NO_TRUNC
290Return 1 if file names longer than KERN_NAME_MAX are truncated.
291.It Li KERN_OSRELEASE
292The system release string.
293.It Li KERN_OSREV
294The system revision string.
295.It Li KERN_OSTYPE
296The system type string.
297.It Li KERN_PATH_MAX
298The maximum number of bytes in a pathname.
299.It Li KERN_PIPE_BUF
300The maximum number of bytes which will be written atomically to a pipe.
301.It Li KERN_POSIX1
302The version of ISO/IEC 9945 (POSIX 1003.1) with which the system
303attempts to comply.
304.It Li KERN_PROC
305Return the entire process table, or a subset of it.
306An array of
307.Va struct kinfo_proc
308structures is returned,
309whose size depends on the current number of such objects in the system.
310The third and fourth level names are as follows:
311.Bl -column "Third level nameXXXXXX" "Fourth level is:XXXXXX" -offset indent
312.It Pa Third level name	Fourth level is:
313.It KERN\_PROC\_ALL	None
314.It KERN\_PROC\_PID	A process ID
315.It KERN\_PROC\_PGRP	A process group
316.It KERN\_PROC\_TTY	A tty device
317.It KERN\_PROC\_UID	A user ID
318.It KERN\_PROC\_RUID	A real user ID
319.El
320.It Li KERN_PROF
321Return profiling information about the kernel.
322If the kernel is not compiled for profiling,
323attempts to retrieve any of the KERN_PROF values will
324fail with EOPNOTSUPP.
325The third level names for the string and integer profiling information
326is detailed below.
327The changeable column shows whether a process with appropriate
328privilege may change the value.
329.Bl -column "GPROFXGMONPARAMXXX" "struct gmonparamXXX" -offset indent
330.It Sy Pa Third level name	Type	Changeable
331.It GPROF\_STATE	integer	yes
332.It GPROF\_COUNT	u_short[\|]	yes
333.It GPROF\_FROMS	u_short[\|]	yes
334.It GPROF\_TOS	struct tostruct	yes
335.It GPROF\_GMONPARAM	struct gmonparam	no
336.El
337.Pp
338The variables are as follows:
339.Bl -tag -width "123456"
340.It Li GPROF_STATE
341Returns GMON_PROF_ON or GMON_PROF_OFF to show that profiling
342is running or stopped.
343.It Li GPROF_COUNT
344Array of statistical program counter counts.
345.It Li GPROF_FROMS
346Array indexed by program counter of call-from points.
347.It Li GPROF_TOS
348Array of
349.Va struct tostruct
350describing destination of calls and their counts.
351.It Li GPROF_GMONPARAM
352Structure giving the sizes of the above arrays.
353.El
354.It Li KERN_SAVED_IDS
355Returns 1 if saved set-group and saved set-user ID is available.
356.It Li KERN_SECURELVL
357The system security level.
358This level may be raised by processes with appropriate privilege.
359It may only be lowered by process 1.
360.It Li KERN_VDISABLE
361Returns the terminal character disabling value.
362.It Li KERN_VERSION
363The system version string.
364.It Li KERN_VNODE
365Return the entire vnode table.
366Note, the vnode table is not necessarily a consistent snapshot of
367the system.
368The returned data consists of an array whose size depends on the
369current number of such objects in the system.
370Each element of the array contains the kernel address of a vnode
371.Va struct vnode *
372followed by the vnode itself
373.Va struct vnode .
374.El
375.Sh CTL_MACHDEP
376There are currently no second level names for the machine dependent
377interface.
378.Sh CTL_NET
379The string and integer information available for the CTL_NET level
380is detailed below.
381The changeable column shows whether a process with appropriate
382privilege may change the value.
383.Bl -column "Second level nameXXXXXX" "routing messagesXXX" -offset indent
384.It Sy Pa Second level name	Type	Changeable
385.It PF\_ROUTE	routing messages	no
386.It PF\_INET	internet values	yes
387.El
388.Pp
389.Bl -tag -width "123456"
390.It Li PF_ROUTE
391Return the entire routing table or a subset of it.
392The data is returned as a sequence of routing messages (see
393.Xr route 4
394for the header file, format and meaning).
395The length of each message is contained in the message header.
396.Pp
397The third level name is a protocol number, which is currently always 0.
398The fourth level name is an address family, which may be set to 0 to
399select all address families.
400The fifth and sixth level names are as follows:
401.Bl -column "Fifth level nameXXXXXX" "Sixth level is:XXX" -offset indent
402.It Pa Fifth level name	Sixth level is:
403.It NET\_RT\_FLAGS	rtflags
404.It NET\_RT\_DUMP	None
405.It NET\_RT\_IFLIST	None
406.El
407.It Li PF_INET
408Get or set various global information about the internet protocols.
409The third level name is the protocol.
410The fourth level name is the variable name.
411The currently defined protocols and names are:
412.Bl -column "Protocol nameXXXXXX" "Variable nameXXX" "integerXXX" -offset indent
413.It Pa Protocol name	Variable name	Type	Changeable
414.It ip	forwarding	integer	yes
415.It ip	redirect	integer	yes
416.It ip	ttl	integer	yes
417.It icmp	maskrepl	integer	yes
418.It udp	checksum	integer	yes
419.El
420.Pp
421The variables are as follows:
422.Bl -tag -width "123456"
423.It Li ip.forwarding
424Returns 1 when IP forwarding is enabled for the host,
425meaning that the host is acting as a router.
426.It Li ip.redirect
427Returns 1 when ICMP redirects may be sent by the host.
428This option is ignored unless the host is routing IP packets,
429and should normally be enabled on all systems.
430.It Li ip.ttl
431The maximum time-to-live (hop count) value for an IP packet sourced by
432the system.
433This value applies to normal transport protocols, not to ICMP.
434.It Li icmp.maskrepl
435Returns 1 if ICMP network mask requests are to be answered.
436.It Li udp.checksum
437Returns 1 when UDP checksums are being computed and checked.
438Disabling UDP checksums is strongly discouraged.
439.El
440.Sh CTL_USER
441The string and integer information available for the CTL_USER level
442is detailed below.
443The changeable column shows whether a process with appropriate
444privilege may change the value.
445.Bl -column "USER_COLL_WEIGHTS_MAXXXX" "integerXXX" -offset indent
446.It Sy Pa Second level name	Type	Changeable
447.It USER\_BC\_BASE\_MAX	integer	no
448.It USER\_BC\_DIM\_MAX	integer	no
449.It USER\_BC\_SCALE\_MAX	integer	no
450.It USER\_BC\_STRING\_MAX	integer	no
451.It USER\_COLL\_WEIGHTS\_MAX	integer	no
452.It USER\_CS\_PATH	string	no
453.It USER\_EXPR\_NEST\_MAX	integer	no
454.It USER\_LINE\_MAX	integer	no
455.It USER\_POSIX2\_CHAR\_TERM	integer	no
456.It USER\_POSIX2\_C\_BIND	integer	no
457.It USER\_POSIX2\_C\_DEV	integer	no
458.It USER\_POSIX2\_FORT\_DEV	integer	no
459.It USER\_POSIX2\_FORT\_RUN	integer	no
460.It USER\_POSIX2\_LOCALEDEF	integer	no
461.It USER\_POSIX2\_SW\_DEV	integer	no
462.It USER\_POSIX2\_UPE	integer	no
463.It USER\_POSIX2\_VERSION	integer	no
464.It USER\_RE\_DUP\_MAX	integer	no
465.It USER\_STREAM\_MAX	integer	no
466.It USER\_TZNAME\_MAX	integer	no
467.El
468.Bl -tag -width "123456"
469.Pp
470.It Li USER_BC_BASE_MAX
471The maximum ibase/obase values in the
472.Xr bc 1
473utility.
474.It Li USER_BC_DIM_MAX
475The maximum array size in the
476.Xr bc 1
477utility.
478.It Li USER_BC_SCALE_MAX
479The maximum scale value in the
480.Xr bc 1
481utility.
482.It Li USER_BC_STRING_MAX
483The maximum string length in the
484.Xr bc 1
485utility.
486.It Li USER_COLL_WEIGHTS_MAX
487The maximum number of weights that can be assigned to any entry of
488the LC_COLLATE order keyword in the locale definition file.
489.It Li USER_CS_PATH
490Return a value for the
491.Ev PATH
492environment variable that finds all the standard utilities.
493.It Li USER_EXPR_NEST_MAX
494The maximum number of expressions that can be nested within
495parenthesis by the
496.Xr expr 1
497utility.
498.It Li USER_LINE_MAX
499The maximum length in bytes of a text-processing utility's input
500line.
501.It Li USER_POSIX2_CHAR_TERM
502Return 1 if the system supports at least one terminal type capable of
503all operations described in POSIX 1003.2, otherwise 0.
504.It Li USER_POSIX2_C_BIND
505Return 1 if the system's C-language development facilities support the
506C-Language Bindings Option, otherwise 0.
507.It Li USER_POSIX2_C_DEV
508Return 1 if the system supports the C-Language Development Utilities Option,
509otherwise 0.
510.It Li USER_POSIX2_FORT_DEV
511Return 1 if the system supports the FORTRAN Development Utilities Option,
512otherwise 0.
513.It Li USER_POSIX2_FORT_RUN
514Return 1 if the system supports the FORTRAN Runtime Utilities Option,
515otherwise 0.
516.It Li USER_POSIX2_LOCALEDEF
517Return 1 if the system supports the creation of locales, otherwise 0.
518.It Li USER_POSIX2_SW_DEV
519Return 1 if the system supports the Software Development Utilities Option,
520otherwise 0.
521.It Li USER_POSIX2_UPE
522Return 1 if the system supports the User Portability Utilities Option,
523otherwise 0.
524.It Li USER_POSIX2_VERSION
525The version of POSIX 1003.2 with which the system attempts to comply.
526.It Li USER_RE_DUP_MAX
527The maximum number of repeated occurrences of a regular expression
528permitted when using interval notation.
529.It Li USER_STREAM_MAX
530The minimum maximum number of streams that a process may have open
531at any one time.
532.It Li USER_TZNAME_MAX
533The minimum maximum number of types supported for the name of a
534timezone.
535.El
536.Sh CTL_VM
537The string and integer information available for the CTL_VM level
538is detailed below.
539The changeable column shows whether a process with appropriate
540privilege may change the value.
541.Bl -column "Second level nameXXXXXX" "struct loadavgXXX" -offset indent
542.It Sy Pa Second level name	Type	Changeable
543.It VM\_LOADAVG	struct loadavg	no
544.It VM\_METER	struct vmtotal	no
545.El
546.Pp
547.Bl -tag -width "123456"
548.It Li VM_LOADAVG
549Return the load average history.
550The returned data consists of a
551.Va struct loadavg .
552.It Li VM_METER
553Return the system wide virtual memory statistics.
554The returned data consists of a
555.Va struct vmtotal .
556.El
557.Sh RETURN VALUES
558If the call to
559.Nm sysctl
560is successful, 0 is returned.
561Otherwise \-1 is returned and
562.Va errno
563is set appropriately.
564.Sh ERRORS
565The following errors may be reported:
566.Bl -tag -width Er
567.It Bq Er EFAULT
568The buffer
569.Fa name ,
570.Fa oldp ,
571.Fa newp ,
572or length pointer
573.Fa oldlenp
574contains an invalid address.
575.It Bq Er EINVAL
576The
577.Fa name
578array is less than two or greater than CTL_MAXNAME.
579.It Bq Er EINVAL
580A non-null
581.Fa newp
582is given and its specified length in
583.Fa newlen
584is too large or too small.
585.It Bq Er ENOMEM
586The length pointed to by
587.Fa oldlenp
588is too short to hold the requested value.
589.It Bq Er ENOTDIR
590The
591.Fa name
592array specifies an intermediate rather than terminal name.
593.It Bq Er EOPNOTSUPP
594The
595.Fa name
596array specifies a value that is unknown.
597.It Bq Er EPERM
598An attempt is made to set a read-only value.
599.It Bq Er EPERM
600A process without appropriate privilege attempts to set a value.
601.El
602.Sh FILES
603.Bl -tag -width <netinet/icmpXvar.h> -compact
604.It Pa <sys/sysctl.h>
605definitions for top level identifiers, second level kernel and hardware
606identifiers, and user level identifiers
607.It Pa <sys/socket.h>
608definitions for second level network identifiers
609.It Pa <sys/gmon.h>
610definitions for third level profiling identifiers
611.It Pa <vm/vm_param.h>
612definitions for second level virtual memory identifiers
613.It Pa <netinet/in.h>
614definitions for third level Internet identifiers and
615fourth level IP identifiers
616.It Pa <netinet/icmp_var.h>
617definitions for fourth level ICMP identifiers
618.It Pa <netinet/udp_var.h>
619definitions for fourth level UDP identifiers
620.El
621.Sh SEE ALSO
622.Xr sysctl 8
623.Sh HISTORY
624The
625.Nm sysctl
626function first appeared in 4.4BSD.
627