1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0-or-later 3# Copyright (c) 2019-2022 Petr Vorel <pvorel@suse.cz> 4# Copyright (c) 2018-2019 ARM Ltd. All Rights Reserved. 5# Copyright (c) 2022 Canonical Ltd. 6 7_cgroup_state= 8 9# Find mountpoint of the given controller 10# USAGE: cgroup_get_mountpoint CONTROLLER 11# RETURNS: Prints the mountpoint of the given controller 12# Must call cgroup_require before calling 13cgroup_get_mountpoint() 14{ 15 local ctrl="$1" 16 local mountpoint 17 18 [ "$ctrl" ] || tst_brk TBROK "cgroup_get_mountpoint: controller not defined" 19 [ "$_cgroup_state" ] || tst_brk TBROK "cgroup_get_mountpoint: No previous state found. Forgot to call cgroup_require?" 20 21 mountpoint=$(echo "$_cgroup_state" | grep -w "^$ctrl" | awk '{ print $4 }') 22 echo "$mountpoint" 23 24 return 0 25} 26 27# Get the test path of a given controller that has been created by the cgroup C API 28# USAGE: cgroup_get_test_path CONTROLLER 29# RETURNS: Prints the path to the test direcory 30# Must call cgroup_require before calling 31cgroup_get_test_path() 32{ 33 local ctrl="$1" 34 local mountpoint 35 local test_path 36 37 [ "$ctrl" ] || tst_brk TBROK "cgroup_get_test_path: controller not defined" 38 [ "$_cgroup_state" ] || tst_brk TBROK "cgroup_get_test_path: No previous state found. Forgot to call cgroup_require?" 39 40 mountpoint=$(cgroup_get_mountpoint "$ctrl") 41 42 test_path="$mountpoint/ltp/test-$$" 43 44 [ ! -e "$test_path" ] && tst_brk TBROK "cgroup_get_test_path: No test path found. Forgot to call cgroup_require?" 45 46 echo "$test_path" 47 48 return 0 49} 50 51# Gets the cgroup version of the given controller 52# USAGE: cgroup_get_version CONTROLLER 53# RETURNS: "1" if version 1 and "2" if version 2 54# Must call cgroup_require before calling 55cgroup_get_version() 56{ 57 local ctrl="$1" 58 local version 59 60 [ "$ctrl" ] || tst_brk TBROK "cgroup_get_version: controller not defined" 61 [ "$_cgroup_state" ] || tst_brk TBROK "cgroup_get_version: No previous state found. Forgot to call cgroup_require?" 62 63 version=$(echo "$_cgroup_state" | grep -w "^$ctrl" | awk '{ print $2 }') 64 [ "$version" ] || tst_brk TBROK "cgroup_get_version: Could not find controller $ctrl" 65 66 echo "$version" 67 68 return 0 69} 70 71# Cleans up any setup done by calling cgroup_require. 72# USAGE: cgroup_cleanup 73# Can be safely called even when no setup has been done 74cgroup_cleanup() 75{ 76 [ "$_cgroup_state" ] || return 0 77 78 ROD tst_cgctl cleanup "$_cgroup_state" 79 80 _cgroup_state= 81 82 return 0 83} 84 85# Get the task list of the given controller 86# USAGE: cgroup_get_task_list CONTROLLER 87# RETURNS: prints out "cgroup.procs" if version 2 otherwise "tasks" 88# Must call cgroup_require before calling 89cgroup_get_task_list() 90{ 91 local ctrl="$1" 92 local version 93 94 [ "$ctrl" ] || tst_brk TBROK "cgroup_get_task_list: controller not defined" 95 96 version=$(cgroup_get_version "$ctrl") 97 98 if [ "$version" = "2" ]; then 99 echo "cgroup.procs" 100 else 101 echo "tasks" 102 fi 103 104 return 0 105} 106 107# Mounts and configures the given controller 108# USAGE: cgroup_require CONTROLLER 109cgroup_require() 110{ 111 local ctrl="$1" 112 local ret 113 114 [ "$ctrl" ] || tst_brk TBROK "cgroup_require: controller not defined" 115 116 [ ! -f /proc/cgroups ] && tst_brk TCONF "Kernel does not support control groups" 117 118 _cgroup_state=$(tst_cgctl require "$ctrl" $$) 119 ret=$? 120 121 if [ $ret -eq 32 ]; then 122 tst_brk TCONF "'tst_cgctl require' exited. Controller is probably not available?" 123 return $ret 124 fi 125 126 if [ $ret -ne 0 ]; then 127 tst_brk TBROK "'tst_cgctl require' exited" 128 return $ret 129 fi 130 131 [ "$_cgroup_state" ] || tst_brk TBROK "cgroup_require: No state was set after call to tst_cgctl require?" 132 133 return 0 134} 135 136. tst_test.sh 137