34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering This file is part of systemd.
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering Copyright 2015 Lennart Poettering
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering systemd is free software; you can redistribute it and/or modify it
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering under the terms of the GNU Lesser General Public License as published by
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering (at your option) any later version.
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering systemd is distributed in the hope that it will be useful, but
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering Lesser General Public License for more details.
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering You should have received a copy of the GNU Lesser General Public License
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poetteringint chown_cgroup(pid_t pid, uid_t uid_shift) {
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering _cleanup_free_ char *path = NULL, *fs = NULL;
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering return log_error_errno(r, "Failed to get container cgroup path: %m");
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, path, NULL, &fs);
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering return log_error_errno(r, "Failed to get file system path for container cgroup: %m");
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering fd = open(fs, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering return log_error_errno(errno, "Failed to open %s: %m", fs);
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering "notify_on_release",
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering "cgroup.procs",
aa3be0dd36bae7f32daa35bc269d87f0d809d52fAlban Crequy "cgroup.events",
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering "cgroup.clone_children",
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering "cgroup.subtree_control",
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering "cgroup.populated")
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering if (fchownat(fd, fn, uid_shift, uid_shift, 0) < 0)
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering "Failed to chown() cgroup file %s, ignoring: %m", fn);
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poetteringint sync_cgroup(pid_t pid, bool unified_requested) {
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering char tree[] = "/tmp/unifiedXXXXXX", pid_string[DECIMAL_STR_MAX(pid) + 1];
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering return log_error_errno(unified, "Failed to determine whether the unified hierachy is used: %m");
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering /* When the host uses the legacy cgroup setup, but the
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering * container shall use the unified hierarchy, let's make sure
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering * we copy the path from the name=systemd hierarchy into the
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering * unified hierarchy. Similar for the reverse situation. */
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup);
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering return log_error_errno(r, "Failed to get control group of " PID_FMT ": %m", pid);
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering /* In order to access the unified hierarchy we need to mount it */
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering return log_error_errno(errno, "Failed to generate temporary mount point for unified hierarchy: %m");
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering r = mount("cgroup", tree, "cgroup", MS_NOSUID|MS_NOEXEC|MS_NODEV, "none,name=systemd,xattr");
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering r = mount("cgroup", tree, "cgroup", MS_NOSUID|MS_NOEXEC|MS_NODEV, "__DEVEL__sane_behavior");
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering r = log_error_errno(errno, "Failed to mount unified hierarchy: %m");
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering fn = strjoina(tree, cgroup, "/cgroup.procs");
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering r = write_string_file(fn, pid_string, 0);
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering log_error_errno(r, "Failed to move process: %m");
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poetteringint create_subcgroup(pid_t pid, bool unified_requested) {
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering /* In the unified hierarchy inner nodes may only only contain
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering * subgroups, but not processes. Hence, if we running in the
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering * unified hierarchy and the container does the same, and we
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering * did not create a scope unit for the container move us and
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering * the container into two separate subcgroups. */
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering return log_error_errno(unified, "Failed to determine whether the unified hierachy is used: %m");
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering return log_error_errno(r, "Failed to determine supported controllers: %m");
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup);
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering return log_error_errno(r, "Failed to get our control group: %m");
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, child, pid);
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering return log_error_errno(r, "Failed to create %s subcgroup: %m", child);
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, child, 0);
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering return log_error_errno(r, "Failed to create %s subcgroup: %m", child);
34829a324b1ffc6cb8405223329a9c55cd8de0eeLennart Poettering /* Try to enable as many controllers as possible for the new payload. */