#!/bin/sh
#
# hp_functions	This file contains functions to be used by most or all
#		shell scripts of the HP services.
#
# Version:	/usr/hpserver/etc/functions 1.00 27-Jun-2001
#
# Author:	Rita de Cassia Machado, <rita@tteng.com.br>
#

# A function to stop a program.
hp_killproc() {
	RC=0
	# Test syntax.
	if [ $# = 0 ]; then
		echo "Usage: killproc {program} [signal]"
		return 1
	fi

	notset=0
	# check for second arg to be kill level
	if [ "$2" != "" ] ; then
		killlevel=$2
	else
		notset=1
		killlevel="-9"
	fi

        # Save basename.
        base=`basename $1`

        # Find pid.
	# RedHat
        # pidlist=`hp_pidofproc $base`
	# Caldera
        pidlist=`hp_pidofproc $1`
	
	pid=
	for apid in $pidlist ; do
	   [ -d /proc/$apid ] && pid="$pid $apid"
	done

        # Kill it.
        if [ "$pid" != "" ] ; then
                # [ $BOOTUP = "verbose" ] && echo -n "$base "
		if [ "$notset" = "1" ] ; then
		       if ps h $pid>/dev/null 2>&1; then
			   # TERM first, then KILL if not dead
			   kill -TERM $pid
			   usleep 100000
			   if ps h $pid >/dev/null 2>&1 ; then
				sleep 1
				if ps h $pid >/dev/null 2>&1 ; then
				        sleep 3
					if ps h $pid >/dev/null 2>&1 ; then
					   kill -KILL $pid
					fi
				fi
			   fi
		        fi
			ps h $pid >/dev/null 2>&1
			RC=$?
			# [ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
			RC=$((! $RC))
		# use specified level only
		else
		        if ps h $pid >/dev/null 2>&1; then
	                	kill $killlevel $pid
				RC=$?
				# [ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
			fi
		fi
	else
	    # failure "$base shutdown"
	    RC=1
	fi

        # Remove pid file if any.
	if [ "$notset" = "1" ]; then
            rm -f /var/run/$base.pid
	fi
	return $RC
}

# A function to find the pid of a program.
hp_pidofproc() {
	# Test syntax.
	if [ $# = 0 ] ; then
		echo "Usage: hp_pidofproc {program}"
		return 1
	fi


        # Save basename.
        base=`basename $1`

	# First try "/var/run/*.pid" files
	if [ -f /var/run/$base.pid ] ; then
	        pid=`head -1 /var/run/$base.pid`
	        if [ "$pid" != "" ] ; then
	                echo $pid
	                return 0
	        fi
	fi

	# Next try "pidof"
	# RedHat
	# pid=`pidof -o $$ -o $PPID -o %PPID -x $1`
	# Caldera
	pid=`pidof $1`

	if [ "$pid" != "" ] ; then
	        echo $pid
	        return 0
	fi
}

_status() {
  [ -z "$1" ] || local pidf="$1"
  local ret=-1
  local pid
  if [ -n "$pidf" ] && [  -r "$pidf" ]; then
    pid=$(head -1 $pidf)
  else
    pid=$(pidof $NAME)
  fi

  if [ ! -e /var/lock/subsys/$NAME ]; then
    # no lock-file => not started == stopped?
    ret=3
  elif { [ -n "$pidf" ] && [ ! -f "$pidf" ] } || [ -z "$pid" ]; then
    # pid-file given but not present or no pid => died, but was not stopped
    ret=2
  elif [ -r /proc/$pid/cmdline ] &&
       echo -ne $PATHNAME'\000' | cmp -s - /proc/$pid/cmdline; then
    # pid-file given and present or pid found => check process...
    # but don't compare exe, as this will fail after an update!
    # compares OK => all's well, that ends well...
    ret=0
  else
    # no such process or exe does not match => stale pid-file or process died
    #   just recently...
    ret=1
  fi
  return $ret
}





