HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/core/cgroup.c

179,588 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 543d1ac8e3c43a5af0861e2f1a038485663554eb

Lines 1–1000 of 4,696

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
 
#include "sd-bus.h"
#include "sd-messages.h"
 
#include "af-list.h"
#include "alloc-util.h"
#include "blockdev-util.h"
#include "bpf-bind-iface.h"
#include "bpf-devices.h"
#include "bpf-firewall.h"
#include "bpf-foreign.h"
#include "bpf-program.h"
#include "bpf-restrict-ifaces.h"
#include "bpf-socket-bind.h"
#include "btrfs-util.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "cgroup.h"
#include "cgroup-setup.h"
#include "cgroup-util.h"
#include "devnum-util.h"
#include "errno-util.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fdset.h"
#include "fileio.h"
#include "in-addr-prefix-util.h"
#include "inotify-util.h"
#include "ip-protocol-list.h"
#include "limits-util.h"
#include "manager.h"
#include "mountpoint-util.h"
#include "netlink-internal.h"
#include "nulstr-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "percent-util.h"
#include "pidref.h"
#include "process-util.h"
#include "procfs-util.h"
#include "serialize.h"
#include "set.h"
#include "special.h"
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "virt.h"
 
#if BPF_FRAMEWORK
#include "bpf-link.h"
#include "bpf-restrict-fs.h"
#endif
 
#define CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC (100 * USEC_PER_MSEC)
 
/* Returns the log level to use when cgroup attribute writes fail. When an attribute is missing or we have access
 * problems we downgrade to LOG_DEBUG. This is supposed to be nice to container managers and kernels which want to mask
 * out specific attributes from us. */
#define LOG_LEVEL_CGROUP_WRITE(r) ((ABS(r) == ENOENT || ERRNO_IS_FS_WRITE_REFUSED(r)) ? LOG_DEBUG : LOG_WARNING)
 
static void unit_remove_from_cgroup_empty_queue(Unit *u);
 
uint64_t cgroup_tasks_max_resolve(const CGroupTasksMax *tasks_max) {
        if (tasks_max->scale == 0)
                return tasks_max->value;
 
        return system_tasks_max_scale(tasks_max->value, tasks_max->scale);
}
 
bool manager_owns_host_root_cgroup(Manager *m) {
        assert(m);
 
        /* Returns true if we are managing the root cgroup. Note that it isn't sufficient to just check whether the
         * group root path equals "/" since that will also be the case if CLONE_NEWCGROUP is in the mix. Since there's
         * appears to be no nice way to detect whether we are in a CLONE_NEWCGROUP namespace we instead just check if
         * we run in any kind of container virtualization. */
 
        if (MANAGER_IS_USER(m))
                return false;
 
        if (detect_container() > 0)
                return false;
 
        return empty_or_root(m->cgroup_root);
}
 
bool unit_has_host_root_cgroup(const Unit *u) {
        assert(u);
        assert(u->manager);
 
        /* Returns whether this unit manages the root cgroup. This will return true if this unit is the root slice and
         * the manager manages the root cgroup. */
 
        if (!manager_owns_host_root_cgroup(u->manager))
                return false;
 
        return unit_has_name(u, SPECIAL_ROOT_SLICE);
}
 
bool unit_has_startup_cgroup_constraints(Unit *u) {
        assert(u);
 
        /* Returns true if this unit has any directives which apply during
         * startup/shutdown phases. */
 
        CGroupContext *c;
 
        c = unit_get_cgroup_context(u);
        if (!c)
                return false;
 
        return c->startup_io_weight != CGROUP_WEIGHT_INVALID ||
               c->startup_cpuset_cpus.set ||
               c->startup_cpuset_mems.set ||
               c->startup_memory_high_set ||
               c->startup_memory_max_set ||
               c->startup_memory_swap_max_set||
               c->startup_memory_zswap_max_set ||
               c->startup_memory_low_set;
}
 
static int set_attribute_and_warn(Unit *u, const char *attribute, const char *value) {
        int r;
 
        assert(u);
        assert(attribute);
        assert(value);
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (!crt || !crt->cgroup_path)
                return -EOWNERDEAD;
 
        r = cg_set_attribute(crt->cgroup_path, attribute, value);
        if (r < 0)
                log_unit_full_errno(u, LOG_LEVEL_CGROUP_WRITE(r), r, "Failed to set '%s' attribute on '%s' to '%.*s': %m",
                                    attribute, empty_to_root(crt->cgroup_path), (int) strcspn(value, NEWLINE), value);
 
        return r;
}
 
