3 # Compile server client 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 examines the systemtap command line and packages the files and
13 # information needed to execute the command. This is then sent to a trusted
14 # systemtap server which will process the request and return the resulting
15 # kernel module (if requested) and any other information generated by the
16 # request. If a kernel module is generated, this script will load the module
17 # and execute it using 'staprun', if requested.
19 # Catch ctrl-c and other termination signals
20 trap 'terminate' SIGTERM
21 trap 'interrupt' SIGINT
22 trap 'ignore_signal' SIGHUP SIGPIPE
24 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # function: configuration
28 function configuration
{
29 tmpdir_prefix_client
=stap.client
30 tmpdir_prefix_server
=stap.server
31 avahi_service_tag
=_stap._tcp
34 # function: initialization
35 function initialization
{
41 # Where are we installed?
42 if test "`basename $0`" = "stap" -a "$0" = `which stap`; then
43 # The dejagnu test harness may invoke us as 'stap' relying on $PATH to
44 # find us. If so, then use the $PATH to find the rest of the systemtap
47 # Also, set the prefix to point to where we were found.
49 prefix
=`dirname $prefix`
51 # Assume we were installed normally
52 exec_prefix
=`dirname $0`
53 exec_prefix
=`cd $exec_prefix && pwd`/
54 prefix
=`dirname $exec_prefix`
57 # Default location for server certificates if we're not root
58 if test $EUID != 0; then
59 local_ssl_dbs
="$HOME/.systemtap/ssl/client"
61 # Additional location for all users.
62 public_ssl_dbs
=$prefix/etc
/systemtap
/ssl
/client
64 # Default options settings
70 # Create a temporary directory to package things in
71 # Do this before parsing the command line so that there is a place
72 # to put -I and -R directories.
73 tmpdir_client
=`mktemp -dt $tmpdir_prefix_client.XXXXXX` || \
74 fatal
"ERROR: cannot create temporary directory " $tmpdir_client
75 tmpdir_env
=`dirname $tmpdir_client`
78 # function: parse_options [ STAP-OPTIONS ]
80 # Examine the command line. We need not do much checking, but we do need to
81 # parse all options in order to discover the ones we're interested in.
82 # The server will take care of most situations and return the appropriate
85 function parse_options
{
94 # Start of a new token.
96 until test $advance_p != 0
98 # Identify the next option
99 first_char
=`expr "$first_token" : '\(.\).*'`
101 if test $dash_seen = 0; then
102 if test "$first_char" = "-"; then
103 if test "$first_token" != "-"; then
104 # It's not a lone dash, so it's an option.
105 # Is it a long option (i.e. --option)?
106 second_char
=`expr "$first_token" : '.\(.\).*'`
107 if test "X$second_char" = "X-"; then
108 long_option
=`expr "$first_token" : '--\(.*\)=.*'`
109 test "X$long_option" != "X" || long_option
=`expr "$first_token" : '--\(.*\)'`
112 process_ssl
$first_token
115 # An unknown or unimportant option.
116 # Ignore it, but pass it on to the server.
117 cmdline2
="$cmdline2 $first_token"
120 advance_p
=$
(($advance_p + 1))
123 # It's not a lone dash, or a long option, so it's a short option string.
125 first_token
=`expr "$first_token" : '-\(.*\)'`
127 first_char
=`expr "$first_token" : '\(.\).*'`
128 cmdline2
="$cmdline2 -"
131 if test $dash_seen = 0; then
132 # The dash has not been seen. This is either the script file
133 # name, a long argument or an argument to be passed to the probe module.
134 # If this is the first time, and -e has not been specified,
135 # then it could be the name of the script file.
136 if test "X$second_char" = "X-"; then
137 cmdline2
="$cmdline2 $first_token"
138 elif test "X$e_script" = "X" -a "X$script_file" = "X"; then
139 script_file
=$first_token
142 elif test "$first_char" != "'"; then
143 cmdline2
="$cmdline2 '$first_token'"
145 cmdline2
="$cmdline2 $first_token"
147 advance_p
=$
(($advance_p + 1))
152 # We are at the start of an option. Look at the first character.
158 get_arg
$first_token "$2"
159 process_c
"$stap_arg"
162 get_arg
$first_token $2
163 cmdline2
="${cmdline2}D '$stap_arg'"
166 get_arg
$first_token "$2"
167 process_e
"$stap_arg"
170 get_arg
$first_token $2
177 get_arg
$first_token $2
178 cmdline2
="${cmdline2}l '$stap_arg'"
181 get_arg
$first_token $2
185 get_arg
$first_token $2
189 get_arg
$first_token $2
193 get_arg
$first_token $2
194 cmdline2
="${cmdline2}r '$stap_arg'"
197 get_arg
$first_token $2
201 get_arg
$first_token $2
202 cmdline2
="${cmdline2}s '$stap_arg'"
205 v_level
=$
(($v_level + 1))
208 get_arg
$first_token $2
209 cmdline2
="${cmdline2}x '$stap_arg'"
212 # An unknown or unimportant flag. Ignore it, but pass it on to the server.
216 if test $advance_p = 0; then
217 # Just another flag character. Consume it.
218 cmdline2
="$cmdline2$first_char"
219 first_token
=`expr "$first_token" : '.\(.*\)'`
220 if test "X$first_token" = "X"; then
221 advance_p
=$
(($advance_p + 1))
226 # Consume the arguments we just processed.
227 while test $advance_p != 0
230 advance_p
=$
(($advance_p - 1))
234 # If the script file was given and it's not '-', then replace it with its
235 # client-temp-name in the command string.
236 if test "X$script_file" != "X"; then
238 if test "$script_file" != "-"; then
239 local_name
=`generate_client_temp_name $script_file`
241 local_name
=$script_file
243 cmdline
="$cmdline1 script/$local_name $cmdline2"
245 cmdline
="$cmdline1 $cmdline2"
248 local_ssl_dbs
="$additional_local_ssl_dbs $local_ssl_dbs"
251 # function: get_arg FIRSTWORD SECONDWORD
253 # Collect an argument to the given option
255 # Remove first character.
256 local opt
=`expr "$1" : '\(.\).*'`
257 local first
=`expr "$1" : '.\(.*\)'`
259 # Advance to the next token, if the first one is exhausted.
260 if test "X$first" = "X"; then
262 advance_p
=$
(($advance_p + 1))
266 test "X$first" != "X" || \
267 fatal
"Missing argument to -$opt"
270 advance_p
=$
(($advance_p + 1))
273 # function: process_ssl ARGUMENT
275 # Process the --ssl option.
276 function process_ssl
{
277 local db
=`expr "$1" : '--ssl=\(.*\)'`
279 test "X$db" != "X" || \
280 fatal
"Missing argument to --ssl"
282 additional_local_ssl_dbs
="$additional_local_ssl_dbs $db"
285 # function: process_c ARGUMENT
287 # Process the -c flag.
290 cmdline2
="${cmdline2}c '$1'"
293 # function: process_e ARGUMENT
295 # Process the -e flag.
297 # Only the first -e option is recognized and it overrides any script file name
298 # which may have already been identified.
299 if test "X$e_script" = "X"; then
301 if test "X$script_file" != "X"; then
302 cmdline1
="$cmdline1 $script_file $cmdline2"
307 cmdline2
="${cmdline2}e '$1'"
310 # function: process_I ARGUMENT
312 # Process the -I flag.
314 local local_name
=`include_file_or_directory tapsets $1`
315 test "X$local_name" != "X" ||
return
316 cmdline2
="${cmdline2}I 'tapsets/$local_name'"
319 # function: process_m ARGUMENT
321 # Process the -m flag.
324 cmdline2
="${cmdline2}m '$1'"
327 # function: process_o ARGUMENT
329 # Process the -o flag.
331 stdout_redirection
="$1"
332 cmdline2
="${cmdline2}o '$1'"
335 # function: process_p ARGUMENT
337 # Process the -p flag.
340 cmdline2
="${cmdline2}p '$1'"
343 # function: process_R ARGUMENT
345 # Process the -R flag.
347 local local_name
=`include_file_or_directory runtime $1`
348 test "X$local_name" != "X" ||
return
349 cmdline2
="${cmdline2}R 'runtime/$local_name'"
352 # function: include_file_or_directory PREFIX NAME
354 # Include the given file or directory in the client's temporary
355 # tree to be sent to the server.
356 function include_file_or_directory
{
357 # Add a symbolic link of the named file or directory to our temporary directory
358 local local_name
=`generate_client_temp_name $2`
359 mkdir
-p $tmpdir_client/$1/`dirname $local_name` || \
360 fatal
"ERROR: could not create $tmpdir_client/$1/`dirname $local_name`"
361 ln -s /$local_name $tmpdir_client/$1/$local_name || \
362 fatal
"ERROR: could not link $tmpdir_client/$1/$local_name to /$local_name"
366 # function: generate_client_temp_name NAME
368 # Generate the name to be used for the given file/directory relative to the
369 # client's temporary directory.
370 function generate_client_temp_name
{
371 # Transform the name into a fully qualified path name
372 local full_name
=`echo "$1" | sed "s,^\\\([^/]\\\),$wd/\\\\1,"`
374 # The same name without the initial / or trailing /
375 local local_name
=`echo "$full_name" | sed 's,^/\(.*\),\1,'`
376 local_name
=`echo "$local_name" | sed 's,\(.*\)/$,\1,'`
380 # function: create_request
382 # Add information to the client's temp directory representing the request
384 function create_request
{
385 # Work in our temporary directory
388 if test "X$script_file" != "X"; then
389 if test "$script_file" = "-"; then
390 mkdir
-p $tmpdir_client/script || \
391 fatal
"ERROR: cannot create temporary directory " $tmpdir_client/script
392 cat > $tmpdir_client/script
/$script_file
394 include_file_or_directory
script $script_file > /dev
/null
398 # Add the necessary info to special files in our temporary directory.
399 echo "cmdline: $cmdline" > cmdline
400 echo "sysinfo: `client_sysinfo`" > sysinfo
403 # function client_sysinfo
405 # Generate the client's sysinfo and echo it to stdout
406 function client_sysinfo
{
407 if test "X$sysinfo_client" = "X"; then
408 # Add some info from uname
409 sysinfo_client
="`uname -rvm`"
411 echo "$sysinfo_client"
414 # function: package_request
416 # Package the client's temp directory into a form suitable for sending to the
418 function package_request
{
419 # Package up the temporary directory into a zip file
422 local tmpdir_client_base
=`basename $tmpdir_client`
423 zip_client
=$tmpdir_env/`mktemp $tmpdir_client_base.zip.XXXXXX` || \
424 fatal
"ERROR: cannot create temporary file " $zip_client
426 (rm $zip_client && zip -r $zip_client $tmpdir_client_base > /dev
/null
) || \
427 fatal
"ERROR: zip of request tree, $tmpdir_client, failed"
430 # function: unpack_response
432 # Unpack the jar file received from the server and make the contents available
433 # for printing the results and/or running 'staprun'.
434 function unpack_response
{
435 tmpdir_server
=`mktemp -dt $tmpdir_prefix_client.server.XXXXXX` || \
436 fatal
"ERROR: cannot create temporary file " $tmpdir_server
438 # Unpack and verify the digitally signed server output directory
439 if ! signtool
-d $ssl_db -v $jar_server > /dev
/null
2>&1; then
440 # Run the verification again to get the reason
441 fatal
"ERROR: Verification of server response, $jar_server, failed.
442 "`signtool -d $ssl_db -v $jar_server | grep "reported reason"`
445 # Unpack the server output directory
446 unzip -d $tmpdir_server $jar_server > /dev
/null || \
447 fatal
"ERROR: Cannot unpack server response, $jar_server"
449 # Check the contents of the expanded directory. It should contain:
450 # 1) a file called stdout
451 # 2) a file called stderr
452 # 3) a file called rc
453 # 4) a directory called META-INF
454 # 5) optionally a directory named to match stap??????
455 local num_files
=`ls $tmpdir_server | wc -l`
456 test $num_files = 5 -o $num_files = 4 || \
457 fatal
"ERROR: Wrong number of files in server's temp directory"
458 test -f $tmpdir_server/stdout || \
459 fatal
"ERROR: `pwd`/$tmpdir_server/stdout does not exist or is not a regular file"
460 test -f $tmpdir_server/stderr || \
461 fatal
"ERROR: `pwd`/$tmpdir_server/stderr does not exist or is not a regular file"
462 test -f $tmpdir_server/rc || \
463 fatal
"ERROR: `pwd`/$tmpdir_server/rc does not exist or is not a regular file"
464 test -d $tmpdir_server/META-INF || \
465 fatal
"ERROR: `pwd`/$tmpdir_server/META-INF does not exist or is not a directory"
467 # See if there is a systemtap temp directory
468 tmpdir_stap
=`ls $tmpdir_server | grep stap`
469 tmpdir_stap
=`expr "$tmpdir_stap" : "\\\(stap......\\\)"`
470 if test "X$tmpdir_stap" != "X"; then
471 test -d $tmpdir_server/$tmpdir_stap || \
472 fatal
"ERROR: `pwd`/$tmpdir_server/$tmpdir_stap is not a directory"
474 # Move the systemtap temp directory to a local temp location, if -k
476 if test $keep_temps = 1; then
477 local local_tmpdir_stap
=`mktemp -dt stapXXXXXX` || \
478 fatal
"ERROR: cannot create temporary directory " $local_tmpdir_stap
479 mv $tmpdir_server/$tmpdir_stap/* $local_tmpdir_stap 2>/dev
/null
480 rm -fr $tmpdir_server/$tmpdir_stap
482 # Correct the name of the temp directory in the server's stderr output
483 sed -i "s,^Keeping temporary directory.*,Keeping temporary directory \"$local_tmpdir_stap\"," $tmpdir_server/stderr
484 tmpdir_stap
=$local_tmpdir_stap
486 # Make sure we own the systemtap temp directory if we are root.
487 test $EUID = 0 && chown
$EUID:$EUID $tmpdir_server/$tmpdir_stap
492 # function: find_and_connect_to_server
494 # Find and establish connection with a compatible stap server.
495 function find_and_connect_to_server
{
496 # Use a temp file here instead of a pipeline so that the side effects
497 # of choose_server are seen by the rest of this script.
499 ${exec_prefix}stap-find-servers
> servers
500 choose_server
< servers
504 # function: choose_server
506 # Examine each line from stdin and attempt to connect to each server
507 # specified until successful.
508 function choose_server
{
511 while read name server port remain
513 num_servers
=$
(($num_servers + 1))
515 # The server must match the dns name on the certificate
516 # and must be 'localhost' if the server is on the local host.
517 local server_host_name
=`expr "$name" : "\\\([a-zA-Z0-9-]*\\\).*"`
518 local server_domain_name
=`expr "$name" : "$server_host_name\\\(.*\\\)"`
519 local our_host_name
=`expr "$HOSTNAME" : "\\\([a-zA-Z0-9-]*\\\).*"`
520 local our_domain_name
=`expr "$HOSTNAME" : "$our_host_name\\\(.*\\\)"`
522 if test "X$server_domain_name" = "X.local"; then
523 server_domain_name
=$our_domain_name
525 if test "X$server_host_name$server_domain_name" = "X$our_host_name$our_domain_name"; then
528 server
=$server_host_name$server_domain_name
531 if test "X$server" = "X"; then
532 fatal
"ERROR: server ip address not provided"
535 if test "X$port" = "X"; then
536 fatal
"ERROR: server port not provided"
539 if send_receive
; then
544 if test $num_servers = 0; then
545 fatal
"ERROR: unable to find a server"
548 cat $tmpdir_client/connect
>&2
549 fatal
"ERROR: unable to connect to a server"
552 # function: send_receive
554 # Connect to the server, send the request and receive the response
555 function send_receive
{
556 # Make a place to receive the response file.
557 jar_server
=`mktemp -t $tmpdir_prefix_client.server.jar.XXXXXX` || \
558 fatal
"ERROR: cannot create temporary file " $jar_server
560 # If the server is local, try to connect using each of the given local
561 # certificate databases in turn for verification.
562 if test "X$server" = "Xlocalhost"; then
563 for db
in $local_ssl_dbs
565 # Send the request and receive the response using stap-client-connect
566 echo "Attempting connection with $server using certificate database in '$db'" >> $tmpdir_client/connect
567 ${exec_prefix}stap-client-connect
-i $zip_client -o $jar_server -d $db -p $port -h $server >> $tmpdir_client/connect
2>&1 &
568 wait '%${exec_prefix}stap-client-connect'
569 test $?
= 0 && ssl_db
=$db && return 0
574 # We can try the public certificate databases for all servers.
575 for db
in $public_ssl_dbs
577 # Send the request and receive the response using stap-client-connect
578 echo "Attempting connection with $server using certificate database in '$db'" >> $tmpdir_client/connect
579 ${exec_prefix}stap-client-connect
-i $zip_client -o $jar_server -d $db -p $port -h $server >> $tmpdir_client/connect
2>&1 &
580 wait '%${exec_prefix}stap-client-connect'
581 test $?
= 0 && ssl_db
=$db && return 0
585 # Could not connect using any of the certificate databases
589 # function: process_response
591 # Write the stdout and stderr from the server to stdout and stderr respectively.
592 function process_response
{
593 # Pick up the results of running stap on the server.
597 # Copy the module to the current directory, if -m was specified
598 if test "X$m_name" != "X"; then
599 if test -f $tmpdir_stap/$m_name.ko
; then
600 cp $tmpdir_stap/$m_name.ko
$wd
603 fatal
"module $tmpdir_stap/$m_name.ko does not exist"
607 # Output stdout and stderr as directed
611 # function: stream_output
613 # Output stdout and stderr as directed
614 function stream_output
{
620 # function: maybe_call_staprun
622 # Call staprun using the module returned from the server, if requested.
623 function maybe_call_staprun
{
624 if test $rc != 0; then
625 # stap run on the server failed, so don't bother
629 if test $p_phase -ge 4; then
630 # There should be a systemtap temporary directory.
631 if test "X$tmpdir_stap" = "X"; then
632 # OK if no script specified
633 if test "X$e_script" != "X" -o "X$script_file" != "X"; then
634 fatal
"ERROR: systemtap temporary directory is missing in server response"
639 # There should be a module.
640 local mod_name
=`ls $tmpdir_stap | grep '.ko$'`
641 if test "X$mod_name" = "X"; then
642 fatal
"ERROR: no module was found in $tmpdir_stap"
645 if test $p_phase = 5; then
646 test $v_level -gt 0 && echo "Pass 5: starting run." >&2
648 # We have a module. Try to run it
649 # If a -c command was specified, pass it along.
650 if test "X$c_cmd" != "X"; then
651 staprun_opts
="-c '$c_cmd'"
654 # The -v level will be one less than what was specified
656 for ((vl
=$
((v_level
- 1)); $vl > 0; --vl))
658 staprun_opts
="$staprun_opts -v"
661 # if -o was specified, pass it along
662 if test "X$stdout_redirection" != "X"; then
663 staprun_opts
="$staprun_opts -o $stdout_redirection"
666 # Run it in the background and wait for it. This
667 # way any signals send to us can be caught.
668 if test $v_level -ge 2; then
669 echo "running `which staprun` $staprun_opts $tmpdir_stap/`ls $tmpdir_stap | grep '.ko$'`" >&2
671 eval `staprun_PATH` "$staprun_opts" \
672 $tmpdir_stap/`ls $tmpdir_stap | grep '.ko$'`
674 wait '%?staprun' > /dev
/null
2>&1
677 # 127 from wait means that the job was already finished.
680 # Wait until the job actually disappears so that its output is complete.
681 while jobs '%?staprun' >/dev
/null
2>&1
686 test $v_level -gt 0 && echo "Pass 5: run completed in 0usr/0sys/0real ms." >&2
691 # function: staprun_PATH
693 # Compute a PATH suitable for running staprun.
694 function staprun_PATH
{
695 # If $SYSTEMTAP_STAPRUN is set, then use that
696 if test "X$SYSTEMTAP_STAPRUN" != "X"; then
697 echo $SYSTEMTAP_STAPRUN
701 # Otherwise, if there is an exec_prefix, then use it.
702 if test "X$exec_prefix" != "X"; then
703 echo ${exec_prefix}staprun
707 # Otherwise, we have been called by the dejagnu test harness as 'stap'
708 # and we are the first 'stap' on the path. Since staprun may call
709 # 'stap', remove the PATH component where we live from the PATH in order to
711 local first_stap
=`which stap`
712 local PATH_component
=`dirname $first_stap`
713 echo "PATH=$PATH staprun" |
sed "s,$PATH_component,,g"
716 # function: fatal [ MESSAGE ]
719 # Prints its arguments to stderr and exits
728 # Cleanup work files unless asked to keep them.
732 if test $keep_temps != 1; then
733 rm -fr $tmpdir_client
736 rm -fr $tmpdir_server
740 # function: terminate
742 # Terminate gracefully.
745 echo "$0: terminated by signal" >&2
748 # Kill any running staprun job
749 kill -s SIGTERM
'%?staprun' 2>/dev
/null
751 # Kill any stap-client-connect job
752 kill -s SIGTERM
'%${exec_prefix}stap-client-connect'
757 # function: interrupt
759 # Pass an interrupt (ctrl-C) to staprun
761 # Pass the signal on to any running staprun job
762 if test $staprun_running = 1; then
763 kill -s SIGINT
'%?staprun' 2>/dev
/null
767 # Kill any stap-client-connect job
768 # SIGINT won't do it.
769 kill -s SIGTERM
'%${exec_prefix}stap-client-connect'
771 # If staprun was not running, then exit.
776 # function: ignore_signal
778 # Called in order to ignore a signal
779 function ignore_signal
{
783 #-----------------------------------------------------------------------------
784 # Beginning of main line execution.
785 #-----------------------------------------------------------------------------
791 find_and_connect_to_server