]>
Commit | Line | Data |
---|---|---|
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" | |
3ffd4af2 | 26 | #include "nspawn-cgroup.h" |
07630cea LP |
27 | #include "string-util.h" |
28 | #include "strv.h" | |
29 | #include "util.h" | |
34829a32 LP |
30 | |
31 | int chown_cgroup(pid_t pid, uid_t uid_shift) { | |
32 | _cleanup_free_ char *path = NULL, *fs = NULL; | |
33 | _cleanup_close_ int fd = -1; | |
34 | const char *fn; | |
35 | int r; | |
36 | ||
37 | r = cg_pid_get_path(NULL, pid, &path); | |
38 | if (r < 0) | |
39 | return log_error_errno(r, "Failed to get container cgroup path: %m"); | |
40 | ||
41 | r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, path, NULL, &fs); | |
42 | if (r < 0) | |
43 | return log_error_errno(r, "Failed to get file system path for container cgroup: %m"); | |
44 | ||
45 | fd = open(fs, O_RDONLY|O_CLOEXEC|O_DIRECTORY); | |
46 | if (fd < 0) | |
47 | return log_error_errno(errno, "Failed to open %s: %m", fs); | |
48 | ||
49 | FOREACH_STRING(fn, | |
50 | ".", | |
51 | "tasks", | |
52 | "notify_on_release", | |
53 | "cgroup.procs", | |
aa3be0dd | 54 | "cgroup.events", |
34829a32 LP |
55 | "cgroup.clone_children", |
56 | "cgroup.controllers", | |
ab2c3861 | 57 | "cgroup.subtree_control") |
34829a32 LP |
58 | if (fchownat(fd, fn, uid_shift, uid_shift, 0) < 0) |
59 | log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno, | |
60 | "Failed to chown() cgroup file %s, ignoring: %m", fn); | |
61 | ||
62 | return 0; | |
63 | } | |
64 | ||
5da38d07 | 65 | int sync_cgroup(pid_t pid, CGroupUnified unified_requested) { |
34829a32 LP |
66 | _cleanup_free_ char *cgroup = NULL; |
67 | char tree[] = "/tmp/unifiedXXXXXX", pid_string[DECIMAL_STR_MAX(pid) + 1]; | |
68 | bool undo_mount = false; | |
69 | const char *fn; | |
70 | int unified, r; | |
71 | ||
5da38d07 | 72 | unified = cg_unified(SYSTEMD_CGROUP_CONTROLLER); |
34829a32 | 73 | if (unified < 0) |
998fdc16 | 74 | return log_error_errno(unified, "Failed to determine whether the unified hierarchy is used: %m"); |
34829a32 | 75 | |
5da38d07 | 76 | if ((unified > 0) == (unified_requested >= CGROUP_UNIFIED_SYSTEMD)) |
34829a32 LP |
77 | return 0; |
78 | ||
79 | /* When the host uses the legacy cgroup setup, but the | |
80 | * container shall use the unified hierarchy, let's make sure | |
81 | * we copy the path from the name=systemd hierarchy into the | |
82 | * unified hierarchy. Similar for the reverse situation. */ | |
83 | ||
84 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup); | |
85 | if (r < 0) | |
86 | return log_error_errno(r, "Failed to get control group of " PID_FMT ": %m", pid); | |
87 | ||
88 | /* In order to access the unified hierarchy we need to mount it */ | |
89 | if (!mkdtemp(tree)) | |
90 | return log_error_errno(errno, "Failed to generate temporary mount point for unified hierarchy: %m"); | |
91 | ||
92 | if (unified) | |
93 | r = mount("cgroup", tree, "cgroup", MS_NOSUID|MS_NOEXEC|MS_NODEV, "none,name=systemd,xattr"); | |
94 | else | |
09961995 | 95 | r = mount("cgroup", tree, "cgroup2", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL); |
34829a32 LP |
96 | if (r < 0) { |
97 | r = log_error_errno(errno, "Failed to mount unified hierarchy: %m"); | |
98 | goto finish; | |
99 | } | |
100 | ||
101 | undo_mount = true; | |
102 | ||
103 | fn = strjoina(tree, cgroup, "/cgroup.procs"); | |
104 | (void) mkdir_parents(fn, 0755); | |
105 | ||
106 | sprintf(pid_string, PID_FMT, pid); | |
107 | r = write_string_file(fn, pid_string, 0); | |
108 | if (r < 0) | |
109 | log_error_errno(r, "Failed to move process: %m"); | |
110 | ||
111 | finish: | |
112 | if (undo_mount) | |
113 | (void) umount(tree); | |
114 | ||
115 | (void) rmdir(tree); | |
116 | return r; | |
117 | } | |
118 | ||
5da38d07 | 119 | int create_subcgroup(pid_t pid, CGroupUnified unified_requested) { |
34829a32 LP |
120 | _cleanup_free_ char *cgroup = NULL; |
121 | const char *child; | |
122 | int unified, r; | |
123 | CGroupMask supported; | |
124 | ||
61233823 | 125 | /* In the unified hierarchy inner nodes may only contain |
34829a32 LP |
126 | * subgroups, but not processes. Hence, if we running in the |
127 | * unified hierarchy and the container does the same, and we | |
128 | * did not create a scope unit for the container move us and | |
129 | * the container into two separate subcgroups. */ | |
130 | ||
5da38d07 | 131 | if (unified_requested == CGROUP_UNIFIED_NONE) |
34829a32 LP |
132 | return 0; |
133 | ||
5da38d07 | 134 | unified = cg_unified(SYSTEMD_CGROUP_CONTROLLER); |
34829a32 | 135 | if (unified < 0) |
998fdc16 | 136 | return log_error_errno(unified, "Failed to determine whether the unified hierarchy is used: %m"); |
34829a32 LP |
137 | if (unified == 0) |
138 | return 0; | |
139 | ||
140 | r = cg_mask_supported(&supported); | |
141 | if (r < 0) | |
142 | return log_error_errno(r, "Failed to determine supported controllers: %m"); | |
143 | ||
144 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup); | |
145 | if (r < 0) | |
146 | return log_error_errno(r, "Failed to get our control group: %m"); | |
147 | ||
148 | child = strjoina(cgroup, "/payload"); | |
149 | r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, child, pid); | |
150 | if (r < 0) | |
151 | return log_error_errno(r, "Failed to create %s subcgroup: %m", child); | |
152 | ||
153 | child = strjoina(cgroup, "/supervisor"); | |
154 | r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, child, 0); | |
155 | if (r < 0) | |
156 | return log_error_errno(r, "Failed to create %s subcgroup: %m", child); | |
157 | ||
158 | /* Try to enable as many controllers as possible for the new payload. */ | |
159 | (void) cg_enable_everywhere(supported, supported, cgroup); | |
160 | return 0; | |
161 | } |