void cgroup_context_init(CGroupContext *c) {
        assert(c);
 
        /* Initialize everything to the kernel defaults. When initializing a bool member to 'true', make
         * sure to serialize in execute-serialize.c using serialize_bool() instead of
         * serialize_bool_elide(), as sd-executor will initialize here to 'true', but serialize_bool_elide()
         * skips serialization if the value is 'false' (as that's the common default), so if the value at
         * runtime is zero it would be lost after deserialization. Same when initializing uint64_t and other
         * values, update/add a conditional serialization check. This is to minimize the amount of
         * serialized data that is sent to the sd-executor, so that there is less work to do on the default
         * cases. */
 
        *c = (CGroupContext) {
                .cpu_weight = CGROUP_WEIGHT_INVALID,
                .startup_cpu_weight = CGROUP_WEIGHT_INVALID,
                .cpu_quota_per_sec_usec = USEC_INFINITY,
                .cpu_quota_period_usec = USEC_INFINITY,
 
                .memory_high = CGROUP_LIMIT_MAX,
                .startup_memory_high = CGROUP_LIMIT_MAX,
                .memory_max = CGROUP_LIMIT_MAX,
                .startup_memory_max = CGROUP_LIMIT_MAX,
                .memory_swap_max = CGROUP_LIMIT_MAX,
                .startup_memory_swap_max = CGROUP_LIMIT_MAX,
                .memory_zswap_max = CGROUP_LIMIT_MAX,
                .startup_memory_zswap_max = CGROUP_LIMIT_MAX,
 
                .memory_zswap_writeback = true,
 
                .io_weight = CGROUP_WEIGHT_INVALID,
                .startup_io_weight = CGROUP_WEIGHT_INVALID,
 
                .tasks_max = CGROUP_TASKS_MAX_UNSET,
 
                .cpuset_partition = _CPUSET_PARTITION_INVALID,
 
                .moom_swap = MANAGED_OOM_AUTO,
                .moom_mem_pressure = MANAGED_OOM_AUTO,
                .moom_preference = MANAGED_OOM_PREFERENCE_NONE,
                /* The default duration value in oomd.conf will be used when
                 * moom_mem_pressure_duration_usec is set to infinity. */
                .moom_mem_pressure_duration_usec = USEC_INFINITY,
 
                .pressure = {
                        [PRESSURE_MEMORY] = { .watch = _CGROUP_PRESSURE_WATCH_INVALID, .threshold_usec = USEC_INFINITY },
                        [PRESSURE_CPU]    = { .watch = _CGROUP_PRESSURE_WATCH_INVALID, .threshold_usec = USEC_INFINITY },
                        [PRESSURE_IO]     = { .watch = _CGROUP_PRESSURE_WATCH_INVALID, .threshold_usec = USEC_INFINITY },
                },
        };
}
 
void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a) {
        assert(c);
        assert(a);
 
        LIST_REMOVE(device_allow, c->device_allow, a);
        free(a->path);
        free(a);
}
 
void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w) {
        assert(c);
        assert(w);
 
        LIST_REMOVE(device_weights, c->io_device_weights, w);
        free(w->path);
        free(w);
}
 
void cgroup_context_free_io_device_latency(CGroupContext *c, CGroupIODeviceLatency *l) {
        assert(c);
        assert(l);
 
        LIST_REMOVE(device_latencies, c->io_device_latencies, l);
        free(l->path);
        free(l);
}
 
void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l) {
        assert(c);
        assert(l);
 
        LIST_REMOVE(device_limits, c->io_device_limits, l);
        free(l->path);
        free(l);
}
 
void cgroup_context_remove_bpf_foreign_program(CGroupContext *c, CGroupBPFForeignProgram *p) {
        assert(c);
        assert(p);
 
        LIST_REMOVE(programs, c->bpf_foreign_programs, p);
        free(p->bpffs_path);
        free(p);
}
 
void cgroup_context_remove_socket_bind(CGroupSocketBindItem **head) {
        assert(head);
 
        LIST_CLEAR(socket_bind_items, *head, free);
}
 
void cgroup_context_done(CGroupContext *c) {
        assert(c);
 
        while (c->io_device_weights)
                cgroup_context_free_io_device_weight(c, c->io_device_weights);
 
        while (c->io_device_latencies)
                cgroup_context_free_io_device_latency(c, c->io_device_latencies);
 
        while (c->io_device_limits)
                cgroup_context_free_io_device_limit(c, c->io_device_limits);
 
        while (c->device_allow)
                cgroup_context_free_device_allow(c, c->device_allow);
 
        cgroup_context_remove_socket_bind(&c->socket_bind_allow);
        cgroup_context_remove_socket_bind(&c->socket_bind_deny);
 
        c->ip_address_allow = set_free(c->ip_address_allow);
        c->ip_address_deny = set_free(c->ip_address_deny);
 
        c->ip_filters_ingress = strv_free(c->ip_filters_ingress);
        c->ip_filters_egress = strv_free(c->ip_filters_egress);
 
        while (c->bpf_foreign_programs)
                cgroup_context_remove_bpf_foreign_program(c, c->bpf_foreign_programs);
 
        c->restrict_network_interfaces = set_free(c->restrict_network_interfaces);
 
        c->bind_network_interface = mfree(c->bind_network_interface);
 
        cpu_set_done(&c->cpuset_cpus);
        cpu_set_done(&c->startup_cpuset_cpus);
        cpu_set_done(&c->cpuset_mems);
        cpu_set_done(&c->startup_cpuset_mems);
 
        c->delegate_subgroup = mfree(c->delegate_subgroup);
 
        c->moom_rules = strv_free(c->moom_rules);
 
        nft_set_context_clear(&c->nft_set_context);
}
 
static int unit_get_kernel_memory_limit(Unit *u, const char *file, uint64_t *ret) {
        assert(u);
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (!crt || !crt->cgroup_path)
                return -EOWNERDEAD;
 
        return cg_get_attribute_as_uint64(crt->cgroup_path, file, ret);
}
 
