#!/bin/sh
#
#	HP High Performance Systems Division
#	(c)Copyright 1999 Hewlett-Packard Co.
#
#	$Revision: 5211 $
#
#	Function:	- MPI cleanup script
#
#	"@(#)HP MPI 02.02.05.00 (10/03/2006) Linux x86-64"
#

#
#	usage
#
#	Function:	- print usage information and exit
#
usage ()

{
	echo
	echo "Synopsis:        $prog [-v] -j <id> [<jid> ...]"
	echo "                 $prog [-v] -m"

	echo
	echo "Description:     Kill MPI processes."
	echo
	echo "Options:         -help         Print this help menu."
	echo "                 -v            Be verbose."
	echo "                 -m            Cleanup user's shared-memory \c"
	echo "segments."
	echo "                 -j <id>       Cleanup job # <id>."

	exit 1
}

#
#	verbmsg
#
#	Function:	- print out message if in verbose mode
#
verbmsg ()

{
	if [ $verbose -ne 0 ]; then echo "$1"; fi
}

#
#	killprocs
#
#	Function:	- kill named processes
#
killprocs ()

{
#
# Get process information.
# Store in temporary file to minimize # utilities running.
#
	/bin/rm -f $tmpfile
	ps -f -u $user 1> $tmpfile 2> /dev/null
	if [ $? -ne 0 ]; then
		echo "${prog}: ps failed"
		/bin/rm -f $tmpfile
		exit 1
	fi
#
# Find pids of matching command argv[0]s.
# The -f option is used to get the complete program name.
# There are two types of lines when -f is used:
#	user pid ppid pri mon day tty time command ...
#	user pid ppid pri stime tty time command ...
#
	pids=`awk '($5 ~ /[0-9]/ && $8 == "'$pname'") || \
		($5 !~ /[0-9]/ && $9 == "'$pname'") { print $2 } ' < $tmpfile`

	/bin/rm -f $tmpfile

	if [ -z "$pids" ]; then
		verbmsg "no \"$pname\" processes found"
	else
#
# Kill the processes.
#
		verbmsg "killing \"$pname\" processes: $pids"
		/bin/kill $pids
	fi
}

#
#	killjob
#
#	Function:	- kill processes of given job ID
#			- remote kills use remsh
#
killjob ()

{
#
# Check if it's a valid job.
#
	jobfile=$tmpdir/mpijob_${user}_${jid}
	if [ ! -f $jobfile ]; then
		echo "${prog}: job $jid does not exist for user \"$user\""
		return 1
	fi
#
# Sort processes by host and username.
# Terminate them using "kill" or "remsh kill".
#
	remsh=${MPI_REMSH:-/usr/bin/ssh}

	awk -F: -v remsh=$remsh -v v=$verbose -v hn=`hostname` ' \
	{ k = $1 "_" $2; h[k] = $1; u[k] = $2; c[k]++; p[k, c[k] ] = $3; } \
END	{ for (k in h) { \
		pid=""; for (i = 1; i <= c[k]; i++) pid = pid p[k, i] " "; \
		if (u[k] == "") uu = ""; else uu = "-l " u[k]; \
		hh = h[k]; if ((hh == "") || (hh == "localhost")) hh = hn; \
		if ((hh == hn) && (uu == "")) { \
			if (v == 1) print "killing local processes: " pid; \
			cmd = "/bin/kill " pid; system(cmd);
		} else { \
			if (v == 1) print "killing processes on \"" \
						hh "\": " pid; \
			cmd = remsh " " hh " " uu " /bin/kill " pid; \
			system(cmd); \
		} \
	  } \
	  exit 0 \
	} \
	' $jobfile
#
# Leave the job file around if not successful.
#
	if [ $? -ne 0 ]; then
		echo "cannot kill all processes, job $jid not deleted"
		return 1
	else
		verbmsg "job $jid deleted"
		/bin/rm -f $jobfile
	fi

	return 0
}

#
# main code
#
{
	PATH=/bin:/usr/bin:/usr/ucb
	prog=mpiclean

	user=`whoami`
	tmpdir=${MPI_TMPDIR:-/tmp}
	tmpfile=$tmpdir/mpiclean.$$
	verbose=0
	progs=""
	f_jid=0
	f_shm=0

#
# Loop parsing command line arguments.
#
	while [ $# -ne 0 ]; do
		case "$1" in
# help menu
		-help)	usage;;
# verbose mode
		-v)	verbose=1;;
# subcomplex ID
		-scid)	usage;;
# subcomplex name
		-sc)	usage;;
# job ID
		-j)	if [ $# -eq 1 ]; then usage; fi
			if [ "$progs" != "" ]; then usage; fi
			shift
			f_jid=1
			progs=$1;;
# shared-memory
		-m)	if [ "$progs" != "" ]; then usage; fi
			f_shm=1;;
# unknown option
		-*)	usage;;
# program name
		*)	progs="$progs $1";;
		esac

		if [ $# -ne 0 ]; then shift; fi
	done

#
# Do some error checking.
# Handle job ID and shared-memory cleaning separately.
#
	if [ $f_jid -eq 1 ]; then
		if [ $f_shm -eq 1 ]; then usage; fi

		gerr=0
		for jid in $progs; do
			if [ `expr $jid : \[0-9\]\*$` -eq 0 ]; then
				echo "${prog}: invalid job ID \"$jid\""
				gerr=1
			else
				killjob
				if [ $? -ne 0 ]; then gerr=1; fi
			fi
		done
		exit $gerr

	elif [ $f_shm -eq 1 ]; then
		if [ "$user" = "root" ]; then
			echo "${prog}: root not allowed to use -m"
			exit 1
		fi

		f_ipcrm=0
		for shmid in `ipcs | fgrep $user | awk '{ print $2 }'`; do
			verbmsg "deleting shmem ID $shmid"
			ipcrm -m $shmid
			f_ipcrm=1
		done

		if [ $f_ipcrm -eq 0 ]; then
			verbmsg "no shmem segments found for \"$user\""
		fi
		exit 0

	else
		usage
	fi

#
# Do error checking for program name cleaning mode.
#
	if [ -z "$progs" ]; then usage; fi
#
# Loop killing the named programs.
#
	for pname in $progs; do
		killprocs
	done

	exit 0
}
