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