aboutsummaryrefslogtreecommitdiffstats
path: root/rc.d/init.d/halt
blob: 3ddaa73d4cda91dac52fe9834f682e4cd3b4b8f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
#
# halt          This file is executed by init when it goes into runlevel
#               0 (halt) or runlevel 6 (reboot). It kills all processes,
#               unmounts file systems and then either halts or reboots.
#
# Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#               Modified for RHS Linux by Damien Neil
#

NOLOCALE=1
. /etc/init.d/functions

UMOUNT="umount"
[ ! -w /etc ] && UMOUNT="umount -n"

halt_crypto() {
    fnval=0
    while read dst src key; do
	[ -z "$dst" -o "${dst#\#}" != "$dst" ] && continue
        if [ -b "/dev/mapper/$dst" ]; then
            if /sbin/dmsetup info "$dst" | grep -q '^Open count: *0$'; then
                action $"Stopping disk encryption for $dst" /sbin/cryptsetup remove "$dst"
            else
                fnval=1
            fi
        fi
    done < /etc/crypttab
    return $fnval
}

kill_all() {
    local STRING rc

    STRING=$1
    echo -n "$STRING "
    shift
    /sbin/killall5 "$@"
    rc=$?
    # Retval: 0: success 1: error 2: no processes found to kill
    if [ "$rc" == 1 ]; then
        failure $"$STRING"
    else
        success $"$STRING"
    fi
    echo
    return $rc
}

# See how we were called.
case "$0" in
   *halt)
	message=$"Halting system..."
	command="/sbin/halt"
	;;
   *reboot)
	message=$"Please stand by while rebooting the system..."
	command="/sbin/reboot"
	kexec_command="/sbin/kexec"
	;;
   *)
	echo $"$0: call me as 'halt' or 'reboot' please!"
	exit 1
	;;
esac
case "$1" in
   *start)
   	;;
   *)
	echo $"Usage: $0 {start}"
	exit 1
	;;
esac

# Kill all processes.
[ "${BASH+bash}" = bash ] && enable kill

# Find mdmon pid's and omit killing them
OMITARGS=
for i in /dev/md/*.pid; do
    if [ ! -r "$i" ]; then
        continue
    fi
    OMITARGS="$OMITARGS -o $(cat $i)"
done

kill_all $"Sending all processes the TERM signal..." -15 $OMITARGS
# No need to sleep and kill -9 if no processes to kill were found
if [ "$?" == 0 ]; then
    sleep 2
    kill_all $"Sending all processes the KILL signal..." -9 $OMITARGS
fi

# Write to wtmp file before unmounting /var
/sbin/halt -w

# Save mixer settings, here for lack of a better place.
if [ -s /etc/alsa/alsactl.conf -a -x /sbin/alsactl -a -w /etc/asound.state ]; then
  action $"Saving mixer settings" /sbin/alsactl -E ALSA_CONFIG_PATH=/etc/alsa/alsactl.conf --ignore store
fi

# Save random seed
touch /var/lib/random-seed
chmod 600 /var/lib/random-seed
action $"Saving random seed: " dd if=/dev/urandom of=/var/lib/random-seed count=1 bs=512 2>/dev/null

[ -x /sbin/hwclock -a -e /dev/rtc ] && action $"Syncing hardware clock to system time" /sbin/hwclock --systohc

# Try to unmount tmpfs filesystems to avoid swapping them in.  Ignore failures.
tmpfs=$(awk '$2 ~ /^\/($|proc|dev)/ { next; }
	     $3 == "tmpfs" { print $2; }' /proc/mounts | sort -r)
[ -n "$tmpfs" ] && fstab-decode $UMOUNT $tmpfs 2>/dev/null

# Turn off swap, then unmount file systems.
[ -f /proc/swaps ] && SWAPS=$(awk '! /^Filename/ { print $1 }' /proc/swaps)
if [ -n "$SWAPS" ]; then
    action $"Turning off swap: " swapoff $SWAPS
    for dst in $SWAPS; do
	if [[ "$dst" == /dev/mapper* ]] \
	    && [ "$(dmsetup status "$dst" | cut -d ' ' -f 3)" = crypt ]; then
	    backdev=$(/sbin/cryptsetup status "$dst" \
		| awk '$1 == "device:" { print $2 }')
	    /sbin/cryptsetup remove "$dst"
	fi
    done
fi

[ -x /sbin/quotaoff ] && action $"Turning off quotas: " /sbin/quotaoff -aug

# Unmount file systems, killing processes if we have to.
# Unmount loopback stuff first
__umount_loopback_loop

# Unmount RPC pipe file systems
__umount_loop '$3 ~ /^rpc_pipefs$/ || $3 ~ /^rpc_svc_gss_pipefs$/ {print $2}' \
    /proc/mounts \
    $"Unmounting pipe file systems: " \
    $"Unmounting pipe file systems (retry): " \
    -f

LANG=C __umount_loop '$2 ~ /^\/$|^\/proc|^\/dev/{next}
	$3 == "tmpfs" || $3 == "proc" {print $2 ; next}
	$3 ~ /(loopfs|autofs|nfs|cifs|smbfs|ncpfs|sysfs)/ {next}
	/(^none|^\/dev\/ram|^\/dev\/root$)/ {next}
	{print $2}' /proc/mounts \
	$"Unmounting file systems: " \
	$"Unmounting file systems (retry): " \
	-f

[ -f /proc/bus/usb/devices ] && $UMOUNT /proc/bus/usb

[ -f /etc/crypttab ] && halt_crypto

# remove the crash indicator flag
rm -f /.autofsck

# Try all file systems other than root, essential filesystems and RAM disks,
# one last time.
awk '$2 !~ /\/(|dev|proc|selinux|sys)$/ && $1 !~ /^\/dev\/ram/ { print $2 }' \
    /proc/mounts | sort -r | \
  while read line; do
    fstab-decode $UMOUNT -f $line
done

HALTARGS="-d"
[ "$INIT_HALT" != "HALT" ] && HALTARGS="$HALTARGS -p"

if [ -x /sbin/halt.local ]; then
   /sbin/halt.local "$message" "$command" "$HALTARGS" "$kexec_command"
fi

# Tell init to re-exec itself.
kill -TERM 1

# Remount read only anything that's left mounted.
# echo $"Remounting remaining filesystems readonly"
mount | awk '{ print $1,$3 }' | while read dev dir; do
    fstab-decode mount -n -o ro,remount $dev $dir
done

# If we left mdmon's running wait for the raidsets to become clean
if [ -n "$OMITARGS" ]; then
    mdadm --wait-clean --scan
fi

# Now halt or reboot.
echo $"$message"
if [ -f /fastboot ]; then
 echo $"On the next boot fsck will be skipped."
elif [ -f /forcefsck ]; then
 echo $"On the next boot fsck will be forced."
fi

# Shutdown UPS drivers
if [ "$command" = /sbin/halt -a -f /etc/sysconfig/ups ]; then
	. /etc/sysconfig/ups
	if [ -z $POWERDOWNFLAG ]; then
		POWERDOWNFLAG=/etc/killpower
	fi
	if [ "$SERVER" = "yes" -a -f $POWERDOWNFLAG ]; then
		/sbin/upsdrvctl shutdown
	fi
fi

# Turn off UPS in powerfail situation
if [ -x /etc/apcupsd/apccontrol -a -f /etc/apcupsd/powerfail ]; then 
	/etc/apcupsd/apccontrol killpower
fi 

# First, try kexec. If that fails, fall back to rebooting the old way.
[ -n "$kexec_command" ] && $kexec_command -e -x >& /dev/null


exec $command $HALTARGS