1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
11 #include "alloc-util.h"
12 #include "capability-util.h"
14 #include "extract-word.h"
17 #include "parse-util.h"
20 static const struct capability_name* lookup_capability(register const char *str, register GPERF_LEN_TYPE len);
22 #include "cap-from-name.h"
23 #include "cap-to-name.h"
25 const char *capability_to_name(int id) {
30 if (id >= (int) ELEMENTSOF(capability_names))
33 return capability_names[id];
36 int capability_from_name(const char *name) {
37 const struct capability_name *sc;
42 /* Try to parse numeric capability */
43 r = safe_atoi(name, &i);
45 if (i >= 0 && i < (int) ELEMENTSOF(capability_names))
51 /* Try to parse string capability */
52 sc = lookup_capability(name, strlen(name));
59 int capability_list_length(void) {
60 return (int) ELEMENTSOF(capability_names);
63 int capability_set_to_string_alloc(uint64_t set, char **s) {
64 _cleanup_free_ char *str = NULL;
66 size_t allocated = 0, n = 0;
70 for (i = 0; i < cap_last_cap(); i++)
71 if (set & (UINT64_C(1) << i)) {
75 p = capability_to_name(i);
81 if (!GREEDY_REALLOC(str, allocated, n + add + 2))
84 strcpy(mempcpy(str + n, p, add), " ");
88 if (!GREEDY_REALLOC(str, allocated, n + 1))
91 str[n > 0 ? n - 1 : 0] = '\0'; /* truncate the last space, if it's there */
98 int capability_set_from_string(const char *s, uint64_t *set) {
105 _cleanup_free_ char *word = NULL;
108 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
114 r = capability_from_name(word);
118 val |= ((uint64_t) UINT64_C(1)) << (uint64_t) r;