#! /bin/sh
#set -x
#  Copyright (c) International Business Machines  Corp., 2002
#
#  This program is free software;  you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#  the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program;  if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# 12/05/02  Port to bash -Robbie Williamson <robbiew@us.ibm.com>
# 02/05/03  Modified - Manoj Iyer <manjo@mail.utexas.edu> use USCTEST macros
#           fixed bugs.
# 07/26/05  Michael Reed  <mreedltp@vnet.ibm.com>
#           Made changes to account for the replacement of syslogd 
#           with syslog-ng
#
##################################################################
# case 10: Test setlogmask() with LOG_MASK macro.                #
#                                                                #
#         o Use setlogmask() with LOG_MASK macro to set an       #
#            individual priority level.                          #
#         o Send the message of above prority and expect it to   #
#           be logged.                                           #
#         o Send message which is below the priority level to    #
#           the one set above, which should not be logged.       #
##################################################################

DEBIAN=0
#number of seconds to wait for another syslog test to complete
wait_count=60

#configuration file for syslog or syslog-ng
CONFIG_FILE=""

#Command for syslog or syslog-ng
syslog_cmd=""

cleanup()
{
  if [ $status_flag -eq 0 ]
  then
	tst_resm TPASS " setlogmask() with LOG_MASK macro worked"
  else
	tst_resm TFAIL " setlogmask() with LOG_MASK macro failed"
  fi


#Restore syslog.conf
  mv $CONFIG_FILE.ltpback $CONFIG_FILE

#Restart syslog
  tst_resm TINFO "restart $syslog_cmd"
  $syslog_cmd restart 2>&1 1>/dev/null

 
  sleep 2

  if [ $DEBIAN -eq 1 ];then
     rm -f /etc/init.d/syslog
  fi

exit $status_flag
}

syslog_case10()
{
	#set the trap to handle signals.
	trap '
			tst_resm TBROK "Testing is terminating due to a signal"
			status_flag=1
			cleanup
	' 1 2 3 6 11 15

	echo
	tst_resm TINFO "syslog: Testing setlogmask() with LOG_MASK macro..."
	echo
        #check to see if syslogd or syslog-ng exists
	if [ -e /sbin/syslogd  ]
	then
	    CONFIG_FILE="/etc/syslog.conf"
	    syslog_cmd="/etc/init.d/syslog"
	elif [ -e /sbin/syslog-ng ]
        then
	    CONFIG_FILE="/etc/syslog-ng/syslog-ng.conf"
	    syslog_cmd="/etc/init.d/syslog"
	else
	    tst_resm TBROK "syslogd or syslog-ng  no such command"
	    status_flag=1
	    cleanup	
	fi

#Back up configuration file
       if [ ! -e $CONFIG_FILE ]
       then
	  tst_resm TBROK  "$CONFIG_FILE not found!"
       else
          #Pause if another LTP syslog test is running
          while [ -e $CONFIG_FILE.ltpback ] 
            do
              if [ $(( wait_count-- )) -gt 0 ]
              then
                sleep 1
              else
	        tst_resm TBROK "Another syslog test appears to be\
                                stuck, could not run"
                status_flag=1
	        exit $status_flag
              fi
           done
          cp $CONFIG_FILE $CONFIG_FILE.ltpback
       fi

         echo "Running tests"


	#Create symlink to /etc/init.d/sysklogd for Debian
        find /etc/init.d/ |grep sysklogd 2>&1 1>/dev/null 
	RC=$?
        if [ $RC -eq 0 ]
        then
                ln -s /etc/init.d/sysklogd /etc/init.d/syslog
                DEBIAN=1
        fi

	# check if /etc/init/syslog script exists
	find /etc/init.d/ |grep syslog 2>&1 1>/dev/null 
	RC=$?
	if [ $RC -ne 0 ]
	then
		tst_resm TBROK "/etc/init.d/syslog no such script"
		status_flag=1
		cleanup
	fi

	# Create the configuration file specific to this test case.
	if [ $CONFIG_FILE = "/etc/syslog.conf" ]; then
	    echo "user.debug       /var/log/messages" > /etc/syslog.conf
	else
           echo "source src{ internal(); unix-dgram("/dev/log");\
                 udp(ip("0.0.0.0") port(514)); };" > $CONFIG_FILE 
	    echo "filter f_syslog_debug{ facility(user); };" \
		>> $CONFIG_FILE
	    echo "destination syslog_messages { file("/var/log/messages");};"\
		  >> $CONFIG_FILE
	    echo "log { source(src); filter(f_syslog_debug); \ 
                 destination(syslog_messages); };"  >> $CONFIG_FILE
	fi

if [ $CONFIG_FILE = "/etc/syslog.conf" ]; then
       #Restart syslog
       $syslog_cmd restart 2>&1 1>/dev/null
   

       # check if /var/log/messages script exists
       find /var/log/ |grep messages 2>&1 1>/dev/null 
       RC=$?
        if [ $RC -ne 0 ]
	then
		tst_resm TBROK "/var/log/messages no such log file"
		status_flag=1
		cleanup
	fi
else
       #Restart syslog
       $syslog_cmd restart 2>&1 1>/dev/null
fi

    sleep 2
	if [ -e /var/log/messages ]; then
	    allow1=`grep -c "syslogtst:10 error level is logged" \
                   /var/log/messages`
	    donot_allow1=`grep -c "syslogtst:10 warning level not\
                          to be logged" /var/log/messages`
	else
	    allow1=0
	    donot_allow1=0
	fi

	syslogtst 10 2>/dev/null
	if [ $? -ne 0 ]; then
			status_flag=1
			return
	fi
	sleep 2

	# check if /var/log/messages script exists
	find /var/log/ |grep messages 2>&1 1>/dev/null 
	RC=$?
	if [ $RC -ne 0 ]
	then
		tst_resm TBROK "/var/log/messages no such log file"
		status_flag=1
		cleanup
	fi
	allow2=`grep -c "syslogtst:10 error level is logged" /var/log/messages`
	donot_allow2=`grep -c "syslogtst:10 warning level not to be logged"\
                      /var/log/messages`

	diff1=$(( $allow2 - $allow1 ))
	if [ $diff1 -ne 1 ]; then
			tst_resm TBROK "Expected message is not logged...."
			status_flag=1
			return
	fi

	diff2=$(( $donot_allow2 - $donot_allow1 ))
	if [ $diff2 -ne 0 ]; then
			tst_resm TBROK "Unexpected message is logged..."
			status_flag=1
	fi

}

export TST_TOTAL=1
export TST_COUNT=1
export TCID=$(basename $0)

if ! [ -e /etc/init.d/syslog ]
then
	tst_resm TINFO "/etc/init.d/syslog is not installed"
	exit 0
fi

RC=0
status_flag=0
tst_resm TINFO " Test setlogmask() with LOG_MASK macro."
tst_resm TINFO " o Use setlogmask() with LOG_MASK macro to set an"
tst_resm TINFO "   individual priority level."
tst_resm TINFO " o Send the message of above prority and expect it to be"
tst_resm TINFO "   logged."
tst_resm TINFO " o Send message which is at other priority level to"
tst_resm TINFO "   the one set above, which should not be logged."



syslog_case10
cleanup
