2 This file is part of systemd.
4 Copyright 2016 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/>.
22 #include "alloc-util.h"
23 #include "extract-word.h"
25 #include "seccomp-util.h"
26 #include "string-util.h"
28 const struct namespace_flag_map namespace_flag_map
[] = {
29 { CLONE_NEWCGROUP
, "cgroup" },
30 { CLONE_NEWIPC
, "ipc" },
31 { CLONE_NEWNET
, "net" },
32 /* So, the mount namespace flag is called CLONE_NEWNS for historical reasons. Let's expose it here under a more
33 * explanatory name: "mnt". This is in-line with how the kernel exposes namespaces in /proc/$PID/ns. */
34 { CLONE_NEWNS
, "mnt" },
35 { CLONE_NEWPID
, "pid" },
36 { CLONE_NEWUSER
, "user" },
37 { CLONE_NEWUTS
, "uts" },
41 const char* namespace_flag_to_string(unsigned long flag
) {
44 flag
&= NAMESPACE_FLAGS_ALL
;
46 for (i
= 0; namespace_flag_map
[i
].name
; i
++)
47 if (flag
== namespace_flag_map
[i
].flag
)
48 return namespace_flag_map
[i
].name
;
50 return NULL
; /* either unknown namespace flag, or a combination of many. This call supports neither. */
53 unsigned long namespace_flag_from_string(const char *name
) {
59 for (i
= 0; namespace_flag_map
[i
].name
; i
++)
60 if (streq(name
, namespace_flag_map
[i
].name
))
61 return namespace_flag_map
[i
].flag
;
66 int namespace_flag_from_string_many(const char *name
, unsigned long *ret
) {
67 unsigned long flags
= 0;
78 _cleanup_free_
char *word
= NULL
;
81 r
= extract_first_word(&name
, &word
, NULL
, 0);
87 f
= namespace_flag_from_string(word
);
98 int namespace_flag_to_string_many(unsigned long flags
, char **ret
) {
99 _cleanup_free_
char *s
= NULL
;
102 for (i
= 0; namespace_flag_map
[i
].name
; i
++) {
103 if ((flags
& namespace_flag_map
[i
].flag
) != namespace_flag_map
[i
].flag
)
107 s
= strdup(namespace_flag_map
[i
].name
);
111 if (!strextend(&s
, " ", namespace_flag_map
[i
].name
, NULL
))