static int unit_compare_memory_limit(Unit *u, const char *property_name, uint64_t *ret_unit_value, uint64_t *ret_kernel_value) {
        CGroupContext *c;
        CGroupMask m;
        const char *file;
        uint64_t unit_value;
        int r;
 
        /* Compare kernel memcg configuration against our internal systemd state.
         *
         * Returns:
         *
         * <0: On error.
         *  0: If the kernel memory setting doesn't match our configuration.
         * >0: If the kernel memory setting matches our configuration.
         *
         * The following values are only guaranteed to be populated on return >=0:
         *
         * - ret_unit_value will contain our internal expected value for the unit, page-aligned.
         * - ret_kernel_value will contain the actual value presented by the kernel. */
 
        assert(u);
        assert(ret_unit_value);
        assert(ret_kernel_value);
 
        /* The root slice doesn't have any controller files, so we can't compare anything. */
        if (unit_has_name(u, SPECIAL_ROOT_SLICE))
                return -ENODATA;
 
        /* It's possible to have MemoryFoo set without systemd wanting to have the memory controller enabled,
         * for example, in the case of DisableControllers= or cgroup_disable on the kernel command line. To
         * avoid specious errors in these scenarios, check that we even expect the memory controller to be
         * enabled at all. */
        m = unit_get_target_mask(u);
        if (!FLAGS_SET(m, CGROUP_MASK_MEMORY))
                return -ENODATA;
 
        assert_se(c = unit_get_cgroup_context(u));
 
        bool startup = u->manager && IN_SET(manager_state(u->manager), MANAGER_STARTING, MANAGER_INITIALIZING, MANAGER_STOPPING);
 
        if (streq(property_name, "MemoryLow")) {
                unit_value = c->memory_low;
                file = "memory.low";
        } else if (startup && streq(property_name, "StartupMemoryLow")) {
                unit_value = c->startup_memory_low;
                file = "memory.low";
        } else if (streq(property_name, "MemoryMin")) {
                unit_value = c->memory_min;
                file = "memory.min";
        } else if (streq(property_name, "MemoryHigh")) {
                unit_value = c->memory_high;
                file = "memory.high";
        } else if (startup && streq(property_name, "StartupMemoryHigh")) {
                unit_value = c->startup_memory_high;
                file = "memory.high";
        } else if (streq(property_name, "MemoryMax")) {
                unit_value = c->memory_max;
                file = "memory.max";
        } else if (startup && streq(property_name, "StartupMemoryMax")) {
                unit_value = c->startup_memory_max;
                file = "memory.max";
        } else if (streq(property_name, "MemorySwapMax")) {
                unit_value = c->memory_swap_max;
                file = "memory.swap.max";
        } else if (startup && streq(property_name, "StartupMemorySwapMax")) {
                unit_value = c->startup_memory_swap_max;
                file = "memory.swap.max";
        } else if (streq(property_name, "MemoryZSwapMax")) {
                unit_value = c->memory_zswap_max;
                file = "memory.zswap.max";
        } else if (startup && streq(property_name, "StartupMemoryZSwapMax")) {
                unit_value = c->startup_memory_zswap_max;
                file = "memory.zswap.max";
        } else
                return -EINVAL;
 
        r = unit_get_kernel_memory_limit(u, file, ret_kernel_value);
        if (r < 0)
                return log_unit_debug_errno(u, r, "Failed to parse %s: %m", file);
 
        /* It's intended (soon) in a future kernel to not expose cgroup memory limits rounded to page
         * boundaries, but instead separate the user-exposed limit, which is whatever userspace told us, from
         * our internal page-counting. To support those future kernels, just check the value itself first
         * without any page-alignment. */
        if (*ret_kernel_value == unit_value) {
                *ret_unit_value = unit_value;
                return 1;
        }
 
        /* The current kernel behaviour, by comparison, is that even if you write a particular number of
         * bytes into a cgroup memory file, it always returns that number page-aligned down (since the kernel
         * internally stores cgroup limits in pages). As such, so long as it aligns properly, everything is
         * cricket. */
        if (unit_value != CGROUP_LIMIT_MAX)
                unit_value = PAGE_ALIGN_DOWN(unit_value);
 
        *ret_unit_value = unit_value;
 
        return *ret_kernel_value == *ret_unit_value;
}
 
#define FORMAT_CGROUP_DIFF_MAX 128
 
static char *format_cgroup_memory_limit_comparison(Unit *u, const char *property_name, char *buf, size_t l) {
        uint64_t kval, sval;
        int r;
 
        assert(u);
        assert(property_name);
        assert(buf);
        assert(l > 0);
 
        r = unit_compare_memory_limit(u, property_name, &sval, &kval);
 
        /* memory.swap.max is special in that it relies on CONFIG_MEMCG_SWAP (and the default swapaccount=1).
         * In the absence of reliably being able to detect whether memcg swap support is available or not,
         * only complain if the error is not ENOENT. This is similarly the case for memory.zswap.max relying
         * on CONFIG_ZSWAP. */
        if (r > 0 || IN_SET(r, -ENODATA, -EOWNERDEAD) ||
            (r == -ENOENT && STR_IN_SET(property_name,
                                        "MemorySwapMax",
                                        "StartupMemorySwapMax",
                                        "MemoryZSwapMax",
                                        "StartupMemoryZSwapMax")))
                buf[0] = 0;
        else if (r < 0) {
                errno = -r;
                (void) snprintf(buf, l, " (error getting kernel value: %m)");
        } else
                (void) snprintf(buf, l, " (different value in kernel: %" PRIu64 ")", kval);
 
        return buf;
}
 
