#!/bin/sh
#
#   InterScan VirusWall library for start/stop daemon.
#
PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH

DIS_TYPE="redhat"

# set DIS_TYPE to the correct LINUX distribution
if [ -f /etc/SuSE-release ] ; then
    DIS_TYPE="SuSE"
    DIS_VERSION='cat /etc/SuSE-release'
else
    DIS_TYPE="redhat"
    if [ -f /etc/redhat-release ] ; then
		DIS_VERSION='cat /etc/redhat-release'
    fi
fi

export DIS_TYPE

SYSNAME=`uname -s`
case ${SYSNAME} in	# We need pid,ppid and args of all processes
   "Linux")
	PS_ALL="ps axwo user,pid,ppid,args"
	;;
   "HP-UX")
	PS_ALL="ps -ef"	# -o, only XPG4
	;;
   "AIX")
	PS_ALL="ps -ef -o user,pid,ppid,args"
	;;
   *) # Solaris/Tru64
	PS_ALL="ps -elo user,pid,ppid,args" 
	;;
esac


isdaemon() {
  base=`basename $1`
  ${PS_ALL} | grep -v grep | grep $1>/dev/null 2>&1
  if [ $? = 0 ] ; then	# it's already runnig.
    return 0
  fi
  $@
  return $?
}

iskillproc() {
  if [ $# != 1 ] ; then
    echo "Usage: iskillproc program" 1>&2
    return 1
  fi

  PS_ID=`${PS_ALL} | grep $1 | grep -v grep | awk '{ if ($3 == "1" ) printf "%s ", $2 }' `
  if [ "$PS_ID" != "" ] ; then
    kill $PS_ID
    sleep 2
  fi

  PS_ID=`${PS_ALL} | grep $1 | grep -v grep | grep -v $$ | awk '{ printf "%s ", $2 }' `
  if [ "$PS_ID" != "" ] ; then
    kill $PS_ID
    sleep 2
    kill -KILL $PS_ID
  fi

}
