rc.shutdown revision 187685
1118611Snjl#!/bin/sh
2118611Snjl#
3118611Snjl# Copyright (c) 1997  Ollivier Robert
4151937Sjkim# All rights reserved.
5118611Snjl#
6118611Snjl# Redistribution and use in source and binary forms, with or without
7118611Snjl# modification, are permitted provided that the following conditions
8217365Sjkim# are met:
9229989Sjkim# 1. Redistributions of source code must retain the above copyright
10118611Snjl#    notice, this list of conditions and the following disclaimer.
11118611Snjl# 2. Redistributions in binary form must reproduce the above copyright
12217365Sjkim#    notice, this list of conditions and the following disclaimer in the
13217365Sjkim#    documentation and/or other materials provided with the distribution.
14217365Sjkim#
15217365Sjkim# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16217365Sjkim# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17217365Sjkim# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18217365Sjkim# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19217365Sjkim# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20217365Sjkim# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21217365Sjkim# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22217365Sjkim# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23217365Sjkim# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24217365Sjkim# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25217365Sjkim# SUCH DAMAGE.
26118611Snjl#
27217365Sjkim# $FreeBSD: head/etc/rc.shutdown 187685 2009-01-25 10:31:45Z bz $
28217365Sjkim#
29217365Sjkim
30118611Snjl# Site-specific closing actions for daemons run by init on shutdown,
31217365Sjkim# or before going single-user from multi-user.
32217365Sjkim# Output and errors are directed to console by init, and the
33217365Sjkim# console is the controlling terminal.
34217365Sjkim
35217365Sjkimstty status '^T'
36217365Sjkim
37217365Sjkim# Set shell to ignore SIGINT (2), but not children;
38217365Sjkim# shell catches SIGQUIT (3) and returns to single user after fsck.
39217365Sjkimtrap : 2
40217365Sjkimtrap : 3	# shouldn't be needed
41217365Sjkim
42217365SjkimHOME=/
43217365SjkimPATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
44118611Snjlexport HOME PATH
45118611Snjl
46118611Snjl. /etc/rc.subr
47118611Snjl
48118611Snjlload_rc_config 'XXX'
49217365Sjkim
50217365Sjkim# reverse_list list
51217365Sjkim#	print the list in reverse order
52217365Sjkim#
53118611Snjlreverse_list()
54118611Snjl{
55118611Snjl	_revlist=
56118611Snjl	for _revfile in $*; do
57118611Snjl		_revlist="$_revfile${script_name_sep}$_revlist"
58118611Snjl	done
59118611Snjl	echo $_revlist
60118611Snjl}
61118611Snjl
62118611Snjl# If requested, start a watchdog timer in the background which
63118611Snjl# will terminate rc.shutdown if rc.shutdown doesn't complete
64118611Snjl# within the specified time.
65118611Snjl#
66118611Snjl_rcshutdown_watchdog=
67118611Snjlif [ -n "$rcshutdown_timeout" ]; then
68118611Snjl	debug "Initiating watchdog timer."
69118611Snjl	sleep $rcshutdown_timeout && (
70118611Snjl		_msg="$rcshutdown_timeout second watchdog"
71118611Snjl		_msg="$_msg timeout expired. Shutdown terminated."
72151937Sjkim		logger -t rc.shutdown "$_msg"
73118611Snjl		echo "$_msg"
74151937Sjkim		date
75151937Sjkim		kill -KILL $$ >/dev/null 2>&1
76213806Sjkim	) &
77151937Sjkim	_rcshutdown_watchdog=$!
78233250Sjkimfi
79118611Snjl
80118611Snjl# Determine the shutdown order of the /etc/rc.d scripts,
81118611Snjl# and perform the operation
82118611Snjl#
83118611Snjlrcorder_opts="-k shutdown"
84118611Snjl[ `/sbin/sysctl -n security.jail.jailed` -eq 1 ] && rcorder_opts="$rcorder_opts -s nojail"
85118611Snjl
86118611Snjlcase ${local_startup} in
87151937Sjkim[Nn][Oo] | '') ;;
88233250Sjkim*)     find_local_scripts_new ;;
89151937Sjkimesac
90118611Snjl
91118611Snjlfiles=`rcorder ${rcorder_opts} /etc/rc.d/* ${local_rc} 2>/dev/null`
92118611Snjl
93118611Snjlfor _rc_elem in `reverse_list $files`; do
94118611Snjl	debug "run_rc_script $_rc_elem faststop"
95118611Snjl	run_rc_script $_rc_elem faststop
96118611Snjldone
97118611Snjl
98118611Snjl# Terminate the background watchdog timer (if it is running)
99118611Snjl#
100118611Snjlif [ -n "$_rcshutdown_watchdog" ]; then
101118611Snjl	pkill -TERM -P $_rcshutdown_watchdog >/dev/null 2>&1
102118611Snjlfi
103234623Sjkim
104118611Snjl# Insert other shutdown procedures here
105118611Snjl
106118611Snjl
107234623Sjkimecho '.'
108118611Snjlexit 0
109118611Snjl