const char* cgroup_device_permissions_to_string(CGroupDevicePermissions p) {
        static const char *table[_CGROUP_DEVICE_PERMISSIONS_MAX] = {
                /* Lets simply define a table with every possible combination. As long as those are just 8 we
                 * can get away with it. If this ever grows to more we need to revisit this logic though. */
                [0]                                                          = "",
                [CGROUP_DEVICE_READ]                                         = "r",
                [CGROUP_DEVICE_WRITE]                                        = "w",
                [CGROUP_DEVICE_MKNOD]                                        = "m",
                [CGROUP_DEVICE_READ|CGROUP_DEVICE_WRITE]                     = "rw",
                [CGROUP_DEVICE_READ|CGROUP_DEVICE_MKNOD]                     = "rm",
                [CGROUP_DEVICE_WRITE|CGROUP_DEVICE_MKNOD]                    = "wm",
                [CGROUP_DEVICE_READ|CGROUP_DEVICE_WRITE|CGROUP_DEVICE_MKNOD] = "rwm",
        };
 
        if (p < 0 || p >= _CGROUP_DEVICE_PERMISSIONS_MAX)
                return NULL;
 
        return table[p];
}
 
CGroupDevicePermissions cgroup_device_permissions_from_string(const char *s) {
        CGroupDevicePermissions p = 0;
 
        if (!s)
                return _CGROUP_DEVICE_PERMISSIONS_INVALID;
 
        for (const char *c = s; *c; c++) {
                if (*c == 'r')
                        p |= CGROUP_DEVICE_READ;
                else if (*c == 'w')
                        p |= CGROUP_DEVICE_WRITE;
                else if (*c == 'm')
                        p |= CGROUP_DEVICE_MKNOD;
                else
                        return _CGROUP_DEVICE_PERMISSIONS_INVALID;
        }
 
        return p;
}
 
