]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/nsflags.c
systemctl: show capabilities in human readable format
[thirdparty/systemd.git] / src / shared / nsflags.c
CommitLineData
add00535
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2016 Lennart Poettering
5
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.
10
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.
15
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/>.
18***/
19
20#include <sched.h>
21
22#include "alloc-util.h"
23#include "extract-word.h"
24#include "nsflags.h"
add00535
LP
25#include "string-util.h"
26
27const struct namespace_flag_map namespace_flag_map[] = {
28 { CLONE_NEWCGROUP, "cgroup" },
29 { CLONE_NEWIPC, "ipc" },
30 { CLONE_NEWNET, "net" },
31 /* So, the mount namespace flag is called CLONE_NEWNS for historical reasons. Let's expose it here under a more
32 * explanatory name: "mnt". This is in-line with how the kernel exposes namespaces in /proc/$PID/ns. */
33 { CLONE_NEWNS, "mnt" },
34 { CLONE_NEWPID, "pid" },
35 { CLONE_NEWUSER, "user" },
36 { CLONE_NEWUTS, "uts" },
37 {}
38};
39
40const char* namespace_flag_to_string(unsigned long flag) {
41 unsigned i;
42
43 flag &= NAMESPACE_FLAGS_ALL;
44
45 for (i = 0; namespace_flag_map[i].name; i++)
46 if (flag == namespace_flag_map[i].flag)
47 return namespace_flag_map[i].name;
48
49 return NULL; /* either unknown namespace flag, or a combination of many. This call supports neither. */
50}
51
52unsigned long namespace_flag_from_string(const char *name) {
53 unsigned i;
54
55 if (isempty(name))
56 return 0;
57
58 for (i = 0; namespace_flag_map[i].name; i++)
59 if (streq(name, namespace_flag_map[i].name))
60 return namespace_flag_map[i].flag;
61
62 return 0;
63}
64
65int namespace_flag_from_string_many(const char *name, unsigned long *ret) {
66 unsigned long flags = 0;
67 int r;
68
69 assert_se(ret);
70
add00535
LP
71 for (;;) {
72 _cleanup_free_ char *word = NULL;
73 unsigned long f;
74
75 r = extract_first_word(&name, &word, NULL, 0);
76 if (r < 0)
77 return r;
78 if (r == 0)
79 break;
80
81 f = namespace_flag_from_string(word);
82 if (f == 0)
83 return -EINVAL;
84
85 flags |= f;
86 }
87
88 *ret = flags;
89 return 0;
90}
91
92int namespace_flag_to_string_many(unsigned long flags, char **ret) {
93 _cleanup_free_ char *s = NULL;
94 unsigned i;
95
96 for (i = 0; namespace_flag_map[i].name; i++) {
97 if ((flags & namespace_flag_map[i].flag) != namespace_flag_map[i].flag)
98 continue;
99
100 if (!s) {
101 s = strdup(namespace_flag_map[i].name);
102 if (!s)
103 return -ENOMEM;
104 } else {
105 if (!strextend(&s, " ", namespace_flag_map[i].name, NULL))
106 return -ENOMEM;
107 }
108 }
109
110 if (!s) {
111 s = strdup("");
112 if (!s)
113 return -ENOMEM;
114 }
115
116 *ret = s;
117 s = NULL;
118
119 return 0;
120}