[PATCH net-next 02/12] selftests/vsock: make wait_for_listener() work even if pipefail is on

Bobby Eshleman posted 12 patches 3 months, 2 weeks ago
There is a newer version of this series
[PATCH net-next 02/12] selftests/vsock: make wait_for_listener() work even if pipefail is on
Posted by Bobby Eshleman 3 months, 2 weeks ago
From: Bobby Eshleman <bobbyeshleman@meta.com>

Save/restore pipefail to not mistakenly trip the if-condition
in wait_for_listener().

awk doesn't gracefully handle SIGPIPE with a non-zero exit code, so grep
exiting upon finding a match causes false-positives when the pipefail
option is used. This will enable pipefail usage, so that we can losing
failures when piping test output into log() functions.

Fixes: a4a65c6fe08b ("selftests/vsock: add initial vmtest.sh for vsock")
Signed-off-by: Bobby Eshleman <bobbyeshleman@meta.com>
---
 tools/testing/selftests/vsock/vmtest.sh | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selftests/vsock/vmtest.sh
index 561600814bef..ec3ff443f49a 100755
--- a/tools/testing/selftests/vsock/vmtest.sh
+++ b/tools/testing/selftests/vsock/vmtest.sh
@@ -243,6 +243,7 @@ wait_for_listener()
 	local port=$1
 	local interval=$2
 	local max_intervals=$3
+	local old_pipefail
 	local protocol=tcp
 	local pattern
 	local i
@@ -251,6 +252,13 @@ wait_for_listener()
 
 	# for tcp protocol additionally check the socket state
 	[ "${protocol}" = "tcp" ] && pattern="${pattern}0A"
+
+	# 'grep -q' exits on match, sending SIGPIPE to 'awk', which exits with
+	# an error, causing the if-condition to fail when pipefail is set.
+	# Instead, temporarily disable pipefail and restore it later.
+	old_pipefail=$(set -o | awk '/^pipefail[[:space:]]+(on|off)$/{print $2}')
+	set +o pipefail
+
 	for i in $(seq "${max_intervals}"); do
 		if awk '{print $2" "$4}' /proc/net/"${protocol}"* | \
 		   grep -q "${pattern}"; then
@@ -258,6 +266,10 @@ wait_for_listener()
 		fi
 		sleep "${interval}"
 	done
