Home | History | Annotate | Download | only in syslog
      1 #! /bin/sh
      2 #
      3 #  Copyright (c) Linux Test Project, 2010
      4 #
      5 #  This program is free software;  you can redistribute it and/or modify
      6 #  it under the terms of the GNU General Public License as published by
      7 #  the Free Software Foundation; either version 2 of the License, or
      8 #  (at your option) any later version.
      9 #
     10 #  This program is distributed in the hope that it will be useful,
     11 #  but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13 #  the GNU General Public License for more details.
     14 #
     15 #  You should have received a copy of the GNU General Public License
     16 #  along with this program;  if not, write to the Free Software
     17 #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18 
     19 ##################################################################
     20 
     21 readonly MAILLOG=/var/log/maillog
     22 
     23 # Signals to trap.
     24 readonly TRAP_SIGS="1 2 3 6 11 15"
     25 
     26 # configuration file for syslog or syslog-ng
     27 CONFIG_FILE=""
     28 
     29 # rsyslogd .conf specific args.
     30 RSYSLOG_CONFIG=
     31 
     32 # number of seconds to wait for another syslog test to complete
     33 WAIT_COUNT=60
     34 
     35 cleanup()
     36 {
     37 	# Reentrant cleanup -> bad. Especially since rsyslogd on Fedora 13
     38 	# seems to get stuck FOREVER when not running as root. Lame...
     39 	disable_traps
     40 	exit_code=$1
     41 
     42 	# Restore the previous syslog daemon state.
     43 	if [ -f "$CONFIG_FILE.ltpback" ]; then
     44 		if mv "$CONFIG_FILE.ltpback" "$CONFIG_FILE"; then
     45 			# Make sure that restart_syslog_daemon doesn't loop
     46 			# back to cleanup again.
     47 			restart_syslog_daemon "return 1"
     48 			# Maintain any nonzero exit codes
     49 			if [ $exit_code -ne $? ]; then
     50 				exit_code=1
     51 			fi
     52 		else
     53 			exit_code=1
     54 		fi
     55 	fi
     56 
     57 	exit $exit_code
     58 }
     59 
     60 setup()
     61 {
     62 	tst_require_root
     63 
     64 	trap '	disable_traps
     65 		tst_resm TBROK "Testing is terminating due to a signal"
     66 		cleanup 1' $TRAP_SIGS || exit 1
     67 
     68 	if [ "$SYSLOG_DAEMON" = "syslog" ]; then
     69 		CONFIG_FILE="/etc/syslog.conf"
     70 	elif [ "$SYSLOG_DAEMON" = "syslog-ng" ]; then
     71 		CONFIG_FILE="/etc/syslog-ng/syslog-ng.conf"
     72 	elif [ "$SYSLOG_DAEMON" = "rsyslog" ]; then
     73 		CONFIG_FILE="/etc/rsyslog.conf"
     74 		if grep -q -r '^\$ModLoad[[:space:]]*imjournal' /etc/rsyslog.conf /etc/rsyslog.d/ ; then
     75 			systemd_journal=$(grep -Ehoi "^[^#].*(imjournal|workdirectory).*" -r /etc/rsyslog.conf /etc/rsyslog.d/)
     76 			RSYSLOG_CONFIG=$(cat <<EOF
     77 $systemd_journal
     78 EOF
     79 )
     80 		else
     81 			log_socket=$(grep -ho "^\$SystemLogSocketName .*" -r /etc/rsyslog.conf /etc/rsyslog.d/ | head -1)
     82 			RSYSLOG_CONFIG=$(cat <<EOF
     83 \$ModLoad imuxsock.so
     84 $log_socket
     85 EOF
     86 )
     87 		fi
     88 	else
     89 		tst_resm TBROK "Couldn't find syslogd, syslog-ng or rsyslogd"
     90 		cleanup 1
     91 	fi
     92 
     93 	# Back up configuration file
     94 	if [ -f "$CONFIG_FILE" ]; then
     95 		# Pause if another LTP syslog test is running
     96 		while [ -f "$CONFIG_FILE.ltpback" -a $WAIT_COUNT -gt 0 ]; do
     97 			: $(( WAIT_COUNT -= 1 ))
     98 			sleep 1
     99 		done
    100 		# Oops -- $CONFIG_FILE.ltpback is still there!
    101 		if [ $WAIT_COUNT -eq 0 ]; then
    102 			tst_resm TBROK "another syslog test is stuck"
    103 			cleanup 1
    104 		elif ! cp "$CONFIG_FILE" "$CONFIG_FILE.ltpback"; then
    105 			tst_resm TBROK "failed to backup $CONFIG_FILE"
    106 			cleanup 1
    107 		fi
    108 	else
    109 		tst_resm TBROK "$CONFIG_FILE not found!"
    110 	fi
    111 
    112 }
    113 
    114 disable_traps()
    115 {
    116 	trap - $TRAP_SIGS
    117 }
    118 
    119 # For most cases this isn't exotic. If you're running upstart however, you
    120 # might have fun here :).
    121 restart_syslog_daemon()
    122 {
    123 	# Default to running `cleanup 1' when dealing with error cases.
    124 	if [ $# -eq 0 ]; then
    125 		cleanup_command="cleanup 1"
    126 	else
    127 		cleanup_command=$1
    128 	fi
    129 
    130 	tst_resm TINFO "restarting syslog daemon"
    131 
    132 	if [ -n "$SYSLOG_DAEMON" ]; then
    133 		restart_daemon $SYSLOG_DAEMON
    134 		if [ $? -eq 0 ]; then
    135 			# XXX: this really shouldn't exist; if *syslogd isn't
    136 			# ready once the restart directive has been issued,
    137 			# then it needs to be fixed.
    138 			sleep 2
    139 		else
    140 			#
    141 			# Some distributions name the service syslog even if
    142 			# the package is syslog-ng or rsyslog, so try it once
    143 			# more with just syslog.
    144 			#
    145 			restart_daemon "syslog"
    146 
    147 			if [ $? -ne 0 ]; then
    148 				$cleanup_command
    149 			fi
    150 		fi
    151 	fi
    152 }
    153 
    154 export TST_TOTAL=${TST_TOTAL:=1}
    155 export TST_COUNT=1
    156 export TCID=${TCID:="$(basename "$0")"}
    157 . cmdlib.sh
    158