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