#!/bin/sh

#
# Copyright 1998 VMware, Inc.  All rights reserved.
#

# vmnet:	Manages the services needed to run VMware networking
#
# description:	Manages the services needed to run VMware networking
#
# chkconfig:	235 91 7
#
# probe:	true
# hide:		true

subsys=vmwguests
driver=vmmon
vnet=vmnet
bridge=vmnet-bridge
dhcpd=vmnet-dhcpd
netifup=vmnet-netifup
natd=vmnet-natd
ping=vmware-ping
smbd=vmware-smbd
nmbd=vmware-nmbd
serverd=vmware-serverd
vmx=vmware-vmx
max_wait=60

vmware_etc_dir=/etc/vmware
VM_BINDIR=/usr/bin
VM_SBINDIR=/usr/sbin

# Source config files
# System wide functions
. /etc/rc.d/init.d/functions

# Count the number of running virtual machines by looking at the number of references
# to the $driver module.
countVMs() {
	# Beware of module dependancies here. An exact match is important
	/sbin/lsmod | awk 'BEGIN {n = 0;} $1 == "'"$driver"'" {n = $3;} END {print n;}'
}

# Terminate a process synchronously
vmware_synchrone_kill() {
	local pid="$1"    # IN
	local signal="$2" # IN
	local second

	kill -"$signal" "$pid"
	# Wait a bit to see if the dirty job has really been done
	second=0
	while [ "$second" -lt "$max_wait" ]; do
		if [ ! -d /proc/"$pid" ]; then
			# Success
			return 0
		fi
		sleep 1
		second=`expr "$second" + 1`
	done
	# Timeout
	return 1
}

# Kill the process associated to a pidfile
vmware_stop_pidfile() {
	local pidfile="$1" # IN
	local pid

	pid=`cat "$pidfile" 2>/dev/null`
	if [ "$pid" = '' ]; then
		# The file probably does not exist or is empty. Success
		return 0
	fi
	# Keep only the first number we find, because some Samba pid files are really
	# trashy: they end with NUL characters
	# There is no double quote around $pid on purpose
	set -- $pid
	pid="$1"
	# First try a nice SIGTERM
	if vmware_synchrone_kill "$pid" 15; then
		return 0
	fi
	# Then send a strong SIGKILL
	if vmware_synchrone_kill "$pid" 9; then
		return 0
	fi
	return 1
}

# See how we were called.
case "$1" in
  start)
	if [ -f /var/lock/subsys/"$subsys" ]; then
		msg_already_running "VMware guests"
	fi	
	msg_starting 'VMWare guests'
	busy
	"$VM_SBINDIR"/"$serverd" -s -d
	[ "$?" -eq "0" ] && ok || fail
	touch /var/lock/subsys/"$subsys"
	;;

  stop)
	serverd_pid=`pidof $serverd`
	if [ -n "$serverd_pid" ]; then
		msg_stopping 'VMWare guests [SIGTERM]'
		busy
		vmware_synchrone_kill "$serverd_pid" "15"
		[ "$?" -eq "0" ] && ok || fail
	else
		echo 'VMware serverd is not running.'
	fi
	serverd_pid=`pidof $serverd`
	if [ -n "$serverd_pid" ]; then
		msg_stopping 'VMWare guests [SIGKILL]'
		busy
		vmware_synchrone_kill "$serverd_pid" "9"
		[ "$?" -eq "0" ] && ok || fail
	fi
	pidof -s $vmx | while read vmx_pid; do
		echo "Killing orphan guest(s): $vmx_pid"
		kill -9 $vmx_pid
	done
	rm -f /var/lock/subsys/"$subsys"
	;;
  status)
	serverd_pid=`pidof $serverd`
	if [ -n "$serverd_pid" ]; then
		echo 'VMware serverd is running.'
	else
		echo 'VMware serverd is not running.'
	fi
	if [ "`countVMs`" -gt 0 ]; then
		echo 'At least one VMware guest is still running.'
	fi
	echo
	;;
  restart)
	"$0" stop && "$0" start
	;;
  *)
	echo "Usage: `basename "$0"` {start|stop|status|restart}"
	exit 3
esac

exit 0