+
+	if [[ "${old_pipefail}" == on ]]; then
+		set -o pipefail
+	fi
 }
 
 vm_wait_for_listener() {

-- 
2.47.3
Re: [PATCH net-next 02/12] selftests/vsock: make wait_for_listener() work even if pipefail is on
Posted by Simon Horman 3 months, 2 weeks ago
On Wed, Oct 22, 2025 at 06:00:06PM -0700, Bobby Eshleman wrote:
> From: Bobby Eshleman <bobbyeshleman@meta.com>
> 
> Save/restore pipefail to not mistakenly trip the if-condition
> in wait_for_listener().
> 
> awk doesn't gracefully handle SIGPIPE with a non-zero exit code, so grep
> exiting upon finding a match causes false-positives when the pipefail
> option is used. This will enable pipefail usage, so that we can losing
> failures when piping test output into log() functions.
> 
> Fixes: a4a65c6fe08b ("selftests/vsock: add initial vmtest.sh for vsock")
> Signed-off-by: Bobby Eshleman <bobbyeshleman@meta.com>
> ---
>  tools/testing/selftests/vsock/vmtest.sh | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selftests/vsock/vmtest.sh
> index 561600814bef..ec3ff443f49a 100755
> --- a/tools/testing/selftests/vsock/vmtest.sh
> +++ b/tools/testing/selftests/vsock/vmtest.sh
> @@ -243,6 +243,7 @@ wait_for_listener()
>  	local port=$1
>  	local interval=$2
>  	local max_intervals=$3
> +	local old_pipefail
>  	local protocol=tcp
>  	local pattern
>  	local i
> @@ -251,6 +252,13 @@ wait_for_listener()
>  
>  	# for tcp protocol additionally check the socket state
>  	[ "${protocol}" = "tcp" ] && pattern="${pattern}0A"
> +
> +	# 'grep -q' exits on match, sending SIGPIPE to 'awk', which exits with
> +	# an error, causing the if-condition to fail when pipefail is set.
> +	# Instead, temporarily disable pipefail and restore it later.
> +	old_pipefail=$(set -o | awk '/^pipefail[[:space:]]+(on|off)$/{print $2}')
> +	set +o pipefail
> +
>  	for i in $(seq "${max_intervals}"); do
>  		if awk '{print $2" "$4}' /proc/net/"${protocol}"* | \
>  		   grep -q "${pattern}"; then

Hi Bobby,

I agree this is a problem. But I'm wondering if you considered
moving the pattern matching into the awk script. I'm no awk expert.
But suspect that would lead to a more elegant solution.

> @@ -258,6 +266,10 @@ wait_for_listener()
>  		fi
>  		sleep "${interval}"
>  	done
> +
> +	if [[ "${old_pipefail}" == on ]]; then
> +		set -o pipefail
> +	fi
>  }
>  
>  vm_wait_for_listener() {
> 
> -- 
> 2.47.3
> 
>
Re: [PATCH net-next 02/12] selftests/vsock: make wait_for_listener() work even if pipefail is on
Posted by Bobby Eshleman 3 months, 2 weeks ago
On Mon, Oct 27, 2025 at 04:48:28PM +0000, Simon Horman wrote:
> On Wed, Oct 22, 2025 at 06:00:06PM -0700, Bobby Eshleman wrote:
> > From: Bobby Eshleman <bobbyeshleman@meta.com>
> > 
> > Save/restore pipefail to not mistakenly trip the if-condition
> > in wait_for_listener().
> > 
> > awk doesn't gracefully handle SIGPIPE with a non-zero exit code, so grep
> > exiting upon finding a match causes false-positives when the pipefail
> > option is used. This will enable pipefail usage, so that we can losing
> > failures when piping test output into log() functions.
> > 
> > Fixes: a4a65c6fe08b ("selftests/vsock: add initial vmtest.sh for vsock")
> > Signed-off-by: Bobby Eshleman <bobbyeshleman@meta.com>
> > ---
> >  tools/testing/selftests/vsock/vmtest.sh | 12 ++++++++++++
> >  1 file changed, 12 insertions(+)
> > 
> > diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selftests/vsock/vmtest.sh
> > index 561600814bef..ec3ff443f49a 100755
> > --- a/tools/testing/selftests/vsock/vmtest.sh
> > +++ b/tools/testing/selftests/vsock/vmtest.sh
> > @@ -243,6 +243,7 @@ wait_for_listener()
> >  	local port=$1
> >  	local interval=$2
> >  	local max_intervals=$3
> > +	local old_pipefail
> >  	local protocol=tcp
> >  	local pattern
> >  	local i
> > @@ -251,6 +252,13 @@ wait_for_listener()
> >  
> >  	# for tcp protocol additionally check the socket state
> >  	[ "${protocol}" = "tcp" ] && pattern="${pattern}0A"
> > +
> > +	# 'grep -q' exits on match, sending SIGPIPE to 'awk', which exits with
> > +	# an error, causing the if-condition to fail when pipefail is set.
> > +	# Instead, temporarily disable pipefail and restore it later.
> > +	old_pipefail=$(set -o | awk '/^pipefail[[:space:]]+(on|off)$/{print $2}')
> > +	set +o pipefail
> > +
> >  	for i in $(seq "${max_intervals}"); do
> >  		if awk '{print $2" "$4}' /proc/net/"${protocol}"* | \
> >  		   grep -q "${pattern}"; then
> 
> Hi Bobby,
> 
> I agree this is a problem. But I'm wondering if you considered
> moving the pattern matching into the awk script. I'm no awk expert.
> But suspect that would lead to a more elegant solution.
> 

I bet you are right.

Playing around with awk, I find that this seems to work:

$ pattern=":$(printf '%04X' ${port}) 0A"
$ awk -v pattern="${pattern}" 'BEGIN {rc=1} $2" "$4 ~ pattern {rc=0}
	END {exit rc}' /proc/net/tcp && echo FOUND

I think it beats doing the save/restore on pipefail?

Best,
Bobby
Re: [PATCH net-next 02/12] selftests/vsock: make wait_for_listener() work even if pipefail is on
Posted by Simon Horman 3 months, 1 week ago
On Mon, Oct 27, 2025 at 10:55:52AM -0700, Bobby Eshleman wrote:
> On Mon, Oct 27, 2025 at 04:48:28PM +0000, Simon Horman wrote:
> > On Wed, Oct 22, 2025 at 06:00:06PM -0700, Bobby Eshleman wrote:
> > > From: Bobby Eshleman <bobbyeshleman@meta.com>
> > > 
> > > Save/restore pipefail to not mistakenly trip the if-condition
> > > in wait_for_listener().
> > > 
> > > awk doesn't gracefully handle SIGPIPE with a non-zero exit code, so grep
> > > exiting upon finding a match causes false-positives when the pipefail
> > > option is used. This will enable pipefail usage, so that we can losing
> > > failures when piping test output into log() functions.
> > > 
> > > Fixes: a4a65c6fe08b ("selftests/vsock: add initial vmtest.sh for vsock")
> > > Signed-off-by: Bobby Eshleman <bobbyeshleman@meta.com>
> > > ---
> > >  tools/testing/selftests/vsock/vmtest.sh | 12 ++++++++++++
> > >  1 file changed, 12 insertions(+)
> > > 
> > > diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selftests/vsock/vmtest.sh
> > > index 561600814bef..ec3ff443f49a 100755
> > > --- a/tools/testing/selftests/vsock/vmtest.sh
> > > +++ b/tools/testing/selftests/vsock/vmtest.sh
> > > @@ -243,6 +243,7 @@ wait_for_listener()
> > >  	local port=$1
> > >  	local interval=$2
> > >  	local max_intervals=$3
> > > +	local old_pipefail
> > >  	local protocol=tcp
> > >  	local pattern
> > >  	local i
> > > @@ -251,6 +252,13 @@ wait_for_listener()
> > >  
> > >  	# for tcp protocol additionally check the socket state
> > >  	[ "${protocol}" = "tcp" ] && pattern="${pattern}0A"
> > > +
> > > +	# 'grep -q' exits on match, sending SIGPIPE to 'awk', which exits with
> > > +	# an error, causing the if-condition to fail when pipefail is set.
> > > +	# Instead, temporarily disable pipefail and restore it later.
> > > +	old_pipefail=$(set -o | awk '/^pipefail[[:space:]]+(on|off)$/{print $2}')
> > > +	set +o pipefail
> > > +
> > >  	for i in $(seq "${max_intervals}"); do
> > >  		if awk '{print $2" "$4}' /proc/net/"${protocol}"* | \
> > >  		   grep -q "${pattern}"; then
> > 
> > Hi Bobby,
> > 
> > I agree this is a problem. But I'm wondering if you considered
> > moving the pattern matching into the awk script. I'm no awk expert.
> > But suspect that would lead to a more elegant solution.
> > 
> 
> I bet you are right.
> 
> Playing around with awk, I find that this seems to work:
> 
> $ pattern=":$(printf '%04X' ${port}) 0A"
> $ awk -v pattern="${pattern}" 'BEGIN {rc=1} $2" "$4 ~ pattern {rc=0}
> 	END {exit rc}' /proc/net/tcp && echo FOUND
> 
> I think it beats doing the save/restore on pipefail?

Yes, I think so.