Denys Vlasenko | c21dfaf | 2018-04-20 15:12:52 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | PATH=/sbin:/usr/sbin:/bin:/usr/bin |
| 4 | |
| 5 | # Usually, /sbin/ has symlinks named halt, reboot, poweroff |
| 6 | # (and also possibly shutdown) to e.g. |
| 7 | # /app/shutdown-1.0/script/shutdown (this file). |
| 8 | cd /app/shutdown-1.0/script || exit 1 |
| 9 | test -x ./do_shutdown || exit 1 |
| 10 | test -x ./hardshutdown || exit 1 |
| 11 | |
| 12 | # "reboot -f" -> "shutdown -f -r" -> "hardshutdown -r" -> immediate reboot |
| 13 | # "reboot" -> "shutdown -r" -> "do_shutdown -r" |
| 14 | # ^^^^^^^^^^^^^^^^^^ similarly for halt, poweroff. |
| 15 | # "shutdown" -> "do_shutdown" (everything killed/unmounted, but kernel not asked to do any poweroff etc) |
| 16 | force="" |
| 17 | test x"$1" = x"-f" && { |
| 18 | force="-f" |
| 19 | shift |
| 20 | } |
| 21 | test ! "$*" && test x"${0##*/}" = x"halt" && exec "$0" $force -h |
| 22 | test ! "$*" && test x"${0##*/}" = x"reboot" && exec "$0" $force -r |
| 23 | test ! "$*" && test x"${0##*/}" = x"poweroff" && exec "$0" $force -p |
| 24 | # We have something else than allowed parameters? |
| 25 | test x"$*" = x"" || test x"$*" = x"-h" || test x"$*" = x"-r" || test x"$*" = x"-p" || { |
| 26 | echo "Syntax: $0 [-f] [-h/-r/-p]" |
| 27 | exit 1 |
| 28 | } |
| 29 | |
| 30 | # Emergency shutdown? |
| 31 | test "$force" && { |
| 32 | exec ./hardshutdown "$@" |
| 33 | exit 1 |
| 34 | } |
| 35 | |
| 36 | # Normal shutdown |
| 37 | |
| 38 | # We must have these executables on root fs |
| 39 | # (mount/umount aren't checked, all systems are ok versus that): |
| 40 | test -x /bin/killall5 -o -x /sbin/killall5 || exit 1 |
| 41 | test -x /bin/ps -o -x /sbin/ps || exit 1 |
| 42 | test -x /bin/date -o -x /sbin/date || exit 1 |
| 43 | test -x /bin/xargs -o -x /sbin/xargs || exit 1 |
| 44 | test -x /bin/wc -o -x /sbin/wc || exit 1 |
| 45 | test -x /bin/cat -o -x /sbin/cat || exit 1 |
| 46 | test -x /bin/sort -o -x /sbin/sort || exit 1 |
| 47 | |
| 48 | i="`ulimit -n`" |
| 49 | echo -n "Closing file descriptors $i-3... " |
| 50 | while test "$i" -ge 3; do |
| 51 | eval "exec $i>&-" |
| 52 | i=$((i-1)) |
| 53 | done |
| 54 | |
| 55 | echo "Shutting down. Please stand by..." |
| 56 | |
| 57 | # setsid & /dev/null: |
| 58 | # make it a process leader & detach it from current tty. |
| 59 | # Why /dev/null and not /dev/console? |
| 60 | # I have seen a system which locked up while opening /dev/console |
| 61 | # due to the bug (?) in keyboard driver. |
| 62 | setsid env - PATH="$PATH" ./do_shutdown "$@" </dev/null >/dev/null 2>&1 & |
| 63 | |
| 64 | while true; do read junk; done |