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