[PATCH 1/3] netconsole: terminate netcat background process on exit

Ahmad Fatoum a.fatoum at barebox.org
Sun May 3 01:30:04 PDT 2026


We ship no ncb utility and Debian doesn't package it, so the fallback
path with netcat is what everyone is going to hit.

Drop the ncb branches and make sure to actually kill the netcat process
and not only the subshell that spawned it.

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 scripts/netconsole | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/scripts/netconsole b/scripts/netconsole
index c8109bb09569..7ab81a3a9191 100755
--- a/scripts/netconsole
+++ b/scripts/netconsole
@@ -34,26 +34,27 @@ for nc in netcat nc ; do
 	type ${nc} >/dev/null 2>&1 && break
 done
 
-trap "stty icanon echo intr ^C" 0 2 3 5 10 13 15
+cleanup() {
+	if [ -n "${listener_pid}" ] ; then
+		# The listener subshell is its own process group leader (see set -m
+		# below); kill the whole group so the netcat child dies with it.
+		kill -- -${listener_pid} 2>/dev/null
+		wait ${listener_pid} 2>/dev/null
+	fi
+	stty icanon echo intr ^C
+}
+trap cleanup EXIT INT QUIT TERM HUP
+
 echo "NOTE: the interrupt signal (normally ^C) has been remapped to ^T"
 
 stty -icanon -echo intr ^T
+set -m
 (
-if type ncb 2>/dev/null ; then
-	# see if ncb is in $PATH
-	exec ncb ${port}
-
-elif [ -x ${0%/*}/ncb ] ; then
-	# maybe it's in the same dir as the netconsole script
-	exec ${0%/*}/ncb ${port}
-
-else
-	# blah, just use regular netcat
 	while ${nc} -u -l -p ${port} < /dev/null ; do
 		:
 	done
-fi
 ) &
-pid=$!
+listener_pid=$!
+set +m
+
 ${nc} -u ${ip} ${port}
-kill ${pid} 2>/dev/null
-- 
2.47.3




More information about the barebox mailing list