sysctlbyname
Gets or sets information about the system and environment.
Declaration
int sysctlbyname(const char *, void *, size_t *, void *, size_t);Parameters
- name:
The ASCII name of the requested attribute. To obtain a list of attribute names for the current system, run
sysctl -afrom Terminal. - oldp:
A pointer to a buffer that receives the requested data. Specify the size of this buffer using the
oldlenpparameter. The function copies the requested data into this buffer as space allows. If the buffer is too small to hold all the data, the function updates theoldlenpparameter with the required size.Specify
NULLif you don’t want to get the attribute’s value. For example, set this parameter toNULLand specify a valid parameter inoldlenpto get the size of the data. - oldlenp:
A variable that contains the number of bytes in the buffer in the
oldpparameter. If the amount of data is greater than the value in this parameter, the function updates this parameter to the required size and returnsENOMEM. (The function doesn’t modify the value if it’s larger than or equal to the amount of available data).Specify
NULLif you don’t want to get the attribute’s value. - newp:
A pointer to a buffer that contains new data to assign to the attribute. The function copies the data from this buffer to the attribute. You must specify the size of this buffer using the
newlenparameter. SpecifyNULLif you don’t want to set the attribute’s value.A process must have the root-level privileges to set system information.
- newlen:
A variable that contains the size of the buffer in the
newpparameter, in bytes. Specify0if you don’t want to set the attribute’s value.
Return Value
0 on success, or an error code that indicates a problem occurred. Possible error codes include EFAULT, EINVAL, ENOMEM, ENOTDIR, EISDIR, ENOENT, and EPERM.
Discussion
This function offers a programmatic alternative to the sysctl command-line tool. Use it to get or set relevant system information, and to make decisions dynamically based on the current system information. For example, you might use the hw.cachelinesize attribute to align data in your data structures. Making decisions dynamically is especially important for universal binaries that run on Apple silicon or Intel-based Mac computers.
The following example shows how to retrieve the cache line size using this function.
int64_t cacheLineSize() {
int64_t ret = 0;
size_t size = sizeof(ret);
if (sysctlbyname("hw.cachelinesize", &ret, &size, NULL, 0) == -1) {
NSLog(@"cacheLineSize failed with error: %d", errno);
return -1;
}
return ret;
}Topics
Parameters
See Also
sysctl
sysctl_handle_intsysctl_handle_int2quadsysctl_handle_longsysctl_handle_opaquesysctl_handle_quadsysctl_handle_stringsysctl_io_numbersysctl_io_opaquesysctl_io_stringsysctl_register_oidsysctl_unregister_oidsysctl__childrensysctl__debug_childrensysctl__hw_childrensysctl__kern_childrensysctl__machdep_childrensysctl__net_childrensysctl__sysctl_childrensysctl__user_childrensysctl__vfs_childrensysctl__vm_children