]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/cap-list.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "capability-util.h"
9 #include "extract-word.h"
11 #include "parse-util.h"
12 #include "stdio-util.h"
15 static const struct capability_name
* lookup_capability(register const char *str
, register GPERF_LEN_TYPE len
);
17 #include "cap-from-name.h"
18 #include "cap-to-name.h"
20 const char *capability_to_name(int id
) {
24 if ((size_t) id
>= ELEMENTSOF(capability_names
))
27 return capability_names
[id
];
30 int capability_from_name(const char *name
) {
31 const struct capability_name
*sc
;
36 /* Try to parse numeric capability */
37 r
= safe_atoi(name
, &i
);
45 /* Try to parse string capability */
46 sc
= lookup_capability(name
, strlen(name
));
53 /* This is the number of capability names we are *compiled* with.
54 * For the max capability number of the currently-running kernel,
55 * use cap_last_cap(). */
56 int capability_list_length(void) {
57 return (int) ELEMENTSOF(capability_names
);
60 int capability_set_to_string_alloc(uint64_t set
, char **s
) {
61 _cleanup_free_
char *str
= NULL
;
66 for (unsigned i
= 0; i
<= cap_last_cap(); i
++)
67 if (set
& (UINT64_C(1) << i
)) {
72 p
= capability_to_name(i
);
74 xsprintf(buf
, "0x%x", i
);
80 if (!GREEDY_REALLOC(str
, n
+ add
+ 2))
83 strcpy(mempcpy(str
+ n
, p
, add
), " ");
87 if (!GREEDY_REALLOC(str
, n
+ 1))
90 str
[n
> 0 ? n
- 1 : 0] = '\0'; /* truncate the last space, if it's there */
97 int capability_set_from_string(const char *s
, uint64_t *set
) {
102 for (const char *p
= s
;;) {
103 _cleanup_free_
char *word
= NULL
;
106 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_UNQUOTE
);
112 r
= capability_from_name(word
);
116 val
|= ((uint64_t) UINT64_C(1)) << (uint64_t) r
;