Commit eedb793c authored by Michal Berger's avatar Michal Berger Committed by Tomasz Zawadzki
Browse files

test/iscsi_tgt: Simplify waitfortcp()



Remove extra util checks and stick to procfs. In practice, this test
is bound to linux under our CI so there's no point in having an
explicit checks for it (e.g. the ip is not available under freebsd
and its netstat's output differs from what scripts expects to find,
i.e., it looks for linux's flavor).

Change-Id: I749f73b1705f8e4f9af3d02788a0effd0270a6e4
Signed-off-by: default avatarMichal Berger <michal.berger@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/19560


Reviewed-by: default avatarJim Harris <jim.harris@gmail.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
parent 4641aa53
Loading
Loading
Loading
Loading
+20 −40
Original line number Diff line number Diff line
@@ -8,61 +8,41 @@ rootdir=$(readlink -f $testdir/../../..)
source $rootdir/test/common/autotest_common.sh
source $rootdir/test/iscsi_tgt/common.sh

function waitfortcp() {
	local addr="$2"
tcp_sock() {
	local ap=$1 pid=$2 addr port proc
	addr=${ap%:*} port=${ap#*:}

	if hash ip &> /dev/null; then
		local have_ip_cmd=true
	else
		local have_ip_cmd=false
	fi
	IFS="." read -ra addr <<< "$addr"
	printf -v addr '%02X%02X%02X%02X:%04X' \
		"${addr[3]}" "${addr[2]}" "${addr[1]}" "${addr[0]}" \
		"$port"

	if hash ss &> /dev/null; then
		local have_ss_cmd=true
	else
		local have_ss_cmd=false
	fi
	[[ -e /proc/$pid/net/tcp ]] && proc=/proc/$pid/net/tcp || proc=/proc/net/tcp
	[[ $(< "$proc") == *"$addr 00000000:0000 0A"* ]] # TCP_LISTEN == 0x0A
}

waitfortcp() {
	local addr="$2"

	echo "Waiting for process to start up and listen on address $addr..."
	# turn off trace for this loop
	xtrace_disable
	local ret=0
	local i
	for ((i = 40; i != 0; i--)); do
	local i=40
	while ((i--)); do
		# if the process is no longer running, then exit the script
		#  since it means the application crashed
		if ! kill -s 0 $1; then
			echo "ERROR: process (pid: $1) is no longer running"
			ret=1
			break
			return 1
		fi

		if $have_ip_cmd; then
			namespace=$(ip netns identify $1)
			if [ -n "$namespace" ]; then
				ns_cmd="ip netns exec $namespace"
			fi
		fi

		if $have_ss_cmd; then
			if $ns_cmd ss -ln | grep -E -q "\s+$addr\s+"; then
				break
			fi
		elif [[ "$(uname -s)" == "Linux" ]]; then
			# For Linux, if system doesn't have ss, just assume it has netstat
			if $ns_cmd netstat -an | grep -iw LISTENING | grep -E -q "\s+$addr\$"; then
				break
			fi
		fi
		tcp_sock "$addr" "$1" && return 0
		sleep 0.5
	done

	xtrace_restore
	if ((i == 0)); then

	echo "ERROR: timeout while waiting for process (pid: $1) to start listening on '$addr'"
		ret=1
	fi
	return $ret
	return 1
}

iscsitestinit