]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/nsflags.c
81d21260ebfe1b3d231fa085a87670665d2fc2ca
[thirdparty/systemd.git] / src / shared / nsflags.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2016 Lennart Poettering
4 ***/
5
6 #include <sched.h>
7
8 #include "alloc-util.h"
9 #include "extract-word.h"
10 #include "nsflags.h"
11 #include "string-util.h"
12
13 const struct namespace_flag_map namespace_flag_map[] = {
14 { CLONE_NEWCGROUP, "cgroup" },
15 { CLONE_NEWIPC, "ipc" },
16 { CLONE_NEWNET, "net" },
17 /* So, the mount namespace flag is called CLONE_NEWNS for historical reasons. Let's expose it here under a more
18 * explanatory name: "mnt". This is in-line with how the kernel exposes namespaces in /proc/$PID/ns. */
19 { CLONE_NEWNS, "mnt" },
20 { CLONE_NEWPID, "pid" },
21 { CLONE_NEWUSER, "user" },
22 { CLONE_NEWUTS, "uts" },
23 {}
24 };
25
26 int namespace_flags_from_string(const char *name, unsigned long *ret) {
27 unsigned long flags = 0;
28 int r;
29
30 assert_se(ret);
31
32 for (;;) {
33 _cleanup_free_ char *word = NULL;
34 unsigned long f = 0;
35 unsigned i;
36
37 r = extract_first_word(&name, &word, NULL, 0);
38 if (r < 0)
39 return r;
40 if (r == 0)
41 break;
42
43 for (i = 0; namespace_flag_map[i].name; i++)
44 if (streq(word, namespace_flag_map[i].name)) {
45 f = namespace_flag_map[i].flag;
46 break;
47 }
48
49 if (f == 0)
50 return -EINVAL;
51
52 flags |= f;
53 }
54
55 *ret = flags;
56 return 0;
57 }
58
59 int namespace_flags_to_string(unsigned long flags, char **ret) {
60 _cleanup_free_ char *s = NULL;
61 unsigned i;
62
63 for (i = 0; namespace_flag_map[i].name; i++) {
64 if ((flags & namespace_flag_map[i].flag) != namespace_flag_map[i].flag)
65 continue;
66
67 if (!strextend_with_separator(&s, " ", namespace_flag_map[i].name, NULL))
68 return -ENOMEM;
69 }
70
71 if (!s) {
72 s = strdup("");
73 if (!s)
74 return -ENOMEM;
75 }
76
77 *ret = TAKE_PTR(s);
78
79 return 0;
80 }