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