#!/bin/bash
#
# Start/Stop the CGroups Rules Engine Daemon
#
# Copyright Red Hat Inc. 2008
#
# Copyright (C) 2008,2010 Jiri Slaby <jslaby@suse.cz>
#
# 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:		cgrulesengd
# Required-Start:	$local_fs $remote_fs $syslog cgconfig
# Required-Stop:	$local_fs $remote_fs $syslog cgconfig
# Should-Start:		
# Should-Stop:		
# Default-Start:	2 3 5
# Default-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

prefix=/usr;exec_prefix=/usr;sbindir=/usr/sbin
CGRED_BIN=$sbindir/cgrulesengd
CGRED_CONF=/etc/cgrules.conf

# Sanity checks
[ -x $CGRED_BIN ] || exit 1

. /lib/lsb/init-functions

# Read in configuration options.
if [ -f "/etc/sysconfig/cgred" ] ; then
	. /etc/sysconfig/cgred
	OPTIONS="$CGRED_OPTIONS"
	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
else
	OPTIONS=""
fi

# For convenience
processname=cgrulesengd
servicename=cgred
lockfile="/run/$servicename"
pidfile=/var/run/cgred.pid

start()
{
	if [ -f "$lockfile" ]; then
		pidofproc -k -p $pidfile $CGRED_BIN
		if [ $? -eq 7 ]; then
			log_warning_msg "Removing stale lock file $lockfile"
			rm -f "$lockfile" "$pidfile"
		else
			log_failure_msg "$servicename is already running with PID `cat ${pidfile}`"
			return 1
		fi
	fi
	num=`grep "cgroup" /proc/mounts | awk '$3=="cgroup"' | wc -l`
	if [ $num -eq 0 ]; then
		echo
		log_failure_msg $"Cannot find cgroups, is cgconfig service running?"
		return 1
	fi
	start_daemon -p $pidfile $CGRED_BIN $OPTIONS
	retval=$?
	if [ $retval -ne 0 ]; then
		return 7
	fi
	touch "$lockfile"
	if [ $? -ne 0 ]; then
		return 1
	fi
	pidofproc $CGRED_BIN > $pidfile
	return 0
}

stop()
{
	if [ ! -f $pidfile ]; then
		log_success_msg
		return 0
	fi
	killproc -p $pidfile -TERM "$processname"
	retval=$?
	if [ $retval -ne 0 ]; then
		return 1
	fi
	rm -f "$lockfile" "$pidfile"
	return 0
}

RETVAL=0

# See how we are called
case "$1" in
	start)
		echo -n "Starting CGroup Rules Engine Daemon"
		start
		RETVAL=$?
		rc_status -v
		;;
	stop)
		echo -n "Stopping CGroup Rules Engine Daemon"
		stop
		RETVAL=$?
		rc_status -v
		;;
	status)
		echo -n "Checking for CGroup Rules Engine Daemon"
		checkproc -k -p $pidfile $CGRED_BIN
		RETVAL=$?
		if [ $RETVAL -eq 7 ] ; then
			rc_failed 3
			RETVAL=3
		fi
		rc_status -v
		;;
	restart)
		stop
		start
		RETVAL=$?
		rc_status
		;;
	condrestart)
		if [ -f "$lockfile" ]; then
			stop
			start
			RETVAL=$?
		fi
		rc_status
		;;
	reload|flash)
		if [ -f "$lockfile" ]; then
			echo -n "Reloading rules configuration..."
			killproc -SIGUSR2 -p $pidfile $CGRED_BIN
		else
			echo "$servicename is not running."
			rc_failed 7
		fi
		rc_status -v
		;;
	*)
		echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
		RETVAL=2
		;;
esac

exit $RETVAL
