xref: /csrg-svn/lib/libc/gen/sysctl.3 (revision 58507)
158401Smckusick.\" Copyright (c) 1993 The Regents of the University of California.
258401Smckusick.\" All rights reserved.
358401Smckusick.\"
458401Smckusick.\" %sccs.include.redist.roff%
558401Smckusick.\"
6*58507Smckusick.\"	@(#)sysctl.3	6.3 (Berkeley) 03/05/93
758401Smckusick.\"
858401Smckusick.Dd ""
958401Smckusick.Dt SYSCTL 2
1058401Smckusick.Os
1158401Smckusick.Sh NAME
1258401Smckusick.Nm sysctl
1358401Smckusick.Nd get or set kernel state
1458401Smckusick.Sh SYNOPSIS
1558401Smckusick.Fd #include <sys/sysctl.h>
1658401Smckusick.Ft int
1758462Sbostic.Fn sysctl "int *name" "u_int namelen" "void *old" "size_t *oldlenp" "void *new" "size_t newlen"
1858401Smckusick.Sh DESCRIPTION
1958401SmckusickThe
2058401Smckusick.Fn sysctl
2158401Smckusickfunction retrieves kernel state and allows processes with
2258401Smckusickappropriate privilege to set kernel state.
23*58507SmckusickThe state to be set is described using a
24*58507Smckusick``Management Information Base'' (MIB) style name,
2558401Smckusickdescribed in a
2658401Smckusick.Fa namelen
2758401Smckusicklength array of integers pointed to by
2858401Smckusick.Fa name .
2958401SmckusickThe top level names are defined with a CTL_ prefix in
3058401Smckusick.Pa <sys/sysctl.h> .
3158401SmckusickThe next levels down are found in the files given in the ``FILES''
3258401Smckusicksection of this manual page.
3358401Smckusick.Pp
34*58507SmckusickThe information is copied into the buffer specified by
35*58507Smckusick.Fa old .
36*58507SmckusickThe size of the buffer is given by the location specified by
37*58507Smckusick.Fa oldlenp
38*58507Smckusickbefore the call,
39*58507Smckusickand that location gives the amount of data copied after a successful call.
40*58507SmckusickIf the amount of data available is greater
41*58507Smckusickthan the size of the buffer supplied,
42*58507Smckusickthe call supplies as much data as fits in the buffer provided
43*58507Smckusickand returns with the error code EINVAL.
4458401SmckusickIf the old value is not desired,
4558401Smckusick.Fa old
4658401Smckusickand
4758401Smckusick.Fa oldlenp
4858401Smckusickcan be set to NULL.
4958401Smckusick.Pp
50*58507SmckusickThe size of the available data can be determined by calling
51*58507Smckusick.Fn sysctl
52*58507Smckusickwith a NULL parameter for
53*58507Smckusick.Fa old .
54*58507SmckusickThe size of the available data be returned in the location pointed to by
55*58507Smckusick.Fa oldlenp .
56*58507SmckusickFor some operations, the amount of space may change often;
57*58507Smckusickthe system attempts to round up so that the returned size is
58*58507Smckusicklarge enough for a call to return the data shortly thereafter.
59*58507Smckusick.Pp
6058401SmckusickTo set a new value,
6158401Smckusick.Fa new
6258401Smckusickis set to point to a buffer of length
6358401Smckusick.Fa newlen
6458401Smckusickfrom which the requested value is to be taken.
65*58507SmckusickIf a new value is not to be set,
6658401Smckusick.Fa new
6758401Smckusickshould be set to NULL and
6858401Smckusick.Fa newlen
6958401Smckusickset to 0.
7058401Smckusick.Pp
71*58507SmckusickThe information available from
72*58507Smckusick.Fn sysctl
73*58507Smckusickconsists of integers, strings, and tables.
74*58507SmckusickThe string and integer information is detailed below.
75*58507SmckusickThe changeable column shows whether a process with appropriate
76*58507Smckusickprivilege can change the value.
77*58507SmckusickChangeable values may be retrieved and set using the
78*58507Smckusick.Xr sysctl 1
79*58507Smckusickutility.
80*58507Smckusick.Bl -column CTL_KERN/KERN_OSRELEASEXX "integerxx" -offset indent
81*58507Smckusick.It Sy "Name  " "	Type  " "	Changeable"
82*58507Smckusick.It Pa CTL_KERN/KERN_OSTYPE No "	string" No "	no"
83*58507Smckusick.It Pa CTL_KERN/KERN_OSRELEASE No "	string" No "	no"
84*58507Smckusick.It Pa CTL_KERN/KERN_VERSION No "	string" No "	no"
85*58507Smckusick.It Pa CTL_KERN/KERN_OSREV No "	integer" No "	no"
86*58507Smckusick.It Pa CTL_KERN/KERN_POSIX1 No "	integer" No "	no"
87*58507Smckusick.It Pa CTL_KERN/KERN_MAXPROC No "	integer" No "	yes"
88*58507Smckusick.It Pa CTL_KERN/KERN_MAXFILES No "	integer" No "	yes"
89*58507Smckusick.It Pa CTL_KERN/KERN_ARGMAX No "	integer" No "	no"
90*58507Smckusick.It Pa CTL_KERN/KERN_SECURELVL No "	integer" No "	raise only"
91*58507Smckusick.It Pa CTL_KERN/KERN_HOSTNAME No "	string" No "	yes"
92*58507Smckusick.It Pa CTL_KERN/KERN_HOSTID No "	integer" No "	yes"
93*58507Smckusick.It Pa CTL_HW/HW_MACHINE No "	string" No "	no"
94*58507Smckusick.It Pa CTL_HW/HW_MODEL No "	string" No "	no"
95*58507Smckusick.It Pa CTL_HW/HW_NCPU No "	integer" No "	no"
96*58507Smckusick.It Pa CTL_HW/HW_CPUSPEED No "	integer" No "	no"
97*58507Smckusick.It Pa CTL_HW/HW_PHYSMEM No "	integer" No "	no"
98*58507Smckusick.It Pa CTL_HW/HW_USERMEM No "	integer" No "	no"
99*58507Smckusick.It Pa CTL_HW/HW_PAGESIZE No "	integer" No "	no"
100*58507Smckusick.El
101*58507Smckusick.Pp
102*58507SmckusickFor example, the following retrieves the maximum number of processes allowed
103*58507Smckusickin the system:
10458401Smckusick.Bd -literal -offset indent -compact
10558462Sbosticint name[2], maxproc;
10658462Sbosticsize_t len;
107*58507Smckusick.sp
10858401Smckusickname[0] = CTL_KERN;
10958401Smckusickname[1] = KERN_MAXPROC;
11058462Sbosticlen = sizeof(maxproc);
11158401Smckusicksysctl(name, 2, &maxproc, &len, NULL, 0);
11258401Smckusick.Ed
113*58507Smckusick.Pp
114*58507SmckusickThe tables that can be retrieved from the kernel by
115*58507Smckusick.Fn sysctl
116*58507Smckusickare detailed below.
117*58507SmckusickFor most operations, the
118*58507Smckusick.Fn sysctl
119*58507Smckusickfunction returns a consistent snapshot of the data requested.
120*58507Smckusick(This is not currently true for
121*58507Smckusick.Dv KERN_VNODE ) .
122*58507SmckusickThe consistency is done by locking the destination buffer into memory
123*58507Smckusickso that the data may be copied out without blocking.
124*58507SmckusickCalls are serialized to avoid deadlock.
125*58507SmckusickThe types of data currently available are process information,
126*58507Smckusicksystem vnodes, the open file entries, routing table entries,
127*58507Smckusickvirtual memory statistics, load average history,
128*58507Smckusickand clock rate information.
129*58507SmckusickThe following paragraphs detail how each of these is obtained.
130*58507Smckusick.Pp
131*58507SmckusickThe entire process table, or a subset of it, may be obtained with
132*58507Smckusick.Pa CTL_KERN/KERN_PROC/<op>/<arg> .
133*58507SmckusickAn array of
134*58507Smckusick.Ns ( Fa struct kinfo_proc Ns )
135*58507Smckusickstructures is returned,
136*58507Smckusickwhose size depends on the current number of such objects in the system.
137*58507SmckusickThe values for <op> and <arg> are:
138*58507Smckusick.Bl -column KERN_PROC_PGRPX "ARG Meaningxx" -offset indent
139*58507Smckusick.It Sy "OP  " "	ARG Meaning  "
140*58507Smckusick.It Dv KERN_PROC_ALL No "	none"
141*58507Smckusick.It Dv KERN_PROC_PID No "	process ID"
142*58507Smckusick.It Dv KERN_PROC_PGRP No "	process group"
143*58507Smckusick.It Dv KERN_PROC_TTY No "	tty device"
144*58507Smckusick.It Dv KERN_PROC_UID No "	user ID"
145*58507Smckusick.It Dv KERN_PROC_RUID No "	real user ID"
146*58507Smckusick.El
147*58507Smckusick.Pp
148*58507SmckusickThe entire vnode table may be obtained with
149*58507Smckusick.Pa CTL_KERN/KERN_VNODE .
150*58507SmckusickThe returned data consists of an array
151*58507Smckusickwhose size depends on the current number of such objects in the system.
152*58507SmckusickEach element of the array contains the kernel address of a vnode
153*58507Smckusick.Ns ( Fa struct vnode * Ns )
154*58507Smckusickfollowed by the vnode itself
155*58507Smckusick.Ns ( Fa struct vnode Ns ) .
156*58507Smckusick.Pp
157*58507SmckusickThe entire file table may be obtained with
158*58507Smckusick.Pa CTL_KERN/KERN_FILE .
159*58507SmckusickThe returned data consists of a single
160*58507Smckusick.Ns ( Fa struct filehead Ns )
161*58507Smckusickfollowed by an array of
162*58507Smckusick.Ns ( Fa struct file Ns ) ,
163*58507Smckusickwhose size depends on the current number of such objects in the system.
164*58507Smckusick.Pp
165*58507SmckusickInformation about the system clock rate may be obtained with
166*58507Smckusick.Pa CTL_KERN/KERN_CLOCKRATE .
167*58507SmckusickThe returned data consists of a
168*58507Smckusick.Ns ( Fa struct clockinfo Ns ) .
169*58507Smckusick.Pp
170*58507SmckusickInformation about the load average history may be obtained with
171*58507Smckusick.Pa CTL_VM/VM_LOADAVG .
172*58507SmckusickThe returned data consists of a
173*58507Smckusick.Ns ( Fa struct loadavg Ns ) .
174*58507Smckusick.Pp
175*58507SmckusickInformation about the system wide virtual memory statistics
176*58507Smckusickmay be obtained with
177*58507Smckusick.Pa CTL_VM/VM_METER .
178*58507SmckusickThe returned data consists of a
179*58507Smckusick.Ns ( Fa struct vmtotal Ns ) .
180*58507Smckusick.Pp
181*58507SmckusickThe entire routing table or a subset of it may be obtained with
182*58507Smckusick.Pa CTL_NET/PF_ROUTE/<protocol number>/<address family>/<op>/<arg> .
183*58507SmckusickThe protocol number is currently always zero.
184*58507SmckusickThe address family may be set to zero to select all address families.
185*58507SmckusickThe values for <op> and <arg> are:
186*58507Smckusick.Bl -column NET_RT_IFLISTX "ARG Meaningxx" -offset indent
187*58507Smckusick.It Sy "OP  " "	ARG Meaning  "
188*58507Smckusick.It Dv NET_RT_FLAGS No "	rtflags"
189*58507Smckusick.It Dv NET_RT_DUMP No "	none"
190*58507Smckusick.It Dv NET_RT_IFLIST No "	none"
191*58507Smckusick.El
192*58507SmckusickThe data is returned as a sequence of routing messages (see
193*58507Smckusick.Xr route 4
194*58507Smckusickfor the header file, format and meaning).
195*58507SmckusickThe length of each message is contained in the message header.
19658401Smckusick.Sh RETURN VALUES
19758401SmckusickIf the call to
19858401Smckusick.Fn sysctl
199*58507Smckusickis successful, 0 is returned.
20058401SmckusickOtherwise \-1 is returned and
20158401Smckusick.Va errno
20258401Smckusickis set appropriately.
20358401Smckusick.Sh ERRORS
20458401SmckusickThe following error may be reported:
20558401Smckusick.Bl -tag -width Er
20658401Smckusick.It Bq Er EFAULT
20758401SmckusickThe buffer
20858401Smckusick.Fa name ,
20958401Smckusick.Fa old ,
21058401Smckusick.Fa new ,
21158401Smckusickor length pointer
21258401Smckusick.Fa oldlenp
21358401Smckusickcontains an invalid address.
21458401Smckusick.It Bq Er EINVAL
21558401SmckusickThe
21658401Smckusick.Fa name
21758401Smckusickarray is less than two or greater than CTL_MAXNAME.
21858401Smckusick.It Bq Er EINVAL
21958401SmckusickA non-null
22058401Smckusick.Fa new
22158401Smckusickis given and its specified length in
22258401Smckusick.Fa newlen
22358401Smckusickis too large or too small.
22458401Smckusick.It Bq Er ENOMEM
22558401SmckusickThe length pointed to by
22658401Smckusick.Fa oldlenp
22758401Smckusickis too short to hold the requested value.
22858401Smckusick.It Bq Er ENOTDIR
22958401SmckusickThe
23058401Smckusick.Fa name
23158401Smckusickarray specifies an intermediate rather than terminal name.
23258401Smckusick.It Bq Er EOPNOTSUPP
23358401SmckusickThe
23458401Smckusick.Fa name
23558401Smckusickarray specifies a value that is unknown.
23658401Smckusick.It Bq Er EPERM
23758401SmckusickAn attempt is made to set a read-only value.
23858401Smckusick.It Bq Er EPERM
23958401SmckusickA process without appropriate privilege attempts to set a value.
24058401Smckusick.El
24158401Smckusick.Sh FILES
24258401Smckusick.Bl -tag -width <vm/vm_param.h> -compact
24358401Smckusick.It Pa <sys/sysctl.h>
24458401Smckusickdefinitions for top level identifiers and second level kernel
24558401Smckusickand hardware identifiers
24658401Smckusick.It Pa <sys/socket.h>
24758401Smckusickdefinitions for second level network identifiers
24858401Smckusick.It Pa <vm/vm_param.h>
24958401Smckusickdefinitions for second level virtual memory identifiers
25058401Smckusick.El
25158401Smckusick.Sh SEE ALSO
25258401Smckusick.Xr sysctl 8
25358401Smckusick.Sh HISTORY
254*58507SmckusickThe
25558401Smckusick.Fn sysctl
256*58507Smckusickfunction first appeared in 4.4BSD.
257