#!/bin/bash
#
# syslog        Starts syslogd/klogd.
#
#
# chkconfig: 2345 02 98
# description: Syslog is the facility by which many daemons use to log \
# messages to various system log files.  It is a good idea to always \
# run syslog.

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

[ -f /sbin/syslogd ] || exit 0
[ -f /sbin/klogd ] || exit 0

# Source config
if [ -f /etc/sysconfig/syslog ] ; then
	. /etc/sysconfig/syslog
else
	SYSLOGD_OPTIONS="-m 0"
	KLOGD_OPTIONS="-2"
fi

RETVAL=0

umask 077

# My function to find the pid of a program
my_pidofproc() {
        base=`basename $1`

        # Test syntax.
        if [ $# = 0 ] ; then
                echo "Usage: pidofproc {program}"
                return 1
        fi

        # First try "/var/run/*.pid" files
        if [ -f /var/run/${base}.pid ] ; then
                pid=`cat /var/run/${base}.pid | { read foo ; echo $foo ; }`
                if [ "$pid" != "" ] ; then
                        echo $pid
                        return 0
                fi
        fi

        # Next try "pidof"
        pid=`pidof -o $$ -o $PPID -o %PPID -x ${base}`
        if [ "$pid" != "" ] ; then
                echo $pid
                return 0
        fi
}

# My function to start a program.
my_daemon() {
        # Test syntax.
        gotbase=
        while [ "$1" != "${1##-}" -o "$1" != "${1##+}" ]; do 
          case $1 in
            '')    echo '$0: Usage: daemon {program}'
                   return 1;;
            --check)
                   shift
                   base=$1
                   gotbase="yes"
                   shift
                   ;;
             *)
                   ;;
          esac
        done

        # Save basename.
        [ -z $gotbase ] && base=`basename $1`

        # See if it's already running.
        pidlist=`my_pidofproc $1`

        pid=
        for apid in $pidlist ; do
           [ -d /proc/$apid ] && pid="$pid $apid"
        done

        if [ -n "$pid" ]; then
        	[ "$BOOTUP" = "verbose" ] && echo -n " $base"
		[ "$BOOTUP" != "verbose" ] && echo_success
		return 0
	fi

        # make sure it doesn't core dump anywhere; while this could mask
        # problems with the daemon, it also closes some security problems
#        ulimit -S -c 0 >/dev/null 2>&1

        # Echo daemon
        [ "$BOOTUP" = "verbose" ] && echo -n " $base"

        # And start it up.
        initlog -c "$*" && success "$base startup" || failure "$base startup"

}


start() {
	# The install process requires standard syslogd and removing it
	# requires us to make sure we don't have the wrong syslogd running.
        #
	# The rpm erase process runs a script that kills syslogd (which
	# we don't mind with this change) so we do the check and erase
        # first to avoid this script killing the wrong syslogd.
	#
	# rpm -q returns 0 if it is there, 1 if not. To make sure, we
	# bias towards removing it....
	rpm -q sysklogd > /dev/null 2>&1
	if [ $? != 1  ] ; then
	    echo -n "Replacing system logger with Spinnaker version .."
	    rpm -e sysklogd
	    echo "."
	fi

 	echo -n "Starting system logger: "
	my_daemon syslogd $SYSLOGD_OPTIONS
	RETVAL=$?

	# let syslogd catch up before dumping the console messages via
	# klogd to it...
	sleep 2

	echo
        echo -n "Starting kernel logger: "
        my_daemon klogd $KLOGD_OPTIONS
	echo
	# HACK HACK HACK to reset the kernel paramters to what we want them to be
	# because starting one of these binaries seems to set kernel.printk back to the
	# default values for a system instead of the values we set in /etc/sysctl.conf
	# From the man page for klogd we might probably need to parse the /etc/sysctl.conf
	# file and add a -c # option based on the options that are in that file to set
	# the console level of logging.
	action "Resetting kernel parameters: " sysctl -e -p /etc/sysctl.conf

	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/syslog

	return $RETVAL
}

stop() {
        echo -n "Shutting down kernel logger: "
	killproc klogd
	echo

	echo -n "Shutting down system logger: "
	killproc syslogd
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/syslog
	return $RETVAL
}

rhstatus() {
	status klogd
	status syslogd
}

restart() {
	stop
	start
}

cd /core

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	rhstatus
	;;
  restart|reload)
	restart
	;;
  condrestart)
  	[ -f /var/lock/subsys/syslog ] && restart || :
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart}"
	exit 1
esac

exit $?


