Node:System Parameters, Previous:Filesystem Handling, Up:System Management
This section describes the sysctl
function, which gets and sets
a variety of system parameters.
The symbols used in this section are declared in the file sysctl.h
.
int sysctl (int *names, int nlen, void *oldval, | Function |
size_t *oldlenp, void *newval, size_t newlen)
sysctl gets or sets a specified system parameter. There are so
many of these parameters that it is not practical to list them all here,
but here are some examples:
syslog is concerned are arranged
in a hierarchical structure like a hierarchical filesystem. To identify
a particular parameter, you specify a path through the structure in a
way analogous to specifying the pathname of a file. Each component of
the path is specified by an integer and each of these integers has a
macro defined for it by sysctl.h . names is the path, in
the form of an array of integers. Each component of the path is one
element of the array, in order. nlen is the number of components
in the path.
For example, the first component of the path for all the paging
parameters is the value CTL_VM . For the free page thresholds, the
second component of the path is VM_FREEPG . So to get the free
page threshold values, make names an array containing the two
elements CTL_VM and VM_FREEPG and make nlen = 2.
The format of the value of a parameter depends on the parameter.
Sometimes it is an integer; sometimes it is an ASCII string; sometimes
it is an elaborate structure. In the case of the free page thresholds
used in the example above, the parameter value is a structure containing
several integers.
In any case, you identify a place to return the parameter's value with
oldval and specify the amount of storage available at that
location as *oldlenp. *oldlenp does double duty because it
is also the output location that contains the actual length of the
returned value.
If you don't want the parameter value returned, specify a null pointer
for oldval.
To set the parameter, specify the address and length of the new value
as newval and newlen. If you don't want to set the parameter,
specify a null pointer as newval.
If you get and set a parameter in the same sysctl call, the value
returned is the value of the parameter before it was set.
Each system parameter has a set of permissions similar to the
permissions for a file (including the permissions on directories in its
path) that determine whether you may get or set it. For the purposes of
these permissions, every parameter is considered to be owned by the
superuser and Group 0 so processes with that effective uid or gid may
have more access to system parameters. Unlike with files, the superuser
does not invariably have full permission to all system parameters, because
some of them are designed not to be changed ever.
sysctl returns a zero return value if it succeeds. Otherwise, it
returns -1 and sets errno appropriately. Besides the
failures that apply to all system calls, the following are the
errno codes for all possible failures:
|
proc
filesystem, you can get
and set most of the same parameters by reading and writing to files in
the sys
directory of the proc
filesystem. In the sys
directory, the directory structure represents the hierarchical structure
of the parameters. E.g. you can display the free page thresholds with
cat /proc/sys/vm/freepagesSome more traditional and more widely available, though less general, GNU C library functions for getting and setting some of the same system parameters are:
getdomainname
, setdomainname
gethostname
, sethostname
(See Host Identification.)
uname
(See Platform Type.)
bdflush