]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/cap-list.c
core/namespace: rework the return semantics of clone_device_node yet again
[thirdparty/systemd.git] / src / basic / cap-list.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <string.h>
23
24 #include "alloc-util.h"
25 #include "capability-util.h"
26 #include "cap-list.h"
27 #include "extract-word.h"
28 #include "macro.h"
29 #include "missing.h"
30 #include "parse-util.h"
31 #include "util.h"
32
33 static const struct capability_name* lookup_capability(register const char *str, register GPERF_LEN_TYPE len);
34
35 #include "cap-from-name.h"
36 #include "cap-to-name.h"
37
38 const char *capability_to_name(int id) {
39
40 if (id < 0)
41 return NULL;
42
43 if (id >= (int) ELEMENTSOF(capability_names))
44 return NULL;
45
46 return capability_names[id];
47 }
48
49 int capability_from_name(const char *name) {
50 const struct capability_name *sc;
51 int r, i;
52
53 assert(name);
54
55 /* Try to parse numeric capability */
56 r = safe_atoi(name, &i);
57 if (r >= 0) {
58 if (i >= 0 && i < (int) ELEMENTSOF(capability_names))
59 return i;
60 else
61 return -EINVAL;
62 }
63
64 /* Try to parse string capability */
65 sc = lookup_capability(name, strlen(name));
66 if (!sc)
67 return -EINVAL;
68
69 return sc->id;
70 }
71
72 int capability_list_length(void) {
73 return (int) ELEMENTSOF(capability_names);
74 }
75
76 int capability_set_to_string_alloc(uint64_t set, char **s) {
77 _cleanup_free_ char *str = NULL;
78 unsigned long i;
79 size_t allocated = 0, n = 0;
80
81 assert(s);
82
83 for (i = 0; i < cap_last_cap(); i++)
84 if (set & (UINT64_C(1) << i)) {
85 const char *p;
86 size_t add;
87
88 p = capability_to_name(i);
89 if (!p)
90 return -EINVAL;
91
92 add = strlen(p);
93
94 if (!GREEDY_REALLOC(str, allocated, n + add + 2))
95 return -ENOMEM;
96
97 strcpy(mempcpy(str + n, p, add), " ");
98 n += add + 1;
99 }
100
101 if (!GREEDY_REALLOC(str, allocated, n + 1))
102 return -ENOMEM;
103
104 str[n > 0 ? n - 1 : 0] = '\0'; /* truncate the last space, if it's there */
105
106 *s = TAKE_PTR(str);
107
108 return 0;
109 }
110
111 int capability_set_from_string(const char *s, uint64_t *set) {
112 uint64_t val = 0;
113 const char *p;
114
115 assert(set);
116
117 for (p = s;;) {
118 _cleanup_free_ char *word = NULL;
119 int r;
120
121 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
122 if (r == -ENOMEM)
123 return r;
124 if (r <= 0)
125 break;
126
127 r = capability_from_name(word);
128 if (r < 0)
129 continue;
130
131 val |= ((uint64_t) UINT64_C(1)) << (uint64_t) r;
132 }
133
134 *set = val;
135
136 return 0;
137 }