xref: /original-bsd/lib/libc/gen/sysctl.3 (revision b3c06cab)
1.\" Copyright (c) 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" %sccs.include.redist.roff%
5.\"
6.\"	@(#)sysctl.3	8.4 (Berkeley) 05/09/95
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\_VFS	sys/mount.h	Filesystem
95.It CTL\_HW	sys/sysctl.h	Generic CPU, I/O
96.It CTL\_KERN	sys/sysctl.h	High kernel 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_VFS
154A distinguished second level name, VFS_GENERIC,
155is used to get general information about all filesystems.
156One of its third level identifiers is VFS_MAXTYPENUM
157that gives the highest valid filesystem type number.
158Its other third level identifier is VFS_CONF that
159returns configuration information about the filesystem
160type given as a fourth level identifier (see
161.Xr getvfsbyname 3
162as an example of its use).
163The remaining second level identifiers are the
164filesystem type number returned by a
165.Xr statfs 2
166call or from VFS_CONF.
167The third level identifiers available for each filesystem
168are given in the header file that defines the mount
169argument structure for that filesystem.
170.Sh CTL_HW
171The string and integer information available for the CTL_HW level
172is detailed below.
173The changeable column shows whether a process with appropriate
174privilege may change the value.
175.Bl -column "Second level nameXXXXXX" integerXXX -offset indent
176.It Sy Pa Second level name	Type	Changeable
177.It HW\_MACHINE	string	no
178.It HW\_MODEL	string	no
179.It HW\_NCPU	integer	no
180.It HW\_BYTEORDER	integer	no
181.It HW\_PHYSMEM	integer	no
182.It HW\_USERMEM	integer	no
183.It HW\_PAGESIZE	integer	no
184.\".It HW\_DISKNAMES	integer	no
185.\".It HW\_DISKSTATS	integer	no
186.El
187.Pp
188.Bl -tag -width "123456"
189.It Li HW_MACHINE
190The machine class.
191.It Li HW_MODEL
192The machine model
193.It Li HW_NCPU
194The number of cpus.
195.ne 1i
196.It Li HW_BYTEORDER
197The byteorder (4,321, or 1,234).
198.It Li HW_PHYSMEM
199The bytes of physical memory.
200.It Li HW_USERMEM
201The bytes of non-kernel memory.
202.It Li HW_PAGESIZE
203The software page size.
204.\".It Fa HW_DISKNAMES
205.\".It Fa HW_DISKSTATS
206.El
207.Sh CTL_KERN
208The string and integer information available for the CTL_KERN level
209is detailed below.
210The changeable column shows whether a process with appropriate
211privilege may change the value.
212The types of data currently available are process information,
213system vnodes, the open file entries, routing table entries,
214virtual memory statistics, load average history, and clock rate
215information.
216.Bl -column "KERNXCHOWNXRESTRICTEDXXX" "struct clockrateXXX" -offset indent
217.It Sy Pa Second level name	Type	Changeable
218.It KERN\_ARGMAX	integer	no
219.It KERN\_BOOTTIME	struct timeval	no
220.It KERN\_CHOWN\_RESTRICTED	integer	no
221.It KERN\_CLOCKRATE	struct clockinfo	no
222.It KERN\_FILE	struct file	no
223.It KERN\_HOSTID	integer	yes
224.It KERN\_HOSTNAME	string	yes
225.It KERN\_JOB\_CONTROL	integer	no
226.It KERN\_LINK\_MAX	integer	no
227.It KERN\_MAXFILES	integer	yes
228.It KERN\_MAXPROC	integer	yes
229.It KERN\_MAXVNODES	integer	yes
230.It KERN\_MAX\_CANON	integer	no
231.It KERN\_MAX\_INPUT	integer	no
232.It KERN\_NAME\_MAX	integer	no
233.It KERN\_NGROUPS	integer	no
234.It KERN\_NO\_TRUNC	integer	no
235.It KERN\_OSRELEASE	string	no
236.It KERN\_OSREV	integer	no
237.It KERN\_OSTYPE	string	no
238.It KERN\_PATH\_MAX	integer	no
239.It KERN\_PIPE\_BUF	integer	no
240.It KERN\_POSIX1	integer	no
241.It KERN\_PROC	struct proc	no
242.It KERN\_PROF	node	not applicable
243.It KERN\_SAVED\_IDS	integer	no
244.It KERN\_SECURELVL	integer	raise only
245.It KERN\_VDISABLE	integer	no
246.It KERN\_VERSION	string	no
247.It KERN\_VNODE	struct vnode	no
248.El
249.ne 1i
250.Pp
251.Bl -tag -width "123456"
252.It Li KERN_ARGMAX
253The maximum bytes of argument to
254.Xr exec 2 .
255.It Li KERN_BOOTTIME
256A
257.Va struct timeval
258structure is returned.
259This structure contains the time that the system was booted.
260.It Li KERN_CHOWN_RESTRICTED
261Return 1 if appropriate privileges are required for the
262.Xr chown 2
263system call, otherwise 0.
264.It Li KERN_CLOCKRATE
265A
266.Va struct clockinfo
267structure is returned.
268This structure contains the clock, statistics clock and profiling clock
269frequencies, and the number of micro-seconds per hz tick.
270.It Li KERN_FILE
271Return the entire file table.
272The returned data consists of a single
273.Va struct filehead
274followed by an array of
275.Va struct file ,
276whose size depends on the current number of such objects in the system.
277.It Li KERN_HOSTID
278Get or set the host id.
279.It Li KERN_HOSTNAME
280Get or set the hostname.
281.It Li KERN_JOB_CONTROL
282Return 1 if job control is available on this system, otherwise 0.
283.It Li KERN_LINK_MAX
284The maximum file link count.
285.It Li KERN_MAXFILES
286The maximum number of open files that may be open in the system.
287.It Li KERN_MAXPROC
288The maximum number of simultaneous processes the system will allow.
289.It Li KERN_MAXVNODES
290The maximum number of vnodes available on the system.
291.It Li KERN_MAX_CANON
292The maximum number of bytes in terminal canonical input line.
293.It Li KERN_MAX_INPUT
294The minimum maximum number of bytes for which space is available in
295a terminal input queue.
296.It Li KERN_NAME_MAX
297The maximum number of bytes in a file name.
298.It Li KERN_NGROUPS
299The maximum number of supplemental groups.
300.It Li KERN_NO_TRUNC
301Return 1 if file names longer than KERN_NAME_MAX are truncated.
302.It Li KERN_OSRELEASE
303The system release string.
304.It Li KERN_OSREV
305The system revision string.
306.It Li KERN_OSTYPE
307The system type string.
308.It Li KERN_PATH_MAX
309The maximum number of bytes in a pathname.
310.It Li KERN_PIPE_BUF
311The maximum number of bytes which will be written atomically to a pipe.
312.It Li KERN_POSIX1
313The version of ISO/IEC 9945 (POSIX 1003.1) with which the system
314attempts to comply.
315.It Li KERN_PROC
316Return the entire process table, or a subset of it.
317An array of
318.Va struct kinfo_proc
319structures is returned,
320whose size depends on the current number of such objects in the system.
321The third and fourth level names are as follows:
322.Bl -column "Third level nameXXXXXX" "Fourth level is:XXXXXX" -offset indent
323.It Pa Third level name	Fourth level is:
324.It KERN\_PROC\_ALL	None
325.It KERN\_PROC\_PID	A process ID
326.It KERN\_PROC\_PGRP	A process group
327.It KERN\_PROC\_TTY	A tty device
328.It KERN\_PROC\_UID	A user ID
329.It KERN\_PROC\_RUID	A real user ID
330.El
331.It Li KERN_PROF
332Return profiling information about the kernel.
333If the kernel is not compiled for profiling,
334attempts to retrieve any of the KERN_PROF values will
335fail with EOPNOTSUPP.
336The third level names for the string and integer profiling information
337is detailed below.
338The changeable column shows whether a process with appropriate
339privilege may change the value.
340.Bl -column "GPROFXGMONPARAMXXX" "struct gmonparamXXX" -offset indent
341.It Sy Pa Third level name	Type	Changeable
342.It GPROF\_STATE	integer	yes
343.It GPROF\_COUNT	u_short[\|]	yes
344.It GPROF\_FROMS	u_short[\|]	yes
345.It GPROF\_TOS	struct tostruct	yes
346.It GPROF\_GMONPARAM	struct gmonparam	no
347.El
348.Pp
349The variables are as follows:
350.Bl -tag -width "123456"
351.It Li GPROF_STATE
352Returns GMON_PROF_ON or GMON_PROF_OFF to show that profiling
353is running or stopped.
354.It Li GPROF_COUNT
355Array of statistical program counter counts.
356.It Li GPROF_FROMS
357Array indexed by program counter of call-from points.
358.It Li GPROF_TOS
359Array of
360.Va struct tostruct
361describing destination of calls and their counts.
362.It Li GPROF_GMONPARAM
363Structure giving the sizes of the above arrays.
364.El
365.ne 1i
366.It Li KERN_SAVED_IDS
367Returns 1 if saved set-group and saved set-user ID is available.
368.It Li KERN_SECURELVL
369The system security level.
370This level may be raised by processes with appropriate privilege.
371It may only be lowered by process 1.
372.It Li KERN_VDISABLE
373Returns the terminal character disabling value.
374.It Li KERN_VERSION
375The system version string.
376.It Li KERN_VNODE
377Return the entire vnode table.
378Note, the vnode table is not necessarily a consistent snapshot of
379the system.
380The returned data consists of an array whose size depends on the
381current number of such objects in the system.
382Each element of the array contains the kernel address of a vnode
383.Va struct vnode *
384followed by the vnode itself
385.Va struct vnode .
386.El
387.Sh CTL_MACHDEP
388The set of variables defined is architecture dependent.
389Most architectures define at least the following variables.
390.Bl -column "CONSOLE_DEVICEXXX" "integerXXX" -offset indent
391.It Sy Pa Second level name	Type	Changeable
392.It Li CPU_CONSDEV	dev_t	no
393.El
394.Sh CTL_NET
395The string and integer information available for the CTL_NET level
396is detailed below.
397The changeable column shows whether a process with appropriate
398privilege may change the value.
399.Bl -column "Second level nameXXXXXX" "routing messagesXXX" -offset indent
400.It Sy Pa Second level name	Type	Changeable
401.It PF\_ROUTE	routing messages	no
402.It PF\_INET	internet values	yes
403.El
404.Pp
405.Bl -tag -width "123456"
406.It Li PF_ROUTE
407Return the entire routing table or a subset of it.
408The data is returned as a sequence of routing messages (see
409.Xr route 4
410for the header file, format and meaning).
411The length of each message is contained in the message header.
412.Pp
413The third level name is a protocol number, which is currently always 0.
414The fourth level name is an address family, which may be set to 0 to
415select all address families.
416The fifth and sixth level names are as follows:
417.Bl -column "Fifth level nameXXXXXX" "Sixth level is:XXX" -offset indent
418.It Pa Fifth level name	Sixth level is:
419.It NET\_RT\_FLAGS	rtflags
420.It NET\_RT\_DUMP	None
421.It NET\_RT\_IFLIST	None
422.El
423.It Li PF_INET
424Get or set various global information about the internet protocols.
425The third level name is the protocol.
426The fourth level name is the variable name.
427The currently defined protocols and names are:
428.ne 1i
429.Bl -column "Protocol nameXXXXXX" "Variable nameXXX" "integerXXX" -offset indent
430.It Pa Protocol name	Variable name	Type	Changeable
431.It ip	forwarding	integer	yes
432.It ip	redirect	integer	yes
433.It ip	ttl	integer	yes
434.It icmp	maskrepl	integer	yes
435.It udp	checksum	integer	yes
436.El
437.Pp
438The variables are as follows:
439.Bl -tag -width "123456"
440.It Li ip.forwarding
441Returns 1 when IP forwarding is enabled for the host,
442meaning that the host is acting as a router.
443.It Li ip.redirect
444Returns 1 when ICMP redirects may be sent by the host.
445This option is ignored unless the host is routing IP packets,
446and should normally be enabled on all systems.
447.It Li ip.ttl
448The maximum time-to-live (hop count) value for an IP packet sourced by
449the system.
450This value applies to normal transport protocols, not to ICMP.
451.It Li icmp.maskrepl
452Returns 1 if ICMP network mask requests are to be answered.
453.It Li udp.checksum
454Returns 1 when UDP checksums are being computed and checked.
455Disabling UDP checksums is strongly discouraged.
456.El
457.Sh CTL_USER
458The string and integer information available for the CTL_USER level
459is detailed below.
460The changeable column shows whether a process with appropriate
461privilege may change the value.
462.Bl -column "USER_COLL_WEIGHTS_MAXXXX" "integerXXX" -offset indent
463.It Sy Pa Second level name	Type	Changeable
464.It USER\_BC\_BASE\_MAX	integer	no
465.It USER\_BC\_DIM\_MAX	integer	no
466.It USER\_BC\_SCALE\_MAX	integer	no
467.It USER\_BC\_STRING\_MAX	integer	no
468.It USER\_COLL\_WEIGHTS\_MAX	integer	no
469.It USER\_CS\_PATH	string	no
470.It USER\_EXPR\_NEST\_MAX	integer	no
471.It USER\_LINE\_MAX	integer	no
472.It USER\_POSIX2\_CHAR\_TERM	integer	no
473.It USER\_POSIX2\_C\_BIND	integer	no
474.It USER\_POSIX2\_C\_DEV	integer	no
475.It USER\_POSIX2\_FORT\_DEV	integer	no
476.It USER\_POSIX2\_FORT\_RUN	integer	no
477.It USER\_POSIX2\_LOCALEDEF	integer	no
478.It USER\_POSIX2\_SW\_DEV	integer	no
479.It USER\_POSIX2\_UPE	integer	no
480.It USER\_POSIX2\_VERSION	integer	no
481.It USER\_RE\_DUP\_MAX	integer	no
482.It USER\_STREAM\_MAX	integer	no
483.It USER\_TZNAME\_MAX	integer	no
484.El
485.Bl -tag -width "123456"
486.Pp
487.It Li USER_BC_BASE_MAX
488The maximum ibase/obase values in the
489.Xr bc 1
490utility.
491.It Li USER_BC_DIM_MAX
492The maximum array size in the
493.Xr bc 1
494utility.
495.It Li USER_BC_SCALE_MAX
496The maximum scale value in the
497.Xr bc 1
498utility.
499.It Li USER_BC_STRING_MAX
500The maximum string length in the
501.Xr bc 1
502utility.
503.It Li USER_COLL_WEIGHTS_MAX
504The maximum number of weights that can be assigned to any entry of
505the LC_COLLATE order keyword in the locale definition file.
506.It Li USER_CS_PATH
507Return a value for the
508.Ev PATH
509environment variable that finds all the standard utilities.
510.It Li USER_EXPR_NEST_MAX
511The maximum number of expressions that can be nested within
512parenthesis by the
513.Xr expr 1
514utility.
515.It Li USER_LINE_MAX
516The maximum length in bytes of a text-processing utility's input
517line.
518.It Li USER_POSIX2_CHAR_TERM
519Return 1 if the system supports at least one terminal type capable of
520all operations described in POSIX 1003.2, otherwise 0.
521.It Li USER_POSIX2_C_BIND
522Return 1 if the system's C-language development facilities support the
523C-Language Bindings Option, otherwise 0.
524.It Li USER_POSIX2_C_DEV
525Return 1 if the system supports the C-Language Development Utilities Option,
526otherwise 0.
527.It Li USER_POSIX2_FORT_DEV
528Return 1 if the system supports the FORTRAN Development Utilities Option,
529otherwise 0.
530.It Li USER_POSIX2_FORT_RUN
531Return 1 if the system supports the FORTRAN Runtime Utilities Option,
532otherwise 0.
533.It Li USER_POSIX2_LOCALEDEF
534Return 1 if the system supports the creation of locales, otherwise 0.
535.It Li USER_POSIX2_SW_DEV
536Return 1 if the system supports the Software Development Utilities Option,
537otherwise 0.
538.It Li USER_POSIX2_UPE
539Return 1 if the system supports the User Portability Utilities Option,
540otherwise 0.
541.It Li USER_POSIX2_VERSION
542The version of POSIX 1003.2 with which the system attempts to comply.
543.It Li USER_RE_DUP_MAX
544The maximum number of repeated occurrences of a regular expression
545permitted when using interval notation.
546.ne 1i
547.It Li USER_STREAM_MAX
548The minimum maximum number of streams that a process may have open
549at any one time.
550.It Li USER_TZNAME_MAX
551The minimum maximum number of types supported for the name of a
552timezone.
553.El
554.Sh CTL_VM
555The string and integer information available for the CTL_VM level
556is detailed below.
557The changeable column shows whether a process with appropriate
558privilege may change the value.
559.Bl -column "Second level nameXXXXXX" "struct loadavgXXX" -offset indent
560.It Sy Pa Second level name	Type	Changeable
561.It VM\_LOADAVG	struct loadavg	no
562.It VM\_METER	struct vmtotal	no
563.El
564.Pp
565.Bl -tag -width "123456"
566.It Li VM_LOADAVG
567Return the load average history.
568The returned data consists of a
569.Va struct loadavg .
570.It Li VM_METER
571Return the system wide virtual memory statistics.
572The returned data consists of a
573.Va struct vmtotal .
574.El
575.Sh RETURN VALUES
576If the call to
577.Nm sysctl
578is successful, the number of bytes copied out is returned.
579Otherwise \-1 is returned and
580.Va errno
581is set appropriately.
582.Sh ERRORS
583The following errors may be reported:
584.Bl -tag -width Er
585.It Bq Er EFAULT
586The buffer
587.Fa name ,
588.Fa oldp ,
589.Fa newp ,
590or length pointer
591.Fa oldlenp
592contains an invalid address.
593.It Bq Er EINVAL
594The
595.Fa name
596array is less than two or greater than CTL_MAXNAME.
597.It Bq Er EINVAL
598A non-null
599.Fa newp
600is given and its specified length in
601.Fa newlen
602is too large or too small.
603.It Bq Er ENOMEM
604The length pointed to by
605.Fa oldlenp
606is too short to hold the requested value.
607.It Bq Er ENOTDIR
608The
609.Fa name
610array specifies an intermediate rather than terminal name.
611.It Bq Er EOPNOTSUPP
612The
613.Fa name
614array specifies a value that is unknown.
615.It Bq Er EPERM
616An attempt is made to set a read-only value.
617.It Bq Er EPERM
618A process without appropriate privilege attempts to set a value.
619.El
620.Sh FILES
621.Bl -tag -width <netinet/icmpXvar.h> -compact
622.It Pa <sys/sysctl.h>
623definitions for top level identifiers, second level kernel and hardware
624identifiers, and user level identifiers
625.It Pa <sys/socket.h>
626definitions for second level network identifiers
627.It Pa <sys/gmon.h>
628definitions for third level profiling identifiers
629.It Pa <vm/vm_param.h>
630definitions for second level virtual memory identifiers
631.It Pa <netinet/in.h>
632definitions for third level Internet identifiers and
633fourth level IP identifiers
634.It Pa <netinet/icmp_var.h>
635definitions for fourth level ICMP identifiers
636.It Pa <netinet/udp_var.h>
637definitions for fourth level UDP identifiers
638.El
639.Sh SEE ALSO
640.Xr sysctl 8
641.Sh HISTORY
642The
643.Nm sysctl
644function first appeared in 4.4BSD.
645