]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/cap-list.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
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.
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.
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/>.
24 #include "alloc-util.h"
25 #include "capability-util.h"
27 #include "extract-word.h"
30 #include "parse-util.h"
33 static const struct capability_name
* lookup_capability(register const char *str
, register GPERF_LEN_TYPE len
);
35 #include "cap-from-name.h"
36 #include "cap-to-name.h"
38 const char *capability_to_name(int id
) {
43 if (id
>= (int) ELEMENTSOF(capability_names
))
46 return capability_names
[id
];
49 int capability_from_name(const char *name
) {
50 const struct capability_name
*sc
;
55 /* Try to parse numeric capability */
56 r
= safe_atoi(name
, &i
);
58 if (i
>= 0 && i
< (int) ELEMENTSOF(capability_names
))
64 /* Try to parse string capability */
65 sc
= lookup_capability(name
, strlen(name
));
72 int capability_list_length(void) {
73 return (int) ELEMENTSOF(capability_names
);
76 int capability_set_to_string_alloc(uint64_t set
, char **s
) {
77 _cleanup_free_
char *str
= NULL
;
79 size_t allocated
= 0, n
= 0;
83 for (i
= 0; i
< cap_last_cap(); i
++)
84 if (set
& (UINT64_C(1) << i
)) {
88 p
= capability_to_name(i
);
94 if (!GREEDY_REALLOC(str
, allocated
, n
+ add
+ 2))
97 strcpy(mempcpy(str
+ n
, p
, add
), " ");
101 if (!GREEDY_REALLOC(str
, allocated
, n
+ 1))
104 str
[n
> 0 ? n
- 1 : 0] = '\0'; /* truncate the last space, if it's there */
111 int capability_set_from_string(const char *s
, uint64_t *set
) {
118 _cleanup_free_
char *word
= NULL
;
121 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
127 r
= capability_from_name(word
);
131 val
|= ((uint64_t) UINT64_C(1)) << (uint64_t) r
;