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 11 export HOME=/ 12 export PATH=/sbin:/bin:/usr/sbin:/usr/bin 13 14 . /etc/rc.subr 15 . /etc/rc.conf 16 17 if ! checkyesno do_rcshutdown; then 18 echo "Skipping shutdown hooks." 19 exit 0 20 fi 21 22 _rcshutdown_action="$1" 23 set -- 24 25 stty status '^T' 26 27 # Set shell to ignore SIGINT, but not children; 28 # shell catches SIGQUIT and returns to single user. 29 # 30 trap : INT 31 trap "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= 38 if [ -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=$! 47 fi 48 49 50 # Determine the shutdown order of the rc.d scripts, 51 # and perform the operation 52 # 53 scripts=$(for rcd in ${rc_directories:-/etc/rc.d}; do 54 test -d ${rcd} && echo ${rcd}/*; done) 55 files=$(rcorder -k shutdown ${rcshutdown_rcorder_flags} ${scripts}) 56 57 for _rc_elem in $(reverse_list $files); do 58 run_rc_script $_rc_elem stop 59 done 60 61 # 62 # Run final local handlers (if any exist) 63 # 64 if [ -r /etc/rc.shutdown.final ]; then 65 set -- "$_rcshutdown_action" 66 . /etc/rc.shutdown.final 67 fi 68 69 70 # Terminate the background watchdog timer (if it is running) 71 # 72 if [ -n "$_rcshutdown_watchdog" ]; then 73 kill -TERM $_rcshutdown_watchdog >/dev/null 2>&1 74 fi 75 76 date 77 exit 0 78