• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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#
10warn()
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#
29assert ()                 #  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#
62pid_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#
74kill_pid()
75{
76    PID=$1
77    disown $PID
78    kill -9 $PID > /dev/null 2>&1
79}
80
81############################################################
82## Timing                                                 ##
83############################################################
84
85# Routines in this library are set up to allow timing to be done
86# by defining $TIME to a timing command.  You can define your
87# own handler by defining $TIME before or after including this
88# library.
89TIME=${TIME:-""}
90
91# Allows overriding the filename to use for storing time
92# measurements.  Required in order to
93TIME_TMP_FILE=${TIME_TMP_FILE:-"${TMP:-/tmp}/cpu_$$"}
94
95# perform_timings()
96#
97#  This turns on timings for operations that support timing
98#  via the $TIME variable.  It does this by setting $TIME to
99#  a general purpose time command.
100set_timing_on()
101{
102    TIME="/usr/bin/time -o $TIME_TMP_FILE -f \"%e\""
103}
104
105report_timing()
106{
107    MSG=${1:-"perform operation"}
108    if [ ! -z "${TIME}" ]; then
109        TM=`cat $TIME_TMP_FILE`
110        echo "Time to ${MSG} : $TM"
111    fi
112}
113
114############################################################
115## Interrupt handling and cleanup                         ##
116############################################################
117
118# do_clean()
119#
120#  Virtual function called by do_intr().  Override this to
121#  provide custom cleanup handling.
122#
123do_clean()
124{
125    return 0
126}
127
128# do_testsuite_clean()
129#
130#  Internal routine to do cleanup specific to other routines
131#  in this testsuite.  You may override this routine if you
132#  do not want this behavior.
133#
134do_testsuite_clean()
135{
136    /bin/rm -rf $TIME_TMP_FILE
137}
138
139# exit_clean(EXIT_CODE)
140#
141#  Replacement for exit command.  Prints the date, then calls do_clean
142#  and exits with the given $EXIT_CODE, or 0 if none specified.
143#
144exit_clean()
145{
146    EXIT_CODE=${1:-0}
147    date
148    do_clean
149    exit $EXIT_CODE
150}
151
152# do_intr()
153#
154#  Handler for trapped interrupts (i.e., signals 1 2 15).
155#
156#  This will result in a call do do_clean() when the user
157#  interrupts the test, allowing you to do whatever final
158#  cleanup work is needed (removing tmp files, restoring
159#  resources to initial states, etc.)  This routine will
160#  exit with error code 1 when done.
161#
162do_intr()
163{
164    echo "## Cleaning up... user interrupt"
165    do_testsuite_clean
166    do_clean
167    exit 1
168}
169
170trap "do_intr" 1 2 15
171
172