• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
21readonly MAILLOG=/var/log/maillog
22
23# Signals to trap.
24readonly TRAP_SIGS="1 2 3 6 11 15"
25
26# configuration file for syslog or syslog-ng
27CONFIG_FILE=""
28
29# rsyslogd .conf specific args.
30RSYSLOG_CONFIG=
31
32# number of seconds to wait for another syslog test to complete
33WAIT_COUNT=60
34
35cleanup()
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
60setup()
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		# To cope with systemd-journal, we are looking for either:
75		#   $ModLoad imjournal
76		#   module(load="imjournal"...)
77		# in rsyslog config, and using those settings.
78		if grep -qri '^[^#].*load.*imjournal' /etc/rsyslog.conf /etc/rsyslog.d/ ; then
79			systemd_journal=$(grep -Ehoi "^[^#].*(imjournal|workdirectory).*" -r /etc/rsyslog.conf /etc/rsyslog.d/)
80			RSYSLOG_CONFIG=$(cat <<EOF
81$systemd_journal
82EOF
83)
84		else
85			log_socket=$(grep -ho "^\$SystemLogSocketName .*" -r /etc/rsyslog.conf /etc/rsyslog.d/ | head -1)
86			RSYSLOG_CONFIG=$(cat <<EOF
87\$ModLoad imuxsock.so
88$log_socket
89EOF
90)
91		fi
92	else
93		tst_resm TCONF "Couldn't find syslogd, syslog-ng or rsyslogd"
94		cleanup 32
95	fi
96
97	# Back up configuration file
98	if [ -f "$CONFIG_FILE" ]; then
99		# Pause if another LTP syslog test is running
100		while [ -f "$CONFIG_FILE.ltpback" -a $WAIT_COUNT -gt 0 ]; do
101			: $(( WAIT_COUNT -= 1 ))
102			sleep 1
103		done
104		# Oops -- $CONFIG_FILE.ltpback is still there!
105		if [ $WAIT_COUNT -eq 0 ]; then
106			tst_resm TBROK "another syslog test is stuck"
107			cleanup 1
108		elif ! cp "$CONFIG_FILE" "$CONFIG_FILE.ltpback"; then
109			tst_resm TBROK "failed to backup $CONFIG_FILE"
110			cleanup 1
111		fi
112	else
113		tst_resm TBROK "$CONFIG_FILE not found!"
114	fi
115
116}
117
118disable_traps()
119{
120	trap - $TRAP_SIGS
121}
122
123# For most cases this isn't exotic. If you're running upstart however, you
124# might have fun here :).
125restart_syslog_daemon()
126{
127	# Default to running `cleanup 1' when dealing with error cases.
128	if [ $# -eq 0 ]; then
129		cleanup_command="cleanup 1"
130	else
131		cleanup_command=$1
132	fi
133
134	tst_resm TINFO "restarting syslog daemon"
135
136	if [ -n "$SYSLOG_DAEMON" ]; then
137		restart_daemon $SYSLOG_DAEMON
138		if [ $? -eq 0 ]; then
139			# XXX: this really shouldn't exist; if *syslogd isn't
140			# ready once the restart directive has been issued,
141			# then it needs to be fixed.
142			sleep 2
143		else
144			#
145			# Some distributions name the service syslog even if
146			# the package is syslog-ng or rsyslog, so try it once
147			# more with just syslog.
148			#
149			restart_daemon "syslog"
150
151			if [ $? -ne 0 ]; then
152				$cleanup_command
153			fi
154		fi
155	fi
156}
157
158export TST_TOTAL=${TST_TOTAL:=1}
159export TST_COUNT=1
160export TCID=${TCID:="$(basename "$0")"}
161. cmdlib.sh
162