#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2005                 ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   killall_udp_traffic
#
# Description:
#   Kill all of the udp traffic utilities (ns-udpserver, ns-udpclient)
#
# Arguments:
#   None
#
# Returns:
#   None
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# History:
#   Oct 19 2005 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

# Make sure the value of LTPROOT
LTPROOT=${LTPROOT:-`(cd ../../../../ ; pwd)`}
export LTPROOT

# Check the environmanet variable for the test
. check_envval || exit 1

# Waiting time before outputting a warning message [sec]
WARN_WAIT=300


# Send SIGHUP both server and client
killall -SIGHUP ns-udpserver >/dev/null 2>&1
$LTP_RSH $RHOST "killall -SIGHUP ns-udpclient" >/dev/null 2>&1

# Verify the server is dead.
start_epoc=`date +%s`
while true ; do
    ps auxw | fgrep -v grep | fgrep -l ns-udpserver >/dev/null 2>&1
    if [ $? -ne 0 ]; then
	break
    fi

    current_epoc=`date +%s`
    elapse_epoc=`expr $current_epoc - $start_epoc`
    if [ $elapse_epoc -ge $WARN_WAIT ]; then
	tst_resm TINFO "UDP traffic server is not dead over $WARN_WAIT sec" >&2
    fi

    killall -SIGHUP ns-udpserver >/dev/null 2>&1
    sleep 1
done

# Verify the client is dead.
start_epoc=`date +%s`
while true ; do
    #ret=`$LTP_RSH $RHOST 'ps auxw | fgrep -v grep | grep -l '[[:blank:]]ns-udpclient[[:blank:]]' >/dev/null 2>&1; echo $?'`
    ret=`$LTP_RSH $RHOST 'ps auxw | fgrep -v grep | grep -l '/ns-udpclient[[:blank:]]' >/dev/null 2>&1; echo $?'`

    if [ -z $ret ]; then
	continue
    fi

    if [ $ret -ne 0 ]; then
	break
    fi

    current_epoc=`date +%s`
    elapse_epoc=`expr $current_epoc - $start_epoc`
    if [ $elapse_epoc -ge $WARN_WAIT ]; then
	tst_resm TINFO "UDP traffic client is not dead over $WARN_WAIT sec" >&2
    fi
    $LTP_RSH $RHOST "killall -SIGHUP ns-udpclient" >/dev/null 2>&1
    sleep 1
done