void cgroup_context_dump(Unit *u, FILE* f, const char *prefix) {
        _cleanup_free_ char *disable_controllers_str = NULL, *delegate_controllers_str = NULL, *cpuset_cpus = NULL, *cpuset_mems = NULL, *startup_cpuset_cpus = NULL, *startup_cpuset_mems = NULL;
        CGroupContext *c;
        struct in_addr_prefix *iaai;
        char cda[FORMAT_CGROUP_DIFF_MAX], cdb[FORMAT_CGROUP_DIFF_MAX], cdc[FORMAT_CGROUP_DIFF_MAX], cdd[FORMAT_CGROUP_DIFF_MAX],
                cde[FORMAT_CGROUP_DIFF_MAX], cdf[FORMAT_CGROUP_DIFF_MAX], cdg[FORMAT_CGROUP_DIFF_MAX], cdh[FORMAT_CGROUP_DIFF_MAX],
                cdi[FORMAT_CGROUP_DIFF_MAX], cdj[FORMAT_CGROUP_DIFF_MAX], cdk[FORMAT_CGROUP_DIFF_MAX];
 
        assert(u);
        assert(f);
 
        assert_se(c = unit_get_cgroup_context(u));
 
        prefix = strempty(prefix);
 
        (void) cg_mask_to_string(c->disable_controllers, &disable_controllers_str);
        (void) cg_mask_to_string(c->delegate_controllers, &delegate_controllers_str);
 
        /* "Delegate=" means "yes, but no controllers". Show this as "(none)". */
        const char *delegate_str = delegate_controllers_str ?: c->delegate ? "(none)" : "no";
 
        cpuset_cpus = cpu_set_to_range_string(&c->cpuset_cpus);
        startup_cpuset_cpus = cpu_set_to_range_string(&c->startup_cpuset_cpus);
        cpuset_mems = cpu_set_to_range_string(&c->cpuset_mems);
        startup_cpuset_mems = cpu_set_to_range_string(&c->startup_cpuset_mems);
 
        fprintf(f,
                "%sIOAccounting: %s\n"
                "%sMemoryAccounting: %s\n"
                "%sTasksAccounting: %s\n"
                "%sIPAccounting: %s\n"
                "%sCPUWeight: %" PRIu64 "\n"
                "%sStartupCPUWeight: %" PRIu64 "\n"
                "%sCPUQuotaPerSecSec: %s\n"
                "%sCPUQuotaPeriodSec: %s\n"
                "%sAllowedCPUs: %s\n"
                "%sStartupAllowedCPUs: %s\n"
                "%sAllowedMemoryNodes: %s\n"
                "%sStartupAllowedMemoryNodes: %s\n"
                "%sCPUSetPartition: %s\n"
                "%sIOWeight: %" PRIu64 "\n"
                "%sStartupIOWeight: %" PRIu64 "\n"
                "%sMemoryMin: %" PRIu64 "%s\n"
                "%sMemoryLow: %" PRIu64 "%s\n"
                "%sStartupMemoryLow: %" PRIu64 "%s\n"
                "%sMemoryHigh: %" PRIu64 "%s\n"
                "%sStartupMemoryHigh: %" PRIu64 "%s\n"
                "%sMemoryMax: %" PRIu64 "%s\n"
                "%sStartupMemoryMax: %" PRIu64 "%s\n"
                "%sMemorySwapMax: %" PRIu64 "%s\n"
                "%sStartupMemorySwapMax: %" PRIu64 "%s\n"
                "%sMemoryZSwapMax: %" PRIu64 "%s\n"
                "%sStartupMemoryZSwapMax: %" PRIu64 "%s\n"
                "%sMemoryZSwapWriteback: %s\n"
                "%sTasksMax: %" PRIu64 "\n"
                "%sDevicePolicy: %s\n"
                "%sDisableControllers: %s\n"
                "%sDelegate: %s\n"
                "%sManagedOOMSwap: %s\n"
                "%sManagedOOMMemoryPressure: %s\n"
                "%sManagedOOMMemoryPressureLimit: " PERMYRIAD_AS_PERCENT_FORMAT_STR "\n"
                "%sManagedOOMPreference: %s\n"
                "%sMemoryPressureWatch: %s\n"
                "%sCPUPressureWatch: %s\n"
                "%sIOPressureWatch: %s\n"
                "%sCoredumpReceive: %s\n",
                prefix, yes_no(c->io_accounting),
                prefix, yes_no(c->memory_accounting),
                prefix, yes_no(c->tasks_accounting),
                prefix, yes_no(c->ip_accounting),
                prefix, c->cpu_weight,
                prefix, c->startup_cpu_weight,
                prefix, FORMAT_TIMESPAN(c->cpu_quota_per_sec_usec, 1),
                prefix, FORMAT_TIMESPAN(c->cpu_quota_period_usec, 1),
                prefix, strempty(cpuset_cpus),
                prefix, strempty(startup_cpuset_cpus),
                prefix, strempty(cpuset_mems),
                prefix, strempty(startup_cpuset_mems),
                prefix, strna(cpuset_partition_to_string(c->cpuset_partition)),
                prefix, c->io_weight,
                prefix, c->startup_io_weight,
                prefix, c->memory_min, format_cgroup_memory_limit_comparison(u, "MemoryMin", cda, sizeof(cda)),
                prefix, c->memory_low, format_cgroup_memory_limit_comparison(u, "MemoryLow", cdb, sizeof(cdb)),
                prefix, c->startup_memory_low, format_cgroup_memory_limit_comparison(u, "StartupMemoryLow", cdc, sizeof(cdc)),
                prefix, c->memory_high, format_cgroup_memory_limit_comparison(u, "MemoryHigh", cdd, sizeof(cdd)),
                prefix, c->startup_memory_high, format_cgroup_memory_limit_comparison(u, "StartupMemoryHigh", cde, sizeof(cde)),
                prefix, c->memory_max, format_cgroup_memory_limit_comparison(u, "MemoryMax", cdf, sizeof(cdf)),
                prefix, c->startup_memory_max, format_cgroup_memory_limit_comparison(u, "StartupMemoryMax", cdg, sizeof(cdg)),
                prefix, c->memory_swap_max, format_cgroup_memory_limit_comparison(u, "MemorySwapMax", cdh, sizeof(cdh)),
                prefix, c->startup_memory_swap_max, format_cgroup_memory_limit_comparison(u, "StartupMemorySwapMax", cdi, sizeof(cdi)),
                prefix, c->memory_zswap_max, format_cgroup_memory_limit_comparison(u, "MemoryZSwapMax", cdj, sizeof(cdj)),
                prefix, c->startup_memory_zswap_max, format_cgroup_memory_limit_comparison(u, "StartupMemoryZSwapMax", cdk, sizeof(cdk)),
                prefix, yes_no(c->memory_zswap_writeback),
                prefix, cgroup_tasks_max_resolve(&c->tasks_max),
                prefix, cgroup_device_policy_to_string(c->device_policy),
                prefix, strempty(disable_controllers_str),
                prefix, delegate_str,
                prefix, managed_oom_mode_to_string(c->moom_swap),
                prefix, managed_oom_mode_to_string(c->moom_mem_pressure),
                prefix, PERMYRIAD_AS_PERCENT_FORMAT_VAL(UINT32_SCALE_TO_PERMYRIAD(c->moom_mem_pressure_limit)),
                prefix, managed_oom_preference_to_string(c->moom_preference),
                prefix, cgroup_pressure_watch_to_string(c->pressure[PRESSURE_MEMORY].watch),
                prefix, cgroup_pressure_watch_to_string(c->pressure[PRESSURE_CPU].watch),
                prefix, cgroup_pressure_watch_to_string(c->pressure[PRESSURE_IO].watch),
                prefix, yes_no(c->coredump_receive));
 
        if (c->delegate_subgroup)
                fprintf(f, "%sDelegateSubgroup: %s\n",
                        prefix, c->delegate_subgroup);
 
        if (c->bind_network_interface)
                fprintf(f, "%sBindNetworkInterface: %s\n",
                        prefix, c->bind_network_interface);
 
        if (c->pressure[PRESSURE_MEMORY].threshold_usec != USEC_INFINITY)
                fprintf(f, "%sMemoryPressureThresholdSec: %s\n",
                        prefix, FORMAT_TIMESPAN(c->pressure[PRESSURE_MEMORY].threshold_usec, 1));
 
        if (c->pressure[PRESSURE_CPU].threshold_usec != USEC_INFINITY)
                fprintf(f, "%sCPUPressureThresholdSec: %s\n",
                        prefix, FORMAT_TIMESPAN(c->pressure[PRESSURE_CPU].threshold_usec, 1));
 
        if (c->pressure[PRESSURE_IO].threshold_usec != USEC_INFINITY)
                fprintf(f, "%sIOPressureThresholdSec: %s\n",
                        prefix, FORMAT_TIMESPAN(c->pressure[PRESSURE_IO].threshold_usec, 1));
 
        if (c->moom_mem_pressure_duration_usec != USEC_INFINITY)
                fprintf(f, "%sManagedOOMMemoryPressureDurationSec: %s\n",
                        prefix, FORMAT_TIMESPAN(c->moom_mem_pressure_duration_usec, 1));
 
        LIST_FOREACH(device_allow, a, c->device_allow)
                /* strna() below should be redundant, for avoiding -Werror=format-overflow= error. See #30223. */
                fprintf(f,
                        "%sDeviceAllow: %s %s\n",
                        prefix,
                        a->path,
                        strna(cgroup_device_permissions_to_string(a->permissions)));
 
        LIST_FOREACH(device_weights, iw, c->io_device_weights)
                fprintf(f,
                        "%sIODeviceWeight: %s %" PRIu64 "\n",
                        prefix,
                        iw->path,
                        iw->weight);
 
        LIST_FOREACH(device_latencies, l, c->io_device_latencies)
                fprintf(f,
                        "%sIODeviceLatencyTargetSec: %s %s\n",
                        prefix,
                        l->path,
                        FORMAT_TIMESPAN(l->target_usec, 1));
 
        LIST_FOREACH(device_limits, il, c->io_device_limits)
                for (CGroupIOLimitType type = 0; type < _CGROUP_IO_LIMIT_TYPE_MAX; type++)
                        if (il->limits[type] != cgroup_io_limit_defaults[type])
                                fprintf(f,
                                        "%s%s: %s %s\n",
                                        prefix,
                                        cgroup_io_limit_type_to_string(type),
                                        il->path,
                                        FORMAT_BYTES(il->limits[type]));
 
        SET_FOREACH(iaai, c->ip_address_allow)
                fprintf(f, "%sIPAddressAllow: %s\n", prefix,
                        IN_ADDR_PREFIX_TO_STRING(iaai->family, &iaai->address, iaai->prefixlen));
        SET_FOREACH(iaai, c->ip_address_deny)
                fprintf(f, "%sIPAddressDeny: %s\n", prefix,
                        IN_ADDR_PREFIX_TO_STRING(iaai->family, &iaai->address, iaai->prefixlen));
 
        STRV_FOREACH(path, c->ip_filters_ingress)
                fprintf(f, "%sIPIngressFilterPath: %s\n", prefix, *path);
        STRV_FOREACH(path, c->ip_filters_egress)
                fprintf(f, "%sIPEgressFilterPath: %s\n", prefix, *path);
 
        LIST_FOREACH(programs, p, c->bpf_foreign_programs)
                fprintf(f, "%sBPFProgram: %s:%s",
                        prefix, bpf_cgroup_attach_type_to_string(p->attach_type), p->bpffs_path);
 
        if (c->socket_bind_allow) {
                fprintf(f, "%sSocketBindAllow: ", prefix);
                cgroup_context_dump_socket_bind_items(c->socket_bind_allow, f);
                fputc('\n', f);
        }
 
        if (c->socket_bind_deny) {
                fprintf(f, "%sSocketBindDeny: ", prefix);
                cgroup_context_dump_socket_bind_items(c->socket_bind_deny, f);
                fputc('\n', f);
        }
 
        if (c->restrict_network_interfaces) {
                char *iface;
                SET_FOREACH(iface, c->restrict_network_interfaces)
                        fprintf(f, "%sRestrictNetworkInterfaces: %s\n", prefix, iface);
        }
 
        FOREACH_ARRAY(nft_set, c->nft_set_context.sets, c->nft_set_context.n_sets)
                fprintf(f, "%sNFTSet: %s:%s:%s:%s\n", prefix, nft_set_source_to_string(nft_set->source),
                        nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set);
 
        STRV_FOREACH(rule, c->moom_rules)
                fprintf(f, "%sOOMRules: %s\n", prefix, *rule);
}
 
