xref: /netbsd-src/etc/rc.shutdown (revision 9077f083444fdbbae7d0fbb469fc63ed93b08324)
1c5955775Slukem#!/bin/sh
2c5955775Slukem#
3*9077f083Smartin# $NetBSD: rc.shutdown,v 1.10 2024/12/07 18:45:20 martin Exp $
4c5955775Slukem#
56eef11f2Slukem# rc.shutdown --
6c5955775Slukem#	Run the scripts in /etc/rc.d with reverse rcorder.
796cda5feSbad
86eef11f2Slukem#	System shutdown script run by shutdown(8) at system shutdown time.
96eef11f2Slukem#	Note that halt(8) and reboot(8) do NOT invoke this script.
106eef11f2Slukem
1196cda5feSbadexport HOME=/
12c5955775Slukemexport PATH=/sbin:/bin:/usr/sbin:/usr/bin
1396cda5feSbad
1496cda5feSbad. /etc/rc.subr
1596cda5feSbad. /etc/rc.conf
1696cda5feSbad
17c5955775Slukemif ! checkyesno do_rcshutdown; then
1896cda5feSbad	echo "Skipping shutdown hooks."
1996cda5feSbad	exit 0
2096cda5feSbadfi
2196cda5feSbad
22*9077f083Smartin_rcshutdown_action="$1"
23*9077f083Smartinset --
24*9077f083Smartin
25c5955775Slukemstty status '^T'
2696cda5feSbad
27586521f8Slukem#	Set shell to ignore SIGINT, but not children;
28586521f8Slukem#	shell catches SIGQUIT and returns to single user.
29c5955775Slukem#
30586521f8Slukemtrap : INT
31586521f8Slukemtrap "echo 'Shutdown interrupted.'; exit 1" QUIT
32c5955775Slukem
33586521f8Slukem#	If requested, start a watchdog timer in the background which
34586521f8Slukem#	will terminate rc.shutdown if rc.shutdown doesn't complete
35586521f8Slukem#	within the specified time.
36586521f8Slukem#
37586521f8Slukem_rcshutdown_watchdog=
38586521f8Slukemif [ -n "$rcshutdown_timeout" ]; then
39586521f8Slukem	sleep $rcshutdown_timeout && (
40586521f8Slukem	    _msg="$rcshutdown_timeout second watchdog timeout expired. Shutdown terminated."
41586521f8Slukem	    logger -t rc.shutdown "$_msg"
42586521f8Slukem	    echo "$_msg"
43586521f8Slukem	    date
44586521f8Slukem	    kill -KILL $$ >/dev/null 2>&1
45586521f8Slukem	    ) &
46586521f8Slukem	_rcshutdown_watchdog=$!
47586521f8Slukemfi
48586521f8Slukem
49586521f8Slukem
5055657a27Sjnemeth#	Determine the shutdown order of the rc.d scripts,
51586521f8Slukem#	and perform the operation
52586521f8Slukem#
5355657a27Sjnemethscripts=$(for rcd in ${rc_directories:-/etc/rc.d}; do
5455657a27Sjnemeth	test -d ${rcd} && echo ${rcd}/*; done)
5555657a27Sjnemethfiles=$(rcorder -k shutdown ${rcshutdown_rcorder_flags} ${scripts})
5696cda5feSbad
5796e2ff62Slukemfor _rc_elem in $(reverse_list $files); do
586a4937a4Slukem	run_rc_script $_rc_elem stop
59c5955775Slukemdone
60c5955775Slukem
61*9077f083Smartin#
62*9077f083Smartin#	Run final local handlers (if any exist)
63*9077f083Smartin#
64*9077f083Smartinif [ -r /etc/rc.shutdown.final ]; then
65*9077f083Smartin	set -- "$_rcshutdown_action"
66*9077f083Smartin	. /etc/rc.shutdown.final
67*9077f083Smartinfi
68*9077f083Smartin
69586521f8Slukem
70586521f8Slukem#	Terminate the background watchdog timer (if it is running)
71586521f8Slukem#
72586521f8Slukemif [ -n "$_rcshutdown_watchdog" ]; then
73586521f8Slukem	kill -TERM $_rcshutdown_watchdog >/dev/null 2>&1
74586521f8Slukemfi
75586521f8Slukem
76c5955775Slukemdate
7796cda5feSbadexit 0
78