xref: /aosp_15_r20/external/ltp/testcases/kernel/controllers/cgroup_fj/cgroup_fj_function.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2009 FUJITSU LIMITED
4# Copyright (c) 2015 Cedric Hnyda <[email protected]>
5# Copyright (c) 2015-2016 Cyril Hrubis <[email protected]>
6# Author: Shi Weihua <[email protected]>
7
8TCID="cgroup_fj_function2"
9TST_TESTFUNC=test
10TST_SETUP=setup
11TST_CLEANUP=cleanup
12TST_CNT=9
13TST_POS_ARGS=1
14
15subsystem=$1
16
17usage_and_exit()
18{
19    echo "usage of cgroup_fj_function2.sh: "
20    echo "  ./cgroup_fj_function2.sh subsystem"
21    echo "example: ./cgroup_fj_function2.sh cpuset"
22
23    tst_brk TBROK "$1"
24}
25
26if [ "$#" -ne "1" ]; then
27    usage_and_exit "Invalid number of parameters"
28fi
29
30# Move a task from group to group
31test1()
32{
33    # mv'ing cgroups is not available in cgroup2
34    if [ "$cgroup_version" = "2" ]; then
35        tst_res TCONF "Controller mounted on cgroup2 hierarchy, skipping test"
36        return
37    fi
38
39    if ! attach_and_check "$pid" "$start_path/ltp_1"; then
40         tst_res TFAIL "Failed to attach task"
41         return
42    fi
43
44    if ! attach_and_check "$pid" "$start_path"; then
45         tst_res TFAIL "Failed to attach task"
46         return
47    fi
48
49    tst_res TPASS "Task attached successfully"
50}
51
52# Group can be renamed with mv
53test2()
54{
55    # mv'ing cgroups is not available in cgroup2
56    if [ "$cgroup_version" = "2" ]; then
57        tst_res TCONF "Controller mounted on cgroup2 hierarchy, skipping test"
58        return
59    fi
60
61    create_subgroup "$start_path/ltp_2"
62
63    if ! mv "$start_path/ltp_2" "$start_path/ltp_3"; then
64        tst_res TFAIL "Failed to move $start_path/ltp_2 to $start_path/ltp_3"
65        rmdir "$start_path/ltp_2"
66        return
67    fi
68
69    if ! rmdir "$start_path/ltp_3"; then
70        tst_res TFAIL "Failed to remove $start_path/ltp_3"
71        return
72    fi
73
74    tst_res TPASS "Successfully moved $start_path/ltp_2 to $start_path/ltp_3"
75}
76
77# Group can be renamed with mv unless the target name exists
78test3()
79{
80    # mv'ing cgroups is not available in cgroup2
81    if [ "$cgroup_version" = "2" ]; then
82        tst_res TCONF "Controller mounted on cgroup2 hierarchy, skipping test"
83        return
84    fi
85
86    create_subgroup "$start_path/ltp_2"
87
88    if mv "$start_path/ltp_2" "$start_path/ltp_1" > /dev/null 2>&1; then
89        tst_res TFAIL "Moved $start_path/ltp_2 over existing $start_path/ltp_1"
90        return
91    fi
92
93    tst_res TPASS "Failed to move $start_path/ltp_2 over existing $start_path/ltp_1"
94
95    ROD rmdir "$start_path/ltp_2"
96}
97
98# Group with attached task cannot be removed
99test4()
100{
101    if ! attach_and_check "$pid" "$start_path/ltp_1"; then
102        tst_res TFAIL "Failed to attach $pid to $start_path/ltp_1"
103        return
104    fi
105
106    if rmdir "$start_path/ltp_1" > /dev/null 2>&1; then
107        tst_res TFAIL "Removed $start_path/ltp_1 which contains task $pid"
108        return
109    fi
110
111    tst_res TPASS "Group $start_path/ltp_1 with task $pid cannot be removed"
112}
113
114# Group with a subgroup cannot be removed
115test5()
116{
117    # We need to move the tasks back to root to create a subgroup
118    if [ "$cgroup_version" = "2" ]; then
119        for pid in $(cat "$start_path/ltp_1/$task_list"); do
120		    echo $pid > "$mount_point/$task_list" 2> /dev/null
121	    done
122
123        ROD echo "+$subsystem" \> "$start_path/ltp_1/cgroup.subtree_control"
124    fi
125
126    create_subgroup "$start_path/ltp_1/a"
127
128    if rmdir "$start_path/ltp_1" > /dev/null 2>&1; then
129        tst_res TFAIL "Removed $start_path/ltp_1 which contains subdir 'a'"
130        return
131    fi
132
133    tst_res TPASS "Dir $start_path/ltp_1 with subdir 'a' cannot be removed"
134
135    ROD rmdir "$start_path/ltp_1/a"
136
137    [ "$cgroup_version" = "2" ] && ROD echo "-$subsystem" \> "$start_path/ltp_1/cgroup.subtree_control"
138    ROD echo "$pid" \> "$start_path/ltp_1/$task_list"
139}
140
141# Group cannot be moved outside of hierarchy
142test6()
143{
144    # mv'ing cgroups is not available in cgroup2
145    if [ "$cgroup_version" = "2" ]; then
146        tst_res TCONF "Controller mounted on cgroup2 hierarchy, skipping test"
147        return
148    fi
149
150    if mv "$start_path/ltp_1" "$PWD/ltp" > /dev/null 2>&1; then
151        tst_res TFAIL "Subgroup $start_path/ltp_1 outside hierarchy to $PWD/ltp"
152        return
153    fi
154
155    tst_res TPASS "Subgroup $start_path/ltp_1 cannot be moved to $PWD/ltp"
156}
157
158# Tasks file cannot be removed
159test7()
160{
161    if rm "$start_path/ltp_1/$task_list" > /dev/null 2>&1; then
162        tst_res TFAIL "Tasks file $start_path/ltp_1/$task_list could be removed"
163        return
164    fi
165
166    tst_res TPASS "Tasks file $start_path/ltp_1/tasks cannot be removed"
167}
168
169# Test notify_on_release with invalid inputs
170test8()
171{
172    # notify_on_release is not available in cgroup2 so skip the test
173    if [ "$cgroup_version" = "2" ]; then
174        tst_res TCONF "Controller mounted on cgroup2 hierarchy, skipping test"
175        return
176    fi
177
178    if echo "-1" > "$start_path/ltp_1/notify_on_release" 2>/dev/null; then
179        tst_res TFAIL "Can write -1 to $start_path/ltp_1/notify_on_release"
180        return
181    fi
182
183    if echo "ltp" > "$start_path/ltp_1/notify_on_release" 2>/dev/null; then
184        tst_res TFAIL "Can write ltp to $start_path/ltp_1/notify_on_release"
185        return
186    fi
187
188    tst_res TPASS "Cannot write invalid values to $start_path/ltp_1/notify_on_release"
189}
190
191# Test that notify_on_release can be changed
192test9()
193{
194    # notify_on_release is not available in cgroup2 so skip the test
195    if [ "$cgroup_version" = "2" ]; then
196        tst_res TCONF "Controller mounted on cgroup2 hierarchy, skipping test"
197        return
198    fi
199
200    local notify=$(ROD cat "$start_path/ltp_1/notify_on_release")
201    local value
202
203    if [ "$notify" -eq 0 ]; then
204        value=1
205    else
206        value=0
207    fi
208
209    if ! echo "$value" > "$start_path/ltp_1/notify_on_release"; then
210        tst_res TFAIL "Failed to set $start_path/ltp_1/notify_on_release to $value"
211        return
212    fi
213
214    ROD echo "$notify" \> "$start_path/ltp_1/notify_on_release"
215
216    tst_res TPASS "Set $start_path/ltp_1/notify_on_release to $value"
217}
218
219setup()
220{
221    common_setup
222    cgroup_fj_proc&
223    pid=$!
224    create_subgroup "$start_path/ltp_1"
225}
226
227cleanup()
228{
229    if [ -n "$pid" ]; then
230        kill -9 $pid >/dev/null 2>&1
231        wait $pid >/dev/null 2>&1
232    fi
233
234    rmdir "$start_path/ltp_1" >/dev/null 2>&1
235    common_cleanup
236}
237
238. cgroup_fj_common.sh
239tst_run
240