diff options
Diffstat (limited to 'stap-find-servers')
-rwxr-xr-x | stap-find-servers | 65 |
1 files changed, 45 insertions, 20 deletions
diff --git a/stap-find-servers b/stap-find-servers index 3038c54e..e0838708 100755 --- a/stap-find-servers +++ b/stap-find-servers @@ -2,7 +2,7 @@ # Find compile servers for systemtap # -# Copyright (C) 2008 Red Hat Inc. +# Copyright (C) 2008, 2009 Red Hat Inc. # # This file is part of systemtap, and is free software. You can # redistribute it and/or modify it under the terms of the GNU General @@ -12,14 +12,12 @@ # This script uses avahi to find systemtap compile servers on the local # network. Information about each server found is printed to stdout. +# Initialize the environment +. stap-env + #----------------------------------------------------------------------------- # Helper functions. #----------------------------------------------------------------------------- -# function: configuration -function configuration { - avahi_service_tag=_stap._tcp -} - # function: initialization function initialization { rc=1 # not found yet @@ -28,15 +26,35 @@ function initialization { else find_all=0 fi + timeout=10 # seconds } -# function: find_and_connect_to_server +# function: find_servers # -# Find and establish connection with a compatibale stap server. +# Find and establish connection with a compatible stap server. function find_servers { - # Find a server - avahi-browse $avahi_service_tag --terminate -r 2>/dev/null | match_server - rc=$? + # Create a temp file for the list of servers. We do this instead + # of using a pipe so that we can kill avahi-browse if it + # takes too long. + tmpfile=`mktemp -t stap-serversXXXXXX` || \ + fatal "Cannot create temporary file " $tmpfile + + # Find servers + avahi-browse $stap_avahi_service_tag --terminate -r 2>/dev/null > $tmpfile & + + for ((attempt=0; $attempt < $timeout; ++attempt)) + do + if ! jobs '%avahi-browse' >/dev/null 2>&1; then + break + fi + sleep 1 + done + + # Kill avahi-browse, if it's still running + test $attempt = $timeout && kill -s SIGTERM '%avahi-browse' 2>/dev/null + + match_server < $tmpfile + rm -fr $tmpfile } # function: match_server @@ -47,10 +65,11 @@ function match_server { local server_name local server_sysinfo local server_port - local rc=1 # not found yet + + rc=1 # not found yet # Loop over the avahi service descriptors. - read || exit $rc + read -t $timeout || return while test "X$REPLY" != "X" do server_name= @@ -61,14 +80,14 @@ function match_server { # Examine the next service descriptor # Is it a stap server? if ! echo $REPLY | grep -q "=.* .* IPv4 .*_stap"; then - read || exit $rc + read -t $timeout || return continue fi REPLY= # Get the details of the service local service_tag equal service_data - while read service_tag equal service_data + while read -t $timeout service_tag equal service_data do case $service_tag in hostname ) @@ -76,7 +95,7 @@ function match_server { ;; address ) # Sometimes (seems random), avahi-resolve-host-name resolves a local server to its - # hardware address rather its ip address. Keep trying until we get + # hardware address rather than its ip address. Keep trying until we get # an ip address. server_ip=`expr "$service_data" : '\[\([^]]*\)\]'` local attempt @@ -104,7 +123,7 @@ function match_server { esac done - # It is a stap server, but is it compatible? + # It's an stap server, but is it compatible? if test $find_all = 0 -a "$server_sysinfo" != "`client_sysinfo`"; then continue fi @@ -113,8 +132,6 @@ function match_server { echo "$server_name $server_ip $server_port '$server_sysinfo'" rc=0 done - - exit $rc } # function client_sysinfo @@ -128,10 +145,18 @@ function client_sysinfo { echo sysinfo=$sysinfo_client } +# function: fatal [ MESSAGE ] +# +# Fatal error +# Prints its arguments to stderr and exits +function fatal { + echo "$0: ERROR:" "$@" >&2 + exit 1 +} + #----------------------------------------------------------------------------- # Beginning of main line execution. #----------------------------------------------------------------------------- -configuration initialization "$@" find_servers |