]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/nsflags.c
man/run0: Describe environment variables set (#32622)
[thirdparty/systemd.git] / src / shared / nsflags.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4
5 #include "alloc-util.h"
6 #include "extract-word.h"
7 #include "namespace-util.h"
8 #include "nsflags.h"
9 #include "string-util.h"
10
11 int namespace_flags_from_string(const char *name, unsigned long *ret) {
12 unsigned long flags = 0;
13 int r;
14
15 assert_se(ret);
16
17 for (;;) {
18 _cleanup_free_ char *word = NULL;
19 unsigned long f = 0;
20 unsigned i;
21
22 r = extract_first_word(&name, &word, NULL, 0);
23 if (r < 0)
24 return r;
25 if (r == 0)
26 break;
27
28 for (i = 0; namespace_info[i].proc_name; i++)
29 if (streq(word, namespace_info[i].proc_name)) {
30 f = namespace_info[i].clone_flag;
31 break;
32 }
33
34 if (f == 0)
35 return -EINVAL;
36
37 flags |= f;
38 }
39
40 *ret = flags;
41 return 0;
42 }
43
44 int namespace_flags_to_string(unsigned long flags, char **ret) {
45 _cleanup_free_ char *s = NULL;
46 unsigned i;
47
48 for (i = 0; namespace_info[i].proc_name; i++) {
49 if ((flags & namespace_info[i].clone_flag) != namespace_info[i].clone_flag)
50 continue;
51
52 if (!strextend_with_separator(&s, " ", namespace_info[i].proc_name))
53 return -ENOMEM;
54 }
55
56 *ret = TAKE_PTR(s);
57
58 return 0;
59 }
60
61 const char *namespace_single_flag_to_string(unsigned long flag) {
62 for (unsigned i = 0; namespace_info[i].proc_name; i++)
63 if (namespace_info[i].clone_flag == flag)
64 return namespace_info[i].proc_name;
65
66 return NULL;
67 }