chiark / gitweb /
dd13376a76ecec61fa33654acea8e9d1a3a5a588
[elogind.git] / src / basic / cap-list.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright 2014 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <string.h>
8
9 #include "alloc-util.h"
10 #include "capability-util.h"
11 #include "cap-list.h"
12 #include "extract-word.h"
13 #include "macro.h"
14 #include "missing.h"
15 #include "parse-util.h"
16 #include "util.h"
17
18 static const struct capability_name* lookup_capability(register const char *str, register GPERF_LEN_TYPE len);
19
20 #include "cap-from-name.h"
21 #include "cap-to-name.h"
22
23 const char *capability_to_name(int id) {
24
25         if (id < 0)
26                 return NULL;
27
28         if (id >= (int) ELEMENTSOF(capability_names))
29                 return NULL;
30
31         return capability_names[id];
32 }
33
34 int capability_from_name(const char *name) {
35         const struct capability_name *sc;
36         int r, i;
37
38         assert(name);
39
40         /* Try to parse numeric capability */
41         r = safe_atoi(name, &i);
42         if (r >= 0) {
43                 if (i >= 0 && i < (int) ELEMENTSOF(capability_names))
44                         return i;
45                 else
46                         return -EINVAL;
47         }
48
49         /* Try to parse string capability */
50         sc = lookup_capability(name, strlen(name));
51         if (!sc)
52                 return -EINVAL;
53
54         return sc->id;
55 }
56
57 int capability_list_length(void) {
58         return (int) ELEMENTSOF(capability_names);
59 }
60
61 int capability_set_to_string_alloc(uint64_t set, char **s) {
62         _cleanup_free_ char *str = NULL;
63         unsigned long i;
64         size_t allocated = 0, n = 0;
65
66         assert(s);
67
68         for (i = 0; i < cap_last_cap(); i++)
69                 if (set & (UINT64_C(1) << i)) {
70                         const char *p;
71                         size_t add;
72
73                         p = capability_to_name(i);
74                         if (!p)
75                                 return -EINVAL;
76
77                         add = strlen(p);
78
79                         if (!GREEDY_REALLOC(str, allocated, n + add + 2))
80                                 return -ENOMEM;
81
82                         strcpy(mempcpy(str + n, p, add), " ");
83                         n += add + 1;
84                 }
85
86         if (!GREEDY_REALLOC(str, allocated, n + 1))
87                 return -ENOMEM;
88
89         str[n > 0 ? n - 1 : 0] = '\0'; /* truncate the last space, if it's there */
90
91         *s = TAKE_PTR(str);
92
93         return 0;
94 }
95
96 int capability_set_from_string(const char *s, uint64_t *set) {
97         uint64_t val = 0;
98         const char *p;
99
100         assert(set);
101
102         for (p = s;;) {
103                 _cleanup_free_ char *word = NULL;
104                 int r;
105
106                 r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES);
107                 if (r == -ENOMEM)
108                         return r;
109                 if (r <= 0)
110                         break;
111
112                 r = capability_from_name(word);
113                 if (r < 0)
114                         continue;
115
116                 val |= ((uint64_t) UINT64_C(1)) << (uint64_t) r;
117         }
118
119         *set = val;
120
121         return 0;
122 }