]>
Commit | Line | Data |
---|---|---|
0e47827d DB |
1 | #!/bin/bash |
2 | ||
3 | # Find compile servers for systemtap | |
4 | # | |
98f552c2 | 5 | # Copyright (C) 2008, 2009 Red Hat Inc. |
0e47827d DB |
6 | # |
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 | |
10 | # later version. | |
11 | ||
12 | # This script uses avahi to find systemtap compile servers on the local | |
13 | # network. Information about each server found is printed to stdout. | |
14 | ||
98f552c2 | 15 | # Initialize the environment |
961588fd | 16 | . ${PKGLIBEXECDIR}stap-env |
98f552c2 | 17 | |
0e47827d DB |
18 | #----------------------------------------------------------------------------- |
19 | # Helper functions. | |
20 | #----------------------------------------------------------------------------- | |
0e47827d DB |
21 | # function: initialization |
22 | function initialization { | |
23 | rc=1 # not found yet | |
24 | if test "X$1" = "X--all"; then | |
25 | find_all=1 | |
26 | else | |
27 | find_all=0 | |
28 | fi | |
fdef9f1f | 29 | timeout=20 # seconds |
0e47827d DB |
30 | } |
31 | ||
98f552c2 | 32 | # function: find_servers |
0e47827d | 33 | # |
98f552c2 | 34 | # Find and establish connection with a compatible stap server. |
0e47827d | 35 | function find_servers { |
98f552c2 DB |
36 | # Create a temp file for the list of servers. We do this instead |
37 | # of using a pipe so that we can kill avahi-browse if it | |
38 | # takes too long. | |
39 | tmpfile=`mktemp -t stap-serversXXXXXX` || \ | |
40 | fatal "Cannot create temporary file " $tmpfile | |
41 | ||
42 | # Find servers | |
28c79996 | 43 | avahi-browse $stap_avahi_service_tag --terminate -r > $tmpfile & |
98f552c2 DB |
44 | |
45 | for ((attempt=0; $attempt < $timeout; ++attempt)) | |
46 | do | |
47 | if ! jobs '%avahi-browse' >/dev/null 2>&1; then | |
48 | break | |
49 | fi | |
50 | sleep 1 | |
51 | done | |
52 | ||
53 | # Kill avahi-browse, if it's still running | |
54 | test $attempt = $timeout && kill -s SIGTERM '%avahi-browse' 2>/dev/null | |
55 | ||
56 | match_server < $tmpfile | |
57 | rm -fr $tmpfile | |
0e47827d DB |
58 | } |
59 | ||
60 | # function: match_server | |
61 | # | |
62 | # Find a suitable server using the avahi-browse output provided on stdin. | |
63 | function match_server { | |
64 | local server_ip | |
65 | local server_name | |
66 | local server_sysinfo | |
67 | local server_port | |
98f552c2 DB |
68 | |
69 | rc=1 # not found yet | |
0e47827d DB |
70 | |
71 | # Loop over the avahi service descriptors. | |
98f552c2 | 72 | read -t $timeout || return |
2c92c302 | 73 | while test "X$REPLY" != "X" |
0e47827d | 74 | do |
2c92c302 DB |
75 | server_name= |
76 | server_ip= | |
77 | server_port= | |
78 | server_sysinfo= | |
79 | ||
0e47827d DB |
80 | # Examine the next service descriptor |
81 | # Is it a stap server? | |
2c92c302 | 82 | if ! echo $REPLY | grep -q "=.* .* IPv4 .*_stap"; then |
98f552c2 | 83 | read -t $timeout || return |
2c92c302 DB |
84 | continue |
85 | fi | |
86 | REPLY= | |
0e47827d DB |
87 | |
88 | # Get the details of the service | |
2c92c302 | 89 | local service_tag equal service_data |
98f552c2 | 90 | while read -t $timeout service_tag equal service_data |
0e47827d DB |
91 | do |
92 | case $service_tag in | |
0e47827d DB |
93 | hostname ) |
94 | server_name=`expr "$service_data" : '\[\([^]]*\)\]'` | |
95 | ;; | |
96 | address ) | |
97 | # Sometimes (seems random), avahi-resolve-host-name resolves a local server to its | |
98f552c2 | 98 | # hardware address rather than its ip address. Keep trying until we get |
0e47827d DB |
99 | # an ip address. |
100 | server_ip=`expr "$service_data" : '\[\([^]]*\)\]'` | |
101 | local attempt | |
102 | for ((attempt=0; $attempt < 5; ++attempt)) | |
103 | do | |
2c92c302 | 104 | server_ip=`expr "$server_ip" : '\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)$'` |
0e47827d DB |
105 | if test "X$server_ip" != "X"; then |
106 | break | |
107 | fi | |
108 | # Resolve the server.domain to an ip address. | |
2c92c302 | 109 | server_ip=`avahi-resolve-host-name $server_name` |
0e47827d DB |
110 | server_ip=`expr "$server_ip" : '.* \(.*\)$'` |
111 | done | |
112 | ;; | |
113 | port ) | |
114 | server_port=`expr "$service_data" : '\[\([^]]*\)\]'` | |
115 | ;; | |
116 | txt ) | |
b20db09e | 117 | server_sysinfo=`expr "$service_data" : '\[.*\"\(sysinfo=[^]]*\)\"\]'` |
0e47827d DB |
118 | ;; |
119 | * ) | |
2c92c302 DB |
120 | REPLY="$service_tag $equal $service_data" |
121 | break | |
122 | ;; | |
0e47827d DB |
123 | esac |
124 | done | |
125 | ||
98f552c2 | 126 | # It's an stap server, but is it compatible? |
0e47827d DB |
127 | if test $find_all = 0 -a "$server_sysinfo" != "`client_sysinfo`"; then |
128 | continue | |
129 | fi | |
130 | ||
131 | # It's compatible, or we're finding all servers. Print a summary line | |
2c92c302 | 132 | echo "$server_name $server_ip $server_port '$server_sysinfo'" |
0e47827d DB |
133 | rc=0 |
134 | done | |
0e47827d DB |
135 | } |
136 | ||
137 | # function client_sysinfo | |
138 | # | |
139 | # Generate the client's sysinfo and echo it to stdout | |
140 | function client_sysinfo { | |
141 | if test "X$sysinfo_client" = "X"; then | |
142 | # Add some info from uname | |
7901b115 | 143 | sysinfo_client="`uname -r` `stap_get_arch`" |
0e47827d | 144 | fi |
b20db09e | 145 | echo sysinfo=$sysinfo_client |
0e47827d DB |
146 | } |
147 | ||
98f552c2 DB |
148 | # function: fatal [ MESSAGE ] |
149 | # | |
150 | # Fatal error | |
151 | # Prints its arguments to stderr and exits | |
152 | function fatal { | |
153 | echo "$0: ERROR:" "$@" >&2 | |
154 | exit 1 | |
155 | } | |
156 | ||
0e47827d DB |
157 | #----------------------------------------------------------------------------- |
158 | # Beginning of main line execution. | |
159 | #----------------------------------------------------------------------------- | |
0e47827d DB |
160 | initialization "$@" |
161 | find_servers | |
162 | ||
163 | exit $rc |