Updated: April 19, 2023 |
Get or set information about the socket manager
#include <sys/param.h> #include <sys/sysctl.h> int sysctl( const int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen ); int sysctlbyname( const char *sname, void *oldp, size_t *oldlenp, void * newp, size_t newlen );
The sysctl() function retrieves information about the socket manager and allows processes with appropriate privileges to set this information. The sysctlbyname() function is similar, but you specify the name of the variable as a string instead of as an array of integers.
The data available from these functions consists of integers and tables. You can also get or set data using the sysctl utility at the command line.
The state is described using a Management Information Base (MIB) style name, specified in name as a namelen length array of integers, or in sname as a string, depending on which function you call.
The sysctl() or sysctlbyname() function copies the information for the specified variable 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. If the amount of data available is greater than the size of the buffer supplied, the call delivers as much data as fits in the buffer provided and returns with the error code ENOMEM. If you don't need the old value, you can set oldp and oldlenp to NULL.
You can determine the size of the available data by calling sysctl() or sysctlbyname() with a NULL parameter for oldp. The function stores the size of the available data 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 specify a new value, set newp to point to a buffer of length newlen from which the requested value is to be taken. If you're not setting a new value, set newp to NULL and newlen to 0.
The top-level names are defined with a CTL_ prefix in <sys/sysctl.h>. The next and subsequent levels down are found in the following header files:
This header file: | Contains definitions for: |
---|---|
<sys/sysctl.h> | Top-level identifiers |
<sys/socket.h> | Second-level network identifiers |
<netinet/in.h> | Third-level Internet identifiers and fourth-level IP identifiers |
<netinet/icmp_var.h> | Fourth-level ICMP identifiers |
<netinet/tcp_var.h> | Fourth-level TCP identifiers |
<netinet/udp_var.h> | Fourth-level UDP identifiers |
The <sys/sysctl.h> file defines the top-level identifiers like this:
#define CTL_UNSPEC 0 /* unused */ #define CTL_KERN 1 /* "high kernel": proc, limits */ #define CTL_VM 2 /* virtual memory */ #define CTL_VFS 3 /* file system, mount type is next */ #define CTL_NET 4 /* network, see socket.h */ #define CTL_DEBUG 5 /* debugging parameters */ #define CTL_HW 6 /* generic cpu/io */ #define CTL_MACHDEP 7 /* machine dependent */ #define CTL_USER 8 /* user-level */ #define CTL_DDB 9 /* in-kernel debugger */ #define CTL_PROC 10 /* per-proc attr */ #define CTL_VENDOR 11 /* vendor-specific data */ #define CTL_EMUL 12 /* emulation-specific data */ #define CTL_MAXID 13 /* number of valid top-level ids */ #define CTL_NAMES { \ { 0, 0 }, \ { "kern", CTLTYPE_NODE }, \ { "vm", CTLTYPE_NODE }, \ { "vfs", CTLTYPE_NODE }, \ { "net", CTLTYPE_NODE }, \ { "debug", CTLTYPE_NODE }, \ { "hw", CTLTYPE_NODE }, \ { "machdep", CTLTYPE_NODE }, \ { "user", CTLTYPE_NODE }, \ { "ddb", CTLTYPE_NODE }, \ { "proc", CTLTYPE_NODE }, \ { "vendor", CTLTYPE_NODE }, \ { "emul", CTLTYPE_NODE }, \ }
Note how the CTL_* definitions correspond to the strings defined below. Let's look at how you'd build the array of integers from a few strings:
Entry | Value | See: |
---|---|---|
0 | CTL_KERN | <sys/sysctl.h> |
1 | KERN_CLOCKRATE | <sys/sysctl.h> |
Entry | Value | See: |
---|---|---|
0 | CTL_KERN | <sys/sysctl.h> |
1 | KERN_MBUF | <sys/sysctl.h> |
2 | MBUF_MBLOWAT | <sys/mbuf.h> |
The entries in the name array would be as follows:
Entry | Value | See: |
---|---|---|
0 | CTL_NET | <sys/sysctl.h> |
1 | AF_INET | <sys/socket.h> |
2 | IPPROTO_TCP | <netinet/in.h> |
3 | TCPCTL_MSSDFLT | <netinet/in_offload.h> |
The following code fragment checks whether the UDP packets checksum is enabled:
int mib[5], val; size_t len; mib[0] = CTL_NET; mib[1] = AF_INET; mib[2] = IPPROTO_UDP; mib[3] = UDPCTL_CHECKSUM; len = sizeof(val); sysctl(mib, 4, &val, &len, NULL, 0);
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |