---
name: "sysctl"
section: "3"
platform: "macos"
package: "Libc"
version: "1752.120.2"
release: "macOS 26.5"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/sysctl/3"
---

# sysctl(3)

get or set system information

## NAME

**sysctl**, **sysctlbyname**, **sysctlnametomib** – get or set system information

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<sys/types.h\>**

**#include \<sys/sysctl.h\>**

*int*\

**sysctl**(*int \*name*, *u\_int namelen*, *void \*oldp*, *size\_t \*oldlenp*, *void \*newp*, *size\_t newlen*);

*int*\

**sysctlbyname**(*const char \*name*, *void \*oldp*, *size\_t \*oldlenp*, *void \*newp*, *size\_t newlen*);

*int*\

**sysctlnametomib**(*const char \*name*, *int \*mibp*, *size\_t \*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)](/sysctl/8.md) 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 ASCII 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 NULL.

The size of the available data can be determined by calling **sysctl**() with the NULL 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 NULL and *newlen* set to 0.

The **sysctlnametomib**() function accepts an ASCII representation of the name, looks up the integer name vector, and returns the numeric representation 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 successful call. The resulting *mib* and *size* may be used in subsequent **sysctl**() calls to get the data associated with the requested ASCII name. This interface is intended for use by applications that want to repeatedly request the same variable (the **sysctl**() function runs in about a third the time as the same request made via the **sysctlbyname**() function).

The top level names are defined with a CTL\_ prefix in ⟨sys/sysctl.h⟩, 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 |
| CTL\_DEBUG | sys/sysctl.h | Debugging |
| CTL\_VFS | sys/mount.h | File system |
| CTL\_HW | sys/sysctl.h | Generic CPU, I/O |
| CTL\_KERN | sys/sysctl.h | High kernel limits |
| CTL\_MACHDEP | sys/sysctl.h | Machine dependent |
| CTL\_NET | sys/socket.h | Networking |
| CTL\_USER | sys/sysctl.h | User-level |
| CTL\_VM | sys/resources.h | Virtual memory (struct loadavg) |

For example, the following retrieves the maximum number of processes allowed in the system:

```
int maxproc;
size_t len = sizeof(maxproc);
sysctlbyname("kern.maxproc", &maxproc, &len, NULL, 0);
```

To retrieve the standard search path for the system utilities:

```
char *p;
size_t len;
sysctlbyname("user.cs_path", NULL, &len, NULL, 0);
p = malloc(len);
sysctlbyname("user.cs_path", p, &len, NULL, 0);
```

### CTL\_VFS

A distinguished second level name, VFS\_GENERIC, is used to get general information about all file systems. One of its third level identifiers is VFS\_MAXTYPENUM that gives the highest valid file system type number. Its other third level identifier is VFS\_CONF that returns configuration information about the file system type given as a fourth level identifier (see [getvfsbyname(3)](/getvfsbyname/3.md) as an example of its use). The remaining second level identifiers are the file system type number returned by a [statfs(2)](/statfs/2.md) call or from VFS\_CONF. 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.

### CTL\_HW

The string and integer information available for the CTL\_HW level is detailed below. The changeable column shows whether a process with appropriate privilege may change the value.

| | | |
| --- | --- | --- |
| **Name** | Type | Changeable |
| hw.activecpu | int32\_t | no |
| hw.byteorder | int32\_t | no |
| hw.cacheconfig | uint64\_t\[\] | no |
| hw.cachelinesize | int64\_t | no |
| hw.cachesize | uint64\_t\[\] | no |
| hw.cpu64bit\_capable | int32\_t | no |
| hw.cpufamily | uint32\_t | no |
| hw.cpufrequency | int64\_t | no |
| hw.cpufrequency\_max | int64\_t | no |
| hw.cpufrequency\_min | int64\_t | no |
| hw.cpusubtype | int32\_t | no |
| hw.cputhreadtype | int32\_t | no |
| hw.cputype | int32\_t | no |
| hw.l1dcachesize | int64\_t | no |
| hw.l1icachesize | int64\_t | no |
| hw.l2cachesize | int64\_t | no |
| hw.l3cachesize | int64\_t | no |
| hw.logicalcpu | int32\_t | no |
| hw.logicalcpu\_max | int32\_t | no |
| hw.machine | char\[\] | no |
| hw.memsize | int64\_t | no |
| hw.model | char\[\] | no |
| hw.ncpu | int32\_t | no |
| hw.packages | int32\_t | no |
| hw.pagesize | int64\_t | no |
| hw.physicalcpu | int32\_t | no |
| hw.physicalcpu\_max | int32\_t | no |
| hw.tbfrequency | int64\_t | no |

- hw.byteorder

  The byte order (4321 or 1234).

- hw.model

  The machine model.

- hw.ncpu

  The number of cpus. This attribute is deprecated and it is recommended that *hw.logicalcpu*, *hw.logicalcpu\_max*, *hw.physicalcpu*, or *hw.physicalcpu\_max* be used instead.

- hw.logicalcpu

  The number of logical processors available in the current power management mode.

- hw.logicalcpu\_max

  The maximum number of logical processors that could be available this boot.

- hw.physicalcpu

  The number of physical processors available in the current power management mode.

- hw.physicalcpu\_max

  The maximum number of physical processors that could be available this boot.

- hw.pagesize

  The software page size in bytes.

### CTL\_KERN

The string and integer information available for the CTL\_KERN 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.

| | | |
| --- | --- | --- |
| **Name** | Type | Changeable |
| kern.argmax | int32\_t | no |
| kern.bootargs | char\[\] | no |
| kern.boottime | struct timeval | no |
| kern.check\_openevt | int32\_t | yes |
| kern.clockrate | struct clockinfo | no |
| kern.coredump | int32\_t | yes |
| kern.corefile | char\[\] | yes |
| kern.flush\_cache\_on\_write | int32\_t | yes |
| kern.hostid | int32\_t | yes |
| kern.hostname | char\[\] | yes |
| kern.job\_control | int32\_t | no |
| kern.maxfiles | int32\_t | yes |
| kern.maxfilesperproc | int32\_t | yes |
| kern.maxnbuf | int32\_t | yes |
| kern.maxproc | int32\_t | yes |
| kern.maxprocperuid | int32\_t | yes |
| kern.maxvnodes | int32\_t | yes |
| kern.msgbuf | int32\_t | yes |
| kern.nbuf | int32\_t | no |
| kern.netboot | int32\_t | no |
| kern.ngroups | int32\_t | no |
| kern.nisdomainname | char\[\] | yes |
| kern.num\_files | int32\_t | no |
| kern.num\_tasks | int32\_t | no |
| kern.num\_taskthreads | int32\_t | no |
| kern.num\_threads | int32\_t | no |
| kern.num\_vnodes | int32\_t | no |
| kern.nx | int32\_t | yes |
| kern.osrelease | char\[\] | no |
| kern.osrevision | int32\_t | no |
| kern.ostype | char\[\] | no |
| kern.osversion | char\[\] | yes |
| kern.posix1version | int32\_t | no |
| kern.procname | char\[\] | yes |
| kern.safeboot | int32\_t | no |
| kern.saved\_ids | int32\_t | no |
| kern.secure\_kernel | int32\_t | no |
| kern.securelevel | int32\_t | yes |
| kern.singleuser | int32\_t | no |
| kern.sleeptime | struct timeval | no |
| kern.slide | int32\_t | no |
| kern.stack\_depth\_max | int32\_t | no |
| kern.stack\_size | int32\_t | no |
| kern.sugid\_coredump | int32\_t | yes |
| kern.sugid\_scripts | int32\_t | yes |
| kern.symfile | char\[\] | no |
| kern.usrstack | int32\_t | no |
| kern.usrstack64 | int64\_t | no |
| kern.uuid | char\[\] | no |
| kern.version | char\[\] | no |
| kern.waketime | struct timeval | no |

- kern.argmax

  The maximum bytes of argument to [execve(2)](/execve/2.md).

- kern.boottime

  A *struct* *timeval* structure is returned. This structure contains the time that the system was booted.

- kern.clockrate

  A *struct* *clockinfo* structure is returned. This structure contains the clock, statistics clock and profiling clock frequencies, the number of micro-seconds per hz tick and the skew rate.

- kern.hostid

  Get or set the host id.

- kern.hostname

  Get or set the hostname.

- kern.job\_control

  Return 1 if job control is available on this system, otherwise 0.

- kern.maxfiles

  The maximum number of files that may be open in the system.

- kern.maxfilesperproc

  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 effective uid is changed.

- kern.maxproc

  The maximum number of concurrent processes the system will allow.

- kern.maxprocperuid

  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.

- kern.maxvnodes

  The maximum number of vnodes available on the system.

- kern.ngroups

  The maximum number of supplemental groups.

- kern.nisdomainname

  The name of the current YP/NIS domain.

- kern.osrelease

  The system release string.

- kern.osrevision

  The system revision number.

- kern.ostype

  The system type string.

