1 #!/bin/sh 2 ############################################################ 3 ## Convenience functions for reporting, asserting, etc. ## 4 ############################################################ 5 6 # warn(TEXT) 7 # 8 # Issues a warning message to stderr 9 # 10 warn() 11 { 12 echo $1 1>&2 13 } 14 15 # assert() 16 # 17 # Basic assertion support. Use it like this: 18 # 19 # a=5 20 # b=4 21 # condition="$a -lt $b" # Error message and exit from script. 22 # # Try setting "condition" to something else, 23 # #+ and see what happens. 24 # 25 # assert "$condition" $LINENO 26 # 27 # Note that $LINENO is a built-in 28 # 29 assert () # If condition false, 30 { #+ exit from script with error message. 31 E_PARAM_ERR=98 32 E_ASSERT_FAILED=99 33 34 35 if [ -z "$2" ] # Not enough parameters passed. 36 then 37 return $E_PARAM_ERR # No damage done. 38 fi 39 40 lineno=$2 41 42 if [ ! $1 ] 43 then 44 echo "Assertion failed: \"$1\"" 45 echo "File \"$0\", line $lineno" 46 exit $E_ASSERT_FAILED 47 # else 48 # return 49 # and continue executing script. 50 fi 51 } 52 53 ############################################################ 54 ## Process management ## 55 ############################################################ 56 57 # pid_is_valid(PID) 58 # 59 # Checks if the given $PID is still running. Returns a true value if 60 # it is, false otherwise. 61 # 62 pid_is_valid() 63 { 64 PID=$1 65 ps --pid ${PID} --no-header | grep ${PID} 66 return $? 67 } 68 69 # kill_pid(PID) 70 # 71 # Forcibly kills the process ID and prevents it from 72 # displaying any messages (to stdout, stderr, or otherwise) 73 # 74 kill_pid() 75 { 76 PID=$1 77 kill -9 $PID > /dev/null 2>&1 78 } 79 80 ############################################################ 81 ## Timing ## 82 ############################################################ 83 84 # Routines in this library are set up to allow timing to be done 85 # by defining $TIME to a timing command. You can define your 86 # own handler by defining $TIME before or after including this 87 # library. 88 TIME=${TIME:-""} 89 90 # Allows overriding the filename to use for storing time 91 # measurements. Required in order to 92 TIME_TMP_FILE=${TIME_TMP_FILE:-"${TMP:-/tmp}/cpu_$$"} 93 94 # perform_timings() 95 # 96 # This turns on timings for operations that support timing 97 # via the $TIME variable. It does this by setting $TIME to 98 # a general purpose time command. 99 set_timing_on() 100 { 101 TIME="/usr/bin/time -o $TIME_TMP_FILE -f \"%e\"" 102 } 103 104 report_timing() 105 { 106 MSG=${1:-"perform operation"} 107 if [ ! -z "${TIME}" ]; then 108 TM=`cat $TIME_TMP_FILE` 109 echo "Time to ${MSG} : $TM" 110 fi 111 } 112 113 ############################################################ 114 ## Interrupt handling and cleanup ## 115 ############################################################ 116 117 # do_clean() 118 # 119 # Virtual function called by do_intr(). Override this to 120 # provide custom cleanup handling. 121 # 122 do_clean() 123 { 124 return 0 125 } 126 127 # do_testsuite_clean() 128 # 129 # Internal routine to do cleanup specific to other routines 130 # in this testsuite. You may override this routine if you 131 # do not want this behavior. 132 # 133 do_testsuite_clean() 134 { 135 /bin/rm -rf $TIME_TMP_FILE 136 } 137 138 # exit_clean(EXIT_CODE) 139 # 140 # Replacement for exit command. Prints the date, then calls do_clean 141 # and exits with the given $EXIT_CODE, or 0 if none specified. 142 # 143 exit_clean() 144 { 145 EXIT_CODE=${1:-0} 146 date 147 do_clean 148 exit $EXIT_CODE 149 } 150 151 # do_intr() 152 # 153 # Handler for trapped interrupts (i.e., signals 1 2 15). 154 # 155 # This will result in a call do do_clean() when the user 156 # interrupts the test, allowing you to do whatever final 157 # cleanup work is needed (removing tmp files, restoring 158 # resources to initial states, etc.) This routine will 159 # exit with error code 1 when done. 160 # 161 do_intr() 162 { 163 echo "## Cleaning up... user interrupt" 164 do_testsuite_clean 165 do_clean 166 exit 1 167 } 168 169 trap "do_intr" 1 2 15 170 171