Skip to contentSkip to conversation

common: Move exit related functions to common/exit

PATCH In review Nirjhar Roy (IBM) 3 participants 11 messages Apr 30, 2025 – May 6, 2025

lore
Share & tools
lore
#0 lore raw

v3 cover common: Move exit related functions to common/exit

This patch series moves all the exit related functions to a separate file - common/exit. This will remove the dependency to source non-related files to use these exit related functions. Thanks to Dave for suggesting this[1]. The second patch replaces exit with _exit in check file - I missed replacing them in [2].

[v2] -> v3
Addressed Dave's feedbacks.
In patch [1/2]
- Removed _die() and die_now() from common/exit
- Replaced die_now() with _fatal in common/punch
- Removed sourcing of common/exit and common/test_names from common/config and moved them to the beginning of check.
- Added sourcing of common/test_names in _begin_fstest() since common/config is no more sourcing common/test_names.
- Added a blank line in _begin_fstest() after sourcing common/{exit,test_names}
In patch [2/2]
- Replaced "_exit 1" with _fatal and "echo <error message>; _exit 1" with _fatal <error message>.
- Reverted to "exit \$status" in the trap handler registration in check - just to make it more obvious to the reader that we are capturing $status as the final exit value.

[v1] https://lore.kernel.org/all/[email protected]/
[v2] https://lore.kernel.org/all/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/48dacdf636be19ae8bff66cc3852d27e28030613.1744181682.git.nirjhar.roy.lists@gmail.com/

Nirjhar Roy (IBM) (2):
common: Move exit related functions to a common/exit
check: Replace exit with _fatal and _exit in check

 check           | 54 ++++++++++++++++++-------------------------------
 common/config   | 17 ----------------
 common/exit     | 39 +++++++++++++++++++++++++++++++++++
 common/preamble |  3 +++
 common/punch    | 39 ++++++++++++++++-------------------
 common/rc       | 28 -------------------------
 6 files changed, 79 insertions(+), 101 deletions(-)
 create mode 100644 common/exit

--
2.34.1

#1 lore raw

PATCH v3 1/2 common: Move exit related functions to a common/exit

Introduce a new file common/exit that will contain all the exit related functions. This will remove the dependencies these functions have on other non-related helper files and they can be indepedently sourced. This was suggested by Dave Chinner[1].
While moving the exit related functions, remove _die() and die_now() and replace die_now with _fatal(). It is of no use to keep the unnecessary wrappers.

[1] https://lore.kernel.org/linux-xfs/[email protected]/

---
 check           |  2 ++
 common/config   | 17 -----------------
 common/exit     | 39 +++++++++++++++++++++++++++++++++++++++
 common/preamble |  3 +++
 common/punch    | 39 +++++++++++++++++----------------------
 common/rc       | 28 ----------------------------
 6 files changed, 61 insertions(+), 67 deletions(-)
 create mode 100644 common/exit
check +2 -0
index 9451c350..bd84f213 100755
@@ -46,6 +46,8 @@ export DIFF_LENGTH=${DIFF_LENGTH:=10}
 
 # by default don't output timestamps
 timestamp=${TIMESTAMP:=false}
+. common/exit
+. common/test_names
 
 rm -f $tmp.list $tmp.tmp $tmp.grep $here/$iam.out $tmp.report.* $tmp.arglist
 
common/config +0 -17
index eada3971..22b52432 100644
@@ -39,8 +39,6 @@
 #   validity or mountedness.
 #
 
-. common/test_names
-
 # all tests should use a common language setting to prevent golden
 # output mismatches.
 export LANG=C
@@ -96,15 +94,6 @@ export LOCAL_CONFIGURE_OPTIONS=${LOCAL_CONFIGURE_OPTIONS:=--enable-readline=yes}
 
 export RECREATE_TEST_DEV=${RECREATE_TEST_DEV:=false}
 
-# This functions sets the exit code to status and then exits. Don't use
-# exit directly, as it might not set the value of "$status" correctly, which is
-# used as an exit code in the trap handler routine set up by the check script.
-_exit()
-{
-	test -n "$1" && status="$1"
-	exit "$status"
-}
-
 # Handle mkfs.$fstyp which does (or does not) require -f to overwrite
 set_mkfs_prog_path_with_opts()
 {
@@ -121,12 +110,6 @@ set_mkfs_prog_path_with_opts()
 	fi
 }
 
-_fatal()
-{
-    echo "$*"
-    _exit 1
-}
-
 export MKFS_PROG="$(type -P mkfs)"
 [ "$MKFS_PROG" = "" ] && _fatal "mkfs not found"
 
common/exit new file +39 -0
new file mode 100644
index 00000000..222c79b7
@@ -0,0 +1,39 @@
+##/bin/bash
+
+# This functions sets the exit code to status and then exits. Don't use
+# exit directly, as it might not set the value of "$status" correctly, which is
+# used as an exit code in the trap handler routine set up by the check script.
+_exit()
+{
+	test -n "$1" && status="$1"
+	exit "$status"
+}
+
+_fatal()
+{
+    echo "$*"
+    _exit 1
+}
+
+# just plain bail out
+#
+_fail()
+{
+    echo "$*" | tee -a $seqres.full
+    echo "(see $seqres.full for details)"
+    _exit 1
+}
+
+# bail out, setting up .notrun file. Need to kill the filesystem check files
+# here, otherwise they are set incorrectly for the next test.
+#
+_notrun()
+{
+    echo "$*" > $seqres.notrun
+    echo "$seq not run: $*"
+    rm -f ${RESULT_DIR}/require_test*
+    rm -f ${RESULT_DIR}/require_scratch*
+
+    _exit 0
+}
+
common/preamble +3 -0
index ba029a34..51d03396 100644
@@ -33,6 +33,9 @@ _register_cleanup()
 # explicitly as a member of the 'all' group.
 _begin_fstest()
 {
+	. common/exit
+	. common/test_names
+
 	if [ -n "$seq" ]; then
 		echo "_begin_fstest can only be called once!"
 		_exit 1
common/punch +17 -22
index 64d665d8..ddbe757a 100644
@@ -222,11 +222,6 @@ _filter_bmap()
 	_coalesce_extents
 }
 
-die_now()
-{
-	_exit 1
-}
-
 # test the different corner cases for zeroing a range:
 #
 #	1. into a hole
@@ -305,7 +300,7 @@ _test_generic_punch()
 	$XFS_IO_PROG -f -c "truncate $_20k" \
 		-c "$zero_cmd $_4k $_8k" \
 		-c "$map_cmd -v" $testfile | $filter_cmd
-	[ $? -ne 0 ] && die_now
+	[ $? -ne 0 ] && _fatal
 	_md5_checksum $testfile
 
 	echo "	2. into allocated space"
@@ -316,7 +311,7 @@ _test_generic_punch()
 		-c "pwrite 0 $_20k" $sync_cmd \
 		-c "$zero_cmd $_4k $_8k" \
 		-c "$map_cmd -v" $testfile | $filter_cmd
-	[ $? -ne 0 ] && die_now
+	[ $? -ne 0 ] && _fatal
 	_md5_checksum $testfile
 
 	if [ "$unwritten_tests" ]; then
@@ -328,7 +323,7 @@ _test_generic_punch()
 			-c "$alloc_cmd 0 $_20k" \
 			-c "$zero_cmd $_4k $_8k" \
 			-c "$map_cmd -v" $testfile | $filter_cmd
-		[ $? -ne 0 ] && die_now
+		[ $? -ne 0 ] && _fatal
 		_md5_checksum $testfile
 	fi
 
@@ -340,7 +335,7 @@ _test_generic_punch()
 		-c "pwrite $_8k $_8k" $sync_cmd \
 		-c "$zero_cmd $_4k $_8k" \
 		-c "$map_cmd -v" $testfile | $filter_cmd
-	[ $? -ne 0 ] && die_now
+	[ $? -ne 0 ] && _fatal
 	_md5_checksum $testfile
 
 	if [ "$unwritten_tests" ]; then
@@ -352,7 +347,7 @@ _test_generic_punch()
 			-c "$alloc_cmd $_8k $_8k" \
 			-c "$zero_cmd $_4k $_8k" \
 			-c "$map_cmd -v" $testfile | $filter_cmd
-		[ $? -ne 0 ] && die_now
+		[ $? -ne 0 ] && _fatal
 		_md5_checksum $testfile
 	fi
 
@@ -364,7 +359,7 @@ _test_generic_punch()
 		-c "pwrite 0 $_8k" $sync_cmd \
 		 -c "$zero_cmd $_4k $_8k" \
 		-c "$map_cmd -v" $testfile | $filter_cmd
-	[ $? -ne 0 ] && die_now
+	[ $? -ne 0 ] && _fatal
 	_md5_checksum $testfile
 
 	if [ "$unwritten_tests" ]; then
@@ -377,7 +372,7 @@ _test_generic_punch()
 			-c "$alloc_cmd $_8k $_8k" \
 			-c "$zero_cmd $_4k $_8k" \
 			-c "$map_cmd -v" $testfile | $filter_cmd
-		[ $? -ne 0 ] && die_now
+		[ $? -ne 0 ] && _fatal
 		_md5_checksum $testfile
 
 		echo "	8. unwritten -> hole"
