From 309cc9ecf51b082d6de8f1c3b0028c398daa395f Mon Sep 17 00:00:00 2001 From: Dave Brolley Date: Tue, 9 Dec 2008 15:31:00 -0500 Subject: [PATCH] Use netcat or nc, whichever is available. --- diff --git a/stap-client b/stap-client index 0ac9bd6..255551b 100755 --- a/stap-client +++ b/stap-client @@ -29,6 +29,11 @@ function configuration { tmpdir_prefix_client=stap.client tmpdir_prefix_server=stap.server avahi_service_tag=_stap._tcp + + # We need either netcat or nc. + netcat=`which netcat 2>/dev/null` + test "X$netcat" = "X" && netcat=`which nc 2>/dev/null` + test "X$netcat" = "X" && fatal "ERROR: cannot find required program 'netcat' or 'nc' on PATH" } # function: initialization @@ -386,7 +391,7 @@ function send_request { # Send the request file. for ((attempt=0; $attempt < 10; ++attempt)) do - if nc -w10 $server $(($port+1)) < $tar_client > /dev/null 2>&1; then + if $netcat -w10 $server $(($port+1)) < $tar_client > /dev/null 2>&1; then return; fi sleep 1 @@ -405,7 +410,7 @@ function receive_response { # Retrieve the file. Wait for up to 5 minutes for a response. for ((attempt=0; $attempt < 300; ++attempt)) do - if nc -d $server $(($port+1)) > $tar_server 2>/dev/null; then + if $netcat -d $server $(($port+1)) > $tar_server 2>/dev/null; then return; fi sleep 1 @@ -535,7 +540,7 @@ function choose_server { function connect_to_server { for ((attempt=0; $attempt < 10; ++attempt)) do - if echo "request:" | nc -w10 $1 $2 >/dev/null 2>&1; then + if echo "request:" | $netcat -w10 $1 $2 >/dev/null 2>&1; then return 0 fi sleep 1 diff --git a/stap-server b/stap-server index 67573de..a06adc9 100755 --- a/stap-server +++ b/stap-server @@ -26,6 +26,11 @@ function configuration { tmpdir_prefix_server=stap.server port=$1 test "X$port" = "X" && port=65001 + + # We need either netcat or nc. + netcat=`which netcat 2>/dev/null` + test "X$netcat" = "X" && netcat=`which nc 2>/dev/null` + test "X$netcat" = "X" && fatal "ERROR: cannot find required program 'netcat' or 'nc' on PATH" } # function: initialization @@ -53,12 +58,12 @@ function receive_request { fatal "ERROR: cannot create temporary tar file " $tar_client # Receive the file. - nc -ld $port > $tar_client 2>/dev/null & + $netcat -ld $port > $tar_client 2>/dev/null & # Wait for 10 seconds before timing out for ((t=0; $t < 10; ++t)) do - if jobs '%nc -l' >/dev/null 2>&1; then + if jobs '%$netcat -l' >/dev/null 2>&1; then sleep 1 else return @@ -384,12 +389,12 @@ function package_response { # Wait for the client to take the response file. function send_response { # Now send it. - nc -l $port < $tar_server > /dev/null 2>&1 & + $netcat -l $port < $tar_server > /dev/null 2>&1 & # Wait for 10 seconds before timing out for ((t=0; $t < 10; ++t)) do - if jobs '%nc -l' >/dev/null 2>&1; then + if jobs '%$netcat -l' >/dev/null 2>&1; then sleep 1 else return @@ -430,8 +435,8 @@ function cleanup { rm -fr $tmpdir_stap fi - # Kill any nc job that may be running - kill -s SIGTERM %nc 2> /dev/null + # Kill any $netcat job that may be running + kill -s SIGTERM '%$netcat' 2> /dev/null } # function: terminate diff --git a/stap-serverd b/stap-serverd index 45aacf6..b46a425 100755 --- a/stap-serverd +++ b/stap-serverd @@ -23,13 +23,21 @@ trap 'terminate' SIGTERM SIGINT function initialization { # Default settings. avahi_type=_stap._tcp + + # We need either netcat or nc. + netcat=`which netcat 2>/dev/null` + test "X$netcat" = "X" && netcat=`which nc 2>/dev/null` + test "X$netcat" = "X" && fatal "ERROR: cannot find required program 'netcat' or 'nc' on PATH" + + # See if the given port, or the default port is busy. If so, select another. port=$1 test "X$port" = "X" && port=65000 - export port2=$(($port + 1)) - if netstat -atn | awk '{print $4}' | cut -f2 -d: | egrep -q "^($port|$port2)\$"; then + port2=$(($port + 1)) + while netstat -atn | awk '{print $4}' | cut -f2 -d: | egrep -q "^($port|$port2)\$"; do # Whoops, the port is busy; try another one. - initialization $((1024+($port + $RANDOM)%64000)) - fi + port=$((1024+($port + $RANDOM)%64000)) + port2=$(($port + 1)) + done } # function: advertise_presence @@ -57,8 +65,8 @@ function listen { do for ((attempt=0; $attempt < 5; ++attempt)) do - nc -ld $port 2>/dev/null | process_request & - wait '%nc -l' + $netcat -ld $port 2>/dev/null | process_request & + wait '%$netcat -l' rc=$? if test $rc = 0 -o $rc = 127; then break; # port was read ok @@ -111,11 +119,11 @@ function terminate { # Kill any running 'stap-server' job. kill -s SIGTERM "%stap-server" 2> /dev/null - wait "%stap-server" >/dev/null 2>&1 + wait '%stap-server' >/dev/null 2>&1 - # Kill any running 'nc -l' job. - kill -s SIGTERM "%?nc -l" 2> /dev/null - wait "%?nc - l" >/dev/null 2>&1 + # Kill any running '$netcat -l' job. + kill -s SIGTERM '%$netcat -l' 2>/dev/null + wait '%$netcat -l' >/dev/null 2>&1 exit } -- 1.6.0.5