11.3Slukem#!/bin/sh
21.3Slukem#
31.10Smartin# $NetBSD: rc.shutdown,v 1.10 2024/12/07 18:45:20 martin Exp $
41.3Slukem#
51.5Slukem# rc.shutdown --
61.3Slukem#	Run the scripts in /etc/rc.d with reverse rcorder.
71.5Slukem
81.5Slukem#	System shutdown script run by shutdown(8) at system shutdown time.
91.5Slukem#	Note that halt(8) and reboot(8) do NOT invoke this script.
101.1Sbad
111.1Sbadexport HOME=/
121.3Slukemexport PATH=/sbin:/bin:/usr/sbin:/usr/bin
131.1Sbad
141.3Slukem. /etc/rc.subr
151.3Slukem. /etc/rc.conf
161.1Sbad
171.3Slukemif ! checkyesno do_rcshutdown; then
181.1Sbad	echo "Skipping shutdown hooks."
191.1Sbad	exit 0
201.1Sbadfi
211.1Sbad
221.10Smartin_rcshutdown_action="$1"
231.10Smartinset --
241.10Smartin
251.3Slukemstty status '^T'
261.1Sbad
271.6Slukem#	Set shell to ignore SIGINT, but not children;
281.6Slukem#	shell catches SIGQUIT and returns to single user.
291.3Slukem#
301.6Slukemtrap : INT
311.6Slukemtrap "echo 'Shutdown interrupted.'; exit 1" QUIT
321.3Slukem
331.6Slukem#	If requested, start a watchdog timer in the background which
341.6Slukem#	will terminate rc.shutdown if rc.shutdown doesn't complete
351.6Slukem#	within the specified time.
361.6Slukem#
371.6Slukem_rcshutdown_watchdog=
381.6Slukemif [ -n "$rcshutdown_timeout" ]; then
391.6Slukem	sleep $rcshutdown_timeout && (
401.6Slukem	    _msg="$rcshutdown_timeout second watchdog timeout expired. Shutdown terminated."
411.6Slukem	    logger -t rc.shutdown "$_msg"
421.6Slukem	    echo "$_msg"
431.6Slukem	    date
441.6Slukem	    kill -KILL $$ >/dev/null 2>&1
451.6Slukem	    ) &
461.6Slukem	_rcshutdown_watchdog=$!
471.6Slukemfi
481.6Slukem
491.6Slukem
501.9Sjnemeth#	Determine the shutdown order of the rc.d scripts,
511.6Slukem#	and perform the operation
521.6Slukem#
531.9Sjnemethscripts=$(for rcd in ${rc_directories:-/etc/rc.d}; do
541.9Sjnemeth	test -d ${rcd} && echo ${rcd}/*; done)
551.9Sjnemethfiles=$(rcorder -k shutdown ${rcshutdown_rcorder_flags} ${scripts})
561.3Slukem
571.8Slukemfor _rc_elem in $(reverse_list $files); do
581.7Slukem	run_rc_script $_rc_elem stop
591.3Slukemdone
601.6Slukem
611.10Smartin#
621.10Smartin#	Run final local handlers (if any exist)
631.10Smartin#
641.10Smartinif [ -r /etc/rc.shutdown.final ]; then
651.10Smartin	set -- "$_rcshutdown_action"
661.10Smartin	. /etc/rc.shutdown.final
671.10Smartinfi
681.10Smartin
691.6Slukem
701.6Slukem#	Terminate the background watchdog timer (if it is running)
711.6Slukem#
721.6Slukemif [ -n "$_rcshutdown_watchdog" ]; then
731.6Slukem	kill -TERM $_rcshutdown_watchdog >/dev/null 2>&1
741.6Slukemfi
751.1Sbad
761.3Slukemdate
771.1Sbadexit 0
78