#!/bin/bash
#
# Start/Stop the CGroups Rules Engine Daemon
#
# Copyright Red Hat Inc. 2008
#
# Authors:	Steve Olivieri <sjo@redhat.com>
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2.1 of the GNU Lesser General Public License
# as published by the Free Software Foundation.
# 
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# cgred		CGroups Rules Engine Daemon
# chkconfig:	- 14 86
# description:	This is a daemon for automatically classifying processes \
#		into cgroups based on UID/GID.
#
# processname: cgrulesengd
# pidfile: /var/run/cgred.pid
#
### BEGIN INIT INFO
# Provides:		cgred
# Required-Start:	$local_fs $syslog cgconfig
# Required-Stop:	$local_fs $syslog
# Should-Start:		2 3 4 5
# Should-Stop:		0 1 6
# Short-Description:	start and stop the cgroups rules engine daemon
# Description:		CGroup Rules Engine is a tool for automatically using \
#			cgroups to classify processes
### END INIT INFO

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

# For convenience
PROCESSNAME=cgrulesengd
SERVICENAME=cgred
PIDFILE=/var/run/cgred.pid
LOCKFILE=/var/lock/subsys/$SERVICENAME
RETVAL=0

SourceIfNotEmpty /etc/sysconfig/$SERVICENAME

OPTIONS="$NODAEMON $LOG"
if [ -n "$LOG_FILE" ]; then
	OPTIONS="$OPTIONS --logfile=$LOG_FILE"
fi
if [ -n "$SOCKET_USER" ]; then
		OPTIONS="$OPTIONS -u $SOCKET_USER"
fi
if [ -n "$SOCKET_GROUP" ]; then
		OPTIONS="$OPTIONS -g $SOCKET_GROUP"
fi

start()
{
	start_daemon --make-pidfile --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --displayname "$SERVICENAME" --name "$SERVICENAME" --expect-user root -- $PROCESSNAME $OPTIONS
	RETVAL=$?
	# fix pid
	pidof cgrulesengd > "$PIDFILE"
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --displayname "$SERVICENAME" --expect-user root -- $PROCESSNAME
	RETVAL=$?
	return $RETVAL
}

reload()
{
	msg_reloading $SERVICENAME
	stop_daemon --pidfile "$PIDFILE" --displayname "$SERVICENAME" --expect-user root -HUP -- $PROCESSNAME
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}


# See how we are called
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --displayname "$SERVICENAME" --expect-user root -- $PROCESSNAME
		RETVAL=$?
		;;
	restart)
		restart
		;;
	reload)
		reload
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|reload|status|condrestart}"
		RETVAL=1
		;;
esac

exit $RETVAL
