]> pd.if.org Git - startuptools/blob - scripts/rc.shutdown
make rc.shutdown actually shutdown
[startuptools] / scripts / rc.shutdown
1 #!/bin/sh
2
3 cd /
4
5 . /etc/rc.conf
6 . /etc/rc.d/functions.rc
7
8 kill_all_wait() {
9         # Send SIGTERM then SIGKILL to all processes
10         # loop until killall5 reports all done, or timeout
11         # killall5 does not support the 0 signal, so we use SIGCONT for
12         # checking (which should be ignored)
13
14         local i
15
16         killall5 -$1
17
18         for i in $(seq 1 $2); do
19                 sleep .25
20                 # check via SIGCONT
21                 killall5 -18
22                 test $? -eq 2 && return 0
23         done
24
25         return 1
26 }
27
28 kill_all() {
29         status_begin "Sending SIGTERM to processes"
30         kill_all_wait 15 40
31         status_check
32         if [ $? != 0 ]; then
33                 status_begin "Sending SIGKILL to processes"
34                 kill_all_wait 9 60
35                 status_check
36         fi
37 }
38
39 text -x /etc/rc.local.shutdown && /etc/rc.local.shutdown
40
41 reverse() {
42         local rev
43         for i in "$@"; do
44                 rev="$i $rev"
45         done;
46         printf '%s\n' "$rev"
47 }
48
49 STOPDAEMONS=$(reverse $DAEMONS)
50
51 for D in $STOPDAEMONS; do
52         /etc/rc.d/$D stop
53 done
54
55 # write to wtmp
56 halt -w
57
58 if [ is_true "$USELVM" ] && [ -x /sbin/vgchange ]; then
59         status_begin "Deactivating monitoring of LVM2 groups"
60         /sbin/vgchange --monitor n
61         status_check
62 fi
63
64 if [ -x /sbin/udevadm ]; then
65         status_begin "Shutting down udev"
66         /sbin/udevadm control --exit
67         status_check
68 fi
69
70 kill_all
71
72 status_begin "Unmounting swap-backed filesystems"
73 umount -a -t tmpfs
74 status_check
75
76 status_begin "Deactivating swap"
77 swapoff -a
78 status_check
79
80 status_begin "Unmounting non-API filesystems"
81 umount -a
82 status_check
83
84 status_begin "Remounting root filesystem read-only"
85 mount -o remount,ro /
86 status_check
87
88 sleep 3
89
90 if [ $RUNLEVEL -eq 0 ]; then
91         poweroff -d -f -h -i
92 else
93         reboot -d -f -i
94 fi