| 1 | #!/bin/sh |
| 2 | # Run with a Vile executable and a delegated cgroup v2 directory whose |
| 3 | # subtree_control includes memory. Only the new child group receives limits. |
| 4 | set -eu |
| 5 | if [ "$#" -ne 2 ] || [ ! -x "$1" ] || [ ! -f "$2/cgroup.controllers" ]; then |
| 6 | echo "usage: $0 /path/to/vile /path/to/delegated/cgroup" >&2 |
| 7 | exit 2 |
| 8 | fi |
| 9 | group=$(mktemp -d "$2/vile-memory.XXXXXX") |
| 10 | trap 'rmdir "$group"' EXIT |
| 11 | if [ ! -f "$group/memory.max" ]; then |
| 12 | echo "the delegated cgroup requires the memory controller" >&2 |
| 13 | exit 2 |
| 14 | fi |
| 15 | echo 50331648 > "$group/memory.max" |
| 16 | if [ -f "$group/memory.swap.max" ]; then echo 0 > "$group/memory.swap.max"; fi |
| 17 | |
| 18 | # The worker joins before exec. The shell driving the checks stays outside. |
| 19 | run() { |
| 20 | sh -ec 'echo 0 > "$1/cgroup.procs"; exec "$2" -s -e "$3"' sh "$group" "$1" "$2" |
| 21 | } |
| 22 | |
| 23 | [ "$(run "$1" '(+ 20 22)')" = 42 ] |
| 24 | status=0 |
| 25 | run "$1" '(bytevector-u8-ref (make-bytevector 100663296 17) 0)' || status=$? |
| 26 | [ "$status" -eq 137 ] |
| 27 | awk '$1 == "oom_kill" && $2 > 0 { killed = 1 } END { exit !killed }' "$group/memory.events" |
| 28 | [ "$(run "$1" '(+ 20 22)')" = 42 ] |
| 29 | cat "$group/memory.events" |
| 30 | |