- kern.posix1version

  The version of IEEE Std 1003.1 (“POSIX.1”) with which the system attempts to comply.

- kern.saved\_ids

  Returns 1 if saved set-group and saved set-user ID is available.

- kern.securelevel

  The system security level. This level may be raised by processes with appropriate privilege. It may not be lowered.

- kern.version

  The system version string.

### CTL\_MACHDEP

The set of variables defined is architecture dependent. The following variables are defined for the i386 architecture.

| | | |
| --- | --- | --- |
| **Name** | Type | Changeable |
| machdep.cpu.address\_bits.physical | int32\_t | no |
| machdep.cpu.address\_bits.virtual | int32\_t | no |
| machdep.cpu.brand | int32\_t | no |
| machdep.cpu.brand\_string | char\[\] | no |
| machdep.cpu.cache.L2\_associativity | int32\_t | no |
| machdep.cpu.cache.linesize | int32\_t | no |
| machdep.cpu.cache.size | int32\_t | no |
| machdep.cpu.core\_count | int32\_t | no |
| machdep.cpu.cores\_per\_package | int32\_t | no |
| machdep.cpu.extfamily | int32\_t | no |
| machdep.cpu.extfeature\_bits | int64\_t | no |
| machdep.cpu.extfeatures | char\[\] | no |
| machdep.cpu.extmodel | int32\_t | no |
| machdep.cpu.family | int32\_t | no |
| machdep.cpu.feature\_bits | int64\_t | no |
| machdep.cpu.features | char\[\] | no |
| machdep.cpu.leaf7\_feature\_bits | uint32\_t | no |
| machdep.cpu.leaf7\_features | char\[\] | no |
| machdep.cpu.logical\_per\_package | int32\_t | no |
| machdep.cpu.max\_basic | uint32\_t | no |
| machdep.cpu.max\_ext | uint32\_t | no |
| machdep.cpu.microcode\_version | int32\_t | no |
| machdep.cpu.model | int32\_t | no |
| machdep.cpu.processor\_flag | int32\_t | no |
| machdep.cpu.signature | int32\_t | no |
| machdep.cpu.stepping | int32\_t | no |
| machdep.cpu.thread\_count | int32\_t | no |
| machdep.cpu.tlb.data.large | int32\_t | no |
| machdep.cpu.tlb.data.large\_level1 | int32\_t | no |
| machdep.cpu.tlb.data.small | int32\_t | no |
| machdep.cpu.tlb.data.small\_level1 | int32\_t | no |
| machdep.cpu.tlb.inst.large | int32\_t | no |
| machdep.cpu.tlb.inst.small | int32\_t | no |
| machdep.cpu.tlb.shared | int32\_t | no |
| machdep.cpu.ucupdate | int32\_t | yes |
| machdep.cpu.vendor | char\[\] | no |
| machdep.cpu.xsave.extended\_state | uint32\_t | no |
| machdep.tsc.deep\_idle\_rebase | uint32\_t | yes |
| machdep.tsc.frequency | int64\_t | no |
| machdep.tsc.nanotime.generation | uint32\_t | no |
| machdep.tsc.nanotime.shift | uint32\_t | no |

### CTL\_NET

The string and integer information available for the CTL\_NET level is detailed below. The changeable column shows whether a process with appropriate privilege may change the value.

| | | |
| --- | --- | --- |
| **Second level name** | Type | Changeable |
| PF\_ROUTE | routing messages | no |
| PF\_INET | IPv4 values | yes |
| PF\_INET6 | IPv6 values | yes |

- PF\_ROUTE

  Return the entire routing table or a subset of it. The data is returned as a sequence of routing messages (see [route(4)](/route/4.md) 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, sixth, and seventh level names are as follows:

  | | |
  | --- | --- |
  | **Fifth level Sixth level** | **Seventh level** |
  | NET\_RT\_FLAGS rtflags | None |
  | NET\_RT\_DUMP None | None or fib number |
  | NET\_RT\_IFLIST 0 or if\_index | None |
  | NET\_RT\_IFMALIST 0 or if\_index | None |
  | NET\_RT\_IFLISTL 0 or if\_index | None |

- PF\_INET

  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 |

- PF\_INET6

  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)](/inet6/4.md). For variables net.inet6.\*.ipsec6, please refer to [ipsec(4)](/ipsec/4.md).

### CTL\_USER

The string and integer information available for the CTL\_USER level is detailed below. The changeable column shows whether a process with appropriate privilege may change the value.