void cgroup_context_dump_socket_bind_item(const CGroupSocketBindItem *item, FILE *f) {
        const char *family, *colon1, *protocol = "", *colon2 = "";
 
        family = strempty(af_to_ipv4_ipv6(item->address_family));
        colon1 = isempty(family) ? "" : ":";
 
        if (item->ip_protocol != 0) {
                protocol = ip_protocol_to_tcp_udp(item->ip_protocol);
                colon2 = ":";
        }
 
        if (item->nr_ports == 0)
                fprintf(f, "%s%s%s%sany", family, colon1, protocol, colon2);
        else if (item->nr_ports == 1)
                fprintf(f, "%s%s%s%s%" PRIu16, family, colon1, protocol, colon2, item->port_min);
        else {
                uint16_t port_max = item->port_min + item->nr_ports - 1;
                fprintf(f, "%s%s%s%s%" PRIu16 "-%" PRIu16, family, colon1, protocol, colon2,
                        item->port_min, port_max);
        }
}
 
void cgroup_context_dump_socket_bind_items(const CGroupSocketBindItem *items, FILE *f) {
        bool first = true;
 
        LIST_FOREACH(socket_bind_items, bi, items) {
                if (first)
                        first = false;
                else
                        fputc(' ', f);
 
                cgroup_context_dump_socket_bind_item(bi, f);
        }
}
 
