demo_common.sh (5121B)
1 ## Common shell functions used by demo scripts programs/*/*.sh. 2 3 ## How to write a demo script 4 ## ========================== 5 ## 6 ## Include this file near the top of each demo script: 7 ## . "${0%/*}/demo_common.sh" 8 ## 9 ## Start with a "msg" call that explains the purpose of the script. 10 ## Then call the "depends_on" function to ensure that all config 11 ## dependencies are met. 12 ## 13 ## As the last thing in the script, call the cleanup function. 14 ## 15 ## You can use the functions and variables described below. 16 17 set -e -u 18 19 # Check if the provided path ($1) can be a valid root for Mbed TLS or TF-PSA-Crypto. 20 # This is based on the fact that "scripts/project_name.txt" exists. 21 is_valid_root () { 22 if ! [ -f "$1/scripts/project_name.txt" ]; then 23 return 1; 24 fi 25 return 0; 26 } 27 28 ## At the end of the while loop below $root_dir will point to the root directory 29 ## of the Mbed TLS or TF-PSA-Crypto source tree. 30 root_dir="${0%/*}" 31 ## Find a nice path to the root directory, avoiding unnecessary "../". 32 ## 33 ## The code supports demo scripts nested up to 4 levels deep. 34 ## 35 ## The code works no matter where the demo script is relative to the current 36 ## directory, even if it is called with a relative path. 37 n=4 38 while true; do 39 # If we went up too many folders, then give up and return a failure. 40 if [ $n -eq 0 ]; then 41 echo >&2 "This doesn't seem to be an Mbed TLS source tree." 42 exit 125 43 fi 44 45 if is_valid_root "$root_dir"; then 46 break; 47 fi 48 49 n=$((n - 1)) 50 case $root_dir in 51 .) root_dir="..";; 52 ..|?*/..) root_dir="$root_dir/..";; 53 ?*/*) root_dir="${root_dir%/*}";; 54 /*) root_dir="/";; 55 *) root_dir=".";; 56 esac 57 done 58 59 # Now that we have a root path we can source the "project_detection.sh" script. 60 . "$root_dir/framework/scripts/project_detection.sh" 61 62 ## msg LINE... 63 ## msg <TEXT_ORIGIN 64 ## Display an informational message. 65 msg () { 66 if [ $# -eq 0 ]; then 67 sed 's/^/# /' 68 else 69 for x in "$@"; do 70 echo "# $x" 71 done 72 fi 73 } 74 75 ## run "Message" COMMAND ARGUMENT... 76 ## Display the message, then run COMMAND with the specified arguments. 77 run () { 78 echo 79 echo "# $1" 80 shift 81 echo "+ $*" 82 "$@" 83 } 84 85 ## Like '!', but stop on failure with 'set -e' 86 not () { 87 if "$@"; then false; fi 88 } 89 90 ## run_bad "Message" COMMAND ARGUMENT... 91 ## Like run, but the command is expected to fail. 92 run_bad () { 93 echo 94 echo "$1 This must fail." 95 shift 96 echo "+ ! $*" 97 not "$@" 98 } 99 100 ## This check is temporary and it's due to the fact that we currently build 101 ## query_compile_time_config only in Mbed TLS repo and not in the TF-PSA-Crypto 102 ## one. Once we'll have in both repos this check can be removed. 103 has_query_compile_time_config () { 104 if ! [ -f "$1/programs/test/query_compile_time_config" ]; then 105 return 1; 106 fi 107 return 0; 108 } 109 110 if has_query_compile_time_config "$root_dir"; then 111 query_compile_time_config_dir="$root_dir/programs/test" 112 elif is_valid_root "$root_dir/.." && has_query_compile_time_config "$root_dir/.."; then 113 query_compile_time_config_dir="$root_dir/../programs/test" 114 else 115 query_compile_time_config_dir="" 116 fi 117 118 ## config_has SYMBOL... 119 ## Succeeds if the library configuration has all SYMBOLs set. 120 ## 121 ## Note: depending on the above check query_compile_time_config_dir might be 122 ## intentionally set to "". In this case the following function will fail. 123 config_has () { 124 for x in "$@"; do 125 # This function is commonly called in an if condition, where "set -e" 126 # has no effect, so make sure to stop explicitly on error. 127 "$query_compile_time_config_dir/query_compile_time_config" "$x" || return $? 128 done 129 } 130 131 ## depends_on SYMBOL... 132 ## Exit if the library configuration does not have all SYMBOLs set. 133 depends_on () { 134 if ! [ -f "$query_compile_time_config_dir/query_compile_time_config" ]; then 135 echo "query_compile_time_config is missing" 136 exit 127 137 fi 138 m= 139 for x in "$@"; do 140 if ! config_has "$x"; then 141 m="$m $x" 142 fi 143 done 144 if [ -n "$m" ]; then 145 cat >&2 <<EOF 146 $0: this demo requires the following 147 configuration options to be enabled at compile time: 148 $m 149 EOF 150 # Exit with a success status so that this counts as a pass for run_demos.py. 151 exit 152 fi 153 } 154 155 ## Add the names of files to clean up to this whitespace-separated variable. 156 ## The file names must not contain whitespace characters. 157 files_to_clean= 158 159 ## Call this function at the end of each script. 160 ## It is called automatically if the script is killed by a signal. 161 cleanup () { 162 rm -f -- $files_to_clean 163 } 164 165 166 167 ################################################################ 168 ## End of the public interfaces. Code beyond this point is not 169 ## meant to be called directly from a demo script. 170 171 trap 'cleanup; trap - HUP; kill -HUP $$' HUP 172 trap 'cleanup; trap - INT; kill -INT $$' INT 173 trap 'cleanup; trap - TERM; kill -TERM $$' TERM 174 175 if config_has MBEDTLS_ENTROPY_NV_SEED; then 176 # Create a seedfile that's sufficiently long in all library configurations. 177 # This is necessary for programs that use randomness. 178 # Assume that the name of the seedfile is the default name. 179 files_to_clean="$files_to_clean seedfile" 180 dd if=/dev/urandom of=seedfile ibs=64 obs=64 count=1 181 fi