aboutsummaryrefslogtreecommitdiffstats
path: root/rc.d/init.d/halt
blob: 82ffe34e09ef676420adc98c4e01634763a79fb8 (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
#!/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
#

# Set the path.
PATH=/sbin:/bin:/usr/bin:/usr/sbin

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

runcmd() {
   echo -n $"$1 "
   shift
   if [ "$BOOTUP" = "color" ]; then
      $* && echo_success || echo_failure
   else
      $*
   fi
   echo
}

halt_get_remaining() {
	awk '!/(^#|proc|loopfs|autofs|devfs|^none|^\/dev\/root| \/ )/ {print $2}' /proc/mounts
	awk '{ if ($3 ~ /^proc$/ && $2 !~ /^\/proc/) print $2; }' /proc/mounts
}

# 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"
	;;
   *)
	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

runcmd $"Sending all processes the TERM signal..." /sbin/killall5 -15
sleep 5
runcmd $"Sending all processes the KILL signal..."  /sbin/killall5 -9

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

# Save mixer settings, here for lack of a better place.
grep -q "\(sparcaudio\|sound\)" /proc/devices
if [ $? = 0 -a -x /bin/aumix-minimal ]; then
   runcmd $"Saving mixer settings" /bin/aumix-minimal -f /etc/.aumixrc -S
fi

# Sync the system clock.
ARC=0
SRM=0
UTC=0

if [ -f /etc/sysconfig/clock ]; then
   . /etc/sysconfig/clock

   # convert old style clock config to new values
   if [ "${CLOCKMODE}" = "GMT" ]; then
      UTC=true
   elif [ "${CLOCKMODE}" = "ARC" ]; then
      ARC=true
   fi
fi

CLOCKDEF=""
CLOCKFLAGS="$CLOCKFLAGS --systohc"

case "$UTC" in
   yes|true)
	CLOCKFLAGS="$CLOCKFLAGS -u";
	CLOCKDEF="$CLOCKDEF (utc)";
	;;
   no|false)
	CLOCKFLAGS="$CLOCKFLAGS --localtime";
	CLOCKDEF="$CLOCKDEF (localtime)";
	;;
esac

case "$ARC" in
   yes|true)
	CLOCKFLAGS="$CLOCKFLAGS -A";
	CLOCKDEF="$CLOCKDEF (arc)";
	;;
esac
case "$SRM" in
   yes|true)
	CLOCKFLAGS="$CLOCKFLAGS -S";
	CLOCKDEF="$CLOCKDEF (srm)";
	;;
esac

runcmd $"Syncing hardware clock to system time" /sbin/hwclock $CLOCKFLAGS

# Turn off swap, then unmount file systems.
SWAPS=`awk '! /^Filename/ { print $1 }' /proc/swaps`
[ -n "$SWAPS" ] && runcmd $"Turning off swap: " swapoff $SWAPS

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

# Unmount file systems, killing processes if we have to.
# Unmount loopback stuff first
remaining=`awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts`
devremaining=`awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts`
[ -n "$remaining" ] && {
	sig=
	retry=3
	while [ -n "$remaining" -a "$retry" -gt 0 ]
	do
		if [ "$retry" -lt 3 ]; then
			runcmd $"Unmounting loopback filesystems (retry):" umount $remaining
		else
			runcmd $"Unmounting loopback filesystems: " umount $remaining
		fi
		for dev in $devremaining ; do
			losetup $dev > /dev/null 2>&1 && \
				runcmd $"Detaching loopback device $dev: " losetup -d $dev
		done
		remaining=`awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts`
		devremaining=`awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts`
		[ -z "$remaining" ] && break
		/sbin/fuser -k -m $sig $remaining >/dev/null
		sleep 5
		retry=$(($retry -1))
		sig=-9
	done
}

sig=
retry=3
remaining=`halt_get_remaining | sort -r`

while [ -n "$remaining" -a "$retry" -gt 0 ]
do
	if [ "$retry" -lt 3 ]; then
		LANG=C runcmd $"Unmounting file systems (retry): "  umount -f $remaining
	else
		LANG=C runcmd $"Unmounting file systems: "  umount -f $remaining
	fi
	sleep 2
	remaining=`halt_get_remaining | sort -r`
	[ -z "$remaining" ] && break
	/sbin/fuser -k -m $sig $remaining >/dev/null
	sleep 5
	retry=$(($retry-1))
	sig=-9
done
[ -f /proc/bus/usb/devices ] && umount /proc/bus/usb

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

# Try them all, one last time.
umount -a -f

# Remount read only anything that's left mounted.
#echo $"Remounting remaining filesystems (if any) readonly"
mount | awk '/( \/ |^\/dev\/root)/ { print $3 }' | while read line; do
    mount -n -o ro,remount $line
done

# 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

HALTARGS="-i -d"
if [ -f /poweroff -o ! -f /halt ]; then
 HALTARGS="$HALTARGS -p"
fi

if [ "$command" = /sbin/halt -a -r /etc/ups/upsmon.conf -a -f /etc/killpower -a -f /etc/sysconfig/ups ] ; then
	. /etc/sysconfig/ups
	[ "$SERVER" = "yes" -a "$MODEL" != "NONE" -a -n "$MODEL" -a -n "$DEVICE" ] && $MODEL -k $DEVICE
fi

if [ -x /sbin/halt.local ]; then
   /sbin/halt.local
fi

exec $command $HALTARGS