1#!/bin/sh
2#
3# $NetBSD: rc.shutdown,v 1.10 2024/12/07 18:45:20 martin Exp $
4#
5# rc.shutdown --
6#         Run the scripts in /etc/rc.d with reverse rcorder.
7
8#         System shutdown script run by shutdown(8) at system shutdown time.
9#         Note that halt(8) and reboot(8) do NOT invoke this script.
10
11export HOME=/
12export PATH=/sbin:/bin:/usr/sbin:/usr/bin
13
14. /etc/rc.subr
15. /etc/rc.conf
16
17if ! checkyesno do_rcshutdown; then
18          echo "Skipping shutdown hooks."
19          exit 0
20fi
21
22_rcshutdown_action="$1"
23set --
24
25stty status '^T'
26
27#         Set shell to ignore SIGINT, but not children;
28#         shell catches SIGQUIT and returns to single user.
29#
30trap : INT
31trap "echo 'Shutdown interrupted.'; exit 1" QUIT
32
33#         If requested, start a watchdog timer in the background which
34#         will terminate rc.shutdown if rc.shutdown doesn't complete
35#         within the specified time.
36#
37_rcshutdown_watchdog=
38if [ -n "$rcshutdown_timeout" ]; then
39          sleep $rcshutdown_timeout && (
40              _msg="$rcshutdown_timeout second watchdog timeout expired. Shutdown terminated."
41              logger -t rc.shutdown "$_msg"
42              echo "$_msg"
43              date
44              kill -KILL $$ >/dev/null 2>&1
45              ) &
46          _rcshutdown_watchdog=$!
47fi
48
49
50#         Determine the shutdown order of the rc.d scripts,
51#         and perform the operation
52#
53scripts=$(for rcd in ${rc_directories:-/etc/rc.d}; do
54          test -d ${rcd} && echo ${rcd}/*; done)
55files=$(rcorder -k shutdown ${rcshutdown_rcorder_flags} ${scripts})
56
57for _rc_elem in $(reverse_list $files); do
58          run_rc_script $_rc_elem stop
59done
60
61#
62#         Run final local handlers (if any exist)
63#
64if [ -r /etc/rc.shutdown.final ]; then
65          set -- "$_rcshutdown_action"
66          . /etc/rc.shutdown.final
67fi
68
69
70#         Terminate the background watchdog timer (if it is running)
71#
72if [ -n "$_rcshutdown_watchdog" ]; then
73          kill -TERM $_rcshutdown_watchdog >/dev/null 2>&1
74fi
75
76date
77exit 0
78