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 "string-util.h"
27 const 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" },
40 const char* namespace_flag_to_string(unsigned long flag
) {
43 flag
&= NAMESPACE_FLAGS_ALL
;
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
;
49 return NULL
; /* either unknown namespace flag, or a combination of many. This call supports neither. */
52 unsigned long namespace_flag_from_string(const char *name
) {
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
;
65 int namespace_flag_from_string_many(const char *name
, unsigned long *ret
) {
66 unsigned long flags
= 0;
72 _cleanup_free_
char *word
= NULL
;
75 r
= extract_first_word(&name
, &word
, NULL
, 0);
81 f
= namespace_flag_from_string(word
);
92 int namespace_flag_to_string_many(unsigned long flags
, char **ret
) {
93 _cleanup_free_
char *s
= NULL
;
96 for (i
= 0; namespace_flag_map
[i
].name
; i
++) {
97 if ((flags
& namespace_flag_map
[i
].flag
) != namespace_flag_map
[i
].flag
)
101 s
= strdup(namespace_flag_map
[i
].name
);
105 if (!strextend(&s
, " ", namespace_flag_map
[i
].name
, NULL
))