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
);
48 "cgroup.clone_children",
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
);
58 int chown_cgroup(pid_t pid
, uid_t uid_shift
) {
59 _cleanup_free_
char *path
= NULL
, *fs
= NULL
;
62 r
= cg_pid_get_path(NULL
, pid
, &path
);
64 return log_error_errno(r
, "Failed to get container cgroup path: %m");
66 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, path
, NULL
, &fs
);
68 return log_error_errno(r
, "Failed to get file system path for container cgroup: %m");
70 r
= chown_cgroup_path(fs
, uid_shift
);
72 return log_error_errno(r
, "Failed to chown() cgroup %s: %m", fs
);
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;
82 int r
, unified_controller
;
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
))
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. */
95 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &cgroup
);
97 return log_error_errno(r
, "Failed to get control group of " PID_FMT
": %m", pid
);
99 /* In order to access the unified hierarchy we need to mount it */
101 return log_error_errno(errno
, "Failed to generate temporary mount point for unified hierarchy: %m");
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");
107 r
= mount_verbose(LOG_ERR
, "cgroup", tree
, "cgroup2",
108 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
);
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
);
120 fn
= strjoina(tree
, cgroup
, "/cgroup.procs");
121 (void) mkdir_parents(fn
, 0755);
123 sprintf(pid_string
, PID_FMT
, pid
);
124 r
= write_string_file(fn
, pid_string
, 0);
126 log_error_errno(r
, "Failed to move process: %m");
130 fn
= strjoina(tree
, cgroup
);
131 r
= chown_cgroup_path(fn
, arg_uid_shift
);
133 log_error_errno(r
, "Failed to chown() cgroup %s: %m", fn
);
136 (void) umount_verbose(tree
);
142 int create_subcgroup(pid_t pid
, CGroupUnified unified_requested
) {
143 _cleanup_free_
char *cgroup
= NULL
;
146 CGroupMask supported
;
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. */
154 if (unified_requested
== CGROUP_UNIFIED_NONE
)
157 r
= cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER
);
159 return log_error_errno(r
, "Failed to determine whether the systemd controller is unified: %m");
163 r
= cg_mask_supported(&supported
);
165 return log_error_errno(r
, "Failed to determine supported controllers: %m");
167 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, 0, &cgroup
);
169 return log_error_errno(r
, "Failed to get our control group: %m");
171 child
= strjoina(cgroup
, "/payload");
172 r
= cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER
, child
, pid
);
174 return log_error_errno(r
, "Failed to create %s subcgroup: %m", child
);
176 child
= strjoina(cgroup
, "/supervisor");
177 r
= cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER
, child
, 0);
179 return log_error_errno(r
, "Failed to create %s subcgroup: %m", child
);
181 /* Try to enable as many controllers as possible for the new payload. */
182 (void) cg_enable_everywhere(supported
, supported
, cgroup
);