CAP_FROM_TEXT(3)
β¨ CAP_FROM_TEXT(3) β¨
okay bestie, sit down, hydrate, and let me tell you about the Linux Programmer's Manual page for cap_from_text, cap_to_text, cap_to_name, and cap_from_name β four lil functions that move your capability state back and forth between "computer brain mode" and "human eyeballs mode" π
πΈ NAME πΈ
cap_from_text, cap_to_text, cap_to_name, cap_from_name β capability state textual representation translation π¦
like, it's a translator. but for capabilities. no cap.
π SYNOPSIS π
#include <sys/capability.h>
cap_t cap_from_text(const char *buf_p);
char *cap_to_text(cap_t caps, ssize_t *len_p);
int cap_from_name(const char *name, cap_value_t *cap_p);
char *cap_to_name(cap_value_t cap);
Link with -lcap.
do NOT forget the -lcap bestie, the linker will simply not know her π
π DESCRIPTION π
So these functions translate a capability state between an internal representation and a textual one. The internal representation is managed by the capability functions in working storage. The textual representation is a structured, human-readable string suitable for display. it's giving "bilingual queen" π
cap_from_text()
This one allocates and initializes a capability state in working storage. Then it sets the contents of this newly created capability state to the state represented by a human-readable, nul-terminated character string pointed to by buf_p. It returns a pointer to the newly created capability state.
When the capability state in working storage is no longer required, the caller should free any releasable memory by calling cap_free() with cap_t as an argument. clean up after yourself, the way that a good citizen does β¨
The function returns an error if it cannot parse the contents of the string pointed to by buf_p, or if it does not recognize any capability_name or flag character as valid. It also returns an error if any flag is both set and cleared within a single clause. pick a lane, clause!! π
cap_to_text()
This one converts the capability state in working storage identified by caps into a nul-terminated human-readable string. It allocates any memory necessary to contain the string, and returns a pointer to the string.
If the pointer len_p is not NULL, the function shall also return the full length of the string (not including the nul terminator) in the location pointed to by len_p. The capability state in working storage, identified by caps, is completely represented in the character string β every last bit, no crumbs left behind π
When the capability state in working storage is no longer required, the caller should free any releasable memory by calling cap_free() with the returned string pointer as an argument.
cap_from_name()
Converts a text representation of a capability, such as "cap_chown", to its numerical representation (CAP_CHOWN=0), writing the decoded value into *cap_p.
If cap_p is NULL, no result is written β but the return code of the function still indicates whether or not the specified capability can be represented by the library. she's still answering your question even when you gave her nowhere to put the answer π«Ά
cap_to_name()
Converts a capability index value, cap, to a libcap-allocated textual string. This string should be deallocated with cap_free(). yes. deallocate it. with cap_free(). I'm watching π
π¦ TEXTUAL REPRESENTATION π¦
The text format is described in the cap_text_formats(7) man page.
go read that one too, iykyk, it's the lore drop π
β RETURN VALUE β
cap_from_text(),cap_to_text()andcap_to_name()return a non-NULLvalue on success, andNULLon failure.cap_from_name()returns0for success, and-1on failure (unknown capability).
On failure, errno is set to EINVAL, or ENOMEM.
so likeβ¦ check your return values, bestie. every time. no skipping π
π CONFORMING TO π
cap_from_text() and cap_to_text() are specified by the withdrawn POSIX.1e draft specification.
cap_from_name() and cap_to_name() are Linux extensions.
two of them are vintage, two of them are homegrown linux exclusives π
π₯ EXAMPLE π₯
The example program below demonstrates the use of cap_from_text() and cap_to_text(). The following shell session shows some example runs:
$ ./a.out "cap_chown=p cap_chown+e"
caps_to_text() returned "cap_chown=ep"
$ ./a.out "all=pe cap_chown-e cap_kill-pe"
caps_to_text() returned "=ep cap_chown-e cap_kill-ep"
The source code of the program is as follows:
#include <stdlib.h>
#include <stdio.h>
#include <sys/capability.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int
main(int argc, char *argv[])
{
cap_t caps;
char *txt_caps;
if (argc != 2) {
fprintf(stderr, "%s <textual-cap-set>\n", argv[0]);
exit(EXIT_FAILURE);
}
caps = cap_from_text(argv[1]);
if (caps == NULL)
handle_error("cap_from_text");
txt_caps = cap_to_text(caps, NULL);
if (txt_caps == NULL)
handle_error("cap_to_text");
printf("caps_to_text() returned \"%s\"\n", txt_caps);
if (cap_free(txt_caps) != 0 || cap_free(caps) != 0)
handle_error("cap_free");
exit(EXIT_SUCCESS);
}
notice how she frees BOTH txt_caps and caps?? main character energy, truly π«Άβ¨
π SEE ALSO π
libcap(3)cap_clear(3)cap_copy_ext(3)cap_get_file(3)cap_get_proc(3)cap_init(3)cap_text_formats(7)capabilities(7)
and that's the whole page, bestie!! πβ¨ππ¦πΈπ
this was the Linux Programmer's Manual entry CAP_FROM_TEXT(3), dated 2025-03-19 β go forth and translate your capabilities, and remember: cap_free() your stuff, check errno, and never let a clause set and clear the same flag. periodt. π
πβ
π¦ where this page comes from
- alpine 3.22: libcap-doc 2.78-r0 Β· BSD-3-Clause OR GPL-2.0-only
- alpine 3.23: libcap-doc 2.78-r0 Β· BSD-3-Clause OR GPL-2.0-only
- alpine 3.24: libcap-doc 2.78-r0 Β· BSD-3-Clause OR GPL-2.0-only
- arch rolling: libcap 2.78-1 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 43: libcap-devel 2.76-3.fc43 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 43: libcap-devel 2.76-3.fc43 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 43: libcap-devel 2.76-4.fc43 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 43: libcap-devel 2.76-4.fc43 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 44: libcap-devel 2.77-2.fc44 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 44: libcap-devel 2.77-2.fc44 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 44: libcap-devel 2.78-1.fc44 Β· BSD-3-Clause OR GPL-2.0-only
- fedora 44: libcap-devel 2.78-1.fc44 Β· BSD-3-Clause OR GPL-2.0-only
- nixpkgs unstable: libcap 2.78
- ubuntu stonking: libcap-dev 1:2.78-1ubuntu1 Β· BSD-3-Clause OR GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms