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