2 This file is part of systemd.
4 Copyright 2015 Lennart Poettering
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.
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.
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/>.
20 #include <sys/mount.h>
22 #include "alloc-util.h"
26 #include "mount-util.h"
27 #include "nspawn-cgroup.h"
29 #include "string-util.h"
33 static int chown_cgroup_path(const char *path
, uid_t uid_shift
) {
34 _cleanup_close_
int fd
= -1;
37 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
47 "cgroup.clone_children",
49 "cgroup.subtree_control")
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
);
57 int chown_cgroup(pid_t pid
, uid_t uid_shift
) {
58 _cleanup_free_
char *path
= NULL
, *fs
= NULL
;
61 r
= cg_pid_get_path(NULL
, pid
, &path
);
63 return log_error_errno(r
, "Failed to get container cgroup path: %m");
65 r
= cg_get_path(SYSTEMD_CGROUP_CONTROLLER
, path
, NULL
, &fs
);
67 return log_error_errno(r
, "Failed to get file system path for container cgroup: %m");
69 r
= chown_cgroup_path(fs
, uid_shift
);
71 return log_error_errno(r
, "Failed to chown() cgroup %s: %m", fs
);
76 int sync_cgroup(pid_t pid
, CGroupUnified unified_requested
, uid_t arg_uid_shift
) {
77 _cleanup_free_
char *cgroup
= NULL
;
78 char tree
[] = "/tmp/unifiedXXXXXX", pid_string
[DECIMAL_STR_MAX(pid
) + 1];
79 bool undo_mount
= false;
83 if (cg_unified(SYSTEMD_CGROUP_CONTROLLER
) == (unified_requested
>= CGROUP_UNIFIED_SYSTEMD
))
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. */
91 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &cgroup
);
93 return log_error_errno(r
, "Failed to get control group of " PID_FMT
": %m", pid
);
95 /* In order to access the unified hierarchy we need to mount it */
97 return log_error_errno(errno
, "Failed to generate temporary mount point for unified hierarchy: %m");
99 if (cg_unified(SYSTEMD_CGROUP_CONTROLLER
))
100 r
= mount_verbose(LOG_ERR
, "cgroup", tree
, "cgroup",
101 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, "none,name=systemd,xattr");
103 r
= mount_verbose(LOG_ERR
, "cgroup", tree
, "cgroup2",
104 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
);
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
);
116 fn
= strjoina(tree
, cgroup
, "/cgroup.procs");
117 (void) mkdir_parents(fn
, 0755);
119 sprintf(pid_string
, PID_FMT
, pid
);
120 r
= write_string_file(fn
, pid_string
, 0);
122 log_error_errno(r
, "Failed to move process: %m");
126 fn
= strjoina(tree
, cgroup
);
127 r
= chown_cgroup_path(fn
, arg_uid_shift
);
129 log_error_errno(r
, "Failed to chown() cgroup %s: %m", fn
);
132 (void) umount_verbose(tree
);
138 int create_subcgroup(pid_t pid
, CGroupUnified unified_requested
) {
139 _cleanup_free_
char *cgroup
= NULL
;
142 CGroupMask supported
;
144 /* In the unified hierarchy inner nodes may only contain
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. */
150 if (unified_requested
== CGROUP_UNIFIED_NONE
)
153 if (!cg_unified(SYSTEMD_CGROUP_CONTROLLER
))
156 r
= cg_mask_supported(&supported
);
158 return log_error_errno(r
, "Failed to determine supported controllers: %m");
160 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, 0, &cgroup
);
162 return log_error_errno(r
, "Failed to get our control group: %m");
164 child
= strjoina(cgroup
, "/payload");
165 r
= cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER
, child
, pid
);
167 return log_error_errno(r
, "Failed to create %s subcgroup: %m", child
);
169 child
= strjoina(cgroup
, "/supervisor");
170 r
= cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER
, child
, 0);
172 return log_error_errno(r
, "Failed to create %s subcgroup: %m", child
);
174 /* Try to enable as many controllers as possible for the new payload. */
175 (void) cg_enable_everywhere(supported
, supported
, cgroup
);