chiark / gitweb /
ae1afc829330b54b7df651cbba9bd86f97037f55
[elogind.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
8 #include <sched.h>
9
10 #include "alloc-util.h"
11 #include "extract-word.h"
12 #include "nsflags.h"
13 #include "string-util.h"
14
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"    },
25         {}
26 };
27
28
29 #if 0 /// UNNEEDED by elogind
30 int namespace_flags_from_string(const char *name, unsigned long *ret) {
31         unsigned long flags = 0;
32         int r;
33
34         assert_se(ret);
35
36         for (;;) {
37                 _cleanup_free_ char *word = NULL;
38                 unsigned long f = 0;
39                 unsigned i;
40
41                 r = extract_first_word(&name, &word, NULL, 0);
42                 if (r < 0)
43                         return r;
44                 if (r == 0)
45                         break;
46
47                 for (i = 0; namespace_flag_map[i].name; i++)
48                         if (streq(word, namespace_flag_map[i].name)) {
49                                  f = namespace_flag_map[i].flag;
50                                  break;
51                         }
52
53                 if (f == 0)
54                         return -EINVAL;
55
56                 flags |= f;
57         }
58
59         *ret = flags;
60         return 0;
61 }
62 #endif // 0
63
64 int namespace_flags_to_string(unsigned long flags, char **ret) {
65         _cleanup_free_ char *s = NULL;
66         unsigned i;
67
68         for (i = 0; namespace_flag_map[i].name; i++) {
69                 if ((flags & namespace_flag_map[i].flag) != namespace_flag_map[i].flag)
70                         continue;
71
72                 if (!strextend_with_separator(&s, " ", namespace_flag_map[i].name, NULL))
73                         return -ENOMEM;
74         }
75
76         if (!s) {
77                 s = strdup("");
78                 if (!s)
79                         return -ENOMEM;
80         }
81
82         *ret = TAKE_PTR(s);
83
84         return 0;
85 }