3 # Compile server client for systemtap
5 # Copyright (C) 2008-2010 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 # Initialize the environment
25 .
${PKGLIBEXECDIR}stap-env
27 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # function: initialization
31 function initialization
{
32 our_host_name
=`expr "$HOSTNAME" : "\\\([a-zA-Z0-9-]*\\\).*"`
33 our_domain_name
=`expr "$HOSTNAME" : "$our_host_name\\\(.*\\\)"`
39 # Default location for server certificates if we're not root
40 # Must be owned by us.
42 if test $EUID != 0; then
43 if test -e $stap_user_ssl_db/client
; then
44 if check_db
$stap_user_ssl_db/client
$EUID $USER; then
45 local_ssl_dbs
=$stap_user_ssl_db/client
49 # Additional location for all users. Must be owned by root.
50 if test -e $stap_root_ssl_db/client
; then
51 if check_db
$stap_root_ssl_db/client
0 root
; then
52 public_ssl_dbs
=$stap_root_ssl_db/client
56 # Default options settings
65 # Default variable settings
68 # Create a temporary directory to package things in
69 # Do this before parsing the command line so that there is a place
70 # to put -I and -R directories.
71 tmpdir_client
=`mktemp -dt $stap_tmpdir_prefix_client.XXXXXX` || \
72 fatal
"Cannot create temporary directory " $tmpdir_client
73 tmpdir_env
=`dirname $tmpdir_client`
75 session_only_prompt_done
=0
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
{
86 # Each command line argument will be written to its own file within the
98 # Start of a new token.
100 until test $advance != 0
102 # Identify the next option
103 first_char
="${first_token:0:1}"
105 if test $dash_seen = 0; then
106 if test "$first_char" = "-"; then
107 if test "$first_token" != "-"; then
108 # It's not a lone dash, so it's an option.
109 # Is it a long option (i.e. --option)?
110 second_char
="${first_token:1:1}"
111 if test "X$second_char" = "X-"; then
112 case "$first_token" in
114 process_ssl
"$first_token"
117 process_server
"$first_token"
120 # An unknown or unimportant option. Ignore it.
123 advance
=$
(($advance + 1))
126 # It's not a lone dash, or a long option, so it's a short option string.
128 first_token
="${first_token:1}"
130 first_char
="${first_token:0:1}"
133 if test $dash_seen = 0; then
134 # The dash has not been seen. This is either the script file
135 # name or an argument to be passed to the probe module.
136 # If this is the first time, and -e has not been specified,
137 # then it could be the name of the script file.
138 if test "X$e_script" = "X" -a "X$script_file" = "X"; then
139 script_file
="$first_token"
140 script_file_argc
=$argc
142 advance
=$
(($advance + 1))
147 # We are at the start of an option. Look at the first character.
150 get_arg
"$first_token" "$2"
151 process_a
"$stap_arg"
154 get_arg
"$first_token" "$2"
157 get_arg
"$first_token" "$2"
158 process_c
"$stap_arg"
161 get_arg
"$first_token" "$2"
164 get_arg
"$first_token" "$2"
165 process_e
"$stap_arg"
168 get_arg
"$first_token" "$2"
169 process_I
"$stap_arg"
175 get_arg
"$first_token" "$2"
179 get_arg
"$first_token" "$2"
183 get_arg
"$first_token" "$2"
184 process_m
"$stap_arg"
187 get_arg
"$first_token" "$2"
188 process_o
"$stap_arg"
191 get_arg
"$first_token" "$2"
192 process_p
"$stap_arg"
195 get_arg
"$first_token" "$2"
196 process_r
"$stap_arg"
199 get_arg
"$first_token" "$2"
200 process_R
"$stap_arg"
203 get_arg
"$first_token" "$2"
206 get_arg
"$first_token" "$2"
209 v_level
=$
(($v_level + 1))
212 get_arg
"$first_token" "$2"
215 # An unknown or unimportant flag.
219 if test $advance = 0; then
220 # Just another flag character. Consume it.
221 first_token
="${first_token:1}"
222 if test "X$first_token" = "X"; then
223 advance
=$
(($advance + 1))
228 # Consume the arguments we just processed.
229 while test $advance != 0; do
230 # Does the final argument file contain a client-side file
231 # name which must be changed to a server-side name?
233 if test "X$arg_subst" != "X" -a $advance = 1; then
240 # If it's not client-only argument,
241 # place the argument in a numbered file within our temp
243 # o We don't write a newline at the end, since newline could be
244 # part of the argument.
245 # o We add an X to the beginning of the file
246 # in order to avoid having 'echo' interpret the output as
247 # its own option. We then remove the X.
248 # There must be a better way.
249 if test $client_arg = 0; then
250 echo -n "X$arg" > "$tmpdir_client/argv$argc"
251 sed -i "s|^X||" "$tmpdir_client/argv$argc"
255 # Get the next argument.
257 advance
=$
(($advance - 1))
262 # If the script file was given and it's not '-', then replace it with its
263 # client-temp-name in its argument file.
264 if test "X$script_file" != "X"; then
266 if test "$script_file" != "-"; then
267 generate_client_temp_name
"$script_file"
268 local_name
="$client_temp_name"
272 echo -n "script/$local_name" > "$tmpdir_client/argv$script_file_argc"
275 # Processing based on final options settings
276 # Complete the list of local certificate databases
277 local_ssl_dbs
="$additional_local_ssl_dbs $local_ssl_dbs"
279 # We must have at least one usable certificate database.
280 test "X$local_ssl_dbs" != "X " -o "X$public_ssl_dbs" != "X" || \
281 fatal
"No usable certificate databases found"
284 # function: get_arg FIRSTWORD SECONDWORD
286 # Collect an argument to the given option
288 # Remove first character.
291 packed_options
="${packed_options}$opt"
293 # Advance to the next token, if the first one is exhausted.
294 if test "X$first" = "X"; then
295 advance
=$
(($advance + 1))
300 test "X$first" != "X" && advance
=$
(($advance + 1))
303 # function: process_ssl ARGUMENT
305 # Process the --ssl option.
306 function process_ssl
{
310 test "X$db" != "X" || \
311 fatal
"Missing argument to --ssl"
313 check_db
"$db" ||
return
315 additional_local_ssl_dbs
="$additional_local_ssl_dbs $db"
318 # function: process_server ARGUMENT
320 # Process the --server option.
321 function process_server
{
325 test "X$spec" != "X" || \
326 fatal
"Missing argument to --server"
328 specified_servers
="$specified_servers $spec"
331 # function: process_c ARGUMENT
333 # Process the -c flag.
338 # function: process_e ARGUMENT
340 # Process the -e flag.
342 # Only the first -e option is recognized and it overrides any script file name
343 # which may have already been identified.
344 if test "X$e_script" = "X"; then
350 # function: process_I ARGUMENT ORIGINAL_ARGUMENT
352 # Process the -I flag.
354 test "X$1" = "X" && return
357 include_file_or_directory tapsets
"$1"
358 if test $advance = 1; then
359 arg_subst
="${packed_options}tapsets/$included_name"
361 arg_subst
="tapsets/$included_name"
365 # function: process_m ARGUMENT
367 # Process the -m flag.
373 # function: process_o ARGUMENT
375 # Process the -o flag.
377 stdout_redirection
="$1"
380 # function: process_p ARGUMENT
382 # Process the -p flag.
387 # function: process_r ARGUMENT
389 # Process the -r flag.
391 local first_char
="${1:0:1}"
393 if test "$first_char" = "/"; then # fully specified path
394 kernel_build_tree
="$1"
395 version_file_name
="$kernel_build_tree/include/config/kernel.release"
396 # The file include/config/kernel.release within the
397 # build tree is used to pull out the version information
398 release
=`cat "$version_file_name" 2>/dev/null`
399 if test "X$release" = "X"; then
400 fatal
"Missing $version_file_name"
404 # kernel release specified directly
408 if test "X$release" != "X$uname_r"; then
414 # function: process_a ARGUMENT
416 # Process the -a flag.
418 if test "X$1" != "X$arch"; then
424 # function: process_R ARGUMENT ORIGINAL_ARGUMENT
426 # Process the -R flag.
428 test "X$1" = "X" && return
431 include_file_or_directory runtime
"$1"
432 if test $advance = 1; then
433 arg_subst
="${packed_options}runtime/$included_name"
435 arg_subst
="runtime/$included_name"
439 # function: include_file_or_directory PREFIX NAME
441 # Include the given file or directory in the client's temporary
442 # tree to be sent to the server and save it's name in the variable
443 # included_name. We use a global variable instread of echoing the
444 # result since the use of `include_file_or_directory` loses a trailing
446 function include_file_or_directory
{
447 # Add a symbolic link of the named file or directory to our temporary
448 # directory, but only if the file or directory exists.
449 generate_client_temp_name
"$2"
450 local local_name
="$client_temp_name"
451 included_name
="$local_name"
452 test -e "/$local_name" ||
return
454 local local_dirname
=`dirname "$local_name"`
455 mkdir
-p "$tmpdir_client/$1/$local_dirname" || \
456 fatal
"Could not create $tmpdir_client/$1/$local_dirname"
457 ln -s "/$local_name" "$tmpdir_client/$1/$local_name" || \
458 fatal
"Could not link $tmpdir_client/$1/$local_name to /$local_name"
461 # function: generate_client_temp_name NAME
463 # Generate the name to be used for the given file/directory relative to the
464 # client's temporary directory and stores it in the variable
465 # client_temp_name. We use a global variable instread of echoing the
466 # result since the use of `generate_client_temp_name` loses a trailing
468 function generate_client_temp_name
{
469 # Transform the name into a fully qualified path name
471 test "${full_name:0:1}" != "/" && full_name
="$wd/$full_name"
473 # The same name without the initial / or trailing /
474 local local_name
="${full_name:1}"
475 test "${local_name: -1:1}" = "/" && local_name="${local_name:0:$((${#local_name}-1))}"
477 client_temp_name
="$local_name"
480 # function: create_request
482 # Add information to the client's temp directory representing the request
484 function create_request
{
485 # Work in our temporary directory
488 if test "X$script_file" != "X"; then
489 if test "$script_file" = "-"; then
490 mkdir
-p $tmpdir_client/script || \
491 fatal
"Cannot create temporary directory " $tmpdir_client/script
492 cat > "$tmpdir_client/script/$script_file"
494 include_file_or_directory
script "$script_file"
498 # Add the necessary info to special files in our temporary directory.
499 echo "sysinfo: `client_sysinfo`" > sysinfo
502 # function client_sysinfo
504 # Generate the client's sysinfo and echo it to stdout
505 function client_sysinfo
{
506 echo "$uname_r $arch"
509 # function: package_request
511 # Package the client's temp directory into a form suitable for sending to the
513 function package_request
{
514 # Package up the temporary directory into a zip file
517 local tmpdir_client_base
=`basename $tmpdir_client`
518 zip_client
=$tmpdir_env/`mktemp $tmpdir_client_base.zip.XXXXXX` || \
519 fatal
"Cannot create temporary file " $zip_client
522 (rm -f $zip_client && zip -r $zip_client * > /dev
/null
) || \
523 fatal
"zip of request tree, $tmpdir_client, failed"
526 # function: unpack_response
528 # Unpack the zip file received from the server and make the contents available
529 # for printing the results and/or running 'staprun'.
530 function unpack_response
{
531 tmpdir_server
=`mktemp -dt $stap_tmpdir_prefix_client.server.XXXXXX` || \
532 fatal
"Cannot create temporary file " $tmpdir_server
534 # Unpack the server output directory
535 unzip -d $tmpdir_server $zip_server > /dev
/null || \
536 fatal
"Cannot unpack server response, $zip_server"
538 # Check the contents of the directory. It should contain:
539 # 1) a file called stdout
540 # 2) a file called stderr
541 # 3) a file called rc
542 # 4) optionally a directory named to match stap??????
543 local num_files
=`ls $tmpdir_server | wc -l`
544 test $num_files = 4 -o $num_files = 3 || \
545 fatal
"Wrong number of files in server's temp directory"
546 test -f $tmpdir_server/stdout || \
547 fatal
"`pwd`/$tmpdir_server/stdout does not exist or is not a regular file"
548 test -f $tmpdir_server/stderr || \
549 fatal
"`pwd`/$tmpdir_server/stderr does not exist or is not a regular file"
550 test -f $tmpdir_server/rc || \
551 fatal
"`pwd`/$tmpdir_server/rc does not exist or is not a regular file"
553 # See if there is a systemtap temp directory. There should be at least an empty one.
554 # ls -l $tmpdir_server
555 tmpdir_stap
=`cd $tmpdir_server && ls | grep stap......\$ 2>/dev/null`
556 if test "X$tmpdir_stap" != "X"; then
557 test -d $tmpdir_server/$tmpdir_stap || \
558 fatal
"$tmpdir_server/$tmpdir_stap is not a directory"
560 # Move the systemtap temp directory to a local temp location, if -k
562 if test $keep_temps = 1; then
563 local local_tmpdir_stap
=`mktemp -dt stapXXXXXX` || \
564 fatal
"Cannot create temporary directory " $local_tmpdir_stap
565 mv $tmpdir_server/$tmpdir_stap/* $local_tmpdir_stap 2>/dev
/null
566 rm -fr $tmpdir_server/$tmpdir_stap
568 # Correct the name of the temp directory in the server's stderr output
569 sed -i "s,^Keeping temporary directory.*,Keeping temporary directory \"$local_tmpdir_stap\"," $tmpdir_server/stderr
570 tmpdir_stap
=$local_tmpdir_stap
572 tmpdir_stap
=$tmpdir_server/$tmpdir_stap
573 # Make sure we own the systemtap temp directory if we are root.
574 test $EUID = 0 && chown
$EUID:$EUID $tmpdir_stap
578 if test $keep_temps = 0; then
579 # Remove the output line due to the synthetic server-side -k
580 sed -i "/^Keeping temporary directory.*/ d" $tmpdir_server/stderr
583 if test $p_phase = 5; then
584 # Remove the output line due to the synthetic server-side -p4
585 sed -i "/^.*\.ko$/ d" $tmpdir_server/stdout
589 # function: find_and_connect_to_server
591 # Find and establish connection with a compatible stap server.
592 function find_and_connect_to_server
{
595 # Make a place to receive the response file.
596 zip_server
=`mktemp -t $stap_tmpdir_prefix_client.server.zip.XXXXXX` || \
597 fatal
"Cannot create temporary file " $zip_server
599 # If servers were specified on the command line, then try them
600 # in sequence. Don't try any other servers.
601 if test "X$specified_servers" != "X"; then
602 for server
in $specified_servers; do
603 num_servers
=$
(($num_servers + 1))
605 # If the server is completely specified, (i.e. server:port),
606 # then try it directly.
607 port
=`expr "$server" : '.\+:\([0-9]\+\)'`
608 if test "X$port" != "X"; then
609 name
=`expr "$server" : '\(.\+\):[0-9]\+'`
611 # If we have been given an ip address, then try to resolve it to a name.
612 # If we have been given a name, try to resolve the full name.
613 # The full name is needed in order to validate the server's certificate.
614 address
=`expr "$name" : '\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)'`
615 if test "X$address" = "X"; then
616 # We've been given a host name
617 full_name
=`nslookup $name | awk '/^Name\:/ {print $2}'`
618 if test "X$full_name" != "X"; then
622 # We've been given an ip address.
623 name
=`nslookup $address | awk '/in-addr\.arpa/ {print $4}'`
624 name
=`expr "$name" : '\(.*\)\.'`
625 if test "X$name" = "X"; then
626 echo "Cannot resolve ip address $address" >> "$tmpdir_client/connect"
631 # Now try to contact the given server.
632 send_receive
$name $port && return
636 # Otherwise select the matching server from the available servers
637 # and use the port it is advertizing.
639 # Have we been given an ip address? If so, just use it.
640 address
=`expr "$server" : '\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)'`
641 if test "X$address" = "X"; then
642 # We have not been given an ip address. Try to resolve it as a host name.
643 if test "X$server" = "Xlocalhost"; then
644 # We don't want the address of the loopback interface here. Avahi will present
645 # the actual ip address.
646 server
=$our_host_name$our_domain_name
648 address
=`nslookup $server | awk '/^Address\:[ \t][0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ {print $2}'`
649 if test "X$address" = "X"; then
650 echo "Cannot resolve server $server" >> "$tmpdir_client/connect"
655 ${stap_pkglibexecdir}stap-find-servers
$find_all |
grep $address > "$tmpdir_client/servers"
657 if test `wc -l "$tmpdir_client/servers" | awk '{print $1}'` = "0"; then
658 warning
"No server is available on $server" 2>> "$tmpdir_client/connect"
662 choose_server
&& return
665 # No servers specified. Find available servers and choose one of them.
666 # Remember which ssl certificate database was used to authenticate the chosen
668 ${stap_pkglibexecdir}stap-find-servers
$find_all > "$tmpdir_client/servers"
669 choose_server
&& return
670 num_servers
=`wc -l "$tmpdir_client/servers" | awk '{print $1}'`
673 if test $num_servers = 0; then
674 fatal
"Unable to find a server"
677 test -f "$tmpdir_client/connect" && cat "$tmpdir_client/connect" >&2
678 fatal
"Unable to connect to a server"
681 # function: choose_server
683 # Examine each line from "$tmpdir_client/servers" and attempt to connect to
684 # each server specified until successful.
685 function choose_server
{
686 local name ip port remain
688 while read -u3 name ip port remain
690 if test "X$name" = "X"; then
691 fatal
"Server name not provided by avahi"
694 # if test "X$ip" = "X"; then
695 # fatal "Server ip address not provided by avahi"
698 if test "X$port" = "X"; then
699 fatal
"Server port not provided by avahi"
702 # Does the server build for the kernel release and architecture that we want?
703 sysinfo
=`expr "$remain" : "'sysinfo=\\\(.*\\\)'"`
704 test "X$sysinfo" != "X$uname_r $arch" && continue
706 send_receive
$name $port && return
707 done 3< "$tmpdir_client/servers"
709 # Could not connect to a server
713 # function: send_receive SERVER PORT
715 # Connect to the server, send the request and receive the response.
716 function send_receive
{
720 # The server must match the dns name on the certificate
721 # and must be 'localhost' if the server is on the local host.
722 local server_host_name
=`expr "$server" : "\\\([a-zA-Z0-9-]*\\\).*"`
723 local server_domain_name
=`expr "$server" : "$server_host_name\\\(.*\\\)"`
725 if test "X$server_domain_name" = "X.local"; then
726 server_domain_name
=$our_domain_name
728 if test "X$server_host_name$server_domain_name" = "Xlocalhost$our_domain_name"; then
730 elif test "X$server_host_name$server_domain_name" = "X$our_host_name$our_domain_name"; then
733 server
=$server_host_name$server_domain_name
736 # Try to connect using each of the given local certificate databases in turn
739 for ssl_db
in $local_ssl_dbs
741 # Send the request and receive the response using stap-client-connect
742 attempt_connection
-i $zip_client -o $zip_server -d $ssl_db -p $port -h $server && return
744 # Try the next database, but give the server a chance to reset.
748 # Next, try the public certificate databases.
749 for ssl_db
in $public_ssl_dbs
751 # Send the request and receive the response using stap-client-connect
752 attempt_connection
-i $zip_client -o $zip_server -d $ssl_db -p $port -h $server && return
754 # Try the next database, but give the server a chance to reset.
758 # Could not connect using any of the certificate databases
762 # function: attempt_connection ARGS
764 # Attempt connection with the given server. Give the user a chance to
765 # trust the server if it is not already trusted
766 function attempt_connection
{
767 echo "Attempting connection with $server:$port using certificate database in '$ssl_db'" >> "$tmpdir_client/connect"
769 # Send the request and receive the response using stap-client-connect
770 ${stap_pkglibexecdir}stap-client-connect
"$@" >> "$tmpdir_client/connect" 2>&1 &
771 wait '%${stap_pkglibexecdir}stap-client-connect'
773 test $rc = 0 && return
775 # The connection failed. If it was because the server is not trusted, give
776 # the user a chance to decide whether to trust the server anyway.
777 # The prompt will not be printed and the read will quickly timeout if
778 # stdin is not from a terminal.
779 if test $rc = 2; then
780 # Output any connection messages generated thus far
781 cat "$tmpdir_client/connect" >&2
782 rm "$tmpdir_client/connect"
785 local prompt
="The server at $server:$port is not trusted based on the certificate database in '$ssl_db'
787 if test $session_only_prompt_done = 0; then
788 session_only_prompt_done
=1
789 prompt
="${prompt}Trust this server for for this session only? [y/N] "
790 read -t 30 -p "$prompt" response ||
echo n
791 if test "$response" = "y" -o "$response" = "Y"; then
792 ${stap_pkglibexecdir}stap-client-connect
"$@" -t session
>> "$tmpdir_client/connect" 2>&1 &
793 wait '%${stap_pkglibexecdir}stap-client-connect'
794 test $?
= 0 && return
795 return 1 # Connection failed
799 if test "$ssl_db" = "$stap_root_ssl_db/client"; then
800 prompt
="${prompt}Adding this server's certificate to this database will make this server trusted by all users on the local host.
803 prompt
="${prompt}Add this server's certificate to '$ssl_db'? [y/N] "
804 read -t 30 -p "$prompt" response ||
echo n
805 if test "$response" = "y" -o "$response" = "Y"; then
806 ${stap_pkglibexecdir}stap-client-connect
"$@" -t permanent
>> "$tmpdir_client/connect" 2>&1 &
807 wait '%${stap_pkglibexecdir}stap-client-connect'
808 test $?
= 0 && return
816 # function: process_response
818 # Write the stdout and stderr from the server to stdout and stderr respectively.
819 function process_response
{
820 # Pick up the results of running stap on the server.
824 if test $p_phase -ge 4; then
825 if test -f $tmpdir_stap/*.ko
; then
826 if test $p_phase = 4 -o "X$m_name" != "X"; then
827 cp -p $tmpdir_stap/*.ko
$wd/$module_name.ko
828 test -f $tmpdir_stap/*.sgn
&& cp -p $tmpdir_stap/*.sgn
$wd/$module_name.ko.sgn
830 module_name
=`ls $tmpdir_stap/*.ko`
831 module_name
=`expr "$module_name" : '\(.*\)\.ko'`
833 elif test "X$script_file" != "X" -o "X$e_script" != "X"; then
834 if test "X$rc" != "X" -a $rc = 0; then
836 fatal
"no module returned by the server"
841 # Change the name of the temp directory and module name in stdout and stderr
842 sed -i "s,stap_[0-9]\+,$module_name,g" $tmpdir_server/stdout
843 sed -i "s,stap_[0-9]\+,$module_name,g" $tmpdir_server/stderr
844 sed -i "s,into \".*$module_name,into \"$module_name,g" $tmpdir_server/stdout
845 sed -i "s,into \".*$module_name,into \"$module_name,g" $tmpdir_server/stderr
847 # Output stdout and stderr as directed
851 # function: stream_output
853 # Output stdout and stderr as directed
854 function stream_output
{
860 # function: maybe_call_staprun
862 # Call staprun using the module returned from the server, if requested.
863 function maybe_call_staprun
{
864 if test $rc != 0; then
865 # stap run on the server failed, so don't bother
869 if test $p_phase -ge 4; then
870 # There should be a systemtap temporary directory.
871 if test "X$tmpdir_stap" = "X"; then
872 # OK if no script specified
873 if test "X$e_script" != "X" -o "X$script_file" != "X"; then
874 fatal
"systemtap temporary directory is missing in server response"
879 if test $p_phase = 5; then
880 test $v_level -gt 0 && echo "Pass 5: starting run." >&2
882 # We have a module. Try to run it
883 # If a -c command was specified, pass it along.
884 if test "X$c_cmd" != "X"; then
885 staprun_opts
="-c '$c_cmd'"
888 # The -v level will be one less than what was specified
890 for ((vl
=$
((v_level
- 1)); $vl > 0; --vl))
892 staprun_opts
="$staprun_opts -v"
895 # if -o was specified, pass it along
896 if test "X$stdout_redirection" != "X"; then
897 staprun_opts
="$staprun_opts -o $stdout_redirection"
900 # Run it from our original working directory
903 # Run it in the background and wait for it. This
904 # way any signals sent to us can be caught.
905 if test $v_level -ge 2; then
906 echo "running `staprun_PATH` $staprun_opts $module_name.ko" >&2
908 eval `staprun_PATH` "$staprun_opts" $module_name.ko
911 # Wait until the job actually disappears so that its output is complete.
912 while jobs '%?staprun' >/dev
/null
2>&1
917 test $v_level -gt 0 && echo "Pass 5: run completed in 0usr/0sys/0real ms." >&2
922 # function: staprun_PATH
924 # Compute a PATH suitable for running staprun.
925 function staprun_PATH
{
926 # If $SYSTEMTAP_STAPRUN is set, then use that
927 if test "X$SYSTEMTAP_STAPRUN" != "X"; then
928 echo $SYSTEMTAP_STAPRUN
932 # Otherwise, if there is an exec_prefix, then use it.
933 if test "X$stap_exec_prefix" != "X"; then
934 echo ${stap_exec_prefix}staprun
938 # Otherwise, we have been called by the dejagnu test harness as 'stap'
939 # and we are the first 'stap' on the path. Since staprun may call
940 # 'stap', remove the PATH component where we live from the PATH in order to
942 local first_stap
=`which stap`
943 local PATH_component
=`dirname $first_stap`
944 echo "PATH=$PATH staprun" |
sed "s,$PATH_component,,g"
947 # function: check_db DBNAME [ EUID USER ]
949 # Check the security of the given database directory.
956 # Check that we have been given a directory
957 if ! test -e $dir; then
958 warning
"Certificate database '$dir' does not exist"
961 if ! test -d $dir; then
962 warning
"Certificate database '$dir' is not a directory"
966 # If euid has been specified, then this directory must be owned by that
968 if test "X$euid" != "X"; then
969 local ownerid
=`stat -c "%u" $dir`
970 if test "X$ownerid" != "X$euid"; then
971 warning
"Certificate database '$dir' must be owned by $user"
976 # Check that we can read the directory
977 if ! test -r $dir; then
978 warning
"Certificate database '$dir' is not readble"
982 # Check the access permissions of the directory
983 local perm
=0`stat -c "%a" $dir`
984 if test $
((($perm & 0400) == 0400)) = 0; then
985 warning
"Certificate database '$dir' should be readable by the owner"
987 if test $
((($perm & 0200) == 0200)) = 0; then
988 warning
"Certificate database '$dir' should be writeable by the owner"
990 if test $
((($perm & 0100) == 0100)) = 0; then
991 warning
"Certificate database '$dir' should be searchable by the owner"
993 if test $
((($perm & 0040) == 0040)) = 0; then
994 warning
"Certificate database '$dir' should be readable by the group"
996 if test $
((($perm & 0020) == 0020)) = 1; then
997 warning
"Certificate database '$dir' must not be writable by the group"
1000 if test $
((($perm & 0010) == 0010)) = 0; then
1001 warning
"Certificate database '$dir' should be searchable by the group"
1003 if test $
((($perm & 0004) == 0004)) = 0; then
1004 warning
"Certificate database '$dir' should be readable by others"
1006 if test $
((($perm & 0002) == 0002)) = 1; then
1007 warning
"Certificate database '$dir' must not be writable by others"
1010 if test $
((($perm & 0001) == 0001)) = 0; then
1011 warning
"Certificate database '$dir' should be searchable by others"
1014 # Now check the permissions of the critical files.
1015 check_db_file
$dir/cert8.db
$euid $user || rc
=1
1016 check_db_file
$dir/key3.db
$euid $user || rc
=1
1017 check_db_file
$dir/secmod.db
$euid $user || rc
=1
1019 test $rc = 1 && warning
"Unable to use certificate database '$dir' due to errors"
1024 # function: check_db_file FILENAME [ EUID USER ]
1026 # Check the security of the given database file.
1027 function check_db_file
{
1031 # Check that we have been given a file
1032 if ! test -e $file; then
1033 warning
"Certificate database file '$file' does not exist"
1036 if ! test -f $file; then
1037 warning
"Certificate database file '$file' is not a regular file"
1041 # If euid has been specified, then this directory must be owned by that
1043 if test "X$euid" != "X"; then
1044 local ownerid
=`stat -c "%u" $file`
1045 if test "X$ownerid" != "X$euid"; then
1046 warning
"Certificate database file '$file' must be owned by $user"
1051 # Check that we can read the file
1052 if ! test -r $file; then
1053 warning
"Certificate database file '$file' is not readble"
1057 # Check the access permissions of the file
1058 local perm
=0`stat -c "%a" $file`
1059 if test $
((($perm & 0400) == 0400)) = 0; then
1060 warning
"Certificate database file '$file' should be readable by the owner"
1062 if test $
((($perm & 0200) == 0200)) = 0; then
1063 warning
"Certificate database file '$file' should be writeable by the owner"
1065 if test $
((($perm & 0100) == 0100)) = 1; then
1066 warning
"Certificate database file '$file' must not be executable by the owner"
1069 if test $
((($perm & 0040) == 0040)) = 0; then
1070 warning
"Certificate database file '$file' should be readable by the group"
1072 if test $
((($perm & 0020) == 0020)) = 1; then
1073 warning
"Certificate database file '$file' must not be writable by the group"
1076 if test $
((($perm & 0010) == 0010)) = 1; then
1077 warning
"Certificate database file '$file' must not be executable by the group"
1080 if test $
((($perm & 0004) == 0004)) = 0; then
1081 warning
"Certificate database file '$file' should be readable by others"
1083 if test $
((($perm & 0002) == 0002)) = 1; then
1084 warning
"Certificate database file '$file' must not be writable by others"
1087 if test $
((($perm & 0001) == 0001)) = 1; then
1088 warning
"Certificate database file '$file' must not be executable by others"
1095 # function: warning [ MESSAGE ]
1098 # Prints its arguments to stderr
1100 echo "$0: WARNING:" "$@" >&2
1103 # function: fatal [ MESSAGE ]
1106 # Prints its arguments to stderr and exits
1108 echo "$0: ERROR:" "$@" >&2
1115 # Cleanup work files unless asked to keep them.
1119 if test $keep_temps != 1; then
1120 rm -fr $tmpdir_client
1123 rm -fr $tmpdir_server
1127 # function: terminate
1129 # Terminate gracefully.
1130 function terminate
{
1132 echo "$0: terminated by signal" >&2
1135 # Kill any running staprun job
1136 kill -s SIGTERM
'%?staprun' 2>/dev
/null
1138 # Kill any stap-client-connect job
1139 kill -s SIGTERM
'%${stap_pkglibexecdir}stap-client-connect' 2>/dev
/null
1144 # function: interrupt
1146 # Pass an interrupt (ctrl-C) to staprun
1147 function interrupt
{
1148 # Kill any stap-client-connect job
1149 # SIGINT won't do it.
1150 kill -s SIGTERM
'%${stap_pkglibexecdir}stap-client-connect' 2>/dev
/null
1152 # If staprun was not running, then exit.
1157 # function: ignore_signal
1159 # Called in order to ignore a signal
1160 function ignore_signal
{
1164 #-----------------------------------------------------------------------------
1165 # Beginning of main line execution.
1166 #-----------------------------------------------------------------------------
1171 find_and_connect_to_server