@@ -388,7 +383,7 @@ _test_generic_punch()
 			-c "$alloc_cmd 0 $_8k" \
 			-c "$zero_cmd $_4k $_8k" \
 			-c "$map_cmd -v" $testfile | $filter_cmd
-		[ $? -ne 0 ] && die_now
+		[ $? -ne 0 ] && _fatal
 		_md5_checksum $testfile
 
 		echo "	9. unwritten -> data"
@@ -400,7 +395,7 @@ _test_generic_punch()
 			-c "pwrite $_8k $_8k" $sync_cmd \
 			-c "$zero_cmd $_4k $_8k" \
 			-c "$map_cmd -v" $testfile | $filter_cmd
-		[ $? -ne 0 ] && die_now
+		[ $? -ne 0 ] && _fatal
 		_md5_checksum $testfile
 	fi
 
@@ -412,7 +407,7 @@ _test_generic_punch()
 		-c "pwrite $_8k $_4k" $sync_cmd \
 		-c "$zero_cmd $_4k $_12k" \
 		-c "$map_cmd -v" $testfile | $filter_cmd
-	[ $? -ne 0 ] && die_now
+	[ $? -ne 0 ] && _fatal
 	_md5_checksum $testfile
 
 	echo "	11. data -> hole -> data"
@@ -426,7 +421,7 @@ _test_generic_punch()
 		-c "$punch_cmd $_8k $_4k" \
 		-c "$zero_cmd $_4k $_12k" \
 		-c "$map_cmd -v" $testfile | $filter_cmd
-	[ $? -ne 0 ] && die_now
+	[ $? -ne 0 ] && _fatal
 	_md5_checksum $testfile
 
 	if [ "$unwritten_tests" ]; then
@@ -439,7 +434,7 @@ _test_generic_punch()
 			-c "pwrite $_8k $_4k" $sync_cmd \
 			-c "$zero_cmd $_4k $_12k" \
 			-c "$map_cmd -v" $testfile | $filter_cmd
-		[ $? -ne 0 ] && die_now
+		[ $? -ne 0 ] && _fatal
 		_md5_checksum $testfile
 
 		echo "	13. data -> unwritten -> data"
@@ -452,7 +447,7 @@ _test_generic_punch()
 			-c "pwrite $_12k $_8k" -c "fsync" \
 			-c "$zero_cmd $_4k $_12k" \
 			-c "$map_cmd -v" $testfile | $filter_cmd
-		[ $? -ne 0 ] && die_now
+		[ $? -ne 0 ] && _fatal
 		_md5_checksum $testfile
 	fi
 
@@ -466,7 +461,7 @@ _test_generic_punch()
 			-c "pwrite 0 $_20k" $sync_cmd \
 			-c "$zero_cmd $_12k $_8k" \
 			-c "$map_cmd -v" $testfile | $filter_cmd
-		[ $? -ne 0 ] && die_now
+		[ $? -ne 0 ] && _fatal
 		_md5_checksum $testfile
 	fi
 
@@ -483,7 +478,7 @@ _test_generic_punch()
 		-c "pwrite 0 $_20k" $sync_cmd \
 		-c "$zero_cmd 0 $_8k" \
 		-c "$map_cmd -v" $testfile | $filter_cmd
-	[ $? -ne 0 ] && die_now
+	[ $? -ne 0 ] && _fatal
 	_md5_checksum $testfile
 
 	# If zero_cmd is fcollpase, don't check unaligned offsets
@@ -512,7 +507,7 @@ _test_generic_punch()
 		-c "fadvise -d" \
 		-c "$map_cmd -v" $testfile | $filter_cmd
 	diff $testfile $testfile.2
-	[ $? -ne 0 ] && die_now
+	[ $? -ne 0 ] && _fatal
 	rm -f $testfile.2
 	_md5_checksum $testfile
 
@@ -532,7 +527,7 @@ _test_generic_punch()
 		-c "$zero_cmd 128 128" \
 		-c "$map_cmd -v" $testfile | $filter_cmd | \
 			 sed -e "s/\.\.[0-9]*\]/..7\]/"
-	[ $? -ne 0 ] && die_now
+	[ $? -ne 0 ] && _fatal
 	od -x $testfile | head -n -1
 }
 
common/rc +0 -28
index 9bed6dad..fac9b6da 100644
@@ -1798,28 +1798,6 @@ _do()
     return $ret
 }
 
-# bail out, setting up .notrun file. Need to kill the filesystem check files
-# here, otherwise they are set incorrectly for the next test.
-#
-_notrun()
-{
-    echo "$*" > $seqres.notrun
-    echo "$seq not run: $*"
-    rm -f ${RESULT_DIR}/require_test*
-    rm -f ${RESULT_DIR}/require_scratch*
-
-    _exit 0
-}
-
-# just plain bail out
-#
-_fail()
-{
-    echo "$*" | tee -a $seqres.full
-    echo "(see $seqres.full for details)"
-    _exit 1
-}
-
 #
 # Tests whether $FSTYP should be exclude from this test.
 #
