#!/bin/sh
#
# restorecond:		Daemon used to maintain path file context
#
# chkconfig:	- 12 87
# description:	restorecond uses inotify to look for creation of new files \
# listed in the /etc/selinux/restorecond.conf file, and restores the \
# correct security context.
#
# processname: /usr/sbin/restorecond
# config: /etc/selinux/restorecond.conf 
# pidfile: /run/restorecond.pid
#
# Return values according to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running

PATH=/sbin:/bin:/usr/bin:/usr/sbin

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

[ -f /etc/sysconfig/restorecond ] && . /etc/sysconfig/restorecond

[ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled || exit 7

# Check that we are root ... so non-root users stop here
if [ $EUID  ]; then
	test $EUID = 0  || exit 4
else
	test `id -u` = 0  || exit 4
fi

test -f /etc/selinux/restorecond.conf  || exit 6

RETVAL=0

start() 
{
	if [ -f /var/lock/subsys/restorecond ]; then
		msg_already_running "restorecond"
		return
	fi
        msg_starting "restorecond"
	unset HOME MAIL USER USERNAME
        daemon /usr/sbin/restorecond
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/restorecond
}

stop() 
{
	if [ ! -f /var/lock/subsys/restorecond ]; then
		msg_not_running "restorecond"
		return
	fi

        msg_stopping "restorecond"
	killproc restorecond
	RETVAL=$?
	rm -f  /var/lock/subsys/restorecond
}

restart() 
{
    stop
    start
}

condrestart()
{
	if [ ! -f /var/lock/subsys/restorecond ]; then
		msg_not_running "restorecond"
		RETVAL=$1
		return
	fi
	restart
}

# See how we were called.
case "$1" in
  start)
	start
        ;;
  stop)
	stop
        ;;
  status)
	status restorecond
	RETVAL=$?
	;;
  restart)
	restart
	;;
  force-reload)
	condrestart 7
	;;
  try-restart|condrestart)
	condrestart 0
	;;
  *)
        msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
        RETVAL=3
esac

exit $RETVAL
