# $Owl: Owl/packages/owl-startup/functions,v 1.16 2010/02/19 14:38:40 solar Exp $
#
# Functions defined in this file are used by many /etc/rc.d/init.d scripts;
# they offer some limited Red Hat compatibility.

# Export our version number to the scripts.
OWL_STARTUP_VERSION=3

# Do they recognize us?  Try to be more Red Hat compatible if not.
OWL_STARTUP_ENABLE=0

# We don't really support anything else, but scripts might check this.
BOOTUP=verbose

# The scripts may be run manually so we have to set this here.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

# Emulate which(1) internally to bash.
function absolute()
{
	local WHICH

	WHICH="`type -afp "$1"`" || return 1
	test "$WHICH" != "${WHICH##/}" || return 1

	echo "$WHICH"
}

# These versions of daemon(), killproc(), and status() are implemented in
# terms of start-stop-daemon.  This prevents most of the matching process
# name tricks that were possible on Red Hat systems.

function daemon()
{
	local WHICH BASENAME PIDFILE EXPECT SU NICE FLAGS STATUS

# Process options.
	PIDFILE=
	EXPECT=
	SU=
	NICE=0
	while [ "$1" != "${1##-}" -o "$1" != "${1##+}" ]; do
		case "$1" in
		--pidfile)
			shift
			PIDFILE="$1"
			shift
			;;
		--expect-user)
			shift
			EXPECT="$1"
			shift
			;;
		--user|--set-user)
			shift
			SU="$1"
			test -z "$EXPECT" && EXPECT="$SU"
			shift
			;;
		--)
			shift
			break
			;;
		-*|+*)
			NICE="$1"
			shift
			;;
		esac
	done

# We need the absolute pathname.
	WHICH="`absolute "$1"`" || return 1
	WHICH="`readlink -e "$WHICH"`" || return 1
	BASENAME="`basename "$1"`" || return 1

# Announce the action.
	if [ "$OWL_STARTUP_ENABLE" -ge 1 ]; then
		echo "Starting $BASENAME"
	fi

# Use a safe umask and don't do coredumps (this is redundant when running
# from rc.sysinit).
	umask 077
	ulimit -Sc 0

# Don't export these because they may be invalid under another UID and the
# directories may be gone while the daemon is still running.
	export -n HOME TMP TMPDIR

# Actually start the daemon.
	FLAGS="--start --exec $WHICH"
	test -n "$PIDFILE" && FLAGS="$FLAGS --pidfile $PIDFILE"
	test -n "$EXPECT" && FLAGS="$FLAGS --user $EXPECT"
	shift
	if [ -z "$SU" ]; then
		start-stop-daemon $FLAGS --startas /bin/nice -- \
			-n "$NICE" "$WHICH" $*
	else
		start-stop-daemon $FLAGS --startas /bin/nice -- \
			-n "$NICE" su - -s /bin/sh "$SU" -c "\"$WHICH\" $*"
	fi
	STATUS=$?

	if [ "$OWL_STARTUP_ENABLE" -eq 0 -a $STATUS -eq 0 ]; then
		echo -n " $BASENAME"
	fi

	return $STATUS
}

function killproc()
{
	local NAME WHICH BASENAME PIDFILE EXPECT DELAY SIGNAL DEFAULT FLAGS
	local STATUS

# Process options.
	NAME=
	PIDFILE=
	EXPECT=
	DELAY=3
# start-stop-daemon wants a signal number.
	SIGNAL="`kill -l TERM`"
	DEFAULT=true
	while [ $# -ge 1 ]; do
		case "$1" in
		--pidfile|-p)
			shift
			PIDFILE="$1"
			shift
			;;
		--expect-user)
			shift
			EXPECT="$1"
			shift
			;;
		-d)
			shift
			DELAY="$1"
			shift
			;;
		-[0-9]*)
			SIGNAL="${1##-}"
			DEFAULT=false
			shift
			;;
		--)
			shift
			test -z "$NAME" || return 1
			NAME="$1"
			break
			;;
		-*)
			SIGNAL="${1##-}"
			SIGNAL="`kill -l "$SIGNAL"`"
			DEFAULT=false
			shift
			;;
# Allow the process name to be in any argument for Red Hat compatibility.
		*)
			test -z "$NAME" || return 1
			NAME="$1"
			shift
			;;
		esac
	done

# We need the absolute pathname for /proc/*/exe checks.
	WHICH="`absolute "$NAME"`" || return 1
	BASENAME="`basename "$NAME"`" || return 1

