xref: /titanic_44/usr/src/cmd/logadm/logadm-upgrade (revision 91d7f85e02991954d1e1bd44673df567ad8dcc87)
1#!/sbin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22#
23# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24#
25
26. /lib/svc/share/smf_include.sh
27
28LOGADM=/etc/logadm.conf
29LOGADM_D=${LOGADM%conf}d
30LS=/usr/bin/ls
31AWK=/usr/bin/awk
32GREP=/usr/bin/grep
33
34#
35# This is a temporary service to allow addition (only) to /etc/logadm.conf
36# It is temporary in the sense that logadm(1M) should have its configuration
37# migrated to SMF in the future.
38#
39
40#
41# Display error message and exits with error code
42#
43msg_exit()
44{
45	exit_code=$1
46	msg=$2
47
48	echo "${msg}"
49	exit ${exit_code}
50}
51
52#
53# If there is no /etc/logadm.d we can bail
54#
55if [ ! -d ${LOGADM_D} ]; then
56	exit ${SMF_EXIT_OK}
57fi
58
59#
60# Cache files
61#
62files=$(${LS} -t  ${LOGADM} ${LOGADM_D}/*)
63
64#
65# If there is no /etc/logadm.conf create it and make sure it has the
66# right ownership and permissions.
67# Make sure this is done AFTER $files is set. Otherwise /etc/logadm.conf will be
68# newer than all files is /etc/logadm.d and they will be skipped.
69#
70if [ ! -f ${LOGADM} ]; then
71	touch ${LOGADM}
72	chmod 644 ${LOGADM}
73	chown root:sys ${LOGADM}
74fi
75
76for f in ${files}
77do
78	#
79	# If it is not a file, we skip it.
80	#
81	if [ ! -f ${f} ]; then
82		continue
83	fi
84
85	#
86	# We stop when files at /etc/logadm.d are older than /etc/logadm.conf
87	#
88	if [ ${f} = ${LOGADM} ]; then
89		break
90	fi
91
92	#
93	# We ignore files that are not owned by root, group sys
94	# and have permissions different than 444
95	#
96	perm=$(${LS} -l ${f} | ${AWK} '{printf("%s %s:%s", $1, $3, $4)}')
97	if [ $? != 0 ]; then
98		msg_exit ${SMF_EXIT_ERR_FATAL} "${perm}"
99	fi
100	if [ "${perm}" != "-r--r--r-- root:sys" ]; then
101		echo "Unexpected permission/ownership for ${f}"
102		echo "    expected -r--r--r-- root:sys but got ${perm}"
103		echo "    skipping ${f}"
104		continue
105	fi
106
107	#
108	# Discard comments (lines starting with #)
109	#
110	${GREP} -v '^#' ${f} | while read entry
111	do
112		sig=$(echo ${entry} | ${AWK} '{printf("%s\>", $1);}' 2>&1)
113		if [ $? != 0 ]; then # only happens if awk(1) fails
114			msg_exit ${SMF_EXIT_ERR_FATAL} "${sig}"
115		fi
116
117		#
118		# if ${sig} is null but the previous command succeeded, we skip
119		#
120		if [ ! ${sig} ]; then
121			continue;
122		fi
123
124		err_msg=$(${GREP} ^${sig} ${LOGADM} 2>&1)
125		case $? in
126		'1')
127			echo "${entry}" >> ${LOGADM}
128			;;
129		'0')
130			;;
131		*)
132			msg_exit ${SMF_EXIT_ERR_FATAL} "${err_msg}"
133		esac
134	done
135done
136
137exit ${SMF_EXIT_OK}
138
139