---
name: "cap_nametomib"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/cap_nametomib"
---

# cap\_nametomib(3)

library for getting or setting system information in capability mode

## NAME

**cap\_sysctl** – library for getting or setting system information in capability mode

## LIBRARY

library “libcap\_sysctl”

## SYNOPSIS

**#include \<libcasper.h\>**

**#include \<casper/cap\_sysctl.h\>**

*int*\

**cap\_sysctl**(*cap\_channel\_t \*chan*, *const int \*name*, *u\_int namelen*, *void \*oldp*, *size\_t \*oldlenp*, *const void \*newp*, *size\_t newlen*);

*int*\

**cap\_sysctlbyname**(*cap\_channel\_t \*chan*, *const char \*name*, *void \*oldp*, *size\_t \*oldlenp*, *const void \*newp*, *size\_t newlen*);

*int*\

**cap\_sysctlnametomib**(*cap\_channel\_t \*chan*, *const char \*name*, *int \*mibp*, *size\_t \*sizep*);

*cap\_sysctl\_limit\_t* *\**\

**cap\_sysctl\_limit\_init**(*cap\_channel\_t \*chan*);

*cap\_sysctl\_limit\_t* *\**\

**cap\_sysctl\_limit\_name**(*cap\_sysctl\_limit\_t \*limit*, *const char \*name*, *int flags*);

*cap\_sysctl\_limit\_t* *\**\

**cap\_sysctl\_limit\_mib**(*cap\_sysctl\_limit\_t \*limit*, *const int \*mibp*, *u\_int miblen*, *int flags*);

*int*\

**cap\_sysctl\_limit**(*cap\_sysctl\_limit\_t \*limit*);

## DESCRIPTION

The **cap\_sysctl**(), **cap\_sysctlbyname**() and **cap\_sysctlnametomib**() functions are equivalent to [sysctl(3)](/sysctl/3.md), [sysctlbyname(3)](/sysctlbyname/3.md) and [sysctlnametomib(3)](/sysctlnametomib/3.md), except that they are implemented by the ‘system.sysctl’ [libcasper(3)](/libcasper/3.md) service and require a corresponding [libcasper(3)](/libcasper/3.md) capability.

All of these functions, with the exceptions of **cap\_sysctl\_limit\_init**() and **cap\_sysctl\_limit\_mib**(), are reentrant but not thread-safe. That is, they may be called from separate threads only with different *cap\_channel\_t* arguments or with synchronization.

## LIMITS

By default, the **cap\_sysctl** capability provides unrestricted access to the sysctl namespace. Applications typically only require access to a small number of sysctl variables; the **cap\_sysctl\_limit**() interface can be used to restrict the sysctls that can be accessed using the **cap\_sysctl** capability. **cap\_sysctl\_limit\_init**() returns an opaque limit handle used to store a list of permitted sysctls and access rights. Rights are encoded using the following flags:

```
CAP_SYSCTL_READ		allow reads of the sysctl variable
CAP_SYSCTL_WRITE        allow writes of the sysctl variable
CAP_SYSCTL_RDWR         allow reads and writes of the sysctl variable
CAP_SYSCTL_RECURSIVE    permit access to any child of the sysctl variable
```

The **cap\_sysctl\_limit\_name**() function adds the sysctl identified by *name* to the limit list, and **cap\_sysctl\_limit\_mib**() function adds the sysctl identified by *mibp* to the limit list. The access rights for the sysctl are specified in the *flags* parameter; at least one of CAP\_SYSCTL\_READ, CAP\_SYSCTL\_WRITE and CAP\_SYSCTL\_RDWR must be specified. **cap\_sysctl\_limit**() applies a set of sysctl limits to the capability, denying access to sysctl variables not belonging to the set. It consumes the limit handle. After either success or failure, the user must not access the handle again.

Once a set of limits is applied, subsequent calls to **cap\_sysctl\_limit**() will fail unless the new set is a subset of the current set.

**cap\_sysctlnametomib**() will succeed so long as the named sysctl variable is present in the limit set, regardless of its access rights. When a sysctl variable name is added to a limit set, its MIB identifier is automatically added to the set.

## EXAMPLES

The following example first opens a capability to casper, uses this capability to create the **system.sysctl** casper service, and then uses the **cap\_sysctl** capability to get the value of kern.trap\_enotcap.

```
cap_channel_t *capcas, *capsysctl;
const char *name = "kern.trap_enotcap";
void *limit;
size_t size;
bool value;
/* Open capability to Casper. */
capcas = cap_init();
if (capcas == NULL)
	err(1, "Unable to contact Casper");
/* Enter capability mode sandbox. */
if (cap_enter() < 0 && errno != ENOSYS)
	err(1, "Unable to enter capability mode");
/* Use Casper capability to create capability to the system.sysctl service. */
capsysctl = cap_service_open(capcas, "system.sysctl");
if (capsysctl == NULL)
	err(1, "Unable to open system.sysctl service");
/* Close Casper capability, we don't need it anymore. */
cap_close(capcas);
/* Create limit for one MIB with read access only. */
limit = cap_sysctl_limit_init(capsysctl);
(void)cap_sysctl_limit_name(limit, name, CAP_SYSCTL_READ);
/* Limit system.sysctl. */
if (cap_sysctl_limit(limit) < 0)
	err(1, "Unable to set limits");
/* Fetch value. */
size = sizeof(value);
if (cap_sysctlbyname(capsysctl, name, &value, &size, NULL, 0) < 0)
	err(1, "Unable to get value of sysctl");
printf("The value of %s is %d.\n", name, value);
cap_close(capsysctl);
```

## RETURN VALUES

**cap\_sysctl\_limit\_init**() will return a new limit handle on success or NULL on failure, and set *errno*. **cap\_sysctl\_limit\_mib**() and **cap\_sysctl\_limit\_name**() will return the modified limit handle on success or NULL on failure and set *errno*. After failure, the caller must not access the limit handle again. **cap\_sysctl\_limit**() will return -1 on failure and set *errno*. **cap\_sysctl**(), **cap\_sysctlbyname**(), and **cap\_sysctlnametomib**() have the same return values as their non-capability-mode equivalents as documented in [sysctl(3)](/sysctl/3.md).

## SEE ALSO

[cap\_enter(2)](/cap_enter/2.md), [err(3)](/err/3.md), [sysctl(3)](/sysctl/3.md), [sysctlbyname(3)](/sysctlbyname/3.md), [sysctlnametomib(3)](/sysctlnametomib/3.md), [capsicum(4)](/capsicum/4.md), [nv(9)](/nv/9.md)

## HISTORY

The **cap\_sysctl** service first appeared in FreeBSD 10.3.

## AUTHORS

The **cap\_sysctl** service was implemented by Pawel Jakub Dawidek ⟨*pawel@dawidek.net*⟩ under sponsorship from the FreeBSD Foundation.

This manual page was written by Mariusz Zaborski ⟨*oshogbo@FreeBSD.org*⟩.
