[LTP] [PATCH v1] controllers.memcg_regression: add trap to clean up directories

Edward Liaw edliaw@google.com
Fri Jul 1 23:37:49 CEST 2022


The memcg_regression test creates a memcg/ directory that can be left
behind if the test exits unexpectedly.  Use a trap to clean up the
directories on exit.

Signed-off-by: Edward Liaw <edliaw@google.com>
---
 .../memcg/regression/memcg_regression_test.sh | 20 +++++++++++--------
 .../memcg/regression/memcg_test_4.sh          | 11 ++++++----
 2 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/testcases/kernel/controllers/memcg/regression/memcg_regression_test.sh b/testcases/kernel/controllers/memcg/regression/memcg_regression_test.sh
index c91a4069e..2467ae0e6 100755
--- a/testcases/kernel/controllers/memcg/regression/memcg_regression_test.sh
+++ b/testcases/kernel/controllers/memcg/regression/memcg_regression_test.sh
@@ -54,6 +54,15 @@ nr_null=0
 nr_warning=0
 nr_lockdep=0
 
+clean_up()
+{
+	# remove the cgroup
+	rmdir memcg/0 2> /dev/null
+	# unmount cgroup if still mounted
+	umount memcg/
+	rmdir memcg/
+}
+
 # check_kernel_bug - check if some kind of kernel bug happened
 check_kernel_bug()
 {
@@ -102,12 +111,12 @@ check_kernel_bug()
 #---------------------------------------------------------------------------
 test_1()
 {
-	mkdir memcg/0/
+	mkdir memcg/0
 	echo 0 > memcg/0/memory.limit_in_bytes
 
 	./memcg_test_1
 
-	rmdir memcg/0/
+	rmdir memcg/0
 
 	check_kernel_bug
 	if [ $? -eq 1 ]; then
@@ -211,14 +220,12 @@ test_4()
 	killall -9 memcg_test_4 2> /dev/null
 	killall -9 memcg_test_4.sh 2> /dev/null
 
-	# if test_4.sh gets killed, it won't clean cgroup it created
-	rmdir memcg/0 2> /dev/null
-
 	swapon -a
 }
 
 # main
 failed=0
+trap clean_up EXIT
 mkdir memcg/
 
 for cur in $(seq 1 $TST_TOTAL); do
@@ -236,7 +243,4 @@ for cur in $(seq 1 $TST_TOTAL); do
 	umount memcg/
 done
 
-rmdir memcg/
-
 exit $failed
-
diff --git a/testcases/kernel/controllers/memcg/regression/memcg_test_4.sh b/testcases/kernel/controllers/memcg/regression/memcg_test_4.sh
index 620031366..d002601f1 100755
--- a/testcases/kernel/controllers/memcg/regression/memcg_test_4.sh
+++ b/testcases/kernel/controllers/memcg/regression/memcg_test_4.sh
@@ -22,6 +22,13 @@
 ##                                                                            ##
 ################################################################################
 
+clean_up()
+{
+	# remove the cgroup when exiting
+	rmdir memcg/0
+}
+
+trap clean_up EXIT
 # attach current task to memcg/0/
 mkdir memcg/0
 echo $$ > memcg/0/tasks
@@ -42,7 +49,3 @@ swapoff -a
 sleep 1
 echo $pid > memcg/tasks 2> /dev/null
 echo $$ > memcg/tasks 2> /dev/null
-
-# now remove the cgroup
-rmdir memcg/0
-
-- 
2.37.0.rc0.161.g10f37bed90-goog



More information about the ltp mailing list