aboutsummaryrefslogtreecommitdiffstats
path: root/rc.d/init.d/halt
blob: 095b0e743be4d64ac5009d5073266af3a3bc2cb9 (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
#!/bin/bash
#
# rc.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

. /etc/init.d/functions

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

# See how we were called.
case "$0" in
  *halt)
	message=`gprintf "The system is halted"`
	command="halt"
	;;
  *reboot)
	message=`gprintf "Please stand by while rebooting the system..."`
	command="reboot"
	;;
  *)
	gprintf "%s: call me as 'halt' or 'reboot' please!" $0
	exit 1
	;;
esac

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

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

# Write to wtmp file before unmounting /var
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 clock
runcmd "Syncing hardware clock to system time" /sbin/hwclock --systohc

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

[ -x /sbin/accton ] && runcmd "Turning off accounting: " /sbin/accton

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

# Unmount file systems, killing processes if we have to.
sig=
retry=3
remaining=`awk '!/(^#|proc|loopfs|^none|^\/dev\/root| \/ )/ {print $2}' /proc/mounts`
while [ -n "$remaining" -a "$retry" -gt 0 ]
do
	if [ "$retry" -lt 3 ]; then
		runcmd "Unmounting file systems (retry): "  umount -a -f -t noproc
	else
		runcmd "Unmounting file systems: "  umount -a -f -t noproc
	fi
	sleep 2
	remaining=`awk '!/(^#|proc|loopfs|^none|^\/dev\/root| \/ )/ {print $2}' /proc/mounts`
	[ -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

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

runcmd "Unmounting proc file system: " umount /proc

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

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

eval $command $HALTARGS