@@ -3835,12 +3813,6 @@ _link_out_file()
 	_link_out_file_named $seqfull.out "$features"
 }
 
-_die()
-{
-        echo $@
-        _exit 1
-}
-
 # convert urandom incompressible data to compressible text data
 _ddt()
 {
Signature · 1 line
2.34.1
#6 lore raw

PATCH v3 2/2 check: Replace exit with _fatal and _exit in check

In reply to Nirjhar Roy (IBM)

Some of the "status=<val>;exit" and "exit <val>" were not replaced with _exit <val> and _fatal. Doing it now.

---
 check | 52 ++++++++++++++++++----------------------------------
 1 file changed, 18 insertions(+), 34 deletions(-)
check +18 -34
index bd84f213..ede54f69 100755
@@ -123,7 +123,7 @@ examples:
  check -X .exclude -g auto
  check -E ~/.xfstests.exclude
 '
-	    exit 1
+	    _fatal
 }
 
 get_sub_group_list()
@@ -232,8 +232,7 @@ _prepare_test_list()
 		for group in $GROUP_LIST; do
 			list=$(get_group_list $group)
 			if [ -z "$list" ]; then
-				echo "Group \"$group\" is empty or not defined?"
-				exit 1
+				_fatal "Group \"$group\" is empty or not defined?"
 			fi
 
 			for t in $list; do
@@ -317,15 +316,13 @@ while [ $# -gt 0 ]; do
 	-n)	showme=true ;;
 	-r)
 		if $exact_order; then
-			echo "Cannot specify -r and --exact-order."
-			exit 1
+			_fatal "Cannot specify -r and --exact-order."
 		fi
 		randomize=true
 		;;
 	--exact-order)
 		if $randomize; then
-			echo "Cannnot specify --exact-order and -r."
-			exit 1
+			_fatal "Cannnot specify --exact-order and -r."
 		fi
 		exact_order=true
 		;;
@@ -362,8 +359,7 @@ done
 # we need common/rc, that also sources common/config. We need to source it
 # after processing args, overlay needs FSTYP set before sourcing common/config
 if ! . ./common/rc; then
-	echo "check: failed to source common/rc"
-	exit 1
+	_fatal "check: failed to source common/rc"
 fi
 
 init_rc
@@ -375,8 +371,7 @@ if [ -n "$SOAK_DURATION" ]; then
 		sed -e 's/^\([.0-9]*\)\([a-z]\)*/\1 \2/g' | \
 		$AWK_PROG -f $here/src/soak_duration.awk)"
 	if [ $? -ne 0 ]; then
-		status=1
-		exit 1
+		_fatal
 	fi
 fi
 
@@ -387,8 +382,7 @@ if [ -n "$FUZZ_REWRITE_DURATION" ]; then
 		sed -e 's/^\([.0-9]*\)\([a-z]\)*/\1 \2/g' | \
 		$AWK_PROG -f $here/src/soak_duration.awk)"
 	if [ $? -ne 0 ]; then
-		status=1
-		exit 1
+		_fatal
 	fi
 fi
 
@@ -405,9 +399,7 @@ fi
 if $have_test_arg; then
 	while [ $# -gt 0 ]; do
 		case "$1" in
-		-*)	echo "Arguments before tests, please!"
-			status=1
-			exit $status
+		-*)	_fatal "Arguments before tests, please!"
 			;;
 		*)	# Expand test pattern (e.g. xfs/???, *fs/001)
 			list=$(cd $SRC_DIR; echo $1)
@@ -439,8 +431,7 @@ fi
 
 if [ `id -u` -ne 0 ]
 then
-    echo "check: QA must be run as root"
-    exit 1
+    _fatal "check: QA must be run as root"
 fi
 
 _wipe_counters()
@@ -722,6 +713,9 @@ _detect_kmemleak
 _prepare_test_list
 fstests_start_time="$(date +"%F %T")"
 
+# We are not using _exit in the trap handler so that it is obvious to the reader
+# that we are using the last set value of "status" before we finally exit
+# from the check script.
 if $OPTIONS_HAVE_SECTIONS; then
 	trap "_summary; exit \$status" 0 1 2 3 15
 else
@@ -768,9 +762,7 @@ function run_section()
 
 	mkdir -p $RESULT_BASE
 	if [ ! -d $RESULT_BASE ]; then
