2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
24 #include "capability-util.h"
26 #include "extract-word.h"
29 #include "parse-util.h"
32 static const struct capability_name* lookup_capability(register const char *str, register GPERF_LEN_TYPE len);
34 #include "cap-from-name.h"
35 #include "cap-to-name.h"
37 const char *capability_to_name(int id) {
42 if (id >= (int) ELEMENTSOF(capability_names))
45 return capability_names[id];
48 int capability_from_name(const char *name) {
49 const struct capability_name *sc;
54 /* Try to parse numeric capability */
55 r = safe_atoi(name, &i);
59 /* Try to parse string capability */
60 sc = lookup_capability(name, strlen(name));
67 int capability_list_length(void) {
68 return (int) ELEMENTSOF(capability_names);
71 int capability_set_to_string_alloc(uint64_t set, char **s) {
72 _cleanup_free_ char *str = NULL;
74 size_t allocated = 0, n = 0;
78 for (i = 0; i < cap_last_cap(); i++)
79 if (set & (UINT64_C(1) << i)) {
83 p = capability_to_name(i);
89 if (!GREEDY_REALLOC0(str, allocated, n + add + 2))
92 strcpy(mempcpy(str + n, p, add), " ");
105 int capability_set_from_string(const char *s, uint64_t *set) {
112 _cleanup_free_ char *word = NULL;
115 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
121 r = capability_from_name(word);
125 val |= ((uint64_t) UINT64_C(1)) << (uint64_t) r;