# Try to guess the PID file if one isn't specified manually.
	if [ -z "$PIDFILE" ]; then
		PIDFILE="/var/run/$BASENAME.pid"
		test -f "$PIDFILE" || PIDFILE=
	elif [ "$PIDFILE" = "none" ]; then
		PIDFILE=
	fi

# Announce the action.
	if [ "$OWL_STARTUP_ENABLE" -ge 1 ]; then
		if [ "$DEFAULT" = "true" ]; then
			echo "Stopping $BASENAME"
		else
			echo "Sending `kill -l "$SIGNAL"` to $BASENAME"
		fi
	fi

# Actually tell the daemon to stop.
	FLAGS="--stop --exec $WHICH"
	test -n "$PIDFILE" && FLAGS="$FLAGS --pidfile $PIDFILE"
	test -n "$EXPECT" && FLAGS="$FLAGS --user $EXPECT"
	start-stop-daemon $FLAGS --signal "$SIGNAL"
	STATUS=$?

# We really want to be sure the daemon is dead, unless we're sending a
# custom signal.
	if [ "$DEFAULT" = "true" -a $STATUS -eq 0 ]; then
		usleep 100000
		if start-stop-daemon $FLAGS --test > /dev/null; then
			sleep 1
			if start-stop-daemon $FLAGS --test > /dev/null; then
				sleep "$DELAY"
				SIGNAL="`kill -l KILL`"
				start-stop-daemon $FLAGS --signal "$SIGNAL" \
				    > /dev/null
			fi
		fi
		test -n "$PIDFILE" && rm -f "$PIDFILE"
	fi

	if [ "$OWL_STARTUP_ENABLE" -eq 0 -a $STATUS -eq 0 ]; then
		echo -n " $BASENAME"
	fi

	return $STATUS
}

function status()
{
	local WHICH BASENAME PIDFILE EXPECT FLAGS

# Process options.
	PIDFILE=
	EXPECT=
	while [ "$1" != "${1##-}" ]; do
		case "$1" in
		--pidfile|-p)
			shift
			PIDFILE="$1"
			shift
			;;
		--expect-user)
			shift
			EXPECT="$1"
			shift
			;;
		--)
			shift
			break
			;;
		*)
			return 1
			;;
		esac
	done

# We need the absolute pathname for /proc/*/exe checks.
	WHICH="`absolute "$1"`" || return 1
	BASENAME="`basename "$1"`" || return 1

# Try to guess the PID file if one isn't specified manually.
	if [ -z "$PIDFILE" ]; then
		PIDFILE="/var/run/$BASENAME.pid"
		test -f "$PIDFILE" || PIDFILE=
	elif [ "$PIDFILE" = "none" ]; then
		PIDFILE=
	fi

# Actually determine the status.
	FLAGS="--stop --test --exec $WHICH"
	test -n "$PIDFILE" && FLAGS="$FLAGS --pidfile $PIDFILE"
	test -n "$EXPECT" && FLAGS="$FLAGS --user $EXPECT"
	start-stop-daemon $FLAGS > /dev/null

# Report it.
	if [ $? -eq 0 ]; then
		echo "$BASENAME is running"
		return 0
	fi

	if [ -n "$PIDFILE" -a -f "$PIDFILE" ]; then
		echo "$BASENAME is dead, but stale PID file exists"
		return 1
	fi

	if [ -f /var/lock/subsys/$1 ]; then
		echo "$BASENAME is dead, but subsystem is locked"
		return 2
	fi

	echo "$BASENAME is stopped"
	return 3
}

# This function is inherently unreliable and is provided for Red Hat
# compatibility only.
function pidofproc()
{
	local PID

	test $# -eq 1 || return 1

	if [ -r "/var/run/$1.pid" ]; then
		PID="`head -1 "/var/run/$1.pid"`"
		if [ -n "$PID" ]; then
			echo "$PID"
			return 0
		fi
	fi

	PID="`pidof -o $$ -o "$PPID" -x "$1"`"
	test -n "$PID" || return 1
	echo "$PID"
}

# More compatibility functions.  These were introduced in Red Hat 6.0.

function echo_success()
{
	return 0
}

function echo_failure()
{
	return 1
}

function echo_passed()
{
	return 1
}

function echo_warning()
{
	return 1
}

function success()
{
	return 0
}

function failure()
{
	return $?
}

function passed()
{
	return $?
}

function warning()
{
	return $?
}

function action()
{
	local STATUS

	echo -n "$1 "
	shift
	$*
	STATUS=$?
	echo
	return $STATUS
}