-		echo "failed to create results directory $RESULT_BASE"
-		status=1
-		exit
+		_fatal "failed to create results directory $RESULT_BASE"
 	fi
 
 	if $OPTIONS_HAVE_SECTIONS; then
@@ -785,9 +777,7 @@ function run_section()
 		then
 			echo "our local _test_mkfs routine ..."
 			cat $tmp.err
-			echo "check: failed to mkfs \$TEST_DEV using specified options"
-			status=1
-			exit
+			_fatal "check: failed to mkfs \$TEST_DEV using specified options"
 		fi
 		# Previous FSTYP derived from TEST_DEV could be changed, source
 		# common/rc again with correct FSTYP to get FSTYP specific configs,
@@ -830,9 +820,7 @@ function run_section()
 	  then
 	      echo "our local _scratch_mkfs routine ..."
 	      cat $tmp.err
-	      echo "check: failed to mkfs \$SCRATCH_DEV using specified options"
-	      status=1
-	      exit
+	      _fatal "check: failed to mkfs \$SCRATCH_DEV using specified options"
 	  fi
 
 	  # call the overridden mount - make sure the FS mounts with
@@ -841,9 +829,7 @@ function run_section()
 	  then
 	      echo "our local mount routine ..."
 	      cat $tmp.err
-	      echo "check: failed to mount \$SCRATCH_DEV using specified options"
-	      status=1
-	      exit
+	      _fatal "check: failed to mount \$SCRATCH_DEV using specified options"
 	  else
 	      _scratch_unmount
 	  fi
@@ -1106,12 +1092,10 @@ for ((iters = 0; iters < $iterations; iters++)) do
 		run_section $section
 		if [ "$sum_bad" != 0 ] && [ "$istop" = true ]; then
 			interrupt=false
-			status=`expr $sum_bad != 0`
-			exit
+			_exit `expr $sum_bad != 0`
 		fi
 	done
 done
 
 interrupt=false
-status=`expr $sum_bad != 0`
-exit
+_exit `expr $sum_bad != 0`
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v3 1/2] common: Move exit related functions to a common/exit

In reply to Nirjhar Roy (IBM)
Quoted 26 lines · Nirjhar Roy (IBM)

Introduce a new file common/exit that will contain all the exit related functions. This will remove the dependencies these functions have on other non-related helper files and they can be indepedently sourced. This was suggested by Dave Chinner[1].
While moving the exit related functions, remove _die() and die_now() and replace die_now with _fatal(). It is of no use to keep the unnecessary wrappers.

[1] https://lore.kernel.org/linux-xfs/[email protected]/

---
 check           |  2 ++
 common/config   | 17 -----------------
 common/exit     | 39 +++++++++++++++++++++++++++++++++++++++
 common/preamble |  3 +++
 common/punch    | 39 +++++++++++++++++----------------------
 common/rc       | 28 ----------------------------
 6 files changed, 61 insertions(+), 67 deletions(-)
 create mode 100644 common/exit
check +0 -0
index 9451c350..bd84f213 100755

check export DIFF_LENGTH=${DIFF_LENGTH:=10} · line 50

Quoted 5 lines
check +2 -0
@@ -46,6 +46,8 @@ export DIFF_LENGTH=${DIFF_LENGTH:=10}
 
 # by default don't output timestamps
 timestamp=${TIMESTAMP:=false}
+. common/exit
+. common/test_names

So this gets sourced at the beginning of check script here.

Quoted 3 lines
check +0 -0
 rm -f $tmp.list $tmp.tmp $tmp.grep $here/$iam.out $tmp.report.* $tmp.arglist
 

<...>

common/preamble _register_cleanup() · line 38

