#!/bin/bash

[ "$1" == "--clean" -o "$1" == "--pre-restore" ] || exit 0

# Controller's name may differ depending on the kernel's config:
# `net_prio' if only CONFIG_CGROUP_NET_PRIO is set
# `net_cls,net_prio' if also CONFIG_CGROUP_NET_CLASSID is set
controller=$(sed -n '/net_prio/s/.*:\([^:]*net_prio[^:]*\):.*/\1/p' /proc/self/cgroup)

echo "Controller's name '$controller'"

tname=$(mktemp -d cgclean.XXXXXX)
mount -t cgroup foobar $tname -o "$controller"

echo "Cleaning $tname"

set +e
rmdir "$tname/zdtmtst"
set -e

umount "$tname"
rmdir "$tname"
