summaryrefslogtreecommitdiff
path: root/examples/shutdown-1.0/script/shutdown
blob: dbab9d81ed0153646ecab2f1d2701ca63ff99169 (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
#!/bin/sh

PATH=/sbin:/usr/sbin:/bin:/usr/bin

# Usually, /sbin/ has symlinks named halt, reboot, poweroff
# (and also possibly shutdown) to e.g.
# /app/shutdown-1.0/script/shutdown (this file).
cd /app/shutdown-1.0/script || exit 1
test -x ./do_shutdown || exit 1
test -x ./hardshutdown || exit 1

# "reboot -f" -> "shutdown -f -r" -> "hardshutdown -r" -> immediate reboot
# "reboot" -> "shutdown -r" -> "do_shutdown -r"
# ^^^^^^^^^^^^^^^^^^ similarly for halt, poweroff.
# "shutdown" -> "do_shutdown" (everything killed/unmounted, but kernel not asked to do any poweroff etc)
force=""
test x"$1" = x"-f" && {
	force="-f"
	shift
}
test ! "$*" && test x"${0##*/}" = x"halt" && exec "$0" $force -h
test ! "$*" && test x"${0##*/}" = x"reboot" && exec "$0" $force -r
test ! "$*" && test x"${0##*/}" = x"poweroff" && exec "$0" $force -p
# We have something else than allowed parameters?
test x"$*" = x"" || test x"$*" = x"-h" || test x"$*" = x"-r" || test x"$*" = x"-p" || {
	echo "Syntax: $0 [-f] [-h/-r/-p]"
	exit 1
}

# Emergency shutdown?
test "$force" && {
	exec ./hardshutdown "$@"
	exit 1
}

# Normal shutdown

# We must have these executables on root fs
# (mount/umount aren't checked, all systems are ok versus that):
test -x /bin/killall5 -o -x /sbin/killall5 || exit 1
test -x /bin/ps       -o -x /sbin/ps       || exit 1
test -x /bin/date     -o -x /sbin/date     || exit 1
test -x /bin/xargs    -o -x /sbin/xargs    || exit 1
test -x /bin/wc       -o -x /sbin/wc       || exit 1
test -x /bin/cat      -o -x /sbin/cat      || exit 1
test -x /bin/sort     -o -x /sbin/sort     || exit 1

i="`ulimit -n`"
echo -n "Closing file descriptors $i-3... "
while test "$i" -ge 3; do
	eval "exec $i>&-"
	i=$((i-1))
done

echo "Shutting down. Please stand by..."

# setsid & /dev/null:
# make it a process leader & detach it from current tty.
# Why /dev/null and not /dev/console?
# I have seen a system which locked up while opening /dev/console
# due to the bug (?) in keyboard driver.
setsid env - PATH="$PATH" ./do_shutdown "$@" </dev/null >/dev/null 2>&1 &

while true; do read junk; done