Quoted 11 lines
common/preamble +3 -0
index ba029a34..51d03396 100644
@@ -33,6 +33,9 @@ _register_cleanup()
 # explicitly as a member of the 'all' group.
 _begin_fstest()
 {
+	. common/exit
+	. common/test_names
+

Why do we need to source these files here again?
Isn't check script already sourcing both of this in the beginning itself?

-ritesh

Reviewed-by on v3 2/2
#7 lore raw

Re: [PATCH v3 2/2] check: Replace exit with _fatal and _exit in check

In reply to Nirjhar Roy (IBM)
Quoted 3 lines · Nirjhar Roy (IBM)

Some of the "status=<val>;exit" and "exit <val>" were not replaced with _exit <val> and _fatal. Doing it now.

Indeed a nice cleanup. The changes in this patch looks good to me.

Please feel free to add:

So I guess these couple of series was to cleanup exit routines from common bash scripts. Do we plan to update the tests/ as well where we call...
status=X
exit

...or updating tests/ is not needed since we didn't find any wrong usage of "exit X" routines there?

Either ways - I think we might need to update the README at some point in time which carries this snip. You might need to add that there are helper routines like _exit() and _fatal() perhaps for use in common scripts.

<snip>
To force a non-zero exit status use:
status=1
exit

Note that:
exit 1
won't have the desired effect because of the way the exit trap works.

-ritesh

#3 lore raw

Re: [PATCH v3 1/2] common: Move exit related functions to a common/exit

In reply to Ritesh Harjani (IBM)
Quoted 40 lines · Ritesh Harjani
Quoted 26 lines · Nirjhar Roy (IBM)

Introduce a new file common/exit that will contain all the exit related functions. This will remove the dependencies these functions have on other non-related helper files and they can be indepedently sourced. This was suggested by Dave Chinner[1].
While moving the exit related functions, remove _die() and die_now() and replace die_now with _fatal(). It is of no use to keep the unnecessary wrappers.

[1] https://lore.kernel.org/linux-xfs/[email protected]/

---
 check           |  2 ++
 common/config   | 17 -----------------
 common/exit     | 39 +++++++++++++++++++++++++++++++++++++++
 common/preamble |  3 +++
 common/punch    | 39 +++++++++++++++++----------------------
 common/rc       | 28 ----------------------------
 6 files changed, 61 insertions(+), 67 deletions(-)
 create mode 100644 common/exit
check +0 -0
index 9451c350..bd84f213 100755

check export DIFF_LENGTH=${DIFF_LENGTH:=10} · line 50

Quoted 5 lines
check +2 -0
@@ -46,6 +46,8 @@ export DIFF_LENGTH=${DIFF_LENGTH:=10}
 
 # by default don't output timestamps
 timestamp=${TIMESTAMP:=false}
+. common/exit
+. common/test_names

So this gets sourced at the beginning of check script here.

Quoted 3 lines
check +0 -0
 rm -f $tmp.list $tmp.tmp $tmp.grep $here/$iam.out $tmp.report.* $tmp.arglist
 

<...>

Quoted 15 lines

common/preamble _register_cleanup() · line 38

Quoted 11 lines
common/preamble +3 -0
index ba029a34..51d03396 100644
@@ -33,6 +33,9 @@ _register_cleanup()
 # explicitly as a member of the 'all' group.
 _begin_fstest()
 {
+	. common/exit
+	. common/test_names
+

Why do we need to source these files here again?
Isn't check script already sourcing both of this in the beginning itself?

The _begin_fstest is called at the beginning of each test case (e.g. generic/001). And "check" run each test cases likes:

cmd="generic/001"
./$cmd

So the imported things (by "check") can't help sub-case running

Thanks,
Zorro

Quoted 3 lines

-ritesh

#4 lore raw

Re: [PATCH v3 1/2] common: Move exit related functions to a common/exit

Quoted 57 lines · Zorro Lang
Quoted 40 lines · Ritesh Harjani
Quoted 26 lines · Nirjhar Roy (IBM)

Introduce a new file common/exit that will contain all the exit related functions. This will remove the dependencies these functions have on other non-related helper files and they can be indepedently sourced. This was suggested by Dave Chinner[1].
While moving the exit related functions, remove _die() and die_now() and replace die_now with _fatal(). It is of no use to keep the unnecessary wrappers.

[1] https://lore.kernel.org/linux-xfs/[email protected]/

---
 check           |  2 ++
 common/config   | 17 -----------------
 common/exit     | 39 +++++++++++++++++++++++++++++++++++++++
 common/preamble |  3 +++
 common/punch    | 39 +++++++++++++++++----------------------
 common/rc       | 28 ----------------------------
 6 files changed, 61 insertions(+), 67 deletions(-)
 create mode 100644 common/exit
check +0 -0
index 9451c350..bd84f213 100755

check export DIFF_LENGTH=${DIFF_LENGTH:=10} · line 50

Quoted 5 lines
check +2 -0
@@ -46,6 +46,8 @@ export DIFF_LENGTH=${DIFF_LENGTH:=10}
 
 # by default don't output timestamps
 timestamp=${TIMESTAMP:=false}
+. common/exit
+. common/test_names

So this gets sourced at the beginning of check script here.

Quoted 3 lines
check +0 -0
 rm -f $tmp.list $tmp.tmp $tmp.grep $here/$iam.out $tmp.report.* $tmp.arglist
 

<...>

Quoted 15 lines

common/preamble _register_cleanup() · line 38

Quoted 11 lines
common/preamble +3 -0
index ba029a34..51d03396 100644
@@ -33,6 +33,9 @@ _register_cleanup()
 # explicitly as a member of the 'all' group.
 _begin_fstest()
 {
+	. common/exit
+	. common/test_names
+

Why do we need to source these files here again?
Isn't check script already sourcing both of this in the beginning itself?

Quoted 7 lines

The _begin_fstest is called at the beginning of each test case (e.g. generic/001). And "check" run each test cases likes:

cmd="generic/001"
./$cmd

So the imported things (by "check") can't help sub-case running

aah right. Each testcase is inoked by "exec ./$seq" and it won't have the function definitions sourced from the previous shell process. So we will need to source the necessary files again within the test execution.

-ritesh

#8 lore raw

Re: [PATCH v3 2/2] check: Replace exit with _fatal and _exit in check

In reply to Ritesh Harjani (IBM)
Quoted 9 lines · Ritesh Harjani (IBM)
Quoted 3 lines · Nirjhar Roy (IBM)

Some of the "status=<val>;exit" and "exit <val>" were not replaced with _exit <val> and _fatal. Doing it now.

Indeed a nice cleanup. The changes in this patch looks good to me.

Please feel free to add:

Thank you.

Quoted 10 lines

So I guess these couple of series was to cleanup exit routines from common bash scripts. Do we plan to update the tests/ as well where we call...
status=X
exit

...or updating tests/ is not needed since we didn't find any wrong usage of "exit X" routines there?

Thank you for pointing this out. The exit command is used in 2 ways in the tests:

1. "exit 1"

2. "status=0; exit"

1) works because we set the value of "status" to 1 (failure by default) in _begin_fstest() - so even if "exit 1" is not correctly explicitly setting the value of "status", it simply works. However, "exit <any value != 1>" will not work (although I didn't find any place in tests where exit has been used with any other value apart from 0 and 1).

2) This works since we are setting the value of "status" correcting before "exit"ing.

But yes, we should ideally replace direct usage of exit with either _exit or _fatal (depending on the exit value). I will add this to my ToDo list and send a separate patch series with this and the README change you have suggested below.

Quoted 12 lines

Either ways - I think we might need to update the README at some point in time which carries this snip. You might need to add that there are helper routines like _exit() and _fatal() perhaps for use in common scripts.

<snip>
To force a non-zero exit status use:
status=1
exit

Quoted 4 lines

Note that:
exit 1
won't have the desired effect because of the way the exit trap works.

I agree. I will send a separate patch with this and the exit call replacement of the tests.

--NR

Quoted 3 lines

-ritesh

Signature · 3 lines
Nirjhar Roy
Linux Kernel Developer
IBM, Bangalore
#5 lore raw

Re: [PATCH v3 1/2] common: Move exit related functions to a common/exit

In reply to Ritesh Harjani (IBM)
Quoted 62 lines · Ritesh Harjani (IBM)
Quoted 60 lines · Zorro Lang
Quoted 40 lines · Ritesh Harjani
Quoted 26 lines · Nirjhar Roy (IBM)

Introduce a new file common/exit that will contain all the exit related functions. This will remove the dependencies these functions have on other non-related helper files and they can be indepedently sourced. This was suggested by Dave Chinner[1].
While moving the exit related functions, remove _die() and die_now() and replace die_now with _fatal(). It is of no use to keep the unnecessary wrappers.

[1] https://lore.kernel.org/linux-xfs/[email protected]/

---
 check           |  2 ++
 common/config   | 17 -----------------
 common/exit     | 39 +++++++++++++++++++++++++++++++++++++++
 common/preamble |  3 +++
 common/punch    | 39 +++++++++++++++++----------------------
 common/rc       | 28 ----------------------------
 6 files changed, 61 insertions(+), 67 deletions(-)
 create mode 100644 common/exit
check +0 -0
index 9451c350..bd84f213 100755

check export DIFF_LENGTH=${DIFF_LENGTH:=10} · line 50

Quoted 5 lines
check +2 -0
@@ -46,6 +46,8 @@ export DIFF_LENGTH=${DIFF_LENGTH:=10}
 
 # by default don't output timestamps
 timestamp=${TIMESTAMP:=false}
+. common/exit
+. common/test_names

So this gets sourced at the beginning of check script here.

Quoted 3 lines
check +0 -0
 rm -f $tmp.list $tmp.tmp $tmp.grep $here/$iam.out $tmp.report.* $tmp.arglist
 

<...>

Quoted 15 lines

common/preamble _register_cleanup() · line 38

Quoted 11 lines
common/preamble +3 -0
index ba029a34..51d03396 100644
@@ -33,6 +33,9 @@ _register_cleanup()
 # explicitly as a member of the 'all' group.
 _begin_fstest()
 {
+	. common/exit
+	. common/test_names
+

Why do we need to source these files here again?
Isn't check script already sourcing both of this in the beginning itself?

The _begin_fstest is called at the beginning of each test case (e.g. generic/001). And "check" run each test cases likes:

Quoted 6 lines
Quoted 4 lines

cmd="generic/001"
./$cmd

So the imported things (by "check") can't help sub-case running

aah right. Each testcase is inoked by "exec ./$seq" and it won't have

Ok. To be accurate, it is...

bash -c "<...>; exec ./$seq"

& not just

exec ./$seq

-ritesh

#9 lore raw
In reply to Nirjhar Roy (IBM)
Quoted 5 lines · Nirjhar Roy (IBM)

This patch series moves all the exit related functions to a separate file - common/exit. This will remove the dependency to source non-related files to use these exit related functions. Thanks to Dave for suggesting this[1]. The second patch replaces exit with _exit in check file - I missed replacing them in [2].

Quoted 16 lines

[v2] -> v3
Addressed Dave's feedbacks.
In patch [1/2]
- Removed _die() and die_now() from common/exit
- Replaced die_now() with _fatal in common/punch
- Removed sourcing of common/exit and common/test_names from common/config and moved them to the beginning of check.
- Added sourcing of common/test_names in _begin_fstest() since common/config is no more sourcing common/test_names.
- Added a blank line in _begin_fstest() after sourcing common/{exit,test_names}
In patch [2/2]
- Replaced "_exit 1" with _fatal and "echo <error message>; _exit 1" with _fatal <error message>.
- Reverted to "exit \$status" in the trap handler registration in check - just to make it more obvious to the reader that we are capturing $status as the final exit value.

Hi Dave and Zorro,

Any further feedback in this version?

--NR

Quoted 23 lines

[v1] https://lore.kernel.org/all/[email protected]/
[v2] https://lore.kernel.org/all/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/48dacdf636be19ae8bff66cc3852d27e28030613.1744181682.git.nirjhar.roy.lists@gmail.com/

Nirjhar Roy (IBM) (2):
common: Move exit related functions to a common/exit
check: Replace exit with _fatal and _exit in check

  check           | 54 ++++++++++++++++++-------------------------------
  common/config   | 17 ----------------
  common/exit     | 39 +++++++++++++++++++++++++++++++++++
  common/preamble |  3 +++
  common/punch    | 39 ++++++++++++++++-------------------
  common/rc       | 28 -------------------------
  6 files changed, 79 insertions(+), 101 deletions(-)

create mode 100644 common/exit

--
2.34.1

Signature · 3 lines
Nirjhar Roy
Linux Kernel Developer
IBM, Bangalore
Reviewed-by
#10 lore raw
Quoted 24 lines · Nirjhar Roy (IBM)
Quoted 5 lines · Nirjhar Roy (IBM)

This patch series moves all the exit related functions to a separate file - common/exit. This will remove the dependency to source non-related files to use these exit related functions. Thanks to Dave for suggesting this[1]. The second patch replaces exit with _exit in check file - I missed replacing them in [2].

Quoted 16 lines

[v2] -> v3
Addressed Dave's feedbacks.
In patch [1/2]
- Removed _die() and die_now() from common/exit
- Replaced die_now() with _fatal in common/punch
- Removed sourcing of common/exit and common/test_names from common/config and moved them to the beginning of check.
- Added sourcing of common/test_names in _begin_fstest() since common/config is no more sourcing common/test_names.
- Added a blank line in _begin_fstest() after sourcing common/{exit,test_names}
In patch [2/2]
- Replaced "_exit 1" with _fatal and "echo <error message>; _exit 1" with _fatal <error message>.
- Reverted to "exit \$status" in the trap handler registration in check - just to make it more obvious to the reader that we are capturing $status as the final exit value.

Quoted 3 lines

Hi Dave and Zorro,

Any further feedback in this version?

This version is good to me, if there's not more review points from others, I'll merge it.

Quoted 32 lines

--NR

Quoted 23 lines

[v1] https://lore.kernel.org/all/[email protected]/
[v2] https://lore.kernel.org/all/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/48dacdf636be19ae8bff66cc3852d27e28030613.1744181682.git.nirjhar.roy.lists@gmail.com/

Nirjhar Roy (IBM) (2):
common: Move exit related functions to a common/exit
check: Replace exit with _fatal and _exit in check

  check           | 54 ++++++++++++++++++-------------------------------
  common/config   | 17 ----------------
  common/exit     | 39 +++++++++++++++++++++++++++++++++++
  common/preamble |  3 +++
  common/punch    | 39 ++++++++++++++++-------------------
  common/rc       | 28 -------------------------
  6 files changed, 79 insertions(+), 101 deletions(-)

create mode 100644 common/exit

--
2.34.1

Signature · 3 lines
Nirjhar Roy
Linux Kernel Developer
IBM, Bangalore