xref: /original-bsd/lib/libc/gen/sysctl.3 (revision 03bd62d7)
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.2 (Berkeley) 03/04/93
7.\"
8.Dd ""
9.Dt SYSCTL 2
10.Os
11.Sh NAME
12.Nm sysctl
13.Nd get or set kernel state
14.Sh SYNOPSIS
15.Fd #include <sys/sysctl.h>
16.Ft int
17.Fn sysctl "int *name" "u_int namelen" "void *old" "size_t *oldlenp" "void *new" "size_t newlen"
18.Sh DESCRIPTION
19The
20.Fn sysctl
21function retrieves kernel state and allows processes with
22appropriate privilege to set kernel state.
23The state to be set is described using a ``MIB'' style name,
24described in a
25.Fa namelen
26length array of integers pointed to by
27.Fa name .
28The top level names are defined with a CTL_ prefix in
29.Pa <sys/sysctl.h> .
30The next levels down are found in the files given in the ``FILES''
31section of this manual page.
32.Pp
33To retrieve a value,
34.Fa old
35is set to point to a buffer
36into which the requested value is to be placed.
37The length of the buffer is given in the location pointed to by
38.Fa oldlenp .
39The size of the returned value is put in the location pointed to by
40.Fa oldlenp .
41The size of the requested value can be determined by calling
42.Fn sysctl
43with a NULL parameter for
44.Fa old ;
45the size of the value will be returned in the location pointed to by
46.Fa oldlenp .
47If the old value is not desired,
48.Fa old
49and
50.Fa oldlenp
51can be set to NULL.
52.Pp
53To set a new value,
54.Fa new
55is set to point to a buffer of length
56.Fa newlen
57from which the requested value is to be taken.
58If the setting of a new value is not desired,
59.Fa new
60should be set to NULL and
61.Fa newlen
62set to 0.
63.Pp
64For example, to retrieve the maximum number of processes allowed
65in the system, one would use the follow request:
66.sp
67.Bd -literal -offset indent -compact
68int name[2], maxproc;
69size_t len;
70
71name[0] = CTL_KERN;
72name[1] = KERN_MAXPROC;
73len = sizeof(maxproc);
74sysctl(name, 2, &maxproc, &len, NULL, 0);
75.Ed
76.Sh RETURN VALUES
77If the call to
78.Fn sysctl
79is successful, the length of the old value is returned.
80Otherwise \-1 is returned and
81.Va errno
82is set appropriately.
83.Sh ERRORS
84The following error may be reported:
85.Bl -tag -width Er
86.It Bq Er EFAULT
87The buffer
88.Fa name ,
89.Fa old ,
90.Fa new ,
91or length pointer
92.Fa oldlenp
93contains an invalid address.
94.It Bq Er EINVAL
95The
96.Fa name
97array is less than two or greater than CTL_MAXNAME.
98.It Bq Er EINVAL
99A non-null
100.Fa new
101is given and its specified length in
102.Fa newlen
103is too large or too small.
104.It Bq Er ENOMEM
105The length pointed to by
106.Fa oldlenp
107is too short to hold the requested value.
108.It Bq Er ENOTDIR
109The
110.Fa name
111array specifies an intermediate rather than terminal name.
112.It Bq Er EOPNOTSUPP
113The
114.Fa name
115array specifies a value that is unknown.
116.It Bq Er EPERM
117An attempt is made to set a read-only value.
118.It Bq Er EPERM
119A process without appropriate privilege attempts to set a value.
120.El
121.Sh FILES
122.Bl -tag -width <vm/vm_param.h> -compact
123.It Pa <sys/sysctl.h>
124definitions for top level identifiers and second level kernel
125and hardware identifiers
126.It Pa <sys/socket.h>
127definitions for second level network identifiers
128.It Pa <vm/vm_param.h>
129definitions for second level virtual memory identifiers
130.El
131.Sh SEE ALSO
132.Xr sysctl 8
133.Sh HISTORY
134.Fn sysctl
135first appeared in 4.4BSD.
136