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