chiark / gitweb /
Prep v233.2: Mask unneeded functions and definitions in src/shared
[elogind.git] / src / shared / nsflags.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2016 Lennart Poettering
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include <sched.h>
21
22 #include "alloc-util.h"
23 #include "extract-word.h"
24 #include "nsflags.h"
25 #include "string-util.h"
26
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"    },
37         {}
38 };
39
40 const char* namespace_flag_to_string(unsigned long flag) {
41         unsigned i;
42
43         flag &= NAMESPACE_FLAGS_ALL;
44
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;
48
49         return NULL; /* either unknown namespace flag, or a combination of many. This call supports neither. */
50 }
51
52 unsigned long namespace_flag_from_string(const char *name) {
53         unsigned i;
54
55         if (isempty(name))
56                 return 0;
57
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;
61
62         return 0;
63 }
64
65 #if 0 /// UNNEEDED by elogind
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 #endif // 0
93
94 int namespace_flag_to_string_many(unsigned long flags, char **ret) {
95         _cleanup_free_ char *s = NULL;
96         unsigned i;
97
98         for (i = 0; namespace_flag_map[i].name; i++) {
99                 if ((flags & namespace_flag_map[i].flag) != namespace_flag_map[i].flag)
100                         continue;
101
102                 if (!s) {
103                         s = strdup(namespace_flag_map[i].name);
104                         if (!s)
105                                 return -ENOMEM;
106                 } else {
107                         if (!strextend(&s, " ", namespace_flag_map[i].name, NULL))
108                                 return -ENOMEM;
109                 }
110         }
111
112         if (!s) {
113                 s = strdup("");
114                 if (!s)
115                         return -ENOMEM;
116         }
117
118         *ret = s;
119         s = NULL;
120
121         return 0;
122 }