xref: /illumos-gate/usr/src/cmd/initpkg/shutdown.sh (revision bbf21555)
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
64d53c7adSDan Price# Common Development and Distribution License (the "License").
74d53c7adSDan 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#
224d53c7adSDan 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
283bf13137SDan McDonald#
293bf13137SDan McDonald# Copyright 2017, OmniTI Computer Consulting, Inc. All rights reserved.
303bf13137SDan McDonald#
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate#	Sequence performed to change the init state of a machine.  Only allows
337c478bd9Sstevel@tonic-gate#	transitions to states 0,1,5,6,s,S (i.e.: down or administrative states).
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate#	This procedure checks to see if you are permitted and allows an
367c478bd9Sstevel@tonic-gate#	interactive shutdown.  The actual change of state, killing of
377c478bd9Sstevel@tonic-gate#	processes and such are performed by the new init state, say 0,
387c478bd9Sstevel@tonic-gate#	and its /sbin/rc0.
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gateusage() {
417c478bd9Sstevel@tonic-gate	echo "Usage: $0 [ -y ] [ -g<grace> ] [ -i<initstate> ] [ message ]"
427c478bd9Sstevel@tonic-gate	exit 1
437c478bd9Sstevel@tonic-gate}
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gatenotify() {
467c478bd9Sstevel@tonic-gate	/usr/sbin/wall -a <<-!
477c478bd9Sstevel@tonic-gate	$*
487c478bd9Sstevel@tonic-gate	!
493bf13137SDan McDonald	# We used to do rwall here if showmounts had any output, but
503bf13137SDan McDonald	# rwall is a potential security hole, and it could block this, so
513bf13137SDan McDonald	# we don't bother with it anymore.
527c478bd9Sstevel@tonic-gate}
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gatenologin=/etc/nologin
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate# Set the PATH so that to guarentee behavior of shell built in commands
577c478bd9Sstevel@tonic-gate# (such as echo).
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gatePATH=/usr/sbin:/usr/bin:/sbin
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate# Initial sanity checks:
627c478bd9Sstevel@tonic-gate#	Make sure /usr is mounted
637c478bd9Sstevel@tonic-gate#	Check the user id (only root can run shutdown)
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gateif [ ! -d /usr/bin ]
667c478bd9Sstevel@tonic-gatethen
677c478bd9Sstevel@tonic-gate	echo "$0:  /usr is not mounted.  Mount /usr or use init to shutdown."
687c478bd9Sstevel@tonic-gate	exit 1
697c478bd9Sstevel@tonic-gatefi
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gateif [ -x /usr/bin/id ]
727c478bd9Sstevel@tonic-gatethen
737c478bd9Sstevel@tonic-gate	eval `/usr/bin/id  |  /usr/bin/sed 's/[^a-z0-9=].*//'`
747c478bd9Sstevel@tonic-gate	if [ "${uid:=0}" -ne 0 ]
757c478bd9Sstevel@tonic-gate	then
767c478bd9Sstevel@tonic-gate	        echo "$0:  Only root can run $0"
777c478bd9Sstevel@tonic-gate		exit 2
787c478bd9Sstevel@tonic-gate	fi
797c478bd9Sstevel@tonic-gateelse
807c478bd9Sstevel@tonic-gate	echo "$0:  can't check user id."
817c478bd9Sstevel@tonic-gate	exit 2
827c478bd9Sstevel@tonic-gatefi
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate# Get options (defaults immediately below):
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gategrace=60
877c478bd9Sstevel@tonic-gateaskconfirmation=yes
887c478bd9Sstevel@tonic-gateinitstate=s
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gatewhile getopts g:i:y? c
917c478bd9Sstevel@tonic-gatedo
927c478bd9Sstevel@tonic-gate	case $c in
937c478bd9Sstevel@tonic-gate	g)
947c478bd9Sstevel@tonic-gate		case $OPTARG in
957c478bd9Sstevel@tonic-gate		*[!0-9]* )
967c478bd9Sstevel@tonic-gate			echo "$0: -g requires a numeric option"
977c478bd9Sstevel@tonic-gate			usage
987c478bd9Sstevel@tonic-gate			;;
997c478bd9Sstevel@tonic-gate		[0-9]* )
1007c478bd9Sstevel@tonic-gate			grace=$OPTARG
1017c478bd9Sstevel@tonic-gate			;;
1027c478bd9Sstevel@tonic-gate		esac
1037c478bd9Sstevel@tonic-gate		;;
1047c478bd9Sstevel@tonic-gate	i)
1057c478bd9Sstevel@tonic-gate		case $OPTARG in
1067c478bd9Sstevel@tonic-gate		[Ss0156])
1077c478bd9Sstevel@tonic-gate			initstate=$OPTARG
1087c478bd9Sstevel@tonic-gate			;;
1097c478bd9Sstevel@tonic-gate		[234abcqQ])
1107c478bd9Sstevel@tonic-gate			echo "$0: Initstate $OPTARG is not for system shutdown"
1117c478bd9Sstevel@tonic-gate			exit 1
1127c478bd9Sstevel@tonic-gate			;;
1137c478bd9Sstevel@tonic-gate		*)
1147c478bd9Sstevel@tonic-gate			echo "$0: $OPTARG is not a valid initstate"
1157c478bd9Sstevel@tonic-gate			usage
1167c478bd9Sstevel@tonic-gate			;;
1177c478bd9Sstevel@tonic-gate		esac
1187c478bd9Sstevel@tonic-gate		;;
1197c478bd9Sstevel@tonic-gate	y)
1207c478bd9Sstevel@tonic-gate		askconfirmation=
1217c478bd9Sstevel@tonic-gate		;;
1227c478bd9Sstevel@tonic-gate	\?)	usage
1237c478bd9Sstevel@tonic-gate		;;
1247c478bd9Sstevel@tonic-gate	esac
1257c478bd9Sstevel@tonic-gatedone
1263bf13137SDan McDonaldshift $(($OPTIND - 1))
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gateecho '\nShutdown started.    \c'
1297c478bd9Sstevel@tonic-gate/usr/bin/date
1307c478bd9Sstevel@tonic-gateecho
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gateNODENAME=`uname -n`
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gatecd /
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gatetrap "rm $nologin >/dev/null 2>&1 ;exit 1"  1 2 15
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate# If other users are on the system (and any grace period is given), warn them.
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gatefor i in 7200 3600 1800 1200 600 300 120 60 30 10; do
1417c478bd9Sstevel@tonic-gate	if [ ${grace} -gt $i ]
1427c478bd9Sstevel@tonic-gate	then
1433bf13137SDan McDonald		hours=$((${grace} / 3600))
1443bf13137SDan McDonald		minutes=$((${grace} % 3600 / 60))
1453bf13137SDan McDonald		seconds=$((${grace} % 60))
1467c478bd9Sstevel@tonic-gate		time=""
1477c478bd9Sstevel@tonic-gate		if [ ${hours} -gt 1 ]
1487c478bd9Sstevel@tonic-gate		then
1497c478bd9Sstevel@tonic-gate			time="${hours} hours "
1507c478bd9Sstevel@tonic-gate		elif [ ${hours} -eq 1 ]
1517c478bd9Sstevel@tonic-gate		then
1527c478bd9Sstevel@tonic-gate			time="1 hour "
1537c478bd9Sstevel@tonic-gate		fi
1547c478bd9Sstevel@tonic-gate		if [ ${minutes} -gt 1 ]
1557c478bd9Sstevel@tonic-gate		then
1567c478bd9Sstevel@tonic-gate			time="${time}${minutes} minutes "
1577c478bd9Sstevel@tonic-gate		elif [ ${minutes} -eq 1 ]
1587c478bd9Sstevel@tonic-gate		then
1597c478bd9Sstevel@tonic-gate			time="${time}1 minute "
1607c478bd9Sstevel@tonic-gate		fi
1617c478bd9Sstevel@tonic-gate		if [ ${hours} -eq 0 -a ${seconds} -gt 0 ]
1627c478bd9Sstevel@tonic-gate		then
1637c478bd9Sstevel@tonic-gate			if [ ${seconds} -eq 1 ]
1647c478bd9Sstevel@tonic-gate			then
1657c478bd9Sstevel@tonic-gate				time="${time}${seconds} second"
1667c478bd9Sstevel@tonic-gate			else
1677c478bd9Sstevel@tonic-gate				time="${time}${seconds} seconds"
1687c478bd9Sstevel@tonic-gate			fi
1697c478bd9Sstevel@tonic-gate		fi
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate		(notify \
1727c478bd9Sstevel@tonic-gate"The system ${NODENAME} will be shut down in ${time}
1733bf13137SDan McDonald$*")
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate		rm $nologin >/dev/null 2>&1
1767c478bd9Sstevel@tonic-gate		cat > $nologin <<-!
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate			NO LOGINS: System going down in ${time}
1797c478bd9Sstevel@tonic-gate			$*
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate		!
1827c478bd9Sstevel@tonic-gate
1833bf13137SDan McDonald		/usr/bin/sleep $((${grace} - $i))
1847c478bd9Sstevel@tonic-gate		grace=$i
1857c478bd9Sstevel@tonic-gate	fi
1867c478bd9Sstevel@tonic-gatedone
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate# Confirm that we really want to shutdown.
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gateif [ ${askconfirmation} ]
1917c478bd9Sstevel@tonic-gatethen
1927c478bd9Sstevel@tonic-gate	echo "Do you want to continue? (y or n):   \c"
1937c478bd9Sstevel@tonic-gate	read b
1947c478bd9Sstevel@tonic-gate	if [ "$b" != "y" ]
1957c478bd9Sstevel@tonic-gate	then
1967c478bd9Sstevel@tonic-gate		notify "False Alarm:  The system ${NODENAME} will not be brought down."
1977c478bd9Sstevel@tonic-gate		echo 'Shutdown aborted.'
1987c478bd9Sstevel@tonic-gate		rm $nologin >/dev/null 2>&1
1997c478bd9Sstevel@tonic-gate		exit 1
2007c478bd9Sstevel@tonic-gate	fi
2017c478bd9Sstevel@tonic-gatefi
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate# Final shutdown message, and sleep away the final 10 seconds (or less).
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate(notify \
2067c478bd9Sstevel@tonic-gate"THE SYSTEM ${NODENAME} IS BEING SHUT DOWN NOW ! ! !
2077c478bd9Sstevel@tonic-gateLog off now or risk your files being damaged
2083bf13137SDan McDonald$*")
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gateif [ ${grace} -gt 0 ]
2117c478bd9Sstevel@tonic-gatethen
2127c478bd9Sstevel@tonic-gate	/usr/bin/sleep ${grace}
2137c478bd9Sstevel@tonic-gatefi
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate# Go to the requested initstate.
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gateecho "Changing to init state $initstate - please wait"
2197c478bd9Sstevel@tonic-gate
2203bf13137SDan McDonald# We might be racing with a system that's still booting.
221*bbf21555SRichard Lowe# Before starting init, check to see if smf(7) is running.  The easiest way
2223bf13137SDan McDonald# to do this is to check for the existence of the repository service door.
2233bf13137SDan McDonald
2243bf13137SDan McDonaldi=0
2253bf13137SDan McDonald# Try three times, sleeping one second each time...
2263bf13137SDan McDonaldwhile [ ! -e /etc/svc/volatile/repository_door -a $i -lt 3 ]; do
2273bf13137SDan McDonald	sleep 1
2283bf13137SDan McDonald	i=$(($i + 1))
2293bf13137SDan McDonalddone
2303bf13137SDan McDonald
2313bf13137SDan McDonaldif [ ! -e /etc/svc/volatile/repository_door ]; then
2323bf13137SDan McDonald	notify "Could not find repository door, init-state change may fail!"
2337c478bd9Sstevel@tonic-gatefi
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate/sbin/init ${initstate}
236