summaryrefslogtreecommitdiff
path: root/init.d/halt.sh.in
blob: ab73d8df7b291ba3847c0d014d5529460d1301b1 (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
#!@SHELL@
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.

. @SYSCONFDIR@/init.d/functions.sh
. "${RC_LIBDIR}"/sh/rc-functions.sh
[ -r @SYSCONFDIR@/conf.d/localmount ] && . @SYSCONFDIR@/conf.d/localmount
[ -r @SYSCONFDIR@/rc.conf ] && . @SYSCONFDIR@/rc.conf

# Really kill things off before unmounting
if [ -x /sbin/killall5 ]; then
	killall5 -15
	killall5 -9
fi

# Flush all pending disk writes now
sync; sync

# If we are in a VPS, we don't need anything below here, because
#   1) we don't need (and by default can't) umount anything (VServer) or
#   2) the host utils take care of all umounting stuff (OpenVZ)
if [ "${RC_SYS}" = "VSERVER" -o "${RC_SYS}" = "OPENVZ" ]; then
	[ "${RC_SYS}" = "OPENVZ" -a "$1" = "reboot" ] && echo "" > /reboot
	if [ -e @SYSCONFDIR@/init.d/"$1".sh ]; then
		. @SYSCONFDIR@/init.d/"$1".sh
	else
		exit 0
	fi
fi

# If $svcdir is still mounted, preserve it if we can
mnt=$(mountinfo --node "${RC_SVCDIR}")
if [ -n "${mnt}" ] && \
	rm -rf "${RC_LIBDIR}/tmp.$$" && \
	mkdir -p "${RC_LIBDIR}/tmp.$$" 2>/dev/null \
; then
	rmdir "${RC_LIBDIR}/tmp.$$"
	f_opts="-m -c"
	[ "${RC_UNAME}" = "Linux" ] && f_opts="-c"
	if type fuser >/dev/null 2>&1; then
		if [ -n "$(fuser ${f_opts} "${svcdir}" 2>/dev/null)" ]; then
			fuser -k ${f_opts} "${svcdir}" >/dev/null 2>&1
			sleep 2
		fi
	fi
	cp -p "${RC_SVCDIR}"/deptree "${RC_SVCDIR}"/depconfig \
		"${RC_SVCDIR}"/softlevel "${RC_SVCDIR}"/nettree \
		"${RC_SVCDIR}"/rc.log \
		"${RC_LIBDIR}" 2>/dev/null
	umount "${RC_SVCDIR}"
	rm -rf "${RC_SVCDIR}"/*
	# Pipe errors to /dev/null as we may have future timestamps
	cp -p "${RC_LIBDIR}"/deptree "${RC_LIBDIR}"/depconfig \
		"${RC_LIBDIR}"/softlevel "${RC_LIBDIR}"/nettree \
		"${RC_LIBDIR}"/rc.log \
		"${RC_SVCDIR}" 2>/dev/null
	rm -f "${RC_LIBDIR}"/deptree "${RC_LIBDIR}"/depconfig \
		"${RC_LIBDIR}"/softlevel "${RC_LIBDIR}"/nettree \
		"${RC_LIBDIR}"/rc.log
	# Release the memory disk if we used it
	case "${mnt}" in
		"/dev/md"[0-9]*) mdconfig -d -u "${mnt#/dev/md*}";;
	esac
fi

unmounted=0
# Remount the remaining filesystems read-only
# Most BSD's don't need this as the kernel handles it nicely
if [ "${RC_UNAME}" = "Linux" ]; then
	ebegin "Remounting remaining filesystems read-only"
	# We need the do_unmount function
	. "${RC_LIBDIR}"/sh/rc-mount.sh
	eindent
	no_umounts_r="/dev|/dev/.*|${RC_SVCDIR}"
	# RC_NO_UMOUNTS is an env var that can be set by plugins
	OIFS=${IFS} SIFS=${IFS-y}
	IFS=$IFS:
	for x in ${no_umounts} ${RC_NO_UMOUNTS}; do
		no_umounts_r="${no_umounts_r}|${x}"
	done
	if [ "${SIFS}" = "y" ]; then
		IFS=$OIFS
	else
		unset IFS
	fi
	no_umounts_r="${no_umounts_r}|/proc|/proc/.*|/sys|/sys/.*"
	no_umounts_r="^(${no_umounts_r})$"
	fs=
	for x in ${net_fs_list}; do
		fs="${fs}${fs:+|}${x}"
	done
	[ -n "${fs}" ] && fs="^(${fs})$"
	do_unmount "mount -n -o remount,ro" \
		--skip-point-regex "${no_umounts_r}" \
		${fs:+--skip-fstype-regex} ${fs} --nonetdev
	eoutdent
	eend $?
	unmounted=$?
fi

if [ ${unmounted} -ne 0 ]; then
	if [ -x /sbin/sulogin ]; then
		ewarn "$1 timeout in 30 seconds"
		sulogin -t 30 /dev/console
	fi
fi

# Load the final script - not needed on BSD so they should not exist
[ -e @SYSCONFDIR@/init.d/"$1".sh ] && . @SYSCONFDIR@/init.d/"$1".sh

# Always exit 0 here
exit 0