]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
663996b3 | 2 | |
663996b3 | 3 | #include <fcntl.h> |
60f067b4 | 4 | #include <fnmatch.h> |
663996b3 | 5 | |
db2df898 | 6 | #include "alloc-util.h" |
1d42b86d | 7 | #include "blockdev-util.h" |
bb4f798a | 8 | #include "bpf-devices.h" |
f5e65279 | 9 | #include "bpf-firewall.h" |
b012e921 | 10 | #include "btrfs-util.h" |
98393f85 | 11 | #include "bus-error.h" |
6300502b | 12 | #include "cgroup-util.h" |
db2df898 MP |
13 | #include "cgroup.h" |
14 | #include "fd-util.h" | |
15 | #include "fileio.h" | |
16 | #include "fs-util.h" | |
bb4f798a | 17 | #include "nulstr-util.h" |
db2df898 | 18 | #include "parse-util.h" |
663996b3 | 19 | #include "path-util.h" |
6300502b | 20 | #include "process-util.h" |
1d42b86d | 21 | #include "procfs-util.h" |
14228c0d | 22 | #include "special.h" |
6e866b33 | 23 | #include "stat-util.h" |
f5e65279 | 24 | #include "stdio-util.h" |
db2df898 MP |
25 | #include "string-table.h" |
26 | #include "string-util.h" | |
98393f85 | 27 | #include "virt.h" |
663996b3 | 28 | |
bb4f798a | 29 | #define CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC ((usec_t) 100 * USEC_PER_MSEC) |
60f067b4 | 30 | |
6e866b33 MB |
31 | /* Returns the log level to use when cgroup attribute writes fail. When an attribute is missing or we have access |
32 | * problems we downgrade to LOG_DEBUG. This is supposed to be nice to container managers and kernels which want to mask | |
33 | * out specific attributes from us. */ | |
34 | #define LOG_LEVEL_CGROUP_WRITE(r) (IN_SET(abs(r), ENOENT, EROFS, EACCES, EPERM) ? LOG_DEBUG : LOG_WARNING) | |
35 | ||
36 | bool manager_owns_host_root_cgroup(Manager *m) { | |
98393f85 MB |
37 | assert(m); |
38 | ||
39 | /* Returns true if we are managing the root cgroup. Note that it isn't sufficient to just check whether the | |
40 | * group root path equals "/" since that will also be the case if CLONE_NEWCGROUP is in the mix. Since there's | |
41 | * appears to be no nice way to detect whether we are in a CLONE_NEWCGROUP namespace we instead just check if | |
42 | * we run in any kind of container virtualization. */ | |
43 | ||
6e866b33 MB |
44 | if (MANAGER_IS_USER(m)) |
45 | return false; | |
46 | ||
98393f85 MB |
47 | if (detect_container() > 0) |
48 | return false; | |
49 | ||
b012e921 | 50 | return empty_or_root(m->cgroup_root); |
98393f85 MB |
51 | } |
52 | ||
6e866b33 | 53 | bool unit_has_host_root_cgroup(Unit *u) { |
1d42b86d MB |
54 | assert(u); |
55 | ||
98393f85 MB |
56 | /* Returns whether this unit manages the root cgroup. This will return true if this unit is the root slice and |
57 | * the manager manages the root cgroup. */ | |
1d42b86d | 58 | |
6e866b33 | 59 | if (!manager_owns_host_root_cgroup(u->manager)) |
1d42b86d MB |
60 | return false; |
61 | ||
98393f85 | 62 | return unit_has_name(u, SPECIAL_ROOT_SLICE); |
1d42b86d MB |
63 | } |
64 | ||
6e866b33 MB |
65 | static int set_attribute_and_warn(Unit *u, const char *controller, const char *attribute, const char *value) { |
66 | int r; | |
67 | ||
68 | r = cg_set_attribute(controller, u->cgroup_path, attribute, value); | |
69 | if (r < 0) | |
70 | log_unit_full(u, LOG_LEVEL_CGROUP_WRITE(r), r, "Failed to set '%s' attribute on '%s' to '%.*s': %m", | |
71 | strna(attribute), isempty(u->cgroup_path) ? "/" : u->cgroup_path, (int) strcspn(value, NEWLINE), value); | |
72 | ||
73 | return r; | |
74 | } | |
75 | ||
5a920b42 MP |
76 | static void cgroup_compat_warn(void) { |
77 | static bool cgroup_compat_warned = false; | |
78 | ||
79 | if (cgroup_compat_warned) | |
80 | return; | |
81 | ||
98393f85 MB |
82 | log_warning("cgroup compatibility translation between legacy and unified hierarchy settings activated. " |
83 | "See cgroup-compat debug messages for details."); | |
84 | ||
5a920b42 MP |
85 | cgroup_compat_warned = true; |
86 | } | |
87 | ||
88 | #define log_cgroup_compat(unit, fmt, ...) do { \ | |
89 | cgroup_compat_warn(); \ | |
90 | log_unit_debug(unit, "cgroup-compat: " fmt, ##__VA_ARGS__); \ | |
91 | } while (false) | |
92 | ||
14228c0d MB |
93 | void cgroup_context_init(CGroupContext *c) { |
94 | assert(c); | |
663996b3 | 95 | |
6e866b33 | 96 | /* Initialize everything to the kernel defaults. */ |
663996b3 | 97 | |
6e866b33 MB |
98 | *c = (CGroupContext) { |
99 | .cpu_weight = CGROUP_WEIGHT_INVALID, | |
100 | .startup_cpu_weight = CGROUP_WEIGHT_INVALID, | |
101 | .cpu_quota_per_sec_usec = USEC_INFINITY, | |
bb4f798a | 102 | .cpu_quota_period_usec = USEC_INFINITY, |
8a584da2 | 103 | |
6e866b33 MB |
104 | .cpu_shares = CGROUP_CPU_SHARES_INVALID, |
105 | .startup_cpu_shares = CGROUP_CPU_SHARES_INVALID, | |
6300502b | 106 | |
6e866b33 MB |
107 | .memory_high = CGROUP_LIMIT_MAX, |
108 | .memory_max = CGROUP_LIMIT_MAX, | |
109 | .memory_swap_max = CGROUP_LIMIT_MAX, | |
5a920b42 | 110 | |
6e866b33 | 111 | .memory_limit = CGROUP_LIMIT_MAX, |
60f067b4 | 112 | |
6e866b33 MB |
113 | .io_weight = CGROUP_WEIGHT_INVALID, |
114 | .startup_io_weight = CGROUP_WEIGHT_INVALID, | |
aa27b158 | 115 | |
6e866b33 MB |
116 | .blockio_weight = CGROUP_BLKIO_WEIGHT_INVALID, |
117 | .startup_blockio_weight = CGROUP_BLKIO_WEIGHT_INVALID, | |
6300502b | 118 | |
6e866b33 MB |
119 | .tasks_max = CGROUP_LIMIT_MAX, |
120 | }; | |
14228c0d | 121 | } |
663996b3 | 122 | |
14228c0d MB |
123 | void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a) { |
124 | assert(c); | |
125 | assert(a); | |
663996b3 | 126 | |
60f067b4 | 127 | LIST_REMOVE(device_allow, c->device_allow, a); |
14228c0d MB |
128 | free(a->path); |
129 | free(a); | |
663996b3 MS |
130 | } |
131 | ||
aa27b158 MP |
132 | void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w) { |
133 | assert(c); | |
134 | assert(w); | |
135 | ||
136 | LIST_REMOVE(device_weights, c->io_device_weights, w); | |
137 | free(w->path); | |
138 | free(w); | |
139 | } | |
140 | ||
6e866b33 MB |
141 | void cgroup_context_free_io_device_latency(CGroupContext *c, CGroupIODeviceLatency *l) { |
142 | assert(c); | |
143 | assert(l); | |
144 | ||
145 | LIST_REMOVE(device_latencies, c->io_device_latencies, l); | |
146 | free(l->path); | |
147 | free(l); | |
148 | } | |
149 | ||
aa27b158 MP |
150 | void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l) { |
151 | assert(c); | |
152 | assert(l); | |
153 | ||
154 | LIST_REMOVE(device_limits, c->io_device_limits, l); | |
155 | free(l->path); | |
156 | free(l); | |
157 | } | |
158 | ||
14228c0d MB |
159 | void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w) { |
160 | assert(c); | |
161 | assert(w); | |
663996b3 | 162 | |
60f067b4 | 163 | LIST_REMOVE(device_weights, c->blockio_device_weights, w); |
14228c0d MB |
164 | free(w->path); |
165 | free(w); | |
663996b3 MS |
166 | } |
167 | ||
14228c0d MB |
168 | void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b) { |
169 | assert(c); | |
663996b3 MS |
170 | assert(b); |
171 | ||
60f067b4 | 172 | LIST_REMOVE(device_bandwidths, c->blockio_device_bandwidths, b); |
14228c0d MB |
173 | free(b->path); |
174 | free(b); | |
175 | } | |
663996b3 | 176 | |
14228c0d MB |
177 | void cgroup_context_done(CGroupContext *c) { |
178 | assert(c); | |
663996b3 | 179 | |
aa27b158 MP |
180 | while (c->io_device_weights) |
181 | cgroup_context_free_io_device_weight(c, c->io_device_weights); | |
182 | ||
6e866b33 MB |
183 | while (c->io_device_latencies) |
184 | cgroup_context_free_io_device_latency(c, c->io_device_latencies); | |
185 | ||
aa27b158 MP |
186 | while (c->io_device_limits) |
187 | cgroup_context_free_io_device_limit(c, c->io_device_limits); | |
188 | ||
14228c0d MB |
189 | while (c->blockio_device_weights) |
190 | cgroup_context_free_blockio_device_weight(c, c->blockio_device_weights); | |
663996b3 | 191 | |
14228c0d MB |
192 | while (c->blockio_device_bandwidths) |
193 | cgroup_context_free_blockio_device_bandwidth(c, c->blockio_device_bandwidths); | |
663996b3 | 194 | |
14228c0d MB |
195 | while (c->device_allow) |
196 | cgroup_context_free_device_allow(c, c->device_allow); | |
f5e65279 MB |
197 | |
198 | c->ip_address_allow = ip_address_access_free_all(c->ip_address_allow); | |
199 | c->ip_address_deny = ip_address_access_free_all(c->ip_address_deny); | |
14228c0d | 200 | } |
663996b3 | 201 | |
14228c0d | 202 | void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) { |
aa27b158 MP |
203 | CGroupIODeviceLimit *il; |
204 | CGroupIODeviceWeight *iw; | |
6e866b33 | 205 | CGroupIODeviceLatency *l; |
14228c0d MB |
206 | CGroupBlockIODeviceBandwidth *b; |
207 | CGroupBlockIODeviceWeight *w; | |
208 | CGroupDeviceAllow *a; | |
f5e65279 | 209 | IPAddressAccessItem *iaai; |
60f067b4 | 210 | char u[FORMAT_TIMESPAN_MAX]; |
bb4f798a | 211 | char v[FORMAT_TIMESPAN_MAX]; |
14228c0d MB |
212 | |
213 | assert(c); | |
214 | assert(f); | |
215 | ||
216 | prefix = strempty(prefix); | |
217 | ||
218 | fprintf(f, | |
219 | "%sCPUAccounting=%s\n" | |
aa27b158 | 220 | "%sIOAccounting=%s\n" |
14228c0d MB |
221 | "%sBlockIOAccounting=%s\n" |
222 | "%sMemoryAccounting=%s\n" | |
6300502b | 223 | "%sTasksAccounting=%s\n" |
f5e65279 | 224 | "%sIPAccounting=%s\n" |
8a584da2 MP |
225 | "%sCPUWeight=%" PRIu64 "\n" |
226 | "%sStartupCPUWeight=%" PRIu64 "\n" | |
6300502b MP |
227 | "%sCPUShares=%" PRIu64 "\n" |
228 | "%sStartupCPUShares=%" PRIu64 "\n" | |
60f067b4 | 229 | "%sCPUQuotaPerSecSec=%s\n" |
bb4f798a | 230 | "%sCPUQuotaPeriodSec=%s\n" |
aa27b158 MP |
231 | "%sIOWeight=%" PRIu64 "\n" |
232 | "%sStartupIOWeight=%" PRIu64 "\n" | |
6300502b MP |
233 | "%sBlockIOWeight=%" PRIu64 "\n" |
234 | "%sStartupBlockIOWeight=%" PRIu64 "\n" | |
6e866b33 | 235 | "%sMemoryMin=%" PRIu64 "\n" |
5a920b42 MP |
236 | "%sMemoryLow=%" PRIu64 "\n" |
237 | "%sMemoryHigh=%" PRIu64 "\n" | |
238 | "%sMemoryMax=%" PRIu64 "\n" | |
8a584da2 | 239 | "%sMemorySwapMax=%" PRIu64 "\n" |
14228c0d | 240 | "%sMemoryLimit=%" PRIu64 "\n" |
6300502b | 241 | "%sTasksMax=%" PRIu64 "\n" |
f47781d8 MP |
242 | "%sDevicePolicy=%s\n" |
243 | "%sDelegate=%s\n", | |
14228c0d | 244 | prefix, yes_no(c->cpu_accounting), |
aa27b158 | 245 | prefix, yes_no(c->io_accounting), |
14228c0d MB |
246 | prefix, yes_no(c->blockio_accounting), |
247 | prefix, yes_no(c->memory_accounting), | |
6300502b | 248 | prefix, yes_no(c->tasks_accounting), |
f5e65279 | 249 | prefix, yes_no(c->ip_accounting), |
8a584da2 MP |
250 | prefix, c->cpu_weight, |
251 | prefix, c->startup_cpu_weight, | |
14228c0d | 252 | prefix, c->cpu_shares, |
60f067b4 | 253 | prefix, c->startup_cpu_shares, |
5eef597e | 254 | prefix, format_timespan(u, sizeof(u), c->cpu_quota_per_sec_usec, 1), |
bb4f798a | 255 | prefix, format_timespan(v, sizeof(v), c->cpu_quota_period_usec, 1), |
aa27b158 MP |
256 | prefix, c->io_weight, |
257 | prefix, c->startup_io_weight, | |
14228c0d | 258 | prefix, c->blockio_weight, |
60f067b4 | 259 | prefix, c->startup_blockio_weight, |
6e866b33 | 260 | prefix, c->memory_min, |
5a920b42 MP |
261 | prefix, c->memory_low, |
262 | prefix, c->memory_high, | |
263 | prefix, c->memory_max, | |
8a584da2 | 264 | prefix, c->memory_swap_max, |
14228c0d | 265 | prefix, c->memory_limit, |
6300502b | 266 | prefix, c->tasks_max, |
f47781d8 MP |
267 | prefix, cgroup_device_policy_to_string(c->device_policy), |
268 | prefix, yes_no(c->delegate)); | |
14228c0d | 269 | |
52ad194e MB |
270 | if (c->delegate) { |
271 | _cleanup_free_ char *t = NULL; | |
272 | ||
273 | (void) cg_mask_to_string(c->delegate_controllers, &t); | |
274 | ||
275 | fprintf(f, "%sDelegateControllers=%s\n", | |
276 | prefix, | |
277 | strempty(t)); | |
278 | } | |
279 | ||
14228c0d MB |
280 | LIST_FOREACH(device_allow, a, c->device_allow) |
281 | fprintf(f, | |
282 | "%sDeviceAllow=%s %s%s%s\n", | |
283 | prefix, | |
284 | a->path, | |
285 | a->r ? "r" : "", a->w ? "w" : "", a->m ? "m" : ""); | |
286 | ||
aa27b158 MP |
287 | LIST_FOREACH(device_weights, iw, c->io_device_weights) |
288 | fprintf(f, | |
6e866b33 | 289 | "%sIODeviceWeight=%s %" PRIu64 "\n", |
aa27b158 MP |
290 | prefix, |
291 | iw->path, | |
292 | iw->weight); | |
293 | ||
6e866b33 MB |
294 | LIST_FOREACH(device_latencies, l, c->io_device_latencies) |
295 | fprintf(f, | |
296 | "%sIODeviceLatencyTargetSec=%s %s\n", | |
297 | prefix, | |
298 | l->path, | |
299 | format_timespan(u, sizeof(u), l->target_usec, 1)); | |
300 | ||
aa27b158 MP |
301 | LIST_FOREACH(device_limits, il, c->io_device_limits) { |
302 | char buf[FORMAT_BYTES_MAX]; | |
303 | CGroupIOLimitType type; | |
304 | ||
305 | for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++) | |
306 | if (il->limits[type] != cgroup_io_limit_defaults[type]) | |
307 | fprintf(f, | |
308 | "%s%s=%s %s\n", | |
309 | prefix, | |
310 | cgroup_io_limit_type_to_string(type), | |
311 | il->path, | |
312 | format_bytes(buf, sizeof(buf), il->limits[type])); | |
313 | } | |
314 | ||
14228c0d MB |
315 | LIST_FOREACH(device_weights, w, c->blockio_device_weights) |
316 | fprintf(f, | |
6300502b | 317 | "%sBlockIODeviceWeight=%s %" PRIu64, |
14228c0d MB |
318 | prefix, |
319 | w->path, | |
320 | w->weight); | |
321 | ||
322 | LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) { | |
323 | char buf[FORMAT_BYTES_MAX]; | |
324 | ||
aa27b158 MP |
325 | if (b->rbps != CGROUP_LIMIT_MAX) |
326 | fprintf(f, | |
327 | "%sBlockIOReadBandwidth=%s %s\n", | |
328 | prefix, | |
329 | b->path, | |
330 | format_bytes(buf, sizeof(buf), b->rbps)); | |
331 | if (b->wbps != CGROUP_LIMIT_MAX) | |
332 | fprintf(f, | |
333 | "%sBlockIOWriteBandwidth=%s %s\n", | |
334 | prefix, | |
335 | b->path, | |
336 | format_bytes(buf, sizeof(buf), b->wbps)); | |
14228c0d | 337 | } |
f5e65279 MB |
338 | |
339 | LIST_FOREACH(items, iaai, c->ip_address_allow) { | |
340 | _cleanup_free_ char *k = NULL; | |
341 | ||
342 | (void) in_addr_to_string(iaai->family, &iaai->address, &k); | |
343 | fprintf(f, "%sIPAddressAllow=%s/%u\n", prefix, strnull(k), iaai->prefixlen); | |
344 | } | |
345 | ||
346 | LIST_FOREACH(items, iaai, c->ip_address_deny) { | |
347 | _cleanup_free_ char *k = NULL; | |
348 | ||
349 | (void) in_addr_to_string(iaai->family, &iaai->address, &k); | |
350 | fprintf(f, "%sIPAddressDeny=%s/%u\n", prefix, strnull(k), iaai->prefixlen); | |
351 | } | |
663996b3 MS |
352 | } |
353 | ||
6e866b33 MB |
354 | int cgroup_add_device_allow(CGroupContext *c, const char *dev, const char *mode) { |
355 | _cleanup_free_ CGroupDeviceAllow *a = NULL; | |
356 | _cleanup_free_ char *d = NULL; | |
357 | ||
358 | assert(c); | |
359 | assert(dev); | |
360 | assert(isempty(mode) || in_charset(mode, "rwm")); | |
361 | ||
362 | a = new(CGroupDeviceAllow, 1); | |
363 | if (!a) | |
364 | return -ENOMEM; | |
365 | ||
366 | d = strdup(dev); | |
367 | if (!d) | |
368 | return -ENOMEM; | |
369 | ||
370 | *a = (CGroupDeviceAllow) { | |
371 | .path = TAKE_PTR(d), | |
372 | .r = isempty(mode) || strchr(mode, 'r'), | |
373 | .w = isempty(mode) || strchr(mode, 'w'), | |
374 | .m = isempty(mode) || strchr(mode, 'm'), | |
375 | }; | |
376 | ||
377 | LIST_PREPEND(device_allow, c->device_allow, a); | |
378 | TAKE_PTR(a); | |
379 | ||
380 | return 0; | |
381 | } | |
382 | ||
383 | static void cgroup_xattr_apply(Unit *u) { | |
384 | char ids[SD_ID128_STRING_MAX]; | |
385 | int r; | |
386 | ||
387 | assert(u); | |
388 | ||
389 | if (!MANAGER_IS_SYSTEM(u->manager)) | |
390 | return; | |
391 | ||
392 | if (sd_id128_is_null(u->invocation_id)) | |
393 | return; | |
394 | ||
395 | r = cg_set_xattr(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, | |
396 | "trusted.invocation_id", | |
397 | sd_id128_to_string(u->invocation_id, ids), 32, | |
398 | 0); | |
399 | if (r < 0) | |
400 | log_unit_debug_errno(u, r, "Failed to set invocation ID on control group %s, ignoring: %m", u->cgroup_path); | |
401 | } | |
402 | ||
b012e921 | 403 | static int lookup_block_device(const char *p, dev_t *ret) { |
7c20daf6 FS |
404 | dev_t rdev, dev = 0; |
405 | mode_t mode; | |
14228c0d | 406 | int r; |
663996b3 | 407 | |
14228c0d | 408 | assert(p); |
b012e921 | 409 | assert(ret); |
663996b3 | 410 | |
7c20daf6 | 411 | r = device_path_parse_major_minor(p, &mode, &rdev); |
6e866b33 | 412 | if (r == -ENODEV) { /* not a parsable device node, need to go to disk */ |
7c20daf6 | 413 | struct stat st; |
6e866b33 MB |
414 | if (stat(p, &st) < 0) |
415 | return log_warning_errno(errno, "Couldn't stat device '%s': %m", p); | |
7c20daf6 FS |
416 | rdev = (dev_t)st.st_rdev; |
417 | dev = (dev_t)st.st_dev; | |
418 | mode = st.st_mode; | |
6e866b33 MB |
419 | } else if (r < 0) |
420 | return log_warning_errno(r, "Failed to parse major/minor from path '%s': %m", p); | |
421 | ||
7c20daf6 | 422 | if (S_ISCHR(mode)) { |
6e866b33 MB |
423 | log_warning("Device node '%s' is a character device, but block device needed.", p); |
424 | return -ENOTBLK; | |
7c20daf6 FS |
425 | } else if (S_ISBLK(mode)) |
426 | *ret = rdev; | |
427 | else if (major(dev) != 0) | |
428 | *ret = dev; /* If this is not a device node then use the block device this file is stored on */ | |
b012e921 MB |
429 | else { |
430 | /* If this is btrfs, getting the backing block device is a bit harder */ | |
431 | r = btrfs_get_block_device(p, ret); | |
432 | if (r < 0 && r != -ENOTTY) | |
433 | return log_warning_errno(r, "Failed to determine block device backing btrfs file system '%s': %m", p); | |
434 | if (r == -ENOTTY) { | |
435 | log_warning("'%s' is not a block device node, and file system block device cannot be determined or is not local.", p); | |
436 | return -ENODEV; | |
437 | } | |
14228c0d | 438 | } |
663996b3 | 439 | |
b012e921 MB |
440 | /* If this is a LUKS device, try to get the originating block device */ |
441 | (void) block_get_originating(*ret, ret); | |
442 | ||
443 | /* If this is a partition, try to get the originating block device */ | |
444 | (void) block_get_whole_disk(*ret, ret); | |
14228c0d | 445 | return 0; |
663996b3 MS |
446 | } |
447 | ||
6e866b33 | 448 | static int whitelist_device(BPFProgram *prog, const char *path, const char *node, const char *acc) { |
7c20daf6 FS |
449 | dev_t rdev; |
450 | mode_t mode; | |
663996b3 MS |
451 | int r; |
452 | ||
14228c0d MB |
453 | assert(path); |
454 | assert(acc); | |
663996b3 | 455 | |
6e866b33 MB |
456 | /* Some special handling for /dev/block/%u:%u, /dev/char/%u:%u, /run/systemd/inaccessible/chr and |
457 | * /run/systemd/inaccessible/blk paths. Instead of stat()ing these we parse out the major/minor directly. This | |
458 | * means clients can use these path without the device node actually around */ | |
7c20daf6 | 459 | r = device_path_parse_major_minor(node, &mode, &rdev); |
6e866b33 MB |
460 | if (r < 0) { |
461 | if (r != -ENODEV) | |
462 | return log_warning_errno(r, "Couldn't parse major/minor from device path '%s': %m", node); | |
2897b343 | 463 | |
7c20daf6 | 464 | struct stat st; |
6e866b33 MB |
465 | if (stat(node, &st) < 0) |
466 | return log_warning_errno(errno, "Couldn't stat device %s: %m", node); | |
2897b343 | 467 | |
6e866b33 MB |
468 | if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) { |
469 | log_warning("%s is not a device.", node); | |
470 | return -ENODEV; | |
471 | } | |
7c20daf6 FS |
472 | rdev = (dev_t) st.st_rdev; |
473 | mode = st.st_mode; | |
14228c0d | 474 | } |
663996b3 | 475 | |
6e866b33 MB |
476 | if (cg_all_unified() > 0) { |
477 | if (!prog) | |
478 | return 0; | |
14228c0d | 479 | |
7c20daf6 FS |
480 | return cgroup_bpf_whitelist_device(prog, S_ISCHR(mode) ? BPF_DEVCG_DEV_CHAR : BPF_DEVCG_DEV_BLOCK, |
481 | major(rdev), minor(rdev), acc); | |
663996b3 | 482 | |
6e866b33 MB |
483 | } else { |
484 | char buf[2+DECIMAL_STR_MAX(dev_t)*2+2+4]; | |
663996b3 | 485 | |
6e866b33 MB |
486 | sprintf(buf, |
487 | "%c %u:%u %s", | |
7c20daf6 FS |
488 | S_ISCHR(mode) ? 'c' : 'b', |
489 | major(rdev), minor(rdev), | |
6e866b33 MB |
490 | acc); |
491 | ||
492 | /* Changing the devices list of a populated cgroup might result in EINVAL, hence ignore EINVAL here. */ | |
493 | ||
494 | r = cg_set_attribute("devices", path, "devices.allow", buf); | |
495 | if (r < 0) | |
496 | return log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING, | |
497 | r, "Failed to set devices.allow on %s: %m", path); | |
498 | ||
499 | return 0; | |
500 | } | |
663996b3 MS |
501 | } |
502 | ||
6e866b33 | 503 | static int whitelist_major(BPFProgram *prog, const char *path, const char *name, char type, const char *acc) { |
60f067b4 | 504 | _cleanup_fclose_ FILE *f = NULL; |
6e866b33 | 505 | char buf[2+DECIMAL_STR_MAX(unsigned)+3+4]; |
60f067b4 | 506 | bool good = false; |
6e866b33 | 507 | unsigned maj; |
60f067b4 JS |
508 | int r; |
509 | ||
510 | assert(path); | |
511 | assert(acc); | |
f5e65279 | 512 | assert(IN_SET(type, 'b', 'c')); |
60f067b4 | 513 | |
6e866b33 MB |
514 | if (streq(name, "*")) { |
515 | /* If the name is a wildcard, then apply this list to all devices of this type */ | |
516 | ||
517 | if (cg_all_unified() > 0) { | |
518 | if (!prog) | |
519 | return 0; | |
520 | ||
521 | (void) cgroup_bpf_whitelist_class(prog, type == 'c' ? BPF_DEVCG_DEV_CHAR : BPF_DEVCG_DEV_BLOCK, acc); | |
522 | } else { | |
523 | xsprintf(buf, "%c *:* %s", type, acc); | |
524 | ||
525 | r = cg_set_attribute("devices", path, "devices.allow", buf); | |
526 | if (r < 0) | |
527 | log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, | |
528 | "Failed to set devices.allow on %s: %m", path); | |
529 | return 0; | |
530 | } | |
531 | } | |
532 | ||
533 | if (safe_atou(name, &maj) >= 0 && DEVICE_MAJOR_VALID(maj)) { | |
534 | /* The name is numeric and suitable as major. In that case, let's take is major, and create the entry | |
535 | * directly */ | |
536 | ||
537 | if (cg_all_unified() > 0) { | |
538 | if (!prog) | |
539 | return 0; | |
540 | ||
541 | (void) cgroup_bpf_whitelist_major(prog, | |
542 | type == 'c' ? BPF_DEVCG_DEV_CHAR : BPF_DEVCG_DEV_BLOCK, | |
543 | maj, acc); | |
544 | } else { | |
545 | xsprintf(buf, "%c %u:* %s", type, maj, acc); | |
546 | ||
547 | r = cg_set_attribute("devices", path, "devices.allow", buf); | |
548 | if (r < 0) | |
549 | log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, | |
550 | "Failed to set devices.allow on %s: %m", path); | |
551 | } | |
552 | ||
553 | return 0; | |
554 | } | |
555 | ||
60f067b4 | 556 | f = fopen("/proc/devices", "re"); |
f47781d8 MP |
557 | if (!f) |
558 | return log_warning_errno(errno, "Cannot open /proc/devices to resolve %s (%c): %m", name, type); | |
60f067b4 | 559 | |
6e866b33 MB |
560 | for (;;) { |
561 | _cleanup_free_ char *line = NULL; | |
562 | char *w, *p; | |
60f067b4 | 563 | |
6e866b33 MB |
564 | r = read_line(f, LONG_LINE_MAX, &line); |
565 | if (r < 0) | |
566 | return log_warning_errno(r, "Failed to read /proc/devices: %m"); | |
567 | if (r == 0) | |
568 | break; | |
60f067b4 JS |
569 | |
570 | if (type == 'c' && streq(line, "Character devices:")) { | |
571 | good = true; | |
572 | continue; | |
573 | } | |
574 | ||
575 | if (type == 'b' && streq(line, "Block devices:")) { | |
576 | good = true; | |
577 | continue; | |
578 | } | |
579 | ||
580 | if (isempty(line)) { | |
581 | good = false; | |
582 | continue; | |
583 | } | |
584 | ||
585 | if (!good) | |
586 | continue; | |
587 | ||
588 | p = strstrip(line); | |
589 | ||
590 | w = strpbrk(p, WHITESPACE); | |
591 | if (!w) | |
592 | continue; | |
593 | *w = 0; | |
594 | ||
595 | r = safe_atou(p, &maj); | |
596 | if (r < 0) | |
597 | continue; | |
598 | if (maj <= 0) | |
599 | continue; | |
600 | ||
601 | w++; | |
602 | w += strspn(w, WHITESPACE); | |
603 | ||
604 | if (fnmatch(name, w, 0) != 0) | |
605 | continue; | |
606 | ||
6e866b33 MB |
607 | if (cg_all_unified() > 0) { |
608 | if (!prog) | |
609 | continue; | |
60f067b4 | 610 | |
6e866b33 MB |
611 | (void) cgroup_bpf_whitelist_major(prog, |
612 | type == 'c' ? BPF_DEVCG_DEV_CHAR : BPF_DEVCG_DEV_BLOCK, | |
613 | maj, acc); | |
614 | } else { | |
615 | sprintf(buf, | |
616 | "%c %u:* %s", | |
617 | type, | |
618 | maj, | |
619 | acc); | |
620 | ||
621 | /* Changing the devices list of a populated cgroup might result in EINVAL, hence ignore EINVAL | |
622 | * here. */ | |
623 | ||
624 | r = cg_set_attribute("devices", path, "devices.allow", buf); | |
625 | if (r < 0) | |
626 | log_full_errno(IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING, | |
627 | r, "Failed to set devices.allow on %s: %m", path); | |
628 | } | |
60f067b4 JS |
629 | } |
630 | ||
631 | return 0; | |
60f067b4 JS |
632 | } |
633 | ||
8a584da2 MP |
634 | static bool cgroup_context_has_cpu_weight(CGroupContext *c) { |
635 | return c->cpu_weight != CGROUP_WEIGHT_INVALID || | |
636 | c->startup_cpu_weight != CGROUP_WEIGHT_INVALID; | |
637 | } | |
638 | ||
639 | static bool cgroup_context_has_cpu_shares(CGroupContext *c) { | |
640 | return c->cpu_shares != CGROUP_CPU_SHARES_INVALID || | |
641 | c->startup_cpu_shares != CGROUP_CPU_SHARES_INVALID; | |
642 | } | |
643 | ||
644 | static uint64_t cgroup_context_cpu_weight(CGroupContext *c, ManagerState state) { | |
645 | if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && | |
646 | c->startup_cpu_weight != CGROUP_WEIGHT_INVALID) | |
647 | return c->startup_cpu_weight; | |
648 | else if (c->cpu_weight != CGROUP_WEIGHT_INVALID) | |
649 | return c->cpu_weight; | |
650 | else | |
651 | return CGROUP_WEIGHT_DEFAULT; | |
652 | } | |
653 | ||
654 | static uint64_t cgroup_context_cpu_shares(CGroupContext *c, ManagerState state) { | |
655 | if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && | |
656 | c->startup_cpu_shares != CGROUP_CPU_SHARES_INVALID) | |
657 | return c->startup_cpu_shares; | |
658 | else if (c->cpu_shares != CGROUP_CPU_SHARES_INVALID) | |
659 | return c->cpu_shares; | |
660 | else | |
661 | return CGROUP_CPU_SHARES_DEFAULT; | |
662 | } | |
663 | ||
bb4f798a MB |
664 | usec_t cgroup_cpu_adjust_period(usec_t period, usec_t quota, usec_t resolution, usec_t max_period) { |
665 | /* kernel uses a minimum resolution of 1ms, so both period and (quota * period) | |
666 | * need to be higher than that boundary. quota is specified in USecPerSec. | |
667 | * Additionally, period must be at most max_period. */ | |
668 | assert(quota > 0); | |
669 | ||
670 | return MIN(MAX3(period, resolution, resolution * USEC_PER_SEC / quota), max_period); | |
671 | } | |
672 | ||
673 | static usec_t cgroup_cpu_adjust_period_and_log(Unit *u, usec_t period, usec_t quota) { | |
674 | usec_t new_period; | |
675 | ||
676 | if (quota == USEC_INFINITY) | |
677 | /* Always use default period for infinity quota. */ | |
678 | return CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC; | |
679 | ||
680 | if (period == USEC_INFINITY) | |
681 | /* Default period was requested. */ | |
682 | period = CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC; | |
683 | ||
684 | /* Clamp to interval [1ms, 1s] */ | |
685 | new_period = cgroup_cpu_adjust_period(period, quota, USEC_PER_MSEC, USEC_PER_SEC); | |
686 | ||
687 | if (new_period != period) { | |
688 | char v[FORMAT_TIMESPAN_MAX]; | |
689 | log_unit_full(u, u->warned_clamping_cpu_quota_period ? LOG_DEBUG : LOG_WARNING, 0, | |
690 | "Clamping CPU interval for cpu.max: period is now %s", | |
691 | format_timespan(v, sizeof(v), new_period, 1)); | |
692 | u->warned_clamping_cpu_quota_period = true; | |
693 | } | |
694 | ||
695 | return new_period; | |
696 | } | |
697 | ||
6e866b33 MB |
698 | static void cgroup_apply_unified_cpu_weight(Unit *u, uint64_t weight) { |
699 | char buf[DECIMAL_STR_MAX(uint64_t) + 2]; | |
8a584da2 MP |
700 | |
701 | xsprintf(buf, "%" PRIu64 "\n", weight); | |
6e866b33 MB |
702 | (void) set_attribute_and_warn(u, "cpu", "cpu.weight", buf); |
703 | } | |
704 | ||
bb4f798a | 705 | static void cgroup_apply_unified_cpu_quota(Unit *u, usec_t quota, usec_t period) { |
6e866b33 | 706 | char buf[(DECIMAL_STR_MAX(usec_t) + 1) * 2 + 1]; |
8a584da2 | 707 | |
bb4f798a | 708 | period = cgroup_cpu_adjust_period_and_log(u, period, quota); |
8a584da2 MP |
709 | if (quota != USEC_INFINITY) |
710 | xsprintf(buf, USEC_FMT " " USEC_FMT "\n", | |
bb4f798a | 711 | MAX(quota * period / USEC_PER_SEC, USEC_PER_MSEC), period); |
8a584da2 | 712 | else |
bb4f798a | 713 | xsprintf(buf, "max " USEC_FMT "\n", period); |
6e866b33 | 714 | (void) set_attribute_and_warn(u, "cpu", "cpu.max", buf); |
8a584da2 MP |
715 | } |
716 | ||
6e866b33 MB |
717 | static void cgroup_apply_legacy_cpu_shares(Unit *u, uint64_t shares) { |
718 | char buf[DECIMAL_STR_MAX(uint64_t) + 2]; | |
8a584da2 MP |
719 | |
720 | xsprintf(buf, "%" PRIu64 "\n", shares); | |
6e866b33 MB |
721 | (void) set_attribute_and_warn(u, "cpu", "cpu.shares", buf); |
722 | } | |
723 | ||
bb4f798a | 724 | static void cgroup_apply_legacy_cpu_quota(Unit *u, usec_t quota, usec_t period) { |
6e866b33 | 725 | char buf[DECIMAL_STR_MAX(usec_t) + 2]; |
8a584da2 | 726 | |
bb4f798a MB |
727 | period = cgroup_cpu_adjust_period_and_log(u, period, quota); |
728 | ||
729 | xsprintf(buf, USEC_FMT "\n", period); | |
6e866b33 | 730 | (void) set_attribute_and_warn(u, "cpu", "cpu.cfs_period_us", buf); |
8a584da2 MP |
731 | |
732 | if (quota != USEC_INFINITY) { | |
bb4f798a | 733 | xsprintf(buf, USEC_FMT "\n", MAX(quota * period / USEC_PER_SEC, USEC_PER_MSEC)); |
6e866b33 | 734 | (void) set_attribute_and_warn(u, "cpu", "cpu.cfs_quota_us", buf); |
8a584da2 | 735 | } else |
6e866b33 | 736 | (void) set_attribute_and_warn(u, "cpu", "cpu.cfs_quota_us", "-1\n"); |
8a584da2 MP |
737 | } |
738 | ||
739 | static uint64_t cgroup_cpu_shares_to_weight(uint64_t shares) { | |
740 | return CLAMP(shares * CGROUP_WEIGHT_DEFAULT / CGROUP_CPU_SHARES_DEFAULT, | |
741 | CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX); | |
742 | } | |
743 | ||
744 | static uint64_t cgroup_cpu_weight_to_shares(uint64_t weight) { | |
745 | return CLAMP(weight * CGROUP_CPU_SHARES_DEFAULT / CGROUP_WEIGHT_DEFAULT, | |
746 | CGROUP_CPU_SHARES_MIN, CGROUP_CPU_SHARES_MAX); | |
747 | } | |
748 | ||
aa27b158 MP |
749 | static bool cgroup_context_has_io_config(CGroupContext *c) { |
750 | return c->io_accounting || | |
751 | c->io_weight != CGROUP_WEIGHT_INVALID || | |
752 | c->startup_io_weight != CGROUP_WEIGHT_INVALID || | |
753 | c->io_device_weights || | |
6e866b33 | 754 | c->io_device_latencies || |
aa27b158 MP |
755 | c->io_device_limits; |
756 | } | |
757 | ||
758 | static bool cgroup_context_has_blockio_config(CGroupContext *c) { | |
759 | return c->blockio_accounting || | |
760 | c->blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID || | |
761 | c->startup_blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID || | |
762 | c->blockio_device_weights || | |
763 | c->blockio_device_bandwidths; | |
764 | } | |
765 | ||
766 | static uint64_t cgroup_context_io_weight(CGroupContext *c, ManagerState state) { | |
767 | if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && | |
768 | c->startup_io_weight != CGROUP_WEIGHT_INVALID) | |
769 | return c->startup_io_weight; | |
770 | else if (c->io_weight != CGROUP_WEIGHT_INVALID) | |
771 | return c->io_weight; | |
772 | else | |
773 | return CGROUP_WEIGHT_DEFAULT; | |
774 | } | |
775 | ||
776 | static uint64_t cgroup_context_blkio_weight(CGroupContext *c, ManagerState state) { | |
777 | if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING) && | |
778 | c->startup_blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID) | |
779 | return c->startup_blockio_weight; | |
780 | else if (c->blockio_weight != CGROUP_BLKIO_WEIGHT_INVALID) | |
781 | return c->blockio_weight; | |
782 | else | |
783 | return CGROUP_BLKIO_WEIGHT_DEFAULT; | |
784 | } | |
785 | ||
786 | static uint64_t cgroup_weight_blkio_to_io(uint64_t blkio_weight) { | |
787 | return CLAMP(blkio_weight * CGROUP_WEIGHT_DEFAULT / CGROUP_BLKIO_WEIGHT_DEFAULT, | |
788 | CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX); | |
789 | } | |
790 | ||
791 | static uint64_t cgroup_weight_io_to_blkio(uint64_t io_weight) { | |
792 | return CLAMP(io_weight * CGROUP_BLKIO_WEIGHT_DEFAULT / CGROUP_WEIGHT_DEFAULT, | |
793 | CGROUP_BLKIO_WEIGHT_MIN, CGROUP_BLKIO_WEIGHT_MAX); | |
794 | } | |
795 | ||
5a920b42 | 796 | static void cgroup_apply_io_device_weight(Unit *u, const char *dev_path, uint64_t io_weight) { |
aa27b158 MP |
797 | char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1]; |
798 | dev_t dev; | |
799 | int r; | |
800 | ||
801 | r = lookup_block_device(dev_path, &dev); | |
802 | if (r < 0) | |
803 | return; | |
804 | ||
805 | xsprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), io_weight); | |
6e866b33 | 806 | (void) set_attribute_and_warn(u, "io", "io.weight", buf); |
aa27b158 MP |
807 | } |
808 | ||
5a920b42 | 809 | static void cgroup_apply_blkio_device_weight(Unit *u, const char *dev_path, uint64_t blkio_weight) { |
aa27b158 MP |
810 | char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1]; |
811 | dev_t dev; | |
812 | int r; | |
813 | ||
814 | r = lookup_block_device(dev_path, &dev); | |
815 | if (r < 0) | |
816 | return; | |
817 | ||
818 | xsprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), blkio_weight); | |
6e866b33 MB |
819 | (void) set_attribute_and_warn(u, "blkio", "blkio.weight_device", buf); |
820 | } | |
821 | ||
822 | static void cgroup_apply_io_device_latency(Unit *u, const char *dev_path, usec_t target) { | |
823 | char buf[DECIMAL_STR_MAX(dev_t)*2+2+7+DECIMAL_STR_MAX(uint64_t)+1]; | |
824 | dev_t dev; | |
825 | int r; | |
826 | ||
827 | r = lookup_block_device(dev_path, &dev); | |
aa27b158 | 828 | if (r < 0) |
6e866b33 MB |
829 | return; |
830 | ||
831 | if (target != USEC_INFINITY) | |
832 | xsprintf(buf, "%u:%u target=%" PRIu64 "\n", major(dev), minor(dev), target); | |
833 | else | |
834 | xsprintf(buf, "%u:%u target=max\n", major(dev), minor(dev)); | |
835 | ||
836 | (void) set_attribute_and_warn(u, "io", "io.latency", buf); | |
aa27b158 MP |
837 | } |
838 | ||
b012e921 | 839 | static void cgroup_apply_io_device_limit(Unit *u, const char *dev_path, uint64_t *limits) { |
aa27b158 MP |
840 | char limit_bufs[_CGROUP_IO_LIMIT_TYPE_MAX][DECIMAL_STR_MAX(uint64_t)]; |
841 | char buf[DECIMAL_STR_MAX(dev_t)*2+2+(6+DECIMAL_STR_MAX(uint64_t)+1)*4]; | |
842 | CGroupIOLimitType type; | |
843 | dev_t dev; | |
aa27b158 MP |
844 | int r; |
845 | ||
846 | r = lookup_block_device(dev_path, &dev); | |
847 | if (r < 0) | |
b012e921 | 848 | return; |
aa27b158 | 849 | |
b012e921 MB |
850 | for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++) |
851 | if (limits[type] != cgroup_io_limit_defaults[type]) | |
aa27b158 | 852 | xsprintf(limit_bufs[type], "%" PRIu64, limits[type]); |
b012e921 | 853 | else |
aa27b158 | 854 | xsprintf(limit_bufs[type], "%s", limits[type] == CGROUP_LIMIT_MAX ? "max" : "0"); |
aa27b158 MP |
855 | |
856 | xsprintf(buf, "%u:%u rbps=%s wbps=%s riops=%s wiops=%s\n", major(dev), minor(dev), | |
857 | limit_bufs[CGROUP_IO_RBPS_MAX], limit_bufs[CGROUP_IO_WBPS_MAX], | |
858 | limit_bufs[CGROUP_IO_RIOPS_MAX], limit_bufs[CGROUP_IO_WIOPS_MAX]); | |
6e866b33 | 859 | (void) set_attribute_and_warn(u, "io", "io.max", buf); |
aa27b158 MP |
860 | } |
861 | ||
b012e921 | 862 | static void cgroup_apply_blkio_device_limit(Unit *u, const char *dev_path, uint64_t rbps, uint64_t wbps) { |
aa27b158 MP |
863 | char buf[DECIMAL_STR_MAX(dev_t)*2+2+DECIMAL_STR_MAX(uint64_t)+1]; |
864 | dev_t dev; | |
aa27b158 MP |
865 | int r; |
866 | ||
867 | r = lookup_block_device(dev_path, &dev); | |
868 | if (r < 0) | |
b012e921 | 869 | return; |
aa27b158 | 870 | |
aa27b158 | 871 | sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), rbps); |
6e866b33 | 872 | (void) set_attribute_and_warn(u, "blkio", "blkio.throttle.read_bps_device", buf); |
aa27b158 | 873 | |
aa27b158 | 874 | sprintf(buf, "%u:%u %" PRIu64 "\n", major(dev), minor(dev), wbps); |
6e866b33 | 875 | (void) set_attribute_and_warn(u, "blkio", "blkio.throttle.write_bps_device", buf); |
aa27b158 MP |
876 | } |
877 | ||
5a920b42 | 878 | static bool cgroup_context_has_unified_memory_config(CGroupContext *c) { |
6e866b33 | 879 | return c->memory_min > 0 || c->memory_low > 0 || c->memory_high != CGROUP_LIMIT_MAX || c->memory_max != CGROUP_LIMIT_MAX || c->memory_swap_max != CGROUP_LIMIT_MAX; |
5a920b42 MP |
880 | } |
881 | ||
882 | static void cgroup_apply_unified_memory_limit(Unit *u, const char *file, uint64_t v) { | |
6e866b33 | 883 | char buf[DECIMAL_STR_MAX(uint64_t) + 1] = "max\n"; |
5a920b42 MP |
884 | |
885 | if (v != CGROUP_LIMIT_MAX) | |
886 | xsprintf(buf, "%" PRIu64 "\n", v); | |
887 | ||
6e866b33 | 888 | (void) set_attribute_and_warn(u, "memory", file, buf); |
5a920b42 MP |
889 | } |
890 | ||
52ad194e | 891 | static void cgroup_apply_firewall(Unit *u) { |
52ad194e MB |
892 | assert(u); |
893 | ||
98393f85 | 894 | /* Best-effort: let's apply IP firewalling and/or accounting if that's enabled */ |
f5e65279 | 895 | |
98393f85 | 896 | if (bpf_firewall_compile(u) < 0) |
f5e65279 MB |
897 | return; |
898 | ||
899 | (void) bpf_firewall_install(u); | |
f5e65279 MB |
900 | } |
901 | ||
902 | static void cgroup_context_apply( | |
903 | Unit *u, | |
904 | CGroupMask apply_mask, | |
f5e65279 MB |
905 | ManagerState state) { |
906 | ||
5a920b42 MP |
907 | const char *path; |
908 | CGroupContext *c; | |
6e866b33 | 909 | bool is_host_root, is_local_root; |
663996b3 MS |
910 | int r; |
911 | ||
5a920b42 MP |
912 | assert(u); |
913 | ||
f5e65279 | 914 | /* Nothing to do? Exit early! */ |
6e866b33 | 915 | if (apply_mask == 0) |
14228c0d MB |
916 | return; |
917 | ||
6e866b33 MB |
918 | /* Some cgroup attributes are not supported on the host root cgroup, hence silently ignore them here. And other |
919 | * attributes should only be managed for cgroups further down the tree. */ | |
920 | is_local_root = unit_has_name(u, SPECIAL_ROOT_SLICE); | |
921 | is_host_root = unit_has_host_root_cgroup(u); | |
1d42b86d MB |
922 | |
923 | assert_se(c = unit_get_cgroup_context(u)); | |
924 | assert_se(path = u->cgroup_path); | |
925 | ||
6e866b33 | 926 | if (is_local_root) /* Make sure we don't try to display messages with an empty path. */ |
e735f4d4 MP |
927 | path = "/"; |
928 | ||
6e866b33 MB |
929 | /* We generally ignore errors caused by read-only mounted cgroup trees (assuming we are running in a container |
930 | * then), and missing cgroups, i.e. EROFS and ENOENT. */ | |
f5e65279 | 931 | |
6e866b33 MB |
932 | /* In fully unified mode these attributes don't exist on the host cgroup root. On legacy the weights exist, but |
933 | * setting the weight makes very little sense on the host root cgroup, as there are no other cgroups at this | |
934 | * level. The quota exists there too, but any attempt to write to it is refused with EINVAL. Inside of | |
7c20daf6 | 935 | * containers we want to leave control of these to the container manager (and if cgroup v2 delegation is used |
6e866b33 MB |
936 | * we couldn't even write to them if we wanted to). */ |
937 | if ((apply_mask & CGROUP_MASK_CPU) && !is_local_root) { | |
14228c0d | 938 | |
8a584da2 MP |
939 | if (cg_all_unified() > 0) { |
940 | uint64_t weight; | |
60f067b4 | 941 | |
6e866b33 | 942 | if (cgroup_context_has_cpu_weight(c)) |
8a584da2 | 943 | weight = cgroup_context_cpu_weight(c, state); |
6e866b33 MB |
944 | else if (cgroup_context_has_cpu_shares(c)) { |
945 | uint64_t shares; | |
60f067b4 | 946 | |
6e866b33 | 947 | shares = cgroup_context_cpu_shares(c, state); |
8a584da2 MP |
948 | weight = cgroup_cpu_shares_to_weight(shares); |
949 | ||
6e866b33 | 950 | log_cgroup_compat(u, "Applying [Startup]CPUShares=%" PRIu64 " as [Startup]CPUWeight=%" PRIu64 " on %s", |
8a584da2 MP |
951 | shares, weight, path); |
952 | } else | |
953 | weight = CGROUP_WEIGHT_DEFAULT; | |
954 | ||
6e866b33 | 955 | cgroup_apply_unified_cpu_weight(u, weight); |
bb4f798a | 956 | cgroup_apply_unified_cpu_quota(u, c->cpu_quota_per_sec_usec, c->cpu_quota_period_usec); |
6e866b33 | 957 | |
8a584da2 MP |
958 | } else { |
959 | uint64_t shares; | |
960 | ||
6e866b33 MB |
961 | if (cgroup_context_has_cpu_weight(c)) { |
962 | uint64_t weight; | |
8a584da2 | 963 | |
6e866b33 | 964 | weight = cgroup_context_cpu_weight(c, state); |
8a584da2 MP |
965 | shares = cgroup_cpu_weight_to_shares(weight); |
966 | ||
6e866b33 | 967 | log_cgroup_compat(u, "Applying [Startup]CPUWeight=%" PRIu64 " as [Startup]CPUShares=%" PRIu64 " on %s", |
8a584da2 | 968 | weight, shares, path); |
6e866b33 | 969 | } else if (cgroup_context_has_cpu_shares(c)) |
8a584da2 MP |
970 | shares = cgroup_context_cpu_shares(c, state); |
971 | else | |
972 | shares = CGROUP_CPU_SHARES_DEFAULT; | |
973 | ||
6e866b33 | 974 | cgroup_apply_legacy_cpu_shares(u, shares); |
bb4f798a | 975 | cgroup_apply_legacy_cpu_quota(u, c->cpu_quota_per_sec_usec, c->cpu_quota_period_usec); |
8a584da2 | 976 | } |
663996b3 MS |
977 | } |
978 | ||
7c20daf6 | 979 | /* The 'io' controller attributes are not exported on the host's root cgroup (being a pure cgroup v2 |
6e866b33 MB |
980 | * controller), and in case of containers we want to leave control of these attributes to the container manager |
981 | * (and we couldn't access that stuff anyway, even if we tried if proper delegation is used). */ | |
982 | if ((apply_mask & CGROUP_MASK_IO) && !is_local_root) { | |
983 | char buf[8+DECIMAL_STR_MAX(uint64_t)+1]; | |
984 | bool has_io, has_blockio; | |
985 | uint64_t weight; | |
663996b3 | 986 | |
6e866b33 MB |
987 | has_io = cgroup_context_has_io_config(c); |
988 | has_blockio = cgroup_context_has_blockio_config(c); | |
aa27b158 | 989 | |
6e866b33 MB |
990 | if (has_io) |
991 | weight = cgroup_context_io_weight(c, state); | |
992 | else if (has_blockio) { | |
993 | uint64_t blkio_weight; | |
5a920b42 | 994 | |
6e866b33 MB |
995 | blkio_weight = cgroup_context_blkio_weight(c, state); |
996 | weight = cgroup_weight_blkio_to_io(blkio_weight); | |
5a920b42 | 997 | |
6e866b33 MB |
998 | log_cgroup_compat(u, "Applying [Startup]BlockIOWeight=%" PRIu64 " as [Startup]IOWeight=%" PRIu64, |
999 | blkio_weight, weight); | |
1000 | } else | |
1001 | weight = CGROUP_WEIGHT_DEFAULT; | |
aa27b158 | 1002 | |
6e866b33 MB |
1003 | xsprintf(buf, "default %" PRIu64 "\n", weight); |
1004 | (void) set_attribute_and_warn(u, "io", "io.weight", buf); | |
663996b3 | 1005 | |
6e866b33 MB |
1006 | if (has_io) { |
1007 | CGroupIODeviceLatency *latency; | |
1008 | CGroupIODeviceLimit *limit; | |
1009 | CGroupIODeviceWeight *w; | |
663996b3 | 1010 | |
6e866b33 MB |
1011 | LIST_FOREACH(device_weights, w, c->io_device_weights) |
1012 | cgroup_apply_io_device_weight(u, w->path, w->weight); | |
663996b3 | 1013 | |
6e866b33 MB |
1014 | LIST_FOREACH(device_limits, limit, c->io_device_limits) |
1015 | cgroup_apply_io_device_limit(u, limit->path, limit->limits); | |
5a920b42 | 1016 | |
6e866b33 MB |
1017 | LIST_FOREACH(device_latencies, latency, c->io_device_latencies) |
1018 | cgroup_apply_io_device_latency(u, latency->path, latency->target_usec); | |
5a920b42 | 1019 | |
6e866b33 MB |
1020 | } else if (has_blockio) { |
1021 | CGroupBlockIODeviceWeight *w; | |
1022 | CGroupBlockIODeviceBandwidth *b; | |
14228c0d | 1023 | |
6e866b33 MB |
1024 | LIST_FOREACH(device_weights, w, c->blockio_device_weights) { |
1025 | weight = cgroup_weight_blkio_to_io(w->weight); | |
b012e921 | 1026 | |
6e866b33 MB |
1027 | log_cgroup_compat(u, "Applying BlockIODeviceWeight=%" PRIu64 " as IODeviceWeight=%" PRIu64 " for %s", |
1028 | w->weight, weight, w->path); | |
14228c0d | 1029 | |
6e866b33 MB |
1030 | cgroup_apply_io_device_weight(u, w->path, weight); |
1031 | } | |
14228c0d | 1032 | |
b012e921 | 1033 | LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) { |
aa27b158 MP |
1034 | uint64_t limits[_CGROUP_IO_LIMIT_TYPE_MAX]; |
1035 | CGroupIOLimitType type; | |
14228c0d | 1036 | |
aa27b158 MP |
1037 | for (type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++) |
1038 | limits[type] = cgroup_io_limit_defaults[type]; | |
1039 | ||
1040 | limits[CGROUP_IO_RBPS_MAX] = b->rbps; | |
1041 | limits[CGROUP_IO_WBPS_MAX] = b->wbps; | |
1042 | ||
6e866b33 | 1043 | log_cgroup_compat(u, "Applying BlockIO{Read|Write}Bandwidth=%" PRIu64 " %" PRIu64 " as IO{Read|Write}BandwidthMax= for %s", |
5a920b42 MP |
1044 | b->rbps, b->wbps, b->path); |
1045 | ||
b012e921 | 1046 | cgroup_apply_io_device_limit(u, b->path, limits); |
aa27b158 MP |
1047 | } |
1048 | } | |
1049 | } | |
1050 | ||
f5e65279 | 1051 | if (apply_mask & CGROUP_MASK_BLKIO) { |
6e866b33 | 1052 | bool has_io, has_blockio; |
aa27b158 | 1053 | |
6e866b33 MB |
1054 | has_io = cgroup_context_has_io_config(c); |
1055 | has_blockio = cgroup_context_has_blockio_config(c); | |
1056 | ||
1057 | /* Applying a 'weight' never makes sense for the host root cgroup, and for containers this should be | |
1058 | * left to our container manager, too. */ | |
1059 | if (!is_local_root) { | |
aa27b158 MP |
1060 | char buf[DECIMAL_STR_MAX(uint64_t)+1]; |
1061 | uint64_t weight; | |
1062 | ||
8a584da2 | 1063 | if (has_io) { |
6e866b33 | 1064 | uint64_t io_weight; |
5a920b42 | 1065 | |
6e866b33 | 1066 | io_weight = cgroup_context_io_weight(c, state); |
aa27b158 | 1067 | weight = cgroup_weight_io_to_blkio(cgroup_context_io_weight(c, state)); |
5a920b42 | 1068 | |
6e866b33 | 1069 | log_cgroup_compat(u, "Applying [Startup]IOWeight=%" PRIu64 " as [Startup]BlockIOWeight=%" PRIu64, |
5a920b42 | 1070 | io_weight, weight); |
8a584da2 MP |
1071 | } else if (has_blockio) |
1072 | weight = cgroup_context_blkio_weight(c, state); | |
1073 | else | |
aa27b158 MP |
1074 | weight = CGROUP_BLKIO_WEIGHT_DEFAULT; |
1075 | ||
1076 | xsprintf(buf, "%" PRIu64 "\n", weight); | |
6e866b33 | 1077 | (void) set_attribute_and_warn(u, "blkio", "blkio.weight", buf); |
aa27b158 | 1078 | |
8a584da2 | 1079 | if (has_io) { |
aa27b158 MP |
1080 | CGroupIODeviceWeight *w; |
1081 | ||
5a920b42 MP |
1082 | LIST_FOREACH(device_weights, w, c->io_device_weights) { |
1083 | weight = cgroup_weight_io_to_blkio(w->weight); | |
1084 | ||
6e866b33 | 1085 | log_cgroup_compat(u, "Applying IODeviceWeight=%" PRIu64 " as BlockIODeviceWeight=%" PRIu64 " for %s", |
5a920b42 MP |
1086 | w->weight, weight, w->path); |
1087 | ||
1088 | cgroup_apply_blkio_device_weight(u, w->path, weight); | |
1089 | } | |
8a584da2 MP |
1090 | } else if (has_blockio) { |
1091 | CGroupBlockIODeviceWeight *w; | |
1092 | ||
8a584da2 MP |
1093 | LIST_FOREACH(device_weights, w, c->blockio_device_weights) |
1094 | cgroup_apply_blkio_device_weight(u, w->path, w->weight); | |
aa27b158 MP |
1095 | } |
1096 | } | |
1097 | ||
6e866b33 MB |
1098 | /* The bandwith limits are something that make sense to be applied to the host's root but not container |
1099 | * roots, as there we want the container manager to handle it */ | |
1100 | if (is_host_root || !is_local_root) { | |
1101 | if (has_io) { | |
1102 | CGroupIODeviceLimit *l; | |
aa27b158 | 1103 | |
6e866b33 MB |
1104 | LIST_FOREACH(device_limits, l, c->io_device_limits) { |
1105 | log_cgroup_compat(u, "Applying IO{Read|Write}Bandwidth=%" PRIu64 " %" PRIu64 " as BlockIO{Read|Write}BandwidthMax= for %s", | |
1106 | l->limits[CGROUP_IO_RBPS_MAX], l->limits[CGROUP_IO_WBPS_MAX], l->path); | |
5a920b42 | 1107 | |
6e866b33 MB |
1108 | cgroup_apply_blkio_device_limit(u, l->path, l->limits[CGROUP_IO_RBPS_MAX], l->limits[CGROUP_IO_WBPS_MAX]); |
1109 | } | |
1110 | } else if (has_blockio) { | |
1111 | CGroupBlockIODeviceBandwidth *b; | |
8a584da2 | 1112 | |
6e866b33 MB |
1113 | LIST_FOREACH(device_bandwidths, b, c->blockio_device_bandwidths) |
1114 | cgroup_apply_blkio_device_limit(u, b->path, b->rbps, b->wbps); | |
1115 | } | |
14228c0d MB |
1116 | } |
1117 | } | |
1118 | ||
6e866b33 MB |
1119 | /* In unified mode 'memory' attributes do not exist on the root cgroup. In legacy mode 'memory.limit_in_bytes' |
1120 | * exists on the root cgroup, but any writes to it are refused with EINVAL. And if we run in a container we | |
7c20daf6 | 1121 | * want to leave control to the container manager (and if proper cgroup v2 delegation is used we couldn't even |
6e866b33 MB |
1122 | * write to this if we wanted to.) */ |
1123 | if ((apply_mask & CGROUP_MASK_MEMORY) && !is_local_root) { | |
1124 | ||
8a584da2 | 1125 | if (cg_all_unified() > 0) { |
2897b343 | 1126 | uint64_t max, swap_max = CGROUP_LIMIT_MAX; |
14228c0d | 1127 | |
8a584da2 | 1128 | if (cgroup_context_has_unified_memory_config(c)) { |
5a920b42 | 1129 | max = c->memory_max; |
8a584da2 MP |
1130 | swap_max = c->memory_swap_max; |
1131 | } else { | |
5a920b42 | 1132 | max = c->memory_limit; |
d9dfd233 | 1133 | |
5a920b42 | 1134 | if (max != CGROUP_LIMIT_MAX) |
6e866b33 | 1135 | log_cgroup_compat(u, "Applying MemoryLimit=%" PRIu64 " as MemoryMax=", max); |
5a920b42 | 1136 | } |
d9dfd233 | 1137 | |
6e866b33 | 1138 | cgroup_apply_unified_memory_limit(u, "memory.min", c->memory_min); |
5a920b42 MP |
1139 | cgroup_apply_unified_memory_limit(u, "memory.low", c->memory_low); |
1140 | cgroup_apply_unified_memory_limit(u, "memory.high", c->memory_high); | |
1141 | cgroup_apply_unified_memory_limit(u, "memory.max", max); | |
8a584da2 | 1142 | cgroup_apply_unified_memory_limit(u, "memory.swap.max", swap_max); |
6e866b33 | 1143 | |
d9dfd233 | 1144 | } else { |
5a920b42 | 1145 | char buf[DECIMAL_STR_MAX(uint64_t) + 1]; |
8a584da2 | 1146 | uint64_t val; |
5a920b42 | 1147 | |
8a584da2 | 1148 | if (cgroup_context_has_unified_memory_config(c)) { |
5a920b42 | 1149 | val = c->memory_max; |
6e866b33 | 1150 | log_cgroup_compat(u, "Applying MemoryMax=%" PRIi64 " as MemoryLimit=", val); |
8a584da2 MP |
1151 | } else |
1152 | val = c->memory_limit; | |
5a920b42 MP |
1153 | |
1154 | if (val == CGROUP_LIMIT_MAX) | |
1155 | strncpy(buf, "-1\n", sizeof(buf)); | |
d9dfd233 | 1156 | else |
5a920b42 | 1157 | xsprintf(buf, "%" PRIu64 "\n", val); |
663996b3 | 1158 | |
6e866b33 | 1159 | (void) set_attribute_and_warn(u, "memory", "memory.limit_in_bytes", buf); |
5a920b42 | 1160 | } |
663996b3 MS |
1161 | } |
1162 | ||
7c20daf6 | 1163 | /* On cgroup v2 we can apply BPF everywhere. On cgroup v1 we apply it everywhere except for the root of |
6e866b33 MB |
1164 | * containers, where we leave this to the manager */ |
1165 | if ((apply_mask & (CGROUP_MASK_DEVICES | CGROUP_MASK_BPF_DEVICES)) && | |
1166 | (is_host_root || cg_all_unified() > 0 || !is_local_root)) { | |
1167 | _cleanup_(bpf_program_unrefp) BPFProgram *prog = NULL; | |
14228c0d MB |
1168 | CGroupDeviceAllow *a; |
1169 | ||
6e866b33 MB |
1170 | if (cg_all_unified() > 0) { |
1171 | r = cgroup_init_device_bpf(&prog, c->device_policy, c->device_allow); | |
1172 | if (r < 0) | |
1173 | log_unit_warning_errno(u, r, "Failed to initialize device control bpf program: %m"); | |
1174 | } else { | |
1175 | /* Changing the devices list of a populated cgroup might result in EINVAL, hence ignore EINVAL | |
1176 | * here. */ | |
e735f4d4 | 1177 | |
6e866b33 MB |
1178 | if (c->device_allow || c->device_policy != CGROUP_AUTO) |
1179 | r = cg_set_attribute("devices", path, "devices.deny", "a"); | |
1180 | else | |
1181 | r = cg_set_attribute("devices", path, "devices.allow", "a"); | |
1182 | if (r < 0) | |
1183 | log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING, r, | |
1184 | "Failed to reset devices.allow/devices.deny: %m"); | |
1185 | } | |
14228c0d MB |
1186 | |
1187 | if (c->device_policy == CGROUP_CLOSED || | |
1188 | (c->device_policy == CGROUP_AUTO && c->device_allow)) { | |
1189 | static const char auto_devices[] = | |
60f067b4 JS |
1190 | "/dev/null\0" "rwm\0" |
1191 | "/dev/zero\0" "rwm\0" | |
1192 | "/dev/full\0" "rwm\0" | |
1193 | "/dev/random\0" "rwm\0" | |
1194 | "/dev/urandom\0" "rwm\0" | |
1195 | "/dev/tty\0" "rwm\0" | |
1d42b86d | 1196 | "/dev/ptmx\0" "rwm\0" |
5a920b42 | 1197 | /* Allow /run/systemd/inaccessible/{chr,blk} devices for mapping InaccessiblePaths */ |
6e866b33 MB |
1198 | "/run/systemd/inaccessible/chr\0" "rwm\0" |
1199 | "/run/systemd/inaccessible/blk\0" "rwm\0"; | |
14228c0d MB |
1200 | |
1201 | const char *x, *y; | |
1202 | ||
1203 | NULSTR_FOREACH_PAIR(x, y, auto_devices) | |
6e866b33 | 1204 | (void) whitelist_device(prog, path, x, y); |
60f067b4 | 1205 | |
1d42b86d | 1206 | /* PTS (/dev/pts) devices may not be duplicated, but accessed */ |
6e866b33 | 1207 | (void) whitelist_major(prog, path, "pts", 'c', "rw"); |
14228c0d MB |
1208 | } |
1209 | ||
1210 | LIST_FOREACH(device_allow, a, c->device_allow) { | |
2897b343 | 1211 | char acc[4], *val; |
14228c0d MB |
1212 | unsigned k = 0; |
1213 | ||
1214 | if (a->r) | |
1215 | acc[k++] = 'r'; | |
1216 | if (a->w) | |
1217 | acc[k++] = 'w'; | |
1218 | if (a->m) | |
1219 | acc[k++] = 'm'; | |
1220 | ||
1221 | if (k == 0) | |
1222 | continue; | |
1223 | ||
1224 | acc[k++] = 0; | |
60f067b4 | 1225 | |
f5e65279 | 1226 | if (path_startswith(a->path, "/dev/")) |
6e866b33 | 1227 | (void) whitelist_device(prog, path, a->path, acc); |
2897b343 | 1228 | else if ((val = startswith(a->path, "block-"))) |
6e866b33 | 1229 | (void) whitelist_major(prog, path, val, 'b', acc); |
2897b343 | 1230 | else if ((val = startswith(a->path, "char-"))) |
6e866b33 | 1231 | (void) whitelist_major(prog, path, val, 'c', acc); |
60f067b4 | 1232 | else |
6e866b33 MB |
1233 | log_unit_debug(u, "Ignoring device '%s' while writing cgroup attribute.", a->path); |
1234 | } | |
1235 | ||
1236 | r = cgroup_apply_device_bpf(u, prog, c->device_policy, c->device_allow); | |
1237 | if (r < 0) { | |
1238 | static bool warned = false; | |
1239 | ||
1240 | log_full_errno(warned ? LOG_DEBUG : LOG_WARNING, r, | |
1241 | "Unit %s configures device ACL, but the local system doesn't seem to support the BPF-based device controller.\n" | |
1242 | "Proceeding WITHOUT applying ACL (all devices will be accessible)!\n" | |
1243 | "(This warning is only shown for the first loaded unit using device ACL.)", u->id); | |
1244 | ||
1245 | warned = true; | |
14228c0d MB |
1246 | } |
1247 | } | |
6300502b | 1248 | |
1d42b86d MB |
1249 | if (apply_mask & CGROUP_MASK_PIDS) { |
1250 | ||
6e866b33 | 1251 | if (is_host_root) { |
1d42b86d MB |
1252 | /* So, the "pids" controller does not expose anything on the root cgroup, in order not to |
1253 | * replicate knobs exposed elsewhere needlessly. We abstract this away here however, and when | |
1254 | * the knobs of the root cgroup are modified propagate this to the relevant sysctls. There's a | |
1255 | * non-obvious asymmetry however: unlike the cgroup properties we don't really want to take | |
1256 | * exclusive ownership of the sysctls, but we still want to honour things if the user sets | |
1257 | * limits. Hence we employ sort of a one-way strategy: when the user sets a bounded limit | |
1258 | * through us it counts. When the user afterwards unsets it again (i.e. sets it to unbounded) | |
1259 | * it also counts. But if the user never set a limit through us (i.e. we are the default of | |
1260 | * "unbounded") we leave things unmodified. For this we manage a global boolean that we turn on | |
1261 | * the first time we set a limit. Note that this boolean is flushed out on manager reload, | |
1262 | * which is desirable so that there's an offical way to release control of the sysctl from | |
1263 | * systemd: set the limit to unbounded and reload. */ | |
1264 | ||
1265 | if (c->tasks_max != CGROUP_LIMIT_MAX) { | |
1266 | u->manager->sysctl_pid_max_changed = true; | |
1267 | r = procfs_tasks_set_limit(c->tasks_max); | |
1268 | } else if (u->manager->sysctl_pid_max_changed) | |
1269 | r = procfs_tasks_set_limit(TASKS_MAX); | |
1270 | else | |
1271 | r = 0; | |
1d42b86d | 1272 | if (r < 0) |
6e866b33 | 1273 | log_unit_full(u, LOG_LEVEL_CGROUP_WRITE(r), r, |
1d42b86d | 1274 | "Failed to write to tasks limit sysctls: %m"); |
6e866b33 | 1275 | } |
6300502b | 1276 | |
6e866b33 MB |
1277 | /* The attribute itself is not available on the host root cgroup, and in the container case we want to |
1278 | * leave it for the container manager. */ | |
1279 | if (!is_local_root) { | |
1d42b86d MB |
1280 | if (c->tasks_max != CGROUP_LIMIT_MAX) { |
1281 | char buf[DECIMAL_STR_MAX(uint64_t) + 2]; | |
6300502b | 1282 | |
1d42b86d | 1283 | sprintf(buf, "%" PRIu64 "\n", c->tasks_max); |
6e866b33 | 1284 | (void) set_attribute_and_warn(u, "pids", "pids.max", buf); |
1d42b86d | 1285 | } else |
6e866b33 | 1286 | (void) set_attribute_and_warn(u, "pids", "pids.max", "max\n"); |
1d42b86d | 1287 | } |
6300502b | 1288 | } |
f5e65279 | 1289 | |
6e866b33 | 1290 | if (apply_mask & CGROUP_MASK_BPF_FIREWALL) |
52ad194e | 1291 | cgroup_apply_firewall(u); |
663996b3 MS |
1292 | } |
1293 | ||
6e866b33 MB |
1294 | static bool unit_get_needs_bpf_firewall(Unit *u) { |
1295 | CGroupContext *c; | |
1296 | Unit *p; | |
1297 | assert(u); | |
1298 | ||
1299 | c = unit_get_cgroup_context(u); | |
1300 | if (!c) | |
1301 | return false; | |
1302 | ||
1303 | if (c->ip_accounting || | |
1304 | c->ip_address_allow || | |
1305 | c->ip_address_deny) | |
1306 | return true; | |
1307 | ||
1308 | /* If any parent slice has an IP access list defined, it applies too */ | |
1309 | for (p = UNIT_DEREF(u->slice); p; p = UNIT_DEREF(p->slice)) { | |
1310 | c = unit_get_cgroup_context(p); | |
1311 | if (!c) | |
1312 | return false; | |
1313 | ||
1314 | if (c->ip_address_allow || | |
1315 | c->ip_address_deny) | |
1316 | return true; | |
1317 | } | |
1318 | ||
1319 | return false; | |
1320 | } | |
1321 | ||
1322 | static CGroupMask cgroup_context_get_mask(CGroupContext *c) { | |
d9dfd233 | 1323 | CGroupMask mask = 0; |
14228c0d | 1324 | |
6e866b33 MB |
1325 | /* Figure out which controllers we need, based on the cgroup context object */ |
1326 | ||
1327 | if (c->cpu_accounting) | |
1328 | mask |= get_cpu_accounting_mask(); | |
14228c0d | 1329 | |
6e866b33 | 1330 | if (cgroup_context_has_cpu_weight(c) || |
8a584da2 | 1331 | cgroup_context_has_cpu_shares(c) || |
5eef597e | 1332 | c->cpu_quota_per_sec_usec != USEC_INFINITY) |
6e866b33 | 1333 | mask |= CGROUP_MASK_CPU; |
14228c0d | 1334 | |
aa27b158 MP |
1335 | if (cgroup_context_has_io_config(c) || cgroup_context_has_blockio_config(c)) |
1336 | mask |= CGROUP_MASK_IO | CGROUP_MASK_BLKIO; | |
14228c0d MB |
1337 | |
1338 | if (c->memory_accounting || | |
5a920b42 MP |
1339 | c->memory_limit != CGROUP_LIMIT_MAX || |
1340 | cgroup_context_has_unified_memory_config(c)) | |
d9dfd233 | 1341 | mask |= CGROUP_MASK_MEMORY; |
14228c0d | 1342 | |
f47781d8 MP |
1343 | if (c->device_allow || |
1344 | c->device_policy != CGROUP_AUTO) | |
6e866b33 | 1345 | mask |= CGROUP_MASK_DEVICES | CGROUP_MASK_BPF_DEVICES; |
6300502b MP |
1346 | |
1347 | if (c->tasks_accounting || | |
1d42b86d | 1348 | c->tasks_max != CGROUP_LIMIT_MAX) |
6300502b MP |
1349 | mask |= CGROUP_MASK_PIDS; |
1350 | ||
6e866b33 MB |
1351 | return CGROUP_MASK_EXTEND_JOINED(mask); |
1352 | } | |
1353 | ||
1354 | static CGroupMask unit_get_bpf_mask(Unit *u) { | |
1355 | CGroupMask mask = 0; | |
1356 | ||
1357 | /* Figure out which controllers we need, based on the cgroup context, possibly taking into account children | |
1358 | * too. */ | |
1359 | ||
1360 | if (unit_get_needs_bpf_firewall(u)) | |
1361 | mask |= CGROUP_MASK_BPF_FIREWALL; | |
1362 | ||
14228c0d | 1363 | return mask; |
663996b3 MS |
1364 | } |
1365 | ||
d9dfd233 | 1366 | CGroupMask unit_get_own_mask(Unit *u) { |
14228c0d | 1367 | CGroupContext *c; |
663996b3 | 1368 | |
6e866b33 MB |
1369 | /* Returns the mask of controllers the unit needs for itself. If a unit is not properly loaded, return an empty |
1370 | * mask, as we shouldn't reflect it in the cgroup hierarchy then. */ | |
1371 | ||
1372 | if (u->load_state != UNIT_LOADED) | |
1373 | return 0; | |
d9dfd233 | 1374 | |
14228c0d MB |
1375 | c = unit_get_cgroup_context(u); |
1376 | if (!c) | |
1377 | return 0; | |
663996b3 | 1378 | |
6e866b33 | 1379 | return (cgroup_context_get_mask(c) | unit_get_bpf_mask(u) | unit_get_delegate_mask(u)) & ~unit_get_ancestor_disable_mask(u); |
52ad194e MB |
1380 | } |
1381 | ||
1382 | CGroupMask unit_get_delegate_mask(Unit *u) { | |
1383 | CGroupContext *c; | |
1384 | ||
1385 | /* If delegation is turned on, then turn on selected controllers, unless we are on the legacy hierarchy and the | |
1386 | * process we fork into is known to drop privileges, and hence shouldn't get access to the controllers. | |
d9dfd233 | 1387 | * |
52ad194e | 1388 | * Note that on the unified hierarchy it is safe to delegate controllers to unprivileged services. */ |
f47781d8 | 1389 | |
98393f85 | 1390 | if (!unit_cgroup_delegate(u)) |
52ad194e MB |
1391 | return 0; |
1392 | ||
1393 | if (cg_all_unified() <= 0) { | |
f47781d8 MP |
1394 | ExecContext *e; |
1395 | ||
1396 | e = unit_get_exec_context(u); | |
52ad194e MB |
1397 | if (e && !exec_context_maintains_privileges(e)) |
1398 | return 0; | |
f47781d8 MP |
1399 | } |
1400 | ||
98393f85 | 1401 | assert_se(c = unit_get_cgroup_context(u)); |
6e866b33 | 1402 | return CGROUP_MASK_EXTEND_JOINED(c->delegate_controllers); |
663996b3 MS |
1403 | } |
1404 | ||
d9dfd233 | 1405 | CGroupMask unit_get_members_mask(Unit *u) { |
14228c0d MB |
1406 | assert(u); |
1407 | ||
52ad194e | 1408 | /* Returns the mask of controllers all of the unit's children require, merged */ |
d9dfd233 | 1409 | |
60f067b4 | 1410 | if (u->cgroup_members_mask_valid) |
6e866b33 | 1411 | return u->cgroup_members_mask; /* Use cached value if possible */ |
14228c0d | 1412 | |
60f067b4 JS |
1413 | u->cgroup_members_mask = 0; |
1414 | ||
1415 | if (u->type == UNIT_SLICE) { | |
52ad194e | 1416 | void *v; |
60f067b4 JS |
1417 | Unit *member; |
1418 | Iterator i; | |
1419 | ||
52ad194e | 1420 | HASHMAP_FOREACH_KEY(v, member, u->dependencies[UNIT_BEFORE], i) { |
6e866b33 MB |
1421 | if (UNIT_DEREF(member->slice) == u) |
1422 | u->cgroup_members_mask |= unit_get_subtree_mask(member); /* note that this calls ourselves again, for the children */ | |
60f067b4 | 1423 | } |
663996b3 MS |
1424 | } |
1425 | ||
60f067b4 JS |
1426 | u->cgroup_members_mask_valid = true; |
1427 | return u->cgroup_members_mask; | |
1428 | } | |
1429 | ||
d9dfd233 | 1430 | CGroupMask unit_get_siblings_mask(Unit *u) { |
60f067b4 JS |
1431 | assert(u); |
1432 | ||
d9dfd233 MP |
1433 | /* Returns the mask of controllers all of the unit's siblings |
1434 | * require, i.e. the members mask of the unit's parent slice | |
1435 | * if there is one. */ | |
1436 | ||
60f067b4 JS |
1437 | if (UNIT_ISSET(u->slice)) |
1438 | return unit_get_members_mask(UNIT_DEREF(u->slice)); | |
1439 | ||
52ad194e | 1440 | return unit_get_subtree_mask(u); /* we are the top-level slice */ |
d9dfd233 MP |
1441 | } |
1442 | ||
6e866b33 MB |
1443 | CGroupMask unit_get_disable_mask(Unit *u) { |
1444 | CGroupContext *c; | |
1445 | ||
1446 | c = unit_get_cgroup_context(u); | |
1447 | if (!c) | |
1448 | return 0; | |
1449 | ||
1450 | return c->disable_controllers; | |
1451 | } | |
1452 | ||
1453 | CGroupMask unit_get_ancestor_disable_mask(Unit *u) { | |
1454 | CGroupMask mask; | |
1455 | ||
1456 | assert(u); | |
1457 | mask = unit_get_disable_mask(u); | |
1458 | ||
1459 | /* Returns the mask of controllers which are marked as forcibly | |
1460 | * disabled in any ancestor unit or the unit in question. */ | |
1461 | ||
1462 | if (UNIT_ISSET(u->slice)) | |
1463 | mask |= unit_get_ancestor_disable_mask(UNIT_DEREF(u->slice)); | |
1464 | ||
1465 | return mask; | |
1466 | } | |
1467 | ||
d9dfd233 MP |
1468 | CGroupMask unit_get_subtree_mask(Unit *u) { |
1469 | ||
1470 | /* Returns the mask of this subtree, meaning of the group | |
1471 | * itself and its children. */ | |
1472 | ||
1473 | return unit_get_own_mask(u) | unit_get_members_mask(u); | |
60f067b4 JS |
1474 | } |
1475 | ||
d9dfd233 MP |
1476 | CGroupMask unit_get_target_mask(Unit *u) { |
1477 | CGroupMask mask; | |
1478 | ||
1479 | /* This returns the cgroup mask of all controllers to enable | |
1480 | * for a specific cgroup, i.e. everything it needs itself, | |
1481 | * plus all that its children need, plus all that its siblings | |
1482 | * need. This is primarily useful on the legacy cgroup | |
1483 | * hierarchy, where we need to duplicate each cgroup in each | |
1484 | * hierarchy that shall be enabled for it. */ | |
60f067b4 | 1485 | |
d9dfd233 MP |
1486 | mask = unit_get_own_mask(u) | unit_get_members_mask(u) | unit_get_siblings_mask(u); |
1487 | mask &= u->manager->cgroup_supported; | |
6e866b33 | 1488 | mask &= ~unit_get_ancestor_disable_mask(u); |
d9dfd233 MP |
1489 | |
1490 | return mask; | |
1491 | } | |
1492 | ||
1493 | CGroupMask unit_get_enable_mask(Unit *u) { | |
1494 | CGroupMask mask; | |
1495 | ||
1496 | /* This returns the cgroup mask of all controllers to enable | |
1497 | * for the children of a specific cgroup. This is primarily | |
1498 | * useful for the unified cgroup hierarchy, where each cgroup | |
1499 | * controls which controllers are enabled for its children. */ | |
1500 | ||
1501 | mask = unit_get_members_mask(u); | |
60f067b4 | 1502 | mask &= u->manager->cgroup_supported; |
6e866b33 | 1503 | mask &= ~unit_get_ancestor_disable_mask(u); |
60f067b4 | 1504 | |
14228c0d | 1505 | return mask; |
663996b3 MS |
1506 | } |
1507 | ||
6e866b33 | 1508 | void unit_invalidate_cgroup_members_masks(Unit *u) { |
f5e65279 MB |
1509 | assert(u); |
1510 | ||
6e866b33 MB |
1511 | /* Recurse invalidate the member masks cache all the way up the tree */ |
1512 | u->cgroup_members_mask_valid = false; | |
f5e65279 | 1513 | |
6e866b33 MB |
1514 | if (UNIT_ISSET(u->slice)) |
1515 | unit_invalidate_cgroup_members_masks(UNIT_DEREF(u->slice)); | |
60f067b4 JS |
1516 | } |
1517 | ||
98393f85 | 1518 | const char *unit_get_realized_cgroup_path(Unit *u, CGroupMask mask) { |
60f067b4 | 1519 | |
98393f85 | 1520 | /* Returns the realized cgroup path of the specified unit where all specified controllers are available. */ |
60f067b4 JS |
1521 | |
1522 | while (u) { | |
98393f85 | 1523 | |
60f067b4 JS |
1524 | if (u->cgroup_path && |
1525 | u->cgroup_realized && | |
b012e921 | 1526 | FLAGS_SET(u->cgroup_realized_mask, mask)) |
60f067b4 | 1527 | return u->cgroup_path; |
663996b3 | 1528 | |
60f067b4 JS |
1529 | u = UNIT_DEREF(u->slice); |
1530 | } | |
1531 | ||
1532 | return NULL; | |
663996b3 MS |
1533 | } |
1534 | ||
98393f85 MB |
1535 | static const char *migrate_callback(CGroupMask mask, void *userdata) { |
1536 | return unit_get_realized_cgroup_path(userdata, mask); | |
1537 | } | |
1538 | ||
6e866b33 | 1539 | char *unit_default_cgroup_path(const Unit *u) { |
d9dfd233 MP |
1540 | _cleanup_free_ char *escaped = NULL, *slice = NULL; |
1541 | int r; | |
1542 | ||
1543 | assert(u); | |
1544 | ||
1545 | if (unit_has_name(u, SPECIAL_ROOT_SLICE)) | |
1546 | return strdup(u->manager->cgroup_root); | |
1547 | ||
1548 | if (UNIT_ISSET(u->slice) && !unit_has_name(UNIT_DEREF(u->slice), SPECIAL_ROOT_SLICE)) { | |
1549 | r = cg_slice_to_path(UNIT_DEREF(u->slice)->id, &slice); | |
1550 | if (r < 0) | |
1551 | return NULL; | |
1552 | } | |
1553 | ||
1554 | escaped = cg_escape(u->id); | |
1555 | if (!escaped) | |
1556 | return NULL; | |
1557 | ||
1558 | if (slice) | |
2897b343 MP |
1559 | return strjoin(u->manager->cgroup_root, "/", slice, "/", |
1560 | escaped); | |
d9dfd233 | 1561 | else |
2897b343 | 1562 | return strjoin(u->manager->cgroup_root, "/", escaped); |
d9dfd233 MP |
1563 | } |
1564 | ||
1565 | int unit_set_cgroup_path(Unit *u, const char *path) { | |
1566 | _cleanup_free_ char *p = NULL; | |
1567 | int r; | |
1568 | ||
1569 | assert(u); | |
1570 | ||
1571 | if (path) { | |
1572 | p = strdup(path); | |
1573 | if (!p) | |
1574 | return -ENOMEM; | |
1575 | } else | |
1576 | p = NULL; | |
1577 | ||
1578 | if (streq_ptr(u->cgroup_path, p)) | |
1579 | return 0; | |
1580 | ||
1581 | if (p) { | |
1582 | r = hashmap_put(u->manager->cgroup_unit, p, u); | |
1583 | if (r < 0) | |
1584 | return r; | |
1585 | } | |
1586 | ||
1587 | unit_release_cgroup(u); | |
1588 | ||
b012e921 | 1589 | u->cgroup_path = TAKE_PTR(p); |
d9dfd233 MP |
1590 | |
1591 | return 1; | |
1592 | } | |
1593 | ||
1594 | int unit_watch_cgroup(Unit *u) { | |
aa27b158 | 1595 | _cleanup_free_ char *events = NULL; |
d9dfd233 MP |
1596 | int r; |
1597 | ||
1598 | assert(u); | |
1599 | ||
1600 | if (!u->cgroup_path) | |
1601 | return 0; | |
1602 | ||
1603 | if (u->cgroup_inotify_wd >= 0) | |
1604 | return 0; | |
1605 | ||
1606 | /* Only applies to the unified hierarchy */ | |
2897b343 | 1607 | r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER); |
d9dfd233 | 1608 | if (r < 0) |
2897b343 | 1609 | return log_error_errno(r, "Failed to determine whether the name=systemd hierarchy is unified: %m"); |
d9dfd233 MP |
1610 | if (r == 0) |
1611 | return 0; | |
1612 | ||
1613 | /* Don't watch the root slice, it's pointless. */ | |
1614 | if (unit_has_name(u, SPECIAL_ROOT_SLICE)) | |
1615 | return 0; | |
1616 | ||
1617 | r = hashmap_ensure_allocated(&u->manager->cgroup_inotify_wd_unit, &trivial_hash_ops); | |
1618 | if (r < 0) | |
1619 | return log_oom(); | |
1620 | ||
aa27b158 | 1621 | r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, "cgroup.events", &events); |
d9dfd233 MP |
1622 | if (r < 0) |
1623 | return log_oom(); | |
1624 | ||
aa27b158 | 1625 | u->cgroup_inotify_wd = inotify_add_watch(u->manager->cgroup_inotify_fd, events, IN_MODIFY); |
d9dfd233 MP |
1626 | if (u->cgroup_inotify_wd < 0) { |
1627 | ||
1628 | if (errno == ENOENT) /* If the directory is already | |
1629 | * gone we don't need to track | |
1630 | * it, so this is not an error */ | |
1631 | return 0; | |
1632 | ||
1633 | return log_unit_error_errno(u, errno, "Failed to add inotify watch descriptor for control group %s: %m", u->cgroup_path); | |
1634 | } | |
1635 | ||
1636 | r = hashmap_put(u->manager->cgroup_inotify_wd_unit, INT_TO_PTR(u->cgroup_inotify_wd), u); | |
1637 | if (r < 0) | |
1638 | return log_unit_error_errno(u, r, "Failed to add inotify watch descriptor to hash map: %m"); | |
1639 | ||
1640 | return 0; | |
1641 | } | |
1642 | ||
52ad194e MB |
1643 | int unit_pick_cgroup_path(Unit *u) { |
1644 | _cleanup_free_ char *path = NULL; | |
1645 | int r; | |
1646 | ||
1647 | assert(u); | |
1648 | ||
1649 | if (u->cgroup_path) | |
1650 | return 0; | |
1651 | ||
1652 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) | |
1653 | return -EINVAL; | |
1654 | ||
1655 | path = unit_default_cgroup_path(u); | |
1656 | if (!path) | |
1657 | return log_oom(); | |
1658 | ||
1659 | r = unit_set_cgroup_path(u, path); | |
1660 | if (r == -EEXIST) | |
1661 | return log_unit_error_errno(u, r, "Control group %s exists already.", path); | |
1662 | if (r < 0) | |
1663 | return log_unit_error_errno(u, r, "Failed to set unit's control group path to %s: %m", path); | |
1664 | ||
1665 | return 0; | |
1666 | } | |
1667 | ||
d9dfd233 MP |
1668 | static int unit_create_cgroup( |
1669 | Unit *u, | |
1670 | CGroupMask target_mask, | |
f5e65279 | 1671 | CGroupMask enable_mask, |
6e866b33 | 1672 | ManagerState state) { |
d9dfd233 | 1673 | |
b012e921 | 1674 | bool created; |
6e866b33 | 1675 | int r; |
14228c0d MB |
1676 | |
1677 | assert(u); | |
1678 | ||
6e866b33 | 1679 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
f47781d8 MP |
1680 | return 0; |
1681 | ||
52ad194e MB |
1682 | /* Figure out our cgroup path */ |
1683 | r = unit_pick_cgroup_path(u); | |
1684 | if (r < 0) | |
1685 | return r; | |
663996b3 | 1686 | |
60f067b4 | 1687 | /* First, create our own group */ |
d9dfd233 MP |
1688 | r = cg_create_everywhere(u->manager->cgroup_supported, target_mask, u->cgroup_path); |
1689 | if (r < 0) | |
1690 | return log_unit_error_errno(u, r, "Failed to create cgroup %s: %m", u->cgroup_path); | |
6e866b33 | 1691 | created = r; |
d9dfd233 MP |
1692 | |
1693 | /* Start watching it */ | |
1694 | (void) unit_watch_cgroup(u); | |
1695 | ||
b012e921 | 1696 | /* Preserve enabled controllers in delegated units, adjust others. */ |
6e866b33 MB |
1697 | if (created || !u->cgroup_realized || !unit_cgroup_delegate(u)) { |
1698 | CGroupMask result_mask = 0; | |
b012e921 MB |
1699 | |
1700 | /* Enable all controllers we need */ | |
6e866b33 | 1701 | r = cg_enable_everywhere(u->manager->cgroup_supported, enable_mask, u->cgroup_path, &result_mask); |
b012e921 | 1702 | if (r < 0) |
6e866b33 MB |
1703 | log_unit_warning_errno(u, r, "Failed to enable/disable controllers on cgroup %s, ignoring: %m", u->cgroup_path); |
1704 | ||
1705 | /* If we just turned off a controller, this might release the controller for our parent too, let's | |
1706 | * enqueue the parent for re-realization in that case again. */ | |
1707 | if (UNIT_ISSET(u->slice)) { | |
1708 | CGroupMask turned_off; | |
1709 | ||
1710 | turned_off = (u->cgroup_realized ? u->cgroup_enabled_mask & ~result_mask : 0); | |
1711 | if (turned_off != 0) { | |
1712 | Unit *parent; | |
1713 | ||
1714 | /* Force the parent to propagate the enable mask to the kernel again, by invalidating | |
1715 | * the controller we just turned off. */ | |
1716 | ||
1717 | for (parent = UNIT_DEREF(u->slice); parent; parent = UNIT_DEREF(parent->slice)) | |
1718 | unit_invalidate_cgroup(parent, turned_off); | |
1719 | } | |
1720 | } | |
1721 | ||
1722 | /* Remember what's actually enabled now */ | |
1723 | u->cgroup_enabled_mask = result_mask; | |
b012e921 | 1724 | } |
14228c0d | 1725 | |
60f067b4 | 1726 | /* Keep track that this is now realized */ |
14228c0d | 1727 | u->cgroup_realized = true; |
d9dfd233 | 1728 | u->cgroup_realized_mask = target_mask; |
60f067b4 | 1729 | |
98393f85 | 1730 | if (u->type != UNIT_SLICE && !unit_cgroup_delegate(u)) { |
f47781d8 MP |
1731 | |
1732 | /* Then, possibly move things over, but not if | |
1733 | * subgroups may contain processes, which is the case | |
1734 | * for slice and delegation units. */ | |
1735 | r = cg_migrate_everywhere(u->manager->cgroup_supported, u->cgroup_path, u->cgroup_path, migrate_callback, u); | |
1736 | if (r < 0) | |
d9dfd233 | 1737 | log_unit_warning_errno(u, r, "Failed to migrate cgroup from to %s, ignoring: %m", u->cgroup_path); |
f47781d8 MP |
1738 | } |
1739 | ||
6e866b33 MB |
1740 | /* Set attributes */ |
1741 | cgroup_context_apply(u, target_mask, state); | |
1742 | cgroup_xattr_apply(u); | |
1743 | ||
f47781d8 MP |
1744 | return 0; |
1745 | } | |
1746 | ||
98393f85 MB |
1747 | static int unit_attach_pid_to_cgroup_via_bus(Unit *u, pid_t pid, const char *suffix_path) { |
1748 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1749 | char *pp; | |
f47781d8 | 1750 | int r; |
98393f85 | 1751 | |
f47781d8 MP |
1752 | assert(u); |
1753 | ||
98393f85 MB |
1754 | if (MANAGER_IS_SYSTEM(u->manager)) |
1755 | return -EINVAL; | |
1756 | ||
1757 | if (!u->manager->system_bus) | |
1758 | return -EIO; | |
1759 | ||
1760 | if (!u->cgroup_path) | |
1761 | return -EINVAL; | |
1762 | ||
1763 | /* Determine this unit's cgroup path relative to our cgroup root */ | |
1764 | pp = path_startswith(u->cgroup_path, u->manager->cgroup_root); | |
1765 | if (!pp) | |
1766 | return -EINVAL; | |
1767 | ||
1768 | pp = strjoina("/", pp, suffix_path); | |
b012e921 | 1769 | path_simplify(pp, false); |
98393f85 MB |
1770 | |
1771 | r = sd_bus_call_method(u->manager->system_bus, | |
1772 | "org.freedesktop.systemd1", | |
1773 | "/org/freedesktop/systemd1", | |
1774 | "org.freedesktop.systemd1.Manager", | |
1775 | "AttachProcessesToUnit", | |
1776 | &error, NULL, | |
1777 | "ssau", | |
1778 | NULL /* empty unit name means client's unit, i.e. us */, pp, 1, (uint32_t) pid); | |
60f067b4 | 1779 | if (r < 0) |
98393f85 MB |
1780 | return log_unit_debug_errno(u, r, "Failed to attach unit process " PID_FMT " via the bus: %s", pid, bus_error_message(&error, r)); |
1781 | ||
1782 | return 0; | |
1783 | } | |
1784 | ||
1785 | int unit_attach_pids_to_cgroup(Unit *u, Set *pids, const char *suffix_path) { | |
1786 | CGroupMask delegated_mask; | |
1787 | const char *p; | |
1788 | Iterator i; | |
1789 | void *pidp; | |
1790 | int r, q; | |
f47781d8 | 1791 | |
98393f85 MB |
1792 | assert(u); |
1793 | ||
1794 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) | |
1795 | return -EINVAL; | |
1796 | ||
1797 | if (set_isempty(pids)) | |
1798 | return 0; | |
1799 | ||
1800 | r = unit_realize_cgroup(u); | |
f47781d8 MP |
1801 | if (r < 0) |
1802 | return r; | |
14228c0d | 1803 | |
98393f85 MB |
1804 | if (isempty(suffix_path)) |
1805 | p = u->cgroup_path; | |
1806 | else | |
1807 | p = strjoina(u->cgroup_path, "/", suffix_path); | |
1808 | ||
1809 | delegated_mask = unit_get_delegate_mask(u); | |
1810 | ||
1811 | r = 0; | |
1812 | SET_FOREACH(pidp, pids, i) { | |
1813 | pid_t pid = PTR_TO_PID(pidp); | |
1814 | CGroupController c; | |
1815 | ||
1816 | /* First, attach the PID to the main cgroup hierarchy */ | |
1817 | q = cg_attach(SYSTEMD_CGROUP_CONTROLLER, p, pid); | |
1818 | if (q < 0) { | |
1819 | log_unit_debug_errno(u, q, "Couldn't move process " PID_FMT " to requested cgroup '%s': %m", pid, p); | |
1820 | ||
1821 | if (MANAGER_IS_USER(u->manager) && IN_SET(q, -EPERM, -EACCES)) { | |
1822 | int z; | |
1823 | ||
1824 | /* If we are in a user instance, and we can't move the process ourselves due to | |
1825 | * permission problems, let's ask the system instance about it instead. Since it's more | |
1826 | * privileged it might be able to move the process across the leaves of a subtree who's | |
1827 | * top node is not owned by us. */ | |
1828 | ||
1829 | z = unit_attach_pid_to_cgroup_via_bus(u, pid, suffix_path); | |
1830 | if (z < 0) | |
1831 | log_unit_debug_errno(u, z, "Couldn't move process " PID_FMT " to requested cgroup '%s' via the system bus either: %m", pid, p); | |
1832 | else | |
1833 | continue; /* When the bus thing worked via the bus we are fully done for this PID. */ | |
1834 | } | |
1835 | ||
1836 | if (r >= 0) | |
1837 | r = q; /* Remember first error */ | |
1838 | ||
1839 | continue; | |
1840 | } | |
1841 | ||
1842 | q = cg_all_unified(); | |
1843 | if (q < 0) | |
1844 | return q; | |
1845 | if (q > 0) | |
1846 | continue; | |
1847 | ||
1848 | /* In the legacy hierarchy, attach the process to the request cgroup if possible, and if not to the | |
1849 | * innermost realized one */ | |
1850 | ||
1851 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) { | |
1852 | CGroupMask bit = CGROUP_CONTROLLER_TO_MASK(c); | |
1853 | const char *realized; | |
1854 | ||
1855 | if (!(u->manager->cgroup_supported & bit)) | |
1856 | continue; | |
1857 | ||
1858 | /* If this controller is delegated and realized, honour the caller's request for the cgroup suffix. */ | |
1859 | if (delegated_mask & u->cgroup_realized_mask & bit) { | |
1860 | q = cg_attach(cgroup_controller_to_string(c), p, pid); | |
1861 | if (q >= 0) | |
1862 | continue; /* Success! */ | |
1863 | ||
1864 | log_unit_debug_errno(u, q, "Failed to attach PID " PID_FMT " to requested cgroup %s in controller %s, falling back to unit's cgroup: %m", | |
1865 | pid, p, cgroup_controller_to_string(c)); | |
1866 | } | |
1867 | ||
1868 | /* So this controller is either not delegate or realized, or something else weird happened. In | |
1869 | * that case let's attach the PID at least to the closest cgroup up the tree that is | |
1870 | * realized. */ | |
1871 | realized = unit_get_realized_cgroup_path(u, bit); | |
1872 | if (!realized) | |
1873 | continue; /* Not even realized in the root slice? Then let's not bother */ | |
1874 | ||
1875 | q = cg_attach(cgroup_controller_to_string(c), realized, pid); | |
1876 | if (q < 0) | |
1877 | log_unit_debug_errno(u, q, "Failed to attach PID " PID_FMT " to realized cgroup %s in controller %s, ignoring: %m", | |
1878 | pid, realized, cgroup_controller_to_string(c)); | |
1879 | } | |
1880 | } | |
1881 | ||
1882 | return r; | |
663996b3 MS |
1883 | } |
1884 | ||
6e866b33 MB |
1885 | static bool unit_has_mask_realized( |
1886 | Unit *u, | |
1887 | CGroupMask target_mask, | |
1888 | CGroupMask enable_mask) { | |
8a584da2 MP |
1889 | |
1890 | assert(u); | |
1891 | ||
6e866b33 MB |
1892 | /* Returns true if this unit is fully realized. We check four things: |
1893 | * | |
1894 | * 1. Whether the cgroup was created at all | |
7c20daf6 FS |
1895 | * 2. Whether the cgroup was created in all the hierarchies we need it to be created in (in case of cgroup v1) |
1896 | * 3. Whether the cgroup has all the right controllers enabled (in case of cgroup v2) | |
6e866b33 MB |
1897 | * 4. Whether the invalidation mask is currently zero |
1898 | * | |
1899 | * If you wonder why we mask the target realization and enable mask with CGROUP_MASK_V1/CGROUP_MASK_V2: note | |
7c20daf6 FS |
1900 | * that there are three sets of bitmasks: CGROUP_MASK_V1 (for real cgroup v1 controllers), CGROUP_MASK_V2 (for |
1901 | * real cgroup v2 controllers) and CGROUP_MASK_BPF (for BPF-based pseudo-controllers). Now, cgroup_realized_mask | |
1902 | * is only matters for cgroup v1 controllers, and cgroup_enabled_mask only used for cgroup v2, and if they | |
6e866b33 MB |
1903 | * differ in the others, we don't really care. (After all, the cgroup_enabled_mask tracks with controllers are |
1904 | * enabled through cgroup.subtree_control, and since the BPF pseudo-controllers don't show up there, they | |
1905 | * simply don't matter. */ | |
8a584da2 | 1906 | |
6e866b33 MB |
1907 | return u->cgroup_realized && |
1908 | ((u->cgroup_realized_mask ^ target_mask) & CGROUP_MASK_V1) == 0 && | |
1909 | ((u->cgroup_enabled_mask ^ enable_mask) & CGROUP_MASK_V2) == 0 && | |
1910 | u->cgroup_invalidated_mask == 0; | |
1911 | } | |
8a584da2 | 1912 | |
6e866b33 MB |
1913 | static bool unit_has_mask_disables_realized( |
1914 | Unit *u, | |
1915 | CGroupMask target_mask, | |
1916 | CGroupMask enable_mask) { | |
1917 | ||
1918 | assert(u); | |
1919 | ||
1920 | /* Returns true if all controllers which should be disabled are indeed disabled. | |
1921 | * | |
1922 | * Unlike unit_has_mask_realized, we don't care what was enabled, only that anything we want to remove is | |
1923 | * already removed. */ | |
1924 | ||
1925 | return !u->cgroup_realized || | |
1926 | (FLAGS_SET(u->cgroup_realized_mask, target_mask & CGROUP_MASK_V1) && | |
1927 | FLAGS_SET(u->cgroup_enabled_mask, enable_mask & CGROUP_MASK_V2)); | |
8a584da2 MP |
1928 | } |
1929 | ||
6e866b33 | 1930 | static bool unit_has_mask_enables_realized( |
f5e65279 MB |
1931 | Unit *u, |
1932 | CGroupMask target_mask, | |
6e866b33 | 1933 | CGroupMask enable_mask) { |
f5e65279 | 1934 | |
60f067b4 JS |
1935 | assert(u); |
1936 | ||
6e866b33 MB |
1937 | /* Returns true if all controllers which should be enabled are indeed enabled. |
1938 | * | |
1939 | * Unlike unit_has_mask_realized, we don't care about the controllers that are not present, only that anything | |
1940 | * we want to add is already added. */ | |
1941 | ||
f5e65279 | 1942 | return u->cgroup_realized && |
6e866b33 MB |
1943 | ((u->cgroup_realized_mask | target_mask) & CGROUP_MASK_V1) == (u->cgroup_realized_mask & CGROUP_MASK_V1) && |
1944 | ((u->cgroup_enabled_mask | enable_mask) & CGROUP_MASK_V2) == (u->cgroup_enabled_mask & CGROUP_MASK_V2); | |
60f067b4 JS |
1945 | } |
1946 | ||
6e866b33 | 1947 | void unit_add_to_cgroup_realize_queue(Unit *u) { |
52ad194e MB |
1948 | assert(u); |
1949 | ||
1950 | if (u->in_cgroup_realize_queue) | |
1951 | return; | |
1952 | ||
1953 | LIST_PREPEND(cgroup_realize_queue, u->manager->cgroup_realize_queue, u); | |
1954 | u->in_cgroup_realize_queue = true; | |
1955 | } | |
1956 | ||
1957 | static void unit_remove_from_cgroup_realize_queue(Unit *u) { | |
1958 | assert(u); | |
1959 | ||
1960 | if (!u->in_cgroup_realize_queue) | |
1961 | return; | |
1962 | ||
1963 | LIST_REMOVE(cgroup_realize_queue, u->manager->cgroup_realize_queue, u); | |
1964 | u->in_cgroup_realize_queue = false; | |
1965 | } | |
1966 | ||
6e866b33 MB |
1967 | /* Controllers can only be enabled breadth-first, from the root of the |
1968 | * hierarchy downwards to the unit in question. */ | |
1969 | static int unit_realize_cgroup_now_enable(Unit *u, ManagerState state) { | |
1970 | CGroupMask target_mask, enable_mask, new_target_mask, new_enable_mask; | |
1971 | int r; | |
1972 | ||
1973 | assert(u); | |
1974 | ||
1975 | /* First go deal with this unit's parent, or we won't be able to enable | |
1976 | * any new controllers at this layer. */ | |
1977 | if (UNIT_ISSET(u->slice)) { | |
1978 | r = unit_realize_cgroup_now_enable(UNIT_DEREF(u->slice), state); | |
1979 | if (r < 0) | |
1980 | return r; | |
1981 | } | |
1982 | ||
1983 | target_mask = unit_get_target_mask(u); | |
1984 | enable_mask = unit_get_enable_mask(u); | |
1985 | ||
1986 | /* We can only enable in this direction, don't try to disable anything. | |
1987 | */ | |
1988 | if (unit_has_mask_enables_realized(u, target_mask, enable_mask)) | |
1989 | return 0; | |
1990 | ||
1991 | new_target_mask = u->cgroup_realized_mask | target_mask; | |
1992 | new_enable_mask = u->cgroup_enabled_mask | enable_mask; | |
1993 | ||
1994 | return unit_create_cgroup(u, new_target_mask, new_enable_mask, state); | |
1995 | } | |
1996 | ||
1997 | /* Controllers can only be disabled depth-first, from the leaves of the | |
1998 | * hierarchy upwards to the unit in question. */ | |
1999 | static int unit_realize_cgroup_now_disable(Unit *u, ManagerState state) { | |
2000 | Iterator i; | |
2001 | Unit *m; | |
2002 | void *v; | |
2003 | ||
2004 | assert(u); | |
2005 | ||
2006 | if (u->type != UNIT_SLICE) | |
2007 | return 0; | |
2008 | ||
2009 | HASHMAP_FOREACH_KEY(v, m, u->dependencies[UNIT_BEFORE], i) { | |
2010 | CGroupMask target_mask, enable_mask, new_target_mask, new_enable_mask; | |
2011 | int r; | |
2012 | ||
2013 | if (UNIT_DEREF(m->slice) != u) | |
2014 | continue; | |
2015 | ||
2016 | /* The cgroup for this unit might not actually be fully | |
2017 | * realised yet, in which case it isn't holding any controllers | |
2018 | * open anyway. */ | |
2019 | if (!m->cgroup_path) | |
2020 | continue; | |
2021 | ||
2022 | /* We must disable those below us first in order to release the | |
2023 | * controller. */ | |
2024 | if (m->type == UNIT_SLICE) | |
2025 | (void) unit_realize_cgroup_now_disable(m, state); | |
2026 | ||
2027 | target_mask = unit_get_target_mask(m); | |
2028 | enable_mask = unit_get_enable_mask(m); | |
2029 | ||
2030 | /* We can only disable in this direction, don't try to enable | |
2031 | * anything. */ | |
2032 | if (unit_has_mask_disables_realized(m, target_mask, enable_mask)) | |
2033 | continue; | |
2034 | ||
2035 | new_target_mask = m->cgroup_realized_mask & target_mask; | |
2036 | new_enable_mask = m->cgroup_enabled_mask & enable_mask; | |
2037 | ||
2038 | r = unit_create_cgroup(m, new_target_mask, new_enable_mask, state); | |
2039 | if (r < 0) | |
2040 | return r; | |
2041 | } | |
2042 | ||
2043 | return 0; | |
2044 | } | |
2045 | ||
60f067b4 JS |
2046 | /* Check if necessary controllers and attributes for a unit are in place. |
2047 | * | |
6e866b33 MB |
2048 | * - If so, do nothing. |
2049 | * - If not, create paths, move processes over, and set attributes. | |
2050 | * | |
2051 | * Controllers can only be *enabled* in a breadth-first way, and *disabled* in | |
2052 | * a depth-first way. As such the process looks like this: | |
2053 | * | |
2054 | * Suppose we have a cgroup hierarchy which looks like this: | |
2055 | * | |
2056 | * root | |
2057 | * / \ | |
2058 | * / \ | |
2059 | * / \ | |
2060 | * a b | |
2061 | * / \ / \ | |
2062 | * / \ / \ | |
2063 | * c d e f | |
2064 | * / \ / \ / \ / \ | |
2065 | * h i j k l m n o | |
2066 | * | |
2067 | * 1. We want to realise cgroup "d" now. | |
2068 | * 2. cgroup "a" has DisableControllers=cpu in the associated unit. | |
2069 | * 3. cgroup "k" just started requesting the memory controller. | |
2070 | * | |
2071 | * To make this work we must do the following in order: | |
2072 | * | |
2073 | * 1. Disable CPU controller in k, j | |
2074 | * 2. Disable CPU controller in d | |
2075 | * 3. Enable memory controller in root | |
2076 | * 4. Enable memory controller in a | |
2077 | * 5. Enable memory controller in d | |
2078 | * 6. Enable memory controller in k | |
2079 | * | |
2080 | * Notice that we need to touch j in one direction, but not the other. We also | |
2081 | * don't go beyond d when disabling -- it's up to "a" to get realized if it | |
2082 | * wants to disable further. The basic rules are therefore: | |
2083 | * | |
2084 | * - If you're disabling something, you need to realise all of the cgroups from | |
2085 | * your recursive descendants to the root. This starts from the leaves. | |
2086 | * - If you're enabling something, you need to realise from the root cgroup | |
2087 | * downwards, but you don't need to iterate your recursive descendants. | |
60f067b4 JS |
2088 | * |
2089 | * Returns 0 on success and < 0 on failure. */ | |
2090 | static int unit_realize_cgroup_now(Unit *u, ManagerState state) { | |
d9dfd233 | 2091 | CGroupMask target_mask, enable_mask; |
60f067b4 | 2092 | int r; |
663996b3 | 2093 | |
14228c0d MB |
2094 | assert(u); |
2095 | ||
52ad194e | 2096 | unit_remove_from_cgroup_realize_queue(u); |
14228c0d | 2097 | |
d9dfd233 | 2098 | target_mask = unit_get_target_mask(u); |
aa27b158 MP |
2099 | enable_mask = unit_get_enable_mask(u); |
2100 | ||
6e866b33 | 2101 | if (unit_has_mask_realized(u, target_mask, enable_mask)) |
663996b3 MS |
2102 | return 0; |
2103 | ||
6e866b33 MB |
2104 | /* Disable controllers below us, if there are any */ |
2105 | r = unit_realize_cgroup_now_disable(u, state); | |
2106 | if (r < 0) | |
2107 | return r; | |
f5e65279 | 2108 | |
6e866b33 | 2109 | /* Enable controllers above us, if there are any */ |
60f067b4 | 2110 | if (UNIT_ISSET(u->slice)) { |
6e866b33 | 2111 | r = unit_realize_cgroup_now_enable(UNIT_DEREF(u->slice), state); |
60f067b4 JS |
2112 | if (r < 0) |
2113 | return r; | |
2114 | } | |
663996b3 | 2115 | |
6e866b33 MB |
2116 | /* Now actually deal with the cgroup we were trying to realise and set attributes */ |
2117 | r = unit_create_cgroup(u, target_mask, enable_mask, state); | |
60f067b4 JS |
2118 | if (r < 0) |
2119 | return r; | |
2120 | ||
6e866b33 MB |
2121 | /* Now, reset the invalidation mask */ |
2122 | u->cgroup_invalidated_mask = 0; | |
60f067b4 | 2123 | return 0; |
14228c0d | 2124 | } |
663996b3 | 2125 | |
f5e65279 | 2126 | unsigned manager_dispatch_cgroup_realize_queue(Manager *m) { |
60f067b4 | 2127 | ManagerState state; |
14228c0d | 2128 | unsigned n = 0; |
60f067b4 JS |
2129 | Unit *i; |
2130 | int r; | |
2131 | ||
f5e65279 MB |
2132 | assert(m); |
2133 | ||
60f067b4 | 2134 | state = manager_state(m); |
663996b3 | 2135 | |
f5e65279 MB |
2136 | while ((i = m->cgroup_realize_queue)) { |
2137 | assert(i->in_cgroup_realize_queue); | |
14228c0d | 2138 | |
52ad194e MB |
2139 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(i))) { |
2140 | /* Maybe things changed, and the unit is not actually active anymore? */ | |
2141 | unit_remove_from_cgroup_realize_queue(i); | |
2142 | continue; | |
2143 | } | |
2144 | ||
60f067b4 JS |
2145 | r = unit_realize_cgroup_now(i, state); |
2146 | if (r < 0) | |
d9dfd233 | 2147 | log_warning_errno(r, "Failed to realize cgroups for queued unit %s, ignoring: %m", i->id); |
14228c0d MB |
2148 | |
2149 | n++; | |
2150 | } | |
2151 | ||
2152 | return n; | |
2153 | } | |
2154 | ||
f5e65279 | 2155 | static void unit_add_siblings_to_cgroup_realize_queue(Unit *u) { |
14228c0d MB |
2156 | Unit *slice; |
2157 | ||
2158 | /* This adds the siblings of the specified unit and the | |
2159 | * siblings of all parent units to the cgroup queue. (But | |
2160 | * neither the specified unit itself nor the parents.) */ | |
663996b3 | 2161 | |
14228c0d MB |
2162 | while ((slice = UNIT_DEREF(u->slice))) { |
2163 | Iterator i; | |
2164 | Unit *m; | |
52ad194e | 2165 | void *v; |
663996b3 | 2166 | |
52ad194e | 2167 | HASHMAP_FOREACH_KEY(v, m, u->dependencies[UNIT_BEFORE], i) { |
60f067b4 JS |
2168 | /* Skip units that have a dependency on the slice |
2169 | * but aren't actually in it. */ | |
14228c0d MB |
2170 | if (UNIT_DEREF(m->slice) != slice) |
2171 | continue; | |
2172 | ||
60f067b4 JS |
2173 | /* No point in doing cgroup application for units |
2174 | * without active processes. */ | |
2175 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(m))) | |
2176 | continue; | |
2177 | ||
2178 | /* If the unit doesn't need any new controllers | |
2179 | * and has current ones realized, it doesn't need | |
2180 | * any changes. */ | |
f5e65279 MB |
2181 | if (unit_has_mask_realized(m, |
2182 | unit_get_target_mask(m), | |
6e866b33 | 2183 | unit_get_enable_mask(m))) |
60f067b4 JS |
2184 | continue; |
2185 | ||
f5e65279 | 2186 | unit_add_to_cgroup_realize_queue(m); |
663996b3 MS |
2187 | } |
2188 | ||
14228c0d | 2189 | u = slice; |
663996b3 | 2190 | } |
14228c0d MB |
2191 | } |
2192 | ||
2193 | int unit_realize_cgroup(Unit *u) { | |
14228c0d MB |
2194 | assert(u); |
2195 | ||
d9dfd233 | 2196 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
14228c0d | 2197 | return 0; |
663996b3 | 2198 | |
14228c0d MB |
2199 | /* So, here's the deal: when realizing the cgroups for this |
2200 | * unit, we need to first create all parents, but there's more | |
2201 | * actually: for the weight-based controllers we also need to | |
2202 | * make sure that all our siblings (i.e. units that are in the | |
60f067b4 | 2203 | * same slice as we are) have cgroups, too. Otherwise, things |
14228c0d MB |
2204 | * would become very uneven as each of their processes would |
2205 | * get as much resources as all our group together. This call | |
2206 | * will synchronously create the parent cgroups, but will | |
2207 | * defer work on the siblings to the next event loop | |
2208 | * iteration. */ | |
663996b3 | 2209 | |
14228c0d | 2210 | /* Add all sibling slices to the cgroup queue. */ |
f5e65279 | 2211 | unit_add_siblings_to_cgroup_realize_queue(u); |
14228c0d | 2212 | |
60f067b4 JS |
2213 | /* And realize this one now (and apply the values) */ |
2214 | return unit_realize_cgroup_now(u, manager_state(u->manager)); | |
663996b3 MS |
2215 | } |
2216 | ||
d9dfd233 MP |
2217 | void unit_release_cgroup(Unit *u) { |
2218 | assert(u); | |
2219 | ||
6e866b33 MB |
2220 | /* Forgets all cgroup details for this cgroup — but does *not* destroy the cgroup. This is hence OK to call |
2221 | * when we close down everything for reexecution, where we really want to leave the cgroup in place. */ | |
d9dfd233 MP |
2222 | |
2223 | if (u->cgroup_path) { | |
2224 | (void) hashmap_remove(u->manager->cgroup_unit, u->cgroup_path); | |
2225 | u->cgroup_path = mfree(u->cgroup_path); | |
2226 | } | |
2227 | ||
2228 | if (u->cgroup_inotify_wd >= 0) { | |
2229 | if (inotify_rm_watch(u->manager->cgroup_inotify_fd, u->cgroup_inotify_wd) < 0) | |
6e866b33 | 2230 | log_unit_debug_errno(u, errno, "Failed to remove cgroup inotify watch %i for %s, ignoring: %m", u->cgroup_inotify_wd, u->id); |
d9dfd233 MP |
2231 | |
2232 | (void) hashmap_remove(u->manager->cgroup_inotify_wd_unit, INT_TO_PTR(u->cgroup_inotify_wd)); | |
2233 | u->cgroup_inotify_wd = -1; | |
2234 | } | |
2235 | } | |
2236 | ||
2237 | void unit_prune_cgroup(Unit *u) { | |
663996b3 | 2238 | int r; |
d9dfd233 | 2239 | bool is_root_slice; |
663996b3 | 2240 | |
14228c0d | 2241 | assert(u); |
663996b3 | 2242 | |
d9dfd233 MP |
2243 | /* Removes the cgroup, if empty and possible, and stops watching it. */ |
2244 | ||
14228c0d MB |
2245 | if (!u->cgroup_path) |
2246 | return; | |
2247 | ||
8a584da2 MP |
2248 | (void) unit_get_cpu_usage(u, NULL); /* Cache the last CPU usage value before we destroy the cgroup */ |
2249 | ||
d9dfd233 MP |
2250 | is_root_slice = unit_has_name(u, SPECIAL_ROOT_SLICE); |
2251 | ||
2252 | r = cg_trim_everywhere(u->manager->cgroup_supported, u->cgroup_path, !is_root_slice); | |
f47781d8 | 2253 | if (r < 0) { |
5a920b42 | 2254 | log_unit_debug_errno(u, r, "Failed to destroy cgroup %s, ignoring: %m", u->cgroup_path); |
f47781d8 MP |
2255 | return; |
2256 | } | |
14228c0d | 2257 | |
d9dfd233 MP |
2258 | if (is_root_slice) |
2259 | return; | |
2260 | ||
2261 | unit_release_cgroup(u); | |
663996b3 | 2262 | |
14228c0d | 2263 | u->cgroup_realized = false; |
60f067b4 | 2264 | u->cgroup_realized_mask = 0; |
aa27b158 | 2265 | u->cgroup_enabled_mask = 0; |
6e866b33 MB |
2266 | |
2267 | u->bpf_device_control_installed = bpf_program_unref(u->bpf_device_control_installed); | |
663996b3 MS |
2268 | } |
2269 | ||
d9dfd233 | 2270 | int unit_search_main_pid(Unit *u, pid_t *ret) { |
14228c0d | 2271 | _cleanup_fclose_ FILE *f = NULL; |
bb4f798a | 2272 | pid_t pid = 0, npid; |
d9dfd233 | 2273 | int r; |
663996b3 | 2274 | |
14228c0d | 2275 | assert(u); |
d9dfd233 | 2276 | assert(ret); |
663996b3 | 2277 | |
14228c0d | 2278 | if (!u->cgroup_path) |
d9dfd233 | 2279 | return -ENXIO; |
663996b3 | 2280 | |
d9dfd233 MP |
2281 | r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, &f); |
2282 | if (r < 0) | |
2283 | return r; | |
14228c0d | 2284 | |
14228c0d | 2285 | while (cg_read_pid(f, &npid) > 0) { |
14228c0d MB |
2286 | |
2287 | if (npid == pid) | |
2288 | continue; | |
2289 | ||
bb4f798a | 2290 | if (pid_is_my_child(npid) == 0) |
14228c0d MB |
2291 | continue; |
2292 | ||
d9dfd233 | 2293 | if (pid != 0) |
14228c0d MB |
2294 | /* Dang, there's more than one daemonized PID |
2295 | in this group, so we don't know what process | |
2296 | is the main process. */ | |
d9dfd233 MP |
2297 | |
2298 | return -ENODATA; | |
14228c0d MB |
2299 | |
2300 | pid = npid; | |
663996b3 MS |
2301 | } |
2302 | ||
d9dfd233 MP |
2303 | *ret = pid; |
2304 | return 0; | |
2305 | } | |
2306 | ||
2307 | static int unit_watch_pids_in_path(Unit *u, const char *path) { | |
2308 | _cleanup_closedir_ DIR *d = NULL; | |
2309 | _cleanup_fclose_ FILE *f = NULL; | |
2310 | int ret = 0, r; | |
2311 | ||
2312 | assert(u); | |
2313 | assert(path); | |
2314 | ||
2315 | r = cg_enumerate_processes(SYSTEMD_CGROUP_CONTROLLER, path, &f); | |
2316 | if (r < 0) | |
2317 | ret = r; | |
2318 | else { | |
2319 | pid_t pid; | |
2320 | ||
2321 | while ((r = cg_read_pid(f, &pid)) > 0) { | |
bb4f798a | 2322 | r = unit_watch_pid(u, pid, false); |
d9dfd233 MP |
2323 | if (r < 0 && ret >= 0) |
2324 | ret = r; | |
2325 | } | |
2326 | ||
2327 | if (r < 0 && ret >= 0) | |
2328 | ret = r; | |
2329 | } | |
2330 | ||
2331 | r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, path, &d); | |
2332 | if (r < 0) { | |
2333 | if (ret >= 0) | |
2334 | ret = r; | |
2335 | } else { | |
2336 | char *fn; | |
2337 | ||
2338 | while ((r = cg_read_subgroup(d, &fn)) > 0) { | |
2339 | _cleanup_free_ char *p = NULL; | |
2340 | ||
2897b343 | 2341 | p = strjoin(path, "/", fn); |
d9dfd233 MP |
2342 | free(fn); |
2343 | ||
2344 | if (!p) | |
2345 | return -ENOMEM; | |
2346 | ||
2347 | r = unit_watch_pids_in_path(u, p); | |
2348 | if (r < 0 && ret >= 0) | |
2349 | ret = r; | |
2350 | } | |
2351 | ||
2352 | if (r < 0 && ret >= 0) | |
2353 | ret = r; | |
2354 | } | |
2355 | ||
2356 | return ret; | |
2357 | } | |
2358 | ||
1d42b86d MB |
2359 | int unit_synthesize_cgroup_empty_event(Unit *u) { |
2360 | int r; | |
2361 | ||
2362 | assert(u); | |
2363 | ||
2364 | /* Enqueue a synthetic cgroup empty event if this unit doesn't watch any PIDs anymore. This is compatibility | |
2365 | * support for non-unified systems where notifications aren't reliable, and hence need to take whatever we can | |
2366 | * get as notification source as soon as we stopped having any useful PIDs to watch for. */ | |
2367 | ||
2368 | if (!u->cgroup_path) | |
2369 | return -ENOENT; | |
2370 | ||
2371 | r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER); | |
2372 | if (r < 0) | |
2373 | return r; | |
2374 | if (r > 0) /* On unified we have reliable notifications, and don't need this */ | |
2375 | return 0; | |
2376 | ||
2377 | if (!set_isempty(u->pids)) | |
2378 | return 0; | |
2379 | ||
2380 | unit_add_to_cgroup_empty_queue(u); | |
2381 | return 0; | |
2382 | } | |
2383 | ||
d9dfd233 | 2384 | int unit_watch_all_pids(Unit *u) { |
2897b343 MP |
2385 | int r; |
2386 | ||
d9dfd233 MP |
2387 | assert(u); |
2388 | ||
2389 | /* Adds all PIDs from our cgroup to the set of PIDs we | |
2390 | * watch. This is a fallback logic for cases where we do not | |
2391 | * get reliable cgroup empty notifications: we try to use | |
2392 | * SIGCHLD as replacement. */ | |
2393 | ||
2394 | if (!u->cgroup_path) | |
2395 | return -ENOENT; | |
2396 | ||
2897b343 MP |
2397 | r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER); |
2398 | if (r < 0) | |
2399 | return r; | |
2400 | if (r > 0) /* On unified we can use proper notifications */ | |
d9dfd233 MP |
2401 | return 0; |
2402 | ||
2403 | return unit_watch_pids_in_path(u, u->cgroup_path); | |
2404 | } | |
2405 | ||
f5e65279 MB |
2406 | static int on_cgroup_empty_event(sd_event_source *s, void *userdata) { |
2407 | Manager *m = userdata; | |
2408 | Unit *u; | |
d9dfd233 MP |
2409 | int r; |
2410 | ||
f5e65279 MB |
2411 | assert(s); |
2412 | assert(m); | |
d9dfd233 | 2413 | |
f5e65279 MB |
2414 | u = m->cgroup_empty_queue; |
2415 | if (!u) | |
d9dfd233 MP |
2416 | return 0; |
2417 | ||
f5e65279 MB |
2418 | assert(u->in_cgroup_empty_queue); |
2419 | u->in_cgroup_empty_queue = false; | |
2420 | LIST_REMOVE(cgroup_empty_queue, m->cgroup_empty_queue, u); | |
2421 | ||
2422 | if (m->cgroup_empty_queue) { | |
2423 | /* More stuff queued, let's make sure we remain enabled */ | |
2424 | r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT); | |
2425 | if (r < 0) | |
b012e921 | 2426 | log_debug_errno(r, "Failed to reenable cgroup empty event source, ignoring: %m"); |
f5e65279 | 2427 | } |
d9dfd233 MP |
2428 | |
2429 | unit_add_to_gc_queue(u); | |
2430 | ||
2431 | if (UNIT_VTABLE(u)->notify_cgroup_empty) | |
2432 | UNIT_VTABLE(u)->notify_cgroup_empty(u); | |
2433 | ||
2434 | return 0; | |
2435 | } | |
2436 | ||
f5e65279 MB |
2437 | void unit_add_to_cgroup_empty_queue(Unit *u) { |
2438 | int r; | |
2439 | ||
2440 | assert(u); | |
2441 | ||
2442 | /* Note that there are four different ways how cgroup empty events reach us: | |
2443 | * | |
2444 | * 1. On the unified hierarchy we get an inotify event on the cgroup | |
2445 | * | |
2446 | * 2. On the legacy hierarchy, when running in system mode, we get a datagram on the cgroup agent socket | |
2447 | * | |
2448 | * 3. On the legacy hierarchy, when running in user mode, we get a D-Bus signal on the system bus | |
2449 | * | |
2450 | * 4. On the legacy hierarchy, in service units we start watching all processes of the cgroup for SIGCHLD as | |
2451 | * soon as we get one SIGCHLD, to deal with unreliable cgroup notifications. | |
2452 | * | |
2453 | * Regardless which way we got the notification, we'll verify it here, and then add it to a separate | |
2454 | * queue. This queue will be dispatched at a lower priority than the SIGCHLD handler, so that we always use | |
2455 | * SIGCHLD if we can get it first, and only use the cgroup empty notifications if there's no SIGCHLD pending | |
2456 | * (which might happen if the cgroup doesn't contain processes that are our own child, which is typically the | |
2457 | * case for scope units). */ | |
2458 | ||
2459 | if (u->in_cgroup_empty_queue) | |
2460 | return; | |
2461 | ||
2462 | /* Let's verify that the cgroup is really empty */ | |
2463 | if (!u->cgroup_path) | |
2464 | return; | |
2465 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path); | |
2466 | if (r < 0) { | |
2467 | log_unit_debug_errno(u, r, "Failed to determine whether cgroup %s is empty: %m", u->cgroup_path); | |
2468 | return; | |
2469 | } | |
2470 | if (r == 0) | |
2471 | return; | |
2472 | ||
2473 | LIST_PREPEND(cgroup_empty_queue, u->manager->cgroup_empty_queue, u); | |
2474 | u->in_cgroup_empty_queue = true; | |
2475 | ||
2476 | /* Trigger the defer event */ | |
2477 | r = sd_event_source_set_enabled(u->manager->cgroup_empty_event_source, SD_EVENT_ONESHOT); | |
2478 | if (r < 0) | |
2479 | log_debug_errno(r, "Failed to enable cgroup empty event source: %m"); | |
2480 | } | |
2481 | ||
d9dfd233 MP |
2482 | static int on_cgroup_inotify_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
2483 | Manager *m = userdata; | |
2484 | ||
2485 | assert(s); | |
2486 | assert(fd >= 0); | |
2487 | assert(m); | |
2488 | ||
2489 | for (;;) { | |
2490 | union inotify_event_buffer buffer; | |
2491 | struct inotify_event *e; | |
2492 | ssize_t l; | |
2493 | ||
2494 | l = read(fd, &buffer, sizeof(buffer)); | |
2495 | if (l < 0) { | |
f5e65279 | 2496 | if (IN_SET(errno, EINTR, EAGAIN)) |
d9dfd233 MP |
2497 | return 0; |
2498 | ||
2499 | return log_error_errno(errno, "Failed to read control group inotify events: %m"); | |
2500 | } | |
2501 | ||
2502 | FOREACH_INOTIFY_EVENT(e, buffer, l) { | |
2503 | Unit *u; | |
2504 | ||
2505 | if (e->wd < 0) | |
2506 | /* Queue overflow has no watch descriptor */ | |
2507 | continue; | |
2508 | ||
2509 | if (e->mask & IN_IGNORED) | |
2510 | /* The watch was just removed */ | |
2511 | continue; | |
2512 | ||
2513 | u = hashmap_get(m->cgroup_inotify_wd_unit, INT_TO_PTR(e->wd)); | |
2514 | if (!u) /* Not that inotify might deliver | |
2515 | * events for a watch even after it | |
2516 | * was removed, because it was queued | |
2517 | * before the removal. Let's ignore | |
2518 | * this here safely. */ | |
2519 | continue; | |
2520 | ||
f5e65279 | 2521 | unit_add_to_cgroup_empty_queue(u); |
d9dfd233 MP |
2522 | } |
2523 | } | |
663996b3 MS |
2524 | } |
2525 | ||
6e866b33 MB |
2526 | static int cg_bpf_mask_supported(CGroupMask *ret) { |
2527 | CGroupMask mask = 0; | |
2528 | int r; | |
2529 | ||
2530 | /* BPF-based firewall */ | |
2531 | r = bpf_firewall_supported(); | |
2532 | if (r > 0) | |
2533 | mask |= CGROUP_MASK_BPF_FIREWALL; | |
2534 | ||
2535 | /* BPF-based device access control */ | |
2536 | r = bpf_devices_supported(); | |
2537 | if (r > 0) | |
2538 | mask |= CGROUP_MASK_BPF_DEVICES; | |
2539 | ||
2540 | *ret = mask; | |
2541 | return 0; | |
2542 | } | |
2543 | ||
663996b3 | 2544 | int manager_setup_cgroup(Manager *m) { |
14228c0d | 2545 | _cleanup_free_ char *path = NULL; |
f5e65279 | 2546 | const char *scope_path; |
d9dfd233 | 2547 | CGroupController c; |
2897b343 | 2548 | int r, all_unified; |
6e866b33 | 2549 | CGroupMask mask; |
d9dfd233 | 2550 | char *e; |
663996b3 MS |
2551 | |
2552 | assert(m); | |
2553 | ||
663996b3 | 2554 | /* 1. Determine hierarchy */ |
d9dfd233 | 2555 | m->cgroup_root = mfree(m->cgroup_root); |
14228c0d | 2556 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &m->cgroup_root); |
f47781d8 MP |
2557 | if (r < 0) |
2558 | return log_error_errno(r, "Cannot determine cgroup we are running in: %m"); | |
663996b3 | 2559 | |
d9dfd233 MP |
2560 | /* Chop off the init scope, if we are already located in it */ |
2561 | e = endswith(m->cgroup_root, "/" SPECIAL_INIT_SCOPE); | |
5eef597e | 2562 | |
d9dfd233 MP |
2563 | /* LEGACY: Also chop off the system slice if we are in |
2564 | * it. This is to support live upgrades from older systemd | |
2565 | * versions where PID 1 was moved there. Also see | |
2566 | * cg_get_root_path(). */ | |
aa27b158 | 2567 | if (!e && MANAGER_IS_SYSTEM(m)) { |
14228c0d | 2568 | e = endswith(m->cgroup_root, "/" SPECIAL_SYSTEM_SLICE); |
60f067b4 | 2569 | if (!e) |
d9dfd233 | 2570 | e = endswith(m->cgroup_root, "/system"); /* even more legacy */ |
663996b3 | 2571 | } |
d9dfd233 MP |
2572 | if (e) |
2573 | *e = 0; | |
663996b3 | 2574 | |
52ad194e MB |
2575 | /* And make sure to store away the root value without trailing slash, even for the root dir, so that we can |
2576 | * easily prepend it everywhere. */ | |
2577 | delete_trailing_chars(m->cgroup_root, "/"); | |
663996b3 MS |
2578 | |
2579 | /* 2. Show data */ | |
14228c0d | 2580 | r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, NULL, &path); |
f47781d8 MP |
2581 | if (r < 0) |
2582 | return log_error_errno(r, "Cannot find cgroup mount point: %m"); | |
663996b3 | 2583 | |
2897b343 MP |
2584 | r = cg_unified_flush(); |
2585 | if (r < 0) | |
2586 | return log_error_errno(r, "Couldn't determine if we are running in the unified hierarchy: %m"); | |
8a584da2 | 2587 | |
2897b343 | 2588 | all_unified = cg_all_unified(); |
52ad194e MB |
2589 | if (all_unified < 0) |
2590 | return log_error_errno(all_unified, "Couldn't determine whether we are in all unified mode: %m"); | |
2591 | if (all_unified > 0) | |
d9dfd233 | 2592 | log_debug("Unified cgroup hierarchy is located at %s.", path); |
2897b343 MP |
2593 | else { |
2594 | r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER); | |
2595 | if (r < 0) | |
2596 | return log_error_errno(r, "Failed to determine whether systemd's own controller is in unified mode: %m"); | |
2597 | if (r > 0) | |
2598 | log_debug("Unified cgroup hierarchy is located at %s. Controllers are on legacy hierarchies.", path); | |
2599 | else | |
2600 | log_debug("Using cgroup controller " SYSTEMD_CGROUP_CONTROLLER_LEGACY ". File system hierarchy is at %s.", path); | |
2601 | } | |
d9dfd233 | 2602 | |
f5e65279 MB |
2603 | /* 3. Allocate cgroup empty defer event source */ |
2604 | m->cgroup_empty_event_source = sd_event_source_unref(m->cgroup_empty_event_source); | |
2605 | r = sd_event_add_defer(m->event, &m->cgroup_empty_event_source, on_cgroup_empty_event, m); | |
2606 | if (r < 0) | |
2607 | return log_error_errno(r, "Failed to create cgroup empty event source: %m"); | |
663996b3 | 2608 | |
f5e65279 MB |
2609 | r = sd_event_source_set_priority(m->cgroup_empty_event_source, SD_EVENT_PRIORITY_NORMAL-5); |
2610 | if (r < 0) | |
2611 | return log_error_errno(r, "Failed to set priority of cgroup empty event source: %m"); | |
d9dfd233 | 2612 | |
f5e65279 MB |
2613 | r = sd_event_source_set_enabled(m->cgroup_empty_event_source, SD_EVENT_OFF); |
2614 | if (r < 0) | |
2615 | return log_error_errno(r, "Failed to disable cgroup empty event source: %m"); | |
d9dfd233 | 2616 | |
f5e65279 | 2617 | (void) sd_event_source_set_description(m->cgroup_empty_event_source, "cgroup-empty"); |
d9dfd233 | 2618 | |
f5e65279 MB |
2619 | /* 4. Install notifier inotify object, or agent */ |
2620 | if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) > 0) { | |
d9dfd233 | 2621 | |
f5e65279 | 2622 | /* In the unified hierarchy we can get cgroup empty notifications via inotify. */ |
d9dfd233 | 2623 | |
f5e65279 MB |
2624 | m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source); |
2625 | safe_close(m->cgroup_inotify_fd); | |
d9dfd233 | 2626 | |
f5e65279 MB |
2627 | m->cgroup_inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); |
2628 | if (m->cgroup_inotify_fd < 0) | |
2629 | return log_error_errno(errno, "Failed to create control group inotify object: %m"); | |
d9dfd233 | 2630 | |
f5e65279 MB |
2631 | r = sd_event_add_io(m->event, &m->cgroup_inotify_event_source, m->cgroup_inotify_fd, EPOLLIN, on_cgroup_inotify_event, m); |
2632 | if (r < 0) | |
2633 | return log_error_errno(r, "Failed to watch control group inotify object: %m"); | |
d9dfd233 | 2634 | |
f5e65279 MB |
2635 | /* Process cgroup empty notifications early, but after service notifications and SIGCHLD. Also |
2636 | * see handling of cgroup agent notifications, for the classic cgroup hierarchy support. */ | |
2637 | r = sd_event_source_set_priority(m->cgroup_inotify_event_source, SD_EVENT_PRIORITY_NORMAL-4); | |
2638 | if (r < 0) | |
2639 | return log_error_errno(r, "Failed to set priority of inotify event source: %m"); | |
d9dfd233 | 2640 | |
f5e65279 | 2641 | (void) sd_event_source_set_description(m->cgroup_inotify_event_source, "cgroup-inotify"); |
663996b3 | 2642 | |
6e866b33 | 2643 | } else if (MANAGER_IS_SYSTEM(m) && manager_owns_host_root_cgroup(m) && !MANAGER_IS_TEST_RUN(m)) { |
f5e65279 MB |
2644 | |
2645 | /* On the legacy hierarchy we only get notifications via cgroup agents. (Which isn't really reliable, | |
2646 | * since it does not generate events when control groups with children run empty. */ | |
d9dfd233 | 2647 | |
f5e65279 | 2648 | r = cg_install_release_agent(SYSTEMD_CGROUP_CONTROLLER, SYSTEMD_CGROUP_AGENT_PATH); |
f47781d8 | 2649 | if (r < 0) |
f5e65279 MB |
2650 | log_warning_errno(r, "Failed to install release agent, ignoring: %m"); |
2651 | else if (r > 0) | |
2652 | log_debug("Installed release agent."); | |
2653 | else if (r == 0) | |
2654 | log_debug("Release agent already installed."); | |
2655 | } | |
663996b3 | 2656 | |
f5e65279 MB |
2657 | /* 5. Make sure we are in the special "init.scope" unit in the root slice. */ |
2658 | scope_path = strjoina(m->cgroup_root, "/" SPECIAL_INIT_SCOPE); | |
2659 | r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, scope_path, 0); | |
b012e921 MB |
2660 | if (r >= 0) { |
2661 | /* Also, move all other userspace processes remaining in the root cgroup into that scope. */ | |
2662 | r = cg_migrate(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, SYSTEMD_CGROUP_CONTROLLER, scope_path, 0); | |
2663 | if (r < 0) | |
2664 | log_warning_errno(r, "Couldn't move remaining userspace processes, ignoring: %m"); | |
5eef597e | 2665 | |
b012e921 MB |
2666 | /* 6. And pin it, so that it cannot be unmounted */ |
2667 | safe_close(m->pin_cgroupfs_fd); | |
2668 | m->pin_cgroupfs_fd = open(path, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY|O_NONBLOCK); | |
2669 | if (m->pin_cgroupfs_fd < 0) | |
2670 | return log_error_errno(errno, "Failed to open pin file: %m"); | |
f5e65279 | 2671 | |
6e866b33 | 2672 | } else if (!MANAGER_IS_TEST_RUN(m)) |
b012e921 | 2673 | return log_error_errno(r, "Failed to create %s control group: %m", scope_path); |
f5e65279 MB |
2674 | |
2675 | /* 7. Always enable hierarchical support if it exists... */ | |
6e866b33 | 2676 | if (!all_unified && !MANAGER_IS_TEST_RUN(m)) |
f5e65279 | 2677 | (void) cg_set_attribute("memory", "/", "memory.use_hierarchy", "1"); |
663996b3 | 2678 | |
6e866b33 | 2679 | /* 8. Figure out which controllers are supported */ |
d9dfd233 MP |
2680 | r = cg_mask_supported(&m->cgroup_supported); |
2681 | if (r < 0) | |
2682 | return log_error_errno(r, "Failed to determine supported controllers: %m"); | |
6e866b33 MB |
2683 | |
2684 | /* 9. Figure out which bpf-based pseudo-controllers are supported */ | |
2685 | r = cg_bpf_mask_supported(&mask); | |
2686 | if (r < 0) | |
2687 | return log_error_errno(r, "Failed to determine supported bpf-based pseudo-controllers: %m"); | |
2688 | m->cgroup_supported |= mask; | |
2689 | ||
2690 | /* 10. Log which controllers are supported */ | |
d9dfd233 | 2691 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) |
5a920b42 | 2692 | log_debug("Controller '%s' supported: %s", cgroup_controller_to_string(c), yes_no(m->cgroup_supported & CGROUP_CONTROLLER_TO_MASK(c))); |
663996b3 | 2693 | |
663996b3 MS |
2694 | return 0; |
2695 | } | |
2696 | ||
2697 | void manager_shutdown_cgroup(Manager *m, bool delete) { | |
2698 | assert(m); | |
2699 | ||
14228c0d MB |
2700 | /* We can't really delete the group, since we are in it. But |
2701 | * let's trim it. */ | |
b012e921 | 2702 | if (delete && m->cgroup_root && m->test_run_flags != MANAGER_TEST_RUN_MINIMAL) |
d9dfd233 MP |
2703 | (void) cg_trim(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, false); |
2704 | ||
f5e65279 MB |
2705 | m->cgroup_empty_event_source = sd_event_source_unref(m->cgroup_empty_event_source); |
2706 | ||
d9dfd233 MP |
2707 | m->cgroup_inotify_wd_unit = hashmap_free(m->cgroup_inotify_wd_unit); |
2708 | ||
2709 | m->cgroup_inotify_event_source = sd_event_source_unref(m->cgroup_inotify_event_source); | |
2710 | m->cgroup_inotify_fd = safe_close(m->cgroup_inotify_fd); | |
663996b3 | 2711 | |
60f067b4 | 2712 | m->pin_cgroupfs_fd = safe_close(m->pin_cgroupfs_fd); |
663996b3 | 2713 | |
d9dfd233 | 2714 | m->cgroup_root = mfree(m->cgroup_root); |
663996b3 MS |
2715 | } |
2716 | ||
14228c0d | 2717 | Unit* manager_get_unit_by_cgroup(Manager *m, const char *cgroup) { |
663996b3 | 2718 | char *p; |
14228c0d | 2719 | Unit *u; |
663996b3 MS |
2720 | |
2721 | assert(m); | |
2722 | assert(cgroup); | |
663996b3 | 2723 | |
14228c0d MB |
2724 | u = hashmap_get(m->cgroup_unit, cgroup); |
2725 | if (u) | |
2726 | return u; | |
663996b3 MS |
2727 | |
2728 | p = strdupa(cgroup); | |
663996b3 MS |
2729 | for (;;) { |
2730 | char *e; | |
2731 | ||
2732 | e = strrchr(p, '/'); | |
d9dfd233 MP |
2733 | if (!e || e == p) |
2734 | return hashmap_get(m->cgroup_unit, SPECIAL_ROOT_SLICE); | |
663996b3 MS |
2735 | |
2736 | *e = 0; | |
2737 | ||
14228c0d MB |
2738 | u = hashmap_get(m->cgroup_unit, p); |
2739 | if (u) | |
2740 | return u; | |
663996b3 MS |
2741 | } |
2742 | } | |
2743 | ||
d9dfd233 | 2744 | Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid) { |
14228c0d | 2745 | _cleanup_free_ char *cgroup = NULL; |
663996b3 | 2746 | |
663996b3 MS |
2747 | assert(m); |
2748 | ||
1d42b86d | 2749 | if (!pid_is_valid(pid)) |
663996b3 MS |
2750 | return NULL; |
2751 | ||
1d42b86d | 2752 | if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup) < 0) |
663996b3 MS |
2753 | return NULL; |
2754 | ||
14228c0d | 2755 | return manager_get_unit_by_cgroup(m, cgroup); |
663996b3 MS |
2756 | } |
2757 | ||
d9dfd233 | 2758 | Unit *manager_get_unit_by_pid(Manager *m, pid_t pid) { |
1d42b86d | 2759 | Unit *u, **array; |
d9dfd233 MP |
2760 | |
2761 | assert(m); | |
2762 | ||
1d42b86d MB |
2763 | /* Note that a process might be owned by multiple units, we return only one here, which is good enough for most |
2764 | * cases, though not strictly correct. We prefer the one reported by cgroup membership, as that's the most | |
2765 | * relevant one as children of the process will be assigned to that one, too, before all else. */ | |
2766 | ||
2767 | if (!pid_is_valid(pid)) | |
d9dfd233 MP |
2768 | return NULL; |
2769 | ||
1d42b86d | 2770 | if (pid == getpid_cached()) |
d9dfd233 MP |
2771 | return hashmap_get(m->units, SPECIAL_INIT_SCOPE); |
2772 | ||
1d42b86d | 2773 | u = manager_get_unit_by_pid_cgroup(m, pid); |
d9dfd233 MP |
2774 | if (u) |
2775 | return u; | |
2776 | ||
1d42b86d | 2777 | u = hashmap_get(m->watch_pids, PID_TO_PTR(pid)); |
d9dfd233 MP |
2778 | if (u) |
2779 | return u; | |
2780 | ||
1d42b86d MB |
2781 | array = hashmap_get(m->watch_pids, PID_TO_PTR(-pid)); |
2782 | if (array) | |
2783 | return array[0]; | |
2784 | ||
2785 | return NULL; | |
d9dfd233 MP |
2786 | } |
2787 | ||
14228c0d MB |
2788 | int manager_notify_cgroup_empty(Manager *m, const char *cgroup) { |
2789 | Unit *u; | |
663996b3 | 2790 | |
14228c0d MB |
2791 | assert(m); |
2792 | assert(cgroup); | |
663996b3 | 2793 | |
f5e65279 MB |
2794 | /* Called on the legacy hierarchy whenever we get an explicit cgroup notification from the cgroup agent process |
2795 | * or from the --system instance */ | |
2796 | ||
aa27b158 MP |
2797 | log_debug("Got cgroup empty notification for: %s", cgroup); |
2798 | ||
14228c0d | 2799 | u = manager_get_unit_by_cgroup(m, cgroup); |
e3bff60a MP |
2800 | if (!u) |
2801 | return 0; | |
663996b3 | 2802 | |
f5e65279 MB |
2803 | unit_add_to_cgroup_empty_queue(u); |
2804 | return 1; | |
e3bff60a MP |
2805 | } |
2806 | ||
2807 | int unit_get_memory_current(Unit *u, uint64_t *ret) { | |
2808 | _cleanup_free_ char *v = NULL; | |
2809 | int r; | |
2810 | ||
2811 | assert(u); | |
2812 | assert(ret); | |
2813 | ||
f5e65279 MB |
2814 | if (!UNIT_CGROUP_BOOL(u, memory_accounting)) |
2815 | return -ENODATA; | |
2816 | ||
e3bff60a MP |
2817 | if (!u->cgroup_path) |
2818 | return -ENODATA; | |
2819 | ||
98393f85 | 2820 | /* The root cgroup doesn't expose this information, let's get it from /proc instead */ |
6e866b33 | 2821 | if (unit_has_host_root_cgroup(u)) |
7c20daf6 | 2822 | return procfs_memory_get_used(ret); |
98393f85 | 2823 | |
d9dfd233 | 2824 | if ((u->cgroup_realized_mask & CGROUP_MASK_MEMORY) == 0) |
e3bff60a MP |
2825 | return -ENODATA; |
2826 | ||
2897b343 MP |
2827 | r = cg_all_unified(); |
2828 | if (r < 0) | |
2829 | return r; | |
2830 | if (r > 0) | |
d9dfd233 | 2831 | r = cg_get_attribute("memory", u->cgroup_path, "memory.current", &v); |
2897b343 MP |
2832 | else |
2833 | r = cg_get_attribute("memory", u->cgroup_path, "memory.usage_in_bytes", &v); | |
e3bff60a MP |
2834 | if (r == -ENOENT) |
2835 | return -ENODATA; | |
2836 | if (r < 0) | |
2837 | return r; | |
2838 | ||
2839 | return safe_atou64(v, ret); | |
2840 | } | |
2841 | ||
6300502b MP |
2842 | int unit_get_tasks_current(Unit *u, uint64_t *ret) { |
2843 | _cleanup_free_ char *v = NULL; | |
2844 | int r; | |
2845 | ||
2846 | assert(u); | |
2847 | assert(ret); | |
2848 | ||
f5e65279 MB |
2849 | if (!UNIT_CGROUP_BOOL(u, tasks_accounting)) |
2850 | return -ENODATA; | |
2851 | ||
6300502b MP |
2852 | if (!u->cgroup_path) |
2853 | return -ENODATA; | |
2854 | ||
1d42b86d | 2855 | /* The root cgroup doesn't expose this information, let's get it from /proc instead */ |
6e866b33 | 2856 | if (unit_has_host_root_cgroup(u)) |
1d42b86d MB |
2857 | return procfs_tasks_get_current(ret); |
2858 | ||
98393f85 MB |
2859 | if ((u->cgroup_realized_mask & CGROUP_MASK_PIDS) == 0) |
2860 | return -ENODATA; | |
2861 | ||
6300502b MP |
2862 | r = cg_get_attribute("pids", u->cgroup_path, "pids.current", &v); |
2863 | if (r == -ENOENT) | |
2864 | return -ENODATA; | |
2865 | if (r < 0) | |
2866 | return r; | |
2867 | ||
2868 | return safe_atou64(v, ret); | |
2869 | } | |
2870 | ||
e3bff60a MP |
2871 | static int unit_get_cpu_usage_raw(Unit *u, nsec_t *ret) { |
2872 | _cleanup_free_ char *v = NULL; | |
2873 | uint64_t ns; | |
2874 | int r; | |
2875 | ||
2876 | assert(u); | |
2877 | assert(ret); | |
2878 | ||
2879 | if (!u->cgroup_path) | |
2880 | return -ENODATA; | |
2881 | ||
98393f85 | 2882 | /* The root cgroup doesn't expose this information, let's get it from /proc instead */ |
6e866b33 | 2883 | if (unit_has_host_root_cgroup(u)) |
98393f85 MB |
2884 | return procfs_cpu_get_usage(ret); |
2885 | ||
6e866b33 MB |
2886 | /* Requisite controllers for CPU accounting are not enabled */ |
2887 | if ((get_cpu_accounting_mask() & ~u->cgroup_realized_mask) != 0) | |
2888 | return -ENODATA; | |
2889 | ||
2897b343 MP |
2890 | r = cg_all_unified(); |
2891 | if (r < 0) | |
2892 | return r; | |
2893 | if (r > 0) { | |
8a584da2 MP |
2894 | _cleanup_free_ char *val = NULL; |
2895 | uint64_t us; | |
e3bff60a | 2896 | |
98393f85 | 2897 | r = cg_get_keyed_attribute("cpu", u->cgroup_path, "cpu.stat", STRV_MAKE("usage_usec"), &val); |
98393f85 MB |
2898 | if (IN_SET(r, -ENOENT, -ENXIO)) |
2899 | return -ENODATA; | |
6e866b33 MB |
2900 | if (r < 0) |
2901 | return r; | |
8a584da2 MP |
2902 | |
2903 | r = safe_atou64(val, &us); | |
2904 | if (r < 0) | |
2905 | return r; | |
2906 | ||
2907 | ns = us * NSEC_PER_USEC; | |
2908 | } else { | |
8a584da2 MP |
2909 | r = cg_get_attribute("cpuacct", u->cgroup_path, "cpuacct.usage", &v); |
2910 | if (r == -ENOENT) | |
2911 | return -ENODATA; | |
2912 | if (r < 0) | |
2913 | return r; | |
2914 | ||
2915 | r = safe_atou64(v, &ns); | |
2916 | if (r < 0) | |
2917 | return r; | |
2918 | } | |
e3bff60a MP |
2919 | |
2920 | *ret = ns; | |
2921 | return 0; | |
2922 | } | |
2923 | ||
2924 | int unit_get_cpu_usage(Unit *u, nsec_t *ret) { | |
2925 | nsec_t ns; | |
2926 | int r; | |
2927 | ||
8a584da2 MP |
2928 | assert(u); |
2929 | ||
2930 | /* Retrieve the current CPU usage counter. This will subtract the CPU counter taken when the unit was | |
2931 | * started. If the cgroup has been removed already, returns the last cached value. To cache the value, simply | |
2932 | * call this function with a NULL return value. */ | |
2933 | ||
f5e65279 MB |
2934 | if (!UNIT_CGROUP_BOOL(u, cpu_accounting)) |
2935 | return -ENODATA; | |
2936 | ||
e3bff60a | 2937 | r = unit_get_cpu_usage_raw(u, &ns); |
8a584da2 MP |
2938 | if (r == -ENODATA && u->cpu_usage_last != NSEC_INFINITY) { |
2939 | /* If we can't get the CPU usage anymore (because the cgroup was already removed, for example), use our | |
2940 | * cached value. */ | |
2941 | ||
2942 | if (ret) | |
2943 | *ret = u->cpu_usage_last; | |
2944 | return 0; | |
2945 | } | |
e3bff60a MP |
2946 | if (r < 0) |
2947 | return r; | |
2948 | ||
8a584da2 MP |
2949 | if (ns > u->cpu_usage_base) |
2950 | ns -= u->cpu_usage_base; | |
e3bff60a MP |
2951 | else |
2952 | ns = 0; | |
2953 | ||
8a584da2 MP |
2954 | u->cpu_usage_last = ns; |
2955 | if (ret) | |
2956 | *ret = ns; | |
2957 | ||
e3bff60a MP |
2958 | return 0; |
2959 | } | |
2960 | ||
f5e65279 MB |
2961 | int unit_get_ip_accounting( |
2962 | Unit *u, | |
2963 | CGroupIPAccountingMetric metric, | |
2964 | uint64_t *ret) { | |
2965 | ||
2966 | uint64_t value; | |
2967 | int fd, r; | |
2968 | ||
2969 | assert(u); | |
2970 | assert(metric >= 0); | |
2971 | assert(metric < _CGROUP_IP_ACCOUNTING_METRIC_MAX); | |
2972 | assert(ret); | |
2973 | ||
f5e65279 MB |
2974 | if (!UNIT_CGROUP_BOOL(u, ip_accounting)) |
2975 | return -ENODATA; | |
2976 | ||
2977 | fd = IN_SET(metric, CGROUP_IP_INGRESS_BYTES, CGROUP_IP_INGRESS_PACKETS) ? | |
2978 | u->ip_accounting_ingress_map_fd : | |
2979 | u->ip_accounting_egress_map_fd; | |
f5e65279 MB |
2980 | if (fd < 0) |
2981 | return -ENODATA; | |
2982 | ||
2983 | if (IN_SET(metric, CGROUP_IP_INGRESS_BYTES, CGROUP_IP_EGRESS_BYTES)) | |
2984 | r = bpf_firewall_read_accounting(fd, &value, NULL); | |
2985 | else | |
2986 | r = bpf_firewall_read_accounting(fd, NULL, &value); | |
2987 | if (r < 0) | |
2988 | return r; | |
2989 | ||
2990 | /* Add in additional metrics from a previous runtime. Note that when reexecing/reloading the daemon we compile | |
2991 | * all BPF programs and maps anew, but serialize the old counters. When deserializing we store them in the | |
2992 | * ip_accounting_extra[] field, and add them in here transparently. */ | |
2993 | ||
2994 | *ret = value + u->ip_accounting_extra[metric]; | |
2995 | ||
2996 | return r; | |
2997 | } | |
2998 | ||
2999 | int unit_reset_cpu_accounting(Unit *u) { | |
e3bff60a MP |
3000 | nsec_t ns; |
3001 | int r; | |
3002 | ||
3003 | assert(u); | |
3004 | ||
8a584da2 MP |
3005 | u->cpu_usage_last = NSEC_INFINITY; |
3006 | ||
e3bff60a MP |
3007 | r = unit_get_cpu_usage_raw(u, &ns); |
3008 | if (r < 0) { | |
8a584da2 | 3009 | u->cpu_usage_base = 0; |
e3bff60a | 3010 | return r; |
663996b3 MS |
3011 | } |
3012 | ||
8a584da2 | 3013 | u->cpu_usage_base = ns; |
14228c0d | 3014 | return 0; |
663996b3 MS |
3015 | } |
3016 | ||
f5e65279 MB |
3017 | int unit_reset_ip_accounting(Unit *u) { |
3018 | int r = 0, q = 0; | |
d9dfd233 MP |
3019 | |
3020 | assert(u); | |
3021 | ||
f5e65279 MB |
3022 | if (u->ip_accounting_ingress_map_fd >= 0) |
3023 | r = bpf_firewall_reset_accounting(u->ip_accounting_ingress_map_fd); | |
3024 | ||
3025 | if (u->ip_accounting_egress_map_fd >= 0) | |
3026 | q = bpf_firewall_reset_accounting(u->ip_accounting_egress_map_fd); | |
d9dfd233 | 3027 | |
f5e65279 MB |
3028 | zero(u->ip_accounting_extra); |
3029 | ||
3030 | return r < 0 ? r : q; | |
d9dfd233 MP |
3031 | } |
3032 | ||
6300502b MP |
3033 | void unit_invalidate_cgroup(Unit *u, CGroupMask m) { |
3034 | assert(u); | |
3035 | ||
3036 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) | |
3037 | return; | |
3038 | ||
3039 | if (m == 0) | |
3040 | return; | |
3041 | ||
aa27b158 MP |
3042 | /* always invalidate compat pairs together */ |
3043 | if (m & (CGROUP_MASK_IO | CGROUP_MASK_BLKIO)) | |
3044 | m |= CGROUP_MASK_IO | CGROUP_MASK_BLKIO; | |
3045 | ||
f5e65279 MB |
3046 | if (m & (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT)) |
3047 | m |= CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT; | |
3048 | ||
6e866b33 | 3049 | if (FLAGS_SET(u->cgroup_invalidated_mask, m)) /* NOP? */ |
6300502b MP |
3050 | return; |
3051 | ||
6e866b33 | 3052 | u->cgroup_invalidated_mask |= m; |
f5e65279 MB |
3053 | unit_add_to_cgroup_realize_queue(u); |
3054 | } | |
3055 | ||
3056 | void unit_invalidate_cgroup_bpf(Unit *u) { | |
3057 | assert(u); | |
3058 | ||
3059 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) | |
3060 | return; | |
3061 | ||
6e866b33 | 3062 | if (u->cgroup_invalidated_mask & CGROUP_MASK_BPF_FIREWALL) /* NOP? */ |
f5e65279 MB |
3063 | return; |
3064 | ||
6e866b33 | 3065 | u->cgroup_invalidated_mask |= CGROUP_MASK_BPF_FIREWALL; |
f5e65279 MB |
3066 | unit_add_to_cgroup_realize_queue(u); |
3067 | ||
3068 | /* If we are a slice unit, we also need to put compile a new BPF program for all our children, as the IP access | |
3069 | * list of our children includes our own. */ | |
3070 | if (u->type == UNIT_SLICE) { | |
3071 | Unit *member; | |
3072 | Iterator i; | |
52ad194e | 3073 | void *v; |
f5e65279 | 3074 | |
52ad194e | 3075 | HASHMAP_FOREACH_KEY(v, member, u->dependencies[UNIT_BEFORE], i) { |
6e866b33 MB |
3076 | if (UNIT_DEREF(member->slice) == u) |
3077 | unit_invalidate_cgroup_bpf(member); | |
f5e65279 MB |
3078 | } |
3079 | } | |
6300502b MP |
3080 | } |
3081 | ||
98393f85 MB |
3082 | bool unit_cgroup_delegate(Unit *u) { |
3083 | CGroupContext *c; | |
3084 | ||
3085 | assert(u); | |
3086 | ||
3087 | if (!UNIT_VTABLE(u)->can_delegate) | |
3088 | return false; | |
3089 | ||
3090 | c = unit_get_cgroup_context(u); | |
3091 | if (!c) | |
3092 | return false; | |
3093 | ||
3094 | return c->delegate; | |
3095 | } | |
3096 | ||
6300502b MP |
3097 | void manager_invalidate_startup_units(Manager *m) { |
3098 | Iterator i; | |
3099 | Unit *u; | |
3100 | ||
3101 | assert(m); | |
3102 | ||
3103 | SET_FOREACH(u, m->startup_units, i) | |
aa27b158 | 3104 | unit_invalidate_cgroup(u, CGROUP_MASK_CPU|CGROUP_MASK_IO|CGROUP_MASK_BLKIO); |
6300502b MP |
3105 | } |
3106 | ||
14228c0d MB |
3107 | static const char* const cgroup_device_policy_table[_CGROUP_DEVICE_POLICY_MAX] = { |
3108 | [CGROUP_AUTO] = "auto", | |
3109 | [CGROUP_CLOSED] = "closed", | |
3110 | [CGROUP_STRICT] = "strict", | |
3111 | }; | |
663996b3 | 3112 | |
14228c0d | 3113 | DEFINE_STRING_TABLE_LOOKUP(cgroup_device_policy, CGroupDevicePolicy); |