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