1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Lennart Poettering
10 #include "alloc-util.h"
11 #include "extract-word.h"
13 #include "string-util.h"
15 const struct namespace_flag_map namespace_flag_map
[] = {
16 { CLONE_NEWCGROUP
, "cgroup" },
17 { CLONE_NEWIPC
, "ipc" },
18 { CLONE_NEWNET
, "net" },
19 /* So, the mount namespace flag is called CLONE_NEWNS for historical reasons. Let's expose it here under a more
20 * explanatory name: "mnt". This is in-line with how the kernel exposes namespaces in /proc/$PID/ns. */
21 { CLONE_NEWNS
, "mnt" },
22 { CLONE_NEWPID
, "pid" },
23 { CLONE_NEWUSER
, "user" },
24 { CLONE_NEWUTS
, "uts" },
28 int namespace_flags_from_string(const char *name
, unsigned long *ret
) {
29 unsigned long flags
= 0;
35 _cleanup_free_
char *word
= NULL
;
39 r
= extract_first_word(&name
, &word
, NULL
, 0);
45 for (i
= 0; namespace_flag_map
[i
].name
; i
++)
46 if (streq(word
, namespace_flag_map
[i
].name
)) {
47 f
= namespace_flag_map
[i
].flag
;
61 int namespace_flags_to_string(unsigned long flags
, char **ret
) {
62 _cleanup_free_
char *s
= NULL
;
65 for (i
= 0; namespace_flag_map
[i
].name
; i
++) {
66 if ((flags
& namespace_flag_map
[i
].flag
) != namespace_flag_map
[i
].flag
)
69 if (!strextend_with_separator(&s
, " ", namespace_flag_map
[i
].name
, NULL
))