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