blob: fde7d9efcea00ce9c2b39cad402b8d54e84c5a62 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
#!/bin/bash
# Find compile servers for systemtap
#
# 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
# Public License (GPL); either version 2, or (at your option) any
# later version.
# This script uses avahi to find systemtap compile servers on the local
# network. Information about each server found is printed to stdout.
#-----------------------------------------------------------------------------
# Helper functions.
#-----------------------------------------------------------------------------
# function: configuration
function configuration {
avahi_service_tag=_stap._tcp
timeout=10 # seconds
}
# function: initialization
function initialization {
rc=1 # not found yet
if test "X$1" = "X--all"; then
find_all=1
else
find_all=0
fi
}
# function: find_servers
#
# Find and establish connection with a compatible stap server.
function find_servers {
# 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 more than a minute.
tmpfile=`mktemp -t stap-serversXXXXXX` || \
fatal "Cannot create temporary file " $tmpfile
# Find servers
avahi-browse $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
#
# Find a suitable server using the avahi-browse output provided on stdin.
function match_server {
local server_ip
local server_name
local server_sysinfo
local server_port
rc=1 # not found yet
# Loop over the avahi service descriptors.
read -t $timeout || return
while test "X$REPLY" != "X"
do
server_name=
server_ip=
server_port=
server_sysinfo=
# Examine the next service descriptor
# Is it a stap server?
if ! echo $REPLY | grep -q "=.* .* IPv4 .*_stap"; then
read -t $timeout || return
continue
fi
REPLY=
# Get the details of the service
local service_tag equal service_data
while read -t $timeout service_tag equal service_data
do
case $service_tag in
hostname )
server_name=`expr "$service_data" : '\[\([^]]*\)\]'`
;;
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
# an ip address.
server_ip=`expr "$service_data" : '\[\([^]]*\)\]'`
local attempt
for ((attempt=0; $attempt < 5; ++attempt))
do
server_ip=`expr "$server_ip" : '\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)$'`
if test "X$server_ip" != "X"; then
break
fi
# Resolve the server.domain to an ip address.
server_ip=`avahi-resolve-host-name $server_name`
server_ip=`expr "$server_ip" : '.* \(.*\)$'`
done
;;
port )
server_port=`expr "$service_data" : '\[\([^]]*\)\]'`
;;
txt )
server_sysinfo=`expr "$service_data" : '\[.*\"\(sysinfo=[^]]*\)\"\]'`
;;
* )
REPLY="$service_tag $equal $service_data"
break
;;
esac
done
# It is a stap server, but is it compatible?
if test $find_all = 0 -a "$server_sysinfo" != "`client_sysinfo`"; then
continue
fi
# It's compatible, or we're finding all servers. Print a summary line
echo "$server_name $server_ip $server_port '$server_sysinfo'"
rc=0
done
}
# function client_sysinfo
#
# Generate the client's sysinfo and echo it to stdout
function client_sysinfo {
if test "X$sysinfo_client" = "X"; then
# Add some info from uname
sysinfo_client="`uname -rvm`"
fi
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
exit $rc
|