1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -eux
4set -o pipefail
5
6if grep -q cgroup2 /proc/filesystems ; then
7    systemd-run --wait --unit=test0.service -p "DynamicUser=1" -p "Delegate=" \
8                test -w /sys/fs/cgroup/system.slice/test0.service/ -a \
9                -w /sys/fs/cgroup/system.slice/test0.service/cgroup.procs -a \
10                -w /sys/fs/cgroup/system.slice/test0.service/cgroup.subtree_control
11
12    systemd-run --wait --unit=test1.service -p "DynamicUser=1" -p "Delegate=memory pids" \
13                grep -q memory /sys/fs/cgroup/system.slice/test1.service/cgroup.controllers
14
15    systemd-run --wait --unit=test2.service -p "DynamicUser=1" -p "Delegate=memory pids" \
16                grep -q pids /sys/fs/cgroup/system.slice/test2.service/cgroup.controllers
17
18    # "io" is not among the controllers enabled by default for all units, verify that
19    grep -qv io /sys/fs/cgroup/system.slice/cgroup.controllers
20
21    # Run a service with "io" enabled, and verify it works
22    systemd-run --wait --unit=test3.service -p "IOAccounting=yes" -p "Slice=system-foo-bar-baz.slice"  \
23                grep -q io /sys/fs/cgroup/system.slice/system-foo.slice/system-foo-bar.slice/system-foo-bar-baz.slice/test3.service/cgroup.controllers
24
25    # We want to check if "io" is removed again from the controllers
26    # list. However, PID 1 (rightfully) does this asynchronously. In order
27    # to force synchronization on this, let's start a short-lived service
28    # which requires PID 1 to refresh the cgroup tree, so that we can
29    # verify that this all works.
30    systemd-run --wait --unit=test4.service true
31
32    # And now check again, "io" should have vanished
33    grep -qv io /sys/fs/cgroup/system.slice/cgroup.controllers
34else
35    echo "Skipping TEST-19-DELEGATE, as the kernel doesn't actually support cgroup v2" >&2
36fi
37
38echo OK >/testok
39
40exit 0
41