]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/nspawn/nspawn-cgroup.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / nspawn / nspawn-cgroup.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
34829a32
LP
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
b5efdb8a 23#include "alloc-util.h"
3ffd4af2 24#include "fd-util.h"
07630cea
LP
25#include "fileio.h"
26#include "mkdir.h"
60e76d48 27#include "mount-util.h"
3ffd4af2 28#include "nspawn-cgroup.h"
f0bef277 29#include "rm-rf.h"
07630cea
LP
30#include "string-util.h"
31#include "strv.h"
32#include "util.h"
34829a32 33
f0bef277 34static int chown_cgroup_path(const char *path, uid_t uid_shift) {
34829a32
LP
35 _cleanup_close_ int fd = -1;
36 const char *fn;
34829a32 37
f0bef277 38 fd = open(path, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
34829a32 39 if (fd < 0)
f0bef277 40 return -errno;
34829a32
LP
41
42 FOREACH_STRING(fn,
43 ".",
44 "tasks",
45 "notify_on_release",
46 "cgroup.procs",
aa3be0dd 47 "cgroup.events",
34829a32
LP
48 "cgroup.clone_children",
49 "cgroup.controllers",
ab2c3861 50 "cgroup.subtree_control")
34829a32
LP
51 if (fchownat(fd, fn, uid_shift, uid_shift, 0) < 0)
52 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
bd68e99b 53 "Failed to chown \"%s/%s\", ignoring: %m", path, fn);
34829a32
LP
54
55 return 0;
56}
57
f0bef277
EV
58int chown_cgroup(pid_t pid, uid_t uid_shift) {
59 _cleanup_free_ char *path = NULL, *fs = NULL;
f0bef277
EV
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
77int sync_cgroup(pid_t pid, CGroupUnified unified_requested, uid_t arg_uid_shift) {
34829a32
LP
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;
b4cccbc1 82 int r, unified_controller;
34829a32 83
c22800e4 84 unified_controller = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER);
b4cccbc1
LP
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))
34829a32
LP
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
b4cccbc1 103 if (unified_controller > 0)
60e76d48
ZJS
104 r = mount_verbose(LOG_ERR, "cgroup", tree, "cgroup",
105 MS_NOSUID|MS_NOEXEC|MS_NODEV, "none,name=systemd,xattr");
34829a32 106 else
60e76d48
ZJS
107 r = mount_verbose(LOG_ERR, "cgroup", tree, "cgroup2",
108 MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
109 if (r < 0)
34829a32 110 goto finish;
34829a32
LP
111
112 undo_mount = true;
113
f0bef277
EV
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
34829a32
LP
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);
f0bef277 125 if (r < 0) {
34829a32 126 log_error_errno(r, "Failed to move process: %m");
f0bef277
EV
127 goto finish;
128 }
34829a32 129
f0bef277
EV
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);
34829a32
LP
134finish:
135 if (undo_mount)
60e76d48 136 (void) umount_verbose(tree);
34829a32
LP
137
138 (void) rmdir(tree);
139 return r;
140}
141
5da38d07 142int create_subcgroup(pid_t pid, CGroupUnified unified_requested) {
34829a32
LP
143 _cleanup_free_ char *cgroup = NULL;
144 const char *child;
415fc41c 145 int r;
34829a32
LP
146 CGroupMask supported;
147
61233823 148 /* In the unified hierarchy inner nodes may only contain
34829a32
LP
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
5da38d07 154 if (unified_requested == CGROUP_UNIFIED_NONE)
34829a32
LP
155 return 0;
156
c22800e4 157 r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER);
b4cccbc1
LP
158 if (r < 0)
159 return log_error_errno(r, "Failed to determine whether the systemd controller is unified: %m");
160 if (r == 0)
34829a32
LP
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}