| | | |
| --- | --- | --- |
| **Name** | Type | Changeable |
| user.bc\_base\_max | int32\_t | no |
| user.bc\_dim\_max | int32\_t | no |
| user.bc\_scale\_max | int32\_t | no |
| user.bc\_string\_max | int32\_t | no |
| user.coll\_weights\_max | int32\_t | no |
| user.cs\_path | char\[\] | no |
| user.expr\_nest\_max | int32\_t | no |
| user.line\_max | int32\_t | no |
| user.posix2\_c\_bind | int32\_t | no |
| user.posix2\_c\_dev | int32\_t | no |
| user.posix2\_char\_term | int32\_t | no |
| user.posix2\_fort\_dev | int32\_t | no |
| user.posix2\_fort\_run | int32\_t | no |
| user.posix2\_localedef | int32\_t | no |
| user.posix2\_sw\_dev | int32\_t | no |
| user.posix2\_upe | int32\_t | no |
| user.posix2\_version | int32\_t | no |
| user.re\_dup\_max | int32\_t | no |
| user.stream\_max | int32\_t | no |
| user.tzname\_max | int32\_t | no |

- user.bc\_base\_max

  The maximum ibase/obase values in the [bc(1)](/bc/1.md) utility.

- user.bc\_dim\_max

  The maximum array size in the [bc(1)](/bc/1.md) utility.

- user.bc\_scale\_max

  The maximum scale value in the [bc(1)](/bc/1.md) utility.

- user.bc\_string\_max

  The maximum string length in the [bc(1)](/bc/1.md) utility.

- user.coll\_weights\_max

  The maximum number of weights that can be assigned to any entry of the LC\_COLLATE order keyword in the locale definition file.

- user.cs\_path

  Return a value for the PATH environment variable that finds all the standard utilities.

- user.expr\_nest\_max

  The maximum number of expressions that can be nested within parenthesis by the [expr(1)](/expr/1.md) utility.

- user.line\_max

  The maximum length in bytes of a text-processing utility's input line.

- user.posix2\_c\_bind

  Return 1 if the system's C-language development facilities support the C-Language Bindings Option, otherwise 0.

- user.posix2\_c\_dev

  Return 1 if the system supports the C-Language Development Utilities Option, otherwise 0.

- user.posix2\_char\_term

  Return 1 if the system supports at least one terminal type capable of all operations described in IEEE Std 1003.2 (“POSIX.2”), otherwise 0.

- user.posix2\_fort\_dev

  Return 1 if the system supports the FORTRAN Development Utilities Option, otherwise 0.

- user.posix2\_fort\_run

  Return 1 if the system supports the FORTRAN Runtime Utilities Option, otherwise 0.

- user.posix2\_localedef

  Return 1 if the system supports the creation of locales, otherwise 0.

- user.posix2\_sw\_dev

  Return 1 if the system supports the Software Development Utilities Option, otherwise 0.

- user.posix2\_upe

  Return 1 if the system supports the User Portability Utilities Option, otherwise 0.

- user.posix2\_version

  The version of IEEE Std 1003.2 (“POSIX.2”) with which the system attempts to comply.

- user.re\_dup\_max

  The maximum number of repeated occurrences of a regular expression permitted when using interval notation.

- user.stream\_max

  The minimum maximum number of streams that a process may have open at any one time.

- user.tzname\_max

  The minimum maximum number of types supported for the name of a timezone.

### CTL\_VM

The string and integer information available for the CTL\_VM level is detailed below. The changeable column shows whether a process with appropriate privilege may change the value.

| | | |
| --- | --- | --- |
| **Name** | Type | Changeable |
| vm.loadavg | struct loadavg | no |
| vm.swapusage | struct xsw\_usage | no |

- vm.loadavg

  Return the load average history. The returned data consists of a *struct* *loadavg*.

## RETURN VALUES

The **sysctl()** function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

## FILES

- In sys/sysctl.h

  definitions for top level identifiers, second level kernel and hardware identifiers, and user level identifiers

- In sys/socket.h

  definitions for second level network identifiers

- In netinet/in.h

  definitions for third level IPv4/IPv6 identifiers and fourth level IPv4/v6 identifiers

- In netinet/icmp\_var.h

  definitions for fourth level ICMP identifiers

- In netinet/icmp6.h

  definitions for fourth level ICMPv6 identifiers

- In netinet/udp\_var.h

  definitions for fourth level UDP identifiers

## ERRORS

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 CTL\_MAXNAME.

- \[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.

## SEE ALSO

[confstr(3)](/confstr/3.md), [sysconf(3)](/sysconf/3.md), [sysctl(8)](/sysctl/8.md)

## HISTORY

The **sysctl**() function first appeared in 4.4BSD.
