On 12/02/2014 01:38 PM, Cyril Hrubis wrote: > Hi! >> Function tst_timeout() added into LTP test interface: >> >> tst_timeout "command arg1 arg2 ..." timeout >> >> Function enables waiting for specified command for timeout >> seconds. Example usage: >> >> cmd_output=$(tst_timeout "ping -c 3 localhost" 5) >> if [ $? -ne 0 ]; then >> tst_brkm TBROK "timeout reached!" >> fi >> >> where $cmd_output contains stdout and stderr of ping command. > > Nice idea, comments below. > >> Signed-off-by: Matus Marhefka <mmarh...@redhat.com> >> --- >> testcases/lib/test.sh | 54 >> +++++++++++++++++++++++++++++++++++++++++++++++++++ >> 1 file changed, 54 insertions(+) >> >> diff --git a/testcases/lib/test.sh b/testcases/lib/test.sh >> index eecbfba..1c5bdf2 100644 >> --- a/testcases/lib/test.sh >> +++ b/testcases/lib/test.sh >> @@ -129,6 +129,60 @@ tst_check_cmds() >> done >> } >> >> +# tst_timeout "command arg1 arg2 ..." timeout >> +# Runs command for specified timeout (in seconds). >> +# Function returns retcode of command or 1 if arguments are invalid. >> +tst_timeout() >> +{ >> + local command=$1 >> + local timeout=$2 >> + local usleep_time=100000 >> + >> + # command must be non-empty string with command to run >> + if [ -z "$command" ]; then >> + echo "first argument must be non-empty string" >> + return 1 >> + fi >> + >> + # accept only numbers as timeout >> + re='^[0-9]+$' >> + if ! [[ $timeout =~ $re ]]; then > > This is bashism. The portable way to do this seems to be do > tr -d [:digit:] on the string and checking if result is empty or use > grep.
Indeed, 'tr' may be used in this case, but an (AFAIK) portable replacement for =~ is expr(1): if ! expr "$timeout" : '.*[0-9]\+$' >/dev/null; then although, as you can see, there are several limitations: - IIRC the busybox version doesn't support '+ TOKEN' syntax, so we rely on ie. $timeout != "match" - different implementations may have different syntax, although the `:' notation is AFAIK the oldest - regexp matching is always done from the beginning, so there's `^' implied - path to /dev/null may be different on some systems (_PATH_DEVNULL), not sure if "/dev/null" is a POSIX requirement > >> + echo "only numbers as second argument" >> + return 1 >> + fi >> + >> + if [ -z "$TMPDIR" ]; then >> + TMPDIR="/tmp" >> + fi > > I guess that better solution would be to move this check from the > tst_tmpdir() after the LTPROOT check in the test.sh so that we have only > one copy of it. > >> + local output=$(mktemp --tmpdir=$TMPDIR) >> + sh -c "eval $command" >$output 2>&1 & > > Do we really need to redirect the output to the file here? Shouldn't > just redirecting stderr to stdout work? > >> + local pid=$! >> + timeout=$((timeout*1000000)) >> + while [ $timeout -gt 0 ]; do >> + kill -s 0 $pid 2>/dev/null >> + if [ $? -ne 0 ]; then >> + break >> + fi >> + timeout=$((timeout - usleep_time)) >> + usleep $usleep_time > > The usleep is bashism as well. What is not POSIX but tend to work is > sleep with a real number, i.e. sleep 0.1 but it does not seems to be > supported under BussyBox. > > So we can either do the polling once a second or create our own > tst_usleep C source that calls usleep(). > > Ah and I've found that coreutils even includes timeout command that > can be used to fairly simplify the code. > > Try: > > timeout $timeout sh -c "eval $command" > echo $? > >> + done >> + >> + local ret=0 >> + if [ $timeout -le 0 ]; then >> + ret=128 >> + kill -9 $pid >> + >> + fi >> + >> + wait $pid >> + ret=$((ret | $?)) >> + >> + cat $output >> + rm -f $output >> + >> + return $ret >> +} >> + >> # Check that test name is set >> if [ -z "$TCID" ]; then >> tst_brkm TBROK "TCID is not defined" >> -- >> 1.8.3.1 >> >> >> ------------------------------------------------------------------------------ >> Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server >> from Actuate! Instantly Supercharge Your Business Reports and Dashboards >> with Interactivity, Sharing, Native Excel Exports, App Integration & more >> Get technology previously reserved for billion-dollar corporations, FREE >> http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk >> _______________________________________________ >> Ltp-list mailing list >> Ltp-list@lists.sourceforge.net >> https://lists.sourceforge.net/lists/listinfo/ltp-list > ------------------------------------------------------------------------------ Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server from Actuate! Instantly Supercharge Your Business Reports and Dashboards with Interactivity, Sharing, Native Excel Exports, App Integration & more Get technology previously reserved for billion-dollar corporations, FREE http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk _______________________________________________ Ltp-list mailing list Ltp-list@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/ltp-list