3 # Find compile servers for systemtap
5 # Copyright (C) 2008, 2009 Red Hat Inc.
7 # This file is part of systemtap, and is free software. You can
8 # redistribute it and/or modify it under the terms of the GNU General
9 # Public License (GPL); either version 2, or (at your option) any
12 # This script uses avahi to find systemtap compile servers on the local
13 # network. Information about each server found is printed to stdout.
15 # Deprecation warning.
16 echo "WARNING: stap-find-servers is deprecated and will be removed in release 1.5" >&2
17 echo "Please use 'stap --list-servers'. See stap(1) for more information" >&2
19 # Initialize the environment
20 .
${PKGLIBEXECDIR}stap-env
22 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # function: initialization
26 function initialization
{
28 if test "X$1" = "X--all"; then
36 # function: find_servers
38 # Find and establish connection with a compatible stap server.
39 function find_servers
{
40 # Create a temp file for the list of servers. We do this instead
41 # of using a pipe so that we can kill avahi-browse if it
43 tmpfile
=`mktemp -t stap-serversXXXXXX` || \
44 fatal
"Cannot create temporary file " $tmpfile
47 avahi-browse
$stap_avahi_service_tag --terminate -r > $tmpfile &
49 for ((attempt
=0; $attempt < $timeout; ++attempt
))
51 if ! jobs '%avahi-browse' >/dev
/null
2>&1; then
57 # Kill avahi-browse, if it's still running
58 test $attempt = $timeout && kill -s SIGTERM
'%avahi-browse' 2>/dev
/null
60 match_server
< $tmpfile
64 # function: match_server
66 # Find a suitable server using the avahi-browse output provided on stdin.
67 function match_server
{
75 # Loop over the avahi service descriptors.
76 read -t $timeout ||
return
77 while test "X$REPLY" != "X"
84 # Examine the next service descriptor
85 # Is it a stap server?
86 if ! echo $REPLY |
grep -q "=.* .* IPv4 .*_stap"; then
87 read -t $timeout ||
return
92 # Get the details of the service
93 local service_tag equal service_data
94 while read -t $timeout service_tag equal service_data
98 server_name
=`expr "$service_data" : '\[\([^]]*\)\]'`
101 # Sometimes (seems random), avahi-resolve-host-name resolves a local server to its
102 # hardware address rather than its ip address. Keep trying until we get
104 server_ip
=`expr "$service_data" : '\[\([^]]*\)\]'`
106 for ((attempt
=0; $attempt < 5; ++attempt
))
108 server_ip
=`expr "$server_ip" : '\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)$'`
109 if test "X$server_ip" != "X"; then
112 # Resolve the server.domain to an ip address.
113 server_ip
=`avahi-resolve-host-name $server_name`
114 server_ip
=`expr "$server_ip" : '.* \(.*\)$'`
118 server_port
=`expr "$service_data" : '\[\([^]]*\)\]'`
121 server_sysinfo
=`expr "$service_data" : '\[.*\"\(sysinfo=[^]]*\)\"\]'`
124 REPLY
="$service_tag $equal $service_data"
130 # It's an stap server, but is it compatible?
131 if test $find_all = 0 -a "$server_sysinfo" != "`client_sysinfo`"; then
135 # It's compatible, or we're finding all servers. Print a summary line
136 echo "$server_name $server_ip $server_port '$server_sysinfo'"
141 # function client_sysinfo
143 # Generate the client's sysinfo and echo it to stdout
144 function client_sysinfo
{
145 if test "X$sysinfo_client" = "X"; then
146 # Add some info from uname
147 sysinfo_client
="`uname -r` `stap_get_arch`"
149 echo sysinfo
=$sysinfo_client
152 # function: fatal [ MESSAGE ]
155 # Prints its arguments to stderr and exits
157 echo "$0: ERROR:" "$@" >&2
161 #-----------------------------------------------------------------------------
162 # Beginning of main line execution.
163 #-----------------------------------------------------------------------------