#!/bin/sh
-# TODO stop all the daemons
+cd /
-reboot -f -i
+. /etc/rc.conf
+. /etc/rc.d/functions.rc
+
+kill_all_wait() {
+ # Send SIGTERM then SIGKILL to all processes
+ # loop until killall5 reports all done, or timeout
+ # killall5 does not support the 0 signal, so we use SIGCONT for
+ # checking (which should be ignored)
+
+ local i
+
+ killall5 -$1
+
+ for i in $(seq 1 $2); do
+ sleep .25
+ # check via SIGCONT
+ killall5 -18
+ test $? -eq 2 && return 0
+ done
+
+ return 1
+}
+
+kill_all() {
+ status_begin "Sending SIGTERM to processes"
+ kill_all_wait 15 40
+ status_check
+ if [ $? != 0 ]; then
+ status_begin "Sending SIGKILL to processes"
+ kill_all_wait 9 60
+ status_check
+ fi
+}
+
+text -x /etc/rc.local.shutdown && /etc/rc.local.shutdown
+
+reverse() {
+ local rev
+ for i in "$@"; do
+ rev="$i $rev"
+ done;
+ printf '%s\n' "$rev"
+}
+
+STOPDAEMONS=$(reverse $DAEMONS)
+
+for D in $STOPDAEMONS; do
+ /etc/rc.d/$D stop
+done
+
+# write to wtmp
+halt -w
+
+if [ is_true "$USELVM" ] && [ -x /sbin/vgchange ]; then
+ status_begin "Deactivating monitoring of LVM2 groups"
+ /sbin/vgchange --monitor n
+ status_check
+fi
+
+if [ -x /sbin/udevadm ]; then
+ status_begin "Shutting down udev"
+ /sbin/udevadm control --exit
+ status_check
+fi
+
+kill_all
+
+status_begin "Unmounting swap-backed filesystems"
+umount -a -t tmpfs
+status_check
+
+status_begin "Deactivating swap"
+swapoff -a
+status_check
+
+status_begin "Unmounting non-API filesystems"
+umount -a
+status_check
+
+status_begin "Remounting root filesystem read-only"
+mount -o remount,ro /
+status_check
+
+sleep 3
+
+if [ $RUNLEVEL -eq 0 ]; then
+ poweroff -d -f -h -i
+else
+ reboot -d -f -i
+fi