xref: /titanic_44/usr/src/cmd/initpkg/shutdown.sh (revision 4d53c7addddea6a86730beb59363b8ba12e8252e)
17c478bd9Sstevel@tonic-gate#!/sbin/sh
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*4d53c7adSDan Price# Common Development and Distribution License (the "License").
7*4d53c7adSDan Price# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
22*4d53c7adSDan Price#	Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate#	Use is subject to license terms.
247c478bd9Sstevel@tonic-gate#
257c478bd9Sstevel@tonic-gate#	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
267c478bd9Sstevel@tonic-gate#	  All Rights Reserved
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate#	Sequence performed to change the init state of a machine.  Only allows
317c478bd9Sstevel@tonic-gate#	transitions to states 0,1,5,6,s,S (i.e.: down or administrative states).
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate#	This procedure checks to see if you are permitted and allows an
347c478bd9Sstevel@tonic-gate#	interactive shutdown.  The actual change of state, killing of
357c478bd9Sstevel@tonic-gate#	processes and such are performed by the new init state, say 0,
367c478bd9Sstevel@tonic-gate#	and its /sbin/rc0.
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gateusage() {
397c478bd9Sstevel@tonic-gate	echo "Usage: $0 [ -y ] [ -g<grace> ] [ -i<initstate> ] [ message ]"
407c478bd9Sstevel@tonic-gate	exit 1
417c478bd9Sstevel@tonic-gate}
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gatenotify() {
447c478bd9Sstevel@tonic-gate	/usr/sbin/wall -a <<-!
457c478bd9Sstevel@tonic-gate	$*
467c478bd9Sstevel@tonic-gate	!
477c478bd9Sstevel@tonic-gate	if [ -x /usr/sbin/showmount -a -x /usr/sbin/rwall ]
487c478bd9Sstevel@tonic-gate	then
497c478bd9Sstevel@tonic-gate		remotes=`/usr/sbin/showmount`
507c478bd9Sstevel@tonic-gate		if [ "X${remotes}" != "X" ]
517c478bd9Sstevel@tonic-gate		then
527c478bd9Sstevel@tonic-gate			/usr/sbin/rwall -q ${remotes} <<-!
537c478bd9Sstevel@tonic-gate			$*
547c478bd9Sstevel@tonic-gate			!
557c478bd9Sstevel@tonic-gate		fi
567c478bd9Sstevel@tonic-gate	fi
577c478bd9Sstevel@tonic-gate}
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gatenologin=/etc/nologin
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate# Set the PATH so that to guarentee behavior of shell built in commands
627c478bd9Sstevel@tonic-gate# (such as echo).
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gatePATH=/usr/sbin:/usr/bin:/sbin
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate# Initial sanity checks:
677c478bd9Sstevel@tonic-gate#	Make sure /usr is mounted
687c478bd9Sstevel@tonic-gate#	Check the user id (only root can run shutdown)
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gateif [ ! -d /usr/bin ]
717c478bd9Sstevel@tonic-gatethen
727c478bd9Sstevel@tonic-gate	echo "$0:  /usr is not mounted.  Mount /usr or use init to shutdown."
737c478bd9Sstevel@tonic-gate	exit 1
747c478bd9Sstevel@tonic-gatefi
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gateif [ -x /usr/bin/id ]
777c478bd9Sstevel@tonic-gatethen
787c478bd9Sstevel@tonic-gate	eval `/usr/bin/id  |  /usr/bin/sed 's/[^a-z0-9=].*//'`
797c478bd9Sstevel@tonic-gate	if [ "${uid:=0}" -ne 0 ]
807c478bd9Sstevel@tonic-gate	then
817c478bd9Sstevel@tonic-gate	        echo "$0:  Only root can run $0"
827c478bd9Sstevel@tonic-gate		exit 2
837c478bd9Sstevel@tonic-gate	fi
847c478bd9Sstevel@tonic-gateelse
857c478bd9Sstevel@tonic-gate	echo "$0:  can't check user id."
867c478bd9Sstevel@tonic-gate	exit 2
877c478bd9Sstevel@tonic-gatefi
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate# Get options (defaults immediately below):
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gategrace=60
927c478bd9Sstevel@tonic-gateaskconfirmation=yes
937c478bd9Sstevel@tonic-gateinitstate=s
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gatewhile getopts g:i:y? c
967c478bd9Sstevel@tonic-gatedo
977c478bd9Sstevel@tonic-gate	case $c in
987c478bd9Sstevel@tonic-gate	g)
997c478bd9Sstevel@tonic-gate		case $OPTARG in
1007c478bd9Sstevel@tonic-gate		*[!0-9]* )
1017c478bd9Sstevel@tonic-gate			echo "$0: -g requires a numeric option"
1027c478bd9Sstevel@tonic-gate			usage
1037c478bd9Sstevel@tonic-gate			;;
1047c478bd9Sstevel@tonic-gate		[0-9]* )
1057c478bd9Sstevel@tonic-gate			grace=$OPTARG
1067c478bd9Sstevel@tonic-gate			;;
1077c478bd9Sstevel@tonic-gate		esac
1087c478bd9Sstevel@tonic-gate		;;
1097c478bd9Sstevel@tonic-gate	i)
1107c478bd9Sstevel@tonic-gate		case $OPTARG in
1117c478bd9Sstevel@tonic-gate		[Ss0156])
1127c478bd9Sstevel@tonic-gate			initstate=$OPTARG
1137c478bd9Sstevel@tonic-gate			;;
1147c478bd9Sstevel@tonic-gate		[234abcqQ])
1157c478bd9Sstevel@tonic-gate			echo "$0: Initstate $OPTARG is not for system shutdown"
1167c478bd9Sstevel@tonic-gate			exit 1
1177c478bd9Sstevel@tonic-gate			;;
1187c478bd9Sstevel@tonic-gate		*)
1197c478bd9Sstevel@tonic-gate			echo "$0: $OPTARG is not a valid initstate"
1207c478bd9Sstevel@tonic-gate			usage
1217c478bd9Sstevel@tonic-gate			;;
1227c478bd9Sstevel@tonic-gate		esac
1237c478bd9Sstevel@tonic-gate		;;
1247c478bd9Sstevel@tonic-gate	y)
1257c478bd9Sstevel@tonic-gate		askconfirmation=
1267c478bd9Sstevel@tonic-gate		;;
1277c478bd9Sstevel@tonic-gate	\?)	usage
1287c478bd9Sstevel@tonic-gate		;;
1297c478bd9Sstevel@tonic-gate	esac
1307c478bd9Sstevel@tonic-gatedone
1317c478bd9Sstevel@tonic-gateshift `expr $OPTIND - 1`
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gateecho '\nShutdown started.    \c'
1347c478bd9Sstevel@tonic-gate/usr/bin/date
1357c478bd9Sstevel@tonic-gateecho
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gateNODENAME=`uname -n`
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gatecd /
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gatetrap "rm $nologin >/dev/null 2>&1 ;exit 1"  1 2 15
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate# If other users are on the system (and any grace period is given), warn them.
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gatefor i in 7200 3600 1800 1200 600 300 120 60 30 10; do
1467c478bd9Sstevel@tonic-gate	if [ ${grace} -gt $i ]
1477c478bd9Sstevel@tonic-gate	then
1487c478bd9Sstevel@tonic-gate		hours=`/usr/bin/expr ${grace} / 3600`
1497c478bd9Sstevel@tonic-gate		minutes=`/usr/bin/expr ${grace} % 3600 / 60`
1507c478bd9Sstevel@tonic-gate		seconds=`/usr/bin/expr ${grace} % 60`
1517c478bd9Sstevel@tonic-gate		time=""
1527c478bd9Sstevel@tonic-gate		if [ ${hours} -gt 1 ]
1537c478bd9Sstevel@tonic-gate		then
1547c478bd9Sstevel@tonic-gate			time="${hours} hours "
1557c478bd9Sstevel@tonic-gate		elif [ ${hours} -eq 1 ]
1567c478bd9Sstevel@tonic-gate		then
1577c478bd9Sstevel@tonic-gate			time="1 hour "
1587c478bd9Sstevel@tonic-gate		fi
1597c478bd9Sstevel@tonic-gate		if [ ${minutes} -gt 1 ]
1607c478bd9Sstevel@tonic-gate		then
1617c478bd9Sstevel@tonic-gate			time="${time}${minutes} minutes "
1627c478bd9Sstevel@tonic-gate		elif [ ${minutes} -eq 1 ]
1637c478bd9Sstevel@tonic-gate		then
1647c478bd9Sstevel@tonic-gate			time="${time}1 minute "
1657c478bd9Sstevel@tonic-gate		fi
1667c478bd9Sstevel@tonic-gate		if [ ${hours} -eq 0 -a ${seconds} -gt 0 ]
1677c478bd9Sstevel@tonic-gate		then
1687c478bd9Sstevel@tonic-gate			if [ ${seconds} -eq 1 ]
1697c478bd9Sstevel@tonic-gate			then
1707c478bd9Sstevel@tonic-gate				time="${time}${seconds} second"
1717c478bd9Sstevel@tonic-gate			else
1727c478bd9Sstevel@tonic-gate				time="${time}${seconds} seconds"
1737c478bd9Sstevel@tonic-gate			fi
1747c478bd9Sstevel@tonic-gate		fi
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate		(notify \
1777c478bd9Sstevel@tonic-gate"The system ${NODENAME} will be shut down in ${time}
1787c478bd9Sstevel@tonic-gate$*") &
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gatepid1=$!
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate		rm $nologin >/dev/null 2>&1
1837c478bd9Sstevel@tonic-gate		cat > $nologin <<-!
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate			NO LOGINS: System going down in ${time}
1867c478bd9Sstevel@tonic-gate			$*
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate		!
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate		/usr/bin/sleep `/usr/bin/expr ${grace} - $i`
1917c478bd9Sstevel@tonic-gate		grace=$i
1927c478bd9Sstevel@tonic-gate	fi
1937c478bd9Sstevel@tonic-gatedone
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate# Confirm that we really want to shutdown.
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gateif [ ${askconfirmation} ]
1987c478bd9Sstevel@tonic-gatethen
1997c478bd9Sstevel@tonic-gate	echo "Do you want to continue? (y or n):   \c"
2007c478bd9Sstevel@tonic-gate	read b
2017c478bd9Sstevel@tonic-gate	if [ "$b" != "y" ]
2027c478bd9Sstevel@tonic-gate	then
2037c478bd9Sstevel@tonic-gate		notify "False Alarm:  The system ${NODENAME} will not be brought down."
2047c478bd9Sstevel@tonic-gate		echo 'Shutdown aborted.'
2057c478bd9Sstevel@tonic-gate		rm $nologin >/dev/null 2>&1
2067c478bd9Sstevel@tonic-gate		exit 1
2077c478bd9Sstevel@tonic-gate	fi
2087c478bd9Sstevel@tonic-gatefi
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate# Final shutdown message, and sleep away the final 10 seconds (or less).
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate(notify \
2137c478bd9Sstevel@tonic-gate"THE SYSTEM ${NODENAME} IS BEING SHUT DOWN NOW ! ! !
2147c478bd9Sstevel@tonic-gateLog off now or risk your files being damaged
2157c478bd9Sstevel@tonic-gate$*") &
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gatepid2=$!
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gateif [ ${grace} -gt 0 ]
2207c478bd9Sstevel@tonic-gatethen
2217c478bd9Sstevel@tonic-gate	/usr/bin/sleep ${grace}
2227c478bd9Sstevel@tonic-gatefi
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate# Go to the requested initstate.
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gateecho "Changing to init state $initstate - please wait"
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gateif [ "$pid1" ] || [ "$pid2" ]
2307c478bd9Sstevel@tonic-gatethen
2317c478bd9Sstevel@tonic-gate	/usr/bin/kill $pid1 $pid2 > /dev/null 2>&1
2327c478bd9Sstevel@tonic-gatefi
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate/sbin/init ${initstate}
235