Debian Codegraph @

testsuite/lib/http_server.exp

raw
1 # Returns a random integer between 'min' and 'max'.
2 proc rand_between { min max } {
3 set maxFactor [expr [expr $max + 1] - $min]
4 set value [expr int([expr rand() * 100])]
5 set value [expr [expr $value % $maxFactor] + $min]
6 return $value
7 }
8
9 # Returns a random unused tcp port.
10 proc find_random_unused_tcp_port { } {
11 # Find a random unused unprivileged (> 1024) port.
12 if {[catch {exec netstat --listening --all --tcp --numeric} output]} {
13 return 0
14 }
15 set used_ports {}
16 foreach line [split $output "\n"] {
17 if {[regexp {^tcp[ \t]+[0-9]+[ \t]+[^:]+:([0-9]+)[ \t]+} \
18 $line match port]} {
19 lappend used_ports $port
20 }
21 }
22 set used_ports [lsort -integer $used_ports]
23 set port 0
24 while {$port == 0} {
25 # If the port wasn't found in the list of used ports, return it.
26 set rand_port [rand_between 1024 65535]
27 if {[lsearch -exact -sorted -integer $used_ports $rand_port] == -1} {
28 set port $rand_port
29 }
30 }
31 verbose -log "returning port $port"
32 return $port
33 }
34
35 proc http_server_p {} {
36 global env
37 global systemtap_stap_server_user_exists
38
39 # OK, there isn't a 'stap -V' test that will tell us if the HTTP
40 # server was compiled into systemtap. So, we fake it. But, the
41 # HTTP server does require NSS support (for module signing).
42 if {! [nss_p]} {
43 verbose -log "http_server_p: no NSS support"
44 return 0
45 }
46
47 # Let's make sure the server binary exists.
48 if {! [info exists env(PKGLIBDIR)]
49 || [string length $env(PKGLIBDIR)] == 0} {
50 verbose -log "http_server_p: no PKGLIBDIR"
51 return 0
52 }
53 set http_server_path "$env(PKGLIBDIR)/stap-httpd"
54 if {! [file exists $http_server_path]} {
55 verbose -log "http_server_p: no $http_server_path"
56 return 0
57 }
58
59 # Let's also make sure the 'stap-server' user exists (we can't run
60 # the server as root).
61 if {! $systemtap_stap_server_user_exists} {
62 verbose -log "http_server_p: no 'stap-server' user"
63 return 0
64 }
65
66 return 1
67 }
68
69 # as_root_with_output: run 'command' as root. If the command should be
70 # run in the background, set 'background' to non-zero. Returns a list
71 # of [ RETURN_STATUS OUTPUT ]. A typical call would look like:
72 #
73 # lassign [as_root_with_output "mktemp"] rc output
74 #
75 proc as_root_with_output { command {background 0} } {
76 set effective_uid [exec /usr/bin/id -u]
77
78 if {$effective_uid != 0} {
79 set command "sudo $command"
80 }
81 if {$background == 1} {
82 set command "$command &"
83 }
84 verbose -log "running: [subst $command]"
85 set rc 0
86 if {[catch {eval exec $command} output]} {
87 # non-zero exit status, get it:
88 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
89 set rc [lindex $::errorCode 2]
90 }
91 }
92 verbose -log "RC: $rc"
93 verbose -log "$output"
94 return [list $rc $output]
95 }
96
97 # as_user_with_output: run 'command' as user 'user'. If the command
98 # should be run in the background, set 'background' to non-zero.
99 # Returns a list of [ RETURN_STATUS OUTPUT ]. A typical call would
100 # look like:
101 #
102 # lassign [as_user_with_output "user" "mktemp"] rc output
103 #
104 proc as_user_with_output { user command {background 0} } {
105 set effective_uid [exec /usr/bin/id -u]
106
107 # The following should work whether we're root or not.
108 set command "sudo -u $user -s -- $command"
109 if {$background == 1} {
110 set command "$command &"
111 }
112 verbose -log "running: [subst $command]"
113 set rc 0
114 if {[catch {eval exec $command} output]} {
115 # non-zero exit status, get it:
116 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
117 set rc [lindex $::errorCode 2]
118 }
119 }
120 verbose -log "RC: $rc"
121 verbose -log "$output"
122 return [list $rc $output]
123 }
124
125 # Starts a http server on the local system. Note that if you want to
126 # test a remote server, you can set the 'SYSTEMTAP_HTTP_SERVER_SPEC'
127 # environment variable. The command would look something like:
128 # SYSTEMTAP_HTTP_SERVER_SPEC=host.redhat.com:1234 make installcheck \
129 # RUNTESTFLAGS=http_server.exp
130 proc http_start_server { } {
131 global env systemtap_http_server_pid
132 global systemtap_http_server_logfile
133 global systemtap_http_server_pid
134 global systemtap_http_server_spec
135
136 # Make sure we have the minimum requirements.
137 if {! [http_server_p]} {
138 return 1
139 }
140
141 # Let's only use the installed server.
142 if {! [installtest_p]} {
143 return 1
144 }
145
146 # Create a temporary server log file. Note that we'll delete it
147 # when shutting the server down.
148 lassign [as_user_with_output "stap-server" "mktemp" 0] rc systemtap_http_server_logfile
149 if {$rc != 0} {
150 verbose -log "$systemtap_http_server_logfile"
151 return 1
152 }
153
154 # Special shortcut. If the 'SYSTEMTAP_HTTP_SERVER_SPEC'
155 # environment variable exists, use its value as the server spec.
156 if {[info exists env(SYSTEMTAP_HTTP_SERVER_SPEC)]} {
157 set systemtap_http_server_spec $env(SYSTEMTAP_HTTP_SERVER_SPEC)
158 set systemtap_http_server_pid 0
159 return 0
160 }
161
162 # Grab a random port number. We'll use a random port to avoid
163 # bothering any running servers. Note that there is a TOCTTOU
164 # problem here. Between the time the port is found and the server
165 # is started, another process could have been started on that
166 # port.
167 set port [find_random_unused_tcp_port]
168 if {$port == 0} {
169 return 1
170 }
171
172 # Start the server with the random port as the 'stap-server' user.
173 set http_server_path "$env(PKGLIBDIR)/stap-httpd"
174 set cmd "$http_server_path --port=$port --log=$systemtap_http_server_logfile"
175 lassign [as_user_with_output "stap-server" $cmd 1] rc output
176 # Give stap_httpd a chance to get setup, especially pk12util/openssl spawn
177 # Check output log for spawn result instead?
178 sleep 5
179 if {$rc != 0 || [string length $output] == 0
180 || [catch {expr int($output)}]} {
181 verbose -log "Cannot start an http server: $output"
182 set systemtap_http_server_pid 0
183 return 1
184 }
185
186 # Server started successfully. Find the actual server pid. The pid
187 # returned by the command above might be the pid of 'sudo', not
188 # the actual httpd server process.
189 #
190 # Note that since we started the server in the background, we
191 # might have to wait till it actually gets started.
192 set rc 1
193 set waited 0
194 set cmd {ps ax | grep -F /stap-httpd | grep -v sudo | grep -Fe --port=$port}
195 while { $rc != 0 && $waited < 10 } {
196 catch { exec sleep 1 }
197 incr waited
198
199 verbose -log "running: [subst $cmd]"
200 set rc 0
201 if {[catch {eval exec $cmd} output] } {
202 verbose -log "$output"
203 # non-zero exit status, get it:
204 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
205 set rc [lindex $::errorCode 2]
206 }
207 }
208 }
209 if {$rc != 0} {
210 verbose -log "Cannot find http server pid"
211 verbose -log "$output"
212 set systemtap_http_server_pid 0
213 return 1
214 }
215
216 # OK, now we've got to be sure we've only got 1 output line (so if
217 # for some odd reason we found two http server processes we'll
218 # know).
219 set lines [llength [split $output "\n"]]
220 if {$lines != 1} {
221 verbose -log "Found too many http servers!"
222 verbose -log "$output"
223 set systemtap_http_server_pid 0
224 return 1
225 }
226
227 # OK, we should how have the http server 'ps' line output. Extract
228 # the pid.
229 if {! [regexp {^[ ]*([0-9]+)[ \t]+} $output match pid_str]} {
230 verbose -log "Couldn't find 'pid' in ps output"
231 verbose -log "$output"
232 set systemtap_http_server_pid 0
233 return 1
234 }
235 set systemtap_http_server_pid [expr int($pid_str)]
236
237 # Make sure the server started up and is running.
238 set systemtap_http_server_spec [info hostname]:$port
239 set cmd {stap --list-servers --use-http-server=$systemtap_http_server_spec}
240 verbose -log "running: [subst $cmd]"
241 set rc [catch {eval exec $cmd} output]
242 verbose -log "$output"
243
244 set cmd {stap --trust-servers=ssl,no-prompt --use-http-server=$systemtap_http_server_spec}
245 verbose -log "running: [subst $cmd]"
246 set rc [catch {eval exec $cmd} output]
247 set errorcode "$::errorCode"
248 if {$errorcode != "NONE"} {
249 verbose -log "Couldn't add trust in the http server $errorcode"
250 http_shutdown_server
251 return 1
252 }
253
254 verbose -log "Started an http server as $systemtap_http_server_pid"
255 return 0
256 }
257
258 proc http_shutdown_server { } {
259 global env
260 global systemtap_http_server_pid
261 global systemtap_http_server_logfile
262 global systemtap_http_server_spec
263
264 # If we're using a remote server, just return.
265 if {[info exists env(SYSTEMTAP_HTTP_SERVER_SPEC)]} {
266 return
267 }
268
269 if {$systemtap_http_server_pid != 0} {
270 set cmd {stap --trust-servers=revoke,ssl,no-prompt --use-http-server=$systemtap_http_server_spec}
271 verbose -log "running: [subst $cmd]"
272 set rc [catch {eval exec $cmd} output]
273 set errorcode "$::errorCode"
274 if {$errorcode != "NONE"} {
275 verbose -log "Couldn't revoke trust in the http server $errorcode"
276 }
277
278 verbose -log "Stopping the systemtap http server with PID==$systemtap_http_server_pid"
279 kill -INT $systemtap_http_server_pid 5 1
280 set systemtap_http_server_pid 0
281
282 # Delete the temporary server log file. Note that we're
283 # ignoring success or failure.
284 as_user_with_output "stap-server" "rm -f $systemtap_http_server_logfile" 0]
285 }
286
287 # Make sure that stap can no longer find the server.
288 set waited 0
289 set rc 1
290 while { $rc == 1 && $waited < 10 } {
291 catch {exec sleep 1}
292 incr waited
293
294 set rc 0
295 if {[catch {exec sh -c "stap --list-servers --use-http-server=$systemtap_http_server_spec 2>&1 | grep -F 'No servers found'"} output]} {
296 # non-zero exit status, get it:
297 if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
298 set rc [lindex $::errorCode 2]
299 }
300 }
301 }
302 unset systemtap_http_server_spec
303
304 if {$rc == 0} {
305 verbose -log "http_shutdown_server: no http servers (after $waited tries)"
306 } else {
307 verbose -log "http_shutdown_server: warning - stap can still find a http server"
308 }
309 }