]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/nspawn/nspawn-cgroup.c
cgroup-util: check unified_cache before invoking streq()
[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,
52 "Failed to chown() cgroup file %s, ignoring: %m", fn);
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;
415fc41c 81 int r;
34829a32 82
415fc41c 83 if (cg_unified(SYSTEMD_CGROUP_CONTROLLER) == (unified_requested >= CGROUP_UNIFIED_SYSTEMD))
34829a32
LP
84 return 0;
85
86 /* When the host uses the legacy cgroup setup, but the
87 * container shall use the unified hierarchy, let's make sure
88 * we copy the path from the name=systemd hierarchy into the
89 * unified hierarchy. Similar for the reverse situation. */
90
91 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup);
92 if (r < 0)
93 return log_error_errno(r, "Failed to get control group of " PID_FMT ": %m", pid);
94
95 /* In order to access the unified hierarchy we need to mount it */
96 if (!mkdtemp(tree))
97 return log_error_errno(errno, "Failed to generate temporary mount point for unified hierarchy: %m");
98
415fc41c 99 if (cg_unified(SYSTEMD_CGROUP_CONTROLLER))
60e76d48
ZJS
100 r = mount_verbose(LOG_ERR, "cgroup", tree, "cgroup",
101 MS_NOSUID|MS_NOEXEC|MS_NODEV, "none,name=systemd,xattr");
34829a32 102 else
60e76d48
ZJS
103 r = mount_verbose(LOG_ERR, "cgroup", tree, "cgroup2",
104 MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
105 if (r < 0)
34829a32 106 goto finish;
34829a32
LP
107
108 undo_mount = true;
109
f0bef277
EV
110 /* If nspawn dies abruptly the cgroup hierarchy created below
111 * its unit isn't cleaned up. So, let's remove it
112 * https://github.com/systemd/systemd/pull/4223#issuecomment-252519810 */
113 fn = strjoina(tree, cgroup);
114 (void) rm_rf(fn, REMOVE_ROOT|REMOVE_ONLY_DIRECTORIES);
115
34829a32
LP
116 fn = strjoina(tree, cgroup, "/cgroup.procs");
117 (void) mkdir_parents(fn, 0755);
118
119 sprintf(pid_string, PID_FMT, pid);
120 r = write_string_file(fn, pid_string, 0);
f0bef277 121 if (r < 0) {
34829a32 122 log_error_errno(r, "Failed to move process: %m");
f0bef277
EV
123 goto finish;
124 }
34829a32 125
f0bef277
EV
126 fn = strjoina(tree, cgroup);
127 r = chown_cgroup_path(fn, arg_uid_shift);
128 if (r < 0)
129 log_error_errno(r, "Failed to chown() cgroup %s: %m", fn);
34829a32
LP
130finish:
131 if (undo_mount)
60e76d48 132 (void) umount_verbose(tree);
34829a32
LP
133
134 (void) rmdir(tree);
135 return r;
136}
137
5da38d07 138int create_subcgroup(pid_t pid, CGroupUnified unified_requested) {
34829a32
LP
139 _cleanup_free_ char *cgroup = NULL;
140 const char *child;
415fc41c 141 int r;
34829a32
LP
142 CGroupMask supported;
143
61233823 144 /* In the unified hierarchy inner nodes may only contain
34829a32
LP
145 * subgroups, but not processes. Hence, if we running in the
146 * unified hierarchy and the container does the same, and we
147 * did not create a scope unit for the container move us and
148 * the container into two separate subcgroups. */
149
5da38d07 150 if (unified_requested == CGROUP_UNIFIED_NONE)
34829a32
LP
151 return 0;
152
415fc41c 153 if (!cg_unified(SYSTEMD_CGROUP_CONTROLLER))
34829a32
LP
154 return 0;
155
156 r = cg_mask_supported(&supported);
157 if (r < 0)
158 return log_error_errno(r, "Failed to determine supported controllers: %m");
159
160 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup);
161 if (r < 0)
162 return log_error_errno(r, "Failed to get our control group: %m");
163
164 child = strjoina(cgroup, "/payload");
165 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, child, pid);
166 if (r < 0)
167 return log_error_errno(r, "Failed to create %s subcgroup: %m", child);
168
169 child = strjoina(cgroup, "/supervisor");
170 r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, child, 0);
171 if (r < 0)
172 return log_error_errno(r, "Failed to create %s subcgroup: %m", child);
173
174 /* Try to enable as many controllers as possible for the new payload. */
175 (void) cg_enable_everywhere(supported, supported, cgroup);
176 return 0;
177}