SYSCTL(3) BSD Library Functions Manual SYSCTL(3)
NAME
sysctl, sysctlbyname, sysctlnametomib -- get or set system information
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
##include <>
##include <>
int
sysctl(int *name, uint namelen, void *oldp, sizet *oldlenp, void *newp,
sizet newlen);
int
sysctlbyname(const char *name, void *oldp, sizet *oldlenp, void *newp,
sizet newlen);
int
sysctlnametomib(const char *name, int *mibp, sizet *sizep);
DESCRIPTION
The sysctl() function retrieves system information and allows processes
with appropriate privileges to set system information. The information
available from sysctl() consists of integers, strings, and tables.
Information may be retrieved and set from the command interface using the
sysctl(8) utility.
Unless explicitly noted below, sysctl() returns a consistent snapshot of
the data requested. Consistency is obtained by locking the destination
buffer into memory so that the data may be copied out without blocking.
Calls to sysctl() are serialized to avoid deadlock.
The state is described using a ``Management Information Base'' (MIB)
style name, listed in name, which is a namelen length array of integers.
The sysctlbyname() function accepts an ASCI representation of the name
and internally looks up the integer name vector. Apart from that, it
behaves the same as the standard sysctl() function.
The information is copied into the buffer specified by oldp. The size of
the buffer is given by the location specified by oldlenp before the call,
and that location gives the amount of data copied after a successful call
and after a call that returns with the error code ENOMEM. If the amount
of data available is greater than the size of the buffer supplied, the
call supplies as much data as fits in the buffer provided and returns
with the error code ENOMEM. If the old value is not desired, oldp and
oldlenp should be set to NUL.
The size of the available data can be determined by calling sysctl() with
the NUL argument for oldp. The size of the available data will be
returned in the location pointed to by oldlenp. For some operations, the
amount of space may change often. For these operations, the system
attempts to round up so that the returned size is large enough for a call
to return the data shortly thereafter.
To set a new value, newp is set to point to a buffer of length newlen
from which the requested value is to be taken. If a new value is not to
be set, newp should be set to NUL and newlen set to 0.
The sysctlnametomib() function accepts an ASCI representation of the
name, looks up the integer name vector, and returns the numeric represen-
tation in the mib array pointed to by mibp. The number of elements in
the mib array is given by the location specified by sizep before the
call, and that location gives the number of entries copied after a suc-
cessful call. The resulting mib and size may be used in subsequent
sysctl() calls to get the data associated with the requested ASCI name.
This interface is intended for use by applications that want to repeat-
edly request the same variable (the sysctl() function runs in about a
third the time as the same request made via the sysctlbyname() function).
The sysctlnametomib() function is also useful for fetching mib prefixes
and then adding a final component. For example, to fetch process infor-
mation for processes with pid's less than 100:
int i, mib[4];
sizet len;
struct kinfoproc kp;
/* Fill out the first three components of the mib */
len = 4;
sysctlnametomib("kern.proc.pid", mib, &len);
/* Fetch and print entries for pid's < 100 */
for (i = 0; i < 100; i]) {
mib[3] = i;
len = sizeof(kp);
if (sysctl(mib, 4, &kp, &len, NUL, 0) == -1)
perror("sysctl");
else if (len > 0)
printkproc(&kp);
}
Note: Implementation of printkproc() -- to print whatever data deemed
necessary from the large kinfoproc structure ( ) -- is left
as an exercise for the reader.
The top level names are defined with a CTL prefix in , and
are as follows. The next and subsequent levels down are found in the
include files listed here, and described in separate sections below.
Name Next level names Description
CTLDEBUG sys/sysctl.h Debugging
CTLVFS sys/mount.h File system
CTLHW sys/sysctl.h Generic CPU, I/O
CTLKERN sys/sysctl.h High kernel limits
CTLMACHDEP sys/sysctl.h Machine dependent
CTLNET sys/socket.h Networking
CTLUSER sys/sysctl.h User-level
CTLVM sys/resources.h Virtual memory (struct loadavg)
CTLVM sys/vmmeter.h Virtual memory (struct vmtotal)
For example, the following retrieves the maximum number of processes
allowed in the system:
int mib[2], maxproc;
sizet len;
mib[0] = CTLKERN;
mib[1] = KERNMAXPROC;
len = sizeof(maxproc);
sysctl(mib, 2, &maxproc, &len, NUL, 0);
To retrieve the standard search path for the system utilities:
int mib[2];
sizet len;
char *p;
mib[0] = CTLUSER;
mib[1] = USERCSPATH;
sysctl(mib, 2, NUL, &len, NUL, 0);
p = malloc(len);
sysctl(mib, 2, p, &len, NUL, 0);
CTLDEBUG
The debugging variables vary from system to system. A debugging variable
may be added or deleted without need to recompile sysctl() to know about
it. Each time it runs, sysctl() gets the list of debugging variables
from the kernel and displays their current values. The system defines
twenty (struct ctldebug) variables named debug0 through debug19. They
are declared as separate variables so that they can be individually ini-
tialized at the location of their associated variable. The loader pre-
vents multiple use of the same variable by issuing errors if a variable
is initialized in more than one place. For example, to export the vari-
able dospecialcheck as a debugging variable, the following declaration
would be used:
int dospecialcheck = 1;
struct ctldebug debug5 = { "dospecialcheck", &dospecialcheck };
CTLVFS
A distinguished second level name, VFSGENERIC, is used to get general
information about all file systems. One of its third level identifiers
is VFSMAXTYPENUM that gives the highest valid file system type number.
Its other third level identifier is VFSCONF that returns configuration
information about the file system type given as a fourth level identifier
(see getvfsbyname(3) as an example of its use). The remaining second
level identifiers are the file system type number returned by a statfs(2)
call or from VFSCONF. The third level identifiers available for each
file system are given in the header file that defines the mount argument
structure for that file system.
CTLHW
The string and integer information available for the CTLHW level is
detailed below. The changeable column shows whether a process with
appropriate privilege may change the value.
Second level name Type Changeable
HWMACHINE string no
HWMODEL string no
HWNCPU integer no
HWBYTEORDER integer no
HWPHYSMEM integer no
HWUSERMEM integer no
HWPAGESIZE integer no
HWFLOATINGPOINT integer no
HWMACHINEARCH string no
HWMACHINE
The machine class.
HWMODEL
The machine model
HWNCPU
The number of cpus.
HWBYTEORDER
The byteorder (4,321, or 1,234).
HWPHYSMEM
The bytes of physical memory.
HWUSERMEM
The bytes of non-kernel memory.
HWPAGESIZE
The software page size.
HWFLOATINGPOINT
Nonzero if the floating point support is in hardware.
HWMACHINEARCH
The machine dependent architecture type.
CTLKERN
The string and integer information available for the CTLKERN level is
detailed below. The changeable column shows whether a process with
appropriate privilege may change the value. The types of data currently
available are process information, system vnodes, the open file entries,
routing table entries, virtual memory statistics, load average history,
and clock rate information.
Second level name Type Changeable
KERNARGMAX integer no
KERNBOTFILE string yes
KERNBOTIME struct timeval no
KERNCLOCKRATE struct clockinfo no
KERNFILE struct file no
KERNHOSTID integer yes
KERNHOSTNAME string yes
KERNJOBCONTROL integer no
KERNMAXFILES integer yes
KERNMAXFILESPERPROC integer yes
KERNMAXPROC integer no
KERNMAXPROCPERUID integer yes
KERNMAXVNODES integer yes
KERNGROUPS integer no
KERNISDOMAINAME string yes
KERNOSRELDATE integer no
KERNOSRELEASE string no
KERNOSREV integer no
KERNOSTYPE string no
KERNPOSIX1 integer no
KERNPROC struct proc no
KERNPROF node not applicable
KERNQUANTUM integer yes
KERNSAVEDIDS integer no
KERNSECURELVL integer raise only
KERNUPDATEINTERVAL integer no
KERNVERSION string no
KERNVNODE struct vnode no
KERNARGMAX
The maximum bytes of argument to execve(2).
KERNBOTFILE
The full pathname of the file from which the kernel was loaded.
KERNBOTIME
A struct timeval structure is returned. This structure contains
the time that the system was booted.
KERNCLOCKRATE
A struct clockinfo structure is returned. This structure con-
tains the clock, statistics clock and profiling clock frequen-
cies, the number of micro-seconds per hz tick and the skew rate.
KERNFILE
Return the entire file table. The returned data consists of a
single struct filehead followed by an array of struct file, whose
size depends on the current number of such objects in the system.
KERNHOSTID
Get or set the host id.
KERNHOSTNAME
Get or set the hostname.
KERNJOBCONTROL
Return 1 if job control is available on this system, otherwise 0.
KERNMAXFILES
The maximum number of files that may be open in the system.
KERNMAXFILESPERPROC
The maximum number of files that may be open for a single
process. This limit only applies to processes with an effective
uid of nonzero at the time of the open request. Files that have
already been opened are not affected if the limit or the effec-
tive uid is changed.
KERNMAXPROC
The maximum number of concurrent processes the system will allow.
KERNMAXPROCPERUID
The maximum number of concurrent processes the system will allow
for a single effective uid. This limit only applies to processes
with an effective uid of nonzero at the time of a fork request.
Processes that have already been started are not affected if the
limit is changed.
KERNMAXVNODES
The maximum number of vnodes available on the system.
KERNGROUPS
The maximum number of supplemental groups.
KERNISDOMAINAME
The name of the current YP/NIS domain.
KERNOSRELDATE
The kernel release version in the format MmmRxx, where M is the
major version, mm is the two digit minor version, R is 0 if
release branch, otherwise 1, and xx is updated when the available
APIs change.
The userland release version is available from ;
parse this file if you need to get the release version of the
currently installed userland.
KERNOSRELEASE
The system release string.
KERNOSREV
The system revision string.
KERNOSTYPE
The system type string.
KERNPOSIX1
The version of IE Std 1003.1 (``POSIX.1'') with which the sys-
tem attempts to comply.
KERNPROC
Return the entire process table, or a subset of it. An array of
pairs of struct proc followed by corresponding struct eproc
structures is returned, whose size depends on the current number
of such objects in the system. The third and fourth level names
are as follows:
Third level name Fourth level is:
KERNPROCAL None
KERNPROCPID A process ID
KERNPROCPGRP A process group
KERNPROCTY A tty device
KERNPROCUID A user ID
KERNPROCRUID A real user ID
If the third level name is KERNPROCARGS then the command line
argument array is returned in a flattened form, i.e., zero-termi-
nated arguments follow each other. The total size of array is
returned. It is also possible for a process to set its own
process title this way.
Third level name Fourth level is::
KERNPROCARGS A process ID
KERNPROF
Return profiling information about the kernel. If the kernel is
not compiled for profiling, attempts to retrieve any of the
KERNPROF values will fail with ENOENT. The third level names
for the string and integer profiling information is detailed
below. The changeable column shows whether a process with appro-
priate privilege may change the value.
Third level name Type Changeable
GPROFSTATE integer yes
GPROFCOUNT ushort[] yes
GPROFROMS ushort[] yes
GPROFTOS struct tostruct yes
GPROFGMONPARAM struct gmonparam no
The variables are as follows:
GPROFSTATE
Returns GMONPROFON or GMONPROFOF to show that pro-
filing is running or stopped.
GPROFCOUNT
Array of statistical program counter counts.
GPROFROMS
Array indexed by program counter of call-from points.
GPROFTOS
Array of struct tostruct describing destination of calls
and their counts.
GPROFGMONPARAM
Structure giving the sizes of the above arrays.
KERNQUANTUM
The maximum period of time, in microseconds, for which a process
is allowed to run without being preempted if other processes are
in the run queue.
KERNSAVEDIDS
Returns 1 if saved set-group and saved set-user ID is available.
KERNSECURELVL
The system security level. This level may be raised by processes
with appropriate privilege. It may not be lowered.
KERNVERSION
The system version string.
KERNVNODE
Return the entire vnode table. Note, the vnode table is not nec-
essarily a consistent snapshot of the system. The returned data
consists of an array whose size depends on the current number of
such objects in the system. Each element of the array contains
the kernel address of a vnode struct vnode * followed by the
vnode itself struct vnode.
CTLMACHDEP
The set of variables defined is architecture dependent. The following
variables are defined for the i386 architecture.
Second level name Type Changeable
CPUCONSDEV devt no
CPUADJKERNTZ int yes
CPUDISRTCSET int yes
CPUBOTINFO struct bootinfo no
CPUWALCLOCK int yes
CTLNET
The string and integer information available for the CTLNET level is
detailed below. The changeable column shows whether a process with
appropriate privilege may change the value.
Second level name Type Changeable
PFROUTE routing messages no
PFINET IPv4 values yes
PFINET6 IPv6 values yes
PFROUTE
Return the entire routing table or a subset of it. The data is
returned as a sequence of routing messages (see route(4) for the
header file, format and meaning). The length of each message is
contained in the message header.
The third level name is a protocol number, which is currently
always 0. The fourth level name is an address family, which may
be set to 0 to select all address families. The fifth and sixth
level names are as follows:
Fifth level name Sixth level is::
NETRTFLAGS rtflags
NETRTDUMP None
NETRTIFLIST 0 or ifindex
NETRTIFMALIST 0 or ifindex
The NETRTIFMALIST name returns information about multicast
group memberships on all interfaces if 0 is specified, or for the
interface specified by ifindex.
PFINET
Get or set various global information about the IPv4 (Internet
Protocol version 4). The third level name is the protocol. The
fourth level name is the variable name. The currently defined
protocols and names are:
Protocol Variable Type Changeable
icmp bmcastecho integer yes
icmp maskrepl integer yes
ip forwarding integer yes
ip redirect integer yes
ip ttl integer yes
udp checksum integer yes
The variables are as follows:
icmp.bmcastecho
Returns 1 if an ICMP echo request to a broadcast or mul-
ticast address is to be answered.
icmp.maskrepl
Returns 1 if ICMP network mask requests are to be
answered.
ip.forwarding
Returns 1 when IP forwarding is enabled for the host,
meaning that the host is acting as a router.
ip.redirect
Returns 1 when ICMP redirects may be sent by the host.
This option is ignored unless the host is routing IP
packets, and should normally be enabled on all systems.
ip.ttl The maximum time-to-live (hop count) value for an IP
packet sourced by the system. This value applies to nor-
mal transport protocols, not to ICMP.
udp.checksum
Returns 1 when UDP checksums are being computed and
checked. Disabling UDP checksums is strongly discour-
aged.
For variables net.inet.*.ipsec, please refer to ipsec(4).
PFINET6
Get or set various global information about the IPv6 (Internet
Protocol version 6). The third level name is the protocol. The
fourth level name is the variable name.
For variables net.inet6.* please refer to inet6(4). For vari-
ables net.inet6.*.ipsec6, please refer to ipsec(4).
CTLUSER
The string and integer information available for the CTLUSER level is
detailed below. The changeable column shows whether a process with
appropriate privilege may change the value.
Second level name Type Changeable
USERBCBASEMAX integer no
USERBCDIMAX integer no
USERBCSCALEMAX integer no
USERBCSTRINGMAX integer no
USERCOLWEIGHTSMAX integer no
USERCSPATH string no
USEREXPRNESTMAX integer no
USERLINEMAX integer no
USERPOSIX2CHARTERM integer no
USERPOSIX2CBIND integer no
USERPOSIX2CDEV integer no
USERPOSIX2FORTDEV integer no
USERPOSIX2FORTRUN integer no
USERPOSIX2LOCALEDEF integer no
USERPOSIX2SWDEV integer no
USERPOSIX2UPE integer no
USERPOSIX2VERSION integer no
USEREDUPMAX integer no
USERSTREAMAX integer no
USERTZNAMEMAX integer no
USERBCBASEMAX
The maximum ibase/obase values in the bc(1) utility.
USERBCDIMAX
The maximum array size in the bc(1) utility.
USERBCSCALEMAX
The maximum scale value in the bc(1) utility.
USERBCSTRINGMAX
The maximum string length in the bc(1) utility.
USERCOLWEIGHTSMAX
The maximum number of weights that can be assigned to any entry
of the LCOLATE order keyword in the locale definition file.
USERCSPATH
Return a value for the PATH environment variable that finds all
the standard utilities.
USEREXPRNESTMAX
The maximum number of expressions that can be nested within
parenthesis by the expr(1) utility.
USERLINEMAX
The maximum length in bytes of a text-processing utility's input
line.
USERPOSIX2CHARTERM
Return 1 if the system supports at least one terminal type capa-
ble of all operations described in IE Std 1003.2 (``POSIX.2''),
otherwise 0.
USERPOSIX2CBIND
Return 1 if the system's C-language development facilities sup-
port the C-Language Bindings Option, otherwise 0.
USERPOSIX2CDEV
Return 1 if the system supports the C-Language Development Utili-
ties Option, otherwise 0.
USERPOSIX2FORTDEV
Return 1 if the system supports the FORTRAN Development Utilities
Option, otherwise 0.
USERPOSIX2FORTRUN
Return 1 if the system supports the FORTRAN Runtime Utilities
Option, otherwise 0.
USERPOSIX2LOCALEDEF
Return 1 if the system supports the creation of locales, other-
wise 0.
USERPOSIX2SWDEV
Return 1 if the system supports the Software Development Utili-
ties Option, otherwise 0.
USERPOSIX2UPE
Return 1 if the system supports the User Portability Utilities
Option, otherwise 0.
USERPOSIX2VERSION
The version of IE Std 1003.2 (``POSIX.2'') with which the sys-
tem attempts to comply.
USEREDUPMAX
The maximum number of repeated occurrences of a regular expres-
sion permitted when using interval notation.
USERSTREAMAX
The minimum maximum number of streams that a process may have
open at any one time.
USERTZNAMEMAX
The minimum maximum number of types supported for the name of a
timezone.
CTLVM
The string and integer information available for the CTLVM level is
detailed below. The changeable column shows whether a process with
appropriate privilege may change the value.
Second level name Type Changeable
VMLOADAVG struct loadavg no
VMETER struct vmtotal no
VMPAGEOUTALGORITHM integer yes
VMSWAPINGENABLED integer maybe
VMVCACHEMAX integer yes
VMVCACHEMIN integer yes
VMVFREMIN integer yes
VMVFRERESERVED integer yes
VMVFRETARGET integer yes
VMVINACTIVETARGET integer yes
VMVPAGEOUTFREMIN integer yes
VMLOADAVG
Return the load average history. The returned data consists of a
struct loadavg.
VMETER
Return the system wide virtual memory statistics. The returned
data consists of a struct vmtotal.
VMPAGEOUTALGORITHM
0 if the statistics-based page management algorithm is in use or
1 if the near-LRU algorithm is in use.
VMSWAPINGENABLED
1 if process swapping is enabled or 0 if disabled. This variable
is permanently set to 0 if the kernel was built with swapping
disabled.
VMVCACHEMAX
Maximum desired size of the cache queue.
VMVCACHEMIN
Minimum desired size of the cache queue. If the cache queue size
falls very far below this value, the pageout daemon is awakened.
VMVFREMIN
Minimum amount of memory (cache memory plus free memory) required
to be available before a process waiting on memory will be awak-
ened.
VMVFRERESERVED
Processes will awaken the pageout daemon and wait for memory if
the number of free and cached pages drops below this value.
VMVFRETARGET
The total amount of free memory (including cache memory) that the
pageout daemon tries to maintain.
VMVINACTIVETARGET
The desired number of inactive pages that the pageout daemon
should achieve when it runs. Inactive pages can be quickly
inserted into process address space when needed.
VMVPAGEOUTFREMIN
If the amount of free and cache memory falls below this value,
the pageout daemon will enter "memory conserving mode" to avoid
deadlock.
RETURN VALUES
Upon successful completion, the value 0 is returned; otherwise the
value -1 is returned and the global variable errno is set to indicate the
error.
ERORS
The following errors may be reported:
[EFAULT] The buffer name, oldp, newp, or length pointer oldlenp
contains an invalid address.
[EINVAL] The name array is less than two or greater than
CTLMAXNAME.
[EINVAL] A non-null newp is given and its specified length in
newlen is too large or too small.
[ENOMEM] The length pointed to by oldlenp is too short to hold
the requested value.
[ENOMEM] The smaller of either the length pointed to by oldlenp
or the estimated size of the returned data exceeds the
system limit on locked memory.
[ENOMEM] Locking the buffer oldp, or a portion of the buffer if
the estimated size of the data to be returned is
smaller, would cause the process to exceed its per-
process locked memory limit.
[ENOTDIR] The name array specifies an intermediate rather than
terminal name.
[EISDIR] The name array specifies a terminal name, but the
actual name is not terminal.
[ENOENT] The name array specifies a value that is unknown.
[EPERM] An attempt is made to set a read-only value.
[EPERM] A process without appropriate privilege attempts to
set a value.
FILES
definitions for top level identifiers, second level
kernel and hardware identifiers, and user level
identifiers
definitions for second level network identifiers
definitions for third level profiling identifiers
definitions for second level virtual memory identi-
fiers
definitions for third level IPv4/IPv6 identifiers
and fourth level IPv4/v6 identifiers
definitions for fourth level ICMP identifiers
definitions for fourth level ICMPv6 identifiers
definitions for fourth level UDP identifiers
SEE ALSO
sysconf(3), sysctl(8)
HISTORY
The sysctl() function first appeared in 4.4BSD.
BSD January 23, 2001 BSD
|