int cgroup_context_add_device_allow(CGroupContext *c, const char *dev, CGroupDevicePermissions p) {
        _cleanup_free_ CGroupDeviceAllow *a = NULL;
        _cleanup_free_ char *d = NULL;
 
        assert(c);
        assert(dev);
        assert(p >= 0 && p < _CGROUP_DEVICE_PERMISSIONS_MAX);
 
        if (p == 0)
                p = _CGROUP_DEVICE_PERMISSIONS_ALL;
 
        a = new(CGroupDeviceAllow, 1);
        if (!a)
                return -ENOMEM;
 
        d = strdup(dev);
        if (!d)
                return -ENOMEM;
 
        *a = (CGroupDeviceAllow) {
                .path = TAKE_PTR(d),
                .permissions = p,
        };
 
        LIST_PREPEND(device_allow, c->device_allow, a);
        TAKE_PTR(a);
 
        return 0;
}
 
int cgroup_context_add_or_update_device_allow(CGroupContext *c, const char *dev, CGroupDevicePermissions p) {
        assert(c);
        assert(dev);
        assert(p >= 0 && p < _CGROUP_DEVICE_PERMISSIONS_MAX);
 
        if (p == 0)
                p = _CGROUP_DEVICE_PERMISSIONS_ALL;
 
        LIST_FOREACH(device_allow, b, c->device_allow)
                if (path_equal(b->path, dev)) {
                        b->permissions = p;
                        return 0;
                }
 
        return cgroup_context_add_device_allow(c, dev, p);
}
 
int cgroup_context_add_bpf_foreign_program(CGroupContext *c, uint32_t attach_type, const char *bpffs_path) {
        CGroupBPFForeignProgram *p;
        _cleanup_free_ char *d = NULL;
 
        assert(c);
        assert(bpffs_path);
 
        if (!path_is_normalized(bpffs_path) || !path_is_absolute(bpffs_path))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path is not normalized.");
 
        d = strdup(bpffs_path);
        if (!d)
                return log_oom();
 
        p = new(CGroupBPFForeignProgram, 1);
        if (!p)
                return log_oom();
 
        *p = (CGroupBPFForeignProgram) {
                .attach_type = attach_type,
                .bpffs_path = TAKE_PTR(d),
        };
 
        LIST_PREPEND(programs, c->bpf_foreign_programs, TAKE_PTR(p));
 
        return 0;
}
 
static void unit_set_xattr_graceful(Unit *u, const char *name, const void *data, size_t size) {
        int r;
 
        assert(u);
        assert(name);
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (!crt || !crt->cgroup_path)
                return;
 
        r = cg_set_xattr(crt->cgroup_path, name, data, size, 0);
        if (r < 0)
                log_unit_debug_errno(u, r, "Failed to set '%s' xattr on control group %s, ignoring: %m", name, empty_to_root(crt->cgroup_path));
}
 
static void unit_remove_xattr_graceful(Unit *u, const char *name) {
        int r;
 
        assert(u);
        assert(name);
 
        CGroupRuntime *crt = unit_get_cgroup_runtime(u);
        if (!crt || !crt->cgroup_path)
                return;
 
        r = cg_remove_xattr(crt->cgroup_path, name);
        if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
                log_unit_debug_errno(u, r, "Failed to remove '%s' xattr flag on control group %s, ignoring: %m", name, empty_to_root(crt->cgroup_path));
}
 
static void cgroup_oomd_xattr_apply(Unit *u) {
        CGroupContext *c;
 
        assert(u);
 
        c = unit_get_cgroup_context(u);
        if (!c)
                return;
 
        if (c->moom_preference == MANAGED_OOM_PREFERENCE_OMIT)
                unit_set_xattr_graceful(u, "user.oomd_omit", "1", 1);
 
        if (c->moom_preference == MANAGED_OOM_PREFERENCE_AVOID)
                unit_set_xattr_graceful(u, "user.oomd_avoid", "1", 1);
 
        if (c->moom_preference != MANAGED_OOM_PREFERENCE_AVOID)
                unit_remove_xattr_graceful(u, "user.oomd_avoid");
 
        if (c->moom_preference != MANAGED_OOM_PREFERENCE_OMIT)
                unit_remove_xattr_graceful(u, "user.oomd_omit");
}
 
