]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/nsflags.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / shared / nsflags.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2016 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <sched.h>
22
23 #include "alloc-util.h"
24 #include "extract-word.h"
25 #include "nsflags.h"
26 #include "string-util.h"
27
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" },
38 {}
39 };
40
41 const char* namespace_flag_to_string(unsigned long flag) {
42 unsigned i;
43
44 flag &= NAMESPACE_FLAGS_ALL;
45
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;
49
50 return NULL; /* either unknown namespace flag, or a combination of many. This call supports neither. */
51 }
52
53 unsigned long namespace_flag_from_string(const char *name) {
54 unsigned i;
55
56 if (isempty(name))
57 return 0;
58
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;
62
63 return 0;
64 }
65
66 int namespace_flag_from_string_many(const char *name, unsigned long *ret) {
67 unsigned long flags = 0;
68 int r;
69
70 assert_se(ret);
71
72 for (;;) {
73 _cleanup_free_ char *word = NULL;
74 unsigned long f;
75
76 r = extract_first_word(&name, &word, NULL, 0);
77 if (r < 0)
78 return r;
79 if (r == 0)
80 break;
81
82 f = namespace_flag_from_string(word);
83 if (f == 0)
84 return -EINVAL;
85
86 flags |= f;
87 }
88
89 *ret = flags;
90 return 0;
91 }
92
93 int namespace_flag_to_string_many(unsigned long flags, char **ret) {
94 _cleanup_free_ char *s = NULL;
95 unsigned i;
96
97 for (i = 0; namespace_flag_map[i].name; i++) {
98 if ((flags & namespace_flag_map[i].flag) != namespace_flag_map[i].flag)
99 continue;
100
101 if (!s) {
102 s = strdup(namespace_flag_map[i].name);
103 if (!s)
104 return -ENOMEM;
105 } else {
106 if (!strextend(&s, " ", namespace_flag_map[i].name, NULL))
107 return -ENOMEM;
108 }
109 }
110
111 if (!s) {
112 s = strdup("");
113 if (!s)
114 return -ENOMEM;
115 }
116
117 *ret = s;
118 s = NULL;
119
120 return 0;
121 }