chiark / gitweb /
tree-wide: drop 'This file is part of systemd' blurb
[elogind.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
27 #if 0 /// UNNEEDED by elogind
28 int namespace_flags_from_string(const char *name, unsigned long *ret) {
29         unsigned long flags = 0;
30         int r;
31
32         assert_se(ret);
33
34         for (;;) {
35                 _cleanup_free_ char *word = NULL;
36                 unsigned long f = 0;
37                 unsigned i;
38
39                 r = extract_first_word(&name, &word, NULL, 0);
40                 if (r < 0)
41                         return r;
42                 if (r == 0)
43                         break;
44
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;
48                                  break;
49                         }
50
51                 if (f == 0)
52                         return -EINVAL;
53
54                 flags |= f;
55         }
56
57         *ret = flags;
58         return 0;
59 }
60 #endif // 0
61
62 int namespace_flags_to_string(unsigned long flags, char **ret) {
63         _cleanup_free_ char *s = NULL;
64         unsigned i;
65
66         for (i = 0; namespace_flag_map[i].name; i++) {
67                 if ((flags & namespace_flag_map[i].flag) != namespace_flag_map[i].flag)
68                         continue;
69
70                 if (!strextend_with_separator(&s, " ", namespace_flag_map[i].name, NULL))
71                         return -ENOMEM;
72         }
73
74         if (!s) {
75                 s = strdup("");
76                 if (!s)
77                         return -ENOMEM;
78         }
79
80         *ret = TAKE_PTR(s);
81
82         return 0;
83 }