]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-cgroup.c
Merge pull request #2131 from evverx/regenerate-m4-on-reconfigure
[thirdparty/systemd.git] / src / nspawn / nspawn-cgroup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2015 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/mount.h>
23
24 #include "alloc-util.h"
25 #include "cgroup-util.h"
26 #include "fd-util.h"
27 #include "fileio.h"
28 #include "mkdir.h"
29 #include "nspawn-cgroup.h"
30 #include "string-util.h"
31 #include "strv.h"
32 #include "util.h"
33
34 int chown_cgroup(pid_t pid, uid_t uid_shift) {
35 _cleanup_free_ char *path = NULL, *fs = NULL;
36 _cleanup_close_ int fd = -1;
37 const char *fn;
38 int r;
39
40 r = cg_pid_get_path(NULL, pid, &path);
41 if (r < 0)
42 return log_error_errno(r, "Failed to get container cgroup path: %m");
43
44 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, path, NULL, &fs);
45 if (r < 0)
46 return log_error_errno(r, "Failed to get file system path for container cgroup: %m");
47
48 fd = open(fs, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
49 if (fd < 0)
50 return log_error_errno(errno, "Failed to open %s: %m", fs);
51
52 FOREACH_STRING(fn,
53 ".",
54 "tasks",
55 "notify_on_release",
56 "cgroup.procs",
57 "cgroup.events",
58 "cgroup.clone_children",
59 "cgroup.controllers",
60 "cgroup.subtree_control",
61 "cgroup.populated")
62 if (fchownat(fd, fn, uid_shift, uid_shift, 0) < 0)
63 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
64 "Failed to chown() cgroup file %s, ignoring: %m", fn);
65
66 return 0;
67 }
68
69 int sync_cgroup(pid_t pid, bool unified_requested) {
70 _cleanup_free_ char *cgroup = NULL;
71 char tree[] = "/tmp/unifiedXXXXXX", pid_string[DECIMAL_STR_MAX(pid) + 1];
72 bool undo_mount = false;
73 const char *fn;
74 int unified, r;
75
76 unified = cg_unified();
77 if (unified < 0)
78 return log_error_errno(unified, "Failed to determine whether the unified hierachy is used: %m");
79
80 if ((unified > 0) == unified_requested)
81 return 0;
82
83 /* When the host uses the legacy cgroup setup, but the
84 * container shall use the unified hierarchy, let's make sure
85 * we copy the path from the name=systemd hierarchy into the
86 * unified hierarchy. Similar for the reverse situation. */
87
88 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup);
89 if (r < 0)
90 return log_error_errno(r, "Failed to get control group of " PID_FMT ": %m", pid);
91
92 /* In order to access the unified hierarchy we need to mount it */
93 if (!mkdtemp(tree))
94 return log_error_errno(errno, "Failed to generate temporary mount point for unified hierarchy: %m");
95
96 if (unified)
97 r = mount("cgroup", tree, "cgroup", MS_NOSUID|MS_NOEXEC|MS_NODEV, "none,name=systemd,xattr");
98 else
99 r = mount("cgroup", tree, "cgroup", MS_NOSUID|MS_NOEXEC|MS_NODEV, "__DEVEL__sane_behavior");
100 if (r < 0) {
101 r = log_error_errno(errno, "Failed to mount unified hierarchy: %m");
102 goto finish;
103 }
104
105 undo_mount = true;
106
107 fn = strjoina(tree, cgroup, "/cgroup.procs");
108 (void) mkdir_parents(fn, 0755);
109
110 sprintf(pid_string, PID_FMT, pid);
111 r = write_string_file(fn, pid_string, 0);
112 if (r < 0)
113 log_error_errno(r, "Failed to move process: %m");
114
115 finish:
116 if (undo_mount)
117 (void) umount(tree);
118
119 (void) rmdir(tree);
120 return r;
121 }
122
123 int create_subcgroup(pid_t pid, bool unified_requested) {
124 _cleanup_free_ char *cgroup = NULL;
125 const char *child;
126 int unified, r;
127 CGroupMask supported;
128
129 /* In the unified hierarchy inner nodes may only only contain
130 * subgroups, but not processes. Hence, if we running in the
131 * unified hierarchy and the container does the same, and we
132 * did not create a scope unit for the container move us and
133 * the container into two separate subcgroups. */
134
135 if (!unified_requested)
136 return 0;
137
138 unified = cg_unified();
139 if (unified < 0)
140 return log_error_errno(unified, "Failed to determine whether the unified hierachy is used: %m");
141 if (unified == 0)
142 return 0;
143
144 r = cg_mask_supported(&supported);
145 if (r < 0)
146 return log_error_errno(r, "Failed to determine supported controllers: %m");
147
148 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup);
149 if (r < 0)
150 return log_error_errno(r, "Failed to get our control group: %m");
151
152 child = strjoina(cgroup, "/payload");
153 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, child, pid);
154 if (r < 0)
155 return log_error_errno(r, "Failed to create %s subcgroup: %m", child);
156
157 child = strjoina(cgroup, "/supervisor");
158 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, child, 0);
159 if (r < 0)
160 return log_error_errno(r, "Failed to create %s subcgroup: %m", child);
161
162 /* Try to enable as many controllers as possible for the new payload. */
163 (void) cg_enable_everywhere(supported, supported, cgroup);
164 return 0;
165 }