static int cgroup_log_xattr_apply(Unit *u) {
        ExecContext *c;
        size_t len, allowed_patterns_len, denied_patterns_len;
        _cleanup_free_ char *patterns = NULL, *allowed_patterns = NULL, *denied_patterns = NULL;
        char *last;
        int r;
 
        assert(u);
 
        c = unit_get_exec_context(u);
        if (!c)
                /* Some unit types have a cgroup context but no exec context, so we do not log
                 * any error here to avoid confusion. */
                return 0;
 
        if (set_isempty(c->log_filter_allowed_patterns) && set_isempty(c->log_filter_denied_patterns)) {
                unit_remove_xattr_graceful(u, "user.journald_log_filter_patterns");
                return 0;
        }
 
        r = set_make_nulstr(c->log_filter_allowed_patterns, &allowed_patterns, &allowed_patterns_len);
        if (r < 0)
                return log_debug_errno(r, "Failed to make nulstr from set: %m");
 
        r = set_make_nulstr(c->log_filter_denied_patterns, &denied_patterns, &denied_patterns_len);
        if (r < 0)
                return log_debug_errno(r, "Failed to make nulstr from set: %m");
 
        /* Use nul character separated strings without trailing nul */
        allowed_patterns_len = LESS_BY(allowed_patterns_len, 1u);
        denied_patterns_len = LESS_BY(denied_patterns_len, 1u);
 
        len = allowed_patterns_len + 1 + denied_patterns_len;
        patterns = new(char, len);
        if (!patterns)
                return log_oom_debug();
 
        last = mempcpy_safe(patterns, allowed_patterns, allowed_patterns_len);
        *(last++) = '\xff';
        memcpy_safe(last, denied_patterns, denied_patterns_len);
 
        unit_set_xattr_graceful(u, "user.journald_log_filter_patterns", patterns, len);
 
        return 0;
}
 
static void cgroup_invocation_id_xattr_apply(Unit *u) {
        bool b;
 
        assert(u);
 
        b = !sd_id128_is_null(u->invocation_id);
        FOREACH_STRING(xn, "trusted.invocation_id", "user.invocation_id") {
                if (b)
                        unit_set_xattr_graceful(u, xn, SD_ID128_TO_STRING(u->invocation_id), 32);
                else
                        unit_remove_xattr_graceful(u, xn);
        }
}
 
static void cgroup_coredump_xattr_apply(Unit *u) {
        CGroupContext *c;
 
        assert(u);
 
        c = unit_get_cgroup_context(u);
        if (!c)
                return;
 
        if (unit_cgroup_delegate(u) && c->coredump_receive)
                unit_set_xattr_graceful(u, "user.coredump_receive", "1", 1);
        else
                unit_remove_xattr_graceful(u, "user.coredump_receive");
}
 
static void cgroup_delegate_xattr_apply(Unit *u) {
        bool b;
 
        assert(u);
 
        /* Indicate on the cgroup whether delegation is on, via an xattr. This is best-effort, as old kernels
         * didn't support xattrs on cgroups at all. Later they got support for setting 'trusted.*' xattrs,
         * and even later 'user.*' xattrs. We started setting this field when 'trusted.*' was added, and
         * given this is now pretty much API, let's continue to support that. But also set 'user.*' as well,
         * since it is readable by any user, not just CAP_SYS_ADMIN. This hence comes with slightly weaker
         * security (as users who got delegated cgroups could turn it off if they like), but this shouldn't
         * be a big problem given this communicates delegation state to clients, but the manager never reads
         * it. */
        b = unit_cgroup_delegate(u);
        FOREACH_STRING(xn, "trusted.delegate", "user.delegate") {
                if (b)
                        unit_set_xattr_graceful(u, xn, "1", 1);
                else
                        unit_remove_xattr_graceful(u, xn);
        }
}
 
static void cgroup_survive_xattr_apply(Unit *u) {
        assert(u);
 
        if (u->survive_final_kill_signal)
                unit_set_xattr_graceful(u, "user.survive_final_kill_signal", "1", 1);
        else
                unit_remove_xattr_graceful(u, "user.survive_final_kill_signal");
}
 
static void cgroup_xattr_apply(Unit *u) {
        assert(u);
 
        /* The 'user.*' xattrs can be set from a user manager. */
        cgroup_oomd_xattr_apply(u);
        cgroup_log_xattr_apply(u);
        cgroup_coredump_xattr_apply(u);
 
        if (!MANAGER_IS_SYSTEM(u->manager))
                return;
 
        cgroup_invocation_id_xattr_apply(u);
        cgroup_delegate_xattr_apply(u);
        cgroup_survive_xattr_apply(u);
}
 
static int lookup_block_device(const char *p, dev_t *ret) {
        dev_t rdev, dev = 0;
        mode_t mode;
        int r;
 
        assert(p);
        assert(ret);
 
        r = device_path_parse_major_minor(p, &mode, &rdev);
        if (r == -ENODEV) { /* not a parsable device node, need to go to disk */
                struct stat st;
 
                if (stat(p, &st) < 0)
                        return log_warning_errno(errno, "Couldn't stat device '%s': %m", p);
 
                mode = st.st_mode;
                rdev = st.st_rdev;
                dev = st.st_dev;
        } else if (r < 0)
                return log_warning_errno(r, "Failed to parse major/minor from path '%s': %m", p);
 
        if (S_ISCHR(mode))
                return log_warning_errno(SYNTHETIC_ERRNO(ENOTBLK),
                                         "Device node '%s' is a character device, but block device needed.", p);
        if (S_ISBLK(mode))
                *ret = rdev;
        else if (major(dev) != 0)
                *ret = dev; /* If this is not a device node then use the block device this file is stored on */
        else {
                /* If this is btrfs, getting the backing block device is a bit harder */
                r = btrfs_get_block_device(p, ret);
                if (r == -ENOTTY)
                        return log_warning_errno(SYNTHETIC_ERRNO(ENODEV),
                                                 "'%s' is not a block device node, and file system block device cannot be determined or is not local.", p);
                if (r < 0)
                        return log_warning_errno(r, "Failed to determine block device backing btrfs file system '%s': %m", p);
        }