1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
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.
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.
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/>.
21 #include <sys/mount.h>
23 #include "alloc-util.h"
27 #include "mount-util.h"
28 #include "nspawn-cgroup.h"
30 #include "string-util.h"
34 static int chown_cgroup_path(const char *path
, uid_t uid_shift
) {
35 _cleanup_close_
int fd
= -1;
38 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
44 "cgroup.clone_children",
49 "cgroup.subtree_control",
53 if (fchownat(fd
, fn
, uid_shift
, uid_shift
, 0) < 0)
54 log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_WARNING
, errno
,
55 "Failed to chown \"%s/%s\", ignoring: %m", path
, fn
);
60 int chown_cgroup(pid_t pid
, CGroupUnified unified_requested
, uid_t uid_shift
) {
61 _cleanup_free_
char *path
= NULL
, *fs
= NULL
;
64 r
= cg_pid_get_path(NULL
, pid
, &path
);
66 return log_error_errno(r
, "Failed to get container cgroup path: %m");
68 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, path
, NULL
, &fs
);
70 return log_error_errno(r
, "Failed to get file system path for container cgroup: %m");
72 r
= chown_cgroup_path(fs
, uid_shift
);
74 return log_error_errno(r
, "Failed to chown() cgroup %s: %m", fs
);
76 if (unified_requested
== CGROUP_UNIFIED_SYSTEMD
) {
77 _cleanup_free_
char *lfs
= NULL
;
78 /* Always propagate access rights from unified to legacy controller */
80 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER_LEGACY
, path
, NULL
, &lfs
);
82 return log_error_errno(r
, "Failed to get file system path for container cgroup: %m");
84 r
= chown_cgroup_path(lfs
, uid_shift
);
86 return log_error_errno(r
, "Failed to chown() cgroup %s: %m", lfs
);
92 int sync_cgroup(pid_t pid
, CGroupUnified unified_requested
, uid_t arg_uid_shift
) {
93 _cleanup_free_
char *cgroup
= NULL
;
94 char tree
[] = "/tmp/unifiedXXXXXX", pid_string
[DECIMAL_STR_MAX(pid
) + 1];
95 bool undo_mount
= false;
97 int r
, unified_controller
;
99 unified_controller
= cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER
);
100 if (unified_controller
< 0)
101 return log_error_errno(unified_controller
, "Failed to determine whether the systemd hierarchy is unified: %m");
102 if ((unified_controller
> 0) == (unified_requested
>= CGROUP_UNIFIED_SYSTEMD
))
105 /* When the host uses the legacy cgroup setup, but the
106 * container shall use the unified hierarchy, let's make sure
107 * we copy the path from the name=systemd hierarchy into the
108 * unified hierarchy. Similar for the reverse situation. */
110 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &cgroup
);
112 return log_error_errno(r
, "Failed to get control group of " PID_FMT
": %m", pid
);
114 /* In order to access the unified hierarchy we need to mount it */
116 return log_error_errno(errno
, "Failed to generate temporary mount point for unified hierarchy: %m");
118 if (unified_controller
> 0)
119 r
= mount_verbose(LOG_ERR
, "cgroup", tree
, "cgroup",
120 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, "none,name=systemd,xattr");
122 r
= mount_verbose(LOG_ERR
, "cgroup", tree
, "cgroup2",
123 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
);
129 /* If nspawn dies abruptly the cgroup hierarchy created below
130 * its unit isn't cleaned up. So, let's remove it
131 * https://github.com/systemd/systemd/pull/4223#issuecomment-252519810 */
132 fn
= strjoina(tree
, cgroup
);
133 (void) rm_rf(fn
, REMOVE_ROOT
|REMOVE_ONLY_DIRECTORIES
);
135 fn
= strjoina(tree
, cgroup
, "/cgroup.procs");
136 (void) mkdir_parents(fn
, 0755);
138 sprintf(pid_string
, PID_FMT
, pid
);
139 r
= write_string_file(fn
, pid_string
, 0);
141 log_error_errno(r
, "Failed to move process: %m");
145 fn
= strjoina(tree
, cgroup
);
146 r
= chown_cgroup_path(fn
, arg_uid_shift
);
148 log_error_errno(r
, "Failed to chown() cgroup %s: %m", fn
);
151 (void) umount_verbose(tree
);
157 int create_subcgroup(pid_t pid
, CGroupUnified unified_requested
) {
158 _cleanup_free_
char *cgroup
= NULL
;
161 CGroupMask supported
;
163 /* In the unified hierarchy inner nodes may only contain
164 * subgroups, but not processes. Hence, if we running in the
165 * unified hierarchy and the container does the same, and we
166 * did not create a scope unit for the container move us and
167 * the container into two separate subcgroups. */
169 if (unified_requested
== CGROUP_UNIFIED_NONE
)
172 r
= cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER
);
174 return log_error_errno(r
, "Failed to determine whether the systemd controller is unified: %m");
178 r
= cg_mask_supported(&supported
);
180 return log_error_errno(r
, "Failed to determine supported controllers: %m");
182 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, 0, &cgroup
);
184 return log_error_errno(r
, "Failed to get our control group: %m");
186 child
= strjoina(cgroup
, "/payload");
187 r
= cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER
, child
, pid
);
189 return log_error_errno(r
, "Failed to create %s subcgroup: %m", child
);
191 child
= strjoina(cgroup
, "/supervisor");
192 r
= cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER
, child
, 0);
194 return log_error_errno(r
, "Failed to create %s subcgroup: %m", child
);
196 /* Try to enable as many controllers as possible for the new payload. */
197 (void) cg_enable_everywhere(supported
, supported
, cgroup
);